ATLAS Offline Software
CountHepMC.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef XAOD_ANALYSIS
6 
8 #include "GaudiKernel/IEventProcessor.h"
9 #include "GaudiKernel/IAlgManager.h"
10 #include "GaudiKernel/IOpaqueAddress.h"
11 #include "GaudiKernel/IProperty.h"
12 #include "GaudiKernel/ClassID.h"
13 #include "GaudiKernel/IClassIDSvc.h"
15 #include "EventInfo/EventInfo.h"
16 #include "EventInfo/EventID.h"
17 #include "EventInfo/EventType.h"
23 #include <cmath>
24 #include <cassert>
25 #include <string>
26 
27 CountHepMC::CountHepMC(const std::string& name, ISvcLocator* pSvcLocator) :
28  GenBase(name, pSvcLocator)
29 {
30  declareProperty("RequestedOutput", m_nCount=5000);
31  declareProperty("FirstEvent", m_firstEv=1);
32  declareProperty("CorrectHepMC", m_corHepMC=false);
33  declareProperty("CorrectEventID", m_corEvtID=false);
34  declareProperty("CorrectRunNumber", m_corRunNumber=false);
35  declareProperty("NewRunNumber", m_newRunNumber=999999);
36  declareProperty("CopyRunNumber", m_copyRunNumber=true);
37  declareProperty("inputKeyName", m_inputKeyName = "GEN_EVENT");
38 }
39 
41 {
43 
45 
46  if(m_corEvtID || m_corRunNumber) {
49  if(!m_mcWeightsKey.empty()) ATH_CHECK(m_mcWeightsKey.initialize());
50  }
51 
52  return StatusCode::SUCCESS;
53 }
54 
56 
58  m_nPass++;
59  ATH_MSG_DEBUG("Current count = " << m_nPass);
60  ATH_MSG_INFO("Options for HepMC event number, EvtID event number, EvtID run number = " << m_corHepMC << m_corEvtID << m_corRunNumber );
61  // Fix the event number
62  long long int newnum = m_nPass + m_firstEv - 1;
63  if (newnum<=0){
64  ATH_MSG_ERROR("Event number must be positive-definite; " << newnum << " is not allowed");
65  return StatusCode::FAILURE;
66  }
67 
68 #ifndef HEPMC3
69  constexpr long long int max32 = std::pow(2, 31) - 1;
71  if (newnum >= max32) {
72  ATH_MSG_ERROR("Event number " << newnum << " exceeds 32bit limit. In HepMC2 it is not allowed.");
73  return StatusCode::FAILURE;
74  }
75 #else
76 // Temporary solution to suppress the Warnings from HepMC3 printed for every event.
77 // Will be removed when generators authors fix the way they fill the x-section or when we switch to a new HepMC3 version , where the printout is limited.
78  if (m_nPass == 100) {
79  ATH_MSG_INFO("After " << m_nPass << " events we switch off HepMC3 warnings to avoid blowing up logs.");
80  HepMC3::Setup::set_print_warnings(false);
81  }
82 #endif
83 
84  if (m_corHepMC) {
85  std::string key = m_inputKeyName;
86  // retrieve event from Transient Store (Storegate)
87  const McEventCollection* oldmcEvtColl=0;
88  if (evtStore()->retrieve(oldmcEvtColl, key).isSuccess()){
89  McEventCollection* newmcEvtColl = new McEventCollection(*oldmcEvtColl);
90  McEventCollection::iterator evt = newmcEvtColl->begin();
91  HepMC::GenEvent* hepMC = *evt;
92  HepMC::set_ll_event_number(hepMC, newnum);
93  CHECK(evtStore()->overwrite( newmcEvtColl, key));
94  }
95  else{
96  ATH_MSG_ERROR("No McEventCollection object found");
97  return StatusCode::SUCCESS;
98  }
99  }
100 
101  xAOD::EventInfo* outputEvtInfo{nullptr};
102  int inpRunNumber{-1};
106  ATH_CHECK(outputEvtInfoHandle.record(std::make_unique<xAOD::EventInfo>(), std::make_unique<xAOD::EventAuxInfo>()));
107 
108  outputEvtInfo = outputEvtInfoHandle.ptr();
109  *outputEvtInfo = *inputEvtInfoHandle;
110  inpRunNumber = inputEvtInfoHandle->runNumber();
111  if(!m_mcWeightsKey.empty()) {
113  outputEvtInfo->setMCEventWeights(mcWeights(0));
114  }
115  }
116 
117  if (m_corEvtID) {
118  // Change the EventID in the eventinfo header
119  const EventInfo* pInputEvt(nullptr);
120  if (evtStore()->retrieve(pInputEvt).isSuccess()) {
121  assert(pInputEvt);
122  EventID* eventID = const_cast<EventID*>(pInputEvt->event_ID());
123  eventID->set_event_number(newnum);
124  ATH_MSG_DEBUG("Set new event number in event_ID");
125  } else {
126  ATH_MSG_ERROR("No EventInfo object found");
127  return StatusCode::SUCCESS;
128  }
129 
130  outputEvtInfo->setEventNumber(newnum);
131  }
132 
133 
134  // Copy generation run number into mc channel number
135  if (m_copyRunNumber) {
136  // Legacy Event Info
137  const EventInfo* pInputEvt(nullptr);
138  if (evtStore()->retrieve(pInputEvt).isSuccess()) {
139  assert(pInputEvt);
140 
141  unsigned int run_number = pInputEvt->event_ID()->run_number();
142 
143  EventType* eventType = const_cast<EventType*>(pInputEvt->event_type());
144  eventType->set_mc_channel_number(run_number);
145  eventType->set_mc_event_number (newnum);
146 
147  ATH_MSG_DEBUG("Copied run number into mc channel number: " << run_number);
148  } else {
149  ATH_MSG_ERROR("No EventInfo object found");
150  return StatusCode::FAILURE;
151  }
152 
153  // xAOD::EventInfo
154  outputEvtInfo->setMCChannelNumber(inpRunNumber);
155  outputEvtInfo->setMCEventNumber(newnum);
156  outputEvtInfo->setEventNumber(newnum);
157  }
158 
159 
160  if (m_corRunNumber) {
161  // Change the EventID in the eventinfo header
162  const EventInfo* pInputEvt(nullptr);
163  unsigned int oldRunNumber = 0;
164  if (evtStore()->retrieve(pInputEvt).isSuccess()) {
165  assert(pInputEvt);
166  EventID* eventID = const_cast<EventID*>(pInputEvt->event_ID());
167  oldRunNumber = eventID->run_number();
168  eventID->set_run_number(m_newRunNumber);
169  ATH_MSG_DEBUG("Set new run number in event_ID " << m_newRunNumber);
170 
171  // also set the MC channel number
172  EventType* event_type = const_cast<EventType*>(pInputEvt->event_type());
174  ATH_MSG_DEBUG("Set new MC channel number " << event_type->mc_channel_number());
175  } else {
176  ATH_MSG_ERROR("No EventInfo object found");
177  return StatusCode::SUCCESS;
178  }
179 
180  oldRunNumber = outputEvtInfo->runNumber();
181  outputEvtInfo->setRunNumber(m_newRunNumber);
182  outputEvtInfo->setMCChannelNumber(m_newRunNumber);
183 
184  {
185  // change the channel number where /Generation/Parameters are found
186  auto newChannelNumber =
188  auto oldChannelNumber =
189  static_cast< CondAttrListCollection::ChanNum >(oldRunNumber);
190 
191  const char* key = "/Generation/Parameters";
192  const IOVMetaDataContainer * iovContainer = nullptr;
193  if (m_metaDataStore->retrieve(iovContainer, key).isSuccess()
194  && iovContainer) {
195  // get a hold of the payload
196  const IOVPayloadContainer * payloadContainer =
197  iovContainer->payloadContainer();
198 
199  // Grab the attribute list
200  for (CondAttrListCollection* collection : *payloadContainer) {
201  for(unsigned int index = 0; index < collection->size(); ++index) {
202  if (collection->chanNum(index) != oldChannelNumber) {
203  ATH_MSG_INFO("Not updating \"" << key << "\" on channel number "
204  << collection->chanNum(index));
205  continue;
206  }
207 
208  if (collection->fixChanNum(oldChannelNumber, newChannelNumber))
209  ATH_MSG_INFO("Updated \"" << key << "\" channel number from "
210  << oldChannelNumber << " to " << newChannelNumber);
211  else
212  ATH_MSG_ERROR("Channel number update from " << oldChannelNumber
213  << " to " << newChannelNumber << " on \"" << key
214  << "\" FAILED");
215  }
216  }
217 
218  {
219  // Update the MC channel number in the "/TagInfo"
220  const char* key = "/TagInfo";
221  const IOVMetaDataContainer * iovContainer = nullptr;
222  if (m_metaDataStore->retrieve(iovContainer, key).isSuccess()
223  && iovContainer) {
224  // get a hold of the payload
225  const IOVPayloadContainer * payloadContainer =
226  iovContainer->payloadContainer();
227 
228  // Grab the attribute list
229  for (CondAttrListCollection* collection : *payloadContainer) {
230  for (auto pair : *collection) {
231  // pair is a pair of Channel number and AttributeList
232  if (pair.second.exists("mc_channel_number")) {
233  try {
234  pair.second["mc_channel_number"].setValue(
236  ATH_MSG_INFO("Updated \"" << key << "\" mc_channel_number"
237  << " to " << m_newRunNumber);
238  } catch (std::exception&) {
239  try {
240  pair.second["mc_channel_number"].setValue(m_newRunNumber);
241  ATH_MSG_INFO("Updated \"" << key << "\" mc_channel_number"
242  << " to " << m_newRunNumber);
243  } catch (std::exception&) {
244  ATH_MSG_ERROR("mc_channel_number update from to "
245  << m_newRunNumber << " on \"" << key
246  << "\" FAILED");
247  }
248  }
249  }
250  }
251  }
252  }
253  }
254  } else {
255  ATH_MSG_INFO("Could not retrieve \"" << key << "\" from MetaDataStore");
256  }
257  }
258  }
259 
260 
261  if (m_nPass == m_nCount) {
262  ATH_MSG_INFO("Stopping the event processing...." << m_nPass << "/" << m_nCount);
263  SmartIF<IEventProcessor> apm(serviceLocator()->service("AthenaEventLoopMgr", /*createIf*/false));
264  if (apm) {
265  ATH_CHECK(apm->stopRun());
266  }
267  else {
268  ATH_MSG_WARNING("No EventLoop Manager found ");
269  }
270  }
271 
272  return StatusCode::SUCCESS;
273 }
274 
275 
277  ATH_MSG_INFO("Events passing all checks and written = " << m_nPass);
278  return StatusCode::SUCCESS;
279 }
280 
281 #endif
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
IOVPayloadContainer.h
This class is a container for the payload of conditions data. It is intended to be used to store cond...
IOVMetaDataContainer::payloadContainer
const IOVPayloadContainer * payloadContainer() const
Access to payload container.
Definition: IOVMetaDataContainer.h:141
HepMC::set_ll_event_number
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
Definition: GenEvent.h:612
plotting.plot_kinematics.run_number
run_number
Definition: plot_kinematics.py:29
CountHepMC::m_outputEvtInfoKey
SG::WriteHandleKey< xAOD::EventInfo > m_outputEvtInfoKey
Definition: CountHepMC.h:48
IOVMetaDataContainer
This class is a container for conditions data. It is intended to be used to store conditions data fro...
Definition: IOVMetaDataContainer.h:37
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CountHepMC::m_inputKeyName
std::string m_inputKeyName
Definition: CountHepMC.h:45
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
EventType::set_mc_channel_number
void set_mc_channel_number(number_type chan)
Add in the MC generator channel number (aka gen run number)
Definition: EventType.cxx:197
EventType
This class represents the "type of event" where the type is given by one or more "characteristics".
Definition: EventType.h:92
CountHepMC::CountHepMC
CountHepMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition: CountHepMC.cxx:27
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
EventType.h
This class provides general information about an event. It extends EventInfo with a list of sub-evts ...
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
CondAttrListCollection
This class is a collection of AttributeLists where each one is associated with a channel number....
Definition: CondAttrListCollection.h:52
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
EventType::mc_channel_number
number_type mc_channel_number() const
Access to the MC generator channel number (was used as run number for generator events)
Definition: EventType.cxx:165
CountHepMC::m_corRunNumber
bool m_corRunNumber
Definition: CountHepMC.h:43
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CountHepMC::execute
virtual StatusCode execute() override
Definition: CountHepMC.cxx:55
EventID.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
CountHepMC.h
CountHepMC::initialize
virtual StatusCode initialize() override
Definition: CountHepMC.cxx:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
GenBase
Base class for common behaviour of MC truth algorithms.
Definition: GenBase.h:47
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
EventInfo::event_ID
EventID * event_ID()
the unique identification of the event.
Definition: EventInfo/EventInfo/EventInfo.h:224
calibdata.exception
exception
Definition: calibdata.py:496
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
CondAttrListCollection::ChanNum
unsigned int ChanNum
Definition: CondAttrListCollection.h:55
IOVPayloadContainer
This class is a container for the payload of conditions data. It is intended to be used to store cond...
Definition: IOVPayloadContainer.h:35
IOVMetaDataContainer.h
This class is a container for conditions data. It is intended to be used to store conditions data fro...
EventAuxInfo.h
CountHepMC::m_nPass
int m_nPass
Definition: CountHepMC.h:35
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
errorcheck.h
Helpers for checking error return status codes and reporting errors.
EventInfo
This class provides general information about an event. Event information is provided by the accessor...
Definition: EventInfo/EventInfo/EventInfo.h:43
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
CountHepMC::m_mcWeightsKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_mcWeightsKey
Definition: CountHepMC.h:50
DeMoScan.index
string index
Definition: DeMoScan.py:364
EventType::set_mc_event_number
void set_mc_event_number(uint64_t evt)
Add in the MC generator event number.
Definition: EventType.cxx:202
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
EventID
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventID.h:35
ReadDecorHandle.h
Handle class for reading a decoration on an object.
CountHepMC::m_inputEvtInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_inputEvtInfoKey
Definition: CountHepMC.h:47
CountHepMC::m_metaDataStore
ServiceHandle< StoreGateSvc > m_metaDataStore
Definition: CountHepMC.h:33
CountHepMC::m_nCount
int m_nCount
Definition: CountHepMC.h:36
CountHepMC::finalize
virtual StatusCode finalize() override
Definition: CountHepMC.cxx:276
CountHepMC::m_firstEv
long long int m_firstEv
Definition: CountHepMC.h:38
CountHepMC::m_newRunNumber
int m_newRunNumber
Definition: CountHepMC.h:39
CountHepMC::m_copyRunNumber
bool m_copyRunNumber
Definition: CountHepMC.h:44
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
CountHepMC::m_corEvtID
bool m_corEvtID
Definition: CountHepMC.h:42
GenBase::initialize
virtual StatusCode initialize() override
Definition: GenBase.cxx:17
CountHepMC::m_corHepMC
bool m_corHepMC
Definition: CountHepMC.h:41
EventInfo::event_type
EventType * event_type()
the type of the event, e.g. simulation, testbeam, etc
Definition: EventInfo/EventInfo/EventInfo.h:234
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37