18#include <unordered_set>
21#include "HepMC3/GenEvent.h"
24#include "marley/Error.hh"
25#include "marley/Generator.hh"
26#include "marley/JSON.hh"
27#include "marley/OMPWeightCalculator.hh"
28#include "marley/StrengthVariationWeightCalculator.hh"
29#include "marley/WeightCalculator.hh"
30#include "marley/Weighter.hh"
49 if ( !config.is_array() ) {
50 throw marley::Error(
"Non-array JSON configuration passed to constructor"
51 " of marley::Weighter" );
54 for (
const auto& obj : config.array_range() ) {
56 if ( !obj.is_object() ) {
57 throw marley::Error(
"Each weight calculator configuration must be"
58 " specified as a JSON object" );
61 if ( !obj.has_key(
"type") ) {
62 throw marley::Error(
"Missing \"type\" key in a weight calculator"
63 " JSON configuration" );
68 std::shared_ptr< WeightCalculator > wc;
69 auto type = obj.at(
"type" ).to_string();
70 if ( type ==
"trivial" ) {
71 auto twc = std::make_shared< marley::TrivialWeightCalculator >( obj );
72 wc = std::static_pointer_cast< marley::WeightCalculator >( twc );
74 else if ( type ==
"optical_model" ) {
75 auto omp_wc = std::make_shared< marley::OMPWeightCalculator >( obj );
76 wc = std::static_pointer_cast< marley::WeightCalculator >( omp_wc );
78 else if ( type ==
"strength_variation" ) {
81 auto instances = marley::StrengthVariationWeightCalculator
82 ::create_instances( obj, gen );
84 for (
auto& svc : instances ) {
85 if ( !used_names.insert( svc->name() ).second ) {
86 throw marley::Error(
"Duplicate weight calculator name \""
87 + svc->name() +
"\"" );
97 throw marley::Error(
"Unrecognized weight calculator type"
98 " specification \"" + type +
'\"' );
102 if ( !used_names.insert( wc->name() ).second ) {
103 throw marley::Error(
"Duplicate weight calculator name \""
104 + wc->name() +
"\"" );
117 auto& weights_vec =
event.weights();
118 size_t num_weights = weights_vec.size();
123 throw marley::Error(
"The number of configured weights is not the same"
124 " as the number of configured weight calculators" );
128 auto temp_weights = compute_weights( event, gen );
130 for (
size_t w = 0u; w < num_weights; ++w ) {
131 double wgt = temp_weights.at( w );
135 double& evw = weights_vec.at( w );
141std::vector< double > marley::Weighter::compute_weights(
145 std::vector< double > weights;
146 for (
const auto& weight_calc : calc_vec_ ) {
147 double wgt = weight_calc->weight( event, gen );
148 weights.push_back( wgt );
156 std::vector< std::string > names;
157 for (
const auto& wgt_calc :
calc_vec_ ) {
158 names.push_back( wgt_calc->name() );
166 bool currently_using =
false;
168 auto first_calc_name =
calc_vec_.front()->name();
174 if ( currently_using == use_it )
return;
192std::shared_ptr< marley::WeightCalculator > marley::Weighter
193 ::make_cv_weight_calc()
197 auto cv_wgt = std::make_shared< marley
198 ::TrivialWeightCalculator >( temp_js );
199 return std::static_pointer_cast< marley::WeightCalculator >( cv_wgt );
Stores event-related information.
Base class for all exceptions thrown by MARLEY functions.
The MARLEY Event generator.
static const std::string CV_WEIGHT_NAME
Reserved name for the central-value weights (required for compliance with the NuHepMC standard)
std::vector< std::shared_ptr< WeightCalculator > > calc_vec_
Owned vector of weight calculators used to actually compute weights.
std::vector< std::string > get_weight_names() const
Returns the names of the weights associated with all configured weight calculators.
void set_use_cv_weight(bool use_it)
Toggles inclusion of the central-value weight.
std::shared_ptr< marley::WeightCalculator > make_cv_weight_calc()
Helper function to create the weight calculator that handles the central-value weights during event g...
virtual void process_event(HepMC3::GenEvent &event, marley::Generator &gen) override
Processes an input GenEvent object.