]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliStack.cxx
New base for all classes in HLT/PHOS
[u/mrichter/AliRoot.git] / STEER / AliStack.cxx
index 9ee1ed7723cd3ab3ea6eb7905a1bf1ab426019b6..747481180ce3d4ddf9fb18433b60ca86fbb65a58 100644 (file)
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
-//  Particles stack class
+//  Particles stack class                                                    //
+//  Implements the TMCVirtualStack of the Virtual Monte Carlo                //
+//  Holds the particles transported during simulation                        //
+//  Is used to compare results of reconstruction with simulation             //
+//  Author A.Morsch                                                          //
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
  
-#include <TFile.h>
-#include <TFolder.h>
+#include <TClonesArray.h>
 #include <TObjArray.h>
+#include <TPDGCode.h>
 #include <TParticle.h>
 #include <TParticlePDG.h>
 #include <TTree.h>
 
+#include "AliLog.h"
 #include "AliHit.h"
 #include "AliModule.h"
 #include "AliRun.h"
@@ -44,6 +49,7 @@ AliStack::AliStack():
   fParticleMap(0),
   fParticleFileMap(0),
   fParticleBuffer(0),
+  fCurrentTrack(0),
   fTreeK(0),
   fNtrack(0),
   fNprimary(0),
@@ -51,7 +57,7 @@ AliStack::AliStack():
   fCurrentPrimary(-1),
   fHgwmk(0),
   fLoadPoint(0),
-  fEventFolderName(AliConfig::fgkDefaultEventFolderName)
+  fEventFolderName(AliConfig::GetDefaultEventFolderName())
 {
   //
   // Default constructor
@@ -64,6 +70,7 @@ AliStack::AliStack(Int_t size, const char* evfoldname):
   fParticleMap(new TObjArray(size)),
   fParticleFileMap(0),
   fParticleBuffer(0),
+  fCurrentTrack(0),
   fTreeK(0),
   fNtrack(0),
   fNprimary(0),
@@ -80,29 +87,30 @@ AliStack::AliStack(Int_t size, const char* evfoldname):
 
 //_______________________________________________________________________
 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)
+    TVirtualMCStack(st),
+    fParticles(new TClonesArray("TParticle",1000)),
+    fParticleMap(new TObjArray(*st.Particles())),
+    fParticleFileMap(st.fParticleFileMap),
+    fParticleBuffer(0),
+    fCurrentTrack(0),
+    fTreeK((TTree*)(st.fTreeK->Clone())),
+    fNtrack(st.GetNtrack()),
+    fNprimary(st.GetNprimary()),
+    fCurrent(-1),
+    fCurrentPrimary(-1),
+    fHgwmk(0),
+    fLoadPoint(0),
+    fEventFolderName(0)
 {
-  //
-  // Copy constructor
-  //
-  st.Copy(*this);
+    // Copy constructor
+    ConnectTree();
 }
 
+
 //_______________________________________________________________________
-void AliStack::Copy(AliStack&) const
+void AliStack::Copy(TObject&) const
 {
-  Fatal("Copy","Not implemented!\n");
+  AliFatal("Not implemented!");
 }
 
 //_______________________________________________________________________
@@ -167,8 +175,8 @@ void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
                 vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
                 mech, ntr, weight, is);
     } else {
-       Warning("PushTrack", "Particle type %d not defined in PDG Database !\n", pdg);
-       Warning("PushTrack", "Particle skipped !\n");
+       AliWarning(Form("Particle type %d not defined in PDG Database !", pdg));
+       AliWarning("Particle skipped !");
     }
 }
 
@@ -203,7 +211,8 @@ void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg,
 
   const Int_t kFirstDaughter=-1;
   const Int_t kLastDaughter=-1;
-  
+
+
   TClonesArray &particles = *fParticles;
   TParticle* particle
     = new(particles[fLoadPoint++]) 
