]>
Commit | Line | Data |
---|---|---|
e84c79f2 | 1 | // $Id$ |
1cea10fd | 2 | // |
3 | // Macro to overlay the histograms produced by | |
4 | // HLT/QA/tasks/macros/drawTHnSparse.C | |
5 | // | |
6 | // It assumes a txt file where the input is specified in | |
7 | // the following format: | |
8 | // | |
9 | // number of files | |
10 | // file1 legend1 | |
11 | // file2 legend2 | |
12 | // //file3 legend3 | |
13 | // //file4 legend4 | |
14 | // ... | |
15 | // So it is possible to "comment out" a file by a // in the beginning of the name. While reading the | |
16 | // the names of the input files, the macro skips the ones that have the // in front of them. | |
17 | // | |
18 | // @ingroup alihlt_qa | |
19 | // @author Kalliopi.Kanaki@ift.uib.no | |
20 | ||
a08d87a7 | 21 | |
22 | #if !defined(__CINT__) || defined(__MAKECINT__) | |
23 | #include "TSystem.h" | |
24 | #include "TROOT.h" | |
25 | #include "TFile.h" | |
26 | #include "TString.h" | |
27 | #include "TList.h" | |
28 | #include "THnSparse.h" | |
29 | #include "TCanvas.h" | |
30 | #include "TText.h" | |
31 | #include "TPaveText.h" | |
32 | #include "TPaveStats.h" | |
33 | #include "TH1D.h" | |
34 | #include "TH2D.h" | |
35 | #include "TH3D.h" | |
36 | #include "TLegend.h" | |
37 | #include "TStyle.h" | |
38 | #include "TPad.h" | |
39 | #include <iostream> | |
40 | #include <cstdlib> | |
41 | #include <fstream> | |
42 | using std::endl; | |
43 | #endif | |
44 | ||
45 | void printStats(TH1D *h1, TH1D *h2); | |
46 | void defineYaxisMax(TH1D *h1, TH1D *h2); | |
47 | ||
48 | void overlayPlots(const char* option="HLT"/* or "OFF" */, Bool_t bAddRunName=kTRUE, Bool_t bDrawNormalized=kTRUE, string fi="files.txt"){ | |
49 | ||
50 | ||
51 | printf("test\n"); | |
e84c79f2 | 52 | gROOT->SetStyle("Plain"); |
53 | gStyle->SetPalette(1); | |
1cea10fd | 54 | gStyle->SetOptStat("emr"); |
38faad18 | 55 | gStyle->SetTitleX(gStyle->GetPadLeftMargin()); |
56 | ||
e84c79f2 | 57 | char filenames[100]; |
58 | sprintf(filenames,"%s",fi.c_str()); | |
a08d87a7 | 59 | ifstream infile; |
60 | infile.open(filenames); | |
61 | if(!infile){ | |
62 | printf("File %s does not exist", fi.data()); | |
63 | return; | |
e84c79f2 | 64 | } |
65 | string c; | |
66 | TString f; | |
67 | int nr_textfile = 0; | |
68 | ||
a08d87a7 | 69 | infile>>nr_textfile; |
70 | if(!infile.good()) return; | |
38faad18 | 71 | printf("Number of files: %d\n", nr_textfile); |
e84c79f2 | 72 | |
73 | const int nr_files = nr_textfile; | |
74 | TString file[nr_files]; | |
75 | string cutnames[nr_files]; | |
a08d87a7 | 76 | string plotname=""; |
e84c79f2 | 77 | |
78 | nr_textfile=0; | |
79 | while(nr_textfile < nr_files){ | |
a08d87a7 | 80 | infile >> f >> c; |
81 | if(!infile.good()) break; | |
e84c79f2 | 82 | file[nr_textfile] = f; |
83 | cutnames[nr_textfile] = c; | |
1cea10fd | 84 | if(f.BeginsWith("//")) continue; |
85 | printf("\nfile %d : %s\n", nr_textfile, f.Data()); | |
e84c79f2 | 86 | nr_textfile++; |
e84c79f2 | 87 | } |
a08d87a7 | 88 | infile.close(); |
e84c79f2 | 89 | |
38faad18 | 90 | TCanvas *ca; |
91 | TFile *ff; | |
92 | TPad *pad; | |
e84c79f2 | 93 | TH1D *g[nr_files]; |
94 | ||
a08d87a7 | 95 | TCanvas *d = new TCanvas("d",Form("Compare %s distributions",option),1600,1000); |
54821965 | 96 | d->Divide(4,2); |
38faad18 | 97 | //d->Divide(3,2); |
e84c79f2 | 98 | |
a08d87a7 | 99 | TLegend *l = new TLegend(0.25,0.2,0.89,0.8); |
e84c79f2 | 100 | l->SetFillColor(10); |
101 | l->SetLineColor(10); | |
102 | ||
103 | char cut[100]; | |
104 | ||
38faad18 | 105 | //for(int j=1; j<7; j++){ |
54821965 | 106 | for(int j=1; j<9; j++){ |
e84c79f2 | 107 | for(int i=0; i<nr_files; i++){ |
108 | ||
38faad18 | 109 | ff = TFile::Open(file[i].Data()); |
110 | if(!ff || ff->IsZombie()){ | |
e84c79f2 | 111 | printf("Non-existent, corrupted or zombie file %s\n", file[i].Data()); |
112 | return; | |
113 | } | |
38faad18 | 114 | //ca = (TCanvas*)ff->GetObjectUnchecked("can0"); |
115 | ca = (TCanvas*)ff->GetObjectUnchecked("can3"); | |
116 | if(!ca){ | |
e84c79f2 | 117 | printf("Empty canvas in file %s.\n", file[i].Data()); |
118 | continue; | |
38faad18 | 119 | } |
120 | //pad = (TPad*)ca->GetListOfPrimitives()->FindObject(Form("can0_%d",j)); | |
121 | pad = (TPad*)ca->GetListOfPrimitives()->FindObject(Form("can3_%d",j)); | |
122 | if(!pad){ | |
123 | printf("Empty pad in canvas %s.\n", ca->GetName()); | |
e84c79f2 | 124 | continue; |
125 | } | |
38faad18 | 126 | //g[i] =(TH1D*)pad->FindObject(Form("fEvent%s_proj_%d",option,j-1)); |
127 | g[i] =(TH1D*)pad->FindObject(Form("fTrack%s_proj_%d",option,j-1)); | |
e84c79f2 | 128 | if(!g[i]){ |
129 | printf("Empty histogram for i=%d, file %s.\n", i, file[i].Data()); | |
130 | continue; | |
131 | } | |
132 | ||
133 | d->cd(j); | |
1cea10fd | 134 | |
e9c1a43b | 135 | if(i==0){ |
a08d87a7 | 136 | g[i]->SetLineColor(kBlack); |
137 | if(bDrawNormalized) | |
138 | g[i]->DrawNormalized(); | |
139 | else | |
140 | g[i]->Draw(); | |
1cea10fd | 141 | if(option=="OFF"){ |
142 | TPaveStats *st = (TPaveStats*)g[i]->FindObject("stats"); | |
143 | st->SetTextColor(kBlack); | |
144 | d->Update(); | |
145 | } | |
e9c1a43b | 146 | } |
e84c79f2 | 147 | else { |
a08d87a7 | 148 | if(i<4) |
149 | g[i]->SetLineColor(i+1); | |
150 | else | |
151 | g[i]->SetLineColor(i+2); | |
e84c79f2 | 152 | defineYaxisMax(g[0], g[i]); |
a08d87a7 | 153 | if(bDrawNormalized) g[i]->DrawNormalized("sames"); |
154 | else g[i]->Draw("sames"); | |
155 | ||
156 | } | |
157 | if(!bDrawNormalized) | |
158 | if(i>0) printStats(g[i-1], g[i]); | |
159 | ||
160 | ff->Close(); | |
e84c79f2 | 161 | sprintf( cut,"%s",cutnames[i].c_str() ); |
a08d87a7 | 162 | if(j==1) { |
163 | l->AddEntry(g[i],cut,"l"); | |
164 | cutnames[i].resize(6); | |
165 | plotname+="_"+cutnames[i]; | |
166 | } | |
e84c79f2 | 167 | else continue; |
168 | } | |
169 | if(j==1) l->Draw("same"); | |
a08d87a7 | 170 | d->Update(); |
e84c79f2 | 171 | } |
a08d87a7 | 172 | |
173 | d->Update(); | |
174 | ||
175 | sprintf(filenames,"%s",plotname.c_str()); | |
176 | if( bAddRunName){ | |
177 | d->SaveAs(Form("overlay_%s_for%s.root",option,plotname)); | |
178 | d->Print(Form("overlay_%s_for%s.png",option,plotname)); | |
179 | } | |
180 | else{ | |
181 | d->SaveAs(Form("overlay_%s.root",option)); | |
182 | d->Print(Form("overlay_%s.png",option)); | |
183 | } | |
184 | ||
38faad18 | 185 | return; |
e84c79f2 | 186 | } |
187 | ||
188 | void printStats(TH1D *h1, TH1D *h2){ | |
189 | gPad->Update(); | |
190 | TPaveStats *st1 = (TPaveStats*)h1->FindObject("stats"); | |
191 | st1->SetLineColor(0); | |
192 | ||
193 | gPad->Update(); | |
194 | TPaveStats *st2 = (TPaveStats*)h2->FindObject("stats"); | |
195 | st2->SetY2NDC(st1->GetY1NDC()-0.05); | |
196 | st2->SetY1NDC(st2->GetY2NDC()-TMath::Abs(st1->GetY1NDC()-st1->GetY2NDC())); | |
197 | st2->SetLineColor(0); | |
198 | st2->SetTextColor(h2->GetLineColor()); | |
199 | st2->SetFillStyle(0); | |
200 | st2->Draw(); | |
38faad18 | 201 | return; |
e84c79f2 | 202 | } |
203 | ||
204 | void defineYaxisMax(TH1D *h1, TH1D *h2){ | |
205 | //Y axis | |
206 | if(h1->GetMaximum() > h2->GetMaximum()) h2->SetMaximum(1.1*h1->GetMaximum()); | |
207 | else h1->SetMaximum(1.1*h2->GetMaximum()); | |
208 | ||
209 | h1->SetMinimum(0); | |
210 | h2->SetMinimum(0); | |
211 | ||
212 | // X axis | |
213 | double xmin, xmax; | |
214 | if(h1->GetBinLowEdge(1) > h2->GetBinLowEdge(1)) xmin = h1->GetBinLowEdge(1); | |
215 | else xmin = h2->GetBinLowEdge(1); | |
216 | if(h1->GetBinLowEdge(h1->GetNbinsX()+1) > h2->GetBinLowEdge(h1->GetNbinsX()+1)) xmax = h1->GetBinLowEdge(h1->GetNbinsX()+1); | |
217 | else xmax = h2->GetBinLowEdge(h2->GetNbinsX()+1); | |
218 | ||
219 | h2->SetAxisRange(xmin, xmax, "X"); | |
38faad18 | 220 | return; |
e84c79f2 | 221 | } |