18#include "HepMC3/Attribute.h"
19#include "HepMC3/GenRunInfo.h"
22#include "marley/Error.hh"
23#include "marley/Generator.hh"
24#include "marley/JSON.hh"
25#include "marley/JSONConfig.hh"
26#include "marley/Logger.hh"
27#include "marley/OutputFile.hh"
28#include "marley/OutputFileAscii.hh"
29#include "marley/Weighter.hh"
30#include "marley/marley_utils.hh"
33 #include "marley/OutputFileRoot.hh"
36marley::OutputFile::OutputFile(
const marley::JSON& config ) {
38 if ( !config.has_key(
"file") )
throw marley::Error(
"Missing file"
39 " name for an output file specification in the configuration file." );
41 name_ = config.at(
"file").to_string();
44 if ( config.has_key(
"force") ) force_ = config.at(
"force" ).to_bool();
46 std::string mode_str(
"overwrite" );
47 if ( config.has_key(
"mode") ) mode_str = config.at(
"mode" ).to_string();
49 if ( mode_str ==
"overwrite" ) mode_ = Mode::OVERWRITE;
50 else if ( mode_str ==
"resume" ) mode_ = Mode::RESUME;
51 else throw marley::Error(
"Invalid output mode \"" + mode_str
52 +
"\" given in an output file specification" );
59 auto gen = std::make_unique< marley::Generator >( jc.create_generator() );
67 bool overwrite = marley_utils::prompt_yes_no(
68 "Overwrite file " +
name_ );
71 +
"\" already exists. To overwrite it automatically,"
72 " add \"force\": true to this output file's"
73 " configuration entry. To append new events to the"
74 " existing file, use \"mode\": \"resume\" instead"
75 " of \"mode\": \"overwrite\"." );
84 "MARLEY.ReweightConfig.count" );
85 if ( !count_attr )
return;
86 int rw_count = count_attr->
value();
87 if ( rw_count == 0 )
return;
90 "MARLEY.JSONconfig" );
91 if ( !config_attr )
return;
93 marley::JSON gen_json = marley::JSON::load( config_attr->value() );
96 if ( gen_json.has_key(
"weights") && gen_json.at(
"weights").is_array() ) {
97 for (
const auto& w : gen_json.at(
"weights").array_range() )
98 combined_weights.append( w );
101 for (
int i = 0; i < rw_count; ++i ) {
103 "MARLEY.ReweightConfig." + std::to_string( i ) );
104 if ( !rw_attr )
continue;
106 marley::JSON rw_json = marley::JSON::load( rw_attr->value() );
107 if ( rw_json.has_key(
"weights") && rw_json.at(
"weights").is_array() ) {
108 for (
const auto& w : rw_json.at(
"weights").array_range() )
109 combined_weights.append( w );
113 auto combined_weighter = std::make_shared< marley::Weighter >(
114 combined_weights, gen );
120std::shared_ptr< marley::OutputFile > marley::OutputFile::make_OutputFile(
121 const JSON& output_config )
123 if ( !output_config.has_key(
"format") )
throw marley::Error(
"Missing format"
124 " for an output file specification in the configuration file." );
126 std::string format = output_config.at(
"format" ).to_string();
128 if ( format ==
"ascii" ) {
129 auto out = std::make_shared< marley::OutputFileAscii >( output_config );
130 MARLEY_LOG( INFO,
"io" ) <<
"Opened output file \"" << out->name()
131 <<
"\" (format: ascii)";
135 else if ( format ==
"root" ) {
136 auto out = std::make_shared< marley::OutputFileRoot >( output_config );
137 MARLEY_LOG( INFO,
"io" ) <<
"Opened output file \"" << out->name()
138 <<
"\" (format: root)";
142 else throw marley::Error(
"Invalid output file format \"" + format
143 +
"\" given in an output file specification" );
Stores run-related information.
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Attribute that holds an Integer implemented as an int.
int value() const
get the value associated to this Attribute.
Attribute that holds a string.
Base class for all exceptions thrown by MARLEY functions.
The MARLEY Event generator.
void set_weighter(std::shared_ptr< marley::Weighter > w)
Replace the owned Weighter object.
std::unique_ptr< marley::Generator > restore_generator(const marley::JSON &config)
Helper function for resume() that instantiates a marley::Generator object given the previous JSON con...
void prompt_before_overwrite()
If mode_ is OVERWRITE and the output file already exists and force_ is false, prompt the user before ...
static void merge_reweight_provenance_weights(HepMC3::GenRunInfo &run_info, marley::Generator &gen)
When resuming from a file that has been through one or more reweight passes, merge the reweight prove...
bool check_if_file_exists(const std::string &filename)
Checks that a given file exists (and is readable)
std::string name_
Name of the file to receive output.