Skip to content

Commit f87e83a

Browse files
authored
[Common] add support for variable bins in THxRatio (#2438)
The added support for variable bins requires two changes: * implementation of the corresponding constructors * the histogram axes need to be updated using the `TAxis::Copy()` function
1 parent 30c12d0 commit f87e83a

4 files changed

Lines changed: 159 additions & 3 deletions

File tree

Modules/Common/include/Common/TH1Ratio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class TH1Ratio : public T, public o2::mergers::MergeInterface
3232
TH1Ratio();
3333
TH1Ratio(TH1Ratio const& copymerge);
3434
TH1Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, bool uniformScaling = false);
35+
TH1Ratio(const char* name, const char* title, int nbinsx, float* xbins, bool uniformScaling = false);
36+
TH1Ratio(const char* name, const char* title, int nbinsx, double* xbins, bool uniformScaling = false);
3537
TH1Ratio(const char* name, const char* title, bool uniformScaling = false);
3638

3739
~TH1Ratio();

Modules/Common/include/Common/TH1Ratio.inl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,56 @@ TH1Ratio<T>::TH1Ratio(const char* name, const char* title, int nbinsx, double xm
105105
init();
106106
}
107107

108+
template<class T>
109+
TH1Ratio<T>::TH1Ratio(const char* name, const char* title, int nbinsx, float* xbins, bool uniformScaling)
110+
: T(name, title, nbinsx, xbins),
111+
o2::mergers::MergeInterface(),
112+
mUniformScaling(uniformScaling)
113+
{
114+
// do not add created histograms to gDirectory
115+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
116+
{
117+
TString nameNum = T::GetName() + TString("_num");
118+
TString nameDen = T::GetName() + TString("_den");
119+
TString titleNum = T::GetTitle() + TString(" num");
120+
TString titleDen = T::GetTitle() + TString(" den");
121+
TDirectory::TContext ctx(nullptr);
122+
mHistoNum = new T(nameNum, titleNum, nbinsx, xbins);
123+
if (mUniformScaling) {
124+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1);
125+
} else {
126+
mHistoDen = new T(nameDen, titleDen, nbinsx, xbins);
127+
}
128+
}
129+
130+
init();
131+
}
132+
133+
template<class T>
134+
TH1Ratio<T>::TH1Ratio(const char* name, const char* title, int nbinsx, double* xbins, bool uniformScaling)
135+
: T(name, title, nbinsx, xbins),
136+
o2::mergers::MergeInterface(),
137+
mUniformScaling(uniformScaling)
138+
{
139+
// do not add created histograms to gDirectory
140+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
141+
{
142+
TString nameNum = T::GetName() + TString("_num");
143+
TString nameDen = T::GetName() + TString("_den");
144+
TString titleNum = T::GetTitle() + TString(" num");
145+
TString titleDen = T::GetTitle() + TString(" den");
146+
TDirectory::TContext ctx(nullptr);
147+
mHistoNum = new T(nameNum, titleNum, nbinsx, xbins);
148+
if (mUniformScaling) {
149+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1);
150+
} else {
151+
mHistoDen = new T(nameDen, titleDen, nbinsx, xbins);
152+
}
153+
}
154+
155+
init();
156+
}
157+
108158
template<class T>
109159
TH1Ratio<T>::TH1Ratio(const char* name, const char* title, bool uniformScaling)
110160
: T(name, title, 10, 0, 10),
@@ -168,7 +218,7 @@ void TH1Ratio<T>::update()
168218
}
169219

170220
T::Reset();
171-
T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax());
221+
mHistoNum->GetXaxis()->Copy(*T::GetXaxis());
172222
T::SetBinsLength();
173223

174224
// Copy bin labels between histograms.

Modules/Common/include/Common/TH2Ratio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class TH2Ratio : public T, public o2::mergers::MergeInterface
3232
TH2Ratio();
3333
TH2Ratio(TH2Ratio const& copymerge);
3434
TH2Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, bool uniformScaling = false);
35+
TH2Ratio(const char* name, const char* title, int nbinsx, const float* xbins, int nbinsy, const float* ybins, bool uniformScaling = false);
36+
TH2Ratio(const char* name, const char* title, int nbinsx, const double* xbins, int nbinsy, const double* ybins, bool uniformScaling = false);
37+
TH2Ratio(const char* name, const char* title, int nbinsx, const double* xbins, int nbinsy, double ymin, double ymax, bool uniformScaling = false);
38+
TH2Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, int nbinsy, const double* ybins, bool uniformScaling = false);
3539
TH2Ratio(const char* name, const char* title, bool uniformScaling = false);
3640

3741
~TH2Ratio();

Modules/Common/include/Common/TH2Ratio.inl

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,106 @@ TH2Ratio<T>::TH2Ratio(const char* name, const char* title, int nbinsx, double xm
114114
init();
115115
}
116116

