Several corrections. By A.Morsch
[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 <TMCParticle.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     AliMC* pMC = AliMC::GetMC();
116
117     Float_t polar[3] =   {0,0,0};
118     Float_t origin[3]=   {0,0,0};
119     Float_t origin_p[3]= {0,0,0};
120     Float_t origin0[3]=  {0,0,0};
121     Float_t p[3], p_p[4], random[6];
122 //  converts from mm/c to s
123     const Float_t kconv=0.001/2.999792458e8;
124     
125     
126 //
127     Int_t nt=0;
128     Int_t nt_p=0;
129     Int_t jev=0;
130     Int_t j;
131     
132     fTrials=0;
133     for (j=0;j<3;j++) origin0[j]=fOrigin[j];
134     if(fVertexSmear==perEvent) {
135         pMC->Rndm(random,6);
136         for (j=0;j<3;j++) {
137             origin0[j]+=fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
138                 TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
139             fPythia->SetMSTP(151,0);
140         }
141     } else if (fVertexSmear==perTrack) {
142         fPythia->SetMSTP(151,0);
143         for (j=0;j<3;j++) {
144             fPythia->SetPARP(151+j, fOsigma[j]*10.);
145         }
146     }
147     while(1)
148     {
149         fPythia->PyEvnt();
150         fTrials++;
151         TObjArray* particles = fPythia->GetPrimaries() ;
152         Int_t np = particles->GetEntriesFast();
153         printf("\n **************************************************%d\n",np);
154         Int_t nc=0;
155         if (np == 0 ) continue;
156         if (fProcess != mb) {
157             for (Int_t i = 0; i<np; i++) {
158                 TMCParticle *  iparticle = (TMCParticle *) particles->At(i);
159                 Int_t kf = iparticle->GetKF();
160                 fChildWeight=(fPythia->GetBraPart(kf))*fParentWeight;     
161 //
162 // Parent
163                 if (ParentSelected(TMath::Abs(kf))) {
164                     if (KinematicSelection(iparticle)) {
165                         if (nc==0) {
166 //
167 // Store information concerning the hard scattering process
168 //
169                             Float_t mass_p = fPythia->GetPARI(13);
170                             Float_t   pt_p = fPythia->GetPARI(17);
171                             Float_t    y_p = fPythia->GetPARI(37);
172                             Float_t  xmt_p = sqrt(pt_p*pt_p+mass_p*mass_p);
173                             Float_t     ty = Float_t(TMath::TanH(y_p));
174                             p_p[0] = pt_p;
175                             p_p[1] = 0;
176                             p_p[2] = xmt_p*ty/sqrt(1.-ty*ty);
177                             p_p[3] = mass_p;
178                             gAlice->SetTrack(0,-1,-1,
179                                              p_p,origin_p,polar,
180                                              0,"Hard Scat.",nt_p,fParentWeight);
181                             gAlice->KeepTrack(nt_p);
182                         }
183                         nc++;
184 //
185 // store parent track information
186                         p[0]=iparticle->GetPx();
187                         p[1]=iparticle->GetPy();
188                         p[2]=iparticle->GetPz();
189                         origin[0]=origin0[0]+iparticle->GetVx()/10;
190                         origin[1]=origin0[1]+iparticle->GetVy()/10;
191                         origin[2]=origin0[2]+iparticle->GetVz()/10;
192
193                         Int_t ifch=iparticle->GetFirstChild();
194                         Int_t ilch=iparticle->GetLastChild();   
195                         if (ifch !=0 && ilch !=0) {
196                             gAlice->SetTrack(0,nt_p,fPythia->GetGeantCode(kf),
197                                              p,origin,polar,
198                                              0,"Primary",nt,fParentWeight);
199                             gAlice->KeepTrack(nt);
200                             Int_t iparent = nt;
201 //
202 // Children         
203
204                             for (Int_t j=ifch; j<=ilch; j++)
205                             {
206                                 TMCParticle *  ichild = 
207                                     (TMCParticle *) particles->At(j-1);
208                                 Int_t kf = ichild->GetKF();
209 //
210 // 
211                                 if (ChildSelected(TMath::Abs(kf))) {
212                                     Int_t kg=fPythia->GetGeantCode(kf);
213                                     origin[0]=ichild->GetVx();
214                                     origin[1]=ichild->GetVy();
215                                     origin[2]=ichild->GetVz();          
216                                     p[0]=ichild->GetPx();
217                                     p[1]=ichild->GetPy();
218                                     p[2]=ichild->GetPz();
219                                     Float_t tof=kconv*ichild->GetTime();
220                                     gAlice->SetTrack(fTrackIt, iparent, kg,
221                                                      p,origin,polar,
222                                                      tof,"Decay",nt,fChildWeight);
223                                     gAlice->KeepTrack(nt);
224                                 } // select child
225                             } // child loop
226                         }
227                     } // kinematic selection
228                 } // select particle
229             } // particle loop
230         } else {
231             for (Int_t i = 0; i<np; i++) {
232                 TMCParticle *  iparticle = (TMCParticle *) particles->At(i);
233                 Int_t kf = iparticle->GetKF();
234                 Int_t ks = iparticle->GetKS();
235                 Int_t gc = fPythia->GetGeantCode(kf);
236                 if (ks==1 && gc!=0 && KinematicSelection(iparticle)) {
237                         nc++;
238 //
239 // store track information
240                         p[0]=iparticle->GetPx();
241                         p[1]=iparticle->GetPy();
242                         p[2]=iparticle->GetPz();
243                         origin[0]=origin0[0]+iparticle->GetVx()/10;
244                         origin[1]=origin0[1]+iparticle->GetVy()/10;
245                         origin[2]=origin0[2]+iparticle->GetVz()/10;
246                         Float_t tof=kconv*iparticle->GetTime();
247                         gAlice->SetTrack(fTrackIt,-1,gc,p,origin,polar,
248                                          tof,"Primary",nt);
249                         gAlice->KeepTrack(nt);
250                 } // select particle
251             } // particle loop 
252             printf("\n I've put %i particles on the stack \n",nc);
253         } // mb ?
254         if (nc > 0) {
255             jev+=nc;
256             if (jev >= fNpart) {
257                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
258                 printf("\n Trials: %i\n",fTrials);
259                 break;
260             }
261         }
262     } // event loop
263 //  adjust weight due to kinematic selection
264     AdjustWeights();
265 //  get cross-section
266     fXsection=fPythia->GetPARI(1);
267 }
268
269 Bool_t AliGenPythia::ParentSelected(Int_t ip)
270 {
271     for (Int_t i=0; i<5; i++)
272     {
273         if (fParentSelect[i]==ip) return kTRUE;
274     }
275     return kFALSE;
276 }
277
278 Bool_t AliGenPythia::ChildSelected(Int_t ip)
279 {
280     for (Int_t i=0; i<5; i++)
281     {
282         if (fChildSelect[i]==ip) return kTRUE;
283     }
284     return kFALSE;
285 }
286
287 Bool_t AliGenPythia::KinematicSelection(TMCParticle *particle)
288 {
289     Float_t px=particle->GetPx();
290     Float_t py=particle->GetPy();
291     Float_t pz=particle->GetPz();
292     Float_t  e=particle->GetEnergy();
293
294 //
295 //  transverse momentum cut    
296     Float_t pt=TMath::Sqrt(px*px+py*py);
297     if (pt > fPtMax || pt < fPtMin) 
298     {
299 //      printf("\n failed pt cut %f %f %f \n",pt,fPtMin,fPtMax);
300         return kFALSE;
301     }
302 //
303 // momentum cut
304     Float_t p=TMath::Sqrt(px*px+py*py+pz*pz);
305     if (p > fPMax || p < fPMin) 
306     {
307 //      printf("\n failed p cut %f %f %f \n",p,fPMin,fPMax);
308         return kFALSE;
309     }
310     
311 //
312 // theta cut
313     Float_t  theta = Float_t(TMath::ATan2(Double_t(pt),Double_t(pz)));
314     if (theta > fThetaMax || theta < fThetaMin) 
315     {
316 //      printf("\n failed theta cut %f %f %f \n",theta,fThetaMin,fThetaMax);
317         return kFALSE;
318     }
319
320 //
321 // rapidity cut
322     Float_t y = 0.5*TMath::Log((e+pz)/(e-pz));
323     if (y > fYMax || y < fYMin)
324     {
325 //      printf("\n failed y cut %f %f %f \n",y,fYMin,fYMax);
326         return kFALSE;
327     }
328
329 //
330 // phi cut
331     Float_t phi=Float_t(TMath::ATan2(Double_t(py),Double_t(px)))+TMath::Pi();
332     if (phi > fPhiMax || phi < fPhiMin)
333     {
334 //      printf("\n failed phi cut %f %f %f \n",phi,fPhiMin,fPhiMax);
335         return kFALSE;
336     }
337
338     return kTRUE;
339 }
340 void AliGenPythia::AdjustWeights()
341 {
342     TClonesArray *PartArray = gAlice->Particles();
343     GParticle *Part;
344     Int_t ntrack=gAlice->GetNtrack();
345     for (Int_t i=0; i<ntrack; i++) {
346         Part= (GParticle*) PartArray->UncheckedAt(i);
347         Part->SetWgt(Part->GetWgt()*fKineBias);
348     }
349 }
350
351
352
353
354
355
356
357
358