Fix Coverity 24835
[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"
92ff9348 33#include "AliStack.h"
88cb7938 34#include "AliRunLoader.h"
8020fb14 35
925e6570 36ClassImp(AliGenReaderTreeK)
8020fb14 37
88cb7938 38const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
39
40AliGenReaderTreeK::AliGenReaderTreeK():
198bb1c7 41 AliGenReader(),
42 fNcurrent(0),
43 fNparticle(0),
44 fNp(0),
45 fInRunLoader(0),
46 fBaseFile(0),
47 fStack(0),
48 fOnlyPrimaries(kFALSE),
49 fDirs(0x0),
50 fCurrentDir(0)
8020fb14 51{
52// Default constructor
ae872676 53}
54
88cb7938 55AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
198bb1c7 56 AliGenReader(reader),
57 fNcurrent(0),
58 fNparticle(0),
59 fNp(0),
60 fInRunLoader(0),
61 fBaseFile(0),
62 fStack(0),
63 fOnlyPrimaries(kFALSE),
64 fDirs(0x0),
65 fCurrentDir(0)
43342f51 66{
198bb1c7 67 reader.Copy(*this);
43342f51 68}
69
70
ae872676 71AliGenReaderTreeK::~AliGenReaderTreeK()
72{
73// Destructor
88cb7938 74 delete fInRunLoader;//it cleans all the loaded data
75 delete fDirs;
8020fb14 76}
77
78void AliGenReaderTreeK::Init()
79{
80// Initialization
81// Connect base file and file to read from
82
33c3c91a 83 TTree *ali = AliRunLoader::Instance()->TreeE();
8020fb14 84 if (ali) {
88cb7938 85 fBaseFile = ali->GetCurrentFile();
8020fb14 86 } else {
88cb7938 87 printf("\n Warning: Basefile cannot be found !\n");
8020fb14 88 }
88cb7938 89 //if (!fFile) fFile = new TFile(fFileName);
90 if (fInRunLoader == 0x0)
91 {
92 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
93 fInRunLoader->LoadHeader();
94 fInRunLoader->LoadKinematics("READ");
95 }
8020fb14 96}
97
98Int_t AliGenReaderTreeK::NextEvent()
99{
314a87b5 100// Read the next event
8020fb14 101// cd to file with old kine tree
102 if (!fBaseFile) Init();
8020fb14 103// Get next event
88cb7938 104
105 if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
106 {
107 if (fCurrentDir >= fDirs->GetEntries())
108 {
109 Warning("NextEvent","No more events");
110 return 0;
111 }
112 delete fInRunLoader;
113 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
114 fInRunLoader->LoadHeader();
115 fInRunLoader->LoadKinematics("READ");
116 fNcurrent = 0;
117 }
118 fInRunLoader->GetEvent(fNcurrent);
119 fStack = fInRunLoader->Stack();
120
8020fb14 121// cd back to base file
122 fBaseFile->cd();
123//
124 fNcurrent++;
125 fNparticle = 0;
88cb7938 126 fNp = fStack->GetNtrack();
127 printf("\n Next event contains %d particles", fNp);
314a87b5 128//
88cb7938 129 return fNp;
8020fb14 130}
131
132TParticle* AliGenReaderTreeK::NextParticle()
133{
88cb7938 134//Return next particle
135 TParticle* part = GetParticle(fNparticle++);
136 if (part == 0x0) return 0x0;
137 //if only primaries are to be read, and this particle is not primary enter loop
138 if (fOnlyPrimaries && ( part->GetFirstMother() > -1) )
139 for (;;)
140 { //look for a primary
141 part = GetParticle(fNparticle++);
142 if (part == 0x0) return 0x0;
143 if (part->GetFirstMother() == -1) return part;
144 }
145
146 return part;
8020fb14 147}
148
d1d1da57 149void AliGenReaderTreeK::RewindEvent()
150{
151 // Go back to the first particle of the event
152 fNparticle = 0;
153}
8020fb14 154
155
ae872676 156AliGenReaderTreeK& AliGenReaderTreeK::operator=(const AliGenReaderTreeK& rhs)
157{
158// Assignment operator
198bb1c7 159 rhs.Copy(*this);
ae872676 160 return *this;
161}
8020fb14 162
dc1d768c 163void AliGenReaderTreeK::Copy(TObject&) const
198bb1c7 164{
165 //
166 // Copy
167 //
168 Fatal("Copy","Not implemented!\n");
169}
170
8020fb14 171
172
88cb7938 173TString& AliGenReaderTreeK::GetDirName(Int_t entry)
174 {
345de409 175// Get the current directory name
176
88cb7938 177 TString* retval;//return value
178 if (fDirs == 0x0)
179 {
180 retval = new TString(".");
181 return *retval;
182 }
183
184 if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
185 { //note that entry==0 is accepted even if array is empty (size=0)
186 Error("GetDirName","Name out of bounds");
187 retval = new TString();
188 return *retval;
189 }
190
191 if (fDirs->GetEntries() == 0)
192 {
193 retval = new TString(".");
194 return *retval;
195 }
196
197 TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
198 if(dir == 0x0)
199 {
200 Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
201 retval = new TString();
202 return *retval;
203 }
204 if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
205 return dir->String();
206 }
207
208void AliGenReaderTreeK::AddDir(const char* dirname)
209{
210 //adds a directory to the list of directories where data are looked for
211 if(fDirs == 0x0)
212 {
213 fDirs = new TObjArray();
214 fDirs->SetOwner(kTRUE);
215 }
216 TObjString *odir= new TObjString(dirname);
217 fDirs->Add(odir);
218}
92ff9348 219
220TParticle* AliGenReaderTreeK::GetParticle(Int_t i)
221 {
222 if (fStack && i<fNp) return fStack->Particle(i);
223 return 0x0;
224 }