]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG0/AliSelector.cxx
correct deletion of tree
[u/mrichter/AliRoot.git] / PWG0 / AliSelector.cxx
CommitLineData
dc740de4 1/* $Id$ */
2
539b6cb4 3// The class definition in esdV0.h has been generated automatically
4// by the ROOT utility TTree::MakeSelector(). This class is derived
5// from the ROOT class TSelector. For more information on the TSelector
6// framework see $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
7
8// The following methods are defined in this file:
9// Begin(): called everytime a loop on the tree starts,
10// a convenient place to create your histograms.
11// SlaveBegin(): called after Begin(), when on PROOF called only on the
12// slave servers.
13// Process(): called for each event, in this function you decide what
14// to read and fill your histograms.
15// SlaveTerminate: called at the end of the loop on the tree, when on PROOF
16// called only on the slave servers.
17// Terminate(): called at the end of the loop on the tree,
18// a convenient place to draw/fit your histograms.
19//
20// To use this file, try the following session on your Tree T:
21//
22// Root > T->Process("AliSelector.C")
23// Root > T->Process("AliSelector.C","some options")
24// Root > T->Process("AliSelector.C+")
25//
26
27#include "AliSelector.h"
b8e8168f 28
539b6cb4 29#include <TStyle.h>
30#include <TSystem.h>
31#include <TCanvas.h>
32#include <TRegexp.h>
b8e8168f 33#include <TTime.h>
dc740de4 34#include <TFriendElement.h>
f8e00da3 35#include <TTree.h>
36#include <TChain.h>
37#include <TFile.h>
539b6cb4 38
b8e8168f 39#include <AliLog.h>
f8e00da3 40#include <AliESD.h>
539b6cb4 41
42ClassImp(AliSelector)
43
ee998961 44AliSelector::AliSelector() :
539b6cb4 45 TSelector(),
16e24ca3 46 fTree(0),
539b6cb4 47 fESD(0),
dc740de4 48 fCountFiles(0),
16e24ca3 49 fKineFile(0)
539b6cb4 50{
79ab56b9 51 //
539b6cb4 52 // Constructor. Initialization of pointers
79ab56b9 53 //
539b6cb4 54}
55
56AliSelector::~AliSelector()
57{
79ab56b9 58 //
59 // Destructor
60 //
539b6cb4 61
05317a93 62 if (fTree)
63 fTree->ResetBranchAddresses();
64
65 if (fESD)
66 {
67 delete fESD;
68 fESD = 0;
69 }
539b6cb4 70}
71
16e24ca3 72void AliSelector::Begin(TTree*)
539b6cb4 73{
74 // The Begin() function is called at the start of the query.
75 // When running with PROOF Begin() is only called on the client.
76 // The tree argument is deprecated (on PROOF 0 is passed).
25db2d85 77
0ab29cfa 78 TString option = GetOption();
79
80 if (option.Contains("debug"))
81 {
82 AliLog::SetClassDebugLevel("AliSelector", AliLog::kDebug);
83 AliInfo(Form("Called with option %s.", option.Data()));
84 }
85
25db2d85 86 AliDebug(AliLog::kDebug, "============BEGIN===========");
539b6cb4 87}
88
16e24ca3 89void AliSelector::SlaveBegin(TTree* tree)
539b6cb4 90{
91 // The SlaveBegin() function is called after the Begin() function.
92 // When running with PROOF SlaveBegin() is called on each slave server.
93 // The tree argument is deprecated (on PROOF 0 is passed).
94
ea1224c5 95 TString option = GetOption();
96
97 if (option.Contains("debug"))
98 {
99 AliLog::SetClassDebugLevel("AliSelector", AliLog::kDebug);
100 AliInfo(Form("Called with option %s.", option.Data()));
101 }
102
b8e8168f 103 AliDebug(AliLog::kDebug, "=======SLAVEBEGIN========");
104 AliDebug(AliLog::kDebug, Form("Hostname: %s", gSystem->HostName()));
105 AliDebug(AliLog::kDebug, Form("Time: %s", gSystem->Now().AsString()));
539b6cb4 106
16e24ca3 107 if (tree != 0)
108 Init(tree);
539b6cb4 109}
110
111void AliSelector::Init(TTree *tree)
112{
113 // The Init() function is called when the selector needs to initialize
114 // a new tree or chain. Typically here the branch addresses of the tree
115 // will be set. It is normaly not necessary to make changes to the
116 // generated code, but the routine can be extended by the user if needed.
117 // Init() will be called many times when running with PROOF.
118
b8e8168f 119 AliDebug(AliLog::kDebug, "=========Init==========");
539b6cb4 120
16e24ca3 121 fTree = tree;
539b6cb4 122
16e24ca3 123 if (fTree == 0)
539b6cb4 124 {
16e24ca3 125 AliDebug(AliLog::kError, "ERROR: tree argument is 0.");
539b6cb4 126 return;
127 }
128
16e24ca3 129 // Set branch address
130 fTree->SetBranchAddress("ESD", &fESD);
539b6cb4 131 if (fESD != 0)
b8e8168f 132 AliDebug(AliLog::kInfo, "INFO: Found ESD branch in chain.");
539b6cb4 133}
134
135Bool_t AliSelector::Notify()
136{
137 // The Notify() function is called when a new file is opened. This
138 // can be either for a new TTree in a TChain or when when a new TTree
139 // is started when using PROOF. Typically here the branch pointers
140 // will be retrieved. It is normaly not necessary to make changes
141 // to the generated code, but the routine can be extended by the
142 // user if needed.
143
b8e8168f 144 AliDebug(AliLog::kDebug, "=========NOTIFY==========");
145 AliDebug(AliLog::kDebug, Form("Hostname: %s", gSystem->HostName()));
146 AliDebug(AliLog::kDebug, Form("Time: %s", gSystem->Now().AsString()));
ee998961 147
dc740de4 148 ++fCountFiles;
16e24ca3 149 if (fTree)
150 {
151 TFile *f = fTree->GetCurrentFile();
152 AliDebug(AliLog::kInfo, Form("Processing %d. file %s", fCountFiles, f->GetName()));
153 }
154 else
155 {
156 AliDebug(AliLog::kError, "fTree not available");
157 }
539b6cb4 158
159 DeleteKinematicsFile();
dc740de4 160
539b6cb4 161 return kTRUE;
162}
163
164Bool_t AliSelector::Process(Long64_t entry)
165{
166 // The Process() function is called for each entry in the tree (or possibly
167 // keyed object in the case of PROOF) to be processed. The entry argument
168 // specifies which entry in the currently loaded tree is to be processed.
169 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
170 // to read either all or the required parts of the data. When processing
171 // keyed objects with PROOF, the object is already loaded and is available
172 // via the fObject pointer.
173 //
174 // This function should contain the "body" of the analysis. It can contain
175 // simple or elaborate selection criteria, run algorithms on the data
176 // of the event and typically fill histograms.
177
178 // WARNING when a selector is used with a TChain, you must use
179 // the pointer to the current TTree to call GetEntry(entry).
180 // The entry is always the local entry number in the current tree.
16e24ca3 181 // Assuming that fTree is the pointer to the TChain being processed,
182 // use fTree->GetTree()->GetEntry(entry).
539b6cb4 183
b8e8168f 184 AliDebug(AliLog::kDebug, Form("=========PROCESS========== Entry %lld", entry));
539b6cb4 185
16e24ca3 186 if (!fTree)
539b6cb4 187 {
16e24ca3 188 AliDebug(AliLog::kError, "ERROR: fTree is 0.");
539b6cb4 189 return kFALSE;
190 }
191
16e24ca3 192 fTree->GetTree()->GetEntry(entry);
539b6cb4 193
194 if (fESD)
b8e8168f 195 AliDebug(AliLog::kDebug, Form("ESD: We have %d tracks.", fESD->GetNumberOfTracks()));
539b6cb4 196
539b6cb4 197 return kTRUE;
198}
199
200void AliSelector::SlaveTerminate()
201{
202 // The SlaveTerminate() function is called after all entries or objects
203 // have been processed. When running with PROOF SlaveTerminate() is called
204 // on each slave server.
205
25db2d85 206 AliDebug(AliLog::kDebug, "=======SLAVETERMINATE=======");
207
539b6cb4 208 DeleteKinematicsFile();
539b6cb4 209}
210
211void AliSelector::Terminate()
212{
213 // The Terminate() function is the last function to be called during
214 // a query. It always runs on the client, it can be used to present
215 // the results graphically or save the results to file.
216
b8e8168f 217 AliDebug(AliLog::kDebug, "=========TERMINATE==========");
539b6cb4 218}
219
220TTree* AliSelector::GetKinematics()
221{
16e24ca3 222 // Returns kinematics tree corresponding to current ESD active in fTree
79ab56b9 223 // Loads the kinematics from the kinematics file, the file is identified by replacing "AliESDs" to
224 // "Kinematics" in the file path of the ESD file. This is a hack, to be changed!
225
539b6cb4 226 if (!fKineFile)
227 {
16e24ca3 228 if (!fTree->GetCurrentFile())
539b6cb4 229 return 0;
230
16e24ca3 231 TString fileName(fTree->GetCurrentFile()->GetName());
539b6cb4 232 fileName.ReplaceAll("AliESDs", "Kinematics");
233
dc740de4 234 AliDebug(AliLog::kInfo, Form("Opening %s", fileName.Data()));
235
539b6cb4 236 fKineFile = TFile::Open(fileName);
237 if (!fKineFile)
238 return 0;
239 }
240
16e24ca3 241 return dynamic_cast<TTree*> (fKineFile->Get(Form("Event%d/TreeK", fTree->GetTree()->GetReadEntry())));
539b6cb4 242}
243
244void AliSelector::DeleteKinematicsFile()
245{
79ab56b9 246 //
247 // Closes the kinematics file and deletes the pointer.
248 //
249
539b6cb4 250 if (fKineFile)
251 {
252 fKineFile->Close();
253 delete fKineFile;
254 fKineFile = 0;
255 }
256}