]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliStack.cxx
Replacing Header with Id
[u/mrichter/AliRoot.git] / STEER / AliStack.cxx
index fbe212a04d9e682490183d6573bf760be2bc4916..ddf2a04ef84bad49617138d22939679e2c943d65 100644 (file)
@@ -13,9 +13,7 @@
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-*/
+/* $Id$ */
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
@@ -23,7 +21,7 @@ $Log$
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
-#include <iostream.h>
+#include <Riostream.h>
  
 #include <TObjArray.h>
 #include <TParticle.h>
@@ -36,49 +34,75 @@ $Log$
 #include "AliRun.h"
 #include "AliModule.h"
 #include "AliHit.h"
-//#include "ETrackBits.h"
 
 ClassImp(AliStack)
 
-//_____________________________________________________________________________
-AliStack::AliStack(Int_t size)
+//_______________________________________________________________________
+AliStack::AliStack():
+  fParticles(0),
+  fParticleMap(0),
+  fParticleFileMap(0),
+  fParticleBuffer(0),
+  fTreeK(0),
+  fNtrack(0),
+  fNprimary(0),
+  fCurrent(-1),
+  fCurrentPrimary(-1),
+  fHgwmk(0),
+  fLoadPoint(0)
 {
   //
-  //  Constructor
+  // Default constructor
   //
-  
-  // Create the particles arrays 
-  fParticles      = new TClonesArray("TParticle",1000);
-  fParticleMap    = new TObjArray(size);
-  fParticleBuffer = new TParticle();
-  fNtrack         =  0;
-  fNprimary      =  0;
-  fCurrent        = -1;
-  fCurrentPrimary = -1;
-  fTreeK          =  0;
 }
 
+//_______________________________________________________________________
+AliStack::AliStack(Int_t size):
+  fParticles(new TClonesArray("TParticle",1000)),
+  fParticleMap(new TObjArray(size)),
+  fParticleFileMap(0),
+  fParticleBuffer(0),
+  fTreeK(0),
+  fNtrack(0),
+  fNprimary(0),
+  fCurrent(-1),
+  fCurrentPrimary(-1),
+  fHgwmk(0),
+  fLoadPoint(0)
+{
+  //
+  //  Constructor
+  //
+}
 
-//_____________________________________________________________________________
-AliStack::AliStack()
+//_______________________________________________________________________
+AliStack::AliStack(const AliStack& st):
+  TVirtualMCStack(st),
+  fParticles(0),
+  fParticleMap(0),
+  fParticleFileMap(0),
+  fParticleBuffer(0),
+  fTreeK(0),
+  fNtrack(0),
+  fNprimary(0),
+  fCurrent(-1),
+  fCurrentPrimary(-1),
+  fHgwmk(0),
+  fLoadPoint(0)
 {
   //
-  //  Default constructor
+  // Copy constructor
   //
-  
-  // Create the particles arrays 
-  fParticles      = new TClonesArray("TParticle",1000);
-  fParticleMap    = new TObjArray(10000);
-  fParticleBuffer = new TParticle();
-  fNtrack         =  0;
-  fCurrent        = -1;
-  fNprimary      =  0;
-  fCurrentPrimary = -1;
-  fTreeK          =  0;
+  st.Copy(*this);
 }
 
+//_______________________________________________________________________
+void AliStack::Copy(AliStack&) const
+{
+  Fatal("Copy","Not implemented!\n");
+}
 
-//_____________________________________________________________________________
+//_______________________________________________________________________
 AliStack::~AliStack()
 {
   //
@@ -90,7 +114,6 @@ AliStack::~AliStack()
     delete fParticles;
   }
   delete fParticleMap;
-  delete fParticleBuffer;
   delete fTreeK;
 }
 
@@ -101,7 +124,7 @@ AliStack::~AliStack()
 //_____________________________________________________________________________
 void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
                      Float_t *vpos, Float_t *polar, Float_t tof,
-                     AliMCProcess mech, Int_t &ntr, Float_t weight)
+                     TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
 { 
   //
   // Load a track on the stack
@@ -118,10 +141,6 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
   // ntr      on output the number of the track stored
   //
 
-  Float_t mass;
-  const Int_t kfirstdaughter=-1;
-  const Int_t klastdaughter=-1;
-  const Int_t kS=0;
   //  const Float_t tlife=0;
   
   //
@@ -131,44 +150,17 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
   // also, this method is potentially dangerous if the mass
   // used in the MC is not the same of the PDG database
   //
-  mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
+  Float_t mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
   Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
                        pmom[1]*pmom[1]+pmom[2]*pmom[2]);
   
