FairShip
Loading...
Searching...
No Matches
getGeoInformation.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
5# prints z-coordinators of SHiP detector volumes
6# WARNING: printing the entire geometry takes a lot of time
7# 24-02-2015 comments to EvH
8
9import operator
10from argparse import ArgumentParser
11from array import array
12
13import ROOT
14
15
17 Info = {}
18 nav = ROOT.gGeoManager.GetCurrentNavigator()
19 nav.cd(n)
20 Info["node"] = nav.GetCurrentNode()
21 Info["path"] = n
22 tmp = Info["node"].GetVolume().GetShape()
23 Info["material"] = Info["node"].GetVolume().GetMaterial().GetName()
24 if options.moreInfo:
25 x = ROOT.gGeoManager.GetVerboseLevel()
26 ROOT.gGeoManager.SetVerboseLevel(0)
27 Info["weight"] = Info["node"].GetVolume().Weight() # kg
28 Info["cubicmeter"] = Info["node"].GetVolume().Capacity() / 1000000.0 #
29 ROOT.gGeoManager.SetVerboseLevel(x)
30 o = [tmp.GetOrigin()[0], tmp.GetOrigin()[1], tmp.GetOrigin()[2]]
31 Info["locorign"] = o
32 local = array("d", o)
33 globOrigin = array("d", [0, 0, 0])
34 nav.LocalToMaster(local, globOrigin)
35 Info["origin"] = globOrigin
36 shifts = [
37 [-tmp.GetDX() + o[0], o[1], o[2]],
38 [tmp.GetDX() + o[0], o[1], o[2]],
39 [o[0], -tmp.GetDY() + o[1], o[2]],
40 [o[0], tmp.GetDY() + o[1], o[2]],
41 [o[0], o[1], -tmp.GetDZ() + o[2]],
42 [o[0], o[1], tmp.GetDZ() + o[2]],
43 ]
44 shifted = []
45 for s in shifts:
46 local = array("d", s)
47 glob = array("d", [0, 0, 0])
48 nav.LocalToMaster(local, glob)
49 shifted.append([glob[0], glob[1], glob[2]])
50 Info["boundingbox"] = {}
51 for j in range(3):
52 jmin = 1e30
53 jmax = -1e30
54 for s in shifted:
55 if s[j] < jmin:
56 jmin = s[j]
57 if s[j] > jmax:
58 jmax = s[j]
59 Info["boundingbox"][j] = [jmin, jmax]
60 return Info
61
62
63def print_info(path: str, node, level: int, currentlevel: int, print_sub_det_info=False) -> None:
64 sub_nodes = {}
65 fullInfo = {}
66 for subnode in node.GetNodes():
67 name = subnode.GetName()
68 fullInfo[name] = local2Global(path + "/" + name)
69 sub_nodes[name] = fullInfo[name]["origin"][2]
70
71 for name, _ in sorted(sub_nodes.items(), key=operator.itemgetter(1)):
72 boundingbox = fullInfo[name]["boundingbox"]
73
74 format_string = (
75 "{:<28s}: z={:10.4F}cm dZ={:10.4F}cm [{:10.4F} {:10.4F}]"
76 + " dx={:10.4F}cm [{:10.4F} {:10.4F}] dy={:10.4F}cm [{:10.4F} {:10.4F}] {:>20s}"
77 )
78
79 format_variable = [
80 " " * int(currentlevel) + name,
81 fullInfo[name]["origin"][2],
82 abs(boundingbox[2][0] - boundingbox[2][1]) / 2.0,
83 boundingbox[2][0],
84 boundingbox[2][1],
85 abs(boundingbox[0][0] - boundingbox[0][1]) / 2.0,
86 boundingbox[0][0],
87 boundingbox[0][1],
88 abs(boundingbox[1][0] - boundingbox[1][1]) / 2.0,
89 boundingbox[1][0],
90 boundingbox[1][1],
91 fullInfo[name]["material"],
92 ]
93
94 if options.moreInfo:
95 cubicmeter = fullInfo[name]["cubicmeter"]
96 weight = fullInfo[name]["weight"]
97 format_string += " {:10.1F}kg {:10.1F}m3"
98 format_variable.extend([weight, cubicmeter])
99
100 print(format_string.format(*format_variable))
101
102 if options.volume in ["", name]:
103 print_sub_det_info = True
104
105 if print_sub_det_info and currentlevel < level and fullInfo[name]["node"].GetNodes():
106 print_info(fullInfo[name]["path"], fullInfo[name]["node"], level, currentlevel + 1, print_sub_det_info)
107
108 if currentlevel == 0:
109 print_sub_det_info = False
110
111
112parser = ArgumentParser()
113parser.add_argument("-g", "--geometry", help="Input geometry file", required=True)
114parser.add_argument("-l", "--level", help="Max subnode level", default=0)
115parser.add_argument("-v", "--volume", help="Name of node to expand", default="")
116parser.add_argument("-X", "--moreInfo", help="Print weight and capacity", action="store_true")
117
118options = parser.parse_args()
119fname = options.geometry
120fgeom = ROOT.TFile.Open(fname)
121fGeo = fgeom.Get("FAIRGeom")
122top = fGeo.GetTopVolume()
123
124
125if options.moreInfo:
126 print(
127 " Detector element z(midpoint) halflength volume-start volume-end dx"
128 " x-start x-end dy y-start y-end material weight capacity"
129 )
130else:
131 print(
132 " Detector element z(midpoint) halflength volume-start volume-end dx"
133 " x-start x-end dy y-start y-end material"
134 )
135
136currentlevel = 0
137print_info("", top, int(options.level), currentlevel)
None print_info(str path, node, int level, int currentlevel, print_sub_det_info=False)