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 GenParticlePtr p1=std::make_shared<GenParticle>( FourVector(1.0,1.0,7000,7000),2212, 3 );
60 evt.add_particle( p1 );
61 p1->add_attribute("flow1", std::make_shared<IntAttribute>(231));
62 p1->add_attribute("flow1", std::make_shared<IntAttribute>(231));
63 p1->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
64 p1->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
65
66 GenVertexPtr v2=std::make_shared<GenVertex>();
67 evt.add_vertex( v2 );
68 GenParticlePtr p2=std::make_shared<GenParticle>( FourVector(1.0,1.0,-7000,7000),2212, 3 );
69 evt.add_particle( p2 );
70 p2->add_attribute("flow1", std::make_shared<IntAttribute>(243));
71 p2->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
72 p2->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
73 v2->add_particle_in( p2 );
74 //
75 // create the outgoing particles of v1 and v2
76 GenParticlePtr p3=std::make_shared<GenParticle>( FourVector(.750,-1.569,32.191,32.238),1, 3 );
77 evt.add_particle( p3 );
78 p3->add_attribute("flow1", std::make_shared<IntAttribute>(231));
79 p3->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
80 p3->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
81 v1->add_particle_out( p3 );
82 GenParticlePtr p4=std::make_shared<GenParticle>( FourVector(-3.047,-19.,-54.629,57.920),-2, 3 );
83 evt.add_particle( p4 );
84 p4->add_attribute("flow1", std::make_shared<IntAttribute>(243));
85 p4->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
86 p4->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
87 v2->add_particle_out( p4 );
88 //
89 // create v3
90 GenVertexPtr v3=std::make_shared<GenVertex>();
91 evt.add_vertex( v3 );
92 v3->add_particle_in( p3 );
93 v3->add_particle_in( p4 );
94 GenParticlePtr p6=std::make_shared<GenParticle>( FourVector(-3.813,0.113,-1.833,4.233 ),22, 1 );
95 evt.add_particle( p6 );
96 p6->add_attribute("flow1", std::make_shared<IntAttribute>(231));
97 p6->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
98 p6->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
99 v3->add_particle_out( p6 );
100 GenParticlePtr p5=std::make_shared<GenParticle>( FourVector(1.517,-20.68,-20.605,85.925),-24, 3 );
101 evt.add_particle( p5 );
102 p5->add_attribute("flow1", std::make_shared<IntAttribute>(243));
103 p5->add_attribute("theta", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI));
104 p5->add_attribute("phi", std::make_shared<DoubleAttribute>(std::rand()/double(RAND_MAX)*M_PI*2));
105 v3->add_particle_out( p5 );
106 //
107 // create v4
108 GenVertexPtr v4=std::make_shared<GenVertex>(FourVector(0.12,-0.3,0.05,0.004));
109 evt.add_vertex( v4 );
110 v4->add_particle_in( p5 );
111 GenParticlePtr p7(new GenParticle( FourVector(-2.445,28.816,6.082,29.552), 1,1 ));
112 evt.add_particle( p7 );
113 v4->add_particle_out( p7 );
114 GenParticlePtr p8(new GenParticle( FourVector(3.962,-49.498,-26.687,56.373), -2,1 ));
115 evt.add_particle( p8 );
116 v4->add_particle_out( p8 );
117 //
118 // tell the event which vertex is the signal process vertex
119 //evt.set_signal_process_vertex( v3 );
120 evt.add_attribute("signal_process_vertex", std::make_shared<IntAttribute>(v3->id()));
121 // the event is complete, we now print it out
122 Print::content(evt);
123 //we now print it out in old format
124 Print::listing(evt,8);
125 // print each particle so we can see the polarization
126 for ( GenParticlePtr ip: evt.particles()) {
127 Print::line(ip,true);
128 }
129 WriterAscii xout1("testBoost1.out");
130 xout1.set_precision(6);
131 xout1.write_event(evt);
132 xout1.close();
133
134 FourVector b(0.1,0.3,-0.2,0);
135 FourVector bp(-0.1,-0.3,0.2,0);
136 evt.boost(b);
137 for ( GenParticlePtr ip: evt.particles()) {
138 Print::line(ip,true);
139 }
140 evt.boost(bp);
141 for ( GenParticlePtr ip: evt.particles()) {
142 Print::line(ip,true);
143 }
144 WriterAscii xout2("testBoost2.out");
145 xout2.set_precision(6);
146 xout2.write_event(evt);
147 xout2.close();
148 /// Test the boost * invboost give the same event.
149 if (COMPARE_ASCII_FILES("testBoost1.out","testBoost2.out")!=0) return 1;
150
151 FourVector bwrong1(-1.1,-0.3,0.2,0);
152 ///Test that wrong boost will not work
153 if (evt.boost(bwrong1)) return 2;
154
155 FourVector bwrong2(-1.0,-0.0,0.0,0);
156 ///Test that boost with v=c will not work
157 if (evt.boost(bwrong2)) return 3;
158
159 FourVector bwrong3(std::numeric_limits<double>::epsilon()*0.9,0.0,0.0,0);
160 ///Test that boost with v=0 will be OK
161 if (!evt.boost(bwrong3)) return 4;
162
163 FourVector rz(0.0,0.0,-0.9,0);
164 FourVector rzinv(0.0,0.0,0.9,0);
165 evt.rotate(rz);
166 for ( GenParticlePtr ip: evt.particles()) {
167 Print::line(ip,true);
168 }
169 evt.rotate(rzinv);
170 for ( GenParticlePtr ip: evt.particles()) {
171 Print::line(ip,true);
172 }
173 WriterAscii xout3("testBoost3.out");
174 xout3.set_precision(6);
175 xout3.write_event(evt);
176 xout3.close();
177 /// Test the rotate * rotate give the same event.
178 if (COMPARE_ASCII_FILES("testBoost1.out","testBoost3.out")!=0) return 5;
179 evt.clear();
180 return 0;
181}
Definition of class Attribute, class IntAttribute and class StringAttribute.
#define M_PI
Definition of PI. Needed on some platforms.
Definition FourVector.h:15
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:35
Stores event-related information.
Definition GenEvent.h:42
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:18
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition Print.cc:51
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition Print.cc:203
GenEvent I/O serialization for structured text files.
Definition WriterAscii.h:25
HepMC3 main namespace.
Definition ReaderGZ.h:28