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 using namespace HepMC3;
18 int main()
19 {
20  GenEvent evt;
21  std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();;
22  evt.set_run_info(run);
23  // original functionality
24  evt.weights().push_back(2.0);
25  evt.weights().push_back(4.56);
26  assert( evt.weights()[0] == 2.0 );
27  assert( evt.weights()[1] == 4.56 );
28  assert( evt.weights().size() == 2 );
29  assert( !evt.weights().empty() );
30 
31  std::vector<double> vec;
32  for( int i = 0; i < 15; ++i )
33  {
34  double x = (double)i + 0.14*(double)i;
35  vec.push_back( x );
36  }
37  evt.weights() = vec;
38  assert( evt.weights().size() == 15 );
39  evt.weights().pop_back();
40  assert( evt.weights().size() == 14 );
41 
42  // new functionality
43  std::vector<std::string> names;
44  for( size_t i = 0; i < evt.weights().size() - 1; ++i ) names.push_back(std::to_string((unsigned long long)i));
45  std::string nm = "tau";
46  names.push_back(nm);
47  run->set_weight_names(names);
48 
49  evt.weight(nm) = 3.1;
50  //assert( evt.weights().size() == (vs) );
51 
52  // lookup a nonexistent name
53  try
54  {
55  double x = evt.weight("bad");
56  std::cout << "lookup of nonexistent name returns " << x << std::endl;
57  }
58  catch (std::exception& e)
59  {
60  std::cout << e.what() << std::endl;
61  std::cout << "HepMC testWeights: the above error is intentional" << std::endl;
62  }
63  Print::listing(evt);
64  return 0;
65 }
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:129
Definition of class GenRunInfo.
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:51
Stores event-related information.
Definition: GenEvent.h:42
int main(int argc, char **argv)
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
Definition of class GenEvent.
double weight(const size_t &index=0) const
Definition: GenEvent.h:92