]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PYTHIA6/AliPythia.cxx
f58c91526a8fb86a212464c84698697005b3d12e
[u/mrichter/AliRoot.git] / PYTHIA6 / AliPythia.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 #include "AliPythia.h"
19 #include "AliPythiaRndm.h"
20 #include "../FASTSIM/AliFastGlauber.h"
21 #include "../FASTSIM/AliQuenchingWeights.h"
22 #include "TVector3.h"
23
24 ClassImp(AliPythia)
25
26 #ifndef WIN32
27 # define pyclus pyclus_
28 # define pycell pycell_
29 # define pyshow pyshow_
30 # define pyrobo pyrobo_
31 # define pyquen pyquen_
32 # define pyevnw pyevnw_
33 # define type_of_call
34 #else
35 # define pyclus PYCLUS
36 # define pycell PYCELL
37 # define pyrobo PYROBO
38 # define pyquen PYQUEN
39 # define pyevnw PYEVNW
40 # define type_of_call _stdcall
41 #endif
42
43 extern "C" void type_of_call pyclus(Int_t & );
44 extern "C" void type_of_call pycell(Int_t & );
45 extern "C" void type_of_call pyshow(Int_t &, Int_t &, Double_t &);
46 extern "C" void type_of_call pyrobo(Int_t &, Int_t &, Double_t &, Double_t &, Double_t &, Double_t &, Double_t &);
47 extern "C" void type_of_call pyquen(Double_t &, Int_t &, Double_t &);
48 extern "C" void type_of_call pyevnw(){;}
49
50 //_____________________________________________________________________________
51
52 AliPythia* AliPythia::fgAliPythia=NULL;
53
54 AliPythia::AliPythia()
55 {
56 // Default Constructor
57 //
58 //  Set random number
59     if (!AliPythiaRndm::GetPythiaRandom()) 
60       AliPythiaRndm::SetPythiaRandom(GetRandom());
61     fGlauber          = 0;
62     fQuenchingWeights = 0;
63 }
64
65 void AliPythia::ProcInit(Process_t process, Float_t energy, StrucFunc_t strucfunc)
66 {
67 // Initialise the process to generate 
68     if (!AliPythiaRndm::GetPythiaRandom()) 
69       AliPythiaRndm::SetPythiaRandom(GetRandom());
70     
71     fProcess = process;
72     fEcms = energy;
73     fStrucFunc = strucfunc;
74 //...Switch off decay of pi0, K0S, Lambda, Sigma+-, Xi0-, Omega-.
75     SetMDCY(Pycomp(111) ,1,0);
76     SetMDCY(Pycomp(310) ,1,0);
77     SetMDCY(Pycomp(3122),1,0);
78     SetMDCY(Pycomp(3112),1,0);
79     SetMDCY(Pycomp(3212),1,0);
80     SetMDCY(Pycomp(3222),1,0);
81     SetMDCY(Pycomp(3312),1,0);
82     SetMDCY(Pycomp(3322),1,0);
83     SetMDCY(Pycomp(3334),1,0);
84     // Select structure function 
85     SetMSTP(52,2);
86     SetMSTP(51,strucfunc);
87     // Particles produced in string fragmentation point directly to either of the two endpoints
88     // of the string (depending in the side they were generated from).
89     SetMSTU(16,2);
90
91 //
92 // Pythia initialisation for selected processes//
93 //
94 // Make MSEL clean
95 //
96     for (Int_t i=1; i<= 200; i++) {
97         SetMSUB(i,0);
98     }
99 //  select charm production
100     switch (process) 
101     {
102     case kPyOldUEQ2ordered:  //Old underlying events with Q2 ordered QCD processes
103 //        Multiple interactions on.
104         SetMSTP(81,1);
105 // Double Gaussian matter distribution.
106         SetMSTP(82,4);
107         SetPARP(83,0.5);
108         SetPARP(84,0.4);
109 //  pT0.
110         SetPARP(82,2.0);
111 //  Reference energy for pT0 and energy rescaling pace.
112         SetPARP(89,1800);
113         SetPARP(90,0.25);
114 //  String drawing almost completely minimizes string length.
115         SetPARP(85,0.9);
116         SetPARP(86,0.95);
117 // ISR and FSR activity.
118         SetPARP(67,4);
119         SetPARP(71,4);
120 // Lambda_FSR scale.
121         SetPARJ(81,0.29);
122         break;
123     case kPyOldUEQ2ordered2:   
124 // Old underlying events with Q2 ordered QCD processes
125 // Multiple interactions on.
126         SetMSTP(81,1);
127 // Double Gaussian matter distribution.
128         SetMSTP(82,4);
129         SetPARP(83,0.5);
130         SetPARP(84,0.4);
131 // pT0.
132         SetPARP(82,2.0);
133 // Reference energy for pT0 and energy rescaling pace.
134         SetPARP(89,1800);
135         SetPARP(90,0.16);  // here is the difference with  kPyOldUEQ2ordered
136 // String drawing almost completely minimizes string length.
137         SetPARP(85,0.9);
138         SetPARP(86,0.95);
139 // ISR and FSR activity.
140         SetPARP(67,4);
141         SetPARP(71,4);
142 // Lambda_FSR scale.
143         SetPARJ(81,0.29);       
144         break;
145     case kPyOldPopcorn:  
146 // Old production mechanism: Old Popcorn
147         SetMSEL(1);
148         SetMSTJ(12,3); 
149 // (D=2) Like MSTJ(12)=2 but added prod ofthe 1er rank baryon
150         SetMSTP(88,2); 
151 // (D=1)see can be used to form  baryons (BARYON JUNCTION)
152         SetMSTJ(1,1);  
153         AtlasTuning();
154         break;
155     case kPyCharm:
156         SetMSEL(4);
157 //  heavy quark masses
158
159         SetPMAS(4,1,1.2);
160 //
161 //    primordial pT
162         SetMSTP(91,1);
163         SetPARP(91,1.);
164         SetPARP(93,5.);
165 //
166         break;
167     case kPyBeauty:
168         SetMSEL(5);
169         SetPMAS(5,1,4.75);
170         break;
171     case kPyJpsi:
172         SetMSEL(0);
173 // gg->J/Psi g
174         SetMSUB(86,1);
175         break;
176     case kPyJpsiChi:
177         SetMSEL(0);
178 // gg->J/Psi g
179         SetMSUB(86,1);
180 // gg-> chi_0c g
181         SetMSUB(87,1);
182 // gg-> chi_1c g
183         SetMSUB(88,1);
184 // gg-> chi_2c g
185         SetMSUB(89,1);  
186         break;
187     case kPyCharmUnforced:
188         SetMSEL(0);
189 // gq->qg   
190         SetMSUB(28,1);
191 // gg->qq
192         SetMSUB(53,1);
193 // gg->gg
194         SetMSUB(68,1);
195         break;
196     case kPyBeautyUnforced:
197         SetMSEL(0);
198 // gq->qg   
199         SetMSUB(28,1);
200 // gg->qq
201         SetMSUB(53,1);
202 // gg->gg
203         SetMSUB(68,1);
204         break;
205     case kPyMb:
206 // Minimum Bias pp-Collisions
207 //
208 //   
209 //      select Pythia min. bias model
210         SetMSEL(0);
211         SetMSUB(92,1);             // single diffraction AB-->XB
212         SetMSUB(93,1);             // single diffraction AB-->AX
213         SetMSUB(94,1);             // double diffraction
214         SetMSUB(95,1);             // low pt production
215
216         AtlasTuning();
217         break;
218     case kPyMbNonDiffr:
219 // Minimum Bias pp-Collisions
220 //
221 //   
222 //      select Pythia min. bias model
223         SetMSEL(0);
224         SetMSUB(95,1);             // low pt production
225
226         AtlasTuning();
227         break;
228     case kPyMbMSEL1:
229         ConfigHeavyFlavor();
230 // Intrinsic <kT^2>
231         SetMSTP(91,1);// Width (1=gaussian) primordial kT dist. inside hadrons
232         SetPARP(91,1.);     // <kT^2> = PARP(91,1.)^2
233         SetPARP(93,5.);     // Upper cut-off
234 // Set Q-quark mass
235         SetPMAS(4,1,1.2);   // Charm quark mass
236         SetPMAS(5,1,4.78);  // Beauty quark mass
237         SetPARP(71,4.);     // Defaut value
238 // Atlas Tuning
239         AtlasTuning();
240         break;
241     case kPyJets:
242 //
243 //  QCD Jets
244 //
245         SetMSEL(1);
246  // Pythia Tune A (CDF)
247  //
248        SetPARP(67,4.);            // Regulates Initial State Radiation
249        SetMSTP(82,4);             // Double Gaussian Model
250        SetPARP(82,2.0);           // [GeV]    PT_min at Ref. energy
251        SetPARP(84,0.4);           // Core radius
252        SetPARP(85,0.90) ;         // Regulates gluon prod. mechanism
253        SetPARP(86,0.95);          // Regulates gluon prod. mechanism
254        SetPARP(89,1800.);         // [GeV]   Ref. energy
255        SetPARP(90,0.25);          // 2*epsilon (exponent in power law)
256        break;
257     case kPyDirectGamma:
258         SetMSEL(10);
259         break;
260     case kPyCharmPbPbMNR:
261     case kPyD0PbPbMNR:
262     case kPyDPlusPbPbMNR:
263     case kPyDPlusStrangePbPbMNR:
264       // Tuning of Pythia parameters aimed to get a resonable agreement
265       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
266       // c-cbar single inclusive and double differential distributions.
267       // This parameter settings are meant to work with Pb-Pb collisions
268       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
269       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
270       // has to be set to 2.1GeV. Example in ConfigCharmPPR.C.
271         ConfigHeavyFlavor();
272       // Intrinsic <kT>
273       SetMSTP(91,1);
274       SetPARP(91,1.304);
275       SetPARP(93,6.52);
276       // Set c-quark mass
277       SetPMAS(4,1,1.2);
278       break;
279     case kPyCharmpPbMNR:
280     case kPyD0pPbMNR:
281     case kPyDPluspPbMNR:
282     case kPyDPlusStrangepPbMNR:
283       // Tuning of Pythia parameters aimed to get a resonable agreement
284       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
285       // c-cbar single inclusive and double differential distributions.
286       // This parameter settings are meant to work with p-Pb collisions
287       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
288       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
289       // has to be set to 2.1GeV. Example in ConfigCharmPPR.C.
290         ConfigHeavyFlavor();
291       // Intrinsic <kT>
292         SetMSTP(91,1);
293         SetPARP(91,1.16);
294         SetPARP(93,5.8);
295         
296       // Set c-quark mass
297         SetPMAS(4,1,1.2);
298       break;
299     case kPyCharmppMNR:
300     case kPyD0ppMNR:
301     case kPyDPlusppMNR:
302     case kPyDPlusStrangeppMNR:
303       // Tuning of Pythia parameters aimed to get a resonable agreement
304       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
305       // c-cbar single inclusive and double differential distributions.
306       // This parameter settings are meant to work with pp collisions
307       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
308       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
309       // has to be set to 2.1GeV. Example in ConfigCharmPPR.C.
310         ConfigHeavyFlavor();
311       // Intrinsic <kT^2>
312         SetMSTP(91,1);
313         SetPARP(91,1.);
314         SetPARP(93,5.);
315         
316       // Set c-quark mass
317         SetPMAS(4,1,1.2);
318       break;
319     case kPyCharmppMNRwmi:
320       // Tuning of Pythia parameters aimed to get a resonable agreement
321       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
322       // c-cbar single inclusive and double differential distributions.
323       // This parameter settings are meant to work with pp collisions
324       // and with kCTEQ5L PDFs.
325       // Added multiple interactions according to ATLAS tune settings.
326       // To get a "reasonable" agreement with MNR results, events have to be 
327       // generated with the minimum ptHard (AliGenPythia::SetPtHard)
328       // set to 2.76 GeV.
329       // To get a "perfect" agreement with MNR results, events have to be 
330       // generated in four ptHard bins with the following relative 
331       // normalizations:
332       // 2.76-3 GeV: 25%
333       //    3-4 GeV: 40%
334       //    4-8 GeV: 29%
335       //     >8 GeV:  6%
336         ConfigHeavyFlavor();
337       // Intrinsic <kT^2>
338         SetMSTP(91,1);
339         SetPARP(91,1.);
340         SetPARP(93,5.);
341
342       // Set c-quark mass
343         SetPMAS(4,1,1.2);
344         AtlasTuning();
345         break;
346     case kPyBeautyPbPbMNR:
347       // Tuning of Pythia parameters aimed to get a resonable agreement
348       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
349       // b-bbar single inclusive and double differential distributions.
350       // This parameter settings are meant to work with Pb-Pb collisions
351       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
352       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
353       // has to be set to 2.75GeV. Example in ConfigBeautyPPR.C.
354         ConfigHeavyFlavor();
355       // QCD scales
356         SetPARP(67,1.0);
357         SetPARP(71,1.0);
358       // Intrinsic <kT>
359         SetMSTP(91,1);
360         SetPARP(91,2.035);
361         SetPARP(93,10.17);
362       // Set b-quark mass
363         SetPMAS(5,1,4.75);
364       break;
365     case kPyBeautypPbMNR:
366       // Tuning of Pythia parameters aimed to get a resonable agreement
367       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
368       // b-bbar single inclusive and double differential distributions.
369       // This parameter settings are meant to work with p-Pb collisions
370       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
371       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
372       // has to be set to 2.75GeV. Example in ConfigBeautyPPR.C.
373         ConfigHeavyFlavor();
374       // QCD scales
375         SetPARP(67,1.0);
376         SetPARP(71,1.0);
377       // Intrinsic <kT>
378         SetMSTP(91,1);
379         SetPARP(91,1.60);
380         SetPARP(93,8.00);
381       // Set b-quark mass
382         SetPMAS(5,1,4.75);
383       break;
384     case kPyBeautyppMNR:
385       // Tuning of Pythia parameters aimed to get a resonable agreement
386       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
387       // b-bbar single inclusive and double differential distributions.
388       // This parameter settings are meant to work with pp collisions
389       // (AliGenPythia::SetNuclei) and with kCTEQ4L PDFs.
390       // To get a good agreement the minimum ptHard (AliGenPythia::SetPtHard)
391       // has to be set to 2.75GeV. Example in ConfigBeautyPPR.C.
392         ConfigHeavyFlavor();
393       // QCD scales
394         SetPARP(67,1.0);
395         SetPARP(71,1.0);
396         
397         // Intrinsic <kT>
398         SetMSTP(91,1);
399         SetPARP(91,1.);
400         SetPARP(93,5.);
401         
402         // Set b-quark mass
403         SetPMAS(5,1,4.75);
404       break;
405      case kPyBeautyppMNRwmi:
406       // Tuning of Pythia parameters aimed to get a resonable agreement
407       // between with the NLO calculation by Mangano, Nason, Ridolfi for the
408       // b-bbar single inclusive and double differential distributions.
409       // This parameter settings are meant to work with pp collisions
410       // and with kCTEQ5L PDFs.
411       // Added multiple interactions according to ATLAS tune settings.
412       // To get a "reasonable" agreement with MNR results, events have to be 
413       // generated with the minimum ptHard (AliGenPythia::SetPtHard)
414       // set to 2.76 GeV.
415       // To get a "perfect" agreement with MNR results, events have to be 
416       // generated in four ptHard bins with the following relative 
417       // normalizations:
418       // 2.76-4 GeV:  5% 
419       //    4-6 GeV: 31%
420       //    6-8 GeV: 28%
421       //     >8 GeV: 36%
422          ConfigHeavyFlavor();
423       // QCD scales
424          SetPARP(67,1.0);
425          SetPARP(71,1.0);
426          
427          // Intrinsic <kT>
428          SetMSTP(91,1);
429          SetPARP(91,1.);
430          SetPARP(93,5.);
431
432       // Set b-quark mass
433          SetPMAS(5,1,4.75);
434
435          AtlasTuning();
436          break; 
437     case kPyW:
438
439       //Inclusive production of W+/-
440       SetMSEL(0);
441       //f fbar -> W+ 
442       SetMSUB(2,1);
443       //        //f fbar -> g W+
444       //        SetMSUB(16,1);
445       //        //f fbar -> gamma W+
446       //        SetMSUB(20,1);
447       //        //f g -> f W+  
448       //        SetMSUB(31,1);
449       //        //f gamma -> f W+
450       //        SetMSUB(36,1);
451       
452       // Initial/final parton shower on (Pythia default)
453       // With parton showers on we are generating "W inclusive process"
454       SetMSTP(61,1); //Initial QCD & QED showers on
455       SetMSTP(71,1); //Final QCD & QED showers on
456       
457       break;  
458
459     case kPyZ:
460
461       //Inclusive production of Z
462       SetMSEL(0);
463       //f fbar -> Z/gamma
464       SetMSUB(1,1);
465       
466       //       // f fbar -> g Z/gamma
467       //       SetMSUB(15,1);
468       //       // f fbar -> gamma Z/gamma
469       //       SetMSUB(19,1);
470       //       // f g -> f Z/gamma
471       //       SetMSUB(30,1);
472       //       // f gamma -> f Z/gamma
473       //       SetMSUB(35,1);
474       
475       //only Z included, not gamma
476       SetMSTP(43,2);
477       
478       // Initial/final parton shower on (Pythia default)
479       // With parton showers on we are generating "Z inclusive process"
480       SetMSTP(61,1); //Initial QCD & QED showers on
481       SetMSTP(71,1); //Final QCD & QED showers on
482       
483       break;  
484
485     }
486 //
487 //  Initialize PYTHIA
488     SetMSTP(41,1);   // all resonance decays switched on
489
490     Initialize("CMS","p","p",fEcms);
491
492 }
493
494 Int_t AliPythia::CheckedLuComp(Int_t kf)
495 {
496 // Check Lund particle code (for debugging)
497     Int_t kc=Pycomp(kf);
498     printf("\n Lucomp kf,kc %d %d",kf,kc);
499     return kc;
500 }
501
502 void AliPythia::SetNuclei(Int_t a1, Int_t a2)
503 {
504 // Treat protons as inside nuclei with mass numbers a1 and a2  
505 //    The MSTP array in the PYPARS common block is used to enable and 
506 //    select the nuclear structure functions. 
507 //    MSTP(52)  : (D=1) choice of proton and nuclear structure-function library
508 //            =1: internal PYTHIA acording to MSTP(51) 
509 //            =2: PDFLIB proton  s.f., with MSTP(51)  = 1000xNGROUP+NSET
510 //    If the following mass number both not equal zero, nuclear corrections of the stf are used.
511 //    MSTP(192) : Mass number of nucleus side 1
512 //    MSTP(193) : Mass number of nucleus side 2
513     SetMSTP(52,2);
514     SetMSTP(192, a1);
515     SetMSTP(193, a2);  
516 }
517         
518
519 AliPythia* AliPythia::Instance()
520
521 // Set random number generator 
522     if (fgAliPythia) {
523         return fgAliPythia;
524     } else {
525         fgAliPythia = new AliPythia();
526         return fgAliPythia;
527     }
528 }
529
530 void AliPythia::PrintParticles()
531
532 // Print list of particl properties
533     Int_t np = 0;
534     char*   name = new char[16];    
535     for (Int_t kf=0; kf<1000000; kf++) {
536         for (Int_t c = 1;  c > -2; c-=2) {
537             Int_t kc = Pycomp(c*kf);
538             if (kc) {
539                 Float_t mass  = GetPMAS(kc,1);
540                 Float_t width = GetPMAS(kc,2);  
541                 Float_t tau   = GetPMAS(kc,4);
542
543                 Pyname(kf,name);
544         
545                 np++;
546                 
547                 printf("\n mass, width, tau: %6d %s %10.3f %10.3e %10.3e", 
548                        c*kf, name, mass, width, tau);
549             }
550         }
551     }
552     printf("\n Number of particles %d \n \n", np);
553 }
554
555 void  AliPythia::ResetDecayTable()
556 {
557 //  Set default values for pythia decay switches
558     Int_t i;
559     for (i = 1; i <  501; i++) SetMDCY(i,1,fDefMDCY[i]);
560     for (i = 1; i < 2001; i++) SetMDME(i,1,fDefMDME[i]);
561 }
562
563 void  AliPythia::SetDecayTable()
564 {
565 //  Set default values for pythia decay switches
566 //
567     Int_t i;
568     for (i = 1; i <  501; i++) fDefMDCY[i] = GetMDCY(i,1);
569     for (i = 1; i < 2001; i++) fDefMDME[i] = GetMDME(i,1);
570 }
571
572 void  AliPythia::Pyclus(Int_t& njet)
573 {
574 //  Call Pythia clustering algorithm
575 //
576     pyclus(njet);
577 }
578
579 void  AliPythia::Pycell(Int_t& njet)
580 {
581 //  Call Pythia jet reconstruction algorithm
582 //
583     pycell(njet);
584 }
585
586 void  AliPythia::Pyshow(Int_t ip1, Int_t ip2, Double_t qmax)
587 {
588 //  Call Pythia jet reconstruction algorithm
589 //
590     pyshow(ip1, ip2, qmax);
591 }
592
593 void AliPythia::Pyrobo(Int_t imi, Int_t ima, Double_t the, Double_t phi, Double_t bex, Double_t bey, Double_t bez)
594 {
595     pyrobo(imi, ima, the, phi, bex, bey, bez);
596 }
597
598
599
600 void AliPythia::InitQuenching(Float_t cMin, Float_t cMax, Float_t k, Int_t iECMethod)
601 {
602 // Initializes 
603 // (1) The quenching model using quenching weights according to C. Salgado and U. Wiedemann
604 // (2) The nuclear geometry using the Glauber Model
605 //     
606
607
608     fGlauber = new AliFastGlauber();
609     fGlauber->Init(2);
610     fGlauber->SetCentralityClass(cMin, cMax); 
611
612     fQuenchingWeights = new AliQuenchingWeights();
613     fQuenchingWeights->InitMult();
614     fQuenchingWeights->SetK(k);
615     fQuenchingWeights->SetECMethod(AliQuenchingWeights::kECMethod(iECMethod));
616 }
617
618
619 void  AliPythia::Quench()
620 {
621 //
622 //
623 //  Simple Jet Quenching routine:
624 //  =============================
625 //  The jet formed by all final state partons radiated by the parton created 
626 //  in the hard collisions is quenched by a factor (1-z) using light cone variables in 
627 //  the initial parton reference frame:
628 //  (E + p_z)new = (1-z) (E + p_z)old
629 //
630 //
631 //
632 //
633 //   The lost momentum is first balanced by one gluon with virtuality > 0.   
634 //   Subsequently the gluon splits to yield two gluons with E = p.
635 //
636 //
637 // 
638     static Float_t eMean = 0.;
639     static Int_t   icall = 0;
640     
641     Double_t p0[4][5];
642     Double_t p1[4][5];
643     Double_t p2[4][5];
644     Int_t   klast[4] = {-1, -1, -1, -1};
645
646     Int_t numpart   = fPyjets->N;
647     Double_t px = 0., py = 0., pz = 0., e = 0., m = 0., p = 0., pt = 0., theta = 0., phi = 0.;
648     Double_t pxq[4], pyq[4], pzq[4], eq[4], yq[4], mq[4], pq[4], phiq[4], thetaq[4], ptq[4];
649     Bool_t  quenched[4];
650     Double_t wjtKick[4];
651     Int_t nGluon[4];
652     Int_t qPdg[4];
653     Int_t   imo, kst, pdg;
654     
655 //
656 //  Sore information about Primary partons
657 //
658 //  j =
659 //  0, 1 partons from hard scattering
660 //  2, 3 partons from initial state radiation
661 // 
662     for (Int_t i = 2; i <= 7; i++) {
663         Int_t j = 0;
664         // Skip gluons that participate in hard scattering
665         if (i == 4 || i == 5) continue;
666         // Gluons from hard Scattering
667         if (i == 6 || i == 7) {
668             j = i - 6;
669             pxq[j]    = fPyjets->P[0][i];
670             pyq[j]    = fPyjets->P[1][i];
671             pzq[j]    = fPyjets->P[2][i];
672             eq[j]     = fPyjets->P[3][i];
673             mq[j]     = fPyjets->P[4][i];
674         } else {
675             // Gluons from initial state radiation
676             //
677             // Obtain 4-momentum vector from difference between original parton and parton after gluon 
678             // radiation. Energy is calculated independently because initial state radition does not 
679             // conserve strictly momentum and energy for each partonic system independently.
680             //
681             // Not very clean. Should be improved !
682             //
683             //
684             j = i;
685             pxq[j]    = fPyjets->P[0][i] - fPyjets->P[0][i+2];
686             pyq[j]    = fPyjets->P[1][i] - fPyjets->P[1][i+2];
687             pzq[j]    = fPyjets->P[2][i] - fPyjets->P[2][i+2];
688             mq[j]     = fPyjets->P[4][i];
689             eq[j]     = TMath::Sqrt(pxq[j] * pxq[j] + pyq[j] * pyq[j] + pzq[j] * pzq[j] + mq[j] * mq[j]);
690         }
691 //
692 //  Calculate some kinematic variables
693 //
694         yq[j]     = 0.5 * TMath::Log((eq[j] + pzq[j] + 1.e-14) / (eq[j] - pzq[j] + 1.e-14));
695         pq[j]     = TMath::Sqrt(pxq[j] * pxq[j] + pyq[j] * pyq[j] + pzq[j] * pzq[j]);
696         phiq[j]   = TMath::Pi()+TMath::ATan2(-pyq[j], -pxq[j]);
697         ptq[j]    = TMath::Sqrt(pxq[j] * pxq[j] + pyq[j] * pyq[j]);
698         thetaq[j] = TMath::ATan2(ptq[j], pzq[j]);
699         qPdg[j]   =  fPyjets->K[1][i];
700     }
701   
702     Double_t int0[4];
703     Double_t int1[4];
704     
705     fGlauber->GetI0I1ForPythiaAndXY(4, phiq, int0, int1, fXJet, fYJet, 15.);
706
707     for (Int_t j = 0; j < 4; j++) {
708         //
709         // Quench only central jets and with E > 10.
710         //
711
712
713         Int_t itype = (qPdg[j] == 21) ? 2 : 1;
714         Double_t eloss = fQuenchingWeights->GetELossRandomKFast(itype, int0[j], int1[j], eq[j]);
715
716         if (TMath::Abs(yq[j]) > 2.5 || eq[j] < 10.) {
717             fZQuench[j] = 0.;
718         } else {
719             if (eq[j] > 40. && TMath::Abs(yq[j]) < 0.5) {
720                 icall ++;
721                 eMean += eloss;
722             }
723             //
724             // Extra pt
725             Double_t l =   fQuenchingWeights->CalcLk(int0[j], int1[j]);     
726             wjtKick[j] = TMath::Sqrt(l *  fQuenchingWeights->CalcQk(int0[j], int1[j]));
727             //
728             // Fractional energy loss
729             fZQuench[j] = eloss / eq[j];
730             //
731             // Avoid complete loss
732             //
733             if (fZQuench[j] == 1.) fZQuench[j] = 0.95;
734             //
735             // Some debug printing
736
737             
738 //          printf("Initial parton # %3d, Type %3d Energy %10.3f Phi %10.3f Length %10.3f Loss %10.3f Kick %10.3f Mean: %10.3f %10.3f\n", 
739 //                 j, itype, eq[j], phiq[j], l, eloss, wjtKick[j], eMean / Float_t(icall+1), yq[j]);
740             
741 //          fZQuench[j] = 0.8;
742 //          while (fZQuench[j] >= 0.95)  fZQuench[j] = gRandom->Exp(0.2);
743         }
744         
745         quenched[j] = (fZQuench[j] > 0.01);
746     } // primary partons
747     
748     
749
750     Double_t pNew[1000][4];
751     Int_t    kNew[1000];
752     Int_t icount = 0;
753     Double_t zquench[4];
754     
755 //
756 //  System Loop    
757     for (Int_t isys = 0; isys < 4; isys++) {
758 //      Skip to next system if not quenched.
759         if (!quenched[isys]) continue;
760         
761         nGluon[isys]   = 1 + Int_t(fZQuench[isys] / (1. - fZQuench[isys]));
762         if (nGluon[isys] > 6) nGluon[isys] = 6;
763         zquench[isys] = 1. - TMath::Power(1. - fZQuench[isys], 1./Double_t(nGluon[isys]));
764         wjtKick[isys]  = wjtKick[isys] / TMath::Sqrt(Double_t(nGluon[isys]));
765
766
767         
768         Int_t igMin = -1;
769         Int_t igMax = -1;
770         Double_t pg[4] = {0., 0., 0., 0.};
771         
772 //
773 // Loop on radiation events
774
775         for (Int_t iglu = 0; iglu < nGluon[isys]; iglu++) {
776             while (1) {
777                 icount = 0;
778                 for (Int_t k = 0; k < 4; k++)
779                 {
780                     p0[isys][k] = 0.;
781                     p1[isys][k] = 0.;
782                     p2[isys][k] = 0.;
783                 }
784 //      Loop over partons
785                 for (Int_t i = 0; i < numpart; i++)
786                 {
787                     imo =  fPyjets->K[2][i];
788                     kst =  fPyjets->K[0][i];
789                     pdg =  fPyjets->K[1][i];
790                     
791                 
792                 
793 //      Quarks and gluons only
794                     if (pdg != 21 && TMath::Abs(pdg) > 6) continue;
795 //      Particles from hard scattering only
796                     
797                     if (imo > 8 && imo < 1000) imo = fPyjets->K[2][imo - 1];
798                     Int_t imom = imo % 1000;
799                     if ((isys == 0 || isys == 1) && ((imom != (isys + 7)))) continue;
800                     if ((isys == 2 || isys == 3) && ((imom != (isys + 1)))) continue;               
801                     
802                     
803 //      Skip comment lines
804                     if (kst != 1 && kst != 2) continue;
805 //
806 //      Parton kinematic
807                     px    = fPyjets->P[0][i];
808                     py    = fPyjets->P[1][i];
809                     pz    = fPyjets->P[2][i];
810                     e     = fPyjets->P[3][i];
811                     m     = fPyjets->P[4][i];
812                     pt    = TMath::Sqrt(px * px + py * py);
813                     p     = TMath::Sqrt(px * px + py * py + pz * pz); 
814                     phi   = TMath::Pi() + TMath::ATan2(-py, -px);
815                     theta = TMath::ATan2(pt, pz);
816                 
817 //
818 //      Save 4-momentum sum for balancing
819                     Int_t index = isys;
820                     
821                     p0[index][0] += px;
822                     p0[index][1] += py;
823                     p0[index][2] += pz;
824                     p0[index][3] += e;
825                 
826                     klast[index] = i;
827                     
828 //
829 //      Fractional energy loss
830                     Double_t z = zquench[index];
831                     
832                     
833 //      Don't fully quench radiated gluons
834 //
835                     if (imo > 1000) {
836 //      This small factor makes sure that the gluons are not too close in phase space to avoid recombination
837 //
838
839                         z = 0.02;
840                     }
841 //                  printf("z: %d %f\n", imo, z);
842                     
843
844 //
845                     
846                     //
847                     //
848                     //      Transform into frame in which initial parton is along z-axis
849                     //
850                     TVector3 v(px, py, pz);
851                     v.RotateZ(-phiq[index]);  v.RotateY(-thetaq[index]);
852                     Double_t pxs = v.X(); Double_t pys = v.Y(); Double_t pl  = v.Z();
853
854                     Double_t jt  = TMath::Sqrt(pxs * pxs + pys * pys);
855                     Double_t mt2 = jt * jt + m * m;
856                     Double_t zmax = 1.;     
857                     //
858                     // Kinematic limit on z
859                     //
860                     if (m > 0.) zmax = 1. - m / TMath::Sqrt(m * m + jt * jt);
861                     //
862                     // Change light-cone kinematics rel. to initial parton
863                     //  
864                     Double_t eppzOld = e + pl;
865                     Double_t empzOld = e - pl;
866                     
867                     Double_t eppzNew = (1. - z) * eppzOld;
868                     Double_t empzNew = empzOld - mt2 * z / eppzOld;
869                     Double_t eNew    = 0.5 * (eppzNew + empzNew);
870                     Double_t plNew   = 0.5 * (eppzNew - empzNew);
871                     
872                     Double_t jtNew;
873                     //
874                     // if mt very small (or sometimes even < 0 for numerical reasons) set it to 0
875                     Double_t mt2New = eppzNew * empzNew;
876                     if (mt2New < 1.e-8) mt2New = 0.;
877                     if (z < zmax) {
878                         if (m * m > mt2New) {
879                             //
880                             // This should not happen 
881                             //
882                             Fatal("Quench()", "This should never happen %e %e %e!", m, eppzNew, empzNew);
883                             jtNew = 0;
884                         } else {
885                             jtNew    = TMath::Sqrt(mt2New - m * m);
886                         }
887                     } else {
888                         // If pT is to small (probably a leading massive particle) we scale only the energy
889                         // This can cause negative masses of the radiated gluon
890                         // Let's hope for the best ...
891                         jtNew = jt;
892                         eNew  = TMath::Sqrt(plNew * plNew + mt2);
893                         
894                     }
895                     //
896                     //     Calculate new px, py
897                     //
898                     Double_t pxNew   = jtNew / jt * pxs;
899                     Double_t pyNew   = jtNew / jt * pys;        
900                     
901 //                  Double_t dpx = pxs - pxNew;
902 //                  Double_t dpy = pys - pyNew;
903 //                  Double_t dpz = pl  - plNew;
904 //                  Double_t de  = e   - eNew;
905 //                  Double_t dmass2 = de * de  - dpx * dpx - dpy * dpy - dpz * dpz;
906 //                  printf("New mass (1) %e %e %e %e %e %e %e \n", dmass2, jt, jtNew, pl, plNew, e, eNew);
907 //                  printf("New mass (2) %e %e \n", pxNew, pyNew);
908                     //
909                     //      Rotate back
910                     //  
911                     TVector3 w(pxNew, pyNew, plNew);
912                     w.RotateY(thetaq[index]); w.RotateZ(phiq[index]);
913                     pxNew = w.X(); pyNew = w.Y(); plNew = w.Z();
914                 
915                     p1[index][0] += pxNew;
916                     p1[index][1] += pyNew;
917                     p1[index][2] += plNew;
918                     p1[index][3] += eNew;       
919                     //
920                     // Updated 4-momentum vectors
921                     //
922                     pNew[icount][0]  = pxNew;
923                     pNew[icount][1]  = pyNew;
924                     pNew[icount][2]  = plNew;
925                     pNew[icount][3]  = eNew;
926                     kNew[icount]     = i;
927                     icount++;
928                 } // parton loop
929                 //
930                 // Check if there was phase-space for quenching
931                 //
932
933                 if (icount == 0) quenched[isys] = kFALSE;
934                 if (!quenched[isys]) break;
935                 
936                 for (Int_t j = 0; j < 4; j++) 
937                 {
938                     p2[isys][j] = p0[isys][j] - p1[isys][j];
939                 }
940                 p2[isys][4] = p2[isys][3] * p2[isys][3] - p2[isys][0] * p2[isys][0] - p2[isys][1] * p2[isys][1] - p2[isys][2] * p2[isys][2];
941                 if (p2[isys][4] > 0.) {
942                     p2[isys][4] = TMath::Sqrt(p2[isys][4]);
943                     break;
944                 } else {
945                     printf("Warning negative mass squared in system %d %f ! \n", isys, zquench[isys]);
946                     printf("4-Momentum: %10.3e %10.3e %10.3e %10.3e %10.3e \n", p2[isys][0], p2[isys][1], p2[isys][2], p2[isys][3], p2[isys][4]);
947                     if (p2[isys][4] < -0.01) {
948                         printf("Negative mass squared !\n");
949                         // Here we have to put the gluon back to mass shell
950                         // This will lead to a small energy imbalance
951                         p2[isys][4]  = 0.;
952                         p2[isys][3]  = TMath::Sqrt(p2[isys][0] * p2[isys][0] + p2[isys][1] * p2[isys][1] + p2[isys][2] * p2[isys][2]);
953                         break;
954                     } else {
955                         p2[isys][4] = 0.;
956                         break;
957                     }
958                 }
959                 /*
960                 zHeavy *= 0.98;
961                 printf("zHeavy lowered to %f\n", zHeavy);
962                 if (zHeavy < 0.01) {
963                     printf("No success ! \n");
964                     icount = 0;
965                     quenched[isys] = kFALSE;
966                     break;
967                 }
968                 */
969             } // iteration on z (while)
970             
971 //          Update  event record
972             for (Int_t k = 0; k < icount; k++) {
973 //              printf("%6d %6d %10.3e %10.3e %10.3e %10.3e\n", k, kNew[k], pNew[k][0],pNew[k][1], pNew[k][2], pNew[k][3] );
974                 fPyjets->P[0][kNew[k]] = pNew[k][0];
975                 fPyjets->P[1][kNew[k]] = pNew[k][1];
976                 fPyjets->P[2][kNew[k]] = pNew[k][2];
977                 fPyjets->P[3][kNew[k]] = pNew[k][3];
978             }
979             //
980             // Add the gluons
981             //
982             Int_t ish = 0;    
983             Int_t iGlu;
984             if (!quenched[isys]) continue;
985 //
986 //      Last parton from shower i
987             Int_t in = klast[isys];
988 //
989 //      Continue if no parton in shower i selected
990             if (in == -1) continue;
991 //  
992 //      If this is the second initial parton and it is behind the first move pointer by previous ish
993             if (isys == 1 && klast[1] > klast[0]) in += ish;
994 //
995 //      Starting index
996             
997 //          jmin = in - 1;
998 // How many additional gluons will be generated
999             ish  = 1;
1000             if (p2[isys][4] > 0.05) ish = 2;
1001 //
1002 //      Position of gluons
1003             iGlu = numpart;
1004             if (iglu == 0) igMin = iGlu;
1005             igMax = iGlu;
1006             numpart += ish;
1007             (fPyjets->N) += ish;
1008             
1009             if (ish == 1) {
1010                 fPyjets->P[0][iGlu] = p2[isys][0];
1011                 fPyjets->P[1][iGlu] = p2[isys][1];
1012                 fPyjets->P[2][iGlu] = p2[isys][2];
1013                 fPyjets->P[3][iGlu] = p2[isys][3];
1014                 fPyjets->P[4][iGlu] = p2[isys][4];
1015                 
1016                 fPyjets->K[0][iGlu] = 1;
1017                 if (iglu == nGluon[isys] - 1) fPyjets->K[0][iGlu] = 1;
1018                 fPyjets->K[1][iGlu] = 21;       
1019                 fPyjets->K[2][iGlu] = fPyjets->K[2][in] + 1000;
1020                 fPyjets->K[3][iGlu] = -1;       
1021                 fPyjets->K[4][iGlu] = -1;
1022                 
1023                 pg[0] += p2[isys][0];
1024                 pg[1] += p2[isys][1];
1025                 pg[2] += p2[isys][2];
1026                 pg[3] += p2[isys][3];
1027             } else {
1028                 //
1029                 // Split gluon in rest frame.
1030                 //
1031                 Double_t bx   =  p2[isys][0] / p2[isys][3];
1032                 Double_t by   =  p2[isys][1] / p2[isys][3];
1033                 Double_t bz   =  p2[isys][2] / p2[isys][3];
1034                 Double_t pst  =  p2[isys][4] / 2.;
1035                 //
1036                 // Isotropic decay ????
1037                 Double_t cost = 2. * gRandom->Rndm() - 1.;
1038                 Double_t sint = TMath::Sqrt(1. - cost * cost);
1039                 Double_t phi =  2. * TMath::Pi() * gRandom->Rndm();
1040                 
1041                 Double_t pz1 =   pst * cost;
1042                 Double_t pz2 =  -pst * cost;
1043                 Double_t pt1 =   pst * sint;
1044                 Double_t pt2 =  -pst * sint;
1045                 Double_t px1 =   pt1 * TMath::Cos(phi);
1046                 Double_t py1 =   pt1 * TMath::Sin(phi);     
1047                 Double_t px2 =   pt2 * TMath::Cos(phi);
1048                 Double_t py2 =   pt2 * TMath::Sin(phi);     
1049                 
1050                 fPyjets->P[0][iGlu] = px1;
1051                 fPyjets->P[1][iGlu] = py1;
1052                 fPyjets->P[2][iGlu] = pz1;
1053                 fPyjets->P[3][iGlu] = pst;
1054                 fPyjets->P[4][iGlu] = 0.;
1055                 
1056                 fPyjets->K[0][iGlu] = 1 ;
1057                 fPyjets->K[1][iGlu] = 21;       
1058                 fPyjets->K[2][iGlu] = fPyjets->K[2][in] + 1000;
1059                 fPyjets->K[3][iGlu] = -1;       
1060                 fPyjets->K[4][iGlu] = -1;
1061                 
1062                 fPyjets->P[0][iGlu+1] = px2;
1063                 fPyjets->P[1][iGlu+1] = py2;
1064                 fPyjets->P[2][iGlu+1] = pz2;
1065                 fPyjets->P[3][iGlu+1] = pst;
1066                 fPyjets->P[4][iGlu+1] = 0.;
1067                 
1068                 fPyjets->K[0][iGlu+1] = 1;
1069                 if (iglu == nGluon[isys] - 1) fPyjets->K[0][iGlu+1] = 1;
1070                 fPyjets->K[1][iGlu+1] = 21;     
1071                 fPyjets->K[2][iGlu+1] = fPyjets->K[2][in] + 1000;
1072                 fPyjets->K[3][iGlu+1] = -1;     
1073                 fPyjets->K[4][iGlu+1] = -1;
1074                 SetMSTU(1,0);
1075                 SetMSTU(2,0);
1076                 //
1077                 // Boost back
1078                 //
1079                 Pyrobo(iGlu + 1, iGlu + 2, 0., 0., bx, by, bz);
1080             }
1081 /*    
1082             for (Int_t ig = iGlu; ig < iGlu+ish; ig++) {
1083                 Double_t px, py, pz;
1084                 px = fPyjets->P[0][ig]; 
1085                 py = fPyjets->P[1][ig]; 
1086                 pz = fPyjets->P[2][ig]; 
1087                 TVector3 v(px, py, pz);
1088                 v.RotateZ(-phiq[isys]);
1089                 v.RotateY(-thetaq[isys]);
1090                 Double_t pxs     = v.X(); Double_t pys = v.Y(); Double_t pzs  = v.Z();     
1091                 Double_t r       = AliPythiaRndm::GetPythiaRandom()->Rndm();
1092                 Double_t jtKick  = 0.3 * TMath::Sqrt(-TMath::Log(r));
1093                 if (ish == 2)   jtKick  = wjtKick[i] * TMath::Sqrt(-TMath::Log(r)) / TMath::Sqrt(2.);
1094                 Double_t phiKick = 2. * TMath::Pi() * AliPythiaRndm::GetPythiaRandom()->Rndm();
1095                 pxs += jtKick * TMath::Cos(phiKick);
1096                 pys += jtKick * TMath::Sin(phiKick);
1097                 TVector3 w(pxs, pys, pzs);
1098                 w.RotateY(thetaq[isys]);
1099                 w.RotateZ(phiq[isys]);
1100                 fPyjets->P[0][ig] = w.X(); 
1101                 fPyjets->P[1][ig] = w.Y(); 
1102                 fPyjets->P[2][ig] = w.Z(); 
1103                 fPyjets->P[2][ig] = w.Mag();
1104             }
1105 */
1106         } // kGluon         
1107         
1108         
1109     // Check energy conservation
1110         Double_t pxs = 0.;
1111         Double_t pys = 0.;
1112         Double_t pzs = 0.;      
1113         Double_t es  = 14000.;
1114         
1115         for (Int_t i = 0; i < numpart; i++)
1116         {
1117             kst =  fPyjets->K[0][i];
1118             if (kst != 1 && kst != 2) continue;
1119             pxs += fPyjets->P[0][i];
1120             pys += fPyjets->P[1][i];
1121             pzs += fPyjets->P[2][i];        
1122             es  -= fPyjets->P[3][i];        
1123         }
1124         if (TMath::Abs(pxs) > 1.e-2 ||
1125             TMath::Abs(pys) > 1.e-2 ||
1126             TMath::Abs(pzs) > 1.e-1) {
1127             printf("%e %e %e %e\n", pxs, pys, pzs, es);
1128 //              Fatal("Quench()", "4-Momentum non-conservation");
1129         }
1130         
1131     } // end quenching loop (systems)
1132 // Clean-up
1133     for (Int_t i = 0; i < numpart; i++)
1134     {
1135         imo =  fPyjets->K[2][i];
1136         if (imo > 1000) {
1137             fPyjets->K[2][i] = fPyjets->K[2][i] % 1000;
1138         }
1139     }
1140 //      this->Pylist(1);
1141 } // end quench
1142
1143
1144 void AliPythia::Pyquen(Double_t a, Int_t ibf, Double_t b)
1145 {
1146     // Igor Lokthine's quenching routine
1147     pyquen(a, ibf, b);
1148 }
1149
1150 void AliPythia::Pyevnw()
1151 {
1152     // New multiple interaction scenario
1153     pyevnw();
1154 }
1155
1156 void AliPythia::GetQuenchingParameters(Double_t& xp, Double_t& yp, Double_t z[4])
1157 {
1158     // Return event specific quenching parameters
1159     xp = fXJet;
1160     yp = fYJet;
1161     for (Int_t i = 0; i < 4; i++) z[i] = fZQuench[i];
1162
1163 }
1164
1165 void AliPythia::ConfigHeavyFlavor()
1166 {
1167     //
1168     // Default configuration for Heavy Flavor production
1169     //
1170     // All QCD processes
1171     //
1172     SetMSEL(1);
1173     
1174     // No multiple interactions
1175     SetMSTP(81,0);
1176     // Initial/final parton shower on (Pythia default)
1177     SetMSTP(61,1);
1178     SetMSTP(71,1);
1179     
1180     // 2nd order alpha_s
1181     SetMSTP(2,2);
1182     
1183     // QCD scales
1184     SetMSTP(32,2);
1185     SetPARP(34,1.0);
1186 }
1187
1188 void AliPythia::AtlasTuning()
1189 {
1190     //
1191     // Configuration for the ATLAS tuning
1192         SetMSTP(51, kCTEQ5L);      // CTEQ5L pdf
1193         SetMSTP(81,1);             // Multiple Interactions ON
1194         SetMSTP(82,4);             // Double Gaussian Model
1195         SetPARP(82,1.8);           // [GeV]    PT_min at Ref. energy
1196         SetPARP(89,1000.);         // [GeV]   Ref. energy
1197         SetPARP(90,0.16);          // 2*epsilon (exponent in power law)
1198         SetPARP(83,0.5);           // Core density in proton matter distribution (def.value)
1199         SetPARP(84,0.5);           // Core radius
1200         SetPARP(85,0.33);          // Regulates gluon prod. mechanism
1201         SetPARP(86,0.66);          // Regulates gluon prod. mechanism
1202         SetPARP(67,1);             // Regulates Initial State Radiation
1203 }