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

Hist1D.cpp

Go to the documentation of this file.
00001 
00002 #include "cafe/Hist1D.hpp"
00003 #include "cafe/Event.hpp"
00004 #include "cafe/Config.hpp"
00005 #include "cafe/HistoGetter.hpp"
00006 
00007 #include <cstdlib>
00008 #include <stdexcept>
00009 
00010 #include "TH1F.h"
00011 #include "TFile.h"
00012 #include "TTreeFormula.h"
00013 #include "TTreeFormulaManager.h"
00014 
00015 namespace cafe {
00016     
00017     Hist1D::Hist1D(const char *name)
00018         : Processor(name),
00019           _title(name),
00020           _num_bins(100),
00021           _bin_min(0.0),
00022           _bin_max(100.0),
00023           _select(0),
00024           _draw(0),
00025           _mgr(0),
00026           _getter(0),
00027           _branch(""),
00028           _method("")
00029     {
00030         using namespace std;
00031 
00032         // from configuration get:
00033         // _expr, _title, _bin_min, _bin_max, _num_bins
00034         //
00035         Config config(name);
00036         _title    = config.get("Title", _title);
00037         _weight   = config.get("Weight", "");
00038 
00039         std::vector<std::string> bins = config.getVString("Bins", " ,");
00040 
00041         if(bins.size() != 3) {
00042             err() << "Hist1D[" << name << "] : Expected three parameters for .Bins, got: " << config.get("Bins", "") << std::endl;
00043             throw std::runtime_error("Hist1D: wrong number of Bins parameters");
00044         } else {
00045             _num_bins = strtol(bins[0].c_str(), 0, 0);
00046             _bin_min  = strtod(bins[1].c_str(), 0);
00047             _bin_max  = strtod(bins[2].c_str(), 0);
00048         }
00049     }
00050 
00051     Hist1D::~Hist1D()
00052     {
00053         delete _select;
00054         delete _draw;
00055     }
00056 
00057     void Hist1D::begin()
00058     {
00059 
00060         using namespace std;
00061 
00062         Config config(name());
00063 
00064         std::string sel  = config.get("Select", "1");
00065         std::string expr = config.get("Draw", "");
00066 
00067         sel = replace(sel);
00068         if(sel == "") {
00069             err() << fullName() << " : replacement failed for " << config.get("Select", "") << endl;
00070         }
00071 
00072         _select = new Formula(this);
00073         _select->setFormula(sel);
00074 
00075         expr = replace(expr);
00076         if(expr == "") {
00077             err() << fullName() << " : replacement failed for " << config.get("Draw", "") << endl;
00078         }
00079 
00080         _draw = new Formula(this);
00081         _draw->setFormula(expr);
00082 
00083         if(sel == "1") {
00084             HistoGetter::parse(expr, _branch, _method);
00085         }
00086 
00087         //        _title    = config.get("Title", expr);
00088 
00089         if ( _title == name() )
00090           _title = expr;
00091         else
00092           _title = replace(_title);
00093           
00094         if( _title == "")
00095             err() << fullName() << " : replacement failed for " << config.get("Title", "") << endl;
00096 
00097         if(TDirectory *dir = getDirectory()) {
00098             dir->cd();
00099             _hist = new TH1F(name().c_str(), _title.c_str(), _num_bins, _bin_min, _bin_max);
00100             out() << "Hist1D: Title: \"" << _title << "\" Bins: " << _num_bins << " Min: " << _bin_min << " Max: " << _bin_max << std::endl;
00101         } else {
00102             err() << fullName() << ": No valid directory" << std::endl;
00103         }
00104 
00105     }
00106 
00107     void Hist1D::inputFileOpened(TFile *file)
00108     {
00109         // otherwise check if we can optimize
00110         if(_getter == 0 && !_branch.empty()) {
00111             if(TTree *tree = dynamic_cast<TTree*>(file->Get("TMBTree"))) {
00112                 _getter = HistoGetter::create(tree, _branch, _method);
00113                 if(_getter == 0) _branch = ""; 
00114             }
00115         }
00116 
00117         _select->inputFileOpened(file);
00118         _draw->inputFileOpened(file);
00119 
00120         if(_mgr == 0) {
00121             _mgr = new TTreeFormulaManager();
00122             _mgr->Add(_select->getFormula());
00123             _mgr->Add(_draw->getFormula());
00124             _mgr->Sync();
00125         }
00126         _mgr->UpdateFormulaLeaves();
00127     }
00128 
00129     bool Hist1D::processEvent(Event& event)
00130     {
00131         Float_t weight = 1.0;
00132         if(!_weight.empty()) {
00133             event.get(_weight, weight);
00134         }
00135 
00136         TTreeFormula *sel  = _select->getFormula();
00137         TTreeFormula *draw = _draw->getFormula();
00138 
00139         const TClonesArray *values = 0;
00140         if(_getter && (values = event.getClonesArray(_branch, _getter->vars()))) {
00141             for(int i = 0; i < values->GetLast() + 1; i++) {
00142                 _hist->Fill(_getter->get(values->At(i)), weight);
00143             }
00144         } else {
00145             
00146             int ndata = _mgr->GetNdata();
00147             for(int i = 0; i < ndata; i++) {
00148                 if(sel->EvalInstance(i)) {
00149                     _hist->Fill(draw->EvalInstance(i), weight);
00150                 }
00151             } 
00152         }
00153         return true;
00154     }
00155     
00156 }
00157 
00158 ClassImp(cafe::Hist1D)
00159 

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