ATLAS Offline Software
xAODRootTest.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 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 (cppyy.gbl.TString ("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  return
41 
42 
43 
44 cvec_cls=ROOT.DataVector(ROOT.DMTest.C_v1)
45 cel_cls=ROOT.ElementLink(cvec_cls)
46 
47 
48 
49 def format_int(x): return '%d'%x
50 def format_float(x): return '%.1f'%x
51 def format_str(x): return f"'{x}'"
52 def format_el(x): return '%s[%s]' % (x.dataID(), ('inv' if x.isDefaultIndex() else x.index()))
53 def format_el_vec(v): return '[' + ', '.join([format_el(el) for el in v.asVector()]) + ']'
55  l = [format_float(x) for x in v]
56  return '[' + ','.join(l) + ']'
58  l = [format_int(x) for x in v]
59  return '[' + ','.join(l) + ']'
61  l = [format_str(x) for x in v]
62  return '[' + ','.join(l) + ']'
63 CVec_type = ROOT.DataVector(ROOT.DMTest.C_v1)
64 accessors = {
65  'int' : (ROOT.SG.ConstAccessor(int), format_int),
66  'unsigned int' : (getattr (ROOT, 'SG::ConstAccessor<unsigned int>'), format_int),
67  'float' : (ROOT.SG.ConstAccessor(float), format_float),
68  'std::vector<float>' : (getattr (ROOT, 'SG::ConstAccessor<std::vector<float> >'), format_float_vec),
69  'std::vector<int>' : (getattr (ROOT, 'SG::ConstAccessor<std::vector<int> >'), format_int_vec),
70  'ElementLink<DataVector<DMTest::C_v1> >' :
71  (ROOT.SG.ConstAccessor(cel_cls), format_el),
72  'SG::JaggedVecElt<int>' :
73  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.int)),
74  format_int_vec),
75  'SG::JaggedVecElt<float>' :
76  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.float)),
77  format_float_vec),
78  'SG::JaggedVecElt<double>' :
79  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.double)),
80  format_float_vec),
81  'SG::JaggedVecElt<std::string>' :
82  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.std.string)),
83  format_str_vec),
84  'SG::JaggedVecElt<ElementLink<DataVector<DMTest::C_v1> > >' :
85  (ROOT.SG.ConstAccessor(ROOT.SG.JaggedVecElt(ROOT.ElementLink(CVec_type))),
86  format_el_vec),
87  'SG::PackedLink<DataVector<DMTest::C_v1> >' :
88  (ROOT.SG.ConstAccessor(ROOT.SG.PackedLink(ROOT.DataVector(ROOT.DMTest.C_v1))),
89  format_el),
90  'std::vector<SG::PackedLink<DataVector<DMTest::C_v1> > >' :
91  (ROOT.SG.ConstAccessor(ROOT.std.vector(ROOT.SG.PackedLink(ROOT.DataVector(ROOT.DMTest.C_v1)))),
92  format_el_vec),
93  }
94 
95 def dump_auxitem (x, auxid, f = sys.stdout):
96  tname = reg.getTypeName (auxid)
97  ac_p = accessors.get (tname)
98  if not ac_p:
99  print ('<unknown %s>'%tname, end='', file=f)
100  else:
101  (ac_cl, formatter) = ac_p
102  val = ac_cl(reg.getName(auxid))(x)
103  print (formatter(val) + '; ', end='', file=f)
104  return
105 
106 
107 def dump_auxdata (x, exclude=[], f = sys.stdout):
108  auxids = list(x.getAuxIDs())
109  auxids = [(reg.getName(id), id) for id in auxids]
110  auxids.sort()
111  for name, auxid in auxids:
112  if name in exclude: continue
113  if reg.isLinked (auxid): continue
114  print (name + ': ', file=f, end='')
115  dump_auxitem (x, auxid, f)
116  return
117 
118 
119 def dump_c (c, f=sys.stdout):
120  if hasattr(c, '__deref__'):
121  c = c.__deref__()
122  print (' anInt1: %d; aFloat: %.1f; ' % (c.anInt(), c.aFloat()), end='')
123  dump_auxdata (c, exclude = ['anInt1', 'aFloat'])
124  print ('')
125  return
126 
127 
128 def dump_xaodobj (h, f=sys.stdout):
129  if hasattr(h, '__deref__'):
130  h = h.__deref__()
131  dump_auxdata (h)
132  print ('')
133  return
134 
135 
136 def dump_plinks (p, f=sys.stdout):
137  if hasattr(p, '__deref__'):
138  p = p.__deref__()
139  dump_auxdata (p)
140  print ('')
141  return
142 
143 
144 def copy_obj (event, obj, key):
145  copy = obj.__class__()
146  copy_aux = obj.getConstStore().__class__()
147  copy.setNonConstStore (copy_aux)
148  copy.__assign__ (obj)
149  CHECK (event.record (copy, key))
150  CHECK (event.record (copy_aux, key + 'Aux.'))
151  ROOT.SetOwnership (copy, False)
152  ROOT.SetOwnership (copy_aux, False)
153  return
154 
155 
156 def copy_vec (event, obj, key):
157  copy = obj.__class__()
158  copy_aux = obj.getConstStore().__class__()
159  copy.setNonConstStore (copy_aux)
160  for i in range(obj.size()):
161  elt_orig = obj[i]
162  if _typename (elt_orig.__class__).startswith ('DataModel_detail::ElementProxy<'):
163  elt_orig = elt_orig.__follow__()
164  elt = elt_orig.__class__()
165  copy.push_back(elt)
166  ROOT.SetOwnership (elt, False)
167  elt.__assign__ (elt_orig)
168  CHECK (event.record (copy, key))
169  CHECK (event.record (copy_aux, key + 'Aux.'))
170  ROOT.SetOwnership (copy, False)
171  ROOT.SetOwnership (copy_aux, False)
172  return
173 
174 
175 def copy_view (event, obj, key):
176  copy = obj.__class__(obj)
177  copy.toPersistent()
178  CHECK (event.record (copy, key))
179  ROOT.SetOwnership (copy, False)
180  return
181 
182 
184  def __init__ (self, readPrefix = ''):
185  self.readPrefix = readPrefix
186  return
187 
188 
189  def execute (self, event):
190  print (self.readPrefix + 'cvec')
191  vec = event[self.readPrefix + 'cvec']
192  for c in vec:
193  dump_c (c)
194 
195  print (self.readPrefix + 'cinfo')
196  cinfo = event[self.readPrefix + 'cinfo']
197  dump_c (cinfo)
198 
199  print (self.readPrefix + 'ctrig')
200  ctrig = event[self.readPrefix + 'ctrig']
201  for c in ctrig:
202  dump_c (c)
203 
204  vec = event[self.readPrefix + 'cvecWD']
205  print (self.readPrefix + 'cvecWD' + ' ', vec.meta1)
206  for c in vec:
207  dump_c (c)
208 
209  vec = event[self.readPrefix + 'cview']
210  print (self.readPrefix + 'cview')
211  for c in vec:
212  dump_c (c)
213 
214  print (self.readPrefix + 'pvec')
215  vec = event[self.readPrefix + 'pvec']
216  for p in vec:
217  dump_xaodobj (p)
218 
219  print (self.readPrefix + 'hvec')
220  vec = event[self.readPrefix + 'hvec']
221  for h in vec:
222  dump_xaodobj (h)
223 
224  print (self.readPrefix + 'jvecContainer')
225  vec = event[self.readPrefix + 'jvecContainer']
226  for h in vec:
227  dump_xaodobj (h)
228 
229  print (self.readPrefix + 'jvecInfo')
230  jvecInfo = event[self.readPrefix + 'jvecInfo']
231  dump_xaodobj (jvecInfo)
232 
233  print (self.readPrefix + 'plinksContainer')
234  vec = event[self.readPrefix + 'plinksContainer']
235  for h in vec:
236  dump_plinks (h)
237 
238  print (self.readPrefix + 'plinksInfo')
239  plinksInfo = event[self.readPrefix + 'plinksInfo']
240  dump_plinks (plinksInfo)
241 
242  #vec = event[self.readPrefix + 'hview']
243  #print (self.readPrefix + 'hview')
244  #for h in vec:
245  # dump_h (h)
246 
247  return
248 
249 
251  def __init__ (self, readPrefix = '', writePrefix = None):
252  self.readPrefix = readPrefix
253  self.writePrefix = writePrefix
254  return
255 
256  def execute (self, event):
257  CHECK (event.copy (self.readPrefix + 'cvec'))
258  CHECK (event.copy (self.readPrefix + 'cinfo'))
259  CHECK (event.copy (self.readPrefix + 'ctrig'))
260  CHECK (event.copy (self.readPrefix + 'cvecWD'))
261  CHECK (event.copy (self.readPrefix + 'cview'))
262  CHECK (event.copy (self.readPrefix + 'pvec'))
263  CHECK (event.copy (self.readPrefix + 'hvec'))
264  CHECK (event.copy (self.readPrefix + 'jvecContainer'))
265  CHECK (event.copy (self.readPrefix + 'jvecInfo'))
266  CHECK (event.copy (self.readPrefix + 'plinksContainer'))
267  CHECK (event.copy (self.readPrefix + 'plinksInfo'))
268  #CHECK (event.copy (self.readPrefix + 'hview'))
269 
270  if self.writePrefix != None:
271  cinfo = event[self.readPrefix + 'cinfo']
272  copy_obj (event, cinfo, self.writePrefix + 'cinfo')
273 
274  cvec = event[self.readPrefix + 'cvec']
275  copy_vec (event, cvec, self.writePrefix + 'cvec')
276 
277  ctrig = event[self.readPrefix + 'ctrig']
278  copy_vec (event, ctrig, self.writePrefix + 'ctrig')
279 
280  cvecwd = event[self.readPrefix + 'cvecWD']
281  copy_vec (event, cvecwd, self.writePrefix + 'cvecWD')
282 
283  cview = event[self.readPrefix + 'cview']
284  copy_view (event, cview, self.writePrefix + 'cview')
285 
286  pvec = event[self.readPrefix + 'pvec']
287  copy_vec (event, pvec, self.writePrefix + 'pvec')
288 
289  hvec = event[self.readPrefix + 'hvec']
290  copy_vec (event, hvec, self.writePrefix + 'hvec')
291 
292  jvec = event[self.readPrefix + 'jvecContainer']
293  copy_vec (event, jvec, self.writePrefix + 'jvecContainer')
294 
295  jvecinfo = event[self.readPrefix + 'jvecInfo']
296  copy_obj (event, jvecinfo, self.writePrefix + 'jvecInfo')
297 
298  plinks = event[self.readPrefix + 'plinksContainer']
299  copy_vec (event, plinks, self.writePrefix + 'plinksContainer')
300 
301  plinksinfo = event[self.readPrefix + 'plinksInfo']
302  copy_obj (event, plinksinfo, self.writePrefix + 'plinksInfo')
303 
304  #hview = event[self.readPrefix + 'hview']
305  #copy_view (event, hview, self.writePrefix + 'hview')
306 
307  return
308 
309 
311  def __init__ (self, decorName, offset=0, readPrefix = ''):
312  self.readPrefix = readPrefix
313  self.offset = offset
314  self.decor = ROOT.SG.Decorator(int)(decorName)
315  return
316 
317 
318  def execute (self, event):
319  cvec = event[self.readPrefix + 'cvec']
320  for c in cvec:
321  self.decor.set(c, self.offset + c.anInt())
322 
323  ctrig = event[self.readPrefix + 'ctrig']
324  for c in cvec:
325  self.decor.set(c, self.offset + c.anInt())
326 
327  cinfo = event[self.readPrefix + 'cinfo']
328  self.decor.set(cinfo, self.offset + cinfo.anInt())
329  return
330 
331 
333  def __init__ (self, decorName, offset=0, readPrefix = ''):
334  self.readPrefix = readPrefix
335  self.offset = offset
336  self.decor = ROOT.SG.Decorator(int)(decorName)
337  return
338 
339 
340  def execute (self, event):
341  cvec = event[self.readPrefix + 'cvec']
342  assert cvec.setOption (self.decor.auxid(), ROOT.SG.AuxDataOption ('nbins', 23))
343  for c in cvec:
344  self.decor.set(c, self.offset + c.anInt())
345  return
346 
347 
349  def __init__ (self, readPrefix = ''):
350  self.readPrefix = readPrefix
351  return
352 
353 
354  def execute (self, event):
355  cvec = event[self.readPrefix + 'cvec']
356  cvec.clearDecorations()
357 
358  ctrig = event[self.readPrefix + 'ctrig']
359  ctrig.clearDecorations()
360 
361  cinfo = event[self.readPrefix + 'cinfo']
362  cinfo.clearDecorations()
363  return
364 
365 
366 
368  def execute (self, event):
369  cont = event['AllocTest']
370  print ('AllocTest: ', end='')
371  for a in cont:
372  print (a.atInt1(), a.atInt2(), end=' ')
373  print()
374  return
375 
376 
377 
378 class Analysis:
379  def __init__ (self, ifname, ofname = None):
380  self.algs = []
381  self.f = ROOT.TFile (ifname)
382  from xAODRootAccess.TPyEvent import TPyEvent
383  self.event = TPyEvent (ROOT.xAOD.TEvent.kAthenaAccess)
384  CHECK (self.event.readFrom (self.f, True, 'CollectionTree'))
385  self.fout = None
386  if ofname:
387  self.fout = ROOT.TFile.Open (ofname, 'recreate')
388  CHECK (self.event.writeTo (self.fout))
389  return
390 
391  def add (self, alg):
392  self.algs.append (alg)
393  return
394 
395  def run (self, n=None):
396  nent = self.event.getEntries()
397  if n != None:
398  nent = min (n, nent)
399  for i in range(nent):
400  self.event.getEntry(i)
401  print ('---> Event', i)
402  for a in self.algs:
403  a.execute (self.event)
404  if self.fout != None:
405  self.event.fill()
406  return
407 
408  def finalize (self):
409  if self.fout:
410  CHECK (self.event.finishWritingTo (self.fout))
411  return
412 
413 
xAODRootTest.format_str
def format_str(x)
Definition: xAODRootTest.py:51
xAODRootTest.xAODTestPDecor.execute
def execute(self, event)
Definition: xAODRootTest.py:340
xAODRootTest.xAODTestClearDecor
Definition: xAODRootTest.py:348
xAODRootTest.AllocTestRead
Definition: xAODRootTest.py:367
xAODRootTest.format_el_vec
def format_el_vec(v)
Definition: xAODRootTest.py:53
xAODRootTest.xAODTestPDecor
Definition: xAODRootTest.py:332
xAODRootTest.xAODTestClearDecor.__init__
def __init__(self, readPrefix='')
Definition: xAODRootTest.py:349
xAODRootTest.xAODTestCopy.writePrefix
writePrefix
Definition: xAODRootTest.py:253
xAODRootTest.dump_plinks
def dump_plinks(p, f=sys.stdout)
Definition: xAODRootTest.py:136
xAODRootTest.xAODTestDecor.execute
def execute(self, event)
Definition: xAODRootTest.py:318
xAODRootTest.xAODTestPDecor.readPrefix
readPrefix
Definition: xAODRootTest.py:334
xAODRootTest.xAODTestPDecor.decor
decor
Definition: xAODRootTest.py:336
xAODRootTest._typename
def _typename(t)
Definition: xAODRootTest.py:34
xAODRootTest.format_int_vec
def format_int_vec(v)
Definition: xAODRootTest.py:57
xAODRootTest.dump_c
def dump_c(c, f=sys.stdout)
Definition: xAODRootTest.py:119
xAODRootTest.xAODTestRead.execute
def execute(self, event)
Definition: xAODRootTest.py:189
xAODRootTest.Analysis.event
event
Definition: xAODRootTest.py:383
xAODRootTest.xAODTestDecor.__init__
def __init__(self, decorName, offset=0, readPrefix='')
Definition: xAODRootTest.py:311
xAODRootTest.xAODTestDecor.decor
decor
Definition: xAODRootTest.py:314
xAODRootTest.xAODTestRead
Definition: xAODRootTest.py:183
xAODRootTest.xAODTestDecor.offset
offset
Definition: xAODRootTest.py:313
xAODRootTest.Analysis
Definition: xAODRootTest.py:378
xAODRootTest.xAODTestCopy
Definition: xAODRootTest.py:250
xAODRootTest.Analysis.finalize
def finalize(self)
Definition: xAODRootTest.py:408
xAODRootTest.AllocTestRead.execute
def execute(self, event)
Definition: xAODRootTest.py:368
xAODRootTest.format_int
def format_int(x)
Definition: xAODRootTest.py:49
xAODRootTest.Analysis.algs
algs
Definition: xAODRootTest.py:380
xAODRootTest.format_el
def format_el(x)
Definition: xAODRootTest.py:52
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
xAODRootTest.Analysis.f
f
Definition: xAODRootTest.py:381
xAODRootTest.Analysis.__init__
def __init__(self, ifname, ofname=None)
Definition: xAODRootTest.py:379
xAODRootTest.xAODTestClearDecor.execute
def execute(self, event)
Definition: xAODRootTest.py:354
xAODRootTest.copy_view
def copy_view(event, obj, key)
Definition: xAODRootTest.py:175
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
xAODRootTest.format_str_vec
def format_str_vec(v)
Definition: xAODRootTest.py:60
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
xAODRootTest.xAODTestCopy.readPrefix
readPrefix
Definition: xAODRootTest.py:252
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
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:350
vtune_athena.formatter
formatter
Definition: vtune_athena.py:19
xAODRootTest.format_float
def format_float(x)
Definition: xAODRootTest.py:50
xAODRootTest.xAODInit
def xAODInit()
Definition: xAODRootTest.py:37
xAODRootTest.xAODTestPDecor.__init__
def __init__(self, decorName, offset=0, readPrefix='')
Definition: xAODRootTest.py:333
xAODRootTest.xAODTestDecor.readPrefix
readPrefix
Definition: xAODRootTest.py:312
xAODRootTest.dump_auxitem
def dump_auxitem(x, auxid, f=sys.stdout)
Definition: xAODRootTest.py:95
lumiFormat.fill
fill
Definition: lumiFormat.py:104
xAODRootTest.CHECK
def CHECK(sc)
Definition: xAODRootTest.py:25
xAODRootTest.xAODTestRead.__init__
def __init__(self, readPrefix='')
Definition: xAODRootTest.py:184
xAODRootTest.xAODTestCopy.execute
def execute(self, event)
Definition: xAODRootTest.py:256
xAODRootTest.dump_xaodobj
def dump_xaodobj(h, f=sys.stdout)
Definition: xAODRootTest.py:128
xAODRootTest.copy_obj
def copy_obj(event, obj, key)
Definition: xAODRootTest.py:144
xAODRootTest.Analysis.add
def add(self, alg)
Definition: xAODRootTest.py:391
xAODRootTest.xAODTestDecor
Definition: xAODRootTest.py:310
xAODRootTest.xAODTestCopy.__init__
def __init__(self, readPrefix='', writePrefix=None)
Definition: xAODRootTest.py:251
xAODRootTest.xAODTestPDecor.offset
offset
Definition: xAODRootTest.py:335
xAODRootTest.format_float_vec
def format_float_vec(v)
Definition: xAODRootTest.py:54
xAODRootTest.Analysis.fout
fout
Definition: xAODRootTest.py:385
xAODRootTest.xAODTestRead.readPrefix
readPrefix
Definition: xAODRootTest.py:185
xAODRootTest.copy_vec
def copy_vec(event, obj, key)
Definition: xAODRootTest.py:156
xAODRootTest.Analysis.run
def run(self, n=None)
Definition: xAODRootTest.py:395
xAODRootTest.dump_auxdata
def dump_auxdata(x, exclude=[], f=sys.stdout)
Definition: xAODRootTest.py:107