ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ISF::TruthPreselectionTool Class Reference

#include <TruthPreselectionTool.h>

Inheritance diagram for ISF::TruthPreselectionTool:
Collaboration diagram for ISF::TruthPreselectionTool:

Public Member Functions

 TruthPreselectionTool (const std::string &t, const std::string &n, const IInterface *p)
 Constructor with parameters. More...
 
virtual ~TruthPreselectionTool ()=default
 Destructor. More...
 
virtual StatusCode initialize () override final
 Athena algorithm's interface method initialize() More...
 
virtual std::unique_ptr< HepMC::GenEvent > filterGenEvent (const HepMC::GenEvent &inputEvent) const override final
 IGenEventFilter interface. More...
 

Private Member Functions

bool passesFilters (HepMC::ConstGenParticlePtr part, const ToolHandleArray< IGenParticleFilter > &filters) const
 check if the given particle passes all filters More...
 
bool identifiedQuasiStableParticleForSim (HepMC::ConstGenParticlePtr part) const
 
bool hasQuasiStableAncestorParticle (HepMC::ConstGenParticlePtr part) const
 
bool isPostQuasiStableParticleVertex (HepMC::ConstGenVertexPtr vtx) const
 

Private Attributes

ToolHandleArray< IGenParticleFilterm_genParticleFilters {this, "GenParticleFilters", {}, "Tools for filtering out GenParticles"}
 Filter passes if a difference between the decision of m_genParticleOldFilters and m_genParticleNewFilters is found. More...
 
ToolHandle< IGenParticleFilterm_quasiStableFilter {this, "QuasiStableParticleFilter", "", "Tools for finding quasi-stable particles"}
 

Detailed Description

Definition at line 21 of file TruthPreselectionTool.h.

Constructor & Destructor Documentation

◆ TruthPreselectionTool()

ISF::TruthPreselectionTool::TruthPreselectionTool ( const std::string &  t,
const std::string &  n,
const IInterface *  p 
)

Constructor with parameters.

Definition at line 16 of file TruthPreselectionTool.cxx.

16  :
17  base_class( t, n, p )
18 {
19 }

◆ ~TruthPreselectionTool()

virtual ISF::TruthPreselectionTool::~TruthPreselectionTool ( )
virtualdefault

Destructor.

Member Function Documentation

◆ filterGenEvent()

std::unique_ptr< HepMC::GenEvent > ISF::TruthPreselectionTool::filterGenEvent ( const HepMC::GenEvent &  inputEvent) const
finaloverridevirtual

IGenEventFilter interface.

Definition at line 133 of file TruthPreselectionTool.cxx.

