-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cc
609 lines (507 loc) · 19.9 KB
/
main.cc
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
// PYTHIA script to generate neutrino production.
// No EW correction added
// Author : Q.R. Liu
#include "Pythia8/Pythia.h"
// Allow assertions
#include <cassert>
#include <cstdlib>
using namespace Pythia8;
//==========================================================================
// Pythia generator.
Pythia pythia;
// Debug flag.
const bool DEBUG = false;
//const bool DEBUG = true;
// Binning definitions.
/* Hist nuAll ("all flavor spectrum", nBinIn, 0., xMaxIn); */
Hist nuE;
Hist nuEBar;
Hist nuMu;
Hist nuMuBar;
Hist nuTau;
Hist nuTauBar;
//==========================================================================
// Pretty print.
template <class T>
void prettyPrint(string name, T value){
if (!DEBUG) return;
cout << "||| " << setw(20) << name << " = " << setw(10) << value << endl;
return;
}
//==========================================================================
// Check if particle is a B Meson.
bool isBMeson(int& id){
int idAbs = abs(id);
if (idAbs == 511 || idAbs == 521 || idAbs == 531 || idAbs == 541 )
return true;
else return false;
}
//==========================================================================
// Check if particle is a B Baryon.
bool isBBaryon(int& id){
int idAbs = abs(id);
if (idAbs == 5122 || idAbs == 5132 || idAbs == 5232 || idAbs == 5332 ||
idAbs == 5142 || idAbs == 5242 || idAbs == 5412 || idAbs == 5422 ||
idAbs == 5414 || idAbs == 5424 || idAbs == 5342 || idAbs == 5432 ||
idAbs == 5434 || idAbs == 5442 || idAbs == 5444 || idAbs == 5512 ||
idAbs == 5522 || idAbs == 5514 || idAbs == 5524 || idAbs == 5532 ||
idAbs == 5534 || idAbs == 5542 || idAbs == 5544 || idAbs == 5554)
return true;
else return false;
}
//==========================================================================
// Check if particle is a D Meson.
bool isDMeson(int& id){
int idAbs = abs(id);
if (idAbs == 411 || idAbs == 421 || idAbs == 431)
return true;
else return false;
}
//==========================================================================
// Check if particle is a C Baryon.
bool isCBaryon(int& id){
int idAbs = abs(id);
if (idAbs == 4122 || idAbs == 4232 || idAbs == 4132 || idAbs == 4332 ||
idAbs == 4412 || idAbs == 4422 || idAbs == 4414 || idAbs == 4424 ||
idAbs == 4432 || idAbs == 4434 || idAbs == 4444)
return true;
else return false;
}
//==========================================================================
// Check if particle is long-lived
bool isLongLived(int& id){
int idAbs = abs(id);
//if (idAbs == 13 || idAbs == 211 || idAbs == 321 || idAbs == 130 ||
// idAbs == 2112
// )
//K-, n, pi- are absorbed. K0L should oscillate to K0S
if (idAbs == 13 || id == 211 || id == 321
)
return true;
else return false;
}
// Fill histogram with final state neutrinos.
void fillFSNeutrino(Event& event, double& weight, double& Mdm){
//test
if (DEBUG) event.list();
for (int i = 0; i < event.size(); ++i){
if (event[i].isFinal()) {
int idFS = event[i].id();
double eFS = event[i].e();
if (idFS == 14) nuMu.fill(eFS/Mdm, weight);
else if (idFS == -14) nuMuBar.fill(eFS/Mdm, weight);
else if (idFS == 12) nuE.fill(eFS/Mdm, weight);
else if (idFS == -12) nuEBar.fill(eFS/Mdm, weight);
else if (idFS == 16) nuTau.fill(eFS/Mdm, weight);
else if (idFS == -16) nuTauBar.fill(eFS/Mdm, weight);
}
}
return;
}
//==========================================================================
// Decay a particle.
// Input event record is updated with the decay event record.
bool decay(int& id, Vec4& p4Vec, Event& event){
if (DEBUG) cout << "||| Entering decay" << endl;
prettyPrint("id", id);
prettyPrint("p4", p4Vec);
// Reset event record and set particle to decay.
pythia.event.reset();
if (abs(id) != 310)
pythia.particleData.mayDecay(id, true);
// Decay with Pythia.
double mass = pythia.particleData.m0(id);
pythia.event.append(id, 91, 0, 0, p4Vec, mass);
if (!pythia.moreDecays()){
cout << "decay failed for id =" << id << '\n';
return false;
}
// Reset decay of particle.
if (abs(id) != 310)
pythia.particleData.mayDecay(id, false);
// Set the event record.
event = pythia.event;
if (DEBUG) event.list();
if (DEBUG) cout << "||| Leaving decay" << endl;
return true;
}
//==========================================================================
// Hadronize the jet produced from the heavy quark in an interaction.
// Input event record is updated with the interaction event record.
bool interact(int& id, Vec4& p4Vec, Event& event){
if (DEBUG) cout << "||| Entering interact" << endl;
// Define interaction kinematics.
double m = pythia.particleData.m0(id);
double mc = pythia.particleData.m0(4);
double qE = p4Vec.e();
if ( isBMeson (id) ) qE = qE*0.7;
else if ( isDMeson (id) ) qE = qE*0.6*mc/m;
else if ( isBBaryon (id) ) qE = qE*0.7;
else if ( isCBaryon (id) ) qE = qE*0.6*mc/m;
if (qE > m){
double qP = sqrt(qE*qE-m*m);
prettyPrint("id", id);
prettyPrint("p4", p4Vec);
prettyPrint("m", m);
prettyPrint("qE", qE);
prettyPrint("qP", qP);
pythia.event.reset();
pythia.event.append(id, 91, 0, 0, 0., 0., qP, qE, m);
//pythia.next();
event = pythia.event;
if (DEBUG) event.list();
if (DEBUG) cout << "||| Leaving interact" << endl;
return true;
}
else
return false;
}
//==========================================================================
// Longlived particles lose energy before decaying.
bool RestDecayLongLived(int& id, double Mdm, double inWeight=1.){
Event restEvent;
double m_id = pythia.particleData.m0(id);
Vec4 p4vec = Vec4(0.,0.,0.,m_id);
if ( !decay (id, p4vec, restEvent)) return false;
prettyPrint("restDecay size", restEvent.size());
double weight = inWeight/(4*M_PI);
fillFSNeutrino(restEvent, weight, Mdm);
for (int i = 0; i < restEvent.size(); ++i){
if (restEvent[i].isFinal() && inWeight!=0.) {
int idFS = restEvent[i].id();
if (isLongLived(idFS)){
double EFS = restEvent[i].e();
double mFS = pythia.particleData.m0(idFS);
if ( EFS >= mFS ){
if ( !RestDecayLongLived(idFS,Mdm,weight)) return false;
}
}
}
}
if (DEBUG) cout << "|| Leaving rest decay" << endl;
return true;
}
//==========================================================================
bool RegDecay(int& id, Vec4& p4Vec, double Mdm, double inWeight=1.){
int idreg;
Event regEvent = pythia.event;
if (id == 130) idreg = 310;
else if (id == -130) idreg = -310;
if ( !decay (idreg, p4Vec, regEvent)) return false;
prettyPrint("regeneratedDecay size", regEvent.size());
fillFSNeutrino(regEvent, inWeight, Mdm);
for (int i = 0; i < regEvent.size(); ++i){
if (regEvent[i].isFinal() && inWeight!=0.) {
int idFS = regEvent[i].id();
if (isLongLived(idFS)){
double EFS = regEvent[i].e();
double mFS = pythia.particleData.m0(idFS);
if ( EFS >= mFS ){
if ( !RestDecayLongLived(idFS,Mdm,inWeight)) return false;
}
}
}
}
if (DEBUG) cout << "|| Leaving regenerated decay" << endl;
return true;
}
// Account for interactions as well as decays of a particle.
// Particle must be either a B Meson, D Meson, B Baryon, C Baryon.
// Events are weighted according to the interaction and decay rates.
bool interDecay(int& id, Vec4& p4Vec, double Mdm, string loc="Sun", double inWeight=1.,double rho_matter=148.9,double lower_bound = 0.){
if (DEBUG) cout << "|| Entering interDecay" << endl;
if (isnan(inWeight)) assert(0);
// Define decay length, depending on the boost factor.
double e = p4Vec.e();
prettyPrint ("energy", e);
if (e <= 10.) return true;
double mass = pythia.particleData.m0(id);
double gamma = e / mass;
if (gamma<1.) return true;
double beta = sqrt(1. - ((1. / gamma) * (1. / gamma)));
double lifetime = pythia.particleData.tau0(id);
double decayLength = lifetime * gamma;
//Check if particle decays immediately.
if (decayLength == 0.){
cout << "Error, particle ID " << id << " should be set to decay" << endl;
assert (0);
}
// Define interaction length, depending on the type of particle.
// In units of mm/c.
double interLength;
double xSec;
if ( isBMeson (id) ) xSec = 1.4;
else if ( isDMeson (id) ) xSec = 1.4;
else if ( isBBaryon (id) ) xSec = 2.4;
else if ( isCBaryon (id) ) xSec = 2.4;
else {
cout << "Error, trying to decay " << id << "\n" << endl;
assert(0);
}
double rho = rho_matter;
double N = 6.0221409e23;
prettyPrint("rho", rho);
prettyPrint("N", N);
prettyPrint("xSec", xSec);
interLength = 1./(rho*N*xSec*1e-26*beta)*10.; //mm/c
double epsilon = 1e-6;
if (inWeight < epsilon) return true;
// Weight by the ratio of the interaction and decay rates.
double decayWeight = inWeight * interLength / (interLength + decayLength);
double interWeight = inWeight * decayLength / (interLength + decayLength);
// Do interactions and decays.
prettyPrint("particle", id);
prettyPrint("decayLength", decayLength);
prettyPrint("interLength", interLength);
prettyPrint("inWeight", inWeight);
prettyPrint("decayWeight", decayWeight);
prettyPrint("interWeight", interWeight);
prettyPrint("energy", e);
prettyPrint("gamma", gamma);
prettyPrint("beta", beta);
prettyPrint("lifetime", lifetime);
prettyPrint("decay length", decayLength);
if (isnan(decayWeight)) assert(0);
if (isnan(interWeight)) assert(0);
Event decayEvent, interEvent;
if ( !decay (id, p4Vec, decayEvent) ) return false;
prettyPrint("decayEvent size", decayEvent.size());
fillFSNeutrino(decayEvent, decayWeight, Mdm);
if ( !interact(id, p4Vec, interEvent) ) return false;
prettyPrint("interEvent size", interEvent.size());
fillFSNeutrino(interEvent, interWeight, Mdm);
// Fill histogram with final state neutrinos.
// Handle decay products further.
for (int i = 0; i < decayEvent.size(); ++i){
if (decayEvent[i].isFinal() && decayWeight!=0.) {
int idFS = decayEvent[i].id();
if (isBMeson(idFS) || isBBaryon(idFS) ||
isDMeson(idFS) || isCBaryon(idFS)){
Vec4 p4vecFS = decayEvent[i].p();
if ( !interDecay(idFS, p4vecFS, Mdm, loc, decayWeight, rho,lower_bound) ) return false;
}
else if (isLongLived(idFS) && lower_bound <= pythia.particleData.m0(idFS)){
if ( !RestDecayLongLived(idFS,Mdm,decayWeight) ) {
cout << "restdecaylonglived failed with id = " << idFS << "\n";
return false;
}
}
else if (abs(idFS) == 130 && lower_bound <= pythia.particleData.m0(idFS)){
Vec4 p4Vec_reg = decayEvent[i].p();
if (!RegDecay(idFS, p4Vec_reg, Mdm, decayWeight)) return false;
}
}
}
// Handle interaction products further.
for (int i = 0; i < interEvent.size(); ++i){
if (interEvent[i].isFinal() && interWeight!=0.) {
int idFS = interEvent[i].id();
if (isBMeson(idFS) || isBBaryon(idFS) ||
isDMeson(idFS) || isCBaryon(idFS)){
Vec4 p4vecFS = interEvent[i].p();
if ( !interDecay(idFS, p4vecFS, Mdm, loc,interWeight, rho,lower_bound) ) return false;
}
}
}
if (DEBUG) cout << "|| Leaving interDecay" << endl;
return true;
}
// A derived class for (e+ e- ->) GenericResonance -> various final states.
class Sigma1GenRes : public Sigma1Process {
public:
// Constructor.
Sigma1GenRes() {}
// Evaluate sigmaHat(sHat): dummy unit cross section.
virtual double sigmaHat() {return 1.;}
// Select flavour. No colour or anticolour.
virtual void setIdColAcol() {setId( -11, 11, 999999);
setColAcol( 0, 0, 0, 0, 0, 0);}
// Info on the subprocess.
virtual string name() const {return "GenericResonance";}
virtual int code() const {return 9001;}
virtual string inFlux() const {return "ffbarSame";}
};
//==========================================================================
// Main program function.
int main(int argc, char** argv) {
// A class to generate the fictitious resonance initial state.
SigmaProcess* sigma1GenRes = new Sigma1GenRes();
// Hand pointer to Pythia.
pythia.setSigmaPtr(sigma1GenRes);
string channel = string(argv[1]);
float xMaxIn = atof(argv[2]);
int nBinIn = atoi(argv[3]);
double bin = double(nBinIn);
string location = string(argv[4]);
string process = string(argv[5]);
double rho_matter = atof(argv[6]); //density of mediator decay position
int seed = atoi(argv[7]);
string binscale = string(argv[8]);
float lower_edge = atof(argv[9]);
string secluded = string(argv[11]);
(void)argc;
bool Log;
if (binscale == "log") Log = true;
else Log = false;
nuE = Hist("electron neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
nuEBar = Hist("anti electron neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
nuMu = Hist("muon neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
nuMuBar = Hist("anti muon neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
nuTau = Hist("tau neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
nuTauBar= Hist("anti tau neutrino spectrum", nBinIn, lower_edge/xMaxIn, 1.+1e-7, Log);
if (secluded == "secluded")
pythia.readFile("./cmnd/"+channel+"_"+argv[2]+"_"+argv[10]+"_"+process+".cmnd");
else
pythia.readFile("./cmnd/"+channel+"_"+argv[2]+"_"+process+".cmnd");
if (rho_matter != 0.) {
prettyPrint("Consider interaction at: ", location);
pythia.readFile("decays.cmnd");
}
else if (rho_matter == 0.){
prettyPrint("No interaction at: ", location);
//Long-lived particles should decay
pythia.particleData.mayDecay(13, true); //mu+-
pythia.particleData.mayDecay(211, true); //pi+-
pythia.particleData.mayDecay(321, true); //K+-
pythia.particleData.mayDecay(130, true); //K0_L
pythia.particleData.mayDecay(2112, true); //n
}
// Intialize hadronization Pythia object.
//pythia.readString("SoftQCD:all = on");
pythia.readString("HardQCD:all = on");
pythia.readString("Random:setSeed = on");
pythia.readString("Random:seed = "+std::to_string(seed));
pythia.readString("WeakBosonExchange:all = on");
pythia.readString("WeakBosonExchange:ff2ff(t:W) = on");
pythia.readString("WeakSingleBoson:all = on");
pythia.readString("WeakDoubleBoson:all = on");
pythia.readString("WeakBosonAndParton:all = on");
pythia.readString("TimeShower:weakShower = on");
// Initialization.
if (!DEBUG) pythia.readString("Print:quiet = on");
pythia.init();
// Extract settings to be used in the main program.
int nEvent = pythia.mode("Main:numberOfEvents");
int nAbort = pythia.mode("Main:timesAllowErrors");
// Progress bar settings.
int barWidth = 70;
// Begin event loop.
int iAbort = 0;
for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
// Progress bar
if (DEBUG) {
cout << "[";
double progress = (iEvent / (double)nEvent);
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << int(progress * 100.0) << " %\r";
std::cout.flush();
}
// Generate events. Quit if many failures.
if (DEBUG) cout << "======================================" << endl;
if (DEBUG) cout << "GENERATING NEW EVENT #" << iEvent+1 << endl;
if (!pythia.next()) {
if (++iAbort < nAbort){
--iEvent;
continue;
}
//cout << " Event generation aborted prematurely, owing to error!\n";
//assert(0);
}
// Get the Event Record.
Event event = pythia.event;
if (DEBUG) cout << "Event size = " << event.size() << endl;
if (DEBUG) cout << "======================================" << endl;
// Loop over all particles and analyze the final-state ones.
//test
if (DEBUG) event.list();
for (int i = 0; i < event.size(); ++i){
if (event[i].isFinal()) {
int idFS = event[i].id();
double eFS = event[i].e();
// Fill final state neutrinos into histogram.
if (idFS == 14) nuMu.fill(eFS/xMaxIn);
else if (idFS == -14) nuMuBar.fill(eFS/xMaxIn);
else if (idFS == 12) nuE.fill(eFS/xMaxIn);
else if (idFS == -12) nuEBar.fill(eFS/xMaxIn);
else if (idFS == 16) nuTau.fill(eFS/xMaxIn);
else if (idFS == -16) nuTauBar.fill(eFS/xMaxIn);
// Undergo further interactions for particles which interact.
if (location == "Sun" ||location == "Earth") {
if (isBMeson(idFS) || isBBaryon(idFS) || isDMeson(idFS) ||
isCBaryon(idFS)){
Vec4 p4Vec = event[i].p();
if ( !interDecay(idFS, p4Vec,xMaxIn,location,1.,rho_matter,lower_edge) ){
if (++iAbort < nAbort) continue;
cout << " Event generation aborted prematurely, owing to error!\n";
assert(0);
};
}
else if (isLongLived(idFS) && lower_edge <= pythia.particleData.m0(idFS)){
if ( !RestDecayLongLived(idFS,xMaxIn,1.) ){
if (++iAbort < nAbort) continue;
cout << " Event generation aborted prematurely, owing to error!\n";
assert(0);
};
}
else if (abs(idFS) == 130 && lower_edge <= pythia.particleData.m0(idFS)){
Vec4 p4Vec_reg = event[i].p();
if (!RegDecay(idFS, p4Vec_reg, xMaxIn, 1.)){
if (++iAbort < nAbort) continue;
cout << " Event generation aborted prematurely, owing to error!\n";
assert(0);
};
}
}
}
}
// End of event loop.
}
// Final statistics and histograms.
// Rescale according to number of events and binning.
pythia.stat();
double width;
string yaxis;
if (binscale == "log") {width = (log10 (xMaxIn * (1. + 1e-7) / xMaxIn) - log10 (lower_edge / xMaxIn)) / bin;
yaxis = "$\\mathrm{d}N / \\mathrm{d}logx$";
}
else {width = (xMaxIn * (1. + 1e-7) - lower_edge) / (xMaxIn * bin);
yaxis = "$\\mathrm{d}N / \\mathrm{d}x$";
}
nuE *= 1. / (nEvent * width);
nuEBar *= 1. / (nEvent * width);
nuMu *= 1. / (nEvent * width);
nuMuBar *= 1. / (nEvent * width);
nuTau *= 1. / (nEvent * width);
nuTauBar *= 1. / (nEvent * width);
if (DEBUG){
cout << nuE << nuEBar << nuMu << nuMuBar << nuTau << nuTauBar << endl;
}
if (secluded == "secluded")
{HistPlot hpl("./plot_"+channel+"_"+argv[2]+"_"+argv[10]+"_"+argv[6]);
hpl.frame("./secluded/"+channel+"_"+argv[2]+"_"+argv[10]+"_"+process+"_"+argv[6]+"_"+binscale, "Particle energy spectra", "$E$ (GeV)", yaxis);
hpl.add(nuE, "-", "$\\nu_e$");
hpl.add(nuEBar, "-", "$\\bar{\\nu}_e$");
hpl.add(nuMu, "-", "$\\nu_\\mu$");
hpl.add(nuMuBar, "-", "$\\bar{\\nu}_\\mu$");
hpl.add(nuTau, "-", "$\\nu_\\tau$");
hpl.add(nuTauBar, "-", "$\\bar{\\nu}_\\tau}$");
hpl.plot(true);}
else
{HistPlot hpl("./plot_"+channel+"_"+argv[2]+"_"+location);
hpl.frame("./"+location+"/"+channel+"_"+argv[2]+"_"+location+"_"+process+"_"+binscale, "Particle energy spectra", "$E$ (GeV)", yaxis);
hpl.add(nuE, "-", "$\\nu_e$");
hpl.add(nuEBar, "-", "$\\bar{\\nu}_e$");
hpl.add(nuMu, "-", "$\\nu_\\mu$");
hpl.add(nuMuBar, "-", "$\\bar{\\nu}_\\mu$");
hpl.add(nuTau, "-", "$\\nu_\\tau$");
hpl.add(nuTauBar, "-", "$\\bar{\\nu}_\\tau}$");
hpl.plot(true);}
delete sigma1GenRes;
return 0;
}
//==========================================================================