LDMX Software
HepMCParticle.cxx
2
3#include <iostream>
4
5#include "Framework/Exception/Exception.h"
6#include "HepMC3/FourVector.h"
7#include "HepMC3/GenEvent.h"
8#include "HepMC3/GenVertex.h"
9
10namespace simcore {
11namespace hepmc {
12
13HepMCParticle::HepMCParticle(std::shared_ptr<HepMC3::GenParticle> particle)
14 : particle_(particle) {
15 if (!particle_) {
16 EXCEPTION_RAISE("NullParticle",
17 "Attempted to create HepMCParticle with null GenParticle");
18 }
19}
20
21int HepMCParticle::getPdgId() const { return particle_->pid(); }
22
23int HepMCParticle::getStatus() const { return particle_->status(); }
24
25double HepMCParticle::getMomentum(int index) const {
26 const HepMC3::FourVector& momentum = particle_->momentum();
27 switch (index) {
28 case 0:
29 return momentum.px();
30 case 1:
31 return momentum.py();
32 case 2:
33 return momentum.pz();
34 case 3:
35 return momentum.e();
36 default:
37 EXCEPTION_RAISE(
38 "InvalidIndex",
39 "Momentum index must be 0 (px), 1 (py), 2 (pz), or 3 (E)");
40 }
41}
42
43double HepMCParticle::getMass() const { return particle_->generated_mass(); }
44
46 auto vertex = particle_->production_vertex();
47 return vertex ? vertex->id() : 0;
48}
49
50HepMC3::FourVector HepMCParticle::getProductionPosition() const {
51 // GenVertex::position falls back to ancestors and then the event position
52 if (auto vertex = particle_->production_vertex()) return vertex->position();
53 if (auto event = particle_->parent_event()) return event->event_pos();
54 return HepMC3::FourVector::ZERO_VECTOR();
55}
56
57std::shared_ptr<HepMC3::GenParticle> HepMCParticle::getGenParticle() const {
58 return particle_;
59}
60
61void HepMCParticle::print() const { std::cout << *this << std::endl; }
62
63std::ostream& operator<<(std::ostream& stream, const HepMCParticle& particle) {
64 stream << "HepMCParticle: " << "PDG=" << particle.getPdgId()
65 << " Status=" << particle.getStatus()
66 << " px=" << particle.getMomentum(0)
67 << " py=" << particle.getMomentum(1)
68 << " pz=" << particle.getMomentum(2)
69 << " E=" << particle.getMomentum(3) << " mass=" << particle.getMass();
70 return stream;
71}
72
73} // namespace hepmc
74} // namespace simcore
Class defining a single particle record in a HepMC event.
Wrapper class for HepMC3::GenParticle.
double getMomentum(int index) const
Get a momentum component by index.
double getMass() const
Get the particle's mass.
std::shared_ptr< HepMC3::GenParticle > particle_
The underlying HepMC3 GenParticle.
int getStatus() const
Get the status code.
void print() const
Print particle information to an output stream.
int getProductionVertexId() const
Get the production vertex id, 0 if the particle has none.
HepMCParticle(std::shared_ptr< HepMC3::GenParticle > particle)
Class constructor.
int getPdgId() const
Get the PDG code.
std::shared_ptr< HepMC3::GenParticle > getGenParticle() const
Get the underlying HepMC3 GenParticle.
HepMC3::FourVector getProductionPosition() const
Get the production position (x, y, z in mm, t in mm/c).
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...