Use gMC and not pMC everywhere
[u/mrichter/AliRoot.git] / EVGEN / AliGenPythia.cxx
1 #include "AliGenerator.h"
2 #include "AliGenPythia.h"
3 #include "TGeant3.h"
4 #include "AliRun.h"
5 #include "AliPythia.h"
6 #include <TDirectory.h>
7 #include <TFile.h>
8 #include <TTree.h>
9 #include <stdlib.h>
10 #include <AliPythia.h>
11 #include <TParticle.h>
12 //#include <GParticle.h>
13  ClassImp(AliGenPythia)
14
15 AliGenPythia::AliGenPythia()
16                  :AliGenerator()
17 {
18 }
19
20 AliGenPythia::AliGenPythia(Int_t npart)
21                  :AliGenerator(npart)
22 {
23 // default charm production at 5. 5 TeV
24 // semimuonic decay
25 // structure function GRVHO
26 //
27     fXsection  = 0.;
28     fParentSelect.Set(5);
29     fChildSelect.Set(5);
30     for (Int_t i=0; i<5; i++) fParentSelect[i]=fChildSelect[i]=0;
31     SetProcess();
32     SetStrucFunc();
33     ForceDecay();
34     SetPtHard();
35     SetEnergyCMS();
36 }
37
38 AliGenPythia::~AliGenPythia()
39 {
40 }
41
42 void AliGenPythia::Init()
43 {
44     SetMC(new AliPythia());
45     fPythia=(AliPythia*) fgMCEvGen;
46 //
47     fParentWeight=1./Float_t(fNpart);
48 //
49 //  Forward Paramters to the AliPythia object
50     fPythia->DefineParticles();
51     fPythia->SetCKIN(3,fPtHardMin);
52     fPythia->SetCKIN(4,fPtHardMax);    
53     fPythia->ProcInit(fProcess,fEnergyCMS,fStrucFunc);
54     fPythia->ForceDecay(fForceDecay);
55     fPythia->LuList(0);
56     fPythia->PyStat(2);
57 //  Parent and Children Selection
58     switch (fProcess) 
59     {
60     case charm:
61
62         fParentSelect[0]=411;
63         fParentSelect[1]=421;
64         fParentSelect[2]=431;
65         fParentSelect[3]=4122;  
66         break;
67     case charm_unforced:
68
69         fParentSelect[0]=411;
70         fParentSelect[1]=421;
71         fParentSelect[2]=431;
72         fParentSelect[3]=4122;  
73         break;
74     case beauty:
75         fParentSelect[0]=511;
76         fParentSelect[1]=521;
77         fParentSelect[2]=531;
78         fParentSelect[3]=5122;  
79         break;
80     case beauty_unforced:
81         fParentSelect[0]=511;
82         fParentSelect[1]=521;
83         fParentSelect[2]=531;
84         fParentSelect[3]=5122;  
85         break;
86     case jpsi_chi:
87     case jpsi:
88         fParentSelect[0]=443;
89         break;
90     case mb:
91         break;
92     }
93
94     switch (fForceDecay) 
95     {
96     case semielectronic:
97     case dielectron:
98     case b_jpsi_dielectron:
99     case b_psip_dielectron:
100         fChildSelect[0]=11;     
101         break;
102     case semimuonic:
103     case dimuon:
104     case b_jpsi_dimuon:
105     case b_psip_dimuon:
106     case pitomu:
107     case katomu:
108         fChildSelect[0]=13;
109         break;
110     }
111 }
112
113 void AliGenPythia::Generate()
114 {
115
116     Float_t polar[3] =   {0,0,0};
117     Float_t origin[3]=   {0,0,0};
118     Float_t origin_p[3]= {0,0,0};
119     Float_t origin0[3]=  {0,0,0};
120     Float_t p[3], p_p[4], random[6];
121 //  converts from mm/c to s
122     const Float_t kconv=0.001/2.999792458e8;
123     
124     
125 //
126     Int_t nt=0;
127     Int_t nt_p=0;
128     Int_t jev=0;
129     Int_t j;
130     
131     fTrials=0;
132     for (j=0;j<3;j++) origin0[j]=fOrigin[j];
133     if(fVertexSmear==perEvent) {
134         gMC->Rndm(random,6);
135         for (j=0;j<3;j++) {
136             origin0[j]+=fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
137                 TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
138             fPythia->SetMSTP(151,0);
139         }
140     } else if (fVertexSmear==perTrack) {
141         fPythia->SetMSTP(151,0);
142         for (j=0;j<3;j++) {
143             fPythia->SetPARP(151+j, fOsigma[j]*10.);
144         }
145     }
146     while(1)
147     {
148         fPythia->PyEvnt();
149         fTrials++;
150         TObjArray* particles = fPythia->ImportParticles();
151         Int_t np = particles->GetEntriesFast();
152         printf("\n **************************************************%d\n",np);
153         Int_t nc=0;
154         if (np == 0 ) continue;
155         if (fProcess != mb) {
156             for (Int_t i = 0; i<np; i++) {
157                 TParticle *  iparticle = (TParticle *) particles->At(i);
158                 Int_t kf = iparticle->GetPdgCode();
159                 fChildWeight=(fPythia->GetBraPart(kf))*fParentWeight;     
160 //
161 // Parent
162                 if (ParentSelected(TMath::Abs(kf))) {
163                     if (KinematicSelection(iparticle)) {
164                         if (nc==0) {
165 //
166 // Store information concerning the hard scattering process
167 //
168                             Float_t mass_p = fPythia->GetPARI(13);
169                             Float_t   pt_p = fPythia->GetPARI(17);
170                             Float_t    y_p = fPythia->GetPARI(37);
171                             Float_t  xmt_p = sqrt(pt_p*pt_p+mass_p*mass_p);
172                             Float_t     ty = Float_t(TMath::TanH(y_p));
173                             p_p[0] = pt_p;
174                             p_p[1] = 0;
175                             p_p[2] = xmt_p*ty/sqrt(1.-ty*ty);
176                             p_p[3] = mass_p;
177                             gAlice->SetTrack(0,-1,-1,
178                                              p_p,origin_p,polar,
179                                              0,"Hard Scat.",nt_p,fParentWeight);
180                             gAlice->KeepTrack(nt_p);
181                         }
182                         nc++;
183 //
184 // store parent track information
185                         p[0]=iparticle->Px();
186                         p[1]=iparticle->Py();
187                         p[2]=iparticle->Pz();
188                         origin[0]=origin0[0]+iparticle->Vx()/10;
189                         origin[1]=origin0[1]+iparticle->Vy()/10;
190                         origin[2]=origin0[2]+iparticle->Vz()/10;
191
192                         Int_t ifch=iparticle->GetFirstDaughter();
193                         Int_t ilch=iparticle->GetLastDaughter();        
194                         if (ifch !=0 && ilch !=0) {
195                             gAlice->SetTrack(0,nt_p,kf,
196                                              p,origin,polar,
197                                              0,"Primary",nt,fParentWeight);
198                             gAlice->KeepTrack(nt);
199                             Int_t iparent = nt;
200 //
201 // Children         
202
203                             for (Int_t j=ifch; j<=ilch; j++)
204                             {
205                                 TParticle *  ichild = 
206                                     (TParticle *) particles->At(j-1);
207                                 Int_t kf = ichild->GetPdgCode();
208 //
209 // 
210                                 if (ChildSelected(TMath::Abs(kf))) {
211                                     origin[0]=ichild->Vx();
212                                     origin[1]=ichild->Vy();
213                                     origin[2]=ichild->Vz();             
214                                     p[0]=ichild->Px();
215                                     p[1]=ichild->Py();
216                                     p[2]=ichild->Pz();
217                                     Float_t tof=kconv*ichild->T();
218                                     gAlice->SetTrack(fTrackIt, iparent, kf,
219                                                      p,origin,polar,
220                                                      tof,"Decay",nt,fChildWeight);
221                                     gAlice->KeepTrack(nt);
222                                 } // select child
223                             } // child loop
224                         }
225                     } // kinematic selection
226                 } // select particle
227             } // particle loop
228         } else {
229             for (Int_t i = 0; i<np; i++) {
230                 TParticle *  iparticle = (TParticle *) particles->At(i);
231                 Int_t kf = iparticle->GetPdgCode();
232                 Int_t ks = iparticle->GetStatusCode();
233                 if (ks==1 && kf!=0 && KinematicSelection(iparticle)) {
234                         nc++;
235 //
236 // store track information
237                         p[0]=iparticle->Px();
238                         p[1]=iparticle->Py();
239                         p[2]=iparticle->Pz();
240                         origin[0]=origin0[0]+iparticle->Vx()/10;
241                         origin[1]=origin0[1]+iparticle->Vy()/10;
242                         origin[2]=origin0[2]+iparticle->Vz()/10;
243                         Float_t tof=kconv*iparticle->T();
244                         gAlice->SetTrack(fTrackIt,-1,kf,p,origin,polar,
245                                          tof,"Primary",nt);
246                         gAlice->KeepTrack(nt);
247                 } // select particle
248             } // particle loop 
249             printf("\n I've put %i particles on the stack \n",nc);
250         } // mb ?
251         if (nc > 0) {
252             jev+=nc;
253             if (jev >= fNpart) {
254                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
255                 printf("\n Trials: %i\n",fTrials);
256                 break;
257             }
258         }
259     } // event loop
260 //  adjust weight due to kinematic selection
261     AdjustWeights();
262 //  get cross-section
263     fXsection=fPythia->GetPARI(1);
264 }
265
266 Bool_t AliGenPythia::ParentSelected(Int_t ip)
267 {
268     for (Int_t i=0; i<5; i++)
269     {
270         if (fParentSelect[i]==ip) return kTRUE;
271     }
272     return kFALSE;
273 }
274
275 Bool_t AliGenPythia::ChildSelected(Int_t ip)
276 {
277     for (Int_t i=0; i<5; i++)
278     {
279         if (fChildSelect[i]==ip) return kTRUE;
280     }
281     return kFALSE;
282 }
283
284 Bool_t AliGenPythia::KinematicSelection(TParticle *particle)
285 {
286     Float_t px=particle->Px();
287     Float_t py=particle->Py();
288     Float_t pz=particle->Pz();
289     Float_t  e=particle->Energy();
290
291 //
292 //  transverse momentum cut    
293     Float_t pt=TMath::Sqrt(px*px+py*py);
294     if (pt > fPtMax || pt < fPtMin) 
295     {
296 //      printf("\n failed pt cut %f %f %f \n",pt,fPtMin,fPtMax);
297         return kFALSE;
298     }
299 //
300 // momentum cut
301     Float_t p=TMath::Sqrt(px*px+py*py+pz*pz);
302     if (p > fPMax || p < fPMin) 
303     {
304 //      printf("\n failed p cut %f %f %f \n",p,fPMin,fPMax);
305         return kFALSE;
306     }
307     
308 //
309 // theta cut
310     Float_t  theta = Float_t(TMath::ATan2(Double_t(pt),Double_t(pz)));
311     if (theta > fThetaMax || theta < fThetaMin) 
312     {
313 //      printf("\n failed theta cut %f %f %f \n",theta,fThetaMin,fThetaMax);
314         return kFALSE;
315     }
316
317 //
318 // rapidity cut
319     Float_t y = 0.5*TMath::Log((e+pz)/(e-pz));
320     if (y > fYMax || y < fYMin)
321     {
322 //      printf("\n failed y cut %f %f %f \n",y,fYMin,fYMax);
323         return kFALSE;
324     }
325
326 //
327 // phi cut
328     Float_t phi=Float_t(TMath::ATan2(Double_t(py),Double_t(px)))+TMath::Pi();
329     if (phi > fPhiMax || phi < fPhiMin)
330     {
331 //      printf("\n failed phi cut %f %f %f \n",phi,fPhiMin,fPhiMax);
332         return kFALSE;
333     }
334
335     return kTRUE;
336 }
337 void AliGenPythia::AdjustWeights()
338 {
339     TClonesArray *PartArray = gAlice->Particles();
340     TParticle *Part;
341     Int_t ntrack=gAlice->GetNtrack();
342     for (Int_t i=0; i<ntrack; i++) {
343         Part= (TParticle*) PartArray->UncheckedAt(i);
344         Part->SetWeight(Part->GetWeight()*fKineBias);
345     }
346 }
347
348
349
350
351
352
353
354
355