New quenching algorithm.
[u/mrichter/AliRoot.git] / PYTHIA6 / AliPythia.cxx
index 798e76a..0a8d044 100644 (file)
@@ -17,6 +17,9 @@
 
 #include "AliPythia.h"
 #include "AliPythiaRndm.h"
+#include "../FASTSIM/AliFastGlauber.h"
+#include "../FASTSIM/AliQuenchingWeights.h"
+#include "TVector3.h"
 
 ClassImp(AliPythia)
 
@@ -49,7 +52,8 @@ AliPythia::AliPythia()
 //  Set random number
     if (!AliPythiaRndm::GetPythiaRandom()) 
       AliPythiaRndm::SetPythiaRandom(GetRandom());
-
+    fGlauber          = 0;
+    fQuenchingWeights = 0;
 }
 
 void AliPythia::ProcInit(Process_t process, Float_t energy, StrucFunc_t strucfunc)
@@ -136,30 +140,51 @@ void AliPythia::ProcInit(Process_t process, Float_t energy, StrucFunc_t strucfun
 //   
 //      select Pythia min. bias model
        SetMSEL(0);
-       SetMSUB(92,1);      // single diffraction AB-->XB
-       SetMSUB(93,1);      // single diffraction AB-->AX
-       SetMSUB(94,1);      // double diffraction
-       SetMSUB(95,1);      // low pt production
-       SetMSTP(81,1);      // multiple interactions switched on
-       SetMSTP(82,3);      // model with varying impact param. & a single Gaussian
-       SetPARP(82,3.47);   // set value pT_0  for turn-off of the cross section of                  
-                            // multiple interaction at a reference energy = 14000 GeV
-       SetPARP(89,14000.); // reference energy for the above parameter
-       SetPARP(90,0.174);  // set exponent for energy dependence of pT_0
+       SetMSUB(92,1);             // single diffraction AB-->XB
+       SetMSUB(93,1);             // single diffraction AB-->AX
+       SetMSUB(94,1);             // double diffraction
+       SetMSUB(95,1);             // low pt production
+
+//
+// ATLAS Tuning
+//
+        SetMSTP(51,7);             // CTEQ5L pdf
+       SetMSTP(81,1);             // Multiple Interactions ON
+       SetMSTP(82,4);             // Double Gaussian Model
+
+       SetPARP(82,1.8);           // [GeV]    PT_min at Ref. energy
+       SetPARP(89,1000.);         // [GeV]   Ref. energy
+       SetPARP(90,0.16);          // 2*epsilon (exponent in power law)
+       SetPARP(83,0.5);           // Core density in proton matter distribution (def.value)
+       SetPARP(84,0.5);           // Core radius
+       SetPARP(85,0.33);          // Regulates gluon prod. mechanism
+       SetPARP(86,0.66);          // Regulates gluon prod. mechanism
+       SetPARP(67,1);             // Regulates Initial State Radiation
+       break;
     case kPyMbNonDiffr:
 // Minimum Bias pp-Collisions
 //
 //   
 //      select Pythia min. bias model
        SetMSEL(0);
-       SetMSUB(95,1);      // low pt production
-       SetMSTP(81,1);      // multiple interactions switched on
-       SetMSTP(82,3);      // model with varying impact param. & a single Gaussian
-       SetPARP(82,3.47);   // set value pT_0  for turn-off of the cross section of                  
-                            // multiple interaction at a reference energy = 14000 GeV
-       SetPARP(89,14000.); // reference energy for the above parameter
-       SetPARP(90,0.174);  // set exponent for energy dependence of pT_0
+       SetMSUB(95,1);             // low pt production
+
+//
+// ATLAS Tuning
+//
+       
+       SetMSTP(51,7);             // CTEQ5L pdf
+       SetMSTP(81,1);             // Multiple Interactions ON
+       SetMSTP(82,4);             // Double Gaussian Model
+
+       SetPARP(82,1.8);           // [GeV]    PT_min at Ref. energy
+       SetPARP(89,1000.);         // [GeV]   Ref. energy
+       SetPARP(90,0.16);          // 2*epsilon (exponent in power law)
+       SetPARP(83,0.5);           // Core density in proton matter distribution (def.value)
+       SetPARP(84,0.5);           // Core radius
+       SetPARP(85,0.33);          // Regulates gluon prod. mechanism
+       SetPARP(86,0.66);          // Regulates gluon prod. mechanism
+       SetPARP(67,1);             // Regulates Initial State Radiation
        break;
     case kPyJets:
 //
@@ -527,6 +552,28 @@ void AliPythia::Pyrobo(Int_t imi, Int_t ima, Double_t the, Double_t phi, Double_
 
 
 
+void AliPythia::InitQuenching(Float_t cMin, Float_t cMax, Float_t qTransport, Float_t maxLength, Int_t iECMethod)
+{
+// Initializes 
+// (1) The quenching model using quenching weights according to C. Salgado and U. Wiedemann
+// (2) The nuclear geometry using the Glauber Model
+//     
+
+
+    fGlauber = new AliFastGlauber();
+    fGlauber->Init(2);
+    fGlauber->SetCentralityClass(cMin, cMax); 
+
+    fQuenchingWeights = new AliQuenchingWeights();
+    fQuenchingWeights->InitMult();
+    fQuenchingWeights->SetQTransport(qTransport);
+    fQuenchingWeights->SetECMethod(AliQuenchingWeights::kECMethod(iECMethod));
+    fQuenchingWeights->SetLengthMax(Int_t(maxLength));
+    fQuenchingWeights->SampleEnergyLoss();
+    
+}
+
+
 void  AliPythia::Quench()
 {
 //
@@ -534,256 +581,493 @@ void  AliPythia::Quench()
 //  Simple Jet Quenching routine:
 //  =============================
 //  The jet formed by all final state partons radiated by the parton created 
-//  in the hard collisions is quenched by a factor z using:
+//  in the hard collisions is quenched by a factor (1-z) using light cone variables in 
+//  the initial parton reference frame:
 //  (E + p_z)new = (1-z) (E + p_z)old
 //
+//
+//
+//
 //   The lost momentum is first balanced by one gluon with virtuality > 0.   
 //   Subsequently the gluon splits to yield two gluons with E = p.
 //
-    Float_t p0[2][5];
-    Float_t p1[2][5];
-    Float_t p2[2][5];
+//
+// 
+    const Int_t kGluons = 1;
+    
+    Double_t p0[2][5];
+    Double_t p1[2][5];
+    Double_t p2[2][5];
     Int_t   klast[2] = {-1, -1};
     Int_t   kglu[2];
-    for (Int_t i = 0; i < 4; i++)
-    {
-       p0[0][i] = 0.;
-       p0[1][i] = 0.;
-       p1[0][i] = 0.;
-       p1[1][i] = 0.;
-       p2[0][i] = 0.;
-       p2[1][i] = 0.;
-    }
 
     Int_t numpart   = fPyjets->N;
-
-    for (Int_t i = 0; i < numpart; i++)
-    {
-       Int_t imo =  fPyjets->K[2][i];
-       Int_t kst =  fPyjets->K[0][i];
-       Int_t pdg =  fPyjets->K[1][i];
+    Double_t px = 0., py = 0., pz = 0., e = 0., m = 0., p = 0., pt = 0., theta = 0.;
+    Double_t pxq[2], pyq[2], pzq[2], eq[2], yq[2], mq[2], pq[2], phiq[2], thetaq[2], ptq[2];
+    Bool_t  quenched[2];
+    Double_t phi;
+    Double_t zInitial[2], wjtKick[2];
+    Int_t   imo, kst, pdg;
+//
+//  Primary partons
+//
+    
+    for (Int_t i = 6; i <= 7; i++) {
+       Int_t j = i - 6;
        
-//      Quarks and gluons only
-       if (pdg != 21 && TMath::Abs(pdg) > 6) continue;
-
-//      Particles from hard scattering only
-
-
-       Float_t px = fPyjets->P[0][i];
-       Float_t py = fPyjets->P[1][i];
-       Float_t pz = fPyjets->P[2][i];
-       Float_t e  = fPyjets->P[3][i];
-       Float_t m  = fPyjets->P[4][i];
-       Float_t pt = TMath::Sqrt(px * px + py * py);
-//      Skip comment lines
-       if (kst != 1 && kst != 2) continue;
-
-       Float_t mt = TMath::Sqrt(px * px + py * py + m * m);
-
-       //
-       // Some cuts to be in a save kinematic region
-       //
-       if (imo != 7 && imo != 8) continue;
-       Int_t index = imo - 7;
-       klast[index] = i;
+       pxq[j]    = fPyjets->P[0][i];
+       pyq[j]    = fPyjets->P[1][i];
+       pzq[j]    = fPyjets->P[2][i];
+       eq[j]     = fPyjets->P[3][i];
+       mq[j]     = fPyjets->P[4][i];
+       yq[j]     = 0.5 * TMath::Log((e + pz + 1.e-14) / (e - pz + 1.e-14));
+       pq[j]     = TMath::Sqrt(pxq[j] * pxq[j] + pyq[j] * pyq[j] + pzq[j] * pzq[j]);
+       phiq[j]   = TMath::Pi()+TMath::ATan2(-pyq[j], -pxq[j]);
+       ptq[j]    = TMath::Sqrt(pxq[j] * pxq[j] + pyq[j] * pyq[j]);
+       thetaq[j] = TMath::ATan2(ptq[j], pzq[j]);
+       phi       = phiq[j];
        
-       p0[index][0] += px;
-       p0[index][1] += py;
-       p0[index][2] += pz;
-       p0[index][3] += e;      
+       // Quench only central jets
+       if (TMath::Abs(yq[j]) > 2.5) {
+           zInitial[j] = 0.;
+       } else {
+           pdg =  fPyjets->K[1][i];
+           
+           // Get length in nucleus
+           Double_t l;
+           fGlauber->GetLengthsForPythia(1, &phi, &l, -1.);
+           //
+           // Energy loss for given length and parton typr 
+           Int_t itype = (pdg == 21) ? 2 : 1;
+           Double_t eloss   = fQuenchingWeights->GetELossRandom(itype, l, eq[j]);
+           //
+           // Extra pt
+           wjtKick[j] = TMath::Sqrt(l *  fQuenchingWeights->GetQTransport());
+           //
+           // Fractional energy loss
+           zInitial[j] = eloss / eq[j];
+           //
+           // Avoid complete loss
+           //
+           if (zInitial[j] == 1.) zInitial[j] = 0.95;
+           //
+           // Some debug printing
+           printf("Initial parton # %3d, Type %3d Energy %10.3f Phi %10.3f Length %10.3f Loss %10.3f Kick %10.3f\n", 
+                  j, itype, eq[j], phi, l, eloss, wjtKick[j]);
+       }
        
-       //
-       // Fix z
-       //
-
-       Float_t z       = 0.2;
-       Float_t eppzOld = e + pz;
-       Float_t empzOld = e - pz;
-
-
-       //
-       // Kinematics of the original parton
-       // 
-
-       Float_t eppzNew = (1. - z) * eppzOld;
-       Float_t empzNew = empzOld - mt * mt * z / eppzOld;
-       Float_t eNew0    = 0.5 * (eppzNew + empzNew);
-       Float_t pzNew0   = 0.5 * (eppzNew - empzNew);
-       //
-        // Skip if pt too small
-       // 
-       if (m * m > eppzNew * empzNew) continue;
-       Float_t ptNew    = TMath::Sqrt(eppzNew * empzNew - m * m);
-       Float_t pxNew0   = ptNew / pt * px;
-       Float_t pyNew0   = ptNew / pt * py;     
-
-       p1[index][0] += pxNew0;
-       p1[index][1] += pyNew0;
-       p1[index][2] += pzNew0;
-       p1[index][3] += eNew0;  
-       //
-       // Update event record
-       //
-       fPyjets->P[0][i] = pxNew0;
-       fPyjets->P[1][i] = pyNew0;
-       fPyjets->P[2][i] = pzNew0;
-       fPyjets->P[3][i] = eNew0;
+       quenched[j] = (zInitial[j] > 0.01);
        
     }
-
-    //
-    // Gluons
-    // 
+  
+//
+// Radiated partons
+//
+    zInitial[0] = 1. - TMath::Power(1. - zInitial[0], 1./Double_t(kGluons));
+    zInitial[1] = 1. - TMath::Power(1. - zInitial[1], 1./Double_t(kGluons));
+    wjtKick[0]  = wjtKick[0] / TMath::Sqrt(Double_t(kGluons));
+    wjtKick[1]  = wjtKick[1] / TMath::Sqrt(Double_t(kGluons));
+//    this->Pylist(1);
     
-    for (Int_t k = 0; k < 2; k++) 
-    {
-       for (Int_t j = 0; j < 4; j++) 
+    for (Int_t iglu = 0; iglu < kGluons; iglu++) {
+       for (Int_t k = 0; k < 4; k++)
        {
-           p2[k][j] = p0[k][j] - p1[k][j];
+           p0[0][k] = 0.; p0[1][k] = 0.;
+           p1[0][k] = 0.; p1[1][k] = 0.;
+           p2[0][k] = 0.; p2[1][k] = 0.;
        }
-       p2[k][4] = p2[k][3] * p2[k][3] - p2[k][0] * p2[k][0] - p2[k][1] * p2[k][1] - p2[k][2] * p2[k][2];
-
-       if (p2[k][4] > 0.)
+       
+       Int_t nq[2] = {0, 0};
+       
+       for (Int_t i = 0; i < numpart; i++)
        {
+           imo =  fPyjets->K[2][i];
+           kst =  fPyjets->K[0][i];
+           pdg =  fPyjets->K[1][i];
+
+           
+           
+//      Quarks and gluons only
+           if (pdg != 21 && TMath::Abs(pdg) > 6) continue;
+//      Particles from hard scattering only
+           if (imo > 8 && imo < 1000) imo = fPyjets->K[2][imo - 1];
+           if (imo != 7 && imo != 8 && imo != 1007 && imo != 1008) continue;
+           
+//      Skip comment lines
+           if (kst != 1 && kst != 2) continue;
+//
+//      Parton kinematic
+           px    = fPyjets->P[0][i];
+           py    = fPyjets->P[1][i];
+           pz    = fPyjets->P[2][i];
+           e     = fPyjets->P[3][i];
+           m     = fPyjets->P[4][i];
+           pt    = TMath::Sqrt(px * px + py * py);
+           p     = TMath::Sqrt(px * px + py * py + pz * pz); 
+           phi   = TMath::Pi() + TMath::ATan2(-py, -px);
+           theta = TMath::ATan2(pt, pz);
+
+//
+//      Save 4-momentum sum for balancing      
+           Int_t index = imo - 7;
+           if (index >=  1000) index -= 1000;
+
+           p0[index][0] += px;
+           p0[index][1] += py;
+           p0[index][2] += pz;
+           p0[index][3] += e;
+           
+//      Don't quench radiated gluons
+//
+           if (imo == 1007 || imo == 1008) {
+               p1[index][0] += px;
+               p1[index][1] += py;
+               p1[index][2] += pz;
+               p1[index][3] += e;      
+               continue;
+           }
+           
+//
+
+           klast[index] = i;
+//
+//      Fractional energy loss
+           Double_t z = zInitial[index];
+           if (!quenched[index]) continue;
+           //
+           //
+           //      Transform into frame in which initial parton is along z-axis
+           //
+           TVector3 v(px, py, pz);
+           v.RotateZ(-phiq[index]);
+           v.RotateY(-thetaq[index]);
+           Double_t pxs = v.X(); Double_t pys = v.Y(); Double_t pl  = v.Z();
+           Double_t jt  = TMath::Sqrt(pxs * pxs + pys * pys);
+           Double_t mt2 = jt * jt + m * m;
+           Double_t mt  = TMath::Sqrt(mt2);
+           Double_t zmin = 0.;
+           Double_t zmax = 1.;     
+           //
+           // z**2 mt**2 - pt**2 + pt'**2 > 0
+            // z**2 mt**2 + mt'**2 + m**2 - pt**2
+            // z**2 mt**2 + (1-z)**2 mt**2 + m**2 - pt**2
+            // mt**2(z**2 + 1 + z**2  - 2z) + m**2 - pt**2
+            // mt**2(2z**2 + 1 - 2z) + m**2 - pt**2 > 0
+            // mt**2(2z**2 + 1 - 2z) + 2 m**2 - mt**2 > 0
+            // mt**2(2z**2 - 2z) + 2 m**2 > 0
+            // z mt**2 (1 - z) -  m**2 < 0
+           // z**2 - z + 1/4 > 1/4 - m**2/mt**2
+            // (z-1/2)**2 > 1/4 - m**2/mt**2
+            // |z-1/2| > sqrt(1/4 - m**2/mt**2)
+            //
+            // m/mt < 1/2
+            // mt   > 2m
+           //
+           if (mt < 2. * m) {
+               printf("No phase space for quenching !: mt (%e) < 2 m (%e) \n", mt, m);
+               p1[index][0] += px;
+               p1[index][1] += py;
+               p1[index][2] += pz;
+               p1[index][3] += e;
+               continue;
+           } else {
+               zmin = 0.5 - TMath::Sqrt(0.25 - m * m / mt2);
+               if (z < zmin) {
+                   printf("No phase space for quenching ??: z (%e) < zmin (%e) \n", z, zmin);
+//                 z = zmin * 1.01;
+
+                   p1[index][0] += px;
+                   p1[index][1] += py;
+                   p1[index][2] += pz;
+                   p1[index][3] += e;
+                   continue;
+
+               }
+           }
+           //
+           // Kinematic limit on z
+           //
 
+           if (m > 0.) {
+               zmax = 1. - m / TMath::Sqrt(m * m + jt * jt);
+               if (z > zmax) {
+                   printf("We have to put z to the kinematic limit %e %e \n", z, zmax);
+                   z = 0.9999 * zmax;
+               } // z > zmax
+               if (z < 0.01) {
+//
+//           If z is too small, there is no phase space for quenching
+//
+                   printf("No phase space for quenching ! %e  \n", z);
+                   
+                   p1[index][0] += px;
+                   p1[index][1] += py;
+                   p1[index][2] += pz;
+                   p1[index][3] += e;
+                   continue;
+               }
+           } // massive particles
+           
            //
-           // Bring gluon back to mass shell via momentum scaling 
-           // (momentum will not be conserved, but energy)
+           // Change light-cone kinematics rel. to initial parton
+           //  
+           Double_t eppzOld = e + pl;
+           Double_t empzOld = e - pl;
+           
+           Double_t eppzNew = (1. - z) * eppzOld;
+           Double_t empzNew = empzOld - mt2 * z / eppzOld;
+           Double_t eNew0    = 0.5 * (eppzNew + empzNew);
+           Double_t pzNew0   = 0.5 * (eppzNew - empzNew);
+           
+           Double_t ptNew;
+           //
+           // if mt very small (or sometimes even < 0 for numerical reasons) set it to 0
+           Double_t mt2New = eppzNew * empzNew;
+           if (mt2New < 1.e-8) mt2New = 0.;
+           
+           if (m * m > mt2New) {
+               //
+               // This should not happen 
+               //
+               Fatal("Quench()", "This should never happen %e %e %e!", m, eppzNew, empzNew);
+               ptNew = 0;
+           } else {
+               ptNew    = TMath::Sqrt(mt2New - m * m);
+           }
+
+           
+           //
+           //     Calculate new px, py
            //
-           // not used anymore
+           Double_t pxNew0   = ptNew / jt * pxs;
+           Double_t pyNew0   = ptNew / jt * pys;       
 /*
-           Float_t psq   = p2[k][0] * p2[k][0] + p2[k][1] * p2[k][1] + p2[k][2] * p2[k][2];
-           Float_t fact  = TMath::Sqrt(1. + p2[k][4] / psq);
-           p2[k][0] *= fact;
-           p2[k][1] *= fact;
-           p2[k][2] *= fact;
-           p2[k][3]  = TMath::Sqrt(psq) * fact;
-           p2[k][4]  = 0.;
+           Double_t dpx = pxs - pxNew0;
+           Double_t dpy = pys - pyNew0;
+           Double_t dpz = pl  - pzNew0;
+           Double_t de  = e   - eNew0;
+           Double_t dmass2 = de * de  - dpx * dpx - dpy * dpy - dpz * dpz;
 */
+           //
+           //      Rotate back
+           //  
+           TVector3 w(pxNew0, pyNew0, pzNew0);
+           w.RotateY(thetaq[index]);
+           w.RotateZ(phiq[index]);
+           pxNew0 = w.X(); pyNew0 = w.Y(); pzNew0 = w.Z();
+           
+           
+           p1[index][0] += pxNew0;
+           p1[index][1] += pyNew0;
+           p1[index][2] += pzNew0;
+           p1[index][3] += eNew0;      
+           //
+           // Update event record
+           //
+           fPyjets->P[0][i] = pxNew0;
+           fPyjets->P[1][i] = pyNew0;
+           fPyjets->P[2][i] = pzNew0;
+           fPyjets->P[3][i] = eNew0;
+           nq[index]++;
+           
        }
-    }
-
-    if (p2[0][4] > 0.) {
-       p2[0][4] = TMath::Sqrt(p2[0][4]);
-    } else {
-       printf("Warning negative mass squared ! \n");
-    }
-    
-    if (p2[1][4] > 0.) {
-       p2[1][4] = TMath::Sqrt(p2[1][4]);
-    } else {
-       printf("Warning negative mass squared ! \n");
-    }
-    
-    //
-    // Add the gluons
-    //
-    
-    
-    for (Int_t i = 0; i < 2; i++) {
-       Int_t ish, jmin, jmax, iGlu, iNew;
-       Int_t in = klast[i];
-       ish = 0;
-       
-       if (in == -1) continue;
-       if (i == 1 && klast[1] > klast[0]) in += ish;
-       
-       jmin = in - 1;
-       ish  = 1;
-
-       if (p2[i][4] > 0) ish = 2;
        
-       iGlu = in;
-       iNew = in + ish;
-       jmax = numpart + ish - 1;
-       if (fPyjets->K[0][in-1] == 1 || fPyjets->K[0][in-1] == 21 || fPyjets->K[0][in-1] == 11) {
-           jmin = in;
-           iGlu = in + 1;
-           iNew = in;
-       }
-
-       kglu[i] = iGlu;
+       //
+       // Gluons
+       // 
        
-       for (Int_t j = jmax; j > jmin; j--)
+       for (Int_t k = 0; k < 2; k++) 
        {
-           for (Int_t k = 0; k < 5; k++) {
-               fPyjets->K[k][j] =  fPyjets->K[k][j-ish];
-               fPyjets->P[k][j] =  fPyjets->P[k][j-ish];
-               fPyjets->V[k][j] =  fPyjets->V[k][j-ish];
-           }
-       } // end shifting
-       numpart += ish;
-       (fPyjets->N) += ish;
-       
-       if (ish == 1) {
-           fPyjets->P[0][iGlu] = p2[i][0];
-           fPyjets->P[1][iGlu] = p2[i][1];
-           fPyjets->P[2][iGlu] = p2[i][2];
-           fPyjets->P[3][iGlu] = p2[i][3];
-           fPyjets->P[4][iGlu] = p2[i][4];
-           
-           fPyjets->K[0][iGlu] = 2;
-           fPyjets->K[1][iGlu] = 21;   
-           fPyjets->K[2][iGlu] = fPyjets->K[2][iNew];
-           fPyjets->K[3][iGlu] = -1;   
-           fPyjets->K[4][iGlu] = -1;
-       } else {
            //
-           // Split gluon in rest frame.
+           // Check if there was phase-space for quenching
            //
-           Double_t bx =  p2[i][0] / p2[i][3];
-           Double_t by =  p2[i][1] / p2[i][3];
-           Double_t bz =  p2[i][2] / p2[i][3];
+           if (nq[k] == 0) quenched[k] = kFALSE;
            
-           Float_t pst  = p2[i][4] / 2.;
+           if (!quenched[k]) continue;
            
-           Float_t cost = 2. * gRandom->Rndm() - 1.;
-           Float_t sint = TMath::Sqrt(1. - cost * cost);
-           Float_t phi = 2. * TMath::Pi() * gRandom->Rndm();
+           for (Int_t j = 0; j < 4; j++) 
+           {
+               p2[k][j] = p0[k][j] - p1[k][j];
+           }
+           p2[k][4] = p2[k][3] * p2[k][3] - p2[k][0] * p2[k][0] - p2[k][1] * p2[k][1] - p2[k][2] * p2[k][2];
+
+           if (p2[k][4] > 0.) {
+               p2[k][4] = TMath::Sqrt(p2[k][4]);
+           } else {
+               printf("Warning negative mass squared in system %d %f ! \n", k, zInitial[k]);
+               printf("Kinematics %10.3e %10.3e %10.3e %10.3e %10.3e \n", p2[k][0], p2[k][1], p2[k][2], p2[k][3], p2[k][4]);
+               if (p2[k][4] < -0.1) Fatal("Boost", "Negative mass squared !");
+               p2[k][4] = 0.;
+           }
+           //
+           // jt-kick
+           //
+           /*
+           TVector3 v(p2[k][0], p2[k][1], p2[k][2]);
+           v.RotateZ(-phiq[k]);
+           v.RotateY(-thetaq[k]);
+           Double_t px = v.X(); Double_t py = v.Y(); Double_t pz  = v.Z();        
+           Double_t r       = AliPythiaRndm::GetPythiaRandom()->Rndm();
+           Double_t  jtKick  = wjtKick[k] * TMath::Sqrt(-TMath::Log(r));
+           Double_t phiKick = 2. * TMath::Pi() * AliPythiaRndm::GetPythiaRandom()->Rndm();
+           px += jtKick * TMath::Cos(phiKick);
+           py += jtKick * TMath::Sin(phiKick);
+           TVector3 w(px, py, pz);
+           w.RotateY(thetaq[k]);
+           w.RotateZ(phiq[k]);
+           p2[k][0] = w.X(); p2[k][1] = w.Y(); p2[k][2] = w.Z();
+           p2[k][3] = TMath::Sqrt(p2[k][0] * p2[k][0] + p2[k][1] * p2[k][1] + p2[k][2] * p2[k][2] + p2[k][4] * p2[k][4]);
+           */
+       }
+       
+       //
+       // Add the gluons
+       //
+       
+       Int_t ish = 0;    
+       for (Int_t i = 0; i < 2; i++) {
+           Int_t jmin, jmax, iGlu, iNew;
+           if (!quenched[i]) continue;
+//
+//      Last parton from shower i
+           Int_t in = klast[i];
+//
+//      Continue if no parton in shower i selected
+           if (in == -1) continue;
+//  
+//      If this is the second initial parton and it is behind the first move pointer by previous ish
+           if (i == 1 && klast[1] > klast[0]) in += ish;
+//
+//      Starting index
            
-           Float_t pz1 =   pst * cost;
-           Float_t pz2 =  -pst * cost;
-           Float_t pt1 =   pst * sint;
-           Float_t pt2 =  -pst * sint;
-           Float_t px1 =   pt1 * TMath::Cos(phi);
-           Float_t py1 =   pt1 * TMath::Sin(phi);          
-           Float_t px2 =   pt2 * TMath::Cos(phi);
-           Float_t py2 =   pt2 * TMath::Sin(phi);          
+           jmin = in - 1;
+// How many additional gluons will be generated
+           ish  = 1;
+           if (p2[i][4] > 0.05) ish = 2;
+//
+//      Position of gluons
+           iGlu = in;
+           iNew = in + ish;
+           jmax = numpart + ish - 1;
            
-           fPyjets->P[0][iGlu] = px1;
-           fPyjets->P[1][iGlu] = py1;
-           fPyjets->P[2][iGlu] = pz1;
-           fPyjets->P[3][iGlu] = pst;
-           fPyjets->P[4][iGlu] = 0.;
+           if (fPyjets->K[0][in-1] == 1 || fPyjets->K[0][in-1] == 21 || fPyjets->K[0][in-1] == 11) {
+               jmin = in;
+               iGlu = in + 1;
+               iNew = in;
+           }
            
-           fPyjets->K[0][iGlu] = 2;
-           fPyjets->K[1][iGlu] = 21;   
-           fPyjets->K[2][iGlu] = fPyjets->K[2][iNew];
-           fPyjets->K[3][iGlu] = -1;   
-           fPyjets->K[4][iGlu] = -1;
-
-           fPyjets->P[0][iGlu+1] = px2;
-           fPyjets->P[1][iGlu+1] = py2;
-           fPyjets->P[2][iGlu+1] = pz2;
-           fPyjets->P[3][iGlu+1] = pst;
-           fPyjets->P[4][iGlu+1] = 0.;
+           kglu[i] = iGlu;
+//     
+// Shift stack
+//
+           for (Int_t j = jmax; j > jmin; j--)
+           {
+               for (Int_t k = 0; k < 5; k++) {
+                   fPyjets->K[k][j] =  fPyjets->K[k][j-ish];
+                   fPyjets->P[k][j] =  fPyjets->P[k][j-ish];
+                   fPyjets->V[k][j] =  fPyjets->V[k][j-ish];
+               }
+           } // end shifting
            
-           fPyjets->K[0][iGlu+1] = 2;
-           fPyjets->K[1][iGlu+1] = 21; 
-           fPyjets->K[2][iGlu+1] = fPyjets->K[2][iNew];
-           fPyjets->K[3][iGlu+1] = -1; 
-           fPyjets->K[4][iGlu+1] = -1;
-           SetMSTU(1,0);
-           SetMSTU(2,0);
-
-           //
-           // Boost back
-           //
-           Pyrobo(iGlu + 1, iGlu + 2, 0., 0., bx, by, bz);
-
+           numpart += ish;
+           (fPyjets->N) += ish;
+           
+           if (ish == 1) {
+               fPyjets->P[0][iGlu] = p2[i][0];
+               fPyjets->P[1][iGlu] = p2[i][1];
+               fPyjets->P[2][iGlu] = p2[i][2];
+               fPyjets->P[3][iGlu] = p2[i][3];
+               fPyjets->P[4][iGlu] = p2[i][4];
+               
+               fPyjets->K[0][iGlu] = 2;
+               fPyjets->K[1][iGlu] = 21;       
+               fPyjets->K[2][iGlu] = fPyjets->K[2][iNew] + 1000;
+               fPyjets->K[3][iGlu] = -1;       
+               fPyjets->K[4][iGlu] = -1;
+           } else {
+               //
+               // Split gluon in rest frame.
+               //
+               Double_t bx   =  p2[i][0] / p2[i][3];
+               Double_t by   =  p2[i][1] / p2[i][3];
+               Double_t bz   =  p2[i][2] / p2[i][3];
+               Double_t pst  =  p2[i][4] / 2.;
+               //
+               // Isotropic decay ????
+               Double_t cost = 2. * gRandom->Rndm() - 1.;
+               Double_t sint = TMath::Sqrt(1. - cost * cost);
+               Double_t phi =  2. * TMath::Pi() * gRandom->Rndm();
+               
+               Double_t pz1 =   pst * cost;
+               Double_t pz2 =  -pst * cost;
+               Double_t pt1 =   pst * sint;
+               Double_t pt2 =  -pst * sint;
+               Double_t px1 =   pt1 * TMath::Cos(phi);
+               Double_t py1 =   pt1 * TMath::Sin(phi);     
+               Double_t px2 =   pt2 * TMath::Cos(phi);
+               Double_t py2 =   pt2 * TMath::Sin(phi);     
+               
+               fPyjets->P[0][iGlu] = px1;
+               fPyjets->P[1][iGlu] = py1;
+               fPyjets->P[2][iGlu] = pz1;
+               fPyjets->P[3][iGlu] = pst;
+               fPyjets->P[4][iGlu] = 0.;
+               
+               fPyjets->K[0][iGlu] = 2;
+               fPyjets->K[1][iGlu] = 21;       
+               fPyjets->K[2][iGlu] = fPyjets->K[2][iNew] + 1000;
+               fPyjets->K[3][iGlu] = -1;       
+               fPyjets->K[4][iGlu] = -1;
+               
+               fPyjets->P[0][iGlu+1] = px2;
+               fPyjets->P[1][iGlu+1] = py2;
+               fPyjets->P[2][iGlu+1] = pz2;
+               fPyjets->P[3][iGlu+1] = pst;
+               fPyjets->P[4][iGlu+1] = 0.;
+               
+               fPyjets->K[0][iGlu+1] = 2;
+               fPyjets->K[1][iGlu+1] = 21;     
+               fPyjets->K[2][iGlu+1] = fPyjets->K[2][iNew] + 1000;
+               fPyjets->K[3][iGlu+1] = -1;     
+               fPyjets->K[4][iGlu+1] = -1;
+               SetMSTU(1,0);
+               SetMSTU(2,0);
+               //
+               // Boost back
+               //
+               Pyrobo(iGlu + 1, iGlu + 2, 0., 0., bx, by, bz);
+           }
+       } // end adding gluons
+       //
+       // Check energy conservation
+       Double_t pxs = 0.;
+       Double_t pys = 0.;
+       Double_t pzs = 0.;      
+       Double_t es  = 14000.;
+       
+       for (Int_t i = 0; i < numpart; i++)
+       {
+           kst =  fPyjets->K[0][i];
+           if (kst != 1 && kst != 2) continue;
+           pxs += fPyjets->P[0][i];
+           pys += fPyjets->P[1][i];
+           pzs += fPyjets->P[2][i];        
+           es  -= fPyjets->P[3][i];        
+       }
+       if (TMath::Abs(pxs) > 1.e-2 ||
+           TMath::Abs(pys) > 1.e-2 ||
+           TMath::Abs(pzs) > 1.e-1) {
+           printf("%e %e %e %e\n", pxs, pys, pzs, es);
+           this->Pylist(1);
+           Fatal("Quench()", "4-Momentum non-conservation");
        }
-    } // end adding gluons
-} // end quench
-
 
+    } // end quenchin loop
+    // Clean-up
+    for (Int_t i = 0; i < numpart; i++)
+    {
+       imo =  fPyjets->K[2][i];
+       if (imo > 1000) fPyjets->K[2][i] -= 1000;
+    }
+       
+} // end quench