ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.19
Committed: Fri Feb 12 15:21:17 2010 UTC (15 years, 3 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.18: +48 -18 lines
Log Message:
improved jetContent vars, plot utils

File Contents

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