Fork me on GitHub

source: svn/trunk/Utilities/ExRootAnalysis/src/ExRootTreeReader.cc@ 325

Last change on this file since 325 was 247, checked in by severine ovyn, 16 years ago

cerr ok

File size: 3.7 KB
RevLine 
[3]1
2/** \class ExRootTreeReader
3 *
4 * Class simplifying access to ROOT tree branches
5 *
[247]6 * $Date: 2009-02-05 13:37:32 $
7 * $Revision: 1.3 $
[3]8 *
9 *
10 * \author S. Ovyn - UCL, Louvain-la-Neuve
11 *
12 */
13
[220]14#include "ExRootTreeReader.h"
[3]15#include "TBranchElement.h"
16#include "TClonesArray.h"
[220]17#include "TChain.h"
[3]18#include <iostream>
[247]19#include <iomanip>
[3]20
21using namespace std;
22
23//------------------------------------------------------------------------------
24
25ExRootTreeReader::ExRootTreeReader(TTree *tree) :
26 fChain(tree), fCurrentTree(-1), fIsInitDone(kFALSE)
27{
28
29}
30
31//------------------------------------------------------------------------------
32
33ExRootTreeReader::~ExRootTreeReader()
34{
35 TBranchMap::iterator it_map;
36
37 for(it_map = fBranchMap.begin(); it_map != fBranchMap.end(); ++it_map)
38 {
39 delete it_map->second.second;
40 }
41}
42
43//------------------------------------------------------------------------------
44
45Bool_t ExRootTreeReader::ReadEntry(Long64_t entry)
46{
47 // Read contents of entry.
48 if(!fChain) return kFALSE;
49
50 if(!fIsInitDone) Init();
51
52 Int_t treeEntry = fChain->LoadTree(entry);
53 if(treeEntry < 0) return kFALSE;
54
55 if(fChain->IsA() == TChain::Class())
56 {
57 TChain *chain = static_cast<TChain*>(fChain);
58 if(chain->GetTreeNumber() != fCurrentTree)
59 {
60 fCurrentTree = chain->GetTreeNumber();
61 Notify();
62 }
63 }
64
65 TBranchMap::iterator it_map;
66 TBranch *branch;
67
68 for(it_map = fBranchMap.begin(); it_map != fBranchMap.end(); ++it_map)
69 {
70 branch = it_map->second.first;
71 if(branch)
72 {
73 branch->GetEntry(treeEntry);
74 }
75 }
76
77 return kTRUE;
78}
79
80//------------------------------------------------------------------------------
81
82TClonesArray *ExRootTreeReader::UseBranch(const TString &branchName)
83{
84 fIsInitDone = kFALSE;
85
86 TClonesArray *array = 0;
87
88 TBranchMap::iterator it_map = fBranchMap.find(branchName);
89
90 if(it_map != fBranchMap.end())
91 {
92 array = it_map->second.second;
93 }
94 else
95 {
96 TBranch *branch = fChain->GetBranch(branchName);
97 if(branch)
98 {
99 if(branch->IsA() == TBranchElement::Class())
100 {
101 TBranchElement *element = static_cast<TBranchElement*>(branch);
102 const char *className = element->GetClonesName();
103 Int_t size = element->GetMaximum();
104 TClass *cl = gROOT->GetClass(className);
105 if(cl)
106 {
107 array = new TClonesArray(cl, size);
108 fBranchMap.insert(make_pair(branchName, make_pair(branch, array)));
109 }
110 }
111 }
112 }
113
114 if(!array)
115 {
[247]116 cerr << left << setw(35) <<"** ERROR: cannot access branch"<<""
117 << left << setw(15) << branchName <<""
118 << right << setw(19) <<" **"<<""<<endl;
[3]119 }
120
121 return array;
122}
123
124//------------------------------------------------------------------------------
125
126void ExRootTreeReader::Init()
127{
128 TBranchMap::iterator it_map;
129
130 for(it_map = fBranchMap.begin(); it_map != fBranchMap.end(); ++it_map)
131 {
132 fChain->SetBranchAddress(it_map->first, &(it_map->second.second));
133 }
134
135 Notify();
136
137 fIsInitDone = kTRUE;
138}
139
140//------------------------------------------------------------------------------
141
142void ExRootTreeReader::Notify()
143{
144 // Called when loading a new file.
145 // Get branch pointers.
146 if(!fChain) return;
147
148 TBranchMap::iterator it_map;
149
150 for(it_map = fBranchMap.begin(); it_map != fBranchMap.end(); ++it_map)
151 {
152 it_map->second.first = fChain->GetBranch(it_map->first);
153 if(!it_map->second.first)
154 {
[247]155 cerr << left << setw(35) <<"** ERROR: cannot get branch"<<""
156 << left << setw(15) << it_map->first <<""
157 << right << setw(19) <<" **"<<""<<endl;
[3]158 }
159 }
160}
161
162//------------------------------------------------------------------------------
163
164
Note: See TracBrowser for help on using the repository browser.