fire v0.19.0
Framework for sImulation and Reconstruction of Events
Reader.h
1#ifndef FIRE_IO_ROOT_READER_H_
2#define FIRE_IO_ROOT_READER_H_
3
4#include <set>
5
6#include "TFile.h"
7#include "TTree.h"
8#include "TBranch.h"
9
10#include "fire/io/Reader.h"
11#include "fire/exception/Exception.h"
12
20namespace fire::io::root {
21
29class Reader : public ::fire::io::Reader {
30 public:
42 Reader(const std::string& file_name);
43
50 virtual void load_into(BaseData& d) final override;
51
62
66 virtual std::pair<std::string, int> type(const std::string& path) final override;
67
75 virtual std::string name() const final override;
76
84 virtual std::size_t entries() const final override;
85
93 virtual std::size_t runs() const final override;
94
116 template <typename DataType>
117 void load(const std::string& name, DataType& obj) {
118 // transform h5 directory-style name into branchname
119 // of ROOT-based framework
120 std::string branch_name{transform(name)};
121 if (attached_.find(branch_name) == attached_.end()) {
122 TTree* tree{event_tree_};
123 if (branch_name == "RunHeader") tree = run_tree_;
124 attached_[branch_name] = attach(tree,branch_name,obj);
125 }
126 if (attached_[branch_name]->GetEntry(attached_[branch_name]->GetReadEntry()+1) < 0) {
127 throw Exception("BadType",
128 "ROOT was unable to deserialize the branch "+branch_name
129 +" into the passed type "
130 +boost::core::demangle(typeid(DataType).name())
131 +".\n This error is usually caused by not providing the correct"
132 " ROOT dictionary for the type you are attempting to read.",false);
133 }
134 }
135
137 Reader(const Reader&) = delete;
139 void operator=(const Reader&) = delete;
140 private:
150 static std::string transform(const std::string& dir_name);
151
167 template<typename T>
168 TBranch* attach(TTree* tree, const std::string& branch_name, T& obj) {
169 TBranch* br = tree->GetBranch(branch_name.c_str());
170 if (br == 0) {
171 throw Exception("NotFound",
172 "Branch "+branch_name+" not found in input ROOT file.");
173 }
175 br->SetAddress(&obj);
176 } else {
177 br->SetObject(&obj);
178 }
179 br->SetStatus(1);
180 return br;
181 }
182 private:
184 TFile* file_;
188 TTree* run_tree_;
189 // branches that have been attached
191}; // Reader
192
193} // namespace fire::io::root
194
195#endif
T c_str(T... args)
Standard base exception class with some useful output information.
Definition: Exception.h:18
Empty data base allowing recursion.
Definition: AbstractData.h:30
Prototype for reading files within fire.
Definition: Reader.h:29
Reading ROOT files into our data structures.
Definition: Reader.h:29
virtual std::pair< std::string, int > type(const std::string &path) final override
Get the type of the input object.
Definition: Reader.cxx:58
TBranch * attach(TTree *tree, const std::string &branch_name, T &obj)
Attach the input object to the TTree on the given branch.
Definition: Reader.h:168
void load(const std::string &name, DataType &obj)
Load the data at the input name into the input object.
Definition: Reader.h:117
static std::string transform(const std::string &dir_name)
pull out pass and object name, construct branch name
Definition: Reader.cxx:96
virtual void load_into(BaseData &d) final override
Following the instructions in fire::io::Reader, we simply call the BaseData's load function.
Definition: Reader.cxx:40
virtual std::size_t runs() const final override
Number of runs in the file.
Definition: Reader.cxx:92
virtual std::size_t entries() const final override
Number of entries in the file.
Definition: Reader.cxx:88
void operator=(const Reader &)=delete
no copy assignment of readers
virtual std::vector< std::pair< std::string, std::string > > availableObjects() final override
Get the event objects available in the file.
Definition: Reader.cxx:44
Reader(const Reader &)=delete
no copy constructor of readers
TFile * file_
file being read
Definition: Reader.h:184
TTree * run_tree_
tree of runs in the file
Definition: Reader.h:188
virtual std::string name() const final override
Get the name of the file.
Definition: Reader.cxx:84
TTree * event_tree_
tree of events in the file
Definition: Reader.h:186
Reader(const std::string &file_name)
open input file for reading
Definition: Reader.cxx:13
Reading of ROOT TTree files generated by ROOT-based Framework.
Definition: Reader.h:20