Particles array has one entry more than pythia particle list. Upper bound of
[u/mrichter/AliRoot.git] / EVGEN / AliGenPythia.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 /*
17 $Log$
18 Revision 1.14  2000/04/05 08:36:13  morsch
19 Check status code of particles in Pythia event
20 to avoid double counting as partonic state and final state particle.
21
22 Revision 1.13  1999/11/09 07:38:48  fca
23 Changes for compatibility with version 2.23 of ROOT
24
25 Revision 1.12  1999/11/03 17:43:20  fca
26 New version from G.Martinez & A.Morsch
27
28 Revision 1.11  1999/09/29 09:24:14  fca
29 Introduction of the Copyright and cvs Log
30 */
31
32 #include "AliGenerator.h"
33 #include "AliGenPythia.h"
34 #include "AliRun.h"
35 #include "AliPythia.h"
36 #include <TDirectory.h>
37 #include <TFile.h>
38 #include <TTree.h>
39 #include <stdlib.h>
40 #include <AliPythia.h>
41 #include <TParticle.h>
42 //#include <GParticle.h>
43  ClassImp(AliGenPythia)
44
45 AliGenPythia::AliGenPythia()
46                  :AliGenerator()
47 {
48 }
49
50 AliGenPythia::AliGenPythia(Int_t npart)
51                  :AliGenerator(npart)
52 {
53 // default charm production at 5. 5 TeV
54 // semimuonic decay
55 // structure function GRVHO
56 //
57     fXsection  = 0.;
58     fParentSelect.Set(5);
59     fChildSelect.Set(5);
60     for (Int_t i=0; i<5; i++) fParentSelect[i]=fChildSelect[i]=0;
61     SetProcess();
62     SetStrucFunc();
63     SetForceDecay();
64     SetPtHard();
65     SetEnergyCMS();
66 }
67
68 AliGenPythia::~AliGenPythia()
69 {
70 }
71
72 void AliGenPythia::Init()
73 {
74     SetMC(new AliPythia());
75     fPythia=(AliPythia*) fgMCEvGen;
76 //
77     fParentWeight=1./Float_t(fNpart);
78 //
79 //  Forward Paramters to the AliPythia object
80     fPythia->DefineParticles();
81     fPythia->SetCKIN(3,fPtHardMin);
82     fPythia->SetCKIN(4,fPtHardMax);    
83     fPythia->ProcInit(fProcess,fEnergyCMS,fStrucFunc);
84     fPythia->ForceDecay(fForceDecay);
85     fPythia->Lulist(0);
86     fPythia->Pystat(2);
87 //  Parent and Children Selection
88     switch (fProcess) 
89     {
90     case charm:
91
92         fParentSelect[0]=411;
93         fParentSelect[1]=421;
94         fParentSelect[2]=431;
95         fParentSelect[3]=4122;  
96         break;
97     case charm_unforced:
98
99         fParentSelect[0]=411;
100         fParentSelect[1]=421;
101         fParentSelect[2]=431;
102         fParentSelect[3]=4122;  
103         break;
104     case beauty:
105         fParentSelect[0]=511;
106         fParentSelect[1]=521;
107         fParentSelect[2]=531;
108         fParentSelect[3]=5122;  
109         break;
110     case beauty_unforced:
111         fParentSelect[0]=511;
112         fParentSelect[1]=521;
113         fParentSelect[2]=531;
114         fParentSelect[3]=5122;  
115         break;
116     case jpsi_chi:
117     case jpsi:
118         fParentSelect[0]=443;
119         break;
120     case mb:
121         break;
122     }
123
124     switch (fForceDecay) 
125     {
126     case semielectronic:
127     case dielectron:
128     case b_jpsi_dielectron:
129     case b_psip_dielectron:
130         fChildSelect[0]=11;     
131         break;
132     case semimuonic:
133     case dimuon:
134     case b_jpsi_dimuon:
135     case b_psip_dimuon:
136     case pitomu:
137     case katomu:
138         fChildSelect[0]=13;
139         break;
140     case all:
141     case nodecay:
142         break;
143     }
144 }
145
146 void AliGenPythia::Generate()
147 {
148
149     Float_t polar[3] =   {0,0,0};
150     Float_t origin[3]=   {0,0,0};
151     Float_t origin_p[3]= {0,0,0};
152     Float_t origin0[3]=  {0,0,0};
153     Float_t p[3], p_p[4], random[6];
154     static TClonesArray *particles;
155 //  converts from mm/c to s
156     const Float_t kconv=0.001/2.999792458e8;
157     
158     
159 //
160     Int_t nt=0;
161     Int_t nt_p=0;
162     Int_t jev=0;
163     Int_t j, kf;
164
165     if(!particles) particles=new TClonesArray("TParticle",1000);
166     
167     fTrials=0;
168     for (j=0;j<3;j++) origin0[j]=fOrigin[j];
169     if(fVertexSmear==perEvent) {
170         gMC->Rndm(random,6);
171         for (j=0;j<3;j++) {
172             origin0[j]+=fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
173                 TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
174             fPythia->SetMSTP(151,0);
175         }
176     } else if (fVertexSmear==perTrack) {
177         fPythia->SetMSTP(151,0);
178         for (j=0;j<3;j++) {
179             fPythia->SetPARP(151+j, fOsigma[j]*10.);
180         }
181     }
182     while(1)
183     {
184         fPythia->Pyevnt();
185         fPythia->Lulist(1);
186         fTrials++;
187         fPythia->ImportParticles(particles,"All");
188         Int_t np = particles->GetEntriesFast();
189         printf("\n **************************************************%d\n",np);
190         Int_t nc=0;
191         if (np == 0 ) continue;
192         if (fProcess != mb) {
193             for (Int_t i = 0; i<np-1; i++) {
194                 TParticle *  iparticle = (TParticle *) particles->At(i);
195                 Int_t ks = iparticle->GetStatusCode();
196                 kf = CheckPDGCode(iparticle->GetPdgCode());
197                 if (ks==21) continue;
198
199                 fChildWeight=(fPythia->GetBraPart(kf))*fParentWeight;     
200 //
201 // Parent
202                 if (ParentSelected(TMath::Abs(kf))) {
203                     if (KinematicSelection(iparticle)) {
204                         if (nc==0) {
205 //
206 // Store information concerning the hard scattering process
207 //
208                             Float_t mass_p = fPythia->GetPARI(13);
209                             Float_t   pt_p = fPythia->GetPARI(17);
210                             Float_t    y_p = fPythia->GetPARI(37);
211                             Float_t  xmt_p = sqrt(pt_p*pt_p+mass_p*mass_p);
212                             Float_t     ty = Float_t(TMath::TanH(y_p));
213                             p_p[0] = pt_p;
214                             p_p[1] = 0;
215                             p_p[2] = xmt_p*ty/sqrt(1.-ty*ty);
216                             p_p[3] = mass_p;
217                             gAlice->SetTrack(0,-1,-1,
218                                              p_p,origin_p,polar,
219                                              0,"Hard Scat.",nt_p,fParentWeight);
220                             gAlice->KeepTrack(nt_p);
221                         }
222                         nc++;
223 //
224 // store parent track information
225                         p[0]=iparticle->Px();
226                         p[1]=iparticle->Py();
227                         p[2]=iparticle->Pz();
228                         origin[0]=origin0[0]+iparticle->Vx()/10;
229                         origin[1]=origin0[1]+iparticle->Vy()/10;
230                         origin[2]=origin0[2]+iparticle->Vz()/10;
231
232                         Int_t ifch=iparticle->GetFirstDaughter();
233                         Int_t ilch=iparticle->GetLastDaughter();        
234                         if (ifch !=0 && ilch !=0) {
235                             gAlice->SetTrack(0,nt_p,kf,
236                                              p,origin,polar,
237                                              0,"Primary",nt,fParentWeight);
238                             gAlice->KeepTrack(nt);
239                             Int_t iparent = nt;
240 //
241 // Children         
242
243                             for (j=ifch; j<=ilch; j++)
244                             {
245                                 TParticle *  ichild = 
246                                     (TParticle *) particles->At(j-1);
247                                 kf = CheckPDGCode(ichild->GetPdgCode());
248 //
249 // 
250                                 if (ChildSelected(TMath::Abs(kf))) {
251                                     origin[0]=ichild->Vx();
252                                     origin[1]=ichild->Vy();
253                                     origin[2]=ichild->Vz();             
254                                     p[0]=ichild->Px();
255                                     p[1]=ichild->Py();
256                                     p[2]=ichild->Pz();
257                                     Float_t tof=kconv*ichild->T();
258                                     gAlice->SetTrack(fTrackIt, iparent, kf,
259                                                      p,origin,polar,
260                                                      tof,"Decay",nt,fChildWeight);
261                                     gAlice->KeepTrack(nt);
262                                 } // select child
263                             } // child loop
264                         }
265                     } // kinematic selection
266                 } // select particle
267             } // particle loop
268         } else {
269             for (Int_t i = 0; i<np-1; i++) {
270                 TParticle *  iparticle = (TParticle *) particles->At(i);
271                 kf = CheckPDGCode(iparticle->GetPdgCode());
272                 Int_t ks = iparticle->GetStatusCode();
273
274                 if (ks==1 && kf!=0 && KinematicSelection(iparticle)) {
275                         nc++;
276 //
277 // store track information
278                         p[0]=iparticle->Px();
279                         p[1]=iparticle->Py();
280                         p[2]=iparticle->Pz();
281                         origin[0]=origin0[0]+iparticle->Vx()/10;
282                         origin[1]=origin0[1]+iparticle->Vy()/10;
283                         origin[2]=origin0[2]+iparticle->Vz()/10;
284                         Float_t tof=kconv*iparticle->T();
285                         gAlice->SetTrack(fTrackIt,-1,kf,p,origin,polar,
286                                          tof,"Primary",nt);
287                         gAlice->KeepTrack(nt);
288                 } // select particle
289             } // particle loop 
290             printf("\n I've put %i particles on the stack \n",nc);
291         } // mb ?
292         if (nc > 0) {
293             jev+=nc;
294             if (jev >= fNpart) {
295                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
296                 printf("\n Trials: %i %i %i\n",fTrials, fNpart, jev);
297                 break;
298             }
299         }
300     } // event loop
301 //  adjust weight due to kinematic selection
302     AdjustWeights();
303 //  get cross-section
304     fXsection=fPythia->GetPARI(1);
305 }
306
307 Bool_t AliGenPythia::ParentSelected(Int_t ip)
308 {
309     for (Int_t i=0; i<5; i++)
310     {
311         if (fParentSelect[i]==ip) return kTRUE;
312     }
313     return kFALSE;
314 }
315
316 Bool_t AliGenPythia::ChildSelected(Int_t ip)
317 {
318     for (Int_t i=0; i<5; i++)
319     {
320         if (fChildSelect[i]==ip) return kTRUE;
321     }
322     return kFALSE;
323 }
324
325 Bool_t AliGenPythia::KinematicSelection(TParticle *particle)
326 {
327     Float_t px=particle->Px();
328     Float_t py=particle->Py();
329     Float_t pz=particle->Pz();
330     Float_t  e=particle->Energy();
331
332 //
333 //  transverse momentum cut    
334     Float_t pt=TMath::Sqrt(px*px+py*py);
335     if (pt > fPtMax || pt < fPtMin) 
336     {
337 //      printf("\n failed pt cut %f %f %f \n",pt,fPtMin,fPtMax);
338         return kFALSE;
339     }
340 //
341 // momentum cut
342     Float_t p=TMath::Sqrt(px*px+py*py+pz*pz);
343     if (p > fPMax || p < fPMin) 
344     {
345 //      printf("\n failed p cut %f %f %f \n",p,fPMin,fPMax);
346         return kFALSE;
347     }
348     
349 //
350 // theta cut
351     Float_t  theta = Float_t(TMath::ATan2(Double_t(pt),Double_t(pz)));
352     if (theta > fThetaMax || theta < fThetaMin) 
353     {
354 //      printf("\n failed theta cut %f %f %f \n",theta,fThetaMin,fThetaMax);
355         return kFALSE;
356     }
357
358 //
359 // rapidity cut
360     Float_t y = 0.5*TMath::Log((e+pz)/(e-pz));
361     if (y > fYMax || y < fYMin)
362     {
363 //      printf("\n failed y cut %f %f %f \n",y,fYMin,fYMax);
364         return kFALSE;
365     }
366
367 //
368 // phi cut
369     Float_t phi=Float_t(TMath::ATan2(Double_t(py),Double_t(px)))+TMath::Pi();
370     if (phi > fPhiMax || phi < fPhiMin)
371     {
372 //      printf("\n failed phi cut %f %f %f \n",phi,fPhiMin,fPhiMax);
373         return kFALSE;
374     }
375
376     return kTRUE;
377 }
378 void AliGenPythia::AdjustWeights()
379 {
380     TClonesArray *PartArray = gAlice->Particles();
381     TParticle *Part;
382     Int_t ntrack=gAlice->GetNtrack();
383     for (Int_t i=0; i<ntrack; i++) {
384         Part= (TParticle*) PartArray->UncheckedAt(i);
385         Part->SetWeight(Part->GetWeight()*fKineBias);
386     }
387 }
388
389 Int_t AliGenPythia::CheckPDGCode(Int_t pdgcode)
390 {
391 //
392 //  If the particle is in a diffractive state, then take action accordingly
393   switch (pdgcode) {
394   case 110:
395     //rho_diff0 -- difficult to translate, return rho0
396     return 113;
397   case 210:
398     //pi_diffr+ -- change to pi+
399     return 211;
400   case 220:
401     //omega_di0 -- change to omega0
402     return 223;
403   case 330:
404     //phi_diff0 -- return phi0
405     return 333;
406   case 440:
407     //J/psi_di0 -- return J/psi
408     return 443;
409   case 2110:
410     //n_diffr -- return neutron
411     return 2112;
412   case 2210:
413     //p_diffr+ -- return proton
414     return 2212;
415   }
416   //non diffractive state -- return code unchanged
417   return pdgcode;
418 }
419                   
420
421
422
423
424
425
426
427