Indexing bug fixed.
[u/mrichter/AliRoot.git] / EVGEN / AliGenReaderTreeK.cxx
CommitLineData
8020fb14 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
88cb7938 16/* $Id$ */
345de409 17//
18// Realisation of AliGenReader to be used with AliGenExtFile
19// It reads events from a kinematics TreeK.
20// NextEvent() is used to loop over events
21// and NextParticle() to loop over particles.
22// Author: andreas.morsch@cern.ch
23//
8020fb14 24#include <TFile.h>
25#include <TTree.h>
26#include <TParticle.h>
88cb7938 27#include <TObjString.h>
28#include <TObjArray.h>
8020fb14 29
30#include "AliGenReaderTreeK.h"
8020fb14 31#include "AliHeader.h"
32#include "AliRun.h"
88cb7938 33#include "AliRunLoader.h"
8020fb14 34
925e6570 35ClassImp(AliGenReaderTreeK)
8020fb14 36
88cb7938 37const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
38
39AliGenReaderTreeK::AliGenReaderTreeK():
198bb1c7 40 AliGenReader(),
41 fNcurrent(0),
42 fNparticle(0),
43 fNp(0),
44 fInRunLoader(0),
45 fBaseFile(0),
46 fStack(0),
47 fOnlyPrimaries(kFALSE),
48 fDirs(0x0),
49 fCurrentDir(0)
8020fb14 50{
51// Default constructor
ae872676 52}
53
88cb7938 54AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
198bb1c7 55 AliGenReader(reader),
56 fNcurrent(0),
57 fNparticle(0),
58 fNp(0),
59 fInRunLoader(0),
60 fBaseFile(0),
61 fStack(0),
62 fOnlyPrimaries(kFALSE),
63 fDirs(0x0),
64 fCurrentDir(0)
43342f51 65{
198bb1c7 66 reader.Copy(*this);
43342f51 67}
68
69
ae872676 70AliGenReaderTreeK::~AliGenReaderTreeK()
71{
72// Destructor
88cb7938 73 delete fInRunLoader;//it cleans all the loaded data
74 delete fDirs;
8020fb14 75}
76
77void AliGenReaderTreeK::Init()
78{
79// Initialization
80// Connect base file and file to read from
81
33c3c91a 82 TTree *ali = AliRunLoader::Instance()->TreeE();
8020fb14 83 if (ali) {
88cb7938 84 fBaseFile = ali->GetCurrentFile();
8020fb14 85 } else {
88cb7938 86 printf("\n Warning: Basefile cannot be found !\n");
8020fb14 87 }
88cb7938 88 //if (!fFile) fFile = new TFile(fFileName);
89 if (fInRunLoader == 0x0)
90 {
91 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
92 fInRunLoader->LoadHeader();
93 fInRunLoader->LoadKinematics("READ");
94 }
8020fb14 95}
96
97Int_t AliGenReaderTreeK::NextEvent()
98{
314a87b5 99// Read the next event
8020fb14 100// cd to file with old kine tree
101 if (!fBaseFile) Init();
8020fb14 102// Get next event
88cb7938 103
104 if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
105 {
106 if (fCurrentDir >= fDirs->GetEntries())
107 {
108 Warning("NextEvent","No more events");
109 return 0;
110 }
111 delete fInRunLoader;
112 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
113 fInRunLoader->LoadHeader();
114 fInRunLoader->LoadKinematics("READ");
115 fNcurrent = 0;
116 }
117 fInRunLoader->GetEvent(fNcurrent);
118 fStack = fInRunLoader->Stack();
119
8020fb14 120// cd back to base file
121 fBaseFile->cd();
122//
123 fNcurrent++;
124 fNparticle = 0;
88cb7938 125 fNp = fStack->GetNtrack();
126 printf("\n Next event contains %d particles", fNp);
314a87b5 127//
88cb7938 128 return fNp;
8020fb14 129}
130
131TParticle* AliGenReaderTreeK::NextParticle()
132{
88cb7938 133//Return next particle
134 TParticle* part = GetParticle(fNparticle++);
135 if (part == 0x0) return 0x0;
136 //if only primaries are to be read, and this particle is not primary enter loop
137 if (fOnlyPrimaries && ( part->GetFirstMother() > -1) )
138 for (;;)
139 { //look for a primary
140 part = GetParticle(fNparticle++);
141 if (part == 0x0) return 0x0;
142 if (part->GetFirstMother() == -1) return part;
143 }
144
145 return part;
8020fb14 146}
147
d1d1da57 148void AliGenReaderTreeK::RewindEvent()
149{
150 // Go back to the first particle of the event
151 fNparticle = 0;
152}
8020fb14 153
154
ae872676 155AliGenReaderTreeK& AliGenReaderTreeK::operator=(const AliGenReaderTreeK& rhs)
156{
157// Assignment operator
198bb1c7 158 rhs.Copy(*this);
ae872676 159 return *this;
160}
8020fb14 161
dc1d768c 162void AliGenReaderTreeK::Copy(TObject&) const
198bb1c7 163{
164 //
165 // Copy
166 //
167 Fatal("Copy","Not implemented!\n");
168}
169
8020fb14 170
171
88cb7938 172TString& AliGenReaderTreeK::GetDirName(Int_t entry)
173 {
345de409 174// Get the current directory name
175
88cb7938 176 TString* retval;//return value
177 if (fDirs == 0x0)
178 {
179 retval = new TString(".");
180 return *retval;
181 }
182
183 if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
184 { //note that entry==0 is accepted even if array is empty (size=0)
185 Error("GetDirName","Name out of bounds");
186 retval = new TString();
187 return *retval;
188 }
189
190 if (fDirs->GetEntries() == 0)
191 {
192 retval = new TString(".");
193 return *retval;
194 }
195
196 TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
197 if(dir == 0x0)
198 {
199 Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
200 retval = new TString();
201 return *retval;
202 }
203 if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
204 return dir->String();
205 }
206
207void AliGenReaderTreeK::AddDir(const char* dirname)
208{
209 //adds a directory to the list of directories where data are looked for
210 if(fDirs == 0x0)
211 {
212 fDirs = new TObjArray();
213 fDirs->SetOwner(kTRUE);
214 }
215 TObjString *odir= new TObjString(dirname);
216 fDirs->Add(odir);
217}