ATLAS Offline Software
Loading...
Searching...
No Matches
DumperConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2import os
3import subprocess
4from PyDumper.Dumpers import get_dumper_fct
5from AthenaPython import PyAthena
6from PyUtils.fprint import fprintln
7
8if ('ATLAS_REFERENCE_TAG' not in globals() and
9 'ATLAS_REFERENCE_TAG' in os.environ):
10 ATLAS_REFERENCE_TAG = os.environ['ATLAS_REFERENCE_TAG']
11
12def 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#
26def 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
virtual StatusCode execute() override
virtual StatusCode finalize() override
virtual StatusCode initialize() override
__init__(self, name, inputFile, Keys, refpaths)
STL class.
find_file(fname, refPaths)