2from __future__
import print_function
3import ROOT,os,sys,getopt,time,shipRoot_conf
5from ShipGeoConfig
import ConfigRegistry
24charmInputFile =
"root://eoslhcb.cern.ch//eos/ship/data/Charm/Cascade-parp16-MSTP82-1-MSEL4-76Mpot_1.root"
28theSeed = int(10000 * time.time() % 10000000)
48logger = logging.getLogger(os.path.splitext(os.path.basename(os.sys.argv[0]))[0])
49logger.setLevel(logging.INFO)
54 host = socket.gethostname()
55 job_base_name = os.path.splitext(os.path.basename(os.sys.argv[0]))[0]
56 if tag: out_dir =
"{host}_{base}_{runnr}_{comment}".format(host=host, base=job_base_name, runnr=run_number, comment=tag)
57 else: out_dir =
"{host}_{base}_{runnr}".format(host=host, base=job_base_name, runnr=run_number)
62 global runnr, nev, ecut, G4only, tauOnly,JpsiMainly, work_dir,Debug,withEvtGen,boostDiMuon,\
63 boostFactor,charm,beauty,charmInputFile,nStart,storeOnlyMuons,chicc,chibb,npot, nStart
64 logger.info(
"SHiP proton-on-taget simulator (C) Thomas Ruf, 2017")
66 ap = argparse.ArgumentParser(
67 description=
'Run SHiP "pot" simulation')
68 ap.add_argument(
'-d',
'--debug', action=
'store_true', dest=
'debug')
69 ap.add_argument(
'-f',
'--force', action=
'store_true', help=
"force overwriting output directory")
70 ap.add_argument(
'-cs',
'--CharmdetSetup', type=int, dest=
'CharmdetSetup',help=
"setting detector setup", default=0)
71 ap.add_argument(
'-ct',
'--CharmTarget', type=int, dest=
'CharmTarget',help=
"choosing target configuration for charm exposure", default=3)
72 ap.add_argument(
'-r',
'--run-number', type=int, dest=
'runnr', default=runnr)
73 ap.add_argument(
'-e',
'--ecut', type=float, help=
"energy cut", dest=
'ecut', default=ecut)
74 ap.add_argument(
'-n',
'--num-events', type=int, help=
"number of events to generate", dest=
'nev', default=nev)
75 ap.add_argument(
'-G',
'--G4only', action=
'store_true', dest=
'G4only', default=
False, help=
"use Geant4 directly, no Pythia8")
76 ap.add_argument(
'-P',
'--PythiaDecay', action=
'store_true', dest=
'pythiaDecay', default=
False, help=
"use Pythia8 for decays")
77 ap.add_argument(
'-V',
'--EvtGen', action=
'store_true', dest=
'withEvtGen', default=withEvtGen, help=
"use EvtGen for decays")
78 ap.add_argument(
'-t',
'--tau-only', action=
'store_true', dest=
'tauOnly', default=
False)
79 ap.add_argument(
'-J',
'--Jpsi-mainly', action=
'store_true', dest=
'JpsiMainly', default=
False)
80 ap.add_argument(
'-b',
'--boostDiMuon', type=float, dest=
'boostDiMuon', default=boostDiMuon, help=
"boost Di-muon branching ratios")
81 ap.add_argument(
'-X',
'--boostFactor', type=float, dest=
'boostFactor', default=boostFactor, help=
"boost Di-muon prod cross sections")
82 ap.add_argument(
'-C',
'--charm', action=
'store_true', dest=
'charm', default=charm, help=
"generate charm decays")
83 ap.add_argument(
'-B',
'--beauty', action=
'store_true', dest=
'beauty', default=beauty, help=
"generate beauty decays")
84 ap.add_argument(
'-M',
'--storeOnlyMuons', action=
'store_true', dest=
'storeOnlyMuons', default=storeOnlyMuons, help=
"store only muons, ignore neutrinos")
86 ap.add_argument(
'-cc',
'--chicc',action=
'store_true', dest=
'chicc', default=chicc, help=
"ccbar over mbias cross section")
87 ap.add_argument(
'-bb',
'--chibb',action=
'store_true', dest=
'chibb', default=chibb, help=
"bbbar over mbias cross section")
88 ap.add_argument(
'-p',
'--pot',action=
'store_true', dest=
'npot', default=npot, help=
"number of protons on target per spill to normalize on")
89 ap.add_argument(
'-S',
'--nStart',action=
'store_true', dest=
'nStart', default=nStart, help=
"first event of input file to start")
90 ap.add_argument(
'-I',
'--InputFile', type=str, dest=
'charmInputFile', default=charmInputFile, help=
"input file for charm/beauty decays")
91 ap.add_argument(
'-o',
'--output' , type=str, help=
"output directory", dest=
'work_dir', default=
None)
92 args = ap.parse_args()
94 logger.setLevel(logging.DEBUG)
98 tauOnly = args.tauOnly
99 JpsiMainly = args.JpsiMainly
101 boostFactor = args.boostFactor
102 boostDiMuon = args.boostDiMuon
103 storeOnlyMuons = args.storeOnlyMuons
107 args.withEvtGen =
False
108 args.pythiaDecay =
False
109 elif args.pythiaDecay:
111 args.withEvtGen =
False
112 logger.info(
"use Pythia8 as primary decayer")
115 logger.info(
"use EvtGen as primary decayer")
116 withEvtGen = args.withEvtGen
120 logger.warn(
"charm and beauty decays are set! Beauty gets priority")
122 charmInputFile = args.charmInputFile
123 nStart = int(args.nStart)
125 if args.work_dir
is None:
129 work_dir = args.work_dir
130 logger.debug(
"work_dir: %s" % work_dir)
131 logger.debug(
"command line arguments: %s", args)
132 if os.path.exists(work_dir):
133 logger.warn(
"output directory '%s' already exists." % work_dir)
135 logger.warn(
"...cleaning")
136 for root, dirs, files
in os.walk(work_dir):
138 os.unlink(os.path.join(root, f))
140 shutil.rmtree(os.path.join(root, d))
142 logger.warn(
"...use '-f' option to overwrite it")
144 os.makedirs(work_dir)
150ROOT.gRandom.SetSeed(theSeed)
153ship_geo = ConfigRegistry.loadpy(
"$FAIRSHIP/geometry/charm-geometry_config.py", Setup = args.CharmdetSetup, cTarget = args.CharmTarget)
155txt =
'pythia8_Geant4_'
156if withEvtGen: txt =
'pythia8_evtgen_Geant4_'
157outFile = outputDir+
'/'+txt+str(runnr)+
'_'+str(ecut)+
'.root'
158parFile = outputDir+
'/ship.params.'+txt+str(runnr)+
'_'+str(ecut)+
'.root'
161timer = ROOT.TStopwatch()
165run = ROOT.FairRunSim()
167run.SetOutputFile(outFile)
168run.SetUserConfig(
"g4Config.C")
169rtdb = run.GetRuntimeDb()
172run.SetMaterials(
"media.geo")
174import charmDet_conf
as shipDet_conf
178primGen = ROOT.FairPrimaryGenerator()
179P8gen = ROOT.FixedTargetGenerator()
180if (ship_geo.MufluxSpectrometer.muflux==
True):
181 P8gen.SetTarget(
"/TargetArea_1",0.,0.)
183 P8gen.SetCharmTarget()
184 P8gen.SetTarget(
"volTarget_1",0.,0.)
185 if ship_geo.Box.gausbeam:
186 primGen.SetBeam(0.,0., 0.5, 0.5)
187 primGen.SmearGausVertexXY(
True)
189 primGen.SetBeam(0.,0., ship_geo.Box.TX-1., ship_geo.Box.TY-1.)
190 primGen.SmearVertexXY(
True)
191P8gen.SetMom(400.*u.GeV)
192P8gen.SetEnergyCut(ecut*u.GeV)
194if G4only: P8gen.SetG4only()
195if withEvtGen: P8gen.WithEvtGen()
197 P8gen.SetBoost(boostDiMuon)
199P8gen.SetSeed(theSeed)
204 P8gen.InitForCharmOrBeauty(
"root://eoslhcb.cern.ch//eos/ship/data/Charm/Cascade-parp16-MSTP82-1-MSEL4-76Mpot_1.root",nev,npot,nStart)
205primGen.AddGenerator(P8gen)
207run.SetGenerator(primGen)
212gMC = ROOT.TVirtualMC.GetMC()
213fStack = gMC.GetStack()
214fStack.SetMinPoints(1)
215fStack.SetEnergyCut(-1.)
217import AddDiMuonDecayChannelsToG4
222 ROOT.gROOT.ProcessLine(
'#include "Geant4/G4ProcessTable.hh"')
223 ROOT.gROOT.ProcessLine(
'#include "Geant4/G4AnnihiToMuPair.hh"')
224 ROOT.gROOT.ProcessLine(
'#include "Geant4/G4GammaConversionToMuons.hh"')
225 gProcessTable = ROOT.G4ProcessTable.GetProcessTable()
226 procAnnihil = gProcessTable.FindProcess(ROOT.G4String(
'AnnihiToMuPair'),ROOT.G4String(
'e+'))
227 procGMuPair = gProcessTable.FindProcess(ROOT.G4String(
'GammaToMuPair'),ROOT.G4String(
'gamma'))
228 procGMuPair.SetCrossSecFactor(boostFactor)
229 procAnnihil.SetCrossSecFactor(boostFactor)
237rtime = timer.RealTime()
238ctime = timer.CpuTime()
240print(
"Macro finished succesfully.")
241print(
"Output file is ", outFile)
242print(
"Real time ",rtime,
" s, CPU time ",ctime,
"s")
244if (ship_geo.MufluxSpectrometer.muflux==
True):
246 tmpFile = outFile+
"tmp"
247 fin = ROOT.gROOT.GetListOfFiles()[0]
249 fHeader = fin.FileHeader
250 fHeader.SetRunId(runnr)
253 poteq = P8gen.GetPotForCharm()
254 fHeader.SetTitle(
"POT equivalent = %7.3G"%(poteq))
256 fHeader.SetTitle(
"POT = "+str(nev))
257 print(
"Data generated ", fHeader.GetTitle())
259 fout = ROOT.TFile(tmpFile,
'recreate' )
260 sTree = t.CloneTree(0)
262 for n
in range(t.GetEntries()):
264 if (t.ScintillatorPoint.GetEntries()>0):
269 for x
in fin.GetList():
270 if not x.Class().GetName().find(
'TH')<0:
274 ff = fin.FileHeader.Clone(fout.GetName())
276 ff.Write(
"FileHeader", ROOT.TObject.kSingleKey)
279 os.system(
"mv "+tmpFile+
" "+outFile)
281 print(
"Number of events produced with activity after hadron absorber:",nEvents)
284 print(
"No post processing done")
286sGeo = ROOT.gGeoManager
287run.CreateGeometryFile(
"%s/geofile_full.root" % (outputDir))
290 sGeo = ROOT.gGeoManager
293 run.CreateGeometryFile(
"%s/geofile_full.root" % (outputDir))
294 import saveBasicParameters
get_work_dir(run_number, tag=None)
execute(f, ox, name='ShipGeo')
configure(darkphoton=None)