ATLAS Offline Software
HistValProfile.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <utility>
6 
7 
8 //____________________________________________________________________
9 HistValProfile::HistValProfile( bool trigger_conversion_all,
10  const std::string& name, const std::string& title,
11  int nbins, const double& xmin, const double& xmax,
12  const double& profparmin, const double& profparmax )
13  : HistValBase()
14 {
15  m_h1 = new TProfile(name.c_str(),title.c_str(),nbins,xmin,xmax,profparmin,profparmax,profile_test_erroroption());
16  m_h2 = TProfile_LW::create(name.c_str(),title.c_str(),nbins,xmin,xmax,profparmin,profparmax,profile_test_erroroption());
17  HistValProfile::compareMetaData();
18 
19  if (trigger_conversion_all)
20  triggerConversionToROOTHist();
21 }
22 
23 
24 //____________________________________________________________________
25 template <class TFloat>
26 HistValProfile::HistValProfile( bool trigger_conversion_all,
27  const std::string& name, const std::string& title,
28  int nbins, const TFloat* xbins )
29  : HistValBase()
30 {
31  m_h1 = new TProfile(name.c_str(),title.c_str(),nbins,xbins,profile_test_erroroption());
32  m_h2 = TProfile_LW::create(name.c_str(),title.c_str(),nbins,xbins,profile_test_erroroption());
33  HistValProfile::compareMetaData();
34  if (trigger_conversion_all)
35  triggerConversionToROOTHist();
36 }
37 
38 //____________________________________________________________________
39 HistValProfile::~HistValProfile()
40 {
41  compareAll();
42  delete m_h1;
43  LWHist::safeDelete(m_h2);
44 }
45 
46 //____________________________________________________________________
47 unsigned HistValProfile::getXAxis_NBins() const
48 {
49  return std::as_const(*m_h1).GetXaxis()->GetNbins();
50 }
51 
52 //____________________________________________________________________
53 unsigned HistValProfile::getYAxis_NBins() const
54 {
55  return std::as_const(*m_h1).GetYaxis()->GetNbins();
56 }
57 
58 //____________________________________________________________________
59 void HistValProfile::fill(const double& x, const double& y)
60 {
61  m_h1->Fill(x,y);
62  m_h2->Fill(x,y);
63  this->binContentsChanged();
64 }
65 
66 //____________________________________________________________________
67 void HistValProfile::fill(const double& x, const double& y, const double& w)
68 {
69  if (w<0) return;//Latest root gives NaN's in case of negative weights => not worth reproducing that.
70  if (w==0)
71  return;
72  m_h1->Fill(x,y,w);
73  m_h2->Fill(x,y,w);
74  this->binContentsChanged();
75 }
76 
77 //____________________________________________________________________
78 void HistValProfile::setBinEntries(unsigned bin, const double& entries)
79 {
80  if (entries<=0)
81  return;//Fixme: this is because of some NaN resulting from ROOT internals which I can't reproduce.
82  //(entries==0 ignored due to internals being to weird)
83  m_h1->SetBinEntries(bin,entries);
84  m_h2->SetBinEntries(bin,entries);
85  this->binContentsChanged();
86 }
87 
88 //____________________________________________________________________
89 void HistValProfile::setBinContent(unsigned bin, const double& content)
90 {
91  m_h1->SetBinContent(bin,content);
92  m_h2->SetBinContent(bin,content);
93  this->binContentsChanged();
94 }
95 
96 //____________________________________________________________________
97 void HistValProfile::setBinError(unsigned bin, const double& error)
98 {
99  if (error<0)
100  return;
101  m_h1->SetBinError(bin,error);
102  m_h2->SetBinError(bin,error);
103  this->binContentsChanged();
104 }
105 
106 //____________________________________________________________________
107 void HistValProfile::setBinInfo(unsigned bin, const double& entries, const double& content,const double& error)
108 {
109  if (entries<0)
110  return;//Fixme: this is because of some NaN resulting from ROOT internals which I can't reproduce.
111  m_h1->SetBinEntries(bin,entries);
112  m_h1->SetBinContent(bin,content);
113  m_h1->SetBinError(bin,error);
114  m_h2->SetBinInfo(bin,entries,content,error);
115  this->binContentsChanged();
116 }
117 
118 //____________________________________________________________________
119 void HistValProfile::compareBinContents() const
120 {
121  HistValFunctions::test("GetNbinsX()",int(m_h1->GetNbinsX()), int(m_h2->GetNbinsX()));
122  int n = m_h1->GetNbinsX();
123  std::ostringstream s;
124  for (int i=0;i<=n+1;++i) {
125  double entries_1 = m_h1->GetBinEntries(i);
126  double content_1 = m_h1->GetBinContent(i);
127  double error_1 = m_h1->GetBinError(i);
128  double entries_2 = m_h2->GetBinEntries(i);
129  double content_2 = m_h2->GetBinContent(i);
130  double error_2 = m_h2->GetBinError(i);
131  double entries_3, content_3, error_3;
132  const double errorCompatEpsilon(1.0e-3);
133  m_h2->GetBinInfo(i,entries_3, content_3, error_3);
134  if (!HistValFunctions::compatible(entries_1,entries_2)) {
135  s << "[TProfile vs. TProfile_LW] GetBinEntries("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
136  HistValFunctions::test(s.str(),entries_1,entries_2);
137  }
138  if (!HistValFunctions::compatible(content_1,content_2)) {
139  s << "[TProfile vs. TProfile_LW] GetBinContent("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
140  HistValFunctions::test(s.str(),content_1,content_2);
141  }
142  if (!HistValFunctions::floatCompat(error_1,error_2,errorCompatEpsilon)) {
143  s << "[TProfile vs. TProfile_LW] GetBinError("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
144  HistValFunctions::test(s.str(),error_1,error_2);
145  }
146  if (!HistValFunctions::compatible(entries_1,entries_3)) {
147  s << "[TProfile vs. TProfile_LW] entries from GetBinInfo("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
148  HistValFunctions::test(s.str(),entries_1,entries_3);
149  }
150  if (!HistValFunctions::compatible(content_1,content_3)) {
151  s << "[TProfile vs. TProfile_LW] content from GetBinInfo("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
152  HistValFunctions::test(s.str(),content_1,content_3);
153  }
154  if (!HistValFunctions::floatCompat(error_1,error_3,errorCompatEpsilon)) {
155  s << "[TProfile vs. TProfile_LW] error from GetBinInfo("<<i<<", nbins="<<n<<", ninternalbins="<<n+2<<")";
156  HistValFunctions::test(s.str(),error_1,error_3);
157  }
158  }
159 }
160 
161 //____________________________________________________________________
162 void HistValProfile::compareMetaData()
163 {
164  HistValFunctions::compareMetaData(m_h1, m_h2);
165 }
166 
167 //____________________________________________________________________
168 void HistValProfile::compareTH1s()
169 {
170  if (converted())
171  {
172  assert(m_h1->GetName()[0]==':');
173  }
174 
175  std::string name1(m_h1->GetName());
176 
177  TProfile * th1_gen(0);
178  if (!converted()) {
179  m_h1->SetName((name1+"tmp").c_str());
180  LWHistControls::setCleanupOnGetROOT(false);
181  m_h2->SetName("tmpname");
182  th1_gen = m_h2->getROOTHist();
183  LWHistControls::setCleanupOnGetROOT(true);
184  } else {
185  th1_gen = m_h2->getROOTHist();
186  }
187 
188 
189  if (!th1_gen)
190  HistValFunctions::testfailed("getROOTHist(..) returns null!");
191 
192  if (!converted())
193  HistValFunctions::test("[TH1-level] GetName()",std::string("tmpname"),th1_gen->GetName());
194  else
195  HistValFunctions::test("[TH1-level] GetName()",&(m_h1->GetName()[11]),th1_gen->GetName());//"stripping off "::trigger::"
196 
197  //Test various meta data including type and other state data:
198  HistValFunctions::test("[TH1-level] GetTitle()",m_h1->GetTitle(), th1_gen->GetTitle());
199  HistValFunctions::test("[TH1-level] ClassName()",m_h1->ClassName(), th1_gen->ClassName());
200 
201  HistValFunctions::test("[TH1-level] GetYmin()",m_h1->GetYmin(), th1_gen->GetYmin());
202  HistValFunctions::test("[TH1-level] GetYmax()",m_h1->GetYmax(), th1_gen->GetYmax());
203 
204  HistValFunctions::compareBinContents_1D(m_h1,th1_gen,true/*relaxedErrorComparison*/);
205  HistValFunctions::compareFields(m_h1,th1_gen);
206 
207  //NB: ROOT very suspicious behaviour!!!: When calling GetStats
208  //(directly or from GetRMS, GetMean, GetRMSError, GetMeanError), and
209  //fTsumwy and fTsumwy2 are both 0, it casts away the constness and
210  //sets those two variables. ARRRGH. Thus we don't call any of those
211  //methods here.
212 
213  // -> And all other supported state data...
214 // HistValFunctions::test("[TH1-level] GetRMS()",m_h1->GetRMS(), th1_gen->GetRMS());
215 // HistValFunctions::test("[TH1-level] GetMean()",m_h1->GetMean(), th1_gen->GetMean());
216 // HistValFunctions::test("[TH1-level] GetMeanError()",m_h1->GetMeanError(), th1_gen->GetMeanError());
217  //HistValFunctions::test("[TH1-level] ComputeIntegral()",m_h1->ComputeIntegral(), th1_gen->ComputeIntegral());
218  HistValFunctions::test("[TH1-level] Integral()",m_h1->Integral(), th1_gen->Integral());
219 
220  //TODO: Compare more axes values (like min and max, nbins etc.)!!
221  const TArrayD * h1_xbins = m_h1->GetXaxis()->GetXbins();
222  const TArrayD * gen_xbins = th1_gen->GetXaxis()->GetXbins();
223  if ((h1_xbins!=0)!=(gen_xbins!=0))
224  HistValFunctions::testfailed("2D hist has inconsistent presence of xbins");
225  if (h1_xbins&&gen_xbins)
226  HistValFunctions::test("[TH1-level] GetXaxis()->GetXbins()->GetSize()",h1_xbins->GetSize(), gen_xbins->GetSize());
227 
228  HistValFunctions::compareBinContents_1D(m_h1, m_h2,true/*relaxedErrorComparison*/);
229  HistValFunctions::compareBinContents_1D(th1_gen, m_h2,true/*relaxedErrorComparison*/);
230  HistValFunctions::compareBinContents_1D(th1_gen, m_h1,true/*relaxedErrorComparison*/);
231  HistValFunctions::compareMetaData(static_cast<TProfile*>(th1_gen), m_h2,true/*ignore name*/);
232  HistValFunctions::compareMetaData(m_h1, m_h2,true/*ignore name*/);
233 
234  if (!converted()) {
235  LWHistVal::clearKeptROOTHist(m_h2);//deletes th1_gen
236  m_h1->SetName(name1.c_str());
237  m_h2->SetName(name1.c_str());
238  }
239 }
240 
241 //____________________________________________________________________
242 void HistValProfile::compareFastLoop()
243 {
244 }
245 
246 
247 //____________________________________________________________________
248 void HistValProfile::setXAxis_BinLabel(unsigned bin, const char* label)
249 {
250  m_h1->GetXaxis()->SetBinLabel(bin,label);
251  m_h2->GetXaxis()->SetBinLabel(bin,label);
252  this->titleOrNameWasChanged();
253 }
254 
255 //____________________________________________________________________
256 void HistValProfile::setYAxis_BinLabel(unsigned bin, const char* label)
257 {
258  m_h1->GetYaxis()->SetBinLabel(bin,label);
259  m_h2->GetYaxis()->SetBinLabel(bin,label);
260  this->titleOrNameWasChanged();
261 }
262 
263 //____________________________________________________________________
264 void HistValProfile::setXAxis_LabelSize(float s)
265 {
266  m_h1->GetXaxis()->SetLabelSize(s);
267  m_h2->GetXaxis()->SetLabelSize(s);
268  this->titleOrNameWasChanged();
269 }
270 
271 //____________________________________________________________________
272 void HistValProfile::setYAxis_LabelSize(float s)
273 {
274  m_h1->GetYaxis()->SetLabelSize(s);
275  m_h2->GetYaxis()->SetLabelSize(s);
276  this->titleOrNameWasChanged();
277 }
278 
279 //____________________________________________________________________
280 void HistValProfile::setXAxis_Title(const std::string&t)
281 {
282  m_h1->GetXaxis()->SetTitle(t.c_str());
283  m_h2->GetXaxis()->SetTitle(t.c_str());
284  this->titleOrNameWasChanged();
285 }
286 
287 //____________________________________________________________________
288 void HistValProfile::setYAxis_Title(const std::string&t)
289 {
290  m_h1->GetYaxis()->SetTitle(t.c_str());
291  m_h2->GetYaxis()->SetTitle(t.c_str());
292  this->titleOrNameWasChanged();
293 }
294 
295 //____________________________________________________________________
296 void HistValProfile::setZAxis_Title(const std::string&t)
297 {
298  m_h1->GetZaxis()->SetTitle(t.c_str());
299  m_h2->GetZaxis()->SetTitle(t.c_str());
300  this->titleOrNameWasChanged();
301 }
302 
303 //____________________________________________________________________
304 void HistValProfile::SetMarkerColor( short c)
305 {
306  m_h1->SetMarkerColor(c);
307  m_h2->SetMarkerColor(c);
308  this->titleOrNameWasChanged();
309 }
310 
311 //____________________________________________________________________
312 void HistValProfile::SetMarkerStyle( short s )
313 {
314  m_h1->SetMarkerStyle(s);
315  m_h2->SetMarkerStyle(s);
316  assert(m_h1->GetMarkerStyle()==s);
317  assert(m_h2->GetMarkerStyle()==s);
318  this->titleOrNameWasChanged();
319 }
320 
321 //____________________________________________________________________
322 void HistValProfile::SetMarkerSize( float sz)
323 {
324  m_h1->SetMarkerSize(sz);
325  m_h2->SetMarkerSize(sz);
326  this->titleOrNameWasChanged();
327 }
328 
329 //____________________________________________________________________
330 void HistValProfile::setMinimum( const double& m )
331 {
332  m_h1->SetMinimum(m);
333  m_h2->SetMinimum(m);
334  this->titleOrNameWasChanged();
335 }
336 
337 //____________________________________________________________________
338 void HistValProfile::setMaximum( const double& m )
339 {
340  m_h1->SetMaximum(m);
341  m_h2->SetMaximum(m);
342  this->titleOrNameWasChanged();
343 }
344 
345 //____________________________________________________________________
346 void HistValProfile::setName(const std::string& name)
347 {
348  m_h1->SetName(convertedRootName(name).c_str());
349  m_h2->SetName(name.c_str());
350  this->titleOrNameWasChanged();
351 }
352 
353 //____________________________________________________________________
354 void HistValProfile::setTitle(const std::string& title)
355 {
356  m_h1->SetTitle(title.c_str());
357  m_h2->SetTitle(title.c_str());
358  this->titleOrNameWasChanged();
359 }
360 
361 //____________________________________________________________________
362 void HistValProfile::setNameTitle(const std::string& name,
363  const std::string& title)
364 {
365  m_h1->SetNameTitle(convertedRootName(name).c_str(),title.c_str());
366  m_h2->SetNameTitle(name.c_str(),title.c_str());
367  this->titleOrNameWasChanged();
368 }