HepMC3 event record library
search_example.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 #include "HepMC3/GenEvent.h"
7 #include "HepMC3/Print.h"
8 #include "HepMC3/GenParticle.h"
9 #include "HepMC3/GenVertex.h"
10 #include "HepMC3/Relatives.h"
11 
12 HepMC3::GenEvent* generate_event(const size_t n,const size_t iterations)
13 {
15  HepMC3::GenVertexPtr v0=std::make_shared<HepMC3::GenVertex>();
16  e->add_vertex(v0);
17  size_t it=0;
18  for (;;)
19  {
20  if (it>iterations)
21  {
22  for (auto v: e->vertices())
23  {
24  if (v->particles_out().size()!=0) continue;
25  for (int i=0; i<n; i++) v->add_particle_out(std::make_shared<HepMC3::GenParticle>());
26  }
27  break;
28  }
29  auto vertices=e->vertices();
30  for (auto v: vertices)
31  {
32  if (v->particles_out().size()!=0) continue;
33  for (int i=0; i<n; i++) v->add_particle_out(std::make_shared<HepMC3::GenParticle>());
34  for (auto p: v->particles_out())
35  {
36  HepMC3::GenVertexPtr vx=std::make_shared<HepMC3::GenVertex>();
37  vx->add_particle_in(p);
38  e->add_vertex(vx);
39  }
40  }
41  it++;
42  }
43  return e;
44 }
45 
46 int main()
47 {
48  std::cout<<"search_example: start"<<std::endl;
49  auto start0 = std::chrono::system_clock::now();
50  for (int i=0; i<10000; i++)
51  {
52  HepMC3::GenEvent* evt=generate_event(3,4);
53  delete evt;
54  }
55  auto end0 = std::chrono::system_clock::now();
56  std::cout<<"search_example: generation of events "<<std::chrono::duration_cast<std::chrono::milliseconds>(end0 - start0).count()<<" ms"<<std::endl;
57 
58  auto start1 = std::chrono::system_clock::now();
59  size_t np1=0;
60  for (int i=0; i<10000; i++)
61  {
62  HepMC3::GenEvent* evt=generate_event(3,4);
63  for (auto p: evt->particles()) np1+=HepMC3::descendant_particles(p).size();
64  delete evt;
65  }
66  auto end1 = std::chrono::system_clock::now();
67  std::cout<<"search_example: generation of events and descendants_of_same_type() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1).count()<<" ms"<<std::endl;
68 
69  auto start2 = std::chrono::system_clock::now();
70  size_t np2=0;
71  for (int i=0; i<10000; i++)
72  {
73  HepMC3::GenEvent* evt=generate_event(3,4);
74  for (auto p: evt->particles()) np2+=(HepMC3::Relatives::DESCENDANTS(p)).size();
75  delete evt;
76  }
77  auto end2 = std::chrono::system_clock::now();
78  std::cout<<"search_example: generation of events and Relatives::DESCENDANTS() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end2 - start2).count()<<" ms"<<std::endl;
79 
80  auto start3 = std::chrono::system_clock::now();
81  size_t np3=0;
82  for (int i=0; i<10000; i++)
83  {
84  HepMC3::GenEvent* evt=generate_event(3,4);
85  for (auto p: evt->particles()) np3+=HepMC3::ancestor_particles(p).size();
86  delete evt;
87  }
88  auto end3 = std::chrono::system_clock::now();
89  std::cout<<"search_example: generation of events and ancestors_of_same_type() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end3 - start3).count()<<" ms"<<std::endl;
90 
91 
92  auto start4 = std::chrono::system_clock::now();
93  size_t np4=0;
94  for (int i=0; i<10000; i++)
95  {
96  HepMC3::GenEvent* evt=generate_event(3,4);
97  for (auto p: evt->particles()) np4+=(HepMC3::Relatives::ANCESTORS(p)).size();
98  delete evt;
99  }
100  auto end4 = std::chrono::system_clock::now();
101  std::cout<<"search_example: generation of events and Relatives::ANCESTORS() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end4 - start4).count()<<" ms"<<std::endl;
102 
103  auto start1o = std::chrono::system_clock::now();
104  size_t np1o=0;
105  for (int i=0; i<10000; i++)
106  {
107  HepMC3::GenEvent* evt=generate_event(3,4);
108  for (auto p: evt->particles()) np1o+=HepMC3::descendant_vertices(p).size();
109  delete evt;
110  }
111  auto end1o = std::chrono::system_clock::now();
112  std::cout<<"search_example: generation of events and descendants_of_other_type() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end1o - start1o).count()<<" ms"<<std::endl;
113 
114 
115  auto start3o = std::chrono::system_clock::now();
116  size_t np3o=0;
117  for (int i=0; i<10000; i++)
118  {
119  HepMC3::GenEvent* evt=generate_event(3,4);
120  for (auto p: evt->particles()) np3o+=HepMC3::ancestor_vertices(p).size();
121  delete evt;
122  }
123  auto end3o = std::chrono::system_clock::now();
124  std::cout<<"search_example: generation of events and ancestors_of_other_type() "<<std::chrono::duration_cast<std::chrono::milliseconds>(end3o - start3o).count()<<" ms"<<std::endl;
125 
126  std::cout<<"search_example: end"<<std::endl;
127  return EXIT_SUCCESS;
128 }
void add_vertex(GenVertexPtr v)
Add vertex.
Definition: GenEvent.cc:97
Definition of class GenParticle.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
Definition: GenEvent.cc:43
std::vector< HepMC3::ConstGenVertexPtr > ancestor_vertices(HepMC3::ConstGenParticlePtr obj)
Return ancestor vertices.
Definition: Relatives.cc:186
Definition of class GenVertex.
Stores event-related information.
Definition: GenEvent.h:41
Defines helper classes to extract relatives of an input GenParticle or GenVertex. ...
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:39
std::vector< HepMC3::ConstGenVertexPtr > descendant_vertices(HepMC3::ConstGenParticlePtr obj)
Return descendant vertices.
Definition: Relatives.cc:172
std::vector< HepMC3::ConstGenParticlePtr > descendant_particles(HepMC3::ConstGenVertexPtr obj)
Return descendant particles.
Definition: Relatives.cc:165
int main(int argc, char **argv)
Definition of class GenEvent.
std::vector< HepMC3::ConstGenParticlePtr > ancestor_particles(HepMC3::ConstGenVertexPtr obj)
Return ancestor particles.
Definition: Relatives.cc:179