ATLAS Offline Software
DumperConfig.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 import os
3 import subprocess
4 from PyDumper.Dumpers import get_dumper_fct
5 from AthenaPython import PyAthena
6 from PyUtils.fprint import fprintln
7 
8 if ('ATLAS_REFERENCE_TAG' not in globals() and
9  'ATLAS_REFERENCE_TAG' in os.environ):
10  ATLAS_REFERENCE_TAG = os.environ['ATLAS_REFERENCE_TAG']
11 
12 def find_file (fname,refPaths):
13  for p in refPaths:
14  if p:
15  path = os.path.join (p, fname)
16  if os.path.exists (path):
17  return path
18  print ('ERROR: Cannot find file: ', fname)
19  return None
20 
21 
22 #
23 # If a new xAOD variable appears, print a warning, but don't treat
24 # it as a failure.
25 #
26 def checknewvars (output):
27  names = set()
28  for l in output.split ('\n'):
29  if not l: continue
30  if l.startswith ('+++') or l.startswith ('---'): continue
31  if l[0] == '-': return None
32  if l[0] == '+':
33  pos = l.find (':')
34  if l.startswith ('+ ') and pos > 0:
35  names.add (l[6:pos])
36  else:
37  return None
38  l = list(names)
39  l.sort()
40  return l
41 
42 
44  def __init__ (self, name, inputFile, Keys, refpaths):
45  PyAthena.Alg.__init__ (self, name)
46  self.infile = inputFile
47  self.keys = Keys
48  self.refpaths = refpaths
49  return
50 
51  def initialize (self):
52  self.sg = PyAthena.py_svc ('StoreGateSvc')
53  self.ofile_name = os.path.basename (self.infile) + '.dump'
54  refbase = os.path.basename (self.infile) + '.ref'
55  self.reffile_name = '../share/' + refbase
56  if not os.path.exists (self.reffile_name):
57  self.reffile_name = '../' + self.reffile_name
58 
59  if not os.path.exists (self.reffile_name) and 'ATLAS_REFERENCE_TAG' in globals():
60  from AthenaCommon.Utils.unixtools import find_datafile
61  r = find_datafile (ATLAS_REFERENCE_TAG)
62  if r:
63  self.reffile_name = os.path.join (r, ATLAS_REFERENCE_TAG,
64  refbase)
65 
66  if not os.path.exists (self.reffile_name) and 'ATLAS_REFERENCE_TAG' in globals():
67  self.reffile_name = find_file (os.path.join (ATLAS_REFERENCE_TAG,
68  refbase), self.refpaths)
69 
70  self.ofile = open (self.ofile_name, 'w')
71  self.icount = 0
72  return 1
73 
74  def finalize (self):
75  self.ofile.close()
76  ret, output = subprocess.getstatusoutput ('diff -u %s %s' % (self.reffile_name, self.ofile_name))
77  if ret != 0:
78  newvars = checknewvars (output)
79  if newvars:
80  print ('WARNING: new xAOD variables ', newvars)
81  else:
82  print ('ERROR running diff with reference')
83  print (output)
84  return 1
85 
86  def execute (self):
87  fprintln (self.ofile, 'Event index', self.icount)
88  self.icount += 1
89  for k in self.keys:
90  nmax = None
91  apos = k.find ('@')
92  if apos >= 0:
93  nmax = int (k[apos+1:])
94  k = k[:apos]
95  fprintln (self.ofile, '-->', k)
96 
97  store = self.sg
98  spos = k.find ('/')
99  if spos >= 0:
100  store = PyAthena.py_svc (k[:spos])
101  k = k[spos+1:]
102 
103  spos = k.find ('#')
104  if spos >= 0:
105  (typ, k) = k.split ('#')
106  o = store.retrieve (typ, k)
107  else:
108  o = store[k]
109  dumper = get_dumper_fct (type (o), self.ofile, nmax = nmax)
110  dumper (o)
111  fprintln (self.ofile, '\n')
112  return 1
python.DumperConfig.Dumper.ofile
ofile
Definition: DumperConfig.py:70
python.DumperConfig.Dumper.ofile_name
ofile_name
Definition: DumperConfig.py:53
python.DumperConfig.checknewvars
def checknewvars(output)
Definition: DumperConfig.py:26
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
python.DumperConfig.Dumper.__init__
def __init__(self, name, inputFile, Keys, refpaths)
Definition: DumperConfig.py:44
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
python.DumperConfig.Dumper
Definition: DumperConfig.py:43
python.DumperConfig.Dumper.icount
icount
Definition: DumperConfig.py:71
python.DumperConfig.Dumper.refpaths
refpaths
Definition: DumperConfig.py:48
python.DumperConfig.Dumper.infile
infile
Definition: DumperConfig.py:46
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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
python.DumperConfig.find_file
def find_file(fname, refPaths)
Definition: DumperConfig.py:12
python.DumperConfig.Dumper.reffile_name
reffile_name
Definition: DumperConfig.py:55
python.DumperConfig.Dumper.keys
keys
Definition: DumperConfig.py:47
python.DumperConfig.Dumper.sg
sg
Definition: DumperConfig.py:52
PyAthena::Alg
Definition: PyAthenaAlg.h:33