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#ifdef HEPMC3
130 HepMC::GenEvent * evt = new HepMC::GenEvent(c_evt);
132 const int bunchCrossingTime=static_cast<int>(timeOffset);
133 evt->add_attribute("BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime));
134 evt->add_attribute("PileUpType",std::make_shared<HepMC3::IntAttribute>(pileupType));
135 for (const auto& itVer: evt->vertices()) {
136 HepMC::FourVector newPos(itVer->position().x(),itVer->position().y(),itVer->position().z(),itVer->position().t()+timeOffset);
137 itVer->set_position(newPos);
138 }
139 outputMcEventCollection->push_back(evt);
140#else
141 HepMC::GenEvent * evt = new HepMC::GenEvent(c_evt);
142 for (HepMC::GenEvent::vertex_iterator itVer=evt->vertices_begin(); itVer!=evt->vertices_end(); ++itVer) {
143 HepMC::FourVector newPos((*itVer)->position().x(),(*itVer)->position().y(),(*itVer)->position().z(),(*itVer)->position().t()+timeOffset);
144 (*itVer)->set_position(newPos);
145 }
146 outputMcEventCollection->push_back(evt);
147#endif
148 }
149 }
150 return StatusCode::SUCCESS;
151}
152
154{
155 DataVector<HepMC::GenEvent>::const_iterator outputEventItr(outputMcEventCollection->begin());
156 const DataVector<HepMC::GenEvent>::const_iterator endOfEvents(outputMcEventCollection->end());
157 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
158 ATH_MSG_INFO ( "pileUpType: " << m_pileUpType);
159 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
160 ATH_MSG_INFO ( "Current OUTPUT GenEvent: " );
161 while(outputEventItr!=endOfEvents) {
162 const int signal_process_id(HepMC::signal_process_id((*outputEventItr)));
163 const int event_number((*outputEventItr)->event_number());
164 ATH_MSG_INFO ( "GenEvent #"<<event_number<<", signal_process_id="<<signal_process_id<</*", category="<<event->second<<*/", number of Vertices="<<(*outputEventItr)->vertices_size() );
165 char fname[80];
166 sprintf(fname,"%s.event%d.txt",m_truthCollInputKey.value().c_str(),event_number);
167 std::ofstream of(fname);
168 const HepMC::GenEvent *evt=(*outputEventItr);
169 HepMC::Print::line(of,*evt); // verbose output
170 of.close();
171 ++outputEventItr;
172 }
173 ATH_MSG_INFO ( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
174 return;
175}
#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*.
void line(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:678
int signal_process_id(const GenEvent &e)
Definition GenEvent.h:637
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:628
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