The Gaudi Framework  v36r1 (3e2fb5a8)
HistoUtilsEx.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
19 """
20 Simple example to illustrate the usage functions from HistoUtils module
21 (Gaudi histograms outside of algorithm-scope in 'script-like' environment)
22 """
23 from __future__ import print_function
24 # =============================================================================
25 __author__ = "Vanya BELYAEV Ivan.Belyaev@nikhef.nl"
26 # =============================================================================
27 import sys
28 from math import sin, cos
29 
30 from GaudiPython.Bindings import AppMgr
31 from GaudiPython.Bindings import gbl as cpp
32 
33 from GaudiPython.HistoUtils import (book, fill, aida2root)
34 
35 print(__doc__)
36 
37 
38 def cpp_flush():
39  cpp.gInterpreter.ProcessLine('cout.flush();')
40 
41 
42 # get the application manager (create if needed)
43 gaudi = AppMgr()
44 
45 # no external input
46 gaudi.EvtSel = 'NONE'
47 
48 # define the histogram persistency
49 gaudi.HistogramPersistency = "ROOT"
50 
51 # define the name of the output file with histograms:
52 hsvc = gaudi.service('HistogramPersistencySvc')
53 hsvc.OutputFile = "HistoUtilsEx.root"
54 
55 
56 gaudi.config()
57 gaudi.initialize()
58 
59 # get some random numbers
60 Rndm = cpp.Rndm
61 IRndmGenSvc = cpp.IRndmGenSvc
62 rndmSvc = gaudi.service('RndmGenSvc', IRndmGenSvc)
63 if not rndmSvc:
64  gaudi.createSvc('RndmGenSvc')
65 rndmSvc = gaudi.service('RndmGenSvc', IRndmGenSvc)
66 gauss = Rndm.Numbers(cpp.SmartIF("IRndmGenSvc")(rndmSvc), Rndm.Gauss(0.0, 1.0))
67 cpp_flush()
68 
69 # book some histograms
70 
71 histo1 = book('path/to/my/histos/MyHisto', 'the title', 100, -3,
72  3) # nBins, low&high edges
73 
74 histo2 = book('path/to/my/histos', 'ID of 2nd histo', 'the title of 2nd histo',
75  100, -3, 3) # nBins, low&high edges
76 
77 # fill the histos (using native AIDA 'fill' method
78 for i in range(0, 10000):
79  histo1.fill(gauss())
80  histo2.fill(gauss())
81 
82 # print them:
83 print(' Histo1: ', histo1)
84 print(' Histo2: ', histo2)
85 sys.stdout.flush()
86 
87 # convert to ROOT:
88 rhisto1 = aida2root(histo1)
89 rhisto2 = aida2root(histo2)
90 
91 # print them as ROOT objects
92 rhisto1.Print()
93 rhisto2.Print()
94 
95 # power fill through AIDA interface:
96 fill(histo1, range(0, 5000), sin)
97 fill(histo2, range(0, 5000), cos)
98 
99 # power fill through ROOT interface:
100 fill(rhisto1, range(0, 10000), sin)
101 fill(rhisto2, range(0, 10000), cos)
102 
103 # print again them as ROOT objects
104 rhisto1.Print()
105 rhisto2.Print()
106 
107 # power fill through AIDA interface:
108 fill(histo1, range(0, 5000), sin, lambda x: x % 2 == 0)
109 fill(histo2, range(0, 5000), cos, lambda x: x % 2 != 0)
110 
111 # print again them as ROOT objects
112 rhisto1.Print()
113 rhisto2.Print()
114 cpp_flush()
115 
116 # get some "extra infomration"
117 print(' Histo1 : mean /err: %10f +- %10f ' % (histo1.mean(),
118  histo1.meanErr()))
119 print(' Histo1 : rms /err: %10f +- %10f ' % (histo1.rms(),
120  histo1.rmsErr()))
121 print(' Histo1 : skewness/err: %10f +- %10f ' % (histo1.skewness(),
122  histo1.skewnessErr()))
123 print(' Histo1 : kurtosis/err: %10f +- %10f ' % (histo1.kurtosis(),
124  histo1.kurtosisErr()))
125 print(' Histo1 : path in THS : "%s"' % histo1.path())
126 
127 print(' Histo2 : mean /err: %10f +- %10f ' % (histo2.mean(),
128  histo2.meanErr()))
129 print(' Histo2 : rms /err: %10f +- %10f ' % (histo2.rms(),
130  histo2.rmsErr()))
131 print(' Histo2 : skewness/err: %10f +- %10f ' % (histo2.skewness(),
132  histo2.skewnessErr()))
133 print(' Histo2 : kurtosis/err: %10f +- %10f ' % (histo2.kurtosis(),
134  histo2.kurtosisErr()))
135 print(' Histo2 : path in THS : "%s"' % histo2.path())
136 sys.stdout.flush()
137 
138 # =============================================================================
139 # The END
140 # =============================================================================
GaudiPython.HistoUtils
Definition: HistoUtils.py:1
HistoUtilsEx.cpp_flush
def cpp_flush()
Definition: HistoUtilsEx.py:38
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:842
GaudiPython.Bindings
Definition: Bindings.py:1
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
Gaudi::Histos::book
GAUDI_API AIDA::IHistogram1D * book(IHistogramSvc *svc, const std::string &path, const Gaudi::Histo1DDef &hist)
helper function to book 1D-histogram
Definition: HistoDef.cpp:97
HistoUtilsEx.gauss
gauss
Definition: HistoUtilsEx.py:66
GaudiMP.GMPBase.aida2root
aida2root
Definition: GMPBase.py:67
Gaudi::Utils::Histos::fill
GAUDI_API void fill(AIDA::IHistogram1D *histo, const double value, const double weight=1.0)
simple function to fill AIDA::IHistogram1D objects
Definition: Fill.cpp:45
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:97