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