1919namespace 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+
2239template <class T >
2340TH1Ratio<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
101134template <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+
154209template <class T >
155210void 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 ()) {
0 commit comments