506 args = parse_args()
507 signal = SIGNAL[args.heavy_quark]
508 p_threshold, signal_ids, signal_process = signal.p_threshold, signal.ids, signal.process
509 proton_fraction = PROTON_FRACTION[args.target_composition]
510 if args.pythia8_tune == "FTFT":
511 tune_settings, kfactors = read_pythia8_settings(FTFT_FILE), signal.kfactors
512 else:
513 tune_settings, kfactors = [], (1.0, 1.0)
514
515 random.seed(args.seed)
516 new_pythia = PythiaFactory(args.seed, tune_settings, [*CASCADE_IDS, *signal_ids])
517
518
519 mbias_pythia = [
520 new_pythia(
521 ["SoftQCD:all = on", "Beams:frameType = 3", "Beams:allowVariableEnergy = on", "Beams:allowIDAswitch = on"]
522 + [f"Beams:idB = {nucleon_id}", f"Beams:pzA = {args.p_beam}", "Beams:pzB = 0."]
523 )
524 for nucleon_id in TARGET_NUCLEONS
525 ]
526
527
528
529
530 p_grid = np.geomspace(p_threshold, args.p_beam, args.n_momentum_points)
531 log_p_grid = np.log(p_grid)
532 log_chi, sigma = {}, {}
533 for pdg_id in CASCADE_IDS:
534 for beam_id in sorted({pdg_id, -pdg_id}):
535 if not PDG.GetParticle(beam_id):
536 continue
537 for i_nucleon, nucleon_id in enumerate(TARGET_NUCLEONS):
538 sigma[beam_id, i_nucleon] = []
539 for p in p_grid:
540 pythia = new_pythia(
541 [signal_process, "PartonLevel:all = off", "HadronLevel:all = off"]
542 + cm_beam_settings(beam_id, nucleon_id, p)
543 )
544 for _ in range(args.n_sigma_events):
545 next_event(pythia)
546 sigma[beam_id, i_nucleon].append(
547 signal_kfactor(kfactors, beam_id, nucleon_id, p) * pythia.infoPython().sigmaGen()
548 )
549 sigma_total = [
550 mbias_pythia[i_nucleon].getSigmaTotal(beam_id, nucleon_id, ecm(beam_id, nucleon_id, p))
551 for p in p_grid
552 ]
553 log_chi[beam_id, i_nucleon] = np.log([s / t for s, t in zip(sigma[beam_id, i_nucleon], sigma_total)])
554 print(
555 f"K*sigma and chi at {args.p_beam} GeV for {beam_id} on {nucleon_id}: "
556 f"{sigma[beam_id, i_nucleon][-1]:.3e} mb, {math.exp(log_chi[beam_id, i_nucleon][-1]):.3e}"
557 )
558 z_target, a_target = TARGET_NUCLEUS[args.target_composition]
559 version = round(mbias_pythia[0].settings.parm("Pythia:versionNumber"), 3)
560 model = nucleus_model(version)
561 coll_pythia, decayer = [], None
562 if args.cascade_model == "nucleus":
563
564 coll_pythia = [
565 new_pythia(
566 ["SoftQCD:all = on", "HadronLevel:Decay = off", "Beams:frameType = 3"]
567 + ["Beams:allowVariableEnergy = on", "Beams:allowIDAswitch = on", f"Beams:idB = {nucleon_id}"]
568 + [f"Beams:pzA = {args.p_beam}", "Beams:pzB = 0."]
569 )
570 for nucleon_id in TARGET_NUCLEONS
571 ]
572 decayer = new_pythia(["ProcessLevel:all = off"])
573
574
575 for beam_id in {b for b, _ in log_chi}:
576 chi_nucleus = []
577 for i, p in enumerate(p_grid):
578 sigma_tot = mbias_pythia[0].getSigmaTotal(beam_id, 2212, ecm(beam_id, 2212, p))
579 sigma_n = proton_fraction * sigma[beam_id, 0][i] + (1.0 - proton_fraction) * sigma[beam_id, 1][i]
580 chi_nucleus.append(n_coll_avg(a_target, sigma_tot, model) * sigma_n / sigma_tot)
581 log_chi[beam_id, 0] = log_chi[beam_id, 1] = np.log(chi_nucleus)
582 chi_max = max(np.exp(c).max() for c in log_chi.values())
583
584 sigma_QQ = proton_fraction * sigma[2212, 0][-1] + (1.0 - proton_fraction) * sigma[2212, 1][-1]
585 print(f"K*sigma per nucleon at {args.p_beam} GeV: {1e3 * sigma_QQ:.2f} ub")
586
587 signal_events = SignalEvents(new_pythia, signal_process, signal_ids, args.p_beam)
588 inclusive_events = (
589 InclusiveSignalEvents(new_pythia, signal_ids, args.heavy_quark, args.p_beam)
590 if args.charm_production == "inclusive" and args.heavy_quark == 4
591 else None
592 )
593
594 output_file = ROOT.TFile.Open(args.output, "RECREATE")
595 ntuple = ROOT.TNtuple(
596 "pythia6",
597 "pythia8 heavy flavour",
598 "id:px:py:pz:E:M:mid:mpx:mpy:mpz:mE:mM:k:a0:a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:\
599s0:s1:s2:s3:s4:s5:s6:s7:s8:s9:s10:s11:s12:s13:s14:s15:n_hadrons",
600 )
601
602 depth_hist = ROOT.TH1F("2", "nr signal per cascade depth", 50, 0.5, 50.5)
603
604
605
606 sigma_parameter = ROOT.TParameter("double")("sigma_QQ", sigma_QQ)
607 sigma_parameter.SetMergeMode("f")
608 sigma_parameter.Write()
609
610 t0 = time.time()
611 for i_pot in range(args.n_pot):
612 if i_pot % 1000 == 0:
613 print("Generate event ", i_pot)
614
615 stack: list[tuple[int, float, float, float, int, list[int], list[int]]] = []
616 stack.append((2212, 0.0, 0.0, args.p_beam, 1, [2212] + 99 * [0], 100 * [0]))
617 while stack:
618 beam_id, px, py, pz, depth, ancestors, processes = stack.pop()
619 p = math.sqrt(px**2 + py**2 + pz**2)
620 i_nucleon = 0 if random.random() < proton_fraction else 1
621 nucleon_id = TARGET_NUCLEONS[i_nucleon]
622 if math.exp(np.interp(math.log(p), log_p_grid, log_chi[beam_id, i_nucleon])) / chi_max > random.random():
623 beam_mass = mass(beam_id)
624 beam = ROOT.TLorentzVector(px, py, pz, math.sqrt(p**2 + beam_mass**2))
625 if inclusive_events is not None:
626 code, hadrons = inclusive_events.next(beam_id, i_nucleon, px, py, pz)
627 else:
628 code, hadrons = signal_events.next(beam_id, nucleon_id, p)
629 boost = (beam + ROOT.TLorentzVector(0, 0, 0, mass(nucleon_id))).BoostVector()
630 n_processes = min(depth - 1, 15)
631 for hadron_id, *p4, hadron_mass in hadrons:
632 hadron = ROOT.TLorentzVector(*p4)
633 if inclusive_events is None:
634 hadron.RotateUz(beam.Vect().Unit())
635 hadron.Boost(boost)
636 row = [hadron_id, hadron.Px(), hadron.Py(), hadron.Pz(), hadron.E(), hadron_mass]
637 row += [beam_id, px, py, pz, beam.E(), beam_mass, depth]
638 row += ancestors[:16] + processes[:n_processes] + [code] + (15 - n_processes) * [0]
639
640 row += [len(hadrons)]
641 ntuple.Fill(array("f", row))
642 depth_hist.Fill(depth)
643 if args.cascade_model == "nucleus":
644
645 code, _, final = nuclear_collision(
646 coll_pythia, decayer, model, beam_id, px, py, pz, z_target, a_target, p_threshold
647 )
648 if code == 102:
649
650 leading = max(final, key=lambda f: f[4])
651 if leading[4] > p_threshold and len(stack) < 999:
652 stack.append((beam_id, *leading[1:4], depth, ancestors, processes))
653 continue
654 new_depth = min(depth + 1, 98)
655 if depth == 1:
656 processes = [code] + processes[1:]
657 for part_id, part_px, part_py, part_pz, part_p in final:
658 if abs(part_id) in CASCADE_IDS and part_p > p_threshold and len(stack) < 999:
659 new_ancestors = ancestors[: new_depth - 1] + [part_id] + ancestors[new_depth:]
660 new_processes = processes[: new_depth - 1] + [code] + processes[new_depth:]
661 stack.append((part_id, part_px, part_py, part_pz, new_depth, new_ancestors, new_processes))
662 continue
663
664 i_nucleon = 0 if random.random() < proton_fraction else 1
665 pythia = mbias_pythia[i_nucleon]
666 pythia.setBeamIDs(beam_id, TARGET_NUCLEONS[i_nucleon])
667 pythia.setKinematics(px, py, pz, 0.0, 0.0, 0.0)
668 next_event(pythia)
669 code = pythia.infoPython().code()
670 if code == 102:
671
672
673
674 final = [pythia.event[i] for i in range(pythia.event.size()) if pythia.event[i].isFinal()]
675 leading = max(final, key=lambda part: part.pAbs())
676 if leading.pAbs() > p_threshold and len(stack) < 999:
677 stack.append((beam_id, leading.px(), leading.py(), leading.pz(), depth, ancestors, processes))
678 continue
679 new_depth = min(depth + 1, 98)
680 if depth == 1:
681 processes = [code] + processes[1:]
682 for i in range(pythia.event.size()):
683 part = pythia.event[i]
684 if part.idAbs() in CASCADE_IDS and part.isFinal() and part.pAbs() > p_threshold and len(stack) < 999:
685 new_ancestors = ancestors[: new_depth - 1] + [part.id()] + ancestors[new_depth:]
686 new_processes = processes[: new_depth - 1] + [code] + processes[new_depth:]
687 stack.append((part.id(), part.px(), part.py(), part.pz(), new_depth, new_ancestors, new_processes))
688
689 print(f"Generated {args.n_pot} p.o.t. in {time.time() - t0} s with {new_pythia.n_instances} Pythia8 instances")
690 if inclusive_events is not None:
691 print(f"{inclusive_events.n_tried} inclusive events generated for the charm signal")
692 output_file.Write()
693 output_file.Close()
694 print(f"Output file is {args.output}")
695
696