]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EVGEN/AliGenExtFile.cxx
Problem in memory management fixed
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
index a6ef774c71fbbff37fd2e9f9d0ac06c5b63d920f..7f89bbaadd1adbf6013bf623a5efc60083815a1d 100644 (file)
 
 /*
 $Log$
+Revision 1.24  2002/10/21 09:20:51  alibrary
+Introduce Riostream.h and remove unused variables
+
+Revision 1.23  2002/10/14 14:55:35  hristov
+Merging the VirtualMC branch to the main development branch (HEAD)
+
+Revision 1.20.4.1  2002/06/10 14:57:41  hristov
+Merged with v3-08-02
+
+Revision 1.22  2002/05/15 11:59:49  morsch
+CdEventFile() method added.
+
+Revision 1.21  2002/04/26 10:39:31  morsch
+AliGenExtFile derives from AliGenMC. Generate() uses methods from AliGenMC (N. Carrer)
+
+Revision 1.20  2002/03/22 09:43:28  morsch
+Don't delete the TParticle.
+
+Revision 1.19  2002/02/08 16:50:50  morsch
+Add name and title in constructor.
+
+Revision 1.18  2001/11/12 14:31:00  morsch
+Memory leaks fixed. (M. Bondila)
+
+Revision 1.17  2001/11/09 09:12:58  morsch
+Generalization by using AliGenReader object to read particles from file.
+
+Revision 1.16  2001/07/27 17:09:35  morsch
+Use local SetTrack, KeepTrack and SetHighWaterMark methods
+to delegate either to local stack or to stack owned by AliRun.
+(Piotr Skowronski, A.M.)
+
+Revision 1.15  2001/01/23 13:29:37  morsch
+Add method SetParticleCode and enum type Code_t to handle both PDG (new ntuples)
+and GEANT3 codes (old ntuples) in input file.
+
+Revision 1.14  2000/12/21 16:24:06  morsch
+Coding convention clean-up
+
 Revision 1.13  2000/11/30 07:12:50  alibrary
 Introducing new Rndm and QA classes
 
@@ -45,48 +84,38 @@ Introduction of the Copyright and cvs Log
 */
 
 
-// Event generator that can read the old ALICE event format based on CW-ntuples
-// http://consult.cern.ch/alice/Internal_Notes/1995/32/abstract
-// .cwn file have to be converted to .root using h2root
-// Use SetFileName(file) to read from "file" 
-// Author: andreas.morsch@cern.ch
+// Event generator that using an instance of type AliGenReader
+// reads particles from a file and applies cuts. 
 
-#include <iostream.h>
+#include <Riostream.h>
 
 #include "AliGenExtFile.h"
-#include "AliMC.h"
 #include "AliRun.h"
 
-#include <TDirectory.h>
-#include <TDatabasePDG.h>
+#include <TParticle.h>
 #include <TFile.h>
-#include "TTree.h"
-#include <stdlib.h>
+#include <TTree.h>
+
 
  ClassImp(AliGenExtFile)
-     AliGenExtFile::AliGenExtFile()
-        :AliGenerator(-1)
+
+AliGenExtFile::AliGenExtFile()
+  :AliGenMC()
 {
 //  Constructor
-    fName="ExtFile";
-    fTitle="Primaries from ext. File";
-    fFileName="";
-    fTreeNtuple=0;
-    fNcurrent=0;
 //
 //  Read all particles
-    fNpart=-1;
+    fNpart  =- 1;
+    fReader =  0;
 }
 
 AliGenExtFile::AliGenExtFile(Int_t npart)
-    :AliGenerator(npart)
+    :AliGenMC(npart)
 {
 //  Constructor
-    fName="ExtFile";
-    fTitle="Primaries from ext. File";
-    fFileName="";
-    fTreeNtuple=0;
-    fNcurrent=0;
+    fName   = "ExtFile";
+    fTitle  = "Primaries from ext. File";
+    fReader = 0;
 }
 
 AliGenExtFile::AliGenExtFile(const AliGenExtFile & ExtFile)
@@ -97,134 +126,141 @@ AliGenExtFile::AliGenExtFile(const AliGenExtFile & ExtFile)
 AliGenExtFile::~AliGenExtFile()
 {
 // Destructor
-    delete fTreeNtuple;
+    delete fReader;
 }
 
