/
TreeReader.hpp
176 lines (147 loc) · 5.18 KB
/
TreeReader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifndef ANALYSISTREE_TREEREADER_H
#define ANALYSISTREE_TREEREADER_H
#include <fstream>
#include <memory>
#include <set>
#include "TChain.h"
#include "TFile.h"
#include "TTree.h"
#include "Configuration.hpp"
#include "Detector.hpp"
#include "EventHeader.hpp"
#include "Matching.hpp"
namespace AnalysisTree {
static inline TChain* MakeChain(const std::string& filelist, const std::string& treename) {
auto* chain(new TChain(treename.c_str()));
std::ifstream in;
in.open(filelist);
std::string line;
std::cout << "Adding files to chain:" << std::endl;
while ((in >> line).good()) {
std::cout << line << std::endl;
if (!line.empty()) {
chain->AddFile(line.data());
}
}
return chain;
}
static inline TChain* MakeChain(const std::vector<std::string>& filelists, const std::vector<std::string>& treenames) {
assert(!filelists.empty() && !treenames.empty() && filelists.size() == treenames.size());
auto* chain = MakeChain(filelists.at(0), treenames.at(0));
for (uint i = 1; i < filelists.size(); i++) {
chain->AddFriend(MakeChain(filelists.at(i), treenames.at(i)));
}
if (!chain) {
throw std::runtime_error("AnalysisTree::MakeChain - chain is nullprt");
}
std::cout << "Ntrees = " << chain->GetNtrees() << "\n";
std::cout << "Nentries = " << chain->GetEntries() << "\n";
return chain;
}
static inline TTree* MakeTree(const std::string& filename, const std::string& treename) {
auto file = std::unique_ptr<TFile>(TFile::Open(filename.c_str(), "infra"));
if ((!file) || (file->IsZombie())) {
throw std::runtime_error("No file or file is zombie. Exit here: AnalysisTree::MakeTree");
}
auto* tree = (TTree*) file->Get(treename.c_str());
return tree;
}
/**
* @brief Loads object of type T from one (first) ROOT file specified in filelist
* @tparam T type of object
* @param filelist
* @param name name of the object
* @return pointer to the loaded object
*/
template<class T>
static inline T* GetObjectFromFileList(const std::string& filelist, const std::string& name) {
std::cout << "GetObjectFromFileList " << filelist << " " << name << std::endl;
T* object{nullptr};
std::ifstream in;
in.open(filelist);
std::string line;
in >> line;
if (!line.empty()) {
std::cout << line << " " << name << std::endl;
auto* in_file = TFile::Open(line.data(), "infra");
object = (T*) in_file->Get(name.c_str());
}
if (object == nullptr) {
throw std::runtime_error("AnalysisTree::GetObjectFromFileList - object is nullprt");
}
return object;
}
/**
* @brief Retrieves Configuration from each of provided filelist, merges different Configurations
* @param filelists - list of filelists
* @param name name of Configuration object
* @return pointer to Configuration object
*/
static inline Configuration* GetConfigurationFromFileList(const std::vector<std::string>& filelists, const std::string& name = "Configuration") {
assert(!filelists.empty());
Configuration* config{GetObjectFromFileList<Configuration>(filelists.at(0), name)};
for (uint i = 1; i < filelists.size(); ++i) {
auto* config_i = GetObjectFromFileList<Configuration>(filelists.at(i), name);
for (uint j = 0; j < config_i->GetNumberOfBranches(); ++j) {
config->AddBranchConfig(config_i->GetBranchConfig(j));
}
}
return config;
}
/**
* @brief Loads selected list of branches from TTree
* @param t
* @param config
* @param names List of selected branches. If empty, loads all branches found in Configuration
* @return
*/
static inline std::map<std::string, void*> GetPointersToBranches(TChain* t, const Configuration& config,
std::set<std::string> names = {}) {
std::cout << "GetPointersToBranches" << std::endl;
std::map<std::string, void*> ret;
if (names.empty()) {// all branches by default, if not implicitly specified
for (const auto& branch : config.GetBranchConfigs()) {
names.insert(branch.GetName());
}
}
for (const auto& branch : names) {// Init all pointers to branches
void* branch_ptr{nullptr};
const auto& branch_config = config.GetBranchConfig(branch);
std::cout << "Adding branch pointer: " << branch << std::endl;
switch (branch_config.GetType()) {
case DetType::kTrack: {
branch_ptr = new TrackDetector;
break;
}
case DetType::kHit: {
branch_ptr = new HitDetector;
break;
}
case DetType::kEventHeader: {
branch_ptr = new EventHeader;
break;
}
case DetType::kParticle: {
branch_ptr = new Particles;
break;
}
case DetType::kModule: {
branch_ptr = new ModuleDetector;
break;
}
}
ret.emplace(branch, branch_ptr);
}
for (const auto& match : config.GetMatches()) {// Init all pointers to matching //TODO exclude unused
auto* matching_ptr = new Matching;
std::cout << "Adding branch pointer: " << match.second << std::endl;
ret.emplace(match.second, matching_ptr);
}
for (auto& branch_map_entry : ret) {
t->SetBranchAddress(branch_map_entry.first.c_str(), &branch_map_entry.second);
}
t->GetEntry(0);//init pointers
return ret;
}
}// namespace AnalysisTree
#endif//ANALYSISTREE_TREEREADER_H