231 """Hadron-nucleus collision as a sequence of hadron-nucleon subcollisions, as in PythiaCascade::nextColl.
233 coll_pythia are minimum-bias instances on proton and neutron with hadron decays off, so that a leading
234 resonance collides again itself; the decays are done by decayer once the collision is complete.
236 Returns the process code of the first non-elastic subcollision (102 if all were elastic), the
237 number of subcollisions, and the final particles (id, px, py, pz, p) of all subcollisions, without
238 the hadrons that collided again.
239 Subcollisions stop once the leading hadron is below p_min, which no longer adds particles above it.
241 p = math.sqrt(px**2 + py**2 + pz**2)
242 sigma_hn = coll_pythia[0].getSigmaTotal(beam_id, 2212,
ecm(beam_id, 2212, p))
243 prob_more = 1.0 - 1.0 /
n_coll_avg(a, sigma_hn, model)
244 direction = (px / p, py / p, pz / p)
246 final, latest = [], []
247 projectile = (beam_id, px, py, pz)
248 first_code, n_coll = 102, 0
249 for i_coll
in range(1, a + 1):
252 if random.random() > prob_more:
254 hadrons = [f
for f
in latest
if f[5]]
257 lead = max(hadrons, key=
lambda f: f[1] * direction[0] + f[2] * direction[1] + f[3] * direction[2])
261 projectile = lead[:4]
262 if ecm(lead[0], 2212, lead[4]) > model.sd_min_ecm:
263 proc_type = 4
if random.random() < model.prob_sd
else model.other_process
264 on_proton = random.random() < n_p / (n_p + n_n)
269 pythia = coll_pythia[0
if on_proton
else 1]
270 pythia.setBeamIDs(projectile[0], TARGET_NUCLEONS[0
if on_proton
else 1])
271 pythia.setKinematics(*projectile[1:], 0.0, 0.0, 0.0)
273 if pythia.next(proc_type)
if proc_type
else pythia.next():
276 raise RuntimeError(f
"Pythia8 failed to generate a subcollision for {projectile[0]}")
277 if first_code == 102:
278 first_code = pythia.infoPython().code()
282 (event[i].id(), event[i].px(), event[i].py(), event[i].pz(), event[i].pAbs(), event[i].isHadron())
283 + (event[i].e(), event[i].
m())
284 for i
in range(event.size())
285 if event[i].isFinal()
289 decayer.event.reset()
290 for part_id, part_px, part_py, part_pz, _, _, part_e, part_m
in final:
291 decayer.event.append(part_id, 1, 0, 0, part_px, part_py, part_pz, part_e, part_m)
292 if not decayer.moreDecays():
293 raise RuntimeError(
"Pythia8 failed to decay the final particles of a hadron-nucleus collision")
294 event = decayer.event
296 (event[i].id(), event[i].px(), event[i].py(), event[i].pz(), event[i].pAbs())
297 for i
in range(event.size())
298 if event[i].isFinal()
300 return first_code, n_coll, final
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":
513 tune_settings, kfactors = [], (1.0, 1.0)
515 random.seed(args.seed)
516 new_pythia =
PythiaFactory(args.seed, tune_settings, [*CASCADE_IDS, *signal_ids])
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."]
524 for nucleon_id
in TARGET_NUCLEONS
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):
537 for i_nucleon, nucleon_id
in enumerate(TARGET_NUCLEONS):
538 sigma[beam_id, i_nucleon] = []
541 [signal_process,
"PartonLevel:all = off",
"HadronLevel:all = off"]
544 for _
in range(args.n_sigma_events):
546 sigma[beam_id, i_nucleon].append(
547 signal_kfactor(kfactors, beam_id, nucleon_id, p) * pythia.infoPython().sigmaGen()
550 mbias_pythia[i_nucleon].getSigmaTotal(beam_id, nucleon_id,
ecm(beam_id, nucleon_id, p))
553 log_chi[beam_id, i_nucleon] = np.log([s / t
for s, t
in zip(sigma[beam_id, i_nucleon], sigma_total)])
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}"
558 z_target, a_target = TARGET_NUCLEUS[args.target_composition]
559 version = round(mbias_pythia[0].settings.parm(
"Pythia:versionNumber"), 3)
561 coll_pythia, decayer = [],
None
562 if args.cascade_model ==
"nucleus":
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."]
570 for nucleon_id
in TARGET_NUCLEONS
572 decayer = new_pythia([
"ProcessLevel:all = off"])
575 for beam_id
in {b
for b, _
in log_chi}:
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())
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")
587 signal_events =
SignalEvents(new_pythia, signal_process, signal_ids, args.p_beam)
590 if args.charm_production ==
"inclusive" and args.heavy_quark == 4
594 output_file = ROOT.TFile.Open(args.output,
"RECREATE")
595 ntuple = ROOT.TNtuple(
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",
602 depth_hist = ROOT.TH1F(
"2",
"nr signal per cascade depth", 50, 0.5, 50.5)
606 sigma_parameter = ROOT.TParameter(
"double")(
"sigma_QQ", sigma_QQ)
607 sigma_parameter.SetMergeMode(
"f")
608 sigma_parameter.Write()
611 for i_pot
in range(args.n_pot):
612 if i_pot % 1000 == 0:
613 print(
"Generate event ", i_pot)
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]))
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)
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())
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]
640 row += [len(hadrons)]
641 ntuple.Fill(array(
"f", row))
642 depth_hist.Fill(depth)
643 if args.cascade_model ==
"nucleus":
646 coll_pythia, decayer, model, beam_id, px, py, pz, z_target, a_target, p_threshold
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))
654 new_depth = min(depth + 1, 98)
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))
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)
669 code = pythia.infoPython().code()
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))
679 new_depth = min(depth + 1, 98)
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))
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")
694 print(f
"Output file is {args.output}")