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::OutputFileRoot Class Reference

Class that represents an output file in a ROOT-based binary format for HepMC3 events. More...

#include <OutputFileRoot.hh>

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

Public Member Functions

 OutputFileRoot (const JSON &output_config)
 
virtual int_fast64_t bytes_written () override
 The number of bytes that have been written during the current MARLEY session to this file.
 
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 output file.
 
virtual void write_event (HepMC3::GenEvent *event) override
 Write a new HepMC3::GenEvent to this output file.
 
- Public Member Functions inherited from marley::OutputFile
bool mode_is_resume () const
 
const std::string & name () const
 

Protected Member Functions

void clear_event_data ()
 Clears the temporary storage for event data.
 
virtual void open ()
 Opens the owned ROOT file with the correct settings.
 
- Protected Member Functions inherited from marley::OutputFile
 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.
 

Protected Attributes

std::unique_ptr< TFile > out_tfile_
 TFile object used to manage input/output.
 
TTree * out_tree_ = nullptr
 TTree object used to store the events.
 
std::shared_ptr< HepMC3::GenRunInforun_info_
 Copy of the run information to check for a mismatch.
 
std::unique_ptr< HepMC3::GenEventDatatemp_event_data_
 Temporary storage for the events.
 
HepMC3::GenEventDatatemp_event_data_ptr_ = nullptr
 
std::unique_ptr< HepMC3::GenRunInfoDatatemp_run_info_data_
 Temporary storage for the run information.
 
- Protected Attributes inherited from marley::OutputFile
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.
 

Additional Inherited Members

- Public Types inherited from marley::OutputFile
enum class  Format { ASCII , ROOT }
 
- Static Public Member Functions inherited from marley::OutputFile
static std::shared_ptr< OutputFilemake_OutputFile (const JSON &output_config)
 
- Protected Types inherited from marley::OutputFile
enum class  Mode { OVERWRITE , RESUME }
 
- Static Protected Member Functions inherited from marley::OutputFile
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.
 

Detailed Description

Class that represents an output file in a ROOT-based binary format for HepMC3 events.

Definition at line 38 of file OutputFileRoot.hh.

Constructor & Destructor Documentation

◆ OutputFileRoot()

marley::OutputFileRoot::OutputFileRoot ( const JSON & output_config)

Definition at line 34 of file OutputFileRoot.cc.

35 : marley::OutputFile( output_config )
36{
37 this->open();
38}
virtual void open()
Opens the owned ROOT file with the correct settings.

◆ ~OutputFileRoot()

marley::OutputFileRoot::~OutputFileRoot ( )
virtual

Definition at line 40 of file OutputFileRoot.cc.

40 {
41 out_tfile_->cd();
42 out_tree_->Write();
43}
TTree * out_tree_
TTree object used to store the events.
std::unique_ptr< TFile > out_tfile_
TFile object used to manage input/output.

Member Function Documentation

◆ bytes_written()

virtual int_fast64_t marley::OutputFileRoot::bytes_written ( )
inlineoverridevirtual

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

Implements marley::OutputFile.

Definition at line 49 of file OutputFileRoot.hh.

50 { return out_tfile_->GetBytesWritten(); }

References out_tfile_.

◆ clear_event_data()

void marley::OutputFileRoot::clear_event_data ( )
protected

Clears the temporary storage for event data.

Definition at line 180 of file OutputFileRoot.cc.

180 {
181 temp_event_data_->particles.clear();
182 temp_event_data_->vertices.clear();
183 temp_event_data_->links1.clear();
184 temp_event_data_->links2.clear();
185 temp_event_data_->attribute_id.clear();
186 temp_event_data_->attribute_name.clear();
187 temp_event_data_->attribute_string.clear();
188}
std::unique_ptr< HepMC3::GenEventData > temp_event_data_
Temporary storage for the events.

References temp_event_data_.

Referenced by resume(), and write_event().

◆ open()

void marley::OutputFileRoot::open ( )
protectedvirtual

Opens the owned ROOT file with the correct settings.

