]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliMCEventHandler.cxx
put STEERBase into the include path
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
index 8f204e0ff83fb922337946e56e3cdc115ef7b8b8..0f1fdb8d45b79372768978fc3dce2fa4fa64a375 100644 (file)
@@ -29,7 +29,6 @@
 #include "AliTrackReference.h"
 #include "AliHeader.h"
 #include "AliStack.h"
-#include "AliAnalysisManager.h"
 
 #include <TTree.h>
 #include <TFile.h>
@@ -44,7 +43,7 @@
 ClassImp(AliMCEventHandler)
 
 AliMCEventHandler::AliMCEventHandler() :
-    AliVirtualEventHandler(),
+    AliVEventHandler(),
     fFileE(0),
     fFileK(0),
     fFileTR(0),
@@ -60,13 +59,16 @@ AliMCEventHandler::AliMCEventHandler() :
     fEvent(-1),
     fNprimaries(-1),
     fNparticles(-1),
-    fPathName("./")
+    fPathName("./"),
+    fExtension(""),
+    fFileNumber(0),
+    fEventsPerFile(0)
 {
     // Default constructor
 }
 
 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
-    AliVirtualEventHandler(name, title),
+    AliVEventHandler(name, title),
     fFileE(0),
     fFileK(0),
     fFileTR(0),
@@ -82,7 +84,10 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
     fEvent(-1),
     fNprimaries(-1),
     fNparticles(-1),
-    fPathName("./")
+    fPathName("./"),
+    fExtension(""),
+    fFileNumber(0),
+    fEventsPerFile(0)
 {
     // Constructor
 }
@@ -99,41 +104,53 @@ Bool_t AliMCEventHandler::InitIO(Option_t* /*opt*/)
     // Initialize input
     //
     fFileE = new TFile(Form("%sgalice.root", fPathName));
