ATLAS Offline Software
ISFTruthIncident.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header
7 
8 // ISF includes
10 
11 // HepMC includes
13 #include "AtlasHepMC/GenParticle.h"
14 #include "AtlasHepMC/GenEvent.h"
16 
17 #include <cmath>
18 
19 static HepMC::GenParticlePtr ParticleHelper_convert( const ISF::ISFParticle &particle) {
20 
21  const Amg::Vector3D &mom = particle.momentum();
22  double mass = particle.mass();
23  double energy = std::sqrt( mom.mag2() + mass*mass);
24  HepMC::FourVector fourMomentum( mom.x(), mom.y(), mom.z(), energy);
25 
26  auto hepParticle = HepMC::newGenParticlePtr( fourMomentum, particle.pdgCode(), particle.status() );
27 #ifndef HEPMC3
28  HepMC::suggest_barcode(hepParticle, particle.barcode() );
29 #endif
30  // return a newly created GenParticle
31  return hepParticle;
32 }
33 
34 
35 
38  int process,
40  ISF::KillPrimary killsPrimary,
41  const HepMC::FourVector *position) :
42  ITruthIncident(geoID, children.size()),
43  m_parent(parent),
44  m_children(children),
45  m_process(process),
46  m_killsPrimary(killsPrimary),
47  m_position(position)
48 {
49  if ( !m_position) {
50  // No position was given, so compute it.
51  // Default to the parent particle position in the case that there are no child particles.
52  const ISF::ISFParticle *particle = (m_children.empty()) ? &m_parent : m_children.front();
53  if ( !particle) particle = &m_parent; // protection against nullptrs in m_children ISFParticleVector - this would indicate a bug upstream, better to throw an exception here?
54  const Amg::Vector3D &pos = particle->position();
55 
56  double time = 0.; //<! TODO: FIXME
57  m_position = new HepMC::FourVector( pos.x(), pos.y(), pos.z(), time );
58  }
59 }
60 
62  delete m_position;
63 }
64 
65 const HepMC::FourVector& ISF::ISFTruthIncident::position() const {
66  return *m_position;
67 }
68 
70  return -1;
71 }
72 
74  return m_process;
75 }
76 
78  return m_parent.momentum().mag2();
79 }
80 
82  return m_parent.momentum().perp2();
83 }
84 
86  return m_parent.ekin();
87 }
88 
90  return m_parent.pdgCode();
91 }
92 
94  return m_parent.status();
95 }
96 
98  return getHepMCTruthParticle(m_parent);
99 }
100 
101 int ISF::ISFTruthIncident::parentBarcode() { // TODO Remove this method
102  return HepMC::barcode(m_parent); // FIXME barcode-based
103 }
104 
106  return m_parent.id();
107 }
108 
110  return !(m_killsPrimary == ISF::fKillsPrimary);
111 }
112 
114  // if parent is killed in the interaction -> return nullptr
115  if (m_killsPrimary==ISF::fKillsPrimary) return nullptr;
116 
117  // only update the parent particle, if it survived the interaction
118 
119  // set a new barcode
120  m_parent.setBarcode( newBC );
121 
122  // set a new status
123  m_parent.setStatus( parentStatus() + HepMC::SIM_STATUS_INCREMENT );
124 
125  // FIXME At this point the m_parent ISFParticle's id, truthBinding
126  // and particleLink all still need to be updated
127 
128  // and update truth info (including the ISFParticle's HMPL)
129  return updateHepMCTruthParticle(m_parent, &m_parent);
130 }
131 
132 double ISF::ISFTruthIncident::childP2(unsigned short index) const {
133  return m_children[index]->momentum().mag2();
134 }
135 
136 double ISF::ISFTruthIncident::childPt2(unsigned short index) const {
137  return m_children[index]->momentum().perp2();
138 }
139 
140 double ISF::ISFTruthIncident::childEkin(unsigned short index) const {
141  return m_children[index]->ekin();
142 }
143 
144 
145 int ISF::ISFTruthIncident::childPdgCode(unsigned short index) const {
146  return m_children[index]->pdgCode();
147 }
148 
149 int ISF::ISFTruthIncident::childBarcode(unsigned short index) const {
150  return numberOfChildren() > index ? HepMC::barcode(m_children[index]) : HepMC::UNDEFINED_ID;
151 }
152 
154  int bc) {
155  // the child particle
156  ISF::ISFParticle *sec = m_children[index];
157 
158  // set particle barcode of the child particle
159  if (bc) {
160  sec->setBarcode( bc);
161  }
162 
163  // Enforce that the status is set correctly
165 
166  // FIXME At this point the sec ISFParticle's id, truthBinding
167  // and particleLink all still need to be updated
168 
169  // and update truth info (including the ISFParticle's HMPL)
170  return updateHepMCTruthParticle( *sec, &m_parent );
171 }
172 
173 
176  auto* truthBinding = particle.getTruthBinding();
177  HepMC::GenParticlePtr currentGenParticle = truthBinding ? truthBinding->getCurrentGenParticle() : nullptr;
178 
179  // We have what we want
180  if (currentGenParticle) {
181  return currentGenParticle;
182  }
183  //Otherwise we need to create it
184  return updateHepMCTruthParticle(particle,&particle);
185 }
186 
189  ISF::ISFParticle* parent ) const {
190  auto* truthBinding = particle.getTruthBinding();
191  HepMC::GenParticlePtr newGenParticle = ParticleHelper_convert( particle );
192 
193  if (truthBinding) {
194  truthBinding->setCurrentGenParticle(newGenParticle);
195  } else {
196  auto parentTruthBinding = parent ? parent->getTruthBinding() : nullptr;
197  auto primaryGenParticle = parentTruthBinding ? parentTruthBinding->getPrimaryGenParticle() : nullptr;
198  auto generationZeroGenParticle = newGenParticle; // New physical particle so this is also the generation zero particle
199  truthBinding = new TruthBinding( newGenParticle, primaryGenParticle, generationZeroGenParticle );
200  particle.setTruthBinding(truthBinding);
201  }
202  // At this point the values returned by particle.getParticleLink()
203  // and particle.id() are not consistent with what is stored in the
204  // TruthBinding.
205 
206  // FIXME Consider deleting the HepMcParticleLink and setting the id to HepMC::UNDEFINED_ID at this point?
207  return newGenParticle;
208 }
209 
212  // FIXME Check that we correctly deal with the case that the parent
213  // particle survives the interaction, but is rejected by
214  // registerTruthIncident
215  const ISF::TruthBinding *parentAfterIncidentTruthBinding = m_parent.getTruthBinding();
216  auto parentAfterIncidentGenParticle = (parentAfterIncidentTruthBinding) ? parentAfterIncidentTruthBinding->getCurrentGenParticle() : nullptr;
217  const int parentAfterIncidentID = (parentAfterIncidentGenParticle) ? HepMC::uniqueID(parentAfterIncidentGenParticle) : HepMC::UNDEFINED_ID;
218  HepMcParticleLink* parentAfterIncidentHMPL{};
219  const HepMcParticleLink* parentBeforeIncidentHMPL = m_parent.getParticleLink();
220  int eventIndex{0};
221  if (parentAfterIncidentGenParticle) { eventIndex = parentAfterIncidentGenParticle->parent_event()->event_number(); }
222  else if (parentBeforeIncidentHMPL) { eventIndex = parentBeforeIncidentHMPL->eventIndex(); }
223  const HepMcParticleLink::PositionFlag idxFlag =
225  if (parentBeforeIncidentHMPL) {
226  delete parentBeforeIncidentHMPL;
227  }
228  if (!parentAfterIncidentGenParticle) {
229  parentAfterIncidentHMPL = new HepMcParticleLink(parentAfterIncidentID, eventIndex, idxFlag, HepMcParticleLink::IS_ID);
230  }
231  else {
232  parentAfterIncidentHMPL = new HepMcParticleLink(parentAfterIncidentGenParticle, eventIndex, idxFlag);
233  }
234  m_parent.setId(parentAfterIncidentID);
235  m_parent.setParticleLink(parentAfterIncidentHMPL);
236 }
237 
240  unsigned short numSec = numberOfChildren();
241  for (unsigned short i=0; i<numSec; i++) {
242  // the current particle
243  ISF::ISFParticle *child = m_children[i];
244  ISF::TruthBinding *childTruthBinding = child->getTruthBinding();
245  if (!childTruthBinding) {
246  // Child particles which were rejected during
247  // registerTruthIncident need a TruthBinding
248  auto parentTruthBinding = m_parent.getTruthBinding();
249  if (parentTruthBinding) {
250  childTruthBinding = parentTruthBinding->childTruthBinding(nullptr);
251  }
252  else {
253  // FIXME We really shouldn't end up here, possibly abort if we hit this?
254  childTruthBinding = new TruthBinding( nullptr, nullptr, nullptr );
255  }
256  child->setTruthBinding(childTruthBinding);
257  }
258  auto childGenParticle = childTruthBinding->getCurrentGenParticle();
259  const int childID = (childGenParticle) ? HepMC::uniqueID(childGenParticle) : HepMC::UNDEFINED_ID;
260  HepMcParticleLink* childHMPL{};
261  const HepMcParticleLink* oldChildHMPL = child->getParticleLink();
262  int eventIndex{0};
263  if (childGenParticle) { eventIndex = childGenParticle->parent_event()->event_number(); }
264  else if (oldChildHMPL) { eventIndex = oldChildHMPL->eventIndex(); }
265  const HepMcParticleLink::PositionFlag idxFlag =
267  if (oldChildHMPL) {
268  delete oldChildHMPL;
269  }
270  if (!childGenParticle) {
271  childHMPL = new HepMcParticleLink(childID, eventIndex, idxFlag, HepMcParticleLink::IS_ID);
272  }
273  else {
274  childHMPL = new HepMcParticleLink(childGenParticle, eventIndex, idxFlag);
275  }
276  child->setId(childID);
277  child->setParticleLink(childHMPL);
278  }
279 }
ISF::ISFTruthIncident::parentPdgCode
int parentPdgCode() const override final
Return the PDG Code of the parent particle.
Definition: ISFTruthIncident.cxx:89
HepMC::suggest_barcode
bool suggest_barcode(T &p, int i)
Definition: GenEvent.h:548
ISF::ISFTruthIncident::updateChildParticleProperties
void updateChildParticleProperties()
Update the id and particleLink properties of the child particles (to be called after registerTruthInc...
Definition: ISFTruthIncident.cxx:239
ISF::ISFTruthIncident::childP2
double childP2(unsigned short index) const override final
Return p^2 of the i-th child particle.
Definition: ISFTruthIncident.cxx:132
ISF::ISFTruthIncident::parentParticleAfterIncident
HepMC::GenParticlePtr parentParticleAfterIncident(int newBC) override final
Return the parent particle after the TruthIncident vertex (and give it a new barcode)
Definition: ISFTruthIncident.cxx:113
ISF::ISFTruthIncident::updateHepMCTruthParticle
HepMC::GenParticlePtr updateHepMCTruthParticle(ISF::ISFParticle &particle, ISF::ISFParticle *parent=nullptr) const
convert ISFParticle to GenParticle and attach to ISFParticle's TruthBinding
Definition: ISFTruthIncident.cxx:188
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
ISF::ISFTruthIncident::physicsProcessCode
int physicsProcessCode() const override final
Return specific physics process code of the truth incident (eg ionisation, bremsstrahlung,...
Definition: ISFTruthIncident.cxx:73
GenEvent.h
ISF::ISFTruthIncident::childPdgCode
int childPdgCode(unsigned short index) const override final
Return the PDG Code of the i-th child particle.
Definition: ISFTruthIncident.cxx:145
ISF::ISFTruthIncident::childPt2
double childPt2(unsigned short index) const override final
Return pT^2 of the i-th child particle.
Definition: ISFTruthIncident.cxx:136
ISF::ISFTruthIncident::m_parent
ISF::ISFParticle & m_parent
Definition: ISFTruthIncident.h:106
ISF::ISFParticle::setTruthBinding
void setTruthBinding(TruthBinding *truth)
ISF::ISFTruthIncident::parentSurvivesIncident
bool parentSurvivesIncident() const override final
Return a boolean whether or not the parent particle survives the incident.
Definition: ISFTruthIncident.cxx:109
index
Definition: index.py:1
ISF::TruthBinding
Definition: TruthBinding.h:18
HepMC::SIM_STATUS_INCREMENT
constexpr int SIM_STATUS_INCREMENT
Constant defining the barcode threshold for regenerated particles, i.e. particles surviving an intera...
Definition: MagicNumbers.h:35
ISF::ISFTruthIncident::position
const HepMC::FourVector & position() const override final
Return HepMC position of the truth vertex.
Definition: ISFTruthIncident.cxx:65
ISF::TruthBinding::getCurrentGenParticle
HepMC::GenParticlePtr getCurrentGenParticle()
pointer to the particle in the simulation truth
AtlasDetDescr::AtlasRegion
AtlasRegion
Definition: AtlasRegion.h:27
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
ISF::ISFParticle
Definition: ISFParticle.h:42
ISFTruthIncident.h
ISF::KillPrimary
KillPrimary
Definition: ISFTruthIncident.h:23
ISF::ISFTruthIncident::childBarcode
int childBarcode(unsigned short) const override final
Return the barcode of the i-th child particle (if defined as part of the TruthIncident) otherwise ret...
Definition: ISFTruthIncident.cxx:149
GenParticle.h
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
dqt_zlumi_pandas.mass
mass
Definition: dqt_zlumi_pandas.py:170
ISF::ISFTruthIncident::physicsProcessCategory
int physicsProcessCategory() const override final
Return category of the physics process represented by the truth incident (eg hadronic,...
Definition: ISFTruthIncident.cxx:69
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ISF::ISFParticle::setParticleLink
void setParticleLink(const HepMcParticleLink *partLink)
Definition: ISFParticle.h:173
ISFParticle.h
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
SimpleVector.h
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
lumiFormat.i
int i
Definition: lumiFormat.py:92
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
ISF::ITruthIncident
Definition: ITruthIncident.h:45
ISF::ISFParticleVector
std::vector< ISF::ISFParticle * > ISFParticleVector
ISFParticle vector.
Definition: ISFParticleContainer.h:26
ISF::ISFTruthIncident::parentP2
double parentP2() const override final
Return p^2 of the parent particle.
Definition: ISFTruthIncident.cxx:77
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ISF::ISFParticle::setId
void setId(int id)
set a new unique ID
ISF::ISFParticle::setStatus
void setStatus(int a)
Definition: ISFParticle.h:188
ISF::ISFTruthIncident::childEkin
double childEkin(unsigned short index) const override final
Return Ekin of the i-th child particle.
Definition: ISFTruthIncident.cxx:140
ISF::ISFTruthIncident::parentPt2
double parentPt2() const override final
Return pT^2 of the parent particle.
Definition: ISFTruthIncident.cxx:81
ISF::ISFTruthIncident::ISFTruthIncident
ISFTruthIncident()
ISF::ISFTruthIncident::parentUniqueID
int parentUniqueID() override final
Return the unique ID of the parent particle.
Definition: ISFTruthIncident.cxx:105
ISF::ISFTruthIncident::parentParticle
HepMC::GenParticlePtr parentParticle() override final
Return the parent particle as a HepMC particle type (usually only called for particles that will ente...
Definition: ISFTruthIncident.cxx:97
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
HepMC::SIM_STATUS_THRESHOLD
constexpr int SIM_STATUS_THRESHOLD
Constant definiting the status threshold for simulated particles, eg. can be used to separate generat...
Definition: MagicNumbers.h:38
MagicNumbers.h
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
ISF::ISFParticle::getParticleLink
const HepMcParticleLink * getParticleLink() const
HepMcParticleLink accessors.
Definition: ISFParticle.h:172
ISF::ISFTruthIncident::m_position
const HepMC::FourVector * m_position
Definition: ISFTruthIncident.h:110
DeMoScan.index
string index
Definition: DeMoScan.py:362
HepMC::newGenParticlePtr
GenParticlePtr newGenParticlePtr(const HepMC::FourVector &mom=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), int pid=0, int status=0)
Definition: GenParticle.h:39
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ISF::ISFTruthIncident::updateParentAfterIncidentProperties
void updateParentAfterIncidentProperties()
Update the id and particleLink properties of the parentAfterIncident (to be called after registerTrut...
Definition: ISFTruthIncident.cxx:211
ISF::ISFTruthIncident::parentBarcode
int parentBarcode() override final
Return the barcode of the parent particle.
Definition: ISFTruthIncident.cxx:101
ISF::ISFTruthIncident::childParticle
HepMC::GenParticlePtr childParticle(unsigned short index, int bc) override final
Return the i-th child as a HepMC particle type and assign the given Barcode to the simulator particle...
Definition: ISFTruthIncident.cxx:153
python.DecayParser.children
children
Definition: DecayParser.py:32
ISF::TruthBinding::childTruthBinding
TruthBinding * childTruthBinding(HepMC::GenParticlePtr childP)
Create a TruthBinding for a child particle.
ISF::ISFParticle::setBarcode
void setBarcode(int bc)
set a new barcode
ISF::ISFTruthIncident::parentEkin
double parentEkin() const override final
Return Ekin of the parent particle.
Definition: ISFTruthIncident.cxx:85
ISF::ISFTruthIncident::parentStatus
int parentStatus() override final
Definition: ISFTruthIncident.cxx:93
ISF::ISFTruthIncident::~ISFTruthIncident
virtual ~ISFTruthIncident()
Definition: ISFTruthIncident.cxx:61
ISF::ISFParticle::getTruthBinding
const TruthBinding * getTruthBinding() const
pointer to the simulation truth - optional, can be 0
ISF::ISFTruthIncident::m_children
const ISFParticleVector & m_children
Definition: ISFTruthIncident.h:107
ISF::fKillsPrimary
@ fKillsPrimary
Definition: ISFTruthIncident.h:25
ISF::ISFTruthIncident::getHepMCTruthParticle
HepMC::GenParticlePtr getHepMCTruthParticle(ISF::ISFParticle &particle) const
return attached truth particle
Definition: ISFTruthIncident.cxx:175
EventInfoCnvParams::eventIndex
thread_local event_number_t eventIndex
Definition: IEvtIdModifierSvc.h:34