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

Object that parses MARLEY output files. More...

#include <EventFileReader.hh>

Public Member Functions

 EventFileReader (const std::string &file_name)
 
double flux_averaged_xsec (bool natural_units=false)
 Returns the flux-averaged total cross section used to produce the events in the file.
 
virtual bool next_event (HepMC3::GenEvent &ev)
 Read the next MARLEY event record from the file.
 
virtual operator bool () const
 Implicit boolean conversion allows the state of the input stream (or ROOT file) to be tested for readiness to read in another event.
 
EventFileReaderoperator>> (HepMC3::GenEvent &ev)
 Stream operator for reading in the next event.
 

Protected Member Functions

virtual bool deduce_file_format ()
 Helper function that auto-detects which of the available output formats is appropriate for the requested file.
 
void ensure_initialized ()
 This function should be called at the beginning of all public member functions of EventFileReader that interact with data in the file.
 
void get_flux_averaged_xsec (const HepMC3::GenRunInfo &run_info)
 
virtual void initialize ()
 Prepares the file for reading the events.
 

Protected Attributes

std::string file_name_
 Name of the file (with any needed path specification) to be read.
 
double flux_avg_tot_xs_ = 0.
 Flux-averaged total cross section (MeV -2) used to produce the events in the file, or zero if that information is not included in a particular format.
 
OutputFile::Format format_
 Format of the output file being read.
 
std::ifstream in_
 Input stream used to read from textual output formats.
 
bool initialized_ = false
 Flag that indicates whether initialize() has been called or not.
 
std::shared_ptr< HepMC3::ReaderAsciireader_
 Helper object used to interpret ASCII-format HepMC3 files.
 

Detailed Description

Object that parses MARLEY output files.

Definition at line 48 of file EventFileReader.hh.

Constructor & Destructor Documentation

◆ EventFileReader()

marley::EventFileReader::EventFileReader ( const std::string & file_name)

Definition at line 39 of file EventFileReader.cc.

40 : file_name_( file_name )
41{
42}
std::string file_name_
Name of the file (with any needed path specification) to be read.

Member Function Documentation

◆ deduce_file_format()

bool marley::EventFileReader::deduce_file_format ( )
protectedvirtual

Helper function that auto-detects which of the available output formats is appropriate for the requested file.

Definition at line 47 of file EventFileReader.cc.

47 {
48
49 // Before we bother to check anything else, see if the file exists and is
50 // readable. Skip the MARLEY search path in this case (the event file should
51 // have been passed to the constructor with any needed path specification).
52 // Complain if the file cannot be read.
53 const auto& fm = marley::FileManager::Instance();
54 if ( fm.find_file(file_name_, "").empty() ) throw marley::Error( "Could"
55 " not read from the file \"" + file_name_ + '\"' );
56
57 // Create a temporary event object to use for the following format checks
58 HepMC3::GenEvent temp_event;
59
60 #ifdef USE_ROOT
61 // Before checking if the file is in ROOT format, completely suppress any
62 // error messages from ROOT.
63 auto temp_error_level = gErrorIgnoreLevel;
64 gErrorIgnoreLevel = kFatal;
65
66 // Try to read in a HepMC3::GenEvent from the file assuming that the ROOT
67 // output format was used
68 tfile_ = std::unique_ptr<TFile>( TFile::Open(file_name_.c_str(), "read") );
69
70 // We've completed the ROOT format check, so restore the old error messaging
71 // behavior
72 gErrorIgnoreLevel = temp_error_level;
73
74 if ( tfile_ ) {
75 format_ = marley::OutputFile::Format::ROOT;
76 return true;
77 }
78 #endif
79
80 // Try to read in a HepMC3::GenEvent from the file assuming that the ASCII
81 // output format was used
82 HepMC3::ReaderAscii temp_reader( file_name_ );
83 bool read_ok = temp_reader.read_event( temp_event );
84 if ( read_ok ) {
85
86 // Save the flux-averaged total xsec from the run information for easy
87 // retrieval
88 auto run_info = temp_event.run_info();
89 if ( !run_info ) {
90 throw marley::Error( "Missing run information while parsing an"
91 " ASCII-format HepMC3 file" );
92 }
93
94 this->get_flux_averaged_xsec( *run_info );
95
96 format_ = marley::OutputFile::Format::ASCII;
97 return true;
98 }
99
100 // TODO: add other formats here as needed
101
102 // If everything else failed, then complain that events could not be read
103 return false;
104}
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:144
void get_flux_averaged_xsec(const HepMC3::GenRunInfo &run_info)
OutputFile::Format format_
Format of the output file being read.
static const FileManager & Instance()
Get a const reference to the singleton instance of the FileManager.

