Ticket #7491: testingMerlinCalibration.py

File testingMerlinCalibration.py, 7.1 KB (added by Gesner Passos, 7 years ago)
Line 
1import numpy
2from mantid.simpleapi import *
3import tube
4
5
6
7filename = 'MER12024.raw' # Calibration run ( found in \\isis\inst$\NDXMERLIN\Instrument\data\cycle_11_5 )
8
9# == Set parameters for calibration ==
10
11rangeLower = 3000 # Integrate counts in each spectra from rangeLower to rangeUpper
12rangeUpper = 20000 #
13
14# Get calibration raw file and integrate it   
15rawCalibInstWS = LoadRaw(filename)  #'raw' in 'rawCalibInstWS' means unintegrated.
16print "Integrating Workspace"
17CalibInstWS = Integration( rawCalibInstWS, RangeLower=rangeLower, RangeUpper=rangeUpper )
18DeleteWorkspace(rawCalibInstWS)
19print "Created workspace (CalibInstWS) with integrated data from run and instrument to calibrate" 
20
21# the known positions are given in pixels inside the tubes and transformed to provide the positions
22# with the center of the tube as the origin
23knownPositions = 2.92713867188*(numpy.array([ 27.30074322, 92.5,  294.65178585,  362.37861919 , 512.77103043  ,663.41425323, 798.3223896,   930.9, 997.08480835])/1024 - 0.5)
24funcForm = numpy.array([2,2,1,1,1,1,1,2,2],numpy.int8)
25# The calibration will follow different steps for sets of tubes
26
27# For the door9, the best points to define the known positions are the 1st edge, 5 peaks, last edge.
28points7 = knownPositions[[0,2,3,4,5,6,8]]
29points7func = funcForm[[0,2,3,4,5,6,8]]
30
31door9pos = points7
32door9func = points7func
33CalibratedComponent = 'MERLIN/door9'  # door9
34# == Get the calibration and put results into calibration table ==
35# also put peaks into PeakFile
36calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, door9pos, door9func, 
37    outputPeak=True,
38    margin=30,
39    rangeList=range(20) # because 20, 21, 22, 23 are defective detectors
40    )   
41ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
42####################################
43# Pause and see the instrument check
44####################################
45
46
47
48# For the door8, the best points to define the known positions are the 1st edge, 5 peaks, last_edge
49door8pos = points7
50door8func = points7func
51CalibratedComponent = 'MERLIN/door8'
52calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, door8pos, 
53    door8func,
54    outputPeak = True, #change to peakTable to append to peakTable
55    margin = 30)
56analisePeakTable(peakTable, 'door8_peaks')     
57
58# For the doors 7,6,5,4, 2, 1 we may use the 9 points
59doorpos = knownPositions
60doorfunc = funcForm
61CalibratedComponent = ['MERLIN/door%d'%(i) for i in [7,6,5,4, 2, 1]]
62calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, doorpos, 
63    doorfunc,
64    outputPeak = True, 
65    margin = 30)
66ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
67####################################
68# Pause and see the instrument check
69####################################
70
71
72
73# The door 3 is a special case, because it is composed by diffent kind of tubes.
74# door 3 tubes: 5_8, 5_7, 5_6, 5_5, 5_4, 5_3, 5_2, 5_1, 4_8, 4_7, 4_6, 4_5, 4_4, 4_3, 4_2, 4_1, 3_8, 3_7, 3_6, 3_5, 3_4
75# obeys the same rules as the doors 7, 6, 5, 4, 2, 1
76# For the tubes 3_3, 3_2, 3_1 -> it is better to skip the central peak
77# For the tubes 1_x (smaller tube below), it is better to take the final part of known positions: peak4,peak5,edge6,edge7
78# For the tubes 2_x (smaller tube above, it is better to take the first part of known positions: edge1, edge2, peak1,peak2
79
80# NOTE: the smaller tubes they have length = 1.22879882813, but 1024 detectors
81# so we have to correct the known positiosn by multiplying by its lenght and dividing by the longer dimension
82
83from tube_calib_fit_params import TubeCalibFitParams
84
85# calibrating tubes 1_x
86CalibratedComponent = ['MERLIN/door3/tube_1_%d'%(i) for i in range(1,9)]
87
88half_diff_center = (2.92713867188 -1.22879882813)/2  # difference among the expected center position for both tubes
89
90# here a little bit of attempts is necessary. The efective center position and lengh is different for the calibrated tube, that
91# is the reason, the calibrated values of the smaller tube does not seems aligned with the others. By, finding the 'best' half_diff_center
92# value, the alignment occurs nicely.
93half_diff_center = 0.835 #
94
95# the knownpositions were given with the center of the bigger tube as origin, to convert
96# to the center of the upper tube as origin is necessary to subtract them with  the half_diff_center
97doorpos = knownPositions[[5,6,7,8]] - half_diff_center
98doorfunc = [1,1,2,2]
99# for the smal tubes, automatically searching for the peak position in pixel was not working quite well,
100# so we will give the aproximate position for these tubes through fitPar argument
101fitPar = TubeCalibFitParams([216, 527, 826, 989])
102fitPar.setAutomatic(True)
103
104calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, doorpos, 
105    doorfunc,
106    outputPeak = True, 
107    fitPar = fitPar,
108    margin = 30)
109ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
110####################################
111# Pause and see the instrument check
112####################################
113
114
115# calibrating tubes 2_x
116CalibratedComponent = ['MERLIN/door3/tube_2_%d'%(i) for i in range(1,9)]
117# the knownpositions were given with the center of the bigger tube as origin, to convert
118# to the center of the lower tube as origin is necessary to sum them with  (len_big - len_small)/2
119doorpos = knownPositions[[0,1,2,3]] + half_diff_center
120# print doorpos
121doorfunc = [2,2,1,1]
122
123# for the smal tubes, automatically searching for the peak position in pixel was not working quite well,
124# so we will give the aproximate position for these tubes through fitPar argument
125fitPar = TubeCalibFitParams([50, 202, 664, 815])
126fitPar.setAutomatic(True)
127
128calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, doorpos, 
129    doorfunc,
130    outputPeak = True, 
131    fitPar = fitPar,
132    margin = 30)
133ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
134####################################
135# Pause and see the instrument check
136####################################
137
138
139# calibrating tubes 3_3,3_2,3_1
140CalibratedComponent = ['MERLIN/door3/tube_3_%d'%(i) for i in [1,2,3]]
141doorpos = knownPositions[[0,1,2,3,5,6,7,8]]
142doorfunc = funcForm[[0,1,2,3,5,6,7,8]]
143calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, doorpos, 
144    doorfunc,
145    outputPeak = True, 
146    margin = 30)
147ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
148####################################
149# Pause and see the instrument check
150####################################
151
152
153# calibrating others inside door3
154# 5_8, 5_7, 5_6, 5_5, 5_4, 5_3, 5_2, 5_1, 4_8, 4_7, 4_6, 4_5, 4_4, 4_3, 4_2, 4_1, 3_8, 3_7, 3_6, 3_5, 3_4
155part_3 = ['MERLIN/door3/tube_3_%d'%(i) for i in [4,5,6]]
156part_4 = ['MERLIN/door3/tube_4_%d'%(i) for i in range(1,9)]
157part_5 = ['MERLIN/door3/tube_5_%d'%(i) for i in range(1,9)]
158CalibratedComponent = part_3 + part_4 + part_5
159doorpos = knownPositions
160doorfunc = funcForm
161calibrationTable, peakTable = tube.calibrate(CalibInstWS, CalibratedComponent, doorpos, 
162    doorfunc,
163    outputPeak = True, 
164    margin = 30)
165ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
166####################################
167# Pause and see the instrument check
168####################################
169