-    if (!fFileE) printf("galice.root not found in directory %s ! \n", fPathName);
+    if (!fFileE) printf("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName);
 
     fFileE->GetObject("TE", fTreeE);
     fTreeE->SetBranchAddress("Header", &fHeader);
     fNEvent = fTreeE->GetEntries();
     //
     // Tree K
-    fFileK = new TFile(Form("%sKinematics.root", fPathName));
-    if (!fFileK) printf("Kinematics.root not found in directory %s ! \n", fPathName);
+    fFileK = new TFile(Form("%sKinematics%s.root", fPathName, fExtension));
+    if (!fFileK) printf("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName);
+    fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
     //
     // Tree TR
-    fFileTR = new TFile(Form("%sTrackRefs.root", fPathName));
-    if (!fFileTR) printf("TrackRefs.root not found in directory %s ! \n", fPathName);
+    fFileTR = new TFile(Form("%sTrackRefs%s.root", fPathName, fExtension));
+    if (!fFileTR) printf("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName);
     //
     // Reset the event number
     fEvent = -1;
-    printf("Number of events in this directory %5d \n", fNEvent);
+    fFileNumber = 0;
+    
+    printf("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent);
     return kTRUE;
     
 }
 
-Bool_t AliMCEventHandler::BeginEvent()
-{ 
-    // Read the next event
-    printf("AliMCEventHandler::BeginEvent called \n");
+Bool_t AliMCEventHandler::GetEvent(Int_t iev)
+{
+    // Load the event number iev
+    Int_t inew  = iev/fEventsPerFile;
+    if (inew != fFileNumber) {
+       fFileNumber = inew;
+       if (!OpenFile(fFileNumber)){
+           return kFALSE;
+       }
+    }
     
-    fEvent++;
     char folder[20];
-    sprintf(folder, "Event%d", fEvent);
+    sprintf(folder, "Event%d", iev);
     // TreeE
-    fTreeE->GetEntry(fEvent);
+    fTreeE->GetEntry(iev);
     fStack = fHeader->Stack();
     // Tree K
     TDirectoryFile* dirK  = 0;
     fFileK->GetObject(folder, dirK);
+    if (!dirK) {
+       printf("AliMCEventHandler: Event #%5d not found\n", iev);
+       return kFALSE;
+    }
     dirK->GetObject("TreeK", fTreeK);
     fStack->ConnectTree(fTreeK);
     fStack->GetEvent();
@@ -152,9 +169,49 @@ Bool_t AliMCEventHandler::BeginEvent()
     //
     fNparticles = fStack->GetNtrack();
     fNprimaries = fStack->GetNprimary();
-    printf("Event#: %5d Number of particles %5d \n", fEvent, fNparticles);
+    printf("AliMCEventHandler: Event#: %5d Number of particles %5d \n", fEvent, fNparticles);
     
     return kTRUE;
+
+}
+
+Bool_t AliMCEventHandler::OpenFile(Int_t i)
+{
+    // Open file i
+    Bool_t ok = kTRUE;
+    if (i > 0) {
+       fExtension = Form("%d", i);
+    } else {
+       fExtension = "";
+    }
+    
+    
+    delete fFileK;
+    fFileK = new TFile(Form("%sKinematics%s.root", fPathName, fExtension));
+    if (!fFileK) {
+       printf("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName);
+       ok = kFALSE;
+    }
+    
+    delete fFileTR;
+    fFileTR = new TFile(Form("%sTrackRefs%s.root", fPathName, fExtension));
+    if (!fFileTR) {
+       printf("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName);
+       ok = kFALSE;
+    }
+    
+    return ok;
+}
+
+Bool_t AliMCEventHandler::BeginEvent()
+{ 
+    // Read the next event
+    fEvent++;
+    if (fEvent >= fNEvent) {
+       printf("AliMCEventHandler: Event number out of range %5d\n", fEvent);
+       return kFALSE;
+    }
+    return GetEvent(fEvent);
 }
 
 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
@@ -167,13 +224,10 @@ Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClones
     }
     particle = fStack->Particle(i);
     trefs    = fTrackReferences;
-    printf("Returning %5d \n", particle->GetPdgCode());
-    
     return trefs->GetEntries();
-    
 }
 
-void AliMCEventHandler::DrawCheck(Int_t i)
+void AliMCEventHandler::DrawCheck(Int_t i, Bool_t search)
 {
     // Retrieve entry i and draw momentum vector and hits
     if (i > -1 && i < fNparticles) {
@@ -182,9 +236,19 @@ void AliMCEventHandler::DrawCheck(Int_t i)
        printf("AliMCEventHandler::GetEntry: Index out of range");
     }
 
-    TParticle* particle = fStack->Particle(i);
-    Int_t nh =  fTrackReferences->GetEntries();
+    Int_t nh = fTrackReferences->GetEntries();
+    
     
+    if (search) {
+       while(nh == 0 && i < fNparticles - 1) {
+           i++;
+           fTreeTR->GetEntry(fStack->TreeKEntry(i));
+           nh =  fTrackReferences->GetEntries();
+       }
+       printf("Found Hits at %5d\n", i);
+    }
+    TParticle* particle = fStack->Particle(i);
+
     TH2F*    h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
     Float_t x0 = particle->Vx();
     Float_t y0 = particle->Vy();
@@ -207,15 +271,14 @@ void AliMCEventHandler::DrawCheck(Int_t i)
     }
 }
 
-Bool_t AliMCEventHandler::Notify()
+Bool_t AliMCEventHandler::Notify(const char *path)
 {
     // Notify about directory change
-    TTree* tree = AliAnalysisManager::GetAnalysisManager()->GetTree();
-    TFile *curfile = tree->GetCurrentFile();
-    TString fileName(curfile->GetName());
-    fileName.ReplaceAll("AliESDs.root", "");
-    printf("AliMCEventHandler::Notify() file: %s\n", fileName.Data());
-    fPathName = Form("%s",  fileName.Data());
+    // The directory is taken from the 'path' argument
+    // Reconnect trees
+
+    printf("AliMCEventHandler::Notify() file: %s\n", path);
+    fPathName = Form("%s",  path);
     ResetIO();
     InitIO("");
     return kTRUE;
@@ -223,6 +286,7 @@ Bool_t AliMCEventHandler::Notify()
     
 void AliMCEventHandler::ResetIO()
 {
+    // Reset files
     if (fFileE)  delete fFileE;
     if (fFileK)  delete fFileK;
     if (fFileTR) delete fFileTR;
@@ -291,11 +355,11 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
 //     printf("Particle %5d %5d %5d %5d %5d %5d \n", 
 //            ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(), 
 //            part->GetLastDaughter(), part->TestBit(kTransportBit));
-       
-       // Skip primaries that have not been transported
+
+       // Determine range of secondaries produced by this primary during transport     
        Int_t dau1  = part->GetFirstDaughter();
+       if (dau1 < np) continue;  // This particle has no secondaries produced during transport
        Int_t dau2  = -1;
-       // Determine range of secondaries produced by this primary
        if (dau1 > -1) {
            Int_t inext = ip - 1;
            while (dau2 < 0) {
@@ -313,16 +377,18 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
                inext--;
            } // find upper bound
        }  // dau2 < 0
+       
 
 //     printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
-       
-       // 
-       // Loop over reference hits and find secondary label
+//
+// Loop over reference hits and find secondary label
+// First the tricky part: find the entry in treeTR than contains the hits or
+// make sure that no hits exist.
+//
        Bool_t hasHits   = kFALSE;
        Bool_t isOutside = kFALSE;
+
        it = itlast;
-       if (dau1 < np) continue;
-       
        while (!hasHits && !isOutside && it < nt) {
            fTreeTR->GetEntry(it++);
            for (Int_t ib = 0; ib < 7; ib++) {
@@ -334,21 +400,26 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
                    if (label >= dau1 && label <= dau2) {
                        hasHits = kTRUE;
                        itlast = it - 1;
+                       break;
                    }
                    if (label > dau2 || label < ip) {
                        isOutside = kTRUE;
                        itlast = it - 1;
+                       break;
                    }
                } // hits
+               if (hasHits || isOutside) break;
            } // branches
        } // entries
 
        if (!hasHits) {
+           // Write empty entries
            for (Int_t id = dau1; (id <= dau2); id++) {
                fTmpTreeTR->Fill();
                ifills++;
            } 
        } else {
+           // Collect all hits
            fTreeTR->GetEntry(itlast);
            for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
                for (Int_t ib = 0; ib < 7; ib++) {
@@ -360,7 +431,8 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
                        // Skip primaries
                        if (label == ip) continue;
                        if (label > dau2 || label < dau1) 
-                           printf("Track Reference Label out of range !: %5d %5d %5d %5d \n", itlast, label, dau1, dau2);
+                           printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n", 
+                                  itlast, label, dau1, dau2);
                        if (label == id) {
                            // secondary found
                            tr->SetDetectorId(ib-1);
@@ -376,9 +448,9 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
            } // daughters
        } // has hits
     } // tracks
-    printf("Secondaries %5d %5d \n", ifills, fStack->GetNtrack() - fStack->GetNprimary());
     //
     // Now loop again and write the primaries
+    //
     it = nt - 1;
     for (Int_t ip = 0; ip < np; ip++) {
        Int_t labmax = -1;
@@ -411,9 +483,8 @@ void AliMCEventHandler::ReorderAndExpandTreeTR()
        ifills++;
     } // tracks
     // Check
-    printf("All %5d %5d \n", ifills, fStack->GetNtrack());
     if (ifills != fStack->GetNtrack()) 
-       printf("Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", ifills, fStack->GetNtrack());
+       printf("AliMCEventHandler:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n", 
+              ifills, fStack->GetNtrack());
     fTreeTR = fTmpTreeTR;
-    
 }