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"
34 #include "AliRunLoader.h"
36 ClassImp(AliGenReaderTreeK)
38 const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
40 AliGenReaderTreeK::AliGenReaderTreeK():
48 fOnlyPrimaries(kFALSE),
52 // Default constructor
55 AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
63 fOnlyPrimaries(kFALSE),
71 AliGenReaderTreeK::~AliGenReaderTreeK()
74 delete fInRunLoader;//it cleans all the loaded data
78 void AliGenReaderTreeK::Init()
81 // Connect base file and file to read from
83 TTree *ali = AliRunLoader::Instance()->TreeE();
85 fBaseFile = ali->GetCurrentFile();
87 printf("\n Warning: Basefile cannot be found !\n");
89 //if (!fFile) fFile = new TFile(fFileName);
90 if (fInRunLoader == 0x0)
92 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
93 fInRunLoader->LoadHeader();
94 fInRunLoader->LoadKinematics("READ");
98 Int_t AliGenReaderTreeK::NextEvent()
100 // Read the next event
101 // cd to file with old kine tree
102 if (!fBaseFile) Init();
105 if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
107 if (fCurrentDir >= fDirs->GetEntries())
109 Warning("NextEvent","No more events");
113 fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
114 fInRunLoader->LoadHeader();
115 fInRunLoader->LoadKinematics("READ");
118 fInRunLoader->GetEvent(fNcurrent);
119 fStack = fInRunLoader->Stack();
121 // cd back to base file
126 fNp = fStack->GetNtrack();
127 printf("\n Next event contains %d particles", fNp);
132 TParticle* AliGenReaderTreeK::NextParticle()
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) )
140 { //look for a primary
141 part = GetParticle(fNparticle++);
142 if (part == 0x0) return 0x0;
143 if (part->GetFirstMother() == -1) return part;
149 void AliGenReaderTreeK::RewindEvent()
151 // Go back to the first particle of the event
156 AliGenReaderTreeK& AliGenReaderTreeK::operator=(const AliGenReaderTreeK& rhs)
158 // Assignment operator
163 void AliGenReaderTreeK::Copy(TObject&) const
168 Fatal("Copy","Not implemented!\n");
173 TString& AliGenReaderTreeK::GetDirName(Int_t entry)
175 // Get the current directory name
177 TString* retval;//return value
180 retval = new TString(".");
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();
191 if (fDirs->GetEntries() == 0)
193 retval = new TString(".");
197 TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
200 Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
201 retval = new TString();
204 if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
205 return dir->String();
208 void AliGenReaderTreeK::AddDir(const char* dirname)
210 //adds a directory to the list of directories where data are looked for
213 fDirs = new TObjArray();
214 fDirs->SetOwner(kTRUE);
216 TObjString *odir= new TObjString(dirname);
220 TParticle* AliGenReaderTreeK::GetParticle(Int_t i)
222 if (fStack && i<fNp) return fStack->Particle(i);