]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PYTHIA6/AliGenPythia.cxx
Corrected call to the static method AliBitPacking::UnpackWord
[u/mrichter/AliRoot.git] / PYTHIA6 / 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 /* $Id$ */
17
18 //
19 // Generator using the TPythia interface (via AliPythia)
20 // to generate pp collisions.
21 // Using SetNuclei() also nuclear modifications to the structure functions
22 // can be taken into account. This makes, of course, only sense for the
23 // generation of the products of hard processes (heavy flavor, jets ...)
24 //
25 // andreas.morsch@cern.ch
26 //
27
28 #include <TDatabasePDG.h>
29 #include <TParticle.h>
30 #include <TPDGCode.h>
31 #include <TSystem.h>
32 #include <TTree.h>
33 #include "AliConst.h"
34 #include "AliDecayerPythia.h"
35 #include "AliGenPythia.h"
36 #include "AliHeader.h"
37 #include "AliGenPythiaEventHeader.h"
38 #include "AliPythia.h"
39 #include "AliPythiaRndm.h"
40 #include "AliRun.h"
41 #include "AliStack.h"
42 #include "AliRunLoader.h"
43 #include "AliMC.h"
44 #include "pyquenCommon.h"
45
46 ClassImp(AliGenPythia)
47
48 AliGenPythia::AliGenPythia()
49                  :AliGenMC()
50 {
51 // Default Constructor
52   fParticles = 0;
53   fPythia    = 0;
54   fHeader = 0;
55   fReadFromFile = 0;
56   fEventTime = 0.;
57   fInteractionRate = 0.;
58   fTimeWindow = 0.;
59   fEventsTime = 0;
60   fCurSubEvent = 0;
61   fDecayer   = new AliDecayerPythia();
62   SetEventListRange();
63   SetJetPhiRange();
64   SetJetEtaRange();
65   SetJetEtRange();
66   SetGammaPhiRange();
67   SetGammaEtaRange();
68   SetPtKick();
69   SetQuench();
70   SetHadronisation();  
71   SetTriggerParticle();
72   SetNuclei(0,0);
73   fSetNuclei = kFALSE;
74   fNewMIS    = kFALSE;
75   fHFoff     = kFALSE;
76   if (!AliPythiaRndm::GetPythiaRandom()) 
77     AliPythiaRndm::SetPythiaRandom(GetRandom());
78 }
79
80 AliGenPythia::AliGenPythia(Int_t npart)
81                  :AliGenMC(npart)
82 {
83 // default charm production at 5. 5 TeV
84 // semimuonic decay
85 // structure function GRVHO
86 //
87     fName = "Pythia";
88     fTitle= "Particle Generator using PYTHIA";
89     fXsection  = 0.;
90     fReadFromFile = 0;
91     fEventTime = 0.;
92     fInteractionRate = 0.;
93     fTimeWindow = 0.;
94     fEventsTime = 0;
95     fCurSubEvent = 0;
96     SetProcess();
97     SetStrucFunc();
98     SetForceDecay();
99     SetPtHard();
100     SetYHard();
101     SetEnergyCMS();
102     fDecayer = new AliDecayerPythia();
103     // Set random number generator 
104     if (!AliPythiaRndm::GetPythiaRandom()) 
105       AliPythiaRndm::SetPythiaRandom(GetRandom());
106     fFlavorSelect   = 0;
107     // Produced particles  
108     fParticles = new TClonesArray("TParticle",1000);
109     fHeader = 0;
110     SetEventListRange();
111     SetJetPhiRange();
112     SetJetEtaRange();
113     SetJetEtRange();
114     SetGammaPhiRange();
115     SetGammaEtaRange();
116     SetJetReconstructionMode();
117     SetQuench();
118     SetHadronisation();
119     SetPtKick();
120     SetTriggerParticle();
121     SetNuclei(0,0);
122     // Options determining what to keep in the stack (Heavy flavour generation)
123     fStackFillOpt = kFlavorSelection; // Keep particle with selected flavor
124     fFeedDownOpt = kTRUE;             // allow feed down from higher family
125     // Fragmentation on/off
126     fFragmentation = kTRUE;
127     // Default counting mode
128     fCountMode = kCountAll;
129     // Pycel
130     SetPycellParameters();
131     fSetNuclei = kFALSE;
132     fNewMIS    = kFALSE;
133     fHFoff     = kFALSE;
134 }
135
136 AliGenPythia::AliGenPythia(const AliGenPythia & Pythia)
137     :AliGenMC(Pythia)
138 {
139 // copy constructor
140     Pythia.Copy(*this);
141 }
142
143 AliGenPythia::~AliGenPythia()
144 {
145 // Destructor
146   if(fEventsTime) delete fEventsTime;
147 }
148
149 void AliGenPythia::SetInteractionRate(Float_t rate,Float_t timewindow)
150 {
151 // Generate pileup using user specified rate
152     fInteractionRate = rate;
153     fTimeWindow = timewindow;
154     GeneratePileup();
155 }
156
157 void AliGenPythia::GeneratePileup()
158 {
159 // Generate sub events time for pileup
160     fEventsTime = 0;
161     if(fInteractionRate == 0.) {
162       Warning("GeneratePileup","Zero interaction specified. Skipping pileup generation.\n");
163       return;
164     }
165
166     Int_t npart = NumberParticles();
167     if(npart < 0) {
168       Warning("GeneratePileup","Negative number of particles. Skipping pileup generation.\n");
169       return;
170     }
171
172     if(fEventsTime) delete fEventsTime;
173     fEventsTime = new TArrayF(npart);
174     TArrayF &array = *fEventsTime;
175     for(Int_t ipart = 0; ipart < npart; ipart++)
176       array[ipart] = 0.;
177
178     Float_t eventtime = 0.;
179     while(1)
180       {
181         eventtime += (AliPythiaRndm::GetPythiaRandom())->Exp(1./fInteractionRate);
182         if(eventtime > fTimeWindow) break;
183         array.Set(array.GetSize()+1);
184         array[array.GetSize()-1] = eventtime;
185       }
186
187     eventtime = 0.;
188     while(1)
189       {
190         eventtime -= (AliPythiaRndm::GetPythiaRandom())->Exp(1./fInteractionRate);
191         if(TMath::Abs(eventtime) > fTimeWindow) break;
192         array.Set(array.GetSize()+1);
193         array[array.GetSize()-1] = eventtime;
194       }
195
196     SetNumberParticles(fEventsTime->GetSize());
197 }
198
199 void AliGenPythia::SetPycellParameters(Float_t etamax, Int_t neta, Int_t nphi,
200                                        Float_t thresh, Float_t etseed, Float_t minet, Float_t r)
201 {
202 // Set pycell parameters
203     fPycellEtaMax    =  etamax;
204     fPycellNEta      =  neta;
205     fPycellNPhi      =  nphi;
206     fPycellThreshold =  thresh;
207     fPycellEtSeed    =  etseed;
208     fPycellMinEtJet  =  minet;
209     fPycellMaxRadius =  r;
210 }
211
212
213
214 void AliGenPythia::SetEventListRange(Int_t eventFirst, Int_t eventLast)
215 {
216   // Set a range of event numbers, for which a table
217   // of generated particle will be printed
218   fDebugEventFirst = eventFirst;
219   fDebugEventLast  = eventLast;
220   if (fDebugEventLast==-1) fDebugEventLast=fDebugEventFirst;
221 }
222
223 void AliGenPythia::Init()
224 {
225 // Initialisation
226     
227     SetMC(AliPythia::Instance());
228     fPythia=(AliPythia*) fMCEvGen;
229     
230 //
231     fParentWeight=1./Float_t(fNpart);
232 //
233 //  Forward Paramters to the AliPythia object
234     fDecayer->SetForceDecay(fForceDecay);    
235     fDecayer->Init();
236
237
238     fPythia->SetCKIN(3,fPtHardMin);
239     fPythia->SetCKIN(4,fPtHardMax);
240     fPythia->SetCKIN(7,fYHardMin);
241     fPythia->SetCKIN(8,fYHardMax);
242     
243     if (fAProjectile > 0 && fATarget > 0) fPythia->SetNuclei(fAProjectile, fATarget);  
244     // Fragmentation?
245     if (fFragmentation) {
246       fPythia->SetMSTP(111,1);
247     } else {
248       fPythia->SetMSTP(111,0);
249     }
250
251
252 //  initial state radiation   
253     fPythia->SetMSTP(61,fGinit);
254 //  final state radiation
255     fPythia->SetMSTP(71,fGfinal);
256 //  pt - kick
257     if (fPtKick > 0.) {
258         fPythia->SetMSTP(91,1);
259         fPythia->SetPARP(91,fPtKick);
260     } else {
261         fPythia->SetMSTP(91,0);
262     }
263
264
265     if (fReadFromFile) {
266         fRL  =  AliRunLoader::Open(fFileName, "Partons");
267         fRL->LoadKinematics();
268         fRL->LoadHeader();
269     } else {
270         fRL = 0x0;
271     }
272 // Switch off Heavy Flavors on request  
273     if (fHFoff) {
274         fPythia->SetMSTP(58, 3);
275         fPythia->SetMSTJ(45, 3);        
276         for (Int_t i = 156; i <= 160; i++) fPythia->SetMDME(i, 1, 0);
277     }
278  //
279     fPythia->ProcInit(fProcess,fEnergyCMS,fStrucFunc);
280
281 //  Parent and Children Selection
282     switch (fProcess) 
283     {
284     case kPyOldUEQ2ordered:
285     case kPyOldUEQ2ordered2:
286     case kPyOldPopcorn:
287       break;
288     case kPyCharm:
289     case kPyCharmUnforced:
290     case kPyCharmPbPbMNR:
291     case kPyCharmpPbMNR:
292     case kPyCharmppMNR:
293     case kPyCharmppMNRwmi:
294         fParentSelect[0] =   411;
295         fParentSelect[1] =   421;
296         fParentSelect[2] =   431;
297         fParentSelect[3] =  4122;
298         fFlavorSelect    =  4;  
299         break;
300     case kPyD0PbPbMNR:
301     case kPyD0pPbMNR:
302     case kPyD0ppMNR:
303         fParentSelect[0] =   421;
304         fFlavorSelect    =   4; 
305         break;
306     case kPyDPlusPbPbMNR:
307     case kPyDPluspPbMNR:
308     case kPyDPlusppMNR:
309         fParentSelect[0] =   411;
310         fFlavorSelect    =   4; 
311         break;
312     case kPyDPlusStrangePbPbMNR:
313     case kPyDPlusStrangepPbMNR:
314     case kPyDPlusStrangeppMNR:
315         fParentSelect[0] =   431;
316         fFlavorSelect    =   4; 
317         break;
318     case kPyBeauty:
319     case kPyBeautyPbPbMNR:
320     case kPyBeautypPbMNR:
321     case kPyBeautyppMNR:
322     case kPyBeautyppMNRwmi:
323         fParentSelect[0]=  511;
324         fParentSelect[1]=  521;
325         fParentSelect[2]=  531;
326         fParentSelect[3]= 5122;
327         fParentSelect[4]= 5132;
328         fParentSelect[5]= 5232;
329         fParentSelect[6]= 5332;
330         fFlavorSelect   = 5;    
331         break;
332     case kPyBeautyUnforced:
333         fParentSelect[0] =  511;
334         fParentSelect[1] =  521;
335         fParentSelect[2] =  531;
336         fParentSelect[3] = 5122;
337         fParentSelect[4] = 5132;
338         fParentSelect[5] = 5232;
339         fParentSelect[6] = 5332;
340         fFlavorSelect    = 5;   
341         break;
342     case kPyJpsiChi:
343     case kPyJpsi:
344         fParentSelect[0] = 443;
345         break;
346     case kPyMb:
347     case kPyMbNonDiffr:
348     case kPyMbMSEL1:
349     case kPyJets:
350     case kPyDirectGamma:
351         break;
352     case kPyW:
353     case kPyZ:
354         break;
355     }
356 //
357 //
358 //  JetFinder for Trigger
359 //
360 //  Configure detector (EMCAL like)
361 //
362     fPythia->SetPARU(51, fPycellEtaMax);
363     fPythia->SetMSTU(51, fPycellNEta);
364     fPythia->SetMSTU(52, fPycellNPhi);
365 //
366 //  Configure Jet Finder
367 //  
368     fPythia->SetPARU(58,  fPycellThreshold);
369     fPythia->SetPARU(52,  fPycellEtSeed);
370     fPythia->SetPARU(53,  fPycellMinEtJet);
371     fPythia->SetPARU(54,  fPycellMaxRadius);
372     fPythia->SetMSTU(54,  2);
373 //
374 //  This counts the total number of calls to Pyevnt() per run.
375     fTrialsRun = 0;
376     fQ         = 0.;
377     fX1        = 0.;
378     fX2        = 0.;    
379     fNev       = 0 ;
380 //    
381 //
382 //
383     AliGenMC::Init();
384 //
385 //
386 //  
387     if (fSetNuclei) {
388         fDyBoost = 0;
389         Warning("Init","SetNuclei used. Use SetProjectile + SetTarget instead. fDyBoost has been reset to 0\n");
390     }
391     
392     if (fQuench) {
393         fPythia->InitQuenching(0., 0.1, 0.6e6, 0);
394     }
395 }
396
397 void AliGenPythia::Generate()
398 {
399 // Generate one event
400     
401     fDecayer->ForceDecay();
402
403     Float_t polar[3]   =   {0,0,0};
404     Float_t origin[3]  =   {0,0,0};
405     Float_t p[4];
406 //  converts from mm/c to s
407     const Float_t kconv=0.001/2.999792458e8;
408 //
409     Int_t nt=0;
410     Int_t jev=0;
411     Int_t j, kf;
412     fTrials=0;
413     fEventTime = 0.;
414     
415     
416
417     //  Set collision vertex position 
418     if (fVertexSmear == kPerEvent) Vertex();
419     
420 //  event loop    
421     while(1)
422     {
423 //
424 // Produce event
425 //
426 //
427 // Switch hadronisation off
428 //
429         fPythia->SetMSTJ(1, 0);
430 //
431 // Either produce new event or read partons from file
432 //      
433         if (!fReadFromFile) {
434             if (!fNewMIS) {
435                 fPythia->Pyevnt();
436             } else {
437                 fPythia->Pyevnw();
438             }
439             fNpartons = fPythia->GetN();
440         } else {
441             printf("Loading Event %d\n",AliRunLoader::GetRunLoader()->GetEventNumber());
442             fRL->GetEvent(AliRunLoader::GetRunLoader()->GetEventNumber());
443             fPythia->SetN(0);
444             LoadEvent(fRL->Stack(), 0 , 1);
445             fPythia->Pyedit(21);
446         }
447         
448 //
449 //  Run quenching routine 
450 //
451         if (fQuench == 1) {
452             fPythia->Quench();
453         } else if (fQuench == 2){
454             fPythia->Pyquen(208., 0, 0.);
455         }
456 //
457 // Switch hadronisation on
458 //
459         fPythia->SetMSTJ(1, 1);
460 //
461 // .. and perform hadronisation
462 //      printf("Calling hadronisation %d\n", fPythia->GetN());
463         fPythia->Pyexec();      
464         fTrials++;
465         fPythia->ImportParticles(fParticles,"All");
466         Boost();
467 //
468 //
469 //
470         Int_t i;
471         
472
473         Int_t np = fParticles->GetEntriesFast();
474         
475         if (np == 0) continue;
476 //
477         
478 //
479         Int_t* pParent   = new Int_t[np];
480         Int_t* pSelected = new Int_t[np];
481         Int_t* trackIt   = new Int_t[np];
482         for (i = 0; i < np; i++) {
483             pParent[i]   = -1;
484             pSelected[i] =  0;
485             trackIt[i]   =  0;
486         }
487
488         Int_t nc = 0;        // Total n. of selected particles
489         Int_t nParents = 0;  // Selected parents
490         Int_t nTkbles = 0;   // Trackable particles
491         if (fProcess != kPyMb && fProcess != kPyJets && 
492             fProcess != kPyDirectGamma &&
493             fProcess != kPyMbNonDiffr  &&
494             fProcess != kPyMbMSEL1     &&
495             fProcess != kPyW && fProcess != kPyZ &&
496             fProcess != kPyCharmppMNRwmi && fProcess != kPyBeautyppMNRwmi) {
497             
498             for (i = 0; i < np; i++) {
499                 TParticle* iparticle = (TParticle *) fParticles->At(i);
500                 Int_t ks = iparticle->GetStatusCode();
501                 kf = CheckPDGCode(iparticle->GetPdgCode());
502 // No initial state partons
503                 if (ks==21) continue;
504 //
505 // Heavy Flavor Selection
506 //
507                 // quark ?
508                 kf = TMath::Abs(kf);
509                 Int_t kfl = kf;
510                 // Resonance
511
512                 if (kfl > 100000) kfl %= 100000;
513                 if (kfl > 10000)  kfl %= 10000;
514                 // meson ?
515                 if  (kfl > 10) kfl/=100;
516                 // baryon
517                 if (kfl > 10) kfl/=10;
518                 Int_t ipa = iparticle->GetFirstMother()-1;
519                 Int_t kfMo = 0;
520 //
521 // Establish mother daughter relation between heavy quarks and mesons
522 //
523                 if (kf >= fFlavorSelect && kf <= 6) {
524                     Int_t idau = iparticle->GetFirstDaughter() - 1;
525                     if (idau > -1) {
526                         TParticle* daughter = (TParticle *) fParticles->At(idau);
527                         Int_t pdgD = daughter->GetPdgCode();
528                         if (pdgD == 91 || pdgD == 92) {
529                             Int_t jmin = daughter->GetFirstDaughter() - 1;
530                             Int_t jmax = daughter->GetLastDaughter()  - 1;                          
531                             for (Int_t j = jmin; j <= jmax; j++)
532                                 ((TParticle *) fParticles->At(j))->SetFirstMother(i+1);
533                         } // is string or cluster
534                     } // has daughter
535                 } // heavy quark
536                 
537
538                 if (ipa > -1) {
539                     TParticle *  mother = (TParticle *) fParticles->At(ipa);
540                     kfMo = TMath::Abs(mother->GetPdgCode());
541                 }
542                 
543                 // What to keep in Stack?
544                 Bool_t flavorOK = kFALSE;
545                 Bool_t selectOK = kFALSE;
546                 if (fFeedDownOpt) {
547                     if (kfl >= fFlavorSelect) flavorOK = kTRUE;
548                 } else {
549                     if (kfl > fFlavorSelect) {
550                         nc = -1;
551                         break;
552                     }
553                     if (kfl == fFlavorSelect) flavorOK = kTRUE;
554                 }
555                 switch (fStackFillOpt) {
556                 case kFlavorSelection:
557                     selectOK = kTRUE;
558                     break;
559                 case kParentSelection:
560                     if (ParentSelected(kf) || kf <= 10) selectOK = kTRUE;
561                     break;
562                 }
563                 if (flavorOK && selectOK) { 
564 //
565 // Heavy flavor hadron or quark
566 //
567 // Kinematic seletion on final state heavy flavor mesons
568                     if (ParentSelected(kf) && !KinematicSelection(iparticle, 0)) 
569                     {
570                         continue;
571                     }
572                     pSelected[i] = 1;
573                     if (ParentSelected(kf)) ++nParents; // Update parent count
574 //                  printf("\n particle (HF)  %d %d %d", i, pSelected[i], kf);
575                 } else {
576 // Kinematic seletion on decay products
577                     if (fCutOnChild && ParentSelected(kfMo) && ChildSelected(kf) 
578                         && !KinematicSelection(iparticle, 1)) 
579                     {
580                         continue;
581                     }
582 //
583 // Decay products 
584 // Select if mother was selected and is not tracked
585
586                     if (pSelected[ipa] && 
587                         !trackIt[ipa]  &&     // mother will be  tracked ?
588                         kfMo !=  5 &&         // mother is b-quark, don't store fragments          
589                         kfMo !=  4 &&         // mother is c-quark, don't store fragments 
590                         kf   != 92)           // don't store string
591                     {
592 //
593 // Semi-stable or de-selected: diselect decay products:
594 // 
595 //
596                         if (pSelected[i] == -1 ||  fDecayer->GetLifetime(kf) > fMaxLifeTime)
597                         {
598                             Int_t ipF = iparticle->GetFirstDaughter();
599                             Int_t ipL = iparticle->GetLastDaughter();   
600                             if (ipF > 0) for (j = ipF-1; j < ipL; j++) pSelected[j] = -1;
601                         }
602 //                      printf("\n particle (decay)  %d %d %d", i, pSelected[i], kf);
603                         pSelected[i] = (pSelected[i] == -1) ? 0 : 1;
604                     }
605                 }
606                 if (pSelected[i] == -1) pSelected[i] = 0;
607                 if (!pSelected[i]) continue;
608                 // Count quarks only if you did not include fragmentation
609                 if (fFragmentation && kf <= 10) continue;
610
611                 nc++;
612 // Decision on tracking
613                 trackIt[i] = 0;
614 //
615 // Track final state particle
616                 if (ks == 1) trackIt[i] = 1;
617 // Track semi-stable particles
618                 if ((ks == 1) || (fDecayer->GetLifetime(kf) > fMaxLifeTime))  trackIt[i] = 1;
619 // Track particles selected by process if undecayed. 
620                 if (fForceDecay == kNoDecay) {
621                     if (ParentSelected(kf)) trackIt[i] = 1;
622                 } else {
623                     if (ParentSelected(kf)) trackIt[i] = 0;
624                 }
625                 if (trackIt[i] == 1) ++nTkbles; // Update trackable counter
626 //
627 //
628
629             } // particle selection loop
630             if (nc > 0) {
631                 for (i = 0; i<np; i++) {
632                     if (!pSelected[i]) continue;
633                     TParticle *  iparticle = (TParticle *) fParticles->At(i);
634                     kf = CheckPDGCode(iparticle->GetPdgCode());
635                     Int_t ks = iparticle->GetStatusCode();  
636                     p[0] = iparticle->Px();
637                     p[1] = iparticle->Py();
638                     p[2] = iparticle->Pz();
639                     p[3] = iparticle->Energy();
640                     
641                     origin[0] = fVertex[0]+iparticle->Vx()/10; // [cm]
642                     origin[1] = fVertex[1]+iparticle->Vy()/10; // [cm]
643                     origin[2] = fVertex[2]+iparticle->Vz()/10; // [cm]
644                     
645                     Float_t tof   = kconv*iparticle->T();
646                     Int_t ipa     = iparticle->GetFirstMother()-1;
647                     Int_t iparent = (ipa > -1) ? pParent[ipa] : -1;
648  
649                     PushTrack(fTrackIt*trackIt[i], iparent, kf, 
650                               p[0], p[1], p[2], p[3], 
651                               origin[0], origin[1], origin[2], tof, 
652                               polar[0], polar[1], polar[2],
653                               kPPrimary, nt, 1., ks);
654                     pParent[i] = nt;
655                     KeepTrack(nt); 
656                 } //  PushTrack loop
657             }
658         } else {
659             nc = GenerateMB();
660         } // mb ?
661         
662         GetSubEventTime();
663
664         delete[] pParent;
665         delete[] pSelected;
666         delete[] trackIt;
667
668         if (nc > 0) {
669           switch (fCountMode) {
670           case kCountAll:
671             // printf(" Count all \n");
672             jev += nc;
673             break;
674           case kCountParents:
675             // printf(" Count parents \n");
676             jev += nParents;
677             break;
678           case kCountTrackables:
679             // printf(" Count trackable \n");
680             jev += nTkbles;
681             break;
682           }
683             if (jev >= fNpart || fNpart == -1) {
684                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
685                 
686                 fQ  += fPythia->GetVINT(51);
687                 fX1 += fPythia->GetVINT(41);
688                 fX2 += fPythia->GetVINT(42);
689                 fTrialsRun += fTrials;
690                 fNev++;
691                 MakeHeader();
692                 break;
693             }
694         }
695     } // event loop
696     SetHighWaterMark(nt);
697 //  adjust weight due to kinematic selection
698 //    AdjustWeights();
699 //  get cross-section
700     fXsection=fPythia->GetPARI(1);
701 }
702
703 Int_t  AliGenPythia::GenerateMB()
704 {
705 //
706 // Min Bias selection and other global selections
707 //
708     Int_t i, kf, nt, iparent;
709     Int_t nc = 0;
710     Float_t p[4];
711     Float_t polar[3]   =   {0,0,0};
712     Float_t origin[3]  =   {0,0,0};
713 //  converts from mm/c to s
714     const Float_t kconv=0.001/2.999792458e8;
715     
716
717     
718     Int_t np = (fHadronisation) ? fParticles->GetEntriesFast() : fNpartons;
719
720
721
722     Int_t* pParent = new Int_t[np];
723     for (i=0; i< np; i++) pParent[i] = -1;
724     if (fProcess == kPyJets || fProcess == kPyDirectGamma) {
725         TParticle* jet1 = (TParticle *) fParticles->At(6);
726         TParticle* jet2 = (TParticle *) fParticles->At(7);
727         if (!CheckTrigger(jet1, jet2)) {
728           delete [] pParent;
729           return 0;
730         }
731     }
732
733     if (fTriggerParticle) {
734         Bool_t triggered = kFALSE;
735         for (i = 0; i < np; i++) {
736             TParticle *  iparticle = (TParticle *) fParticles->At(i);
737             kf = CheckPDGCode(iparticle->GetPdgCode());
738             if (kf != fTriggerParticle) continue;
739             if (iparticle->Pt() == 0.) continue;
740             if (TMath::Abs(iparticle->Eta()) > fTriggerEta) continue;
741             triggered = kTRUE;
742             break;
743         }
744         if (!triggered) {
745           delete [] pParent;
746           return 0;
747         }
748     }
749         
750
751     // Check if there is a ccbar or bbbar pair with at least one of the two
752     // in fYMin < y < fYMax
753     if (fProcess == kPyCharmppMNRwmi || fProcess == kPyBeautyppMNRwmi) {
754       TParticle *hvq;
755       Bool_t  theQ=kFALSE,theQbar=kFALSE,inYcut=kFALSE;
756       Float_t yQ;  
757       Int_t   pdgQ;
758       for(i=0; i<np; i++) {
759         hvq = (TParticle*)fParticles->At(i);
760         pdgQ = hvq->GetPdgCode();  
761         if(TMath::Abs(pdgQ) != fFlavorSelect) continue; 
762         if(pdgQ>0) { theQ=kTRUE; } else { theQbar=kTRUE; }
763         yQ = 0.5*TMath::Log((hvq->Energy()+hvq->Pz()+1.e-13)/
764                             (hvq->Energy()-hvq->Pz()+1.e-13));
765         if(yQ>fYMin && yQ<fYMax) inYcut=kTRUE;
766       }
767       if (!theQ || !theQbar || !inYcut) {
768         delete[] pParent;
769         return 0;
770       }
771     }
772
773     //Introducing child cuts in case kPyW, kPyZ, kPyMb, and kPyMbNonDiff
774     if ( (fProcess == kPyW || fProcess == kPyZ || fProcess == kPyMb || fProcess == kPyMbNonDiffr)  
775          && (fCutOnChild == 1) ) {
776       if ( !CheckKinematicsOnChild() ) {
777         delete[] pParent;
778         return 0;
779       }
780     }
781   
782
783     for (i = 0; i < np; i++) {
784         Int_t trackIt = 0;
785         TParticle *  iparticle = (TParticle *) fParticles->At(i);
786         kf = CheckPDGCode(iparticle->GetPdgCode());
787         Int_t ks = iparticle->GetStatusCode();
788         Int_t km = iparticle->GetFirstMother();
789         if ((ks == 1  && kf!=0 && KinematicSelection(iparticle, 0)) ||
790             (ks != 1) ||
791             (fProcess == kPyJets && ks == 21 && km == 0 && i>1)) {
792             nc++;
793             if (ks == 1) trackIt = 1;
794             Int_t ipa = iparticle->GetFirstMother()-1;
795             
796             iparent = (ipa > -1) ? pParent[ipa] : -1;
797             
798 //
799 // store track information
800             p[0] = iparticle->Px();
801             p[1] = iparticle->Py();
802             p[2] = iparticle->Pz();
803             p[3] = iparticle->Energy();
804
805             
806             origin[0] = fVertex[0]+iparticle->Vx()/10; // [cm]
807             origin[1] = fVertex[1]+iparticle->Vy()/10; // [cm]
808             origin[2] = fVertex[2]+iparticle->Vz()/10; // [cm]
809             
810             Float_t tof = fEventTime + kconv * iparticle->T();
811
812             PushTrack(fTrackIt*trackIt, iparent, kf, 
813                       p[0], p[1], p[2], p[3], 
814                       origin[0], origin[1], origin[2], tof, 
815                       polar[0], polar[1], polar[2],
816                       kPPrimary, nt, 1., ks);
817             //
818             // Special Treatment to store color-flow
819             //
820             if (ks == 3 || ks == 13 || ks == 14) {
821                 TParticle* particle = 0;
822                 if (fStack) {
823                     particle = fStack->Particle(nt);
824                 } else {
825                     particle = gAlice->Stack()->Particle(nt);
826                 }
827                 particle->SetFirstDaughter(fPythia->GetK(2, i));
828                 particle->SetLastDaughter(fPythia->GetK(3, i));         
829             }
830             
831             KeepTrack(nt);
832             pParent[i] = nt;
833             SetHighWaterMark(nt);
834             
835         } // select particle
836     } // particle loop 
837
838     delete[] pParent;
839     
840     return 1;
841 }
842
843
844 void AliGenPythia::FinishRun()
845 {
846 // Print x-section summary
847     fPythia->Pystat(1);
848
849     if (fNev > 0.) {
850         fQ  /= fNev;
851         fX1 /= fNev;
852         fX2 /= fNev;    
853     }
854     
855     printf("\nTotal number of Pyevnt() calls %d\n", fTrialsRun);
856     printf("\nMean Q, x1, x2: %f %f %f\n", fQ, fX1, fX2);
857 }
858
859 void AliGenPythia::AdjustWeights() const
860 {
861 // Adjust the weights after generation of all events
862 //
863     if (gAlice) {
864         TParticle *part;
865         Int_t ntrack=gAlice->GetMCApp()->GetNtrack();
866         for (Int_t i=0; i<ntrack; i++) {
867             part= gAlice->GetMCApp()->Particle(i);
868             part->SetWeight(part->GetWeight()*fKineBias);
869         }
870     }
871 }
872     
873 void AliGenPythia::SetNuclei(Int_t a1, Int_t a2)
874 {
875 // Treat protons as inside nuclei with mass numbers a1 and a2  
876
877     fAProjectile = a1;
878     fATarget     = a2;
879     fSetNuclei   = kTRUE;
880 }
881
882
883 void AliGenPythia::MakeHeader()
884 {
885 //
886 // Make header for the simulated event
887 // 
888   if (gAlice) {
889     if (gAlice->GetEvNumber()>=fDebugEventFirst &&
890         gAlice->GetEvNumber()<=fDebugEventLast) fPythia->Pylist(2);
891   }
892
893 // Builds the event header, to be called after each event
894     if (fHeader) delete fHeader;
895     fHeader = new AliGenPythiaEventHeader("Pythia");
896 //
897 // Event type  
898     ((AliGenPythiaEventHeader*) fHeader)->SetProcessType(fPythia->GetMSTI(1));
899 //
900 // Number of trials
901     ((AliGenPythiaEventHeader*) fHeader)->SetTrials(fTrials);
902 //
903 // Event Vertex 
904     fHeader->SetPrimaryVertex(fVertex);
905 //
906 // Jets that have triggered
907
908     if (fProcess == kPyJets)
909     {
910         Int_t ntrig, njet;
911         Float_t jets[4][10];
912         GetJets(njet, ntrig, jets);
913
914         
915         for (Int_t i = 0; i < ntrig; i++) {
916             ((AliGenPythiaEventHeader*) fHeader)->AddJet(jets[0][i], jets[1][i], jets[2][i], 
917                                                         jets[3][i]);
918         }
919     }
920 //
921 // Copy relevant information from external header, if present.
922 //
923     Float_t uqJet[4];
924     
925     if (fRL) {
926         AliGenPythiaEventHeader* exHeader = (AliGenPythiaEventHeader*) (fRL->GetHeader()->GenEventHeader());
927         for (Int_t i = 0; i < exHeader->NTriggerJets(); i++)
928         {
929             printf("Adding Jet %d %d \n", i,  exHeader->NTriggerJets());
930             
931             
932             exHeader->TriggerJet(i, uqJet);
933             ((AliGenPythiaEventHeader*) fHeader)->AddUQJet(uqJet[0], uqJet[1], uqJet[2], uqJet[3]);
934         }
935     }
936 //
937 // Store quenching parameters
938 //
939     if (fQuench){
940         Double_t z[4];
941         Double_t xp, yp;
942         if (fQuench == 1) {
943             // Pythia::Quench()
944             fPythia->GetQuenchingParameters(xp, yp, z);
945         } else {
946             // Pyquen
947             Double_t r1 = PARIMP.rb1;
948             Double_t r2 = PARIMP.rb2;
949             Double_t b  = PARIMP.b1;
950             Double_t r   = 0.5 * TMath::Sqrt(2. * (r1 * r1 + r2 * r2) - b * b);
951             Double_t phi = PARIMP.psib1;
952             xp = r * TMath::Cos(phi);
953             yp = r * TMath::Sin(phi);
954             
955         }
956             ((AliGenPythiaEventHeader*) fHeader)->SetXYJet(xp, yp);
957             ((AliGenPythiaEventHeader*) fHeader)->SetZQuench(z);
958         }
959 //
960 // Store pt^hard 
961     ((AliGenPythiaEventHeader*) fHeader)->SetPtHard(fPythia->GetVINT(47));
962 //
963 //  Pass header
964 //
965     AddHeader(fHeader);
966 }
967
968 void AliGenPythia::AddHeader(AliGenEventHeader* header)
969 {
970     // Add header to container or runloader
971     if (fContainer) {
972         fContainer->AddHeader(header);
973     } else {
974         AliRunLoader::GetRunLoader()->GetHeader()->SetGenEventHeader(header);   
975     }
976 }
977
978
979 Bool_t AliGenPythia::CheckTrigger(TParticle* jet1, TParticle* jet2)
980 {
981 // Check the kinematic trigger condition
982 //
983     Double_t eta[2];
984     eta[0] = jet1->Eta();
985     eta[1] = jet2->Eta();
986     Double_t phi[2];
987     phi[0] = jet1->Phi();
988     phi[1] = jet2->Phi();
989     Int_t    pdg[2]; 
990     pdg[0] = jet1->GetPdgCode();
991     pdg[1] = jet2->GetPdgCode();    
992     Bool_t   triggered = kFALSE;
993
994     if (fProcess == kPyJets) {
995         Int_t njets = 0;
996         Int_t ntrig = 0;
997         Float_t jets[4][10];
998 //
999 // Use Pythia clustering on parton level to determine jet axis
1000 //
1001         GetJets(njets, ntrig, jets);
1002         
1003         if (ntrig) triggered = kTRUE;
1004 //
1005     } else {
1006         Int_t ij = 0;
1007         Int_t ig = 1;
1008         if (pdg[0] == kGamma) {
1009             ij = 1;
1010             ig = 0;
1011         }
1012         //Check eta range first...
1013         if ((eta[ij] < fEtaMaxJet   && eta[ij] > fEtaMinJet) &&
1014             (eta[ig] < fEtaMaxGamma && eta[ig] > fEtaMinGamma))
1015         {
1016             //Eta is okay, now check phi range
1017             if ((phi[ij] < fPhiMaxJet   && phi[ij] > fPhiMinJet) &&
1018                 (phi[ig] < fPhiMaxGamma && phi[ig] > fPhiMinGamma))
1019             {
1020                 triggered = kTRUE;
1021             }
1022         }
1023     }
1024     return triggered;
1025 }
1026
1027
1028
1029 Bool_t AliGenPythia::CheckKinematicsOnChild(){
1030 //
1031 //Checking Kinematics on Child (status code 1, particle code ?, kin cuts
1032 //
1033     Bool_t checking = kFALSE;
1034     Int_t j, kcode, ks, km;
1035     Int_t nPartAcc = 0; //number of particles in the acceptance range
1036     Int_t numberOfAcceptedParticles = 1;
1037     if (fNumberOfAcceptedParticles != 0) { numberOfAcceptedParticles = fNumberOfAcceptedParticles; }
1038     Int_t npart = fParticles->GetEntriesFast();
1039     
1040     for (j = 0; j<npart; j++) {
1041         TParticle *  jparticle = (TParticle *) fParticles->At(j);
1042         kcode = TMath::Abs( CheckPDGCode(jparticle->GetPdgCode()) );
1043         ks = jparticle->GetStatusCode();
1044         km = jparticle->GetFirstMother(); 
1045         
1046         if( (ks == 1)  &&  (kcode == fPdgCodeParticleforAcceptanceCut)  &&  (KinematicSelection(jparticle,1)) ){
1047             nPartAcc++;
1048         }
1049         if( numberOfAcceptedParticles <= nPartAcc){
1050           checking = kTRUE;
1051           break;
1052         }
1053     }
1054
1055     return checking;
1056 }
1057
1058           
1059 AliGenPythia& AliGenPythia::operator=(const  AliGenPythia& rhs)
1060 {
1061 // Assignment operator
1062     rhs.Copy(*this);
1063     return *this;
1064 }
1065
1066 void  AliGenPythia::LoadEvent(AliStack* stack, Int_t flag, Int_t reHadr)
1067 {
1068 //
1069 // Load event into Pythia Common Block
1070 //
1071
1072     Int_t npart = stack -> GetNprimary();
1073     Int_t n0 = 0;
1074     
1075     if (!flag) {
1076         (fPythia->GetPyjets())->N = npart;
1077     } else {
1078         n0 = (fPythia->GetPyjets())->N;
1079         (fPythia->GetPyjets())->N = n0 + npart;
1080     }
1081     
1082     
1083     for (Int_t part = 0; part < npart; part++) {
1084         TParticle *mPart = stack->Particle(part);
1085         
1086         Int_t kf     =  mPart->GetPdgCode();
1087         Int_t ks     =  mPart->GetStatusCode();
1088         Int_t idf    =  mPart->GetFirstDaughter();
1089         Int_t idl    =  mPart->GetLastDaughter();
1090         
1091         if (reHadr) {
1092             if (ks == 11 || ks == 12) {
1093                 ks  -= 10;
1094                 idf  = -1;
1095                 idl  = -1;
1096             }
1097         }
1098         
1099         Float_t px = mPart->Px();
1100         Float_t py = mPart->Py();
1101         Float_t pz = mPart->Pz();
1102         Float_t e  = mPart->Energy();
1103         Float_t m  = mPart->GetCalcMass();
1104         
1105         
1106         (fPythia->GetPyjets())->P[0][part+n0] = px;
1107         (fPythia->GetPyjets())->P[1][part+n0] = py;
1108         (fPythia->GetPyjets())->P[2][part+n0] = pz;
1109         (fPythia->GetPyjets())->P[3][part+n0] = e;
1110         (fPythia->GetPyjets())->P[4][part+n0] = m;
1111         
1112         (fPythia->GetPyjets())->K[1][part+n0] = kf;
1113         (fPythia->GetPyjets())->K[0][part+n0] = ks;
1114         (fPythia->GetPyjets())->K[3][part+n0] = idf + 1;
1115         (fPythia->GetPyjets())->K[4][part+n0] = idl + 1;
1116         (fPythia->GetPyjets())->K[2][part+n0] = mPart->GetFirstMother() + 1;
1117     }
1118 }
1119
1120
1121 void AliGenPythia::RecJetsUA1(Int_t& njets, Float_t jets [4][50])
1122 {
1123 //
1124 //  Calls the Pythia jet finding algorithm to find jets in the current event
1125 //
1126 //
1127 //
1128 //  Save jets
1129     Int_t n     = fPythia->GetN();
1130
1131 //
1132 //  Run Jet Finder
1133     fPythia->Pycell(njets);
1134     Int_t i;
1135     for (i = 0; i < njets; i++) {
1136         Float_t px    = (fPythia->GetPyjets())->P[0][n+i];
1137         Float_t py    = (fPythia->GetPyjets())->P[1][n+i];
1138         Float_t pz    = (fPythia->GetPyjets())->P[2][n+i];
1139         Float_t e     = (fPythia->GetPyjets())->P[3][n+i];
1140
1141         jets[0][i] = px;
1142         jets[1][i] = py;
1143         jets[2][i] = pz;
1144         jets[3][i] = e;
1145     }
1146 }
1147
1148
1149
1150 void  AliGenPythia::GetJets(Int_t& nJets, Int_t& nJetsTrig, Float_t jets[4][10])
1151 {
1152 //
1153 //  Calls the Pythia clustering algorithm to find jets in the current event
1154 //
1155     Int_t n     = fPythia->GetN();
1156     nJets       = 0;
1157     nJetsTrig   = 0;
1158     if (fJetReconstruction == kCluster) {
1159 //
1160 //  Configure cluster algorithm
1161 //    
1162         fPythia->SetPARU(43, 2.);
1163         fPythia->SetMSTU(41, 1);
1164 //
1165 //  Call cluster algorithm
1166 //    
1167         fPythia->Pyclus(nJets);
1168 //
1169 //  Loading jets from common block
1170 //
1171     } else {
1172
1173 //
1174 //  Run Jet Finder
1175         fPythia->Pycell(nJets);
1176     }
1177
1178     Int_t i;
1179     for (i = 0; i < nJets; i++) {
1180         Float_t px    = (fPythia->GetPyjets())->P[0][n+i];
1181         Float_t py    = (fPythia->GetPyjets())->P[1][n+i];
1182         Float_t pz    = (fPythia->GetPyjets())->P[2][n+i];
1183         Float_t e     = (fPythia->GetPyjets())->P[3][n+i];
1184         Float_t pt    = TMath::Sqrt(px * px + py * py);
1185         Float_t phi   = TMath::Pi() + TMath::ATan2(-py, -px);  
1186         Float_t theta = TMath::ATan2(pt,pz);
1187         Float_t et    = e * TMath::Sin(theta);
1188         Float_t eta   = -TMath::Log(TMath::Tan(theta / 2.));
1189         if (
1190             eta > fEtaMinJet && eta < fEtaMaxJet && 
1191             phi > fPhiMinJet && phi < fPhiMaxJet &&
1192             et  > fEtMinJet  && et  < fEtMaxJet     
1193             ) 
1194         {
1195             jets[0][nJetsTrig] = px;
1196             jets[1][nJetsTrig] = py;
1197             jets[2][nJetsTrig] = pz;
1198             jets[3][nJetsTrig] = e;
1199             nJetsTrig++;
1200 //          printf("\n........-Jet #%d: %10.3f %10.3f %10.3f %10.3f \n", i, pt, et, eta, phi * kRaddeg);
1201         } else {
1202 //          printf("\n........-Jet #%d: %10.3f %10.3f %10.3f %10.3f \n", i, pt, et, eta, phi * kRaddeg);
1203         }
1204     }
1205 }
1206
1207 void AliGenPythia::GetSubEventTime()
1208 {
1209   // Calculates time of the next subevent
1210   fEventTime = 0.;
1211   if (fEventsTime) {
1212     TArrayF &array = *fEventsTime;
1213     fEventTime = array[fCurSubEvent++];
1214   }
1215   //  printf(" Event time: %d %f %p",fCurSubEvent,fEventTime,fEventsTime);
1216   return;
1217 }
1218
1219 #ifdef never
1220 void AliGenPythia::Streamer(TBuffer &R__b)
1221 {
1222    // Stream an object of class AliGenPythia.
1223
1224    if (R__b.IsReading()) {
1225       Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1226       AliGenerator::Streamer(R__b);
1227       R__b >> (Int_t&)fProcess;
1228       R__b >> (Int_t&)fStrucFunc;
1229       R__b >> (Int_t&)fForceDecay;
1230       R__b >> fEnergyCMS;
1231       R__b >> fKineBias;
1232       R__b >> fTrials;
1233       fParentSelect.Streamer(R__b);
1234       fChildSelect.Streamer(R__b);
1235       R__b >> fXsection;
1236 //      (AliPythia::Instance())->Streamer(R__b);
1237       R__b >> fPtHardMin;
1238       R__b >> fPtHardMax;
1239 //      if (fDecayer) fDecayer->Streamer(R__b);
1240    } else {
1241       R__b.WriteVersion(AliGenPythia::IsA());
1242       AliGenerator::Streamer(R__b);
1243       R__b << (Int_t)fProcess;
1244       R__b << (Int_t)fStrucFunc;
1245       R__b << (Int_t)fForceDecay;
1246       R__b << fEnergyCMS;
1247       R__b << fKineBias;
1248       R__b << fTrials;
1249       fParentSelect.Streamer(R__b);
1250       fChildSelect.Streamer(R__b);
1251       R__b << fXsection;
1252 //      R__b << fPythia;
1253       R__b << fPtHardMin;
1254       R__b << fPtHardMax;
1255       //     fDecayer->Streamer(R__b);
1256    }
1257 }
1258 #endif
1259
1260
1261