ATLAS Offline Software
readDataHeader.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4 
5 from AthenaPython.PyAthena import Alg, py_svc, StatusCode
6 from AthenaCommon.Logging import logging
7 
8 # A basic algorithm that reads the DataHeader to print some information
9 class ReadDataHeaderAlg(Alg):
10 
11  def __init__(self,name='ReadDataHeaderAlg', totalEvents=0):
12  super(ReadDataHeaderAlg,self).__init__(name=name)
13  self.totalEvents = totalEvents
14  self.nevt = 0
15  return
16 
17  def initialize(self):
18  self.sg = py_svc('StoreGateSvc')
19  return StatusCode.Success
20 
21  def execute(self):
22  if self.sg.contains('DataHeader', 'EventSelector'):
23  dh = self.sg.retrieve('DataHeader', 'EventSelector')
24  sz = dh.size()
25  self.nevt += 1
26  if self.nevt % 100 == 0:
27  logging.info(f'DataHeader Size {sz} in event #{self.nevt}')
28  if sz > 0:
29  it = dh.begin()
30  for idx in range(sz):
31  logging.debug(f' >> Key {it.getKey()} : Token {it.getToken().toString()}')
32  it+=1
33  else:
34  logging.debug(' >> Successfully read through the DataHeader')
35  else:
36  logging.error(f' >> Negative DataHeader size ({sz})!')
37  return StatusCode.Failure
38  else:
39  logging.error('Could NOT find the DataHeader!')
40  return StatusCode.Failure
41 
42  return StatusCode.Success
43 
44  def finalize(self):
45  # Now this hack is needed because some errors, e.g. those in loading proxies,
46  # don't cause a job failure because most loop managers seem to ignore them.
47  # The problem is that even though we fail here, the job still "succeeds"
48  if self.nevt != self.totalEvents:
49  logging.error(f'Expected {self.totalEvents} events but processed {self.nevt}!')
50  logging.error('This most likely means we could not process some events successfully!')
51  return StatusCode.Failure
52 
53  return StatusCode.Success
54 
55 # A minimal job that reads the DataHeader
56 if __name__ == '__main__':
57  """
58  Example usage: readDataHeader.py --filesInput=DAOD_PHYS.pool.root --evtMax=10 --loglevel=DEBUG
59  """
60 
61  # Import the common flags/services
62  from AthenaConfiguration.AllConfigFlags import initConfigFlags
63  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
64 
65  # Set the necessary configuration flags and lock them
66  # This is filled from the command line arguments as shown above
67  flags = initConfigFlags()
68  flags.fillFromArgs()
69  flags.lock()
70 
71  # Figure out how many events we expect
72  if flags.Exec.MaxEvents < 0:
73  totalEvents = 0
74  from AthenaConfiguration.AutoConfigFlags import GetFileMD
75  for filename in flags.Input.Files:
76  totalEvents += GetFileMD(filename).get('nentries', 0)
77  else:
78  totalEvents = flags.Exec.MaxEvents
79 
80  # Set up the configuration and add the relevant services
81  cfg = MainServicesCfg(flags)
82 
83  # Input reading
84  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
85  cfg.merge(PoolReadCfg(flags))
86 
87  # Schedule the Custom Algorithm
88  cfg.addEventAlgo(ReadDataHeaderAlg('ReadDataHeaderAlg', totalEvents=totalEvents), sequenceName = 'AthAllAlgSeq')
89 
90  # Now run the job and exit accordingly
91  sc = cfg.run()
92  import sys
93  sys.exit(not sc.isSuccess())
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
readDataHeader.ReadDataHeaderAlg.initialize
def initialize(self)
Definition: readDataHeader.py:17
readDataHeader.ReadDataHeaderAlg.execute
def execute(self)
Definition: readDataHeader.py:21
python.AutoConfigFlags.GetFileMD
def GetFileMD(filenames, allowEmpty=True)
Definition: AutoConfigFlags.py:51
python.Bindings.py_svc
def py_svc(svcName, createIf=True, iface=None)
Definition: Control/AthenaPython/python/Bindings.py:98
readDataHeader.ReadDataHeaderAlg.sg
sg
Definition: readDataHeader.py:18
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:260
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
readDataHeader.ReadDataHeaderAlg.finalize
def finalize(self)
Definition: readDataHeader.py:44
readDataHeader.ReadDataHeaderAlg.__init__
def __init__(self, name='ReadDataHeaderAlg', totalEvents=0)
Definition: readDataHeader.py:11
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
readDataHeader.ReadDataHeaderAlg
Definition: readDataHeader.py:9
readDataHeader.ReadDataHeaderAlg.totalEvents
totalEvents
Definition: readDataHeader.py:13
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
readDataHeader.ReadDataHeaderAlg.nevt
nevt
Definition: readDataHeader.py:14