fire v0.19.0
Framework for sImulation and Reconstruction of Events
fire::io::root::Reader Class Reference

Reading ROOT files into our data structures. More...

#include <Reader.h>

Inheritance diagram for fire::io::root::Reader:
[legend]
Collaboration diagram for fire::io::root::Reader:
[legend]

Public Member Functions

 Reader (const std::string &file_name)
 open input file for reading More...
 
virtual void load_into (BaseData &d) final override
 Following the instructions in fire::io::Reader, we simply call the BaseData's load function. More...
 
virtual std::vector< std::pair< std::string, std::string > > availableObjects () final override
 Get the event objects available in the file. More...
 
virtual std::pair< std::string, int > type (const std::string &path) final override
 Get the type of the input object. More...
 
virtual std::string name () const final override
 Get the name of the file. More...
 
virtual std::size_t entries () const final override
 Number of entries in the file. More...
 
virtual std::size_t runs () const final override
 Number of runs in the file. More...
 
template<typename DataType >
void load (const std::string &name, DataType &obj)
 Load the data at the input name into the input object. More...
 
 Reader (const Reader &)=delete
 no copy constructor of readers
 
void operator= (const Reader &)=delete
 no copy assignment of readers
 
- Public Member Functions inherited from fire::io::Reader
 Reader (const std::string &file_name)
 open the file at the passed location More...
 
virtual ~Reader ()=default
 virtual destructor so derived classes can be closed
 
virtual bool canCopy () const
 Event::get needs to know if the reader implements a copy that advances the entry index of the data sets being read. More...
 
virtual void copy (long unsigned int i_entry, const std::string &path, Writer &output)
 Copy the input object into the output file. More...
 

Private Member Functions

template<typename T >
TBranch * attach (TTree *tree, const std::string &branch_name, T &obj)
 Attach the input object to the TTree on the given branch. More...
 

Static Private Member Functions

static std::string transform (const std::string &dir_name)
 pull out pass and object name, construct branch name More...
 

Private Attributes

TFile * file_
 file being read
 
TTree * event_tree_
 tree of events in the file
 
TTree * run_tree_
 tree of runs in the file
 
std::map< std::string, TBranch * > attached_
 

Additional Inherited Members

- Public Types inherited from fire::io::Reader
using Factory = ::fire::factory::Factory< Reader, std::unique_ptr< Reader >, const std::string & >
 Type of factory used to create readers.
 

Detailed Description

Reading ROOT files into our data structures.

If you are familiar with the ROOT-based Framework, this Reader effectively does tasks done by the framework::Event and framework::EventFile classes.

Constructor & Destructor Documentation

◆ Reader()

fire::io::root::Reader::Reader ( const std::string file_name)

open input file for reading

Exceptions
Exceptionif file cannot be opened, a TTree named 'LDMX_Events' is not found, or if a TTree named 'LDMX_Run' is not found.

After opening the file and retrieve the TTrees we will read, we turn off ROOT's "feature" of handling standard Unix signals.

Parameters
[in]file_namefile to open with ROOT

Member Function Documentation

◆ attach()

template<typename T >
TBranch * fire::io::root::Reader::attach ( TTree *  tree,
const std::string branch_name,
T &  obj 
)
inlineprivate

Attach the input object to the TTree on the given branch.

Exceptions
Exceptionif branch cannot be found.

If the type is arithmetic (numeric_limits are specialized for it), then we use the SetAddress function for attaching to the branch; otherwise, we can use the SetObject method.

Template Parameters
Ttype of object to attach
Parameters
[in]treeTTree to attach to
[in]branch_namename of branch to attach to
[in]objreference to object to attach
Returns
pointer to branch that we attached to

◆ availableObjects()

std::vector< std::pair< std::string, std::string > > fire::io::root::Reader::availableObjects ( )
finaloverridevirtual

Get the event objects available in the file.

We loop through the branches of the event TTree and parse their type names, object name, and pass name. We skip the branch named "EventHeader" to achieve the same behavior as h5::Reader.

Returns
vector of string pairs { obj_name, pass }

Implements fire::io::Reader.

◆ entries()

std::size_t fire::io::root::Reader::entries ( ) const
finaloverridevirtual

Number of entries in the file.

We get this from the number of entries in the event ttree.

Returns
number of entries in the file

Implements fire::io::Reader.

◆ load()

template<typename DataType >
void fire::io::root::Reader::load ( const std::string name,
DataType &  obj 
)
inline

Load the data at the input name into the input object.

See also
transform for how we get the ROOT branch name from the input HDF5-style nested group names.

We choose the tree from the deduced branch name. If the branch name is "RunHeader", the we use the run tree ; otherwise, the event tree.

If the branch name isn't found in our 'attached_' map, then we use attach to attempt to attach it.

At the end, we load the data into memory using TBranch GetReadEntry to retrieve the current entry and increment it before providing it back to GetEntry.

Template Parameters
DataTypetype of data to load into
Parameters
[in]namename of data to load
[in]objreference to load into

◆ load_into()

void fire::io::root::Reader::load_into ( BaseData d)
finaloverridevirtual

Following the instructions in fire::io::Reader, we simply call the BaseData's load function.

Parameters
[in]dData to load data into

Implements fire::io::Reader.

◆ name()

std::string fire::io::root::Reader::name ( ) const
finaloverridevirtual

Get the name of the file.

We use ROOT's TFile rather than storing the name of the file ourselves.

Returns
full name of file currently reading

Implements fire::io::Reader.

◆ runs()

std::size_t fire::io::root::Reader::runs ( ) const
finaloverridevirtual

Number of runs in the file.

We get this from the number of runs in the run ttree.

Returns
number of runs in the file

Implements fire::io::Reader.

◆ transform()

std::string fire::io::root::Reader::transform ( const std::string dir_name)
staticprivate

pull out pass and object name, construct branch name

static caching map since this function is called so frequently

Parameters
[in]dir_nameHDF5-style (<pass>/<object>) group name of event object
Returns
branch name in ROOT-framework-style (<object>_<pass>)

◆ type()

std::pair< std::string, int > fire::io::root::Reader::type ( const std::string path)
finaloverridevirtual

Get the type of the input object.

Implements fire::io::Reader.


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