ATLAS Offline Software
xAODRootTest.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
2 #
3 # File: share/xAODRootTest.py
4 # Author: snyder@bnl.gov
5 # Date: Jun 2014
6 # Purpose: Test reading xAOD objects directly from root.
7 #
8 
9 # Work around library loading order issue seen with the LTO build.
10 # Otherwise we can get inconsistent resolution of a static std::string,
11 # leading to a free() failure during exit().
12 import ROOT
13 if ROOT.gSystem.FindDynamicLibrary ("libGaudiKernel", True):
14  ROOT.gSystem.Load("libGaudiKernel")
15 
16 import cppyy
17 
18 import sys
19 cppyy.load_library("libDataModelTestDataCommonDict")
20 if 'LOAD_WRITE_DIR' in globals():
21  cppyy.load_library("libDataModelTestDataWriteDict")
22 else:
23  cppyy.load_library("libDataModelTestDataReadDict")
24 
25 def CHECK(sc):
26  if not sc.isSuccess():
27  raise Exception ('bad StatusCode')
28  return
29 
30 
31 
32 reg=ROOT.SG.AuxTypeRegistry.instance()
33 
34 def _typename(t):
35  return getattr (t, '__cpp_name__', t.__name__)
36 
37 def xAODInit():
38  ROOT.xAOD.TEvent
39  CHECK(ROOT.xAOD.Init())
40 
41  # Monkey-patch...
42  ROOT.xAOD.TEvent.record_impl = ROOT.xAOD.TEvent.record
43  def record (event, obj, key, basketSize=32000, splitLevel = 1):
44  return event.record_impl (obj,
45  _typename (obj.__class__),
46  key,
47  basketSize, splitLevel)
48  ROOT.xAOD.TEvent.record = record
49  return
50 
51 
52 
53 cvec_cls=ROOT.DataVector(ROOT.DMTest.C_v1)
54 cel_cls=ROOT.ElementLink(cvec_cls)
55 
56 
57 
58 def format_int(x): return '%d'%x
59 def format_float(x): return '%.1f'%x
60 def format_str(x): return f"'{x}'"
61 def format_el(x): return '%s[%s]' % (x.dataID(), ('inv' if x.isDefaultIndex() else x.index()))
62 def format_el_vec(v): return '[' + ', '.join([format_el(el) for el in v.asVector()]) + ']'
64  l = [format_float(x) for x in v]
65  return '[' + ','.join(l) + ']'
67  l = [format_int(x) for x in v]
68  return '[' + ','.join(l) + ']'
70  l = [format_str(x) for x in v]
71  return '[' + ','.join(l) + ']'
72 CVec_type = ROOT.DataVector(ROOT.DMTest.C_v1)
73 accessors = {
74  'int' : (ROOT.SG.ConstAccessor(int), format_int),
75  'unsigned int' : (getattr (ROOT, 'SG::ConstAccessor<unsigned int>'), format_int),
76  'float' : (ROOT.SG.ConstAccessor(float), format_float),
77  'std::vector<float>' : (getattr (ROOT, 'SG::ConstAccessor<std::vector<float> >'), format_float_vec),
78  'std::vector<int>' : (getattr (ROOT, 'SG::ConstAccessor<std::vector<int> >'), format_int_vec),
79  'ElementLink<DataVector<DMTest::C_v1> >' :
80  (ROOT.SG.ConstAccessor(cel_cls), format_el),
81  'SG::JaggedVecElt<int>' :
82  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.int)),
83  format_int_vec),
84  'SG::JaggedVecElt<float>' :
85  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.float)),
86  format_float_vec),
87  'SG::JaggedVecElt<double>' :
88  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.double)),
89  format_float_vec),
90  'SG::JaggedVecElt<std::string>' :
91  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.std.string)),
92  format_str_vec),
93  'SG::JaggedVecElt<ElementLink<DataVector<DMTest::C_v1> > >' :
94  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.ElementLink(CVec_type))),
95  format_el_vec),
96  'SG::PackedLink<DataVector<DMTest::C_v1> >' :
97  (ROOT.SG.ConstAccessor(ROOT.SG.PackedLink(ROOT.DataVector(ROOT.DMTest.C_v1))),
98  format_el),
99  'std::vector<SG::PackedLink<DataVector<DMTest::C_v1> > >' :
100  (ROOT.SG.ConstAccessor(ROOT.std.vector(ROOT.SG.PackedLink(ROOT.DataVector(ROOT.DMTest.C_v1)))),
101  format_el_vec),
102  }
103 
104 def dump_auxitem (x, auxid, f = sys.stdout):
105  tname = reg.getTypeName (auxid)
106  ac_p = accessors.get (tname)
107  if not ac_p:
108  print ('<unknown %s>'%tname, end='', file=f)
109  else:
110  (ac_cl, formatter) = ac_p
111  val = ac_cl(reg.getName(auxid))(x)
112  print (formatter(val) + '; ', end='', file=f)
113  return
114 
115 
116 def dump_auxdata (x, exclude=[], f = sys.stdout):
117  auxids = list(x.getAuxIDs())
118  auxids = [(reg.getName(id), id) for id in auxids]
119  auxids.sort()
120  for name, auxid in auxids:
121  if name in exclude: continue
122  if reg.isLinked (auxid): continue
123  print (name + ': ', file=f, end='')
124  dump_auxitem (x, auxid, f)
125  return
126 
127 
128 def dump_c (c, f=sys.stdout):
129  if hasattr(c, '__deref__'):
130  c = c.__deref__()
131  print (' anInt1: %d; aFloat: %.1f; ' % (c.anInt(), c.aFloat()), end='')
132  dump_auxdata (c, exclude = ['anInt1', 'aFloat'])
133  print ('')
134  return
135 
136 
137 def dump_xaodobj (h, f=sys.stdout):
138  if hasattr(h, '__deref__'):
139  h = h.__deref__()
140  dump_auxdata (h)
141  print ('')
142  return
143 
144 
145 def dump_plinks (p, f=sys.stdout):
146  if hasattr(p, '__deref__'):
147  p = p.__deref__()
148  dump_auxdata (p)
149  print ('')
150  return
151 
152 
153 def copy_obj (event, obj, key):
154  copy = obj.__class__()
155  copy_aux = obj.getConstStore().__class__()
156  copy.setNonConstStore (copy_aux)
157  copy.__assign__ (obj)
158  CHECK (event.record (copy, key))
159  CHECK (event.record (copy_aux, key + 'Aux.'))
160  ROOT.SetOwnership (copy, False)
161  ROOT.SetOwnership (copy_aux, False)
162  return
163 
164 
165 def copy_vec (event, obj, key):
166  copy = obj.__class__()
167  copy_aux = obj.getConstStore().__class__()
168  copy.setNonConstStore (copy_aux)
169  for i in range(obj.size()):
170  elt_orig = obj[i]
171  if _typename (elt_orig.__class__).startswith ('DataModel_detail::ElementProxy<'):
172  elt_orig = elt_orig.__follow__()
173  elt = elt_orig.__class__()
174  copy.push_back(elt)
175  ROOT.SetOwnership (elt, False)
176  elt.__assign__ (elt_orig)
177  CHECK (event.record (copy, key))
178  CHECK (event.record (copy_aux, key + 'Aux.'))
179  ROOT.SetOwnership (copy, False)
180  ROOT.SetOwnership (copy_aux, False)
181  return
182 
183 
184 def copy_view (event, obj, key):
185  copy = obj.__class__(obj)
186  copy.toPersistent()
187  CHECK (event.record (copy, key))
188  ROOT.SetOwnership (copy, False)
189  return
190 
191 
193  def __init__ (self, readPrefix = ''):
194  self.readPrefix = readPrefix
195  return
196 
197 
198  def execute (self, tree, event=None):
199  print (self.readPrefix + 'cvec')
200  vec = getattr (tree, self.readPrefix + 'cvec')
201  for c in vec:
202  dump_c (c)
203 
204  print (self.readPrefix + 'cinfo')
205  cinfo = getattr (tree, self.readPrefix + 'cinfo')
206  dump_c (cinfo)
207 
208  print (self.readPrefix + 'ctrig')
209  ctrig = getattr (tree, self.readPrefix + 'ctrig')
210  for c in ctrig:
211  dump_c (c)
212 
213  vec = getattr (tree, self.readPrefix + 'cvecWD')
214  print (self.readPrefix + 'cvecWD' + ' ', vec.meta1)
215  for c in vec:
216  dump_c (c)
217 
218  vec = getattr (tree, self.readPrefix + 'cview')
219  print (self.readPrefix + 'cview')
220  for c in vec:
221  dump_c (c)
222 
223  print (self.readPrefix + 'pvec')
224  vec = getattr (tree, self.readPrefix + 'pvec')
225  for p in vec:
226  dump_xaodobj (p)
227 
228  print (self.readPrefix + 'hvec')
229  vec = getattr (tree, self.readPrefix + 'hvec')
230  for h in vec:
231  dump_xaodobj (h)
232 
233  print (self.readPrefix + 'jvecContainer')
234  vec = getattr (tree, self.readPrefix + 'jvecContainer')
235  for h in vec:
236  dump_xaodobj (h)
237 
238  print (self.readPrefix + 'jvecInfo')
239  jvecInfo = getattr (tree, self.readPrefix + 'jvecInfo')
240  dump_xaodobj (jvecInfo)
241 
242  print (self.readPrefix + 'plinksContainer')
243  vec = getattr (tree, self.readPrefix + 'plinksContainer')
244  for h in vec:
245  dump_plinks (h)
246 
247  print (self.readPrefix + 'plinksInfo')
248  plinksInfo = getattr (tree, self.readPrefix + 'plinksInfo')
249  dump_plinks (plinksInfo)
250 
251  #vec = getattr (tree, self.readPrefix + 'hview')
252  #print (self.readPrefix + 'hview')
253  #for h in vec:
254  # dump_h (h)
255 
256  return
257 
258 
260  def __init__ (self, readPrefix = '', writePrefix = None):
261  self.readPrefix = readPrefix
262  self.writePrefix = writePrefix
263  return
264 
265  def execute (self, tree, event):
266  CHECK (event.copy (self.readPrefix + 'cvec'))
267  CHECK (event.copy (self.readPrefix + 'cinfo'))
268  CHECK (event.copy (self.readPrefix + 'ctrig'))
269  CHECK (event.copy (self.readPrefix + 'cvecWD'))
270  CHECK (event.copy (self.readPrefix + 'cview'))
271  CHECK (event.copy (self.readPrefix + 'pvec'))
272  CHECK (event.copy (self.readPrefix + 'hvec'))
273  CHECK (event.copy (self.readPrefix + 'jvecContainer'))
274  CHECK (event.copy (self.readPrefix + 'jvecInfo'))
275  CHECK (event.copy (self.readPrefix + 'plinksContainer'))
276  CHECK (event.copy (self.readPrefix + 'plinksInfo'))
277  #CHECK (event.copy (self.readPrefix + 'hview'))
278 
279  if self.writePrefix != None:
280  cinfo = getattr (tree, self.readPrefix + 'cinfo')
281  copy_obj (event, cinfo, self.writePrefix + 'cinfo')
282 
283  cvec = getattr (tree, self.readPrefix + 'cvec')
284  copy_vec (event, cvec, self.writePrefix + 'cvec')
285 
286  ctrig = getattr (tree, self.readPrefix + 'ctrig')
287  copy_vec (event, ctrig, self.writePrefix + 'ctrig')
288 
289  cvecwd = getattr (tree, self.readPrefix + 'cvecWD')
290  copy_vec (event, cvecwd, self.writePrefix + 'cvecWD')
291 
292  cview = getattr (tree, self.readPrefix + 'cview')
293  copy_view (event, cview, self.writePrefix + 'cview')
294 
295  pvec = getattr (tree, self.readPrefix + 'pvec')
296  copy_vec (event, pvec, self.writePrefix + 'pvec')
297 
298  hvec = getattr (tree, self.readPrefix + 'hvec')
299  copy_vec (event, hvec, self.writePrefix + 'hvec')
300 
301  jvec = getattr (tree, self.readPrefix + 'jvecContainer')
302  copy_vec (event, jvec, self.writePrefix + 'jvecContainer')
303 
304  jvecinfo = getattr (tree, self.readPrefix + 'jvecInfo')
305  copy_obj (event, jvecinfo, self.writePrefix + 'jvecInfo')
306 
307  plinks = getattr (tree, self.readPrefix + 'plinksContainer')
308  copy_vec (event, plinks, self.writePrefix + 'plinksContainer')
309 
310  plinksinfo = getattr (tree, self.readPrefix + 'plinksInfo')
311  copy_obj (event, plinksinfo, self.writePrefix + 'plinksInfo')
312 
313  #hview = getattr (tree, self.readPrefix + 'hview')
314  #copy_view (event, hview, self.writePrefix + 'hview')
315 
316  return
317 
318 
320  def __init__ (self, decorName, offset=0, readPrefix = ''):
321  self.readPrefix = readPrefix
322  self.offset = offset
323  self.decor = ROOT.SG.Decorator(int)(decorName)
324  return
325 
326 
327  def execute (self, tree, event=None):
328  cvec = getattr (tree, self.readPrefix + 'cvec')
329  for c in cvec:
330  self.decor.set(c, self.offset + c.anInt())
331 
332  ctrig = getattr (tree, self.readPrefix + 'ctrig')
333  for c in cvec:
334  self.decor.set(c, self.offset + c.anInt())
335 
336  cinfo = getattr (tree, self.readPrefix + 'cinfo')
337  self.decor.set(cinfo, self.offset + cinfo.anInt())
338  return
339 
340 
342  def __init__ (self, decorName, offset=0, readPrefix = ''):
343  self.readPrefix = readPrefix
344  self.offset = offset
345  self.decor = ROOT.SG.Decorator(int)(decorName)
346  return
347 
348 
349  def execute (self, tree, event=None):
350  cvec = getattr (tree, self.readPrefix + 'cvec')
351  assert cvec.setOption (self.decor.auxid(), ROOT.SG.AuxDataOption ('nbins', 23))
352  for c in cvec:
353  self.decor.set(c, self.offset + c.anInt())
354  return
355 
356 
358  def __init__ (self, readPrefix = ''):
359  self.readPrefix = readPrefix
360  return
361 
362 
363  def execute (self, tree, event=None):
364  cvec = getattr (tree, self.readPrefix + 'cvec')
365  cvec.clearDecorations()
366 
367  ctrig = getattr (tree, self.readPrefix + 'ctrig')
368  ctrig.clearDecorations()
369 
370  cinfo = getattr (tree, self.readPrefix + 'cinfo')
371  cinfo.clearDecorations()
372  return
373 
374 
375 
377  def execute (self, tree, event=None):
378  cont = tree.AllocTest
379  print ('AllocTest: ', end='')
380  for a in cont:
381  print (a.atInt1(), a.atInt2(), end=' ')
382  print()
383  return
384 
385 
386 
387 class Analysis:
388  def __init__ (self, ifname, ofname = None):
389  self.algs = []
390  self.f = ROOT.TFile (ifname)
391  self.event = ROOT.xAOD.TEvent (ROOT.xAOD.TEvent.kAthenaAccess)
392  CHECK (self.event.readFrom (self.f, True, 'CollectionTree'))
393  self.tree = ROOT.xAOD.MakeTransientTree(self.event, 'CollectionTree')
394  self.fout = None
395  if ofname:
396  self.fout = ROOT.TFile.Open (ofname, 'recreate')
397  CHECK (self.event.writeTo (self.fout))
398  return
399 
400  def add (self, alg):
401  self.algs.append (alg)
402  return
403 
404  def run (self, n=None):
405  nent = self.tree.GetEntries()
406  if n != None:
407  nent = min (n, nent)
408  for i in range(nent):
409  self.tree.GetEntry(i)
410  print ('---> Event', i)
411  for a in self.algs:
412  a.execute (self.tree, self.event)
413  if self.fout != None:
414  self.event.fill()
415  return
416 
417  def finalize (self):
418  if self.fout:
419  CHECK (self.event.finishWritingTo (self.fout))
420  return
421 
422 
xAODRootTest.format_str
def format_str(x)
Definition: xAODRootTest.py:60
xAODRootTest.xAODTestClearDecor
Definition: xAODRootTest.py:357
xAODRootTest.AllocTestRead
Definition: xAODRootTest.py:376
xAODRootTest.xAODTestRead.execute
def execute(self, tree, event=None)
Definition: xAODRootTest.py:198
xAODRootTest.format_el_vec
def format_el_vec(v)
Definition: xAODRootTest.py:62
xAODRootTest.xAODTestPDecor
Definition: xAODRootTest.py:341
xAODRootTest.xAODTestClearDecor.__init__
def __init__(self, readPrefix='')
Definition: xAODRootTest.py:358
xAODRootTest.xAODTestCopy.writePrefix
writePrefix
Definition: xAODRootTest.py:262
xAODRootTest.dump_plinks
def dump_plinks(p, f=sys.stdout)
Definition: xAODRootTest.py:145
xAODRootTest.xAODTestCopy.execute
def execute(self, tree, event)
Definition: xAODRootTest.py:265
xAODRootTest.xAODTestPDecor.readPrefix
readPrefix
Definition: xAODRootTest.py:343
xAODRootTest.xAODTestPDecor.decor
decor
Definition: xAODRootTest.py:345
GetEntries
TGraphErrors * GetEntries(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:4019
xAODRootTest._typename
def _typename(t)
Definition: xAODRootTest.py:34
xAODRootTest.format_int_vec
def format_int_vec(v)
Definition: xAODRootTest.py:66
xAODRootTest.dump_c
def dump_c(c, f=sys.stdout)
Definition: xAODRootTest.py:128
xAODRootTest.Analysis.event
event
Definition: xAODRootTest.py:391
xAODRootTest.xAODTestDecor.__init__
def __init__(self, decorName, offset=0, readPrefix='')
Definition: xAODRootTest.py:320
xAODRootTest.xAODTestDecor.decor
decor
Definition: xAODRootTest.py:323
xAODRootTest.xAODTestRead
Definition: xAODRootTest.py:192
xAODRootTest.xAODTestDecor.offset
offset
Definition: xAODRootTest.py:322
xAODRootTest.Analysis
Definition: xAODRootTest.py:387
xAODRootTest.xAODTestCopy
Definition: xAODRootTest.py:259
xAODRootTest.Analysis.finalize
def finalize(self)
Definition: xAODRootTest.py:417
xAODRootTest.xAODTestDecor.execute
def execute(self, tree, event=None)
Definition: xAODRootTest.py:327
xAODRootTest.format_int
def format_int(x)
Definition: xAODRootTest.py:58
xAODRootTest.AllocTestRead.execute
def execute(self, tree, event=None)
Definition: xAODRootTest.py:377
xAODRootTest.Analysis.algs
algs
Definition: xAODRootTest.py:389
xAODRootTest.format_el
def format_el(x)
Definition: xAODRootTest.py:61
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
xAODRootTest.Analysis.f
f
Definition: xAODRootTest.py:390
xAODRootTest.Analysis.__init__
def __init__(self, ifname, ofname=None)
Definition: xAODRootTest.py:388
xAODRootTest.xAODTestPDecor.execute
def execute(self, tree, event=None)
Definition: xAODRootTest.py:349
xAODRootTest.copy_view
def copy_view(event, obj, key)
Definition: xAODRootTest.py:184
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
xAODRootTest.format_str_vec
def format_str_vec(v)
Definition: xAODRootTest.py:69
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
fill
void fill(H5::Group &out_file, size_t iterations)
Definition: test-hdf5-writer.cxx:95
xAODRootTest.xAODTestCopy.readPrefix
readPrefix
Definition: xAODRootTest.py:261
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
xAODRootTest.xAODTestClearDecor.readPrefix
readPrefix
Definition: xAODRootTest.py:359
vtune_athena.formatter
formatter
Definition: vtune_athena.py:19
xAODRootTest.format_float
def format_float(x)
Definition: xAODRootTest.py:59
xAODRootTest.xAODInit
def xAODInit()
Definition: xAODRootTest.py:37
xAODRootTest.xAODTestPDecor.__init__
def __init__(self, decorName, offset=0, readPrefix='')
Definition: xAODRootTest.py:342
xAODRootTest.xAODTestDecor.readPrefix
readPrefix
Definition: xAODRootTest.py:321
xAODRootTest.dump_auxitem
def dump_auxitem(x, auxid, f=sys.stdout)
Definition: xAODRootTest.py:104
xAODRootTest.CHECK
def CHECK(sc)
Definition: xAODRootTest.py:25
xAODRootTest.xAODTestRead.__init__
def __init__(self, readPrefix='')
Definition: xAODRootTest.py:193
xAODRootTest.dump_xaodobj
def dump_xaodobj(h, f=sys.stdout)
Definition: xAODRootTest.py:137
xAODRootTest.xAODTestClearDecor.execute
def execute(self, tree, event=None)
Definition: xAODRootTest.py:363
xAODRootTest.copy_obj
def copy_obj(event, obj, key)
Definition: xAODRootTest.py:153
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
xAODRootTest.Analysis.add
def add(self, alg)
Definition: xAODRootTest.py:400
xAODRootTest.xAODTestDecor
Definition: xAODRootTest.py:319
xAODRootTest.xAODTestCopy.__init__
def __init__(self, readPrefix='', writePrefix=None)
Definition: xAODRootTest.py:260
xAODRootTest.xAODTestPDecor.offset
offset
Definition: xAODRootTest.py:344
xAODRootTest.format_float_vec
def format_float_vec(v)
Definition: xAODRootTest.py:63
xAODRootTest.Analysis.fout
fout
Definition: xAODRootTest.py:394
xAODRootTest.Analysis.tree
tree
Definition: xAODRootTest.py:393
xAODRootTest.xAODTestRead.readPrefix
readPrefix
Definition: xAODRootTest.py:194
xAODRootTest.copy_vec
def copy_vec(event, obj, key)
Definition: xAODRootTest.py:165
xAODRootTest.Analysis.run
def run(self, n=None)
Definition: xAODRootTest.py:404
xAODRootTest.dump_auxdata
def dump_auxdata(x, exclude=[], f=sys.stdout)
Definition: xAODRootTest.py:116