HepMC3 event record library
testUnits.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
8#include "HepMC3/Units.h"
9#include "HepMC3/GenEvent.h"
10using namespace HepMC3;
11double conversion_factor( Units::MomentumUnit from, Units::MomentumUnit to )
12{
13 FourVector m( 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand());
14 FourVector msave(m);
15 Units::convert(m,from,to );
16 return m.e()/msave.e();//NAN?
17}
18double conversion_factor( Units::LengthUnit from, Units::LengthUnit to )
19{
20 FourVector m( 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand(), 0.5*RAND_MAX-std::rand());
21 FourVector msave(m);
22 Units::convert(m,from,to );
23 return m.e()/msave.e();//NAN?
24}
25bool neq(const double a,const double b)
26{
27 if (std::abs(a-b)<0.001*(std::abs(a)+std::abs(b))) return false;
28 return true;
29}
30int main()
31{
32
33 int err = 0;
34 double cf;
35 GenEvent evt;
36 std::cout << "Default units: " << Units::name(evt.momentum_unit())
37 << " " << Units::name(evt.length_unit()) << std::endl;
38
39 // check momentum conversion factors
40 cf = conversion_factor( Units::GEV, Units::GEV );
41 if( neq(cf,1) )
42 {
43 ++err;
44 std::cerr << "wrong conversion factor " << cf
45 << " for GEV to GEV - should be 1 \n";
46 }
47 cf = conversion_factor( Units::MEV, Units::MEV );
48 if( neq(cf,1) )
49 {
50 ++err;
51 std::cerr << "wrong conversion factor " << cf
52 << " for MEV to MEV - should be 1 \n";
53 }
54 cf = conversion_factor( Units::MEV, Units::GEV );
55 if( neq(cf,0.001) )
56 {
57 ++err;
58 std::cerr << "wrong conversion factor " << cf
59 << " for MEV to GEV - should be 0.001 \n";
60 }
61 cf = conversion_factor( Units::GEV, Units::MEV );
62 if( neq(cf,1000.0) )
63 {
64 ++err;
65 std::cerr << "wrong conversion factor " << cf
66 << " for GEV to MEV - should be 1000 \n";
67 }
68
69 // check length conversion factors
70 cf = conversion_factor( Units::MM, Units::MM );
71 if( neq(cf,1) )
72 {
73 ++err;
74 std::cerr << "wrong conversion factor " << cf
75 << " for MM to MM - should be 1 \n";
76 }
77 cf = conversion_factor( Units::CM, Units::CM );
78 if( neq(cf,1) )
79 {
80 ++err;
81 std::cerr << "wrong conversion factor " << cf
82 << " for CM to CM - should be 1 \n";
83 }
84 cf = conversion_factor( Units::CM, Units::MM );
85 if( neq(cf,10.0) )
86 {
87 ++err;
88 std::cerr << "wrong conversion factor " << cf
89 << " for CM to MM - should be 10 \n";
90 }
91 cf = conversion_factor( Units::MM, Units::CM );
92 if( neq(cf,0.1) )
93 {
94 ++err;
95 std::cerr << "wrong conversion factor " << cf
96 << " for MM to CM - should be 0.1 \n";
97 }
98
99 return err;
100}
Definition of class GenEvent.
Definition of class Units.
Generic 4-vector.
Definition: FourVector.h:36
Stores event-related information.
Definition: GenEvent.h:41
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition: GenEvent.h:153
const Units::LengthUnit & length_unit() const
Get length unit.
Definition: GenEvent.h:155
static void convert(T &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
Definition: Units.h:81
LengthUnit
Position units.
Definition: Units.h:32
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition: Units.h:56
MomentumUnit
Momentum units.
Definition: Units.h:29
HepMC3 main namespace.