10from array
import array
16ROOT.gROOT.LoadMacro(
"$VMCWORKDIR/gconfig/basiclibs.C")
18timer = ROOT.TStopwatch()
21ap = argparse.ArgumentParser(description=
"Run SHiP makeCascade: generate ccbar or bbar")
26 default=
int(time.time() * 100000000 % 900000000),
27 help=
"Random number seed, integer. If not given, current time will be used",
33 help=
"Name of ntuple output file, default: Cascade{nevgen/1000}k-parp16-MSTP82-1-MSEL{mselcb}-ntuple.root",
35ap.add_argument(
"-n",
"--nevgen", type=int, default=100000, help=
"Number of events to produce, default 100000")
36ap.add_argument(
"-E",
"--pbeamh", type=float, default=400.0, help=
"Energy of beam in GeV, default 400 GeV")
38 "-m",
"--mselcb", type=int, default=4, help=
"4 (5): charm (beauty) production, default charm", choices=[4, 5]
43 action=argparse.BooleanOptionalAction,
45 help=
"If this argument is used, store all particles produced together with charm.",
48 "--target-composition",
50 help=
"Target composition (to determine the ratio of protons in the material). Default is Tungsten (W). Only other choice is Molybdenum (Mo)",
56 help=
"Choices of Pythia tune are PoorE791 (default, settings with default Pythia6 pdf, based on getting <pt> at 500 GeV pi-) or LHCb (settings by LHCb for Pythia 6.427)",
57 choices=[
"PoorE791",
"LHCb"],
60ap.add_argument(
"--nev", type=int, default=5000, help=
"Events / momentum")
61ap.add_argument(
"--nrpoints", type=int, default=20, help=
"Number of momentum points taken to calculate sig/sigtot")
65 args.Fntuple = f
"Cascade{int(args.nevgen / 1000)}k-parp16-MSTP82-1-MSEL{args.mselcb}-ntuple.root"
67print(f
"Generate {args.nevgen} p.o.t. with msel = {args.mselcb} proton beam {args.pbeamh}GeV")
68print(f
"Output ntuples written to: {args.Fntuple}")
71idbeam = [2212, 211, 2112, 321, 130, 310]
73print(f
"Chi generation with {args.nev} events/point, nr points={args.nrpoints}")
74print(f
"Cascade beam particle: {idbeam}")
77if args.target_composition ==
"W":
82print(f
"Target particles: {target}, fraction of protons in {args.target_composition}={fracp}")
88 idsig = [411, 421, 431, 4122, 4132, 4232, 4332, 4412, 4414, 4422, 4424, 4432, 4434, 4444]
122 print(f
"Error: msel is unknown: {args.mselcb}, STOP program")
123 sys.exit(
"ERROR on input, exit")
125PDG = ROOT.TDatabasePDG.Instance()
126myPythia = ROOT.TPythia6()
127tp = ROOT.tPythia6Generator()
130for _pdg_code, _name
in [(2212,
"p+"), (-2212,
"pbar-"), (2112,
"n0"), (-2112,
"nbar0"), (130,
"KL0"), (310,
"KS0")]:
131 _p = ut.get_particle(_pdg_code)
134myPythia.SetPARP(2, 2.0)
141 print(
"********** PoorE791_tune **********")
144 print(f
"PARP(91)={P6.GetPARP(91)}")
146 print(
"MSTP PDF info, i.e. (51) and (52)=", P6.GetMSTP(51), P6.GetMSTP(52))
149 P6.SetPARP(89, 1000.0)
151 "And corresponding multiple parton stuff, i.e. MSTP(82),PARP(81,89,90)=",
157 print(
"********** PoorE791_tune **********")
165 print(
"********** LHCb_tune **********")
173 P6.SetMSTP(51, 10042)
177 P6.SetPARP(89, 14000)
178 P6.SetPARP(90, 0.238)
182 P6.SetPARP(149, 0.02)
183 P6.SetPARP(150, 0.085)
186 P6.SetPARJ(13, 0.769)
188 P6.SetPARJ(15, 0.018)
189 P6.SetPARJ(16, 0.0815)
190 P6.SetPARJ(17, 0.0815)
193 print(
"********** LHCb_tune **********")
199 nb = hist.GetNbinsX()
200 i1 = hist.FindBin(pbeaml, 0.0, 0.0)
201 y1 = hist.GetBinContent(i1)
202 p1 = hist.GetBinCenter(i1)
203 for i
in range(i1 + 1, nb + 1):
204 if hist.GetBinContent(i) > 0.0:
205 y2 = hist.GetBinContent(i)
206 p2 = hist.GetBinCenter(i)
207 tg = (y2 - y1) / (p2 - p1)
208 for ib
in range(i1 + 1, i):
209 px = hist.GetBinCenter(ib)
210 yx = (px - p1) * tg + y1
217if args.pythia_tune ==
"PoorE791":
219 myPythia.OpenFortranFile(11, os.devnull)
220 myPythia.SetMSTU(11, 11)
223 myPythia.OpenFortranFile(6, os.devnull)
226print(f
"Setting random number seed = {args.seed}")
227myPythia.SetMRPY(1, args.seed)
237idhist = len(idbeam) * 4 * [0]
238print(
"Get chi vs momentum for all beam+target particles")
239for idp
in range(0, len(idbeam)):
241 idw = idbeam[idp] * idpm
242 _p = PDG.GetParticle(idw)
246 for idnp
in range(2):
247 idb = id * 10 + idnp * 4
248 ut.bookHist(h, str(idb + 1), f
"sigma vs p, {name} -> {target[idnp]}", nb, 0.5, args.pbeamh + 0.5)
249 ut.bookHist(h, str(idb + 2), f
"sigma-tot vs p, {name} -> {target[idnp]}", nb, 0.5, args.pbeamh + 0.5)
250 ut.bookHist(h, str(idb + 3), f
"chi vs p, {name}-> {target[idnp]}", nb, 0.5, args.pbeamh + 0.5)
251 ut.bookHist(h, str(idb + 4), f
"Prob(normalised), {name} -> {target[idnp]}", nb, 0.5, args.pbeamh + 0.5)
254 for idpn
in range(2):
256 print(f
"{name}, {target[idpn]}, for chi, seconds: {time.time() - t0}")
257 for ipbeam
in range(args.nrpoints):
258 pbw = ipbeam * (args.pbeamh - pbeaml) / (args.nrpoints - 1) + pbeaml
260 ibin = h[str(id * 10 + 1 + idadd)].FindBin(pbw, 0.0, 0.0)
261 pbw = h[str(id * 10 + 1 + idadd)].GetBinCenter(ibin)
263 myPythia.SetMSEL(args.mselcb)
264 myPythia.Initialize(
"FIXT", name, target[idpn], pbw)
265 for iev
in range(args.nev):
266 myPythia.GenerateEvent()
268 h[str(id * 10 + 1 + idadd)].Fill(pbw, tp.getPyint5_XSEC(2, 0))
270 myPythia.Initialize(
"FIXT", name, target[idpn], pbw)
271 for iev
in range(args.nev // 10):
273 myPythia.GenerateEvent()
275 h[str(id * 10 + 2 + idadd)].Fill(pbw, tp.getPyint5_XSEC(2, 0))
277 h[str(id * 10 + 3 + idadd)].Divide(h[str(id * 10 + 1 + idadd)], h[str(id * 10 + 2 + idadd)], 1.0, 1.0)
279 fillp1(h[str(id * 10 + 3 + idadd)])
283for i
in range(1, id + 1):
284 for idpn
in range(2):
285 idw = i * 10 + idpn * 4 + 3
286 ibh = h[str(idw)].FindBin(args.pbeamh)
288 f
"beam id: {i}, {idw}, {idhist[i]}, momentum: {args.pbeamh},chi: {h[str(idw)].GetBinContent(ibh)}, chimx: {chimx}"
290 if h[str(idw)].GetBinContent(ibh) > chimx:
291 chimx = h[str(idw)].GetBinContent(ibh)
293for i
in range(1, id + 1):
294 for idpn
in range(2):
295 idw = i * 10 + idpn * 4 + 3
296 h[str(idw + 1)].Add(h[str(idw)], h[str(idw)], chimx, 0.0)
301ut.bookHist(h, str(1),
"E of signals", 100, 0.0, 400.0)
302ut.bookHist(h, str(2),
"nr signal per cascade depth", 50, 0.5, 50.5)
303ut.bookHist(h, str(3),
"D0 pt**2", 40, 0.0, 4.0)
304ut.bookHist(h, str(4),
"D0 pt**2", 100, 0.0, 18.0)
305ut.bookHist(h, str(5),
"D0 pt", 100, 0.0, 10.0)
306ut.bookHist(h, str(6),
"D0 XF", 100, -1.0, 1.0)
308ftup = ROOT.TFile.Open(args.Fntuple,
"RECREATE")
311 "pythia6 heavy flavour",
312 "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:\
313s0:s1:s2:s3:s4:s5:s6:s7:s8:s9:s10:s11:s12:s13:s14:s15",
318 kc = myPythia.Pycomp(kf)
319 myPythia.SetMDCY(kc, 1, 0)
322 kc = myPythia.Pycomp(kf)
323 myPythia.SetMDCY(kc, 1, 0)
327stack: list[Any] = 1000 * [0]
328for iev
in range(args.nevgen):
330 print(
"Generate event ", iev)
334 stack[nstack] = [2212, 0.0, 0.0, args.pbeamh, 1, 100 * [0], 100 * [0]]
335 stack[nstack][5][0] = 2212
338 ptot = ROOT.TMath.Sqrt(stack[nstack][1] ** 2 + stack[nstack][2] ** 2 + stack[nstack][3] ** 2)
341 for i
in range(1, id + 1):
342 if stack[nstack][0] == idhist[i]:
344 if random.random() > fracp:
346 idw = i * 10 + idpn * 4 + 4
347 ib = h[str(idw)].FindBin(ptot, 0.0, 0.0)
348 prbsig = h[str(idw)].GetBinContent(ib)
349 if prbsig > random.random():
351 for k
in range(1, 4):
352 myPythia.SetP(1, k, stack[nstack][k])
353 myPythia.SetP(2, k, 0.0)
355 myPythia.SetMSEL(args.mselcb)
356 _beam = ut.get_particle(stack[nstack][0])
357 myPythia.Initialize(
"3MOM", _beam.GetName(), target[idpn], 0.0)
358 myPythia.GenerateEvent()
361 for itrk
in range(1, myPythia.GetN() + 1):
362 idabs = ROOT.TMath.Abs(myPythia.GetK(itrk, 2))
365 vl.append(
float(myPythia.GetK(itrk, 2)))
366 for i
in range(1, 6):
367 vl.append(
float(myPythia.GetP(itrk, i)))
368 vl.append(
float(myPythia.GetK(1, 2)))
369 for i
in range(1, 6):
370 vl.append(
float(myPythia.GetP(1, i)))
371 vl.append(
float(stack[nstack][4]))
373 vl.append(
float(stack[nstack][5][i]))
374 nsub = stack[nstack][4] - 1
377 for i
in range(nsub):
378 vl.append(
float(stack[nstack][6][i]))
379 vl.append(
float(myPythia.GetMSTI(1)))
380 for i
in range(nsub + 1, 16):
383 charmFound.append(itrk)
384 h[
"1"].Fill(myPythia.GetP(itrk, 4))
385 h[
"2"].Fill(stack[nstack][4])
386 if idabs == 421
and stack[nstack][4] == 1:
388 pt2 = myPythia.GetP(itrk, 1) ** 2 + myPythia.GetP(itrk, 2) ** 2
391 h[
"5"].Fill(ROOT.TMath.Sqrt(pt2))
393 beta = args.pbeamh / (myPythia.GetP(1, 4) + myPythia.GetP(2, 5))
394 gamma = (1 - beta**2) ** (-0.5)
395 pbcm = -gamma * beta * myPythia.GetP(1, 4) + gamma * myPythia.GetP(1, 3)
396 pDcm = -gamma * beta * myPythia.GetP(itrk, 4) + gamma * myPythia.GetP(itrk, 3)
399 if len(charmFound) > 0
and args.storePrimaries:
400 for itP
in range(1, myPythia.GetN() + 1):
401 if itP
in charmFound:
403 if myPythia.GetK(itP, 1) == 1:
407 float(myPythia.GetK(itP, 2)),
408 float(myPythia.GetP(itP, 1)),
409 float(myPythia.GetP(itP, 2)),
410 float(myPythia.GetP(itP, 3)),
411 float(myPythia.GetP(itP, 4)),
412 float(myPythia.GetP(itP, 5)),
414 float(myPythia.GetV(itP, 1) - myPythia.GetV(charmFound[0], 1)),
415 float(myPythia.GetV(itP, 2) - myPythia.GetV(charmFound[0], 2)),
416 float(myPythia.GetV(itP, 3) - myPythia.GetV(charmFound[0], 3)),
423 for k
in range(1, 4):
424 myPythia.SetP(1, k, stack[nstack][k])
425 myPythia.SetP(2, k, 0.0)
429 if random.random() > fracp:
431 _beam = ut.get_particle(stack[nstack][0])
432 myPythia.Initialize(
"3MOM", _beam.GetName(), target[idpn], 0.0)
433 myPythia.GenerateEvent()
436 icas = stack[nstack][4] + 1
439 anclist = copy.copy(stack[nstack][5])
440 sublist = copy.copy(stack[nstack][6])
443 sublist[0] = myPythia.GetMSTI(1)
445 for itrk
in range(1, myPythia.GetN() + 1):
446 if myPythia.GetK(itrk, 1) == 1:
447 ptot = ROOT.TMath.Sqrt(
448 myPythia.GetP(itrk, 1) ** 2 + myPythia.GetP(itrk, 2) ** 2 + myPythia.GetP(itrk, 3) ** 2
452 for isig
in range(len(idbeam)):
453 if ROOT.TMath.Abs(myPythia.GetK(itrk, 2)) == idbeam[isig]
and nstack < 999:
457 tmp = copy.copy(anclist)
458 tmp[icas - 1] = myPythia.GetK(itrk, 2)
459 stmp = copy.copy(sublist)
461 stmp[icas - 1] = myPythia.GetMSTI(1)
463 myPythia.GetK(itrk, 2),
464 myPythia.GetP(itrk, 1),
465 myPythia.GetP(itrk, 2),
466 myPythia.GetP(itrk, 3),
472print(
"Now at Ntup.Write()")
479rtime = timer.RealTime()
480ctime = timer.CpuTime()
482print(
"Macro finished successfully.")
483print(f
"Output file is {ftup.GetName()}")
484print(f
"Real time {rtime} s, CPU time {ctime} s")