ATLAS Offline Software
AodEventInfo.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 '''
6 @file AodEventInfo.py
7 @brief A basic script to output run number, event number of events in the input AOD file(s)
8 '''
9 
10 from AthenaPython.PyAthena import Alg, py_svc, StatusCode
11 
12 # A basic filtering algorithm
13 class PyxAODEventInfo(Alg):
14 
15  # Constructor
16  def __init__(self, name = 'PyxAODEventInfo', **kwds):
17  kwds['name'] = name
18  super().__init__(**kwds)
19  self.isMC = kwds.get('isMC', False)
20  self.prefix = kwds.get('prefix')
21 
22  # Initialize the algorithm
23  def initialize(self):
24  self.sg = py_svc('StoreGateSvc')
25  from collections import defaultdict
26  self.info = defaultdict(list)
27  return StatusCode.Success
28 
29  # Finalize the algorithm
30  def finalize(self):
31  if not self.isMC:
32  key_name = 'run_number'
33  else:
34  key_name = 'mc_channel_number'
35  for run, event in zip(self.info[key_name], self.info['event_number']): #, strict=True): Changed in version 3.10: Added the strict argument.
36  print(f"{'' if self.prefix is None else self.prefix}{run:d} {event:d}")
37  return StatusCode.Success
38 
39  # Execute the algorithm
40  def execute(self):
41 
42  # Read the run/event number from xAOD::EventInfo
43  if self.sg.contains('xAOD::EventInfo', 'EventInfo'):
44  ei = self.sg.retrieve('xAOD::EventInfo', 'EventInfo')
45  runNumber = ei.runNumber()
46  eventNumber = ei.eventNumber()
47  mcChannelNumber = ei.mcChannelNumber()
48 
49  self.info['run_number'].append(runNumber)
50  self.info['mc_channel_number'].append(mcChannelNumber)
51  self.info['event_number'].append(eventNumber)
52 
53  # Let's happily move to the next event
54  return StatusCode.Success
55 
56  # If we made it thus far something went wrong
57  return StatusCode.Failure
58 
59 # Main executable
60 if '__main__' in __name__:
61 
62  # Parse user input
63  import argparse
64  parser = argparse.ArgumentParser(description='Output run number (mc channel number in case of Monte Carlo),'\
65  ' event number of events in the input AOD file(s)')
66  parser.add_argument('--inputAODFiles', required=True,
67  help='Input AOD file(s) separated with commas')
68  parser.add_argument('--prefix',
69  help='Prefix to print in front of each line of output')
70  args = parser.parse_args()
71 
72  # Setup configuration logging
73  from AthenaCommon.Logging import logging
74  log = logging.getLogger('AodEventInfo')
75  log.info('== Listing EventInfo for events from AOD files (the CA Configuration)')
76 
77  # Set the configuration flags
78  log.info('== Setting ConfigFlags')
79  from AthenaConfiguration.AllConfigFlags import initConfigFlags
80  flags = initConfigFlags()
81  flags.Input.Files = args.inputAODFiles.split(',')
82 
83  # Lock and dump the configuration flags
84  flags.lock()
85  log.info('== ConfigFlags Locked')
86 
87  # Setup the main services
88  log.info('== Configuring Main Services')
89  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
90  cfg = MainServicesCfg(flags)
91 
92  # Setup the input reading
93  log.info('== Configuring Input Reading')
94  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
95  cfg.merge(PoolReadCfg(flags))
96 
97  # Setup event picking
98  cfg.addEventAlgo(PyxAODEventInfo('xAodEventInfoAlg', isMC = flags.Input.isMC, prefix = args.prefix),
99  sequenceName = 'AthAlgSeq')
100 
101  # For (un)packing Cell Containers
102  from LArGeoAlgsNV.LArGMConfig import LArGMCfg
103  cfg.merge(LArGMCfg(flags))
104  from TileGeoModel.TileGMConfig import TileGMCfg
105  cfg.merge(TileGMCfg(flags))
106 
107  # For being able to read pre Run-3 data w/ Trk objects
108  from TrkEventCnvTools.TrkEventCnvToolsConfig import TrkEventCnvSuperToolCfg
109  cfg.merge(TrkEventCnvSuperToolCfg(flags))
110 
111  # Now run the job
112  log.info('== Running...')
113  sc = cfg.run()
114 
115  # Exit accordingly
116  import sys
117  sys.exit(not sc.isSuccess())
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
AodEventInfo.PyxAODEventInfo.info
info
Definition: AodEventInfo.py:26
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
AodEventInfo.PyxAODEventInfo.finalize
def finalize(self)
Definition: AodEventInfo.py:30
AodEventInfo.PyxAODEventInfo.initialize
def initialize(self)
Definition: AodEventInfo.py:23
python.TrkEventCnvToolsConfig.TrkEventCnvSuperToolCfg
def TrkEventCnvSuperToolCfg(flags, name='EventCnvSuperTool', **kwargs)
Definition: TrkEventCnvToolsConfig.py:51
python.Bindings.py_svc
def py_svc(svcName, createIf=True, iface=None)
Definition: Control/AthenaPython/python/Bindings.py:98
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:259
AodEventInfo.PyxAODEventInfo.execute
def execute(self)
Definition: AodEventInfo.py:40
AodEventInfo.PyxAODEventInfo.prefix
prefix
Definition: AodEventInfo.py:20
LArGMConfig.LArGMCfg
def LArGMCfg(flags)
Definition: LArGMConfig.py:8
AodEventInfo.PyxAODEventInfo
Definition: AodEventInfo.py:13
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
AodEventInfo.PyxAODEventInfo.__init__
def __init__(self, name='PyxAODEventInfo', **kwds)
Definition: AodEventInfo.py:16
AodEventInfo.PyxAODEventInfo.isMC
isMC
Definition: AodEventInfo.py:19
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
AodEventInfo.PyxAODEventInfo.sg
sg
Definition: AodEventInfo.py:24
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
TileGMConfig.TileGMCfg
def TileGMCfg(flags)
Definition: TileGMConfig.py:7