ATLAS Offline Software
Loading...
Searching...
No Matches
NewMergeMcEventCollTool.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 <fstream>
7
9 const std::string& name,
10 const IInterface *parent) :
11 PileUpToolBase(type, name, parent)
12{
13}
14
16{
17 ATH_CHECK(m_pMergeSvc.retrieve());
18
19 ATH_CHECK( m_truthCollOutputKey.initialize() );
20 return StatusCode::SUCCESS;
21}
22
24StatusCode NewMergeMcEventCollTool::prepareEvent(const EventContext& ctx, unsigned int nInputEvents)
25{
26 ATH_MSG_VERBOSE( this->name()<<"::prepareEvent()" );
27
28 //Check we are getting at least one event
29 if (0 == nInputEvents) {
30 ATH_MSG_ERROR("prepareEvent: TimedTruthList with key "
31 << m_truthCollInputKey.value() << " is empty");
32 return StatusCode::RECOVERABLE;
33 }
34 ATH_MSG_VERBOSE( this->name()<<"::prepareEvent: there are " << nInputEvents << " subevents in this event.");
35
36 if (!m_outputMcEventCollection.isValid()) {
37 // Would be nice to avoid having the WriteHandle as a member
38 // variable, but this is the only way to allow multiple function
39 // calls to add information to the version of the
40 // McEventCollection in the output StoreGate
42 ATH_CHECK(m_outputMcEventCollection.record(std::make_unique<McEventCollection>()));
43 }
44 else {
45 ATH_MSG_ERROR("WriteHandle already valid??");
46 return StatusCode::FAILURE;
47 }
48
49 return StatusCode::SUCCESS;
50}
51
52StatusCode NewMergeMcEventCollTool::processBunchXing(int /*bunchXing*/,
53 SubEventIterator bSubEvents,
54 SubEventIterator eSubEvents)
55{
56 SubEventIterator iEvt(bSubEvents);
57 //loop over the McEventCollections (each one assumed to containing exactly one GenEvent) of the various input events
58 while (iEvt != eSubEvents){
59 StoreGateSvc& seStore(*iEvt->ptr()->evtStore());
60 const McEventCollection *pMEC(nullptr);
61 ATH_CHECK(seStore.retrieve(pMEC, m_truthCollInputKey.value()));
62 ATH_MSG_VERBOSE( this->name()<<"::processBunchXing: SubEvt McEventCollection from StoreGate " << seStore.name() << " of PileUpType " << iEvt->type() );
63 if(m_pileUpType==iEvt->type()) {
65 static_cast<int>(iEvt->type()), iEvt->time()));
66 }
67 ++iEvt;
68 }
69 return StatusCode::SUCCESS;
70}
71
72StatusCode NewMergeMcEventCollTool::mergeEvent(const EventContext& /*ctx*/)
73{
74 ATH_MSG_VERBOSE( this->name()<<"::mergeEvent()" );
75 if(msgLvl(MSG::VERBOSE)) { this->printDetailsOfMergedMcEventCollection(m_outputMcEventCollection.ptr()); }
76 return StatusCode::SUCCESS;
77}
78
80StatusCode NewMergeMcEventCollTool::processAllSubEvents(const EventContext& ctx)
81{
82 ATH_MSG_VERBOSE ( this->name()<<"::processAllSubEvents()" );
83 SG::WriteHandle<McEventCollection> outputMcEventCollection(m_truthCollOutputKey, ctx);
84 ATH_CHECK(outputMcEventCollection.record(std::make_unique<McEventCollection>()));
85
86 //first get the list of McEventCollections
88 TimedTruthList truthList;
89 ATH_CHECK(m_pMergeSvc->retrieveSubEvtsData(m_truthCollInputKey.value(), truthList));
90
91 //Check we are getting at least one event
92 const unsigned int nInputMcEventColls=truthList.size();
93 if (0 == nInputMcEventColls) {
94 ATH_MSG_WARNING("TimedTruthList with key " << m_truthCollInputKey.value() << " is empty.");
95 return StatusCode::SUCCESS;
96 }
97
98 ATH_MSG_DEBUG( "execute: there are " << nInputMcEventColls << " subevents in this event.");
99 //TODO can we make this into an auto for loop?
100 TimedTruthList::iterator timedTruthListIter(truthList.begin()), endOfTimedTruthList(truthList.end());
101 //loop over the McEventCollections (each one assumed to containing exactly one GenEvent) of the various input events
102 while (timedTruthListIter != endOfTimedTruthList) {
103 ATH_MSG_VERBOSE( this->name()<<"::processBunchXing: SubEvt McEventCollection of PileUpType " << timedTruthListIter->first.type() );
104 if(m_pileUpType==timedTruthListIter->first.type()) {
105 const McEventCollection *pBackgroundMcEvtColl(&*(timedTruthListIter->second));
106 ATH_CHECK(this->processEvent(pBackgroundMcEvtColl, outputMcEventCollection.ptr(), static_cast<int>(timedTruthListIter->first.type()), timedTruthListIter->first.time()));
107 }
108 ++timedTruthListIter;
109 } //timed colls
110
111 if(msgLvl(MSG::VERBOSE)) { this->printDetailsOfMergedMcEventCollection(outputMcEventCollection.ptr()); }
112 return StatusCode::SUCCESS;
113}
114
115StatusCode NewMergeMcEventCollTool::processEvent(const McEventCollection *pMcEvtColl, McEventCollection *outputMcEventCollection, int pileupType, long timeOffset)
116{
117 ATH_MSG_VERBOSE( this->name()<<"::processEvent() Event Type: " << pileupType );
118 if (!outputMcEventCollection) {
119 ATH_MSG_ERROR( this->name()<<"::processEvent() was passed an null output McEventCollection pointer." );
120 return StatusCode::FAILURE;
121 }
122 if (!pMcEvtColl) {
123 ATH_MSG_ERROR( this->name()<<"::processEvent() was passed an null input McEventCollection pointer." );
124 return StatusCode::FAILURE;
125 }
126 if (!pMcEvtColl->empty()) {
127 for (unsigned int iEv=0; iEv<pMcEvtColl->size(); iEv++) {
128 const HepMC::GenEvent& c_evt(*((*pMcEvtColl)[iEv]));
129 HepMC::GenEvent * evt = new HepMC::GenEvent(c_evt);
131 const int bunchCrossingTime=static_cast<int>(timeOffset);
132 evt->add_attribute(HepMCStr::BunchCrossingTime,std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime));
133 evt->add_attribute(HepMCStr::PileUpType,std::make_shared<HepMC3::IntAttribute>(pileupType));
134 for (const auto& itVer: evt->vertices()) {
135 HepMC::FourVector newPos(itVer->position().x(),itVer->position().y(),itVer->position().z(),itVer->position().t()+timeOffset);
136 itVer->set_position(newPos);
137 }
138 outputMcEventCollection->push_back(evt);
139 }
140 }
141 return StatusCode::SUCCESS;
142}
143
145{
146 DataVector<HepMC::GenEvent>::const_iterator outputEventItr(outputMcEventCollection->begin());
147 const DataVector<HepMC::GenEvent>::const_iterator endOfEvents(outputMcEventCollection->end());
148 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
149 ATH_MSG_INFO ( "pileUpType: " << m_pileUpType);
150 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
151 ATH_MSG_INFO ( "Current OUTPUT GenEvent: " );
152 while(outputEventItr!=endOfEvents) {
153 const int signal_process_id(HepMC::signal_process_id((*outputEventItr)));
154 const int event_number((*outputEventItr)->event_number());
155 ATH_MSG_INFO ( "GenEvent #"<<event_number<<", signal_process_id="<<signal_process_id<</*", category="<<event->second<<*/", number of Vertices="<<(*outputEventItr)->vertices_size() );
156 char fname[80];
157 sprintf(fname,"%s.event%d.txt",m_truthCollInputKey.value().c_str(),event_number);
158 std::ofstream of(fname);
159 const HepMC::GenEvent *evt=(*outputEventItr);
160 HepMC::Print::line(of,*evt); // verbose output
161 of.close();
162 ++outputEventItr;
163 }
164 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
165 return;
166}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::vector< xAOD::EventInfo::SubEvent >::const_iterator SubEventIterator
Definition IPileUpTool.h:22
DataModel_detail::const_iterator< DataVector > const_iterator
Standard const_iterator.
Definition DataVector.h:838
value_type push_back(value_type pElem)
Add an element to the end of the collection.
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.
bool empty() const noexcept
Returns true if the collection is empty.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
virtual StatusCode initialize() override final
SG::WriteHandle< McEventCollection > m_outputMcEventCollection
virtual StatusCode prepareEvent(const EventContext &ctx, unsigned int nInputEvents) override final
called before the subevts loop.
StatusCode processEvent(const McEventCollection *pMcEvtColl, McEventCollection *outputMcEventCollection, int pileupType, long timeOffset=0)
virtual StatusCode processBunchXing(int, SubEventIterator bSubEvents, SubEventIterator eSubEvents) override final
called for each active bunch-crossing to process current SubEvents bunchXing is in ns
NewMergeMcEventCollTool(const std::string &type, const std::string &name, const IInterface *parent)
SG::WriteHandleKey< McEventCollection > m_truthCollOutputKey
ServiceHandle< PileUpMergeSvc > m_pMergeSvc
virtual StatusCode mergeEvent(const EventContext &ctx) override final
called at the end of the subevts loop.
virtual StatusCode processAllSubEvents(const EventContext &ctx) override final
Algorithm Approach.
void printDetailsOfMergedMcEventCollection(McEventCollection *outputMcEventCollection) const
Gaudi::Property< int > m_pileUpType
PileUpToolBase(const std::string &type, const std::string &name, const IInterface *parent)
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
The Athena Transient Store API.
StatusCode retrieve(const T *&ptr) const
Retrieve the default object into a const T*.
const std::string BunchCrossingTime
const std::string PileUpType
int signal_process_id(const GenEvent &evt)
Definition GenEvent.h:572
HepMC3::FourVector FourVector
void fillBarcodesAttribute(GenEvent *e)
Definition GenEvent.h:393
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
std::list< value_t > type
type of the collection of timed data object