ATLAS Offline Software
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) :
12 {
13 }
14 
16 {
17  ATH_CHECK(m_pMergeSvc.retrieve());
18 
20  return StatusCode::SUCCESS;
21 }
22 
24 StatusCode 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 
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 
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 
72 StatusCode NewMergeMcEventCollTool::mergeEvent(const EventContext& /*ctx*/)
73 {
74  ATH_MSG_VERBOSE( this->name()<<"::mergeEvent()" );
76  return StatusCode::SUCCESS;
77 }
78 
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 
115 StatusCode 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 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
NewMergeMcEventCollTool::processEvent
StatusCode processEvent(const McEventCollection *pMcEvtColl, McEventCollection *outputMcEventCollection, int pileupType, long timeOffset=0)
Definition: NewMergeMcEventCollTool.cxx:115
NewMergeMcEventCollTool::m_outputMcEventCollection
SG::WriteHandle< McEventCollection > m_outputMcEventCollection
Definition: NewMergeMcEventCollTool.h:57
NewMergeMcEventCollTool::m_pMergeSvc
ServiceHandle< PileUpMergeSvc > m_pMergeSvc
Definition: NewMergeMcEventCollTool.h:51
NewMergeMcEventCollTool::m_truthCollInputKey
StringProperty m_truthCollInputKey
Definition: NewMergeMcEventCollTool.h:53
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:635
NewMergeMcEventCollTool::printDetailsOfMergedMcEventCollection
void printDetailsOfMergedMcEventCollection(McEventCollection *outputMcEventCollection) const
Definition: NewMergeMcEventCollTool.cxx:153
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
HepMC::Print::line
void line(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:676
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:626
NewMergeMcEventCollTool::prepareEvent
virtual StatusCode prepareEvent(const EventContext &ctx, unsigned int nInputEvents) override final
called before the subevts loop.
Definition: NewMergeMcEventCollTool.cxx:24
PileUpMergeSvc::TimedList::type
std::list< value_t > type
type of the collection of timed data object
Definition: PileUpMergeSvc.h:75
StoreGateSvc::retrieve
StatusCode retrieve(const T *&ptr) const
Retrieve the default object into a const T*.
NewMergeMcEventCollTool::m_pileUpType
Gaudi::Property< int > m_pileUpType
Definition: NewMergeMcEventCollTool.h:55
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:125
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
NewMergeMcEventCollTool::NewMergeMcEventCollTool
NewMergeMcEventCollTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: NewMergeMcEventCollTool.cxx:8
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
test_pyathena.parent
parent
Definition: test_pyathena.py:15
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
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
NewMergeMcEventCollTool::mergeEvent
virtual StatusCode mergeEvent(const EventContext &ctx) override final
called at the end of the subevts loop.
Definition: NewMergeMcEventCollTool.cxx:72
PileUpToolBase
Definition: PileUpToolBase.h:18
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
NewMergeMcEventCollTool::processBunchXing
virtual StatusCode processBunchXing(int, SubEventIterator bSubEvents, SubEventIterator eSubEvents) override final
called for each active bunch-crossing to process current SubEvents bunchXing is in ns
Definition: NewMergeMcEventCollTool.cxx:52
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
NewMergeMcEventCollTool::processAllSubEvents
virtual StatusCode processAllSubEvents(const EventContext &ctx) override final
Algorithm Approach.
Definition: NewMergeMcEventCollTool.cxx:80
NewMergeMcEventCollTool.h
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
SG::WriteHandle< McEventCollection >
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
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
NewMergeMcEventCollTool::m_truthCollOutputKey
SG::WriteHandleKey< McEventCollection > m_truthCollOutputKey
Definition: NewMergeMcEventCollTool.h:59
SubEventIterator
std::vector< xAOD::EventInfo::SubEvent >::const_iterator SubEventIterator
Definition: IPileUpTool.h:22
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
NewMergeMcEventCollTool::initialize
virtual StatusCode initialize() override final
Definition: NewMergeMcEventCollTool.cxx:15
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
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.