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 */
12#include "HepMC3/GenEvent.h"
13#include <cstring> // memcmp
14#include <cstdlib> // atoi
15#include <sstream>
16#include <iomanip>
17
18namespace HepMC3 {
19
20
21int GenCrossSection::windx(std::string wName) const {
22 if ( !event() || !event()->run_info() ) return 0;
23 return event()->run_info()->weight_index(wName);
24}
25
26void 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
38bool GenCrossSection::from_string(const std::string &att) {
39 const char *cursor = att.data();
40 cross_sections.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 HEPMC3_WARNING( "GenCrossSection::from_string: optional cross-sections are available not for all weights")
72 for (size_t i=cross_sections.size(); i<N; i++) {cross_sections[i]=cross_section; cross_section_errors[i]=cross_section_error;}
73
74 return true;
75}
76
77bool GenCrossSection::to_string(std::string &att) const {
78 std::ostringstream os;
79
80 os << std::setprecision(8) << std::scientific
81 << cross_sections.at(0) << " "
82 << cross_section_errors.at(0) << " "
83 << accepted_events << " "
85
86 for (size_t i = 1; i < cross_sections.size(); ++i )
87 os << " " << cross_sections.at(i)
88 << " " << cross_section_errors.at(i);
89
90 att = os.str();
91
92 return true;
93}
94
96 return ( memcmp( (void*)this, (void*) &a, sizeof(class GenCrossSection) ) == 0 );
97}
98
100 return !( a == *this );
101}
102
104 if( cross_sections.size() == 0 ) return false;
105 if( cross_section_errors.size() == 0 ) return false;
106 if( cross_section_errors.size()!=cross_sections.size() ) return false;
107 if( cross_sections.at(0) != 0 ) return true;
108 if( cross_section_errors.at(0) != 0 ) return true;
109 return false;
110}
111
112} // namespace HepMC3
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition: Errors.h:26
Definition of attribute class GenCrossSection.
Definition of class GenEvent.
const GenEvent * event() const
Definition: Attribute.h:110
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:124
HepMC3 main namespace.