-
Notifications
You must be signed in to change notification settings - Fork 3
/
DPPrimaryGeneratorAction.cxx
741 lines (615 loc) · 29.1 KB
/
DPPrimaryGeneratorAction.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include "DPPrimaryGeneratorAction.h"
#include <fstream>
#include <string>
#include "Randomize.hh"
#include "G4SystemOfUnits.hh"
#include "G4PhysicalConstants.hh"
#include <TFile.h>
#include <TTree.h>
#include <TMath.h>
#include <TVector3.h>
#include <TLorentzVector.h>
namespace DPGEN
{
// global parameters
const double pi = TMath::Pi();
const double twopi = 2.*pi;
const double sqrt2pi = TMath::Sqrt(twopi);
// masses
const double mp = 0.93827;
const double mmu = 0.10566;
const double mjpsi = 3.097;
const double mpsip = 3.686;
// 4-vectors
const double ebeam = 120.;
const TLorentzVector p_beam(0., 0., TMath::Sqrt(ebeam*ebeam - mp*mp), ebeam);
const TLorentzVector p_target(0., 0., 0., mp);
const TLorentzVector p_cms = p_beam + p_target;
const TVector3 bv_cms = p_cms.BoostVector();
const double s = p_cms.M2();
const double sqrts = p_cms.M();
//distribution-wise constants
const double pT0DY = 2.8;
const double pTpowDY = 1./(6. - 1.);
const double pT0JPsi = 3.0;
const double pTpowJPsi = 1./(6. - 1.);
//charmonium generation constants Ref: Schub et al Phys Rev D 52, 1307 (1995)
const double sigmajpsi = 0.2398; //Jpsi xf gaussian width
const double brjpsi = 0.0594; //Br(Jpsi -> mumu)
const double ajpsi = 0.001464*TMath::Exp(-16.66*mjpsi/sqrts);
const double bjpsi = 2.*sigmajpsi*sigmajpsi;
const double psipscale = 0.019; //psip relative to jpsi
}
DPPrimaryGeneratorAction::DPPrimaryGeneratorAction()
{
p_config = DPSimConfig::instance();
p_IOmamnger = DPIOManager::instance();
p_vertexGen = DPVertexGenerator::instance();
particleGun = new G4ParticleGun(1);
particleDict = G4ParticleTable::GetParticleTable();
proton = particleDict->FindParticle(2212);
mup = particleDict->FindParticle(-13);
mum = particleDict->FindParticle(13);
ep = particleDict->FindParticle(-11);
em = particleDict->FindParticle(11);
pip = particleDict->FindParticle(211);
pim = particleDict->FindParticle(-211);
if(p_config->finalParticle == "mu")
{
finalPar[0] = mup;
finalPar[1] = mum;
}
else if(p_config->finalParticle == "e")
{
finalPar[0] = ep;
finalPar[1] = em;
}
else if(p_config->finalParticle == "pi")
{
finalPar[0] = pip;
finalPar[1] = pim;
}
pdf = LHAPDF::mkPDF("CT10nlo", 0);
//TODO: need to find a way to pass the random number seed to pythia as well
//initilize all kinds of generators
if(p_config->generatorType == "dimuon")
{
if(p_config->generatorEng == "legacyDY")
{
std::cout << " Using legacy Drell-Yan generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateDrellYan;
}
else if(p_config->generatorEng == "legacyJPsi")
{
std::cout << " Using legacy JPsi generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateJPsi;
}
else if(p_config->generatorEng == "legacyPsip")
{
std::cout << " Using Psip generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generatePsip;
}
else if(p_config->generatorEng == "PHSP")
{
std::cout << " Using phase space generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generatePhaseSpace;
}
else if(p_config->generatorEng == "pythia")
{
std::cout << " Using pythia pythia generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generatePythiaDimuon;
ppGen.readFile(p_config->pythiaConfig.Data());
pnGen.readFile(p_config->pythiaConfig.Data());
ppGen.readString("Beams:idB = 2212");
ppGen.readString("Beams:idB = 2112");
ppGen.init();
pnGen.init();
}
else if(p_config->generatorEng == "DarkPhotonFromEta")
{
std::cout << " Using dark photon generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateDarkPhotonFromEta;
ppGen.readFile(p_config->pythiaConfig.Data());
pnGen.readFile(p_config->pythiaConfig.Data());
ppGen.readString("Beams:idB = 2212");
ppGen.readString("Beams:idB = 2112");
ppGen.init();
pnGen.init();
}
else if(p_config->generatorEng == "custom")
{
std::cout << " Using custom LUT dimuon generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateCustomDimuon;
//read and parse the lookup table
std::ifstream fin(p_config->customLUT.Data());
std::cout << " Initializing custom dimuon cross section from LUT " << p_config->customLUT << std::endl;
//Load the range and number of bins in each dimension
std::string line;
int n, n_m, n_xF;
double m_min, m_max, xF_min, xF_max;
double m_bin, xF_bin;
getline(fin, line);
std::stringstream ss(line);
ss >> n >> n_m >> m_min >> m_max >> m_bin >> n_xF >> xF_min >> xF_max >> xF_bin;
//test if the range is acceptable
if(p_config->massMin < m_min || p_config->massMax > m_max || p_config->xfMin < xF_min || p_config->xfMax > xF_max)
{
std::cout << " ERROR: the specified phase space limits are larger than LUT limits!" << std::endl;
exit(EXIT_FAILURE);
}
lut = new TH2D("LUT", "LUT", n_m, m_min - 0.5*(m_max - m_min)/(n_m - 1), m_max + 0.5*(m_max - m_min)/(n_m - 1),
n_xF, xF_min - 0.5*(xF_max - xF_min)/(n_xF - 1), xF_max + 0.5*(xF_max - xF_min)/(n_xF - 1));
while(getline(fin, line))
{
double mass, xF, xsec;
std::stringstream ss(line);
ss >> mass >> xF >> xsec;
xsec *= (m_bin*xF_bin);
lut->Fill(mass, xF, xsec);
}
}
else
{
std::cout << "ERROR: Generator engine is not set or ncd /seaot supported in dimuon mode" << std::endl;
exit(EXIT_FAILURE);
}
}
else if(p_config->generatorType == "single")
{
if(p_config->generatorEng == "pythia")
{
std::cout << " Using pythia single generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generatePythiaSingle;
ppGen.readFile(p_config->pythiaConfig.Data());
pnGen.readFile(p_config->pythiaConfig.Data());
ppGen.readString("Beams:idB = 2212");
ppGen.readString("Beams:idB = 2112");
ppGen.init();
pnGen.init();
}
else if(p_config->generatorEng == "geant")
{
std::cout << " Using geant4 single generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateGeant4Single;
}
else if(p_config->generatorEng == "test")
{
std::cout << " Using test single generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateTestSingle;
if(p_config->testParticle == "mu")
{
testPar[0] = mup;
testPar[1] = mum;
}
else if(p_config->testParticle == "e")
{
testPar[0] = ep;
testPar[1] = em;
}
else if(p_config->testParticle == "pi")
{
testPar[0] = pip;
testPar[1] = pim;
}
}
else
{
std::cout << "ERROR: Generator engine is not set or not supported in single mode" << std::endl;
exit(EXIT_FAILURE);
}
}
else if(p_config->generatorType == "external")
{
std::cout << " Using external generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateExternal;
externalInputFile = new TFile(p_config->externalInput.Data(), "READ");
externalInputTree = (TTree*)externalInputFile->Get("save");
externalPositions = new TClonesArray("TVector3");
externalMomentums = new TClonesArray("TVector3");
externalInputTree->SetBranchAddress("eventID", &externalEventID);
externalInputTree->SetBranchAddress("n", &nExternalParticles);
externalInputTree->SetBranchAddress("pdg", externalParticlePDGs);
externalInputTree->SetBranchAddress("pos", &externalPositions);
externalInputTree->SetBranchAddress("mom", &externalMomentums);
//take over the control of the buffer flushing, TODO: move this thing to somewhere else
lastFlushPosition = 0;
p_IOmamnger->setBufferState(DPIOManager::CLEAN);
}
else if(p_config->generatorType == "Debug")
{
std::cout << " Using simple debug generator ..." << std::endl;
p_generator = &DPPrimaryGeneratorAction::generateDebug;
}
else
{
std::cout << "ERROR: Generator type not recognized! Will exit.";
exit(EXIT_FAILURE);
}
//force pion/kaon decay by changing the lifetime
if(p_config->forcePionDecay)
{
std::cout << " Forcing pion to decay immediately ..." << std::endl;
particleDict->FindParticle(211)->SetPDGStable(false);
particleDict->FindParticle(211)->SetPDGLifeTime(0.);
particleDict->FindParticle(-211)->SetPDGStable(false);
particleDict->FindParticle(-211)->SetPDGLifeTime(0.);
}
if(p_config->forceKaonDecay)
{
std::cout << " Forcing kaon to decay immediately ..." << std::endl;
particleDict->FindParticle(321)->SetPDGStable(false);
particleDict->FindParticle(321)->SetPDGLifeTime(0.);
particleDict->FindParticle(-321)->SetPDGStable(false);
particleDict->FindParticle(-321)->SetPDGLifeTime(0.);
}
}
DPPrimaryGeneratorAction::~DPPrimaryGeneratorAction()
{
delete pdf;
delete particleGun;
if(p_config->generatorType == "custom") delete lut;
}
void DPPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
p_config->nEventsThrown++;
theEvent = anEvent;
(this->*p_generator)();
}
void DPPrimaryGeneratorAction::generateDrellYan()
{
DPMCDimuon dimuon;
double mass = G4UniformRand()*(p_config->massMax - p_config->massMin) + p_config->massMin;
double xF = G4UniformRand()*(p_config->xfMax - p_config->xfMin) + p_config->xfMin;
if(!generateDimuon(mass, xF, dimuon, true)) return;
p_vertexGen->generateVertex(dimuon);
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
//calculate the cross section
//PDF-related
double zOverA = p_vertexGen->getPARatio();
double nOverA = 1. - zOverA;
double dbar1 = pdf->xfxQ(-1, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double ubar1 = pdf->xfxQ(-2, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double d1 = pdf->xfxQ(1, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double u1 = pdf->xfxQ(2, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double s1 = pdf->xfxQ(3, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double c1 = pdf->xfxQ(4, dimuon.fx1, dimuon.fMass)/dimuon.fx1;
double dbar2 = pdf->xfxQ(-1, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double ubar2 = pdf->xfxQ(-2, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double d2 = pdf->xfxQ(1, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double u2 = pdf->xfxQ(2, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double s2 = pdf->xfxQ(3, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double c2 = pdf->xfxQ(4, dimuon.fx2, dimuon.fMass)/dimuon.fx2;
double xsec_pdf = 4./9.*(u1*(zOverA*ubar2 + nOverA*dbar2) + ubar1*(zOverA*u2 + nOverA*d2) + 2*c1*c2) +
1./9.*(d1*(zOverA*dbar2 + nOverA*ubar2) + dbar1*(zOverA*d2 + nOverA*u2) + 2*s1*s2);
//KFactor
double xsec_kfactor = 1.;
if(dimuon.fMass < 2.5)
{
xsec_kfactor = 1.25;
}
else if(dimuon.fMass < 7.5)
{
xsec_kfactor = 1.25 + (1.82 - 1.25)*(dimuon.fMass - 2.5)/5.;
}
else
{
xsec_kfactor = 1.82;
}
//phase space
double xsec_phsp = dimuon.fx1*dimuon.fx2/(dimuon.fx1 + dimuon.fx2)/dimuon.fMass/dimuon.fMass/dimuon.fMass;
//generation limitation
double xsec_limit = (p_config->massMax - p_config->massMin)*(p_config->xfMax - p_config->xfMin)*
(p_config->cosThetaMax*p_config->cosThetaMax*p_config->cosThetaMax/3. + p_config->cosThetaMax
- p_config->cosThetaMin*p_config->cosThetaMin*p_config->cosThetaMin/3. - p_config->cosThetaMin)*4./3.;
double xsec = xsec_pdf*xsec_kfactor*xsec_phsp*xsec_limit*p_vertexGen->getLuminosity();
dimuon.fPosTrackID = 1;
dimuon.fNegTrackID = 2;
p_IOmamnger->fillOneDimuon(xsec, dimuon);
}
void DPPrimaryGeneratorAction::generateJPsi()
{
DPMCDimuon dimuon;
double xF = G4UniformRand()*(p_config->xfMax - p_config->xfMin) + p_config->xfMin;
if(!generateDimuon(DPGEN::mjpsi, xF, dimuon)) return;
p_vertexGen->generateVertex(dimuon);
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
//calculate the cross section
//xf distribution
double xsec_xf = DPGEN::ajpsi*TMath::Exp(-dimuon.fxF*dimuon.fxF/DPGEN::bjpsi)/(DPGEN::sigmajpsi*DPGEN::sqrt2pi);
//generation limitation
double xsec_limit = p_config->xfMax - p_config->xfMin;
double xsec = DPGEN::brjpsi*xsec_xf*xsec_limit*p_vertexGen->getLuminosity();
dimuon.fPosTrackID = 1;
dimuon.fNegTrackID = 2;
p_IOmamnger->fillOneDimuon(xsec, dimuon);
}
void DPPrimaryGeneratorAction::generatePsip()
{
DPMCDimuon dimuon;
double xF = G4UniformRand()*(p_config->xfMax - p_config->xfMin) + p_config->xfMin;
if(!generateDimuon(DPGEN::mpsip, xF, dimuon)) return;
p_vertexGen->generateVertex(dimuon);
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
//calculate the cross section
//xf distribution
double xsec_xf = DPGEN::ajpsi*TMath::Exp(-dimuon.fxF*dimuon.fxF/DPGEN::bjpsi)/(DPGEN::sigmajpsi*DPGEN::sqrt2pi);
//generation limitation
double xsec_limit = p_config->xfMax - p_config->xfMin;
double xsec = DPGEN::psipscale*DPGEN::brjpsi*xsec_xf*xsec_limit*p_vertexGen->getLuminosity();
dimuon.fPosTrackID = 1;
dimuon.fNegTrackID = 2;
p_IOmamnger->fillOneDimuon(xsec, dimuon);
}
void DPPrimaryGeneratorAction::generateDarkPhotonFromEta()
{
TVector3 vtx = p_vertexGen->generateVertex();
double pARatio = p_vertexGen->getPARatio();
Pythia8::Pythia* p_pythia = G4UniformRand() < pARatio ? &ppGen : &pnGen;
while(!p_pythia->next()) {}
int nEtas = 1;
Pythia8::Event& particles = p_pythia->event;
for(int i = 1; i < particles.size(); ++i)
{
if(particles[i].id() == 221)
{
//Fill eta to particle gun as well, it will probably make no difference in detector
G4ThreeVector g4vtx = G4ThreeVector(vtx.X()*cm, vtx.Y()*cm, vtx.Z()*cm) + G4ThreeVector(particles[i].xProd()*mm, particles[i].yProd()*mm, particles[i].zProd()*mm);
particleGun->SetParticleDefinition(particleDict->FindParticle(221));
particleGun->SetParticlePosition(g4vtx);
particleGun->SetParticleMomentum(G4ThreeVector(particles[i].px()*GeV, particles[i].py()*GeV, particles[i].pz()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
DPMCDimuon dimuon;
//dimuon.fVertex.SetXYZ(g4vtx.x(), g4vtx.y(), g4vtx.z());
dimuon.fVertex.SetXYZ(G4RandGauss::shoot(0., 1.5), G4RandGauss::shoot(0., 1.5), G4UniformRand()*(p_config->zOffsetMax - p_config->zOffsetMin) + p_config->zOffsetMin);
//eta -> gamma A', this step decays isotropically
TLorentzVector p_eta(particles[i].px(), particles[i].py(), particles[i].pz(), particles[i].e());
double mass_eta_decays[2] = {G4UniformRand()*(p_eta.M() - 2.*finalPar[0]->GetPDGMass()/GeV) + 2.*finalPar[0]->GetPDGMass()/GeV, 0.};
phaseGen.SetDecay(p_eta, 2, mass_eta_decays);
phaseGen.Generate();
TLorentzVector p_AP = *(phaseGen.GetDecay(0));
//A' -> mumu, this step has a 1 + cos^2\theta distribution
double mass_AP_decays[2] = {finalPar[0]->GetPDGMass()/GeV, finalPar[0]->GetPDGMass()/GeV};
phaseGen.SetDecay(p_AP, 2, mass_AP_decays);
bool angular = true;
while(angular)
{
phaseGen.Generate();
dimuon.fPosMomentum = *(phaseGen.GetDecay(0));
dimuon.fNegMomentum = *(phaseGen.GetDecay(1));
dimuon.calcVariables();
angular = 2.*G4UniformRand() > 1. + dimuon.fCosTh*dimuon.fCosTh;
}
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
//add to the IO stream
dimuon.fPosTrackID = nEtas*2 - 1;
dimuon.fNegTrackID = nEtas*2;
p_IOmamnger->fillOneDimuon(1., dimuon);
++nEtas;
}
}
}
void DPPrimaryGeneratorAction::generateCustomDimuon()
{
DPMCDimuon dimuon;
double mass = G4UniformRand()*(p_config->massMax - p_config->massMin) + p_config->massMin;
double xF = G4UniformRand()*(p_config->xfMax - p_config->xfMin) + p_config->xfMin;
if(!generateDimuon(mass, xF, dimuon, true)) return; //TODO: maybe later need to add an option or flag in lut to specify angular distribution
p_vertexGen->generateVertex(dimuon);
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
//calculate the cross section
double xsec = lut->Interpolate(mass, xF)*p_vertexGen->getLuminosity();
dimuon.fPosTrackID = 1;
dimuon.fNegTrackID = 2;
p_IOmamnger->fillOneDimuon(xsec, dimuon);
}
void DPPrimaryGeneratorAction::generatePythiaDimuon()
{
p_config->nEventsPhysics++;
DPMCDimuon dimuon;
TVector3 vtx = p_vertexGen->generateVertex();
double pARatio = p_vertexGen->getPARatio();
Pythia8::Pythia* p_pythia = G4UniformRand() < pARatio ? &ppGen : &pnGen;
while(!p_pythia->next()) {}
int pParID = 0;
for(int i = 1; i < p_pythia->event.size(); ++i)
{
Pythia8::Particle par = p_pythia->event[i];
if(par.status() > 0 && par.id() != 22)
{
G4ThreeVector g4vtx = G4ThreeVector(vtx.X()*cm, vtx.Y()*cm, vtx.Z()*cm) + G4ThreeVector(par.xProd()*mm, par.yProd()*mm, par.zProd()*mm);
particleGun->SetParticleDefinition(particleDict->FindParticle(par.id()));
particleGun->SetParticlePosition(g4vtx);
particleGun->SetParticleMomentum(G4ThreeVector(par.px()*GeV, par.py()*GeV, par.pz()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
++pParID;
if(par.id() == finalPar[0]->GetPDGEncoding())
{
dimuon.fPosTrackID = pParID;
dimuon.fPosMomentum.SetXYZM(par.px(), par.py(), par.pz(), finalPar[0]->GetPDGMass()/GeV);
}
else if(par.id() == finalPar[1]->GetPDGEncoding())
{
dimuon.fNegTrackID = pParID;
dimuon.fNegMomentum.SetXYZM(par.px(), par.py(), par.pz(), finalPar[1]->GetPDGMass()/GeV);
}
dimuon.fVertex.SetXYZ(g4vtx.x(), g4vtx.y(), g4vtx.z());
}
}
p_IOmamnger->fillOneDimuon(1., dimuon);
}
void DPPrimaryGeneratorAction::generatePythiaSingle()
{
p_config->nEventsPhysics++;
TVector3 vtx = p_vertexGen->generateVertex();
double pARatio = p_vertexGen->getPARatio();
Pythia8::Pythia* p_pythia = G4UniformRand() < pARatio ? &ppGen : &pnGen;
while(!p_pythia->next()) {}
for(int j = 1; j < p_pythia->event.size(); ++j)
{
Pythia8::Particle par = p_pythia->event[j];
//for every muon track, find its mother and fill it to the track list as well
if(par.status() > 0 && par.id() != 22)
{
particleGun->SetParticleDefinition(particleDict->FindParticle(par.id()));
particleGun->SetParticlePosition(G4ThreeVector(vtx.X()*cm, vtx.Y()*cm, vtx.Z()*cm) + G4ThreeVector(par.xProd()*mm, par.yProd()*mm, par.zProd()*mm));
particleGun->SetParticleMomentum(G4ThreeVector(par.px()*GeV, par.py()*GeV, par.pz()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
}
}
}
void DPPrimaryGeneratorAction::generateGeant4Single()
{
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(proton);
particleGun->SetParticlePosition(G4ThreeVector(0., 0., -600*cm));
particleGun->SetParticleMomentum(G4ThreeVector(0., 0., p_config->beamMomentum*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
}
void DPPrimaryGeneratorAction::generateTestSingle()
{
p_config->nEventsPhysics++;
double mom = (5. + (p_config->beamMomentum - 5.)*G4UniformRand())*GeV;
double costheta = p_config->cosThetaMin + (p_config->cosThetaMax - p_config->cosThetaMin)*G4UniformRand();
double phi = G4UniformRand()*DPGEN::twopi;
double pz = mom*costheta;
double px = mom*TMath::Sqrt(1. - costheta*costheta)*TMath::Cos(phi);
double py = mom*TMath::Sqrt(1. - costheta*costheta)*TMath::Sin(phi);
double x = G4RandGauss::shoot(0., 1.5)*cm;
double y = G4RandGauss::shoot(0., 1.5)*cm;
double z = (G4UniformRand()*(p_config->zOffsetMax - p_config->zOffsetMin) + p_config->zOffsetMin)*cm;
particleGun->SetParticleDefinition(G4UniformRand() > 0.5 ? testPar[0] : testPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(x, y, z));
particleGun->SetParticleMomentum(G4ThreeVector(px, py, pz));
particleGun->GeneratePrimaryVertex(theEvent);
}
void DPPrimaryGeneratorAction::generatePhaseSpace()
{
DPMCDimuon dimuon;
double mass = G4UniformRand()*(p_config->massMax - p_config->massMin) + p_config->massMin;
double xF = G4UniformRand()*(p_config->xfMax - p_config->xfMin) + p_config->xfMin;
if(!generateDimuon(mass, xF, dimuon)) return;
p_vertexGen->generateVertex(dimuon);
p_config->nEventsPhysics++;
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fPosMomentum.X()*GeV, dimuon.fPosMomentum.Y()*GeV, dimuon.fPosMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
particleGun->SetParticleDefinition(finalPar[1]);
particleGun->SetParticlePosition(G4ThreeVector(dimuon.fVertex.X()*cm, dimuon.fVertex.Y()*cm, dimuon.fVertex.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(dimuon.fNegMomentum.X()*GeV, dimuon.fNegMomentum.Y()*GeV, dimuon.fNegMomentum.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
dimuon.fPosTrackID = 1;
dimuon.fNegTrackID = 2;
p_IOmamnger->fillOneDimuon(1., dimuon);
}
void DPPrimaryGeneratorAction::generateExternal()
{
int eventID = theEvent->GetEventID();
externalInputTree->GetEntry(eventID);
for(int i = 0; i < nExternalParticles; ++i)
{
TVector3 pos = *((TVector3*)externalPositions->At(i));
TVector3 mom = *((TVector3*)externalMomentums->At(i));
particleGun->SetParticleDefinition(particleDict->FindParticle(externalParticlePDGs[i]));
particleGun->SetParticlePosition(G4ThreeVector(pos.X()*cm, pos.Y()*cm, pos.Z()*cm));
particleGun->SetParticleMomentum(G4ThreeVector(mom.X()*GeV, mom.Y()*GeV, mom.Z()*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
}
//send the flush signal in two cases: 1. the external eventID increment exceeds bucket size,
// 2. this is the last event in external tree
if(p_config->bucket_size == 1) return;
if(externalEventID - lastFlushPosition >= p_config->bucket_size || eventID + 1 == p_config->nEvents)
{
lastFlushPosition = (externalEventID/p_config->bucket_size)*p_config->bucket_size;
p_IOmamnger->setBufferState(DPIOManager::FLUSH);
}
}
void DPPrimaryGeneratorAction::generateDebug()
{
particleGun->SetParticleDefinition(finalPar[0]);
particleGun->SetParticlePosition(G4ThreeVector(0., 0., -500.*cm));
particleGun->SetParticleMomentum(G4ThreeVector(0., 0., 50.*GeV));
particleGun->GeneratePrimaryVertex(theEvent);
}
bool DPPrimaryGeneratorAction::generateDimuon(double mass, double xF, DPMCDimuon& dimuon, bool angular)
{
double pz = xF*(DPGEN::sqrts - mass*mass/DPGEN::sqrts)/2.;
double pTmaxSq = (DPGEN::s*DPGEN::s*(1. - xF*xF) - 2.*DPGEN::s*mass*mass + mass*mass*mass*mass)/DPGEN::s/4.;
if(pTmaxSq < 0.) return false;
double pTmax = sqrt(pTmaxSq);
double pT = 10.;
if(pTmax < 0.3)
{
pT = pTmax*sqrt(G4UniformRand());
}
else if(p_config->drellyanMode)
{
while(pT > pTmax) pT = DPGEN::pT0DY*TMath::Sqrt(1./TMath::Power(G4UniformRand(), DPGEN::pTpowDY) - 1.);
}
else
{
while(pT > pTmax) pT = DPGEN::pT0JPsi*TMath::Sqrt(1./TMath::Power(G4UniformRand(), DPGEN::pTpowJPsi) - 1.);
}
double phi = G4UniformRand()*DPGEN::twopi;
double px = pT*TMath::Cos(phi);
double py = pT*TMath::Sin(phi);
//configure phase space generator
TLorentzVector p_dimuon;
p_dimuon.SetXYZM(px, py, pz, mass);
p_dimuon.Boost(DPGEN::bv_cms);
double masses[2] = {finalPar[0]->GetPDGMass()/GeV, finalPar[1]->GetPDGMass()/GeV};
phaseGen.SetDecay(p_dimuon, 2, masses);
bool firstTry = true;
while(firstTry || angular)
{
firstTry = false;
phaseGen.Generate();
dimuon.fPosMomentum = *(phaseGen.GetDecay(0));
dimuon.fNegMomentum = *(phaseGen.GetDecay(1));
dimuon.calcVariables();
angular = 2.*G4UniformRand() > 1. + dimuon.fCosTh*dimuon.fCosTh;
}
if(dimuon.fx1 < p_config->x1Min || dimuon.fx1 > p_config->x1Max) return false;
if(dimuon.fx2 < p_config->x2Min || dimuon.fx2 > p_config->x2Max) return false;
if(dimuon.fCosTh < p_config->cosThetaMin || dimuon.fCosTh > p_config->cosThetaMax) return false;
return true;
}