Major overhaul of the QA code.
[u/mrichter/AliRoot.git] / PWG2 / FORWARD / analysis2 / qa / DrawSteps.C
CommitLineData
56199f2b 1/**
2dbde04b 2 * @file DrawSteps.C
3 * @author Christian Holm Christensen <cholm@nbi.dk>
4 * @date Thu Nov 17 11:34:01 2011
56199f2b 5 *
2dbde04b 6 * @brief
56199f2b 7 *
8 *
2dbde04b 9 * @defgroup pwg2_forward_scripts_qa Quality Assurance scripts
f0ef1e71 10 * @ingroup pwg2_forward_scripts
56199f2b 11 */
56199f2b 12/**
13 * Get a stack
14 *
15 * @param forward Input list
16 * @param sub Sub-list
17 * @param name Name of stack
18 *
19 * @return A stack or null
20 *
f0ef1e71 21 * @ingroup pwg2_forward_scripts_qa
56199f2b 22 */
797161e8 23THStack*
24GetStack(const TList& forward, const char* sub, const char* name)
25{
26 TList* lsub = &forward;
27
28 if (sub && sub[0] != '\0')
29 lsub = static_cast<TList*>(forward.FindObject(sub));
30
31 if (!lsub) {
32 Warning("GetStack", "Sub list %s not found in %s", sub, forward.GetName());
33 return 0;
34 }
35 THStack* ret = static_cast<THStack*>(lsub->FindObject(name));
36 if (!ret)
37 Warning("GetStack" "Stack %s not found in %s", name, sub);
38 return ret;
39}
40
56199f2b 41/**
42 * Rebin a histogram
43 *
44 * @param h Histogram
45 * @param rebin Rebinning factor
46 *
47 * @return Histogram
48 *
f0ef1e71 49 * @ingroup pwg2_forward_scripts_qa
56199f2b 50 */
797161e8 51TH1*
52Rebin(TH1* h, Int_t rebin)
53{
54 if (rebin <= 1) return h;
55 h->Rebin(rebin);
56 h->Scale(1. / rebin);
57 return h;
58}
59
56199f2b 60/**
61 * Ratio of two histograms
62 *
63 * @param h1 numerator
64 * @param h2 denominator
65 *
66 * @return Ratio
67 *
f0ef1e71 68 * @ingroup pwg2_forward_scripts_qa
56199f2b 69 */
797161e8 70TH1*
71Ratio(const TH1* h1, const TH1* h2)
72{
73 if (!h1) return;
74 if (!h2) return;
75
76 TH1* copy = static_cast<TH1*>(h2->Clone("tmp"));
77 copy->SetName(Form("%s_%s", h2->GetName(), h1->GetName()));
78 copy->SetTitle(Form("%s/%s", h2->GetTitle(), h1->GetTitle()));
79 copy->SetDirectory(0);
80 copy->Divide(h1);
81
82 return copy;
83}
84
56199f2b 85/**
86 * Ratio all histograms in stacks
87 *
88 * @param r Result
89 * @param h1 Numerators
90 * @param h2 Denominators
91 *
92 * @return Number of histograms
93 *
f0ef1e71 94 * @ingroup pwg2_forward_scripts_qa
56199f2b 95*/
797161e8 96Int_t
97Ratio(THStack* r, const THStack* h1, const THStack* h2)
98{
99 if (!h1) return 0;
100 if (!h2) return 0;
101
102 int n1 = h1->GetHists()->GetEntries();
103 int n2 = h2->GetHists()->GetEntries();
104 int nH = 0;
105 for (int i = 0; i < n1 && i < n2; i++) {
106 TH1* hh1 = static_cast<TH1*>(h1->GetHists()->At(i));
107 TH1* hh2 = static_cast<TH1*>(h2->GetHists()->At(i));
108 TH1* h = Ratio(hh1, hh2);
109 if (!h) continue;
110 nH++;
111 r->Add(h);
112 }
113 return nH;
114}
115
56199f2b 116/**
117 * Add a histogram to the all stack
118 *
119 * @param all Stack
120 * @param h Histogram
121 * @param singleStep Showing individual steps?
122 *
f0ef1e71 123 * @ingroup pwg2_forward_scripts_qa
56199f2b 124*/
797161e8 125void
126AddToAll(THStack* all, const TH1* h, Bool_t singleStep)
127{
128 TH1* copy = static_cast<TH1*>(h->Clone(Form("%s_copy", h->GetName())));
129 copy->SetDirectory(0);
130 if (singleStep) {
131 copy->SetMarkerColor(kGray);
132 copy->SetLineColor(kGray);
133 }
134 all->Add(copy);
135}
136
56199f2b 137/**
138 * Dim an entry
139 *
140 * @param thisId This step
141 * @param step Current step
142 * @param e Entry in legend
143 *
f0ef1e71 144 * @ingroup pwg2_forward_scripts_qa
56199f2b 145 */
797161e8 146void
147DimEntry(Int_t thisId, Int_t step, TLegendEntry* e)
148{
149
150 Int_t col = (thisId == step || step <= 0) ? kBlack : kGray;
151 e->SetMarkerColor(col);
152 e->SetLineColor(col);
153 e->SetTextColor(col);
154}
155
56199f2b 156/**
157 * Draw a step
158 *
159 * @param deltas From energy loss
160 * @param nchs After 2nd correction
161 * @param prims Primaries
162 * @param dndeta Result
163 * @param step Step number
164 *
f0ef1e71 165 * @ingroup pwg2_forward_scripts_qa
56199f2b 166 */
797161e8 167void
168DrawStep(THStack* deltas, THStack* nchs, THStack* prims,
169 TH1* dndeta, Int_t step)
170{
171 THStack* all = new THStack("all", "Analysis steps");
172 if (step > 0) all->SetTitle(Form("Step %d", step));
173
174 if (deltas) {
175 deltas->SetTitle("#sum_{} #Delta/#Delta_{mip}");
176 TIter next(deltas->GetHists());
177 TH1* h = 0;
178 while ((h = static_cast<TH1*>(next()))) {
179 h->SetMarkerStyle(25);
180 // Info("DrawStep", "Adding %s", h->GetName());
181 AddToAll(all, h, step>0);
182 }
183 }
184 if (nchs) {
185 nchs->SetTitle("#sum_{} N_{ch,incl}");
186 TIter next(nchs->GetHists());
187 TH1* h = 0;
188 while ((h = static_cast<TH1*>(next()))) {
189 h->SetMarkerStyle(21);
190 // Info("DrawStep", "Adding %s", h->GetName());
191 AddToAll(all, h, step>0);
192 }
193 }
194 if (prims) {
195 prims->SetTitle("#sum_{} N_{ch,primary}");
196 TIter next(prims->GetHists());
197 TH1* h = 0;
198 while ((h = static_cast<TH1*>(next()))) {
199 h->SetMarkerStyle(22);
200 // Info("DrawStep", "Adding %s", h->GetName());
201 AddToAll(all, h, step>0);
202 }
203 }
204 if (dndeta) {
205 dndeta->SetTitle("1/N dN_{ch}/d#eta");
206 dndeta->SetMarkerStyle(20);
207 dndeta->SetMarkerColor(kBlack);
208 // Info("DrawStep", "Adding %s", dndeta->GetName());
209 AddToAll(all, dndeta, step>0);
210 }
211
212 all->Draw("nostack");
213 all->GetHistogram()->SetXTitle("#eta");
214 all->GetHistogram()->SetYTitle("signal");
215 all->GetHistogram()->GetXaxis()->SetLabelFont(132);
216 all->GetHistogram()->GetXaxis()->SetTitleFont(132);
217 all->GetHistogram()->GetYaxis()->SetLabelFont(132);
218 all->GetHistogram()->GetYaxis()->SetTitleFont(132);
219 c->SetGridx();
220
221 TLegend* l = new TLegend(.33, .2, .53, .9);
222 TLegendEntry* e = 0;
223 l->SetFillColor(0);
224 l->SetFillStyle(0);
225 l->SetBorderSize(0);
226 l->SetNColumns(1);
227 l->SetTextFont(132);
228 Int_t i = 0;
229 if (deltas) {
230 TIter next(deltas->GetHists());
231 TH1* h = 0;
232 while ((h = static_cast<TH1*>(next()))) {
233 e = l->AddEntry(Form("dummy%02d", i++),h->GetTitle(),"pl");
234 e->SetMarkerStyle(20);
235 e->SetMarkerColor(h->GetMarkerColor());
236 }
237 e = l->AddEntry(Form("dummy%02d", i++), deltas->GetTitle(),"pl");
238 TH1* h = static_cast<TH1*>(deltas->GetHists()->At(0));
239 e->SetMarkerStyle(h->GetMarkerStyle());
240 DimEntry(1, step, e);
241 }
242 if (nchs) {
243 e = l->AddEntry(Form("dummy%02d",i++),nchs->GetTitle(),"pl");
244 TH1* h = static_cast<TH1*>(nchs->GetHists()->At(0));
245 e->SetMarkerStyle(h->GetMarkerStyle());
246 DimEntry(2, step, e);
247 }
248 if (prims) {
249 e = l->AddEntry(Form("dummy%02d", i++), prims->GetTitle(),"pl");
250 TH1* h = static_cast<TH1*>(prims->GetHists()->At(0));
251 e->SetMarkerStyle(h->GetMarkerStyle());
252 DimEntry(3, step, e);
253 }
254 if (dndeta) {
255 e = l->AddEntry(Form("dummy%02d", i++), dndeta->GetTitle(),"pl");
256 e->SetMarkerStyle(dndeta->GetMarkerStyle());
257 DimEntry(4, step, e);
258 }
259 l->Draw();
260
261 TString what;
262 if (step > 0) {
263 switch (step) {
264 case 1:
265 deltas->Draw("same nostack");
266 what = "After merging";
267 break;
268 case 2:
269 nchs->Draw("same nostack");
270 what = "After particle counting";
271 break;
272 case 3:
273 prims->Draw("same nostack");
274 what = "After corrections";
275 break;
276 case 4:
277 dndeta->Draw("same");
278 what = "After normalisation";
279 break;
280 default:
281 Error("DrawSteps", "Unknown step: %d (must be in 1-4)");
282 break;
283 }
284 }
285 TLatex* ltx = new TLatex(.95, .85, what);
286 ltx->SetNDC();
287 ltx->SetTextSize(.07);
288 ltx->SetTextAlign(33);
289 ltx->SetTextFont(132);
290 ltx->Draw();
291}
292
56199f2b 293/**
294 * Draw steps
295 *
296 * @param filename Input file
297 * @param single Whether to show individial steps
298 *
f0ef1e71 299 * @ingroup pwg2_forward_scripts_qa
56199f2b 300 */
b5c9a732 301void DrawSteps(const char* filename="forward.root", Bool_t single=true)
797161e8 302{
303 gStyle->SetPalette(1);
304 gStyle->SetOptFit(0);
305 gStyle->SetOptStat(0);
306
307 TFile* file = TFile::Open(filename, "READ");
308 if (!file) {
309 Error("DrawMCResult", "failed to open %s", filename);
310 return;
311 }
312 const char* fname2 = "forward_dndeta.root";
313 TFile* file2 = TFile::Open(fname2, "READ");
314 if (!file2) {
315 Error("DrawSteps", "File %s not found", fname2);
316 }
317
318 TList* forward = static_cast<TList*>(file->Get("Forward"));
319 if (!forward) {
320 Error("DrawMCResult", "List Forward not found in %s", filename);
321 return;
322 }
323 TList* forwardRes = (file2 ?
324 static_cast<TList*>(file2->Get("ForwardResults")) :
325 0);
326 TList* forwardAll = (forwardRes ?
327 static_cast<TList*>(forwardRes->FindObject("all")) :
328 0);
329
330
331 // THStack* res = GetStack(*forward, "ringResults", "all");
332 // THStack* mcRes = GetStack(*forward, "mcRingResults", "all");
333 THStack* deltas = GetStack(*forward, "fmdSharingFilter", "sums");
334 THStack* nchs = GetStack(*forward, "fmdDensityCalculator", "sums");
335 THStack* prims = GetStack(*forward, "fmdCorrector", "sums");
336 TH1* dndeta = (forwardAll ?
337 static_cast<TH1*>(forwardAll->FindObject("dndetaForward")):
338 0);
339
340 Info("DrawSteps", "Got steps deltas=%p, nchs=%p, prims=%p, dndeta=%p",
341 deltas, nchs, prims, dndeta);
342
343
344 gStyle->SetTitleBorderSize(0);
345 gStyle->SetTitleFillColor(0);
346 gStyle->SetTitleStyle(0);
347 gStyle->SetTitleX(.7);
348 gStyle->SetTitleY(.95);
349 gStyle->SetTitleH(.1);
350 gStyle->SetTitleW(.25);
b5c9a732 351 gStyle->SetOptTitle(1);
797161e8 352 // gStyle->SetTitleColor(kBlack);
353
354
355
356 if (!single) {
357 TCanvas* c = new TCanvas("c", "C", 900, 700);
358 c->SetFillColor(0);
359 c->SetBorderSize(0);
360 c->SetTopMargin(0.05);
361 c->SetRightMargin(0.05);
362
363 DrawStep(deltas, nchs, prims, dndeta, 0);
b5c9a732 364 c->SaveAs("steps_all.png");
797161e8 365 return;
366 }
367 Int_t nSteps = 0;
368 if (deltas) nSteps++;
369 if (nchs) nSteps++;
370 if (prims) nSteps++;
371 if (dndeta) nSteps++;
372
373 Int_t w = (nSteps >= 4 ? 1100 : 700);
374 Int_t h = (nSteps >= 4 ? 800 : 1100);
375
376 TCanvas* c = new TCanvas("c", "C", w, h);
377 c->SetFillColor(0);
378 c->SetBorderSize(0);
379 c->SetTopMargin(0.05);
380 c->SetRightMargin(0.05);
381
382 if (nSteps >= 4)
383 c->Divide(2,(nSteps+1)/2,0,0);
384 else
385 c->Divide(1,nSteps,0,0);
386
387 for (Int_t i=1; i<=nSteps; i++) {
388 TVirtualPad* p = c->cd(i);
389 p->SetFillColor(0);
390 p->SetFillStyle(0);
391 p->SetBorderSize(0);
392 p->SetGridx();
393 p->SetGridy();
394
395 DrawStep(deltas, nchs, prims, dndeta, i);
396 }
b5c9a732 397 c->SaveAs("steps_comic.png");
797161e8 398}
b5c9a732 399//
400// EOF
401//
797161e8 402
403