@@ -219,6 +228,8 @@ void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg,
   //  Declare that the daughter information is valid
   particle->SetBit(kDaughtersBit);
   //  Add the particle to the stack
+  
+  
   fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
 
   if(parent>=0) {
@@ -228,7 +239,7 @@ void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg,
       if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
     }
     else {
-      printf("Error in AliStack::PushTrack: Parent %d does not exist\n",parent);
+      AliError(Form("Parent %d does not exist",parent));
     }
   } 
   else { 
@@ -294,12 +305,11 @@ void AliStack::PurifyKine()
   TArrayI map(particles.GetLast()+1);
 
   // Save in Header total number of tracks before compression
-
   // If no tracks generated return now
   if(fHgwmk+1 == fNtrack) return;
 
   // First pass, invalid Daughter information
-  for(i=0; i<fNtrack; i++) {
+   for(i=0; i<fNtrack; i++) {
     // Preset map, to be removed later
     if(i<=fHgwmk) map[i]=i ; 
     else {
@@ -358,16 +368,19 @@ void AliStack::PurifyKine()
       }
     }
   }
+
   
   // Now loop on all registered hit lists
   TList* hitLists = gAlice->GetMCApp()->GetHitLists();
   TIter next(hitLists);
   TCollection *hitList;
+  
   while((hitList = dynamic_cast<TCollection*>(next()))) {
     TIter nexthit(hitList);
     AliHit *hit;
+    
     while((hit = dynamic_cast<AliHit*>(nexthit()))) {
-      hit->SetTrack(map[hit->GetTrack()]);
+       hit->SetTrack(map[hit->GetTrack()]);
     }
   }
 
@@ -400,11 +413,140 @@ void AliStack::PurifyKine()
 
    Int_t toshrink = fNtrack-fHgwmk-1;
    fLoadPoint-=toshrink;
+
+   
    for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
 
    fNtrack=nkeep;
    fHgwmk=nkeep-1;
-   //   delete [] map;
+}
+
+void AliStack::ReorderKine()
+{
+//
+// In some transport code children might not come in a continuous sequence.
+// In this case the stack  has  to  be reordered in order to establish the 
+// mother daughter relation using index ranges.
+//    
+  if(fHgwmk+1 == fNtrack) return;
+
+  //
+  // Howmany secondaries have been produced ?
+  Int_t nNew = fNtrack - fHgwmk - 1;
+    
+  if (nNew > 0) {
+      Int_t i, j;
+      TObjArray &particles = *fParticleMap;
+      TArrayI map1(nNew);
+      //
+      // Copy pointers to temporary array
+      TParticle** tmp = new TParticle*[nNew];
+      
+      for (i = 0; i < nNew; i++) {
+         if (particles.At(fHgwmk + 1 + i)) {
+             tmp[i] = (TParticle*) (particles.At(fHgwmk + 1 + i));
+         } else {
+             tmp[i] = 0x0;
+         }
+         map1[i] = -99;
+      }
+  
+      
+      //
+      // Reset  LoadPoint 
+      // 
+      fLoadPoint = fHgwmk + 1;
+      //
+      // Re-Push particles into stack 
+      // The outer loop is over parents, the inner over children.
+      // -1 refers to the primary particle
+      //
+      for (i = -1; i < nNew-1; i++) {
+         Int_t ipa;
+         TParticle* parP;
+         if (i == -1) {
+             ipa  = tmp[0]->GetFirstMother();
+             parP =dynamic_cast<TParticle*>(particles.At(ipa));
+         } else {
+             ipa = (fHgwmk + 1 + i);
+              // Skip deleted particles
+             if (!tmp[i])                          continue;
+              // Skip particles without children
+             if (tmp[i]->GetFirstDaughter() == -1) continue;
+             parP = tmp[i];
+         }
+          // Reset daughter information
+
+         Int_t idaumin = parP->GetFirstDaughter() - fHgwmk - 1;
+         Int_t idaumax = parP->GetLastDaughter()  - fHgwmk - 1;
+         parP->SetFirstDaughter(-1);
+         parP->SetLastDaughter(-1);
+         for (j = idaumin; j <= idaumax; j++) {
+              // Skip deleted particles
+             if (!tmp[j])        continue;
+              // Skip particles already handled
+             if (map1[j] != -99) continue;
+             Int_t jpa = tmp[j]->GetFirstMother();
+              // Check if daughter of current parent
+             if (jpa == ipa) {
+                 particles[fLoadPoint] = tmp[j];
+                 // Re-establish daughter information
+                 parP->SetLastDaughter(fLoadPoint);
+                 if (parP->GetFirstDaughter() == -1) parP->SetFirstDaughter(fLoadPoint);
+                 // Set Mother information
+                 if (i != -1) {
+                     tmp[j]->SetFirstMother(map1[i]);
+                 } 
+                 // Build the map
+                 map1[j] = fLoadPoint;
+                 // Increase load point
+                 fLoadPoint++;
+             }
+         } // children
+      } // parents
+
+      delete[] tmp;
+
+      //
+      // Build map for remapping of hits
+      // 
+      TArrayI map(fNtrack);
+      for (i = 0; i < fNtrack; i ++) {
+         if (i <= fHgwmk) {
+             map[i] = i;
+         } else{
+             map[i] = map1[i - fHgwmk -1];
+         }
+      }
+      
+      // Now loop on all registered hit lists
+      
+      TList* hitLists = gAlice->GetMCApp()->GetHitLists();
+      TIter next(hitLists);
+      TCollection *hitList;
+      
+      while((hitList = dynamic_cast<TCollection*>(next()))) {
+         TIter nexthit(hitList);
+         AliHit *hit;
+         while((hit = dynamic_cast<AliHit*>(nexthit()))) {
+             hit->SetTrack(map[hit->GetTrack()]);
+         }
+      }
+  
+  // 
+  // This for detectors which have a special mapping mechanism
+  // for hits, such as TPC and TRD
+  //
+  
+      TObjArray* modules = gAlice->Modules();
+      TIter nextmod(modules);
+      AliModule *detector;
+      while((detector = dynamic_cast<AliModule*>(nextmod()))) {
+         detector->RemapTrackHitIDs(map.GetArray());
+         detector->RemapTrackReferencesIDs(map.GetArray());
+      }
+      gAlice->GetMCApp()->RemapTrackReferencesIDs(map.GetArray());
+  } // new particles poduced
 }
 
 Bool_t AliStack::KeepPhysics(TParticle* part)
