]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVGEN/AliGenExtFile.cxx
Store all the particles in the stack, but track only the selected ones
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 // Event generator that using an instance of type AliGenReader
19 // reads particles from a file and applies cuts.
20 // Example: In your Config.C you can include the following lines
21 //   AliGenExtFile *gener = new AliGenExtFile(-1);
22 //  gener->SetMomentumRange(0,999);
23 //  gener->SetPhiRange(-180.,180.);
24 //  gener->SetThetaRange(0,180);
25 //  gener->SetYRange(-999,999);
26 //  AliGenReaderTreeK * reader = new AliGenReaderTreeK();
27 //  reader->SetFileName("myFileWithTreeK.root");
28 //  gener->SetReader(reader);
29 //  gener->Init();
30
31
32 #include <Riostream.h>
33
34 #include "AliGenExtFile.h"
35 #include "AliRun.h"
36
37 #include <TParticle.h>
38 #include <TFile.h>
39 #include <TTree.h>
40
41
42  ClassImp(AliGenExtFile)
43
44 AliGenExtFile::AliGenExtFile()
45   :AliGenMC()
46 {
47 //  Constructor
48 //
49 //  Read all particles
50     fNpart  =- 1;
51     fReader =  0;
52 }
53
54 AliGenExtFile::AliGenExtFile(Int_t npart)
55     :AliGenMC(npart)
56 {
57 //  Constructor
58     fName   = "ExtFile";
59     fTitle  = "Primaries from ext. File";
60     fReader = 0;
61 }
62
63 AliGenExtFile::AliGenExtFile(const AliGenExtFile & ExtFile)
64 {
65 // copy constructor
66 }
67 //____________________________________________________________
68 AliGenExtFile::~AliGenExtFile()
69 {
70 // Destructor
71     delete fReader;
72 }
73
74 //___________________________________________________________
75 void AliGenExtFile::Init()
76 {
77 // Initialize
78     if (fReader) fReader->Init();
79 }
80
81     
82 void AliGenExtFile::Generate()
83 {
84 // Generate particles
85
86   Float_t polar[3]  = {0,0,0};
87   //
88   Float_t origin[3] = {0,0,0};
89   Float_t p[3];
90   Float_t random[6];
91   Int_t i, j, nt;
92   //
93   for (j=0;j<3;j++) origin[j]=fOrigin[j];
94   if(fVertexSmear == kPerTrack) {
95     Rndm(random,6);
96     for (j = 0; j < 3; j++) {
97         origin[j] += fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
98             TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
99     }
100   }
101
102   while(1) {
103     Int_t nTracks = fReader->NextEvent();       
104     if (nTracks == 0) {
105       // printf("\n No more events !!! !\n");
106       Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
107       return;
108     }
109
110     //
111     // Particle selection loop
112     //
113     // The selction criterium for the external file generator is as follows:
114     //
115     // 1) All tracs are subjects to the cuts defined by AliGenerator, i.e.
116     //    fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
117     //    fYMin, fYMax.
118     //    If the particle does not satisfy these cuts, it is not put on the
119     //    stack.
120     // 2) If fCutOnChild and some specific child is selected (e.g. if
121     //    fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
122     //    child falls into the child-cuts.
123     if(fCutOnChild) {
124       // Count the selected children
125       Int_t nSelected = 0;
126
127       for (i = 0; i < nTracks; i++) {
128         TParticle* iparticle = fReader->NextParticle();
129         Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
130         kf = TMath::Abs(kf);
131         if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
132           nSelected++;
133         }
134       }
135       if (!nSelected) continue;    // No particle selected:  Go to next event
136       fReader->RewindEvent();
137     }
138
139     //
140     // Stack filling loop
141     //
142     for (i = 0; i < nTracks; i++) {
143
144       TParticle* iparticle = fReader->NextParticle();
145       Bool_t selected = KinematicSelection(iparticle,0); 
146       if (!selected) {
147         Double_t  pz   = iparticle->Pz();
148         Double_t  e    = iparticle->Energy();
149         Double_t  y;
150         if ((e-pz) == 0) {
151           y = 20.;
152         } else if ((e+pz) == 0.) {
153           y = -20.;
154         } else {
155           y = 0.5*TMath::Log((e+pz)/(e-pz));      
156         }
157         printf("\n Not selected %d %f %f %f %f %f", i,
158                iparticle->Theta(),
159                iparticle->Phi(),
160                iparticle->P(),
161                iparticle->Pt(),
162                y);
163         //PH    delete iparticle;
164         //      continue;
165       }
166       p[0] = iparticle->Px();
167       p[1] = iparticle->Py();
168       p[2] = iparticle->Pz();
169       Int_t idpart = iparticle->GetPdgCode();
170       if(fVertexSmear==kPerTrack) {
171           Rndm(random,6);
172           for (j = 0; j < 3; j++) {
173               origin[j]=fOrigin[j]
174                   +fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
175                   TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
176           }
177       }
178       Int_t decayed = iparticle->GetFirstDaughter();
179       Int_t doTracking = fTrackIt && (decayed < 0) &&
180                          (TMath::Abs(idpart) > 10) && selected;
181       // printf("*** pdg, first daughter, trk = %d, %d, %d\n",
182       //   idpart,decayed, doTracking);
183       //PH      SetTrack(doTracking,-1,idpart,p,origin,polar,0,kPPrimary,nt);
184       Int_t parent = iparticle->GetFirstMother();
185       SetTrack(doTracking,parent,idpart,p,origin,polar,0,kPPrimary,nt);
186       KeepTrack(nt);
187     } // track loop
188
189     break;
190
191   } // event loop
192
193   SetHighWaterMark(nt);
194   CdEventFile();
195 }
196
197 void AliGenExtFile::CdEventFile()
198 {
199 // CD back to the event file
200   TFile *pFile=0;
201   if (!gAlice) {
202       gAlice = (AliRun*)pFile->Get("gAlice");
203       if (gAlice) printf("AliRun object found on file\n");
204       if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
205   }
206   TTree *fAli=gAlice->TreeK();
207   if (fAli) pFile =fAli->GetCurrentFile();
208   pFile->cd();    
209 }
210
211
212 AliGenExtFile& AliGenExtFile::operator=(const  AliGenExtFile& rhs)
213 {
214 // Assignment operator
215     return *this;
216 }
217
218
219
220
221
222
223
224