Skip to content

Commit d0e6ecc

Browse files
authored
Common: properly handle Sumw2 in THxRatio classes (#2322)
* [Common] properly handle Sumw2 in THxRatio classes In the original implementation the creation of the sum-of-weights structure was forcibly enabled in the `init()` function, for both the ratio histogram and the internal numerato/denominator ones, without the possibility to disable it. This doubles the size of the produced objects, because of the extra memory needed to store the sum-of-weights structures. The new implementation allows to disable the sum-of-weights on a per-histogram basis, by calling `Sumw2(kFALSE)` immediately after the instantiation of the histogram ratio. This applies to both the ratio histogram and the internal numerator/denominator ones. The sum-of-weights is still enabled by default in the `init()` function of the histogram ratio, to match the previous behaviour and guarantee that errors are correctly computed and propagated at each step. The relevant changes are the following: - added a `Bool_t mSumw2Enabled` member variable to keep track of wether or not the sum-of-weights structure should be updated in the histogram computations - overridden the `void Sumw2(Bool_t flag)` virtual function to propagate the flag to the internal numerator/denominator histograms - modified the `update()` function to call `T::Scale(norm, "nosw2")` when the sum-of-weights structure is not enabled (otherwise the `Scale()` function automatically creates the structure regardless of the user settings) * [Common] changed mSumw2Enabled default value
1 parent c68e06f commit d0e6ecc

4 files changed

Lines changed: 44 additions & 14 deletions

File tree

Modules/Common/include/Common/TH1Ratio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ class TH1Ratio : public T, public o2::mergers::MergeInterface
7070
Bool_t Add(const TH1* h1, const TH1* h2, Double_t c1 = 1, Double_t c2 = 1) override;
7171
Bool_t Add(const TH1* h1, Double_t c1 = 1) override;
7272
void SetBins(Int_t nx, Double_t xmin, Double_t xmax) override;
73+
void Sumw2(Bool_t flag = kTRUE) override;
7374

7475
private:
7576
T* mHistoNum{ nullptr };
7677
T* mHistoDen{ nullptr };
7778
bool mUniformScaling{ true };
79+
Bool_t mSumw2Enabled{ kTRUE };
7880
std::string mTreatMeAs{ T::Class_Name() };
7981

8082
ClassDefOverride(TH1Ratio, 1);

Modules/Common/include/Common/TH1Ratio.inl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ TH1Ratio<T>::~TH1Ratio()
123123
template<class T>
124124
void TH1Ratio<T>::init()
125125
{
126-
if (!mHistoNum || !mHistoDen) {
127-
return;
128-
}
129-
mHistoNum->Sumw2();
130-
mHistoDen->Sumw2();
131-
T::Sumw2();
126+
Sumw2(kTRUE);
132127
}
133128

134129
template<class T>
@@ -159,7 +154,12 @@ void TH1Ratio<T>::update()
159154
if (mUniformScaling) {
160155
double entries = mHistoDen->GetBinContent(1);
161156
double norm = (entries > 0) ? 1.0 / entries : 0;
162-
T::Scale(norm);
157+
// make sure the sum-of-weights structure is not initialized if not required
158+
if (mSumw2Enabled == kTRUE) {
159+
T::Scale(norm);
160+
} else {
161+
T::Scale(norm, "nosw2");
162+
}
163163
} else {
164164
if (T::GetXaxis()->GetLabels()) {
165165
// copy bin labels to denominator before dividing, otherwise we get a warning
@@ -279,4 +279,17 @@ void TH1Ratio<T>::SetBins(Int_t nx, Double_t xmin, Double_t xmax)
279279
T::SetBins(nx, xmin, xmax);
280280
}
281281

282+
template<class T>
283+
void TH1Ratio<T>::Sumw2(Bool_t flag)
284+
{
285+
if (!mHistoNum || !mHistoDen) {
286+
return;
287+
}
288+
289+
mSumw2Enabled = flag;
290+
mHistoNum->Sumw2(flag);
291+
mHistoDen->Sumw2(flag);
292+
T::Sumw2(flag);
293+
}
294+
282295
} // namespace o2::quality_control_modules::common

Modules/Common/include/Common/TH2Ratio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ class TH2Ratio : public T, public o2::mergers::MergeInterface
7070
Bool_t Add(const TH1* h1, const TH1* h2, Double_t c1 = 1, Double_t c2 = 1) override;
7171
Bool_t Add(const TH1* h1, Double_t c1 = 1) override;
7272
void SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax) override;
73+
void Sumw2(Bool_t flag = kTRUE) override;
7374

7475
private:
7576
T* mHistoNum{ nullptr };
7677
T* mHistoDen{ nullptr };
7778
bool mUniformScaling{ true };
79+
Bool_t mSumw2Enabled{ kTRUE };
7880
std::string mTreatMeAs{ T::Class_Name() };
7981

8082
ClassDefOverride(TH2Ratio, 1);

Modules/Common/include/Common/TH2Ratio.inl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ TH2Ratio<T>::~TH2Ratio()
123123
template<class T>
124124
void TH2Ratio<T>::init()
125125
{
126-
if (!mHistoNum || !mHistoDen) {
127-
return;
128-
}
129-
mHistoNum->Sumw2();
130-
mHistoDen->Sumw2();
131-
T::Sumw2();
126+
Sumw2(kTRUE);
132127
}
133128

134129
template<class T>
@@ -160,7 +155,12 @@ void TH2Ratio<T>::update()
160155
if (mUniformScaling) {
161156
double entries = mHistoDen->GetBinContent(1, 1);
162157
double norm = (entries > 0) ? 1.0 / entries : 0;
163-
T::Scale(norm);
158+
// make sure the sum-of-weights structure is not initialized if not required
159+
if (mSumw2Enabled == kTRUE) {
160+
T::Scale(norm);
161+
} else {
162+
T::Scale(norm, "nosw2");
163+
}
164164
} else {
165165
// copy bin labels to denominator before dividing, otherwise we get a warning
166166
if (T::GetXaxis()->GetLabels()) {
@@ -286,4 +286,17 @@ void TH2Ratio<T>::SetBins(Int_t nx, Double_t xmin, Double_t xmax,
286286
T::SetBins(nx, xmin, xmax, ny, ymin, ymax);
287287
}
288288

289+
template<class T>
290+
void TH2Ratio<T>::Sumw2(Bool_t flag)
291+
{
292+
if (!mHistoNum || !mHistoDen) {
293+
return;
294+
}
295+
296+
mSumw2Enabled = flag;
297+
mHistoNum->Sumw2(flag);
298+
mHistoDen->Sumw2(flag);
299+
T::Sumw2(flag);
300+
}
301+
289302
} // namespace o2::quality_control_modules::common

0 commit comments

Comments
 (0)