Definition at line 45 of file OutputFileRoot.cc.

45 {
46
48
49 if ( mode_ == Mode::RESUME && !check_if_file_exists( name_ ) )
50 throw marley::Error( "Cannot resume run. Could"
51 " not open the file \"" + name_ + '\"' );
52
53 std::string open_mode_str( "update" );
54 if ( mode_ == Mode::OVERWRITE ) open_mode_str = "recreate";
55
56 // Open the ROOT file with the appropriate mode
57 out_tfile_ = std::make_unique< TFile >( name_.c_str(),
58 open_mode_str.c_str() );
59
60 // Set up storage for event data to be written to the output
61 temp_event_data_ptr_ = new HepMC3::GenEventData;
63
64 // Check if the event TTree already exists in the file. If it does,
65 // use it. Otherwise, create a new one and set up the "event" branch.
66 out_tfile_->GetObject( "MARLEY_event_tree", out_tree_ );
67 if ( !out_tree_ ) {
68 out_tfile_->cd();
69 out_tree_ = new TTree( "MARLEY_event_tree",
70 "HepMC3-format MARLEY events" );
71 out_tree_->Branch( "event", temp_event_data_ptr_ );
72 }
73
74 // Set the "event" branch address for the event data
75 out_tree_->SetBranchAddress( "event", &temp_event_data_ptr_ );
76}
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
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.
HepMC3::GenEventData * temp_event_data_ptr_

References marley::OutputFile::check_if_file_exists(), marley::OutputFile::name_, out_tfile_, out_tree_, marley::OutputFile::prompt_before_overwrite(), temp_event_data_, and temp_event_data_ptr_.

◆ resume()

bool marley::OutputFileRoot::resume ( std::unique_ptr< marley::Generator > & gen,
long & num_previous_events )
overridevirtual

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.

Implements marley::OutputFile.

Definition at line 78 of file OutputFileRoot.cc.

80{
81 if ( mode_ != Mode::RESUME ) {
82 throw marley::Error( "Cannot call OutputFileRoot::resume() for an output"
83 " mode other than \"resume\"" );
84 return false;
85 }
86
87 // Retrieve the run information from the file
88 HepMC3::GenRunInfoData* temp_run_info_data_ptr = nullptr;
89 out_tfile_->GetObject( "MARLEY_run_info", temp_run_info_data_ptr );
90 temp_run_info_data_.reset( temp_run_info_data_ptr );
91
92 if ( !temp_run_info_data_ ) {
93 throw marley::Error( "Failed to retrieve run information from the ROOT"
94 " file \"" + name_ + "\"" );
95 return false;
96 }
97
98 run_info_ = std::make_shared< HepMC3::GenRunInfo >();
99 run_info_->read_data( *temp_run_info_data_ );
100
101 // Get the generator JSON configuration
102 auto config_attr = run_info_->attribute< HepMC3::StringAttribute >(
103 "MARLEY.JSONconfig" );
104
105 if ( !config_attr ) {
106 throw marley::Error( "Failed to retrieve generator configuration from the"
107 " ROOT file \"" + name_ + "\"" );
108 return false;
109 }
110
111 std::string config_str = config_attr->value();
112
113 // Initialize the generator using the JSON configuration
114 marley::JSON config = marley::JSON::load( config_str );
115 gen = this->restore_generator( config );
116
117 // Retrieve the last event from the TTree
118 num_previous_events = out_tree_->GetEntries();
119
120 this->clear_event_data();
121 out_tree_->GetEntry( num_previous_events - 1 );
122
123 auto ev = std::make_shared< HepMC3::GenEvent >();
124 ev->read_data( *temp_event_data_ );
125
126 // Retrieve the generator state string from this event
127 auto state_attr = ev->attribute< HepMC3::StringAttribute >(
128 "MARLEY.GeneratorState" );
129
130 if ( !state_attr ) {
131 throw marley::Error( "Failed to retrieve generator state from the ROOT"
132 " file \"" + name_ + "\"" );
133 return false;
134 }
135
136 std::string state_str = state_attr->value();
137
138 // Restore the random number generator state of the generator
139 gen->seed_using_state_string( state_str );
140
141 // If the file has reweight provenance, merge the accumulated weight
142 // calculator configurations into the Generator's Weighter before
143 // assigning the run info. This ensures new events have the correct
144 // number of weight slots to match the existing events in the file.
146
147 // Use the retrieved run information for making new events
148 gen->set_run_info( run_info_ );
149
150 return true;
151}
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
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
std::unique_ptr< HepMC3::GenRunInfoData > temp_run_info_data_
Temporary storage for the run information.
std::shared_ptr< HepMC3::GenRunInfo > run_info_
Copy of the run information to check for a mismatch.
void clear_event_data()
Clears the temporary storage for event data.

