ATLAS Offline Software
PyTHistTestsLib.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2019 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 from __future__ import print_function
8 
9 __doc__ = """Test read and write histograms and trees via ITHistSvc"""
10 __version__ = "$Revision: 1.4 $"
11 __author__ = "Sebastien Binet <binet@cern.ch>"
12 
13 from AthenaPython import PyAthena
14 StatusCode = PyAthena.StatusCode
15 
17  """Simple algorithm to read data from Root hist. svc
18  """
19  def __init__(self, name='PyHistReader', **kw):
20  # init base class
21  kw['name'] = name
22  super(PyHistReader,self).__init__(**kw)
23 
24  # handle to the root hist svc
25  self.hsvc = None
26  return
27 
28  def initialize(self):
29  _info = self.msg.info
30  _info("==> initialize...")
31  self.hsvc = PyAthena.py_svc('THistSvc/THistSvc')
32  if not self.hsvc:
33  self.msg.error('Could not retrieve THistSvc')
34  return StatusCode.Failure
35 
36  def print_properties(h):
37  _info(' -%-20s: %i <mean>=%8.3f rms=%8.3f',
38  h.GetName(), h.GetEntries(), h.GetMean(), h.GetRMS())
39 
40  # load histos from stream and print some of their properties
41  o = self.hsvc.load('/read1/xxx/gauss1d', oid_type='hist')
42  print_properties(o)
43 
44  o = self.hsvc.load('/read2/gauss2d', oid_type='hist')
45  print_properties(o)
46 
47  o = self.hsvc.load('/read2/gauss3d', oid_type='hist')
48  print_properties(o)
49 
50  o = self.hsvc.load('/read2/profile', oid_type='hist')
51  print_properties(o)
52 
53 
55  try:
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:
59  self.msg.error(err)
60  self.msg.error('bug #36379 still not fixed...')
61  return StatusCode.Success
62 
63  def execute(self):
64  _info = self.msg.info
65  _info('==> execute...')
66  return StatusCode.Success
67 
68  def finalize(self):
69  self.msg.info('==> finalize...')
70  return StatusCode.Success
71 
72  pass # class PyHistReader
73 
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)
80  """
81  def __init__(self, name='PyHistWriter', **kw):
82  # init base class
83  kw['name']=name
84  super(PyHistWriter,self).__init__(**kw)
85 
86  # handle to the root hist svc
87  self.hsvc = None
88  return
89 
90  def initialize(self):
91  _info = self.msg.info
92  _info('==> initialize...')
93  self.hsvc = PyAthena.py_svc('THistSvc/THistSvc')
94  if not self.hsvc:
95  self.msg.error('could not retrieve THistSvc/THistSvc')
96  return StatusCode.Failure
97 
98  from ROOT import TH1F, TH2F, TH3F, TProfile, TTree
99  # helpers
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.)
104 
105  # temporary trees
106  self.hsvc['/temp/h1'] = th1('h1', 'Temporary hist 1')
107  self.hsvc['/temp/other/h1a'] = th1('h1a', 'Temporary hist 1a')
108 
109  # write to stream 'new'
110  self.hsvc['/new/hists/h1'] = th1('h1', 'Persistent hist 1')
111 
112  # update to stream 'upd', dir '/xxx'
113  self.hsvc['/upd/xxx/gauss1d'] = TH1F('gauss1d', '1D gaussian',
114  100,-50.,50.)
115 
116  # recreate stuff in '/'
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')
120 
121  # tree with branches in '/trees/stuff'
122  self.hsvc['/rec/trees/stuff/tree1'] = TTree('tree1','tree title')
123 
124  self._n = -1
125  return StatusCode.Success
126 
127  def execute(self):
128  _info = self.msg.info
129  _info('==> execute...')
130  self._n += 1
131  n = self._n
132  x = math_sin(float(n)) * 52. + 50.
133  hsvc = self.hsvc
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.)
146 
147  tr = hsvc['/rec/trees/stuff/tree1']
148  if n == 0:
149  p1 = array('i',[0])
150  p2 = array('i',[0])
151  p3 = array('i',[0])
152  tr.Branch('branch1', p1, 'point1/I')
153  tr.Branch('branch2', p2, 'point2/I')
154  tr.Branch('branch3', p3, 'point3/I')
155  _tr_fill = tr.Fill
156  for i in range(1000):
157  p1[0] = i
158  p2[0] = i%10
159  p3[0] = i%7
160  _tr_fill()
161  return StatusCode.Success
162 
163  def finalize(self):
164  _info = self.msg.info
165  _info('==> finalize...')
166  for n in ('/temp/h1', '/temp/other/h1a',
167  '/new/hists/h1'):
168  _info('='*20)
169  _info(' - histo [%r]', n)
170  h = self.hsvc[n]
171  _info(' entries = %i', h.GetEntries())
172  _info(' mean = %8.3f', h.GetMean())
173  _info(' rms = %8.3f', h.GetRMS())
174  _info('='*20)
175  return StatusCode.Success
176 
177  pass # class PyHistWriter
grepfile.info
info
Definition: grepfile.py:38
python.tests.PyTHistTestsLib.PyHistWriter.hsvc
hsvc
Definition: PyTHistTestsLib.py:87
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
ParticleTest.tp
tp
Definition: ParticleTest.py:25
python.tests.PyTHistTestsLib.PyHistWriter
Definition: PyTHistTestsLib.py:77
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
python.tests.PyTHistTestsLib.PyHistWriter.__init__
def __init__(self, name='PyHistWriter', **kw)
Definition: PyTHistTestsLib.py:81
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
array
python.tests.PyTHistTestsLib.PyHistReader.__init__
def __init__(self, name='PyHistReader', **kw)
Definition: PyTHistTestsLib.py:19
python.tests.PyTHistTestsLib.PyHistWriter._n
_n
Definition: PyTHistTestsLib.py:124
TH1F
Definition: rootspy.cxx:320
python.SystemOfUnits.gauss
int gauss
Definition: SystemOfUnits.py:230
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
python.tests.PyTHistTestsLib.PyHistReader.hsvc
hsvc
Definition: PyTHistTestsLib.py:25
PyAthena::Alg
Definition: PyAthenaAlg.h:33
python.root_pickle.load
def load(f, use_proxy=1, key=None)
Definition: root_pickle.py:476
error
Definition: IImpactPoint3dEstimator.h:70
readCCLHist.float
float
Definition: readCCLHist.py:83
python.tests.PyTHistTestsLib.PyHistReader
Definition: PyTHistTestsLib.py:16