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

Hist2D.cpp

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

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