fire v0.19.0
Framework for sImulation and Reconstruction of Events
fire::UserReader Class Reference

class for easier reading of files written by fire More...

#include <UserReader.h>

Collaboration diagram for fire::UserReader:
[legend]

Public Member Functions

 UserReader (bool wrap_around=false)
 Configure the reading mode. More...
 
 UserReader (const std::string &fn, unsigned long int n=0, bool wrap_around=false)
 Configure the reading mode and open a file. More...
 
void open (const std::string &fn, unsigned long int n=0)
 Open the provided file, skipping an initial number of entries. More...
 
bool next ()
 go to the next event entry in the file More...
 
unsigned long int entries () const
 number of events in this file More...
 
bool is_open () const
 check if reader is open or not More...
 
template<typename T >
const T & get (const std::string &name, const std::string &pass="") const
 get the object for the current entry under the passed name More...
 

Private Attributes

Event event_
 event bus for this reader
 
std::unique_ptr< io::Readerreader_
 handle of underlying reader
 
unsigned long int i_entry_ {0}
 index of entry within file that is open
 
bool wrap_around_ {false}
 do we wrap from end to beginning or throw exception?
 
bool in_file_ {false}
 have we started yet?
 

Detailed Description

class for easier reading of files written by fire

The UserReader uses a specially-configured Event instance to interface with the file it is reading. This allows for it to read everything in the same manner that a processor would see while giving a user control over when to go to the next event in the file.

Note
opening the Event bus with a NULL writer is dangerous. Any development of this class needs to be done extremely carefully and tested thoroughly.

Basic Usage

fire::UserReader r("filename.h5");
while (r.next()) {
const auto& obj = r.get<T>(name,pass);
}
class for easier reading of files written by fire
Definition: UserReader.h:44

Wrap Around Usage

If the reader is configured to wrap around from the end of the file to the beginning, then the reader cannot be in control of the event loop (since it would be an infinite loop). The dummy example below shows how a user can go through a file of arbitrary number of non-zero events repeating if necessary.

fire::UserReader r("filename.h5", n_events_to_skip, true);
for (std::size_t i{0}; i < n_events_wanted; ++i) {
r.next();
const auto& obj = r.get<T>(name, pass);
}

Constructor & Destructor Documentation

◆ UserReader() [1/2]

fire::UserReader::UserReader ( bool  wrap_around = false)

Configure the reading mode.

Parameters
[in]wrap_around(optional) will we loop from end of file to beginning?

◆ UserReader() [2/2]

fire::UserReader::UserReader ( const std::string fn,
unsigned long int  n = 0,
bool  wrap_around = false 
)

Configure the reading mode and open a file.

See also
open for details on opening the file
Parameters
[in]fnfile path to be opened
[in]n(optional) number of entries at beginning of file to skip
[in]wrap_around(optional) will we loop from end of file to beginning?

Member Function Documentation

◆ entries()

unsigned long int fire::UserReader::entries ( ) const

number of events in this file

Returns
number of event entries

◆ get()

template<typename T >
const T & fire::UserReader::get ( const std::string name,
const std::string pass = "" 
) const
inline

get the object for the current entry under the passed name

This is a direct call to Event::get so look there for more detailed documentation.

Template Parameters
Ttype of data
Parameters
[in]namename of event object
[in]pass(optional) name of pass that produced the event object

◆ is_open()

bool fire::UserReader::is_open ( ) const

check if reader is open or not

Returns
true if reader is open and false otherwise

◆ next()

bool fire::UserReader::next ( )

go to the next event entry in the file

Exceptions
Exceptionif a file hasn't been opened yet

We simply mimic the interaction between Process and Event such that Event::next is called "after" the last event and Event::load is called in preparation for the next event. If we have been configured to wrap around, then we simply re-call open and next instead of returning false at the end of the file.

Returns
true if another entry has been loaded

◆ open()

void fire::UserReader::open ( const std::string fn,
unsigned long int  n = 0 
)

Open the provided file, skipping an initial number of entries.

See also
io::open for how we open ROOT and/or H5 files
io::root::Reader for reading of ROOT files
io::h5::Reader for reading of H5 files
Event::setInputFile for connecting the file reader and event bus

Skipping the initial number of entries is done by just calling next the requested number of times. Without any objects loaded onto the event bus, this is simply moving the entry index within Event.

Parameters
[in]fnfile path to be opened
[in]n(optional) number of entries at beginning of file to skip

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