ParticleNavigator.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief Particle list in DetSim contains Monte Carlo particle information.
3 ///
4 /// \author seligman@nevis.columbia.edu
5 /// \date
6 ////////////////////////////////////////////////////////////////////////
7 
8 #ifndef SIM_PARTICLENAVIGATOR_H
9 #define SIM_PARTICLENAVIGATOR_H
10 
11 #include <iostream>
12 #include <map>
13 #include <set>
14 
15 #include "Simulation/Particle.h"
16 
17 namespace sim
18 {
19  // Forward declarations.
20  class EveIdCalculator;
21 
22  /// A container for particles generated during an event simulation.
23  /// It acts like a map<int,Particle*> but with additional features:
24  ///
25  /// - A method Cut(double) that will remove all particles with energy
26  /// less than the argument.
27  ///
28  /// - Methods TrackId(int) and Particle(int) for those who are unfamiliar with the
29  /// concepts associated with STL maps:
30  /// sim::ParticleNavigator* particleList = // ...;
31  /// int numberOfParticles = particleList->size();
32  /// for (int i=0; i<numberOfParticles; ++i)
33  /// {
34  /// int trackID = particleList->TrackId(i);
35  /// sim::Particle* particle = particleList->Particle(i);
36  /// }
37  /// The STL equivalent to the above statements (more efficient):
38  /// sim::ParticleNavigator* particleList = // ...;
39  /// for ( sim::ParticleNavigator::const_iterator i = particleList->begin();
40  /// i != particleList->end(); ++i )
41  /// {
42  /// const sim::Particle* particle = (*i).second;
43  /// int trackID = particle->TrackId(); // or...
44  /// int trackID = (*i).first;
45  /// }
46  ///
47  /// - Methods to access the list of primary particles in the event:
48  /// sim::ParticleNavigator particleList = // ...;
49  /// int numberOfPrimaries = particleList->NumberOfPrimaries();
50  /// for ( int i = 0; i != numberOfPrimaries; ++i )
51  /// {
52  /// sim::Particle* particle = particleList->Primary(i);
53  /// ...
54  /// }
55  /// There's also a simple test:
56  /// int trackID = // ...;
57  /// if ( particleList->IsPrimary(trackID) ) {...}
58  ///
59  /// (Aside: note that particleList[i] does NOT give you the "i-th"
60  /// particle in the list; it gives you the particle whose trackID
61  /// is "i".)
62  ///
63  /// - A method EveId(int) to determine the "eve ID" (or ultimate
64  /// mother) for a given particle. For more information, including how
65  /// to supply your own eve ID calculation, see
66  /// Simulation/EveIdCalculator.h and Simulation/EmEveIdCalculator.h.
67  ///
68  /// - If you use the clear() or erase() methods, the list will also
69  /// delete the underlying Particle*. (This means that if you use
70  /// insert() or Add() to add a particle to the list, the
71  /// ParticleNavigator will take over management of it. Don't delete the
72  /// pointer yourself!)
73  ///
74  /// - Print() and operator<< methods for ROOT display and ease of
75  /// debugging.
77  public:
78  // Some type definitions to make life easier, and to help "hide"
79  // the implementation details. (If you're not familiar with STL,
80  // you can ignore these definitions.)
81  typedef std::map<int,sim::Particle*> list_type;
82  typedef list_type::key_type key_type;
83  typedef list_type::mapped_type mapped_type;
84  typedef list_type::value_type value_type;
85  typedef list_type::iterator iterator;
86  typedef list_type::const_iterator const_iterator;
87  typedef list_type::reverse_iterator reverse_iterator;
88  typedef list_type::const_reverse_iterator const_reverse_iterator;
89  typedef list_type::size_type size_type;
90  typedef list_type::difference_type difference_type;
91  typedef list_type::key_compare key_compare;
92  typedef list_type::allocator_type allocator_type;
93 
94  // Standard constructor and destructor.
96  explicit ParticleNavigator(const std::vector<sim::Particle>& parts);
97  virtual ~ParticleNavigator();
98 
99  // Because this list contains pointers, we have to provide the
100  // copy and assignment constructors.
101  ParticleNavigator( const ParticleNavigator& rhs );
103 
104  // The methods advertised above:
105 
106  /// Apply an energy threshold cut to the particles in the list,
107  /// removing all those that fall below the cut.
108  void Cut( const double& );
109 
110  const key_type& TrackId( const size_type ) const;
111  mapped_type Particle( const size_type ) const;
112  mapped_type Particle( const size_type );
113 
114  bool IsPrimary( int trackID ) const;
115  int NumberOfPrimaries() const;
116  const sim::Particle* Primary( const int ) const;
117  sim::Particle* Primary( const int );
118 
119  // Implementation note: we haven't defined a "+=" for adding the
120  // integer offset, because it would involve many insertions and
121  // deletions into the same map, or be equivalent to creating a new
122  // map and deleting an old one.
123  ParticleNavigator Add(const int& offset) const;
124  ParticleNavigator operator+(const int& value) const
125  {
126  return Add(value);
127  }
128 
129  // Just in case: define the result of "scalar + ParticleNavigator" to be
130  // the same as "ParticleNavigator + scalar".
131  friend ParticleNavigator operator+(const int& value, const ParticleNavigator& list);
132 
133  // Standard STL methods, to make this class look like an STL map.
134  // Again, if you don't know STL, you can just ignore these
135  // methods.
136  iterator begin() { return fParticleList.begin(); }
137  const_iterator begin() const { return fParticleList.begin(); }
138  iterator end() { return fParticleList.end(); }
139  const_iterator end() const { return fParticleList.end(); }
140  reverse_iterator rbegin() { return fParticleList.rbegin(); }
141  const_reverse_iterator rbegin() const { return fParticleList.rbegin(); }
142  reverse_iterator rend() { return fParticleList.rend(); }
143  const_reverse_iterator rend() const { return fParticleList.rend(); }
144 
145  size_type size() const { return fParticleList.size(); }
146  bool empty() const { return fParticleList.empty(); }
148 
149  iterator find(const key_type& key) { return fParticleList.find(key); }
150  const_iterator find(const key_type& key) const { return fParticleList.find(key); }
151  iterator upper_bound(const key_type& key) { return fParticleList.upper_bound(key); }
152  const_iterator upper_bound(const key_type& key) const { return fParticleList.upper_bound(key); }
153  iterator lower_bound(const key_type& key) { return fParticleList.lower_bound(key); }
154  const_iterator lower_bound(const key_type& key) const { return fParticleList.lower_bound(key); }
155 
156  /// Be careful when using operator[] here! It takes the track ID as the argument:
157  /// sim::ParticleNavigator partList;
158  /// const sim::Particle* = partList[3];
159  /// The above line means the particle with trackID==3, NOT the third
160  /// particle in the list! Use partList.Particle(3) if you want to
161  /// get the particles by index number instead of track ID.
162  /// Note that this only works in a const context. Use the insert()
163  /// or Add() methods to add a new particle to the list.
164  mapped_type operator[]( const key_type& key ) const;
165  /// This non-const version of operator[] does NOT permit you to insert
166  /// Particles into the list. Use Add() or insert() for that.
167  mapped_type operator[]( const key_type& key );
168  mapped_type at(const key_type& key) { return operator[](key); }
169  mapped_type at(const key_type& key) const { return operator[](key); }
170 
171  /// These two methods (insert and Add) do the same thing:
172  /// - Add the Particle to the list, using the track ID as the key.
173  /// - Update the list of primary particles as needed.
174  /// Note that when you insert a Particle* into a ParticleNavigator, it
175  /// takes over management of the pointer. Don't delete it yourself!
176  void insert( sim::Particle* value );
177  void Add( sim::Particle* value ) { insert(value); }
178 
179  void clear();
180  size_type erase( const key_type& key );
181 
182  friend std::ostream& operator<< ( std::ostream& output, const ParticleNavigator& );
183 
184  // Methods associated with the eve ID calculation.
185  // Calculate the eve ID.
186  int EveId ( const int trackID ) const;
187  /// Set a pointer to a different eve ID calculation. The name
188  /// begins with "Adopt" because it accepts control of the ponters;
189  /// do NOT delete the pointer yourself if you use this method.
190  static void AdoptEveIdCalculator( EveIdCalculator* );
191 
192  private:
193  list_type fParticleList; ///< Sorted list of particles in the event
194 
195  typedef std::set< int > primaries_type;
196  typedef primaries_type::iterator primaries_iterator;
197  typedef primaries_type::const_iterator primaries_const_iterator;
198  primaries_type fPrimaries; ///< Sorted list of the track IDs of primary particles.
199 
200  public:
201  };
202 }
203 
204 #endif // SIM_PARTICLENAVIGATOR_H
205 ///////////////////////////////////////////////////////////////////////////
list_type::size_type size_type
const_iterator find(const key_type &key) const
list_type::key_type key_type
const_reverse_iterator rend() const
ofstream output
list_type fParticleList
Sorted list of particles in the event.
friend std::ostream & operator<<(std::ostream &output, const ParticleNavigator &)
primaries_type::iterator primaries_iterator
static void AdoptEveIdCalculator(EveIdCalculator *)
list_type::key_compare key_compare
size_type size() const
list_type::const_iterator const_iterator
const_iterator begin() const
const_reverse_iterator rbegin() const
void Cut(const double &)
list_type::reverse_iterator reverse_iterator
list_type::iterator iterator
iterator upper_bound(const key_type &key)
size_type erase(const key_type &key)
list_type::mapped_type mapped_type
list_type::difference_type difference_type
const key_type & TrackId(const size_type) const
ParticleNavigator operator+(const int &value) const
void swap(ParticleNavigator &other)
const sim::Particle * Primary(const int) const
const XML_Char int const XML_Char * value
Definition: expat.h:331
const_iterator end() const
list_type::allocator_type allocator_type
std::set< int > primaries_type
void Add(sim::Particle *value)
const_iterator lower_bound(const key_type &key) const
list_type::const_reverse_iterator const_reverse_iterator
const_iterator upper_bound(const key_type &key) const
primaries_type::const_iterator primaries_const_iterator
reverse_iterator rend()
Example routine for calculating the "ultimate e-m mother" of a particle in a simulated event...
Definition: FillTruth.h:16
bool IsPrimary(int trackID) const
reverse_iterator rbegin()
void insert(sim::Particle *value)
mapped_type operator[](const key_type &key) const
std::map< int, sim::Particle * > list_type
ParticleNavigator Add(const int &offset) const
Int_t trackID
Definition: plot.C:84
ParticleNavigator & operator=(const ParticleNavigator &rhs)
mapped_type at(const key_type &key)
iterator find(const key_type &key)
mapped_type at(const key_type &key) const
list_type::value_type value_type
primaries_type fPrimaries
Sorted list of the track IDs of primary particles.
iterator lower_bound(const key_type &key)
mapped_type Particle(const size_type) const
int EveId(const int trackID) const