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
OutputFile.cc
1
4//
5// This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6//
7// MARLEY is free software: you can redistribute it and/or modify it under the
8// terms of version 3 of the GNU General Public License as published by the
9// Free Software Foundation.
10//
11// For the full text of the license please see COPYING or
12// visit http://opensource.org/licenses/GPL-3.0
13//
14// Please respect the MCnet academic usage guidelines. See GUIDELINES
15// or visit https://www.montecarlonet.org/GUIDELINES for details.
16
17// HepMC3 includes
18#include "HepMC3/Attribute.h"
19#include "HepMC3/GenRunInfo.h"
20
21// MARLEY includes
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"
31
32#ifdef USE_ROOT
33 #include "marley/OutputFileRoot.hh"
34#endif
35
36marley::OutputFile::OutputFile( const marley::JSON& config ) {
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}
54
55std::unique_ptr< marley::Generator > marley::OutputFile::restore_generator(
56 const marley::JSON& config )
57{
58 marley::JSONConfig jc( config );
59 auto gen = std::make_unique< marley::Generator >( jc.create_generator() );
60 return gen;
61}
62
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}
79
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}
117
118// Factory method that constructs an appropriate derived object given the
119// input JSON configuration
120std::shared_ptr< marley::OutputFile > marley::OutputFile::make_OutputFile(
121 const JSON& output_config )
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}
Stores run-related information.
Definition GenRunInfo.h:33
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Definition GenRunInfo.h:181
Attribute that holds an Integer implemented as an int.
Definition Attribute.h:157
int value() const
get the value associated to this Attribute.
Definition Attribute.h:180
Attribute that holds a string.
Definition Attribute.h:343
Base class for all exceptions thrown by MARLEY functions.
Definition Error.hh:26
The MARLEY Event generator.
Definition Generator.hh:54
void set_weighter(std::shared_ptr< marley::Weighter > w)
Replace the owned Weighter object.
Definition Generator.hh:320
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...
Definition OutputFile.cc:55
void prompt_before_overwrite()
If mode_ is OVERWRITE and the output file already exists and force_ is false, prompt the user before ...
Definition OutputFile.cc:63
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...
Definition OutputFile.cc:80
bool check_if_file_exists(const std::string &filename)
Checks that a given file exists (and is readable)
Definition OutputFile.hh:85
std::string name_
Name of the file to receive output.