]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliMCEventHandler.cxx
Additional cuts for leading particle alignment in event mixing. (Alessandro Grelli)
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
index 209e93289657d47c29d564b388cdd50951d60614..ca1855e9cabb2b830858ffa5d58e8d5dbff28046 100644 (file)
@@ -1,5 +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.              *
@@ -27,6 +27,7 @@
 
 #include "AliMCEventHandler.h"
 #include "AliMCEvent.h"
+#include "AliMCParticle.h"
 #include "AliPDG.h"
 #include "AliTrackReference.h"
 #include "AliHeader.h"
@@ -35,6 +36,7 @@
 
 #include <TTree.h>
 #include <TFile.h>
+#include <TList.h>
 #include <TParticle.h>
 #include <TString.h>
 #include <TClonesArray.h>
@@ -53,6 +55,8 @@ AliMCEventHandler::AliMCEventHandler() :
     fTreeTR(0),
     fDirK(0),
     fDirTR(0),
+    fParticleSelected(0),
+    fLabelMap(0),
     fNEvent(-1),
     fEvent(-1),
     fPathName(new TString("./")),
@@ -60,7 +64,9 @@ AliMCEventHandler::AliMCEventHandler() :
     fFileNumber(0),
     fEventsPerFile(0),
     fReadTR(kTRUE),
-    fInitOk(kFALSE)
+    fInitOk(kFALSE),
+    fSubsidiaryHandlers(0),
+    fEventsInContainer(0)
 {
   //
   // Default constructor
@@ -80,6 +86,8 @@ 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("./")),
@@ -87,7 +95,9 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
     fFileNumber(0),
     fEventsPerFile(0),
     fReadTR(kTRUE),
-    fInitOk(kFALSE)
+    fInitOk(kFALSE),
+    fSubsidiaryHandlers(0),
+    fEventsInContainer(0)
 {
   //
   // Constructor
@@ -129,7 +139,7 @@ Bool_t AliMCEventHandler::Init(Option_t* opt)
     if (!fFileK) {
        AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
        fInitOk = kFALSE;
-       return kFALSE;
+       return kTRUE;
     }
     
     fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
@@ -140,7 +150,7 @@ Bool_t AliMCEventHandler::Init(Option_t* opt)
        if (!fFileTR) {
            AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
            fInitOk = kFALSE;
-           return kFALSE;
+           return kTRUE;
        }
     }
     //
@@ -149,6 +159,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;
 }
 
@@ -177,6 +198,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);
@@ -189,6 +211,7 @@ Bool_t AliMCEventHandler::GetEvent(Int_t iev)
        // Connect TR to MCEvent
        fMCEvent->ConnectTreeTR(fTreeTR);
     }
+
     //
     return kTRUE;
 }
@@ -222,12 +245,21 @@ Bool_t AliMCEventHandler::OpenFile(Int_t i)
     }
     
     fInitOk = kTRUE;
+
     return kTRUE;
 }
 
 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;
@@ -236,10 +268,111 @@ 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 %5d %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();
+    }
+    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 (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)  {
+  // 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();
+
+  Int_t iNew = 0;
+  for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
+    if(IsParticleSelected(i)){
+      fLabelMap.Add(i,iNew);
+      iNew++;
+    }
+  }
+}
+
+Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
+  // Gets the label 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;
+  }
+
+  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))
+       SelectParticle(imo);
+    } 
+  }// loop over all tracks
 }
 
 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
@@ -274,30 +407,59 @@ Bool_t AliMCEventHandler::Notify(const char *path)
        // for running with galice and kinematics alone...
        fileName.ReplaceAll("galice.root", "");
     }
-    
+    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;
 }
 
 void AliMCEventHandler::ResetIO()
 {
 //  Clear header and stack
+    
     if (fInitOk) fMCEvent->Clean();
     
 // Delete Tree E
     delete fTreeE; fTreeE = 0;
-
+     
 // Reset files
     if (fFileE)  {delete fFileE;  fFileE  = 0;}
     if (fFileK)  {delete fFileK;  fFileK  = 0;}
     if (fFileTR) {delete fFileTR; fFileTR = 0;}
     fExtension="";
     fInitOk = kFALSE;
+
+    if (fSubsidiaryHandlers) {
+       TIter next(fSubsidiaryHandlers);
+       AliMCEventHandler *handler;
+       while((handler = (AliMCEventHandler*)next())) {
+           handler->ResetIO();
+       }
+    }
+
 }
 
                            
@@ -307,6 +469,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;
 }
 
@@ -329,3 +500,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);
+}