@@ -470,7 +612,7 @@ void AliStack::FinishEvent()
       
       // When all primaries were filled no particle!=0
       // should be left => to be removed later.
-      if (allFilled) printf("Why != 0 part # %d?\n",i);
+      if (allFilled) AliWarning(Form("Why != 0 part # %d?\n",i));
     }
     else 
      {
@@ -557,10 +699,10 @@ void AliStack::SetHighWaterMark(Int_t)
   //
   // Set high water mark for last track in event
   //
-  
+
+    
   fHgwmk = fNtrack-1;
   fCurrentPrimary=fHgwmk;
-  
   // Set also number of primary tracks
   fNprimary = fHgwmk+1;
   fNtrack   = fHgwmk+1;      
@@ -582,9 +724,9 @@ TParticle* AliStack::Particle(Int_t i)
     // and the fParticleFileMap[i] give the same;
     // give the fatal error if not
     if (entry != fParticleFileMap[i]) {
-      Fatal("Particle",
+      AliFatal(Form(
         "!! The algorithmic way and map are different: !!\n entry: %d map: %d",
-       entry, fParticleFileMap[i]); 
+       entry, fParticleFileMap[i]))
     } 
       
     TreeK()->GetEntry(entry);
@@ -634,7 +776,7 @@ Int_t AliStack::GetCurrentParentTrackNumber() const
   if (current) 
     return current->GetFirstMother();
   else {
-    Warning("GetCurrentParentTrackNumber", "Current track not found in the stack");
+    AliWarning("Current track not found in the stack");
     return -1;
   }  
 }
