The Gaudi Framework  v36r1 (3e2fb5a8)
HistoUtils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
18 """
19 This module contains set of simple and useful utilities for booking and
20 manipulations with Gaudi-AIDA histograms, inspired by Thomas' request
21 
22 The module contains following public symbols:
23 
24  - book for booking of various 1D,2D&3D-histograms
25  - bookProf for booking of various 1D&2D-profiles
26  - getAsAIDA for retrieval of histograms/profiles from HTS in AIDA format
27  - getAsROOT for retrieval of histograms/profiles from HTS in ROOT format
28  - fill for smart filling of 1D-histogram (AIDA or ROOT)
29  - aida2root for conversion of AIDA histogram to ROOT
30  - HistoStats for many statistical information
31  - HistoFile class for storing histograms to a file
32  - histoDump for dumping of the histogram in text format (a'la HBOOK)
33  - dumpHisto for dumping of the histogram in text format (a'la HBOOK)
34 
35 """
36 from __future__ import print_function
37 # =============================================================================
38 __author__ = "Vanya BELYAEV ibelyaev@physics.syr.edu"
39 # =============================================================================
40 __all__ = (
41  'book', # book AIDA histogram using Histogram Service
42  'bookProf', # book AIDA profile histogram using Histogram Service
43  'getAsAIDA', # get the histogram form Histogram Service as AIDA histogram
44  'getAsROOT', # get the histogram form Histogram Service as AIDA histogram
45  'fill', # "power-fill" method for filling of histograms
46  'aida2root', # AIDA -> ROOT converter
47  'HistoStats', # statistical information for 1D-histograms
48  'HistoFile', # class for storing histograms to a file
49  'histoDump', # dump histogramintext format a'la HBOOK
50  'dumpHisto' # dump histogramintext format a'la HBOOK
51 )
52 # =============================================================================
53 # import core of Gaudi
54 import ROOT
55 from GaudiPython.Bindings import AppMgr
56 from GaudiPython.Bindings import iHistogramSvc
57 from GaudiPython.Bindings import gbl as cpp
58 HID = cpp.GaudiAlg.ID
59 
60 
61 useROOT = False
62 
63 # =============================================================================
64 # Helper private auxiliary function to get Application Manager
65 
66 
67 def _getAppMgr(**kwargs):
68  """
69  Helper private auxiliary function to get Application Manager
70  """
71  gaudi = kwargs.get('gaudi', None)
72  if not gaudi:
73  gaudi = AppMgr()
74  if not gaudi:
75  raise RuntimeError('Unable to get valid ApplicationMgr')
76 
77  state = gaudi._isvc.FSMState()
78  if state < cpp.Gaudi.StateMachine.CONFIGURED:
79  gaudi.config()
80  state = gaudi._isvc.FSMState()
81  if state < cpp.Gaudi.StateMachine.INITIALIZED:
82  gaudi.initialize()
83 
84  return gaudi # RETURN
85 
86 
87 # =============================================================================
88 # Helper private auxiliary function to get iHistogramSvs
89 
90 
91 def _getHistoSvc(**kwargs):
92  """
93  Helper private auxiliary function to get iHistogramSvs
94  """
95  svc = kwargs.get('service', None)
96  if not svc:
97  svc = kwargs.get('svc', None)
98  else:
99  return svc # RETURN
100  gaudi = _getAppMgr(**kwargs)
101  return gaudi.histsvc() # RETURN
102 
103 
104 # =============================================================================
105 # Helper private auxiliary function to get iDataSvs
106 
107 
108 def _getEvtSvc(**kwargs):
109  """
110  Helper private auxiliary function to get iDataSvs
111  """
112  svc = kwargs.get('service', None)
113  if not svc:
114  svc = kwargs.get('svc', None)
115  else:
116  return svc # RETURN
117  gaudi = _getAppMgr(**kwargs)
118  return gaudi.evtsvc() # RETURN
119 
120 
121 # =============================================================================
122 # The trivial function to book the various 1D,2D&3D-histograms
123 
124 
125 def book(*args, **kwargs):
126  """
127  The trivial function to book the various 1D,2D&3D-histograms
128 
129  (1) book the trivial 1D histogram with full path
130 
131  >>> h1D = book ( 'path/to/my/histo' , ## path in Histogram Transient Store
132  'cosine of decay angle ' , ## histogram title
133  100 , ## number of bins
134  -1 , ## low edge
135  100 ) ## high edge
136 
137  (2) book the trivial 1D histogram with directory path and string ID :
138 
139  >>> h1D = book ( 'path/to/directory' , ## the path to directory in HTS
140  'H1' , ## string histogram identifier
141  'cosine of decay angle ' , ## histogram title
142  100 , ## number of bins
143  -1 , ## low edge
144  100 ) ## high edge
145 
146  (3) book the trivial 1D histogram with directory path and integer ID :
147 
148  >>> h1D = book ( 'path/to/directory' , ## the path to directory in HTS
149  124 , ## integer histogram identifier
150  'cosine of decay angle ' , ## histogram title
151  100 , ## number of bins
152  -1 , ## low edge
153  100 ) ## high edge
154 
155  (4) book the trivial 2D histogram with full path
156 
157  >>> h1D = book ( 'path/to/my/histo' , ## path in Histogram Transient Store
158  'm12**2 versus m13**2' , ## histogram title
159  50 , ## number of X-bins
160  1.0 , ## low X-edge
161  4.0 , ## high X-edge
162  50 , ## number of Y-bins
163  1 , ## low Y-edge
164  2 ) ## high Y-edge
165 
166  (5) book the trivial 2D histogram with directory path and literal ID
167 
168  >>> h1D = book ( 'path/to/directory' , ## path in Histogram Transient Store
169  'Dalitz1' , ## literal histogram identifier
170  'm12**2 versus m13**2' , ## histogram title
171  50 , ## number of X-bins
172  1.0 , ## low X-edge
173  4.0 , ## high X-edge
174  50 , ## number of Y-bins
175  1 , ## low Y-edge
176  2 ) ## high Y-edge
177 
178  (6) book the trivial 2D histogram with directory path and integer ID
179 
180  >>> h1D = book ( 'path/to/directory' , ## path in Histogram Transient Store
181  854 , ## integer histogram identifier
182  'm12**2 versus m13**2' , ## histogram title
183  50 , ## number of X-bins
184  1.0 , ## low X-edge
185  4.0 , ## high X-edge
186  50 , ## number of Y-bins
187  1.0 , ## low Y-edge
188  4.0 ) ## high Y-edge
189 
190  (7) book the trivial 3D histogram with full path
191 
192  >>> h1D = book ( 'path/to/my/histo' , ## path in Histogram Transient Store
193  'x vs y vs z ' , ## histogram title
194  10 , ## number of X-bins
195  -1.0 , ## low X-edge
196  1.0 , ## high X-edge
197  10 , ## number of Y-bins
198  -1.0 , ## low Y-edge
199  1.0 , ## high Y-edge
200  10 , ## number of Z-bins
201  -1.0 , ## low Z-edge
202  1.0 ) ## high Z-edge
203 
204  (8) book the trivial 3D histogram with directory path and literal ID
205 
206  >>> h1D = book ( 'path/to/directory' , ## path in Histogram Transient Store
207  'xyz' , ## literal histogram identifier
208  'x vs y vs z' , ## histogram title
209  10 , ## number of X-bins
210  -1.0 , ## low X-edge
211  1.0 , ## high X-edge
212  10 , ## number of Y-bins
213  -1.0 , ## low Y-edge
214  1.0 , ## high Y-edge
215  10 , ## number of Z-bins
216  -1.0 , ## low Z-edge
217  1.0 ) ## high Z-edge
218 
219  (9) book the trivial 3D histogram with directory path and integer ID
220 
221  >>> h1D = book ( 'path/to/directory' , ## path in Histogram Transient Store
222  888 , ## integer histogram identifier
223  'x vs y vs z' , ## histogram title
224  10 , ## number of X-bins
225  -1.0 , ## low X-edge
226  1.0 , ## high X-edge
227  10 , ## number of Y-bins
228  -1.0 , ## low Y-edge
229  1.0 , ## high Y-edge
230  10 , ## number of Z-bins
231  -1.0 , ## low Z-edge
232  1.0 ) ## high Z-edge
233 
234  Many other booking methods are available,
235  e.g. for the histograms with non-equidistant bins, see IHistogamSvc::book
236 
237  """
238  if useROOT or kwargs.get('useROOT',
239  False) or not kwargs.get('useAIDA', True):
240  from ROOT import TH1D
241  a0 = args[0]
242  a1 = args[1]
243  a2 = args[2]
244  if not str is type(a1):
245  a1 = 'h' + str(a1)
246  if str is type(a2):
247  return TH1D(a0 + a1, a2, *args[3:])
248  else:
249  return TH1D(a0, a1, *args[2:])
250 
251  svc = _getHistoSvc(**kwargs)
252  if not svc:
253  raise RuntimeError('Unable to get valid HistogramService ')
254  # book the histogram using the service
255  return svc.book(*args) # RETURN
256 
257 
258 book.__doc__ += '\n\n' + '\thelp(iHistogramSvc.book) : \n\n' \
259  + iHistogramSvc.book . __doc__
260 book.__doc__ += '\n\n' + '\thelp(IHistogramSvc::book) : \n\n' \
261  + cpp.IHistogramSvc.book . __doc__
262 
263 # =============================================================================
264 # The trivial function to book 1D&2D profile histograms:
265 
266 
267 def bookProf(*args, **kwargs):
268  """
269 
270  The trivial function to book 1D&2D profile histograms:
271 
272  (1) book 1D-profile histogram with full path in Histogram Transient Store:
273  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
274  'Energy Correction' , ## the histogram title
275  100 , ## number of X-bins
276  0.0 , ## low X-edge
277  100 ) ## high X-edge
278 
279  (2) book 1D-profile histogram with directory path and literal ID
280  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
281  'Calibration' , ## the histogram literal identifier
282  'Energy Correction' , ## the histogram title
283  100 , ## number of X-bins
284  0.0 , ## low X-edge
285  100 ) ## high X-edge
286 
287  (3) book 1D-profile histogram with directory path and integer ID
288  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
289  418 , ## the histogram integer identifier
290  'Energy Correction' , ## the histogram title
291  100 , ## number of X-bins
292  0.0 , ## low X-edge
293  100 ) ## high X-edge
294 
295  (4) book 2D-profile histogram with full path in Histogram Transient Store:
296  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
297  'Energy Correction' , ## the histogram title
298  50 , ## number of X-bins
299  0.0 , ## low X-edge
300  100 , ## high X-edge
301  50 , ## number of Y-bins
302  0.0 , ## low Y-edge
303  100 ) ## high Y-edge
304 
305  (5) book 2D-profile histogram with directory path and literal ID
306  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
307  'Calibration' , ## the histogram literal identifier
308  'Energy Correction' , ## the histogram title
309  50 , ## number of X-bins
310  0.0 , ## low X-edge
311  100 , ## high X-edge
312  50 , ## number of Y-bins
313  0.0 , ## low Y-edge
314  100 ) ## high Y-edge
315 
316  (6) book 2D-profile histogram with directory path and integer ID
317  >>> histo = bookProf ( 'path/to/my/profile' , ## path in Histogram Transient Store
318  418 , ## the histogram integer identifier
319  'Energy Correction' , ## the histogram title
320  50 , ## number of X-bins
321  0.0 , ## low X-edge
322  100 , ## high X-edge
323  50 , ## number of Y-bins
324  0.0 , ## low Y-edge
325  100 ) ## high Y-edge
326 
327  Many other booking methods are available,
328  e.g. for the histograms with non-equidistant bins, see IHistogamSvc::book
329 
330  """
331  svc = _getHistoSvc(**kwargs)
332  if not svc:
333  raise RuntimeError('Unable to get valid HistogramService ')
334  # book the histogram using the service
335  return svc.bookProf(*args) # RETURN
336 
337 
338 bookProf.__doc__ += '\n\n' + '\thelp(iHistogramSvc.bookProf) : \n\n' \
339  + iHistogramSvc.bookProf . __doc__
340 bookProf.__doc__ += '\n\n' + '\thelp(IHistogramSvc::bookProf) : \n\n' \
341  + cpp.IHistogramSvc.bookProf . __doc__
342 
343 # =============================================================================
344 # The most trivial function to retrieve the histogram from Histogram Transient Store
345 
346 
347 def getAsAIDA(path, **kwargs):
348  """
349 
350  The most trivial function to retrieve the histogram from Histogram Transient Store
351  The histogram is returned by reference to its AIDA-representation (if possible)
352 
353  >>> h = getAsAIDA ( 'some/path/to/my/histogram' )
354 
355  """
356  svc = _getHistoSvc(**kwargs)
357  if not svc:
358  raise RuntimeError('Unable to get valid HistogramService ')
359  # return the histogram
360  return svc.getAsAIDA(path) # RETURN
361 
362 
363 getAsAIDA.__doc__ += '\n\n' + '\thelp(iHistogramSvc.getAsAIDA) : \n\n' \
364  + iHistogramSvc.getAsAIDA . __doc__
365 getAsAIDA.__doc__ += '\n\n' + '\thelp(iHistogramSvc.retrieve) : \n\n' \
366  + iHistogramSvc.retrieve . __doc__
367 
368 # =============================================================================
369 # The most trivial function to retrieve the histogram from Histogram Transient Store
370 
371 
372 def getAsROOT(path, **kwargs):
373  """
374 
375  The most trivial function to retrieve the histogram from Histogram Transient Store
376  The histogram is returned by reference to its underlying native ROOT-representation (if possible)
377 
378  >>> h = getAsROOT ( 'some/path/to/my/histogram' )
379 
380  """
381  svc = _getHistoSvc(**kwargs)
382  if not svc:
383  raise RuntimeError('Unable to get valid HistogramService ')
384  # return the histogram
385  return svc.getAsROOT(path) # RETURN
386 
387 
388 getAsROOT.__doc__ += '\n\n' + '\thelp(iHistogramSvc.getAsROOT) : \n\n' \
389  + iHistogramSvc.getAsROOT . __doc__
390 
391 
392 # =============================================================================
393 # The function which allows 'the smart fill' of 1D-histogram
394 def fill(
395  histo, # histogram
396  data, # input data
397  fun=lambda x: x, # function to be used
398  cut=lambda x: True, # cut to be applied
399  **kwargs): # optional extra arguments
400  """
401 
402  The function which allows 'the smart fill' of 1D-histogram
403 
404  >>> histo = ...
405 
406  The most trivial case, fill with the value
407  >>> fill ( histo , 1.0 )
408 
409  Fill from any iterable object (sequence)
410  >>> fill ( histo , [1,,2,3,4,5,10] )
411 
412  Fill from iterable object and apply the function:
413  >>> fill ( histo , [1,2,3,4,5] , math.sin )
414 
415  Use lambda form:
416  >>> fill ( histo , [1,2,3,4,5] , lambda x : x*x )
417 
418  The same
419  >>> fill ( histo , [1,2,3,4,5] , fun = lambda x : x*x )
420 
421  Use internal attributes:
422  >>> tracks = evtSvc['Rec/Track/Best'] ## iterable container of tracks
423  >>> fill ( histo , tracks , lambda t : t.pt() )
424 
425  Apply the predicate: fill only even numbers:
426  >>> fill ( histo , [1,2,3,4,5,6,7] , lambda x : x , lambda y : y%2 )
427 
428  The same (omit the trivial function) :
429  >>> fill ( histo , [1,2,3,4,5,6,7] , cut = lambda y : y%2 )
430 
431  Apply the predicate: fill only pt for positively charged tracks:
432  >>> tracks = evtSvc['Rec/Track/Best']
433  >>> fill ( histo , tracks , lambda t : t.pt() , lambda t : 0<t.charge() )
434 
435  The same:
436  >>> fill ( histo , tracks ,
437  fun = lambda t : t.pt() ,
438  cut = lambda t : 0<t.charge() )
439 
440  Ordinary functions are also fine:
441  >>> def myfun ( track ) : return sin( track.pt() + track.p() )
442  >>> def mycut ( track ) : return track.p() > 100 * GeV
443  >>> fill ( histo , tracks , myfun , mycut )
444 
445  The 'data' could be the address in TES, in this case the object
446  is retrieved from TES and the method is applied to the objects,
447  retrieved from TES:
448  >>> fill ( histo , ## the reference to the histogram
449  'Rec/Track/Best' , ## the location of objects in TES
450  lambda t : t.pt() ) ## function to be used for histogram fill
451  >>> fill ( histo , ## the reference to the histogram
452  'Rec/Track/Best' , ## the address of objects in TES
453  lambda t : t.pt() , ## the function to be used for histogram fill
454  lambda t : t.charge()>0 ) ## the criteria to select tracks
455 
456  The arguments 'fun' and 'cut' could be strings, in this case
457  they are evaluated by python before usage.
458  This option could be often very useful.
459 
460  """
461 
462  # if value is a string, try to get the objects from TES
463  if type(data) == str:
464  svc = _getEvtSvc(**kwargs)
465  data = svc[data]
466  return fill(histo, data, fun, cut, **kwargs)
467 
468  # if the function is a string: evaluate it!
469  if type(fun) == str:
470  fun = eval(fun, globals())
471 
472  # if the criterion is a string: evaluate it!
473  if type(cut) == str:
474  cut = eval(cut, globals())
475 
476  if not hasattr(data, '__iter__'):
477  data = [data]
478 
479  if not hasattr(histo, 'fill') and hasattr(histo, 'Fill'):
480  setattr(histo, 'fill', getattr(histo, 'Fill'))
481 
482  for item in data:
483  if not cut(item):
484  continue # CONTINUE
485  histo.fill(fun(item))
486 
487  return histo # RETURN
488 
489 
490 # =============================================================================
491 # AIDA -> ROOT converter
492 aida2root = cpp.Gaudi.Utils.Aida2ROOT.aida2root
493 
494 # =============================================================================
495 # Convert AIDA to ROOT
496 
497 
498 def _to_root_(self):
499  """
500  Convert AIDA to ROOT
501 
502  >>> aida = ... ## get AIDA histogram
503  >>> root = aida.toROOT() ## convert it to ROOT
504 
505  """
506  return aida2root(self)
507 
508 
509 _to_root_.__doc__ += aida2root.__doc__
510 
511 for t in (cpp.AIDA.IHistogram3D, cpp.AIDA.IHistogram2D, cpp.AIDA.IHistogram1D,
512  cpp.AIDA.IProfile2D, cpp.AIDA.IProfile1D):
513  if not hasattr(t, 'Fill') and hasattr(t, 'fill'):
514  setattr(t, 'Fill', getattr(t, 'fill'))
515  for attr in ('toROOT', 'toRoot', 'asROOT', 'asRoot', 'AsROOT', 'AsRoot'):
516  if not hasattr(t, attr):
517  setattr(t, attr, _to_root_)
518 
519 cpp.AIDA.IHistogram3D. __repr__ = lambda s: cpp.GaudiAlg.Print3D.toString(
520  s, HID(s.title()))
521 cpp.AIDA.IHistogram3D.__str__ = cpp.AIDA.IHistogram3D.__repr__
522 
523 HistoStats = cpp.Gaudi.Utils.HistoStats
524 
525 # =============================================================================
526 # Evaluate 'bin-by-bin' momentum of certain order around the value
527 
528 
529 def _moment_(self, order, value=0):
530  """
531  Evaluate 'bin-by-bin' momentum of order 'order' around the value 'value'
532  for 1D histogram
533 
534  >>> h1 = ...
535  >>> print(h1.moment ( 5 ))
536 
537  """
538  return HistoStats.moment(self, order, value)
539 
540 
541 # =============================================================================
542 # Evaluate error in 'bin-by-bin' momentum of certain order around the value
543 
544 
545 def _momentErr_(self, order):
546  """
547  Evaluate error for 'bin-by-bin' momentum of order 'order' around the value 'value'
548  for 1D histogram
549 
550  >>> h1 = ...
551  >>> print(h1.momentErr ( 5 ))
552 
553  """
554  return HistoStats.momentErr(self, order)
555 
556 
557 # =============================================================================
558 # Evaluate 'bin-by-bin' central momentum (around mean value)
559 
560 
561 def _centralMoment_(self, order):
562  """
563  Evaluate 'bin-by-bin' central momentum (around mean value)
564  for 1D histogram
565 
566  >>> h1 = ...
567  >>> print(h1.centralMoment ( 5 ))
568 
569  """
570  return HistoStats.centralMoment(self, order)
571 
572 
573 # =============================================================================
574 # Evaluate error in 'bin-by-bin' momentum of certain order around the value
575 
576 
577 def _centralMomentErr_(self, order):
578  """
579  Evaluate error for 'bin-by-bin' central momentum (around mean value)
580  for 1D histogram
581 
582  >>> h1 = ...
583  >>> print(h1.centralMomentErr ( 5 ))
584 
585  """
586  return HistoStats.centralMomentErr(self, order)
587 
588 
589 # =============================================================================
590 # Evaluate 'bin-by-bin' skewness for 1D histogram
591 
592 
593 def _skewness_(self):
594  """
595  Evaluate 'bin-by-bin' skewness for 1D AIDA histogram
596 
597  >>> h1 = ...
598  >>> print(h1.skewness())
599 
600  """
601  return HistoStats.skewness(self)
602 
603 
604 # =============================================================================
605 # Evaluate error for 'bin-by-bin' skewness for 1D histogram
606 
607 
608 def _skewnessErr_(self):
609  """
610  Evaluate error for 'bin-by-bin' skewness
611 
612  >>> h1 = ...
613  >>> print(h1.skewnessErr())
614 
615  """
616  return HistoStats.skewnessErr(self)
617 
618 
619 # =============================================================================
620 # Evaluate 'bin-by-bin' kurtosis for 1D histogram
621 
622 
623 def _kurtosis_(self):
624  """
625  Evaluate 'bin-by-bin' kurtosis
626 
627  >>> h1 = ...
628  >>> print(h1.kurtosis ())
629 
630  """
631  return HistoStats.kurtosis(self)
632 
633 
634 # =============================================================================
635 # Evaluate error for 'bin-by-bin' kurtosis for 1D histogram
636 
637 
638 def _kurtosisErr_(self):
639  """
640  Evaluate error for 'bin-by-bin' kurtotis for 1D AIDA histogram
641 
642  >>> h1 = ...
643  >>> print(h1.kurtotisErr())
644 
645  """
646  return HistoStats.kurtosisErr(self)
647 
648 
649 # =============================================================================
650 
651 
652 def _nEff_(self):
653  """
654  Number of equivalent entries
655  """
656  return HistoStats.nEff(self)
657 
658 
659 # =============================================================================
660 
661 
662 def _mean_(self):
663  """
664  Evaluate the MEAN value
665  """
666  return HistoStats.mean(self)
667 
668 
669 # =============================================================================
670 
671 
672 def _meanErr_(self):
673  """
674  Evaluate the error for MEAN estimate
675  """
676  return HistoStats.meanErr(self)
677 
678 
679 # =============================================================================
680 
681 
682 def _rms_(self):
683  """
684  Evaluate the RMS for AIDA histogram
685  """
686  return HistoStats.rms(self)
687 
688 
689 # =============================================================================
690 
691 
692 def _rmsErr_(self):
693  """
694  Evaluate the error for RMS estimate
695  """
696  return HistoStats.rmsErr(self)
697 
698 
699 # =============================================================================
700 
701 
703  """
704  Get an error in the sum bin height ('in-range integral')
705  """
706  return HistoStats.sumBinHeightErr(self)
707 
708 
709 # =============================================================================
710 
711 
713  """ Get an error in the sum bin height ('in-range integral') """
714  return HistoStats.sumAllBinHeightErr(self)
715 
716 
717 # =============================================================================
718 
719 
721  """
722  The fraction of overflow entries (useful for shape comparison)
723  """
724  return HistoStats.overflowEntriesFrac(self)
725 
726 
727 # =============================================================================
728 
729 
731  """
732  The error for fraction of overflow entries (useful for shape comparison)
733  """
734  return HistoStats.overflowEntriesFracErr(self)
735 
736 
737 # =============================================================================
738 
739 
741  """
742  The fraction of underflow entries (useful for shape comparison)
743  """
744  return HistoStats.underflowEntriesFrac(self)
745 
746 
747 # =============================================================================
748 
749 
751  """
752  The error for fraction of underflow entries (useful for shape comparison)
753  """
754  return HistoStats.underflowEntriesFracErr(self)
755 
756 
757 # =============================================================================
758 
759 
761  """
762  The fraction of overflow integral (useful for shape comparison)
763  """
764  return HistoStats.overflowIntegralFrac(self)
765 
766 
767 # =============================================================================
768 
769 
771  """
772  The error for fraction of overflow integral (useful for shape comparison)
773  """
774  return HistoStats.overflowIntegralFracErr(self)
775 
776 
777 # =============================================================================
778 
779 
781  """
782  The fraction of underflow integral (useful for shape comparison)
783  """
784  return HistoStats.underflowIntegralFrac(self)
785 
786 
787 # =============================================================================
788 
789 
791  """
792  The error for fraction of underflow integral (useful for shape comparison)
793  """
794  return HistoStats.underflowIntegralFracErr(self)
795 
796 
797 # =============================================================================
798 # get number of entries in histogram up to the certain bin (not-included)
799 # get number of entries in histogram form the certain
800 # minimal bin up to the certain maximal bin (not-included)
801 
802 
803 def _nEntries_(self, i1, i2=-10000000):
804  """
805  Get number of entries in histogram up to the certain bin (not-included)
806 
807  attention: underflow bin is included!
808 
809  >>> h1
810  >>> print(h1.nEntries ( 10 ))
811 
812  Get number of entries in histogram form the certain
813  minimal bin up to the certain maximal bin (not-included)
814 
815  >>> h1
816  >>> print(h1.nEntries ( 10 , 15 ))
817 
818  """
819  if i2 < i1 or i2 < 0:
820  return HistoStats.nEntries(self, i1)
821  return HistoStats.nEntries(self, i1, i2)
822 
823 
824 # =============================================================================
825 
826 
827 def _nEntriesFrac_(self, i1, i2=-10000000):
828  """
829  Get the fraction of entries in histogram up to the certain bin (not-included)
830 
831  attention: underflow bin is included!
832 
833  >>> h1
834  >>> print(h1.nEntriesFrac ( 10 ))
835 
836  Get the fraction of entries in histogram form the certain
837  minimal bin up to the certain maximal bin (not-included)
838 
839  >>> h1
840  >>> print(h1.nEntriesFrac ( 10 , 15 ))
841 
842  """
843  if i2 < i1 or i2 < 0:
844  return HistoStats.nEntriesFrac(self, i1)
845  return HistoStats.nEntriesFrac(self, i1, i2)
846 
847 
848 # =============================================================================
849 
850 
851 def _nEntriesFracErr_(self, i1, i2=-10000000):
852  """
853  Get error for fraction of entries in histogram up to the certain bin (not-included)
854 
855  attention: underflow bin is included!
856 
857  >>> h1
858  >>> print(h1.nEntriesFracErr( 10 ))
859 
860  Get error fraction of entries in histogram form the certain
861  minimal bin up to the certain maximal bin (not-included)
862 
863  >>> h1
864  >>> print(h1.nEntriesFracErr ( 10 , 15 ))
865 
866  """
867  if i2 < i1 or i2 < 0:
868  return HistoStats.nEntriesFracErr(self, i1)
869  return HistoStats.nEntriesFracErr(self, i1, i2)
870 
871 
872 # =============================================================================
873 i1DH = cpp.AIDA.IHistogram1D
874 
875 if not hasattr(i1DH, 'moment'):
876  i1DH.moment = _moment_
877 if not hasattr(i1DH, 'momentErr'):
878  i1DH.momentErr = _momentErr_
879 if not hasattr(i1DH, 'centralMoment'):
880  i1DH.centralMoment = _centralMoment_
881 if not hasattr(i1DH, 'momentMomentErr'):
882  i1DH.centralMomentErr = _centralMomentErr_
883 if not hasattr(i1DH, 'nEff'):
884  i1DH.nEff = _nEff_
885 if not hasattr(i1DH, 'mean'):
886  i1DH.mean = _mean_
887 if not hasattr(i1DH, 'meanErr'):
888  i1DH.meanErr = _meanErr_
889 if not hasattr(i1DH, 'rms'):
890  i1DH.rms = _rms_
891 if not hasattr(i1DH, 'rmsErr'):
892  i1DH.rmsErr = _rmsErr_
893 if not hasattr(i1DH, 'skewness'):
894  i1DH.skewness = _skewness_
895 if not hasattr(i1DH, 'skewnessErr'):
896  i1DH.skewnessErr = _skewnessErr_
897 if not hasattr(i1DH, 'kurtosis'):
898  i1DH.kurtosis = _kurtosis_
899 if not hasattr(i1DH, 'kurtosisErr'):
900  i1DH.kurtosisErr = _kurtosisErr_
901 
902 if not hasattr(i1DH, 'overflowEntriesFrac'):
903  i1DH.overflowEntriesFrac = _overflowEntriesFrac_
904 if not hasattr(i1DH, 'overflowEntriesFracErr'):
905  i1DH.overflowEntriesFracErr = _overflowEntriesFracErr_
906 if not hasattr(i1DH, 'underflowEntriesFrac'):
907  i1DH.underflowEntriesFrac = _underflowEntriesFrac_
908 if not hasattr(i1DH, 'underflowEntriesFracErr'):
909  i1DH.underflowEntriesFracErr = _underflowEntriesFracErr_
910 
911 if not hasattr(i1DH, 'overflowIntegralFrac'):
912  i1DH.overflowIntegralFrac = _overflowIntegralFrac_
913 if not hasattr(i1DH, 'overflowIntegralFracErr'):
914  i1DH.overflowIntegralFracErr = _overflowIntegralFracErr_
915 if not hasattr(i1DH, 'underflowIntegralFrac'):
916  i1DH.underflowIntegralFrac = _underflowIntegralFrac_
917 if not hasattr(i1DH, 'underflowIntegralFracErr'):
918  i1DH.underflowIntegralFracErr = _underflowIntegralFracErr_
919 
920 if not hasattr(i1DH, 'nEntries'):
921  i1DH.nEntries = _nEntries_
922 if not hasattr(i1DH, 'nEntriesFrac'):
923  i1DH.nEntriesFrac = _nEntriesFrac_
924 if not hasattr(i1DH, 'nEntriesFracErr'):
925  i1DH.nEntriesFracErr = _nEntriesFracErr_
926 
927 # ============================================================================
928 
929 
930 def _path_(self):
931  """
932  Get the path in THS for the given AIDA object:
933 
934  >>> aida =
935  >>> print(aida.path())
936 
937  """
938  return cpp.Gaudi.Utils.Histos.path(self)
939 
940 
941 iBH = cpp.AIDA.IBaseHistogram
942 if not hasattr(iBH, 'path'):
943  iBH.path = _path_
944 if not hasattr(iBH, 'TESpath'):
945  iBH.TESpath = _path_
946 if not hasattr(iBH, 'location'):
947  iBH.location = _path_
948 
949 
950 # =============================================================================
951 def __dumpHisto__(histo, *args):
952  """
953 
954  Dump the histogram/profile in text format (a'la HBOOK)
955 
956  >>> histo
957  >>> print(dumpHisto ( histo ))
958 
959  >>> print(histo.dump())
960  >>> print(histo.dump( 20 , 20 ))
961  >>> print(histo.dump( 20 , 20 , True ))
962 
963  Uses:
964 
965  """
966  return cpp.Gaudi.Utils.Histos.histoDump(histo, *args)
967 
968 
969 __dumpHisto__.__doc__ = '\n' + cpp.Gaudi.Utils.Histos.histoDump.__doc__
970 
971 # =============================================================================
972 # the actual function for text dump of the histogram
973 histoDump = __dumpHisto__
974 dumpHisto = __dumpHisto__
975 
976 for t in (cpp.AIDA.IHistogram1D, cpp.AIDA.IProfile1D, ROOT.TH1D, ROOT.TH1F,
977  ROOT.TH1, ROOT.TProfile):
978  for method in ('dump', 'dumpHisto', 'dumpAsText'):
979  if not hasattr(t, method):
980  setattr(t, method, __dumpHisto__)
981 
982 # ==============================================================================
983 
984 
985 class HistoFile:
986  """
987  Class to write histograms to a ROOT file.
988  hFile = HistoFile("myFile.root")
989  myHisto = ...
990  hFile.save(myHisto)
991  myHisto0 = ...
992  myHisto1 = ...
993  myHisto2 = ...
994  hFile.save(myHisto0, myHisto1, myHisto2)
995  histoList = [h0, h1, h2, h3]
996  hFile.save(histoList)
997  ...
998  hWriter.close()
999  """
1000  __author__ = "Juan Palacios juan.palacios@nikhef.nl"
1001 
1002  def __init__(self, fileName):
1003  self.file = ROOT.TFile(fileName, "RECREATE")
1004  from GaudiPython import gbl
1005  self.aida2root = gbl.Gaudi.Utils.Aida2ROOT.aida2root
1006  self.aidaTypes = [
1007  gbl.AIDA.IHistogram1D, gbl.AIDA.IHistogram2D,
1008  gbl.AIDA.IHistogram3D, gbl.AIDA.IProfile1D, gbl.AIDA.IProfile2D,
1009  gbl.AIDA.IHistogram
1010  ]
1011 
1012  def __convertibleType(self, histo):
1013  histoType = type(histo)
1014  for t in self.aidaTypes:
1015  if histoType == t:
1016  return True
1017  return False
1018 
1019  def save(self, *args):
1020  """
1021  This function stores histograms on the file for future saving.
1022  It takes an arbitrary number of AIDA or ROOT histograms or
1023  a list of them.
1024  """
1025  if args:
1026  if type(args[0]) == list:
1027  histoList = args[0]
1028  else:
1029  histoList = args
1030  for h in histoList:
1031  if self.__convertibleType(h):
1032  h = self.aida2root(h)
1033  h.Write()
1034 
1035  def close(self):
1036  self.file.Write()
1037  self.file.Close()
1038 
1039 
1040 # =============================================================================
1041 if '__main__' == __name__:
1042  import sys
1043  print(__doc__)
1044  for o in __all__:
1045  print(o)
1046  print(sys.modules[__name__].__dict__[o].__doc__)
1047 
1048 # =============================================================================
1049 # The END
1050 # =============================================================================
GaudiPython.HistoUtils._rmsErr_
def _rmsErr_(self)
Definition: HistoUtils.py:692
GaudiPython.HistoUtils.bookProf
def bookProf(*args, **kwargs)
Definition: HistoUtils.py:267
GaudiPython.HistoUtils.HistoFile
Definition: HistoUtils.py:985
GaudiPython.HistoUtils.HistoFile.__convertibleType
def __convertibleType(self, histo)
Definition: HistoUtils.py:1012
GaudiPython.HistoUtils.getAsAIDA
def getAsAIDA(path, **kwargs)
Definition: HistoUtils.py:347
GaudiPython.HistoUtils.HistoFile.close
def close(self)
Definition: HistoUtils.py:1035
GaudiPython.HistoUtils._nEntriesFrac_
def _nEntriesFrac_(self, i1, i2=-10000000)
Definition: HistoUtils.py:827
GaudiPython.HistoUtils._meanErr_
def _meanErr_(self)
Definition: HistoUtils.py:672
GaudiPython.HistoUtils.HistoFile.__init__
def __init__(self, fileName)
Definition: HistoUtils.py:1002
GaudiPython.HistoUtils._skewnessErr_
def _skewnessErr_(self)
Definition: HistoUtils.py:608
GaudiPython.HistoUtils._overflowEntriesFrac_
def _overflowEntriesFrac_(self)
Definition: HistoUtils.py:720
GaudiPython.HistoUtils.book
def book(*args, **kwargs)
Definition: HistoUtils.py:125
GaudiPython.HistoUtils._underflowEntriesFrac_
def _underflowEntriesFrac_(self)
Definition: HistoUtils.py:740
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:842
GaudiPython.GaudiAlgs.HID
HID
Definition: GaudiAlgs.py:92
GaudiPython.HistoUtils._overflowIntegralFrac_
def _overflowIntegralFrac_(self)
Definition: HistoUtils.py:760
GaudiPython.HistoUtils.HistoFile.save
def save(self, *args)
Definition: HistoUtils.py:1019
GaudiPython.HistoUtils.aida2root
aida2root
Definition: HistoUtils.py:492
GaudiPython.HistoUtils._sumAllBinHeightErr_
def _sumAllBinHeightErr_(self)
Definition: HistoUtils.py:712
GaudiPython.HistoUtils._kurtosis_
def _kurtosis_(self)
Definition: HistoUtils.py:623
GaudiPython.Bindings
Definition: Bindings.py:1
GaudiPython.HistoUtils._nEntries_
def _nEntries_(self, i1, i2=-10000000)
Definition: HistoUtils.py:803
GaudiPython.HistoUtils._getAppMgr
def _getAppMgr(**kwargs)
Definition: HistoUtils.py:67
GaudiPython.HistoUtils._momentErr_
def _momentErr_(self, order)
Definition: HistoUtils.py:545
GaudiPython.HistoUtils._overflowEntriesFracErr_
def _overflowEntriesFracErr_(self)
Definition: HistoUtils.py:730
GaudiPython.HistoUtils._underflowIntegralFracErr_
def _underflowIntegralFracErr_(self)
Definition: HistoUtils.py:790
GaudiPython.HistoUtils._path_
def _path_(self)
Definition: HistoUtils.py:930
GaudiPython.HistoUtils._getEvtSvc
def _getEvtSvc(**kwargs)
Definition: HistoUtils.py:108
GaudiPython.HistoUtils._underflowEntriesFracErr_
def _underflowEntriesFracErr_(self)
Definition: HistoUtils.py:750
GaudiPython.HistoUtils._centralMoment_
def _centralMoment_(self, order)
Definition: HistoUtils.py:561
GaudiPython.HistoUtils._rms_
def _rms_(self)
Definition: HistoUtils.py:682
GaudiPython.HistoUtils._to_root_
def _to_root_(self)
Definition: HistoUtils.py:498
GaudiPython.HistoUtils._kurtosisErr_
def _kurtosisErr_(self)
Definition: HistoUtils.py:638
GaudiPython.HistoUtils._getHistoSvc
def _getHistoSvc(**kwargs)
Definition: HistoUtils.py:91
GaudiPython.HistoUtils._nEntriesFracErr_
def _nEntriesFracErr_(self, i1, i2=-10000000)
Definition: HistoUtils.py:851
GaudiPython.HistoUtils.HistoFile.aida2root
aida2root
Definition: HistoUtils.py:1005
GaudiPython.HistoUtils._skewness_
def _skewness_(self)
Definition: HistoUtils.py:593
gaudirun.type
type
Definition: gaudirun.py:154
GaudiPython.HistoUtils.fill
def fill(histo, data, fun=lambda x:x, cut=lambda x:True, **kwargs)
Definition: HistoUtils.py:394
GaudiPython.HistoUtils._moment_
def _moment_(self, order, value=0)
Definition: HistoUtils.py:529
Write
Definition: Write.py:1
GaudiPython.HistoUtils._overflowIntegralFracErr_
def _overflowIntegralFracErr_(self)
Definition: HistoUtils.py:770
GaudiPython.HistoUtils.HistoFile.file
file
Definition: HistoUtils.py:1003
GaudiPython.HistoUtils._sumBinHeightErr_
def _sumBinHeightErr_(self)
Definition: HistoUtils.py:702
GaudiPython.HistoUtils._underflowIntegralFrac_
def _underflowIntegralFrac_(self)
Definition: HistoUtils.py:780
GaudiPython.HistoUtils._centralMomentErr_
def _centralMomentErr_(self, order)
Definition: HistoUtils.py:577
GaudiPython.HistoUtils._mean_
def _mean_(self)
Definition: HistoUtils.py:662
GaudiPython.HistoUtils.HistoFile.aidaTypes
aidaTypes
Definition: HistoUtils.py:1006
GaudiPython.HistoUtils.__dumpHisto__
def __dumpHisto__(histo, *args)
Definition: HistoUtils.py:951
GaudiPython.HistoUtils._nEff_
def _nEff_(self)
Definition: HistoUtils.py:652
GaudiPython.HistoUtils.getAsROOT
def getAsROOT(path, **kwargs)
Definition: HistoUtils.py:372