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