17 top = fGeo.GetTopVolume()
18 listOfVolumes = [top.GetName()]
19 findNode(top,listOfVolumes)
22 for v
in fGeo.GetListOfVolumes():
24 if not name
in listOfVolumes:
26 if not name
in gIndex: gIndex[name]=[]
27 gIndex[name].append(v.GetNumber())
28 print(
"list of orphan volumes:",orphan)
31 if len(gIndex[x])>1: vSame[x]=len(gIndex[x])
32 print(
"list of volumes with same name",vSame)
35 print(
'setMagnetField() called. Out of date, does not set field for tau neutrino detector!')
36 fGeo = ROOT.gGeoManager
37 vols = fGeo.GetListOfVolumes()
42 if not field:
continue
43 bx = field.GetFieldValue()[0]/u.tesla*G4Unit.tesla
44 by = field.GetFieldValue()[1]/u.tesla*G4Unit.tesla
45 bz = field.GetFieldValue()[2]/u.tesla*G4Unit.tesla
46 magFieldIron = G4UniformMagField(G4ThreeVector(bx,by,bz))
47 FieldIronMgr = G4FieldManager(magFieldIron)
48 FieldIronMgr.CreateChordFinder(magFieldIron)
49 listOfFields[v.GetName()]=FieldIronMgr
50 gt = ROOT.G4TransportationManager.GetTransportationManager()
51 gn = gt.GetNavigatorForTracking()
52 world = gn.GetWorldVolume().GetLogicalVolume()
54 for da
in range(world.GetNoDaughters()):
55 vl0 = world.GetDaughter(da)
56 vln = vl0.GetName().c_str()
57 lvl0 = vl0.GetLogicalVolume()
58 if vln
in listOfFields : setField[lvl0]=vln
59 for dda
in range(lvl0.GetNoDaughters()):
60 vl = lvl0.GetDaughter(dda)
61 vln = vl.GetName().c_str()
62 lvl = vl.GetLogicalVolume()
63 if vln
in listOfFields : setField[lvl]=vln
67 fm = lvl.GetFieldManager()
68 if not fm.DoesFieldExist():
69 lvl.SetFieldManager(listOfFields[setField[lvl]],
True)
72 constField = listOfFields[setField[lvl]].GetDetectorField().GetConstantFieldValue()
73 print(
'set field for ',setField[lvl], constField)
76 print(
'field already set:',setField[lvl])
78 g4Run = G4RunManager.GetRunManager()
79 g4Run.GeometryHasBeenModified(
True)
81def printWF(vl,alreadyPrinted,onlyWithField=True):
83 vname = vl.GetName().data()
84 if vname
in alreadyPrinted:
return magnetMass
85 vln = vname+
' '+str(vl.GetCopyNo())
86 mvl = vl.GetMotherLogical().GetName().data()
87 alreadyPrinted[vname]=mvl
88 if mvl !=
'cave': vln = mvl+
'/'+vln
89 lvl = vl.GetLogicalVolume()
90 cvol = lvl.GetSolid().GetCubicVolume()/G4Unit.m3
91 M = lvl.GetMass()/G4Unit.kg
92 fm = lvl.GetFieldManager()
93 if not fm
and onlyWithField:
return magnetMass
94 if M < 5000.: print(
'%-35s volume = %5.2Fm3 mass = %5.2F kg'%(vln,cvol,M))
95 else: print(
'%-35s volume = %5.2Fm3 mass = %5.2F t'%(vln,cvol,M/1000.))
97 fi = fm.GetDetectorField()
98 if hasattr(fi,
'GetConstantFieldValue'):
99 print(
' Magnetic field:',fi.GetConstantFieldValue()/G4Unit.tesla)
101 serv = ROOT.TG4GeometryServices.Instance()
102 pos = array(
'd',[0,0,0])
103 bf = array(
'd',[0,0,0])
104 name = ROOT.G4String(lvl.GetName().c_str())
105 print (
'debug',name,lvl.GetName(),lvl)
106 serv.GetField(name,pos,bf)
107 print(
' Magnetic field Bx,By,Bz: %4.2F %4.2F %4.2F'%(bf[0]/G4Unit.tesla,bf[1]/G4Unit.tesla,bf[2]/G4Unit.tesla))
109 name = vl.GetName().c_str()
110 if "_" in name
and "Mag" in name.split(
'_')[1]: magnetMass = M
112def nextLevel(lv,magnetMass,onlyWithField,exclude,alreadyPrinted):
114 for da
in range(lv.GetNoDaughters()):
115 lvn = lv.GetDaughter(da)
116 name = lvn.GetName().c_str()
117 if name
in exclude:
continue
118 lvln = lvn.GetLogicalVolume()
119 if lvln.GetNoDaughters()>0:
120 xtmp,dummy =
nextLevel(lvln,magnetMass,onlyWithField,exclude,alreadyPrinted)
123 tmp+=
printWF(lvn,alreadyPrinted,onlyWithField)
124 return tmp,magnetMass
127 print(
"will not search in ",exclude)
128 gt = ROOT.G4TransportationManager.GetTransportationManager()
129 gn = gt.GetNavigatorForTracking()
130 world = gn.GetWorldVolume().GetLogicalVolume()
133 dummy,nM =
nextLevel(world,magnetMass,onlyWithField,exclude,alreadyPrinted)
134 print(
'total magnet mass',nM/1000.,
't')
137def addVMCFields(shipGeo, controlFile = '', verbose = False, withVirtualMC = True):
139 Define VMC B fields, e.g. global field, field maps, local or local+global fields
141 print(
'Calling addVMCFields')
143 fieldMaker = ROOT.ShipFieldMaker(verbose)
147 if controlFile !=
'':
148 fieldMaker.readInputFile(controlFile)
151 if hasattr(shipGeo,
'Bfield'):
153 fieldMaker.defineFieldMap(
'MainSpecMap',
'files/MainSpectrometerField.root',
154 ROOT.TVector3(0.0, 0.0, shipGeo.Bfield.z))
155 fieldsList.append(
'MainSpecMap')
157 withConstFieldNuTauDet =
False
158 if hasattr(shipGeo.EmuMagnet,
'WithConstField'): withConstFieldNuTauDet = shipGeo.EmuMagnet.WithConstField
159 if not withConstFieldNuTauDet:
160 fieldMaker.defineFieldMap(
'NuMap',
'files/nuTauDetField.root', ROOT.TVector3(0.0,0.0,shipGeo.EmuMagnet.zC))
161 fieldsList.append(
'NuMap')
163 if not shipGeo.hadronAbsorber.WithConstField:
164 fieldMaker.defineFieldMap(
'HadronAbsorberMap',
'files/FieldHadronStopper_raised_20190411.root', ROOT.TVector3(0.0,0.0,shipGeo.hadronAbsorber.z))
165 fieldsList.append(
'HadronAbsorberMap')
167 if not shipGeo.muShield.WithConstField:
169 fieldMaker.defineFieldMap(
'muonShieldField',
'files/MuonShieldField.root',
170 ROOT.TVector3(0.0, 0.0, field_center), ROOT.TVector3(0.0, 0.0, 0.0),
True)
171 fieldsList.append(
'muonShieldField')
173 if len(fieldsList) > 1:
174 fieldMaker.defineComposite(
'TotalField', *fieldsList)
175 fieldMaker.defineGlobalField(
'TotalField')
177 fieldMaker.defineGlobalField(
'MainSpecMap')
181 geom = ROOT.TG4GeometryManager.Instance()
183 geom.SetUserPostDetConstruction(fieldMaker)
185 geom.ConstructSDandField()
194 Method to print out information about VMC fields
196 print(
'Printing VMC fields and associated volumes')
198 fGeo = ROOT.gGeoManager
199 vols = fGeo.GetListOfVolumes()
205 print(
'Vol is {0}, field is {1}'.format(v.GetName(), field))
207 print(
'Vol is {0}'.format(v.GetName()))
212 centre = array(
'd',[0.0, 0.0, 0.0])
213 B = array(
'd',[0.0, 0.0, 0.0])
214 field.Field(centre, B)
215 print(
'Volume {0} has B = ({1}, {2}, {3}) T'.format(v.GetName(), B[0]/u.tesla,
216 B[1]/u.tesla, B[2]/u.tesla))
224 gt = ROOT.G4TransportationManager.GetTransportationManager()
225 gn = gt.GetNavigatorForTracking()
226 world = gn.GetWorldVolume().GetLogicalVolume()
228 for da
in range(world.GetNoDaughters()):
229 vl = world.GetDaughter(da)
230 vmap[vl.GetName().c_str()] = vl
231 print(da, vl.GetName())
232 lvl = vmap[
'MagB'].GetLogicalVolume()
233 print(lvl.GetMass()/G4Unit.kg,lvl.GetMaterial().GetName())
234 print(lvl.GetFieldManager())
236 for da
in range(world.GetNoDaughters()):
237 vl = world.GetDaughter(da)
238 vln = vl.GetName().c_str()
239 lvl = vl.GetLogicalVolume()
240 fm = lvl.GetFieldManager()
242 v = fm.GetDetectorField().GetConstantFieldValue()
243 print(vln,fm,v.getX(),v.getY())
245 fgeom = ROOT.gGeoManager
246 magB = fgeom.GetVolume(
'MagB')
248 print(fl.GetFieldValue()[0],fl.GetFieldValue()[1],fl.GetFieldValue()[2])