ATLAS Offline Software
comphistpars.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "rootincludes.h"
6 #include "jobflags.h"
7 #include <cmath>
8 
10 // Interface: //
12 
13 template <class T>
14 bool checkParameters(T*h1,T*h2);
15 
16 
18 // Helpers: //
20 
21 #define xstringify(s) str(s)
22 #define stringify(s) #s
23 
24 #define REPORT(x) { std::cout << " -> ERROR: "<<x<<std::endl; }
25 template <class T> bool varCompatible(const T var1,const T var2) {
26  if (var1==var2)
27  return true;
28  const bool var1_nan = std::isnan(var1);
29  const bool var2_nan = std::isnan(var2);
30  if (var1_nan&&var2_nan) {
31  if (cfg_report_NaN)
32  std::cout<<"WARNING: Spotted NaN values in parameter (but same in both of the compared histograms)!"<<std::endl;
33  return true;
34  }
35  if (var1_nan||var2_nan) {
36  if (cfg_report_NaN)
37  REPORT("Spotted NaN values in parameter (from only one of the compared histograms)!");
38  return false;
39  }
40  REPORT("different values found: "<<var1<<" vs. "<<var2);
41  return false;
42 }
43 
44 inline bool floatingpointcompat(double var1, double var2, const double& epsilon)
45 {
46  const bool var1_nan = (var1!=var1);
47  const bool var2_nan = (var2!=var2);
48  if (var1_nan&&var2_nan) {
49  if (cfg_report_NaN)
50  std::cout<<"WARNING: Spotted NaN values in floating point number (but same in both of the compared histograms)!"<<std::endl;
51  return true;
52  }
53  if (var1_nan||var2_nan) {
54  if (cfg_report_NaN)
55  REPORT("Spotted NaN values in floating point number (from only one of the compared histograms)!");
56  return false;
57  }
58  const double test(fabs(var1-var2)/(1.0+std::max<double>(fabs(var1),fabs(var2))));
59  // if (!(test<epsilon)) {
60  // std::cout<<"NB: var difference: "<<var1<<" vs. "<<var2<<" :"<<test<<std::endl;
61  // }
62  return test<epsilon;
63 }
64 template <> bool varCompatible(const double var1,const double var2) { return floatingpointcompat(var1,var2,1.0e-5); }
65 template <> bool varCompatible(const float var1,const float var2) { return floatingpointcompat(var1,var2,1.0e-3); }
66 template <> bool varCompatible(const char* var1,const char* var2) { return std::string(var1)==std::string(var2); }
67 template <> bool varCompatible(TString var1,TString var2) { return var1 == var2; }
68 
69 
70 template <class TContainedType>
71 bool tlistsCompatible(const TList*h1,const TList*h2);//forward decl.
72 
73 template <> bool varCompatible(TFunction var1, TFunction var2)
74 {
75  if (!varCompatible(var1.GetMangledName(), var2.GetMangledName()))
76  return false;
77  if (!varCompatible(var1.GetSignature(), var2.GetSignature()))
78  return false;
79  if (!tlistsCompatible<TString>(var1.GetListOfMethodArgs(), var2.GetListOfMethodArgs()))
80  return false;
81  return true;
82 }
83 
84 #define TESTSIMPLEVAR(varname) \
85  { if (!varCompatible(h1->varname,h2->varname)) { \
86  REPORT("Incompatible values of "<<stringify(varname)<<" ("<<h1->varname<<" vs. "<<h2->varname<<")"); \
87  return false; } }
88 
89 #define TESTSIMPLEEXTVAR(var1,var2,varname) \
90  { if (!varCompatible(var1,var2)) { \
91  REPORT("Incompatible values of "<<stringify(varname)<<" ("<<var1<<" vs. "<<var2<<")"); \
92  return false; } }
93 
94 #define TESTSIMPLEUNSUPPORTEDSTYLEVAR(varname) { if (!cfg_ignore_unsupported_style_vars) { TESTSIMPLEVAR(varname); } }
95 
96 
97 template <class TArrayX>
98 bool arrayCompatible(const TArrayX* a1,const TArrayX* a2,const char*name) {
99  if (!varCompatible(a1->fN,a2->fN)) {
100  REPORT("Incompatible arrays "<<name<<" (number of entries)");
101  return false;
102  }
103  if (a1->fArray==0&&a2->fArray==0)
104  return true;
105  if (a1->fArray==0||a2->fArray==0) {
106  REPORT("Incompatible arrays "<<name<<" (one NULL, one not NULL)"); \
107  return false;
108  }
109  for (int i = 0; i < a1->fN; ++i)
110  if (!varCompatible(a1->fArray[i],a2->fArray[i])) {
111  REPORT("Incompatible arrays "<<name<<" (at least one entry incompatible)"); \
112  return false;
113  }
114  return true;
115 }
116 
117 #define ACTUALTESTARRAY(a,b,name) \
118  { if (!arrayCompatible(a,b,name)) { \
119  return false; } }
120 
121 #define TESTASARRAY() {ACTUALTESTARRAY(h1,h2,"")}
122 #define TESTARRAY(varname) {ACTUALTESTARRAY(&(h1->varname),&(h2->varname),stringify(varname))}
123 #define TESTARRAYPTR(varname) {ACTUALTESTARRAY((h1->varname),(h2->varname),stringify(varname))}
124 
125 bool axisCompatible(const TAxis* h1,const TAxis* h2) {
126  //TAxis:
127  TESTSIMPLEVAR(GetNbins());
128  TESTSIMPLEVAR(GetXmin());
129  TESTSIMPLEVAR(GetXmax());
130  TESTSIMPLEVAR(GetFirst());
131  TESTSIMPLEVAR(GetLast());
132  //TESTSIMPLEVAR(IsAlphanumeric());
133  //TESTSIMPLEVAR(CanBeAlphanumeric());
134  TESTSIMPLEVAR(CanExtend());
135  TESTSIMPLEVAR(GetTimeDisplay());
136  TESTSIMPLEVAR(GetTimeFormat());
137  TESTARRAYPTR(GetXbins());
138  if (!tlistsCompatible<TString>((TList*)(h1->GetLabels()),(TList*)(h2->GetLabels()))) {
139  REPORT("Incompatible fLabels");
140  return false;
141  }
142  //TAttAxis:
143  TESTSIMPLEUNSUPPORTEDSTYLEVAR(GetNdivisions());
144  TESTSIMPLEUNSUPPORTEDSTYLEVAR(GetAxisColor());
145  TESTSIMPLEVAR(GetLabelColor());
146  TESTSIMPLEVAR(GetLabelFont());
147  TESTSIMPLEVAR(GetLabelOffset());
148  TESTSIMPLEVAR(GetLabelSize());
149  TESTSIMPLEUNSUPPORTEDSTYLEVAR(GetTickLength());
150  TESTSIMPLEUNSUPPORTEDSTYLEVAR(GetTitleOffset());
151  TESTSIMPLEVAR(GetTitleSize());
152  TESTSIMPLEVAR(GetTitleColor());
153  TESTSIMPLEVAR(GetTitleFont());
154  //TNamed:
155  TESTSIMPLEVAR(GetName());
156  TESTSIMPLEVAR(GetTitle());
157  return true;
158 }
159 
160 template <class TContainedType>
161 bool tlistsCompatible(const TList*h1,const TList*h2) {
162  if (!h1&&!h2)
163  return true;
164  if (!h1||!h2)
165  return false;
166  //TCollection:
167  TESTSIMPLEVAR(GetName());
168  TESTSIMPLEVAR(Capacity());
169  //TSeqCollection:
170  TESTSIMPLEVAR(IsSorted());
171  //For the TList itself we loop, cast each element to TContainedType,
172  //and compare using operator==.
173  Int_t sz = h1->Capacity();
174  for (int i = 0; i<sz; ++i) {
175  TContainedType * t1 = dynamic_cast<TContainedType*>(h1->At(i));
176  TContainedType * t2 = dynamic_cast<TContainedType*>(h1->At(i));
177  if (!t1&&!t2)
178  continue;
179  if (!t1||!t2)
180  return false;
181  if (!varCompatible(*t1,*t2))
182  return false;
183  }
184  return true;
185 }
186 
188 // Specific Implementations: //
190 
191 template <>
193 {
194  assert(h1!=h2);
195  assert(h1&&h2);
196  //TH1:
197  //Ignore fDirectory and buffer related methods.
199  TESTSIMPLEVAR(GetMaximumStored());
200  TESTSIMPLEVAR(GetMinimumStored());
201  TESTSIMPLEVAR(GetNormFactor());
202  Double_t stats1[20], stats2[20];
203  h1->GetStats(stats1);
204  h2->GetStats(stats2);
205  TESTSIMPLEEXTVAR(stats1[0], stats2[0], fTsumw);
206  TESTSIMPLEEXTVAR(stats1[1], stats2[1], fTsumw2);
207  TESTSIMPLEEXTVAR(stats1[2], stats2[2], fTsumwx);
208  TESTSIMPLEEXTVAR(stats1[3], stats2[3], fTsumwx2);
209  TESTSIMPLEVAR(GetDimension());
210  TESTSIMPLEVAR(GetNcells());
211  TESTSIMPLEVAR(GetBarOffset());
212  TESTSIMPLEVAR(GetBarWidth());
213 
214  Int_t nlevels1 = h1->GetContour(nullptr);
215  Int_t nlevels2 = h2->GetContour(nullptr);
216  TESTSIMPLEEXTVAR (nlevels1, nlevels2, fContour.fN);
217  if (nlevels1 > 0) {
218  TArrayD levels1 (nlevels1);
219  TArrayD levels2 (nlevels1);
220  h1->GetContour (levels1.GetArray());
221  h2->GetContour (levels2.GetArray());
222  ACTUALTESTARRAY(&levels1, &levels2, "fContour");
223  }
224 
225  bool test_sumw2(true);
227  if ((h1->GetSumw2()->fN==0||h2->GetSumw2()->fN==0) && ((h1->GetSumw2()->fN==0)!=(h2->GetSumw2()->fN==0)))
228  test_sumw2 = false;
229  }
230  if (test_sumw2)
231  TESTARRAYPTR(GetSumw2());
232  TESTSIMPLEVAR(GetOption());
233  if (!tlistsCompatible<TFunction>(h1->GetListOfFunctions(),h2->GetListOfFunctions())) {
234  REPORT("Incompatible fXaxis");
235  return false;
236  }
237  if (!axisCompatible(h1->GetXaxis(), h2->GetXaxis())) {
238  REPORT("Incompatible fXaxis");
239  return false;
240  }
241  if (!axisCompatible(h1->GetYaxis(), h2->GetYaxis())) {
242  REPORT("Incompatible fYaxis");
243  return false;
244  }
245  if (!axisCompatible(h1->GetZaxis(), h2->GetZaxis())) {
246  REPORT("Incompatible fZaxis");
247  return false;
248  }
249 
250  //TNamed:
251  //Ignore fName
252  TESTSIMPLEVAR(GetTitle());
253  //TAttLine:
254  TESTSIMPLEVAR(GetLineColor());
255  TESTSIMPLEVAR(GetLineStyle());
256  TESTSIMPLEVAR(GetLineWidth());
257  //TAttFill:
258  TESTSIMPLEVAR(GetFillColor());
259  TESTSIMPLEVAR(GetFillStyle());
260  //TAttMarker:
261  TESTSIMPLEVAR(GetMarkerColor());
262  TESTSIMPLEVAR(GetMarkerStyle());
263  TESTSIMPLEVAR(GetMarkerSize());
264  //TObject:
265  //Ignore fUniqueID
266  for (int i=0; i < 32; i++)
267  TESTSIMPLEVAR(TestBit(i));
268  return true;
269 }
270 
271 //Actual implementations:
272 template <> bool checkParameters(TH1F*h1,TH1F*h2) { TESTASARRAY(); return true; }
273 template <> bool checkParameters(TH1D*h1,TH1D*h2) { TESTASARRAY(); return true; }
274 template <> bool checkParameters(TH1I*h1,TH1I*h2) { TESTASARRAY(); return true; }
275 template <> bool checkParameters(TH1C*h1,TH1C*h2) { TESTASARRAY(); return true; }
276 template <> bool checkParameters(TH1S*h1,TH1S*h2) { TESTASARRAY(); return true; }
277 
278 template <>
280 {
281  //TESTSIMPLEVAR(fScalefactor);
282  Double_t stats1[20], stats2[20];
283  h1->GetStats(stats1);
284  h2->GetStats(stats2);
285  TESTSIMPLEEXTVAR(stats1[4], stats2[4], fTsumwy);
286  TESTSIMPLEEXTVAR(stats1[5], stats2[5], fTsumwy2);
287  TESTSIMPLEEXTVAR(stats1[6], stats2[6], fTsumwxy);
288  return true;
289 }
290 
291 template <> bool checkParameters(TH2F*h1,TH2F*h2) { TESTASARRAY(); return true; }
292 template <> bool checkParameters(TH2D*h1,TH2D*h2) { TESTASARRAY(); return true; }
293 template <> bool checkParameters(TH2I*h1,TH2I*h2) { TESTASARRAY(); return true; }
294 template <> bool checkParameters(TH2C*h1,TH2C*h2) { TESTASARRAY(); return true; }
295 template <> bool checkParameters(TH2S*h1,TH2S*h2) { TESTASARRAY(); return true; }
296 
297 template <> bool checkParameters(TProfile*h1,TProfile*h2)
298 {
299  TESTSIMPLEVAR(GetNcells());
300  Int_t sz = h1->GetNcells();
301  for (Int_t i = 0; i < sz; i++)
302  TESTSIMPLEVAR (GetBinEntries(i));
303  TESTSIMPLEVAR(GetErrorOption());
304  TESTSIMPLEVAR(GetYmin());
305  TESTSIMPLEVAR(GetYmax());
306  //TESTSIMPLEVAR(fScaling);
307  Double_t stats1[20], stats2[20];
308  h1->GetStats(stats1);
309  h2->GetStats(stats2);
310  TESTSIMPLEEXTVAR(stats1[4], stats2[4], fTsumwy);
311  TESTSIMPLEEXTVAR(stats1[5], stats2[5], fTsumwy2);
312  return true;
313 }
314 
316 {
317  TESTSIMPLEVAR(GetNcells());
318  Int_t sz = h1->GetNcells();
319  for (Int_t i = 0; i < sz; i++)
320  TESTSIMPLEVAR (GetBinEntries(i));
321  TESTSIMPLEVAR(GetErrorOption());
322  TESTSIMPLEVAR(GetZmin());
323  TESTSIMPLEVAR(GetZmax());
324  //TESTSIMPLEVAR(fScaling);
325  Double_t stats1[20], stats2[20];
326  h1->GetStats(stats1);
327  h2->GetStats(stats2);
328  TESTSIMPLEEXTVAR(stats1[7], stats2[7], fTsumwz);
329  TESTSIMPLEEXTVAR(stats1[8], stats2[8], fTsumwz2);
330  return true;
331 }
cfg_ignore_errors_related_to_a_non_existant_sumw2_array
std::atomic< bool > cfg_ignore_errors_related_to_a_non_existant_sumw2_array
Definition: jobflags.h:11
REPORT
#define REPORT(x)
Definition: comphistpars.h:24
fitman.sz
sz
Definition: fitman.py:527
checkParameters
bool checkParameters(T *h1, T *h2)
varCompatible
bool varCompatible(const T var1, const T var2)
Definition: comphistpars.h:25
TESTSIMPLEEXTVAR
#define TESTSIMPLEEXTVAR(var1, var2, varname)
Definition: comphistpars.h:89
ACTUALTESTARRAY
#define ACTUALTESTARRAY(a, b, name)
Definition: comphistpars.h:117
jobflags.h
TH1I
Definition: rootspy.cxx:332
TH2F
Definition: rootspy.cxx:420
TESTASARRAY
#define TESTASARRAY()
Definition: comphistpars.h:121
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
TProfile2D
Definition: rootspy.cxx:531
TH1D
Definition: rootspy.cxx:342
TrigInDetValidation_Base.test
test
Definition: TrigInDetValidation_Base.py:144
read_hist_ntuple.h1
h1
Definition: read_hist_ntuple.py:21
arrayCompatible
bool arrayCompatible(const TArrayX *a1, const TArrayX *a2, const char *name)
Definition: comphistpars.h:98
TH2S
Definition: rootspy.cxx:400
axisCompatible
bool axisCompatible(const TAxis *h1, const TAxis *h2)
Definition: comphistpars.h:125
TH1C
Definition: rootspy.cxx:352
TESTSIMPLEVAR
#define TESTSIMPLEVAR(varname)
Definition: comphistpars.h:84
lumiFormat.i
int i
Definition: lumiFormat.py:92
TESTARRAYPTR
#define TESTARRAYPTR(varname)
Definition: comphistpars.h:123
TH2I
Definition: rootspy.cxx:410
TH2D
Definition: rootspy.cxx:430
TH2
Definition: rootspy.cxx:373
rootincludes.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
cfg_report_NaN
std::atomic< bool > cfg_report_NaN
Definition: jobflags.h:9
TProfile
Definition: rootspy.cxx:515
floatingpointcompat
bool floatingpointcompat(double var1, double var2, const double &epsilon)
Definition: comphistpars.h:44
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
TH1F
Definition: rootspy.cxx:320
TH2C
Definition: rootspy.cxx:390
TH1S
Definition: rootspy.cxx:362
TH1
Definition: rootspy.cxx:268
generate::GetEntries
double GetEntries(TH1D *h, int ilow, int ihi)
Definition: rmsFrac.cxx:20
tlistsCompatible
bool tlistsCompatible(const TList *h1, const TList *h2)
Definition: comphistpars.h:161
TESTSIMPLEUNSUPPORTEDSTYLEVAR
#define TESTSIMPLEUNSUPPORTEDSTYLEVAR(varname)
Definition: comphistpars.h:94
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35