* provided "as is" without express or implied warranty. *
**************************************************************************/
-/*
-$Log$
-Revision 1.1 2001/11/09 09:11:24 morsch
-Realisation of AliGenReader that reads the kine tree (TreeK).
-
-*/
+/* $Id$ */
+//
+// Realisation of AliGenReader to be used with AliGenExtFile
+// It reads events from a kinematics TreeK.
+// NextEvent() is used to loop over events
+// and NextParticle() to loop over particles.
+// Author: andreas.morsch@cern.ch
+//
#include <TFile.h>
#include <TTree.h>
#include <TParticle.h>
+#include <TObjString.h>
+#include <TObjArray.h>
#include "AliGenReaderTreeK.h"
-#include "AliStack.h"
#include "AliHeader.h"
#include "AliRun.h"
-
-ClassImp(AliGenReaderTreeK);
-
-
-AliGenReaderTreeK::AliGenReaderTreeK():AliGenReader()
+#include "AliRunLoader.h"
+
+ClassImp(AliGenReaderTreeK)
+
+const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
+
+AliGenReaderTreeK::AliGenReaderTreeK():
+ AliGenReader(),
+ fNcurrent(0),
+ fNparticle(0),
+ fNp(0),
+ fInRunLoader(0),
+ fBaseFile(0),
+ fStack(0),
+ fOnlyPrimaries(kFALSE),
+ fDirs(0x0),
+ fCurrentDir(0)
{
// Default constructor
- fFileName = NULL;
- fStack = 0;
- fHeader = 0;
- fNcurrent = 0;
- fNparticle = 0;
- fFile = 0;
- fBaseFile = 0;
- fTreeE = 0;
}
+AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
+ AliGenReader(reader),
+ fNcurrent(0),
+ fNparticle(0),
+ fNp(0),
+ fInRunLoader(0),
+ fBaseFile(0),
+ fStack(0),
+ fOnlyPrimaries(kFALSE),
+ fDirs(0x0),
+ fCurrentDir(0)
+{
+ reader.Copy(*this);
+}
+
+
AliGenReaderTreeK::~AliGenReaderTreeK()
{
// Destructor
- delete fTreeE;
+ delete fInRunLoader;//it cleans all the loaded data
+ delete fDirs;
}
void AliGenReaderTreeK::Init()
TTree *ali = gAlice->TreeE();
if (ali) {
- fBaseFile = ali->GetCurrentFile();
+ fBaseFile = ali->GetCurrentFile();
} else {
- printf("\n Warning: Basefile cannot be found !\n");
+ printf("\n Warning: Basefile cannot be found !\n");
}
- fFile = new TFile(fFileName);
+ //if (!fFile) fFile = new TFile(fFileName);
+ if (fInRunLoader == 0x0)
+ {
+ fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
+ fInRunLoader->LoadHeader();
+ fInRunLoader->LoadKinematics("READ");
+ }
}
Int_t AliGenReaderTreeK::NextEvent()
{
-// Read the next event
+// Read the next event
// cd to file with old kine tree
if (!fBaseFile) Init();
- if (fStack) delete fStack;
- fStack = new AliStack(1000);
- fFile->cd();
-// Connect treeE
- if (fTreeE) {
- delete fTreeE;
- } else {
- fTreeE = (TTree*)gDirectory->Get("TE");
- }
-
- if (fHeader) delete fHeader;
- fHeader = 0;
- fTreeE->SetBranchAddress("Header", &fHeader);
// Get next event
- fTreeE->GetEntry(fNcurrent);
- fStack = fHeader->Stack();
- fStack->GetEvent(fNcurrent);
+
+ if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
+ {
+ if (fCurrentDir >= fDirs->GetEntries())
+ {
+ Warning("NextEvent","No more events");
+ return 0;
+ }
+ delete fInRunLoader;
+ fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
+ fInRunLoader->LoadHeader();
+ fInRunLoader->LoadKinematics("READ");
+ fNcurrent = 0;
+ }
+ fInRunLoader->GetEvent(fNcurrent);
+ fStack = fInRunLoader->Stack();
// cd back to base file
fBaseFile->cd();
//
fNcurrent++;
fNparticle = 0;
- Int_t ntrack = fStack->GetNtrack();
- printf("\n Next event contains %d particles", ntrack);
-
- return ntrack;
+ fNp = fStack->GetNtrack();
+ printf("\n Next event contains %d particles", fNp);
+//
+ return fNp;
}
TParticle* AliGenReaderTreeK::NextParticle()
{
-// Return next particle
- TParticle* part = fStack->Particle(fNparticle);
- fNparticle++;
- return part;
+//Return next particle
+ TParticle* part = GetParticle(fNparticle++);
+ if (part == 0x0) return 0x0;
+ //if only primaries are to be read, and this particle is not primary enter loop
+ if (fOnlyPrimaries && ( part->GetFirstMother() > -1) )
+ for (;;)
+ { //look for a primary
+ part = GetParticle(fNparticle++);
+ if (part == 0x0) return 0x0;
+ if (part->GetFirstMother() == -1) return part;
+ }
+
+ return part;
}
+void AliGenReaderTreeK::RewindEvent()
+{
+ // Go back to the first particle of the event
+ fNparticle = 0;
+}
AliGenReaderTreeK& AliGenReaderTreeK::operator=(const AliGenReaderTreeK& rhs)
{
// Assignment operator
+ rhs.Copy(*this);
return *this;
}
+void AliGenReaderTreeK::Copy(TObject&) const
+{
+ //
+ // Copy
+ //
+ Fatal("Copy","Not implemented!\n");
+}
+
+TString& AliGenReaderTreeK::GetDirName(Int_t entry)
+ {
+// Get the current directory name
+
+ TString* retval;//return value
+ if (fDirs == 0x0)
+ {
+ retval = new TString(".");
+ return *retval;
+ }
+
+ if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
+ { //note that entry==0 is accepted even if array is empty (size=0)
+ Error("GetDirName","Name out of bounds");
+ retval = new TString();
+ return *retval;
+ }
+
+ if (fDirs->GetEntries() == 0)
+ {
+ retval = new TString(".");
+ return *retval;
+ }
+
+ TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
+ if(dir == 0x0)
+ {
+ Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
+ retval = new TString();
+ return *retval;
+ }
+ if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
+ return dir->String();
+ }
+
+void AliGenReaderTreeK::AddDir(const char* dirname)
+{
+ //adds a directory to the list of directories where data are looked for
+ if(fDirs == 0x0)
+ {
+ fDirs = new TObjArray();
+ fDirs->SetOwner(kTRUE);
+ }
+ TObjString *odir= new TObjString(dirname);
+ fDirs->Add(odir);
+}