Introducing new Rndm and QA classes
[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.25  2000/10/18 19:11:27  hristov
19 Division by zero fixed
20
21 Revision 1.24  2000/09/18 10:41:35  morsch
22 Add possibility to use nuclear structure functions from PDF library V8.
23
24 Revision 1.23  2000/09/14 14:05:40  morsch
25 dito
26
27 Revision 1.22  2000/09/14 14:02:22  morsch
28 - Correct conversion from mm to cm when passing particle vertex to MC.
29 - Correct handling of fForceDecay == all.
30
31 Revision 1.21  2000/09/12 14:14:55  morsch
32 Call fDecayer->ForceDecay() at the beginning of Generate().
33
34 Revision 1.20  2000/09/06 14:29:33  morsch
35 Use AliPythia for event generation an AliDecayPythia for decays.
36 Correct handling of "nodecay" option
37
38 Revision 1.19  2000/07/11 18:24:56  fca
39 Coding convention corrections + few minor bug fixes
40
41 Revision 1.18  2000/06/30 12:40:34  morsch
42 Pythia takes care of vertex smearing. Correct conversion from Pythia units (mm) to
43 Geant units (cm).
44
45 Revision 1.17  2000/06/09 20:34:07  morsch
46 All coding rule violations except RS3 corrected
47
48 Revision 1.16  2000/05/15 15:04:20  morsch
49 The full event is written for fNtrack = -1
50 Coding rule violations corrected.
51
52 Revision 1.15  2000/04/26 10:14:24  morsch
53 Particles array has one entry more than pythia particle list. Upper bound of
54 particle loop changed to np-1 (R. Guernane, AM)
55
56 Revision 1.14  2000/04/05 08:36:13  morsch
57 Check status code of particles in Pythia event
58 to avoid double counting as partonic state and final state particle.
59
60 Revision 1.13  1999/11/09 07:38:48  fca
61 Changes for compatibility with version 2.23 of ROOT
62
63 Revision 1.12  1999/11/03 17:43:20  fca
64 New version from G.Martinez & A.Morsch
65
66 Revision 1.11  1999/09/29 09:24:14  fca
67 Introduction of the Copyright and cvs Log
68 */
69
70 #include "AliGenPythia.h"
71 #include "AliDecayerPythia.h"
72 #include "AliRun.h"
73 #include "AliPythia.h"
74 #include "AliPDG.h"
75 #include <TParticle.h>
76 #include <TSystem.h>
77
78  ClassImp(AliGenPythia)
79
80 AliGenPythia::AliGenPythia()
81                  :AliGenerator()
82 {
83 // Default Constructor
84   fDecayer = new AliDecayerPythia();
85 }
86
87 AliGenPythia::AliGenPythia(Int_t npart)
88                  :AliGenerator(npart)
89 {
90 // default charm production at 5. 5 TeV
91 // semimuonic decay
92 // structure function GRVHO
93 //
94     fXsection  = 0.;
95     fNucA1=0;
96     fNucA2=0;
97     fParentSelect.Set(5);
98     fChildSelect.Set(5);
99     for (Int_t i=0; i<5; i++) fParentSelect[i]=fChildSelect[i]=0;
100     SetProcess();
101     SetStrucFunc();
102     SetForceDecay();
103     SetPtHard();
104     SetEnergyCMS();
105     fDecayer = new AliDecayerPythia();
106 }
107
108 AliGenPythia::AliGenPythia(const AliGenPythia & Pythia)
109 {
110 // copy constructor
111 }
112
113 AliGenPythia::~AliGenPythia()
114 {
115 // Destructor
116 }
117
118 void AliGenPythia::Init()
119 {
120 // Initialisation
121   SetMC(AliPythia::Instance());
122     fPythia=(AliPythia*) fgMCEvGen;
123 //
124     fParentWeight=1./Float_t(fNpart);
125 //
126 //  Forward Paramters to the AliPythia object
127     //    gSystem->Exec("ln -s $ALICE_ROOT/data/Decay.table fort.1");
128     //    fPythia->Pyupda(2,1);    
129     //    gSystem->Exec("rm fort.1");
130
131     fDecayer->SetForceDecay(fForceDecay);    
132     fDecayer->Init();
133
134
135     fPythia->SetCKIN(3,fPtHardMin);
136     fPythia->SetCKIN(4,fPtHardMax);    
137     if (fNucA1 > 0 && fNucA2 > 0) fPythia->SetNuclei(fNucA1, fNucA2);  
138     fPythia->ProcInit(fProcess,fEnergyCMS,fStrucFunc);
139
140     //    fPythia->Pylist(0);
141     //    fPythia->Pystat(2);
142 //  Parent and Children Selection
143     switch (fProcess) 
144     {
145     case charm:
146
147         fParentSelect[0]=411;
148         fParentSelect[1]=421;
149         fParentSelect[2]=431;
150         fParentSelect[3]=4122;  
151         break;
152     case charm_unforced:
153
154         fParentSelect[0]=411;
155         fParentSelect[1]=421;
156         fParentSelect[2]=431;
157         fParentSelect[3]=4122;  
158         break;
159     case beauty:
160         fParentSelect[0]=511;
161         fParentSelect[1]=521;
162         fParentSelect[2]=531;
163         fParentSelect[3]=5122;  
164         break;
165     case beauty_unforced:
166         fParentSelect[0]=511;
167         fParentSelect[1]=521;
168         fParentSelect[2]=531;
169         fParentSelect[3]=5122;  
170         break;
171     case jpsi_chi:
172     case jpsi:
173         fParentSelect[0]=443;
174         break;
175     case mb:
176         break;
177     }
178
179     switch (fForceDecay) 
180     {
181     case semielectronic:
182     case dielectron:
183     case b_jpsi_dielectron:
184     case b_psip_dielectron:
185         fChildSelect[0]=kElectron;      
186         break;
187     case semimuonic:
188     case dimuon:
189     case b_jpsi_dimuon:
190     case b_psip_dimuon:
191     case pitomu:
192     case katomu:
193         fChildSelect[0]=kMuonMinus;
194         break;
195     case hadronicD:
196       fChildSelect[0]=kPiPlus;
197       fChildSelect[1]=kKPlus;
198       break;
199     case all:
200     case nodecay:
201       break;
202     }
203 }
204
205 void AliGenPythia::Generate()
206 {
207 // Generate one event
208     fDecayer->ForceDecay();
209
210     Float_t polar[3] =   {0,0,0};
211     Float_t origin[3]=   {0,0,0};
212     Float_t originP[3]= {0,0,0};
213     Float_t origin0[3]=  {0,0,0};
214     Float_t p[3], pP[4];
215 //    Float_t random[6];
216     static TClonesArray *particles;
217 //  converts from mm/c to s
218     const Float_t kconv=0.001/2.999792458e8;
219     
220     
221 //
222     Int_t nt=0;
223     Int_t ntP=0;
224     Int_t jev=0;
225     Int_t j, kf;
226
227     if(!particles) particles=new TClonesArray("TParticle",1000);
228     
229     fTrials=0;
230     for (j=0;j<3;j++) origin0[j]=fOrigin[j];
231     if(fVertexSmear==kPerEvent) {
232         fPythia->SetMSTP(151,1);
233         for (j=0;j<3;j++) {
234             fPythia->SetPARP(151+j, fOsigma[j]/10.);
235         }
236     } else if (fVertexSmear==kPerTrack) {
237         fPythia->SetMSTP(151,0);
238     }
239     
240     while(1)
241     {
242         fPythia->Pyevnt();
243 //      fPythia->Pylist(1);
244         fTrials++;
245         fPythia->ImportParticles(particles,"All");
246         Int_t np = particles->GetEntriesFast();
247         printf("\n **************************************************%d\n",np);
248         Int_t nc=0;
249         if (np == 0 ) continue;
250         if (fProcess != mb) {
251             for (Int_t i = 0; i<np-1; i++) {
252                 TParticle *  iparticle = (TParticle *) particles->At(i);
253                 Int_t ks = iparticle->GetStatusCode();
254                 kf = CheckPDGCode(iparticle->GetPdgCode());
255                 if (ks==21) continue;
256
257                 fChildWeight=(fDecayer->GetPartialBranchingRatio(kf))*fParentWeight;      
258 //
259 // Parent
260                 if (ParentSelected(TMath::Abs(kf))) {
261                     if (KinematicSelection(iparticle)) {
262                         if (nc==0) {
263 //
264 // Store information concerning the hard scattering process
265 //
266                             Float_t massP  = fPythia->GetPARI(13);
267                             Float_t   ptP  = fPythia->GetPARI(17);
268                             Float_t    yP  = fPythia->GetPARI(37);
269                             Float_t  xmtP  = sqrt(ptP*ptP+massP*massP);
270                             Float_t    ty  = Float_t(TMath::TanH(yP));
271                             pP[0] = ptP;
272                             pP[1] = 0;
273                             pP[2] = xmtP*ty/sqrt(1.-ty*ty);
274                             pP[3] = massP;
275                             gAlice->SetTrack(0,-1,-1,
276                                              pP,originP,polar,
277                                              0,kPPrimary,ntP,fParentWeight);
278 //                                           0,"Hard Scat.",ntP,fParentWeight);
279                             gAlice->KeepTrack(ntP);
280                         }
281                         nc++;
282 //
283 // store parent track information
284                         p[0]=iparticle->Px();
285                         p[1]=iparticle->Py();
286                         p[2]=iparticle->Pz();
287                         origin[0]=origin0[0]+iparticle->Vx()/10.;
288                         origin[1]=origin0[1]+iparticle->Vy()/10.;
289                         origin[2]=origin0[2]+iparticle->Vz()/10.;
290
291                         Int_t ifch=iparticle->GetFirstDaughter();
292                         Int_t ilch=iparticle->GetLastDaughter();        
293
294                         if ((ifch !=0 && ilch !=0) || fForceDecay == nodecay) {
295                           Int_t trackit=0;
296                           if (fForceDecay == nodecay) trackit = 1;
297                             gAlice->SetTrack(trackit,ntP,kf,
298                                              p,origin,polar,
299                                              0,kPPrimary,nt,fParentWeight);
300                             gAlice->KeepTrack(nt);
301                             Int_t iparent = nt;
302 //
303 // Children         
304                             if (fForceDecay != nodecay) {
305                               for (j=ifch; j<=ilch; j++)
306                                 {
307                                   TParticle *  ichild = 
308                                     (TParticle *) particles->At(j-1);
309                                   kf = CheckPDGCode(ichild->GetPdgCode());
310 //
311 // 
312                                   if (ChildSelected(TMath::Abs(kf))) {
313                                     origin[0]=origin0[0]+ichild->Vx()/10.;
314                                     origin[1]=origin0[1]+ichild->Vy()/10.;
315                                     origin[2]=origin0[2]+ichild->Vz()/10.;              
316                                     p[0]=ichild->Px();
317                                     p[1]=ichild->Py();
318                                     p[2]=ichild->Pz();
319                                     Float_t tof=kconv*ichild->T();
320                                     gAlice->SetTrack(fTrackIt, iparent, kf,
321                                                      p,origin,polar,
322                                                      tof,kPDecay,nt,fChildWeight);
323                                     gAlice->KeepTrack(nt);
324                                   } // select child
325                                 } // child loop
326                             } 
327                         }
328                     } // kinematic selection
329                 } // select particle
330             } // particle loop
331         } else {
332             for (Int_t i = 0; i<np-1; i++) {
333                 TParticle *  iparticle = (TParticle *) particles->At(i);
334                 kf = CheckPDGCode(iparticle->GetPdgCode());
335                 Int_t ks = iparticle->GetStatusCode();
336
337                 if (ks==1 && kf!=0 && KinematicSelection(iparticle)) {
338                         nc++;
339 //
340 // store track information
341                         p[0]=iparticle->Px();
342                         p[1]=iparticle->Py();
343                         p[2]=iparticle->Pz();
344                         origin[0]=origin0[0]+iparticle->Vx()/10.;
345                         origin[1]=origin0[1]+iparticle->Vy()/10.;
346                         origin[2]=origin0[2]+iparticle->Vz()/10.;
347                         Float_t tof=kconv*iparticle->T();
348                         gAlice->SetTrack(fTrackIt,-1,kf,p,origin,polar,
349                                          tof,kPPrimary,nt);
350                         gAlice->KeepTrack(nt);
351                 } // select particle
352             } // particle loop 
353             printf("\n I've put %i particles on the stack \n",nc);
354         } // mb ?
355         if (nc > 0) {
356             jev+=nc;
357             if (jev >= fNpart || fNpart == -1) {
358                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
359                 printf("\n Trials: %i %i %i\n",fTrials, fNpart, jev);
360 // Print x-section summary
361                 fPythia->Pystat(1);
362                 break;
363             }
364         }
365     } // event loop
366 //  adjust weight due to kinematic selection
367     AdjustWeights();
368 //  get cross-section
369     fXsection=fPythia->GetPARI(1);
370 }
371
372 Bool_t AliGenPythia::ParentSelected(Int_t ip)
373 {
374 // True if particle is in list of parent particles to be selected
375     for (Int_t i=0; i<5; i++)
376     {
377         if (fParentSelect[i]==ip) return kTRUE;
378     }
379     return kFALSE;
380 }
381
382 Bool_t AliGenPythia::ChildSelected(Int_t ip)
383 {
384 // True if particle is in list of decay products to be selected
385     if (fForceDecay == all) return kTRUE;
386     
387     for (Int_t i=0; i<5; i++)
388     {
389         if (fChildSelect[i]==ip) return kTRUE;
390     }
391     return kFALSE;
392 }
393
394 Bool_t AliGenPythia::KinematicSelection(TParticle *particle)
395 {
396 // Perform kinematic selection
397     Float_t px=particle->Px();
398     Float_t py=particle->Py();
399     Float_t pz=particle->Pz();
400     Float_t  e=particle->Energy();
401
402 //
403 //  transverse momentum cut    
404     Float_t pt=TMath::Sqrt(px*px+py*py);
405     if (pt > fPtMax || pt < fPtMin) 
406     {
407 //      printf("\n failed pt cut %f %f %f \n",pt,fPtMin,fPtMax);
408         return kFALSE;
409     }
410 //
411 // momentum cut
412     Float_t p=TMath::Sqrt(px*px+py*py+pz*pz);
413     if (p > fPMax || p < fPMin) 
414     {
415 //      printf("\n failed p cut %f %f %f \n",p,fPMin,fPMax);
416         return kFALSE;
417     }
418     
419 //
420 // theta cut
421     Float_t  theta = Float_t(TMath::ATan2(Double_t(pt),Double_t(pz)));
422     if (theta > fThetaMax || theta < fThetaMin) 
423     {
424 //      printf("\n failed theta cut %f %f %f \n",theta,fThetaMin,fThetaMax);
425         return kFALSE;
426     }
427
428 //
429 // rapidity cut
430     if (e==pz) {
431       return kFALSE;
432     }
433     else {
434       Float_t y = 0.5*TMath::Log((e+pz)/(e-pz));
435       if (y > fYMax || y < fYMin)
436         {
437 //      printf("\n failed y cut %f %f %f \n",y,fYMin,fYMax);
438           return kFALSE;
439         }
440     }
441
442 //
443 // phi cut
444     Float_t phi=Float_t(TMath::ATan2(Double_t(py),Double_t(px)));
445     if (phi > fPhiMax || phi < fPhiMin)
446     {
447 //      printf("\n failed phi cut %f %f %f \n",phi,fPhiMin,fPhiMax);
448         return kFALSE;
449     }
450
451     return kTRUE;
452 }
453 void AliGenPythia::AdjustWeights()
454 {
455 // Adjust the weights after generation of all events
456 //
457     TClonesArray *partArray = gAlice->Particles();
458     TParticle *part;
459     Int_t ntrack=gAlice->GetNtrack();
460     for (Int_t i=0; i<ntrack; i++) {
461         part= (TParticle*) partArray->UncheckedAt(i);
462         part->SetWeight(part->GetWeight()*fKineBias);
463     }
464 }
465
466 Int_t AliGenPythia::CheckPDGCode(Int_t pdgcode)
467 {
468 //
469 //  If the particle is in a diffractive state, then take action accordingly
470   switch (pdgcode) {
471   case 91:
472     return 92;
473   case 110:
474     //rho_diff0 -- difficult to translate, return rho0
475     return 113;
476   case 210:
477     //pi_diffr+ -- change to pi+
478     return 211;
479   case 220:
480     //omega_di0 -- change to omega0
481     return 223;
482   case 330:
483     //phi_diff0 -- return phi0
484     return 333;
485   case 440:
486     //J/psi_di0 -- return J/psi
487     return 443;
488   case 2110:
489     //n_diffr -- return neutron
490     return 2112;
491   case 2210:
492     //p_diffr+ -- return proton
493     return 2212;
494   }
495   //non diffractive state -- return code unchanged
496   return pdgcode;
497 }
498
499     
500 void AliGenPythia::SetNuclei(Int_t a1, Int_t a2)
501 {
502 // Treat protons as inside nuclei with mass numbers a1 and a2  
503     fNucA1 = a1;
504     fNucA2 = a2;
505 }
506         
507           
508 AliGenPythia& AliGenPythia::operator=(const  AliGenPythia& rhs)
509 {
510 // Assignment operator
511     return *this;
512 }
513
514
515
516 void AliGenPythia::Streamer(TBuffer &R__b)
517 {
518    // Stream an object of class AliGenPythia.
519
520    if (R__b.IsReading()) {
521       Version_t R__v = R__b.ReadVersion(); if (R__v) { }
522       AliGenerator::Streamer(R__b);
523       R__b >> (Int_t&)fProcess;
524       R__b >> (Int_t&)fStrucFunc;
525       R__b >> (Int_t&)fForceDecay;
526       R__b >> fEnergyCMS;
527       R__b >> fKineBias;
528       R__b >> fTrials;
529       fParentSelect.Streamer(R__b);
530       fChildSelect.Streamer(R__b);
531       R__b >> fXsection;
532 //      (AliPythia::Instance())->Streamer(R__b);
533       R__b >> fPtHardMin;
534       R__b >> fPtHardMax;
535 //      if (fDecayer) fDecayer->Streamer(R__b);
536    } else {
537       R__b.WriteVersion(AliGenPythia::IsA());
538       AliGenerator::Streamer(R__b);
539       R__b << (Int_t)fProcess;
540       R__b << (Int_t)fStrucFunc;
541       R__b << (Int_t)fForceDecay;
542       R__b << fEnergyCMS;
543       R__b << fKineBias;
544       R__b << fTrials;
545       fParentSelect.Streamer(R__b);
546       fChildSelect.Streamer(R__b);
547       R__b << fXsection;
548 //      R__b << fPythia;
549       R__b << fPtHardMin;
550       R__b << fPtHardMax;
551       //     fDecayer->Streamer(R__b);
552    }
553 }
554
555
556
557 #ifndef WIN32
558 #define pyr    pyr_
559 #define pyrset pyrset_
560 #define pyrget pyrget_
561 #else
562 #define pyr    PYR
563 #define pyrset PYRSET
564 #define pyrget PYRGET
565 #endif
566
567 extern "C" {
568   Double_t pyr(Int_t*) {return sRandom->Rndm();}
569   void pyrset(Int_t*,Int_t*) {}
570   void pyrget(Int_t*,Int_t*) {}
571 }