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(std::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 = std::vector<double>(N, cross_section);
34  cross_section_errors = std::vector<double>(N, cross_section_error);
35 }
36 
37 
38 bool GenCrossSection::from_string(const std::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; attempted_events = -1;}
52  else
53  {
54  accepted_events = atoi(cursor);
55  if( !(cursor = strchr(cursor+1,' ')) ) attempted_events = -1;
56  else attempted_events = atoi(cursor);
57  }
58  size_t N=1;
59  if ( event() ) N=std::max(event()->weights().size(),N);
60  const size_t max_n_cross_sections=1000;
61  while (cross_sections.size()<max_n_cross_sections) {
62  if( !(cursor = strchr(cursor+1,' ')) ) break;
63  cross_sections.push_back(atof(cursor));
64  if( !(cursor = strchr(cursor+1,' ')) ) break;
65  cross_section_errors.push_back(atof(cursor));
66  }
67  if (cross_sections.size()>=max_n_cross_sections)
68  HEPMC3_WARNING( "GenCrossSection::from_string: too many optional cross-sections N="<<cross_sections.size()<<" or ill-formed input:"<<att )
69 // if (cross_sections.size()!=N)
70 // So far it is not clear if there should be a warning or not
71 // Frank suggests no. HEPMC3_WARNING( "GenCrossSection::from_string: optional cross-sections are available not for all weights")
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.at(0) << " "
83  << cross_section_errors.at(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.at(i);
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
bool is_valid() const
Verify that the instance contains non-zero information.
int windx(std::string wName) const
get the weight index given a weight name.
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition: Errors.h:26
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.
bool to_string(std::string &att) const override
Implementation of Attribute::to_string.
const GenEvent * event() const
Definition: Attribute.h:109
long attempted_events
The number of events attempted so far.
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 ==.
std::vector< double > cross_sections
Per-weight cross-section.
Definition of class GenEvent.
Definition of attribute class GenCrossSection.
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:124
Stores additional information about cross-section.