EventAnalysis  1.0.0
SparseTree.h
Go to the documentation of this file.
1 /*
2  * SparseTree.h
3  *
4  * Created on: 7 Aug 2019
5  * Author: Nicola Mori
6  */
7 
8 #ifndef SPARSETREE_H_
9 #define SPARSETREE_H_
10 
11 // Root headers
12 #include "TLeaf.h"
13 #include "TTree.h"
14 
15 // C/C++ headers
16 #include <functional>
17 
18 namespace EA {
19 
54 class SparseTree : public TTree {
55 public:
56  using TTree::TTree;
57 
68  template <typename T> Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr = 0);
69 
70  ClassDef(SparseTree, 1)
71 };
72 
73 template <typename T> Int_t SparseTree::SetBranchAddress(const char *bname, T **add, TBranch **ptr) {
74 
75  // Deactivate all branches on 1st call (i.e. when all the branches are active)
76  auto *branches = GetListOfBranches();
77  bool firstCall = true;
78  for (int iBranch = 0; iBranch < branches->GetEntries(); ++iBranch) {
79  auto *branch = (TBranch *)(branches->UncheckedAt(iBranch));
80  if (branch->TestBit(kDoNotProcess)) {
81  firstCall = false;
82  break;
83  }
84  }
85  if (firstCall) {
86  SetBranchStatus("*", 0);
87  }
88 
89  auto *branch = GetBranch(bname);
90  if (branch) {
91  // Recursive lambda that activates a branch and all its sub-branches and leaves
92  std::function<void(TBranch *)> ActivateBranch = [&ActivateBranch](TBranch *branch) {
93  auto *subBranches = branch->GetListOfBranches();
94  for (int iBranch = 0; iBranch < subBranches->GetEntries(); ++iBranch) {
95  ActivateBranch((TBranch *)(subBranches->UncheckedAt(iBranch)));
96  }
97  branch->SetStatus(1);
98 
99  auto *leaves = branch->GetListOfLeaves();
100  for (int iLeaf = 0; iLeaf < leaves->GetEntries(); ++iLeaf) {
101  auto *leaf = (TLeaf *)(leaves->UncheckedAt(iLeaf));
102  leaf->GetBranch()->SetStatus(1);
103  auto *leafCount = leaf->GetLeafCount();
104  if (leafCount) {
105  leafCount->GetBranch()->SetStatus(1);
106  }
107  }
108  };
109 
110  // Activate the branch and its sub-branches
111  ActivateBranch(branch);
112 
113  // Set the branch address
114  return TTree::SetBranchAddress<T>(bname, add, ptr);
115  }
116  return 0;
117 }
118 
119 } // namespace EA
120 
121 #endif /* SPARSETREE_H_ */
Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr=0)
Set branch address and enable the branch.
Definition: SparseTree.h:73
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
A tree that reads only branches which have an address.
Definition: SparseTree.h:54