@@ -778,6 +920,8 @@ TParticle* AliStack::GetNextParticle()
   
   // nothing to be tracked
   fCurrent = -1;
+  
   return particle;  
 }
 //__________________________________________________________________________________________
@@ -794,17 +938,18 @@ TTree* AliStack::TreeK()
      AliRunLoader *rl = AliRunLoader::GetRunLoader(fEventFolderName);
      if (rl == 0x0)
       {
-        Fatal("TreeK","Can not get RunLoader from event folder named %s",fEventFolderName.Data());
+        AliFatal(Form("Can not get RunLoader from event folder named %s",fEventFolderName.Data()));
         return 0x0;//pro forma
       }
     fTreeK = rl->TreeK();
-    if ( fTreeK == 0x0)
+    if ( fTreeK )
      {
-      Error("TreeK","Can not get TreeK from RL. Ev. Folder is %s",fEventFolderName.Data());
+      ConnectTree();
      }
     else
      {
-      ConnectTree();
+      //don't panic - could be Lego
+      AliWarning(Form("Can not get TreeK from RL. Ev. Folder is %s",fEventFolderName.Data()));
      }
    }
   return fTreeK;//never reached
@@ -816,12 +961,12 @@ void AliStack::ConnectTree()
 //
 //  Creates branch for writing particles
 //
-  if (AliLoader::fgDebug) Info("ConnectTree","Connecting TreeK");
+  AliDebug(1, "Connecting TreeK");
   if (fTreeK == 0x0)
    {
     if (TreeK() == 0x0)
      {
-      Fatal("ConnectTree","Parameter is NULL");//we don't like such a jokes
+      AliFatal("Parameter is NULL");//we don't like such a jokes
       return;
      }
     return;//in this case TreeK() calls back this method (ConnectTree) 
@@ -831,35 +976,32 @@ void AliStack::ConnectTree()
 
  //  Create a branch for particles   
   
-  if (AliLoader::fgDebug) 
-   Info("ConnectTree","Tree name is %s",fTreeK->GetName());
+  AliDebug(2, Form("Tree name is %s",fTreeK->GetName()));
    
   if (fTreeK->GetDirectory())
    {
-     if (AliLoader::fgDebug)    
-      Info("ConnectTree","and dir is %s",fTreeK->GetDirectory()->GetName());
+     AliDebug(2, Form("and dir is %s",fTreeK->GetDirectory()->GetName()));
    }    
   else
-    Warning("ConnectTree","DIR IS NOT SET !!!");
+    AliWarning("DIR IS NOT SET !!!");
   
-  TBranch *branch=fTreeK->GetBranch(AliRunLoader::fgkKineBranchName);
+  TBranch *branch=fTreeK->GetBranch(AliRunLoader::GetKineBranchName());
   if(branch == 0x0)
    {
-    branch = fTreeK->Branch(AliRunLoader::fgkKineBranchName, "TParticle", &fParticleBuffer, 4000);
-    if (AliLoader::fgDebug) Info("ConnectTree","Creating Branch in Tree");
+    branch = fTreeK->Branch(AliRunLoader::GetKineBranchName(), "TParticle", &fParticleBuffer, 4000);
+    AliDebug(2, "Creating Branch in Tree");
    }  
   else
    {
-    if (AliLoader::fgDebug) Info("ConnectTree","Branch Found in Tree");
+    AliDebug(2, "Branch Found in Tree");
     branch->SetAddress(&fParticleBuffer);
    }
   if (branch->GetDirectory())
    {
-    if (AliLoader::fgDebug) 
-      Info("ConnectTree","Branch Dir Name is %s",branch->GetDirectory()->GetName());
+    AliDebug(1, Form("Branch Dir Name is %s",branch->GetDirectory()->GetName()));
    } 
   else
-    Warning("ConnectTree","Branch Dir is NOT SET");
+    AliWarning("Branch Dir is NOT SET");
 }
 //__________________________________________________________________________________________
 
