21#include "HepMC3/GenEvent.h"
22#include "HepMC3/ReaderAscii.h"
25#include "marley/marley_utils.hh"
26#include "marley/Error.hh"
27#include "marley/FileManager.hh"
28#include "marley/EventFileReader.hh"
35 #include "HepMC3/Data/GenEventData.h"
39marley::EventFileReader::EventFileReader(
const std::string& file_name )
40 : file_name_( file_name )
55 " not read from the file \"" +
file_name_ +
'\"' );
63 auto temp_error_level = gErrorIgnoreLevel;
64 gErrorIgnoreLevel = kFatal;
68 tfile_ = std::unique_ptr<TFile>( TFile::Open(
file_name_.c_str(),
"read") );
72 gErrorIgnoreLevel = temp_error_level;
75 format_ = marley::OutputFile::Format::ROOT;
83 bool read_ok = temp_reader.
read_event( temp_event );
88 auto run_info = temp_event.
run_info();
90 throw marley::Error(
"Missing run information while parsing an"
91 " ASCII-format HepMC3 file" );
96 format_ = marley::OutputFile::Format::ASCII;
109 case marley::OutputFile::Format::ASCII:
112 reader_ = std::make_shared< HepMC3::ReaderAscii >(
in_ );
117 case marley::OutputFile::Format::ROOT:
119 tfile_->GetObject(
"MARLEY_event_tree", ttree_ );
120 if ( !ttree_ )
throw marley::Error(
"Failed to load MARLEY event TTree"
121 " from the ROOT file \"" +
file_name_ +
'\"' );
123 temp_event_data_ = std::make_unique< HepMC3::GenEventData >();
124 temp_event_data_ptr_ = temp_event_data_.get();
125 ttree_->SetBranchAddress(
"event", &temp_event_data_ptr_ );
127 tfile_->GetObject(
"MARLEY_run_info", temp_run_info_data_ );
128 if ( !temp_run_info_data_ )
throw marley::Error(
"Failed to load"
129 " MARLEY run information from the ROOT file \"" +
file_name_ +
'\"' );
131 run_info_ = std::make_shared< HepMC3::GenRunInfo >();
132 run_info_->read_data( *temp_run_info_data_ );
141 throw marley::Error(
"Unrecognized file format encountered in"
142 " marley::EventFileReader::initialize()" );
151 case marley::OutputFile::Format::ASCII:
153 bool read_ok =
reader_->read_event( ev );
154 return read_ok &&
in_;
159 case marley::OutputFile::Format::ROOT:
162 if ( event_num_ < ttree_->GetEntries() ) {
164 temp_event_data_->particles.clear();
165 temp_event_data_->vertices.clear();
166 temp_event_data_->links1.clear();
167 temp_event_data_->links2.clear();
168 temp_event_data_->attribute_id.clear();
169 temp_event_data_->attribute_name.clear();
170 temp_event_data_->attribute_string.clear();
172 ttree_->GetEntry( event_num_ );
186 throw marley::Error(
"Unrecognized file format encountered in"
187 " marley::EventFileReader::next_event()" );
194marley::EventFileReader::operator bool()
const {
198 case marley::OutputFile::Format::ASCII:
200 return static_cast< bool >(
in_ );
205 case marley::OutputFile::Format::ROOT:
207 return ( tfile_ && ttree_ && event_num_ < ttree_->GetEntries() );
213 throw marley::Error(
"Unrecognized file format encountered in"
214 " marley::EventFileReader::operator bool()" );
224 " read MARLEY events from the file \"" +
file_name_ +
'\"' );
235 * marley_utils::fm2_to_minus40_cm2 * 1e2;
243 "NuHepMC.FluxAveragedTotalCrossSection" );
244 if ( !avg_xsec_attr ) {
245 throw marley::Error(
"Missing flux-averaged total cross section while"
246 " parsing a HepMC3 file" );
252 / ( marley_utils::hbar_c2 * marley_utils::fm2_to_picobarn );
Attribute that holds a real number as a double.
Stores event-related information.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Stores run-related information.
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
GenEvent I/O parsing for structured text files.
bool read_event(GenEvent &evt) override
Load event from file.
Base class for all exceptions thrown by MARLEY functions.
virtual void initialize()
Prepares the file for reading the events.
virtual bool deduce_file_format()
Helper function that auto-detects which of the available output formats is appropriate for the reques...
bool initialized_
Flag that indicates whether initialize() has been called or not.
double flux_avg_tot_xs_
Flux-averaged total cross section (MeV -2) used to produce the events in the file,...
virtual bool next_event(HepMC3::GenEvent &ev)
Read the next MARLEY event record from the file.
void ensure_initialized()
This function should be called at the beginning of all public member functions of EventFileReader tha...
std::string file_name_
Name of the file (with any needed path specification) to be read.
void get_flux_averaged_xsec(const HepMC3::GenRunInfo &run_info)
double flux_averaged_xsec(bool natural_units=false)
Returns the flux-averaged total cross section used to produce the events in the file.
std::shared_ptr< HepMC3::ReaderAscii > reader_
Helper object used to interpret ASCII-format HepMC3 files.
OutputFile::Format format_
Format of the output file being read.
std::ifstream in_
Input stream used to read from textual output formats.
static const FileManager & Instance()
Get a const reference to the singleton instance of the FileManager.