1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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
26 #include <TParticle.h>
27 #include <TObjString.h>
28 #include <TObjArray.h>
30 #include "AliGenReaderTreeK.h"
31 #include "AliHeader.h"
33 #include "AliRunLoader.h"
35 ClassImp(AliGenReaderTreeK)
37 const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
39 AliGenReaderTreeK::AliGenReaderTreeK():
47 fOnlyPrimaries(kFALSE),
51 // Default constructor
54 AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
62 fOnlyPrimaries(kFALSE),
70 AliGenReaderTreeK::~AliGenReaderTreeK()
73 delete fInRunLoader;//it cleans all the loaded data
77 void AliGenReaderTreeK::Init()
80 // Connect base file and file to read from
82 TTree *ali = gAlice->TreeE();
84 fBaseFile = ali->GetCurrentFile();
86 printf("\n Warning: Basefile cannot be found !\n");
88 //if (!fFile) fFile = new TFile(fFileName);
89 if (fInRunLoader == 0x0)
91 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
92 fInRunLoader->LoadHeader();
93 fInRunLoader->LoadKinematics("READ");
97 Int_t AliGenReaderTreeK::NextEvent()
99 // Read the next event
100 // cd to file with old kine tree
101 if (!fBaseFile) Init();
104 if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
106 if (fCurrentDir >= fDirs->GetEntries())
108 Warning("NextEvent","No more events");
112 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
113 fInRunLoader->LoadHeader();
114 fInRunLoader->LoadKinematics("READ");
117 fInRunLoader->GetEvent(fNcurrent);
118 fStack = fInRunLoader->Stack();
120 // cd back to base file
125 fNp = fStack->GetNtrack();
126 printf("\n Next event contains %d particles", fNp);
131 TParticle* AliGenReaderTreeK::NextParticle()
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) )
139 { //look for a primary
140 part = GetParticle(fNparticle++);
141 if (part == 0x0) return 0x0;
142 if (part->GetFirstMother() == -1) return part;
148 void AliGenReaderTreeK::RewindEvent()
150 // Go back to the first particle of the event
155 AliGenReaderTreeK& AliGenReaderTreeK::operator=(const AliGenReaderTreeK& rhs)
157 // Assignment operator
162 void AliGenReaderTreeK::Copy(TObject&) const
167 Fatal("Copy","Not implemented!\n");
172 TString& AliGenReaderTreeK::GetDirName(Int_t entry)
174 // Get the current directory name
176 TString* retval;//return value
179 retval = new TString(".");
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();
190 if (fDirs->GetEntries() == 0)
192 retval = new TString(".");
196 TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
199 Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
200 retval = new TString();
203 if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
204 return dir->String();
207 void AliGenReaderTreeK::AddDir(const char* dirname)
209 //adds a directory to the list of directories where data are looked for
212 fDirs = new TObjArray();
213 fDirs->SetOwner(kTRUE);
215 TObjString *odir= new TObjString(dirname);