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