AliPhysics  251aa1e (251aa1e)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompareResults.C
Go to the documentation of this file.
1 
11 #ifndef __CINT__
12 # include <TLegend.h>
13 # include <TLegendEntry.h>
14 # include <TCanvas.h>
15 # include <TH1.h>
16 # include <TFile.h>
17 # include <THStack.h>
18 # include <TError.h>
19 # include <TApplication.h>
20 #else
21 class TH1;
22 class TDirectory;
23 class TLegend;
24 #endif
25 
26 //____________________________________________________________________
27 struct Compare
28 {
31  TLegend* fLegend;
33  : fMin(+1e9),
34  fMax(-1e9)
35  {}
36 
37  //____________________________________________________________________
38  static TObject* GetO(TDirectory* dir, const char* name, TClass* cls=0)
39  {
40  if (!dir) {
41  Warning("GetO", "No directory passed");
42  return 0;
43  }
44 
45  TObject* o = dir->Get(name);
46  if (!o) {
47  Warning("GetO", "object %s not found in %s",
48  name, dir->GetPath());
49  return 0;
50  }
51  if (!cls) return o;
52  if (!o->IsA()->InheritsFrom(cls)) {
53  Warning("GetO", "Object %s in %s is not a %s, but a %s",
54  name, dir->GetPath(), cls->GetName(), o->ClassName());
55  return 0;
56  }
57  return o;
58  }
59  //____________________________________________________________________
60  static TDirectory* GetD(TDirectory* dir, const char* name)
61  {
62  return static_cast<TDirectory*>(GetO(dir,name,TDirectory::Class()));
63  }
64 
65  //____________________________________________________________________
66  static TH1* GetH1(TDirectory* dir, const char* name)
67  {
68  return static_cast<TH1*>(GetO(dir,name,TH1::Class()));
69  }
70  //____________________________________________________________________
71  TH1* One(TDirectory* newDir, TDirectory* oldDir, Double_t c1, Double_t c2)
72  {
73  TString name;
74  name.Form("cent%03dd%02d_%03dd%02d",
75  Int_t(c1), Int_t(c1*100)%100,
76  Int_t(c2), Int_t(c2*100)%100);
77  TDirectory* newSubDir = GetD(newDir, name);
78  TDirectory* oldSubDir = GetD(oldDir, name);
79  if (!newSubDir || !oldSubDir) return 0;
80  Int_t newDim = 0;
81  if (TString(newDir->GetName()).Contains("etaipz")) newDim = 3;
82  else if (TString(newDir->GetName()).Contains("eta")) newDim = 2;
83  else if (TString(newDir->GetName()).Contains("const")) newDim = 1;
84  Int_t oldDim = 0;
85  if (TString(oldDir->GetName()).Contains("etaipz")) oldDim = 3;
86  else if (TString(oldDir->GetName()).Contains("eta")) oldDim = 2;
87  else if (TString(oldDir->GetName()).Contains("const")) oldDim = 1;
88 
89  TDirectory* newSubSubDir = GetD(newSubDir, Form("results%dd",newDim));
90  TDirectory* oldSubSubDir = GetD(oldSubDir, Form("results%dd",oldDim));
91  if (!newSubSubDir || !oldSubSubDir) return 0;
92 
93  TH1* newRes = GetH1(newSubSubDir, "result");
94  TH1* oldRes = GetH1(oldSubSubDir, "result");
95  if (!newRes || !oldRes) return 0;
96 
97  TH1* ratio = static_cast<TH1*>(newRes->Clone(name));
98  ratio->SetDirectory(0);
99  ratio->SetTitle(Form("%5.1f - %5.1f%%", c1, c2));
100  ratio->SetYTitle("New / Old");
101  ratio->Divide(oldRes);
102  fMin = TMath::Min(fMin, ratio->GetMinimum());
103  fMax = TMath::Max(fMax, ratio->GetMaximum());
104 
105  Printf("Calculated %s/%s", newDir->GetName(), oldDir->GetName());
106  if (!fLegend) return ratio;
107 
108 
109  TLegendEntry* e =
110  fLegend->AddEntry("", Form("%3.0f - %3.0f%%", c1, c2), "f");
111  e->SetFillStyle(1001);
112  e->SetFillColor(ratio->GetMarkerColor());
113 
114  return ratio;
115  }
116  //____________________________________________________________________
117  void Run(const char* newName, const char* oldName,
118  const char* newTitle="New", const char* oldTitle="Old")
119  {
120  TFile* newFile = TFile::Open(newName,"READ");
121  TFile* oldFile = TFile::Open(oldName,"READ");
122  if (!newFile || !oldFile) return;
123 
124  TH1* newCent = GetH1(newFile, "realCent");
125  TH1* oldCent = GetH1(oldFile, "realCent");
126  if (!newCent || !oldCent) return;
127 
128  TString t; t.Form("#it{R}=#frac{%s}{%s}", newTitle, oldTitle);
129  TCanvas* c = new TCanvas("c", t, 1200, 800);
130  c->SetTopMargin(0.01);
131  c->SetRightMargin(0.20);
132  fLegend = new TLegend(1-c->GetRightMargin(),
133  c->GetBottomMargin(),
134  1, 1-c->GetTopMargin(),
135  t);
136  fLegend->SetFillStyle(0);
137  fLegend->SetBorderSize(0);
138  THStack* stack = new THStack("ratios","");
139 
140  fMin = +1e6;
141  fMax = -1e6;
142  TH1* one = 0;
143  for (Int_t i = newCent->GetNbinsX(); i--;) {
144  Double_t c1 = newCent->GetXaxis()->GetBinLowEdge(i+1);
145  Double_t c2 = newCent->GetXaxis()->GetBinUpEdge(i+1);
146  Info("", "c1=%f c2=%f", c1, c2);
147  TH1* r = One(newFile, oldFile, c1, c2);
148  if (!r) continue;
149  if (!one) {
150  one = static_cast<TH1*>(r->Clone("one"));
151  one->SetDirectory(0);
152  one->Reset();
153  for (Int_t j = 1; j <= one->GetNbinsX(); j++) {
154  one->SetBinContent(j,1);
155  one->SetBinError (j,0);
156  }
157  }
158  // r->Add(one, i-1);
159  // r->Scale(TMath::Power(10,i));
160  stack->Add(r);
161  }
162  stack->Draw("nostack");
163  stack->SetMinimum(0.95*fMin);
164  stack->SetMaximum(1.05*fMax);
165  stack->GetHistogram()->SetXTitle("#eta");
166  stack->GetHistogram()->SetYTitle("#it{R}");
167  fLegend->Draw();
168  c->Modified();
169  c->Update();
170  c->cd();
171  c->SaveAs(Form("%sover%s.png", newTitle, oldTitle));
172  }
173 };
174 
175 void CompareResults(const char* newFile, const char* oldFile,
176  const char* newTit, const char* oldTit)
177 {
178  Compare* c = new Compare;
179  c->Run(newFile,oldFile,newTit,oldTit);
180 }
181 
182 void CompareResults(const char** argv)
183 {
184  TString newFile;
185  TString oldFile;
186  TString newTit("");
187  TString oldTit("");
188  const char** ptr = argv;
189  while ((*ptr)) {
190  TString argi = *ptr;
191  ptr++;
192  if (argi.Contains("help")) {
193  Printf("Usage: CompareResults AFILE BFILE [ATITLTE [BTITLE]]");
194  return;
195  }
196  if (argi.Contains("CompareResults.C")) continue;
197  if (argi.BeginsWith("-")) continue;
198  if (argi.EndsWith(".root")) {
199  if (newFile.IsNull()) newFile = argi;
200  else oldFile = argi;
201  }
202  else {
203  if (newTit.IsNull()) newTit = argi;
204  else oldTit = argi;
205  }
206  }
207  if (newTit.IsNull()) newTit = "New";
208  if (oldTit.IsNull()) oldTit = "Old";
209 
210 
211  CompareResults(newFile, oldFile, newTit, oldTit);
212 }
213 
215 {
216  CompareResults(const_cast<const char**>(&(gApplication->Argv()[1])));
217  gApplication->ClearInputFiles();
218 }
219 
220 
221 
222 //
223 // EOF
224 //
225 
double Double_t
Definition: External.C:58
Double_t fMax
void Run(const char *newName, const char *oldName, const char *newTitle="New", const char *oldTitle="Old")
TCanvas * c
Definition: TestFitELoss.C:172
static TObject * GetO(TDirectory *dir, const char *name, TClass *cls=0)
int Int_t
Definition: External.C:63
TH1 * Compare(TH1 *def, TH1 *oth)
Definition: Compare.C:124
TLegend * fLegend
void CompareResults(const char *newName="NewTaskNewPost.root", const char *oldName="OldTaskNewCorrect.root", const char *newTitle="New", const char *oldTitle="Old")
static TDirectory * GetD(TDirectory *dir, const char *name)
Double_t fMin
static TH1 * GetH1(TDirectory *dir, const char *name)
TH1 * One(TDirectory *newDir, TDirectory *oldDir, Double_t c1, Double_t c2)
Definition: External.C:196