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