44 if ( args.size() < 2u ) {
45 std::string first_arg;
46 if ( !args.empty() ) first_arg = args.front();
50 args.push_front(
"reweight" );
55 if ( first_arg ==
"-h" || first_arg ==
"--help" )
return true;
60 std::string config_file_name( args.front() );
62 std::vector< std::string > input_files( args.begin(), args.end() );
65 marley::JSON rw_config = marley::JSON::load_file( config_file_name );
66 if ( !rw_config.has_key(
"weights") )
throw marley::Error(
"Missing"
67 " \"weights\" key in marley reweight configuration file \""
68 + config_file_name +
"\"" );
70 const auto& json_weights = rw_config.at(
"weights" );
76 if ( !(first_reader >> first_ev) ) {
77 throw marley::Error(
"Failed to read the first event from input file \""
78 + input_files[0] +
"\". The file may be empty or corrupt." );
81 auto first_run_info = first_ev.
run_info();
82 const std::vector< std::string > wgt_names = first_run_info->weight_names();
86 "MARLEY.JSONconfig" );
88 if ( !prior_config_str ) {
90 " configuration from the input file \"" + input_files[0] +
"\"" );
93 auto prior_json_config = marley::JSON::load( prior_config_str->value() );
95 auto gen = std::make_unique< marley::Generator >( jc.create_generator() );
103 for (
const auto& wc : calc_vec ) {
104 if ( std::find( wgt_names.cbegin(), wgt_names.cend(), wc->name() )
105 != wgt_names.cend() )
108 +
"\" from the reweight configuration file \"" + config_file_name
109 +
"\" conflicts with an existing weight in the input file" );
116 for (
auto riter = wgt_names.crbegin();
117 riter != wgt_names.crend(); ++riter )
119 const auto& w_name = *riter;
121 temp_json[
"name" ] = w_name;
122 auto w_calc = std::make_shared< marley
123 ::TrivialWeightCalculator >( temp_json );
124 calc_vec.insert( calc_vec.begin(), w_calc );
131 bool has_rw_section =
false;
132 if ( rw_config.has_key(
"reweight") ) {
133 const marley::JSON& rw_section_ref = rw_config.at(
"reweight" );
135 "The \"reweight\" section in the marley reweight configuration"
136 " file \"" + config_file_name +
"\" must be a JSON object" );
137 rw_section = rw_section_ref;
138 has_rw_section =
true;
141 std::vector< std::shared_ptr<marley::OutputFile> > output_files;
143 if ( has_rw_section && rw_section.has_key(
"output") ) {
146 " \"output\" key in the reweighting configuration must have a value"
147 " that is a JSON array." );
148 else for (
const auto& el : output_set.array_range() ) {
149 if ( el.has_key(
"mode") ) {
150 std::string mode_str = el.at(
"mode" ).to_string();
151 if ( mode_str !=
"overwrite" )
throw marley::Error(
"Only the"
152 " \"overwrite\" output file mode is allowed for a reweighting"
155 output_files.push_back( marley::OutputFile::make_OutputFile(el) );
159 std::string out_config_str =
"{ format: \"ascii\","
160 " file: \"reweighted_events.hepmc3\", mode: \"overwrite\" }";
161 auto out_config = marley::JSON::load( out_config_str );
163 output_files.push_back( marley::OutputFile::make_OutputFile(out_config) );
167 bool multi_file = ( input_files.size() > 1 );
168 auto reweighted_run_info = std::make_shared< HepMC3::GenRunInfo >(
170 reweighted_run_info->set_weight_names( full_name_vec );
176 if ( multi_file ) reweighted_run_info->remove_attribute(
183 "MARLEY.ReweightConfig.count" );
184 if ( count_attr ) rw_index = count_attr->
value();
189 prov_obj[
"weights"] = json_weights;
190 if ( has_rw_section ) {
191 prov_obj[
"reweight"] = rw_section;
194 reweighted_run_info->add_attribute(
195 "MARLEY.ReweightConfig." + std::to_string( rw_index ),
196 std::make_shared< HepMC3::StringAttribute >(
197 prov_obj.dump_string() ) );
199 reweighted_run_info->add_attribute(
200 "MARLEY.ReweightConfig.count",
201 std::make_shared< HepMC3::IntAttribute >( rw_index + 1 ) );
206 for_each_event( input_files,
208 double ,
const auto& )
210 std::cout <<
"Event " << event_count <<
'\n';
214 auto orig_weights = ev.
weights();
221 for (
size_t i = 0; i < orig_weights.size(); ++i )
222 ev.
weights()[ i ] = orig_weights[ i ];
228 for (
const auto& file : output_files )
229 file->write_event( &ev );