ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.11
Committed: Mon Dec 7 14:02:01 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.10: +70 -8 lines
Log Message:
enabled auto plot of variables

File Contents

# User Rev Content
1 tschum 1.1 #include "PlotTool.h"
2     //------------------------------------------------------------------
3     //Constructur: Set some global Variables to define canvas output
4 gebbert 1.6 PlotTool::PlotTool() {
5 tschum 1.1
6 gebbert 1.6 this->SetClass("TChain", 100);
7 tschum 1.1
8 gebbert 1.6 samePad_trees = true;
9     samePad_vars = false;
10     samePad_cuts = false;
11     sameCanv_trees = false;
12     sameCanv_vars = false;
13     sameCanv_cuts = false;
14 tschum 1.2
15 gebbert 1.6 showLegend = false;
16     logY = true;
17 tschum 1.11 addTrackJets = false;
18 tschum 1.8 verbose = true;
19 tschum 1.1
20     }
21    
22     //------------------------------------------------------------------
23     //Fill object PlotTool with Chains constructed from files from given source
24 tschum 1.7
25 gebbert 1.6 int PlotTool::init(string fileName, string dirPath, string treeName,
26     string fileLabel) {
27     this->New(this->GetEntries() );
28     int currChain = this->GetEntries() - 1;
29    
30     if (currChain < 0)
31     return currChain;
32    
33 tschum 1.8 TStopwatch timer;
34     timer.Start();
35    
36    
37 gebbert 1.6 fileNames.clear();
38    
39     //check if alternative label is different from default(searchString)
40     if (fileLabel=="") {
41     fileLabel=fileName;
42     }
43     ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
44 tschum 1.10
45     TList *files = new TList();
46     TSystemFile* sysFile = 0;
47     if(fileName.find(".") != string::npos) {
48     ifstream f(fileName.c_str());
49     if( ! f.is_open() ) return -1;
50     string line;
51    
52     while (!f.eof()) {
53     getline(f,line);
54     sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
55     files->Add(sysFile);
56     }
57    
58    
59     } else {
60 gebbert 1.6 TSystemDirectory dir("sourceDir", dirPath.c_str());
61 tschum 1.10 files = dir.GetListOfFiles();
62     }
63     if (files->GetEntries()>0) {
64 gebbert 1.6 TIter next(files);
65     TSystemFile *file;
66     TString fname;
67     string filePath;
68    
69 tschum 1.10 if(verbose && fileName.find(".") == string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
70 gebbert 1.6 <<fileName.c_str()<<endl;
71 tschum 1.10 if(verbose && fileName.find(".") != string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
72     <<fileName.c_str()<<endl;
73    
74 gebbert 1.6
75     while ((file=(TSystemFile*)next())) {
76     fname = file->GetName();
77     if (!fname.EndsWith(".root"))
78     continue;
79 tschum 1.10 if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
80 gebbert 1.6 continue;
81    
82     filePath = dirPath;
83     filePath += fname.Data();
84     //fwlite::ChainEvent to lop over events, jets, etc
85     fileNames.push_back(filePath.c_str());
86    
87     if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
88     treeName.c_str()) )
89 tschum 1.8 if(verbose) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
90 gebbert 1.6 else
91     return -1;
92    
93     }
94     } else
95     return -1;
96    
97     for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
98    
99     string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
100 tschum 1.7
101     string branch_alias = s;
102 gebbert 1.6 string branch_name = s;
103     if (s.find(".", s.size()-1) != string::npos)
104     branch_name += "obj";
105 tschum 1.7
106     size_t a = s.find("_");
107     if (a != string::npos) {
108     size_t b = s.find("_", a+1);
109     if (b != string::npos) {
110     size_t c = s.find("_", b+1);
111     if (c != string::npos) {
112 tschum 1.9 string _prod =s.substr(0,a);
113 tschum 1.7 string _label =s.substr(a+1, b-a-1);
114     string _instance =s.substr(b+1, c-b-1);
115     branch_alias = _label;
116     if(_instance.length() > 0 ) {
117     branch_alias += "_";
118     branch_alias += _instance;
119 tschum 1.9 ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
120     branch_name.c_str());
121 tschum 1.7 }
122 tschum 1.9 string branch_alias_full = _prod + "_" + branch_alias;
123     ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
124     branch_name.c_str());
125 tschum 1.7 }
126     }
127     }
128    
129 gebbert 1.6 ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
130     branch_name.c_str());
131    
132     }
133    
134     // add branch with track Jets
135 thomsen 1.5
136 tschum 1.7 if( addTrackJets ) {
137 gebbert 1.6
138 tschum 1.7 //make file for tree friends (adding additional, computed branches)
139 tschum 1.8 string friendFileName("/scratch/hh/current/cms/user/");
140     friendFileName += gSystem->GetUserInfo()->fUser;
141     friendFileName += "/temp/",
142 tschum 1.7 friendFileName += fileName;
143     friendFileName += ".root";
144     TFile *f = new TFile(friendFileName.c_str(),"recreate");
145 tschum 1.8
146     if( f->IsZombie() ) return -1;
147    
148 tschum 1.7 string friendTreeName("friendTree");
149     //char number = currChain;
150     //friendTreeName += number;
151     //TTree *friendTree = new TTree("friendTree","friendTree");
152     TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
153    
154     int nJetsKT;
155     TrackJetKT = new float [100];
156     fwlite::ChainEvent ev(fileNames);
157    
158     friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
159     friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
160    
161 tschum 1.8 if(verbose) cout<<"calculating additional variables..."<<endl;
162    
163    
164    
165     int tenth = ev.size() / 10;
166     int eventNum =0;
167     for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
168 tschum 1.7
169     fwlite::Handle<reco::CaloJetCollection> jets;
170     jets.getByLabel(ev, "antikt5CaloJets");
171    
172     fwlite::Handle<reco::TrackCollection> tracks;
173     tracks.getByLabel(ev, "generalTracks");
174    
175     if (!jets.isValid())
176     continue;
177     if (!tracks.isValid())
178     continue;
179     double trackSum;
180     nJetsKT = 0;
181     for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
182     !=jets->end(); jet++) {
183     trackSum = 0;
184     for (reco::TrackCollection::const_iterator track = tracks->begin(); track
185     !=tracks->end(); track++) {
186     if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
187     < 0.5)
188     trackSum += track->pt();
189     }
190     TrackJetKT[nJetsKT] = trackSum;
191     nJetsKT++;
192     }
193     friendTree->Fill();
194 tschum 1.8
195     if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
196    
197 tschum 1.7 }
198     f->cd();
199     friendTree->Write();
200     f->Close();
201     ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
202     friendFileName.c_str());
203 gebbert 1.6 }
204 tschum 1.8
205     timer.Stop();
206     if(verbose) timer.Print();
207    
208 gebbert 1.6 return this->GetEntries();
209 tschum 1.1
210     }
211     //------------------------------------------------------------------
212     //Draw given Chain with given variable and given cuts
213 gebbert 1.6 int PlotTool::plot(int chainIndex, string histName, string cutName,
214     int nEntries, string drwOpt) {
215 tschum 1.1
216 gebbert 1.6 if (chainIndex >= this->GetEntries() )
217     return -1;
218 tschum 1.1
219 tschum 1.8 TStopwatch timer;
220 tschum 1.11 if(verbose) {
221     cout<<"Plot: "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
222     timer.Start();
223     }
224 tschum 1.8
225 gebbert 1.6 int currN = nEntries;
226     if (nEntries < 0)
227     currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
228    
229     //++++ Create and name Canvases according to global variables +++++++++++++
230     ostringstream currHistName;
231     if (samePad_trees)
232     currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
233     if (samePad_vars)
234     currHistName<<histName;
235     if (samePad_cuts)
236     currHistName<<"{"<<cutName<<"}";
237    
238     ostringstream currPadName;
239     if (!samePad_trees)
240     currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
241     if (!samePad_vars)
242     currPadName<<histName;
243     if (!samePad_cuts)
244     currPadName<<"{"<<cutName<<"}";
245    
246     ostringstream currCanvName;
247     if (!sameCanv_trees && !samePad_trees)
248     currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
249     if (!sameCanv_vars && !samePad_vars)
250     currCanvName<<histName;
251     if (!sameCanv_cuts && !samePad_cuts)
252     currCanvName<<"{"<<cutName<<"}";
253    
254     if ( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars)
255     && (sameCanv_cuts || samePad_cuts))
256     currCanvName<<"All";
257    
258     string currOpt = drwOpt;
259     bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars
260     && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
261    
262     if (useSubPads) {
263 tschum 1.7 if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
264 gebbert 1.6 canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
265     } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
266     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
267     }
268     }
269 tschum 1.1
270 tschum 1.7 if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
271 gebbert 1.6 pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
272 tschum 1.9 // if (logY)
273     // pads_[currPadName.str()]->SetLogy(1);
274 gebbert 1.6 } else {
275     pads_[currPadName.str()]->cd();
276     currOpt += "SAMES";
277     if (useSubPads)
278     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
279     }
280     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
281 tschum 1.1
282    
283 gebbert 1.6 //Draw histogram:
284     int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(),
285     currOpt.c_str(), currN);
286     if (draw_err < 0)
287     return draw_err;
288    
289     //++++ Fix for histos with no entries +++++++++++++
290     if (draw_err == 0) {
291     if (currOpt.find("SAMES") == string::npos) {
292     pads_[currPadName.str()]->Close();
293     pads_.erase(currPadName.str() );
294     if (useSubPads)
295     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
296     }
297     cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()
298     <<" has no entries and is not drawn!"<<endl;
299     return draw_err;
300     }
301     //++++++++++++++++++++++++++++++++++++++++++++++++++
302 tschum 1.1
303 gebbert 1.6 ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
304    
305 tschum 1.8 // Update for 2D histograms
306     if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
307     pads_[currPadName.str()]->Draw();
308     setCanvas( pads_[currPadName.str()] );
309     }
310 gebbert 1.6
311 tschum 1.11
312 tschum 1.8 if(verbose) {
313 tschum 1.11 timer.Stop();
314     cout<<"Done: "<<currN<<" events."<<endl;
315 tschum 1.8 timer.Print();
316     }
317 gebbert 1.6 return draw_err;
318 tschum 1.1
319     }
320     //------------------------------------------------------------------
321     //Standard loop to draw all chains and multiple variables and cuts
322 gebbert 1.6 int PlotTool::loop(vector<string> _histName, vector<string> _cutName,
323     int nEntries, string drwOpt, bool correspond) {
324 tschum 1.1
325 gebbert 1.6 int numProcessed = 0;
326 tschum 1.1
327 gebbert 1.6 if (correspond == false) {
328 tschum 1.1
329 gebbert 1.6 for (int i=0; i<this->GetEntries(); ++i) {
330     for (vector<string>::iterator it1 =_histName.begin(); it1
331     != _histName.end(); ++it1) {
332     for (vector<string>::iterator it2 =_cutName.begin(); it2
333     != _cutName.end(); ++it2) {
334 tschum 1.1
335 gebbert 1.6 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
336 tschum 1.8
337 tschum 1.1
338 gebbert 1.6 }
339     }
340     }
341     } else {
342 tschum 1.1
343 gebbert 1.6 if (_histName.size() != _cutName.size() )
344     return -1;
345 tschum 1.1
346 gebbert 1.6 for (int i=0; i<this->GetEntries(); ++i) {
347     for (vector<string>::iterator it1 =_histName.begin(), it2 =
348     _cutName.begin(); it1 != _cutName.end()&&it2
349     != _cutName.end(); ++it1, ++it2) {
350 tschum 1.1
351 gebbert 1.6 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
352 tschum 1.1
353 gebbert 1.6 }
354     }
355     }
356 tschum 1.1
357 tschum 1.8 updatePads();
358 gebbert 1.6 fillCanvases();
359 tschum 1.1
360 gebbert 1.6 return numProcessed;
361 tschum 1.1 }
362    
363     //Helper methods to allow for using simple strings as input
364 gebbert 1.6 int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt) {
365 tschum 1.1
366 gebbert 1.6 vector<string> _histName;
367     _histName.push_back(histName);
368     vector<string> _cutName;
369     _cutName.push_back(cutName);
370 tschum 1.1
371 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
372 tschum 1.1
373     }
374    
375 gebbert 1.6 int PlotTool::loop(vector<string> _histName, string cutName, int nEntries,
376     string drwOpt) {
377 tschum 1.1
378 gebbert 1.6 vector<string> _cutName;
379     _cutName.push_back(cutName);
380 tschum 1.1
381 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
382 tschum 1.1
383     }
384    
385 gebbert 1.6 int PlotTool::loop(string histName, vector<string> _cutName, int nEntries,
386     string drwOpt) {
387 tschum 1.1
388 gebbert 1.6 vector<string> _histName;
389     _histName.push_back(histName);
390 tschum 1.1
391 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
392 tschum 1.1 }
393     //------------------------------------------------------------------
394     //redraw the canvas to make changes in style visible
395 gebbert 1.6 int PlotTool::updatePads() {
396 tschum 1.1
397 gebbert 1.6 for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
398 tschum 1.7 if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
399 gebbert 1.6 (*it).second->Draw();
400     setCanvas((*it).second);
401     } else
402     pads_.erase(it);
403     }
404 tschum 1.1
405 gebbert 1.6 return pads_.size();
406 tschum 1.1
407     }
408    
409     int PlotTool::fillCanvases() {
410    
411 gebbert 1.6 for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
412     != canvases_.end(); ++it) {
413 tschum 1.8 string canvName = (*it).first;
414     if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
415 gebbert 1.6
416     int numP = (*it).second->GetNumber()+1;
417    
418     if (numP <= 0)
419     continue;
420    
421     int x = int( sqrt(numP) );
422     int y = x;
423     if (x*y < numP)
424     x += 1;
425     if (x*y < numP)
426     x += 1;
427     if (x*y < numP) {
428     x -= 1;
429     y += 1;
430     }
431    
432 tschum 1.8
433 gebbert 1.6 (*it).second->Divide(x, y);
434     int padIndex = 1;
435     for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
436     != pads_.end(); ++it2) {
437     string padName = (*it2).first;
438 tschum 1.7 if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
439 tschum 1.8 string n1 = canvName.substr(0,canvName.find(":"));
440     string n2("");
441     if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
442    
443     if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
444 gebbert 1.6 (*it).second->cd(padIndex);
445     (*it2).second->DrawClonePad();
446     (*it2).second->Close();
447     pads_.erase(it2);
448     ++padIndex;
449     }
450     } else
451     pads_.erase(it2);
452     }
453     (*it).second->SetNumber(-1);
454 tschum 1.1
455 gebbert 1.6 } else
456     canvases_.erase(it);
457     }
458 tschum 1.1
459 gebbert 1.6 return canvases_.size();
460 tschum 1.1
461     }
462     //------------------------------------------------------------------
463     void PlotTool::setCanvas(TCanvas* thisCanvas) {
464    
465 gebbert 1.6 TH1* thisHist = 0;
466     TPaveStats* thisStatsBox = 0;
467     TPaletteAxis* palette =0;
468     TLegend* thisLeg = 0;
469     int counter =0;
470     double maxEntry=0;
471 tschum 1.1
472 gebbert 1.6 ((TFrame*) thisCanvas->GetFrame())->Delete();
473     thisCanvas->GetFrame()->SetLineWidth(gStyle->GetLineWidth() );
474 tschum 1.1
475 gebbert 1.6 thisCanvas->SetLeftMargin(0.2);
476     thisCanvas->SetRightMargin(0.06);
477     thisCanvas->SetBottomMargin(0.2);
478     thisCanvas->SetTopMargin(0.1);
479 tschum 1.1
480 gebbert 1.6 if (logY)
481     thisCanvas->SetLogy(1);
482     else
483     thisCanvas->SetLogy(0);
484 tschum 1.1
485 gebbert 1.6 if (showLegend)
486     thisLeg = new TLegend();
487 tschum 1.1
488 gebbert 1.6 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
489 tschum 1.1
490 gebbert 1.6 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1"))
491     continue;
492 tschum 1.1
493 gebbert 1.6 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
494     setColor(thisHist, counter);
495     setMathLabels(thisHist);
496 tschum 1.1
497 gebbert 1.6 if (thisHist->GetMaximum() > maxEntry)
498     maxEntry = thisHist->GetMaximum();
499 tschum 1.1
500 gebbert 1.6 thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
501     if (thisStatsBox)
502     setStats(thisCanvas, thisStatsBox, thisHist, counter);
503 tschum 1.1
504 gebbert 1.6 palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
505     if (palette)
506     setPalette(thisCanvas, palette);
507 tschum 1.1
508 gebbert 1.6 if (thisLeg)
509     thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
510 tschum 1.1
511 gebbert 1.6 ++counter;
512 tschum 1.1
513 gebbert 1.6 }
514 tschum 1.1
515 gebbert 1.6 if (maxEntry != 0)
516     setHistMax(thisCanvas, maxEntry);
517 tschum 1.1
518 gebbert 1.6 thisCanvas->cd();
519     thisCanvas->Update();
520 tschum 1.1
521 gebbert 1.6 if (thisLeg)
522     setLegend(thisCanvas, thisLeg, counter);
523 tschum 1.1
524     }
525     //------------------------------------------------------------------
526     //private helper classes to set the canvas and hist style
527 gebbert 1.6 void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox,
528     TH1* thisHist, int counter) {
529 tschum 1.1
530 gebbert 1.6 if (thisCanvas->GetRightMargin() < .2)
531     thisCanvas->SetRightMargin(.2);
532 tschum 1.1
533 gebbert 1.6 thisStatsBox->SetLineColor(thisHist->GetLineColor());
534     thisStatsBox->SetX2NDC(1.);
535     thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
536     thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
537     thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
538 tschum 1.1
539     }
540    
541     void PlotTool::setMathLabels(TH1* thisHist) {
542    
543 gebbert 1.6 string t = thisHist ->GetTitle();
544     string x = thisHist->GetXaxis()->GetTitle();
545     string y = thisHist->GetYaxis()->GetTitle();
546    
547     if (t.find(".phi()") != string::npos)
548     t.replace(t.find(".phi()"), 6, " #phi");
549     if (x.find(".phi()") != string::npos)
550     x.replace(x.find(".phi()"), 6, " #phi");
551     if (y.find(".phi()") != string::npos)
552     y.replace(y.find(".phi()"), 6, " #phi");
553    
554     if (t.find(".eta()") != string::npos)
555     t.replace(t.find(".eta()"), 6, " #eta");
556     if (x.find(".eta()") != string::npos)
557     x.replace(x.find(".eta()"), 6, " #eta");
558     if (y.find(".eta()") != string::npos)
559     y.replace(y.find(".eta()"), 6, " #eta");
560    
561     if (t.find(".pt()") != string::npos)
562     t.replace(t.find(".pt()"), 5, " p_{T}");
563     if (x.find(".pt()") != string::npos)
564     x.replace(x.find(".pt()"), 5, " p_{T}");
565     if (y.find(".pt()") != string::npos)
566     y.replace(y.find(".pt()"), 5, " p_{T}");
567    
568     thisHist ->SetTitle(t.c_str());
569     thisHist->GetXaxis()->SetTitle(x.c_str());
570     thisHist->GetYaxis()->SetTitle(y.c_str());
571 tschum 1.1
572     }
573    
574     void PlotTool::setColor(TH1* thisHist, int counter) {
575    
576 gebbert 1.6 if (counter == 0) {
577 tschum 1.1
578 gebbert 1.6 thisHist->SetLineColor(kRed+1);
579     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
580     //else
581     thisHist->SetFillStyle(0);
582     thisHist->SetMarkerColor(kRed+1);
583     } else if (counter == 1) {
584     thisHist->SetLineColor(kBlue+1);
585     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
586     //else
587     thisHist->SetFillStyle(0);
588     thisHist->SetMarkerColor(kBlue+1);
589    
590     } else if (counter == 2) {
591     thisHist->SetLineColor(kGreen+2);
592     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
593     //else
594     thisHist->SetFillStyle(0);
595     thisHist->SetMarkerColor(kGreen+2);
596    
597     } else if (counter == 3) {
598     thisHist->SetLineColor(kMagenta+2);
599     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
600     //else
601     thisHist->SetFillStyle(0);
602     thisHist->SetMarkerColor(kMagenta+2);
603    
604     } else if (counter == 4) {
605     thisHist->SetLineColor(kCyan+2);
606     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
607     //else
608     thisHist->SetFillStyle(0);
609     thisHist->SetMarkerColor(kCyan+2);
610    
611     } else {
612     thisHist->SetLineColor(kBlack);
613     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
614     //else
615     thisHist->SetFillStyle(0);
616     thisHist->SetMarkerColor(kBlack);
617 tschum 1.1
618 gebbert 1.6 }
619 tschum 1.1
620     }
621    
622     void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
623    
624 gebbert 1.6 palette->SetLabelSize(0.045);
625     if (thisCanvas->GetRightMargin() < .15)
626     thisCanvas->SetRightMargin(.15);
627     palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
628     palette->SetY1NDC(thisCanvas->GetBottomMargin());
629     palette->SetX2NDC(palette->GetX1NDC()+0.05);
630     palette->SetY2NDC(1-thisCanvas->GetTopMargin());
631 tschum 1.1
632     }
633    
634     void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
635    
636 gebbert 1.6 TH1* thisHist = 0;
637 tschum 1.1
638 gebbert 1.6 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
639 tschum 1.1
640 gebbert 1.6 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") )
641     continue;
642     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
643     if (thisHist->GetMaximum() < maxEntry) {
644     double minEntry=thisHist->GetBinContent(thisHist->GetMinimumBin());
645     if (logY) {
646     int bin = (thisHist->GetMinimumBin()+1);
647     minEntry=thisHist->GetBinContent(bin);
648     }
649     thisHist->GetYaxis()->SetRangeUser(minEntry, maxEntry*1.2);
650     }
651     break;
652 tschum 1.1
653 gebbert 1.6 }
654 tschum 1.1
655     }
656    
657     void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
658    
659 gebbert 1.6 thisLeg->SetFillStyle(0);
660     thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
661     thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
662     thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
663     thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
664     thisLeg->SetEntrySeparation(0.5);
665     thisLeg->Draw("NDC");
666 tschum 1.1
667     }
668    
669 gebbert 1.6 void PlotTool::createColors() {
670     const Int_t NRGBs = 5;
671     const Int_t NCont = 255;
672    
673     Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
674     Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
675     Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
676     Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
677     TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
678     gStyle->SetNumberContours(NCont);
679 tschum 1.1
680     }
681    
682     //------------------------------------------------------------------
683    
684 tschum 1.9 int PlotTool::saveCanvases(string name, string path) {
685 tschum 1.1
686 gebbert 1.6 TSystemDirectory d("", path.c_str());
687     if (!d.GetListOfFiles())
688     return -1;
689    
690 tschum 1.9 string namePs = path +"/" + name;
691     string nameRt = path +"/" + name;
692    
693     if (name.find(".ps") == string::npos)
694     namePs += ".ps";
695    
696     if (name.find(".root") == string::npos)
697     nameRt += ".root";
698    
699     int savedCanvs =0;
700    
701     TPostScript ps(namePs.c_str(),112);
702     TFile rt(nameRt.c_str(),"recreate");
703 gebbert 1.6
704     TIter next(gROOT->GetListOfCanvases());
705     TCanvas* canv;
706     while ((canv=(TCanvas*)next())) {
707 tschum 1.9 ps.NewPage();
708     (*canv).Draw();
709     (*canv).Update();
710     (*canv).Write();
711     ++savedCanvs;
712 gebbert 1.6
713 tschum 1.9 }
714 gebbert 1.6
715 tschum 1.9 ps.Close();
716     rt.Close();
717 tschum 1.1
718 tschum 1.9 return savedCanvs;
719 tschum 1.1
720     }
721     //------------------------------------------------------------------
722 tschum 1.11
723     int PlotTool::clearCanvases()
724 tschum 1.7 {
725    
726 tschum 1.11 while(gROOT->GetListOfCanvases()->GetEntries()) ((TCanvas*) gROOT->GetListOfCanvases()->At(0))->Close();
727     pads_.clear();
728     autoVars.clear();
729     return pads_.size() + gROOT->GetListOfCanvases()->GetEntries();
730    
731     }
732     //------------------------------------------------------------------
733     int PlotTool::setVariables(string label)
734     {
735 tschum 1.7
736     cout<<endl;
737     cout<<"****** Show Chain Information:"<<endl;
738    
739    
740     this->ls();
741    
742     cout<<endl;
743     cout<<"We have "<<this->GetEntries()<<" TChains with following aliases set:"<<endl;
744     for (int i=0; i< this->GetEntries(); ++i) {
745 tschum 1.11 cout<<"--------------------------------"<<endl;
746 tschum 1.7 cout<<((TChain*) this->At(i))->GetName()<<endl;
747 tschum 1.11 cout<<"------------"<<endl;
748    
749     TList* leaves = (TList*) ((TChain*) this->At(i))->GetListOfLeaves();
750     for (int j=0; j< leaves->GetEntries(); ++j) {
751     TString leafName ( leaves->At(j)->GetName() );
752     if(! leafName.EndsWith(".") ) continue;
753     if(! leafName.Contains(label.c_str() ) ) continue;
754    
755     TClass cl( ( (TLeafElement*) leaves->At(j) )->GetTypeName() );
756     cout<<"++++++"<<endl;
757     cout<< leafName.Data() <<endl;
758     for(int k=0;k<cl.GetListOfAllPublicMethods()->GetEntries();++k) {
759     string typeName ( ((TMethod*) cl.GetListOfAllPublicMethods()->At(k))->GetReturnTypeName() );
760     string methName ( cl.GetListOfAllPublicMethods()->At(k)->GetName() );
761     if( methName != "product") continue;
762     cout<< typeName <<endl;
763     string _type;
764     TString testString( typeName.c_str() );
765     if( testString.BeginsWith("vector<") ) {
766     _type = typeName.substr(typeName.find("<")+1,typeName.find(">")-typeName.find("<")-1);
767     string _varSize = leafName.Data();
768     _varSize += "obj@.size()";
769     autoVars.push_back( _varSize );
770     }
771     else _type = typeName.substr(0,typeName.find("*"));
772     TClass _cl( _type.c_str() );
773     for(int l=0;l<_cl.GetListOfAllPublicMethods()->GetEntries();++l) {
774     string _typeName ( ((TMethod*) _cl.GetListOfAllPublicMethods()->At(l))->GetReturnTypeName() );
775     string _methName ( _cl.GetListOfAllPublicMethods()->At(l)->GetName() );
776     // if(_typeName.find("void") != string::npos ) continue;
777     // cout<<" "<<_typeName<<" "<<_methName<<endl;
778    
779     cout<<" "<<_typeName<<" "<<_methName<<endl;
780     if(_methName.find("operator")==string::npos&&(_typeName=="float"||_typeName=="double"||_typeName=="int"||_typeName=="unsigned int"||_typeName=="bool"||_typeName=="unsigned short"||_typeName=="unsigned long"||_typeName=="unsigned long long")) {
781    
782     cout<<"--> "<<_typeName<<" "<<_methName<<endl;
783    
784     string _varName = leafName.Data();
785     _varName += "obj.";
786     _varName += _methName;
787     _varName += "()";
788     autoVars.push_back( _varName );
789     }
790     }
791    
792    
793     }
794    
795     }
796 tschum 1.7
797     }
798    
799 tschum 1.11 return autoVars.size();
800 tschum 1.7
801     }
802 tschum 1.8 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
803     //Draw efficiencies
804     int PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
805     {
806    
807     if( chainIndex >= this->GetEntries() ) return -1;
808    
809     int currN = nEntries;
810     if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
811    
812     //++++ Create and name Canvases according to global variables +++++++++++++
813     ostringstream currHistName;
814     if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
815     if( samePad_vars) currHistName<<histName;
816     if( samePad_cuts) currHistName<<"{"<<cutName<<"}";
817    
818     ostringstream currPadName;
819     if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
820     if(! samePad_vars) currPadName<<histName;
821     if(! samePad_cuts) currPadName<<"{"<<cutName<<"}";
822    
823    
824     //Draw total histogram:
825     if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
826     pads_["total"] = new TCanvas("total", "total");
827     } else {
828     pads_["total"]->cd();
829     }
830     ostringstream bins_total;
831     bins_total<<histName;
832     bins_total<<">>total(100,0,1000)";
833    
834    
835     int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
836     if( draw_err <= 0 ) return draw_err;
837    
838    
839     TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
840    
841     ostringstream bins_bkg;
842     bins_bkg<<histName;
843     bins_bkg<<">>bkg(";
844     bins_bkg<<total->GetNbinsX();
845     bins_bkg<<",";
846     bins_bkg<<total->GetXaxis()->GetXmin();
847     bins_bkg<<",";
848     bins_bkg<<total->GetXaxis()->GetXmax();
849     bins_bkg<<")";
850    
851     //Draw bkg histogram:
852     if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
853     pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
854     } else {
855     pads_["bkg"]->cd();
856     }
857    
858     draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
859     if( draw_err <= 0 ) return draw_err;
860    
861    
862     TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
863    
864     //Draw pass histogram:
865     ostringstream bins_pass;
866     bins_pass<<histName;
867     bins_pass<<">>pass(";
868     bins_pass<<total->GetNbinsX();
869     bins_pass<<",";
870     bins_pass<<total->GetXaxis()->GetXmin();
871     bins_pass<<",";
872     bins_pass<<total->GetXaxis()->GetXmax();
873     bins_pass<<")";
874    
875     ostringstream cut_pass;
876     cut_pass<<"!(";
877     cut_pass<<cutName;
878     cut_pass<<")";
879    
880    
881     if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
882     pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
883     } else {
884     pads_["pass"]->cd();
885     }
886    
887     draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
888     if( draw_err <= 0 ) return draw_err;
889    
890    
891     TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
892    
893    
894     currPadName<<"Eff";
895     //Draw Efficiency Graph:
896     if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
897     pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
898     } else {
899     pads_["EffGraph"]->cd();
900     }
901    
902     TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
903     EffGraph->SetName(currHistName.str().c_str());
904    
905     TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
906     EffGraph->Apply(reverse);
907     EffGraph->Draw("A*");
908    
909     TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
910     EffGraph->Fit("fitfunc","R+");
911    
912    
913     //Save fit function
914    
915     ostringstream savefuncName;
916     savefuncName<<histName;
917     savefuncName<<"_";
918    
919     if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
920     if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
921     savefuncName<<cutName;
922    
923    
924     TFile file("ABCDFunctions.root","UPDATE");
925     TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
926     savefunc->SetParameters( fitfunc->GetParameters() );
927     savefunc->SetParErrors ( fitfunc->GetParErrors() );
928    
929     //Show results
930     if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
931     pads_["abcd"] = new TCanvas("abcd", "abcd");
932     } else {
933     pads_["abcd"]->cd();
934     }
935    
936     bkg->Multiply(savefunc);
937     bkg->Draw();
938    
939     // total->Add(bkg,-1);
940     pass->SetLineColor(kRed);
941     pass->Draw("sames,e");
942     pads_["abcd"]->SetLogy();
943    
944    
945     savefunc->Write();
946     // file.Close();
947    
948    
949     return draw_err;
950    
951     }
952     //------------------------------------------------------------------