ATLAS Offline Software
Loading...
Searching...
No Matches
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
11from AthenaPython.PyAthena import Alg, py_svc, StatusCode
12
13# A basic filtering algorithm
14class 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
64if __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())
void print(char *figname, TCanvas *c1)
__init__(self, name='PyEventInfo', **kwargs)
Definition EventInfo.py:17
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
void initialize()