FairShip
Loading...
Searching...
No Matches
makeDecay.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
4# Use Pythia8 to decay the signals (Charm/Beauty) as produced by makeCascade.
5# Output is an ntuple with muon/neutrinos
6import argparse
7import os
8
9import ROOT
10import rootUtils as ut
11from heavyFlavourScaling import (
12 check_run_type_override,
13 derive_cross_sections,
14 format_summary,
15)
16
17ROOT.gROOT.LoadMacro("$VMCWORKDIR/gconfig/basiclibs.C")
18ROOT.basiclibs()
19
20ap = argparse.ArgumentParser(description="Decay charm/beauty signals from makeCascade output with Pythia8")
21ap.add_argument(
22 "-f",
23 "--input",
24 default="/eos/experiment/ship/data/Charm/Cascade-parp16-MSTP82-1-MSEL4-76Mpot_1",
25 help="input file with charm/beauty hadrons (.root suffix optional)",
26)
27ap.add_argument("-p", "--pot", type=float, default=5.0e13, help="number of protons on target per spill to normalise on")
28# A run is either charm or beauty, so only one ratio override is ever meaningful.
29cross_section = ap.add_mutually_exclusive_group()
30cross_section.add_argument(
31 "-c", "--chicc", type=float, default=None, help="ccbar over mbias cross section (overrides target-derived value)"
32)
33cross_section.add_argument(
34 "--chibb", type=float, default=None, help="bbbar over mbias cross section (overrides target-derived value)"
35)
36ap.add_argument(
37 "--target-composition",
38 default="W",
39 choices=["W", "Mo"],
40 help="Target composition. Default is Tungsten (W); Molybdenum (Mo) is the other preset.",
41)
42ap.add_argument(
43 "-A",
44 type=float,
45 default=None,
46 help=(
47 "Target mass number; overrides --target-composition preset. "
48 "Used to scale chicc/chibb as (A/A_Mo)^(heavyflavour_Ascale-mbias_Ascale) "
49 "(default exponent: 0.29)."
50 ),
51)
52args = ap.parse_args()
53
54fname = args.input.replace(".root", "")
55nrpotspill = args.pot
56
57cs = derive_cross_sections(args.target_composition, args.A, args.chicc, args.chibb)
58chicc, chibb = cs.chicc, cs.chibb
59
60print(format_summary(cs, None if args.A is not None else args.target_composition))
61
62FIN = fname + ".root"
63tmp = os.path.abspath(FIN).split("/")
64FOUT = "Decay-" + tmp[len(tmp) - 1]
65fin = ROOT.TFile.Open(FIN)
66sTree = fin.FindObjectAny("pythia6")
67nEvents = sTree.GetEntries()
68
69# Calculate weights, for the whole file.
70# get histogram with number of pot to normalise
71hc = {}
72if fin.GetKey("2"):
73 hc["2"] = fin.Get("2")
74else:
75 fhin = ROOT.TFile.Open(FIN.replace("ntuple", "hists"))
76 hc["2"] = fhin.Get("2")
77
78# pot are counted double, i.e. for each signal, i.e. pot/2.
79nrcpot = hc["2"].GetBinContent(1) / 2.0
80print("Input file: ", FIN, " with ", nEvents, " entries, corresponding to nr-pot=", nrcpot)
81# nEvents=100
82print("Output ntuples written to: ", FOUT)
83
84P8gen = ROOT.TPythia8()
85P8 = P8gen.Pythia8()
86P8.readString("ProcessLevel:all = off")
87
88# let strange particle decay in Geant4
89n = 1
90while n != 0:
91 n = P8.particleData.nextId(n)
92 p = P8.particleData.particleDataEntryPtr(n)
93 if p.tau0() > 1:
94 command = str(n) + ":mayDecay = false"
95 P8.readString(command)
96 print("Pythia8 configuration: Made %s stable for Pythia, should decay in Geant4" % p.name())
97P8.init()
98
99
100# output ntuple:
101ftup = ROOT.TFile.Open(FOUT, "RECREATE")
102Ntup = ROOT.TNtuple("Decay", "pythia8 heavy flavour decays", "id:px:py:pz:E:M:weight:mid:mpx:mpy:mpz:mE:pot:ptGM:pzGM")
103
104h = {}
105# book hists for Genie neutrino momentum distrubition, just as check
106# type of neutrino
107PDG = ROOT.TDatabasePDG.Instance()
108for idnu in range(12, 18, 2):
109 # nu or anti-nu
110 for idadd in range(-1, 3, 2):
111 idhnu = 1000 + idnu
112 idw = idnu
113 if idadd == -1:
114 idhnu += 1000
115 idw = -idnu
116 _p = ut.get_particle(idw)
117 name = _p.GetName()
118 ut.bookHist(h, str(idhnu), name + " momentum (GeV)", 400, 0.0, 400.0)
119 ut.bookHist(h, str(idhnu + 100), name + " log10-p vs log10-pt", 100, -0.3, 1.7, 100, -2.0, 0.5)
120 ut.bookHist(h, str(idhnu + 200), name + " log10-p vs log10-pt", 25, -0.3, 1.7, 100, -2.0, 0.5)
121
122pot = 0.0
123# Determine fDs on this file for primaries
124nDsprim = 0
125ntotprim = 0
126wspill = 0.0 # populated on the n == 0 event below
127
128for n in range(nEvents):
129 rc = sTree.GetEvent(n)
130 # check if we deal with charm or beauty:
131 if n == 0:
132 is_beauty = sTree.M > 5
133 check_run_type_override(is_beauty, args.chicc, args.chibb)
134 if is_beauty:
135 chicc = chibb
136 print("automatic detection of beauty, configured for beauty")
137 print("bb cross section / mbias ", chicc)
138 else:
139 print("cc cross section / mbias ", chicc)
140 # convert pot to weight corresponding to one spill of 5e13 pot
141 print("weights: ", nrpotspill, " p.o.t. per spill")
142 print(" ")
143 wspill = nrpotspill * chicc / nrcpot
144 # sanity check, count number of p.o.t. on input file.
145 pt = ROOT.TMath.Sqrt(sTree.mpx**2 + sTree.mpy**2)
146 # every event appears twice, i.e.
147 if pt < 1.0e-5 and int(sTree.mid) == 2212:
148 pot = pot + 0.5
149 ntotprim += 1
150 idabs = int(abs(sTree.id))
151 if idabs == 431:
152 nDsprim += 1
153 P8.event.reset()
154 P8.event.append(int(sTree.id), 1, 0, 0, sTree.px, sTree.py, sTree.pz, sTree.E, sTree.M, 0.0, 9.0)
155 P8.next()
156 # P8.event.list()
157 for n in range(len(P8.event)):
158 # ask for stable particles
159 if P8.event[n].isFinal():
160 # select neutrinos and mu
161 idabs = int(abs(P8.event[n].id()))
162 if idabs > 11 and idabs < 17:
163 par = P8.event[n]
164 ptGM = ROOT.TMath.Sqrt(sTree.mpx * sTree.mpx + sTree.mpy * sTree.mpy)
165 Ntup.Fill(
166 par.id(),
167 par.px(),
168 par.py(),
169 par.pz(),
170 par.e(),
171 par.m(),
172 wspill,
173 sTree.id,
174 sTree.px,
175 sTree.py,
176 sTree.pz,
177 sTree.E,
178 nrpotspill,
179 ptGM,
180 sTree.mpz,
181 )
182 # count total muons from charm/spill, and within some angular range..
183 if idabs == 16 or idabs == 14 or idabs == 12:
184 idhnu = idabs + 1000
185 if par.id() < 0:
186 idhnu += 1000
187 pt2 = par.px() ** 2 + par.py() ** 2
188 ptot = ROOT.TMath.Sqrt(pt2 + par.pz() ** 2)
189 l10ptot = min(max(ROOT.TMath.Log10(ptot), -0.3), 1.69999)
190 l10pt = min(max(ROOT.TMath.Log10(ROOT.TMath.Sqrt(pt2)), -2.0), 0.4999)
191 h[str(idhnu)].Fill(ptot, wspill)
192 h[str(idhnu + 100)].Fill(l10ptot, l10pt, wspill)
193 h[str(idhnu + 200)].Fill(l10ptot, l10pt, wspill)
194
195print("Now at Ntup.Write() for pot=", pot, nrcpot)
196if (1.0 - pot / nrcpot) < 1.0e-2:
197 print("write ntuple, weight/event=", nrpotspill, "x", chicc, "/", nrcpot, "=", wspill)
198 Ntup.Write()
199 for akey in h:
200 h[akey].Write()
201 ftup.Close()
202 print("Neutrino statistics/spill of 5.e15 pot:")
203 for idnu in range(12, 18, 2):
204 # nu or anti-nu
205 for idadd in range(-1, 3, 2):
206 idhnu = 1000 + idnu
207 idw = idnu
208 if idadd == -1:
209 idhnu += 1000
210 idw = -idnu
211 print(idhnu, h[str(idhnu)].GetTitle(), ("%8.3E " % (h[str(idhnu)].Integral())))
212
213 fDsP6 = 1.0 * nDsprim / ntotprim
214 fDs = 0.077 # Used in TP..
215 print(" ")
216 print("fDs of primary proton interactions in P6=", fDsP6, " should be ", fDs)
217
218else:
219 print("*********** WARNING ***********")
220 print("number of POT does not agree between ntuple and hists, i.e.:", pot, "<>", nrcpot)
221 print("mu/neutrino ntuple has NOT been written")
222#