-//____________________________________________________________
-void AliGenExtFile::NtupleInit() 
+//___________________________________________________________
+void AliGenExtFile::Init()
 {
-//
-// reset the existing file environment and open a new root file if
-// the pointer to the Fluka tree is null
-    
-    TFile *pFile=0;
-    if (fTreeNtuple==0) {
-        if (!pFile) {
-           pFile = new TFile(fFileName);
-           pFile->cd();
-           cout<<"I have opened "<<fFileName<<" file "<<endl;
-        }
-// get the tree address in the Fluka boundary source file
-       fTreeNtuple = (TTree*)gDirectory->Get("h888");
-    } else {
-        pFile = fTreeNtuple->GetCurrentFile();
-        pFile->cd();
-    }
-
-    TTree *h2=fTreeNtuple;
-//Set branch addresses
-//Set branch addresses
-    h2->SetBranchAddress("Nihead",&fNihead);
-    h2->SetBranchAddress("Ihead",fIhead);
-    h2->SetBranchAddress("Nrhead",&fNrhead);
-    h2->SetBranchAddress("Rhead",fRhead);
-    h2->SetBranchAddress("Idpart",&fIdpart);
-    h2->SetBranchAddress("Theta",&fTheta);
-    h2->SetBranchAddress("Phi",&fPhi);
-    h2->SetBranchAddress("P",&fP);
-    h2->SetBranchAddress("E",&fE);
+// Initialize
+    if (fReader) fReader->Init();
 }
 
