AliPhysics  b5b0183 (b5b0183)
DetailsCompare.C
Go to the documentation of this file.
1 
11 #ifndef __CINT__
12 #include <TH1.h>
13 #include <THStack.h>
14 #include <TCanvas.h>
15 #include <TList.h>
16 #include <TArrayI.h>
17 #include <TFile.h>
18 #include <TError.h>
19 #include <TLegend.h>
20 #include <TLegendEntry.h>
21 #include <TStyle.h>
22 #include <TLatex.h>
23 #include <TApplication.h>
24 #include <TSystem.h>
25 #include <TPaletteAxis.h>
26 #else
27 class TH1;
28 class TArrayI;
29 class TLegend;
30 class THStack;
31 class TCanvas;
32 class TList;
33 class TVirtualPad;
34 class TFile;
35 #endif
36 
37 //====================================================================
38 namespace {
42  struct SuppressGuard3
43  {
45  Int_t save = 0;
51  SuppressGuard3(Int_t lvl=2000)
52  {
53  save = gErrorIgnoreLevel;
54  gErrorIgnoreLevel = lvl;
55  }
59  ~SuppressGuard3()
60  {
61  gErrorIgnoreLevel = save;
62  }
63  };
64 }
65 
72 {
73  TCanvas* fCanvas;
74  TVirtualPad* fTop;
75  TVirtualPad* fBody;
76  TVirtualPad* fBottom;
77  TFile* fOutput;
78  TLegend* fLegend;
80 
81  void MakeCanvas()
82  {
83  fCanvas = new TCanvas("c","summary.pdf", 800, 1000);
84  fTop = new TPad("top", "",0,.9,1,1);
85  fBody = new TPad("body", "",0,.2,1,.9);
86  fBottom = new TPad("bottom","",0,0, 1,.2);
87  fTop->SetFillColor(kGray);
88  fBody->SetFillColor(0);
89  fBottom->SetFillColor(0);
90 
91  fCanvas->cd();
92  fTop->Draw();
93  fCanvas->cd();
94  fBody->Draw();
95  fCanvas->cd();
96  fBottom->Draw();
97  fCanvas->cd();
98  gSystem->Exec("rm -rf compare");
99  gSystem->Exec("mkdir -p compare");
100  SuppressGuard3 g;
101  fCanvas->SaveAs(Form("compare/%s[", fCanvas->GetTitle()), "PDF");
102 
103  fOutput = TFile::Open("compare/result.root", "RECREATE");
104  }
105  void ClearCanvas()
106  {
107  fTop->Clear();
108  fBody->Clear();
109  fBottom->Clear();
110  }
111  void PrintCanvas(const char* title, const char* shortTitle=0)
112  {
113  TLatex* ltx = new TLatex(.5,.5,title);
114  ltx->SetNDC();
115  ltx->SetTextSize(.6);
116  ltx->SetTextAlign(22);
117  fTop->cd();
118  ltx->Draw();
119 
120  SuppressGuard3 g;
121  fCanvas->Modified();
122  fCanvas->Update();
123  fCanvas->cd();
124  fCanvas->SaveAs(Form("compare/%s", fCanvas->GetTitle()),
125  Form("PDF title=%s", title));
126  if (shortTitle && shortTitle[0] != '\0')
127  fCanvas->SaveAs(Form("compare/%s.png", shortTitle));
128  fCanvas->WaitPrimitive();
129 
130  }
131  void CloseCanvas()
132  {
133  fCanvas->SaveAs(Form("compare/%s]", fCanvas->GetTitle()), "PDF");
134  fCanvas->Close();
135  fOutput->Write();
136  // fOutput->Close();
137  }
138  Color_t IndexColor(Int_t cnt)
139  {
140  static Color_t colors[] = { kRed+2,
141  kOrange+2,
142  kYellow+2,
143  kSpring+2,
144  kGreen+2,
145  kTeal+2,
146  kCyan+2,
147  kBlue+2,
148  kViolet+2,
149  kMagenta+2,
150  kPink+2,
151  kBlack };
152  return colors[(cnt/2+(cnt%2)*6) % 12];
153  }
154  void MakeTitlePage(const TList* files)
155  {
156  ClearCanvas();
157  fBody->cd();
158 
159  TLatex* l1 = new TLatex(.1,.95, "Files");
160  l1->SetTextFont(62);
161  l1->SetTextAlign(13);
162  l1->SetTextSize(0.06);
163  l1->SetNDC();
164  l1->Draw();
165 
166  TLatex* l2 = new TLatex(.15,.90,"");
167  l2->SetTextFont(82);
168  l2->SetTextAlign(13);
169  l2->SetTextSize(0.05);
170  l2->SetNDC();
171  TLatex* l3 = new TLatex(.18,.90,"");
172  l3->SetTextFont(42);
173  l3->SetTextAlign(13);
174  l3->SetTextSize(0.04);
175  l3->SetNDC();
176  TIter next(files);
177  TFile* file = 0;
178  Double_t y = l2->GetY();
179  while ((file = static_cast<TFile*>(next()))) {
180  TString tit = file->GetTitle();
181  TString nam = file->GetName();
182  if (file == files->First()) nam.Append(" *");
183  l2->DrawLatex(l2->GetX(), y, nam);
184  if (!nam.Contains(tit)) {
185  y -= 1.1*l3->GetTextSize();
186  l3->DrawLatex(l3->GetX(), y, tit);
187  }
188  y -= 1.1*l2->GetTextSize();
189  }
190  y -= 1.1*l2->GetTextSize();
191  TLatex* l4 = new TLatex(.1, y, "* Other files compared to this file");
192  l4->SetTextFont(42);
193  l4->SetTextAlign(13);
194  l4->SetTextSize(0.05);
195  l4->SetNDC();
196  l4->Draw();
197 
198  PrintCanvas("Comparison of details", "");
199  fOutput->cd();
200  fLegend->Write("legend");
201  files->Write("files", TObject::kSingleKey);
202  }
203 
204 
205  THStack* RatioStack(THStack* stack)
206  {
207  TH1* h0 = static_cast<TH1*>(stack->GetHists()->First());
208  THStack* ratios = new THStack("ratios", Form("to %s", h0->GetName()));
209  TIter nextH (stack->GetHists());
210  TH1* hr = 0;
211  Double_t min = +1e9;
212  Double_t max = -1e9;
213  while ((hr = static_cast<TH1*>(nextH()))) {
214  if (hr == h0) continue;
215  hr = static_cast<TH1*>(hr->Clone());
216  hr->Divide(h0);
217  hr->SetDirectory(0);
218  ratios->Add(hr);
219  for (Int_t i = 1; i <= hr->GetNbinsX(); i++) {
220  Double_t c = hr->GetBinContent(i);
221  Double_t e = hr->GetBinError(i);
222  if (c < 1e-6 || e < 1e-9) continue;
223  min = TMath::Min(min, c-1.1*e);
224  max = TMath::Max(max, c+1.1*e);
225  }
226  }
227  ratios->SetMinimum(TMath::Max(0.5,min));
228  ratios->SetMaximum(TMath::Min(1.5,max));
229  return ratios;
230  }
231  TH1* AddHisto(TDirectory* dir,
232  Int_t dimen,
233  const TString& binName,
234  const char* sub,
235  THStack* stack,
236  Color_t col)
237  {
238  TString hname;
239  hname.Form("%s/%s%dd/%s%s",binName.Data(),sub, dimen,
240  (f2D && TString(sub).EqualTo("results") ? "full/" : ""),
241  stack->GetName());
242  // Printf("Will get histogram %s", hname.Data());
243  TH1* h = static_cast<TH1*>(dir->Get(hname));
244  if (!h) {
245  Warning("Addhisto", "No histogram %s in %s", hname.Data(),dir->GetName());
246  return 0;;
247  }
248  stack->Add(h);
249  Double_t min = TMath::Min(stack->GetMinimum("nostack"), h->GetMinimum());
250  Double_t max = TMath::Max(stack->GetMaximum("nostack"), h->GetMaximum());
251  stack->SetMaximum(max);
252  stack->SetMinimum(min);
253  TString fn(dir->GetName());
254  TString dn(gSystem->DirName(fn));
255  h->SetName(dn.IsNull() || dn == "." ? fn : dn);
256  h->SetLineColor(col);
257  h->SetMarkerColor(col);
258  return h;
259  }
261  const TArrayI& dimens,
262  const TString& binName,
263  const char* sub,
264  TList* stacks)
265  {
266  TIter nextD(files);
267  TDirectory* dir = 0;
268  Int_t cnt = 0;
269  while ((dir = static_cast<TDirectory*>(nextD()))) {
270  TIter nextS(stacks);
271  THStack* stack = 0;
272  Color_t col = IndexColor(cnt);
273  Bool_t ok = true;
274  while ((stack = static_cast<THStack*>(nextS())))
275  if (!AddHisto(dir, dimens[cnt], binName, sub, stack, col)) ok = false;
276  if (!ok) continue;
277  cnt++;
278  }
279  return cnt;
280  }
281  void DrawStack(TVirtualPad* mother,
282  Int_t sub,
283  THStack* stack,
284  TDirectory* out,
285  Bool_t log,
286  Double_t tbase=0.05)
287  {
288  if (!stack || !stack->GetHists()) {
289  Warning("DrawStack", "No stack passed");
290  return;
291  }
292  if (TString(stack->GetName()).EqualTo("scaleProj")) log = false;
293 
294  TDirectory* dir = out->mkdir(stack->GetName());
295  dir->cd();
296  stack->Write("full");
297 
298  TVirtualPad* p = mother->cd(sub);
299  // gStyle->SetTitleFontSize(2*tbase/p->GetHNDC());
300  p->SetTopMargin(0.01);
301  p->SetRightMargin(0.01);
302  p->SetLeftMargin(0.15);
303  p->SetBottomMargin(0.15);
304  p->Divide(1,2,0,0);
305 
306  TVirtualPad* q = p->cd(1);
307  q->SetRightMargin(f2D ? 0.1 : 0.01);
308  q->SetLogx(log);
309  q->SetLogy(log);
310  q->SetGridx();
311  q->SetGridy();
312  q->SetTicks();
313  TString tit(stack->GetTitle());
314  stack->SetTitle("");
315  stack->Draw(f2D ? "nostack colz" : "nostack");
316  if (stack->GetHistogram()) {
317  TH1* h = stack->GetHistogram();
318  h->GetXaxis()->SetTitleSize(tbase/p->GetHNDC());
319  h->GetXaxis()->SetLabelSize(tbase/p->GetHNDC());
320  h->GetXaxis()->SetTitleOffset(0.4);
321  h->GetXaxis()->SetNdivisions(207);
322  h->GetYaxis()->SetTitleSize(tbase/p->GetHNDC());
323  h->GetYaxis()->SetLabelSize(tbase/p->GetHNDC());
324  h->GetYaxis()->SetNdivisions(207);
325  h->GetYaxis()->SetTitleOffset(0.4);
326  h->GetYaxis()->CenterTitle(true);
327  h->SetYTitle(tit);
328  if (f2D) {
329  q->Update();
330  h = static_cast<TH1*>(stack->GetHists()->Last());
331  TPaletteAxis* pal = static_cast<TPaletteAxis*>(h->GetListOfFunctions()
332  ->FindObject("palette"));
333  if (pal) {
334  pal->GetAxis()->SetTitleSize(tbase/p->GetHNDC());
335  pal->GetAxis()->SetLabelSize(tbase/p->GetHNDC());
336  pal->GetAxis()->SetNdivisions(207);
337  pal->GetAxis()->SetTitleOffset(0.4);
338  }
339  }
340  }
341  q->Modified();
342 
343  q = p->cd(2);
344  q->SetRightMargin(f2D ? 0.1 : 0.01);
345  q->SetLogx(log);
346  q->SetTicks();
347  q->SetGridx();
348  q->SetGridy();
349 
350  THStack* ratios = RatioStack(stack);
351  if (!ratios) {
352  Warning("DrawStack", "Failed to make ratio");
353  return;
354  }
355  // if (ratios->GetMaximum("nostack") > 1.5) ratios->SetMaximum(1.5);
356  // if (ratios->GetMinimum("nostack") < 0.5) ratios->SetMinimum(0.5);
357  ratios->Write("ratios");
358  tit = ratios->GetTitle();
359  ratios->SetTitle("");
360  ratios->Draw(f2D ? "nostack colz" : "nostack");
361  if (ratios->GetHistogram()) {
362  TH1* h = ratios->GetHistogram();
363  h->GetXaxis()->SetTitleSize(tbase/p->GetHNDC());
364  h->GetXaxis()->SetLabelSize(tbase/p->GetHNDC());
365  h->GetXaxis()->SetTitleOffset(0.4);
366  h->GetXaxis()->SetNdivisions(207);
367  h->GetYaxis()->SetTitleSize(tbase/p->GetHNDC());
368  h->GetYaxis()->SetLabelSize(tbase/p->GetHNDC());
369  h->GetYaxis()->SetTitleOffset(0.4);
370  h->GetYaxis()->SetNdivisions(207);
371  h->GetYaxis()->CenterTitle(true);
372  h->SetYTitle("Ratio");
373  if (f2D) {
374  q->Update();
375  h = static_cast<TH1*>(ratios->GetHists()->Last());
376  TPaletteAxis* pal = static_cast<TPaletteAxis*>(h->GetListOfFunctions()
377  ->FindObject("palette"));
378  if (pal) {
379  pal->GetAxis()->SetTitleSize(tbase/p->GetHNDC());
380  pal->GetAxis()->SetLabelSize(tbase/p->GetHNDC());
381  pal->GetAxis()->SetNdivisions(207);
382  pal->GetAxis()->SetTitleOffset(0.4);
383  }
384  }
385  }
386  q->Modified();
387  p->Modified();
388  }
389  void DrawStacks(TVirtualPad* mother,
390  TList* stacks,
391  TDirectory* out,
392  Bool_t log)
393  {
394  Int_t cnt = 1;
395  TIter nextS(stacks);
396  THStack* stack = 0;
397  while ((stack = static_cast<THStack*>(nextS()))) {
398  DrawStack(mother, cnt, stack, out, log);
399  cnt++;
400  }
401  }
402  void ProcessOne(TList* files,
403  const TArrayI& dimens,
404  const TString& binName,
405  const char* sub,
406  const TString& title,
407  const char** names,
408  const char** titles,
409  TDirectory* out,
410  Bool_t log=false)
411  {
412  TList* stacks = new TList;
413  const char** pname = names;
414  const char** ptit = titles;
415  while (*pname) {
416  TString n(*pname);
417  TString t(*ptit);
418  pname++;
419  ptit++;
420  THStack* stack = new THStack(n, t);
421  stacks->Add(stack);
422  }
423  if (stacks->GetEntries() <= 0) return;
424  ClearCanvas();
425 
426  fBody->SetTopMargin(0.01);
427  fBody->SetRightMargin(0.01);
428  Int_t nCol = 2;
429  Int_t nRow = (stacks->GetEntries()+1)/2;
430  fBody->Divide(nCol, nRow, 0, 0);
431 
432 
433  if (FillStacks(files, dimens, binName, sub, stacks) <= 0) return;
434  DrawStacks(fBody, stacks,out, log);
435 
436  fBottom->cd();
437  fLegend->Draw();
438 
439  PrintCanvas(title,Form("%s_%s", sub, binName.Data()));
440  }
441 
442  void ProcessCentDelta(TList* files,
443  const TArrayI& dimens,
444  const TString& binName,
445  const TString& centTitle,
446  TDirectory* out)
447  {
448  const char* names[] = { "realDeltaM", "realDeltaI",
449  "simDeltaM" , "simDeltaI" ,
450  "simDeltaC" , "simDeltaP" ,
451  "simDeltaS" , "scaleProj",
452  0 };
453  const char* titles[] = { "#Delta_{M}", "#Delta_{I}",
454  "#Delta_{M'}", "#Delta_{I'}",
455  "#Delta_{C'}", "#Delta_{P'}",
456  "#Delta_{S'}", "#LTk#it{k}#GT",
457  0 };
458  TDirectory* dir = out->mkdir("delta");
459 
460  ProcessOne(files, dimens, binName, "delta",
461  Form("#Delta #minus %s", centTitle.Data()),
462  names, titles, dir, true);
463  }
465  const TArrayI& dimens,
466  const TString& binName,
467  const TString& centTitle,
468  TDirectory* out)
469  {
470  const char* names[] = { "result", "simG", "realS", "simS",
471  "realM", "simM", "realC", "simC", 0 };
472  const char* titles[] = { "dN/d#eta", "G'", "S", "S'",
473  "M", "M'", "C", "C'", 0 };
474  TDirectory* dir = out->mkdir("parts");
475  ProcessOne(files, dimens, binName, "results",
476  Form("Parts #minus %s", centTitle.Data()),
477  names, titles, dir);
478  }
479 
480 
481  void ProcessCentBin(TList* files,
482  const TArrayI& dimens,
483  const TString& binName,
484  const TString& centTitle,
485  TDirectory* out)
486  {
487  TDirectory* dir = out->mkdir(binName);
488  ProcessCentDelta (files, dimens, binName, centTitle, dir);
489  ProcessCentResult(files, dimens, binName, centTitle, dir);
490  }
491 
492  void Run(const char* f1, const char* f2,
493  const char* f3=0, const char* f4=0,
494  const char* f5=0, const char* f6=0,
495  const char* f7=0, const char* f8=0)
496  {
497  const char* filenames[] = { f1, f2, f3, f4,
498  f5, f6, f7, f8,
499  0 };
500  Run(filenames);
501  }
502  void Run()
503  {
504  Int_t argc = gApplication->Argc();
505  char** argv = gApplication->Argv();
506  char** args = new char*[argc];
507  Int_t j = 0;
508  for (Int_t i = 1; i < argc; i++) {
509  TString tmp(argv[i]);
510  if (tmp.EndsWith(".C") ||
511  tmp.EndsWith(".C+") ||
512  tmp.BeginsWith("-")) continue;
513  args[j] = StrDup(argv[i]);
514  Printf("Argument %s", args[j]);
515  j++;
516  }
517  args[j] = 0;
518  // gApplication->ClearInputFiles();
519  this->Run(const_cast<const char**>(args));
520  }
521  void Run(const char** filenames)
522  {
523  MakeCanvas();
524  fLegend = new TLegend(.05, .05, .95, .95);
525  fLegend->SetFillStyle(0);
526  fLegend->SetBorderSize(0);
527 
528  TList* files = new TList;
529  TArrayI dimens;
530  Int_t cnt = 0;
531  TString last = "";
532  const char** fptr = filenames;
533  while (*fptr) {
534  // Extract information
535  TString fn(*fptr);
536  if (!fn.EndsWith(".root")) {
537  last = fn;
538  fptr++;
539  continue;
540  }
541  TString dn(gSystem->DirName(fn));
542  TString tit(last);
543  if (tit.IsNull()) {
544  tit = fn;
545  tit.ReplaceAll(".root","");
546  if (!dn.IsNull() && !dn.EqualTo(".")) tit = dn;
547  }
548  last = "";
549 
550  // Try o open the file
551  TFile* file = TFile::Open(fn, "READ");
552  fptr++;
553  if (!file) continue;
554 
555  // Set the title and increase count
556  file->SetTitle(tit);
557  cnt++;
558 
559  // Create legend entry
560  Color_t color = IndexColor(cnt-1);
561  TLegendEntry* e = fLegend->AddEntry("dummy", tit, "f");
562  e->SetFillColor(color);
563  e->SetLineColor(color);
564  e->SetFillStyle(1001);
565 
566  // Figure out dimension
567  Int_t dim = 0;
568  if (fn.EndsWith("etaipz.root") || dn.EndsWith("etaipz")) dim = 3;
569  else if (fn.EndsWith("eta.root") || dn.EndsWith("eta")) dim = 2;
570  else if (fn.EndsWith("const.root") || dn.EndsWith("const")) dim = 1;
571  dimens.Set(cnt);
572  dimens[cnt-1] = dim;
573 
574  // Add to list of files
575  files->Add(file);
576  }
577  if (cnt <= 0) {
578  Warning("", "No files opened");
579  return;
580  }
581  f2D = false; // cnt == 2;
582  TFile* f0 = static_cast<TFile*>(files->At(0));
583  TH1* cent = static_cast<TH1*>(f0->Get("realCent"));
584  // Printf("Got centrality histogram %p", cent);
585 
586  MakeTitlePage(files);
587 
588  for (Int_t i = 1; i <= cent->GetNbinsX(); i++) {
589  Double_t c1 = cent->GetXaxis()->GetBinLowEdge(i);
590  Double_t c2 = cent->GetXaxis()->GetBinUpEdge(i);
591  TString cn, ct;
592  cn.Form("cent%06.2f_%06.2f", c1, c2); cn.ReplaceAll(".","d");
593  ct.Form("%5.1f-%5.1f%%", c1, c2);
594  Printf("Processing centrality bin %6.2f-%6.2f%%", c1, c2);
595  ProcessCentBin(files, dimens, cn, ct, fOutput);
596  }
597  CloseCanvas();
598  }
599 };
600 
606 void
608 {
609  DetailsComparer d;
610  d.Run(); // "hijing_etaipz.root", "eposlhc_etaipz.root", "whijing_const.root");
611 }
612 //
613 // EOF
614 //
Int_t color[]
print message on plot with ok/not ok
Int_t FillStacks(TList *files, const TArrayI &dimens, const TString &binName, const char *sub, TList *stacks)
double Double_t
Definition: External.C:58
void ProcessCentResult(TList *files, const TArrayI &dimens, const TString &binName, const TString &centTitle, TDirectory *out)
const char * title
Definition: MakeQAPdf.C:27
TLegend * fLegend
TCanvas * fCanvas
void ProcessCentDelta(TList *files, const TArrayI &dimens, const TString &binName, const TString &centTitle, TDirectory *out)
TH1 * AddHisto(TDirectory *dir, Int_t dimen, const TString &binName, const char *sub, THStack *stack, Color_t col)
TSystem * gSystem
TCanvas * c
Definition: TestFitELoss.C:172
void Run(const char **filenames)
AliStack * stack
TVirtualPad * fTop
void DrawStack(TVirtualPad *p, THStack *s, Double_t min, Double_t max)
Definition: DrawDeltas3.C:48
int Int_t
Definition: External.C:63
void MakeTitlePage(const TList *files)
void ProcessCentBin(TList *files, const TArrayI &dimens, const TString &binName, const TString &centTitle, TDirectory *out)
Color_t IndexColor(Int_t cnt)
void ProcessOne(const char *meth, Int_t style, TList *stacks, TList *ratios, TList *mins, TList *maxs, TList *avgs, TAxis *&centAxis)
Definition: CentSysErr.C:118
Int_t colors[nPtBins]
THStack * RatioStack(THStack *stack)
TCanvas * MakeCanvas(const char *name, const char *title, const char *img=0)
Definition: CompareMuon.C:2
TVirtualPad * fBottom
void PrintCanvas(const char *title, const char *shortTitle=0)
void ClearCanvas(TCanvas *c)
Definition: DrawCorrAcc2.C:16
void DrawStacks(TVirtualPad *mother, TList *stacks, TDirectory *out, Bool_t log)
void ProcessOne(TList *files, const TArrayI &dimens, const TString &binName, const char *sub, const TString &title, const char **names, const char **titles, TDirectory *out, Bool_t log=false)
void DetailsCompare()
TFile * file
TList with histograms for a given trigger.
bool Bool_t
Definition: External.C:53
void DrawStack(TVirtualPad *mother, Int_t sub, THStack *stack, TDirectory *out, Bool_t log, Double_t tbase=0.05)
void Run(const char *f1, const char *f2, const char *f3=0, const char *f4=0, const char *f5=0, const char *f6=0, const char *f7=0, const char *f8=0)
TVirtualPad * fBody
Definition: External.C:196
TDirectoryFile * dir