FairShip
Loading...
Searching...
No Matches
study_muMSC.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# SPDX-License-Identifier: LGPL-3.0-or-later
3# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
4
5import sys
6import time
7from typing import NamedTuple
8
9import ROOT
10import shipRoot_conf
11
12ROOT.gROOT.ProcessLine('#include "FairModule.h"')
13time.sleep(20)
14
15import geometry_config
16import shipunit as u
17
18mcEngine = "TGeant4"
19runnr = 1
20nev = 10000000
21
22
23class Setup(NamedTuple):
24 """Target block and beam configuration for one multiple-scattering study point."""
25
26 thickness: float
27 material: str
28 momentum: float
29 maxTheta: float
30
31
32setup = {
33 "Fig3": Setup(0.1 * u.cm, "lead", 2 * u.GeV, 0.2),
34 "Fig4": Setup(0.1 * u.cm, "lead", 8 * u.GeV, 0.04),
35 "Fig5": Setup(0.1 * u.cm, "lead", 14 * u.GeV, 0.02),
36 "Fig6": Setup(1.44 * u.cm, "copper", 11.7 * u.GeV, 0.045),
37 "Fig7": Setup(1.44 * u.cm, "copper", 7.3 * u.GeV, 0.045),
38}
39
40s = sys.argv[1]
41thickness, material, momentum, maxTheta = setup[s]
42
43checkOverlap = True
44storeOnlyMuons = True
45
46outFile = "msc" + s + ".root"
47theSeed = int(10000 * time.time() % 10000000)
48ecut = 0.0
49
50# -------------------------------------------------------------------
51ROOT.gRandom.SetSeed(theSeed) # this should be propagated via ROOT to Pythia8 and Geant4VMC
52shipRoot_conf.configure() # load basic libraries, prepare atexit for python
53ship_geo = geometry_config.create_config(Yheight=10, shieldName="TRY_2026")
54
55# -----Timer--------------------------------------------------------
56timer = ROOT.TStopwatch()
57timer.Start()
58
59# -----Create simulation run----------------------------------------
60run = ROOT.FairRunSim()
61run.SetName(mcEngine) # Transport engine
62sink = ROOT.FairRootFileSink(outFile)
63run.SetSink(sink)
64ROOT.SetOwnership(sink, False) # C++ FairRun takes ownership
65run.SetUserConfig("g4Config.C") # user configuration file default g4Config.C
66rtdb = run.GetRuntimeDb()
67
68# -----Materials----------------------------------------------
69run.SetMaterials("media.geo")
70# -----Create geometry----------------------------------------------
71cave = ROOT.ShipCave("CAVE")
72cave.SetGeometryFileName("cave.geo")
73run.AddModule(cave)
74
75
76class Block(ROOT.pyFairModule):
77 "block of material"
78
79 def __init__(self) -> None:
80 ROOT.pyFairModule.__init__(self, self)
81
82 def ConstructGeometry(self) -> None:
83 print("Construct Block")
84 top = ROOT.gGeoManager.GetTopVolume()
85 geoLoad = ROOT.FairGeoLoader.Instance()
86 geoFace = geoLoad.getGeoInterface()
87 media = geoFace.getMedia()
88 geoBuild = geoLoad.getGeoBuilder()
89 ShipMedium = media.getMedium(material)
90 W = ROOT.gGeoManager.GetMedium(material)
91 if not W:
92 geoBuild.createMedium(ShipMedium)
93 W = ROOT.gGeoManager.GetMedium(material)
94 aBox = ROOT.gGeoManager.MakeBox("target", W, 100.0 * u.cm, 100.0 * u.cm, thickness)
95 top.AddNode(aBox, 1, ROOT.TGeoTranslation(0, 0, 0))
96
97 def InitParContainers() -> None:
98 print("not implemented!")
99
100
101sensPlane = ROOT.exitHadronAbsorber()
102sensPlane.SetEnergyCut(ecut * u.GeV)
103if storeOnlyMuons:
104 sensPlane.SetOnlyMuons()
105sensPlane.SetZposition(thickness + 10 * u.cm)
106run.AddModule(sensPlane)
107target = Block()
108run.AddModule(target)
109
110primGen = ROOT.FairPrimaryGenerator()
111myPgun = ROOT.FairBoxGenerator(13, 1) # pdg id and multiplicity
112myPgun.SetPRange(momentum - 0.01, momentum + 0.01)
113myPgun.SetPhiRange(0, 0) # // Azimuth angle range [degree]
114myPgun.SetThetaRange(0, 0) # // Polar angle in lab system range [degree]
115myPgun.SetXYZ(0.0 * u.cm, 0.0 * u.cm, -10.0 * u.cm - (thickness))
116primGen.AddGenerator(myPgun)
117run.SetGenerator(primGen)
118#
119run.SetGenerator(primGen)
120# -----Initialize simulation run------------------------------------
121run.Init()
122gMC = ROOT.TVirtualMC.GetMC()
123
124fStack = gMC.GetStack()
125fStack.SetMinPoints(1)
126fStack.SetEnergyCut(-1.0)
127
128# -----Start run----------------------------------------------------
129run.Run(nev)
130
131# -----Start Analysis---------------
132ROOT.gROOT.ProcessLine('#include "Geant4/G4EmParameters.hh"')
133emP = ROOT.G4EmParameters.Instance()
134emP.Dump()
135
136import rootUtils as ut
137
138f = ROOT.gROOT.GetListOfFiles()[0]
139h = {}
140ut.bookHist(h, "theta", "scattering angle " + str(momentum) + "GeV/c;{Theta}(rad)", 500, 0, maxTheta)
141sTree = f.Get("cbmsim")
142for n in range(sTree.GetEntries()):
143 rc = sTree.GetEvent(n)
144 for aHit in sTree.vetoPoint:
145 if aHit.GetTrackID() != 0:
146 continue
147 pt = ROOT.TMath.Sqrt(aHit.GetPx() ** 2 + aHit.GetPy() ** 2)
148 scat = ROOT.TMath.ATan2(pt, aHit.GetPz())
149 rc = h["theta"].Fill(scat)
150ut.bookCanvas(h, key=s, title=s, nx=900, ny=600, cx=1, cy=1)
151tc = h[s].cd(1)
152tc.SetLogy(1)
153h["theta_100"] = h["theta"].Clone("theta_100")
154h["theta_100"] = h["theta"].Rebin(5)
155h["theta_100"].Scale(1.0 / h["theta_100"].GetMaximum())
156h["theta_100"].Draw()
157h[s].Print(s + ".png")
158h[s].Print(s + ".root")
159
160f.Write(h["theta"].GetName())
161f.Write(h["theta_100"].GetName())
162
163# -----Finish-------------------------------------------------------
164timer.Stop()
165rtime = timer.RealTime()
166ctime = timer.CpuTime()
167print(" ")
168print("Macro finished successfully.")
169print("Output file is ", outFile)
170print("Real time ", rtime, " s, CPU time ", ctime, "s")
None ConstructGeometry(self)
None __init__(self)
None InitParContainers()
create_config(str DecayVolumeMedium="helium", float Yheight=6.0, int strawDesign=10, str shieldName="TRY_2026", int nuTargetPassive=1, bool SND=True, SND_design=None, TARGET_YAML=None)
None configure(darkphoton=None)