-//  printf("Loading  mass %f ene %f No %d ip %d parent %d done %d pos %f %f %f mom %f %f %f kS %d m \n",
-//  mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
-
-  TClonesArray &particles = *fParticles;
-  TParticle* particle
-   = new(particles[fLoadPoint++]) 
-     TParticle(pdg, kS, parent, -1, kfirstdaughter, klastdaughter,
-               pmom[0], pmom[1], pmom[2], e, vpos[0], vpos[1], vpos[2], tof);
-  particle->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
-  particle->SetWeight(weight);
-  particle->SetUniqueID(mech);
-  if(!done) particle->SetBit(kDoneBit);
-  
+//    printf("Loading  mass %f ene %f No %d ip %d parent %d done %d pos %f %f %f mom %f %f %f kS %d m \n",
+//        mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
   
-  //  Declare that the daughter information is valid
-  particle->SetBit(kDaughtersBit);
-  //  Add the particle to the stack
-  fParticleMap->AddAtAndExpand(particle, fNtrack);
 
-  if(parent>=0) {
-    particle = (TParticle*) fParticleMap->At(parent);
-    particle->SetLastDaughter(fNtrack);
-    if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
-  } 
-  else { 
-    //
-    // This is a primary track. Set high water mark for this event
-    fHgwmk = fNtrack;
-    //
-    // Set also number if primary tracks
-    fNprimary = fHgwmk+1;
-    fCurrentPrimary++;
-  }
-  ntr = fNtrack++;
+  SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
+           vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
+           mech, ntr, weight, is);
 }
 
 //_____________________________________________________________________________
@@ -176,7 +168,7 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
                      Double_t px, Double_t py, Double_t pz, Double_t e,
                      Double_t vx, Double_t vy, Double_t vz, Double_t tof,
                      Double_t polx, Double_t poly, Double_t polz,