-
-//____________________________________________________________
+    
 void AliGenExtFile::Generate()
 {
 // Generate particles
 
-  Float_t polar[3]= {0,0,0};
+  Float_t polar[3]  = {0,0,0};
   //
-  Float_t origin[3]={0,0,0};
+  Float_t origin[3] = {0,0,0};
   Float_t p[3];
   Float_t random[6];
-  Float_t prwn;
-  Int_t i, j, nt, nTracks=0;
+  Int_t i, j, nt;
   //
-  NtupleInit();
-  TTree *h2=fTreeNtuple;
-  Int_t nentries = (Int_t) h2->GetEntries();
-  // loop over number of particles
-  Int_t nb = (Int_t)h2->GetEvent(fNcurrent);
-  Int_t i5=fIhead[4];
-  Int_t i6=fIhead[5];
-
   for (j=0;j<3;j++) origin[j]=fOrigin[j];
-  if(fVertexSmear==kPerTrack) {
+  if(fVertexSmear == kPerTrack) {
     Rndm(random,6);
-    for (j=0;j<3;j++) {
-       origin[j]+=fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
+    for (j = 0; j < 3; j++) {
+       origin[j] += fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
            TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
     }
   }
 
-  if (fNcurrent >= nentries) {
-      printf("\n No more entries !!! !\n");
+  while(1) {
+    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;
-  }
-  
-         
-  if (i5==0) {
-      printf("\n This should never happen !\n");
-  } else {
-      printf("\n Next event contains %d tracks! \n", i6);
-      nTracks=i6;
-  }
-  for (i=0; i<nTracks; i++) {
-      fIdpart=gMC->PDGFromId(fIdpart);
-      Double_t amass = TDatabasePDG::Instance()->GetParticle(fIdpart)->Mass();
-      if(fE<=amass) {
-       Warning("Generate","Particle %d no %d E = %f mass = %f %f %f \n",
-               fIdpart,i,fE,amass, fPhi, fTheta);
-       prwn=0;
-      } else {
-       prwn=sqrt((fE+amass)*(fE-amass));
+    }
+
+    //
+    // Particle selection loop
+    //
+    // The selction criterium for the external file generator is as follows:
+    //
+    // 1) All tracs are subjects to the cuts defined by AliGenerator, i.e.
+    //    fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
+    //    fYMin, fYMax.
+    //    If the particle does not satisfy these cuts, it is not put on the
+    //    stack.
+    // 2) If fCutOnChild and some specific child is selected (e.g. if
+    //    fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
+    //    child falls into the child-cuts.
+    if(fCutOnChild) {
+      // Count the selected children
+      Int_t nSelected = 0;
+
+      for (i = 0; i < nTracks; i++) {
+       TParticle* iparticle = fReader->NextParticle();
+       Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
+       kf = TMath::Abs(kf);
+       if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
+         nSelected++;
+       }
       }
+      if (!nSelected) continue;    // No particle selected:  Go to next event
+      fReader->RewindEvent();
+    }
 
-      fTheta *= TMath::Pi()/180.;
-      fPhi    = (fPhi-180)*TMath::Pi()/180.;      
-      if(fTheta<fThetaMin || fTheta>fThetaMax ||
-        fPhi<fPhiMin || fPhi>fPhiMax         ||
-        prwn<fPMin || prwn>fPMax)          
-      {
-         ;
-      } else {
-         p[0]=prwn*TMath::Sin(fTheta)*TMath::Cos(fPhi);
-         p[1]=prwn*TMath::Sin(fTheta)*TMath::Sin(fPhi);      
-         p[2]=prwn*TMath::Cos(fTheta);
-         
-         if(fVertexSmear==kPerTrack) {
-             Rndm(random,6);
-             for (j=0;j<3;j++) {
-                 origin[j]=fOrigin[j]
-                     +fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
-                     TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
-             }
+    //
+    // Stack filling loop
+    //
+    for (i = 0; i < nTracks; i++) {
+
+      TParticle* iparticle = fReader->NextParticle();
+      if (!KinematicSelection(iparticle,0)) {
+       Double_t  pz   = iparticle->Pz();
+       Double_t  e    = iparticle->Energy();
+       Double_t  y;
+       if ((e-pz) == 0) {
+         y = 20.;
+       } else if ((e+pz) == 0.) {
+         y = -20.;
+       } else {
+         y = 0.5*TMath::Log((e+pz)/(e-pz));      
+       }
+       printf("\n Not selected %d %f %f %f %f %f", i,
+              iparticle->Theta(),
+              iparticle->Phi(),
+              iparticle->P(),
+              iparticle->Pt(),
+              y);
+       //PH    delete iparticle;
+       continue;
+      }
+      p[0] = iparticle->Px();
+      p[1] = iparticle->Py();
+      p[2] = iparticle->Pz();
+      Int_t idpart = iparticle->GetPdgCode();
+      if(fVertexSmear==kPerTrack) {
+         Rndm(random,6);
+         for (j = 0; j < 3; j++) {
+             origin[j]=fOrigin[j]
+                 +fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
+                 TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
          }
-         gAlice->SetTrack(fTrackIt,-1,fIdpart,p,origin,polar,0,kPPrimary,nt);
       }
-      fNcurrent++;
-      nb = (Int_t)h2->GetEvent(fNcurrent); 
+      Int_t decayed = iparticle->GetFirstDaughter();
+      Int_t doTracking = fTrackIt && (decayed < 0) &&
+                        (TMath::Abs(idpart) > 10);
+      // printf("*** pdg, first daughter, trk = %d, %d, %d\n",
+      //   idpart,decayed, doTracking);
+      SetTrack(doTracking,-1,idpart,p,origin,polar,0,kPPrimary,nt);
+      KeepTrack(nt);
+    } // track loop
+
+    break;
+
+  } // event loop
+
+  SetHighWaterMark(nt);
+  CdEventFile();
+}
+
+void AliGenExtFile::CdEventFile()
+{
+// CD back to the event file
+  TFile *pFile=0;
+  if (!gAlice) {
+      gAlice = (AliRun*)pFile->Get("gAlice");
+      if (gAlice) printf("AliRun object found on file\n");
+      if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
   }
-    TFile *pFile=0;
-// Get AliRun object or create it 
-    if (!gAlice) {
-       gAlice = (AliRun*)pFile->Get("gAlice");
-       if (gAlice) printf("AliRun object found on file\n");
-       if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
-    }
-    TTree *fAli=gAlice->TreeK();
-    if (fAli) pFile =fAli->GetCurrentFile();
-    pFile->cd();
+  TTree *fAli=gAlice->TreeK();
+  if (fAli) pFile =fAli->GetCurrentFile();
+  pFile->cd();    
 }