ATLAS Offline Software
Loading...
Searching...
No Matches
CopyMcEventCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
7
8CopyMcEventCollection::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
18 ATH_CHECK( m_bkgInputKey.initialize(!m_bkgInputKey.key().empty()) );
19 ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_bkgInputKey);
20 ATH_CHECK( m_signalInputKey.initialize() );
21 ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_signalInputKey);
22 ATH_CHECK( m_outputKey.initialize() );
23 ATH_MSG_VERBOSE("Initialized WriteHandleKey: " << m_outputKey);
24
25 ATH_CHECK( m_eventInfoKey.initialize() );
26
27 return StatusCode::SUCCESS;
28}
29
30StatusCode 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
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 unsigned int droppedSeparatorGenEvents{0};
66 int backupPileUpType{0};
67 int backupBunchCrossingTime{0};
68 // Copy signal GenEvents
69 for (McEventCollection::const_iterator it = signalContainer->begin(); it != signalContainer->end(); ++it) {
70 if ( (*it)->event_number() == -1 ) {
71 ++droppedSeparatorGenEvents;
72 ATH_MSG_VERBOSE("Signal: Skipping a separator GenEvent. " << droppedSeparatorGenEvents << " skipped so far.");
73 backupBunchCrossingTime+=25; // NB This is a bit of a hack, but better than having everything in-time
74 continue;
75 }
76 HepMC::GenEvent* copiedEvent = new HepMC::GenEvent(**it);
78 auto bunchCrossingTime = (*it)->attribute<HepMC3::IntAttribute>(HepMCStr::BunchCrossingTime);
79 if (bunchCrossingTime) {
80 copiedEvent->add_attribute(HepMCStr::BunchCrossingTime,std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime->value()));
81 }
82 else {
83 copiedEvent->add_attribute(HepMCStr::BunchCrossingTime,std::make_shared<HepMC3::IntAttribute>(backupBunchCrossingTime));
84 }
85 auto pileupType = (*it)->attribute<HepMC3::IntAttribute>(HepMCStr::PileUpType);
86 if (pileupType) {
87 copiedEvent->add_attribute(HepMCStr::PileUpType,std::make_shared<HepMC3::IntAttribute>(pileupType->value()));
88 }
89 else {
90 copiedEvent->add_attribute(HepMCStr::PileUpType,std::make_shared<HepMC3::IntAttribute>(backupPileUpType));
91 if (backupPileUpType == 0) { backupPileUpType = 1; } // ignore the possibility for cavern background for this back-up case
92 }
93 if (!copiedEvent->heavy_ion() && (*it)->heavy_ion()) {
94 // It should be clarified if we want to get a copy or the
95 // content.
96 HepMC::GenHeavyIonPtr hinew=std::make_shared<HepMC::GenHeavyIon>(*((*it)->heavy_ion()));
97 copiedEvent->set_heavy_ion(std::move(hinew));
98 }
99 outputContainer->push_back(copiedEvent);
100 }
101
102 backupBunchCrossingTime = 0;
103 // Copy background GenEvents if configured
104 if (!m_bkgInputKey.key().empty()) {
105 McEventCollection::const_iterator it = bkgContainerPtr->begin();
106 if (m_removeBkgHardScatterTruth.value()) {
107 // Do not copy the single particle neutrino GenEvent.
108 ++it;
109 }
110 for ( ; it != bkgContainerPtr->end(); ++it) {
111 if ( (*it)->event_number() == -1 ) {
112 ++droppedSeparatorGenEvents;
113 ATH_MSG_VERBOSE("Background: Skipping a separator GenEvent. " << droppedSeparatorGenEvents << " skipped so far.");
114 backupBunchCrossingTime+=25; // NB This is a bit of a hack, but better than having everything in-time
115 continue;
116 }
117 HepMC::GenEvent* copiedEvent = new HepMC::GenEvent(**it);
118 HepMC::fillBarcodesAttribute(copiedEvent);
119 auto bunchCrossingTime = (*it)->attribute<HepMC3::IntAttribute>(HepMCStr::BunchCrossingTime);
120 if (bunchCrossingTime) {
121 copiedEvent->add_attribute(HepMCStr::BunchCrossingTime,std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime->value()));
122 }
123 else {
124 copiedEvent->add_attribute(HepMCStr::BunchCrossingTime,std::make_shared<HepMC3::IntAttribute>(backupBunchCrossingTime));
125 }
126 auto pileupType = (*it)->attribute<HepMC3::IntAttribute>(HepMCStr::PileUpType);
127 if (pileupType) {
128 copiedEvent->add_attribute(HepMCStr::PileUpType,std::make_shared<HepMC3::IntAttribute>(pileupType->value()));
129 }
130 else {
131 copiedEvent->add_attribute(HepMCStr::PileUpType,std::make_shared<HepMC3::IntAttribute>(backupPileUpType));
132 if (backupPileUpType == 0) { backupPileUpType = 1; } // ignore the possibility for cavern background for this back-up case
133 }
134 if (!copiedEvent->heavy_ion() && (*it)->heavy_ion()) {
135 // It should be clarified if we want to get a copy or the
136 // content.
137 HepMC::GenHeavyIonPtr hinew=std::make_shared<HepMC::GenHeavyIon>(*((*it)->heavy_ion()));
138 copiedEvent->set_heavy_ion(std::move(hinew));
139 }
140 outputContainer->push_back(copiedEvent);
141 }
142 }
143 ATH_MSG_VERBOSE("output size: " << outputContainer->size() << ", Signal Input size: " << signalContainer->size() << ", Bkg Input size: " << bkgContainerPtr->size() << ", dropped neutrino GenEvents: " << (m_removeBkgHardScatterTruth ? 1 : 0) << " , dropped Separator GenEvents: " << droppedSeparatorGenEvents);
144 // dump McEventCollection in debug mode to confirm everything is as expected
145 if (msgLvl(MSG::DEBUG)) {
146 if (!outputContainer->empty()) {
147 ATH_MSG_DEBUG("McEventCollection contents:");
148 for (const HepMC::GenEvent *event : *outputContainer) {
149 ATH_MSG_DEBUG(" GenEvent #" << event->event_number() << ", signal_process_id=" << HepMC::signal_process_id(event));
150 }
151 }
152 }
153
154 ATH_MSG_DEBUG("execute() end");
155 return StatusCode::SUCCESS;
156}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
bool msgLvl(const MSG::Level lvl) const
An algorithm that can be simultaneously executed in multiple threads.
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Gaudi::Property< bool > m_removeBkgHardScatterTruth
SG::ReadHandleKey< McEventCollection > m_bkgInputKey
virtual StatusCode initialize() override
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< McEventCollection > m_outputKey
CopyMcEventCollection(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< McEventCollection > m_signalInputKey
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
std::string store() const
Return the name of the store holding the object we are proxying.
const std::string & name() const
Return the StoreGate ID for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const std::string BunchCrossingTime
const std::string PileUpType
int signal_process_id(const GenEvent &evt)
Definition GenEvent.h:572
HepMC3::GenHeavyIonPtr GenHeavyIonPtr
Definition HeavyIon.h:11
void fillBarcodesAttribute(GenEvent *e)
Definition GenEvent.h:393
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39