]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliMCEventHandler.cxx
Coverity fixes.
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
index a4a9824584a8d187a187b5ea2ea216543439a500..8c2a21b55b43cba2e267457a34b7bd231ae5506b 100644 (file)
@@ -1,4 +1,5 @@
-/************************************************************************* * 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.              *
@@ -26,6 +27,7 @@
 
 #include "AliMCEventHandler.h"
 #include "AliMCEvent.h"
+#include "AliMCParticle.h"
 #include "AliPDG.h"
 #include "AliTrackReference.h"
 #include "AliHeader.h"
@@ -34,6 +36,7 @@
 
 #include <TTree.h>
 #include <TFile.h>
+#include <TList.h>
 #include <TParticle.h>
 #include <TString.h>
 #include <TClonesArray.h>
@@ -61,7 +64,10 @@ AliMCEventHandler::AliMCEventHandler() :
     fFileNumber(0),
     fEventsPerFile(0),
     fReadTR(kTRUE),
-    fInitOk(kFALSE)
+    fInitOk(kFALSE),
+    fSubsidiaryHandlers(0),
+    fEventsInContainer(0),
+    fPreReadMode(kNoPreRead)
 {
   //
   // Default constructor
@@ -90,7 +96,10 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
     fFileNumber(0),
     fEventsPerFile(0),
     fReadTR(kTRUE),
-    fInitOk(kFALSE)
+    fInitOk(kFALSE),
+    fSubsidiaryHandlers(0),
+    fEventsInContainer(0),
+    fPreReadMode(kNoPreRead)
 {
   //
   // Constructor
@@ -101,6 +110,7 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
 AliMCEventHandler::~AliMCEventHandler()
 { 
     // Destructor
+  delete fPathName;
     delete fMCEvent;
     delete fFileE;
     delete fFileK;
@@ -130,7 +140,7 @@ Bool_t AliMCEventHandler::Init(Option_t* opt)
     // Tree K
     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
     if (!fFileK) {
-       AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
+       AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName->Data()));
        fInitOk = kFALSE;
        return kTRUE;
     }
@@ -152,6 +162,17 @@ Bool_t AliMCEventHandler::Init(Option_t* opt)
     fFileNumber =  0;
     AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
     fInitOk = kTRUE;
+
+
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*)next())) {
+           handler->Init(opt);
+           handler->SetNumberOfEventsInContainer(fNEvent);
+       }
+    }
+
     return kTRUE;
 }
 
@@ -160,41 +181,55 @@ Bool_t AliMCEventHandler::GetEvent(Int_t iev)
     // Load the event number iev
     //
     // Calculate the file number
-    if (!fInitOk) return kFALSE;
+  if (!fInitOk) return kFALSE;
     
-    Int_t inew  = iev / fEventsPerFile;
-    if (inew != fFileNumber) {
-       fFileNumber = inew;
-       if (!OpenFile(fFileNumber)){
-           return kFALSE;
-       }
-    }
-    // Folder name
-    char folder[20];
-    sprintf(folder, "Event%d", iev);
-    // TreeE
-    fTreeE->GetEntry(iev);
-    // Tree K
-    fFileK->GetObject(folder, fDirK);
-    if (!fDirK) {
-       AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
-       return kFALSE;
+  Int_t inew  = iev / fEventsPerFile;
+  if (inew != fFileNumber) {
+    fFileNumber = inew;
+    if (!OpenFile(fFileNumber)){
+      return kFALSE;
     }
+  }
+  // Folder name
+  char folder[20];
+  snprintf(folder, 20, "Event%d", iev);
+  // TreeE
+  fTreeE->GetEntry(iev);
+  // Tree K
+  fFileK->GetObject(folder, fDirK);
+  if (!fDirK) {
+    AliWarning(Form("AliMCEventHandler: Event #%5d - Cannot get kinematics\n", iev));
+    return kFALSE;
+  }
     
-    fDirK ->GetObject("TreeK", fTreeK);
-    // Connect TreeK to MCEvent
-    fMCEvent->ConnectTreeK(fTreeK);
-    //Tree TR 
-    if (fFileTR) {
-       // Check which format has been read
-       fFileTR->GetObject(folder, fDirTR);
-       fDirTR->GetObject("TreeTR", fTreeTR);
-       //
-       // Connect TR to MCEvent
-       fMCEvent->ConnectTreeTR(fTreeTR);
-    }
+  fDirK ->GetObject("TreeK", fTreeK);
+  if (!fTreeK) {
+    AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeK\n",iev));
+    return kFALSE;
+  }  
+  // Connect TreeK to MCEvent
+  fMCEvent->ConnectTreeK(fTreeK);
+  //Tree TR 
+  if (fFileTR) {
+    // Check which format has been read
+    fFileTR->GetObject(folder, fDirTR);
+    if (!fDirTR) {
+      AliError(Form("AliMCEventHandler: Event #%5d - Cannot get track references\n",iev));
+      return kFALSE;
+    }  
+     
+    fDirTR->GetObject("TreeTR", fTreeTR);
     //
-    return kTRUE;
+    if (!fTreeTR) {
+      AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeTR\n",iev));
+      return kFALSE;
+    }  
+    // Connect TR to MCEvent
+    fMCEvent->ConnectTreeTR(fTreeTR);
+  }
+
+  //
+  return kTRUE;
 }
 
 Bool_t AliMCEventHandler::OpenFile(Int_t i)
@@ -226,6 +261,7 @@ Bool_t AliMCEventHandler::OpenFile(Int_t i)
     }
     
     fInitOk = kTRUE;
+
     return kTRUE;
 }
 
@@ -234,6 +270,12 @@ Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
     fParticleSelected.Delete();
     fLabelMap.Delete();
     // Read the next event
+
+    if (fEventsInContainer != 0) {
+       entry = (Long64_t) ( entry * Float_t(fNEvent) / Float_t (fEventsInContainer));
+    }
+
+
     if (entry == -1) {
        fEvent++;
        entry = fEvent;
@@ -242,17 +284,39 @@ Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
     }
 
     if (entry >= fNEvent) {
-       AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
+       AliWarning(Form("AliMCEventHandler: Event number out of range %5lld %5d\n", entry, fNEvent));
        return kFALSE;
     }
-    return GetEvent(entry);
+    
+    Bool_t result = GetEvent(entry);
+
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*)next())) {
+           handler->BeginEvent(entry);
+       }
+       next.Reset();
+       while((handler = (AliMCEventHandler*)next())) {
+           fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
+       }
+       fMCEvent->InitEvent();
+    }
+    
+    if (fPreReadMode == kLmPreRead) {
+       fMCEvent->PreReadAll();
+    }
+
+    return result;
+    
 }
 
 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);
+    if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i =  fMCEvent->BgLabelToIndex(TMath::Abs(i));
+    if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
 }
 
 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i)  {
@@ -276,10 +340,9 @@ void AliMCEventHandler::CreateLabelMap(){
   }
 
   VerifySelectedParticles();
-  AliStack *pStack = fMCEvent->Stack();
 
   Int_t iNew = 0;
-  for(int i = 0;i < pStack->GetNtrack();++i){
+  for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
     if(IsParticleSelected(i)){
       fLabelMap.Add(i,iNew);
       iNew++;
@@ -288,7 +351,7 @@ void AliMCEventHandler::CreateLabelMap(){
 }
 
 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
-  // Gets the labe from the new created Map
+  // Gets the label from the new created Map
   // Call CreatLabelMap before
   // otherwise only 0 returned
   return fLabelMap.GetValue(TMath::Abs(i));
@@ -302,28 +365,29 @@ void  AliMCEventHandler::VerifySelectedParticles(){
   // Private, should be only called by CreateLabelMap
 
   if(!fMCEvent){
-    fParticleSelected.Delete();
-    return;
+      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
+  Int_t nprim = fMCEvent->GetNumberOfPrimaries();
+
+  for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
+      if(i < nprim){
+         SelectParticle(i);// take all primaries
+         continue;
+      }
+
+      if(!IsParticleSelected(i))continue;
+
+      AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(i);
+      Int_t imo = mcpart->GetMother();
+      while((imo >= nprim)&&!IsParticleSelected(imo)){
+         // Mother not yet selected
+         SelectParticle(imo);
+         mcpart = (AliMCParticle*) fMCEvent->GetTrack(imo);
+         imo = mcpart->GetMother();
+      }
+    // after last step we may have an unselected primary
     // mother
     if(imo>=0){
       if(!IsParticleSelected(imo))
@@ -367,12 +431,29 @@ Bool_t AliMCEventHandler::Notify(const char *path)
     else if (fileName.BeginsWith("root:")) {
       fileName.Append("?ZIP=");
     }
+
     *fPathName = fileName;
-    AliInfo(Form("Notify() Path: %s\n", fPathName->Data()));
+    AliInfo(Form("Path: -%s-\n", fPathName->Data()));
     
     ResetIO();
     InitIO("");
 
+// Handle subsidiary handlers
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*) next())) {
+           TString* spath = handler->GetInputPath();
+           if (spath->Contains("merged")) {
+               if (! fPathName->IsNull()) {
+                   handler->Notify(Form("%s/../.", fPathName->Data()));
+               } else {
+                   handler->Notify("../");
+               }
+           }
+       }
+    }
+    
     return kTRUE;
 }
 
@@ -391,6 +472,15 @@ void AliMCEventHandler::ResetIO()
     if (fFileTR) {delete fFileTR; fFileTR = 0;}
     fExtension="";
     fInitOk = kFALSE;
+
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*)next())) {
+           handler->ResetIO();
+       }
+    }
+
 }
 
                            
@@ -400,6 +490,15 @@ Bool_t AliMCEventHandler::FinishEvent()
     delete fDirTR;  fDirTR = 0;
     delete fDirK;   fDirK  = 0;    
     if (fInitOk) fMCEvent->FinishEvent();
+
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*)next())) {
+           handler->FinishEvent();
+       }
+    }
+
     return kTRUE;
 }
 
@@ -422,3 +521,11 @@ void AliMCEventHandler::SetInputPath(const char* fname)
     delete fPathName;
     fPathName = new TString(fname);
 }
+
+void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
+{
+    // Add a subsidiary handler. For example for background events
+
+    if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
+    fSubsidiaryHandlers->Add(handler);
+}