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