]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EVGEN/AliGenReaderTreeK.cxx
Indexing bug fixed.
[u/mrichter/AliRoot.git] / EVGEN / AliGenReaderTreeK.cxx
index 880c74285415c8d2c41a51523b9000da0b8d2e5d..5f31a5b7312b6acc056c34fd1e3836dddf6b7b5a 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-*/
+/* $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"
+#include "AliRunLoader.h"
 
-ClassImp(AliGenReaderTreeK);
+ClassImp(AliGenReaderTreeK)
 
+const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
 
-AliGenReaderTreeK::AliGenReaderTreeK():AliGenReader() 
+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;
+}
+
+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 fInRunLoader;//it cleans all the loaded data
+    delete fDirs;
 }
 
 void AliGenReaderTreeK::Init() 
@@ -45,55 +79,139 @@ void AliGenReaderTreeK::Init()
 // Initialization
 // Connect base file and file to read from
 
-    TTree *ali = gAlice->TreeE();
+    TTree *ali = AliRunLoader::Instance()->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
-    TTree* treeE = (TTree*)gDirectory->Get("TE");
-    treeE->ls();
-    if (fHeader) delete fHeader;
-    fHeader = 0;
-    treeE->SetBranchAddress("Header", &fHeader);
 //  Get next event
-    treeE->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);
+}