ATLAS Offline Software
AODEventPicking.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 AODEventPicking.py
7 @author Alaettin Serhan Mete
8 @brief A basic script for picking events from AOD files
9 '''
10 
11 from AthenaPython.PyAthena import Alg, py_svc, StatusCode
12 
13 # A basic filtering algorithm
14 class PyxAODEvtFilter(Alg):
15 
16  # Constructor
17  def __init__(self, name = 'PyxAODEvtFilter', **kw):
18  kw['name'] = name
19  super(PyxAODEvtFilter, self).__init__(**kw)
20  self.evtList = kw.get('evtList', None)
21  self.isMC = kw.get('isMC', False)
22  return
23 
24  # Initialize the algorithm
25  def initialize(self):
26  self.sg = py_svc('StoreGateSvc')
27  return StatusCode.Success
28 
29  # Execute the algorithm
30  def execute(self):
31 
32  # Read the run/event number from xAOD::EventInfo
33  if self.sg.contains('xAOD::EventInfo', 'EventInfo'):
34  ei = self.sg.retrieve('xAOD::EventInfo', 'EventInfo')
35  if not self.isMC:
36  runNumber = ei.runNumber()
37  else:
38  runNumber = ei.mcChannelNumber()
39  eventNumber = ei.eventNumber()
40 
41  # Check to see if we should accept or reject the event
42  if (runNumber, eventNumber) in self.evtList:
43  self.setFilterPassed(True)
44  else:
45  self.setFilterPassed(False)
46 
47  # Let's happily move to the next event
48  return StatusCode.Success
49 
50  # If we made it thus far something went wrong
51  return StatusCode.Failure
52 
53 # Main executable
54 if '__main__' in __name__:
55 
56  # Parse user input
57  import argparse
58  parser = argparse.ArgumentParser(description='Select events identified by run number (mc channel number'\
59  ' in case of Monte Carlo), event number from the input AOD file(s)')
60  parser.add_argument('--inputAODFiles', required=True,
61  help='Input AOD file(s) separated with commas')
62  parser.add_argument('--outputAODFile', required=True,
63  help='Output AOD file')
64  parser.add_argument('--eventList', required=True,
65  help='Text file containing <run number> <event number> [<guid>] record(s) (one per line)')
66  args, _ = parser.parse_known_args()
67 
68  # Setup configuration logging
69  from AthenaCommon.Logging import logging
70  log = logging.getLogger('AODEventPicking')
71  log.info('== Picking events from AOD files w/ the CA Configuration')
72 
73  # Parse input file that contains run_number, event_number[, guid] combinations
74  evtList = []
75  with open(args.eventList) as f:
76  for line in f:
77  run, evt, *guid = line.rstrip().split()
78  evtList.append((int(run), int(evt)))
79 
80  # Set the configuration flags
81  log.info('== Setting ConfigFlags')
82  from AthenaConfiguration.AllConfigFlags import initConfigFlags
83  flags = initConfigFlags()
84  flags.Input.Files = args.inputAODFiles.split(',')
85  flags.Output.AODFileName = args.outputAODFile
86  flags.PerfMon.doFastMonMT = True
87  flags.PerfMon.OutputJSON = 'perfmonmt_AODEventPicking.json'
88 
89  # Lock and dump the configuration flags
90  flags.lock()
91  log.info('== ConfigFlags Locked')
92 
93  # Setup the main services
94  log.info('== Configuring Main Services')
95  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
96  cfg = MainServicesCfg(flags)
97 
98  # Setup the input reading
99  log.info('== Configuring Input Reading')
100  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
101  cfg.merge(PoolReadCfg(flags))
102 
103  # Setup event picking
104  cfg.addEventAlgo(PyxAODEvtFilter('EventFilterAlg', evtList = evtList, isMC = flags.Input.isMC), sequenceName = 'AthAlgSeq')
105 
106  # Configure the output stream
107  log.info('== Configuring Output Stream')
108  from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg, outputStreamName
109  cfg.merge(OutputStreamCfg(flags, 'AOD', takeItemsFromInput=True))
110 
111  # Configure metadata
112  log.info('== Configuring metadata for the output stream')
113  from xAODMetaDataCnv.InfileMetaDataConfig import SetupMetaDataForStreamCfg
114  cfg.merge(SetupMetaDataForStreamCfg(flags, 'AOD'))
115 
116  # Setup the output stream algorithm
117  StreamAOD = cfg.getEventAlgo(outputStreamName("AOD"))
118  StreamAOD.ForceRead = True
119  StreamAOD.AcceptAlgs += ['EventFilterAlg']
120 
121  # For (un)packing Cell Containers
122  from LArGeoAlgsNV.LArGMConfig import LArGMCfg
123  cfg.merge(LArGMCfg(flags))
124  from TileGeoModel.TileGMConfig import TileGMCfg
125  cfg.merge(TileGMCfg(flags))
126 
127  # For being able to read pre Run-3 data w/ Trk objects
128  from TrkEventCnvTools.TrkEventCnvToolsConfig import TrkEventCnvSuperToolCfg
129  cfg.merge(TrkEventCnvSuperToolCfg(flags))
130 
131  # Setup PerfMon
132  log.info('== Configuring PerfMon')
133  from PerfMonComps.PerfMonCompsConfig import PerfMonMTSvcCfg
134  cfg.merge(PerfMonMTSvcCfg(flags))
135 
136  # Now run the job
137  log.info('== Running...')
138  sc = cfg.run()
139 
140  # Exit accordingly
141  import sys
142  sys.exit(not sc.isSuccess())
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
AthenaPoolExample_WriteCond.outputStreamName
string outputStreamName
Definition: AthenaPoolExample_WriteCond.py:21
python.OutputStreamConfig.OutputStreamCfg
def OutputStreamCfg(flags, streamName, ItemList=[], MetadataItemList=[], disableEventTag=False, trigNavThinningSvc=None, takeItemsFromInput=False, extendProvenanceRecord=True, AcceptAlgs=[], HelperTools=[])
Definition: OutputStreamConfig.py:12
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
AODEventPicking.PyxAODEvtFilter.evtList
evtList
Definition: AODEventPicking.py:20
python.PerfMonCompsConfig.PerfMonMTSvcCfg
def PerfMonMTSvcCfg(flags, **kwargs)
A minimal new-style configuration for PerfMonMTSvc.
Definition: PerfMonCompsConfig.py:10
AODEventPicking.PyxAODEvtFilter.sg
sg
Definition: AODEventPicking.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
AODEventPicking.PyxAODEvtFilter.execute
def execute(self)
Definition: AODEventPicking.py:30
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
AODEventPicking.PyxAODEvtFilter.__init__
def __init__(self, name='PyxAODEvtFilter', **kw)
Definition: AODEventPicking.py:17
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:260
LArGMConfig.LArGMCfg
def LArGMCfg(flags)
Definition: LArGMConfig.py:8
AODEventPicking.PyxAODEvtFilter.isMC
isMC
Definition: AODEventPicking.py:21
AODEventPicking.PyxAODEvtFilter.initialize
def initialize(self)
Definition: AODEventPicking.py:25
Trk::open
@ open
Definition: BinningType.h:40
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
InfileMetaDataConfig.SetupMetaDataForStreamCfg
def SetupMetaDataForStreamCfg(flags, streamName="", AcceptAlgs=None, createMetadata=None, propagateMetadataFromInput=True, *args, **kwargs)
Definition: InfileMetaDataConfig.py:222
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
AODEventPicking.PyxAODEvtFilter
Definition: AODEventPicking.py:14
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
TileGMConfig.TileGMCfg
def TileGMCfg(flags)
Definition: TileGMConfig.py:7