9 This module contains set of simple and useful utilities for booking and 10 manipulations with Gaudi-AIDA histograms, inspired by Thomas' request 12 The module contains following public symbols: 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) 27 __author__ =
"Vanya BELYAEV ibelyaev@physics.syr.edu" 58 Helper private auxiliary function to get Application Manager 60 gaudi = kwargs.get(
'gaudi',
None)
64 raise RuntimeError,
'Unable to get valid ApplicationMgr' 66 state = gaudi._isvc.FSMState()
67 if state < cpp.Gaudi.StateMachine.CONFIGURED:
69 state = gaudi._isvc.FSMState()
70 if state < cpp.Gaudi.StateMachine.INITIALIZED:
81 Helper private auxiliary function to get iHistogramSvs 83 svc = kwargs.get(
'service',
None)
85 svc = kwargs.get(
'svc',
None)
89 return gaudi.histsvc()
97 Helper private auxiliary function to get iDataSvs 99 svc = kwargs.get(
'service',
None)
101 svc = kwargs.get(
'svc',
None)
105 return gaudi.evtsvc()
113 The trivial function to book the various 1D,2D&3D-histograms 115 (1) book the trivial 1D histogram with full path 117 >>> h1D = book ( 'path/to/my/histo' , ## path in Histogram Transient Store 118 'cosine of decay angle ' , ## histogram title 119 100 , ## number of bins 123 (2) book the trivial 1D histogram with directory path and string ID : 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 132 (3) book the trivial 1D histogram with directory path and integer ID : 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 141 (4) book the trivial 2D histogram with full path 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 148 50 , ## number of Y-bins 152 (5) book the trivial 2D histogram with directory path and literal ID 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 160 50 , ## number of Y-bins 164 (6) book the trivial 2D histogram with directory path and integer ID 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 172 50 , ## number of Y-bins 176 (7) book the trivial 3D histogram with full path 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 183 10 , ## number of Y-bins 186 10 , ## number of Z-bins 190 (8) book the trivial 3D histogram with directory path and literal ID 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 198 10 , ## number of Y-bins 201 10 , ## number of Z-bins 205 (9) book the trivial 3D histogram with directory path and integer ID 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 213 10 , ## number of Y-bins 216 10 , ## number of Z-bins 220 Many other booking methods are available, 221 e.g. for the histograms with non-equidistant bins, see IHistogamSvc::book 224 if useROOT
or kwargs.get(
'useROOT',
False)
or not kwargs.get(
'useAIDA',
True):
225 from ROOT
import TH1D
229 if not str
is type(a1):
232 return TH1D(a0 + a1, a2, *args[3:])
234 return TH1D(a0, a1, *args[2:])
238 raise RuntimeError,
'Unable to get valid HistogramService ' 240 return svc.book(*args)
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__
255 The trivial function to book 1D&2D profile histograms: 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 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 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 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 286 50 , ## number of Y-bins 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 297 50 , ## number of Y-bins 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 308 50 , ## number of Y-bins 312 Many other booking methods are available, 313 e.g. for the histograms with non-equidistant bins, see IHistogamSvc::book 318 raise RuntimeError,
'Unable to get valid HistogramService ' 320 return svc.bookProf(*args)
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__
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) 338 >>> h = getAsAIDA ( 'some/path/to/my/histogram' ) 343 raise RuntimeError,
'Unable to get valid HistogramService ' 345 return svc.getAsAIDA(path)
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__
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) 363 >>> h = getAsROOT ( 'some/path/to/my/histogram' ) 368 raise RuntimeError,
'Unable to get valid HistogramService ' 370 return svc.getAsROOT(path)
373 getAsROOT.__doc__ +=
'\n\n' +
'\thelp(iHistogramSvc.getAsROOT) : \n\n' \
374 + iHistogramSvc.getAsROOT . __doc__
386 The function which allows 'the smart fill' of 1D-histogram 390 The most trivial case, fill with the value 391 >>> fill ( histo , 1.0 ) 393 Fill from any iterable object (sequence) 394 >>> fill ( histo , [1,,2,3,4,5,10] ) 396 Fill from iterable object and apply the function: 397 >>> fill ( histo , [1,2,3,4,5] , math.sin ) 400 >>> fill ( histo , [1,2,3,4,5] , lambda x : x*x ) 403 >>> fill ( histo , [1,2,3,4,5] , fun = lambda x : x*x ) 405 Use internal attributes: 406 >>> tracks = evtSvc['Rec/Track/Best'] ## iterable container of tracks 407 >>> fill ( histo , tracks , lambda t : t.pt() ) 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 ) 412 The same (omit the trivial function) : 413 >>> fill ( histo , [1,2,3,4,5,6,7] , cut = lambda y : y%2 ) 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() ) 420 >>> fill ( histo , tracks , 421 fun = lambda t : t.pt() , 422 cut = lambda t : 0<t.charge() ) 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 ) 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, 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 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. 447 if type(data) == str:
450 return fill(histo, data, fun, cut, **kwargs)
454 fun = eval(fun, globals())
458 cut = eval(cut, globals())
460 if not hasattr(data,
'__iter__'):
463 if not hasattr(histo,
'fill')
and hasattr(histo,
'Fill'):
464 setattr(histo,
'fill', getattr(histo,
'Fill'))
469 histo.fill(
fun(item))
476 aida2root = cpp.Gaudi.Utils.Aida2ROOT.aida2root
485 >>> aida = ... ## get AIDA histogram 486 >>> root = aida.toROOT() ## convert it to ROOT 492 _to_root_ . __doc__ += aida2root . __doc__
494 for t
in (cpp.AIDA.IHistogram3D,
495 cpp.AIDA.IHistogram2D,
496 cpp.AIDA.IHistogram1D,
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',
504 if not hasattr(t, attr):
505 setattr(t, attr, _to_root_)
507 cpp.AIDA.IHistogram3D. __repr__ =
lambda s: cpp.GaudiAlg.Print3D.toString(
509 cpp.AIDA.IHistogram3D. __str__ = cpp.AIDA.IHistogram3D. __repr__
512 HistoStats = cpp.Gaudi.Utils.HistoStats
520 Evaluate 'bin-by-bin' momentum of order 'order' around the value 'value' 524 >>> print h1.moment ( 5 ) 527 return HistoStats.moment(self, order, value)
535 Evaluate error for 'bin-by-bin' momentum of order 'order' around the value 'value' 539 >>> print h1.momentErr ( 5 ) 542 return HistoStats.momentErr(self, order)
549 Evaluate 'bin-by-bin' central momentum (around mean value) 553 >>> print h1.centralMoment ( 5 ) 556 return HistoStats.centralMoment(self, order)
564 Evaluate error for 'bin-by-bin' central momentum (around mean value) 568 >>> print h1.centralMomentErr ( 5 ) 571 return HistoStats.centralMomentErr(self, order)
579 Evaluate 'bin-by-bin' skewness for 1D AIDA histogram 582 >>> print h1.skewness() 585 return HistoStats.skewness(self)
593 Evaluate error for 'bin-by-bin' skewness 596 >>> print h1.skewnessErr() 599 return HistoStats.skewnessErr(self)
607 Evaluate 'bin-by-bin' kurtosis 610 >>> print h1.kurtosis () 613 return HistoStats.kurtosis(self)
621 Evaluate error for 'bin-by-bin' kurtotis for 1D AIDA histogram 624 >>> print h1.kurtotisErr() 627 return HistoStats.kurtosisErr(self)
634 Number of equivalent entries 636 return HistoStats.nEff(self)
642 Evaluate the MEAN value 644 return HistoStats.mean(self)
650 Evaluate the error for MEAN estimate 652 return HistoStats.meanErr(self)
659 Evaluate the RMS for AIDA histogram 661 return HistoStats.rms(self)
667 Evaluate the error for RMS estimate 669 return HistoStats.rmsErr(self)
676 Get an error in the sum bin height ('in-range integral') 678 return HistoStats.sumBinHeightErr(self)
684 """ Get an error in the sum bin height ('in-range integral') """ 685 return HistoStats.sumAllBinHeightErr(self)
692 The fraction of overflow entries (useful for shape comparison) 694 return HistoStats.overflowEntriesFrac(self)
700 The error for fraction of overflow entries (useful for shape comparison) 702 return HistoStats.overflowEntriesFracErr(self)
708 The fraction of underflow entries (useful for shape comparison) 710 return HistoStats.underflowEntriesFrac(self)
716 The error for fraction of underflow entries (useful for shape comparison) 718 return HistoStats.underflowEntriesFracErr(self)
725 The fraction of overflow integral (useful for shape comparison) 727 return HistoStats.overflowIntegralFrac(self)
733 The error for fraction of overflow integral (useful for shape comparison) 735 return HistoStats.overflowIntegralFracErr(self)
741 The fraction of underflow integral (useful for shape comparison) 743 return HistoStats.underflowIntegralFrac(self)
749 The error for fraction of underflow integral (useful for shape comparison) 751 return HistoStats.underflowIntegralFracErr(self)
761 Get number of entries in histogram up to the certain bin (not-included) 763 attention: underflow bin is included! 766 >>> print h1.nEntries ( 10 ) 768 Get number of entries in histogram form the certain 769 minimal bin up to the certain maximal bin (not-included) 772 >>> print h1.nEntries ( 10 , 15 ) 775 if i2 < i1
or i2 < 0:
776 return HistoStats.nEntries(self, i1)
777 return HistoStats.nEntries(self, i1, i2)
783 Get the fraction of entries in histogram up to the certain bin (not-included) 785 attention: underflow bin is included! 788 >>> print h1.nEntriesFrac ( 10 ) 790 Get the fraction of entries in histogram form the certain 791 minimal bin up to the certain maximal bin (not-included) 794 >>> print h1.nEntriesFrac ( 10 , 15 ) 797 if i2 < i1
or i2 < 0:
798 return HistoStats.nEntriesFrac(self, i1)
799 return HistoStats.nEntriesFrac(self, i1, i2)
805 Get error for fraction of entries in histogram up to the certain bin (not-included) 807 attention: underflow bin is included! 810 >>> print h1.nEntriesFracErr( 10 ) 812 Get error fraction of entries in histogram form the certain 813 minimal bin up to the certain maximal bin (not-included) 816 >>> print h1.nEntriesFracErr ( 10 , 15 ) 819 if i2 < i1
or i2 < 0:
820 return HistoStats.nEntriesFracErr(self, i1)
821 return HistoStats.nEntriesFracErr(self, i1, i2)
825 i1DH = cpp.AIDA.IHistogram1D
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'):
837 if not hasattr(i1DH,
'mean'):
839 if not hasattr(i1DH,
'meanErr'):
840 i1DH.meanErr = _meanErr_
841 if not hasattr(i1DH,
'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_
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_
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_
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_
884 Get the path in THS for the given AIDA object: 887 >>> print aida.path() 890 return cpp.Gaudi.Utils.Histos.path(self)
893 iBH = cpp.AIDA.IBaseHistogram
894 if not hasattr(iBH,
'path'):
896 if not hasattr(iBH,
'TESpath'):
898 if not hasattr(iBH,
'location'):
899 iBH.location = _path_
906 Dump the histogram/profile in text format (a'la HBOOK) 909 >>> print dumpHisto ( histo ) 911 >>> print histo.dump() 912 >>> print histo.dump( 20 , 20 ) 913 >>> print histo.dump( 20 , 20 , True ) 918 return cpp.Gaudi.Utils.Histos.histoDump(histo, *args)
921 __dumpHisto__ .__doc__ =
'\n' + cpp.Gaudi.Utils.Histos.histoDump . __doc__
925 histoDump = __dumpHisto__
926 dumpHisto = __dumpHisto__
928 for t
in (cpp.AIDA.IHistogram1D,
934 for method
in (
'dump',
937 if not hasattr(t, method):
938 setattr(t, method, __dumpHisto__)
945 Class to write histograms to a ROOT file. 946 hFile = HistoFile("myFile.root") 952 hFile.save(myHisto0, myHisto1, myHisto2) 953 histoList = [h0, h1, h2, h3] 954 hFile.save(histoList) 958 __author__ =
"Juan Palacios juan.palacios@nikhef.nl" 961 self.
file = ROOT.TFile(fileName,
"RECREATE")
962 from GaudiPython
import gbl
965 gbl.AIDA.IHistogram2D,
966 gbl.AIDA.IHistogram3D,
972 histoType =
type(histo)
980 This function stores histograms on the file for future saving. 981 It takes an arbitrary number of AIDA or ROOT histograms or 985 if type(args[0]) == list:
1000 if '__main__' == __name__:
1005 print sys.modules[__name__].__dict__[o].__doc__
def _underflowIntegralFrac_(self)
def _nEntriesFracErr_(self, i1, i2=-10000000)
def getAsAIDA(path, kwargs)
def _sumBinHeightErr_(self)
def __init__(self, fileName)
def _sumAllBinHeightErr_(self)
def _overflowIntegralFracErr_(self)
def getAsROOT(path, kwargs)
def _overflowEntriesFrac_(self)
def _moment_(self, order, value=0)
def _nEntriesFrac_(self, i1, i2=-10000000)
def _underflowEntriesFrac_(self)
def _overflowIntegralFrac_(self)
def _overflowEntriesFracErr_(self)
def bookProf(args, kwargs)
def _underflowEntriesFracErr_(self)
def _centralMomentErr_(self, order)
double fun(const std::vector< double > &x)
def fill(histo, data, fun=lambda x:x, cut=lambda x:True, kwargs)
def _nEntries_(self, i1, i2=-10000000)
def _momentErr_(self, order)
def _centralMoment_(self, order)
def _underflowIntegralFracErr_(self)
def __dumpHisto__(histo, args)
def __convertibleType(self, histo)