Fix for coverity (AdC)
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
index b1d0a38..0ed3455 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <Riostream.h>
 
+#include "AliLog.h"
 #include "AliGenExtFile.h"
 #include "AliRunLoader.h"
 #include "AliHeader.h"
@@ -43,6 +44,8 @@
 #include <TTree.h>
 
 
+using std::cout;
+using std::endl;
 ClassImp(AliGenExtFile)
 
 AliGenExtFile::AliGenExtFile()
@@ -106,10 +109,13 @@ void AliGenExtFile::Generate()
       Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
       return;
     }
-    TParticle* iparticle = 0x0;
-    while ((iparticle=fReader->NextParticle()) ) ;
+    for (Int_t i=0; i<nTracks; ++i) {
+      if (NULL == fReader->NextParticle())
+       AliFatal("Error while skipping tracks");
+    }
     cout << "Skipping event " << ie << endl;
   }  
+  fStartEvent = 0; // do not skip events the second time 
 
   while(1) {
     Int_t nTracks = fReader->NextEvent();      
@@ -151,11 +157,28 @@ void AliGenExtFile::Generate()
     //
     // Stack filling loop
     //
+    map<Int_t,Int_t> newIdMap;
+    map<Int_t,Int_t> newFirstMotherMap;
     fNprimaries = 0;
     for (i = 0; i < nTracks; i++) {
-       TParticle* jparticle = fReader->NextParticle();
-       Bool_t selected = KinematicSelection(jparticle,0); 
-       if (!selected) continue;
+       TParticle* jparticle = fReader->NextParticle();
+       Int_t parent = jparticle->GetFirstMother();
+       if (parent > -1) {
+          Int_t parentNewId = newIdMap[parent];
+          if (parentNewId == -1) { // if parent was skipped
+             parent = newFirstMotherMap[parent]; // re-adjust parent using stored information
+          } else {
+             parent = parentNewId; // otherwise re-map parent to new id
+          }
+       }
+       newFirstMotherMap[i] = parent; // store re-mapped parent data so child nodes can use it
+       Bool_t selected = KinematicSelection(jparticle,0);
+       if (!selected) {
+          newIdMap[i] = -1; // -1 marks this particle was skipped
+          continue;
+       }
+       newIdMap[i] = fNprimaries; // maps old id to new id
+
        p[0] = jparticle->Px();
        p[1] = jparticle->Py();
        p[2] = jparticle->Pz();
@@ -181,7 +204,6 @@ void AliGenExtFile::Generate()
            time = fTime + jparticle->T();
        }
        Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
-       Int_t parent     = jparticle->GetFirstMother();
        
        PushTrack(doTracking, parent, idpart,
                  p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
@@ -193,8 +215,8 @@ void AliGenExtFile::Generate()
     } // track loop
 
     // Generated event header
-    
-    AliGenEventHeader * header = new AliGenEventHeader();
+    AliGenEventHeader * header = fReader->GetGenEventHeader();
+    if (!header) header = new AliGenEventHeader();
     header->SetNProduced(fNprimaries);
     header->SetPrimaryVertex(fVertex);
     header->SetInteractionTime(fTime);