@@ -887,7 +1029,7 @@ Bool_t AliStack::GetEvent()
 
     if (TreeK() == 0x0) //forces connecting
      {
-      Error("GetEvent","cannot find Kine Tree for current event\n");
+      AliError("cannot find Kine Tree for current event");
       return kFALSE;
      }
       
@@ -902,3 +1044,100 @@ void AliStack::SetEventFolderName(const char* foldname)
  //Sets event folder name
  fEventFolderName = foldname;
 }
+
+Bool_t AliStack::IsStable(Int_t pdg) const
+{
+//
+// Decide whether particle (pdg) is stable
+//
+
+    const Int_t kNstable = 14;
+    Int_t i;
+
+    Int_t pdgStable[kNstable] = {
+       kGamma,             // Photon
+       kElectron,          // Electron
+       kMuonPlus,          // Muon 
+       kPiPlus,            // Pion
+       kKPlus,             // Kaon
+       kProton,            // Proton 
+       kNeutron,           // Neutron
+       kLambda0,           // Lambda_0
+       kSigmaMinus,        // Sigma Minus
+       kSigma0,            // Sigma_0
+       kSigmaPlus,         // Sigma Plus
+       3312,               // Xsi Minus 
+       3322,               // Xsi 
+       3334,               // Omega
+    };
+    
+    Bool_t isStable = kFALSE;
+    for (i = 0; i < kNstable; i++) {
+       if (pdg == TMath::Abs(pdgStable[i])) {
+           isStable = kTRUE;
+           break;
+       }
+    }
+
+    return isStable;
+}
+
+Bool_t AliStack::IsPhysicalPrimary(Int_t index)
+{
+    //
+    // Test if a particle is a physical primary according to the following definition:
+    // Particles produced in the collision including products of strong and
+    // electromagnetic decay and excluding feed-down from weak decays of strange
+    // particles.
+    //
+    TParticle* p = Particle(index);
+    Int_t ist = p->GetStatusCode();
+    
+    //
+    // Initial state particle
+    if (ist > 20) return kFALSE;
+    
+    Int_t pdg = TMath::Abs(p->GetPdgCode());
+    
+    if (!IsStable(pdg)) return kFALSE;
+    
+    if (index < GetNprimary()) {
+//
+// Particle produced by generator
+       return kTRUE;
+    } else {
+//
+// Particle produced during transport
+//
+// Check if this is a heavy flavor decay product
+       Int_t imo =  p->GetFirstMother();
+       TParticle* pm  = Particle(imo);
+       Int_t mpdg = TMath::Abs(pm->GetPdgCode());
+       Int_t mfl  = Int_t (mpdg / TMath::Power(10, Int_t(TMath::Log10(mpdg))));
+       //
+       // Light hadron
+       if (mfl < 4) return kFALSE;
+       
+       //
+       // Heavy flavor hadron produced by generator
+       if (imo <  GetNprimary()) {
+           return kTRUE;
+       }
+       
+       // To be sure that heavy flavor has not been produced in a secondary interaction
+       // Loop back to the generated mother
+       while (imo >=  GetNprimary()) {
+           imo = p->GetFirstMother();
+           pm  =  Particle(imo);
+       }
+       mpdg = TMath::Abs(pm->GetPdgCode());
+       mfl  = Int_t (mpdg / TMath::Power(10, Int_t(TMath::Log10(mpdg))));
+
+       if (mfl < 4) {
+           return kFALSE;
+       } else {
+           return kTRUE;
+       } 
+    } // produced by generator ?
+} 
+