References file_name_, format_, get_flux_averaged_xsec(), marley::FileManager::Instance(), HepMC3::ReaderAscii::read_event(), and HepMC3::GenEvent::run_info().

Referenced by ensure_initialized().

◆ ensure_initialized()

void marley::EventFileReader::ensure_initialized ( )
protected

This function should be called at the beginning of all public member functions of EventFileReader that interact with data in the file.

It provides necessary initialization as a workaround to calling virtual functions in the constructor

Definition at line 220 of file EventFileReader.cc.

220 {
221 if ( !initialized_ ) {
222
223 if ( !this->deduce_file_format() ) throw marley::Error( "Could not"
224 " read MARLEY events from the file \"" + file_name_ + '\"' );
225
226 this->initialize();
227 initialized_ = true;
228 }
229}
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.

References deduce_file_format(), file_name_, initialize(), and initialized_.

Referenced by flux_averaged_xsec(), and next_event().

◆ flux_averaged_xsec()

double marley::EventFileReader::flux_averaged_xsec ( bool natural_units = false)

Returns the flux-averaged total cross section used to produce the events in the file.

For file formats which do not include this information, this function will return zero

Parameters
[in]natural_unitsIf true, then this function will return the flux-averaged total cross section in natural units (MeV -2). If false (default), then 10-42 cm2 will be used.

Definition at line 231 of file EventFileReader.cc.

231 {
232 this->ensure_initialized();
233 if ( natural_units ) return flux_avg_tot_xs_;
234 double result = flux_avg_tot_xs_ * marley_utils::hbar_c2
235 * marley_utils::fm2_to_minus40_cm2 * 1e2; // 10^{-42} cm^2
236 return result;
237}
double flux_avg_tot_xs_
Flux-averaged total cross section (MeV -2) used to produce the events in the file,...
void ensure_initialized()
This function should be called at the beginning of all public member functions of EventFileReader tha...

References ensure_initialized(), and flux_avg_tot_xs_.

◆ get_flux_averaged_xsec()

void marley::EventFileReader::get_flux_averaged_xsec ( const HepMC3::GenRunInfo & run_info)
protected

Helper function for loading the flux-averaged total cross section information from the HepMC3 run information

Definition at line 239 of file EventFileReader.cc.

241{
242 auto avg_xsec_attr = run_info.attribute< HepMC3::DoubleAttribute >(
243 "NuHepMC.FluxAveragedTotalCrossSection" );
244 if ( !avg_xsec_attr ) {
245 throw marley::Error( "Missing flux-averaged total cross section while"
246 " parsing a HepMC3 file" );
247 }
248
249 // Retrieve the flux-averaged total cross section and convert it back to
250 // natural units (MeV^{-2}) from picobarn
251 flux_avg_tot_xs_ = avg_xsec_attr->value()
252 / ( marley_utils::hbar_c2 * marley_utils::fm2_to_picobarn );
253}
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Definition GenRunInfo.h:181

References HepMC3::GenRunInfo::attribute(), and flux_avg_tot_xs_.

Referenced by deduce_file_format(), and initialize().

◆ initialize()

void marley::EventFileReader::initialize ( )
protectedvirtual

Prepares the file for reading the events.

Definition at line 106 of file EventFileReader.cc.

106 {
107 switch ( format_ ) {
108
109 case marley::OutputFile::Format::ASCII:
110 {
111 in_.open( file_name_ );
112 reader_ = std::make_shared< HepMC3::ReaderAscii >( in_ );
113 break;
114 }
115
116 #ifdef USE_ROOT
117 case marley::OutputFile::Format::ROOT:
118 {
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_ + '\"' );
122
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_ );
126
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_ + '\"' );
130
131 run_info_ = std::make_shared< HepMC3::GenRunInfo >();
132 run_info_->read_data( *temp_run_info_data_ );
133
134 this->get_flux_averaged_xsec( *run_info_ );
135
136 break;
137 }
138 #endif
139
140 default:
141 throw marley::Error( "Unrecognized file format encountered in"
142 " marley::EventFileReader::initialize()" );
143 }
144}
std::shared_ptr< HepMC3::ReaderAscii > reader_
Helper object used to interpret ASCII-format HepMC3 files.
std::ifstream in_
Input stream used to read from textual output formats.

References file_name_, format_, get_flux_averaged_xsec(), in_, and reader_.

Referenced by ensure_initialized().

