00001 #include "SpectrumMaker.hh"
00002 #include "ProcessedPlotter.hh"
00003 #include "SumChannels.hh"
00004 #include "RootWriter.hh"
00005 #include "EventHandler.hh"
00006
00007 #include "Integrator.hh"
00008 #include "EvalRois.hh"
00009
00010 #include "TFile.h"
00011 #include "TCanvas.h"
00012 #include "TH1D.h"
00013 #include "TH2D.h"
00014 #include "TTree.h"
00015 #include "TBranch.h"
00016 #include "TTreeFormula.h"
00017 #include "TClass.h"
00018 #include "TList.h"
00019 #include "TClassMenuItem.h"
00020
00021 SpectrumMaker::SpectrumMaker(const std::string& name) :
00022 BaseModule(name, "Histogram variables from data") ,
00023 _histo(0), _canvas(0), _tree(0), _branch(0), _xform(0), _yform(0), _cutform(0)
00024 {
00025 RegisterParameter("xvar", _xvar = "", "Variable to plot on x axis");
00026 RegisterParameter("nbinsx",_nbinsx = 200, "Number of bins in the x axis");
00027 RegisterParameter("xmin", _xmin = 0, "Minimum x range of the histogram");
00028 RegisterParameter("xmax", _xmax = 0, "Maximum x range of the histogram");
00029
00030 RegisterParameter("yvar", _yvar = "", "Variable to plot on y axis");
00031 RegisterParameter("nbinsy",_nbinsy = 200, "Number of bins in the y axis");
00032 RegisterParameter("ymin", _ymin = 0, "Minimum y range of the histogram");
00033 RegisterParameter("ymax", _ymax = 0, "Maximum y range of the histogram");
00034
00035 RegisterParameter("cut", _cut = "", "Cut to test before drawing");
00036 RegisterParameter("title", _title="", "Title for the histogram");
00037 RegisterParameter("xtitle", _xtitle="", "Title for the x axis");
00038 RegisterParameter("ytitle", _ytitle="", "Title of the yaxis");
00039 RegisterParameter("logx", _logx = false, "Plot on logarythmic x axis?");
00040 RegisterParameter("logy", _logy = false, "Plot on logarythmic y axis?");
00041
00042 }
00043
00044 SpectrumMaker::~SpectrumMaker()
00045 {
00046 Finalize();
00047 }
00048
00049
00050 int SpectrumMaker::Initialize()
00051 {
00052 if(_xvar == "" || _nbinsx == 0 || _xmin == _xmax ||
00053 (_yvar != "" && (_nbinsy==0 || _ymin == _ymax)) ){
00054
00055 Message(ERROR)<<"Incorrect axis or variable specifications for "
00056 <<GetName()<<"; disabling.\n";
00057 return 1;
00058 }
00059
00060 RootGraphix* graphix = EventHandler::GetInstance()->GetModule<RootGraphix>();
00061
00062 if(_yvar == ""){
00063 _histo = new TH1D(GetName().c_str(), _title.c_str(),
00064 _nbinsx, _xmin, _xmax);
00065 _histo->SetFillColor(kGreen);
00066 }
00067 else
00068 _histo = new TH2D(GetName().c_str(), _title.c_str(),
00069 _nbinsx, _xmin, _xmax,
00070 _nbinsy, _ymin, _ymax);
00071 _histo->SetXTitle(_xtitle.c_str());
00072 _histo->SetYTitle(_ytitle.c_str());
00073
00074
00075 if(std::string(_histo->Class()->GetMenuList()->First()->GetTitle()) !=
00076 "Reset"){
00077 TClass* cl = _histo->Class();
00078 cl->GetMenuList()
00079 ->AddFirst(new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
00080 "Reset","Reset",0,"",-1,1));
00081 }
00082
00083 if(graphix && graphix->enabled){
00084 _canvas = graphix->GetCanvas(GetName().c_str());
00085 _canvas->SetLogy(_logy);
00086 _canvas->SetLogx(_logx);
00087 _canvas->cd();
00088 _histo->Draw( _yvar.empty() ? "" : "colz");
00089 }
00090
00091 _tree = new TTree((GetName()+"tree").c_str(),"");
00092 EventData* ptr = new EventData;
00093 _branch = _tree->Branch(EventData::GetBranchName(),&ptr);
00094 _tree->SetEntries(1);
00095 delete ptr;
00096
00097 _xform = new TTreeFormula((GetName()+"xform").c_str(),_xvar.c_str(),_tree);
00098 _xform->SetQuickLoad(true);
00099 if(!_yvar.empty()){
00100 _yform = new TTreeFormula((GetName()+"yform").c_str(),_yvar.c_str(),_tree);
00101 _yform->SetQuickLoad(true);
00102 }
00103
00104 if(!_cut.empty()){
00105 _cutform = new TTreeFormula((GetName()+"cutform").c_str(),_cut.c_str(),
00106 _tree);
00107 _cutform->SetQuickLoad(true);
00108 }
00109
00110
00111
00112 _draw_cmd = _yvar;
00113 if(!_yvar.empty())
00114 _draw_cmd += " : ";
00115 _draw_cmd += _xvar;
00116
00117 _draw_cmd += " >>+";
00118 _draw_cmd += _histo->GetName();
00119 return 0;
00120 }
00121
00122 int SpectrumMaker::Finalize()
00123 {
00124 if( gFile && gFile->IsOpen() && _histo)
00125 _histo->Write();
00126 if(_histo) delete _histo;
00127 _histo = 0;
00128
00129 _canvas = 0;
00130 delete _tree;
00131 _tree = 0;
00132 _branch = 0;
00133 delete _xform;
00134 _xform = 0;
00135 delete _yform;
00136 _yform = 0;
00137 delete _cutform;
00138 _cutform = 0;
00139 return 0;
00140 }
00141
00142 int SpectrumMaker::Process(EventPtr evt)
00143 {
00144 EventDataPtr data = evt->GetEventData();
00145 EventData* ptr = data.get();
00146 _branch->SetAddress( &ptr );
00147
00148 for(int i=0; i<_xform->GetNdata(); ++i){
00149 if(_cutform){
00150 if(_cutform->GetNdata() <= i)
00151 break;
00152 if(_cutform->EvalInstance(i)==0)
00153 continue;
00154 }
00155 double xval = _xform->EvalInstance(i);
00156 if(_yform){
00157 if(_yform->GetNdata() <= i)
00158 break;
00159 double yval = _yform->EvalInstance(i);
00160 ((TH2D*)(_histo))->Fill(xval,yval);
00161 }
00162 else
00163 _histo->Fill(xval);
00164 }
00165
00166
00167 if(_canvas)
00168 _canvas->Modified();
00169 return 0;
00170 }