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