]> git.uio.no Git - u/mrichter/AliRoot.git/blob - THerwig/AliGenHerwig.cxx
eada69cb627d5501aa70dc2a6b3b590a7296a22f
[u/mrichter/AliRoot.git] / THerwig / AliGenHerwig.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 Herwig as an external generator
20 // The main Herwig options are accessable for the user through this interface.
21 // Uses the THerwig implementation of TGenerator.
22
23
24 #include <Riostream.h>
25 #include <TClonesArray.h>
26 #include <TParticle.h>
27
28 #include <THerwig6.h>
29
30 #include "AliGenHerwig.h"
31 #include "AliHerwigRndm.h"
32 #include "AliMC.h"
33 #include "AliRun.h"
34 #include "driver.h"
35
36 ClassImp(AliGenHerwig)
37
38
39   AliGenHerwig::AliGenHerwig() :
40     AliGenMC(),
41     fAutPDF("LHAPDF"),
42     fModPDF(19070),
43     fStrucFunc(kCTEQ5L),
44     fKeep(0),
45     fDecaysOff(1),
46     fTrigger(0),
47     fSelectAll(0),
48     fFlavor(0),
49     fMomentum1(7000),
50     fMomentum2(7000),
51     fKineBias(1),
52     fTrials(0),
53     fXsection(0),
54     fHerwig(0x0),
55     fProcess(0),
56     fPtHardMin(0.),
57     fPtHardMax(9999.),
58     fPtRMS(0.),
59     fMaxPr(10),
60     fMaxErrors(1000),
61     fEnSoft(1),
62     fEv1Pr(0),
63     fEv2Pr(0),
64     fFileName(0),
65     fEtaMinParton(-20.),     
66     fEtaMaxParton(20.),     
67     fPhiMinParton(0.),     
68     fPhiMaxParton(2.* TMath::Pi()),     
69     fEtaMinGamma(-20.),      
70     fEtaMaxGamma(20.),      
71     fPhiMinGamma(0.),      
72     fPhiMaxGamma(2. * TMath::Pi())  
73 {
74 // Constructor
75   fEnergyCMS = 14000;
76 }
77
78 AliGenHerwig::AliGenHerwig(Int_t npart)
79     :AliGenMC(npart),
80     fAutPDF("LHAPDF"),
81     fModPDF(19070),
82     fStrucFunc(kCTEQ5L),
83     fKeep(0),
84     fDecaysOff(1),
85     fTrigger(0),
86     fSelectAll(0),
87     fFlavor(0),
88     fMomentum1(7000),
89     fMomentum2(7000),
90     fKineBias(1),
91     fTrials(0),
92     fXsection(0),
93     fHerwig(0x0),
94     fProcess(0),
95     fPtHardMin(0.),
96     fPtHardMax(9999.),
97     fPtRMS(0.),
98     fMaxPr(10),
99     fMaxErrors(1000),
100     fEnSoft(1),
101     fEv1Pr(0),
102     fEv2Pr(0),
103     fFileName(0),
104     fEtaMinParton(-20.),     
105     fEtaMaxParton(20.),     
106     fPhiMinParton(0.),     
107     fPhiMaxParton(2.* TMath::Pi()),     
108     fEtaMinGamma(-20.),      
109     fEtaMaxGamma(20.),      
110     fPhiMinGamma(0.),      
111     fPhiMaxGamma(2. * TMath::Pi())  
112 {
113     fEnergyCMS = 14000;
114     SetTarget();
115     SetProjectile();
116     // Set random number generator
117     AliHerwigRndm::SetHerwigRandom(GetRandom());
118 }
119
120 AliGenHerwig::~AliGenHerwig()
121 {
122 // Destructor
123 }
124
125 void AliGenHerwig::SetEventListRange(Int_t eventFirst, Int_t eventLast)
126 {
127   fEv1Pr = eventFirst;
128   fEv2Pr = eventLast;
129   if ( fEv2Pr == -1 ) fEv2Pr = fEv2Pr;
130 }
131
132 void AliGenHerwig::Init()
133 {
134 // Initialisation
135   fTarget.Resize(8);
136   fProjectile.Resize(8);
137   SetMC(new THerwig6());
138   fHerwig=(THerwig6*) fMCEvGen;
139   // initialize common blocks
140   fHerwig->Initialize(fProjectile.Data(), fTarget.Data(), fMomentum1, fMomentum2, fProcess); // 
141   // reset parameters according to user needs
142   InitPDF();
143   fHerwig->SetPTMIN(fPtHardMin);
144   fHerwig->SetPTMAX(fPtHardMax);
145   fHerwig->SetPTRMS(fPtRMS);
146   fHerwig->SetMAXPR(fMaxPr);
147   fHerwig->SetMAXER(fMaxErrors);
148   fHerwig->SetENSOF(fEnSoft);
149 // C---D,U,S,C,B,T QUARK AND GLUON MASSES (IN THAT ORDER)
150 //       RMASS(1)=0.32
151 //       RMASS(2)=0.32
152 //       RMASS(3)=0.5
153 //       RMASS(4)=1.55
154 //       RMASS(5)=4.75
155 //       RMASS(6)=174.3
156 //       RMASS(13)=0.75
157
158   fHerwig->SetRMASS(4,1.2);
159   fHerwig->SetRMASS(5,4.75);
160
161   if ( fProcess < 0 ) strncpy(VVJIN.QQIN,fFileName.Data(),50);
162
163   //fHerwig->Hwusta("PI0     ");
164
165   // compute parameter dependent constants
166   fHerwig->PrepareRun();
167 }
168
169 void AliGenHerwig::InitJimmy()
170 {
171 // Initialisation
172   fTarget.Resize(8);
173   fProjectile.Resize(8);
174   SetMC(new THerwig6());
175   fHerwig=(THerwig6*) fMCEvGen;
176   // initialize common blocks
177   fHerwig->InitializeJimmy(fProjectile, fTarget, fMomentum1, fMomentum2, fProcess);
178   // reset parameters according to user needs
179   InitPDF();
180   fHerwig->SetPTMIN(fPtHardMin);
181   fHerwig->SetPTRMS(fPtRMS);
182   fHerwig->SetMAXPR(fMaxPr);
183   fHerwig->SetMAXER(fMaxErrors);
184   fHerwig->SetENSOF(fEnSoft);
185
186 // C---D,U,S,C,B,T QUARK AND GLUON MASSES (IN THAT ORDER)
187 //       RMASS(1)=0.32
188 //       RMASS(2)=0.32
189 //       RMASS(3)=0.5
190 //       RMASS(4)=1.55
191 //       RMASS(5)=4.75
192 //       RMASS(6)=174.3
193 //       RMASS(13)=0.75
194
195   fHerwig->SetRMASS(4,1.2);
196   fHerwig->SetRMASS(5,4.75);
197
198   if ( fProcess < 0 ) strncpy(VVJIN.QQIN,fFileName.Data(),50);
199
200   //  fHerwig->Hwusta("PI0     ");
201
202   // compute parameter dependent constants
203   fHerwig->PrepareRunJimmy();
204 }
205
206 void AliGenHerwig::InitPDF()
207 {
208   switch(fStrucFunc)
209     {
210 // ONLY USES LHAPDF STRUCTURE FUNCTIONS
211     case kGRVLO98:
212       fModPDF=80060;
213       fAutPDF="HWLHAPDF";
214       break;
215     case kCTEQ6:
216       fModPDF=10040;
217       fAutPDF="HWLHAPDF";
218       break;
219     case kCTEQ61:
220       fModPDF=10100;
221       fAutPDF="HWLHAPDF";
222       break;
223     case kCTEQ6m:
224       fModPDF=10050;
225       fAutPDF="HWLHAPDF";
226       break;
227     case kCTEQ6l:
228       fModPDF=10041;
229       fAutPDF="HWLHAPDF";
230       break;
231     case kCTEQ6ll:
232       fModPDF=10042;
233       fAutPDF="HWLHAPDF";
234       break;
235     case kCTEQ5M:
236       fModPDF=19050;
237       fAutPDF="HWLHAPDF";
238       break;
239     case kCTEQ5L:
240       fModPDF=19070;
241       fAutPDF="HWLHAPDF";
242       break;
243     case kCTEQ4M:
244       fModPDF=19150;
245       fAutPDF="HWLHAPDF";
246       break;
247     case kCTEQ4L:
248       fModPDF=19170;
249       fAutPDF="HWLHAPDF";
250       break;
251 //    case kMRST2004nlo:
252 //      fModPDF=20400;
253 //      fAutPDF="HWLHAPDF";
254 //      break;
255     default:
256       cerr << "This structure function is not inplemented " << fStrucFunc << endl;
257       break;
258     }
259   fAutPDF.Resize(20);
260   fHerwig->SetMODPDF(1,fModPDF);
261   fHerwig->SetMODPDF(2,fModPDF);
262   fHerwig->SetAUTPDF(1,fAutPDF);
263   fHerwig->SetAUTPDF(2,fAutPDF);
264 }
265
266 void AliGenHerwig::Generate()
267 {
268   // Generate one event
269
270   Float_t polar[3] =   {0,0,0};
271   Float_t origin[3]=   {0,0,0};
272   Float_t origin0[3]=  {0,0,0};
273   Float_t p[4], random[6];
274
275   static TClonesArray *particles;
276   //  converts from mm/c to s
277   const Float_t kconv=0.001/2.999792458e8;
278   //
279   Int_t nt=0;
280   Int_t jev=0;
281   Int_t j, kf, ks, imo;
282   kf=0;
283
284   if(!particles) particles=new TClonesArray("TParticle",10000);
285
286   fTrials=0;
287   for (j=0;j<3;j++) origin0[j]=fOrigin[j];
288   if(fVertexSmear==kPerEvent) {
289     Rndm(random,6);
290     for (j=0;j<3;j++) {
291       origin0[j]+=fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
292         TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
293     }
294   }
295
296   while(1)
297     {
298         fHerwig->GenerateEvent();
299         fTrials++;
300         fHerwig->ImportParticles(particles,"All");
301         Int_t np = particles->GetEntriesFast()-1;
302         if (np == 0 ) continue;
303
304         //Check hard partons or direct gamma in kine range
305
306         if (fProcess == kHeJets || fProcess == kHeDirectGamma) {
307           TParticle* parton1 = (TParticle *) particles->At(6);
308           TParticle* parton2 = (TParticle *) particles->At(7);
309           if (!CheckParton(parton1, parton2))  continue ;
310         } 
311
312         // 
313         if (gAlice) {
314             if (gAlice->GetEvNumber()>=fEv1Pr &&
315                 gAlice->GetEvNumber()<=fEv2Pr) fHerwig->PrintEvt();
316
317         }
318
319         
320         Int_t nc=0;
321
322         Int_t * newPos = new Int_t[np];
323         for (Int_t i = 0; i<np; i++) *(newPos+i)=-1;
324
325         for (Int_t i = 0; i<np; i++) {
326             TParticle *  iparticle       = (TParticle *) particles->At(i);
327             imo = iparticle->GetFirstMother();
328             kf        = iparticle->GetPdgCode();
329             ks        = iparticle->GetStatusCode();
330             if (ks != 3 &&
331                 KinematicSelection(iparticle,0))
332             {
333                 nc++;
334                 p[0]=iparticle->Px();
335                 p[1]=iparticle->Py();
336                 p[2]=iparticle->Pz();
337                 p[3]=iparticle->Energy();
338                 origin[0]=origin0[0]+iparticle->Vx()/10;
339                 origin[1]=origin0[1]+iparticle->Vy()/10;
340                 origin[2]=origin0[2]+iparticle->Vz()/10;
341                 Float_t tof = kconv*iparticle->T();
342                 Int_t   iparent = (imo > -1) ? newPos[imo] : -1;
343                 Int_t   trackIt = (ks == 1) && fTrackIt;
344                 PushTrack(trackIt, iparent, kf,
345                           p[0], p[1], p[2], p[3],
346                           origin[0], origin[1], origin[2],
347                           tof,
348                           polar[0], polar[1], polar[2],
349                           kPPrimary, nt, fHerwig->GetEVWGT(), ks);
350                 KeepTrack(nt);
351                 newPos[i]=nt;
352             } // end of if: selection of particle
353         } // end of for: particle loop
354         if (newPos) delete[] newPos;
355         //      MakeHeader();
356         if (nc > 0) {
357             jev+=nc;
358             if (jev >= fNpart || fNpart == -1) {
359                 fKineBias=Float_t(fNpart)/Float_t(fTrials);
360                 break;
361             }
362         }
363     }
364   SetHighWaterMark(nt);
365 //  adjust weight due to kinematic selection
366   AdjustWeights();
367 //  get cross-section
368   fXsection=fHerwig->GetAVWGT();
369   //printf(">> trials << %d\n",fTrials);
370 }
371
372 Bool_t AliGenHerwig::CheckParton(TParticle* parton1, TParticle* parton2)
373 {
374 // Check the kinematic trigger condition
375 //
376 //Select events with parton max energy
377     if(fPtHardMax < parton1->Pt()) return kFALSE;
378
379 // Select events within angular window
380     Double_t eta[2];
381     eta[0] = parton1->Eta();
382     eta[1] = parton2->Eta();
383     Double_t phi[2];
384     phi[0] = parton1->Phi();
385     phi[1] = parton2->Phi();
386     Int_t    pdg[2]; 
387     pdg[0] = parton1->GetPdgCode();
388     pdg[1] = parton2->GetPdgCode();   
389     printf("min %f, max %f\n",fPtHardMin, fPtHardMax);
390     printf("Parton 1: %s, pT= %2.2f, eta = %1.2f, phi = %2.2f\n", parton1->GetName(),parton1->Pt(), eta[0],phi[0]*TMath::RadToDeg());
391     printf("Parton 2: %s, pT= %2.2f, eta = %1.2f, phi = %2.2f\n", parton2->GetName(),parton2->Pt(), eta[1],phi[1]*TMath::RadToDeg());
392     
393     if (fProcess == kHeJets) {
394       //Check if one of the 2 outgoing partons are in the eta-phi window
395       for(Int_t i = 0; i < 2; i++)
396         if ((eta[i] < fEtaMaxParton  && eta[i] > fEtaMinParton) &&
397             (phi[i] < fPhiMaxParton  && phi[i] > fPhiMinParton)) return  kTRUE ;
398     }
399     
400     else {
401       //Check if the gamma and the jet  are in the eta-phi window
402       Int_t igj = 0;
403       Int_t ijj = 0;
404       if(pdg[0] == 22) ijj=1;
405       else igj=1;
406       if ((eta[ijj] < fEtaMaxParton   && eta[ijj] > fEtaMinParton) &&
407           (phi[ijj] < fPhiMaxParton   && phi[ijj] > fPhiMinParton)) {
408         
409         if ((eta[igj] < fEtaMaxGamma   && eta[igj] > fEtaMinGamma) &&
410             (phi[igj] < fPhiMaxGamma   && phi[igj] > fPhiMinGamma)) return  kTRUE;
411         
412       }
413     }
414
415     return kFALSE ;
416 }
417
418 void AliGenHerwig::AdjustWeights()
419 {
420 // Adjust the weights after generation of all events
421     TParticle *part;
422     Int_t ntrack=gAlice->GetMCApp()->GetNtrack();
423     for (Int_t i=0; i<ntrack; i++) {
424         part= gAlice->GetMCApp()->Particle(i);
425         part->SetWeight(part->GetWeight()*fKineBias);
426     }
427 }
428
429
430 void AliGenHerwig::KeepFullEvent()
431 {
432     fKeep=1;
433 }
434
435 Bool_t AliGenHerwig::DaughtersSelection(TParticle* iparticle, TClonesArray* particles)
436 {
437 //
438 // Looks recursively if one of the daughters has been selected
439 //
440 //    printf("\n Consider daughters %d:",iparticle->GetPdgCode());
441     Int_t imin=-1;
442     Int_t imax=-1;
443     Int_t i;
444     Bool_t hasDaughters= (iparticle->GetFirstDaughter() >=0);
445     Bool_t selected=kFALSE;
446     if (hasDaughters) {
447         imin=iparticle->GetFirstDaughter();
448         imax=iparticle->GetLastDaughter();
449         for (i=imin; i<= imax; i++){
450             TParticle *  jparticle       = (TParticle *) particles->At(i);
451             Int_t ip=jparticle->GetPdgCode();
452             if (KinematicSelection(jparticle,0)&&SelectFlavor(ip)) {
453                 selected=kTRUE; break;
454             }
455             if (DaughtersSelection(jparticle, particles)) {selected=kTRUE; break; }
456         }
457     } else {
458         return kFALSE;
459     }
460
461     return selected;
462 }
463
464
465 Bool_t AliGenHerwig::SelectFlavor(Int_t pid)
466 {
467 // Select flavor of particle
468 // 0: all
469 // 4: charm and beauty
470 // 5: beauty
471     if (fFlavor == 0) return kTRUE;
472
473     Int_t ifl=TMath::Abs(pid/100);
474     if (ifl > 10) ifl/=10;
475     return (fFlavor == ifl);
476 }
477
478 Bool_t AliGenHerwig::Stable(TParticle*  particle)
479 {
480 // Return true for a stable particle
481 //
482     Int_t kf = TMath::Abs(particle->GetPdgCode());
483
484     if ( (particle->GetFirstDaughter() < 0 ) || (kf == 1000*fFlavor+122))
485
486     {
487         return kTRUE;
488     } else {
489         return kFALSE;
490     }
491 }
492
493 void AliGenHerwig::FinishRun()
494 {
495   fHerwig->Hwefin();
496 }
497
498 void AliGenHerwig::FinishRunJimmy()
499 {
500   fHerwig->Hwefin();
501   fHerwig->Jmefin();
502
503 }
504