21#include "HepMC3/Data/GenEventData.h"
22#include "HepMC3/Data/GenRunInfoData.h"
23#include "HepMC3/Attribute.h"
24#include "HepMC3/GenEvent.h"
25#include "HepMC3/GenRunInfo.h"
28#include "marley/Error.hh"
29#include "marley/Generator.hh"
30#include "marley/JSON.hh"
31#include "marley/JSONConfig.hh"
32#include "marley/OutputFileRoot.hh"
34marley::OutputFileRoot::OutputFileRoot(
const marley::JSON& output_config )
35 : marley::OutputFile( output_config )
40marley::OutputFileRoot::~OutputFileRoot() {
51 " not open the file \"" +
name_ +
'\"' );
53 std::string open_mode_str(
"update" );
54 if ( mode_ == Mode::OVERWRITE ) open_mode_str =
"recreate";
58 open_mode_str.c_str() );
69 out_tree_ =
new TTree(
"MARLEY_event_tree",
70 "HepMC3-format MARLEY events" );
79 std::unique_ptr< marley::Generator >& gen,
long& num_previous_events )
81 if ( mode_ != Mode::RESUME ) {
82 throw marley::Error(
"Cannot call OutputFileRoot::resume() for an output"
83 " mode other than \"resume\"" );
89 out_tfile_->GetObject(
"MARLEY_run_info", temp_run_info_data_ptr );
93 throw marley::Error(
"Failed to retrieve run information from the ROOT"
94 " file \"" +
name_ +
"\"" );
98 run_info_ = std::make_shared< HepMC3::GenRunInfo >();
103 "MARLEY.JSONconfig" );
105 if ( !config_attr ) {
106 throw marley::Error(
"Failed to retrieve generator configuration from the"
107 " ROOT file \"" +
name_ +
"\"" );
111 std::string config_str = config_attr->
value();
118 num_previous_events =
out_tree_->GetEntries();
121 out_tree_->GetEntry( num_previous_events - 1 );
123 auto ev = std::make_shared< HepMC3::GenEvent >();
128 "MARLEY.GeneratorState" );
131 throw marley::Error(
"Failed to retrieve generator state from the ROOT"
132 " file \"" +
name_ +
"\"" );
136 std::string state_str = state_attr->
value();
139 gen->seed_using_state_string( state_str );
156 " OutputFileRoot::write_event()" );
159 auto ev_run_info =
event->run_info();
168 "MARLEY_run_info",
"WriteDelete" );
171 throw marley::Error(
"Unexpected change in MARLEY run information" );
Stores event-related information.
Attribute that holds a string.
std::string value() const
get the value associated to this Attribute.
Base class for all exceptions thrown by MARLEY functions.
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.
std::unique_ptr< HepMC3::GenEventData > temp_event_data_
Temporary storage for the events.
HepMC3::GenEventData * temp_event_data_ptr_
virtual void open()
Opens the owned ROOT file with the correct settings.
std::unique_ptr< HepMC3::GenRunInfoData > temp_run_info_data_
Temporary storage for the run information.
TTree * out_tree_
TTree object used to store the events.
std::shared_ptr< HepMC3::GenRunInfo > run_info_
Copy of the run information to check for a mismatch.
virtual void write_event(HepMC3::GenEvent *event) override
Write a new HepMC3::GenEvent to this output file.
void clear_event_data()
Clears the temporary storage for event data.
std::unique_ptr< TFile > out_tfile_
TFile object used to manage input/output.
virtual bool resume(std::unique_ptr< marley::Generator > &gen, long &num_previous_events) override
Load a marley::Generator object whose configuration and state were saved to the metadata in the outpu...
Stores serializable event information.
Stores serializable run information.