ATLAS Offline Software
EventInfo.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 @brief A basic script to output run number, event number of events in POOL file(s)
7 @author ATLAS Computing Activity
8 @file EventInfo.py
9 '''
10 
11 from AthenaPython.PyAthena import Alg, py_svc, StatusCode
12 
13 # A basic filtering algorithm
14 class PyEventInfo(Alg):
15 
16  # Constructor
17  def __init__(self, name='PyEventInfo', **kwargs):
18  kwargs['name'] = name
19  super().__init__(**kwargs)
20  self.isMC = kwargs.get('isMC', False)
21  self.output = kwargs.get('output')
22  self.prefix = kwargs.get('prefix')
23 
24  # Initialize the algorithm
25  def initialize(self):
26  self.sg = py_svc('StoreGateSvc')
27  from collections import defaultdict
28  self.info = defaultdict(list)
29  return StatusCode.Success
30 
31  # Finalize the algorithm
32  def finalize(self):
33  if not self.isMC:
34  key_name = 'run_number'
35  else:
36  key_name = 'mc_channel_number'
37  for run, event in zip(self.info[key_name], self.info['event_number'], strict=True):
38  # Changed in version 3.10: Added the strict argument.
39  print(f"{'' if self.prefix is None else self.prefix}{run:d} {event:d}",
40  file=self.output)
41  return StatusCode.Success
42 
43  # Execute the algorithm
44  def execute(self):
45 
46  # Read the run/event number from xAOD::EventInfo
47  if self.sg.contains('xAOD::EventInfo', 'EventInfo'):
48  ei = self.sg.retrieve('xAOD::EventInfo', 'EventInfo')
49  runNumber = ei.runNumber()
50  eventNumber = ei.eventNumber()
51  mcChannelNumber = ei.mcChannelNumber()
52 
53  self.info['run_number'].append(runNumber)
54  self.info['mc_channel_number'].append(mcChannelNumber)
55  self.info['event_number'].append(eventNumber)
56 
57  # Let's happily move to the next event
58  return StatusCode.Success
59 
60  # If we made it thus far something went wrong
61  return StatusCode.Failure
62 
63 # Main executable
64 if __name__ == "__main__":
65 
66  # Parse user input
67  import argparse
68  parser = argparse.ArgumentParser(description='Output run number (mc channel number in case of Monte Carlo),'
69  ' event number of events in POOL file(s)')
70  parser.add_argument('--inputFiles', required=True,
71  help='Input POOL file(s) separated with commas')
72  parser.add_argument('--outputFile',
73  help='Output text file containing <run number> <event number> record(s) (one per line);'
74  ' if is -, write output on standard output (default: -)')
75  parser.add_argument('--prefix',
76  help='Prefix to print in front of each line of output')
77  args = parser.parse_args()
78 
79  import sys
80  if args.outputFile is None or args.outputFile == "-":
81  output = sys.stdout
82  opened = False
83  else:
84  output = open(args.outputFile, 'w', encoding="utf-8")
85  opened = True
86 
87  # Setup configuration logging
88  from AthenaCommon.Logging import logging
89  log = logging.getLogger('EventInfo')
90  log.setLevel(logging.ERROR)
91 
92  log.info('== Listing EventInfo for events from POOL files (the CA Configuration)')
93 
94  # Set the configuration flags
95  log.info('== Setting ConfigFlags')
96  from AthenaConfiguration.AllConfigFlags import initConfigFlags
97  flags = initConfigFlags()
98  flags.Input.Files = args.inputFiles.split(',')
99 
100  # Lock and dump the configuration flags
101  flags.lock()
102  log.info('== ConfigFlags Locked')
103 
104  # Setup the main services
105  log.info('== Configuring Main Services')
106  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
107  cfg = MainServicesCfg(flags)
108 
109  # Setup the input reading
110  log.info('== Configuring Input Reading')
111  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
112  cfg.merge(PoolReadCfg(flags))
113 
114  # Setup event listing
115  cfg.addEventAlgo(PyEventInfo('EventInfoAlg', isMC=flags.Input.isMC, output=output,
116  prefix=args.prefix),
117  sequenceName='AthAlgSeq')
118 
119  for item in flags.Input.TypedCollections:
120  ctype, cname = item.split('#')
121  if ctype.startswith('Trk') or ctype.startswith('InDet'):
122  from TrkEventCnvTools.TrkEventCnvToolsConfig import TrkEventCnvSuperToolCfg
123  cfg.merge(TrkEventCnvSuperToolCfg(flags))
124  if ctype.startswith('Calo') or ctype.startswith('LAr'):
125  from LArGeoAlgsNV.LArGMConfig import LArGMCfg
126  cfg.merge(LArGMCfg(flags))
127  if ctype.startswith('Calo') or ctype.startswith('Tile'):
128  from TileGeoModel.TileGMConfig import TileGMCfg
129  cfg.merge(TileGMCfg(flags))
130  if ctype.startswith('Muon'):
131  from MuonConfig.MuonGeometryConfig import MuonGeoModelCfg
132  cfg.merge(MuonGeoModelCfg(flags))
133 
134  # Now run the job
135  log.info('== Running...')
136  sc = cfg.run()
137 
138  if opened: output.close()
139 
140  # Exit accordingly
141  sys.exit(not sc.isSuccess())
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
EventInfo.PyEventInfo
Definition: EventInfo.py:14
EventInfo.PyEventInfo.prefix
prefix
Definition: EventInfo.py:22
EventInfo.PyEventInfo.isMC
isMC
Definition: EventInfo.py:20
EventInfo.PyEventInfo.execute
def execute(self)
Definition: EventInfo.py:44
EventInfo.PyEventInfo.info
info
Definition: EventInfo.py:28
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
EventInfo.PyEventInfo.sg
sg
Definition: EventInfo.py:26
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
EventInfo.PyEventInfo.finalize
def finalize(self)
Definition: EventInfo.py:32
MuonGeometryConfig.MuonGeoModelCfg
def MuonGeoModelCfg(flags)
Definition: MuonGeometryConfig.py:28
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:310
EventInfo.PyEventInfo.__init__
def __init__(self, name='PyEventInfo', **kwargs)
Definition: EventInfo.py:17
EventInfo.PyEventInfo.initialize
def initialize(self)
Definition: EventInfo.py:25
LArGMConfig.LArGMCfg
def LArGMCfg(flags)
Definition: LArGMConfig.py:8
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
Trk::open
@ open
Definition: BinningType.h:40
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
EventInfo.PyEventInfo.output
output
Definition: EventInfo.py:21
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:71
TileGMConfig.TileGMCfg
def TileGMCfg(flags)
Definition: TileGMConfig.py:7