-
Notifications
You must be signed in to change notification settings - Fork 4
/
hb2a_geometry.py
161 lines (123 loc) · 4.66 KB
/
hb2a_geometry.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
################################################################################
#
# A script to generate (HFIR) HB2A geometry
#
################################################################################
#!/usr/bin/python
import sys
import helper as helper
# definition of important parameters of HB2A
HB2AParam = {
"moderator_distance": 2.0,
"sample_position": [0., 0., 0.],
}
NUM_HB2A_DETS = 44
INST_NAME = 'HB2A'
def importGapFile(gapfilename):
""" Import detector gap file from a file
The gap file is a column file.
A new column is added as a new calibration is made on detectors' gaps.
The right most column is always taken as the detectors' gaps to import.
"""
# import file
try:
gfile = open(gapfilename, "r")
lines = gfile.readlines()
gfile.close()
except IOError as e:
print "Unable to open or read file %s." % (gapfilename)
raise e
# parse file
gapdict = {}
idetgap = 1
for line in lines:
line = line.strip()
if len(line) == 0:
continue
terms = line.split()
try:
tmpgap = float(terms[-1])
tmpdetname = 'anode%d' % (idetgap)
gapdict[tmpdetname] = tmpgap
idetgap += 1
except ValueError as e:
print e
# ENDFOR (line)
if len(gapdict.keys()) != NUM_HB2A_DETS:
raise NotImplementedError("The number of gaps is %d. It is not correct for HB2A which has %d detectors."
% (len(gapdict.keys()), NUM_HB2A_DETS))
return gapdict
def main(argv):
""" Main
"""
if len(argv) != 1 and len(argv) != 3:
print "Create HB2A IDF. Run as: %s [IDF file name] [Gap file name]" % (
argv[0])
exit(2)
if len(argv) == 3:
outidfname = argv[1]
gapfilename = argv[2]
else:
outidfname = INST_NAME+"_Definition.xml"
gapfilename = 'HFIR/HB2A_exp0379__gaps.txt'
# import detector gap (delta-2theta) file
gapdict = importGapFile(gapfilename)
# initialize MantidGeom object
instname = "HB2A"
comment = "Created by Wenduo Zhou"
valid_from = "2015-01-22 00:00:00"
hb2a = helper.MantidGeom(instname, comment, valid_from)
hb2a.addComment("SOURCE AND SAMPLE POSITION")
# add source/moderator
hb2a.addModerator(HB2AParam["moderator_distance"])
# add sample
hb2a.addSamplePosition(location=HB2AParam["sample_position"])
# add detector idlist
hb2a.addComment("Detector list def")
hb2a.addDetectorIds(idname="detectors", idlist=[1,44,1])
hb2a.addComponent(type_name="detectors", idlist="detectors")
# detector banks
hb2a.addComment("Detector Banks")
locationdict = {
"r_position": 0,
"p_position": 0
}
typeattrib = {
"component": "bank_uniq"
}
el = hb2a.makeTypeElement(name="detectors") #, extra_attrs=typeattrib)
el_bank = hb2a.makeDetectorElement(name="bank_uniq", root=el)
hb2a.addLocationRTP(root=el_bank, r='0.', t='rotangle rotangle+0.0', p='0.', rot_x='0.', rot_y='rotangle rotangle+0.0', rot_z='0.')
# add detectors
hb2a.addComment("Definition of the unique existent bank (made of tubes)")
bankattrib = {
"component": "standard_tube"
}
el_dets = hb2a.makeTypeElement(name="bank_uniq") #, extra_attrs=bankattrib)
el_tube = hb2a.makeDetectorElement(name="standard_tube", root=el_dets)
twotheta = 0.0
for i in xrange(1, 45):
pixel_id = "anode%d" % (i)
twotheta += gapdict[pixel_id]
hb2a.addLocationPolar(el_tube, r='2.00', theta=str(twotheta), phi='0.0', name='tube_%d'%(i))
# add single detector/pixel information
hb2a.addComment("Definition of standard_tube")
tubedict = {"outline": "yes"}
el_tube = hb2a.makeTypeElement(name='standard_tube', extra_attrs=tubedict)
el_pixel = hb2a.makeDetectorElement(name='standard_pixel', root=el_tube)
hb2a.addLocation(el_pixel, x=0, y=0, z=0)
# add standard_pixel
hb2a.addCylinderPixel(name="standard_pixel", center_bottom_base=[0.0,0.0,0.0], axis=[0.,1.,0.],
pixel_radius=0.00127, pixel_height=0.0114341328125, algebra='shape')
"""
pixdict = {"is": "detector"}
el_pixel = hb2a.makeTypeElement(name="standard_pixel", extra_attrs=pixdict)
hb2a.makeCylinderPixel(root=el_pixel, center_bottom_base=[0.0,0.0,0.0], axis=[0.,0.,0.],
pixel_radius=0.00127, pixel_height=0.0114341328125, algebra='shape')
"""
# write geometry
hb2a.showGeom()
hb2a.writeGeom(outidfname)
return
if __name__ == "__main__":
main(sys.argv)