7 from __future__
import print_function
9 __doc__ =
"""Test read and write histograms and trees via ITHistSvc"""
10 __version__ =
"$Revision: 1.4 $"
11 __author__ =
"Sebastien Binet <binet@cern.ch>"
13 from AthenaPython
import PyAthena
14 StatusCode = PyAthena.StatusCode
17 """Simple algorithm to read data from Root hist. svc
22 super(PyHistReader,self).
__init__(**kw)
30 _info(
"==> initialize...")
31 self.
hsvc = PyAthena.py_svc(
'THistSvc/THistSvc')
33 self.
msg.
error(
'Could not retrieve THistSvc')
34 return StatusCode.Failure
36 def print_properties(h):
37 _info(
' -%-20s: %i <mean>=%8.3f rms=%8.3f',
38 h.GetName(), h.GetEntries(), h.GetMean(), h.GetRMS())
41 o = self.
hsvc.
load(
'/read1/xxx/gauss1d', oid_type=
'hist')
44 o = self.
hsvc.
load(
'/read2/gauss2d', oid_type=
'hist')
47 o = self.
hsvc.
load(
'/read2/gauss3d', oid_type=
'hist')
50 o = self.
hsvc.
load(
'/read2/profile', oid_type=
'hist')
56 o = self.
hsvc.
load(
'/read2/trees/stuff/tree1', oid_type=
'tree')
57 _info(
' -%-20s: %i', o.GetName(), o.GetEntries())
58 except KeyError
as err:
60 self.
msg.
error(
'bug #36379 still not fixed...')
61 return StatusCode.Success
65 _info(
'==> execute...')
66 return StatusCode.Success
69 self.
msg.
info(
'==> finalize...')
70 return StatusCode.Success
74 from math
import sin
as math_sin
75 from random
import gauss
76 from array
import array
78 """A simple python algorithm to write stuff into a ROOT file via the
79 @c ITHistSvc (translated example from GaudiExamples/src/THist)
84 super(PyHistWriter,self).
__init__(**kw)
92 _info(
'==> initialize...')
93 self.
hsvc = PyAthena.py_svc(
'THistSvc/THistSvc')
95 self.
msg.
error(
'could not retrieve THistSvc/THistSvc')
96 return StatusCode.Failure
98 from ROOT
import TH1F, TH2F, TH3F, TProfile, TTree
100 def th1(n,t):
return TH1F(n,t,100,0.,100.)
101 def th2(n,t):
return TH2F(n,t,100,-50.,50.,100,-50.,50.)
102 def th3(n,t):
return TH3F(n,t,100,-50.,50.,100,-50.,50.,100,-50.,50.)
103 def tp (n,t):
return TProfile(n,t,100,-50.,50.)
106 self.
hsvc[
'/temp/h1'] = th1(
'h1',
'Temporary hist 1')
107 self.
hsvc[
'/temp/other/h1a'] = th1(
'h1a',
'Temporary hist 1a')
110 self.
hsvc[
'/new/hists/h1'] = th1(
'h1',
'Persistent hist 1')
113 self.
hsvc[
'/upd/xxx/gauss1d'] =
TH1F(
'gauss1d',
'1D gaussian',
117 self.
hsvc[
'/rec/gauss2d'] = th2(
'gauss2d',
'2D gaussian')
118 self.
hsvc[
'/rec/gauss3d'] = th3(
'gauss3d',
'3D gaussian')
119 self.
hsvc[
'/rec/prof'] = tp (
'profile',
'profile')
122 self.
hsvc[
'/rec/trees/stuff/tree1'] = TTree(
'tree1',
'tree title')
125 return StatusCode.Success
128 _info = self.
msg.info
129 _info(
'==> execute...')
132 x = math_sin(
float(n)) * 52. + 50.
134 hsvc[
'/temp/h1'].Fill(x)
135 for _
in range(2): hsvc[
'/temp/other/h1a'].Fill(x)
136 for _
in range(3): hsvc[
'/new/hists/h1'].Fill(x)
137 _fill = hsvc[
'/upd/xxx/gauss1d'].Fill
138 for _
in range(1000): _fill(
gauss(mu=0.,sigma=15.),1.)
139 _fill = hsvc[
'/rec/gauss2d'].Fill
140 for _
in range(1000): _fill(
gauss(mu=0.,sigma=15.),
141 gauss(mu=0.,sigma=15.), 1.)
142 _fill = hsvc[
'/rec/gauss3d'].Fill
143 for _
in range(1000): _fill(
gauss(mu=0.,sigma=15.),
144 gauss(mu=0.,sigma=15.),
145 gauss(mu=0.,sigma=15.), 1.)
147 tr = hsvc[
'/rec/trees/stuff/tree1']
152 tr.Branch(
'branch1', p1,
'point1/I')
153 tr.Branch(
'branch2', p2,
'point2/I')
154 tr.Branch(
'branch3', p3,
'point3/I')
156 for i
in range(1000):
161 return StatusCode.Success
164 _info = self.
msg.info
165 _info(
'==> finalize...')
166 for n
in (
'/temp/h1',
'/temp/other/h1a',
169 _info(
' - histo [%r]', n)
171 _info(
' entries = %i', h.GetEntries())
172 _info(
' mean = %8.3f', h.GetMean())
173 _info(
' rms = %8.3f', h.GetRMS())
175 return StatusCode.Success