Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

Hist3D.cpp

Go to the documentation of this file.
00001 
00002 #include "cafe/Hist3D.hpp"
00003 #include "cafe/Event.hpp"
00004 #include "cafe/Config.hpp"
00005 #include "cafe/Formula.hpp"
00006 
00007 #include <stdexcept>
00008 #include <cstdlib>
00009 
00010 #include "TH3F.h"
00011 #include "TFile.h"
00012 #include "TTreeFormula.h"
00013 #include "TTreeFormulaManager.h"
00014 
00015 namespace cafe {
00016     
00017     Hist3D::Hist3D(const char *name)
00018         : Processor(name),
00019           _title(name),
00020           _num_binsX(100),
00021           _bin_minX(0.0),
00022           _bin_maxX(100.0),
00023           _num_binsY(100),
00024           _bin_minY(0.0),
00025           _bin_maxY(100.0),
00026           _num_binsZ(100),
00027           _bin_minZ(0.0),
00028           _bin_maxZ(100.0),
00029           _select(0),
00030           _draw1(0),
00031           _draw2(0),
00032           _draw3(0),
00033           _mgr(0)
00034     {
00035         using namespace std;
00036 
00037         // from configuration get:
00038         // _expr, _title, _binX_min, _bin_max, _num_bins
00039         // _draw1, _draw2
00040         //
00041         Config config(name);
00042         _title    = config.get("Title", _title);
00043         _weight   = config.get("Weight", "");
00044 
00045         vector<string> bins = config.getVString("Bins", " ,");
00046 
00047         if(bins.size() != 9) {
00048             err() << "Hist3D[" << name << "] : Expected nine parameters for .Bins, got: " << config.get("Bins", "") << endl;
00049             throw std::runtime_error("Hist3D: wrong number of Bins parameters");
00050         } else {
00051             _num_binsX = strtol(bins[0].c_str(), 0, 0);
00052             _bin_minX  = strtod(bins[1].c_str(), 0);
00053             _bin_maxX  = strtod(bins[2].c_str(), 0);
00054             _num_binsY = strtol(bins[3].c_str(), 0, 0);
00055             _bin_minY  = strtod(bins[4].c_str(), 0);
00056             _bin_maxY  = strtod(bins[5].c_str(), 0);
00057             _num_binsZ = strtol(bins[6].c_str(), 0, 0);
00058             _bin_minZ  = strtod(bins[7].c_str(), 0);
00059             _bin_maxZ  = strtod(bins[8].c_str(), 0);
00060         }
00061 
00062         string sel  = config.get("Select", "1");
00063 
00064         vector<string> draws = config.getVString("Draw", ":");
00065 
00066         string draw1("0");
00067         string draw2("0");
00068         string draw3("0");
00069 
00070         if(draws.size() != 3) {
00071             err() << "Hist3D[" << name << "]: Expected three dimensions in Draw, got: " 
00072                   << config.get("Draw","") << endl;
00073             throw std::runtime_error("Hist3D: wrong number of Draw dimensions");
00074         } else {
00075             draw1 = replace(draws[0]);
00076             draw2 = replace(draws[1]);
00077             draw3 = replace(draws[2]);
00078             out() << "Hist3D[" << name << "]: Plotting " << draw1 
00079                   << " : " << draw2 
00080                   << " : " << draw3 << endl;
00081         }
00082         _select = new Formula(this);
00083         _select->setFormula(sel);
00084         
00085         _draw1 = new Formula(this);
00086         _draw1->setFormula(draw1);
00087         
00088         _draw2 = new Formula(this);
00089         _draw2->setFormula(draw2);
00090         
00091         _draw3 = new Formula(this);
00092         _draw3->setFormula(draw3);
00093 
00094     }
00095 
00096     Hist3D::~Hist3D()
00097     {
00098         delete _select;
00099         delete _draw1;
00100         delete _draw2;
00101         delete _draw3;
00102         // delete _mgr;
00103     }
00104 
00105     void Hist3D::begin()
00106     {
00107         if(TDirectory *dir = getDirectory()) {
00108             dir->cd();
00109             _hist = new TH3F(name().c_str(), _title.c_str(), 
00110                              _num_binsX, _bin_minX, _bin_maxX,
00111                              _num_binsY, _bin_minY, _bin_maxY,
00112                              _num_binsZ, _bin_minZ, _bin_maxZ
00113                              );
00114             out() << "Hist3D: Title: \"" << _title << "\""
00115                   << " BinsX: " << _num_binsX << " MinX: " << _bin_minX << " MaxX: " << _bin_maxX
00116                   << " BinsY: " << _num_binsY << " MinY: " << _bin_minY << " MaxY: " << _bin_maxY
00117                   << " BinsZ: " << _num_binsZ << " MinZ: " << _bin_minZ << " MaxZ: " << _bin_maxZ
00118                   << std::endl;
00119         } else {
00120             err() << fullName() << ": No valid directory" << std::endl;
00121         }
00122 
00123     }
00124 
00125     void Hist3D::inputFileOpened(TFile *file)
00126     {
00127         _select->inputFileOpened(file);
00128         _draw1->inputFileOpened(file);
00129         _draw2->inputFileOpened(file);
00130         _draw3->inputFileOpened(file);
00131         if(_mgr == 0) {
00132             _mgr = new TTreeFormulaManager();
00133             _mgr->Add(_select->getFormula());
00134             _mgr->Add(_draw1->getFormula());
00135             _mgr->Add(_draw2->getFormula());        
00136             _mgr->Add(_draw3->getFormula());        
00137             _mgr->Sync();
00138         }
00139         _mgr->UpdateFormulaLeaves();
00140     }
00141 
00142     bool Hist3D::processEvent(Event& event)
00143     {
00144         Float_t weight = 1.0;
00145         if(!_weight.empty()) {
00146             event.get(_weight, weight);
00147         }
00148 
00149         TTreeFormula *sel  = _select->getFormula();
00150         TTreeFormula *draw1 = _draw1->getFormula();
00151         TTreeFormula *draw2 = _draw2->getFormula();
00152         TTreeFormula *draw3 = _draw3->getFormula();
00153 
00154         int ndata = _mgr->GetNdata();
00155         for(int i = 0; i < ndata; i++) {
00156             if(sel->EvalInstance(i)) {
00157                 _hist->Fill(draw1->EvalInstance(i), draw2->EvalInstance(i), draw3->EvalInstance(i), weight);
00158             }
00159         } 
00160         return true;
00161     }
00162     
00163 }
00164 
00165 ClassImp(cafe::Hist3D)
00166 

Generated on Tue Mar 28 10:13:04 2006 for CAF by doxygen 1.3.4