Fix for coverity (AdC)
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
index 891ebf8..0ed3455 100644 (file)
 
 #include <Riostream.h>
 
+#include "AliLog.h"
 #include "AliGenExtFile.h"
 #include "AliRunLoader.h"
 #include "AliHeader.h"
+#include "AliStack.h"
 #include "AliGenEventHeader.h"
+#include "AliGenReader.h"
 
 #include <TParticle.h>
 #include <TFile.h>
 #include <TTree.h>
 
 
+using std::cout;
+using std::endl;
 ClassImp(AliGenExtFile)
 
 AliGenExtFile::AliGenExtFile()
     :AliGenMC(),
      fFileName(0),
-     fReader(0)
+     fReader(0),
+     fStartEvent(0)
 {
 //  Constructor
 //
 //  Read all particles
-    fNpart  =- 1;
+    fNpart  = -1;
 }
 
 AliGenExtFile::AliGenExtFile(Int_t npart)
     :AliGenMC(npart),
      fFileName(0),
-     fReader(0)
+     fReader(0),
+     fStartEvent(0)
 {
 //  Constructor
     fName   = "ExtFile";
@@ -78,21 +85,38 @@ void AliGenExtFile::Init()
     if (fReader) fReader->Init();
 }
 
-    
+//___________________________________________________________
 void AliGenExtFile::Generate()
 {
 // Generate particles
 
-  Float_t polar[3]  = {0,0,0};
+  Double_t polar[3]  = {0,0,0};
   //
-  Float_t origin[3] = {0,0,0};
-  Float_t p[3];
+  Double_t origin[3] = {0,0,0};
+  Double_t time = 0.;
+  Double_t p[4];
   Float_t random[6];
   Int_t i = 0, j, nt;
   //
   //
   if (fVertexSmear == kPerEvent) Vertex();
 
+  // Fast forward up to start Event
+  for (Int_t ie=0; ie<fStartEvent; ++ie ) {
+    Int_t nTracks = fReader->NextEvent();      
+    if (nTracks == 0) {
+      // printf("\n No more events !!! !\n");
+      Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
+      return;
+    }
+    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();      
     if (nTracks == 0) {
@@ -133,15 +157,34 @@ 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* iparticle = fReader->NextParticle();
-       Bool_t selected = KinematicSelection(iparticle,0); 
-       if (!selected) continue;
-       p[0] = iparticle->Px();
-       p[1] = iparticle->Py();
-       p[2] = iparticle->Pz();
-       Int_t idpart = iparticle->GetPdgCode();
+       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();
+       p[3] = jparticle->Energy();
+       
+       Int_t idpart = jparticle->GetPdgCode();
        if(fVertexSmear==kPerTrack) 
        {
            Rndm(random,6);
@@ -150,26 +193,33 @@ void AliGenExtFile::Generate()
                    fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
                    TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
            }
+           Rndm(random,2);
+           time = fTimeOrigin + fOsigma[2]/TMath::Ccgs()*
+             TMath::Cos(2*random[0]*TMath::Pi())*
+             TMath::Sqrt(-2*TMath::Log(random[1]));
        } else {
-           origin[0] = fVertex[0] + iparticle->Vx();
-           origin[1] = fVertex[1] + iparticle->Vy();
-           origin[2] = fVertex[2] + iparticle->Vz();
+           origin[0] = fVertex[0] + jparticle->Vx();
+           origin[1] = fVertex[1] + jparticle->Vy();
+           origin[2] = fVertex[2] + jparticle->Vz();
+           time = fTime + jparticle->T();
        }
+       Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
        
-       Int_t decayed    = iparticle->GetFirstDaughter();
-       Int_t doTracking = fTrackIt && (decayed < 0) && (TMath::Abs(idpart) > 10) && selected;
-       Int_t parent     = iparticle->GetFirstMother();
-       
-       PushTrack(doTracking,parent,idpart,p,origin,polar,0,kPPrimary,nt);
+       PushTrack(doTracking, parent, idpart,
+                 p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
+                 polar[0], polar[1], polar[2],
+                 kPPrimary, nt, 1., jparticle->GetStatusCode());
+
        KeepTrack(nt);
        fNprimaries++;
     } // 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);
     AddHeader(header);
     break;
     
@@ -179,10 +229,11 @@ void AliGenExtFile::Generate()
   CdEventFile();
 }
 
+//___________________________________________________________
 void AliGenExtFile::CdEventFile()
 {
 // CD back to the event file
-    (AliRunLoader::GetRunLoader())->CdGAFile();
+  AliRunLoader::Instance()->CdGAFile();
 }