HepMC3 event record library
testAttributes.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 <ctime>
7#include "HepMC3/GenEvent.h"
8#include "HepMC3/GenVertex.h"
10using namespace HepMC3;
11
12GenEvent generate1() {
13 GenEvent evt;
14 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
15 evt.set_run_info(run);
16 GenParticlePtr b1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
17 GenParticlePtr b2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
18 GenParticlePtr b3 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
19 GenVertexPtr v1 = std::make_shared<GenVertex>();
20 v1->add_particle_in (b1);
21 v1->add_particle_in(b2);
22 v1->add_particle_out(b3);
23 evt.add_vertex(v1);
24 for (size_t z= 0; z < 12; z++) {
25 auto particles = evt.particles();
26 for (auto p: particles) {
27 if (p->end_vertex()) continue;
28 GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
29 GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
30 GenVertexPtr v = std::make_shared<GenVertex>();
31 v->add_particle_in (p);
32 v->add_particle_out(p1);
33 v->add_particle_out(p2);
34 evt.add_vertex(v);
35 v->add_attribute("barcode",std::make_shared<HepMC3::IntAttribute>(-20));
36 p1->add_attribute("phi",std::make_shared<HepMC3::DoubleAttribute>(0.1));
37 p1->add_attribute("theta",std::make_shared<HepMC3::DoubleAttribute>(0.1));
38 p1->add_attribute("barcode",std::make_shared<HepMC3::IntAttribute>(10));
39 p2->add_attribute("phi",std::make_shared<HepMC3::DoubleAttribute>(0.1));
40 p2->add_attribute("theta",std::make_shared<HepMC3::DoubleAttribute>(0.1));
41 p2->add_attribute("barcode",std::make_shared<HepMC3::IntAttribute>(10));
42 }
43 }
44 return evt;
45}
46
47
48
49
50
51GenEvent generate2() {
52 GenEvent evt;
53 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
54 evt.set_run_info(run);
55 GenParticlePtr b1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
56 GenParticlePtr b2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
57 GenParticlePtr b3 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
58 GenVertexPtr v1 = std::make_shared<GenVertex>();
59 v1->add_particle_in (b1);
60 v1->add_particle_in(b2);
61 v1->add_particle_out(b3);
62 evt.add_vertex(v1);
63 std::vector<std::string> names;
64 names.reserve(2048);
65 std::vector<std::shared_ptr<Attribute> > atts;
66 atts.reserve(2048);
67 std::vector<int> ids;
68 ids.reserve(2048);
69 for (size_t z= 0; z < 12; z++) {
70 auto particles = evt.particles();
71 for (auto p: particles) {
72 if (p->end_vertex()) continue;
73 GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
74 GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
75 GenVertexPtr v = std::make_shared<GenVertex>();
76 v->add_particle_in (p);
77 v->add_particle_out(p1);
78 v->add_particle_out(p2);
79 evt.add_vertex(v);
80 names.push_back("barcode");
81 names.push_back("barcode");
82 names.push_back("phi");
83 names.push_back("theta");
84 names.push_back("barcode");
85 names.push_back("phi");
86 names.push_back("theta");
87 atts.push_back(std::make_shared<HepMC3::IntAttribute>(-20));
88 atts.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
89 atts.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
90 atts.push_back(std::make_shared<HepMC3::IntAttribute>(10));
91 atts.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
92 atts.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
93 atts.push_back(std::make_shared<HepMC3::IntAttribute>(10));
94 ids.push_back(v->id());
95 ids.push_back(p1->id());
96 ids.push_back(p1->id());
97 ids.push_back(p1->id());
98 ids.push_back(p2->id());
99 ids.push_back(p2->id());
100 ids.push_back(p2->id());
101 }
102 }
103 evt.add_attributes(names, atts, ids);
104 return evt;
105}
106
107GenEvent generate3() {
108 GenEvent evt;
109 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
110 evt.set_run_info(run);
111 GenParticlePtr b1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
112 GenParticlePtr b2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
113 GenParticlePtr b3 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
114 GenVertexPtr v1 = std::make_shared<GenVertex>();
115 v1->add_particle_in (b1);
116 v1->add_particle_in(b2);
117 v1->add_particle_out(b3);
118 evt.add_vertex(v1);
119 std::vector<std::shared_ptr<Attribute> > attsb;
120 attsb.reserve(2048*2);
121 std::vector<int> idsb;
122 idsb.reserve(2048*2);
123 std::vector<std::shared_ptr<Attribute> > attsp;
124 attsp.reserve(2048);
125 std::vector<int> idsp;
126 idsp.reserve(2048);
127 std::vector<std::shared_ptr<Attribute> > attst;
128 attst.reserve(2048);
129 std::vector<int> idst;
130 idst.reserve(2048);
131
132 for (size_t z= 0; z < 12; z++) {
133 auto particles = evt.particles();
134 for (auto p: particles) {
135 if (p->end_vertex()) continue;
136 GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
137 GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
138 GenVertexPtr v = std::make_shared<GenVertex>();
139 v->add_particle_in (p);
140 v->add_particle_out(p1);
141 v->add_particle_out(p2);
142 evt.add_vertex(v);
143 attsb.push_back(std::make_shared<HepMC3::IntAttribute>(-20));
144 attst.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
145 attsp.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
146 attsb.push_back(std::make_shared<HepMC3::IntAttribute>(10));
147 attst.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
148 attsp.push_back(std::make_shared<HepMC3::DoubleAttribute>(0.1));
149 attsb.push_back(std::make_shared<HepMC3::IntAttribute>(10));
150 idsb.push_back(v->id());
151 idsb.push_back(p1->id());
152 idsp.push_back(p1->id());
153 idst.push_back(p1->id());
154 idsb.push_back(p2->id());
155 idsp.push_back(p2->id());
156 idst.push_back(p2->id());
157 }
158 }
159 evt.add_attributes("barcode", attsb, idsb);
160 evt.add_attributes("phi", attsp, idsp);
161 evt.add_attributes("theta", attst, idst);
162 return evt;
163}
164
165GenEvent generate4() {
166 GenEvent evt;
167 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
168 evt.set_run_info(run);
169 GenParticlePtr b1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
170 GenParticlePtr b2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
171 GenParticlePtr b3 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
172 GenVertexPtr v1 = std::make_shared<GenVertex>();
173 v1->add_particle_in (b1);
174 v1->add_particle_in(b2);
175 v1->add_particle_out(b3);
176 evt.add_vertex(v1);
177 std::vector<std::pair<int,std::shared_ptr<Attribute> > > attsb;
178 std::vector<std::pair<int,std::shared_ptr<Attribute> > > attsp;
179 std::vector<std::pair<int,std::shared_ptr<Attribute> > > attst;
180 attsb.reserve(2048*2);
181 attsp.reserve(2048);
182 attst.reserve(2048);
183
184 for (size_t z= 0; z < 12; z++) {
185 auto particles = evt.particles();
186 for (auto p: particles) {
187 if (p->end_vertex()) continue;
188 GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
189 GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
190 GenVertexPtr v = std::make_shared<GenVertex>();
191 v->add_particle_in (p);
192 v->add_particle_out(p1);
193 v->add_particle_out(p2);
194 evt.add_vertex(v);
195 attsb.push_back(std::pair<int,std::shared_ptr<Attribute> > (v->id(), std::make_shared<HepMC3::IntAttribute>(-20)));
196 attst.push_back(std::pair<int,std::shared_ptr<Attribute> > (p1->id(), std::make_shared<HepMC3::DoubleAttribute>(0.1)));
197 attsp.push_back(std::pair<int,std::shared_ptr<Attribute> > (p1->id(),std::make_shared<HepMC3::DoubleAttribute>(0.1)));
198 attsb.push_back(std::pair<int,std::shared_ptr<Attribute> > (p1->id(),std::make_shared<HepMC3::IntAttribute>(10)));
199 attst.push_back(std::pair<int,std::shared_ptr<Attribute> > (p2->id(),std::make_shared<HepMC3::DoubleAttribute>(0.1)));
200 attsp.push_back(std::pair<int,std::shared_ptr<Attribute> > (p2->id(),std::make_shared<HepMC3::DoubleAttribute>(0.1)));
201 attsb.push_back(std::pair<int,std::shared_ptr<Attribute> > (p2->id(),std::make_shared<HepMC3::IntAttribute>(10)));
202 }
203 }
204 evt.add_attributes("barcode", attsb);
205 evt.add_attributes("phi", attsp);
206 evt.add_attributes("theta", attst);
207 return evt;
208}
209#include <chrono>
210typedef std::chrono::high_resolution_clock Clock;
211int main()
212{
213 int N = 10;
214 auto rawstart1= Clock::now();
215 for (int i = 0; i < N; i++) generate1();
216 auto now1 = Clock::now();
217
218
219 auto rawstart2 = Clock::now();
220
221 for (int i = 0; i < N; i++) generate2();
222 auto now2 = Clock::now();
223
224
225 auto rawstart3 = Clock::now();
226
227 for (int i = 0; i < N; i++) generate3();
228 auto now3 = Clock::now();
229
230
231 auto rawstart4 = Clock::now();
232
233 for (int i = 0; i < N; i++) generate4();
234 auto now4 = Clock::now();
235
236
237 std::cout<< std::chrono::duration_cast<std::chrono::nanoseconds>(now1-rawstart1).count() << " "
238 << std::chrono::duration_cast<std::chrono::nanoseconds>(now2-rawstart2).count() << " "
239 << std::chrono::duration_cast<std::chrono::nanoseconds>(now3-rawstart3).count() << " "
240 << std::chrono::duration_cast<std::chrono::nanoseconds>(now4-rawstart4).count() <<
241 std::endl;
242
243 return 0;
244}
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Generic 4-vector.
Definition: FourVector.h:36
Stores event-related information.
Definition: GenEvent.h:41
void add_vertex(GenVertexPtr v)
Add vertex.
Definition: GenEvent.cc:96
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:141
void add_attributes(const std::vector< std::string > &names, const std::vector< std::shared_ptr< Attribute > > &atts, const std::vector< int > &ids)
Add multiple attributes to event.
Definition: GenEvent.cc:821
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:39
HepMC3 main namespace.