Belle II Software development
ComparisonBase Class Reference
Inheritance diagram for ComparisonBase:
PvalueTest AndersonDarlingTest Chi2Test KolmogorovTest

Public Member Functions

 __init__ (self, object_a, object_b, Optional[MetaOptionParser] mop=None, debug=False)
 
 ensure_compute (self)
 
 comparison_result (self)
 
 comparison_result_long (self)
 
 can_compare (self)
 

Public Attributes

 object_a = object_a
 store the first object to compare
 
 object_b = object_b
 store the second object to compare
 
 mop = mop
 MetaOptionParser.
 
 debug = debug
 enable debug?
 
bool computed = False
 used to store, whether the quantities have already been compared
 

Protected Member Functions

str _get_comparison_result (self)
 
str _get_comparison_result_long (self)
 
 _compute (self)
 
bool _has_correct_types (self)
 
None _raise_has_correct_types (self)
 
bool _has_compatible_bins (self)
 
None _raise_has_compatible_bins (self)
 

Static Protected Member Functions

 _convert_teff_to_hist (teff_a)
 

Protected Attributes

str _comparison_result = "not_compared"
 Comparison result, i.e.
 
str _comparison_result_long = ""
 Longer description of the comparison result (e.g.
 

Detailed Description

Base class for all comparison implementations.

Follows 3 steps:

1. Initialize the class together with two ROOT objects of different
revisions (that are to be compared) and the metaoptions (given in the
corresponding validation (steering) file), that determine how to compare
them.

2. The Comparison class saves the ROOT objects and the metaoptions
internally, but does not compute anything yet

3. If :meth:`ensure_compute` is called, or any property is accessed that
depends on computation, the internal implementation :meth:`_compute`
(to be implemented in the subclass) is called.

4. :meth:`_compute` ensures that all values, like chi2, p-value etc. are
computed

5. Two properties :meth:`comparison_result` (pass/warning/error) and
:meth:`comparison_result_long` (longer description of the comparison result)
allow to access the results.

Definition at line 95 of file validationcomparison.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
object_a,
object_b,
Optional[MetaOptionParser] mop = None,
debug = False )
Initialize ComparisonBase class

:param object_a:
:param object_b:
:param mop: MetaOptionParser
:param debug (bool): Debug mode enabled?

Definition at line 121 of file validationcomparison.py.

127 ):
128 """
129 Initialize ComparisonBase class
130
131 :param object_a:
132 :param object_b:
133 :param mop: MetaOptionParser
134 :param debug (bool): Debug mode enabled?
135 """
136
137 self.object_a = object_a
138
139
140 self.object_b = object_b
141
142 if mop is None:
143 mop = MetaOptionParser()
144
145 self.mop = mop
146
147
148 self.debug = debug
149
150
151 self.computed = False
152
153
154 self._comparison_result = "not_compared"
155
157 self._comparison_result_long = ""
158

Member Function Documentation

◆ _compute()

_compute ( self)
protected
 This method performs the actual computations. 

Reimplemented in AndersonDarlingTest, Chi2Test, KolmogorovTest, and PvalueTest.

Definition at line 222 of file validationcomparison.py.

222 def _compute(self):
223 """ This method performs the actual computations. """
224

◆ _convert_teff_to_hist()

_convert_teff_to_hist ( teff_a)
staticprotected
Convert the content of a TEfficiency plot to a histogram and set
the bin content and errors

Definition at line 313 of file validationcomparison.py.

313 def _convert_teff_to_hist(teff_a):
314 """
315 Convert the content of a TEfficiency plot to a histogram and set
316 the bin content and errors
317 """
318 conv_hist = teff_a.GetTotalHistogram()
319 xbin_count = conv_hist.GetNbinsX()
320 xbin_low = conv_hist.GetXaxis().GetXmin()
321 xbin_max = conv_hist.GetXaxis().GetXmax()
322
323 th1 = ROOT.TH1D(
324 teff_a.GetName() + "root_conversion",
325 teff_a.GetName(),
326 xbin_count,
327 xbin_low,
328 xbin_max,
329 )
330 # starting from the first to the last bin, ignoring the under/overflow
331 # bins
332 for i in range(1, xbin_count):
333 th1.SetBinContent(i, teff_a.GetEfficiency(i))
334 th1.SetBinError(i, teff_a.GetEfficiencyErrorLow(i))
335
336 return th1
337
338

