AliPhysics  781d0c7 (781d0c7)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Expectations.C
Go to the documentation of this file.
1 
12 #ifndef __CINT__
13 # include <vector>
14 # include <TFile.h>
15 # include <TError.h>
16 # include <TCollection.h>
17 # include <TString.h>
18 # include <TH1.h>
19 # include <TH2.h>
20 # include <THStack.h>
21 # include <TClass.h>
22 # include <TBrowser.h>
23 # include <TCanvas.h>
24 # include <TLegend.h>
25 # include <TLegendEntry.h>
26 #else
27 class TFile;
28 class TClass;
29 class TH1;
30 class TH2;
31 class TDirectory;
32 class THStack;
33 class TCollection;
34 class TBrowser;
35 class TCanvas;
36 class TVirtualPad;
37 #endif
38 
44 const char* parFmt = "%-10s | %5.3f |";
45 const char* rowFmt = " %5.2f | %5.2f | %5.2f | %5.2f |";
51 struct Calculation
52 {
53  struct Row
54  {
70  Double_t Get(Int_t i) const
71  {
72  return (i == 0 ? Ps :
73  i == 1 ? Ss :
74  i == 2 ? Cs :
75  i == 3 ? Cb : 0);
76  }
84  Double_t GetE(Int_t i) const
85  {
86  return (i == 0 ? ePs :
87  i == 1 ? eSs :
88  i == 2 ? eCs :
89  i == 3 ? eCb : 0);
90  }
91  void Scale(Int_t i, Double_t s)
92  {
93  switch (i) {
94  case 0: Ps *= s; ePs *= s; break;
95  case 1: Ss *= s; eSs *= s; break;
96  case 2: Cs *= s; eCs *= s; break;
97  case 3: Cb *= s; eCb *= s; break;
98  }
99  }
100  void Print() const
101  {
102  printf(rowFmt, Ps, Ss, Cs, Cb);
103  }
104  };
110  struct Particle
111  {
112  TString n;
113  Double_t w;
119  {
120  return (i == 0 ? reduced :
121  i == 1 ? expected :
122  i == 2 ? reweighed :
123  truth);
124  }
125  void Print()
126  {
127  printf(parFmt, n.Data(), w);
128  reduced. Print();
129  expected. Print();
130  reweighed.Print();
131  truth. Print();
132  printf("\n");
133  }
135  Int_t column,
136  TCollection* bin,
137  const TString& sub)
138  {
139  TString pn = "primaries";
140  TString sn = "secondaries";
141  TString cn = "combinatorics";
142  THStack* hp = GetHS(GetC(GetC(GetC(bin,pn),"specie"),sub),"rows");
143  THStack* hs = GetHS(GetC(GetC(GetC(bin,sn),"specie"),sub),"rows");
144  THStack* hc = GetHS(GetC(GetC(GetC(bin,cn),"specie"),sub),"rows");
145  TH1* rps = GetH1(hp->GetHists(), "rowSig");
146  TH1* rss = GetH1(hs->GetHists(), "rowSig");
147  TH1* rcs = GetH1(hc->GetHists(), "rowSig");
148  TH1* rcb = GetH1(hc->GetHists(), "rowBg");
149  Row& ret = (iRow == 0 ? reduced :
150  iRow == 1 ? expected :
151  iRow == 2 ? reweighed : truth);
152  Double_t ww = (iRow == 1 ? w : 1);
153  if (n.IsNull()) n = rps->GetXaxis()->GetBinLabel(column);
154  ret.Ps = ww*rps->GetBinContent(column);
155  ret.Ss = ww*rss->GetBinContent(column);
156  ret.Cs = ww*rcs->GetBinContent(column);
157  ret.Cb = ww*rcb->GetBinContent(column);
158  ret.ePs = ww*rps->GetBinError (column);
159  ret.eSs = ww*rss->GetBinError (column);
160  ret.eCs = ww*rcs->GetBinError (column);
161  ret.eCb = ww*rcb->GetBinError (column);
162  return this;
163  }
164  };
165 #ifndef __CINT__
166  typedef std::vector<Particle*> ParticleList;
167 #else
168  typedef std::vector<void*> ParticleList;
169 #endif
171  Double_t fP; // Ratio of primaries to all tracklets
172  Double_t fS; // Ratio of secondaries to all tracklets
173  Double_t fC; // Ratio of combinatorics to all tracklets
174  Double_t fPs; // Ratio of signal primaries to all tracklets
175  Double_t fSs; // Ratio of signal secondaries to all tracklets
176  Double_t fCs; // Ratio of signal combinatorics to all tracklets
181  void Print()
182  {
183  printf("%-18s |", "Sample");
184  printf(" %-29s |", "Reduced");
185  printf(" %-29s |", "Expected");
186  printf(" %-29s |", "Reweighed");
187  printf(" %-29s |", "Truth");
188  printf("\n");
189  TString head(parFmt);
190  for (Int_t i = 0; i < 4; i++) head.Append(rowFmt);
191  head.ReplaceAll("%5.3f", "%5s");
192  head.ReplaceAll("%5.2f", "%5s");
193  TString lne(head);
194  lne.ReplaceAll("|", "+");
195  lne.ReplaceAll("%5s", "-----");
196  lne.ReplaceAll("%10s", "----------");
197  lne.ReplaceAll(" ", "-");
198  Printf(head.Data(), "Type", "W",
199  "Ps", "Ss", "Cs", "Cb",
200  "Ps", "Ss", "Cs", "Cb",
201  "Ps", "Ss", "Cs", "Cb",
202  "Ps", "Ss", "Cs", "Cb");
203  Printf(lne);
204  for (ParticleList::const_iterator i = particles.begin();
205  i != particles.end(); ++i)
206  (*i)->Print();
207  Printf(lne);
208  }
210  {
211  for (Int_t i = 0; i < 4; i++) {
212  Double_t sum = 0;
213  for (ParticleList::const_iterator j = particles.begin();
214  j != particles.end(); ++j)
215  sum += (*j)->expected.Get(i);
216  for (ParticleList::const_iterator j = particles.begin();
217  j != particles.end(); ++j)
218  (*j)->expected.Scale(i,100/sum);
219  }
220  }
221 
222 
231  static TObject* ChkC(TObject* o, TClass* c)
232  {
233  if (!o) return 0;
234  if (!c) return o;
235  if (!o->IsA()->InheritsFrom(c)) {
236  ::Warning("ChkC", "Object %s is not a %s but a %s",
237  o->GetName(), c->GetName(), o->ClassName());
238  return 0;
239  }
240  return o;
241  }
251  static TObject* ChkO(TObject* src, TObject* o, const char* name)
252  {
253  if (!o) {
254  ::Warning("ChkO", "Object %s not found in %s", name, src->GetName());
255  src->ls();
256  return 0;
257  }
258  return o;
259  }
269  static TObject* GetO(TDirectory* d, const char* name, TClass* cls)
270  {
271  if (!d) {
272  ::Warning("GetO", "No directory passed for %s", name);
273  return 0;
274  }
275  TObject* o = d->Get(name);
276  return ChkC(ChkO(d, o, name), cls);
277  }
287  static TObject* GetO(TCollection* d, const char* name, TClass* cls)
288  {
289  if (!d) {
290  ::Warning("GetO", "No collection passed for %s", name);
291  return 0;
292  }
293  TObject* o = d->FindObject(name);
294  return ChkC(ChkO(d, o, name), cls);
295  }
304  static TCollection* GetC(TDirectory* d, const char* name)
305  {
306  return static_cast<TCollection*>(GetO(d, name, TCollection::Class()));
307  }
316  static TCollection* GetC(TCollection* d, const char* name)
317  {
318  return static_cast<TCollection*>(GetO(d, name, TCollection::Class()));
319  }
328  static TH1* GetH1(TCollection* d, const char* name)
329  {
330  return static_cast<TH1*>(GetO(d, name, TH1::Class()));
331  }
340  static TH2* GetH2(TCollection* d, const char* name)
341  {
342  return static_cast<TH2*>(GetO(d, name, TH2::Class()));
343  }
352  static THStack* GetHS(TCollection* d, const char* name)
353  {
354  return static_cast<THStack*>(GetO(d, name, THStack::Class()));
355  }
370  Int_t column,
371  TCollection* reduced,
372  TCollection* reweighed,
373  TCollection* truth,
374  const TString& sub)
375  {
376  Particle* ret = new Particle;
377  ret->w = w;
378  ret->Read(0, column, reduced, sub);
379  ret->Read(1, column, reduced, sub);
380  ret->Read(2, column, reweighed, sub);
381  ret->Read(3, column, truth, sub);
382  particles.push_back(ret);
383  return ret;
384  }
385  THStack* MakeStack(Int_t column, const char* name, const char* title)
386  {
387  const char* names[] = { "red", "exp", "rew", "trh" };
388  const char* titles[] = { "Reduced", "Expected", "Reweighed", "Truth" };
389  const Color_t colors[] = { kRed+1, kMagenta+1, kBlue+1, kGreen+1 };
390  Int_t nPart = particles.size();
391  THStack* stack = new THStack(name, title);
392  for (Int_t i = 0; i < 4; i++) {
393  TH1* h = new TH1D(names[i],titles[i],nPart,0,nPart);
394  h->SetFillColor(colors[i]);
395  h->SetLineColor(colors[i]);
396  h->SetMarkerColor(colors[i]);
397  h->SetBarOffset(i*0.225+.075);
398  h->SetBarWidth (0.20);
399  h->SetDirectory(0);
400  stack->Add(h, "bar0");
401 
402  for (Int_t j = 0; j < nPart; j++) {
403  Particle* p = particles[j];
404  Row& r = p->Get(i);
405  h->GetXaxis()->SetBinLabel(j+1,p->n);
406  h->SetBinContent (j+1,r.Get (column));
407  h->SetBinError (j+1,r.GetE(column));
408  }
409  }
410  return stack;
411  }
412  void DrawStack(TVirtualPad* mother, Int_t sub, THStack* stack, Bool_t logy)
413  {
414  TVirtualPad* p = mother->cd(sub);
415  p->SetGridx();
416  p->SetGridy();
417  p->SetTicks();
418  if (logy) p->SetLogy();
419  if ((p->GetNumber() % 2) == 0) p->SetRightMargin(0.01);
420  stack->SetMinimum(0.02);
421  stack->SetMaximum(logy ? 150 : 110);
422  stack->Draw("nostack hist");
423  stack->GetHistogram()->GetYaxis()->SetNdivisions(210);
424  stack->GetHistogram()->GetYaxis()->SetLabelSize(0.03/p->GetHNDC());
425  stack->GetHistogram()->GetYaxis()->SetTitleSize(0.03/p->GetHNDC());
426  stack->GetHistogram()->GetXaxis()->SetLabelSize(0.03/p->GetHNDC());
427  stack->GetHistogram()->GetYaxis()->SetTitle("Fraction [%]");
428  }
429 
437  void Run(Double_t c1=0, Double_t c2=0, Bool_t mid=true)
438  {
439  TString bin("all");
440  if (c2 > c1)
441  bin.Form("cent%03dd%02d_%03dd%02d",
442  Int_t(c1), Int_t(100*c1) % 100,
443  Int_t(c2), Int_t(100*c2) % 100);
444 
445  TFile* redFile = TFile::Open("stk.root", "READ");
446  TFile* rweFile = TFile::Open("wstk.root", "READ");
447  TFile* trhFile = TFile::Open("hijing.root", "READ");
448  if (!redFile || !rweFile || !trhFile) return;
449  TString sub = (mid ? "mid" : "fwd");
450  TCollection* redTop = GetC(GetC(redFile,"MidRapidityMCResults"),bin.Data());
451  TCollection* rweTop = GetC(GetC(rweFile,"MidRapidityMCResults"),bin.Data());
452  TCollection* trhTop = GetC(GetC(trhFile,"MidRapidityMCResults"),bin.Data());
453 
454  Create(1.52233, 1, redTop, rweTop, trhTop, sub);
455  Create(1.41178, 2, redTop, rweTop, trhTop, sub);
456  Create(2.75002, 3, redTop, rweTop, trhTop, sub);
457  Create(3.24110, 4, redTop, rweTop, trhTop, sub);
458  Create(2.75002, 5, redTop, rweTop, trhTop, sub);
459  Create(1, 6, redTop, rweTop, trhTop, sub);
460 
461  ScaleExpected();
462  Print();
463 
464  THStack* Ps = MakeStack(0, "Ps", "Signal primaries");
465  THStack* Ss = MakeStack(1, "Ss", "Signal secondaries");
466  THStack* Cs = MakeStack(2, "Cs", "Signal fake");
467  THStack* Cb = MakeStack(3, "Cb", "Background fake");
468 
469  Bool_t logy = true;
470  TCanvas* c = new TCanvas("c","c",1000,1000);
471  c->SetTopMargin(0.01);
472  c->SetRightMargin(0.01);
473  c->SetLeftMargin(0.13);
474  c->Divide(2,2,0,0);
475 
476  DrawStack(c, 1, Ps, logy);
477  DrawStack(c, 2, Ss, logy);
478  DrawStack(c, 3, Cs, logy);
479  DrawStack(c, 4, Cb, logy);
480 
481  TVirtualPad* p = c->cd(1);
482  TString t = mid ? "|#eta|<1" : "|#eta|>1";
483  if (c1 >= c2) t.Append(" MB");
484  else t.Append(Form(" %4.1f#minus%4.1f%%", c1, c2));
485  TLegend* l = (logy
486  ? p->BuildLegend(0.45,0.5,0.95,0.9, t)
487  : p->BuildLegend(0.16,0.4,0.6,0.8, t));
488  l->SetFillStyle(0);
489  l->SetBorderSize(0);
490 
491  c->SaveAs(Form("expected_%s_%s.png", bin.Data(), mid ? "mid" : "fwd"));
492  }
493 };
494 
505 void Expectations(Bool_t mid=true, Double_t c1=0, Double_t c2=5)
506 {
507  Calculation c;
508  c.Run(c1, c2, mid);
509 }
510 //
511 // EOF
512 //
THStack * GetHS(TCollection *d, const char *name)
void Run(Double_t c1=0, Double_t c2=0, Bool_t mid=true)
Definition: Expectations.C:437
TCollection * GetC(TDirectory *d, const char *name)
double Double_t
Definition: External.C:58
const char * title
Definition: MakeQAPdf.C:26
ParticleList particles
static TCollection * GetC(TDirectory *d, const char *name)
Definition: Expectations.C:304
const char * rowFmt
Definition: Expectations.C:45
Particle * Create(Double_t w, Int_t bin, THStack *hp, THStack *hs, THStack *hc)
const char * parFmt
Definition: Expectations.C:44
static THStack * GetHS(TCollection *d, const char *name)
Definition: Expectations.C:352
Double_t Get(Int_t i) const
Definition: Expectations.C:70
TCanvas * c
Definition: TestFitELoss.C:172
Particle * Create(Double_t w, Int_t column, TCollection *reduced, TCollection *reweighed, TCollection *truth, const TString &sub)
Definition: Expectations.C:369
static TH2 * GetH2(TCollection *d, const char *name)
Definition: Expectations.C:340
void ScaleExpected()
Definition: Expectations.C:209
static TObject * ChkC(TObject *o, TClass *c)
Definition: Expectations.C:231
int Int_t
Definition: External.C:63
Double_t GetE(Int_t i) const
Definition: Expectations.C:84
static TCollection * GetC(TCollection *d, const char *name)
Definition: Expectations.C:316
THStack * MakeStack(Int_t column, const char *name, const char *title)
Definition: Expectations.C:385
Definition: External.C:212
TObject * GetO(TDirectory *d, const char *name, TClass *cls) const
static TObject * GetO(TCollection *d, const char *name, TClass *cls)
Definition: Expectations.C:287
void Scale(Int_t i, Double_t s)
Definition: Expectations.C:91
void Print() const
Definition: Expectations.C:100
static TH1 * GetH1(TCollection *d, const char *name)
Definition: Expectations.C:328
TObject * ChkO(TObject *src, TObject *o, const char *name) const
static TObject * ChkO(TObject *src, TObject *o, const char *name)
Definition: Expectations.C:251
Row & Get(Int_t i)
Definition: Expectations.C:118
Particle * Read(Int_t iRow, Int_t column, TCollection *bin, const TString &sub)
Definition: Expectations.C:134
Definition: External.C:220
static TObject * GetO(TDirectory *d, const char *name, TClass *cls)
Definition: Expectations.C:269
TH1 * GetH1(TCollection *d, const char *name)
TObject * ChkC(TObject *o, TClass *c) const
void Run(const char *fileName, const char *binName, Bool_t mid=true)
void Expectations(Bool_t mid=true, Double_t c1=0, Double_t c2=5)
Definition: Expectations.C:505
bool Bool_t
Definition: External.C:53
Definition: External.C:196
void Print()
Definition: Expectations.C:181
void DrawStack(TVirtualPad *mother, Int_t sub, THStack *stack, Bool_t logy)
Definition: Expectations.C:412
std::vector< Particle * > ParticleList
Definition: Expectations.C:166