1 #ifndef CROMBIE_LOADTREE_H 2 #define CROMBIE_LOADTREE_H 15 #include "TTreeFormula.h" 32 void add (
const std::string& expr) {
33 Debug::Debug(__PRETTY_FUNCTION__,
"Adding formula", expr);
35 TTreeFormula* form =
new TTreeFormula(expr.data(), expr.data(),
tree);
36 forms[expr] = std::make_pair(0.0, form);
40 for (
auto expr : exprs)
43 template<
typename A,
typename... Args>
void add(
const A& expr, Args... args) {
49 double&
result (
const std::string& expr) {
50 auto i_form =
forms.find(expr);
51 if (i_form !=
forms.end())
52 return i_form->second.first;
53 throw std::logic_error{expr +
": Asking for expression that wasn't loaded"};
58 for (
auto& form :
forms)
59 form.second.first = form.second.second->EvalInstance();
78 template<
typename... Args>
Tree(
const std::string& infile, Args... args);
85 double&
result (
const std::string& expr) {
return forms.result(expr); }
92 template<
typename C> C*
get(
const std::string& name);
105 void add_branches(std::set<std::string>& needed, TTree&
tree,
const std::string& expr) {
106 for (
auto branch : *(tree.GetListOfBranches())) {
107 auto name =
branch->GetName();
108 if (needed.find(name) == needed.end() && expr.find(name) != std::string::npos) {
110 std::regex regexpr {std::string(
"\\b") + name +
"\\b"};
112 if (std::regex_search(expr, matches, regexpr))
118 void add_branches(std::set<std::string>& needed, TTree& tree,
const Types::strings& exprs) {
119 for (
auto expr : exprs)
120 add_branches(needed, tree, expr);
123 template<
typename A,
typename... Args>
void add_branches(std::set<std::string>& needed, TTree& tree,
const A& expr, Args... args) {
124 add_branches(needed, tree, expr);
125 add_branches(needed, tree, args...);
129 template<
typename... Args> std::set<std::string> needed_branches(TTree& tree, Args... args) {
130 std::set<std::string> needed;
131 add_branches(needed, tree, args...);
137 template<
typename... Args>
Tree::Tree(
const std::string& infile, Args... args)
138 :
file{TFile::Open(infile.data())} {
140 std::runtime_error(std::string(
"Error opening file ") + infile);
146 tree->SetBranchStatus(
"*", 0);
147 auto needed = needed_branches(*
tree, args...);
148 for (
auto need : needed)
149 tree->SetBranchStatus(need.data(), 1);
164 template<
typename C> C*
Tree::get(
const std::string& name) {
165 auto* obj =
dynamic_cast<C*
>(
file->Get(name.data()));
167 throw std::runtime_error(std::string(
"Could not find object '") + name +
"' in " +
file->GetName());