◆ _get_comparison_result()

str _get_comparison_result ( self)
protected
 Used to format the value of :attr:`_comparison_result`. 

Reimplemented in PvalueTest.

Definition at line 202 of file validationcomparison.py.

202 def _get_comparison_result(self) -> str:
203 """ Used to format the value of :attr:`_comparison_result`. """
204

◆ _get_comparison_result_long()

str _get_comparison_result_long ( self)
protected
 Used to format the value of :attr:`_comparison_result_long`. 

Reimplemented in AndersonDarlingTest, Chi2Test, KolmogorovTest, and PvalueTest.

Definition at line 206 of file validationcomparison.py.

206 def _get_comparison_result_long(self) -> str:
207 """ Used to format the value of :attr:`_comparison_result_long`. """
208

◆ _has_compatible_bins()

bool _has_compatible_bins ( self)
protected
Check if both ROOT objects have the same amount of bins
@return: True if the bins are equal, otherwise False

Definition at line 275 of file validationcomparison.py.

275 def _has_compatible_bins(self) -> bool:
276 """
277 Check if both ROOT objects have the same amount of bins
278 @return: True if the bins are equal, otherwise False
279 """
280 if (
281 self.object_a.ClassName()
282 == "TEfficiency"
283 == self.object_b.ClassName()
284 ):
285 nbins_a = self.object_a.GetTotalHistogram().GetNbinsX()
286 nbins_b = self.object_b.GetTotalHistogram().GetNbinsX()
287 else:
288 nbins_a = self.object_a.GetNbinsX()
289 nbins_b = self.object_b.GetNbinsX()
290
291 return nbins_a == nbins_b
292

◆ _has_correct_types()

bool _has_correct_types ( self)
protected
@return: True if the two objects have a) a type supported for
    comparison and b) can be compared with each other

Definition at line 231 of file validationcomparison.py.

231 def _has_correct_types(self) -> bool:
232 """
233 @return: True if the two objects have a) a type supported for
234 comparison and b) can be compared with each other
235 """
236 if self.object_a is None or self.object_b is None:
237 return False
238
239 # check if the supplied object inherit from one of the supported types
240 # and if they are of the same type
241 supported_types = ["TProfile", "TH1D", "TH1F", "TEfficiency"]
242 if self.object_a.ClassName() != self.object_b.ClassName():
243 return False
244 if self.object_a.ClassName() not in supported_types:
245 return False
246
247 if self.object_a.ClassName() == "TEfficiency":
248 # can only handle TEfficiencies with dimension one atm
249 if self.object_a.GetDimension() > 1:
250 return False
251
252 return True
253

◆ _raise_has_compatible_bins()

None _raise_has_compatible_bins ( self)
protected
Raise Exception if not both ROOT objects have the same amount of bins
@return: None

Definition at line 293 of file validationcomparison.py.

293 def _raise_has_compatible_bins(self) -> None:
294 """
295 Raise Exception if not both ROOT objects have the same amount of bins
296 @return: None
297 """
298 if not self._has_compatible_bins():
299 msg = (
300 "The objects have differing x bin count: {} has {} vs. {} "
301 "has {}."
302 )
303 raise DifferingBinCount(
304 msg.format(
305 self.object_a.GetName(),
306 self.object_a.GetNbinsX(),
307 self.object_b.GetName(),
308 self.object_b.GetNbinsX(),
309 )
310 )
311

◆ _raise_has_correct_types()

None _raise_has_correct_types ( self)
protected
Raise Exception if not the two objects have a) a type supported for
comparison and b) can be compared with each other
@return: None

Definition at line 254 of file validationcomparison.py.

