ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.18
Committed: Wed Feb 10 11:41:57 2010 UTC (15 years, 3 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.17: +25 -639 lines
Log Message:
outsourced calculation of extra variables

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