6#ifndef HEPEVT_HELPERS_H
7#define HEPEVT_HELPERS_H
13#if defined(WIN32)&&(!defined(HEPMC3_NO_EXPORTS))
14#if defined(HepMC3_EXPORTS)
15#define HEPMC3_EXPORT_API __declspec(dllexport)
17#define HEPMC3_EXPORT_API __declspec(dllimport)
20#define HEPMC3_EXPORT_API
38template <
int max_particles,
typename momentum_type =
double>
47 momentum_type
phep [max_particles][5];
48 momentum_type
vhep [max_particles][4];
58template <
int max_particles,
typename momentum_type =
double>
67 momentum_type
phep [max_particles*5];
68 momentum_type
vhep [max_particles*4];
78template<
typename momentum_type =
double>
95 bool operator()(ConstGenParticlePtr lx, ConstGenParticlePtr rx)
const;
102 bool operator()(
const std::pair<ConstGenVertexPtr, int>& lx,
const std::pair<ConstGenVertexPtr, int>& rx)
const;
112 if ( !evt ) { std::cerr <<
"HEPEVT_to_GenEvent_nonstatic - passed null event." << std::endl;
return false;}
114 std::map<GenParticlePtr, int > hepevt_particles;
115 std::map<int, GenParticlePtr > particles_index;
116 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > > hepevt_vertices;
117 std::map<int, GenVertexPtr > vertex_index;
119 ne=A->number_entries();
120 for (
int i = 1; i <= ne; i++ )
122 GenParticlePtr p = std::make_shared<GenParticle>();
123 p->set_momentum(
FourVector(A->px(i), A->py(i), A->pz(i), A->e(i)));
124 p->set_status(A->status(i));
125 p->set_pid(A->id(i));
126 p->set_generated_mass(A->m(i));
127 hepevt_particles[p] = i;
128 particles_index[i] = p;
129 GenVertexPtr v = std::make_shared<GenVertex>();
130 v->set_position(
FourVector(A->x(i), A->y(i), A->z(i), A->t(i)));
131 v->add_particle_out(p);
136 hepevt_vertices[v] = std::pair<std::set<int>, std::set<int> >(in, out);
141 for (std::map<GenParticlePtr, int >::iterator it1 = hepevt_particles.begin(); it1 != hepevt_particles.end(); ++it1)
142 for (std::map<GenParticlePtr, int >::iterator it2 = hepevt_particles.begin(); it2 != hepevt_particles.end(); ++it2) {
143 if (A->first_parent(it2->second) <= it1->second && it1->second <= A->last_parent(it2->second)) hepevt_vertices[it2->first->production_vertex()].first.insert(it1->second);
148 for (
int i = 1; i <= A->number_entries(); i++ ) vertex_index[i]->remove_particle_out(particles_index[i]);
151 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > > final_vertices_map;
152 for (std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >::iterator vs = hepevt_vertices.begin(); vs != hepevt_vertices.end(); ++vs)
154 if ((final_vertices_map.size() == 0) || (vs->second.first.size() == 0 && vs->second.second.size() != 0)) { final_vertices_map.insert(*vs);
continue; }
155 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >::iterator v2;
156 for (v2 = final_vertices_map.begin(); v2 != final_vertices_map.end(); ++v2)
if (vs->second.first == v2->second.first) {v2->second.second.insert(vs->second.second.begin(), vs->second.second.end());
break;}
157 if (v2 == final_vertices_map.end()) final_vertices_map.insert(*vs);
160 std::vector<GenParticlePtr> final_particles;
162 for (std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >:: iterator it = final_vertices_map.begin(); it != final_vertices_map.end(); ++it)
164 GenVertexPtr v = it->first;
165 std::set<int> in = it->second.first;
166 std::set<int> out = it->second.second;
167 used.insert(in.begin(), in.end());
168 used.insert(out.begin(), out.end());
169 for (std::set<int>::iterator el = in.begin(); el != in.end(); ++el) v->add_particle_in(particles_index[*el]);
170 if (in.size() !=0 )
for (std::set<int>::iterator el = out.begin(); el != out.end(); ++el) v->add_particle_out(particles_index[*el]);
172 for (std::set<int>::iterator el = used.begin(); el != used.end(); ++el) final_particles.push_back(particles_index[*el]);
188 if ( !evt )
return false;
192 std::map<ConstGenVertexPtr, int> longest_paths;
195 std::vector<std::pair<ConstGenVertexPtr, int> > sorted_paths;
196 std::copy(longest_paths.begin(), longest_paths.end(), std::back_inserter(sorted_paths));
199 std::vector<ConstGenParticlePtr> sorted_particles;
200 std::vector<ConstGenParticlePtr> stable_particles;
202 for (std::pair<ConstGenVertexPtr, int> it: sorted_paths)
204 std::vector<ConstGenParticlePtr> Q = it.first->particles_in();
206 std::copy(Q.begin(), Q.end(), std::back_inserter(sorted_particles));
208 for (ConstGenParticlePtr pp: it.first->particles_out())
209 if (!(pp->end_vertex())) stable_particles.push_back(pp);
212 std::copy(stable_particles.begin(), stable_particles.end(), std::back_inserter(sorted_particles));
214 int particle_counter;
215 particle_counter = std::min(
int(sorted_particles.size()), A->max_number_entries());
218 A->set_number_entries(particle_counter);
219 for (
int i = 1; i <= particle_counter; ++i )
221 A->set_status(i, sorted_particles[i-1]->status());
222 A->set_id(i, sorted_particles[i-1]->pid());
223 FourVector m = sorted_particles[i-1]->momentum();
224 A->set_momentum(i, m.
px(), m.
py(), m.
pz(), m.
e());
225 A->set_mass(i, sorted_particles[i-1]->generated_mass());
226 if ( sorted_particles[i-1]->production_vertex() &&
227 sorted_particles[i-1]->production_vertex()->particles_in().size())
229 FourVector p = sorted_particles[i-1]->production_vertex()->position();
230 A->set_position(i, p.
x(), p.
y(), p.
z(), p.
t() );
231 std::vector<int> mothers;
234 for (ConstGenParticlePtr it: sorted_particles[i-1]->production_vertex()->particles_in())
235 for (
int j = 1; j <= particle_counter; ++j )
236 if (sorted_particles[j-1] == (it))
237 mothers.push_back(j);
238 std::sort(mothers.begin(), mothers.end());
239 if (mothers.size() == 0)
240 mothers.push_back(0);
241 if (mothers.size() == 1) mothers.push_back(mothers[0]);
243 A->set_parents(i, mothers.front(), mothers.back());
247 A->set_position(i, 0, 0, 0, 0);
248 A->set_parents(i, 0, 0);
250 A->set_children(i, 0, 0);
260 if ( !evt ) { std::cerr <<
"HEPEVT_to_GenEvent_static - passed null event." << std::endl;
return false;}
262 std::map<GenParticlePtr, int > hepevt_particles;
263 std::map<int, GenParticlePtr > particles_index;
264 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > > hepevt_vertices;
265 std::map<int, GenVertexPtr > vertex_index;
267 ne=T::number_entries();
268 for (
int i = 1; i <= ne; i++ )
270 GenParticlePtr p = std::make_shared<GenParticle>();
271 p->set_momentum(
FourVector(T::px(i), T::py(i), T::pz(i), T::e(i)));
272 p->set_status(T::status(i));
273 p->set_pid(T::id(i));
274 p->set_generated_mass(T::m(i));
275 hepevt_particles[p] = i;
276 particles_index[i] = p;
277 GenVertexPtr v = std::make_shared<GenVertex>();
278 v->set_position(
FourVector(T::x(i), T::y(i), T::z(i), T::t(i)));
279 v->add_particle_out(p);
284 hepevt_vertices[v] = std::pair<std::set<int>, std::set<int> >(in, out);
289 for (std::map<GenParticlePtr, int >::iterator it1 = hepevt_particles.begin(); it1 != hepevt_particles.end(); ++it1)
290 for (std::map<GenParticlePtr, int >::iterator it2 = hepevt_particles.begin(); it2 != hepevt_particles.end(); ++it2) {
291 if (T::first_parent(it2->second) <= it1->second && it1->second <= T::last_parent(it2->second)) hepevt_vertices[it2->first->production_vertex()].first.insert(it1->second);
296 for (
int i = 1; i <= T::number_entries(); i++ ) vertex_index[i]->remove_particle_out(particles_index[i]);
299 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > > final_vertices_map;
300 for (std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >::iterator vs = hepevt_vertices.begin(); vs != hepevt_vertices.end(); ++vs)
302 if ((final_vertices_map.size() == 0) || (vs->second.first.size() == 0 && vs->second.second.size() != 0)) { final_vertices_map.insert(*vs);
continue; }
303 std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >::iterator v2;
304 for (v2 = final_vertices_map.begin(); v2 != final_vertices_map.end(); ++v2)
if (vs->second.first == v2->second.first) {v2->second.second.insert(vs->second.second.begin(), vs->second.second.end());
break;}
305 if (v2 == final_vertices_map.end()) final_vertices_map.insert(*vs);
308 std::vector<GenParticlePtr> final_particles;
310 for (std::map<GenVertexPtr, std::pair<std::set<int>, std::set<int> > >:: iterator it = final_vertices_map.begin(); it != final_vertices_map.end(); ++it)
312 GenVertexPtr v = it->first;
313 std::set<int> in = it->second.first;
314 std::set<int> out = it->second.second;
315 used.insert(in.begin(), in.end());
316 used.insert(out.begin(), out.end());
317 for (
const auto& el: in) v->add_particle_in(particles_index[el]);
318 if (in.size() !=0 )
for (
const auto& el: out) v->add_particle_out(particles_index[el]);
320 for (
const auto& el: used) final_particles.emplace_back(particles_index[el]);
337 if ( !evt )
return false;
341 std::map<ConstGenVertexPtr, int> longest_paths;
344 std::vector<std::pair<ConstGenVertexPtr, int> > sorted_paths;
345 std::copy(longest_paths.begin(), longest_paths.end(), std::back_inserter(sorted_paths));
348 std::vector<ConstGenParticlePtr> sorted_particles;
349 std::vector<ConstGenParticlePtr> stable_particles;
351 for (std::pair<ConstGenVertexPtr, int> it: sorted_paths)
353 std::vector<ConstGenParticlePtr> Q = it.first->particles_in();
355 std::copy(Q.begin(), Q.end(), std::back_inserter(sorted_particles));
357 for (ConstGenParticlePtr pp: it.first->particles_out())
358 if (!(pp->end_vertex())) stable_particles.push_back(pp);
361 std::copy(stable_particles.begin(), stable_particles.end(), std::back_inserter(sorted_particles));
363 int particle_counter;
364 particle_counter = std::min(
int(sorted_particles.size()), T::max_number_entries());
367 T::set_number_entries(particle_counter);
368 for (
int i = 1; i <= particle_counter; ++i )
370 T::set_status(i, sorted_particles[i-1]->status());
371 T::set_id(i, sorted_particles[i-1]->pid());
372 FourVector m = sorted_particles[i-1]->momentum();
373 T::set_momentum(i, m.
px(), m.
py(), m.
pz(), m.
e());
374 T::set_mass(i, sorted_particles[i-1]->generated_mass());
375 if ( sorted_particles[i-1]->production_vertex() &&
376 sorted_particles[i-1]->production_vertex()->particles_in().size())
378 FourVector p = sorted_particles[i-1]->production_vertex()->position();
379 T::set_position(i, p.
x(), p.
y(), p.
z(), p.
t() );
380 std::vector<int> mothers;
383 for (ConstGenParticlePtr it: sorted_particles[i-1]->production_vertex()->particles_in())
384 for (
int j = 1; j <= particle_counter; ++j )
385 if (sorted_particles[j-1] == (it))
386 mothers.push_back(j);
387 std::sort(mothers.begin(), mothers.end());
388 if (mothers.size() == 0)
389 mothers.push_back(0);
390 if (mothers.size() == 1) mothers.push_back(mothers[0]);
392 T::set_parents(i, mothers.front(), mothers.back());
396 T::set_position(i, 0, 0, 0, 0);
397 T::set_parents(i, 0, 0);
399 T::set_children(i, 0, 0);
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
double e() const
Energy component of momentum.
double pz() const
z-component of momentum
double t() const
Time component of position/displacement.
double px() const
x-component of momentum
double py() const
y-component of momentum
double x() const
x-component of position/displacement
double y() const
y-component of position/displacement
double z() const
z-component of position/displacement
Stores event-related information.
int event_number() const
Get event number.
void set_event_number(const int &num)
Set event number.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void add_tree(const std::vector< GenParticlePtr > &parts)
Add whole tree in topological order.
bool HEPEVT_to_GenEvent_static(GenEvent *evt)
Converts HEPEVT into GenEvent.
bool HEPEVT_to_GenEvent_nonstatic(GenEvent *evt, T *A)
Converts HEPEVT into GenEvent.
void calculate_longest_path_to_top(ConstGenVertexPtr v, std::map< ConstGenVertexPtr, int > &pathl)
Calculates the path to the top (beam) particles.
bool GenEvent_to_HEPEVT_static(const GenEvent *evt)
Converts GenEvent into HEPEVT.
bool GenEvent_to_HEPEVT_nonstatic(const GenEvent *evt, T *A)
Converts GenEvent into HEPEVT.
comparison of two particles
bool operator()(ConstGenParticlePtr lx, ConstGenParticlePtr rx) const
comparison of two particles
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., "A proposed standard event ...
int * jdahep
Pointer to position of 1nd and 2nd (or last!) daughter.
int * isthep
Pointer to Status code.
int * nevhep
Pointer to Event number.
int * idhep
Pointer to PDG ID.
momentum_type * vhep
Pointer to time-space position: x, y, z, t.
momentum_type * phep
Pointer to momentum: px, py, pz, e, m.
int * jmohep
Pointer to position of 1st and 2nd (or last!) mother.
int * nhep
Pointer to Number of entries in the event.
int idhep[max_particles]
PDG ID.
momentum_type vhep[max_particles *4]
Time-space position: x, y, z, t.
int jdahep[max_particles *2]
Position of 1nd and 2nd (or last!) daughter.
int jmohep[max_particles *2]
Position of 1st and 2nd (or last!) mother.
momentum_type phep[max_particles *5]
Momentum: px, py, pz, e, m.
int isthep[max_particles]
Status code.
int nhep
Number of entries in the event.
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., "A proposed standard event ...
int jmohep[max_particles][2]
Position of 1st and 2nd (or last!) mother.
momentum_type vhep[max_particles][4]
Time-space position: x, y, z, t.
momentum_type phep[max_particles][5]
Momentum: px, py, pz, e, m.
int idhep[max_particles]
PDG ID.
int jdahep[max_particles][2]
Position of 1nd and 2nd (or last!) daughter.
int isthep[max_particles]
Status code.
int nhep
Number of entries in the event.
Order vertices with equal paths.
bool operator()(const std::pair< ConstGenVertexPtr, int > &lx, const std::pair< ConstGenVertexPtr, int > &rx) const
Order vertices with equal paths. If the paths are equal, order in other quantities....