254 def _raise_has_correct_types(self) -> None:
255 """
256 Raise Exception if not the two objects have a) a type supported for
257 comparison and b) can be compared with each other
258 @return: None
259 """
260 if not self._has_correct_types():
261 msg = (
262 "Comparison of {} (Type {}) with {} (Type {}) not "
263 "supported.\nPlease open a GitLab issue (validation "
264 "label) if you need this supported. "
265 )
266 raise ObjectsNotSupported(
267 msg.format(
268 self.object_a.GetName(),
269 self.object_a.ClassName(),
270 self.object_b.GetName(),
271 self.object_b.ClassName(),
272 )
273 )
274

◆ can_compare()

can_compare ( self)
@return: True if the two objects can be compared, False otherwise

Definition at line 225 of file validationcomparison.py.

225 def can_compare(self):
226 """
227 @return: True if the two objects can be compared, False otherwise
228 """
229 return self._has_correct_types() and self._has_compatible_bins()
230

◆ comparison_result()

comparison_result ( self)
 Comparison result, i.e. pass/warning/error 

Definition at line 210 of file validationcomparison.py.

210 def comparison_result(self):
211 """ Comparison result, i.e. pass/warning/error """
212 self.ensure_compute()
213 return self._comparison_result
214

◆ comparison_result_long()

comparison_result_long ( self)
 Longer description of the comparison result 

Definition at line 216 of file validationcomparison.py.

216 def comparison_result_long(self):
217 """ Longer description of the comparison result """
218 self.ensure_compute()
219 return self._comparison_result_long
220

◆ ensure_compute()

ensure_compute ( self)
Ensure all required quantities get computed and are cached inside the
class

Definition at line 159 of file validationcomparison.py.

159 def ensure_compute(self):
160 """
161 Ensure all required quantities get computed and are cached inside the
162 class
163 """
164 if self.computed:
165 return
166
167 if self.mop.has_option("nocompare"):
168 # is comparison disabled for this plot ?
169 self._comparison_result_long = "Testing is disabled for this plot"
170 return
171
172 fail_message = "Comparison failed: "
173
174 # Note: default for comparison_result is "not_compared"
175 try:
176 self._compute()
177 except ObjectsNotSupported as e:
178 self._comparison_result_long = fail_message + str(e)
179 except DifferingBinCount as e:
180 self._comparison_result = "error"
181 self._comparison_result_long = fail_message + str(e)
182 except TooFewBins as e:
183 self._comparison_result_long = fail_message + str(e)
184 except ComparisonFailed as e:
185 self._comparison_result = "error"
186 self._comparison_result_long = fail_message + str(e)
187 except Exception as e:
188 self._comparison_result = "error"
189 self._comparison_result_long = (
190 "Unknown error occurred. Please "
191 "submit a bug report. " + str(e)
192 )
193 else:
194 # Will be already set in case of errors above and we don't want
195 # to overwrite this.
196 self._comparison_result_long = self._get_comparison_result_long()
197 self._comparison_result = self._get_comparison_result()
198
199 self.computed = True
200

Member Data Documentation

◆ _comparison_result

str _comparison_result = "not_compared"
protected

Comparison result, i.e.

equal/warning/error

Definition at line 154 of file validationcomparison.py.

◆ _comparison_result_long

str _comparison_result_long = ""
protected

Longer description of the comparison result (e.g.

'performed Chi2 Test ... with chi2 = ...').

Definition at line 157 of file validationcomparison.py.

◆ computed

bool computed = False

used to store, whether the quantities have already been compared

Definition at line 151 of file validationcomparison.py.

◆ debug

debug = debug

enable debug?

Definition at line 148 of file validationcomparison.py.

◆ mop

mop = mop

MetaOptionParser.

Definition at line 145 of file validationcomparison.py.

◆ object_a

object_a = object_a

store the first object to compare

Definition at line 137 of file validationcomparison.py.

◆ object_b

object_b = object_b

store the second object to compare

Definition at line 140 of file validationcomparison.py.


The documentation for this class was generated from the following file: