ATLAS Offline Software
Loading...
Searching...
No Matches
EventGUIDLookupAlgConfig.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3"""Algorithm + CA config for EventGUIDLookup_Skeleton.py."""
4
5from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
6from AthenaPython import PyAthena
7from AthenaPython.PyAthena import StatusCode
8from GaudiKernel.Constants import INFO
9
10
12
13 def __init__(self, name='EventGUIDLookupAlg', **kwargs):
14 self.dataType = kwargs.pop('dataType')
15 self.inputDataType = kwargs.pop('inputDataType')
16 self.requestedEvents = kwargs.pop('requestedEvents', set())
17 self.outputFile = kwargs.pop('outputFile', 'eventGUIDLookup.txt')
18 super(EventGUIDLookupAlg, self).__init__(name=name, **kwargs)
19 self._found = set()
20 self._outFH = None
21
22 def initialize(self):
23 return StatusCode.Success
24
25 def _matchRun(self, ctx_run, evt):
26 """Return the run-like number used for matching and output:
27 the MC channel number (DSID) for simulation, the run number for data.
28 """
29 try:
30 ei = self.evtStore['EventInfo']
31 if ei.eventType(ei.IS_SIMULATION): # isMC
32 dsid = ei.mcChannelNumber()
33 if dsid != 0:
34 return dsid
35 # Some old/private productions have no channel number set
36 self.msg.debug('run=%d event=%d: MC with mcChannelNumber=0; '
37 'using run number', ctx_run, evt)
38 except Exception as e:
39 self.msg.warning('run=%d event=%d: cannot read EventInfo (%s); '
40 'using run number', ctx_run, evt, e)
41 return ctx_run
42
43 def execute(self):
44 from EventGUIDLookup.EventGUIDLookupLib import resolveProvenanceGuid
45
46 # Event number and (data) run number come from the EventContext,
47 # which is format-agnostic. For MC the run number is replaced by
48 # the MC channel number (DSID) read from EventInfo.
49
50 eid = self.getContext().eventID()
51 ctx_run, evt = eid.run_number(), eid.event_number()
52
53 run = self._matchRun(ctx_run, evt) # DSID for MC, run number for data
54 key = (run, evt)
55
56 if key in self.requestedEvents and key not in self._found:
57 guid = resolveProvenanceGuid(self.evtStore, self.dataType,
58 self.inputDataType)
59 if guid is not None:
60 if self._outFH is None:
61 self._outFH = open(self.outputFile, 'w')
62 self._outFH.write('%d %d %s\n' % (run, evt, guid))
63 self._outFH.flush()
64 self._found.add(key)
65 self.msg.info('Found GUID for run/dsid %d event %d dataType %s', run, evt, self.dataType)
66 else:
67 self.msg.warning('run/dsid=%d event=%d has no %s provenance '
68 'reference', run, evt, self.dataType)
69 return StatusCode.Success
70 def finalize(self):
71 if self._outFH:
72 self._outFH.close()
73 missing = self.requestedEvents - self._found
74 if missing:
75 self.msg.warning('%d of %d requested events had no %s GUID',
76 len(missing), len(self.requestedEvents),
77 self.dataType)
78 for run, evt in sorted(missing):
79 self.msg.debug(' missing: run=%d event=%d', run, evt)
80 else:
81 self.msg.info('Resolved GUIDs for all %d requested events',
82 len(self.requestedEvents))
83 return StatusCode.Success
84
85
86def EventGUIDLookupAlgCfg(flags, dataType, inputDataType, requestedEvents,
87 outputFile, name='EventGUIDLookupAlg', **kwargs):
88 acc = ComponentAccumulator()
89 acc.addEventAlgo(EventGUIDLookupAlg(
90 name=name,
91 dataType=dataType,
92 inputDataType=inputDataType,
93 requestedEvents=requestedEvents,
94 outputFile=outputFile,
95 OutputLevel=kwargs.pop('OutputLevel', INFO)))
96 return acc
const bool debug
__init__(self, name='EventGUIDLookupAlg', **kwargs)
virtual StatusCode finalize() override
virtual StatusCode initialize() override
STL class.
bool add(const std::string &hname, TKey *tobj)
Definition fastadd.cxx:55
EventGUIDLookupAlgCfg(flags, dataType, inputDataType, requestedEvents, outputFile, name='EventGUIDLookupAlg', **kwargs)