133  {
134 #ifdef HEPMC3
135  std::unique_ptr<HepMC::GenEvent> outputEvent = std::make_unique<HepMC::GenEvent>(inputEvent);
138  if (inputEvent.run_info()) {
139  outputEvent->set_run_info(std::make_shared<HepMC3::GenRunInfo>(*(inputEvent.run_info().get())));
140  }
141  if (inputEvent.heavy_ion()) {
142  outputEvent->set_heavy_ion(std::make_shared<HepMC::GenHeavyIon>(*(inputEvent.heavy_ion())));
143  }
144  HepMC::fillBarcodesAttribute(outputEvent.get());
145 
146  // First loop: flag the particles which should be passed to simulation
147  for (auto& particle: outputEvent->particles()) {
149  if (passesFilters(cparticle, m_genParticleFilters)) {
150  // Particle to be simulated
151  const int shadowId = particle->id();
152  // Version 1 Use the Id
153  particle->add_attribute("ShadowParticleId",
154  std::make_shared<HepMC3::IntAttribute>(shadowId));
155  // Version 2 Directly save the ConstGenParticlePtr - needs to link to a version of the GenEvent after zero-lifetime positioner as been applied.
156  // HepMC::ConstGenParticlePtr& shadow = inputEvent.particles().at(shadowId);
157  // particle->add_attribute("ShadowParticle",
158  // std::make_shared<HepMC::ShadowParticle>(particle));
159  }
160  }
161  // Second loop(s): flag particles (and vertices) to be removed (i.e. those
162  // with ancestor particle flagged to be passed to simulation).
163  for (;;) {
164  std::vector<HepMC::GenParticlePtr> p_to_remove;
165  std::vector<HepMC::GenVertexPtr> v_to_remove;
166  for (auto& particle: outputEvent->particles()) {
168  if (hasQuasiStableAncestorParticle(cparticle)) {
169  p_to_remove.push_back(particle);
170  }
171  }
172  for (auto& particle: p_to_remove) outputEvent->remove_particle(particle);
173  for (auto& vertex: outputEvent->vertices()) {
175  if (isPostQuasiStableParticleVertex(cvertex)) {
176  v_to_remove.push_back(vertex);
177  }
178  }
179  for (auto& vertex: v_to_remove) outputEvent->remove_vertex(vertex);
180  if (p_to_remove.empty() && v_to_remove.empty()) break;
181  }
182 #else
183 
184  std::unique_ptr<HepMC::GenEvent> outputEvent = std::make_unique<HepMC::GenEvent>(inputEvent.signal_process_id(),
185  inputEvent.event_number());
186 
187  outputEvent->set_mpi ( inputEvent.mpi() );
188  outputEvent->set_event_scale ( inputEvent.event_scale() );
189  outputEvent->set_alphaQCD ( inputEvent.alphaQCD() );
190  outputEvent->set_alphaQED ( inputEvent.alphaQED() );
191  if ( inputEvent.cross_section() ) {
192  outputEvent->set_cross_section ( *inputEvent.cross_section());
193  }
194  if (inputEvent.heavy_ion()) {
195  outputEvent->set_heavy_ion(*(inputEvent.heavy_ion()));
196  }
197  if (inputEvent.pdf_info()) {
198  outputEvent->set_pdf_info(*(inputEvent.pdf_info()));
199  }
200  outputEvent->define_units( inputEvent.momentum_unit(), inputEvent.length_unit() );
201 
202  // 1. create a NEW copy of all vertices from inputEvent
203  // taking care to map new vertices onto the vertices being copied
204  // and add these new vertices to this event.
205  // We do not use GenVertex::operator= because that would copy
206  // the attached particles as well.
207  std::map<const HepMC::GenVertex*,HepMC::GenVertex*> inputEvtVtxToOutputEvtVtx;
208  HepMC::GenEvent::vertex_const_iterator currentVertexIter(inputEvent.vertices_begin());
209  const HepMC::GenEvent::vertex_const_iterator endOfCurrentListOfVertices(inputEvent.vertices_end());
210  ATH_MSG_VERBOSE( "Starting a vertex loop ... " );
211  for (; currentVertexIter != endOfCurrentListOfVertices; ++currentVertexIter) {
212  const HepMC::GenVertex *pCurrentVertex(*currentVertexIter);
213  if (isPostQuasiStableParticleVertex(pCurrentVertex)) {
214  continue; // skip vertices created by the simulation
215  }
216  std::unique_ptr<HepMC::GenVertex> copyOfGenVertex =std::make_unique<HepMC::GenVertex>(pCurrentVertex->position(), pCurrentVertex->id(), pCurrentVertex->weights() );
217  copyOfGenVertex->suggest_barcode( HepMC::barcode(pCurrentVertex) );
218  inputEvtVtxToOutputEvtVtx[pCurrentVertex] = copyOfGenVertex.get();
219  outputEvent->add_vertex( copyOfGenVertex.release() );
220  } //vertex loop
221 
222  // 2. copy the signal process vertex info.
223  if ( inputEvent.signal_process_vertex() ) {
224  outputEvent->set_signal_process_vertex( inputEvtVtxToOutputEvtVtx[inputEvent.signal_process_vertex()] );
225  }
226  else {
227  outputEvent->set_signal_process_vertex( nullptr );
228  }
229  //
230  // 3. create a NEW copy of all particles from inputEvent
231  // taking care to attach them to the appropriate vertex
234  for ( HepMC::GenEvent::particle_const_iterator particleIter = inputEvent.particles_begin();
235  particleIter != inputEvent.particles_end(); ++particleIter ) {
236  const HepMC::GenParticle* currentParticle = *particleIter;
237  if (hasQuasiStableAncestorParticle(currentParticle)) { // TODO Consider making the threshold for this check configurable.
238  continue; // skip particles created by the simulation
239  }
240  std::unique_ptr<HepMC::GenParticle> copyOfGenParticle = std::make_unique<HepMC::GenParticle>(*currentParticle);
241  const bool isBeamParticle1(currentParticle == inputEvent.beam_particles().first);
242  const bool isBeamParticle2(currentParticle == inputEvent.beam_particles().second);
243  // There may (will) be particles which had end vertices added by
244  // the simulation (inputEvent). Those vertices will not be copied
245  // to the outputEvent, so we should not try to use them here.
246  const bool shouldAddProdVertex(currentParticle->production_vertex() && inputEvtVtxToOutputEvtVtx[ currentParticle->production_vertex() ]);
247  const bool shouldAddEndVertex(currentParticle->end_vertex() && inputEvtVtxToOutputEvtVtx[ currentParticle->end_vertex() ]);
248  if ( isBeamParticle1 || isBeamParticle2 || shouldAddProdVertex || shouldAddEndVertex ) {
249  HepMC::GenParticle* particleCopy = copyOfGenParticle.release();
250  if ( isBeamParticle1 ) {
251  beam1 = particleCopy;
252  }
253  if ( isBeamParticle2 ) {
254  beam2 = particleCopy;
255  }
256  if ( shouldAddProdVertex || shouldAddEndVertex ) {
257  if ( shouldAddEndVertex ) {
258  inputEvtVtxToOutputEvtVtx[ currentParticle->end_vertex() ]->
259  add_particle_in(particleCopy);
260  }
261  if ( shouldAddProdVertex ) {
262  inputEvtVtxToOutputEvtVtx[ currentParticle->production_vertex() ]->
263  add_particle_out(particleCopy);
264  }
265  }
266  else {
267  ATH_MSG_WARNING ( "Found GenParticle with no production or end vertex! \n" << *currentParticle);
268  }
269  }
270  }
271  outputEvent->set_beam_particles( beam1, beam2 );
272  //
273  // 4. now that vtx/particles are copied, copy weights and random states
274  outputEvent->set_random_states( inputEvent.random_states() );
275  outputEvent->weights() = inputEvent.weights();
276 #endif
277  return outputEvent;
278 }

