2 * @file DrawAODSummary.C
3 * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
4 * @date Tue Oct 30 09:47:30 2012
6 * @brief Script to draw summary of AOD pass into a PDF
11 # include <TCollection.h>
15 # include <TParameter.h>
22 # include <TLegendEntry.h>
23 # include <TPaveText.h>
39 //____________________________________________________________________
41 * Find an object in a collection
43 * @param parent Parent list
44 * @param name Name of object
46 * @return Pointer to object or null
48 TObject* GetObject(const TCollection* parent, const TString& name)
50 // Info("GetObject", "Getting object %s from %p", name.Data(), parent);
51 // --- Check parent ------------------------------------------------
53 Warning("GetObject", "No parent list");
56 // --- Check name --------------------------------------------------
58 Warning("GetObject", "No name specified");
61 // --- Find the object ---------------------------------------------
62 TObject* o = parent->FindObject(name);
64 Warning("GetObject", "Object \"%s\" not found in parent \"%s\"",
65 name.Data(), parent->GetName());
70 //____________________________________________________________________
72 * Find an object in a directory
74 * @param parent Parent directory
75 * @param name Name of object
77 * @return Pointer to object or null
79 TObject* GetObject(TDirectory* parent, const TString& name)
81 // Info("GetObject", "Getting object %s from %p", name.Data(), parent);
82 // --- Check parent ------------------------------------------------
84 Warning("GetObject", "No parent directory");
87 // --- Check name --------------------------------------------------
89 Warning("GetObject", "No name specified");
92 // --- Find the object ---------------------------------------------
93 TObject* o = parent->Get(name);
95 Warning("GetObject", "Object \"%s\" not found in parent \"%s\"",
96 name.Data(), parent->GetName());
102 //____________________________________________________________________
104 * Check the type of a found object
108 * @param src Source of object
110 * @return true on success, false otherwise
112 Bool_t CheckType(const TObject* o, const TClass* cl, const TString& src)
114 // Info("CheckType", "Checking type of %s vs %s", o->GetName(), cl->GetName());
115 if (!o->IsA()->InheritsFrom(cl)) {
116 Warning("CheckType", "Object \"%s\" retrieved from \"%s\" is not a "
117 "%s but a %s", o->GetName(), src.Data(), cl->GetName(),
124 //_____________________________________________________________________
125 void GetParameter(const TCollection* c, const TString& name, UShort_t& value)
127 // Info("GetParameter", "Getting parameter of %s from %p", name.Data(), c);
128 TObject* o = GetObject(c, name);
130 value = o->GetUniqueID();
132 //_____________________________________________________________________
133 void GetParameter(const TCollection* c, const TString& name, Int_t& value)
135 // Info("GetParameter", "Getting parameter of %s from %p", name.Data(), c);
136 TObject* o = GetObject(c, name);
138 value = o->GetUniqueID();
140 //_____________________________________________________________________
141 void GetParameter(const TCollection* c, const TString& name, Double_t& value)
143 // Info("GetParameter", "Getting parameter of %s from %p", name.Data(), c);
144 TObject* o = GetObject(c, name);
146 UInt_t i = o->GetUniqueID();
147 Float_t v = *reinterpret_cast<Float_t*>(&i);
150 //_____________________________________________________________________
151 void GetParameter(const TCollection* c, const TString& name, Bool_t& value)
153 // Info("GetParameter", "Getting parameter of %s from %p", name.Data(), c);
154 TObject* o = GetObject(c, name);
156 value = o->GetUniqueID();
159 //____________________________________________________________________
161 * Find a collection in another collection
163 * @param parent Parent collection
164 * @param name Name of the collection
166 * @return pointer to collection on success, otherwise null
168 TCollection* GetCollection(const TCollection* parent, const TString& name)
170 // Info("GetCollection", "Getting collection of %s from %p", name.Data(), c);
171 // --- Find the object ---------------------------------------------
172 TObject* o = GetObject(parent, name);
175 // --- Check type of found object ----------------------------------
176 if (!CheckType(o, TCollection::Class(), parent->GetName())) return 0;
178 // --- Return the collection ---------------------------------------
179 return static_cast<TCollection*>(o);
182 //____________________________________________________________________
184 * Find a collection in a directory
186 * @param parent Parent directory
187 * @param name Name of the collection
189 * @return pointer to collection on success, otherwise null
191 TCollection* GetCollection(TDirectory* parent, const TString& name)
193 // Info("GetCollection", "Getting collection of %s from %p",
194 // name.Data(), parent);
195 // --- Find the object ---------------------------------------------
196 TObject* o = GetObject(parent, name);
199 // --- Check the type of object ------------------------------------
200 if (!CheckType(o, TCollection::Class(), parent->GetName())) return 0;
202 // --- Return the collection ---------------------------------------
203 return static_cast<TCollection*>(o);
206 //____________________________________________________________________
208 * Get a 1D histogram from a collection
210 * @param parent Parent collection
211 * @param name Name of histogram
213 * @return pointer or null
215 TH1* GetH1(const TCollection* parent, const TString& name)
217 // Info("GetH1", "Getting 1D histogram of %s from %p", name.Data(), c);
218 // --- Find the object ---------------------------------------------
219 TObject* o = GetObject(parent, name);
222 // --- Check the type of object ------------------------------------
223 if (!CheckType(o, TH1::Class(), parent->GetName())) return 0;
225 // --- Return the collection ---------------------------------------
226 return static_cast<TH1*>(o);
228 //____________________________________________________________________
230 * Get a 2D histogram from a collection
232 * @param parent Parent collection
233 * @param name Name of histogram
235 * @return pointer or null
237 TH2* GetH2(const TCollection* parent, const TString& name)
239 // Info("GetH2", "Getting 2D histogram of %s from %p", name.Data(), c);
240 // --- Find the object ---------------------------------------------
241 TObject* o = GetObject(parent, name);
244 // --- Check the type of object ------------------------------------
245 if (!CheckType(o, TH2::Class(), parent->GetName())) return 0;
247 // --- Return the collection ---------------------------------------
248 return static_cast<TH2*>(o);
250 //____________________________________________________________________
252 * Get a histogram stack from a collection
254 * @param parent Parent collection
255 * @param name Name of histogram
257 * @return pointer or null
259 THStack* GetStack(const TCollection* parent, const TString& name,
262 // Info("GetStack", "Getting histogram stack %s from %p", name.Data(), parent);
263 // --- Find the object ---------------------------------------------
264 TObject* o = GetObject(parent, name);
267 // --- Check the type of object ------------------------------------
268 if (!CheckType(o, THStack::Class(), parent->GetName())) return 0;
270 THStack* stack = static_cast<THStack*>(o);
271 if (sub == 0) return stack;
273 if (stack->GetHists()->GetEntries() <= 0 ||stack->GetMaximum() < 1) {
274 stack->GetHists()->Delete();
275 const char* subs[] = { "FMD1I", "FMD2I", "FMD2O", "FMD3O", "FMD3I", 0 };
276 const char** ptr = subs;
278 TCollection* sc = GetCollection(parent, *ptr);
279 if (!sc) { ptr++; continue; }
281 TH2* h = GetH2(sc, sub);
283 TH1* p = h->ProjectionX(*ptr, 1, h->GetNbinsY(), "e");
284 p->Scale(1., "width");
291 // --- Return the collection ---------------------------------------
295 //____________________________________________________________________
298 printf("Press enter to continue");
302 //____________________________________________________________________
306 * @param c Canvas to clear
308 * @ingroup pwglf_forward_scripts_corr
311 ClearCanvas(TCanvas* c)
313 // Info("ClearCanvas", "Clearing canvas");
314 c->SetLeftMargin(.1);
315 c->SetRightMargin(.05);
316 c->SetBottomMargin(.1);
317 c->SetTopMargin(.05);
321 TPad* p1 = new TPad("top", "Top", 0, 1-dy, 1, 1, 0, 0);
323 p1->SetFillColor(kBlue-5);
324 p1->SetBorderSize(0);
325 p1->SetBorderMode(0);
329 TPad* p2 = new TPad("body", "Body", 0, 0, 1, 1-dy, 0, 0);
333 p2->SetBorderSize(0);
334 p2->SetBorderMode(0);
335 p2->SetRightMargin(0.02);
336 p2->SetTopMargin(0.02);
342 //____________________________________________________________________
346 * @param pname Name of PDF file to make
348 * @return Created canvas
350 TCanvas* CreateCanvas(const TString& pname)
352 // Info("CreateCanvas", "Creating canvas");
354 TCanvas* c = new TCanvas("c", pname.Data(), size / TMath::Sqrt(2), size);
358 c->Print(Form("%s[", pname.Data()));
360 gStyle->SetOptStat(0);
361 gStyle->SetTitleColor(0);
362 gStyle->SetTitleStyle(0);
363 gStyle->SetTitleBorderSize(0);
364 gStyle->SetTitleX(.5);
365 gStyle->SetTitleY(1);
366 gStyle->SetTitleW(.8);
367 gStyle->SetTitleH(.09);
368 gStyle->SetFrameFillColor(kWhite);
369 gStyle->SetFrameBorderSize(1);
370 gStyle->SetFrameBorderMode(1);
371 gStyle->SetPalette(1);
378 //____________________________________________________________________
384 void CloseCanvas(TCanvas* c)
386 // Info("CloseCanvas", "Closing canvas");
388 c->Print(Form("%s]", c->GetTitle()));
391 //____________________________________________________________________
398 void PrintCanvas(TCanvas* c, const TString& title,
399 Float_t size=.7, Bool_t pause=false)
401 // Info("PrintCanvas", "Printing page %s", title.Data());
403 tit.Form("Title:%s", title.Data());
406 TLatex* ltx = new TLatex(.5, .5, title);
408 ltx->SetTextAlign(22);
409 ltx->SetTextSize(size);
410 ltx->SetTextColor(kWhite);
411 ltx->SetTextFont(62);
417 gSystem->RedirectOutput("/dev/null");
418 c->Print(c->GetTitle(), tit);
419 gSystem->RedirectOutput(0);
426 //____________________________________________________________________
428 * Make a chapter page
433 void MakeChapter(TCanvas* c, const TString& title)
437 // Info("MakeChapter", "Making chapter %s", title.Data());
438 TLatex* ltx = new TLatex(.5, .5, title);
440 ltx->SetTextAlign(22);
443 PrintCanvas(c, title);
445 //____________________________________________________________________
446 void DrawInPad(TVirtualPad* c, Int_t padNo, TObject* h, Option_t* opts="",
449 // Info("DrawInPad", "Drawing %p in pad # %d of %p w/options %s, flags 0x%x",
450 // h, padNo, c, opts, flags);
451 TVirtualPad* p = c->cd(padNo);
453 Warning("DrawInPad", "Pad # %d not found in %s", padNo, c->GetName());
456 if (flags & 0x1) p->SetLogx();
457 if (flags & 0x2) p->SetLogy();
458 if (flags & 0x4) p->SetLogz();
461 if (o.Contains("colz", TString::kIgnoreCase))
462 p->SetRightMargin(0.15);
463 if (o.Contains("text", TString::kIgnoreCase)) {
464 TH1* hh = static_cast<TH1*>(h);
465 hh->SetMaximum(1.1*hh->GetMaximum());
466 hh->SetMarkerSize(2);
471 Warning("DrawInPad", "Nothing to draw in pad # %d", padNo);
477 TLegend* l = p->BuildLegend();
486 //____________________________________________________________________
487 void CreateTemplates(TLatex*& name, TLatex*& value, Float_t size=.03)
492 name = new TLatex(x1, y, "");
493 name->SetTextAlign(13);
495 name->SetTextSize(size);
497 value = new TLatex(x2, y, "");
498 value->SetTextAlign(13);
500 value->SetTextSize(size);
503 //____________________________________________________________________
504 void DrawParameter(TLatex* name, TLatex* value, Double_t& y,
505 const TString& sName, const TString& sValue)
507 if (sName.IsNull() && sValue.IsNull()) return;
509 name->DrawLatex(name->GetX(), y, Form("%s:", sName.Data()));
510 if (!sValue.IsNull())
511 value->DrawLatex(value->GetX(), y, sValue.Data());
512 y -= name->GetTextSize() + .02;
516 //____________________________________________________________________
517 void DrawCentSum(const TCollection* sums, TCanvas* can, const TString& base,
518 Int_t cLow, Int_t cHigh)
521 if (cLow < 0 || cHigh < 0 || cLow >= cHigh) {
525 else folder.Form("cent%03d_%03d", cLow, cHigh);
527 TCollection* c = GetCollection(sums, folder);
530 TVirtualPad* body = can->cd(2);
533 DrawInPad(body, 1, GetH1(c, "triggers"), "HIST TEXT");
534 DrawInPad(body, 2, GetH1(c, Form("%sEvents",base.Data())), "HIST TEXT");
535 DrawInPad(body, 3, GetH2(c, base.Data()), "colz");
536 DrawInPad(body, 4, GetH2(c, Form("%s0", base.Data())), "colz");
538 PrintCanvas(can, Form("%s sums: %3d%% - %3d%%", base.Data(), cLow, cHigh));
541 //____________________________________________________________________
542 void DrawSums(TDirectory* top, const TString& base, TCanvas* can, bool onlyMB)
544 TCollection* c = GetCollection(top, Form("%sSums", base.Data()));
547 TAxis* centAxis = static_cast<TAxis*>(GetObject(c, "centAxis"));
549 TVirtualPad* body = can->cd(2);
556 CreateTemplates(name, value);
557 for (Int_t i = 1; i <= centAxis->GetNbins(); i++) {
558 DrawParameter(name, value, y, (i == 1 ? "Centrality classes" : ""),
559 Form("%3d%% - %3d%%",
560 Int_t(centAxis->GetBinLowEdge(i)),
561 Int_t(centAxis->GetBinUpEdge(i))));
563 Int_t sys, sNN, scheme, trigger;
564 GetParameter(c, "sNN", sNN);
565 GetParameter(c, "sys", sys);
566 GetParameter(c, "scheme", scheme);
567 GetParameter(c, "trigger", trigger);
568 DrawParameter(name, value, y, "Collision system",
569 (sys == 1 ? "pp" : (sys == 2 ? "PbPb" : (sys == 3 ? "pPb" :
571 DrawParameter(name, value, y, "#sqrt{s_{NN}}", Form("%4dGeV", sNN));
572 DrawParameter(name, value, y, "Normalization scheme", Form("0x%x", scheme));
573 DrawParameter(name, value, y, "Triggers", Form("0x%x", trigger));
575 DrawInPad(body, 2, GetH1(c, "cent"));
577 PrintCanvas(can, Form("%s sums", base.Data()));
579 DrawCentSum(c, can, base, centAxis->GetXmin(), -centAxis->GetXmax());
582 for (Int_t i = 1; i <= centAxis->GetNbins(); i++)
583 DrawCentSum(c, can, base, centAxis->GetBinLowEdge(i),
584 centAxis->GetBinUpEdge(i));
587 //____________________________________________________________________
588 THStack* CleanStack(const THStack* stack, TLegend* l, const TAxis* axis)
590 THStack* ret = new THStack(stack->GetName(), stack->GetTitle());
591 TList* hists = stack->GetHists();
595 while ((h = static_cast<TH1*>(next()))) {
596 TString name(h->GetName());
597 if (name.Contains("_mirror")) continue;
600 name.Form("%3d%% - %3d%%",
601 Int_t(axis->GetBinLowEdge(j)),
602 Int_t(axis->GetBinUpEdge(j)));
603 TLegendEntry* e = l->AddEntry("dummy", name, "f");
604 e->SetFillStyle(1001);
605 e->SetFillColor(h->GetMarkerColor());
611 // There's no dictinary for TObjLink
612 TObjLink* lnk = hists->FirstLink();
615 TH1* h = static_cast<TH1*>(lnk->GetObject());
617 TString name(h->GetName());
618 if (name.Contains("_mirror")) {
619 // AliWarning(Form("No entries in %s - removing", h->GetName()));
624 name.Form("%3d%% - %3d%%",
625 Int_t(axis->GetBinLowEdge(j)),
626 Int_t(axis->GetBinUpEdge(j)));
627 TLegendEntry* e = l->AddEntry("dummy", name, "f");
628 e->SetFillStyle(1001);
629 e->SetFillColor(h->GetMarkerColor());
632 TObjLink* keep = lnk->Next();
642 //____________________________________________________________________
643 void DrawCentRes(const TCollection* sums, TCanvas* can, const TString& base,
644 Int_t cLow, Int_t cHigh)
647 if (cLow < 0 || cHigh < 0 || cLow >= cHigh) {
651 else folder.Form("cent%03d_%03d", cLow, cHigh);
653 TCollection* c = GetCollection(sums, folder);
656 TVirtualPad* body = can->cd(2);
657 body->Divide(2, 3, 0.05, 0);
660 TVirtualPad* p = body->GetPad(trP);
661 p->SetBottomMargin(0.15);
662 p->SetLeftMargin(0.15);
663 if (trP > 2) p->SetTopMargin(0.05);
665 DrawInPad(body, trP, GetH1(c, "triggers"), "HIST TEXT");
666 DrawInPad(body, 2, GetH1(c, Form("norm%s",base.Data())));
667 DrawInPad(body, 4, GetH1(c, Form("dndeta%s",base.Data())));
668 DrawInPad(body, 6, GetH1(c, Form("dndeta%s_rebin05",base.Data())));
669 DrawInPad(body, 5, GetH2(c, Form("d2Ndetadphi%s", base.Data())),"colz");
671 body->GetPad(2)->SetGridx(); body->GetPad(2)->SetLeftMargin(0.15);
672 body->GetPad(4)->SetGridx(); body->GetPad(4)->SetLeftMargin(0.15);
673 body->GetPad(6)->SetGridx(); body->GetPad(6)->SetLeftMargin(0.15);
675 TObject* normCalc = GetObject(c, "normCalc");
676 TString calc = normCalc->GetTitle();
677 TObjArray* lines = calc.Tokenize("\n");
678 TPaveText* disp = new TPaveText(.1,.1,.9,.9, "NDC");
681 while ((line = next()))
682 disp->AddText(line->GetName());
683 disp->SetBorderSize(0);
684 disp->SetBorderSize(0);
685 disp->SetFillStyle(0);
686 DrawInPad(body, 3, disp);
688 PrintCanvas(can, Form("%s result: %3d%% - %3d%%", base.Data(), cLow, cHigh));
691 //____________________________________________________________________
693 DrawRes(TDirectory* top, const TString& base, TCanvas* can, Bool_t onlyMB)
695 TCollection* c = GetCollection(top, Form("%sResults", base.Data()));
698 TAxis* centAxis = static_cast<TAxis*>(GetObject(c, "centAxis"));
705 CreateTemplates(name, value, .02);
706 for (Int_t i = 1; i <= centAxis->GetNbins(); i++) {
707 DrawParameter(name, value, y, (i == 1 ? "Centrality classes" : ""),
708 Form("%3d%% - %3d%%",
709 Int_t(centAxis->GetBinLowEdge(i)),
710 Int_t(centAxis->GetBinUpEdge(i))));
713 DrawParameter(name, value, y, "Collision system",
714 GetObject(c, "sys")->GetTitle());
715 DrawParameter(name, value, y, "#sqrt{s_{NN}}",GetObject(c,"sNN")->GetTitle());
716 DrawParameter(name, value, y, "trigger",GetObject(c,"trigger")->GetTitle());
717 DrawParameter(name, value, y, "scheme", GetObject(c,"scheme")->GetTitle());
719 Double_t epsT, epsT0;
720 GetParameter(c, "triggerEff", epsT);
721 GetParameter(c, "triggerEff0", epsT0);
722 DrawParameter(name, value, y, "#epsilon_{T}", Form("%f", epsT));
723 DrawParameter(name, value, y, "#epsilon_{T,zero bin}", Form("%f", epsT0));
725 PrintCanvas(can, Form("%s results", base.Data()));
727 TVirtualPad* body = can->cd(2);
728 body->Divide(1, 3, 0, 0);
730 TLegend* l = new TLegend(0.1, 0.1, 0.9, 0.9, "Centralities");
734 THStack* dndeta_ = GetStack(c, "dndeta");
735 THStack* dndeta = CleanStack(dndeta_, l, centAxis);
736 THStack* dndeta5_ = GetStack(c, "dndeta_rebin05");
737 THStack* dndeta5 = CleanStack(dndeta5_, 0, 0);
739 DrawInPad(body, 1, l, "");
740 DrawInPad(body, 2, dndeta, "nostack");
741 DrawInPad(body, 3, dndeta5, "nostack");
742 body->GetPad(2)->SetGridx();
743 body->GetPad(3)->SetGridx();
745 PrintCanvas(can, Form("%s results - stacks", base.Data()));
747 DrawCentRes(c, can, base, centAxis->GetXmin(), -centAxis->GetXmax());
748 if (onlyMB) return dndeta;
750 for (Int_t i = 1; i <= centAxis->GetNbins(); i++)
751 DrawCentRes(c, can, base, centAxis->GetBinLowEdge(i),
752 centAxis->GetBinUpEdge(i));
758 //____________________________________________________________________
759 void DrawdNdetaSummary(const char* fname="forward_dndeta.root",
762 // --- Open the file -----------------------------------------------
763 TString filename(fname);
764 TFile* file = TFile::Open(filename.Data(), "READ");
766 Error("DrawAODSummary", "Failed to open \"%s\"", filename.Data());
770 // --- Make our canvas ---------------------------------------------
771 TString pdfName(filename);
772 pdfName.ReplaceAll(".root", ".pdf");
774 TCanvas* c = CreateCanvas(pdfName);
776 // --- Do each sub-algorithm ---------------------------------------
777 DrawSums(file, "Forward", c, onlyMB);
778 THStack* rF = DrawRes(file, "Forward", c, onlyMB);
780 DrawSums(file, "Central", c, onlyMB);
781 THStack* rC = DrawRes(file, "Central", c, onlyMB);
783 TIter next(rF->GetHists());
785 while ((h = static_cast<TH1*>(next()))) rC->Add(h);
788 PrintCanvas(c, "Both");