]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliMCEventHandler.cxx
Updated task list
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
index 6c6470ebb081b7b00bd0aeea9615902b2dbfde87..a4a9824584a8d187a187b5ea2ea216543439a500 100644 (file)
@@ -1,5 +1,4 @@
-/**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+/************************************************************************* * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved *
  *                                                                        *
  * Author: The ALICE Off-line Project.                                    *
  * Contributors are mentioned in the code where appropriate.              *
@@ -27,6 +26,7 @@
 
 #include "AliMCEventHandler.h"
 #include "AliMCEvent.h"
+#include "AliPDG.h"
 #include "AliTrackReference.h"
 #include "AliHeader.h"
 #include "AliStack.h"
@@ -52,15 +52,22 @@ AliMCEventHandler::AliMCEventHandler() :
     fTreeTR(0),
     fDirK(0),
     fDirTR(0),
+    fParticleSelected(0),
+    fLabelMap(0),
     fNEvent(-1),
     fEvent(-1),
     fPathName(new TString("./")),
     fExtension(""),
     fFileNumber(0),
     fEventsPerFile(0),
-    fReadTR(kTRUE)
+    fReadTR(kTRUE),
+    fInitOk(kFALSE)
 {
-    // Default constructor
+  //
+  // Default constructor
+  //
+  // Be sure to add all particles to the PDG database
+  AliPDG::AddParticlesToPdgDataBase();
 }
 
 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
@@ -74,14 +81,22 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
     fTreeTR(0),
     fDirK(0),
     fDirTR(0),
+    fParticleSelected(0),
+    fLabelMap(0),
     fNEvent(-1),
     fEvent(-1),
     fPathName(new TString("./")),
     fExtension(""),
     fFileNumber(0),
-    fEventsPerFile(0)
+    fEventsPerFile(0),
+    fReadTR(kTRUE),
+    fInitOk(kFALSE)
 {
-    // Constructor
+  //
+  // Constructor
+  //
+  // Be sure to add all particles to the PDG database
+  AliPDG::AddParticlesToPdgDataBase();
 }
 AliMCEventHandler::~AliMCEventHandler()
 { 
@@ -92,16 +107,19 @@ AliMCEventHandler::~AliMCEventHandler()
     delete fFileTR;
 }
 
-Bool_t AliMCEventHandler::InitIO(Option_t* opt)
+Bool_t AliMCEventHandler::Init(Option_t* opt)
 { 
     // Initialize input
     //
     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
     //
-
     fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
-    if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
-
+    if (!fFileE) {
+       AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
+       fInitOk = kFALSE;
+       return kFALSE;
+    }
+    
     //
     // Tree E
     fFileE->GetObject("TE", fTreeE);
@@ -111,20 +129,29 @@ Bool_t AliMCEventHandler::InitIO(Option_t* opt)
     //
     // Tree K
     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
-    if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
+    if (!fFileK) {
+       AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
+       fInitOk = kFALSE;
+       return kTRUE;
+    }
+    
     fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
     //
     // Tree TR
     if (fReadTR) {
-      fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
-      if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
+       fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
+       if (!fFileTR) {
+           AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
+           fInitOk = kFALSE;
+           return kTRUE;
+       }
     }
     //
     // Reset the event number
     fEvent      = -1;
     fFileNumber =  0;
-    
-    AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
+    AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
+    fInitOk = kTRUE;
     return kTRUE;
 }
 
@@ -133,6 +160,8 @@ Bool_t AliMCEventHandler::GetEvent(Int_t iev)
     // Load the event number iev
     //
     // Calculate the file number
+    if (!fInitOk) return kFALSE;
+    
     Int_t inew  = iev / fEventsPerFile;
     if (inew != fFileNumber) {
        fFileNumber = inew;
@@ -151,6 +180,7 @@ Bool_t AliMCEventHandler::GetEvent(Int_t iev)
        AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
        return kFALSE;
     }
+    
     fDirK ->GetObject("TreeK", fTreeK);
     // Connect TreeK to MCEvent
     fMCEvent->ConnectTreeK(fTreeK);
@@ -170,7 +200,6 @@ Bool_t AliMCEventHandler::GetEvent(Int_t iev)
 Bool_t AliMCEventHandler::OpenFile(Int_t i)
 {
     // Open file i
-    Bool_t ok = kTRUE;
     if (i > 0) {
        fExtension = Form("%d", i);
     } else {
@@ -181,35 +210,136 @@ Bool_t AliMCEventHandler::OpenFile(Int_t i)
     delete fFileK;
     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
     if (!fFileK) {
-       AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
-       ok = kFALSE;
+       AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
+       fInitOk = kFALSE;
+       return kFALSE;
     }
     
-    delete fFileTR;
-    fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
-    if (!fFileTR) {
-       AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
-       ok = kFALSE;
+    if (fReadTR) {
+       delete fFileTR;
+       fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
+       if (!fFileTR) {
+           AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
+           fInitOk = kFALSE;
+           return kFALSE;
+       }
     }
     
-    return ok;
+    fInitOk = kTRUE;
+    return kTRUE;
 }
 
-Bool_t AliMCEventHandler::BeginEvent()
+Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
 { 
+    fParticleSelected.Delete();
+    fLabelMap.Delete();
     // Read the next event
-    fEvent++;
-    if (fEvent >= fNEvent) {
-       AliWarning(Form("AliMCEventHandler: Event number out of range %5d\n", fEvent));
+    if (entry == -1) {
+       fEvent++;
+       entry = fEvent;
+    } else {
+       fEvent = entry;
+    }
+
+    if (entry >= fNEvent) {
+       AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
        return kFALSE;
     }
-    return GetEvent(fEvent);
+    return GetEvent(entry);
+}
+
+void AliMCEventHandler::SelectParticle(Int_t i){
+  // taking the absolute values here, need to take care 
+  // of negative daughter and mother
+  // IDs when setting!
+  if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
+}
+
+Bool_t AliMCEventHandler::IsParticleSelected(Int_t i)  {
+  // taking the absolute values here, need to take 
+  // care with negative daughter and mother
+  // IDs when setting!
+  return (fParticleSelected.GetValue(TMath::Abs(i))==1);
+}
+
+
+void AliMCEventHandler::CreateLabelMap(){
+
+  //
+  // this should be called once all selections where done 
+  //
+
+  fLabelMap.Delete();
+  if(!fMCEvent){
+    fParticleSelected.Delete();
+    return;
+  }
+
+  VerifySelectedParticles();
+  AliStack *pStack = fMCEvent->Stack();
+
+  Int_t iNew = 0;
+  for(int i = 0;i < pStack->GetNtrack();++i){
+    if(IsParticleSelected(i)){
+      fLabelMap.Add(i,iNew);
+      iNew++;
+    }
+  }
+}
+
+Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
+  // Gets the labe from the new created Map
+  // Call CreatLabelMap before
+  // otherwise only 0 returned
+  return fLabelMap.GetValue(TMath::Abs(i));
+}
+
+void  AliMCEventHandler::VerifySelectedParticles(){
+
+  //  
+  // Make sure that each particle has at least it's predecessors
+  // selected so that we have the complete ancestry tree
+  // Private, should be only called by CreateLabelMap
+
+  if(!fMCEvent){
+    fParticleSelected.Delete();
+    return;
+  }
+  AliStack *pStack = fMCEvent->Stack();
+
+  Int_t nprim = pStack->GetNprimary();
+
+  for(int i = 0;i < pStack->GetNtrack();++i){
+    if(i<nprim){
+      SelectParticle(i);// take all primaries
+      continue;
+    }
+    if(!IsParticleSelected(i))continue;
+    TParticle *part = pStack->Particle(i);
+    Int_t imo = part->GetFirstMother();
+    while((imo >= nprim)&&!IsParticleSelected(imo)){
+      // Mother not yet selected
+      SelectParticle(imo);
+      TParticle *mother = pStack->Particle(imo);
+      imo = mother->GetFirstMother();
+    }
+    // after last step we may have a unselected primary
+    // mother
+    if(imo>=0){
+      if(!IsParticleSelected(imo))
+       SelectParticle(imo);
+    } 
+  }// loop over all tracks
 }
 
 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
 {
     // Retrieve entry i
-    return (fMCEvent->GetParticleAndTR(i, particle, trefs));
+    if (!fInitOk) {
+       return 0;
+    } else {
+       return (fMCEvent->GetParticleAndTR(i, particle, trefs));
+    }
 }
 
 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
@@ -227,31 +357,40 @@ Bool_t AliMCEventHandler::Notify(const char *path)
     if(fileName.Contains("AliESDs.root")){
        fileName.ReplaceAll("AliESDs.root", "");
     }
+    else if(fileName.Contains("AliAOD.root")){
+       fileName.ReplaceAll("AliAOD.root", "");
+    }
     else if(fileName.Contains("galice.root")){
        // for running with galice and kinematics alone...
        fileName.ReplaceAll("galice.root", "");
     }
-    
+    else if (fileName.BeginsWith("root:")) {
+      fileName.Append("?ZIP=");
+    }
     *fPathName = fileName;
-    printf("AliMCEventHandler::Notify() Path: %s\n", fPathName->Data());
+    AliInfo(Form("Notify() Path: %s\n", fPathName->Data()));
     
     ResetIO();
     InitIO("");
+
     return kTRUE;
 }
-    
+
 void AliMCEventHandler::ResetIO()
 {
 //  Clear header and stack
-    fMCEvent->Clean();
     
-// Delete Tree E    
-    delete fTreeE; fTreeE = 0;
+    if (fInitOk) fMCEvent->Clean();
     
+// Delete Tree E
+    delete fTreeE; fTreeE = 0;
+     
 // Reset files
-    if (fFileE)  delete fFileE;
-    if (fFileK)  delete fFileK;
-    if (fFileTR) delete fFileTR;
+    if (fFileE)  {delete fFileE;  fFileE  = 0;}
+    if (fFileK)  {delete fFileK;  fFileK  = 0;}
+    if (fFileTR) {delete fFileTR; fFileTR = 0;}
+    fExtension="";
+    fInitOk = kFALSE;
 }
 
                            
@@ -260,7 +399,7 @@ Bool_t AliMCEventHandler::FinishEvent()
     // Clean-up after each event
     delete fDirTR;  fDirTR = 0;
     delete fDirK;   fDirK  = 0;    
-    fMCEvent->FinishEvent();
+    if (fInitOk) fMCEvent->FinishEvent();
     return kTRUE;
 }
 
@@ -277,7 +416,7 @@ Bool_t AliMCEventHandler::TerminateIO()
 }
     
 
-void AliMCEventHandler::SetInputPath(char* fname)
+void AliMCEventHandler::SetInputPath(const char* fname)
 {
     // Set the input path name
     delete fPathName;