Belle II Software development
MCParticleGraph.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <mdst/dataobjects/MCParticleGraph.h>
10
11#include <framework/datastore/StoreArray.h>
12#include <framework/logging/Logger.h>
13
14#include <boost/graph/graph_traits.hpp>
15#include <boost/graph/adjacency_list.hpp>
16#include <boost/graph/depth_first_search.hpp>
17
18#include <limits>
19#include <vector>
20#include <queue>
21
22
23using namespace std;
24using namespace Belle2;
25
27struct cycle_detector : public boost::dfs_visitor<> {
35 template <class Edge, class Graph> void back_edge(Edge, Graph&) { throw MCParticleGraph::CyclicReferenceError(); }
36};
37
38
49
50public:
51
59 ParticleSorter(MemoryPool<MCParticleGraph::GraphParticle>& particles, TClonesArray* plist, bool setVertex, bool setTime):
60 m_index(0), m_particles(particles), m_plist(plist), m_setVertex(setVertex), m_setTime(setTime) {}
61
67 void setStartIndex(int index) { m_index = index; }
68
73 template <class Graph> void sort(Graph& g)
74 {
75 //Set seen flag for all vertices to false
76 m_seen.clear();
77 m_seen.resize(num_vertices(g), false);
78
79 //Create a dummy GraphParticle, needed only to find all primary particles
81
82 //Add all direct children of the 0th vertex to the queue.
83 //This are the primary particles
84 find_daughters(0, g, dummy);
85
86 //Go through the queue and write out each particle.
87 //Daughters of particles will be added to the queue by find_daughters
88 while (!m_vqueue.empty()) {
89 unsigned int cur = m_vqueue.front();
90 m_vqueue.pop();
91 finish_vertex(cur, g);
92 }
93 }
94
95
101 template <class Vertex, class Graph> void finish_vertex(Vertex v, Graph& g)
102 {
104
105 //Reset daughter information, will be filled by find_daughters
106 p.setFirstDaughter(0);
107 p.setLastDaughter(0);
108 //Find all direct daughters
109 find_daughters(v, g, p);
110
111 //If stable particle, set decaytime to infinity
112 if (out_degree(v, g) == 0 && m_setTime) {
113 p.setDecayTime(numeric_limits<double>::infinity());
114 }
115 //If given a pointer to a TClonesArray, create MCParticle at the appropriate index position
116 if (m_plist) {
117 new (m_plist->AddrAt(p.getIndex() - 1)) MCParticle(m_plist, p);
118 }
119 }
120
121
128 template <class Vertex, class Graph> void find_daughters(Vertex v, Graph& g, MCParticleGraph::GraphParticle& mother)
129 {
130 //References to the daughter information of the mother for easier access
131 int& d1 = mother.m_firstDaughter;
132 int& d2 = mother.m_lastDaughter;
133
134 typename boost::graph_traits<Graph>::out_edge_iterator j, j_end;
135 for (tie(j, j_end) = out_edges(v, g); j != j_end; ++j) {
136 //Get daughter particle from list
137 Vertex nv = target(*j, g);
138 MCParticleGraph::GraphParticle& daughter = *m_particles[nv - 1];
139
140 if (daughter.m_ignore) {
141 //daughter ignored, search its children and treat them as direct children of mother
142 //if we haven't seen this particle yet, set its index to that of its last unignored parent
143 if (!m_seen[nv]) daughter.setIndex(mother.getIndex());
144 find_daughters(nv, g, mother);
145 } else {
146 //If we didn't see this particle already, set its index and add it to the queue for writing out
147 if (!m_seen[nv]) {
148 daughter.setIndex(++m_index);
149 m_vqueue.push(nv);
150 }
151 //Set daughter information of mother. If 0, no daughters yet so just take current daughter as only
152 //daughter. Otherwise allow extension of daughter information in both directions.
153 if (d1 == 0) {
154 d1 = daughter.getIndex();
155 d2 = d1;
156 } else if ((d2 + 1) == daughter.getIndex()) {
157 ++d2;
158 } else if ((d1 - 1) == daughter.getIndex()) {
159 --d1;
160 } else {
161 //Daughter indices are not continuous, cannot continue
162 throw MCParticleGraph::NonContinousDaughtersError();
163 }
164 //Set Vertex and time information if requested
165 setVertexTime(mother, daughter);
166 daughter.m_mother = mother.getIndex();
167 }
168 //Mark particle as seen
169 m_seen[nv] = true;
170 }
171 }
172
173
180 {
181 //Only set vertex information if both particles have a valid vertex set
182 m.setValidVertex(m.hasValidVertex() && d.hasValidVertex());
183 if (m.hasValidVertex() && d.getProductionTime() >= m.getDecayTime()) {
184 if (m_setVertex) {
185 m.setDecayVertex(d.getProductionVertex());
186 }
187 if (m_setTime) {
188 m.setDecayTime(d.getProductionTime());
189 }
190 }
191 }
192
193
194protected:
195
198 TClonesArray*
202 vector<bool>
204 std::queue<unsigned int> m_vqueue;
205};
206
207
208void MCParticleGraph::generateList(const string& name, int options)
209{
210 StoreArray<MCParticle> MCParticles(name);
211
212 //Make Graph and connect all primary vertices (particles without mother)
213 //to an artificial 0ths vertex to be able to find them easily
214 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> Graph;
215 int num_particles(0);
216 //Determine number of not ignored particles and add an edge from 0ths vertex to any primary
217 //particle
218 for (unsigned int i = 0; i < m_particles.size(); ++i) {
219 if (!m_particles[i]->m_ignore) ++num_particles;
220 if (m_particles[i]->m_primary) m_decays.insert(DecayLine(0, i + 1));
221 }
222 Graph g(m_decays.begin(), m_decays.end(), m_particles.size() + 1);
223
224 //Check for cyclic dependency
225 if (options & c_checkCyclic) {
226 cycle_detector vis;
227 depth_first_search(g, visitor(vis));
228 }
229
230 //Fill TClonesArray in correct order
231 if (options & c_clearParticles) MCParticles.getPtr()->Clear();
232 MCParticles.getPtr()->Expand(num_particles + MCParticles.getEntries());
233 MCParticleGraph::ParticleSorter psorter(m_particles, MCParticles.getPtr(), options & c_setDecayVertex, options & c_setDecayTime);
234 psorter.setStartIndex(MCParticles.getEntries());
235 psorter.sort(g);
236}
237
238void MCParticleGraph::loadList(const string& name)
239{
240 StoreArray<MCParticle> MCParticles(name);
241 if (!MCParticles) {
242 B2ERROR("MCParticle Collection is not valid, cannot load into Graph");
243 return;
244 }
245
246 unsigned numParticles = MCParticles.getEntries();
247 unsigned particleOffset = size();
248 //Here we assume that the MCParticle collection is somehow ordered: All
249 //particles which are product of a decay come in the list after the mother,
250 //thus having a higher index. This is true for all lists generated by the
251 //MCParticleGraph and also for the standard lists produced by Evtgen and
252 //similar generators.
253 for (unsigned i = 0; i < numParticles; ++i) {
254 GraphParticle& newParticle = addParticle();
255 const MCParticle& oldParticle = *MCParticles[i];
256 //Copy all values
257 newParticle = oldParticle;
258 //If this particle has a mother we just add the decay to this mother
259 const MCParticle* oldMother = oldParticle.getMother();
260 if (oldMother != nullptr) {
261 unsigned motherIndex = oldMother->getArrayIndex() + particleOffset;
262 if (motherIndex >= size())
263 B2FATAL("MCParticle collection \"" << name << "\" not sorted correctly: mother index larger than daughter. Cannot load into Graph");
264 newParticle.comesFrom((*this)[oldMother->getArrayIndex() + particleOffset]);
265 }
266 }
267}
Class to represent Particle data in graph.
void comesFrom(GraphParticle &mother)
Tells the graph that this particle is a decay product of mother.
Class to go over all the particles in the Graph an sort them in a sensible way.
void setVertexTime(MCParticleGraph::GraphParticle &m, const MCParticleGraph::GraphParticle &d)
Set the vertex and time information of the mother particle.
ParticleSorter(MemoryPool< MCParticleGraph::GraphParticle > &particles, TClonesArray *plist, bool setVertex, bool setTime)
ParticleSorter constructor.
MemoryPool< MCParticleGraph::GraphParticle > & m_particles
Reference to the list of particles which should be sorted.
void find_daughters(Vertex v, Graph &g, MCParticleGraph::GraphParticle &mother)
Find the daughters of the given vertex.
void finish_vertex(Vertex v, Graph &g)
Go through the daughters of the vertex.
bool m_setTime
True if the production time information should be saved.
std::queue< unsigned int > m_vqueue
The list of the vertices that will be visited.
vector< bool > m_seen
Vector of the particles that were already seen while sorting the graph.
void setStartIndex(int index)
Set the starting index for the particle graph.
int m_index
The latest index given to a particle.
bool m_setVertex
True if the vertex information should be saved.
TClonesArray * m_plist
The final array of sorted particles which is stored in the DataStore.
void sort(Graph &g)
Sort the particles and generate MCParticle list.
@ c_setDecayVertex
Set the decay vertex to the production vertex of the last daughter (ordered by production time)
@ c_setDecayTime
Set decay time to the largest production time of the daughters.
@ c_checkCyclic
Check for cyclic dependencies.
@ c_clearParticles
Clear the particle list before adding the graph.
size_t size() const
Return the number of particles in the graph.
std::pair< unsigned int, unsigned int > DecayLine
Type representing a decay in the graph.
void loadList(const std::string &name="")
Load the MCParticle list given by name into the Graph.
std::set< DecayLine > m_decays
internal set of decay lines
MemoryPool< GraphParticle > m_particles
internal list of particles
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
int m_lastDaughter
1-based index of last daughter particle in collection, 0 if no daughters
Definition: MCParticle.h:548
int getIndex() const
Get 1-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:219
int m_firstDaughter
1-based index of first daughter particle in collection, 0 if no daughters
Definition: MCParticle.h:547
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:233
Class to provide a constant access time memory pool for one kind of objects.
Definition: MemoryPool.h:33
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:311
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:590
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.
STL namespace.
Simple struct to check boost graph for cyclic references.
void back_edge(Edge, Graph &)
This method is invoked on back edges in the graph.