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