From 2ccaf9b63c77609725c5644cbe80c974d1315ab6 Mon Sep 17 00:00:00 2001 From: aferrero2707 Date: Fri, 4 Oct 2024 19:46:25 +0200 Subject: [PATCH] [THRatio] fix update of histograms with bin labels A direct copy of the axis objects from the numerator to the ratio histograms, that was introduced in a recent commit, breaks the preservation of the bin labels that are eventually set in the ratio histogram. The code in this commit replaces the copy with an explicit update of the number and limits of the bins, such that it works also if the numerator is filled with `TH1::kCanRebin` set, and therefore the axis range can change after the histogram creation. --- Modules/Common/include/Common/TH1Ratio.inl | 6 +++++- Modules/Common/include/Common/TH2Ratio.inl | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Modules/Common/include/Common/TH1Ratio.inl b/Modules/Common/include/Common/TH1Ratio.inl index 1ce1605aab..2ccf515c8a 100644 --- a/Modules/Common/include/Common/TH1Ratio.inl +++ b/Modules/Common/include/Common/TH1Ratio.inl @@ -218,7 +218,11 @@ void TH1Ratio::update() } T::Reset(); - mHistoNum->GetXaxis()->Copy(*T::GetXaxis()); + if (mHistoNum->GetXaxis()->IsVariableBinSize()) { + T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXbins()->GetArray()); + } else { + T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax()); + } T::SetBinsLength(); // Copy bin labels between histograms. diff --git a/Modules/Common/include/Common/TH2Ratio.inl b/Modules/Common/include/Common/TH2Ratio.inl index a27e34b2c7..2d5226cde7 100644 --- a/Modules/Common/include/Common/TH2Ratio.inl +++ b/Modules/Common/include/Common/TH2Ratio.inl @@ -277,8 +277,16 @@ void TH2Ratio::update() } T::Reset(); - mHistoNum->GetXaxis()->Copy(*T::GetXaxis()); - mHistoNum->GetYaxis()->Copy(*T::GetYaxis()); + if (mHistoNum->GetXaxis()->IsVariableBinSize()) { + T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXbins()->GetArray()); + } else { + T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax()); + } + if (mHistoNum->GetYaxis()->IsVariableBinSize()) { + T::GetYaxis()->Set(mHistoNum->GetYaxis()->GetNbins(), mHistoNum->GetYaxis()->GetXbins()->GetArray()); + } else { + T::GetYaxis()->Set(mHistoNum->GetYaxis()->GetNbins(), mHistoNum->GetYaxis()->GetXmin(), mHistoNum->GetYaxis()->GetXmax()); + } T::SetBinsLength(); // Copy bin labels between histograms.