]> git.uio.no Git - u/mrichter/AliRoot.git/blob - THijing/AliGenHijing.cxx
Coverity fixes
[u/mrichter/AliRoot.git] / THijing / AliGenHijing.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 // Generator using HIJING as an external generator
19 // The main HIJING options are accessable for the user through this interface.
20 // Uses the THijing implementation of TGenerator.
21 // Author:
22 // Andreas Morsch    (andreas.morsch@cern.ch)
23 //
24
25 #include <TClonesArray.h>
26 #include <TGraph.h>
27 #include <THijing.h>
28 #include <TLorentzVector.h>
29 #include <TPDGCode.h>
30 #include <TParticle.h>
31
32 #include "AliGenHijing.h"
33 #include "AliGenHijingEventHeader.h"
34 #include "AliHijingRndm.h"
35 #include "AliLog.h"
36 #include "AliRun.h"
37
38 ClassImp(AliGenHijing)
39
40 AliGenHijing::AliGenHijing()
41     :AliGenMC(),
42      fFrame("CMS"),
43      fMinImpactParam(0.),
44      fMaxImpactParam(5.),
45      fKeep(0),
46      fQuench(1),
47      fShadowing(1),
48      fDecaysOff(3),
49      fTrigger(0),     
50      fEvaluate(0),
51      fSelectAll(0),
52      fFlavor(0),
53      fKineBias(0.),
54      fTrials(0),
55      fXsection(0.),
56      fHijing(0),
57      fPtHardMin(2.0),
58      fPtHardMax(-1),
59      fSpectators(1),
60      fDsigmaDb(0),
61      fDnDb(0),
62      fPtMinJet(-2.5),
63      fEtaMinJet(-20.),
64      fEtaMaxJet(+20.),
65      fPhiMinJet(0.),
66      fPhiMaxJet(2. * TMath::Pi()),
67      fRadiation(3),
68      fSimpleJet(kFALSE),
69      fNoGammas(kFALSE),
70      fProjectileSpecn(0),
71      fProjectileSpecp(0),
72      fTargetSpecn(0),
73      fTargetSpecp(0),
74      fLHC(kFALSE),
75      fRandomPz(kFALSE),
76      fNoHeavyQuarks(kFALSE),
77      fHeader(AliGenHijingEventHeader("Hijing"))
78 {
79   // Constructor
80   fEnergyCMS = 5500.;
81   AliHijingRndm::SetHijingRandom(GetRandom());
82 }
83
84 AliGenHijing::AliGenHijing(Int_t npart)
85     :AliGenMC(npart),
86      fFrame("CMS"),
87      fMinImpactParam(0.),
88      fMaxImpactParam(5.),
89      fKeep(0),
90      fQuench(1),
91      fShadowing(1),
92      fDecaysOff(3),
93      fTrigger(0),     
94      fEvaluate(0),
95      fSelectAll(0),
96      fFlavor(0),
97      fKineBias(0.),
98      fTrials(0),
99      fXsection(0.),
100      fHijing(0),
101      fPtHardMin(2.0),
102      fPtHardMax(-1),
103      fSpectators(1),
104      fDsigmaDb(0),
105      fDnDb(0),
106      fPtMinJet(-2.5),
107      fEtaMinJet(-20.),
108      fEtaMaxJet(+20.),
109      fPhiMinJet(0.),
110      fPhiMaxJet(2. * TMath::Pi()),
111      fRadiation(3),
112      fSimpleJet(kFALSE),
113      fNoGammas(kFALSE),
114      fProjectileSpecn(0),
115      fProjectileSpecp(0),
116      fTargetSpecn(0),
117      fTargetSpecp(0),
118      fLHC(kFALSE),
119      fRandomPz(kFALSE),
120      fNoHeavyQuarks(kFALSE),
121      fHeader(AliGenHijingEventHeader("Hijing"))
122 {
123 // Default PbPb collisions at 5. 5 TeV
124 //
125     fEnergyCMS = 5500.;
126     fName = "Hijing";
127     fTitle= "Particle Generator using HIJING";
128 //
129 //
130 // Set random number generator   
131     AliHijingRndm::SetHijingRandom(GetRandom());
132     
133 }
134
135 AliGenHijing::~AliGenHijing()
136 {
137 // Destructor
138     if ( fDsigmaDb) delete  fDsigmaDb;  
139     if ( fDnDb)     delete  fDnDb;  
140 }
141
142 void AliGenHijing::Init()
143 {
144 // Initialisation
145     fFrame.Resize(8);
146     fTarget.Resize(8);
147     fProjectile.Resize(8);
148     
149     SetMC(new THijing(fEnergyCMS, fFrame, fProjectile, fTarget, 
150                       fAProjectile, fZProjectile, fATarget, fZTarget, 
151                       fMinImpactParam, fMaxImpactParam));
152
153     fHijing=(THijing*) fMCEvGen;
154     fHijing->SetIHPR2(2,  fRadiation);
155     fHijing->SetIHPR2(3,  fTrigger);
156     fHijing->SetIHPR2(6,  fShadowing);
157     fHijing->SetIHPR2(12, fDecaysOff);    
158     fHijing->SetIHPR2(21, fKeep);
159     fHijing->SetHIPR1(8,  fPtHardMin);  
160     fHijing->SetHIPR1(9,  fPtHardMax);  
161     fHijing->SetHIPR1(10, fPtMinJet);   
162     fHijing->SetHIPR1(50, fSimpleJet);
163 //
164 //  Quenching
165 //
166 //
167 //  fQuench = 0:  no quenching
168 //  fQuench = 1:  hijing default
169 //  fQuench = 2:  new LHC  parameters for HIPR1(11) and HIPR1(14)
170 //  fQuench = 3:  new RHIC parameters for HIPR1(11) and HIPR1(14)
171 //  fQuench = 4:  new LHC  parameters with log(e) dependence
172 //  fQuench = 5:  new RHIC parameters with log(e) dependence
173     fHijing->SetIHPR2(50, 0);
174     if (fQuench > 0) 
175         fHijing->SetIHPR2(4,  1);
176     else
177         fHijing->SetIHPR2(4,  0);
178 // New LHC parameters from Xin-Nian Wang
179     if (fQuench == 2) {
180         fHijing->SetHIPR1(14, 1.1);
181         fHijing->SetHIPR1(11, 3.7);
182     } else if (fQuench == 3) {
183         fHijing->SetHIPR1(14, 0.20);
184         fHijing->SetHIPR1(11, 2.5);
185     } else if (fQuench == 4) {
186         fHijing->SetIHPR2(50, 1);
187         fHijing->SetHIPR1(14, 4.*0.34);
188         fHijing->SetHIPR1(11, 3.7);
189     } else if (fQuench == 5) {
190         fHijing->SetIHPR2(50, 1);
191         fHijing->SetHIPR1(14, 0.34);
192         fHijing->SetHIPR1(11, 2.5);
193     }
194     
195 //
196 // Heavy quarks
197 //    
198     if (fNoHeavyQuarks) {
199         fHijing->SetIHPR2(49, 1);
200     } else {
201         fHijing->SetIHPR2(49, 0);
202     }
203     
204     
205     AliGenMC::Init();
206     
207 //
208 //  Initialize Hijing  
209 //    
210     fHijing->Initialize();
211 //
212     if (fEvaluate) EvaluateCrossSections();
213 //
214 }
215
216 void AliGenHijing::Generate()
217 {
218 // Generate one event
219
220   Float_t polar[3]    =   {0,0,0};
221   Float_t origin[3]   =   {0,0,0};
222   Float_t origin0[3]  =   {0,0,0};
223   Float_t time0 = 0.;
224   Float_t p[3];
225   Float_t tof;
226
227 //  converts from mm/c to s
228   const Float_t kconv = 0.001/2.99792458e8;
229 //
230   Int_t nt  = 0;
231   Int_t jev = 0;
232   Int_t j, kf, ks, ksp, imo;
233   kf = 0;
234     
235
236     
237   fTrials = 0;
238   
239   for (j = 0;j < 3; j++) origin0[j] = fOrigin[j];
240   time0 = fTimeOrigin;
241
242   if(fVertexSmear == kPerEvent) {
243       Vertex();
244       for (j=0; j < 3; j++) origin0[j] = fVertex[j];
245       time0 = fTime;
246   } 
247
248
249   Float_t sign = (fRandomPz && (Rndm() < 0.5))? -1. : 1.;
250
251   while(1)
252   {
253 //    Generate one event
254 // --------------------------------------------------------------------------
255       fProjectileSpecn    = 0;  
256       fProjectileSpecp    = 0;
257       fTargetSpecn        = 0;  
258       fTargetSpecp        = 0;
259 // --------------------------------------------------------------------------
260       fHijing->GenerateEvent();
261       fTrials++;
262       fNprimaries = 0;
263       fHijing->ImportParticles(&fParticles,"All");
264       if (fTrigger != kNoTrigger) {
265           if (!CheckTrigger()) continue;
266       }
267       if (fLHC) Boost();
268       
269       
270       Int_t np = fParticles.GetEntriesFast();
271       Int_t nc = 0;
272       if (np == 0 ) continue;
273       Int_t i;
274       Int_t* newPos     = new Int_t[np];
275       Int_t* pSelected  = new Int_t[np];
276
277       for (i = 0; i < np; i++) {
278           newPos[i]    = i;
279           pSelected[i] = 0;
280       }
281       
282 //      Get event vertex
283 //
284       fVertex[0] = origin0[0];
285       fVertex[1] = origin0[1];  
286       fVertex[2] = origin0[2];
287       fTime = time0;
288 //
289 //      First select parent particles
290 //
291       TParticle *  iparticle = 0;
292       for (i = 0; i < np; i++) {
293           iparticle = (TParticle *) fParticles.At(i);
294
295 // Is this a parent particle ?
296           if (Stable(iparticle)) continue;
297 //
298           Bool_t  selected             =  kTRUE;
299           Bool_t  hasSelectedDaughters =  kFALSE;
300           
301           
302           kf        = iparticle->GetPdgCode();
303           ks        = iparticle->GetStatusCode();
304           if (kf == 92) continue;
305             
306           if (!fSelectAll) selected = KinematicSelection(iparticle, 0) && 
307                                SelectFlavor(kf);
308           hasSelectedDaughters = DaughtersSelection(iparticle);
309 //
310 // Put particle on the stack if it is either selected or 
311 // it is the mother of at least one seleted particle
312 //
313           if (selected || hasSelectedDaughters) {
314               nc++;
315               pSelected[i] = 1;
316           } // selected
317       } // particle loop parents
318 //
319 // Now select the final state particles
320 //
321
322       for (i = 0; i<np; i++) {
323           iparticle = (TParticle *) fParticles.At(i);
324 // Is this a final state particle ?
325           if (!Stable(iparticle)) continue;
326       
327           Bool_t  selected             =  kTRUE;
328           kf        = iparticle->GetPdgCode();
329           ks        = iparticle->GetStatusCode();
330           ksp       = iparticle->GetUniqueID();
331           
332 // --------------------------------------------------------------------------
333 // Count spectator neutrons and protons
334           if(ksp == 0 || ksp == 1){
335               if(kf == kNeutron) fProjectileSpecn += 1;
336               if(kf == kProton)  fProjectileSpecp += 1;
337           }
338           else if(ksp == 10 || ksp == 11){
339               if(kf == kNeutron) fTargetSpecn += 1;
340               if(kf == kProton)  fTargetSpecp += 1;
341           }
342 // --------------------------------------------------------------------------
343 //          
344           if (!fSelectAll) {
345               selected = KinematicSelection(iparticle,0)&&SelectFlavor(kf);
346               if (!fSpectators && selected) selected = (ksp != 0 && ksp != 1 && ksp != 10
347                                                         && ksp != 11);
348           }
349 //
350 // Put particle on the stack if selected
351 //
352           if (selected) {
353               nc++;
354               pSelected[i] = 1;
355           } // selected
356       } // particle loop final state
357
358 //
359 // Write particles to stack
360
361       for (i = 0; i<np; i++) {
362           iparticle = (TParticle *) fParticles.At(i);
363           Bool_t  hasMother   = (iparticle->GetFirstMother()     >=0);
364           Bool_t  hasDaughter = (iparticle->GetFirstDaughter()   >=0);
365           if (pSelected[i]) {
366               kf   = iparticle->GetPdgCode();
367               ks   = iparticle->GetStatusCode();
368               p[0] = iparticle->Px();
369               p[1] = iparticle->Py();
370               p[2] = iparticle->Pz() * sign;
371               origin[0] = origin0[0]+iparticle->Vx()/10;
372               origin[1] = origin0[1]+iparticle->Vy()/10;
373               origin[2] = origin0[2]+iparticle->Vz()/10;
374               tof = time0+kconv * iparticle->T();
375
376               imo = -1;
377               TParticle* mother = 0;
378               if (hasMother) {
379                   imo = iparticle->GetFirstMother();
380                   mother = (TParticle *) fParticles.At(imo);
381                   imo = (mother->GetPdgCode() != 92) ? newPos[imo] : -1;
382               } // if has mother   
383               Bool_t tFlag = (fTrackIt && !hasDaughter);
384               PushTrack(tFlag,imo,kf,p,origin,polar,tof,kPNoProcess,nt, 1., ks);
385               fNprimaries++;
386               KeepTrack(nt);
387               newPos[i] = nt;
388           } // if selected
389       } // particle loop
390       delete[] newPos;
391       delete[] pSelected;
392       
393       AliInfo(Form("\n I've put %i particles on the stack \n",nc));
394       if (nc > 0) {
395           jev += nc;
396           if (jev >= fNpart || fNpart == -1) {
397               fKineBias = Float_t(fNpart)/Float_t(fTrials);
398               AliInfo(Form("\n Trials: %i %i %i\n",fTrials, fNpart, jev));
399               break;
400           }
401       }
402   } // event loop
403   MakeHeader();
404   SetHighWaterMark(nt);
405 }
406
407 void AliGenHijing::KeepFullEvent()
408 {
409     fKeep=1;
410 }
411
412 void AliGenHijing::EvaluateCrossSections()
413 {
414 //     Glauber Calculation of geometrical x-section
415 //
416     Float_t xTot       = 0.;          // barn
417     Float_t xTotHard   = 0.;          // barn 
418     Float_t xPart      = 0.;          // barn
419     Float_t xPartHard  = 0.;          // barn 
420     Float_t sigmaHard  = 0.1;         // mbarn
421     Float_t bMin       = 0.;
422     Float_t bMax       = fHijing->GetHIPR1(34)+fHijing->GetHIPR1(35);
423     const Float_t kdib = 0.2;
424     Int_t   kMax       = Int_t((bMax-bMin)/kdib)+1;
425
426
427     printf("\n Projectile Radius (fm): %f \n",fHijing->GetHIPR1(34));
428     printf("\n Target     Radius (fm): %f \n",fHijing->GetHIPR1(35));    
429     printf("\n Inelastic and total cross section (mb) %f %f \n",fHijing->GetHINT1(12), fHijing->GetHINT1(13));    
430     Int_t i;
431     Float_t oldvalue= 0.;
432
433     Float_t* b   = new Float_t[kMax];
434     Float_t* si1 = new Float_t[kMax];    
435     Float_t* si2 = new Float_t[kMax];    
436     for (i = 0; i < kMax; i++){
437       b[i] = 0.;
438       si1[i] = 0.;
439       si2[i] = 0.;
440     }
441
442     for (i = 0; i < kMax; i++)
443     {
444         Float_t xb  = bMin+i*kdib;
445         Float_t ov;
446         ov=fHijing->Profile(xb);
447         Float_t gb  =  2.*0.01*fHijing->GetHIPR1(40)*kdib*xb*(1.-TMath::Exp(-fHijing->GetHINT1(12)*ov));
448         Float_t gbh =  2.*0.01*fHijing->GetHIPR1(40)*kdib*xb*sigmaHard*ov;
449         xTot+=gb;
450         xTotHard += gbh;
451         printf("profile %f %f %f\n", xb, ov, fHijing->GetHINT1(12));
452         
453         if (xb > fMinImpactParam && xb < fMaxImpactParam)
454         {
455             xPart += gb;
456             xPartHard += gbh;
457         }
458         
459         if(oldvalue) if ((xTot-oldvalue)/oldvalue<0.0001) break;
460         oldvalue = xTot;
461         printf("\n Total cross section (barn): %d %f %f \n",i, xb, xTot);
462         printf("\n Hard  cross section (barn): %d %f %f \n\n",i, xb, xTotHard);
463         if (i>0) {
464             si1[i] = gb/kdib;
465             si2[i] = gbh/gb;
466             b[i]  = xb;
467         }
468     }
469
470     printf("\n Total cross section (barn): %f \n",xTot);
471     printf("\n Hard  cross section (barn): %f \n \n",xTotHard);
472     printf("\n Partial       cross section (barn): %f %f \n",xPart, xPart/xTot*100.);
473     printf("\n Partial  hard cross section (barn): %f %f \n",xPartHard, xPartHard/xTotHard*100.);
474
475 //  Store result as a graph
476     b[0] = 0;
477     si1[0] = 0;
478     si2[0]=si2[1];
479     
480     fDsigmaDb  = new TGraph(i, b, si1);
481     fDnDb      = new TGraph(i, b, si2);
482 }
483
484 Bool_t AliGenHijing::DaughtersSelection(const TParticle* iparticle)
485 {
486 //
487 // Looks recursively if one of the daughters has been selected
488 //
489 //    printf("\n Consider daughters %d:",iparticle->GetPdgCode());
490     Int_t imin = -1;
491     Int_t imax = -1;
492     Int_t i;
493     Bool_t hasDaughters = (iparticle->GetFirstDaughter() >=0);
494     Bool_t selected = kFALSE;
495     if (hasDaughters) {
496         imin = iparticle->GetFirstDaughter();
497         imax = iparticle->GetLastDaughter();       
498         for (i = imin; i <= imax; i++){
499             TParticle *  jparticle = (TParticle *) fParticles.At(i);    
500             Int_t ip = jparticle->GetPdgCode();
501             if (KinematicSelection(jparticle,0)&&SelectFlavor(ip)) {
502                 selected=kTRUE; break;
503             }
504             if (DaughtersSelection(jparticle)) {selected=kTRUE; break; }
505         }
506     } else {
507         return kFALSE;
508     }
509     return selected;
510 }
511
512
513 Bool_t AliGenHijing::SelectFlavor(Int_t pid)
514 {
515 // Select flavor of particle
516 // 0: all
517 // 4: charm and beauty
518 // 5: beauty
519     Bool_t res = 0;
520     
521     if (fFlavor == 0) {
522         res = kTRUE;
523     } else {
524         Int_t ifl = TMath::Abs(pid/100);
525         if (ifl > 10) ifl/=10;
526         res = (fFlavor == ifl);
527     }
528 //
529 //  This part if gamma writing is inhibited
530     if (fNoGammas) 
531         res = res && (pid != kGamma && pid != kPi0);
532 //
533     return res;
534 }
535
536 Bool_t AliGenHijing::Stable(const TParticle*  particle) const
537 {
538 // Return true for a stable particle
539 //
540     
541     if (particle->GetFirstDaughter() < 0 )
542     {
543         return kTRUE;
544     } else {
545         return kFALSE;
546     }
547 }
548
549
550
551 void AliGenHijing::MakeHeader()
552 {
553 // Builds the event header, to be called after each event
554     fHeader.SetNProduced(fNprimaries);
555     fHeader.SetImpactParameter(fHijing->GetHINT1(19));
556     fHeader.SetTotalEnergy(fHijing->GetEATT());
557     fHeader.SetHardScatters(fHijing->GetJATT());
558     fHeader.SetParticipants(fHijing->GetNP(), fHijing->GetNT());
559     fHeader.SetCollisions(fHijing->GetN0(),
560                           fHijing->GetN01(),
561                           fHijing->GetN10(),
562                           fHijing->GetN11());
563     fHeader.SetSpectators(fProjectileSpecn, fProjectileSpecp,
564                           fTargetSpecn,fTargetSpecp);
565     fHeader.SetReactionPlaneAngle(fHijing->GetHINT1(20));
566
567 // 4-momentum vectors of the triggered jets.
568 //
569 // Before final state gluon radiation.
570     TLorentzVector* jet1 = new TLorentzVector(fHijing->GetHINT1(21), 
571                                               fHijing->GetHINT1(22),
572                                               fHijing->GetHINT1(23),
573                                               fHijing->GetHINT1(24));
574
575     TLorentzVector* jet2 = new TLorentzVector(fHijing->GetHINT1(31), 
576                                               fHijing->GetHINT1(32),
577                                               fHijing->GetHINT1(33),
578                                               fHijing->GetHINT1(34));
579 // After final state gluon radiation.
580     TLorentzVector* jet3 = new TLorentzVector(fHijing->GetHINT1(26), 
581                                               fHijing->GetHINT1(27),
582                                               fHijing->GetHINT1(28),
583                                               fHijing->GetHINT1(29));
584
585     TLorentzVector* jet4 = new TLorentzVector(fHijing->GetHINT1(36), 
586                                               fHijing->GetHINT1(37),
587                                               fHijing->GetHINT1(38),
588                                               fHijing->GetHINT1(39));
589     fHeader.SetJets(jet1, jet2, jet3, jet4);
590 // Bookkeeping for kinematic bias
591     fHeader.SetTrials(fTrials);
592 // Event Vertex
593     fHeader.SetPrimaryVertex(fVertex);
594     fHeader.SetInteractionTime(fTime);
595     AddHeader(&fHeader);
596     fCollisionGeometry = &fHeader;
597 }
598
599
600 Bool_t AliGenHijing::CheckTrigger()
601 {
602 // Check the kinematic trigger condition
603 //
604     Bool_t   triggered = kFALSE;
605  
606     if (fTrigger == 1) {
607 //
608 //  jet-jet Trigger     
609         
610         TLorentzVector* jet1 = new TLorentzVector(fHijing->GetHINT1(26), 
611                                                   fHijing->GetHINT1(27),
612                                                   fHijing->GetHINT1(28),
613                                                   fHijing->GetHINT1(29));
614         
615         TLorentzVector* jet2 = new TLorentzVector(fHijing->GetHINT1(36), 
616                                                   fHijing->GetHINT1(37),
617                                                   fHijing->GetHINT1(38),
618                                                   fHijing->GetHINT1(39));
619         Double_t eta1      = jet1->Eta();
620         Double_t eta2      = jet2->Eta();
621         Double_t phi1      = jet1->Phi();
622         Double_t phi2      = jet2->Phi();
623 //    printf("\n Trigger: %f %f %f %f",
624 //         fEtaMinJet, fEtaMaxJet, fPhiMinJet, fPhiMaxJet);
625         if (
626             (eta1 < fEtaMaxJet && eta1 > fEtaMinJet &&  
627              phi1 < fPhiMaxJet && phi1 > fPhiMinJet) 
628             ||
629             (eta2 < fEtaMaxJet && eta2 > fEtaMinJet &&  
630              phi2 < fPhiMaxJet && phi2 > fPhiMinJet)
631             ) 
632             triggered = kTRUE;
633     } else if (fTrigger == 2) {
634 //  Gamma Jet
635 //
636         Int_t np = fParticles.GetEntriesFast();
637         for (Int_t i = 0; i < np; i++) {
638             TParticle* part = (TParticle*) fParticles.At(i);
639             Int_t kf = part->GetPdgCode();
640             Int_t ksp = part->GetUniqueID();
641             if (kf == 22 && ksp == 40) {
642                 Float_t phi = part->Phi();
643                 Float_t eta = part->Eta();
644                 if  (eta < fEtaMaxJet && 
645                      eta > fEtaMinJet &&
646                      phi < fPhiMaxJet && 
647                      phi > fPhiMinJet) {
648                     triggered = 1;
649                     break;
650                 } // check phi,eta within limits
651             } // direct gamma ? 
652         } // particle loop
653     } // fTrigger == 2
654     return triggered;
655 }