forked from shir994/DP_scattering_startup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_simScript.py
610 lines (579 loc) · 27 KB
/
run_simScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#!/usr/bin/env python
import ROOT,os,sys,getopt,time
import shipunit as u
import shipRoot_conf
import rootUtils as ut
from ShipGeoConfig import ConfigRegistry
debug = 0 # 1 print weights and field
# 2 make overlap check
# Default HNL parameters
theMass = 1.0*u.GeV
theCouplings = [0.447e-9, 7.15e-9, 1.88e-9] # ctau=53.3km TP default for HNL
# Default Light Dark Matter parameters
theLDMmass = 0.1*u.GeV
#LDMpdgcode = 9000007
LDMpdgcode = 5000521
# Default dark photon parameters
theDPmass = 0.2*u.GeV
theDPepsilon = 0.00000008
mcEngine = "TGeant4"
simEngine = "Pythia8" # "Genie" # Ntuple
nEvents = 100
firstEvent = 0
inclusive = "True" # True = all processes; if "c" only ccbar -> HNL; if "b" only bbar -> HNL; and for darkphotons: if meson = production through meson decays, pbrem = proton bremstrahlung, to do: QCD prod.
deepCopy = True # False = copy only stable particles to stack, except for HNL events
MCTracksWithHitsOnly = False # copy particles which produced a hit and their history
MCTracksWithEnergyCutOnly = False # copy particles above a certain kin energy cut
MCTracksWithHitsOrEnergyCut = False # or of above, factor 2 file size increase compared to MCTracksWithEnergyCutOnly
charmonly = False # option to be set with -A to enable only charm decays, charm x-sec measurement
HNL = False
DarkPhoton = False
LightDarkMatter = False
RPVSUSY = False
RPVSUSYbench = 2
eventDisplay = False
inputFile = "/eos/experiment/ship/data/Charm/Cascade-parp16-MSTP82-1-MSEL4-76Mpot_1.root"
inputPythiaFile = "out.lhe"
defaultInputFile = True
outputDir = "."
sameSeed = False # can be set to an integer for the muonBackground simulation with specific seed for each muon
theSeed = int(10000 * time.time() % 10000000)
dy = 10.
dv = 5 # 4=TP elliptical tank design, 5 = optimized conical rectangular design, 6=5 without segment-1
ds = 7 # 5=TP muon shield, 6=magnetized hadron, 7=short magnet design
nud = 1 # 0=TP, 1=new magnet option for short muon shield, 2= no magnet surrounding neutrino detector
charm = 0 # !=0 create charm detector instead of SHiP
caloDesign = 0 # 0=ECAL/HCAL TP 1=ECAL/HCAL TP + preshower 2=splitCal
strawDesign = 4 # simplistic tracker design, 4=sophisticated straw tube design, horizontal wires (default), 10=2cm straw diameter for 2018 layout
geofile = None
inactivateMuonProcesses = False # provisionally for making studies of various muon background sources
checking4overlaps = False
if debug>1 : checking4overlaps = True
phiRandom = False # only relevant for muon background generator
followMuon = False # make muonshield active to follow muons
fastMuon = False # only transport muons for a fast muon only background estimate
nuRadiography = False # misuse GenieGenerator for neutrino radiography and geometry timing test
Opt_high = None # switch for cosmic generator
try:
opts, args = getopt.getopt(sys.argv[1:], "D:FHPu:n:i:f:c:hqv:s:l:A:Y:i:m:co:t:g",[\
"PG","Pythia6","Pythia8","Genie","MadDump","MuDIS","Ntuple","Nuage","MuonBack","FollowMuon","FastMuon",\
"Cosmics=","nEvents=", "display", "seed=", "firstEvent=", "phiRandom", "mass=", "couplings=", "coupling=", "epsilon=",\
"output=","tankDesign=","muShieldDesign=","NuRadio","test", "madpy"\
"DarkPhoton","RpvSusy","SusyBench=","sameSeed=","charm=","nuTauTargetDesign=","caloDesign=","strawDesign="])
except getopt.GetoptError:
# print help information and exit:
print ' enter --Pythia8 to generate events with Pythia8 (-A b: signal from b, -A c: signal from c (default) or -A inclusive)'
print ' or --Genie for reading and processing neutrino interactions '
print ' or --Pythia6 for muon nucleon scattering'
print ' or --PG for particle gun'
print ' --MuonBack to generate events from muon background file, --Cosmics=0 for cosmic generator data'
print ' --RpvSusy to generate e vents based on RPV neutralino (default HNL)'
print ' --DarkPhoton to generate events with dark photons (default HNL)'
print ' --MadDump for reading and processing Light Dark Matter interactions, use -madpy for Pythia input in DIS'
print ' for darkphoton generation, use -A meson or -A pbrem or -A qcd'
print ' --SusyBench to specify which of the preset benchmarks to generate (default 2)'
print ' --mass or -m to set HNL or New Particle mass'
print ' --couplings \'U2e,U2mu,U2tau\' or -c \'U2e,U2mu,U2tau\' to set list of HNL couplings'
print ' --epsilon value or -e value to set mixing parameter epsilon'
print ' Note that for RPVSUSY the third entry of the couplings is the stop mass'
sys.exit(2)
for o, a in opts:
if o in ("-D","--display"):
eventDisplay = True
if o in ("--Pythia6",):
simEngine = "Pythia6"
if o in ("--Pythia8",):
simEngine = "Pythia8"
if o in ("--PG",):
simEngine = "PG"
if o in ("-A",):
inclusive = a
if a.lower() == 'charmonly':
charmonly = True
HNL = False
if a not in ['b','c','meson','pbrem','qcd']: inclusive = True
if o in ("--Genie",):
simEngine = "Genie"
if o in ("--MadDump",):
LightDarkMatter = True
simEngine = "MadDump"
if o in ("--NuRadio",):
simEngine = "nuRadiography"
if o in ("--Ntuple",):
simEngine = "Ntuple"
if o in ("--FollowMuon",):
followMuon = True
if o in ("--FastMuon",):
fastMuon = True
if o in ("--MuonBack",):
simEngine = "MuonBack"
if o in ("--Nuage",):
simEngine = "Nuage"
if o in ("--phiRandom",):
phiRandom = True
if o in ("--Cosmics",):
simEngine = "Cosmics"
Opt_high = 0
if a!=str(0): Opt_high = int(a)
if o in ("--MuDIS",):
simEngine = "muonDIS"
if o in ("-n", "--nEvents",):
nEvents = int(a)
if o in ("-i", "--firstEvent",):
firstEvent = int(a)
if o in ("-s", "--seed",):
theSeed = int(a)
if o in ("-s", "--sameSeed",):
sameSeed = int(a)
if o in ("--madpy",):
inputPythiaFile = a
if o in ("-f",):
if a.lower() == "none": inputFile = None
else: inputFile = a
defaultInputFile = False
if o in ("-g",):
geofile = a
if o in ("-o", "--output",):
outputDir = a
if o in ("-Y",):
dy = float(a)
if o in ("--tankDesign",):
dv = int(a)
if o in ("--muShieldDesign",):
ds = int(a)
if o in ("--nuTauTargetDesign",):
nud = int(a)
if o in ("--caloDesign",):
caloDesign = int(a)
if o in ("--strawDesign",):
strawDesign = int(a)
if o in ("--charm",):
charm = int(a)
if o in ("-F",):
deepCopy = True
if o in ("--RpvSusy",):
HNL = False
RPVSUSY = True
if o in ("--DarkPhoton",):
HNL = False
DarkPhoton = True
if o in ("--SusyBench",):
RPVSUSYbench = int(a)
if o in ("-m", "--mass",):
if DarkPhoton: theDPmass = float(a)
else: theMass = float(a)
if o in ("-c", "--couplings", "--coupling",):
theCouplings = [float(c) for c in a.split(",")]
if o in ("-e", "--epsilon",):
theDPepsilon = float(a)
if o in ("-t", "--test"):
inputFile = "../FairShip/files/Cascade-parp16-MSTP82-1-MSEL4-76Mpot_1_5000.root"
nEvents = 50
#sanity check
if (HNL and RPVSUSY) or (HNL and DarkPhoton) or (DarkPhoton and RPVSUSY):
print "cannot have HNL and SUSY or DP at the same time, abort"
sys.exit(2)
if (simEngine == "Genie" or simEngine == "nuRadiography") and defaultInputFile:
inputFile = "/eos/experiment/ship/data/GenieEvents/genie-nu_mu.root"
# "/eos/experiment/ship/data/GenieEvents/genie-nu_mu_bar.root"
if simEngine == "muonDIS" and defaultInputFile:
print 'input file required if simEngine = muonDIS'
print " for example -f /eos/experiment/ship/data/muonDIS/muonDis_1.root"
sys.exit()
if simEngine == "Nuage" and not inputFile:
inputFile = 'Numucc.root'
print "FairShip setup for",simEngine,"to produce",nEvents,"events"
if (simEngine == "Ntuple" or simEngine == "MuonBack") and defaultInputFile :
print 'input file required if simEngine = Ntuple or MuonBack'
print " for example -f /eos/experiment/ship/data/Mbias/pythia8_Geant4-withCharm_onlyMuons_4magTarget.root"
sys.exit()
ROOT.gRandom.SetSeed(theSeed) # this should be propagated via ROOT to Pythia8 and Geant4VMC
shipRoot_conf.configure(DarkPhoton) # load basic libraries, prepare atexit for python
# - muShieldDesign = 2 # 1=passive 5=active (default) 7=short design+magnetized hadron absorber
# - targetOpt = 5 # 0=solid >0 sliced, 5: 5 pieces of tungsten, 4 H20 slits, 17: Mo + W +H2O (default)
# nuTauTargetDesign = 0 # 0 = TP, 1 = NEW with magnet, 2 = NEW without magnet, 3 = 2018 design
if charm == 0: ship_geo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy, tankDesign = dv, \
muShieldDesign = ds, nuTauTargetDesign=nud, CaloDesign=caloDesign, strawDesign=strawDesign, muShieldGeo=geofile)
else: ship_geo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/charm-geometry_config.py")
# switch off magnetic field to measure muon flux
#ship_geo.muShield.Field = 0.
#ship_geo.EmuMagnet.B = 0.
#ship_geo.tauMudet.B = 0.
# Output file name, add dy to be able to setup geometry with ambiguities.
tag = simEngine+"-"+mcEngine
if charmonly: tag = simEngine+"CharmOnly-"+mcEngine
if eventDisplay: tag = tag+'_D'
if dv > 4 : tag = 'conical.'+tag
elif dy: tag = str(dy)+'.'+tag
if not os.path.exists(outputDir):
os.makedirs(outputDir)
outFile = "%s/ship.%s.root" % (outputDir, tag)
# rm older files !!!
for x in os.listdir(outputDir):
if not x.find(tag)<0: os.system("rm %s/%s" % (outputDir, x) )
# Parameter file name
parFile="%s/ship.params.%s.root" % (outputDir, tag)
# In general, the following parts need not be touched
# ========================================================================
# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------
# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine) # Transport engine
run.SetOutputFile(outFile) # Output file
run.SetUserConfig("g4Config.C") # user configuration file default g4Config.C
rtdb = run.GetRuntimeDb()
# -----Create geometry----------------------------------------------
# import shipMuShield_only as shipDet_conf # special use case for an attempt to convert active shielding geometry for use with FLUKA
# import shipTarget_only as shipDet_conf
if charm!=0: import charmDet_conf as shipDet_conf
else: import shipDet_conf
modules = shipDet_conf.configure(run,ship_geo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()
if simEngine == "Pythia8":
primGen.SetTarget(ship_geo.target.z0, 0.)
# -----Pythia8--------------------------------------
if HNL or RPVSUSY:
P8gen = ROOT.HNLPythia8Generator()
import pythia8_conf
if HNL:
print 'Generating HNL events of mass %.3f GeV\n'%theMass
print 'and with couplings=',theCouplings
pythia8_conf.configure(P8gen,theMass,theCouplings,inclusive,deepCopy)
if RPVSUSY:
print 'Generating RPVSUSY events of mass %.3f GeV\n'%theMass
print 'and with couplings=[%.3f,%.3f]\n'%(theCouplings[0],theCouplings[1])
print 'and with stop mass=\%.3f GeV\n',theCouplings[2]
pythia8_conf.configurerpvsusy(P8gen,theMass,[theCouplings[0],theCouplings[1]],
theCouplings[2],RPVSUSYbench,'c',deepCopy)
P8gen.SetSmearBeam(1*u.cm) # finite beam size
P8gen.SetParameters("ProcessLevel:all = off")
if ds==7: # short muon shield
P8gen.SetLmin(44*u.m)
P8gen.SetLmax(107*u.m)
if inputFile:
ut.checkFileExists(inputFile)
# read from external file
P8gen.UseExternalFile(inputFile, firstEvent)
if DarkPhoton:
P8gen = ROOT.DPPythia8Generator()
if inclusive=='qcd':
P8gen.SetDPId(4900023)
else:
P8gen.SetDPId(9900015)
import pythia8darkphoton_conf
passDPconf = pythia8darkphoton_conf.configure(P8gen,theDPmass,theDPepsilon,inclusive,deepCopy)
if (passDPconf!=1): sys.exit()
P8gen.SetSmearBeam(1*u.cm) # finite beam size
if ds==7: # short muon shield
P8gen.SetLmin(44*u.m)
P8gen.SetLmax(107*u.m)
if charmonly:
ut.checkFileExists(inputFile)
primGen.SetBeam(0.,0., ship_geo.Box.TX-2., ship_geo.Box.TY-2.) #Uniform distribution in x/y on the target (1 cm of margin at both sides)
primGen.SmearVertexXY(True)
P8gen = ROOT.Pythia8Generator()
P8gen.UseExternalFile(inputFile, firstEvent)
if ship_geo.MufluxSpectrometer.muflux == False :
P8gen.SetTarget("volTarget_1",0.,0.) # will distribute PV inside target, beam offset x=y=0.
else:
print "ERROR: charmonly option should not be used for the muonflux measurement"
1/0
# pion on proton 500GeV
# P8gen.SetMom(500.*u.GeV)
# P8gen.SetId(-211)
primGen.AddGenerator(P8gen)
if simEngine == "Pythia6":
# set muon interaction close to decay volume
primGen.SetTarget(ship_geo.target.z0+ship_geo.muShield.length, 0.)
# -----Pythia6-------------------------
test = ROOT.TPythia6() # don't know any other way of forcing to load lib
P6gen = ROOT.tPythia6Generator()
P6gen.SetMom(50.*u.GeV)
P6gen.SetTarget("gamma/mu+","n0") # default "gamma/mu-","p+"
primGen.AddGenerator(P6gen)
# -----Particle Gun-----------------------
if simEngine == "PG":
myPgun = ROOT.FairBoxGenerator(22,1)
myPgun.SetPRange(10,10.2)
myPgun.SetPhiRange(0, 360) # // Azimuth angle range [degree]
myPgun.SetThetaRange(0,0) # // Polar angle in lab system range [degree]
myPgun.SetXYZ(0.*u.cm, 0.*u.cm, 0.*u.cm)
primGen.AddGenerator(myPgun)
run.SetGenerator(primGen)
# -----muon DIS Background------------------------
if simEngine == "muonDIS":
ut.checkFileExists(inputFile)
primGen.SetTarget(0., 0.)
DISgen = ROOT.MuDISGenerator()
# from nu_tau detector to tracking station 2
# mu_start, mu_end = ship_geo.tauMudet.zMudetC,ship_geo.TrackStation2.z
#
# in front of UVT up to tracking station 1
mu_start, mu_end = ship_geo.Chamber1.z-ship_geo.chambers.Tub1length-10.*u.cm,ship_geo.TrackStation1.z
print 'MuDIS position info input=',mu_start, mu_end
DISgen.SetPositions(ship_geo.target.z0, mu_start, mu_end)
DISgen.Init(inputFile,firstEvent)
primGen.AddGenerator(DISgen)
nEvents = min(nEvents,DISgen.GetNevents())
print 'Generate ',nEvents,' with DIS input', ' first event',firstEvent
# -----neutrino interactions from nuage------------------------
if simEngine == "Nuage":
primGen.SetTarget(0., 0.)
Nuagegen = ROOT.NuageGenerator()
Nuagegen.EnableExternalDecayer(1) #with 0 external decayer is disable, 1 is enabled
print 'Nuage position info input=',ship_geo.EmuMagnet.zC-ship_geo.NuTauTarget.zdim, ship_geo.EmuMagnet.zC+ship_geo.NuTauTarget.zdim
#--------------------------------
#to Generate neutrino interactions in the whole neutrino target
# Nuagegen.SetPositions(ship_geo.EmuMagnet.zC, ship_geo.NuTauTarget.zC-ship_geo.NuTauTarget.zdim/2, ship_geo.NuTauTarget.zC+ship_geo.NuTauTarget.zdim/2, -ship_geo.NuTauTarget.xdim/2, ship_geo.NuTauTarget.xdim/2, -ship_geo.NuTauTarget.ydim/2, ship_geo.NuTauTarget.ydim/2)
#--------------------------------
#to Generate neutrino interactions ONLY in ONE brick
ntt = 6
nXcells = 7
nYcells = 3
nZcells = ntt -1
startx = -ship_geo.NuTauTarget.xdim/2 + nXcells*ship_geo.NuTauTarget.BrX
endx = -ship_geo.NuTauTarget.xdim/2 + (nXcells+1)*ship_geo.NuTauTarget.BrX
starty = -ship_geo.NuTauTarget.ydim/2 + nYcells*ship_geo.NuTauTarget.BrY
endy = - ship_geo.NuTauTarget.ydim/2 + (nYcells+1)*ship_geo.NuTauTarget.BrY
startz = ship_geo.EmuMagnet.zC - ship_geo.NuTauTarget.zdim/2 + ntt *ship_geo.NuTauTT.TTZ + nZcells * ship_geo.NuTauTarget.CellW
endz = ship_geo.EmuMagnet.zC - ship_geo.NuTauTarget.zdim/2 + ntt *ship_geo.NuTauTT.TTZ + nZcells * ship_geo.NuTauTarget.CellW + ship_geo.NuTauTarget.BrZ
Nuagegen.SetPositions(ship_geo.target.z0, startz, endz, startx, endx, starty, endy)
#--------------------------------
ut.checkFileExists(inputFile)
Nuagegen.Init(inputFile,firstEvent)
primGen.AddGenerator(Nuagegen)
nEvents = min(nEvents,Nuagegen.GetNevents())
run.SetPythiaDecayer("DecayConfigNuAge.C")
print 'Generate ',nEvents,' with Nuage input', ' first event',firstEvent
#-----Light Dark Matter---------------------------
if simEngine == "MadDump":
import convertDMevents
primGen.SetTarget(0., 0.)
ESevent = 'ES'
DISevent = 'DIS'
if inputFile.find(ESevent) != -1 : #convert es if in inputfilename
inputROOTFile = (convertDMevents.convertES(inputFile))
if inputFile.find(DISevent) != -1 : #convert dis if in inputfilename
inputROOTFile = (convertDMevents.convertDIS(inputFile, inputPythiaFile))
# ut.checkFileExists(inputROOTFile)
DMgen = ROOT.DMGenerator()
DMgen.Init(inputROOTFile, firstEvent)
DMgen.SetPositions(ship_geo.target.z0, ship_geo.NuTauTarget.zC-ship_geo.NuTauTarget.zdim/2, ship_geo.NuTauTarget.zC+ship_geo.NuTauTarget.zdim/2)
primGen.AddGenerator(DMgen)
nEvents = min(nEvents, DMgen.GetNevents())
# Add LDM to pdg
pdg_ldm = ROOT.TDatabasePDG.Instance()
pdg_ldm.AddParticle('Xd','LDM', theLDMmass, True, 0., 0., 'Xd', LDMpdgcode)
run.SetPythiaDecayer("DecayConfigNuAge.C")
print 'Generate ', nEvents,' with DMGenerator. Input: first event #', firstEvent, ' from MadDump input file ', inputFile
# -----Neutrino Background------------------------
if simEngine == "Genie":
# Genie
ut.checkFileExists(inputFile)
primGen.SetTarget(0., 0.) # do not interfere with GenieGenerator
Geniegen = ROOT.GenieGenerator()
Geniegen.Init(inputFile,firstEvent)
Geniegen.SetPositions(ship_geo.target.z0,ship_geo.NuTauTarget.zC-ship_geo.NuTauTarget.zdim/2, ship_geo.NuTauTarget.zC+ship_geo.NuTauTarget.zdim/2)
#Geniegen.SetPositions(ship_geo.target.z0, ship_geo.tauMudet.zMudetC-5*u.m, ship_geo.TrackStation2.z)
primGen.AddGenerator(Geniegen)
nEvents = min(nEvents,Geniegen.GetNevents())
run.SetPythiaDecayer("DecayConfigNuAge.C")
print 'Generate ',nEvents,' with Genie input', ' first event',firstEvent
if simEngine == "nuRadiography":
ut.checkFileExists(inputFile)
primGen.SetTarget(0., 0.) # do not interfere with GenieGenerator
Geniegen = ROOT.GenieGenerator()
Geniegen.Init(inputFile,firstEvent)
# Geniegen.SetPositions(ship_geo.target.z0, ship_geo.target.z0, ship_geo.MuonStation3.z)
# Geniegen.SetPositions(ship_geo.target.z0, ship_geo.tauMudet.zMudetC, ship_geo.MuonStation3.z)
Geniegen.SetPositions(ship_geo.target.z0,ship_geo.NuTauTarget.zC-ship_geo.NuTauTarget.zdim/2, ship_geo.NuTauTarget.zC+ship_geo.NuTauTarget.zdim/2)
Geniegen.NuOnly()
primGen.AddGenerator(Geniegen)
print 'Generate ',nEvents,' for nuRadiography', ' first event',firstEvent
# add tungsten to PDG
pdg = ROOT.TDatabasePDG.Instance()
pdg.AddParticle('W','Ion', 1.71350e+02, True, 0., 74, 'XXX', 1000741840)
#
run.SetPythiaDecayer('DecayConfigPy8.C')
# this requires writing a C macro, would have been easier to do directly in python!
# for i in [431,421,411,-431,-421,-411]:
# ROOT.gMC.SetUserDecay(i) # Force the decay to be done w/external decayer
if simEngine == "Ntuple":
# reading previously processed muon events, [-50m - 50m]
ut.checkFileExists(inputFile)
primGen.SetTarget(ship_geo.target.z0+50*u.m,0.)
Ntuplegen = ROOT.NtupleGenerator()
Ntuplegen.Init(inputFile,firstEvent)
primGen.AddGenerator(Ntuplegen)
nEvents = min(nEvents,Ntuplegen.GetNevents())
print 'Process ',nEvents,' from input file'
#
if simEngine == "MuonBack":
# reading muon tracks from previous Pythia8/Geant4 simulation with charm replaced by cascade production
ut.checkFileExists(inputFile)
primGen.SetTarget(ship_geo.target.z0+50*u.m,0.)
MuonBackgen = ROOT.MuonBackGenerator()
MuonBackgen.Init(inputFile,firstEvent,phiRandom)
MuonBackgen.SetSmearBeam(5 * u.cm) # radius of ring, thickness 8mm
if sameSeed: MuonBackgen.SetSameSeed(sameSeed)
primGen.AddGenerator(MuonBackgen)
nEvents = min(nEvents,MuonBackgen.GetNevents())
MCTracksWithHitsOnly = True # otherwise, output file becomes too big
print 'Process ',nEvents,' from input file, with Phi random=',phiRandom, ' with MCTracksWithHitsOnly',MCTracksWithHitsOnly
if followMuon :
fastMuon = True
modules['Veto'].SetFollowMuon()
if fastMuon : modules['Veto'].SetFastMuon()
# missing for the above use case, without making muon shield sensitve
# optional, boost gamma2muon conversion
# ROOT.kShipMuonsCrossSectionFactor = 100.
#
if simEngine == "Cosmics":
primGen.SetTarget(0., 0.)
Cosmicsgen = ROOT.CosmicsGenerator()
import CMBG_conf
CMBG_conf.configure(Cosmicsgen, ship_geo)
if not Cosmicsgen.Init(Opt_high):
print "initialization of cosmic background generator failed ",Opt_high
sys.exit(0)
primGen.AddGenerator(Cosmicsgen)
print 'Process ',nEvents,' Cosmic events'
#
run.SetGenerator(primGen)
# ------------------------------------------------------------------------
#---Store the visualization info of the tracks, this make the output file very large!!
#--- Use it only to display but not for production!
if eventDisplay: run.SetStoreTraj(ROOT.kTRUE)
else: run.SetStoreTraj(ROOT.kFALSE)
# -----Initialize simulation run------------------------------------
run.Init()
gMC = ROOT.TVirtualMC.GetMC()
fStack = gMC.GetStack()
#print "--------------------------------------------------------------"
if MCTracksWithHitsOnly:
fStack.SetMinPoints(1)
fStack.SetEnergyCut(-100.*u.MeV)
elif MCTracksWithEnergyCutOnly:
fStack.SetMinPoints(-1)
fStack.SetEnergyCut(100.*u.MeV)
elif MCTracksWithHitsOrEnergyCut:
fStack.SetMinPoints(1)
fStack.SetEnergyCut(100.*u.MeV)
elif deepCopy:
#print "--------------------------------------------------------------"
fStack.SetMinPoints(0)
fStack.SetEnergyCut(0.*u.MeV)
if eventDisplay:
# Set cuts for storing the trajectories, can only be done after initialization of run (?!)
trajFilter = ROOT.FairTrajFilter.Instance()
trajFilter.SetStepSizeCut(1*u.mm);
trajFilter.SetVertexCut(-20*u.m, -20*u.m,ship_geo.target.z0-1*u.m, 20*u.m, 20*u.m, 200.*u.m)
trajFilter.SetMomentumCutP(0.1*u.GeV)
trajFilter.SetEnergyCut(0., 400.*u.GeV)
trajFilter.SetStorePrimaries(ROOT.kTRUE)
trajFilter.SetStoreSecondaries(ROOT.kTRUE)
# The VMC sets the fields using the "/mcDet/setIsLocalMagField true" option in "gconfig/g4config.in"
import geomGeant4
# geomGeant4.setMagnetField() # replaced by VMC, only has effect if /mcDet/setIsLocalMagField false
# Define extra VMC B fields not already set by the geometry definitions, e.g. a global field,
# any field maps, or defining if any volumes feel only the local or local+global field.
# For now, just keep the fields already defined by the C++ code, i.e comment out the fieldMaker
#fieldMaker = geomGeant4.addVMCFields('field/ExampleBFieldSetup.txt', False)
# Print VMC fields and associated geometry objects
if debug > 0:
geomGeant4.printVMCFields()
geomGeant4.printWeightsandFields(onlyWithField = True,\
exclude=['DecayVolume','Tr1','Tr2','Tr3','Tr4','Veto','Ecal','Hcal','MuonDetector','SplitCal'])
# Plot the field example
#fieldMaker.plotField(1, ROOT.TVector3(-9000.0, 6000.0, 50.0), ROOT.TVector3(-300.0, 300.0, 6.0), 'Bzx.png')
#fieldMaker.plotField(2, ROOT.TVector3(-9000.0, 6000.0, 50.0), ROOT.TVector3(-400.0, 400.0, 6.0), 'Bzy.png')
if inactivateMuonProcesses :
ROOT.gROOT.ProcessLine('#include "Geant4/G4ProcessTable.hh"')
mygMC = ROOT.TGeant4.GetMC()
mygMC.ProcessGeantCommand("/process/inactivate muPairProd")
mygMC.ProcessGeantCommand("/process/inactivate muBrems")
mygMC.ProcessGeantCommand("/process/inactivate muIoni")
mygMC.ProcessGeantCommand("/particle/select mu+")
mygMC.ProcessGeantCommand("/particle/process/dump")
gProcessTable = ROOT.G4ProcessTable.GetProcessTable()
procmu = gProcessTable.FindProcess(ROOT.G4String('muIoni'),ROOT.G4String('mu+'))
procmu.SetVerboseLevel(2)
# -----Start run----------------------------------------------------
run.Run(nEvents)
# -----Runtime database---------------------------------------------
kParameterMerged = ROOT.kTRUE
parOut = ROOT.FairParRootFileIo(kParameterMerged)
parOut.open(parFile)
rtdb.setOutput(parOut)
rtdb.saveOutput()
rtdb.printParamContexts()
getattr(rtdb,"print")()
# ------------------------------------------------------------------------
run.CreateGeometryFile("%s/geofile_full.%s.root" % (outputDir, tag))
# save ShipGeo dictionary in geofile
import saveBasicParameters
saveBasicParameters.execute("%s/geofile_full.%s.root" % (outputDir, tag),ship_geo)
# checking for overlaps
if checking4overlaps:
fGeo = ROOT.gGeoManager
fGeo.SetNmeshPoints(10000)
fGeo.CheckOverlaps(0.1) # 1 micron takes 5minutes
fGeo.PrintOverlaps()
# check subsystems in more detail
for x in fGeo.GetTopNode().GetNodes():
x.CheckOverlaps(0.0001)
fGeo.PrintOverlaps()
# -----Finish-------------------------------------------------------
timer.Stop()
rtime = timer.RealTime()
ctime = timer.CpuTime()
print ' '
print "Macro finished succesfully."
if "P8gen" in globals() :
if (HNL): print "number of retries, events without HNL ",P8gen.nrOfRetries()
elif (DarkPhoton):
print "number of retries, events without Dark Photons ",P8gen.nrOfRetries()
print "total number of dark photons (including multiple meson decays per single collision) ",P8gen.nrOfDP()
print "Output file is ", outFile
print "Parameter file is ", parFile
print "Real time ",rtime, " s, CPU time ",ctime,"s"
# remove empty events
if simEngine == "MuonBack":
tmpFile = outFile+"tmp"
fin = ROOT.gROOT.GetListOfFiles()[0]
t = fin.cbmsim
fout = ROOT.TFile(tmpFile,'recreate')
sTree = t.CloneTree(0)
nEvents = 0
pointContainers = []
for x in sTree.GetListOfBranches():
name = x.GetName()
if not name.find('Point')<0: pointContainers.append('sTree.'+name+'.GetEntries()') # makes use of convention that all sensitive detectors fill XXXPoint containers
for n in range(t.GetEntries()):
rc = t.GetEvent(n)
empty = True
for x in pointContainers:
if eval(x)>0: empty = False
if not empty:
rc = sTree.Fill()
nEvents+=1
sTree.AutoSave()
fout.Close()
print "removed empty events, left with:", nEvents
rc1 = os.system("rm "+outFile)
rc2 = os.system("mv "+tmpFile+" "+outFile)
fin.SetWritable(False) # bpyass flush error
# ------------------------------------------------------------------------
import checkMagFields
def visualizeMagFields():
checkMagFields.run()
def checkOverlapsWithGeant4():
# after /run/initialize, but prints warning messages, problems with TGeo volume
mygMC = ROOT.TGeant4.GetMC()
mygMC.ProcessGeantCommand("/geometry/test/recursion_start 0")
mygMC.ProcessGeantCommand("/geometry/test/recursion_depth 2")
mygMC.ProcessGeantCommand("/geometry/test/run")