FairShip
Loading...
Searching...
No Matches
pythia8_conf.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4import contextlib
5import os
6
7import hnl
8import readDecayTable
9import ROOT
10import rpvsusy
11import shipunit as u
12import yaml
13from method_logger import MethodLogger
14from pythia8_conf_utils import (
15 add_channel,
16 add_particles,
17 add_tau_channel,
18 addHNLtoROOT,
19 compute_max_total_br,
20 exit_if_zero_br,
21 fill_missing_channels,
22 get_br,
23 getbr_rpvsusy,
24 getmaxsumbrrpvsusy,
25 gettotalbrrpvsusy,
26 make_interpolators,
27 make_particles_stable,
28 print_scale_factor,
29)
30
31
33 P8gen, mass, couplings, sfermionmass, benchmark, inclusive, deepCopy: bool = False, debug: bool = True
34) -> None:
35 # configure pythia8 for Ship usage
36 _exit_stack = contextlib.ExitStack()
37 if debug:
38 pythia_log = _exit_stack.enter_context(open("pythia8_conf.txt", "w")) # noqa: SIM115
39 P8gen = MethodLogger(P8gen, sink=pythia_log)
40 h = make_interpolators(os.path.expandvars(f"$FAIRSHIP/shipgen/branchingratiosrpvsusybench{benchmark}.dat"))
41 P8gen.UseRandom3()
42 P8gen.SetMom(400) # beam momentum in GeV
43 if deepCopy:
44 P8gen.UseDeepCopy()
45 ROOT.TDatabasePDG.Instance()
46 # let strange particle decay in Geant4
47 make_particles_stable(P8gen, above_lifetime=1)
48
49 if inclusive is True: # For backward compatibility with a boolean argument
50 inclusive = "True"
51
52 if inclusive == "True":
54
55 # generate RPV neutralino from inclusive charm hadrons
56 if inclusive == "c":
57 P8gen.SetParameters("HardQCD::hardccbar = on")
58 # add RPVSUSY
59 rpvsusy_instance = rpvsusy.RPVSUSY(mass, couplings, sfermionmass, benchmark, debug=True)
60 ctau = rpvsusy_instance.computeNLifetime(system="FairShip") * u.c_light * u.cm
61 print("RPVSUSY ctau ", ctau)
62 P8gen.SetParameters(f"9900015:new = N2 N2 2 0 0 {mass:.12} 0.0 0.0 0.0 {ctau / u.mm:.12} 0 1 0 1 0")
63 P8gen.SetParameters("9900015:isResonance = false")
64 P8gen.SetParameters("Next:numberCount = 0")
65 # Configuring decay modes...
66 rpvsusy_instance.AddChannelsToPythia(P8gen)
67
68 # Finish HNL setup...
69 P8gen.SetParameters("9900015:mayDecay = on")
70 P8gen.SetHNLId(9900015)
71 # also add to PDG
72 gamma = u.hbarc / float(ctau) # 197.3269631e-16 / float(ctau) # hbar*c = 197 MeV*fm = 197e-16 GeV*cm
73 addHNLtoROOT(pid=9900015, m=mass, g=gamma)
74 # 12 14 16 neutrinos replace with N2
75 charmhistograms = ["d_mu", "ds_mu"]
76 # no tau decay here to consider
77 maxsumBR = getmaxsumbrrpvsusy(h, charmhistograms, mass, couplings)
78 exit_if_zero_br(maxsumBR, inclusive, mass, particle="RPV neutralino")
79 gettotalbrrpvsusy(h, charmhistograms, mass, couplings)
80
81 # overwrite D_s+ decays
82 P8gen.SetParameters(
83 "431:new D_s+ D_s- 1 3 0 1.96849"
84 " 0.00000 0.00000 0.00000 1.49900e-01 0 1 0 1 0"
85 )
86 sumBR = 0.0
87 br_ds_mu = getbr_rpvsusy(h, "ds_mu", mass, couplings[1])
88 if br_ds_mu > 0.0:
89 P8gen.SetParameters(f"431:addChannel 1 {br_ds_mu / maxsumBR:.12} 0 -13 9900015")
90 sumBR += float(br_ds_mu / maxsumBR)
91 if sumBR < 1.0 and sumBR > 0.0:
92 P8gen.SetParameters(f"431:addChannel 1 {1.0 - sumBR:.12} 0 22 -11")
93
94 # overwrite D+ decays
95 P8gen.SetParameters(
96 "411:new D+ D- 1 3 0 1.86962 0.00000 0.00000 0.00000 3.11800e-01 0 1 0 1 0"
97 )
98 sumBR = 0.0
99 br_d_mu = getbr_rpvsusy(h, "d_mu", mass, couplings[1])
100 if br_d_mu > 0.0:
101 P8gen.SetParameters(f"411:addChannel 1 {br_d_mu / maxsumBR:.12} 0 -13 9900015")
102 sumBR += float(br_d_mu / maxsumBR)
103 if sumBR < 1.0 and sumBR > 0.0:
104 P8gen.SetParameters(f"411:addChannel 1 {1.0 - sumBR:.12} 0 22 -11")
105
106 P8gen.List(9900015)
107
108 if inclusive == "b":
109 P8gen.SetParameters("HardQCD::hardbbbar = on")
110 # add RPVSUSY
111 rpvsusy_instance = rpvsusy.RPVSUSY(mass, couplings, sfermionmass, benchmark, debug=True)
112 ctau = rpvsusy_instance.computeNLifetime(system="FairShip") * u.c_light * u.cm
113 P8gen.SetParameters(f"9900015:new = N2 N2 2 0 0 {mass:.12} 0.0 0.0 0.0 {ctau / u.mm:.12} 0 1 0 1 0")
114 P8gen.SetParameters("9900015:isResonance = false")
115 # Configuring decay modes...
116 rpvsusy_instance.AddChannelsToPythia(P8gen)
117 # Finish HNL setup...
118 P8gen.SetParameters("9900015:mayDecay = on")
119 P8gen.SetHNLId(9900015)
120 # also add to PDG
121 gamma = u.hbarc / float(ctau) # 197.3269631e-16 / float(ctau) # hbar*c = 197 MeV*fm = 197e-16 GeV*cm
122 addHNLtoROOT(pid=9900015, m=mass, g=gamma)
123 # 12 14 16 neutrinos replace with N2
124 beautyhistograms = ["b_mu", "b_tau", "b0_nu_mu", "b0_nu_tau"]
125 maxsumBR = getmaxsumbrrpvsusy(h, beautyhistograms, mass, couplings)
126 exit_if_zero_br(maxsumBR, inclusive, mass, particle="RPV neutralino")
127 gettotalbrrpvsusy(h, beautyhistograms, mass, couplings)
128
129 # overwrite B+ decays
130 P8gen.SetParameters(
131 "521:new B+ B- 1 3 0 5.27925"
132 " 0.00000 0.00000 0.00000 4.91100e-01 0 1 0 1 0"
133 )
134 sumBR = 0.0
135 br_b_tau = getbr_rpvsusy(h, "b_tau", mass, couplings[1])
136 if br_b_tau > 0.0:
137 P8gen.SetParameters(f"521:addChannel 1 {br_b_tau / maxsumBR:.12} 0 9900015 -15")
138 sumBR += float(br_b_tau / maxsumBR)
139 if sumBR < 1.0 and sumBR > 0.0:
140 P8gen.SetParameters(f"521:addChannel 1 {1.0 - sumBR:.12} 0 22 22")
141
142 # overwrite B0 decays
143 P8gen.SetParameters(
144 "511:new B0 Bbar0 1 0 0 5.27958"
145 " 0.00000 0.00000 0.00000 4.58700e-01 0 1 0 1 0"
146 )
147 sumBR = 0.0
148 br_b0_nu_tau = getbr_rpvsusy(h, "b0_nu_tau", mass, couplings[1])
149 if br_b0_nu_tau > 0.0:
150 P8gen.SetParameters(f"511:addChannel 1 {br_b0_nu_tau / maxsumBR:.12} 22 9900015 16")
151 sumBR += float(br_b0_nu_tau / maxsumBR)
152 if sumBR < 1.0 and sumBR > 0.0:
153 P8gen.SetParameters(f"511:addChannel 1 {1.0 - sumBR:.12} 0 22 22")
154
155 P8gen.List(9900015)
156
157 _exit_stack.close()
158
159
161 P8gen, mass, production_couplings, decay_couplings, process_selection, deepCopy: bool = False, debug: bool = True
162) -> None:
163 """
164 This function configures a HNLPythia8Generator instance for SHiP usage.
165 """
166
167 if process_selection is True: # For backward compatibility
168 process_selection = "inclusive"
169
170 # Wrap the Pythia8 object into a class logging all of its method calls
171 _exit_stack = contextlib.ExitStack()
172 if debug:
173 pythia_log = _exit_stack.enter_context(open("pythia8_conf.txt", "w")) # noqa: SIM115
174 P8gen = MethodLogger(P8gen, sink=pythia_log)
175
176 fairship_root = os.environ["FAIRSHIP"]
177 histograms = make_interpolators(fairship_root + "/shipgen/branchingratios.dat")
178 P8gen.UseRandom3() # TRandom1 or TRandom3 ?
179 P8gen.SetMom(400) # beam momentum in GeV
180 if deepCopy:
181 P8gen.UseDeepCopy()
182 ROOT.TDatabasePDG.Instance()
183 P8gen.SetParameters("Next:numberCount = 0")
184 # let strange particle decay in Geant4
185 make_particles_stable(P8gen, above_lifetime=1)
186
187 # Load particle & decay data
188 # ==========================
189
190 datafile = fairship_root + "/python/hnl_production.yaml"
191 with open(datafile) as f:
192 data = yaml.load(f, Loader=yaml.FullLoader)
193 all_channels = data["channels"]
194
195 # Inclusive
196 # =========
197
198 if process_selection == "inclusive":
200
201 # Charm decays only (with secondary production from tau)
202 # ======================================================
203
204 if process_selection == "c":
205 selection = data["selections"]["c"]
206 for cmd in selection["parameters"]:
207 P8gen.SetParameters(cmd)
208 add_hnl(P8gen, mass, decay_couplings)
209
210 # Add new charmed particles
211 # -------------------------
212
213 # Select all charmed particles (+ tau lepton)
214 c_particles = selection["particles"]
215 tau_id = 15 # tau- Monte-Carlo ID
216 add_particles(P8gen, c_particles + [tau_id], data)
217
218 # Add HNL production channels from charmed particles
219 # --------------------------------------------------
220
221 # Find charm and tau decays to HNLs
222 c_channels = [ch for ch in all_channels if ch["id"] in c_particles]
223 tau_channels = [ch for ch in all_channels if ch["id"] == tau_id]
224 # Standard model process: tau+ production from D_s+ decay
225 ds_id = 431 # D_s+ Monte-Carlo ID
226 ds_tau_br = 0.0548 # SM branching ratio Br(D_s+ -> tau+ nu_tau) (source: PDG 2018)
227
228 # Compute the branching ratio scaling factor, taking into account
229 # secondary production from tau
230 # Decay chains are encoded as follows:
231 # [(top level id A, [br A -> B, br B -> C, ...]), ...]
232
233 # Most charm particles directly decay to HNLs
234 primary_decays = [(ch["id"], [get_br(histograms, ch, mass, production_couplings)]) for ch in c_channels]
235 # The D_s+ can indirectly produce a HNL by first producing a tau+
236 secondary_decays = [
237 (ds_id, [ds_tau_br, get_br(histograms, ch, mass, production_couplings)]) for ch in tau_channels
238 ]
239 all_decays = primary_decays + secondary_decays
240
241 # Compute maximum total branching ratio (to rescale all BRs)
242 max_total_br = compute_max_total_br(all_decays)
243 exit_if_zero_br(max_total_br, process_selection, mass)
244 print_scale_factor(1 / max_total_br)
245
246 # Add charm decays
247 for ch in c_channels:
248 add_channel(P8gen, ch, histograms, mass, production_couplings, 1 / max_total_br)
249 # Add tau production from D_s+
250 # We can freely rescale Br(Ds -> tau) and Br(tau -> N X...) as long as
251 # Br(Ds -> tau -> N X...) remains the same.
252 # Here, we set Br(tau -> N) to unity to make event generation more efficient.
253 # The implicit assumption here is that we will disregard the tau during the analysis.
254 total_tau_br = sum(branching_ratios[1] for (_, branching_ratios) in secondary_decays)
255 assert ds_tau_br * total_tau_br <= max_total_br + 1e-12
256 P8gen.SetParameters(
257 f"431:addChannel 1 {ds_tau_br * total_tau_br / max_total_br:.12} 0 -15 16"
258 )
259 # Add secondary HNL production from tau
260 for ch in tau_channels:
261 # Rescale branching ratios only if some are non-zero. Otherwise leave them at zero.
262 add_tau_channel(P8gen, ch, histograms, mass, production_couplings, 1 / (total_tau_br or 1))
263
264 # Add dummy channels in place of SM processes
265 fill_missing_channels(P8gen, max_total_br, all_decays)
266
267 # List channels to confirm that Pythia has been properly set up
268 P8gen.List(9900015)
269
270 # B/Bc decays only
271 # ================
272
273 if process_selection in ["b", "bc"]:
274 selection = data["selections"][process_selection]
275 for cmd in selection["parameters"]:
276 P8gen.SetParameters(cmd)
277 add_hnl(P8gen, mass, decay_couplings)
278
279 # Add particles
280 particles = selection["particles"]
281 add_particles(P8gen, particles, data)
282
283 # Find all decay channels
284 channels = [ch for ch in all_channels if ch["id"] in particles]
285 decays = [(ch["id"], [get_br(histograms, ch, mass, production_couplings)]) for ch in channels]
286
287 # Compute scaling factor
288 max_total_br = compute_max_total_br(decays)
289 exit_if_zero_br(max_total_br, process_selection, mass)
290 print_scale_factor(1 / max_total_br)
291
292 # Add beauty decays
293 for ch in channels:
294 add_channel(P8gen, ch, histograms, mass, production_couplings, 1 / max_total_br)
295
296 # Add dummy channels in place of SM processes
297 fill_missing_channels(P8gen, max_total_br, decays)
298
299 P8gen.List(9900015)
300
301 _exit_stack.close()
302
303
304def add_hnl(P8gen, mass, decay_couplings) -> None:
305 "Adds the HNL to Pythia and ROOT"
306 hnl_instance = hnl.HNL(mass, decay_couplings, debug=True)
307 ctau = hnl_instance.computeNLifetime(system="FairShip") * u.c_light * u.cm
308 print(f"HNL ctau {ctau}")
309 P8gen.SetParameters(f"9900015:new = N2 N2 2 0 0 {mass:.12} 0.0 0.0 0.0 {ctau / u.mm:.12} 0 1 0 1 0")
310 P8gen.SetParameters("9900015:isResonance = false")
311 # Configuring decay modes...
313 P8gen, hnl_instance, conffile=os.path.expandvars("$FAIRSHIP/python/DecaySelection.conf"), verbose=False
314 )
315 # Finish HNL setup...
316 P8gen.SetParameters("9900015:mayDecay = on")
317 P8gen.SetHNLId(9900015)
318 # also add to PDG
319 gamma = u.hbarc / float(ctau) # 197.3269631e-16 / float(ctau) # hbar*c = 197 MeV*fm = 197e-16 GeV*cm
320 addHNLtoROOT(pid=9900015, m=mass, g=gamma)
321
322
323def setup_pythia_inclusive(P8gen) -> None:
324 P8gen.SetParameters("SoftQCD:inelastic = on")
325 P8gen.SetParameters("PhotonCollision:gmgm2mumu = on")
326 P8gen.SetParameters("PromptPhoton:all = on")
327 P8gen.SetParameters("WeakBosonExchange:all = on")
None add_hnl(P8gen, mass, decay_couplings)
None setup_pythia_inclusive(P8gen)
None configurerpvsusy(P8gen, mass, couplings, sfermionmass, benchmark, inclusive, bool deepCopy=False, bool debug=True)
None configure(P8gen, mass, production_couplings, decay_couplings, process_selection, bool deepCopy=False, bool debug=True)
addHNLdecayChannels(P8Gen, hnl, conffile=os.path.expandvars("$FAIRSHIP/python/DecaySelection.conf"), verbose=True)