Belle II Software  release-05-02-19
MCParticleGraph.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #ifndef MCPARTICLEGRAPH_H
12 #define MCPARTICLEGRAPH_H
13 
14 #include <framework/core/FrameworkExceptions.h>
15 #include <framework/core/MemoryPool.h>
16 #include <mdst/dataobjects/MCParticle.h>
17 
18 #include <set>
19 #include <string>
20 
21 
22 namespace Belle2 {
40  class MCParticleGraph {
41 
42  public:
43 
44  //Exception definition
45 
47  BELLE2_DEFINE_EXCEPTION(CyclicReferenceError, "Cyclic decay, cannot continue")
49  BELLE2_DEFINE_EXCEPTION(NotSameGraphError, "Particles not from same graph")
51  BELLE2_DEFINE_EXCEPTION(NonContinousDaughtersError, "Can not represent decay graph, non continuous indices for daughters")
53  BELLE2_DEFINE_EXCEPTION(DaughterHasMotherError,
54  "A daughter particle was already assigned to a mother. A particle can't have two mothers!")
56  BELLE2_DEFINE_EXCEPTION(OutOfRangeError, "Index out of range")
57 
59  typedef std::pair<unsigned int, unsigned int> DecayLine;
60 
62  enum GraphOptions {
63  c_setNothing = 0,
64  c_setDecayVertex = 1,
65  c_setDecayTime = 2,
67  c_checkCyclic = 4,
68  c_clearParticles = 8
69  };
70 
78  class GraphParticle: public MCParticle {
79 
80  public:
88  GraphParticle& operator=(const MCParticle& particle)
89  {
90  MCParticle::operator=(particle);
91  //The pointer to the TClonesArray the MCParticle is stored in makes no
92  //sense inside the Graph so we set it to some invalid value to avoid
93  //the MCParticle::fixParticleList() to complain
94  m_plist = (TClonesArray*) - 1;
95  return *this;
96  }
97 
102  void decaysInto(GraphParticle& daughter) { m_graph->addDecay(*this, daughter); }
103 
108  void comesFrom(GraphParticle& mother) { m_graph->addDecay(mother, *this); }
109 
117  void setFirstDaughter(int daughter) { m_firstDaughter = daughter; }
118 
125  void setLastDaughter(int daughter) { m_lastDaughter = daughter; }
126 
135  void setIgnore(bool ignore = true) { m_ignore = ignore; }
136 
142  bool getIgnore() const { return m_ignore; }
143 
150  void setTrackID(int trackID) { m_trackID = trackID; }
151 
157  int getTrackID() const { return m_trackID; }
158 
159 
160  private:
161 
165  GraphParticle() = delete;
166 
170  GraphParticle(const GraphParticle&) = delete;
171 
175  GraphParticle(TClonesArray*, const MCParticle&) {}
176 
183  GraphParticle(MCParticleGraph* graph, unsigned int vertexId): MCParticle(),
184  m_graph(graph), m_vertexId(vertexId)
185  {
186  //The pointer to the TClonesArray the MCParticle is stored in makes no
187  //sense inside the Graph so we set it to some invalid value to avoid
188  //the MCParticle::fixParticleList() to complain
189  m_plist = (TClonesArray*) - 1;
190  }
191 
196  void setIndex(int index) { m_index = index; }
197 
199  unsigned int m_vertexId{0};
200  bool m_ignore{false};
201  bool m_primary{true};
202  int m_trackID{0};
204  friend class MCParticleGraph;
205  friend class ParticleSorter;
206  };
207 
211  MCParticleGraph() {}
212 
217  virtual ~MCParticleGraph() { clear(); }
218 
224  GraphParticle& addParticle();
225 
233  void addDecay(GraphParticle& mother, GraphParticle& daughter);
234 
239  GraphParticle& operator[](size_t i) { if (i >= m_particles.size()) throw OutOfRangeError(); return *m_particles[i]; }
240 
245  size_t size() const { return m_particles.size(); }
246 
259  void generateList(const std::string& name = "", int options = c_setNothing);
260 
261 
267  void loadList(const std::string& name = "");
268 
274  void clear();
275 
276 
277  protected:
278 
283  class ParticleSorter;
284 
285  MemoryPool<GraphParticle> m_particles;
286  std::set<DecayLine> m_decays;
287  };
288 
289 
290  inline void MCParticleGraph::clear()
291  {
292  m_particles.clear();
293  m_decays.clear();
294  }
295 
296 
298  {
299  unsigned int index = m_particles.size() + 1;
301  return *p;
302  }
303 
304 
306  {
307  if (this != mother.m_graph || this != daughter.m_graph) throw NotSameGraphError();
308  //if (daughter.getMother() != NULL) throw DaughterHasMotherError();
309  m_decays.insert(DecayLine(mother.m_vertexId, daughter.m_vertexId));
310  daughter.m_primary = false;
311  }
312 
314 } // end namespace Belle2
315 
316 #endif //MCPARTICLEGRAPH_H
Belle2::MCParticleGraph::generateList
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
Definition: MCParticleGraph.cc:211
Belle2::MCParticleGraph::GraphParticle::setTrackID
void setTrackID(int trackID)
Set the track ID for the particle.
Definition: MCParticleGraph.h:158
Belle2::MCParticleGraph::~MCParticleGraph
virtual ~MCParticleGraph()
Destructor.
Definition: MCParticleGraph.h:225
Belle2::MCParticle::m_lastDaughter
int m_lastDaughter
1-based index of last daughter particle in collection, 0 if no daughters
Definition: MCParticle.h:557
Belle2::MCParticleGraph::size
size_t size() const
Return the number of particles in the graph.
Definition: MCParticleGraph.h:253
Belle2::MCParticleGraph::GraphParticle::setIgnore
void setIgnore(bool ignore=true)
Set or remove the ignore flag.
Definition: MCParticleGraph.h:143
Belle2::MCParticleGraph::GraphParticle::setIndex
void setIndex(int index)
Set the 1-based index of the particle.
Definition: MCParticleGraph.h:204
Belle2::MCParticle::m_plist
TClonesArray * m_plist
Internal pointer to DataStore Array containing particles belonging to this collection.
Definition: MCParticle.h:527
Belle2::RelationsInterface::operator=
RelationsInterface & operator=(const RelationsInterface &relationsInterface)
Assignment operator.
Definition: RelationsObject.h:130
Belle2::MCParticleGraph::GraphParticle::m_primary
bool m_primary
Is this a primary particle ?
Definition: MCParticleGraph.h:209
Belle2::MCParticleGraph::GraphParticle::m_trackID
int m_trackID
The track ID from geant4 that created this particle.
Definition: MCParticleGraph.h:210
Belle2::MCParticleGraph::GraphOptions
GraphOptions
The exception is thrown if a cyclic reference in the graph was detected.
Definition: MCParticleGraph.h:70
Belle2::MCParticleGraph
Class to build, validate and sort a particle decay chain.
Definition: MCParticleGraph.h:48
Belle2::MCParticleGraph::ParticleSorter
Class to go over all the particles in the Graph an sort them in a sensible way.
Definition: MCParticleGraph.cc:51
Belle2::MCParticleGraph::c_checkCyclic
@ c_checkCyclic
Check for cyclic dependencies.
Definition: MCParticleGraph.h:75
Belle2::MCParticleGraph::c_setNothing
@ c_setNothing
Do nothing special.
Definition: MCParticleGraph.h:71
Belle2::MCParticleGraph::GraphParticle::operator=
GraphParticle & operator=(const MCParticle &particle)
Assign the values of an existing MCParticle to this GraphParticle.
Definition: MCParticleGraph.h:96
Belle2::MCParticleGraph::c_setDecayTime
@ c_setDecayTime
Set decay time to the largest production time of the daughters.
Definition: MCParticleGraph.h:73
Belle2::MCParticleGraph::GraphParticle::getTrackID
int getTrackID() const
Returns the track ID assigned to this MCParticle.
Definition: MCParticleGraph.h:165
Belle2::MCParticleGraph::loadList
void loadList(const std::string &name="")
Load the MCParticle list given by name into the Graph.
Definition: MCParticleGraph.cc:241
Belle2::MCParticleGraph::c_setDecayInfo
@ c_setDecayInfo
Set decay time and vertex.
Definition: MCParticleGraph.h:74
Belle2::MCParticleGraph::GraphParticle::m_graph
MCParticleGraph * m_graph
internal pointer to the graph this particle belongs to
Definition: MCParticleGraph.h:206
Belle2::MCParticleGraph::GraphParticle::GraphParticle
GraphParticle(TClonesArray *, const MCParticle &)
Hide MCParticle "almost copy" constructor.
Definition: MCParticleGraph.h:183
Belle2::MCParticleGraph::c_setDecayVertex
@ c_setDecayVertex
Set the decay vertex to the production vertex of the last daughter (ordered by production time)
Definition: MCParticleGraph.h:72
Belle2::MCParticleGraph::GraphParticle::setLastDaughter
void setLastDaughter(int daughter)
Set the 1-based index of the last daughter, 0 means no daughters.
Definition: MCParticleGraph.h:133
Belle2::MCParticle::m_firstDaughter
int m_firstDaughter
1-based index of first daughter particle in collection, 0 if no daughters
Definition: MCParticle.h:556
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MCParticleGraph::MCParticleGraph
MCParticleGraph()
Constructor.
Definition: MCParticleGraph.h:219
Belle2::MCParticleGraph::m_particles
MemoryPool< GraphParticle > m_particles
internal list of particles
Definition: MCParticleGraph.h:291
Belle2::MCParticleGraph::addParticle
GraphParticle & addParticle()
Add new particle to the graph.
Definition: MCParticleGraph.h:305
Belle2::MCParticleGraph::GraphParticle::m_ignore
bool m_ignore
ignore particle when writing MCParticle list ?
Definition: MCParticleGraph.h:208
Belle2::MCParticleGraph::GraphParticle::GraphParticle
GraphParticle()=delete
No default constructor.
Belle2::MCParticle::m_index
int m_index
transient pointer to particle list
Definition: MCParticle.h:533
Belle2::MCParticleGraph::m_decays
std::set< DecayLine > m_decays
internal set of decay lines
Definition: MCParticleGraph.h:294
Belle2::MCParticleGraph::c_clearParticles
@ c_clearParticles
Clear the particle list before adding the graph.
Definition: MCParticleGraph.h:76
Belle2::MCParticleGraph::GraphParticle::comesFrom
void comesFrom(GraphParticle &mother)
Tells the graph that this particle is a decay product of mother.
Definition: MCParticleGraph.h:116
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::MCParticleGraph::GraphParticle::decaysInto
void decaysInto(GraphParticle &daughter)
Tells the graph that this particle decays into daughter.
Definition: MCParticleGraph.h:110
BELLE2_DEFINE_EXCEPTION
#define BELLE2_DEFINE_EXCEPTION(ClassName, Message)
Macro that defines an exception with the given message template.
Definition: FrameworkExceptions.h:46
Belle2::MCParticleGraph::clear
void clear()
Reset particles and decay information to make the class reusable.
Definition: MCParticleGraph.h:298
Belle2::MCParticleGraph::GraphParticle::getIgnore
bool getIgnore() const
Get the ignore flag.
Definition: MCParticleGraph.h:150
Belle2::MCParticleGraph::operator[]
GraphParticle & operator[](size_t i)
Return reference to added particle with range check.
Definition: MCParticleGraph.h:247
Belle2::MCParticleGraph::GraphParticle
Class to represent Particle data in graph.
Definition: MCParticleGraph.h:86
Belle2::MCParticleGraph::GraphParticle::m_vertexId
unsigned int m_vertexId
vertex id in the graph
Definition: MCParticleGraph.h:207
Belle2::MCParticleGraph::addDecay
void addDecay(GraphParticle &mother, GraphParticle &daughter)
Add decay information between two particles.
Definition: MCParticleGraph.h:313
Belle2::MCParticleGraph::GraphParticle::setFirstDaughter
void setFirstDaughter(int daughter)
Set the 1-based index of the first daughter, 0 means no daughters.
Definition: MCParticleGraph.h:125