StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testWeights.cc
1 // testWeights.cc
3 //
4 // garren@fnal.gov, January 2010
5 // test WeightContainer
7 
8 #include <assert.h>
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 #include "HepMC/WeightContainer.h"
14 #include <stdexcept>
15 
16 int main() {
17 
19 
20  // original functionality
21  w.push_back(2.0);
22  w.push_back(4.56);
23  assert( w[0] == 2.0 );
24  assert( w[1] == 4.56 );
25  assert( w.size() == 2 );
26  assert( !w.empty() );
27 
28  std::vector<double> vec;
29  for( int i = 0; i < 15; ++i ) {
30  double x = (double)i + 0.14*(double)i;
31  vec.push_back( x );
32  }
33  double v1 = vec[0];
34  w = vec;
35  assert( w.size() == 15 );
36  w.pop_back();
37  assert( w.size() == 14 );
38 
39  // new functionality
40  std::size_t vs = vec.size();
41  std::string nm = "tau";
42  w[nm] = 3.1;
43  assert( w.size() == (vs) );
44  w["my_pdf"] = 11.3445;
45  //w['PDF_SET_3'] = 10.33;
46  assert( w[nm] == 3.1 );
47  assert( w[0] == v1 );
48  assert( w.size() == (vs+1) );
49  assert( w[vs-1] == 3.1 );
50 
51  const HepMC::WeightContainer wc = w;
52  assert( wc[nm] == 3.1 );
53  // lookup a nonexistent name
54  try {
55  double x = wc["bad"];
56  std::cout << "lookup of nonexistent name returns " << x << std::endl;
57  }
58  catch (std::exception& e) {
59  std::cout << e.what() << std::endl;
60  std::cout << "HepMC testWeights: the above error is intentional" << std::endl;
61  }
62  // another option for user code:
63  if( wc.has_key("bad") ) {
64  double x = wc["bad"];
65  std::cout << "lookup of nonexistent name returns " << x << std::endl;
66  }
67 
68  w.write();
69 
70  return 0;
71 }
void pop_back()
pop from weight container
void push_back(const double &)
push onto weight container
bool has_key(const std::string &s) const
check to see if a name exists in the map
bool empty() const
return true if weight container is empty
void write(std::ostream &ostr=std::cout) const
write weights in a readable table
Container for the Weights associated with an event or vertex.
size_type size() const
size of weight container