HepMC3 event record library
GenCrossSection.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2020 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file GenCrossSection.cc
8  * @brief Implementation of \b class GenCrossSection
9  *
10  */
11 #include <cstring> // memcmp
12 #include <cstdlib> // atoi
13 #include <sstream>
14 #include <iomanip>
15 #include "HepMC3/GenCrossSection.h"
16 #include "HepMC3/GenEvent.h"
17 
18 
19 namespace HepMC3 {
20 
21 
22 int GenCrossSection::windx(std::string wName) const {
23  if ( !event() || !event()->run_info() ) return 0;
24  return event()->run_info()->weight_index(wName);
25 }
26 
27 void GenCrossSection::set_cross_section(const double& xs, const double& xs_err, const long& n_acc, const long& n_att) {
28  double cross_section = xs;
29  double cross_section_error = xs_err;
30  accepted_events = n_acc;
31  attempted_events = n_att;
32  size_t N = 1;
33  if ( event() ) N = std::max(event()->weights().size(), N);
34  cross_sections = std::vector<double>(N, cross_section);
35  cross_section_errors = std::vector<double>(N, cross_section_error);
36 }
37 
38 
39 bool GenCrossSection::from_string(const std::string &att) {
40  const char *cursor = att.data();
41  cross_sections.clear();
42  cross_section_errors.clear();
43 
44 
45  double cross_section = atof(cursor);
46  cross_sections.push_back(cross_section);
47 
48  if ( !(cursor = strchr(cursor+1, ' ')) ) return false;
49  double cross_section_error = atof(cursor);
50  cross_section_errors.push_back(cross_section_error);
51 
52  if ( !(cursor = strchr(cursor+1, ' ')) ) {accepted_events = -1; attempted_events = -1;}
53  else
54  {
55  accepted_events = atoi(cursor);
56  if ( !(cursor = strchr(cursor+1, ' ')) ) attempted_events = -1;
57  else attempted_events = atoi(cursor);
58  }
59  size_t N = 1;
60  if ( event() ) N = std::max(event()->weights().size(), N);
61  const size_t max_n_cross_sections = 1000;
62  while (cross_sections.size() < max_n_cross_sections) {
63  if ( !(cursor = strchr(cursor+1, ' ')) ) break;
64  cross_sections.push_back(atof(cursor));
65  if ( !(cursor = strchr(cursor+1, ' ')) ) break;
66  cross_section_errors.push_back(atof(cursor));
67  }
68  if (cross_sections.size() >= max_n_cross_sections) {
69  HEPMC3_WARNING("GenCrossSection::from_string: too many optional cross-sections N=" << cross_sections.size() << " or ill-formed input:" << att)
70  }
71  // Use the default values to fill the vector to the size of N.
72  size_t oldsize = cross_sections.size();
73  for (size_t i = oldsize; i < N; i++) {cross_sections.push_back(cross_section); cross_section_errors.push_back(cross_section_error);}
74 
75  return true;
76 }
77 
78 bool GenCrossSection::to_string(std::string &att) const {
79  std::ostringstream os;
80 
81  os << std::setprecision(8) << std::scientific
82  << (cross_sections.size()>0?cross_sections.at(0):0.0) << " "
83  << (cross_section_errors.size()>0?cross_section_errors.at(0):0.0) << " "
84  << accepted_events << " "
86 
87  for (size_t i = 1; i < cross_sections.size(); ++i )
88  os << " " << cross_sections.at(i)
89  << " " << (cross_section_errors.size()>i?cross_section_errors.at(i):0.0);
90 
91  att = os.str();
92 
93  return true;
94 }
95 
97  return ( memcmp( (void*)this, (void*) &a, sizeof(class GenCrossSection) ) == 0 );
98 }
99 
101  return !( a == *this );
102 }
103 
105  if ( cross_sections.size() == 0 ) return false;
106  if ( cross_section_errors.size() == 0 ) return false;
107  if ( cross_section_errors.size() != cross_sections.size() ) return false;
108  if ( cross_sections.at(0) != 0 ) return true;
109  if ( cross_section_errors.at(0) != 0 ) return true;
110  return false;
111 }
112 
113 } // namespace HepMC3
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition: Errors.h:27
Definition of attribute class GenCrossSection.
Definition of class GenEvent.
const GenEvent * event() const
Definition: Attribute.h:108
Stores additional information about cross-section.
long attempted_events
The number of events attempted so far.
std::vector< double > cross_section_errors
Per-weight errors.
void set_cross_section(const double &xs, const double &xs_err, const long &n_acc=-1, const long &n_att=-1)
Set all fields.
std::vector< double > cross_sections
Per-weight cross-section.
bool is_valid() const
Verify that the instance contains non-zero information.
bool from_string(const std::string &att) override
Implementation of Attribute::from_string.
bool operator==(const GenCrossSection &) const
Operator ==.
bool operator!=(const GenCrossSection &) const
Operator !=.
int windx(std::string wName) const
get the weight index given a weight name.
bool to_string(std::string &att) const override
Implementation of Attribute::to_string.
long accepted_events
The number of events generated so far.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:125
HepMC3 main namespace.