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