HepMC3 event record library
testWeights.cc
1//////////////////////////////////////////////////////////////////////////
2// testWeights.cc
3//
4// garren@fnal.gov, January 2010
5// test Weights
6//////////////////////////////////////////////////////////////////////////
7
8#include <assert.h>
9#include <iostream>
10#include <string>
11#include <vector>
12
13#include "HepMC3/GenRunInfo.h"
14#include "HepMC3/GenEvent.h"
15#include "HepMC3/Print.h"
16#include <stdexcept>
17#include <limits>
18using namespace HepMC3;
19int main()
20{
21 GenEvent evt;
22 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();;
23 evt.set_run_info(run);
24 // original functionality
25 evt.weights().push_back(2.0);
26 evt.weights().push_back(4.56);
27 assert( std::abs(evt.weights()[0] - 2.0) < std::numeric_limits<double>::epsilon() );
28 assert( std::abs(evt.weights()[1] - 4.56) < std::numeric_limits<double>::epsilon() );
29 assert( evt.weights().size() == 2 );
30 assert( !evt.weights().empty() );
31
32 std::vector<double> vec;
33 for( int i = 0; i < 15; ++i )
34 {
35 double x = (double)i + 0.14*(double)i;
36 vec.push_back( x );
37 }
38 evt.weights() = vec;
39 assert( evt.weights().size() == 15 );
40 evt.weights().pop_back();
41 assert( evt.weights().size() == 14 );
42
43 // new functionality
44 std::vector<std::string> names;
45 for( size_t i = 0; i < evt.weights().size() - 1; ++i ) names.push_back(std::to_string((unsigned long long)i));
46 std::string nm = "tau";
47 names.push_back(nm);
48 run->set_weight_names(names);
49
50 evt.weight(nm) = 3.1;
51 //assert( evt.weights().size() == (vs) );
52
53 // lookup a nonexistent name
54 try
55 {
56 double x = evt.weight("bad");
57 std::cout << "lookup of nonexistent name returns " << x << std::endl;
58 }
59 catch (std::exception& e)
60 {
61 std::cout << e.what() << std::endl;
62 std::cout << "HepMC testWeights: the above error is intentional" << std::endl;
63 }
64 Print::listing(evt);
65 return 0;
66}
Definition of class GenEvent.
Definition of class GenRunInfo.
Definition of static class Print.
Stores event-related information.
Definition: GenEvent.h:41
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:141
double weight(const unsigned long &index=0) const
Definition: GenEvent.h:103
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:98
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
HepMC3 main namespace.