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