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