18 ATH_MSG_INFO(
"Initializing xAODtoHepMCTool " << name() <<
"...");
20 return StatusCode::SUCCESS;
23StatusCode xAODtoHepMCTool ::finalize()
25 ATH_MSG_INFO(
"==============================================================");
26 ATH_MSG_INFO(
"========== xAOD -> HepMC Tool :: Run Summary ==========");
27 ATH_MSG_INFO(
"==============================================================");
39 ATH_MSG_INFO(
"Inconsistencies with the beam particles storage in the event record!");
46 ATH_MSG_INFO(
"==============================================================");
47 ATH_MSG_INFO(
"=================== End Run Summary ===================");
48 ATH_MSG_INFO(
"==============================================================");
49 return StatusCode::SUCCESS;
57 std::vector<HepMC::GenEvent> mcEventCollection;
71 std::shared_ptr<HepMC3::GenRunInfo> runinfo = std::make_shared<HepMC3::GenRunInfo>(*(hepmcEvent.run_info().get()));
79 mcEventCollection.push_back(std::move(hepmcEvent));
81 mcEventCollection[mcEventCollection.size()-1].set_run_info(std::move(runinfo));
87 return mcEventCollection;
94 HepMC::GenEvent genEvt;
96 genEvt.set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
100 genEvt.set_event_number(evtNum);
101 ATH_MSG_DEBUG(
"Start createHepMCEvent for event " << evtNum);
104 #ifndef XAOD_STANDALONE
106 std::map<std::string, int> weightNameMap;
111 std::shared_ptr<HepMC3::GenRunInfo> runinfo = std::make_shared<HepMC3::GenRunInfo>();
112 genEvt.set_run_info(std::move(runinfo));
113 std::vector<std::string> wnames;
114 wnames.reserve(
weights.size());
115 for (
int idx = 0; idx < int(
weights.size()); ++idx) {
116 for (
const auto& it : weightNameMap) {
117 if (it.second == idx) {
118 wnames.push_back(it.first);
123 genEvt.run_info()->set_weight_names(wnames);
124 for ( std::vector<float>::const_iterator wgt =
weights.begin(); wgt !=
weights.end(); ++wgt ) {
125 genEvt.weights().push_back(*wgt);
128 if (weightNameMap.size()) {
129 HepMC::WeightContainer& wc = genEvt.weights();
131 for (
int idx = 0; idx < int(
weights.size()); ++idx) {
132 for (
const auto& it : weightNameMap) {
133 if (it.second == idx) {
141 for ( std::vector<float>::const_iterator wgt =
weights.begin(); wgt !=
weights.end(); ++wgt ) {
142 genEvt.weights().push_back(*wgt);
150 std::map<const xAOD::TruthVertex *, HepMC::GenVertexPtr> vertexMap;
154 if (!tlink.isValid()) {
continue; }
158 if (xPart ==
nullptr) {
159 ATH_MSG_WARNING(
"xAOD TruthParticle is equal to NULL. This should not happen!");
184 bool prodVtxSeenBefore(
false);
185 auto hepmcProdVtx =
vertexHelper(xAODProdVtx, vertexMap, prodVtxSeenBefore);
188 hepmcProdVtx->add_particle_out(hepmcParticle);
190 hepmcProdVtx->add_particle_out(hepmcParticle.get());
193 if (!prodVtxSeenBefore) {
194 genEvt.add_vertex(std::move(hepmcProdVtx));
207 if (xPart->
hasProdVtx()) { (void)hepmcParticle.release(); }
211 bool decayVtxSeenBefore(
false);
212 auto hepmcDecayVtx =
vertexHelper(xAODDecayVtx, vertexMap, decayVtxSeenBefore);
215 hepmcDecayVtx->add_particle_in(std::move(hepmcParticle));
217 hepmcDecayVtx->add_particle_in(hepmcParticle.get());
220 if (!decayVtxSeenBefore) {
221 genEvt.add_vertex(std::move(hepmcDecayVtx));
225 (void)hepmcParticle.release();
236 std::map<const xAOD::TruthVertex *, HepMC::GenVertexPtr> &vertexMap,
237 bool &seenBefore)
const
241 std::map<const xAOD::TruthVertex *, HepMC::GenVertexPtr>::iterator vMapItr;
242 vMapItr = vertexMap.find(xaodVertex);
244 if (vMapItr != vertexMap.end()) {
246 hepmcVertex = (*vMapItr).second;
252 hepmcVertex = vertexMap[xaodVertex];
264 hepmcParticle->set_generated_mass(
m_momFac * particle->m());
265 return hepmcParticle;
282 std::vector<int> uidPars;
283 std::vector<int> uidKids;
287 std::cout <<
"======================================================================================" << std::endl;
288 std::cout <<
"xAODTruth Event " << evtNum << std::endl;
289 std::cout <<
" UniqueID PDG Id Status px(GeV) py(GeV) pz(GeV) E(GeV) Parent: Decay" << std::endl;
290 std::cout <<
" -----------------------------------------------------------------------------------" << std::endl;
292 int nPart =
event->nTruthParticles();
293 std::ios oldState(
nullptr);
294 oldState.copyfmt(std::cout);
295 for (
int i = 0; i < nPart; ++i)
298 if (part ==
nullptr)
continue;
300 int id = part->pdgId();
301 if (
id != 25)
continue;
302 int stat = part->status();
303 float px = part->px() / 1000.;
304 float py = part->py() / 1000.;
305 float pz = part->pz() / 1000.;
306 float e = part->e() / 1000.;
310 if (part->hasProdVtx())
317 if (part->hasDecayVtx())
325 std::cout << std::setw(10) <<
HepMC::uniqueID(part) << std::setw(12) <<
id
326 << std::setw(8) << stat
327 << std::setprecision(2) << std::fixed
328 << std::setw(10) << px << std::setw(10) << py
329 << std::setw(10) << pz << std::setw(10) << e <<
" ";
331 for (
unsigned int k = 0; k < uidPars.size(); ++k)
333 std::cout << uidPars[k] <<
" ";
336 for (
unsigned int k = 0; k < uidKids.size(); ++k)
338 std::cout << uidKids[k] <<
" ";
340 std::cout << std::endl;
342 std::cout <<
"======================================================================================" << std::endl;
343 std::cout.copyfmt(oldState);
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
static std::string retrieveMetadata(const std::string &folder, const std::string &key, const ServiceHandle< StoreGateSvc > &inputMetaStore)
method that always returns as a string you can use from, e.g, pyROOT with evt = ROOT....
uint64_t eventNumber() const
The current event's event number.
const TruthParticleLinks_t & truthParticleLinks() const
Get all the truth particles.
const std::vector< float > & weights() const
Const access to the weights vector.
const TruthVertex_v1 * decayVtx() const
The decay vertex of this particle.
bool hasProdVtx() const
Check for a production vertex on this particle.
bool hasDecayVtx() const
Check for a decay vertex on this particle.
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
void line(std::ostream &os, const GenEvent &e)
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation (TODO migrate to be based on status).
HepMC::GenVertex * GenVertexPtr
GenVertexPtr newGenVertexPtr(const HepMC::FourVector &pos=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), const int i=0)
GenParticlePtr newGenParticlePtr(const HepMC::FourVector &mom=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), int pid=0, int status=0)
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
GenParticle * GenParticlePtr
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation (TODO migrate to be...
TruthEventContainer_v1 TruthEventContainer
Declare the latest version of the truth event container.
EventInfo_v1 EventInfo
Definition of the latest event info version.
TruthVertex_v1 TruthVertex
Typedef to implementation.
TruthEvent_v1 TruthEvent
Typedef to implementation.
TruthParticle_v1 TruthParticle
Typedef to implementation.