References clear_event_data(), marley::OutputFile::merge_reweight_provenance_weights(), marley::OutputFile::name_, out_tfile_, out_tree_, marley::OutputFile::restore_generator(), run_info_, temp_event_data_, temp_run_info_data_, and HepMC3::StringAttribute::value().

◆ write_event()

void marley::OutputFileRoot::write_event ( HepMC3::GenEvent * event)
overridevirtual

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

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

Implements marley::OutputFile.

Definition at line 153 of file OutputFileRoot.cc.

153 {
154
155 if ( !event ) throw marley::Error( "Null pointer passed to"
156 " OutputFileRoot::write_event()" );
157
158 // If the run information hasn't been set, then initialize it and save it
159 auto ev_run_info = event->run_info();
160 if ( !run_info_ && ev_run_info ) {
161 run_info_ = ev_run_info;
162
163 temp_run_info_data_ = std::make_unique< HepMC3::GenRunInfoData >();
164 run_info_->write_data( *temp_run_info_data_ );
165
166 out_tfile_->cd();
167 out_tfile_->WriteObject( temp_run_info_data_.get(),
168 "MARLEY_run_info", "WriteDelete" );
169 }
170 else if ( run_info_ != ev_run_info ) {
171 throw marley::Error( "Unexpected change in MARLEY run information" );
172 }
173
174 // Clear out any pre-existing contents from the temporary event storage
175 this->clear_event_data();
176 event->write_data( *temp_event_data_ );
177 out_tree_->Fill();
178}

References clear_event_data(), out_tfile_, out_tree_, run_info_, temp_event_data_, and temp_run_info_data_.

Member Data Documentation

◆ out_tfile_

std::unique_ptr< TFile > marley::OutputFileRoot::out_tfile_
protected

TFile object used to manage input/output.

Definition at line 63 of file OutputFileRoot.hh.

Referenced by bytes_written(), open(), resume(), and write_event().

◆ out_tree_

TTree* marley::OutputFileRoot::out_tree_ = nullptr
protected

TTree object used to store the events.

Definition at line 68 of file OutputFileRoot.hh.

Referenced by open(), resume(), and write_event().

◆ run_info_

std::shared_ptr< HepMC3::GenRunInfo > marley::OutputFileRoot::run_info_
protected

Copy of the run information to check for a mismatch.

Definition at line 81 of file OutputFileRoot.hh.

Referenced by resume(), and write_event().

◆ temp_event_data_

std::unique_ptr< HepMC3::GenEventData > marley::OutputFileRoot::temp_event_data_
protected

Temporary storage for the events.

Definition at line 71 of file OutputFileRoot.hh.

Referenced by clear_event_data(), open(), resume(), and write_event().

◆ temp_event_data_ptr_

HepMC3::GenEventData* marley::OutputFileRoot::temp_event_data_ptr_ = nullptr
protected

Bare pointer to the event data storage (needed for some ROOT manipulations)

Definition at line 75 of file OutputFileRoot.hh.

Referenced by open().

◆ temp_run_info_data_

std::unique_ptr< HepMC3::GenRunInfoData > marley::OutputFileRoot::temp_run_info_data_
protected

Temporary storage for the run information.

Definition at line 78 of file OutputFileRoot.hh.

Referenced by resume(), and write_event().


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