◆ next_event()

bool marley::EventFileReader::next_event ( HepMC3::GenEvent & ev)
virtual

Read the next MARLEY event record from the file.

Parameters
[out]evReference to the object that will be filled with the next event record
Returns
True if reading the next event was successful, or false otherwise. This behavior is designed to be used as a while loop condition for iterating over events in the output file

Definition at line 146 of file EventFileReader.cc.

147{
148 this->ensure_initialized();
149 switch ( format_ ) {
150
151 case marley::OutputFile::Format::ASCII:
152 {
153 bool read_ok = reader_->read_event( ev );
154 return read_ok && in_;
155 break;
156 }
157
158 #ifdef USE_ROOT
159 case marley::OutputFile::Format::ROOT:
160 {
161 ++event_num_;
162 if ( event_num_ < ttree_->GetEntries() ) {
163
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();
171
172 ttree_->GetEntry( event_num_ );
173 ev.read_data( *temp_event_data_ );
174 ev.set_run_info( run_info_ );
175 return true;
176 }
177
178 ev = HepMC3::GenEvent();
179 return false;
180
181 break;
182 }
183 #endif
184
185 default:
186 throw marley::Error( "Unrecognized file format encountered in"
187 " marley::EventFileReader::next_event()" );
188 }
189
190 ev = HepMC3::GenEvent();
191 return false;
192}
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:148
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.

References ensure_initialized(), format_, in_, HepMC3::GenEvent::read_data(), reader_, and HepMC3::GenEvent::set_run_info().

Referenced by operator>>().

◆ operator bool()

marley::EventFileReader::operator bool ( ) const
virtual

Implicit boolean conversion allows the state of the input stream (or ROOT file) to be tested for readiness to read in another event.

Definition at line 194 of file EventFileReader.cc.

194 {
195
196 switch ( format_ ) {
197
198 case marley::OutputFile::Format::ASCII:
199 {
200 return static_cast< bool >( in_ );
201 break;
202 }
203
204 #ifdef USE_ROOT
205 case marley::OutputFile::Format::ROOT:
206 {
207 return ( tfile_ && ttree_ && event_num_ < ttree_->GetEntries() );
208 break;
209 }
210 #endif
211
212 default:
213 throw marley::Error( "Unrecognized file format encountered in"
214 " marley::EventFileReader::operator bool()" );
215 }
216
217 return false;
218}

References format_, and in_.

◆ operator>>()

EventFileReader & marley::EventFileReader::operator>> ( HepMC3::GenEvent & ev)
inline

Stream operator for reading in the next event.

Definition at line 75 of file EventFileReader.hh.

75 {
76 next_event( ev );
77 return *this;
78 }
virtual bool next_event(HepMC3::GenEvent &ev)
Read the next MARLEY event record from the file.

References next_event().

Member Data Documentation

◆ file_name_

std::string marley::EventFileReader::file_name_
protected

Name of the file (with any needed path specification) to be read.

Definition at line 89 of file EventFileReader.hh.

Referenced by deduce_file_format(), ensure_initialized(), and initialize().

◆ flux_avg_tot_xs_

double marley::EventFileReader::flux_avg_tot_xs_ = 0.
protected

Flux-averaged total cross section (MeV -2) used to produce the events in the file, or zero if that information is not included in a particular format.

Definition at line 133 of file EventFileReader.hh.

Referenced by flux_averaged_xsec(), and get_flux_averaged_xsec().

◆ format_

OutputFile::Format marley::EventFileReader::format_
protected

Format of the output file being read.

This format will be determined automatically by deduce_file_format() and does not need to be specified by the user

Definition at line 94 of file EventFileReader.hh.

Referenced by deduce_file_format(), initialize(), next_event(), and operator bool().

◆ in_

std::ifstream marley::EventFileReader::in_
protected

Input stream used to read from textual output formats.

Definition at line 97 of file EventFileReader.hh.

Referenced by initialize(), next_event(), and operator bool().

◆ initialized_

bool marley::EventFileReader::initialized_ = false
protected

Flag that indicates whether initialize() has been called or not.

To avoid problems with using virtual functions in the constructor, we defer nearly all of the initialization to the first call to one of the other public member functions.

Definition at line 139 of file EventFileReader.hh.

Referenced by ensure_initialized().

◆ reader_

std::shared_ptr< HepMC3::ReaderAscii > marley::EventFileReader::reader_
protected

Helper object used to interpret ASCII-format HepMC3 files.

Definition at line 100 of file EventFileReader.hh.

Referenced by initialize(), and next_event().


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