HepMC event record
FindParticles.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file FindParticles.cc
8  * @brief Implementation of \b class FindParticles
9  *
10  */
11 #include "HepMC/Search/FindParticles.h"
12 #include "HepMC/Search/FilterList.h"
13 #include "HepMC/Search/Filter.h"
14 
15 #include "HepMC/GenEvent.h"
16 #include "HepMC/GenVertex.h"
17 #include "HepMC/GenParticle.h"
18 
19 namespace HepMC {
20 
21 
22 FindParticles::FindParticles(const GenEvent &evt, FilterEvent filter_type, FilterList filter_list) {
23 
24  FOREACH( const GenParticlePtr &p, evt.particles() ) {
25 
26  if( passed_all_filters(p,filter_list) ) {
27  if( filter_type == FIND_LAST ) m_results.clear();
28 
29  m_results.push_back( p );
30 
31  if( filter_type == FIND_FIRST ) return;
32  }
33  }
34 }
35 
36 
38 
39  switch(filter_type) {
40  case FIND_ALL_ANCESTORS:
41  if( !p->production_vertex() ) break;
42 
43  recursive_check_ancestors( p->production_vertex(), filter_list );
44  break;
45  case FIND_ALL_DESCENDANTS:
46  if( !p->end_vertex() ) break;
47 
48  recursive_check_descendants( p->end_vertex(), filter_list );
49  break;
50  case FIND_MOTHERS:
51  if( !p->production_vertex() ) break;
52 
53  FOREACH( const GenParticlePtr &p_in, p->production_vertex()->particles_in() ) {
54 
55  if( passed_all_filters(p_in,filter_list) ) {
56  m_results.push_back( p_in );
57  }
58  }
59  break;
60  case FIND_DAUGHTERS:
61  if( !p->end_vertex() ) break;
62 
63  FOREACH( const GenParticlePtr &p_out, p->end_vertex()->particles_out() ) {
64 
65  if( passed_all_filters(p_out,filter_list) ) {
66  m_results.push_back( p_out );
67  }
68  }
69  break;
70  case FIND_PRODUCTION_SIBLINGS:
71  if( !p->end_vertex() ) break;
72 
73  FOREACH( const GenParticlePtr &p_in, p->end_vertex()->particles_in() ) {
74 
75  if( passed_all_filters(p_in,filter_list) ) {
76  m_results.push_back( p_in );
77  }
78  }
79  break;
80  };
81 }
82 
83 
85 
86  switch(filter_type) {
87  case FIND_ALL_ANCESTORS:
88  recursive_check_ancestors( v, filter_list );
89  break;
90  case FIND_ALL_DESCENDANTS:
91  recursive_check_descendants( v, filter_list );
92  break;
93  case FIND_MOTHERS:
94  FOREACH( const GenParticlePtr &p_in, v->particles_in() ) {
95  if ( passed_all_filters(p_in,filter_list) ) {
96  m_results.push_back( p_in );
97  }
98  }
99  break;
100  case FIND_DAUGHTERS:
101  FOREACH( const GenParticlePtr &p_out, v->particles_out() ) {
102  if ( passed_all_filters(p_out,filter_list) ) {
103  m_results.push_back( p_out );
104  }
105  }
106  break;
107  case FIND_PRODUCTION_SIBLINGS:
108  default:
109  throw Exception("Invalid filter type provided for FindParticles(GenVertexPtr)");
110  };
111 }
112 
113 
115 
116  int first_null = -1;
117 
118  // cost-efficient removing of particles that didn't pass filters
119  for(unsigned int i=0; i<m_results.size(); ++i) {
120 
121  if( !passed_all_filters(m_results[i],filter_list) ) {
122 
123  if( first_null < 0 ) first_null = i;
124  }
125  else {
126  if( first_null >= 0 ) {
127  m_results[first_null] = m_results[i];
128  ++first_null;
129  }
130  }
131  }
132 
133  if( first_null >= 0 ) m_results.resize( first_null );
134 }
135 
137  if( filter_list.filters().size() == 0 ) return true;
138 
139  FOREACH( const Filter &f, filter_list.filters() ) {
140  if( f.passed_filter(p) == false ) return false;
141  }
142 
143  DEBUG( 10, "Filter: passed" )
144  return true;
145 }
146 
148 
149  FOREACH( const GenVertexPtr &v_list, m_checked_vertices ) {
150  if( v_list->id() == v->id() ) return;
151  }
152 
153  m_checked_vertices.push_back(v);
154 
155  FOREACH( const GenParticlePtr &p_in, v->particles_in() ) {
156 
157  if( passed_all_filters(p_in,filter_list) ) {
158  m_results.push_back(p_in);
159  }
160 
161  if( !p_in->production_vertex() ) continue;
162  recursive_check_ancestors( p_in->production_vertex(), filter_list );
163  }
164 }
165 
167 
168  FOREACH( const GenVertexPtr &v_list, m_checked_vertices ) {
169  if( v_list->id() == v->id() ) return;
170  }
171 
172  m_checked_vertices.push_back(v);
173 
174  FOREACH( const GenParticlePtr &p_out, v->particles_out() ) {
175  if( passed_all_filters(p_out,filter_list) ) {
176  m_results.push_back(p_out);
177  }
178 
179  if( !p_out->end_vertex() ) continue;
180  recursive_check_descendants( p_out->end_vertex(), filter_list );
181  }
182 }
183 
184 } // namespace HepMC
const std::vector< GenParticlePtr > & particles() const
Get list of particles (const)
List of filters for the search engine.
bool passed_all_filters(const GenParticlePtr &p, FilterList &filter_list)
Check if particle passed all filters.
Class used to define filters for search engine.
const vector< Filter > & filters()
Get list of filters.
FindParticles(const GenEvent &evt, FilterEvent filter_type, FilterList filter_list=FilterList())
GenEvent-based constructor.
void narrow_down(FilterList filter_list)
Narrow down the results applying additional filters.
Stores event-related information.
vector< GenVertexPtr > m_checked_vertices
List of already checked vertices.
FilterType
List of methods of searching through all particles in the event.
bool passed_filter(const GenParticlePtr &p) const
Check if HepMC::GenParticle passed this filter.
Definition: Filter.cc:20
void recursive_check_descendants(const GenVertexPtr &v, FilterList &filter_list)
Check if all descendants passed the filter.
void recursive_check_ancestors(const GenVertexPtr &v, FilterList &filter_list)
Check if all ancestors passed the filter.
Definition of template class SmartPointer.
vector< GenParticlePtr > m_results
List of results.
Relationship
List of methods of searching starting from a particle or vertex.