ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.2
Committed: Fri Nov 13 15:41:42 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -0 lines
Log Message:
enabled LogY

File Contents

# User Rev Content
1 tschum 1.1 #include "PlotTool.h"
2     //------------------------------------------------------------------
3     //Constructur: Set some global Variables to define canvas output
4     PlotTool::PlotTool()
5     {
6    
7     this->SetClass("TChain",100);
8    
9     samePad_trees = true;
10     samePad_vars = false;
11     samePad_cuts = false;
12     sameCanv_trees = false;
13     sameCanv_vars = false;
14     sameCanv_cuts = false;
15    
16     showLegend = false;
17 tschum 1.2 logY = true;
18    
19 tschum 1.1
20     }
21    
22     //------------------------------------------------------------------
23     //Fill object PlotTool with Chains constructed from files from given source
24     int PlotTool::init(string fileName, string dirPath, string treeName)
25     {
26     this->New( this->GetEntries() );
27     int currChain = this->GetEntries() - 1;
28    
29     if(currChain < 0 ) return currChain;
30    
31    
32     ((TChain*) this->At(currChain))->SetName(fileName.c_str());
33     TSystemDirectory dir("sourceDir",dirPath.c_str());
34     TList *files = dir.GetListOfFiles();
35     if (files) {
36     TIter next(files);
37     TSystemFile *file;
38     TString fname;
39     string filePath;
40    
41     cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "<<fileName.c_str()<<endl;
42    
43     while ((file=(TSystemFile*)next())) {
44     fname = file->GetName();
45     if (!fname.EndsWith(".root")) continue;
46     if (!fname.Contains(fileName.c_str())) continue;
47    
48     filePath = dirPath;
49     filePath += fname.Data();
50     if( ((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1, treeName.c_str()) ) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
51     else return -1;
52    
53     }
54     } else return -1;
55    
56     for(int i=0;i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries();++i){
57    
58     string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
59     size_t a = s.find("_");
60     if(a == string::npos) a = 0;
61     size_t b = s.find("_", a+1);
62     if(b == string::npos) b = 50;
63     string branch_alias = s.substr(a+1,b-a-1);
64     string branch_name = s;
65     if(s.find(".",s.size()-1) != string::npos ) branch_name += "obj";
66     ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),branch_name.c_str());
67    
68     }
69    
70     return this->GetEntries();
71    
72     }
73     //------------------------------------------------------------------
74     //Draw given Chain with given variable and given cuts
75     int PlotTool::plot(int chainIndex, string histName, string cutName, int nEntries, string drwOpt)
76     {
77    
78     if( chainIndex >= this->GetEntries() ) return -1;
79    
80     int currN = nEntries;
81     if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
82    
83     //++++ Create and name Canvases according to global variables +++++++++++++
84     ostringstream currHistName;
85     if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
86     if( samePad_vars) currHistName<<histName;
87     if( samePad_cuts) currHistName<<"{"<<cutName<<"}";
88    
89     ostringstream currPadName;
90     if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
91     if(! samePad_vars) currPadName<<histName;
92     if(! samePad_cuts) currPadName<<"{"<<cutName<<"}";
93    
94     ostringstream currCanvName;
95     if(! sameCanv_trees && ! samePad_trees ) currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
96     if(! sameCanv_vars && ! samePad_vars) currCanvName<<histName;
97     if(! sameCanv_cuts && ! samePad_cuts) currCanvName<<"{"<<cutName<<"}";
98    
99     if( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars) && (sameCanv_cuts || samePad_cuts) ) currCanvName<<"All";
100    
101    
102     string currOpt = drwOpt;
103     bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
104    
105     if( useSubPads ) {
106     if( ! canvases_[currCanvName.str()] || ! gROOT->FindObject(currCanvName.str().c_str()) ) {
107     canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
108     } else if ( canvases_[currCanvName.str()]->GetNumber() >= 0 ) {
109     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
110     }
111     }
112    
113     if( ! pads_[currPadName.str()] || ! gROOT->FindObject(currPadName.str().c_str()) ) {
114     pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
115 tschum 1.2 if(logY) pads_[currPadName.str()]->SetLogy(1);
116 tschum 1.1 } else {
117     pads_[currPadName.str()]->cd();
118     currOpt += "SAMES";
119     if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
120     }
121     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122    
123    
124     //Draw histogram:
125     int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(), currOpt.c_str(), currN);
126     if( draw_err < 0 ) return draw_err;
127    
128     //++++ Fix for histos with no entries +++++++++++++
129     if( draw_err == 0 ) {
130     if( currOpt.find("SAMES") == string::npos ) {
131     pads_[currPadName.str()]->Close();
132     pads_.erase( currPadName.str() );
133     if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
134     }
135     cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()<<" has no entries and is not drawn!"<<endl;
136     return draw_err;
137     }
138     //++++++++++++++++++++++++++++++++++++++++++++++++++
139    
140     ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
141    
142    
143     return draw_err;
144    
145     }
146     //------------------------------------------------------------------
147     //Standard loop to draw all chains and multiple variables and cuts
148     int PlotTool::loop(vector<string> _histName, vector<string> _cutName, int nEntries, string drwOpt, bool correspond)
149     {
150    
151    
152    
153     int numProcessed = 0;
154    
155     if( correspond == false ) {
156    
157     for(int i=0; i<this->GetEntries(); ++i) {
158     for(vector<string>::iterator it1 =_histName.begin(); it1 != _histName.end(); ++it1) {
159     for(vector<string>::iterator it2 =_cutName.begin(); it2 != _cutName.end(); ++it2) {
160    
161     numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
162    
163     }
164     }
165     }
166     } else {
167    
168     if( _histName.size() != _cutName.size() ) return -1;
169    
170     for(int i=0; i<this->GetEntries(); ++i) {
171     for(vector<string>::iterator it1 =_histName.begin(),it2 =_cutName.begin(); it1 != _cutName.end()&&it2 != _cutName.end(); ++it1, ++it2) {
172    
173     numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
174    
175    
176     }
177     }
178     }
179    
180     updatePads();
181     fillCanvases();
182    
183     return numProcessed;
184     }
185    
186     //Helper methods to allow for using simple strings as input
187     int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt)
188     {
189    
190     vector<string> _histName;
191     _histName.push_back( histName );
192     vector<string> _cutName;
193     _cutName.push_back( cutName );
194    
195     return loop(_histName, _cutName, nEntries, drwOpt);
196    
197     }
198    
199     int PlotTool::loop(vector<string> _histName, string cutName, int nEntries, string drwOpt)
200     {
201    
202     vector<string> _cutName;
203     _cutName.push_back( cutName );
204    
205     return loop(_histName, _cutName, nEntries, drwOpt);
206    
207     }
208    
209     int PlotTool::loop(string histName, vector<string> _cutName, int nEntries, string drwOpt)
210     {
211    
212     vector<string> _histName;
213     _histName.push_back( histName );
214    
215     return loop(_histName, _cutName, nEntries, drwOpt);
216     }
217     //------------------------------------------------------------------
218     //redraw the canvas to make changes in style visible
219     int PlotTool::updatePads() {
220    
221     for(map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it ) {
222     if( gROOT->FindObject( (*it).first.c_str() ) ) {
223     (*it).second->Draw();
224     setCanvas( (*it).second );
225     } else pads_.erase( it );
226     }
227    
228     return pads_.size();
229    
230     }
231    
232     int PlotTool::fillCanvases() {
233    
234    
235     for(map< string, TCanvas* >::iterator it=canvases_.begin() ; it != canvases_.end(); ++it ) {
236     const char* canvName = (*it).first.c_str();
237     if( gROOT->FindObject( canvName ) ) {
238    
239     int numP = (*it).second->GetNumber()+1;
240    
241     if( numP <= 0 ) continue;
242    
243     int x = int( sqrt(numP) );
244     int y = x;
245     if( x*y < numP ) x += 1;
246     if( x*y < numP ) x += 1;
247     if( x*y < numP ) {
248     x -= 1;
249     y += 1;
250     }
251    
252     (*it).second->Divide(x,y);
253     int padIndex = 1;
254     for(map< string, TCanvas* >::iterator it2=pads_.begin() ; it2 != pads_.end(); ++it2 ) {
255     string padName = (*it2).first;
256     if( gROOT->FindObject( padName.c_str() ) ) {
257     if( ( padName.find(canvName) != string::npos ) || !strcmp(canvName,"All") ) {
258     (*it).second->cd(padIndex);
259     (*it2).second->DrawClonePad();
260     (*it2).second->Close();
261     pads_.erase( it2 );
262     ++padIndex;
263     }
264     } else pads_.erase( it2 );
265     }
266     (*it).second->SetNumber(-1);
267    
268     } else canvases_.erase( it );
269     }
270    
271     return canvases_.size();
272    
273     }
274     //------------------------------------------------------------------
275     void PlotTool::setCanvas(TCanvas* thisCanvas) {
276    
277    
278     TH1* thisHist = 0;
279     TPaveStats* thisStatsBox = 0;
280     TPaletteAxis* palette =0;
281     TLegend* thisLeg = 0;
282     int counter =0;
283     double maxEntry=0;
284    
285    
286     ((TFrame*) thisCanvas->GetFrame())->Delete();
287     thisCanvas->GetFrame()->SetLineWidth( gStyle->GetLineWidth() );
288    
289     thisCanvas->SetLeftMargin(0.2);
290     thisCanvas->SetRightMargin(0.06);
291     thisCanvas->SetBottomMargin(0.2);
292     thisCanvas->SetTopMargin(0.1);
293    
294 tschum 1.2 if(logY) thisCanvas->SetLogy(1);
295     else thisCanvas->SetLogy(0);
296 tschum 1.1
297     if(showLegend) thisLeg = new TLegend();
298    
299    
300     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
301    
302    
303     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1")) continue;
304    
305     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
306     setColor(thisHist, counter);
307     setMathLabels(thisHist);
308    
309     if(thisHist->GetMaximum() > maxEntry) maxEntry = thisHist->GetMaximum();
310    
311     thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
312     if(thisStatsBox) setStats(thisCanvas, thisStatsBox, thisHist, counter);
313    
314     palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
315     if(palette) setPalette(thisCanvas, palette);
316    
317     if(thisLeg) thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
318    
319    
320     ++counter;
321    
322     }
323    
324     if( maxEntry != 0) setHistMax(thisCanvas, maxEntry);
325    
326    
327     thisCanvas->cd();
328     thisCanvas->Update();
329    
330     if(thisLeg) setLegend( thisCanvas, thisLeg, counter);
331    
332    
333     }
334     //------------------------------------------------------------------
335     //private helper classes to set the canvas and hist style
336     void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox, TH1* thisHist, int counter) {
337    
338    
339     if(thisCanvas->GetRightMargin() < .2) thisCanvas->SetRightMargin(.2);
340    
341     thisStatsBox->SetLineColor(thisHist->GetLineColor());
342     thisStatsBox->SetX2NDC(1.);
343     thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
344     thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
345     thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
346    
347    
348    
349     }
350    
351     void PlotTool::setMathLabels(TH1* thisHist) {
352    
353    
354     string t = thisHist ->GetTitle();
355     string x = thisHist->GetXaxis()->GetTitle();
356     string y = thisHist->GetYaxis()->GetTitle();
357    
358     if(t.find(".phi()") != string::npos ) t.replace(t.find(".phi()"),6," #phi");
359     if(x.find(".phi()") != string::npos ) x.replace(x.find(".phi()"),6," #phi");
360     if(y.find(".phi()") != string::npos ) y.replace(y.find(".phi()"),6," #phi");
361    
362     if(t.find(".eta()") != string::npos ) t.replace(t.find(".eta()"),6," #eta");
363     if(x.find(".eta()") != string::npos ) x.replace(x.find(".eta()"),6," #eta");
364     if(y.find(".eta()") != string::npos ) y.replace(y.find(".eta()"),6," #eta");
365    
366     if(t.find(".pt()") != string::npos ) t.replace(t.find(".pt()"),5," p_{T}");
367     if(x.find(".pt()") != string::npos ) x.replace(x.find(".pt()"),5," p_{T}");
368     if(y.find(".pt()") != string::npos ) y.replace(y.find(".pt()"),5," p_{T}");
369    
370     thisHist ->SetTitle(t.c_str());
371     thisHist->GetXaxis()->SetTitle(x.c_str());
372     thisHist->GetYaxis()->SetTitle(y.c_str());
373    
374    
375     }
376    
377    
378     void PlotTool::setColor(TH1* thisHist, int counter) {
379    
380    
381     if(counter == 0) {
382    
383     thisHist->SetLineColor (kRed+1);
384     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
385     else thisHist->SetFillStyle(0);
386     thisHist->SetMarkerColor(kRed+1);
387     } else if(counter == 1) {
388     thisHist->SetLineColor (kBlue+1);
389     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
390     else thisHist->SetFillStyle(0);
391     thisHist->SetMarkerColor(kBlue+1);
392    
393     } else if(counter == 2) {
394     thisHist->SetLineColor (kGreen+2);
395     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
396     else thisHist->SetFillStyle(0);
397     thisHist->SetMarkerColor(kGreen+2);
398    
399     } else if(counter == 3) {
400     thisHist->SetLineColor (kMagenta+2);
401     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
402     else thisHist->SetFillStyle(0);
403     thisHist->SetMarkerColor(kMagenta+2);
404    
405     } else if(counter == 4) {
406     thisHist->SetLineColor (kCyan+2);
407     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
408     else thisHist->SetFillStyle(0);
409     thisHist->SetMarkerColor(kCyan+2);
410    
411     } else {
412     thisHist->SetLineColor (kBlack);
413     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
414     else thisHist->SetFillStyle(0);
415     thisHist->SetMarkerColor(kBlack);
416    
417     }
418    
419     }
420    
421     void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
422    
423    
424     palette->SetLabelSize(0.045);
425     if(thisCanvas->GetRightMargin() < .15) thisCanvas->SetRightMargin(.15);
426     palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
427     palette->SetY1NDC(thisCanvas->GetBottomMargin());
428     palette->SetX2NDC(palette->GetX1NDC()+0.05);
429     palette->SetY2NDC(1-thisCanvas->GetTopMargin());
430    
431    
432     }
433    
434     void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
435    
436     TH1* thisHist = 0;
437    
438     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
439    
440     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") ) continue;
441     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
442     if( thisHist->GetMaximum() < maxEntry ) thisHist->GetYaxis()->SetRangeUser(thisHist->GetMinimum(),maxEntry*1.2);
443     break;
444    
445     }
446    
447    
448     }
449    
450     void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
451    
452     thisLeg->SetFillStyle(0);
453     thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
454     thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
455     thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
456     thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
457     thisLeg->SetEntrySeparation(0.5);
458     thisLeg->Draw("NDC");
459    
460     }
461    
462     void PlotTool::createColors()
463     {
464     const Int_t NRGBs = 5;
465     const Int_t NCont = 255;
466    
467     Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
468     Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
469     Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
470     Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
471     TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
472     gStyle->SetNumberContours(NCont);
473    
474     }
475    
476    
477     //------------------------------------------------------------------
478    
479     int PlotTool::saveCanvases(string type, string path) {
480    
481    
482     TSystemDirectory d("",path.c_str());
483     if(!d.GetListOfFiles()) return -1;
484     if(type.find(".") == string::npos) return -1;
485    
486     int savedFiles =0;
487    
488     TIter next(gROOT->GetListOfCanvases());
489     TCanvas* canv;
490     while ((canv=(TCanvas*)next())) {
491    
492     string s = "";
493     s += path;
494     s += canv->GetName();
495     s += type;
496    
497     canv->SaveAs(s.c_str());
498     ++savedFiles;
499    
500     }
501    
502     return savedFiles;
503    
504     }
505     //------------------------------------------------------------------