MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::OutputFile Class Referenceabstract

Abstract base class for objects that deliver output to a file opened by the marley command-line executable. More...

#include <OutputFile.hh>

Inheritance diagram for marley::OutputFile:
marley::OutputFileAscii marley::OutputFileRoot

Public Types

enum class  Format { ASCII , ROOT }
 

Public Member Functions

virtual int_fast64_t bytes_written ()=0
 The number of bytes that have been written during the current MARLEY session to this file.
 
bool mode_is_resume () const
 
const std::string & name () const
 
virtual bool resume (std::unique_ptr< marley::Generator > &gen, long &num_previous_events)=0
 Load a marley::Generator object whose configuration and state were saved to the metadata in the output file.
 
virtual void write_event (HepMC3::GenEvent *event)=0
 Write a new HepMC3::GenEvent to this output file.
 

Static Public Member Functions

static std::shared_ptr< OutputFilemake_OutputFile (const JSON &output_config)
 

Protected Types

enum class  Mode { OVERWRITE , RESUME }
 

Protected Member Functions

 OutputFile (const JSON &output_config)
 
bool check_if_file_exists (const std::string &filename)
 Checks that a given file exists (and is readable)
 
void prompt_before_overwrite ()
 If mode_ is OVERWRITE and the output file already exists and force_ is false, prompt the user before overwriting. If the user declines, throw a marley::Error with a diagnostic message suggesting the "force" and "mode" configuration keys.
 
std::unique_ptr< marley::Generatorrestore_generator (const marley::JSON &config)
 Helper function for resume() that instantiates a marley::Generator object given the previous JSON configuration object.
 

Static Protected Member Functions

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 provenance weight calculator configurations with the original generate-time weights. Builds a combined Weighter and gives the Generator ownership. If no reweight provenance is found, this function is a no-op.
 

Protected Attributes

bool force_
 
Format format_
 Format to use when writing events to the file.
 
Mode mode_
 
std::string name_
 Name of the file to receive output.
 

Detailed Description

Abstract base class for objects that deliver output to a file opened by the marley command-line executable.

Definition at line 35 of file OutputFile.hh.

Member Enumeration Documentation

◆ Format

enum class marley::OutputFile::Format
strong

Definition at line 80 of file OutputFile.hh.

80{ ASCII, ROOT };

◆ Mode

enum class marley::OutputFile::Mode
strongprotected

Definition at line 115 of file OutputFile.hh.

115{ OVERWRITE, RESUME };

Constructor & Destructor Documentation

◆ OutputFile()

marley::OutputFile::OutputFile ( const JSON & output_config)
protected

Definition at line 36 of file OutputFile.cc.

36 {
37
38 if ( !config.has_key("file") ) throw marley::Error( "Missing file"
39 " name for an output file specification in the configuration file." );
40
41 name_ = config.at("file").to_string();
42
43 force_ = false; // default behavior is to prompt before overwriting
44 if ( config.has_key("force") ) force_ = config.at( "force" ).to_bool();
45
46 std::string mode_str( "overwrite" ); // default mode is "overwrite"
47 if ( config.has_key("mode") ) mode_str = config.at( "mode" ).to_string();
48
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" );
53}
std::string name_
Name of the file to receive output.

Member Function Documentation

◆ bytes_written()

virtual int_fast64_t marley::OutputFile::bytes_written ( )
pure virtual

The number of bytes that have been written during the current MARLEY session to this file.

Implemented in marley::OutputFileAscii, and marley::OutputFileRoot.

◆ check_if_file_exists()

bool marley::OutputFile::check_if_file_exists ( const std::string & filename)
inlineprotected

Checks that a given file exists (and is readable)

Definition at line 85 of file OutputFile.hh.

85 {
86 std::ifstream test_stream( filename );
87 return test_stream.good();
88 }

Referenced by marley::OutputFileAscii::open(), marley::OutputFileRoot::open(), and prompt_before_overwrite().

◆ make_OutputFile()

std::shared_ptr< marley::OutputFile > marley::OutputFile::make_OutputFile ( const JSON & output_config)
static

Definition at line 120 of file OutputFile.cc.

122{
123 if ( !output_config.has_key("format") ) throw marley::Error( "Missing format"
124 " for an output file specification in the configuration file." );
125
126 std::string format = output_config.at( "format" ).to_string();
127
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)";
132 return out;
133 }
134 #ifdef USE_ROOT
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)";
139 return out;
140 }
141 #endif
142 else throw marley::Error( "Invalid output file format \"" + format
143 + "\" given in an output file specification" );
144}

◆ merge_reweight_provenance_weights()

void marley::OutputFile::merge_reweight_provenance_weights ( HepMC3::GenRunInfo & run_info,
marley::Generator & gen )
staticprotected