-                     AliMCProcess mech, Int_t &ntr, Float_t weight)
+                     TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
 { 
   //
   // Load a track on the stack
@@ -200,14 +192,13 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
   // it is passed by argument e.
 
 
-  const Int_t kS=0;
   const Int_t kFirstDaughter=-1;
   const Int_t kLastDaughter=-1;
   
   TClonesArray &particles = *fParticles;
   TParticle* particle
     = new(particles[fLoadPoint++]) 
-      TParticle(pdg, kS, parent, -1, kFirstDaughter, kLastDaughter,
+      TParticle(pdg, is, parent, -1, kFirstDaughter, kLastDaughter,
                px, py, pz, e, vx, vy, vz, tof);
    
   particle->SetPolarisation(polx, poly, polz);
@@ -222,9 +213,14 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
   fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
 
   if(parent>=0) {
-    particle = (TParticle*) fParticleMap->At(parent);
-    particle->SetLastDaughter(fNtrack);
-    if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
+    particle = dynamic_cast<TParticle*>(fParticleMap->At(parent));
+    if (particle) {
+      particle->SetLastDaughter(fNtrack);
+      if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
+    }
+    else {
+      printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
+    }
   } 
   else { 
     //
@@ -239,9 +235,31 @@ void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
 }
 
 //_____________________________________________________________________________
-void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
-                         Float_t &e, Float_t *vpos, Float_t *polar,
-                         Float_t &tof)
+TParticle*  AliStack::GetNextTrack(Int_t& itrack)
+{
+  //
+  // Returns next track from stack of particles
+  //
+  
+
+  TParticle* track = GetNextParticle();
+
+  if (track) {
+    itrack = fCurrent;
+    track->SetBit(kDoneBit);
+  }
+  else 
+    itrack = -1;
+
+  return track;
+}
+
+/*
+//_____________________________________________________________________________
+void  AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,     
+                            Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
+                            Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
+                            Double_t& polx, Double_t& poly, Double_t& polz) 
 {
   //
   // Return next track from stack of particles
@@ -249,29 +267,29 @@ void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
   
 
   TParticle* track = GetNextParticle();
-//  cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
-
-  if(track) {
-    mtrack=fCurrent;
-    ipart=track->GetPdgCode();
-    pmom[0]=track->Px();
-    pmom[1]=track->Py(); 
-    pmom[2]=track->Pz();
-    e      =track->Energy();
-    vpos[0]=track->Vx();
-    vpos[1]=track->Vy();
-    vpos[2]=track->Vz();
+//    cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
+
+  if (track) {
+    itrack = fCurrent;
+    pdg = track->GetPdgCode();
+    px = track->Px();
+    py = track->Py(); 
+    pz = track->Pz();
+    e  = track->Energy();
+    vx = track->Vx();
+    vy = track->Vy();
+    vz = track->Vz();
+    tof = track->T();
     TVector3 pol;
     track->GetPolarisation(pol);
-    polar[0]=pol.X();
-    polar[1]=pol.Y();
-    polar[2]=pol.Z();
-    tof=track->T();
+    polx = pol.X();
+    poly = pol.Y();
+    polz = pol.Z();
     track->SetBit(kDoneBit);
-    //cout << "Filled params" << endl;
+//      cout << "Filled params" << endl;
   }
   else 
-    mtrack=-1;
+    itrack = -1;
 
   //
   // stop and start timer when we start a primary track
@@ -285,6 +303,23 @@ void AliStack::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
   fTimer.Start();
 }
 
+*/
+//_____________________________________________________________________________
+TParticle*  AliStack::GetPrimaryForTracking(Int_t i)
+{
+  //
+  // Returns i-th primary particle if it is flagged to be tracked,
+  // 0 otherwise
+  //
+  
+  TParticle* particle = Particle(i);
+  
+  if (!particle->TestBit(kDoneBit))
+    return particle;
+  else
+    return 0;
+}      
+
 
 //_____________________________________________________________________________
 void AliStack::PurifyKine()
@@ -304,22 +339,27 @@ void AliStack::PurifyKine()
   // If no tracks generated return now
   if(fHgwmk+1 == fNtrack) return;
 
-  Int_t toshrink = fNtrack-fHgwmk-1;
-
   // First pass, invalid Daughter information
   for(i=0; i<fNtrack; i++) {
     // Preset map, to be removed later
     if(i<=fHgwmk) map[i]=i ; 
     else {
       map[i] = -99;
-      //      particles.UncheckedAt(i)->ResetBit(kDaughtersBit);
-      if((part=(TParticle*) particles.At(i))) part->ResetBit(kDaughtersBit);
+      if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
+//
+//        Check of this track should be kept for physics reasons 
+         if (KeepPhysics(part)) KeepTrack(i);
+//
+          part->ResetBit(kDaughtersBit);
+          part->SetFirstDaughter(-1);
+          part->SetLastDaughter(-1);
+      }
     }
   }
   // Invalid daughter information for the parent of the first particle
   // generated. This may or may not be the current primary according to
   // whether decays have been recorded among the primaries
