ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.3
Committed: Mon Nov 16 15:41:19 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.2: +2 -1 lines
Log Message:
fix for 2D plots

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 tschum 1.3 updatePads();
163 tschum 1.1
164     }
165     }
166     }
167     } else {
168    
169     if( _histName.size() != _cutName.size() ) return -1;
170    
171     for(int i=0; i<this->GetEntries(); ++i) {
172     for(vector<string>::iterator it1 =_histName.begin(),it2 =_cutName.begin(); it1 != _cutName.end()&&it2 != _cutName.end(); ++it1, ++it2) {
173    
174     numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
175    
176    
177     }
178     }
179     }
180    
181 tschum 1.3 // updatePads();
182 tschum 1.1 fillCanvases();
183    
184     return numProcessed;
185     }
186    
187     //Helper methods to allow for using simple strings as input
188     int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt)
189     {
190    
191     vector<string> _histName;
192     _histName.push_back( histName );
193     vector<string> _cutName;
194     _cutName.push_back( cutName );
195    
196     return loop(_histName, _cutName, nEntries, drwOpt);
197    
198     }
199    
200     int PlotTool::loop(vector<string> _histName, string cutName, int nEntries, string drwOpt)
201     {
202    
203     vector<string> _cutName;
204     _cutName.push_back( cutName );
205    
206     return loop(_histName, _cutName, nEntries, drwOpt);
207    
208     }
209    
210     int PlotTool::loop(string histName, vector<string> _cutName, int nEntries, string drwOpt)
211     {
212    
213     vector<string> _histName;
214     _histName.push_back( histName );
215    
216     return loop(_histName, _cutName, nEntries, drwOpt);
217     }
218     //------------------------------------------------------------------
219     //redraw the canvas to make changes in style visible
220     int PlotTool::updatePads() {
221    
222     for(map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it ) {
223     if( gROOT->FindObject( (*it).first.c_str() ) ) {
224     (*it).second->Draw();
225     setCanvas( (*it).second );
226     } else pads_.erase( it );
227     }
228    
229     return pads_.size();
230    
231     }
232    
233     int PlotTool::fillCanvases() {
234    
235    
236     for(map< string, TCanvas* >::iterator it=canvases_.begin() ; it != canvases_.end(); ++it ) {
237     const char* canvName = (*it).first.c_str();
238     if( gROOT->FindObject( canvName ) ) {
239    
240     int numP = (*it).second->GetNumber()+1;
241    
242     if( numP <= 0 ) continue;
243    
244     int x = int( sqrt(numP) );
245     int y = x;
246     if( x*y < numP ) x += 1;
247     if( x*y < numP ) x += 1;
248     if( x*y < numP ) {
249     x -= 1;
250     y += 1;
251     }
252    
253     (*it).second->Divide(x,y);
254     int padIndex = 1;
255     for(map< string, TCanvas* >::iterator it2=pads_.begin() ; it2 != pads_.end(); ++it2 ) {
256     string padName = (*it2).first;
257     if( gROOT->FindObject( padName.c_str() ) ) {
258     if( ( padName.find(canvName) != string::npos ) || !strcmp(canvName,"All") ) {
259     (*it).second->cd(padIndex);
260     (*it2).second->DrawClonePad();
261     (*it2).second->Close();
262     pads_.erase( it2 );
263     ++padIndex;
264     }
265     } else pads_.erase( it2 );
266     }
267     (*it).second->SetNumber(-1);
268    
269     } else canvases_.erase( it );
270     }
271    
272     return canvases_.size();
273    
274     }
275     //------------------------------------------------------------------
276     void PlotTool::setCanvas(TCanvas* thisCanvas) {
277    
278    
279     TH1* thisHist = 0;
280     TPaveStats* thisStatsBox = 0;
281     TPaletteAxis* palette =0;
282     TLegend* thisLeg = 0;
283     int counter =0;
284     double maxEntry=0;
285    
286    
287     ((TFrame*) thisCanvas->GetFrame())->Delete();
288     thisCanvas->GetFrame()->SetLineWidth( gStyle->GetLineWidth() );
289    
290     thisCanvas->SetLeftMargin(0.2);
291     thisCanvas->SetRightMargin(0.06);
292     thisCanvas->SetBottomMargin(0.2);
293     thisCanvas->SetTopMargin(0.1);
294    
295 tschum 1.2 if(logY) thisCanvas->SetLogy(1);
296     else thisCanvas->SetLogy(0);
297 tschum 1.1
298     if(showLegend) thisLeg = new TLegend();
299    
300    
301     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
302    
303    
304     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1")) continue;
305    
306     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
307     setColor(thisHist, counter);
308     setMathLabels(thisHist);
309    
310     if(thisHist->GetMaximum() > maxEntry) maxEntry = thisHist->GetMaximum();
311    
312     thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
313     if(thisStatsBox) setStats(thisCanvas, thisStatsBox, thisHist, counter);
314    
315     palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
316     if(palette) setPalette(thisCanvas, palette);
317    
318     if(thisLeg) thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
319    
320    
321     ++counter;
322    
323     }
324    
325     if( maxEntry != 0) setHistMax(thisCanvas, maxEntry);
326    
327    
328     thisCanvas->cd();
329     thisCanvas->Update();
330    
331     if(thisLeg) setLegend( thisCanvas, thisLeg, counter);
332    
333    
334     }
335     //------------------------------------------------------------------
336     //private helper classes to set the canvas and hist style
337     void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox, TH1* thisHist, int counter) {
338    
339    
340     if(thisCanvas->GetRightMargin() < .2) thisCanvas->SetRightMargin(.2);
341    
342     thisStatsBox->SetLineColor(thisHist->GetLineColor());
343     thisStatsBox->SetX2NDC(1.);
344     thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
345     thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
346     thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
347    
348    
349    
350     }
351    
352     void PlotTool::setMathLabels(TH1* thisHist) {
353    
354    
355     string t = thisHist ->GetTitle();
356     string x = thisHist->GetXaxis()->GetTitle();
357     string y = thisHist->GetYaxis()->GetTitle();
358    
359     if(t.find(".phi()") != string::npos ) t.replace(t.find(".phi()"),6," #phi");
360     if(x.find(".phi()") != string::npos ) x.replace(x.find(".phi()"),6," #phi");
361     if(y.find(".phi()") != string::npos ) y.replace(y.find(".phi()"),6," #phi");
362    
363     if(t.find(".eta()") != string::npos ) t.replace(t.find(".eta()"),6," #eta");
364     if(x.find(".eta()") != string::npos ) x.replace(x.find(".eta()"),6," #eta");
365     if(y.find(".eta()") != string::npos ) y.replace(y.find(".eta()"),6," #eta");
366    
367     if(t.find(".pt()") != string::npos ) t.replace(t.find(".pt()"),5," p_{T}");
368     if(x.find(".pt()") != string::npos ) x.replace(x.find(".pt()"),5," p_{T}");
369     if(y.find(".pt()") != string::npos ) y.replace(y.find(".pt()"),5," p_{T}");
370    
371     thisHist ->SetTitle(t.c_str());
372     thisHist->GetXaxis()->SetTitle(x.c_str());
373     thisHist->GetYaxis()->SetTitle(y.c_str());
374    
375    
376     }
377    
378    
379     void PlotTool::setColor(TH1* thisHist, int counter) {
380    
381    
382     if(counter == 0) {
383    
384     thisHist->SetLineColor (kRed+1);
385     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
386     else thisHist->SetFillStyle(0);
387     thisHist->SetMarkerColor(kRed+1);
388     } else if(counter == 1) {
389     thisHist->SetLineColor (kBlue+1);
390     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
391     else thisHist->SetFillStyle(0);
392     thisHist->SetMarkerColor(kBlue+1);
393    
394     } else if(counter == 2) {
395     thisHist->SetLineColor (kGreen+2);
396     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
397     else thisHist->SetFillStyle(0);
398     thisHist->SetMarkerColor(kGreen+2);
399    
400     } else if(counter == 3) {
401     thisHist->SetLineColor (kMagenta+2);
402     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
403     else thisHist->SetFillStyle(0);
404     thisHist->SetMarkerColor(kMagenta+2);
405    
406     } else if(counter == 4) {
407     thisHist->SetLineColor (kCyan+2);
408     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
409     else thisHist->SetFillStyle(0);
410     thisHist->SetMarkerColor(kCyan+2);
411    
412     } else {
413     thisHist->SetLineColor (kBlack);
414     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
415     else thisHist->SetFillStyle(0);
416     thisHist->SetMarkerColor(kBlack);
417    
418     }
419    
420     }
421    
422     void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
423    
424    
425     palette->SetLabelSize(0.045);
426     if(thisCanvas->GetRightMargin() < .15) thisCanvas->SetRightMargin(.15);
427     palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
428     palette->SetY1NDC(thisCanvas->GetBottomMargin());
429     palette->SetX2NDC(palette->GetX1NDC()+0.05);
430     palette->SetY2NDC(1-thisCanvas->GetTopMargin());
431    
432    
433     }
434    
435     void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
436    
437     TH1* thisHist = 0;
438    
439     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
440    
441     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") ) continue;
442     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
443     if( thisHist->GetMaximum() < maxEntry ) thisHist->GetYaxis()->SetRangeUser(thisHist->GetMinimum(),maxEntry*1.2);
444     break;
445    
446     }
447    
448    
449     }
450    
451     void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
452    
453     thisLeg->SetFillStyle(0);
454     thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
455     thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
456     thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
457     thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
458     thisLeg->SetEntrySeparation(0.5);
459     thisLeg->Draw("NDC");
460    
461     }
462    
463     void PlotTool::createColors()
464     {
465     const Int_t NRGBs = 5;
466     const Int_t NCont = 255;
467    
468     Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
469     Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
470     Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
471     Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
472     TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
473     gStyle->SetNumberContours(NCont);
474    
475     }
476    
477    
478     //------------------------------------------------------------------
479    
480     int PlotTool::saveCanvases(string type, string path) {
481    
482    
483     TSystemDirectory d("",path.c_str());
484     if(!d.GetListOfFiles()) return -1;
485     if(type.find(".") == string::npos) return -1;
486    
487     int savedFiles =0;
488    
489     TIter next(gROOT->GetListOfCanvases());
490     TCanvas* canv;
491     while ((canv=(TCanvas*)next())) {
492    
493     string s = "";
494     s += path;
495     s += canv->GetName();
496     s += type;
497    
498     canv->SaveAs(s.c_str());
499     ++savedFiles;
500    
501     }
502    
503     return savedFiles;
504    
505     }
506     //------------------------------------------------------------------