Physics selection QA added in the QA framework
[u/mrichter/AliRoot.git] / PWGPP / EVS / periodLevelQA.C
1 #include "TFile.h"
2 #include "TTree.h"
3 #include "TH1D.h"
4 #include "TCanvas.h"
5 #include "TStyle.h"
6 #include "map"
7 using namespace std;
8 // TODO read number of bits from AliVEvent?
9 #define NBITS 29
10 TString bitNames[NBITS] = {
11 "kMB",
12 "kINT7",
13 "kMUON",
14 "kHighMult",
15 "kEMC1",
16 "kCINT5",
17 "kCMUS5",
18 "kMUSH7",
19 "kMUL7",
20 "kMUU7",
21 "kEMC7",
22 "kMUS7",
23 "kPHI1",
24 "kPHI7/kPHI8",
25 "kEMCEJE",
26 "kEMCEGA",
27 "kCentral",
28 "kSemiCentral",
29 "kDG5",
30 "kZED",
31 "kSPI7/kSPI8",
32 "kINT8",
33 "kMuonSingleLowPt8",
34 "kMuonSingleHighPt8",
35 "kMuonLikeLowPt8",
36 "kMuonUnlikeLowPt8",
37 "kMuonUnlikeLowPt0",
38 "kUserDefined",
39 "kTRD"
40 };
41
42 void SetHisto(TH1D* h);
43 void AddFillSeparationLines(TH1D* h, map<Int_t,Int_t> &fills);
44
45 void periodLevelQA(TString inputFileName ="trending.root"){
46   TFile* f = new TFile(inputFileName.Data());
47   TTree* t = (TTree*) f->Get("trending");
48   Int_t run;
49   Int_t fill = 0;
50   Int_t nBCsPerOrbit = 0;
51   Double_t duration = 0;
52   Double_t mu = 0;
53   Double_t lumi_seen = 0;
54   UInt_t l0b = 0;
55   Int_t all[NBITS];
56   Int_t accepted[NBITS];
57   
58   t->SetBranchAddress("run",&run);
59   t->SetBranchAddress("fill",&fill);
60   t->SetBranchAddress("bcs",&nBCsPerOrbit);
61   t->SetBranchAddress("duration",&duration);
62   t->SetBranchAddress("mu",&mu);
63   t->SetBranchAddress("l0b",&l0b);
64   t->SetBranchAddress("all",&all);
65   t->SetBranchAddress("accepted",&accepted);
66   t->SetBranchAddress("lumi_seen",&lumi_seen);
67   Int_t nRuns = t->GetEntries();
68   map<Int_t,Int_t> fills;
69   TH1D* hAll[NBITS];
70   TH1D* hAccepted[NBITS] = {0x0};
71   TH1D* hRejected[NBITS] = {0x0};
72   TH1D* hAcceptedFraction[NBITS] = {0x0};
73   TH1D* hRejectedFraction[NBITS] = {0x0};
74   
75   gStyle->SetOptStat(0);
76   gStyle->SetLineScalePS(1.5);
77   gStyle->SetPadGridX(0);
78   gStyle->SetPadGridY(1);
79   TCanvas* dummy = new TCanvas("dummy","dummy",1800,500);
80   gPad->SetMargin(0.05,0.01,0.18,0.06);
81   gPad->Print("global_properties.pdf[");
82   gPad->Print("accepted_event_statistics.pdf[");
83   gPad->Print("accepted_fraction.pdf[");
84   gPad->Print("rejected_fraction.pdf[");
85
86   TH1D* hMu       = new TH1D("hMu","Average number of minimum bias collisions per BC",nRuns,0,nRuns);
87   TH1D* hBCs      = new TH1D("hBCs","Number of colliding bunches",nRuns,0,nRuns);
88   TH1D* hDuration = new TH1D("hDuration","Duration, s",nRuns,0,nRuns);
89   TH1D* hLumiSeen = new TH1D("hLumiSeen","Luminosity seen, nb-1",nRuns,0,nRuns);
90
91   for (Int_t r=0;r<nRuns;r++){
92     t->GetEntry(r);
93     hMu->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
94     hBCs->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
95     hDuration->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
96     hLumiSeen->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
97     hMu->SetBinContent(r+1,mu);
98     hBCs->SetBinContent(r+1,nBCsPerOrbit);
99     hDuration->SetBinContent(r+1,duration);
100     hLumiSeen->SetBinContent(r+1,lumi_seen/1000000.);
101     fills[run]=fill;
102   }
103   SetHisto(hMu);
104   SetHisto(hBCs);
105   SetHisto(hDuration);
106   SetHisto(hLumiSeen);
107   TCanvas* cMu = new TCanvas("mu","mu",1800,500);
108   cMu->SetMargin(0.05,0.01,0.18,0.06);
109   hMu->Draw("h");
110   AddFillSeparationLines(hMu,fills);
111   gPad->Print("global_properties.pdf");
112
113   TCanvas* cBCs = new TCanvas("bcs","bcs",1800,500);
114   cBCs->SetMargin(0.05,0.01,0.18,0.06);
115   hBCs->Draw("h");
116   AddFillSeparationLines(hBCs,fills);
117   gPad->Print("global_properties.pdf");
118   
119   TCanvas* cDuration = new TCanvas("duration","duration",1800,500);
120   cDuration->SetMargin(0.05,0.01,0.18,0.06);
121   hDuration->SetTitle(Form("Duration in seconds: total= %.0f s = %.0f h",hDuration->Integral(),hDuration->Integral()/3600));
122   hDuration->Draw("h");
123   AddFillSeparationLines(hDuration,fills);
124   gPad->Print("global_properties.pdf");
125
126   TCanvas* cLumiSeen = new TCanvas("lumiseen","lumi seen",1800,500);
127   cLumiSeen->SetMargin(0.05,0.01,0.18,0.06);
128   hLumiSeen->SetTitle(Form("Luminosity seen [1/nb]: total= %.0f",hLumiSeen->Integral()));
129   hLumiSeen->Draw("h");
130   AddFillSeparationLines(hLumiSeen,fills);
131   gPad->Print("global_properties.pdf");
132
133
134   dummy->Print("global_properties.pdf]");
135
136   for (Int_t ibit=0;ibit<NBITS;ibit++) {
137     printf("bit=%i\n",ibit);
138     const char* bitName = bitNames[ibit];
139     hAll[ibit]      = new TH1D(Form("hAll%02i"     ,ibit),Form("All: %s"     ,bitName),nRuns,0,nRuns);
140     hAccepted[ibit] = new TH1D(Form("hAccepted%02i",ibit),Form("Accepted: %s",bitName),nRuns,0,nRuns);
141     hRejected[ibit] = new TH1D(Form("hRejected%02i",ibit),Form("Rejected: %s",bitName),nRuns,0,nRuns);
142     for (Int_t r=0;r<nRuns;r++){
143       t->GetEntry(r);
144       hAll[ibit]     ->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
145       hAccepted[ibit]->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
146       hRejected[ibit]->GetXaxis()->SetBinLabel(r+1,Form("%i",run));
147       hAll[ibit]->SetBinContent(r+1,all[ibit]);
148       if (all[ibit]) hAccepted[ibit]->SetBinContent(r+1,accepted[ibit]);
149       if (all[ibit]) hRejected[ibit]->SetBinContent(r+1,all[ibit]-accepted[ibit]);
150     }
151     if (hAll[ibit]->Integral()<1) continue;
152     SetHisto(hAll[ibit]);
153     SetHisto(hAccepted[ibit]);
154     SetHisto(hRejected[ibit]);
155     hAll[ibit]->Sumw2();
156     hAccepted[ibit]->Sumw2();
157     hRejected[ibit]->Sumw2();
158     hAll[ibit]->SetLineColor(kBlue);
159     hAll[ibit]->SetFillColor(kBlue);
160     hAccepted[ibit]->SetLineColor(kGreen+2);
161     hAccepted[ibit]->SetFillColor(kGreen+2);
162     hAcceptedFraction[ibit] = (TH1D*) hAll[ibit]->Clone(Form("hAcceptedFraction%02i",ibit));
163     hAcceptedFraction[ibit]->SetTitle(Form("Accepted fraction: %s",bitName));
164     hAcceptedFraction[ibit]->Divide(hAccepted[ibit],hAll[ibit],1,1,"B");
165     hAcceptedFraction[ibit]->SetFillColor(0);
166     hAcceptedFraction[ibit]->SetLineWidth(2);
167     hRejectedFraction[ibit] = (TH1D*) hAll[ibit]->Clone(Form("hRejectedFraction%02i",ibit));
168     hRejectedFraction[ibit]->SetTitle(Form("Rejected fraction: %s",bitName));
169     hRejectedFraction[ibit]->Divide(hRejected[ibit],hAll[ibit],1,1,"B");
170     hRejectedFraction[ibit]->SetFillColor(0);
171     hRejectedFraction[ibit]->SetLineWidth(2);
172     
173     TCanvas* cAll = new TCanvas(Form("all_%s",bitName),Form("All: %s",bitName),1800,500);
174     cAll->SetMargin(0.05,0.01,0.18,0.06);
175     hAll[ibit]->SetTitle(Form("%s: total=%.0f accepted=%.0f",bitName,hAll[ibit]->Integral(),hAccepted[ibit]->Integral()));
176     hAll[ibit]->Draw("h");
177     hAccepted[ibit]->Draw("h same");
178     AddFillSeparationLines(hAccepted[ibit],fills);
179     gPad->RedrawAxis();
180     gPad->Print("accepted_event_statistics.pdf");
181     
182     TCanvas* cAcceptedFraction = new TCanvas(Form("accepted_fraction_%s",bitName),Form("Accepted Fraction: %s",bitName),1800,500);
183     cAcceptedFraction->SetMargin(0.05,0.01,0.18,0.06);
184     hAcceptedFraction[ibit]->SetTitle(Form("%s: average accepted fraction = %.3f",bitName,hAccepted[ibit]->Integral()/hAll[ibit]->Integral()));
185     hAcceptedFraction[ibit]->Draw();
186     AddFillSeparationLines(hAcceptedFraction[ibit],fills);
187     gPad->Print("accepted_fraction.pdf");
188     
189     TCanvas* cRejectedFraction = new TCanvas(Form("rejected_fraction_%s",bitName),Form("Rejected Fraction: %s",bitName),1800,500);
190     cRejectedFraction->SetMargin(0.05,0.01,0.18,0.06);
191     hRejectedFraction[ibit]->SetTitle(Form("%s: average rejected fraction = %.3f",bitName,hRejected[ibit]->Integral()/hAll[ibit]->Integral()));
192     hRejectedFraction[ibit]->Draw();
193     AddFillSeparationLines(hRejectedFraction[ibit],fills);
194     gPad->Print("rejected_fraction.pdf");
195
196   }
197   dummy->Print("accepted_event_statistics.pdf]");
198   dummy->Print("accepted_fraction.pdf]");
199   dummy->Print("rejected_fraction.pdf]");
200
201   TFile* fout = new TFile("hist.root","recreate");
202   for (Int_t ibit=0;ibit<NBITS;ibit++) {
203     if (hAll[ibit]) hAll[ibit]->Write();
204     if (hAccepted[ibit]) hAccepted[ibit]->Write();
205     if (hAcceptedFraction[ibit]) hAcceptedFraction[ibit]->Write();
206     if (hRejectedFraction[ibit]) hRejectedFraction[ibit]->Write();
207   }
208   fout->Close();
209 }
210
211 void SetHisto(TH1D* h){
212   h->SetTitleFont(43);
213   h->SetTitleSize(25);
214   h->GetYaxis()->SetTitleFont(43);
215   h->GetXaxis()->SetLabelFont(43);
216   h->GetYaxis()->SetLabelFont(43);
217   h->GetYaxis()->SetTitleSize(25);
218   h->GetXaxis()->SetLabelSize(15);
219   h->GetYaxis()->SetLabelSize(25);
220   h->GetYaxis()->SetTickLength(0.01);
221   h->GetYaxis()->SetTitleOffset(0.6);
222   h->GetYaxis()->SetDecimals(1);
223   h->LabelsOption("av");
224   h->SetLineWidth(2);
225   h->SetLineColor(kBlue);
226   h->SetMinimum(0);
227 }
228
229 void AddFillSeparationLines(TH1D* h, map<Int_t,Int_t> &fills){
230   gPad->Update();
231   Double_t ymin = gPad->GetUymin();
232   Double_t ymax = gPad->GetUymax();
233   TLine * fillSeparationLine = new TLine();
234   fillSeparationLine->SetLineColor(kRed);
235   fillSeparationLine->SetLineWidth(1);
236   for(Int_t iBin = 1; iBin < h->GetXaxis()->GetNbins(); iBin++) {
237     UInt_t runOld = atoi(h->GetXaxis()->GetBinLabel(iBin));
238     UInt_t runNew = atoi(h->GetXaxis()->GetBinLabel(iBin + 1));
239     if (fills[runOld]==fills[runNew]) continue;
240     fillSeparationLine->DrawLine(iBin,ymin,iBin,ymax);
241   }
242 }