9import scipy.interpolate
12def addHNLtoROOT(pid: int = 9900015, m: float = 1.0, g: float = 3.654203020370371e-21) ->
None:
13 pdg = ROOT.TDatabasePDG.Instance()
14 pdg.AddParticle(
"N2",
"HNL", m,
False, g, 0.0,
"N2", pid)
19 normalized_br = h[histoname](mass)
20 br = normalized_br * coupling
30 for histoname
in histograms:
31 item = histoname.split(
"_")
33 coupling = couplings[1]
38 print(list(sumbrs.values()))
39 maxsumbr = max(sumbrs.values())
45 for histoname
in histograms:
47 coupling = couplings[1]
55 Make the particles with a lifetime above the specified one stable, to allow
56 them to decay in Geant4 instead.
58 p8 = P8gen.getPythiaInstance()
61 n = p8.particleData.nextId(n)
62 p = p8.particleData.particleDataEntryPtr(n)
63 if p.tau0() > above_lifetime:
64 command = f
"{n}:mayDecay = false"
65 p8.readString(command)
66 print(f
"Pythia8 configuration: Made {p.name()} stable for Pythia, should decay in Geant4")
71 This function parses a file containing histograms of branching ratios.
73 It places them in a dictionary indexed by the decay string (e.g. 'd_K0_e'),
74 as a pair ([masses...], [branching ratios...]), where the mass is expressed
77 with open(filepath)
as f:
80 th1f_exp = re.compile(
r"^TH1F\|.+")
81 header_exp = re.compile(
r"^TH1F\|(.+?)\|B(?:R|F)/U2(.+?)\|.+? mass \(GeV\)\|?")
82 subheader_exp = re.compile(
r"^\s*?(\d+?),\s*(\d+?\.\d+?),\s*(\d+\.\d+)\s*$")
83 data_exp = re.compile(
r"^\s*(\d+?)\s*,\s*(-?\d+\.\d+)\s*$")
85 header_line_idx = [i
for i
in range(len(lines))
if th1f_exp.match(lines[i])
is not None]
88 for offset
in header_line_idx:
90 mh = header_exp.match(lines[offset])
91 if mh
is None or len(mh.groups()) != 2:
92 raise ValueError(f
"Malformed header encountered: {lines[offset]}")
93 decay_code = mh.group(1)
95 ms = subheader_exp.match(lines[offset + 1])
96 if ms
is None or len(ms.groups()) != 3:
97 raise ValueError(f
"Malformed sub-header encountered: {lines[offset + 1]}")
98 npoints = int(ms.group(1))
99 min_mass = float(ms.group(2))
100 max_mass = float(ms.group(3))
101 masses = np.linspace(min_mass, max_mass, npoints, endpoint=
False)
102 branching_ratios = np.zeros(npoints)
104 for line
in lines[offset + 2 : offset + npoints + 2]:
105 md = data_exp.match(line)
106 if md
is None or len(md.groups()) != 2:
107 raise ValueError(f
"Malformed data row encountered: {line}")
108 idx = int(md.group(1))
109 br = float(md.group(2))
110 branching_ratios[idx] = br
111 histograms[decay_code] = (masses, branching_ratios)
117 This function reads a file containing branching ratio histograms, and
118 returns a dictionary of interpolators of the branching ratios, indexed by
123 for hist_string, (masses, br)
in histogram_data.items():
124 histograms[hist_string] = scipy.interpolate.interp1d(
125 masses, br, kind=kind, bounds_error=
False, fill_value=0, assume_sorted=
True
130def get_br(histograms, channel, mass, couplings):
132 Utility function used to reliably query the branching ratio for a given
133 channel at a given mass, taking into account the correct coupling.
135 hist = histograms[channel[
"decay"]]
136 coupling = couplings[channel[
"coupling"]]
137 normalized_br = hist(mass)
138 return normalized_br * coupling
143 Adds the corresponding particles to PYTHIA.
145 `particles` must be a list containing either the particles PDG IDs, or
146 their PYTHIA names. The commands needed to add the particles are queried
149 If the particle is not self-conjugate, the antiparticle is automatically
152 for particle_id
in particles:
154 particle = next((p
for p
in data[
"particles"]
if particle_id
in [p[
"id"], p[
"name"]]),
None)
156 raise ValueError(f
"Could not find particle ID {particle_id} in particle database")
158 P8gen.SetParameters(particle[
"cmd"])
161def add_channel(P8gen, ch, histograms, mass, couplings, scale_factor) -> None:
162 "Add to PYTHIA a leptonic or semileptonic decay channel to HNL."
164 br =
get_br(histograms, ch, mass, couplings)
169 "{}:addChannel 1 {:.17} 22 {} 9900015 {}".format(
170 ch[
"id"], br * scale_factor, ch[
"idlepton"], ch[
"idhadron"]
175 "{}:addChannel 1 {:.17} 0 9900015 {}".format(
176 ch[
"id"], br * scale_factor, ch[
"idlepton"]
180 raise ValueError(f
"Missing key 'idlepton' in channel {ch}")
184 "Add to PYTHIA a tau decay channel to HNL."
186 br =
get_br(histograms, ch, mass, couplings)
191 "{}:addChannel 1 {:.16} 1531 9900015 {} {}".format(
192 ch[
"id"], br * scale_factor, ch[
"idlepton"], ch[
"idhadron"]
197 "{}:addChannel 1 {:.16} 1521 9900015 {}".format(
198 ch[
"id"], br * scale_factor, ch[
"idhadron"]
202 raise ValueError(f
"Missing key 'idhadron' in channel {ch}")
207 Add dummy channels for correct rejection sampling.
209 Even after rescaling the branching ratios, they do not sum up to unity
210 for most particles since we are ignoring SM processes.
212 This function adds a "filler" channel for each particle, in order to
213 preserve the ratios between different branching ratios.
216 for particle
in top_level_particles:
218 remainder = 1 - my_total_br / max_total_br
219 assert remainder > -epsilon
220 assert remainder < 1 + epsilon
221 if remainder > epsilon:
227 Add a dummy channel to PYTHIA, with branching ratio equal to `remainder.`
229 The purpose of this function is to compensate for the absence of SM
230 channels, which are ignored when investigating rare processes. A dummy
231 decay channel is instead added to each particle in order to maintain the
232 correct ratios between the branching ratios of each particle to rare
233 processes. This is usually combined with a global reweighting of the
236 In order to keep PYTHIA from complaining about charge conservation, a
237 suitable process is added which conserves electric charge.
239 All dummy channels can be identified by the presence of a photon among the
242 pdg = P8gen.getPythiaInstance().particleData
243 charge = pdg.charge(particle)
245 P8gen.SetParameters(f
"{particle}:addChannel 1 {remainder:.16} 0 22 -11")
247 P8gen.SetParameters(f
"{particle}:addChannel 1 {remainder:.16} 0 22 11")
249 P8gen.SetParameters(f
"{particle}:addChannel 1 {remainder:.16} 0 22 22")
254 This function computes the maximum total branching ratio for all decay chains.
256 In order to make the event generation as efficient as possible when
257 studying very rare processes, it is necessary to rescale the branching
258 ratios, while enforcing the invariant that any total branching ratio must
259 remain lower that unity.
261 This is accomplished by computing, for each particle, the total branching
262 ratio to processes of interest, and then dividing all branching ratios by
263 the highest of those.
265 Note: the decay chain length must be at most 2.
269 total_branching_ratios = [
compute_total_br(particle, decay_chains)
for particle
in top_level_particles]
271 return max(total_branching_ratios)
276 Returns the total branching ratio to HNLs for a given particle.
278 return sum(float(np.prod(branching_ratios))
for (top, branching_ratios)
in decay_chains
if top == particle)
283 Returns the set of particles which are at the top of a decay chain.
285 return {top
for (top, branching_ratios)
in decay_chains}
289 if max_total_br <= 0:
290 print(f
"No phase space for {particle} from {selection} at this mass: {mass}. Quitting.")
295 "Prints the scale factor used to make event generation more efficient."
296 print(f
"One simulated event per {scaling_factor:.4g} meson decays")
None add_tau_channel(P8gen, ch, histograms, mass, couplings, scale_factor)
None addHNLtoROOT(int pid=9900015, float m=1.0, float g=3.654203020370371e-21)
float gettotalbrrpvsusy(h, histograms, mass, couplings)
None add_channel(P8gen, ch, histograms, mass, couplings, scale_factor)
getmaxsumbrrpvsusy(h, histograms, mass, couplings)
get_br(histograms, channel, mass, couplings)
make_interpolators(filepath, str kind="linear")
get_top_level_particles(decay_chains)
float compute_total_br(particle, decay_chains)
None make_particles_stable(P8gen, above_lifetime)
getbr_rpvsusy(h, histoname, mass, coupling)
parse_histograms(filepath)
None add_dummy_channel(P8gen, particle, remainder)
None add_particles(P8gen, particles, data)
None print_scale_factor(scaling_factor)
None fill_missing_channels(P8gen, max_total_br, decay_chains, float epsilon=1e-6)
None exit_if_zero_br(max_total_br, selection, mass, str particle="HNL")
float compute_max_total_br(decay_chains)