HepMC3 event record library: GenCrossSection.h Source File

HepMC3 event record library

GenCrossSection.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_CROSS_SECTION_H
7#define HEPMC3_CROSS_SECTION_H
8/**
9 * @file GenCrossSection.h
10 * @brief Definition of attribute \b class GenCrossSection
11 *
12 * @class HepMC3::GenCrossSection
13 * @brief Stores additional information about cross-section
14 *
15 * This is an example of event attribute used to store cross-section information
16 *
17 * This class is meant to be used to pass, on an event by event basis,
18 * the current best guess of the total cross section.
19 * It is expected that the final cross section will be stored elsewhere.
20 *
21 * - double cross_section; // cross section in pb.
22 * - double cross_section_error; // error associated with this cross section.
23 * - long accepted_events; ///< The number of events generated so far.
24 * - long attempted_events; ///< The number of events attempted so far.
25 *
26 * In addition, several cross sections and related info can be
27 * included in case of runs with mulltiple weights.
28 *
29 * The units of cross_section and cross_section_error are expected to be pb.
30 *
31 * @ingroup attributes
32 *
33 */
34#include <iostream>
35#include <algorithm>
36#include "HepMC3/Attribute.h"
37
38namespace HepMC3 {
39/** Deprecated */
40using namespace std;
41
42 class GenCrossSection : public Attribute {
43
44//
45// Fields
46//
47private:
48
49 long accepted_events; ///< The number of events generated so far.
50 long attempted_events; ///< The number of events attempted so far.
51
52 std::vector<double> cross_sections; ///< Per-weight cross-section.
53 std::vector<double> cross_section_errors; ///< Per-weight errors.
54//
55// Functions
56//
57public:
58 /** @brief Implementation of Attribute::from_string */
59 bool from_string(const std::string &att) override;
60
61 /** @brief Implementation of Attribute::to_string */
62 bool to_string(std::string &att) const override;
63 /// @name Deprecated functionality
64 /// @{
65 /** @brief Set all fields
66 @deprecated Use set_cross_section(const std::vector<double>& xs, const std::vector<double>& xs_err instead
67
68 @param xs Cross section
69 @param xs_err Uncertainty on cross-section
70 @param n_acc Number of accepted events
71 @param n_att Number of attempted events
72 */
73 void set_cross_section(const double& xs, const double& xs_err,const long& n_acc = -1, const long& n_att = -1);
74
75 /// @}
76 /** @brief Set all fields */
77 void set_cross_section(const std::vector<double>& xs, const std::vector<double>& xs_err,const long& n_acc = -1, const long& n_att = -1);
78
79 /** @brief Get the cross-sections
80 */
81 const std::vector<double>& xsecs() const { return cross_sections; }
82
83 /** @brief Get the cross-section errors
84 */
85 const std::vector<double>& xsec_errs() const { return cross_section_errors; }
86
87
88 /** @brief Set the number of accepted events
89 */
90 void set_accepted_events(const long& n_acc ) {
91 accepted_events=n_acc;
92 }
93
94 /** @brief Set the number of attempted events
95 */
96 void set_attempted_events(const long& n_att ) {
97 attempted_events=n_att;
98 }
99
100 /** @brief Get the number of accepted events
101 */
102 long get_accepted_events() const {
103 return accepted_events;
104 }
105
106 /** @brief Get the number of attempted events
107 */
108 long get_attempted_events() const {
109 return attempted_events;
110 }
111
112 /** @brief Set the cross section corresponding to the weight
113 named \a wName.
114 */
115 void set_xsec(const std::string& wName,const double& xs) {
116 int pos = windx(wName);
117 if ( pos < 0 ) throw std::runtime_error("GenCrossSection::set_xsec(const std::string&,const double&): no weight with given name in this run");
118 set_xsec(pos, xs);
119 }
120
121 /** @brief Set the cross section corresponding to the weight with
122 index \a indx.
123 */
124 void set_xsec(const unsigned long& index, const double& xs) {
125 if ( index >= cross_sections.size() ) {throw std::runtime_error("GenCrossSection::set_xsec(const unsigned long&): index outside of range");}
126 cross_sections[index] = xs;
127 }
128
129 /** @brief Set the cross section error corresponding to the weight
130 named \a wName.
131 */
132 void set_xsec_err(const std::string& wName, const double& xs_err) {
133 int pos = windx(wName);
134 if ( pos < 0 ) throw std::runtime_error("GenCrossSection::set_xsec_err(const std::string&,const double&): no weight with given name in this run");
135 set_xsec_err(pos, xs_err);
136 }
137
138 /** @brief Set the cross section error corresponding to the weight
139 with index \a indx.
140 */
141 void set_xsec_err(const unsigned long& index, const double& xs_err) {
142 if ( index >= cross_section_errors.size() ) {throw std::runtime_error("GenCrossSection::set_xsec_err(const unsigned long&): index outside of range");}
143 cross_section_errors[index] = xs_err;
144 }
145
146 /** @brief Get the cross section corresponding to the weight named
147 \a wName.
148 */
149 double xsec(const std::string& wName) const {
150 int pos = windx(wName);
151 if ( pos < 0 ) throw std::runtime_error("GenCrossSection::xsec(const std::string&): no weight with given name in this run");
152 return xsec(pos);
153 }
154
155 /** @brief Get the cross section corresponding to the weight with index
156 \a indx.
157 */
158 double xsec(const unsigned long& index = 0) const {
159 if ( index < cross_sections.size() ) { return cross_sections.at(index); }
160 else { throw std::runtime_error("GenCrossSection::xsec(const unsigned long&): index outside of range");}
161 return 0.0;
162 }
163
164 /** @brief Get the cross section error corresponding to the weight
165 named \a wName.
166 */
167 double xsec_err(const std::string& wName) const {
168 int pos = windx(wName);
169 if ( pos < 0 ) throw std::runtime_error("GenCrossSection::xsec_err(const std::string&): no weight with given name in this run");
170 return xsec_err(pos);
171 }
172
173 /** @brief Get the cross section error corresponding to the weight
174 with index \a indx.
175 */
176 double xsec_err(const unsigned long& index = 0) const {
177 if ( index < cross_section_errors.size() ) {return cross_section_errors.at(index);}
178 else { throw std::runtime_error("GenCrossSection::xsec_err(const unsigned long&): index outside of range");}
179 return 0.0;
180 }
181
182 bool operator==( const GenCrossSection& ) const; ///< Operator ==
183 bool operator!=( const GenCrossSection& ) const; ///< Operator !=
184 bool is_valid() const; ///< Verify that the instance contains non-zero information
185
186private:
187
188 /** @brief get the weight index given a weight name. */
189 int windx(const std::string& wName) const;
190
191};
192
193} // namespace HepMC3
194
195#endif
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
HepMC3::Attribute::Attribute
Attribute()
Default constructor.
Definition Attribute.h:51
HepMC3::GenCrossSection
Stores additional information about cross-section.
Definition GenCrossSection.h:42
HepMC3::GenCrossSection::xsec
double xsec(const std::string &wName) const
Get the cross section corresponding to the weight named wName.
Definition GenCrossSection.h:149
HepMC3::GenCrossSection::xsec
double xsec(const unsigned long &index=0) const
Get the cross section corresponding to the weight with index indx.
Definition GenCrossSection.h:158
HepMC3::GenCrossSection::get_accepted_events
long get_accepted_events() const
Get the number of accepted events.
Definition GenCrossSection.h:102
HepMC3::GenCrossSection::attempted_events
long attempted_events
The number of events attempted so far.
Definition GenCrossSection.h:50
HepMC3::GenCrossSection::set_xsec_err
void set_xsec_err(const unsigned long &index, const double &xs_err)
Set the cross section error corresponding to the weight with index indx.
Definition GenCrossSection.h:141
HepMC3::GenCrossSection::cross_section_errors
std::vector< double > cross_section_errors
Per-weight errors.
Definition GenCrossSection.h:53
HepMC3::GenCrossSection::set_attempted_events
void set_attempted_events(const long &n_att)
Set the number of attempted events.
Definition GenCrossSection.h:96
HepMC3::GenCrossSection::set_cross_section
void set_cross_section(const double &xs, const double &xs_err, const long &n_acc=-1, const long &n_att=-1)
Set all fields.
Definition GenCrossSection.cc:28
HepMC3::GenCrossSection::set_accepted_events
void set_accepted_events(const long &n_acc)
Set the number of accepted events.
Definition GenCrossSection.h:90
HepMC3::GenCrossSection::xsecs
const std::vector< double > & xsecs() const
Get the cross-sections.
Definition GenCrossSection.h:81
HepMC3::GenCrossSection::cross_sections
std::vector< double > cross_sections
Per-weight cross-section.
Definition GenCrossSection.h:52
HepMC3::GenCrossSection::is_valid
bool is_valid() const
Verify that the instance contains non-zero information.
Definition GenCrossSection.cc:121
HepMC3::GenCrossSection::xsec_errs
const std::vector< double > & xsec_errs() const
Get the cross-section errors.
Definition GenCrossSection.h:85
HepMC3::GenCrossSection::from_string
bool from_string(const std::string &att) override
Implementation of Attribute::from_string.
Definition GenCrossSection.cc:47
HepMC3::GenCrossSection::xsec_err
double xsec_err(const std::string &wName) const
Get the cross section error corresponding to the weight named wName.
Definition GenCrossSection.h:167
HepMC3::GenCrossSection::operator==
bool operator==(const GenCrossSection &) const
Operator ==.
Definition GenCrossSection.cc:113
HepMC3::GenCrossSection::operator!=
bool operator!=(const GenCrossSection &) const
Operator !=.
Definition GenCrossSection.cc:117
HepMC3::GenCrossSection::windx
int windx(const std::string &wName) const
get the weight index given a weight name.
Definition GenCrossSection.cc:23
HepMC3::GenCrossSection::xsec_err
double xsec_err(const unsigned long &index=0) const
Get the cross section error corresponding to the weight with index indx.
Definition GenCrossSection.h:176
HepMC3::GenCrossSection::get_attempted_events
long get_attempted_events() const
Get the number of attempted events.
Definition GenCrossSection.h:108
HepMC3::GenCrossSection::set_xsec
void set_xsec(const std::string &wName, const double &xs)
Set the cross section corresponding to the weight named wName.
Definition GenCrossSection.h:115
HepMC3::GenCrossSection::set_xsec_err
void set_xsec_err(const std::string &wName, const double &xs_err)
Set the cross section error corresponding to the weight named wName.
Definition GenCrossSection.h:132
HepMC3::GenCrossSection::set_xsec
void set_xsec(const unsigned long &index, const double &xs)
Set the cross section corresponding to the weight with index indx.
Definition GenCrossSection.h:124
HepMC3::GenCrossSection::to_string
bool to_string(std::string &att) const override
Implementation of Attribute::to_string.
Definition GenCrossSection.cc:92
HepMC3::GenCrossSection::accepted_events
long accepted_events
The number of events generated so far.
Definition GenCrossSection.h:49
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24