HepMC3 event record library
testBoost.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 <iostream>
7#include <fstream>
8#include <vector>
9
10#include "HepMC3/Attribute.h"
11#include "HepMC3/GenEvent.h"
12#include "HepMC3/GenVertex.h"
13#include "HepMC3/GenParticle.h"
14#include "HepMC3/WriterAscii.h"
16#include "HepMC3/Print.h"
17#include "HepMC3TestUtils.h"
18using namespace HepMC3;
19int main()
20{
21 //
22 // In this example we will place the following event into HepMC "by hand"
23 //
24 // name status pdg_id parent Px Py Pz Energy Mass
25 // 1 !p+! 3 2212 0,0 1.000 1.000 7000.000 7000.000 0.938
26 // 2 !p+! 3 2212 0,0 1.000 1.000-7000.000 7000.000 0.938
27 //=========================================================================
28 // 3 !d! 3 1 1,1 0.750 -1.569 32.191 32.238 0.000
29 // 4 !u~! 3 -2 2,2 -3.047 -19.000 -54.629 57.920 0.000
30 // 5 !W-! 3 -24 1,2 1.517 -20.68 -20.605 85.925 80.799
31 // 6 !gamma! 1 22 1,2 -3.813 0.113 -1.833 4.233 0.000
32 // 7 !d! 1 1 5,5 -2.445 28.816 6.082 29.552 0.010
33 // 8 !u~! 1 -2 5,5 3.962 -49.498 -26.687 56.373 0.006
34
35
36
37 // build the graph, which will look like
38 // p7 #
39 // p1 / #
40 // \v1__p3 p5---v4 #
41 // \_v3_/ \ #
42 // / \ p8 #
43 // v2__p4 \ #
44 // / p6 #
45 // p2 #
46 //
47 // define a flow pattern as p1 -> p3 -> p6
48 // and p2 -> p4 -> p5
49 //
50
51 // First create the event container, with Signal Process 20, event number 1
52 //
53 GenEvent evt(Units::GEV,Units::MM);
54 evt.set_event_number(1);
55 evt.add_attribute("signal_process_id", std::make_shared<IntAttribute>(20));
56 // create vertex 1
57 GenVertexPtr v1 = std::make_shared<GenVertex>();
58 evt.add_vertex( v1 );
59 v1->add_attribute("weights", std::make_shared<VectorDoubleAttribute>(std::vector<double> {1.0,2.0,5.0}));
60 GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector(1.0,1.0,7000,7000),2212, 3 );
61 evt.add_particle( p1 );
62 p1->add_attribute("flow1", std::make_shared<IntAttribute>(231));
63 p1->add_attribute("flow1", std::make_shared<IntAttribute>(231));
64 p1->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
65 p1->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
66
67 GenVertexPtr v2 = std::make_shared<GenVertex>();
68 evt.add_vertex( v2 );
69 GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector(1.0,1.0,-7000,7000),2212, 3 );
70 evt.add_particle( p2 );
71 p2->add_attribute("flow1", std::make_shared<IntAttribute>(243));
72 p2->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
73 p2->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
74 v2->add_particle_in( p2 );
75 //
76 // create the outgoing particles of v1 and v2
77 GenParticlePtr p3 = std::make_shared<GenParticle>( FourVector(.750,-1.569,32.191,32.238),1, 3 );
78 evt.add_particle( p3 );
79 p3->add_attribute("flow1", std::make_shared<IntAttribute>(231));
80 p3->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
81 p3->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
82 v1->add_particle_out( p3 );
83 GenParticlePtr p4 = std::make_shared<GenParticle>( FourVector(-3.047,-19.,-54.629,57.920),-2, 3 );
84 evt.add_particle( p4 );
85 p4->add_attribute("flow1", std::make_shared<IntAttribute>(243));
86 p4->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
87 p4->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
88 v2->add_particle_out( p4 );
89 //
90 // create v3
91 GenVertexPtr v3 = std::make_shared<GenVertex>();
92 evt.add_vertex( v3 );
93 v3->add_particle_in( p3 );
94 v3->add_particle_in( p4 );
95 GenParticlePtr p6 = std::make_shared<GenParticle>( FourVector(-3.813,0.113,-1.833,4.233 ),22, 1 );
96 evt.add_particle( p6 );
97 p6->add_attribute("flow1", std::make_shared<IntAttribute>(231));
98 p6->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
99 p6->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
100 v3->add_particle_out( p6 );
101 GenParticlePtr p5 = std::make_shared<GenParticle>( FourVector(1.517,-20.68,-20.605,85.925),-24, 3 );
102 evt.add_particle( p5 );
103 p5->add_attribute("flow1", std::make_shared<IntAttribute>(243));
104 p5->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
105 p5->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
106 v3->add_particle_out( p5 );
107 //
108 // create v4
109 GenVertexPtr v4 = std::make_shared<GenVertex>(FourVector(0.12,-0.3,0.05,0.004));
110 evt.add_vertex( v4 );
111 v4->add_particle_in( p5 );
112 GenParticlePtr p7(new GenParticle( FourVector(-2.445,28.816,6.082,29.552), 1,1 ));
113 evt.add_particle( p7 );
114 v4->add_particle_out( p7 );
115 GenParticlePtr p8(new GenParticle( FourVector(3.962,-49.498,-26.687,56.373), -2,1 ));
116 evt.add_particle( p8 );
117 v4->add_particle_out( p8 );
118 //
119 // tell the event which vertex is the signal process vertex
120 //evt.set_signal_process_vertex( v3 );
121 evt.add_attribute("signal_process_vertex", std::make_shared<IntAttribute>(v3->id()));
122 // the event is complete, we now print it out
123 Print::content(evt);
124 //we now print it out in old format
125 Print::listing(evt,8);
126 // print each particle so we can see the polarization
127 for ( GenParticlePtr ip: evt.particles()) {
128 Print::line(ip,true);
129 }
130 WriterAscii xout1("testBoost1.out");
131 xout1.set_precision(6);
132 xout1.write_event(evt);
133 xout1.close();
134
135 FourVector b(0.1,0.3,-0.2,0);
136 FourVector bp(-0.1,-0.3,0.2,0);
137 evt.boost(b);
138 for ( GenParticlePtr ip: evt.particles()) {
139 Print::line(ip,true);
140 }
141 evt.boost(bp);
142 for ( GenParticlePtr ip: evt.particles()) {
143 Print::line(ip,true);
144 }
145 WriterAscii xout2("testBoost2.out");
146 xout2.set_precision(6);
147 xout2.write_event(evt);
148 xout2.close();
149 /// Test the boost * invboost give the same event.
150 if (COMPARE_ASCII_FILES("testBoost1.out","testBoost2.out")!=0) return 1;
151
152 FourVector bwrong1(-1.1,-0.3,0.2,0);
153 ///Test that wrong boost will not work
154 if (evt.boost(bwrong1)) return 2;
155
156 FourVector bwrong2(-1.0,-0.0,0.0,0);
157 ///Test that boost with v=c will not work
158 if (evt.boost(bwrong2)) return 3;
159
160 FourVector bwrong3(std::numeric_limits<double>::epsilon()*0.9,0.0,0.0,0);
161 ///Test that boost with v=0 will be OK
162 if (!evt.boost(bwrong3)) return 4;
163
164 FourVector rz(0.0,0.0,-0.9,0);
165 FourVector rzinv(0.0,0.0,0.9,0);
166 evt.rotate(rz);
167 for ( GenParticlePtr ip: evt.particles()) {
168 Print::line(ip,true);
169 }
170 evt.rotate(rzinv);
171 for ( GenParticlePtr ip: evt.particles()) {
172 Print::line(ip,true);
173 }
174 WriterAscii xout3("testBoost3.out");
175 xout3.set_precision(6);
176 xout3.write_event(evt);
177 xout3.close();
178 /// Test the rotate * rotate give the same event.
179 if (COMPARE_ASCII_FILES("testBoost1.out","testBoost3.out")!=0) return 5;
180 evt.clear();
181 return 0;
182}
Definition of class Attribute, class IntAttribute and class StringAttribute.
#define M_PI
Definition of PI. Needed on some platforms.
Definition: FourVector.h:16
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Definition of static class Print.
Definition of class WriterAsciiHepMC2.
Definition of class WriterAscii.
Generic 4-vector.
Definition: FourVector.h:36
Stores event-related information.
Definition: GenEvent.h:41
Stores particle-related information.
Definition: GenParticle.h:31
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:17
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition: Print.cc:202
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
HepMC3 main namespace.