55#include <TDatabasePDG.h>
56#include <TEventList.h>
60#include <TGraphErrors.h>
63#include <TObjString.h>
67#include <TProfile2D.h>
81 PairAnalysisSpectrum::PairAnalysisSpectrum()
82 : PairAnalysisSpectrum(
"spectrum",
"title")
90PairAnalysisSpectrum::PairAnalysisSpectrum(
const char* name,
const char* title)
100 fTree =
new TTree(
"PAPa",
"PAPa-Spectrum");
102 for (Int_t i = 0; i < 100; i++)
107PairAnalysisSpectrum::~PairAnalysisSpectrum()
112 if (fResults)
delete fResults;
113 if (fExtractions)
delete fExtractions;
117void PairAnalysisSpectrum::AddInput(TObjArray* raw, TString identifier, TObjArray* mc, TObjArray* truth)
124 if (mc) fMCInput.Add(mc);
125 if (truth) fMCTruth.Add(truth);
126 fInputKeys[fIdx] = identifier;
131void PairAnalysisSpectrum::Init()
136 fTree->Branch(
"Extraction", &fExt);
141void PairAnalysisSpectrum::Process()
150 TIter nextRaw(&fRawInput);
153 PairAnalysisHistos*
h = NULL;
155 TObject* objMC = NULL;
157 PairAnalysisHistos* hMC = NULL;
159 TObject* objMCtruth = NULL;
162 while ((obj = nextRaw())) {
164 TObject::Info(
"Process",
"Check Extraction for %s", fInputKeys[i].Data());
165 TObject::Info(
"Process",
"------------------------------------------------------------"
167 TObject::Info(
"Process",
"Input type: %s \n", obj->ClassName());
170 TObject::Info(
"Process",
"Binning provided for %s from %.3f to %.3f", fVar.Data(), fVarBinning->Min(),
175 TObjArray* histArr =
dynamic_cast<TObjArray*
>(obj);
177 if (!(
h =
dynamic_cast<PairAnalysisHistos*
>(obj))) {
179 TObject::Error(
"Process",
"No input format found");
191 objMC = fMCInput.At(i);
192 TObjArray* histArrMC = NULL;
194 TObject::Info(
"Process",
"Input MC type: %s \n", objMC->ClassName());
195 histArrMC =
dynamic_cast<TObjArray*
>(objMC);
197 if (!(hMC =
dynamic_cast<PairAnalysisHistos*
>(objMC))) {
199 TObject::Error(
"Process",
"No MC input format found");
207 objMCtruth = fMCTruth.At(i);
208 TObjArray* histArrMCt = NULL;
210 TObject::Info(
"Process",
"Input MC truth type: %s \n", objMCtruth->ClassName());
211 histArrMCt =
dynamic_cast<TObjArray*
>(objMCtruth);
219 if (!histArr &&
h) histArr =
h->DrawSame(
"pM-wghtWeight",
224 if (!histArrMC && hMC) histArrMC = hMC->DrawSame(
"pM",
"onlymc eff goff");
229 if (gErrorIgnoreLevel < kWarning) sig->
Print(
"");
235 fExt->setup = fInputKeys[i];
244 fExt->sb = sig->
GetSB();
248 fExt->HistSignal = NULL;
260 if (
h && !
h->SetCutClass(fInputKeys[i].Data()))
continue;
262 if (!histArr &&
h) histArr =
h->DrawSame(Form(
"pM_%s", fVar.Data()),
"nomc goff");
267 tmpArr.SetOwner(kFALSE);
271 if (!histArrMC && hMC) {
272 histArrMC = hMC->DrawSame(Form(
"p%s", fVar.Data()),
"goff sel",
276 TH1* tmpMCnom = (TH1*) histArrMC->At(0);
277 if (histArrMC->GetEntriesFast() < 2)
return;
278 TH1* tmpMCden = (TH1*) histArrMC->At(1);
280 histMC = tmpMCnom->Rebin(fVarBinning->GetNrows() - 1,
"effMC", fVarBinning->GetMatrixArray());
281 TH1* histMCden = tmpMCden->Rebin(fVarBinning->GetNrows() - 1,
"effMCden", fVarBinning->GetMatrixArray());
282 histMC->Divide(histMCden);
286 if (histMC) TObject::Info(
"Process",
"MC histogram found and rebinned");
289 TH1* histMCtruth = NULL;
291 TH1* tmpMCtrue = (TH1*) histArrMCt->At(0);
292 if (!tmpMCtrue)
return;
294 histMCtruth = tmpMCtrue->Rebin(fVarBinning->GetNrows() - 1,
"sMCtrue", fVarBinning->GetMatrixArray());
297 if (histMCtruth) TObject::Info(
"Process",
"MCtruth reference histogram found and rebinned");
300 for (Int_t bin = 0; bin < fVarBinning->GetNrows() - 1; bin++) {
304 Double_t xLo = fVarBinning->GetMatrixArray()[bin];
305 Double_t xHi = fVarBinning->GetMatrixArray()[bin + 1] - 0.000001;
307 Int_t binLo = histPM->GetYaxis()->FindBin(xLo);
308 Int_t binHi = histPM->GetYaxis()->FindBin(xHi);
310 Double_t fndLo = histPM->GetYaxis()->GetBinLowEdge(binLo);
311 Double_t fndHi = histPM->GetYaxis()->GetBinLowEdge(binHi + 1);
313 TObject::Info(
"Process",
"Bin %d: %.3f < %s < %.3f", bin, fndLo, fVar.Data(), fndHi);
316 for (Int_t ih = 0; ih < histArr->GetEntriesFast(); ih++) {
317 TH2* hist = (TH2*) histArr->UncheckedAt(ih);
318 tmpArr.Add(hist->ProjectionX(hist->GetTitle(), binLo, binHi,
"e"));
324 if (gErrorIgnoreLevel < kWarning) sig->
Print(
"");
331 fExt->setup = fInputKeys[i];
334 fExt->var = (fndHi - fndLo) / 2 + fndLo;
335 fExt->varE = (fndHi - fndLo) / 2;
340 fExt->sb = sig->
GetSB();
344 fExt->HistSignal = NULL;
345 fExt->eff = (histMC ? histMC->GetBinContent(bin + 1) : 1.);
346 fExt->effE = (histMC ? histMC->GetBinError(bin + 1) : 0.);
348 fExt->sref = (histMCtruth ? histMCtruth->GetBinContent(bin + 1) : 0.);
349 fExt->srefE = (histMCtruth ? histMCtruth->GetBinError(bin + 1) : 0.);
366 if (histMC)
delete histMC;
367 if (histMCtruth)
delete histMCtruth;
377void PairAnalysisSpectrum::DrawSpectrum(
const char* varexp,
const char* selection, Option_t* option)
395 TString optString(option);
397 printf(
"Plot spectrum: '%s' \t selection: '%s' \t options: '%s' \n", varexp, selection, optString.Data());
398 Bool_t optLegFull = optString.Contains(
"legf");
399 optString.ReplaceAll(
"legf",
"");
400 Bool_t optLeg = optString.Contains(
"leg");
401 optString.ReplaceAll(
"leg",
"");
402 Bool_t optSyst = optString.Contains(
"syst");
403 optString.ReplaceAll(
"syst",
"");
404 Bool_t optPrint = optString.Contains(
"print");
405 optString.ReplaceAll(
"print",
"");
406 Bool_t optSamePad = optString.Contains(
"samepad");
407 optString.ReplaceAll(
"samepad",
"");
416 TString ckey(varexp);
417 Int_t ndim = ckey.CountChar(
':') + 1;
420 TObjArray* carr = ckey.Tokenize(
":");
425 TObjArray* oarr = ckey.Tokenize(
"+-*/:");
427 TString fkey = ((TObjString*) oarr->At(0))->GetString();
431 ckey.ReplaceAll(
"/",
"#");
433 if (optSamePad) c = gPad->GetCanvas();
435 c = (TCanvas*) gROOT->FindObject(ckey.Data());
437 TObject::Info(
"Draw",
"create new canvas: '%s'", ckey.Data());
438 c =
new TCanvas(ckey.Data(), ckey.Data());
445 TList* prim = gPad->GetListOfPrimitives();
447 for (Int_t io = 0; io < prim->GetSize(); io++) {
449 if (obj->InheritsFrom(TGraph::Class()) && obj != prim->At(io + 1)) nobj++;
453 if ((optLeg && !nobj)) {
455 leg =
new TLegend(0. + gPad->GetLeftMargin() + gStyle->GetTickLength(
"Y"),
456 0. + gPad->GetBottomMargin() + gStyle->GetTickLength(
"X"),
457 1. - gPad->GetRightMargin() + gStyle->GetTickLength(
"Y"),
458 1. - gPad->GetTopMargin() + gStyle->GetTickLength(
"X"), GetName(),
"nbNDC");
460 else if (optLeg && nobj) {
461 leg = (TLegend*) prim->FindObject(
"TPave");
464 Info(
"Draw",
"Basics: nobj: %d \t leg: %p", nobj, leg);
467 if (optString.Contains(
"logx")) gPad->SetLogx();
468 if (optString.Contains(
"logy")) gPad->SetLogy();
469 if (optString.Contains(
"logz")) gPad->SetLogz();
470 optString.ReplaceAll(
"logx",
"");
471 optString.ReplaceAll(
"logy",
"");
472 optString.ReplaceAll(
"logz",
"");
474 if (ndim < 3 && !ckey.Contains(
"signal->")) {
477 TString varkey(varexp);
480 n = fTree->Draw(
">>elist", selection);
485 TEventList* elist = gDirectory->Get<TEventList>(
"elist");
487 elist->SetReapplyCut(kTRUE);
489 fTree->SetEventList(elist);
494 if (varkey.Contains(
"setup")) {
497 hist->SetNameTitle(varexp, selection);
500 for (Long64_t i = 0; i < n; i++) {
502 hist->Fill((fExt->setup).Data(), 1.);
507 hist->GetXaxis()->SetRange(1, n);
511 TString errkey = ((TObjString*) carr->At(0))->GetString();
512 errkey.ReplaceAll(fkey, fkey +
"E");
517 if (!fkey.Contains(
"E")) {
518 varkey.Append(errkey.Prepend(
":"));
519 Info(
"Draw",
" Appended '%s' by '%s' for error caluclation", varkey.Data(), errkey.Data());
524 fTree->Draw(varkey, selection,
"goff");
527 Double_t* xval =
new Double_t[fTree->GetSelectedRows()];
528 for (Int_t ix = 0; ix < fTree->GetSelectedRows(); ix++)
532 TGraphErrors*
gr = 0x0;
533 if (ndim > 1)
gr =
new TGraphErrors(fTree->GetSelectedRows(), fTree->GetV2(), fTree->GetV1(), 0, fTree->GetV3());
535 gr =
new TGraphErrors(fTree->GetSelectedRows(), fTree->GetV1(), xval, fTree->GetV2(), 0);
545 TString sel(selection);
546 sel.ReplaceAll(
"setup.Contains",
"");
547 sel.ReplaceAll(
"(\"",
"*");
548 sel.ReplaceAll(
"\")",
"*");
549 sel.ReplaceAll(
"setup==",
"");
550 sel.ReplaceAll(
"\"",
"");
551 gr->SetName(Form(
"%s", sel.Data()));
552 if (sel.Contains(
"setupId==") && sel.Length() < 11) {
553 sel.ReplaceAll(
"setupId==",
"");
554 Int_t iId = sel.Atoi();
555 gr->SetName(Form(
"%s", fInputKeys[iId].Data()));
557 if (optSyst)
gr->SetName(GetTitle());
558 if (fkey.Contains(
"ref"))
gr->SetName(
"MC-truth");
562 TGraphErrors* grE = NULL;
563 TGraphErrors* grS = NULL;
564 TGraphErrors* grC = NULL;
565 if (optSyst && fVarBinning) {
566 grE =
new TGraphErrors();
567 grS =
new TGraphErrors();
568 grC =
new TGraphErrors();
569 Double_t* gx =
gr->GetX();
570 Double_t* gy =
gr->GetY();
571 Double_t* gye =
gr->GetEY();
583 Double_t xvar = gx[
first];
584 for (Int_t i =
first; i <
gr->GetN(); i++) {
587 if ((gx[i] - xvar) > 1.e-8)
break;
593 ysys /= (nsys ? nsys : 1);
598 for (Int_t i = 0; i < nsys; i++) {
603 case ESystMethod::kBarlow:
605 uce = TMath::Sqrt(TMath::Abs(gye[j] * gye[j] - gye[
first] * gye[
first]));
608 esys = TMath::Max(esys, TMath::Abs(ysys - gy[j]) - 0.9 * uce);
610 case ESystMethod::kSystMax: esys = TMath::Max(esys, TMath::Abs(ysys - gy[j]));
break;
611 case ESystMethod::kSystRMS: esys += gy[j] * gy[j];
break;
618 case ESystMethod::kBarlow:
break;
619 case ESystMethod::kSystMax:
break;
620 case ESystMethod::kSystRMS: esys = TMath::Sqrt(TMath::Abs(esys / (nsys ? nsys : 1) - ysys * ysys));
break;
624 grE->SetPoint(ibin, xvar, ysys);
625 grE->SetPointError(ibin, 0.0, gye[
first]);
627 Double_t boxW = (fVarBinning->Max() - fVarBinning->Min()) / (fVarBinning->GetNrows() - 1);
628 grS->SetPoint(ibin, xvar, ysys);
629 grS->SetPointError(ibin, boxW * 0.35, esys);
632 grC->SetPoint(ibin, xvar, ysys);
633 grC->SetPointError(ibin, boxW * 0.35, TMath::Sqrt(esys * esys + gye[
first] * gye[
first]));
642 grC =
new TGraphErrors(*
gr);
645 if (optPrint) grC->Print();
647 Info(
"Draw",
" Draw object with options: '%s'", optString.Data());
650 gr->Draw((optString +
"same").Data());
653 Double_t* valE = grC->GetEY();
654 Double_t* val = grC->GetY();
655 Int_t npnts = grC->GetN();
657 TMath::Sort(npnts, val, idx, kTRUE);
659 Double_t errmin = (TMath::IsNaN(valE[idx[npnts - 1]]) ? 0. : valE[idx[npnts - 1]]);
660 Double_t min = (val[idx[npnts - 1]] - errmin) * 0.9;
663 Double_t errmax = (TMath::IsNaN(valE[idx[0]]) ? 0. : valE[idx[0]]);
664 Double_t max = (val[idx[0]] + errmax) * 1.1;
672 grE->Draw((optString +
"A").Data());
674 grS->SetFillColor(grS->GetLineColor());
675 grS->SetFillStyle(kFEmpty);
680 TString legOpt = optString +
"L";
681 legOpt.ReplaceAll(
"hist",
"");
682 legOpt.ReplaceAll(
"scat",
"");
683 if (legOpt.Contains(
"col")) legOpt =
"";
684 legOpt.ReplaceAll(
"z",
"");
685 legOpt.ReplaceAll(
"e",
"");
687 TString legkey =
gr->GetName();
688 if (leg) leg->AddEntry(
gr,
gr->GetName(), legOpt.Data());
695 fTree->Draw(varexp, selection, optString.Data());
696 fprintf(stderr,
"use plain TTree::Draw command \n");
706 TObjArray* arr = var.Tokenize(
":");
709 TString yt =
"Entries";
710 xt = ((TObjString*) arr->At(0))->GetString();
712 else if (xt.EqualTo(
"s"))
714 else if (xt.EqualTo(
"b"))
716 else if (xt.EqualTo(
"sgn"))
718 else if (xt.EqualTo(
"var"))
720 else if (xt.Contains(
"var")) {
725 if (arr->GetEntriesFast() < 2) {
731 xt = ((TObjString*) arr->At(1))->GetString();
733 else if (xt.EqualTo(
"s"))
735 else if (xt.EqualTo(
"b"))
737 else if (xt.EqualTo(
"sgn"))
739 else if (xt.EqualTo(
"var"))
741 else if (xt.Contains(
"var")) {
753 if (carr)
delete carr;
763 if (!nobj) leg->Draw();
768 fTree->SetEventList(0);
772void PairAnalysisSpectrum::Write()
779 TFile* oldFile = gFile;
780 TDirectory* oldDir = gDirectory;
782 TFile* fout =
new TFile(
"test.root",
"RECREATE");
794void PairAnalysisSpectrum::Fit(TString drawoption)
804 drawoption.ToLower();
805 Bool_t optLeg = drawoption.Contains(
"leg");
806 drawoption.ReplaceAll(
"leg",
"");
810 Info(
"Fit",
"Spectrum fit method: %s", fFuncSigBack->GetName());
811 Int_t fitResult = fSignal->Fit(fFuncSigBack, (fFitOpt +
"EX0").Data());
814 if (fitResult != 0) {
815 Error(
"Fit",
"fit has error/issue (%d)", fitResult);
820 fFuncSigBack->SetLineColor(fSignal->GetLineColor());
824 TF1* fit = fFuncSigBack->DrawCopy((drawoption +
"same").Data());
827 fDof = fFuncSigBack->GetNDF();
828 if (fDof) fChi2Dof = fFuncSigBack->GetChisquare() / fFuncSigBack->GetNDF();
832 TList* prim = gPad->GetListOfPrimitives();
833 TLegend* leg = (TLegend*) prim->FindObject(
"TPave");
834 TString legkey = fFuncSigBack->GetName();
837 leg->AddEntry(fit, fFuncSigBack->GetName(), drawoption.Data());
ClassImp(PairAnalysisSpectrum) PairAnalysisSpectrum
Data class with information on a STS local track.
Int_t GetParticleOfInterest() const
void Print(Option_t *option="") const
static const char * GetValueName(Int_t i)
Double_t GetBackground() const
void Process(TObjArray *const arrhist)
Double_t GetSignalError() const
Double_t GetSignificanceError() const
Double_t GetSBError() const
TObject * FindObject(TObjArray *arrhist, PairAnalysis::EPairType type) const
Double_t GetSignal() const
Double_t GetBackgroundError() const
Double_t GetSignificance() const
static const char * GetValueUnit(Int_t i)
static UInt_t GetValueType(const char *valname)
static const char * GetValueLabel(Int_t i)
TH1 * GetFirstHistogram()
void Style(TObject *obj, Int_t idx=0)
void SetLegendAttributes(TLegend *leg, Bool_t fill=kFALSE)