Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
HistoUtilsEx.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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 # =============================================================================
26 __author__ = "Vanya BELYAEV Ivan.Belyaev@nikhef.nl"
27 # =============================================================================
28 import sys
29 from math import cos, sin
30 
31 from GaudiPython.Bindings import AppMgr
32 from GaudiPython.Bindings import gbl as cpp
33 from GaudiPython.HistoUtils import aida2root, book, fill
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(
72  "path/to/my/histos/MyHisto", "the title", 100, -3, 3
73 ) # nBins, low&high edges
74 
75 histo2 = book(
76  "path/to/my/histos", "ID of 2nd histo", "the title of 2nd histo", 100, -3, 3
77 ) # nBins, low&high edges
78 
79 # fill the histos (using native AIDA 'fill' method
80 for i in range(0, 10000):
81  histo1.fill(gauss())
82  histo2.fill(gauss())
83 
84 # print them:
85 print(" Histo1: ", histo1)
86 print(" Histo2: ", histo2)
87 sys.stdout.flush()
88 
89 # convert to ROOT:
90 rhisto1 = aida2root(histo1)
91 rhisto2 = aida2root(histo2)
92 
93 # print them as ROOT objects
94 rhisto1.Print()
95 rhisto2.Print()
96 
97 # power fill through AIDA interface:
98 fill(histo1, range(0, 5000), sin)
99 fill(histo2, range(0, 5000), cos)
100 
101 # power fill through ROOT interface:
102 fill(rhisto1, range(0, 10000), sin)
103 fill(rhisto2, range(0, 10000), cos)
104 
105 # print again them as ROOT objects
106 rhisto1.Print()
107 rhisto2.Print()
108 
109 # power fill through AIDA interface:
110 fill(histo1, range(0, 5000), sin, lambda x: x % 2 == 0)
111 fill(histo2, range(0, 5000), cos, lambda x: x % 2 != 0)
112 
113 # print again them as ROOT objects
114 rhisto1.Print()
115 rhisto2.Print()
116 cpp_flush()
117 
118 # get some "extra infomration"
119 print(" Histo1 : mean /err: %10f +- %10f " % (histo1.mean(), histo1.meanErr()))
120 print(" Histo1 : rms /err: %10f +- %10f " % (histo1.rms(), histo1.rmsErr()))
121 print(
122  " Histo1 : skewness/err: %10f +- %10f "
123  % (histo1.skewness(), histo1.skewnessErr())
124 )
125 print(
126  " Histo1 : kurtosis/err: %10f +- %10f "
127  % (histo1.kurtosis(), histo1.kurtosisErr())
128 )
129 print(' Histo1 : path in THS : "%s"' % histo1.path())
130 
131 print(" Histo2 : mean /err: %10f +- %10f " % (histo2.mean(), histo2.meanErr()))
132 print(" Histo2 : rms /err: %10f +- %10f " % (histo2.rms(), histo2.rmsErr()))
133 print(
134  " Histo2 : skewness/err: %10f +- %10f "
135  % (histo2.skewness(), histo2.skewnessErr())
136 )
137 print(
138  " Histo2 : kurtosis/err: %10f +- %10f "
139  % (histo2.kurtosis(), histo2.kurtosisErr())
140 )
141 print(' Histo2 : path in THS : "%s"' % histo2.path())
142 sys.stdout.flush()
143 
144 # =============================================================================
145 # The END
146 # =============================================================================
GaudiPython.HistoUtils
Definition: HistoUtils.py:1
HistoUtilsEx.cpp_flush
def cpp_flush()
Definition: HistoUtilsEx.py:38
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:869
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:74
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:102