CbmRoot
Loading...
Searching...
No Matches
CbmQaEff.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#include "CbmQaEff.h"
11
12#include "Logger.h"
13#include "TString.h"
14
15// ---------------------------------------------------------------------------------------------------------------------
16//
17CbmQaEff::CbmQaEff() : TEfficiency() {}
18
19// ---------------------------------------------------------------------------------------------------------------------
20//
21CbmQaEff::CbmQaEff(const CbmQaEff& other) : TEfficiency(other) {}
22
23// ---------------------------------------------------------------------------------------------------------------------
24//
25std::tuple<double, double, double> CbmQaEff::GetTotalEfficiency() const
26{
27 // The efficiency is always calculated based on the number of passed events and total events, which are stored in the
28 // corresponding histograms inside the TEfficiency object. To get an integrated efficiency one needs just carefully
29 // re-bin these histograms and pass them to the TEfficiency ctor.
30 auto pHistP = std::unique_ptr<TH1>((TH1*) this->GetPassedHistogram()->Clone());
31 auto pHistT = std::unique_ptr<TH1>((TH1*) this->GetTotalHistogram()->Clone());
32
33 // X-axis range to calculate integrated efficiency
34 double range[2] = {pHistP->GetXaxis()->GetXmin(), pHistP->GetXaxis()->GetXmax()};
35
36 // Define the re-binned histograms
37 std::unique_ptr<TH1> pHistPR = nullptr; // re-binned histogram of passed events
38 std::unique_ptr<TH1> pHistTR = nullptr; // re-binned histogram of total events
39
40 if (GetDimension() == 1) {
41 pHistPR = std::unique_ptr<TH1>(pHistP->Rebin(1, "tmp_passed", range));
42 pHistTR = std::unique_ptr<TH1>(pHistT->Rebin(1, "tmp_total", range));
43 }
44 if (GetDimension() == 2) {
45 auto pHistPProj = std::unique_ptr<TH1>(static_cast<TH2*>(pHistP.get())->ProjectionX());
46 auto pHistTProj = std::unique_ptr<TH1>(static_cast<TH2*>(pHistT.get())->ProjectionX());
47
48 pHistPR = std::unique_ptr<TH1>(pHistPProj->Rebin(1, "tmp_passed", range));
49 pHistTR = std::unique_ptr<TH1>(pHistTProj->Rebin(1, "tmp_total", range));
50 }
51
52 // Define temporary efficiency, copy all the attributes
53 auto pIntEff = std::make_unique<TEfficiency>(*pHistPR, *pHistTR);
54 pIntEff->SetBetaAlpha(fBeta_alpha);
55 pIntEff->SetBetaBeta(fBeta_beta);
56 pIntEff->SetConfidenceLevel(fConfLevel);
57 pIntEff->SetWeight(fWeight);
58 pIntEff->SetStatisticOption(fStatisticOption);
59 pIntEff->SetPosteriorMode(this->UsesPosteriorMode());
60 pIntEff->SetCentralInterval(this->UsesCentralInterval());
61
62 double effV = pIntEff->GetEfficiency(1);
63 double effL = pIntEff->GetEfficiencyErrorLow(1);
64 double effU = pIntEff->GetEfficiencyErrorUp(1);
65
66 return {effV, effL, effU};
67}
68
69
70// ---------------------------------------------------------------------------------------------------------------------
71//
72CbmQaEff* CbmQaEff::Integral(double lo, double hi)
73{
74 if (GetDimension() != 1) {
75 return nullptr;
76 } // For now efficiency integration works only for 1D
77
78 // Underlying histograms with passed and total events
79 auto* pPassed = (TH1D*) (this->GetPassedHistogram());
80 auto* pTotal = (TH1D*) (this->GetTotalHistogram());
81
82 if (!pPassed) {
83 return nullptr;
84 }
85
86 // Integration range
87 double range[2] = {0};
88 if (lo < hi) {
89 range[0] = lo;
90 range[1] = hi;
91 }
92 else {
93 range[0] = pPassed->GetXaxis()->GetXmin();
94 range[1] = pPassed->GetXaxis()->GetXmax();
95 }
96
97 // Re-binned histograms for a selected integration range
98 auto& histPassedReb = *(pPassed->Rebin(1, "ptmp", range));
99 auto& histTotalReb = *(pTotal->Rebin(1, "ttmp", range));
100
101 TString sName = Form("%s_integrated", this->GetName());
102
103 // New efficiency
104 auto* pIntEff = new CbmQaEff(TEfficiency(histPassedReb, histTotalReb));
105
106 pIntEff->SetName(sName);
107 return pIntEff;
108}
109
110// ---------------------------------------------------------------------------------------------------------------------
111//
112CbmQaEff* CbmQaEff::DrawCopy(Option_t* opt, const char* postfix) const
113{
114 TString option = opt;
115 option.ToLower();
116 if (gPad && !option.Contains("same")) {
117 gPad->Clear();
118 }
119 TString newName = "";
120 if (postfix) {
121 newName.Form("%s%s", GetName(), postfix);
122 }
123 CbmQaEff* pNewEff = (CbmQaEff*) Clone(newName.Data());
124 pNewEff->SetDirectory(nullptr);
125 pNewEff->SetBit(kCanDelete);
126 if (gPad) gPad->IncrementPaletteColor(1, option);
127 pNewEff->AppendPad(option);
128 return pNewEff;
129}
130
131// ---------------------------------------------------------------------------------------------------------------------
132//
134{
135 if (!fpStats) {
136 fpStats = new TPaveText(0.20, 0.17, 0.80, 0.24, "NDC");
137 fpStats->SetFillColor(0);
138
139 // Add integrated efficiency
140 auto [effVal, effLErr, effUErr] = this->GetTotalEfficiency();
141
142 fpStats->SetTextFont(42);
143 fpStats->SetTextSize(0.05);
144 fpStats->SetBorderSize(0);
145 fpStats->SetFillColor(0);
146 fpStats->AddText(0, 0,
147 Form("#epsilon_{tot} = %.4f_{-%.4f}^{+%.4f} (CL %3.3f)", effVal, effLErr, effUErr, fConfLevel));
148 }
149}
Declaration of CbmQaEff class.
CbmQaEff * Integral(double lo, double hi)
Definition CbmQaEff.cxx:72
std::tuple< double, double, double > GetTotalEfficiency() const
Definition CbmQaEff.cxx:25
void SetStats()
Sets statistics box for efficiency.
Definition CbmQaEff.cxx:133
CbmQaEff * DrawCopy(Option_t *opt, const char *postfix="_copy") const
Definition CbmQaEff.cxx:112
TPaveText * fpStats
Statistics box.
Definition CbmQaEff.h:96
CbmQaEff()
Default constructor.
Definition CbmQaEff.cxx:17