ATLAS Offline Software
CopyMcEventCollection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "AtlasHepMC/HeavyIon.h"
7 
8 CopyMcEventCollection::CopyMcEventCollection(const std::string &name, ISvcLocator *pSvcLocator)
9  : AthReentrantAlgorithm(name, pSvcLocator)
10 {
11 }
12 
14 {
15  ATH_MSG_DEBUG("Initializing...");
16 
17  // Check and initialize keys
19  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_bkgInputKey);
21  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_signalInputKey);
23  ATH_MSG_VERBOSE("Initialized WriteHandleKey: " << m_outputKey);
24 
26 
27  return StatusCode::SUCCESS;
28 }
29 
30 StatusCode CopyMcEventCollection::execute(const EventContext& ctx) const
31 {
32  ATH_MSG_DEBUG("execute() begin");
33 
34  // Reading the input containers
35  ATH_MSG_VERBOSE("Retrieving input containers");
36 
37  const McEventCollection *bkgContainerPtr = nullptr;
38  if (!m_bkgInputKey.key().empty()) {
40  if (!bkgContainer.isValid()) {
41  ATH_MSG_ERROR("Could not get background McEventCollection container " << bkgContainer.name() << " from store " << bkgContainer.store());
42  return StatusCode::FAILURE;
43  }
44  bkgContainerPtr = bkgContainer.cptr();
45 
46  ATH_MSG_DEBUG("Found background McEventCollection container " << bkgContainer.name() << " in store " << bkgContainer.store());
47  }
48 
50  if (!signalContainer.isValid()) {
51  ATH_MSG_ERROR("Could not get signal McEventCollection container " << signalContainer.name() << " from store " << signalContainer.store());
52  return StatusCode::FAILURE;
53  }
54  ATH_MSG_DEBUG("Found signal McEventCollection container " << signalContainer.name() << " in store " << signalContainer.store());
55 
56  // Creating output RDO container
57  SG::WriteHandle<McEventCollection> outputContainer(m_outputKey, ctx);
58  ATH_CHECK(outputContainer.record(std::make_unique<McEventCollection>()));
59  if (!outputContainer.isValid()) {
60  ATH_MSG_ERROR("Could not record output McEventCollection container " << outputContainer.name() << " to store " << outputContainer.store());
61  return StatusCode::FAILURE;
62  }
63  ATH_MSG_DEBUG("Recorded output McEventCollection container " << outputContainer.name() << " in store " << outputContainer.store());
64 
65  // Copy signal GenEvents
66  for (McEventCollection::const_iterator it = signalContainer->begin(); it != signalContainer->end(); ++it) {
67  HepMC::GenEvent* copiedEvent = new HepMC::GenEvent(**it);
68  HepMC::fillBarcodesAttribute(copiedEvent);
69 #ifdef HEPMC3
70  auto bunchCrossingTime = (*it)->attribute<HepMC3::IntAttribute>("BunchCrossingTime");
71  if (bunchCrossingTime) {
72  copiedEvent->add_attribute("BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime->value()));
73  }
74  auto pileupType = (*it)->attribute<HepMC3::IntAttribute>("PileUpType");
75  if (pileupType) {
76  copiedEvent->add_attribute("PileUpType",std::make_shared<HepMC3::IntAttribute>(pileupType->value()));
77  }
78 #endif
79  if (!copiedEvent->heavy_ion() && (*it)->heavy_ion()) {
80  // It should be clarified if we want to get a copy or the
81  // content.
82 #ifdef HEPMC3
83  HepMC::GenHeavyIonPtr hinew=std::make_shared<HepMC::GenHeavyIon>(*((*it)->heavy_ion()));
84  copiedEvent->set_heavy_ion(hinew);
85 #else
86  copiedEvent->set_heavy_ion(*((*it)->heavy_ion()));
87 #endif
88  }
89  outputContainer->push_back(copiedEvent);
90  }
91 
92  // Copy background GenEvents if configured
93  if (!m_bkgInputKey.key().empty()) {
94  McEventCollection::const_iterator it = bkgContainerPtr->begin();
95  if (m_removeBkgHardScatterTruth.value()) {
96  // Do not copy the single particle neutrino GenEvent.
97  ++it;
98  }
99  for ( ; it != bkgContainerPtr->end(); ++it) {
100  HepMC::GenEvent* copiedEvent = new HepMC::GenEvent(**it);
101  HepMC::fillBarcodesAttribute(copiedEvent);
102 #ifdef HEPMC3
103  auto bunchCrossingTime = (*it)->attribute<HepMC3::IntAttribute>("BunchCrossingTime");
104  if (bunchCrossingTime) {
105  copiedEvent->add_attribute("BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime->value()));
106  }
107  auto pileupType = (*it)->attribute<HepMC3::IntAttribute>("PileUpType");
108  if (pileupType) {
109  copiedEvent->add_attribute("PileUpType",std::make_shared<HepMC3::IntAttribute>(pileupType->value()));
110  }
111 #endif
112  if (!copiedEvent->heavy_ion() && (*it)->heavy_ion()) {
113  // It should be clarified if we want to get a copy or the
114  // content.
115 #ifdef HEPMC3
116  HepMC::GenHeavyIonPtr hinew=std::make_shared<HepMC::GenHeavyIon>(*((*it)->heavy_ion()));
117  copiedEvent->set_heavy_ion(hinew);
118 #else
119  copiedEvent->set_heavy_ion(*((*it)->heavy_ion()));
120 #endif
121  }
122  outputContainer->push_back(copiedEvent);
123  }
124  }
125 
126  // dump McEventCollection in debug mode to confirm everything is as expected
127  if (msgLvl(MSG::DEBUG)) {
128  if (!outputContainer->empty()) {
129  ATH_MSG_DEBUG("McEventCollection contents:");
130  for (const HepMC::GenEvent *event : *outputContainer) {
131  ATH_MSG_DEBUG(" GenEvent #" << event->event_number() << ", signal_process_id=" << HepMC::signal_process_id(event));
132  }
133  }
134  }
135 
136  ATH_MSG_DEBUG("execute() end");
137  return StatusCode::SUCCESS;
138 }
CopyMcEventCollection::m_bkgInputKey
SG::ReadHandleKey< McEventCollection > m_bkgInputKey
Definition: CopyMcEventCollection.h:24
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
CopyMcEventCollection::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: CopyMcEventCollection.h:23
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::ReadHandle< McEventCollection >
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:513
HeavyIon.h
skel.it
it
Definition: skel.GENtoEVGEN.py:423
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:504
CopyMcEventCollection.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
CopyMcEventCollection::m_signalInputKey
SG::ReadHandleKey< McEventCollection > m_signalInputKey
Definition: CopyMcEventCollection.h:25
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
CopyMcEventCollection::initialize
virtual StatusCode initialize() override
Definition: CopyMcEventCollection.cxx:13
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::VarHandleBase::store
std::string store() const
Return the name of the store holding the object we are proxying.
Definition: StoreGate/src/VarHandleBase.cxx:379
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
CopyMcEventCollection::m_removeBkgHardScatterTruth
Gaudi::Property< bool > m_removeBkgHardScatterTruth
Definition: CopyMcEventCollection.h:28
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
CopyMcEventCollection::m_outputKey
SG::WriteHandleKey< McEventCollection > m_outputKey
Definition: CopyMcEventCollection.h:26
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
SG::WriteHandle< McEventCollection >
CopyMcEventCollection::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: CopyMcEventCollection.cxx:30
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
DEBUG
#define DEBUG
Definition: page_access.h:11
CopyMcEventCollection::CopyMcEventCollection
CopyMcEventCollection(const std::string &name, ISvcLocator *pSvcLocator)
Definition: CopyMcEventCollection.cxx:8
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.