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