117+
template<class T>
118+
TH2Ratio<T>::TH2Ratio(const char* name, const char* title, int nbinsx, const float *xbins, int nbinsy, const float *ybins, bool uniformScaling)
119+
: T(name, title, nbinsx, xbins, nbinsy, ybins),
120+
o2::mergers::MergeInterface(),
121+
mUniformScaling(uniformScaling)
122+
{
123+
// do not add created histograms to gDirectory
124+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
125+
{
126+
TString nameNum = T::GetName() + TString("_num");
127+
TString nameDen = T::GetName() + TString("_den");
128+
TString titleNum = T::GetTitle() + TString(" num");
129+
TString titleDen = T::GetTitle() + TString(" den");
130+
TDirectory::TContext ctx(nullptr);
131+
mHistoNum = new T(nameNum, titleNum, nbinsx, xbins, nbinsy, ybins);
132+
if (mUniformScaling) {
133+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1, 1, -1, 1);
134+
} else {
135+
mHistoDen = new T(nameDen, titleDen, nbinsx, xbins, nbinsy, ybins);
136+
}
137+
}
138+
139+
init();
140+
}
141+
142+
template<class T>
143+
TH2Ratio<T>::TH2Ratio(const char* name, const char* title, int nbinsx, const double *xbins, int nbinsy, const double *ybins, bool uniformScaling)
144+
: T(name, title, nbinsx, xbins, nbinsy, ybins),
145+
o2::mergers::MergeInterface(),
146+
mUniformScaling(uniformScaling)
147+
{
148+
// do not add created histograms to gDirectory
149+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
150+
{
151+
TString nameNum = T::GetName() + TString("_num");
152+
TString nameDen = T::GetName() + TString("_den");
153+
TString titleNum = T::GetTitle() + TString(" num");
154+
TString titleDen = T::GetTitle() + TString(" den");
155+
TDirectory::TContext ctx(nullptr);
156+
mHistoNum = new T(nameNum, titleNum, nbinsx, xbins, nbinsy, ybins);
157+
if (mUniformScaling) {
158+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1, 1, -1, 1);
159+
} else {
160+
mHistoDen = new T(nameDen, titleDen, nbinsx, xbins, nbinsy, ybins);
161+
}
162+
}
163+
164+
init();
165+
}
166+
167+
template<class T>
168+
TH2Ratio<T>::TH2Ratio(const char* name, const char* title, int nbinsx, const double *xbins, int nbinsy, double ymin, double ymax, bool uniformScaling)
169+
: T(name, title, nbinsx, xbins, nbinsy, ymin, ymax),
170+
o2::mergers::MergeInterface(),
171+
mUniformScaling(uniformScaling)
172+
{
173+
// do not add created histograms to gDirectory
174+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
175+
{
176+
TString nameNum = T::GetName() + TString("_num");
177+
TString nameDen = T::GetName() + TString("_den");
178+
TString titleNum = T::GetTitle() + TString(" num");
179+
TString titleDen = T::GetTitle() + TString(" den");
180+
TDirectory::TContext ctx(nullptr);
181+
mHistoNum = new T(nameNum, titleNum, nbinsx, xbins, nbinsy, ymin, ymax);
182+
if (mUniformScaling) {
183+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1, 1, -1, 1);
184+
} else {
185+
mHistoDen = new T(nameDen, titleDen, nbinsx, xbins, nbinsy, ymin, ymax);
186+
}
187+
}
188+
189+
init();
190+
}
191+
192+
template<class T>
193+
TH2Ratio<T>::TH2Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, int nbinsy, const double *ybins, bool uniformScaling)
194+
: T(name, title, nbinsx, xmin, xmax, nbinsy, ybins),
195+
o2::mergers::MergeInterface(),
196+
mUniformScaling(uniformScaling)
197+
{
198+
// do not add created histograms to gDirectory
199+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
200+
{
201+
TString nameNum = T::GetName() + TString("_num");
202+
TString nameDen = T::GetName() + TString("_den");
203+
TString titleNum = T::GetTitle() + TString(" num");
204+
TString titleDen = T::GetTitle() + TString(" den");
205+
TDirectory::TContext ctx(nullptr);
206+
mHistoNum = new T(nameNum, titleNum, nbinsx, xmin, xmax, nbinsy, ybins);
207+
if (mUniformScaling) {
208+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1, 1, -1, 1);
209+
} else {
210+
mHistoDen = new T(nameDen, titleDen, nbinsx, xmin, xmax, nbinsy, ybins);
211+
}
212+
}
213+
214+
init();
215+
}
216+
117217
template<class T>
118218
TH2Ratio<T>::TH2Ratio(const char* name, const char* title, bool uniformScaling)
119219
: T(name, title, 10, 0, 10, 10, 0, 10),
@@ -177,8 +277,8 @@ void TH2Ratio<T>::update()
177277
}
178278

179279
T::Reset();
180-
T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax());
181-
T::GetYaxis()->Set(mHistoNum->GetYaxis()->GetNbins(), mHistoNum->GetYaxis()->GetXmin(), mHistoNum->GetYaxis()->GetXmax());
280+
mHistoNum->GetXaxis()->Copy(*T::GetXaxis());
281+
mHistoNum->GetYaxis()->Copy(*T::GetYaxis());
182282
T::SetBinsLength();
183283

184284
// Copy bin labels between histograms.

0 commit comments

Comments
 (0)