From 050f576ab23ffeb8be33c26da3471d3ea1daf057 Mon Sep 17 00:00:00 2001 From: aferrero2707 Date: Sun, 22 Sep 2024 17:09:54 +0200 Subject: [PATCH] [THRatio] fix again division of histograms with different labels The ROOT `Divide()` function requires that bin labels, if present, are the same in the numerator and the denominator, otherwise it prints a warning message. The changes in this commit fix the copy of bin labels from numerator to denominator in the THxRatio `update()` functions. --- Modules/Common/include/Common/TH1Ratio.inl | 37 ++++++++++++--- Modules/Common/include/Common/TH2Ratio.inl | 53 +++++++++++++++++----- 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/Modules/Common/include/Common/TH1Ratio.inl b/Modules/Common/include/Common/TH1Ratio.inl index 03d21d7f3d..13dff12960 100644 --- a/Modules/Common/include/Common/TH1Ratio.inl +++ b/Modules/Common/include/Common/TH1Ratio.inl @@ -19,6 +19,28 @@ namespace o2::quality_control_modules::common { +namespace th1ratio_internal +{ + +template +bool copyBinLabels(const T* srcHist, T* destHist) +{ + if (srcHist->GetXaxis()->GetNbins() != destHist->GetXaxis()->GetNbins()) { + return false; + } + + bool result = false; + if (srcHist->GetXaxis()->GetLabels()) { + for (int bin = 1; bin <= srcHist->GetXaxis()->GetNbins(); bin++) { + destHist->GetXaxis()->SetBinLabel(bin, srcHist->GetXaxis()->GetBinLabel(bin)); + } + result = true; + } + return result; +} + +} + template TH1Ratio::TH1Ratio() : T(), @@ -149,6 +171,13 @@ void TH1Ratio::update() T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax()); T::SetBinsLength(); + // Copy bin labels between histograms. + // If set, the bin labels of the ratio histograms are copied to the numerator. + // If instead the numerator has bin labels they are copied to the ratio plot. + if (!th1ratio_internal::copyBinLabels(this, mHistoNum)) { + th1ratio_internal::copyBinLabels(mHistoNum, this); + } + if (mUniformScaling) { T::Add(mHistoNum); double entries = mHistoDen->GetBinContent(1); @@ -160,12 +189,8 @@ void TH1Ratio::update() T::Scale(norm, "nosw2"); } } else { - if (T::GetXaxis()->GetLabels()) { - // copy bin labels to denominator before dividing, otherwise we get a warning - for (int bin = 1; bin <= T::GetXaxis()->GetNbins(); bin++) { - mHistoDen->GetXaxis()->SetBinLabel(bin, T::GetXaxis()->GetBinLabel(bin)); - } - } + // copy bin labels from numerator to denominator (if needed) before dividing, otherwise we get a warning from ROOT + th1ratio_internal::copyBinLabels(mHistoNum, mHistoDen); T::Divide(mHistoNum, mHistoDen, 1.0, 1.0, mBinominalErrors ? "B" : ""); } } diff --git a/Modules/Common/include/Common/TH2Ratio.inl b/Modules/Common/include/Common/TH2Ratio.inl index 9f5541ca0b..4063db4f1a 100644 --- a/Modules/Common/include/Common/TH2Ratio.inl +++ b/Modules/Common/include/Common/TH2Ratio.inl @@ -19,6 +19,37 @@ namespace o2::quality_control_modules::common { +namespace th2ratio_internal +{ + +template +bool copyBinLabels(const T* srcHist, T* destHist) +{ + if (srcHist->GetXaxis()->GetNbins() != destHist->GetXaxis()->GetNbins()) { + return false; + } + if (srcHist->GetYaxis()->GetNbins() != destHist->GetYaxis()->GetNbins()) { + return false; + } + + bool result = false; + if (srcHist->GetXaxis()->GetLabels()) { + for (int bin = 1; bin <= srcHist->GetXaxis()->GetNbins(); bin++) { + destHist->GetXaxis()->SetBinLabel(bin, srcHist->GetXaxis()->GetBinLabel(bin)); + } + result = true; + } + if (srcHist->GetYaxis()->GetLabels()) { + for (int bin = 1; bin <= srcHist->GetYaxis()->GetNbins(); bin++) { + destHist->GetYaxis()->SetBinLabel(bin, srcHist->GetYaxis()->GetBinLabel(bin)); + } + result = true; + } + return result; +} + +} + template TH2Ratio::TH2Ratio() : T(), @@ -150,6 +181,13 @@ void TH2Ratio::update() T::GetYaxis()->Set(mHistoNum->GetYaxis()->GetNbins(), mHistoNum->GetYaxis()->GetXmin(), mHistoNum->GetYaxis()->GetXmax()); T::SetBinsLength(); + // Copy bin labels between histograms. + // If set, the bin labels of the ratio histograms are copied to the numerator. + // If instead the numerator has bin labels they are copied to the ratio plot. + if (!th2ratio_internal::copyBinLabels(this, mHistoNum)) { + th2ratio_internal::copyBinLabels(mHistoNum, this); + } + if (mUniformScaling) { T::Add(mHistoNum); double entries = mHistoDen->GetBinContent(1, 1); @@ -161,17 +199,8 @@ void TH2Ratio::update() T::Scale(norm, "nosw2"); } } else { - // copy bin labels to denominator before dividing, otherwise we get a warning - if (T::GetXaxis()->GetLabels()) { - for (int bin = 1; bin <= T::GetXaxis()->GetNbins(); bin++) { - mHistoDen->GetXaxis()->SetBinLabel(bin, T::GetXaxis()->GetBinLabel(bin)); - } - } - if (T::GetYaxis()->GetLabels()) { - for (int bin = 1; bin <= T::GetYaxis()->GetNbins(); bin++) { - mHistoDen->GetYaxis()->SetBinLabel(bin, T::GetYaxis()->GetBinLabel(bin)); - } - } + // copy bin labels from numerator to denominator (if needed) before dividing, otherwise we get a warning from ROOT + th2ratio_internal::copyBinLabels(mHistoNum, mHistoDen); T::Divide(mHistoNum, mHistoDen, 1.0, 1.0, mBinominalErrors ? "B" : ""); } } @@ -278,7 +307,7 @@ Bool_t TH2Ratio::Add(const TH1* h1, Double_t c1) template void TH2Ratio::SetBins(Int_t nx, Double_t xmin, Double_t xmax, - Int_t ny, Double_t ymin, Double_t ymax) + Int_t ny, Double_t ymin, Double_t ymax) { getNum()->SetBins(nx, xmin, xmax, ny, ymin, ymax); getDen()->SetBins(nx, xmin, xmax, ny, ymin, ymax);