◆ hasQuasiStableAncestorParticle()

bool ISF::TruthPreselectionTool::hasQuasiStableAncestorParticle ( HepMC::ConstGenParticlePtr  part) const
private

Definition at line 81 of file TruthPreselectionTool.cxx.

83 {
84  // TODO: investigate making this more efficient
85 #ifdef HEPMC3
86  // Recursively loop over ancestral particles looking for a quasi-stable particle
87  if (!part->production_vertex() || part->production_vertex()->particles_in().empty()) { return false; }
88  for ( auto ancestor: part->production_vertex()->particles_in() ) {
89  // Check ancestor particle for Attribute
90  if ( ancestor->attribute<HepMC3::IntAttribute>("ShadowParticleId") ) { return true; }
91 #else
92  if (!part->production_vertex() || part->production_vertex()->particles_in_size()==0) { return false; }
93  // Recursively loop over ancestral particles looking for a quasi-stable particle
94  auto firstParent = part->production_vertex()->particles_begin(HepMC::parents);
95  auto lastParent = part->production_vertex()->particles_end(HepMC::parents);
96  for (auto pitr = firstParent; pitr != lastParent; ++pitr ) {
97  HepMC::ConstGenParticlePtr ancestor = *pitr;
98  if (identifiedQuasiStableParticleForSim(ancestor)) { return true; }
99 #endif
100  if (hasQuasiStableAncestorParticle(ancestor)) { return true; }
101  }
102  return false;
103 }

◆ identifiedQuasiStableParticleForSim()

bool ISF::TruthPreselectionTool::identifiedQuasiStableParticleForSim ( HepMC::ConstGenParticlePtr  part) const
private

Definition at line 62 of file TruthPreselectionTool.cxx.

64 {
65  bool b_sim = false;
66 #ifdef HEPMC3
67  if (m_quasiStableFilter->pass(part)) {
69  }
70 #else
71  if (m_quasiStableFilter->pass(*part)) {
73  }
74 #endif
75  return b_sim;
76 }

◆ initialize()

StatusCode ISF::TruthPreselectionTool::initialize ( )
finaloverridevirtual

Athena algorithm's interface method initialize()

Definition at line 23 of file TruthPreselectionTool.cxx.

24 {
25  ATH_MSG_VERBOSE ( "Initializing TruthPreselectionTool Algorithm" );
26 
27  if (!m_genParticleFilters.empty()) ATH_CHECK(m_genParticleFilters.retrieve());
28  ATH_CHECK(m_quasiStableFilter.retrieve());
29 
30  // intialziation successful
31  return StatusCode::SUCCESS;
32 }

◆ isPostQuasiStableParticleVertex()

bool ISF::TruthPreselectionTool::isPostQuasiStableParticleVertex ( HepMC::ConstGenVertexPtr  vtx) const
private

Definition at line 113 of file TruthPreselectionTool.cxx.

114 {
115  // Recursively loop over ancestral particles looking for a quasi-stable particle
116 #ifdef HEPMC3
117  for ( auto ancestor: vtx->particles_in() ) {
118  // Check ancestor particle for Attribute
119  if ( ancestor->attribute<HepMC3::IntAttribute>("ShadowParticleId") ) { return true; }
120 #else
121  auto firstParent = vtx->particles_in_const_begin();
122  auto lastParent = vtx->particles_in_const_end();
123  for (auto pitr = firstParent; pitr != lastParent; ++pitr ) {
124  HepMC::ConstGenParticlePtr ancestor = *pitr;
125  if (identifiedQuasiStableParticleForSim(ancestor)) { return true; }
126 #endif
127  if (hasQuasiStableAncestorParticle(ancestor)) { return true; }
128  }
129  return false;
130 }

◆ passesFilters()

bool ISF::TruthPreselectionTool::passesFilters ( HepMC::ConstGenParticlePtr  part,
const ToolHandleArray< IGenParticleFilter > &  filters 
) const
private

check if the given particle passes all filters

Definition at line 38 of file TruthPreselectionTool.cxx.

40 {
41  // TODO: implement this as a std::find_if with a lambda function
42  for ( const auto& filter : filters ) {
43  // determine if the particle passes current filter
44 #ifdef HEPMC3
45  bool passFilter = filter->pass(part);
46 #else
47  bool passFilter = filter->pass(*part);
48 #endif
49  ATH_MSG_VERBOSE("Filter '" << filter.typeAndName() << "' returned: "
50  << (passFilter ? "true, will keep particle."
51  : "false, will remove particle."));
52 
53  if (!passFilter) return false;
54  }
55 
56  return true;
57 }

Member Data Documentation

◆ m_genParticleFilters

ToolHandleArray<IGenParticleFilter> ISF::TruthPreselectionTool::m_genParticleFilters {this, "GenParticleFilters", {}, "Tools for filtering out GenParticles"}
private

Filter passes if a difference between the decision of m_genParticleOldFilters and m_genParticleNewFilters is found.

m_genParticleCommonFilters is applied before to select relevant particles. If only m_genParticleCommonFilters is specified, filter passes if any particle passes this one HepMC::GenParticle filters for both selections

Definition at line 52 of file TruthPreselectionTool.h.

◆ m_quasiStableFilter

ToolHandle<IGenParticleFilter> ISF::TruthPreselectionTool::m_quasiStableFilter {this, "QuasiStableParticleFilter", "", "Tools for finding quasi-stable particles"}
private

Definition at line 53 of file TruthPreselectionTool.h.


The documentation for this class was generated from the following files:
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
ISF::TruthPreselectionTool::isPostQuasiStableParticleVertex
bool isPostQuasiStableParticleVertex(HepMC::ConstGenVertexPtr vtx) const
Definition: TruthPreselectionTool.cxx:113
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
ForwardTracker::beam1
@ beam1
Definition: ForwardTrackerConstants.h:13
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
ForwardTracker::beam2
@ beam2
Definition: ForwardTrackerConstants.h:13
ISF::TruthPreselectionTool::identifiedQuasiStableParticleForSim
bool identifiedQuasiStableParticleForSim(HepMC::ConstGenParticlePtr part) const
Definition: TruthPreselectionTool.cxx:62
ISF::TruthPreselectionTool::hasQuasiStableAncestorParticle
bool hasQuasiStableAncestorParticle(HepMC::ConstGenParticlePtr part) const
Definition: TruthPreselectionTool.cxx:81
python.DecayParser.parents
parents
print ("==> buf:",buf)
Definition: DecayParser.py:31
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
covarianceTool.passFilter
bool passFilter
Definition: covarianceTool.py:604
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:504
covarianceTool.filter
filter
Definition: covarianceTool.py:514
ISF::TruthPreselectionTool::m_genParticleFilters
ToolHandleArray< IGenParticleFilter > m_genParticleFilters
Filter passes if a difference between the decision of m_genParticleOldFilters and m_genParticleNewFil...
Definition: TruthPreselectionTool.h:52
beamspotman.n
n
Definition: beamspotman.py:731
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
ISF::TruthPreselectionTool::passesFilters
bool passesFilters(HepMC::ConstGenParticlePtr part, const ToolHandleArray< IGenParticleFilter > &filters) const
check if the given particle passes all filters
Definition: TruthPreselectionTool.cxx:38
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
ISF::TruthPreselectionTool::m_quasiStableFilter
ToolHandle< IGenParticleFilter > m_quasiStableFilter
Definition: TruthPreselectionTool.h:53
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
GenParticle
@ GenParticle
Definition: TruthClasses.h:30