-  part = (TParticle *)particles.At(fHgwmk+1);
+  part = dynamic_cast<TParticle*>(particles.At(fHgwmk+1));
   particles.At(part->GetFirstMother())->ResetBit(kDaughtersBit);
   // Second pass, build map between old and new numbering
   for(i=fHgwmk+1; i<fNtrack; i++) {
@@ -329,7 +369,7 @@ void AliStack::PurifyKine()
       map[i]=nkeep;
       // If old and new are different, have to move the pointer
       if(i!=nkeep) particles[nkeep]=particles.At(i);
-      part = (TParticle*) particles.At(nkeep);
+      part = dynamic_cast<TParticle*>(particles.At(nkeep));
       
       // as the parent is always *before*, it must be already
       // in place. This is what we are checking anyway!
@@ -343,10 +383,10 @@ void AliStack::PurifyKine()
   
   // Fix daughters information
   for (i=fHgwmk+1; i<nkeep; i++) {
-    part = (TParticle *)particles.At(i);
+    part = dynamic_cast<TParticle*>(particles.At(i));
     parent = part->GetFirstMother();
     if(parent>=0) {
-      father = (TParticle *)particles.At(parent);
+      father = dynamic_cast<TParticle*>(particles.At(parent));
       if(father->TestBit(kDaughtersBit)) {
       
        if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
@@ -364,10 +404,10 @@ void AliStack::PurifyKine()
   TList* hitLists = gAlice->GetHitLists();
   TIter next(hitLists);
   TCollection *hitList;
-  while((hitList = (TCollection*)next())) {
+  while((hitList = dynamic_cast<TCollection*>(next()))) {
     TIter nexthit(hitList);
     AliHit *hit;
-    while((hit = (AliHit*)nexthit())) {
+    while((hit = dynamic_cast<AliHit*>(nexthit()))) {
       hit->SetTrack(map[hit->GetTrack()]);
     }
   }
@@ -380,22 +420,24 @@ void AliStack::PurifyKine()
    TObjArray* modules = gAlice->Modules();
    TIter nextmod(modules);
    AliModule *detector;
-   while((detector = (AliModule*)nextmod())) {
+   while((detector = dynamic_cast<AliModule*>(nextmod()))) {
      detector->RemapTrackHitIDs(map.GetArray());
+     detector->RemapTrackReferencesIDs(map.GetArray());
    }
   
    // Now the output bit, from fHgwmk to nkeep we write everything and we erase
    if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
 
    for (i=fHgwmk+1; i<nkeep; ++i) {
-     fParticleBuffer = (TParticle*) particles.At(i);
-     fParticleFileMap[i]=(Int_t) fTreeK->GetEntries();
+     fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
+     fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
      fTreeK->Fill();
-     particles[i]=0;
+     particles[i]=fParticleBuffer=0;
    }
 
    for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
 
+   Int_t toshrink = fNtrack-fHgwmk-1;
    fLoadPoint-=toshrink;
    for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
 
@@ -404,6 +446,34 @@ void AliStack::PurifyKine()
    //   delete [] map;
 }
 
+Bool_t AliStack::KeepPhysics(TParticle* part)
+{
+    //
+    // Some particles have to kept on the stack for reasons motivated
+    // by physics analysis. Decision is put here.
+    //
+    Bool_t keep = kFALSE;
+    //
+    // Keep first-generation daughter from primaries with heavy flavor 
+    //
+    Int_t parent = part->GetFirstMother();
+    if (parent >= 0 && parent <= fHgwmk) {
+       TParticle* father = dynamic_cast<TParticle*>(Particles()->At(parent));
+       Int_t kf = father->GetPdgCode();
+       kf = TMath::Abs(kf);
+       Int_t kfl = kf;
+       // meson ?
+       if  (kfl > 10) kfl/=100;
+       // baryon
+       if (kfl > 10) kfl/=10;
+       if (kfl > 10) kfl/=10;
+       if (kfl >= 4) {
+           keep = kTRUE;
+       }
+    }
+    return keep;
+}
+
 //_____________________________________________________________________________
 void AliStack::FinishEvent()
 {
@@ -431,10 +501,12 @@ void AliStack::FinishEvent()
   TObject *part;
   for(Int_t i=0; i<fHgwmk+1; ++i) 
     if((part=fParticleMap->At(i))) {
-      fParticleBuffer = (TParticle*) part;
-      fParticleFileMap[i]= (Int_t) fTreeK->GetEntries();
+      fParticleBuffer = dynamic_cast<TParticle*>(part);
+      fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
       fTreeK->Fill();
-      (*fParticleMap)[i]=0;      
+      //PH      (*fParticleMap)[i]=fParticleBuffer=0;      
+      fParticleBuffer=0;      
+      fParticleMap->AddAt(0,i);      
       
       // When all primaries were filled no particle!=0
       // should be left => to be removed later.
@@ -448,7 +520,7 @@ void AliStack::FinishEvent()
         // To be removed later and replaced with break.
       if(!allFilled) allFilled = kTRUE;
     } 
-  //cout << "Nof particles: " << fNtrack << endl;
+//    cout << "Nof particles: " << fNtrack << endl;
   //Reset();   
 } 
 
@@ -463,7 +535,7 @@ void AliStack::FlagTrack(Int_t track)
 
   Int_t curr=track;
   while(1) {
-    particle=(TParticle*)fParticleMap->At(curr);
+    particle=dynamic_cast<TParticle*>(fParticleMap->At(curr));
     
     // If the particle is flagged the three from here upward is saved already
     if(particle->TestBit(kKeepBit)) return;
@@ -477,7 +549,7 @@ void AliStack::FlagTrack(Int_t track)
 }
  
 //_____________________________________________________________________________
-void AliStack::KeepTrack(const Int_t track)
+void AliStack::KeepTrack(Int_t track)
 { 
   //
   // Flags a track to be kept
@@ -508,13 +580,19 @@ void  AliStack::ResetArrays(Int_t size)
   // Resets stack arrays
   //
 
-  fParticles->Clear();
-  fParticleMap->Clear();
-  if (size>0) fParticleMap->Expand(size);  
+  if (fParticles) 
+    fParticles->Clear();
+  else
+    fParticles = new TClonesArray("TParticle",1000);
+  if (fParticleMap) {
+    fParticleMap->Clear();
+    if (size>0) fParticleMap->Expand(size);}
+  else
+    fParticleMap = new TObjArray(size);
 }
 
 //_____________________________________________________________________________
-void AliStack::SetHighWaterMark(const Int_t nt)
+void AliStack::SetHighWaterMark(Int_t)
 {
   //
   // Set high water mark for last track in event
@@ -534,15 +612,12 @@ TParticle* AliStack::Particle(Int_t i)
   //
   // Return particle with specified ID
   
-  if(!(*fParticleMap)[i]) {
-    Int_t nentries = fParticles->GetEntries();
+  //PH  if(!(*fParticleMap)[i]) {
+  if(!fParticleMap->At(i)) {
+    Int_t nentries = fParticles->GetEntriesFast();
     // algorithmic way of getting entry index
     // (primary particles are filled after secondaries)
-    Int_t entry;
-    if (i<fNprimary)
-       entry = i+fNtrack-fNprimary;
-    else 
-       entry = i-fNprimary;
+    Int_t entry = TreeKEntry(i);
     // check whether algorithmic way and 
     // and the fParticleFileMap[i] give the same;
     // give the fatal error if not
@@ -556,24 +631,50 @@ TParticle* AliStack::Particle(Int_t i)
     new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
     fParticleMap->AddAt((*fParticles)[nentries],i);
   }
-  return (TParticle *) (*fParticleMap)[i];
+  //PH  return dynamic_cast<TParticle *>((*fParticleMap)[i]);
+  return dynamic_cast<TParticle*>(fParticleMap->At(i));
 }
 
 //_____________________________________________________________________________
-Int_t AliStack::GetPrimary(Int_t id) const
+TParticle* AliStack::ParticleFromTreeK(Int_t id) const
+{
+// 
+// return pointer to TParticle with label id
+//
+  Int_t entry;
+  if ((entry = TreeKEntry(id)) < 0) return 0;
+  if (fTreeK->GetEntry(entry)<=0) return 0;
+  return fParticleBuffer;
+}
+
+//_____________________________________________________________________________
+Int_t AliStack::TreeKEntry(Int_t id) const 
+{
+//
+// return entry number in the TreeK for particle with label id
+// return negative number if label>fNtrack
+//
+  Int_t entry;
+  if (id<fNprimary)
+    entry = id+fNtrack-fNprimary;
+  else 
+    entry = id-fNprimary;
+  return entry;
+}
+
+//_____________________________________________________________________________
+Int_t AliStack::GetPrimary(Int_t id)
 {
   //
   // Return number of primary that has generated track
   //
   
   int current, parent;
-  TParticle *part;
   //
   parent=id;
   while (1) {
     current=parent;
-    part = (TParticle *)fParticleMap->At(current);
-    parent=part->GetFirstMother();
+    parent=Particle(current)->GetFirstMother();
     if(parent<0) return current;
   }
 }
@@ -585,7 +686,8 @@ void AliStack::DumpPart (Int_t i) const
   // Dumps particle i in the stack
   //
   
-  ((TParticle*) (*fParticleMap)[i])->Print();
+  //PH  dynamic_cast<TParticle*>((*fParticleMap)[i])->Print();
+  dynamic_cast<TParticle*>(fParticleMap->At(i))->Print();
 }
 
 //_____________________________________________________________________________
@@ -595,9 +697,11 @@ void AliStack::DumpPStack ()
   // Dumps the particle stack
   //
 
+  Int_t i;
+
   printf(
         "\n\n=======================================================================\n");
-  for (Int_t i=0;i<fNtrack;i++) 
+  for (i=0;i<fNtrack;i++) 
     {
       TParticle* particle = Particle(i);
       if (particle) {
@@ -613,7 +717,7 @@ void AliStack::DumpPStack ()
   
   // print  particle file map
   printf("\nParticle file map: \n");
-  for (Int_t i=0; i<fNtrack; i++) 
+  for (i=0; i<fNtrack; i++) 
       printf("   %d th entry: %d \n",i,fParticleFileMap[i]);
 }
 
@@ -631,7 +735,7 @@ void AliStack::DumpLoadedStack() const
         "\n\n=======================================================================\n");
   for (Int_t i=0;i<fNtrack;i++) 
     {
-      TParticle* particle = (TParticle*) particles[i];
+      TParticle* particle = dynamic_cast<TParticle*>(particles[i]);
       if (particle) {
         printf("-> %d ",i); particle->Print();
         printf("--------------------------------------------------------------\n");
@@ -661,7 +765,7 @@ void AliStack::CleanParents()
   TParticle *part;
   int i;
   for(i=0; i<fHgwmk+1; i++) {
-    part = (TParticle *)particles.At(i);
+    part = dynamic_cast<TParticle*>(particles.At(i));
     if(part) if(!part->TestBit(kDaughtersBit)) {
       part->SetFirstDaughter(-1);
       part->SetLastDaughter(-1);
@@ -681,7 +785,7 @@ TParticle* AliStack::GetNextParticle()
   // search secondaries
   //for(Int_t i=fNtrack-1; i>=0; i--) {
   for(Int_t i=fNtrack-1; i>fHgwmk; i--) {
-      particle = (TParticle*) fParticleMap->At(i);
+      particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
       if ((particle) && (!particle->TestBit(kDoneBit))) {
          fCurrent=i;    
          //cout << "GetNextParticle() - secondary " 
@@ -693,7 +797,7 @@ TParticle* AliStack::GetNextParticle()
   // take next primary if all secondaries were done
   while (fCurrentPrimary>=0) {
       fCurrent = fCurrentPrimary;    
-      particle = (TParticle*) fParticleMap->At(fCurrentPrimary--);
+      particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
       if ((particle) && (!particle->TestBit(kDoneBit))) {
          //cout << "GetNextParticle() - primary " 
          //   << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
@@ -709,7 +813,7 @@ TParticle* AliStack::GetNextParticle()
 }
 
 //__________________________________________________________________________________________
-void AliStack::MakeTree(Int_t event, const char *file)
+void AliStack::MakeTree(Int_t event, const char * /*file*/)
 {
 //
 //  Make Kine tree and creates branch for writing particles
@@ -717,16 +821,16 @@ void AliStack::MakeTree(Int_t event, const char *file)
   TBranch *branch=0;
   // Make Kinematics Tree
   char hname[30];
-  //    printf("\n MakeTree called %d", event);
   if (!fTreeK) {
     sprintf(hname,"TreeK%d",event);
     fTreeK = new TTree(hname,"Kinematics");
     //  Create a branch for particles
-    branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000, 1);          
+    branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);          
     fTreeK->Write(0,TObject::kOverwrite);
   }
 }
 
+//_____________________________________________________________________________
 void AliStack::BeginEvent(Int_t event)
 {
 // start a new event
@@ -743,6 +847,7 @@ void AliStack::BeginEvent(Int_t event)
     }
 }
 
+//_____________________________________________________________________________
 void AliStack::FinishRun()
 {
 // Clean TreeK information
@@ -751,26 +856,30 @@ void AliStack::FinishRun()
     }
 }
 
-void AliStack::GetEvent(Int_t event)
+Bool_t AliStack::GetEvent(Int_t event)
 {
 //
 // Get new event from TreeK
+
     // Reset/Create the particle stack
     if (fTreeK) delete fTreeK;
     
     // Get Kine Tree from file
     char treeName[20];
     sprintf(treeName,"TreeK%d",event);
-    fTreeK = (TTree*)gDirectory->Get(treeName);
+    fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
 
-    if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
-
-  else    
-      Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
-//
-//    printf("\n primaries %d", fNprimary);
-//    printf("\n tracks    %d", fNtrack);    
-//
-    Int_t size = (Int_t)fTreeK->GetEntries();
+    if (fTreeK) 
+      fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
+    else {
+      //      Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
+      Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
+      return kFALSE;
+    }
+//      printf("\n primaries %d", fNprimary);
+//      printf("\n tracks    %d", fNtrack);    
+      
+    Int_t size = static_cast<Int_t>(fTreeK->GetEntries());
     ResetArrays(size);
+    return kTRUE;
 }