Skip to content

Commit 9da6c0d

Browse files
authored
Improvements to the THRatio initialization and interface (#1906)
* Improvements to the THRatio initialization and interface - numerator and denominator plots are initialized with dummy parameters in the default constructor, instead of assigning nullptr to the corresponding pointers - added override functions to update the histo name and title, and propagate the changes to the internal numerator and denominator - Copy() function also updates the `mUniformScaling` parameter of the destination object - added unit tests for Copy() and Clone() functions * Improved variables naming
1 parent 90b2733 commit 9da6c0d

5 files changed

Lines changed: 350 additions & 56 deletions

File tree

Modules/Common/include/Common/TH1Ratio.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Mergers/MergeInterface.h"
2121
#include <TH1F.h>
2222
#include <TH1D.h>
23+
#include <TDirectory.h>
2324

2425
namespace o2::quality_control_modules::common
2526
{
@@ -28,7 +29,7 @@ template <class T>
2829
class TH1Ratio : public T, public o2::mergers::MergeInterface
2930
{
3031
public:
31-
TH1Ratio() = default;
32+
TH1Ratio();
3233
TH1Ratio(TH1Ratio const& copymerge);
3334
TH1Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, bool uniformScaling = false);
3435
TH1Ratio(const char* name, const char* title, bool uniformScaling = false);
@@ -49,6 +50,11 @@ class TH1Ratio : public T, public o2::mergers::MergeInterface
4950
return mHistoDen;
5051
}
5152

53+
void setHasUniformScaling(bool flag)
54+
{
55+
mUniformScaling = flag;
56+
}
57+
5258
bool hasUniformScaling() const
5359
{
5460
return mUniformScaling;
@@ -58,6 +64,8 @@ class TH1Ratio : public T, public o2::mergers::MergeInterface
5864

5965
// functions inherited from TH1x
6066
void Reset(Option_t* option = "") override;
67+
void SetName(const char* name) override;
68+
void SetTitle(const char* title) override;
6169
void Copy(TObject& obj) const override;
6270
Bool_t Add(const TH1* h1, const TH1* h2, Double_t c1 = 1, Double_t c2 = 1) override;
6371
Bool_t Add(const TH1* h1, Double_t c1 = 1) override;

Modules/Common/include/Common/TH1Ratio.inl

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,41 @@
1919
namespace o2::quality_control_modules::common
2020
{
2121

22+
template<class T>
23+
TH1Ratio<T>::TH1Ratio()
24+
: T(),
25+
o2::mergers::MergeInterface(),
26+
mUniformScaling(true)
27+
{
28+
// do not add created histograms to gDirectory
29+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
30+
{
31+
TDirectory::TContext ctx(nullptr);
32+
mHistoNum = new T("num", "num", 10, 0, 10);
33+
mHistoDen = new T("den", "den", 1, -1, 1);
34+
}
35+
36+
init();
37+
}
38+
2239
template<class T>
2340
TH1Ratio<T>::TH1Ratio(TH1Ratio const& copymerge)
24-
: T(copymerge.GetName(), copymerge.GetTitle(),
25-
copymerge.getNum()->GetXaxis()->GetNbins(), copymerge.getNum()->GetXaxis()->GetXmin(), copymerge.getNum()->GetXaxis()->GetXmax()),
41+
: T(),
2642
o2::mergers::MergeInterface(),
2743
mUniformScaling(copymerge.hasUniformScaling())
2844
{
29-
Bool_t bStatus = TH1::AddDirectoryStatus();
30-
TH1::AddDirectory(kFALSE);
31-
mHistoNum = (T*)copymerge.getNum()->Clone();
32-
mHistoDen = (T*)copymerge.getDen()->Clone();
33-
TH1::AddDirectory(bStatus);
45+
// do not add cloned histograms to gDirectory
46+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
47+
{
48+
TString nameNum = T::GetName() + TString("_num");
49+
TString nameDen = T::GetName() + TString("_den");
50+
TString titleNum = T::GetTitle() + TString(" num");
51+
TString titleDen = T::GetTitle() + TString(" den");
52+
TDirectory::TContext ctx(nullptr);
53+
mHistoNum = new T(nameNum, titleNum, 10, 0, 10);
54+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1);
55+
}
56+
copymerge.Copy(*this);
3457

3558
init();
3659
}
@@ -41,15 +64,21 @@ TH1Ratio<T>::TH1Ratio(const char* name, const char* title, int nbinsx, double xm
4164
o2::mergers::MergeInterface(),
4265
mUniformScaling(uniformScaling)
4366
{
44-
Bool_t bStatus = TH1::AddDirectoryStatus();
45-
TH1::AddDirectory(kFALSE);
46-
mHistoNum = new T("num", "num", nbinsx, xmin, xmax);
47-
if (mUniformScaling) {
48-
mHistoDen = new T("den", "den", 1, -1, 1);
49-
} else {
50-
mHistoDen = new T("den", "den", nbinsx, xmin, xmax);
67+
// do not add created histograms to gDirectory
68+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
69+
{
70+
TString nameNum = T::GetName() + TString("_num");
71+
TString nameDen = T::GetName() + TString("_den");
72+
TString titleNum = T::GetTitle() + TString(" num");
73+
TString titleDen = T::GetTitle() + TString(" den");
74+
TDirectory::TContext ctx(nullptr);
75+
mHistoNum = new T(nameNum, titleNum, nbinsx, xmin, xmax);
76+
if (mUniformScaling) {
77+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1);
78+
} else {
79+
mHistoDen = new T(nameDen, titleDen, nbinsx, xmin, xmax);
80+
}
5181
}
52-
TH1::AddDirectory(bStatus);
5382

5483
init();
5584
}
@@ -60,15 +89,21 @@ TH1Ratio<T>::TH1Ratio(const char* name, const char* title, bool uniformScaling)
6089
o2::mergers::MergeInterface(),
6190
mUniformScaling(uniformScaling)
6291
{
63-
Bool_t bStatus = TH1::AddDirectoryStatus();
64-
TH1::AddDirectory(kFALSE);
65-
mHistoNum = new T("num", "num", 10, 0, 10);
66-
if (mUniformScaling) {
67-
mHistoDen = new T("den", "den", 1, -1, 1);
68-
} else {
69-
mHistoDen = new T("den", "den", 10, 0, 10);
92+
// do not add created histograms to gDirectory
93+
// see https://root.cern.ch/doc/master/TEfficiency_8cxx.html
94+
{
95+
TString nameNum = T::GetName() + TString("_num");
96+
TString nameDen = T::GetName() + TString("_den");
97+
TString titleNum = T::GetTitle() + TString(" num");
98+
TString titleDen = T::GetTitle() + TString(" den");
99+
TDirectory::TContext ctx(nullptr);
100+
mHistoNum = new T(nameNum, titleNum, 10, 0, 10);
101+
if (mUniformScaling) {
102+
mHistoDen = new T(nameDen, titleDen, 1, -1, 1);
103+
} else {
104+
mHistoDen = new T(nameDen, titleDen, 10, 0, 10);
105+
}
70106
}
71-
TH1::AddDirectory(bStatus);
72107

73108
init();
74109
}
@@ -94,8 +129,6 @@ void TH1Ratio<T>::init()
94129
mHistoNum->Sumw2();
95130
mHistoDen->Sumw2();
96131
T::Sumw2();
97-
98-
update();
99132
}
100133

101134
template<class T>
@@ -121,8 +154,6 @@ void TH1Ratio<T>::update()
121154
const char* title = this->GetTitle();
122155

123156
T::Reset();
124-
125-
T::SetNameTitle(name, title);
126157
T::GetXaxis()->Set(mHistoNum->GetXaxis()->GetNbins(), mHistoNum->GetXaxis()->GetXmin(), mHistoNum->GetXaxis()->GetXmax());
127158
T::SetBinsLength();
128159

@@ -151,6 +182,30 @@ void TH1Ratio<T>::Reset(Option_t* option)
151182
T::Reset(option);
152183
}
153184

185+
template<class T>
186+
void TH1Ratio<T>::SetName(const char* name)
187+
{
188+
T::SetName(name);
189+
190+
//setting the names (appending the correct ending)
191+
TString nameNum = name + TString("_num");
192+
TString nameDen = name + TString("_den");
193+
mHistoNum->SetName(nameNum);
194+
mHistoDen->SetName(nameDen);
195+
}
196+
197+
template<class T>
198+
void TH1Ratio<T>::SetTitle(const char* title)
199+
{
200+
T::SetTitle(title);
201+
202+
//setting the titles (appending the correct ending)
203+
TString titleNum = title + TString("_num");
204+
TString titleDen = title + TString("_den");
205+
mHistoNum->SetTitle(titleNum);
206+
mHistoDen->SetTitle(titleDen);
207+
}
208+
154209
template<class T>
155210
void TH1Ratio<T>::Copy(TObject& obj) const
156211
{
@@ -159,6 +214,8 @@ void TH1Ratio<T>::Copy(TObject& obj) const
159214
return;
160215
}
161216

217+
dest->setHasUniformScaling(hasUniformScaling());
218+
162219
T::Copy(obj);
163220

164221
if (mHistoNum && dest->getNum() && mHistoDen && dest->getDen()) {

Modules/Common/include/Common/TH2Ratio.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Mergers/MergeInterface.h"
2121
#include <TH2F.h>
2222
#include <TH2D.h>
23+
#include <TDirectory.h>
2324

2425
namespace o2::quality_control_modules::common
2526
{
@@ -28,7 +29,7 @@ template <class T>
2829
class TH2Ratio : public T, public o2::mergers::MergeInterface
2930
{
3031
public:
31-
TH2Ratio() = default;
32+
TH2Ratio();
3233
TH2Ratio(TH2Ratio const& copymerge);
3334
TH2Ratio(const char* name, const char* title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, bool uniformScaling = false);
3435
TH2Ratio(const char* name, const char* title, bool uniformScaling = false);
@@ -49,6 +50,11 @@ class TH2Ratio : public T, public o2::mergers::MergeInterface
4950
return mHistoDen;
5051
}
5152

53+
void setHasUniformScaling(bool flag)
54+
{
55+
mUniformScaling = flag;
56+
}
57+
5258
bool hasUniformScaling() const
5359
{
5460
return mUniformScaling;
@@ -58,6 +64,8 @@ class TH2Ratio : public T, public o2::mergers::MergeInterface
5864

5965
// functions inherited from TH2x
6066
void Reset(Option_t* option = "") override;
67+
void SetName(const char* name) override;
68+
void SetTitle(const char* title) override;
6169
void Copy(TObject& obj) const override;
6270
Bool_t Add(const TH1* h1, const TH1* h2, Double_t c1 = 1, Double_t c2 = 1) override;
6371
Bool_t Add(const TH1* h1, Double_t c1 = 1) override;

0 commit comments

Comments
 (0)