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