When resuming from a file that has been through one or more reweight passes, merge the reweight provenance weight calculator configurations with the original generate-time weights. Builds a combined Weighter and gives the Generator ownership. If no reweight provenance is found, this function is a no-op.

Definition at line 80 of file OutputFile.cc.

82{
83 auto count_attr = run_info.attribute< HepMC3::IntAttribute >(
84 "MARLEY.ReweightConfig.count" );
85 if ( !count_attr ) return;
86 int rw_count = count_attr->value();
87 if ( rw_count == 0 ) return;
88
89 auto config_attr = run_info.attribute< HepMC3::StringAttribute >(
90 "MARLEY.JSONconfig" );
91 if ( !config_attr ) return;
92
93 marley::JSON gen_json = marley::JSON::load( config_attr->value() );
94
95 marley::JSON combined_weights = marley::JSON::array();
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 );
99 }
100
101 for ( int i = 0; i < rw_count; ++i ) {
102 auto rw_attr = run_info.attribute< HepMC3::StringAttribute >(
103 "MARLEY.ReweightConfig." + std::to_string( i ) );
104 if ( !rw_attr ) continue;
105
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 );
110 }
111 }
112
113 auto combined_weighter = std::make_shared< marley::Weighter >(
114 combined_weights, gen );
115 gen.set_weighter( combined_weighter );
116}
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Definition GenRunInfo.h:181
void set_weighter(std::shared_ptr< marley::Weighter > w)
Replace the owned Weighter object.
Definition Generator.hh:320

References HepMC3::GenRunInfo::attribute(), marley::Generator::set_weighter(), and HepMC3::IntAttribute::value().

Referenced by marley::OutputFileAscii::resume(), and marley::OutputFileRoot::resume().

◆ mode_is_resume()

bool marley::OutputFile::mode_is_resume ( ) const
inline

Definition at line 74 of file OutputFile.hh.

74{ return mode_ == Mode::RESUME; }

◆ name()

const std::string & marley::OutputFile::name ( ) const
inline

Definition at line 52 of file OutputFile.hh.

52{ return name_; }

◆ prompt_before_overwrite()

void marley::OutputFile::prompt_before_overwrite ( )
protected

If mode_ is OVERWRITE and the output file already exists and force_ is false, prompt the user before overwriting. If the user declines, throw a marley::Error with a diagnostic message suggesting the "force" and "mode" configuration keys.

Definition at line 63 of file OutputFile.cc.

63 {
64 if ( mode_ == Mode::OVERWRITE && check_if_file_exists( name_ )
65 && !force_ )
66 {
67 bool overwrite = marley_utils::prompt_yes_no(
68 "Overwrite file " + name_ );
69 if ( !overwrite ) {
70 throw marley::Error( "The output 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\"." );
76 }
77 }
78}
bool check_if_file_exists(const std::string &filename)
Checks that a given file exists (and is readable)
Definition OutputFile.hh:85

References check_if_file_exists(), and name_.

Referenced by marley::OutputFileAscii::open(), and marley::OutputFileRoot::open().

◆ restore_generator()

std::unique_ptr< marley::Generator > marley::OutputFile::restore_generator ( const marley::JSON & config)
protected

Helper function for resume() that instantiates a marley::Generator object given the previous JSON configuration object.

Definition at line 55 of file OutputFile.cc.

57{
58 marley::JSONConfig jc( config );
59 auto gen = std::make_unique< marley::Generator >( jc.create_generator() );
60 return gen;
61}

Referenced by marley::OutputFileAscii::resume(), and marley::OutputFileRoot::resume().

◆ resume()

virtual bool marley::OutputFile::resume ( std::unique_ptr< marley::Generator > & gen,
long & num_previous_events )
pure virtual

Load a marley::Generator object whose configuration and state were saved to the metadata in the output file.

Parameters
[out]genA std::unique_ptr that will be filled with a restored Generator object constructed using the saved metadata
[out]num_previous_eventsThe number of events previously saved to the output file
Returns
true if the Generator is successfully restored using the file's saved metadata, or false otherwise.

Implemented in marley::OutputFileAscii, and marley::OutputFileRoot.

◆ write_event()

virtual void marley::OutputFile::write_event ( HepMC3::GenEvent * event)
pure virtual

Write a new HepMC3::GenEvent to this output file.

If a nullptr is passed to this function, then a marley::Error will be thrown

Implemented in marley::OutputFileAscii, and marley::OutputFileRoot.

Member Data Documentation

◆ force_

bool marley::OutputFile::force_
protected

Definition at line 123 of file OutputFile.hh.

◆ format_

Format marley::OutputFile::format_
protected

Format to use when writing events to the file.

Definition at line 118 of file OutputFile.hh.

◆ mode_

Mode marley::OutputFile::mode_
protected

Definition at line 119 of file OutputFile.hh.

◆ name_

std::string marley::OutputFile::name_
protected

The documentation for this class was generated from the following files: