Skip to content
Snippets Groups Projects
Commit f2e33a31 authored by Anders Markvardsen's avatar Anders Markvardsen
Browse files

Merge remote-tracking branch 'origin/feature/6742_maps_d4window'

parents 81ebfb2a b75be43c
No related merge requests found
#
# TUBE CALIBRATION DEMONSTRATION PROGRAM FOR MAPS - Execute this
#
# Here we run the calibration of a selected part of MAPS
#
from mantid.api import WorkspaceFactory # For table worskspace of calibrations
from tube_calib_fit_params import * # To handle fit parameters
from ideal_tube import * # For ideal tube
from tube_calib import * # For tube calibration functions
from tube_spec import * # For tube specification class
# == Set parameters for calibration ==
path = r"C:/Temp/" # Path name of folder containing input and output files
filename = 'MAPS14919.raw' # Name of calibration run
rangeLower = 2000 # Integrate counts in each spectra from rangeLower to rangeUpper
rangeUpper = 10000 #
# Set initial parameters for peak finding
ExpectedHeight = -1000.0 # Expected Height of Peaks (initial value of fit parameter)
ExpectedWidth = 8.0 # Expected width of centre peak (initial value of fit parameter)
ExpectedPositions = [4.0, 85.0, 128.0, 165.0, 252.0] # Expected positions of the edges and peak (initial values of fit parameters)
# Set what we want to calibrate (e.g whole intrument or one door )
CalibratedComponent = 'D4_window' # Calibrate D4 window
# Get calibration raw file and integrate it
rawCalibInstWS = Load(path+filename) #'raw' in 'rawCalibInstWS' means unintegrated.
print "Integrating Workspace"
CalibInstWS = Integration( rawCalibInstWS, RangeLower=rangeLower, RangeUpper=rangeUpper )
DeleteWorkspace(rawCalibInstWS)
print "Created workspace (CalibInstWS) with integrated data from run and instrument to calibrate"
# == Create Objects needed for calibration ==
#Create Calibration Table
calibrationTable = CreateEmptyTableWorkspace(OutputWorkspace="CalibTable")
calibrationTable.addColumn(type="int",name="Detector ID") # "Detector ID" column required by ApplyCalbration
calibrationTable.addColumn(type="V3D",name="Detector Position") # "Detector Position" column required by ApplyCalbration
# Specify component to calibrate
thisTubeSet = TubeSpec(CalibInstWS)
thisTubeSet.setTubeSpecByString(CalibratedComponent)
# Get ideal tube
iTube = IdealTube()
# The positions of the shadows and ends here are an intelligent guess.
iTube.setPositionsAndForm([-0.65,-0.22,-0.00, 0.22, 0.65 ],[2,1,1,1,2])
# Get fitting parameters
fitPar = TubeCalibFitParams( ExpectedPositions, ExpectedHeight, ExpectedWidth )
print "Created objects needed for calibration."
# == Get the calibration and put results into calibration table ==
# also put peaks into PeakFile
getCalibration( CalibInstWS, thisTubeSet, calibrationTable, fitPar, iTube, PeakFile=path+'TubeDemoMaps01.txt' )
print "Got calibration (new positions of detectors) "
# == Apply the Calibation ==
ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
print "Applied calibration"
# == Save workspace ==
SaveNexusProcessed( CalibInstWS, path+'TubeCalibDemoMapsResult.nxs',"Result of Running TCDemoMaps.py")
print "saved calibrated workspace (CalibInstWS) into Nexus file TubeCalibDemoMapsResult.nxs"
...@@ -109,7 +109,7 @@ class TubeSpec: ...@@ -109,7 +109,7 @@ class TubeSpec:
return self.component return self.component
# We look for the component # We look for the component
#print self.specString, print "Looking for", self.specString,
comp = self.inst.getComponentByName(self.specString) comp = self.inst.getComponentByName(self.specString)
...@@ -126,27 +126,41 @@ class TubeSpec: ...@@ -126,27 +126,41 @@ class TubeSpec:
@param tubeIx: index of Tube in specified set @param tubeIx: index of Tube in specified set
@Return value: ID of first detector and number of detectors @Return value: ID of first detector, number of detectors and step between detectors +1 or -1
""" """
nTubes = self.getNumTubes() nTubes = self.getNumTubes()
if(nTubes < 0): if(nTubes < 0):
print "Error in listing tubes" print "Error in listing tubes"
return 0, 0 return 0, 0, 1
if(tubeIx < 0 or tubeIx >= nTubes): if(tubeIx < 0 or tubeIx >= nTubes):
print "Tube index",tubeIx,"out of range 0 to",nTubes print "Tube index",tubeIx,"out of range 0 to",nTubes
return 0, 0 return 0, 0, 1
comp = self.tubes[tubeIx] comp = self.tubes[tubeIx]
if(comp != 0): if(comp != 0):
firstDet = comp[0].getID() firstDet = comp[0].getID()
numDet = comp.nelements() numDet = comp.nelements()
# Allow for reverse numbering of Detectors
lastDet = comp[numDet-1].getID()
if (lastDet < firstDet):
step = -1
if( firstDet - lastDet + 1 != numDet):
print "Detector number range",firstDet-lastDet+1," not equal to number of detectors",numDet
print "Detectors not numbered continuously in this tube. Calibration will fail for this tube."
else:
step = 1
if( lastDet - firstDet + 1 != numDet):
print "Detector number range",lastDet-firstDet+1," not equal to number of detectors",numDet
print "Detectors not numbered continuously in this tube. Calibration will fail for this tube."
#print "First dectector ", firstDet," Last detector ", firstDet+numDet-1, "Number of detectors ", numDet #print "First dectector ", firstDet," Last detector ", firstDet+numDet-1, "Number of detectors ", numDet
#print "First dectector ", firstDet," Last detector ", comp[numDet-1].getID()
else: else:
print self.specString, tubeIx, "not found" print self.specString, tubeIx, "not found"
return 0, 0 return 0, 0, 1
return firstDet, numDet return firstDet, numDet, step
def getTubeLength( self, tubeIx ): def getTubeLength( self, tubeIx ):
""" """
...@@ -204,21 +218,21 @@ class TubeSpec: ...@@ -204,21 +218,21 @@ class TubeSpec:
def getTubeByString(self, tubeIx): def getTubeByString(self, tubeIx):
""" """
Returns list of workspace indices of a tube specified by string (may be set of tubes) Returns list of workspace indices of a tube set that has been specified by string
It assumes that all the pixels along the tube have consecutive detector IDs and It assumes that all the pixels along the tube have consecutive detector IDs
the tube runs parallel to the Y-axis.
@param tubeIx: index of Tube in specified set @param tubeIx: index of Tube in specified set
Return value: list of indices Return value: list of indices
""" """
firstDet, numDet = self.getDetectorInfoFromTube( tubeIx ) firstDet, numDet, step = self.getDetectorInfoFromTube( tubeIx )
wkIds = [] wkIds = []
#print " First dectector", firstDet," Last detector", firstDet+numDet-1, "Number of detectors", numDet # print " First dectector", firstDet," Last detector", firstDet+numDet-1, "Number of detectors", numDet
#print "Histograms", self.ws.getNumberHistograms() # print "Histograms", self.ws.getNumberHistograms()
# First check we have one detector per histogram/workpsaceID/spectrum # First check we have one detector per histogram/workpsaceID/spectrum
sp = self.ws.getSpectrum(10) sampleIndex = 10
sp = self.ws.getSpectrum(sampleIndex)
detids = sp.getDetectorIDs() detids = sp.getDetectorIDs()
numDetsPerWkID = len(detids) numDetsPerWkID = len(detids)
if( numDetsPerWkID != 1): if( numDetsPerWkID != 1):
...@@ -228,14 +242,18 @@ class TubeSpec: ...@@ -228,14 +242,18 @@ class TubeSpec:
# Go and get workspace Indices # Go and get workspace Indices
if(self.continuousIndicesInTube): if(self.continuousIndicesInTube):
if(step == -1):
startDet = firstDet - numDet + 1
else:
startDet = firstDet
if( numDet > 0): if( numDet > 0):
for i in range (0, self.ws.getNumberHistograms(), numDet): for i in range (0, self.ws.getNumberHistograms(), numDet):
deti = self.ws.getDetector(i) deti = self.ws.getDetector(i)
detID = deti.getID() detID = deti.getID()
if (detID >= firstDet and detID < firstDet+numDet): if (detID >= startDet and detID < startDet+numDet):
iPixel = detID - firstDet iPixel = detID - firstDet
wkIds = range( i - iPixel, i - iPixel + numDet) wkIds = range( i - iPixel, i - iPixel + step*numDet, step)
#print "Workspace indices",i-iPixel,"to",i-iPixel+numDet-1 # print "Workspace indices",i-iPixel,"to",i-iPixel+numDet-1
else: #We can't assume continuous indices within tube, must loop over all indices (there are many). else: #We can't assume continuous indices within tube, must loop over all indices (there are many).
if( numDet > 0): if( numDet > 0):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment