ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaPython
python
tests
PyTHistTestsLib.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2
3
# @file AthenaPython/python/tests/PyTHistTestsLib.py
4
# @purpose Test read and write histograms and trees via the @c ITHistSvc
5
# @author Sebastien Binet <binet@cern.ch>
6
7
__doc__ =
"""Test read and write histograms and trees via ITHistSvc"""
8
__author__ =
"Sebastien Binet <binet@cern.ch>"
9
10
from
AthenaPython
import
PyAthena
11
StatusCode = PyAthena.StatusCode
12
13
class
PyHistReader
(
PyAthena.Alg
):
14
"""Simple algorithm to read data from Root hist. svc
15
"""
16
def
__init__
(self, name='PyHistReader', **kw):
17
# init base class
18
kw[
'name'
] = name
19
super(PyHistReader,self).
__init__
(**kw)
20
21
# handle to the root hist svc
22
self.
hsvc
=
None
23
return
24
25
def
initialize
(self):
26
_info = self.
msg
.info
27
_info(
"==> initialize..."
)
28
self.
hsvc
= PyAthena.py_svc(
'THistSvc/THistSvc'
)
29
if
not
self.
hsvc
:
30
self.
msg
.
error
(
'Could not retrieve THistSvc'
)
31
return
StatusCode.Failure
32
33
def
print_properties(h):
34
_info(
' -%-20s: %i <mean>=%8.3f rms=%8.3f'
,
35
h.GetName(), h.GetEntries(), h.GetMean(), h.GetRMS())
36
37
# load histos from stream and print some of their properties
38
o = self.
hsvc
.load(
'/read1/xxx/gauss1d'
, oid_type=
'hist'
)
39
print_properties(o)
40
41
o = self.
hsvc
.load(
'/read2/gauss2d'
, oid_type=
'hist'
)
42
print_properties(o)
43
44
o = self.
hsvc
.load(
'/read2/gauss3d'
, oid_type=
'hist'
)
45
print_properties(o)
46
47
o = self.
hsvc
.load(
'/read2/profile'
, oid_type=
'hist'
)
48
print_properties(o)
49
50
o = self.
hsvc
.load(
'/read2/trees/stuff/tree1'
, oid_type=
'tree'
)
51
_info(
' -%-20s: %i'
, o.GetName(), o.GetEntries())
52
53
return
StatusCode.Success
54
55
def
execute
(self):
56
_info = self.
msg
.info
57
_info(
'==> execute...'
)
58
return
StatusCode.Success
59
60
def
finalize
(self):
61
self.
msg
.info(
'==> finalize...'
)
62
return
StatusCode.Success
63
64
pass
# class PyHistReader
65
66
from
math
import
sin
as
math_sin
67
from
random
import
gauss
68
from
array
import
array
69
class
PyHistWriter
(
PyAthena.Alg
):
70
"""A simple python algorithm to write stuff into a ROOT file via the
71
@c ITHistSvc (translated example from GaudiExamples/src/THist)
72
"""
73
def
__init__
(self, name='PyHistWriter', **kw):
74
# init base class
75
kw[
'name'
]=name
76
super(PyHistWriter,self).
__init__
(**kw)
77
78
# handle to the root hist svc
79
self.
hsvc
=
None
80
return
81
82
def
initialize
(self):
83
_info = self.
msg
.info
84
_info(
'==> initialize...'
)
85
self.
hsvc
= PyAthena.py_svc(
'THistSvc/THistSvc'
)
86
if
not
self.
hsvc
:
87
self.
msg
.
error
(
'could not retrieve THistSvc/THistSvc'
)
88
return
StatusCode.Failure
89
90
from
ROOT
import
TH1F, TH2F, TH3F, TProfile, TTree
91
# helpers
92
def
th1(n,t):
return
TH1F(n,t,100,0.,100.)
93
def
th2(n,t):
return
TH2F(n,t,100,-50.,50.,100,-50.,50.)
94
def
th3(n,t):
return
TH3F(n,t,100,-50.,50.,100,-50.,50.,100,-50.,50.)
95
def
tp (n,t):
return
TProfile(n,t,100,-50.,50.)
96
97
# temporary trees
98
self.
hsvc
[
'/temp/h1'
] = th1(
'h1'
,
'Temporary hist 1'
)
99
self.
hsvc
[
'/temp/other/h1a'
] = th1(
'h1a'
,
'Temporary hist 1a'
)
100
101
# update to stream 'upd', dir '/xxx'
102
self.
hsvc
[
'/upd/xxx/gauss1d'
] = TH1F(
'gauss1d'
,
'1D gaussian'
,
103
100,-50.,50.)
104
105
# recreate stuff in '/'
106
self.
hsvc
[
'/rec/gauss2d'
] = th2(
'gauss2d'
,
'2D gaussian'
)
107
self.
hsvc
[
'/rec/gauss3d'
] = th3(
'gauss3d'
,
'3D gaussian'
)
108
self.
hsvc
[
'/rec/prof'
] = tp (
'profile'
,
'profile'
)
109
110
# tree with branches in '/trees/stuff'
111
self.
hsvc
[
'/rec/trees/stuff/tree1'
] = TTree(
'tree1'
,
'tree title'
)
112
113
self.
_n
= -1
114
return
StatusCode.Success
115
116
def
execute
(self):
117
_info = self.
msg
.info
118
_info(
'==> execute...'
)
119
self.
_n
+= 1
120
n = self.
_n
121
x = math_sin(float(n)) * 52. + 50.
122
hsvc = self.
hsvc
123
hsvc[
'/temp/h1'
].Fill(x)
124
for
_
in
range(2): hsvc[
'/temp/other/h1a'
].Fill(x)
125
_fill = hsvc[
'/upd/xxx/gauss1d'
].Fill
126
for
_
in
range(1000): _fill(gauss(mu=0.,sigma=15.),1.)
127
_fill = hsvc[
'/rec/gauss2d'
].Fill
128
for
_
in
range(1000): _fill(gauss(mu=0.,sigma=15.),
129
gauss(mu=0.,sigma=15.), 1.)
130
_fill = hsvc[
'/rec/gauss3d'
].Fill
131
for
_
in
range(1000): _fill(gauss(mu=0.,sigma=15.),
132
gauss(mu=0.,sigma=15.),
133
gauss(mu=0.,sigma=15.), 1.)
134
135
tr = hsvc[
'/rec/trees/stuff/tree1'
]
136
if
n == 0:
137
p1 =
array
(
'i'
,[0])
138
p2 =
array
(
'i'
,[0])
139
p3 =
array
(
'i'
,[0])
140
tr.Branch(
'branch1'
, p1,
'point1/I'
)
141
tr.Branch(
'branch2'
, p2,
'point2/I'
)
142
tr.Branch(
'branch3'
, p3,
'point3/I'
)
143
_tr_fill = tr.Fill
144
for
i
in
range(1000):
145
p1[0] = i
146
p2[0] = i%10
147
p3[0] = i%7
148
_tr_fill()
149
return
StatusCode.Success
150
151
def
finalize
(self):
152
_info = self.
msg
.info
153
_info(
'==> finalize...'
)
154
for
n
in
(
'/temp/h1'
,
'/temp/other/h1a'
):
155
_info(
'='
*20)
156
_info(
' - histo [%r]'
, n)
157
h = self.
hsvc
[n]
158
_info(
' entries = %i'
, h.GetEntries())
159
_info(
' mean = %8.3f'
, h.GetMean())
160
_info(
' rms = %8.3f'
, h.GetRMS())
161
_info(
'='
*20)
162
return
StatusCode.Success
163
164
pass
# class PyHistWriter
AthCommonMsg< Gaudi::Algorithm >::msg
MsgStream & msg() const
Definition
AthCommonMsg.h:24
PyAthena::Alg
Definition
PyAthenaAlg.h:33
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition
PyAthenaAlg.cxx:86
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition
PyAthenaAlg.cxx:60
python.tests.PyTHistTestsLib.PyHistReader
Definition
PyTHistTestsLib.py:13
python.tests.PyTHistTestsLib.PyHistReader.hsvc
hsvc
Definition
PyTHistTestsLib.py:22
python.tests.PyTHistTestsLib.PyHistReader.execute
execute(self)
Definition
PyTHistTestsLib.py:55
python.tests.PyTHistTestsLib.PyHistReader.__init__
__init__(self, name='PyHistReader', **kw)
Definition
PyTHistTestsLib.py:16
python.tests.PyTHistTestsLib.PyHistWriter
Definition
PyTHistTestsLib.py:69
python.tests.PyTHistTestsLib.PyHistWriter.hsvc
hsvc
Definition
PyTHistTestsLib.py:79
python.tests.PyTHistTestsLib.PyHistWriter.__init__
__init__(self, name='PyHistWriter', **kw)
Definition
PyTHistTestsLib.py:73
python.tests.PyTHistTestsLib.PyHistWriter.execute
execute(self)
Definition
PyTHistTestsLib.py:116
python.tests.PyTHistTestsLib.PyHistWriter._n
int _n
Definition
PyTHistTestsLib.py:113
array
error
Definition
IImpactPoint3dEstimator.h:72
Generated on
for ATLAS Offline Software by
1.17.0