diff --git a/Code/Mantid/scripts/Calibration/Examples/TubeCalibDemoMaps_D4.py b/Code/Mantid/scripts/Calibration/Examples/TubeCalibDemoMaps_D4.py new file mode 100644 index 0000000000000000000000000000000000000000..41b08d81b11a1596ce496ad82d52a69b97772375 --- /dev/null +++ b/Code/Mantid/scripts/Calibration/Examples/TubeCalibDemoMaps_D4.py @@ -0,0 +1,72 @@ +# +# 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" + diff --git a/Code/Mantid/scripts/Calibration/tube_spec.py b/Code/Mantid/scripts/Calibration/tube_spec.py index 34cb271202a60aa6df32a0419d009630d9fcc438..07027dbabc800c6d43d89b2c2b8fa243f902cb6b 100644 --- a/Code/Mantid/scripts/Calibration/tube_spec.py +++ b/Code/Mantid/scripts/Calibration/tube_spec.py @@ -109,7 +109,7 @@ class TubeSpec: return self.component # We look for the component - #print self.specString, + print "Looking for", self.specString, comp = self.inst.getComponentByName(self.specString) @@ -126,27 +126,41 @@ class TubeSpec: @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() if(nTubes < 0): print "Error in listing tubes" - return 0, 0 + return 0, 0, 1 if(tubeIx < 0 or tubeIx >= nTubes): print "Tube index",tubeIx,"out of range 0 to",nTubes - return 0, 0 + return 0, 0, 1 comp = self.tubes[tubeIx] if(comp != 0): firstDet = comp[0].getID() 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 ", comp[numDet-1].getID() else: print self.specString, tubeIx, "not found" - return 0, 0 + return 0, 0, 1 - return firstDet, numDet + return firstDet, numDet, step def getTubeLength( self, tubeIx ): """ @@ -204,21 +218,21 @@ class TubeSpec: def getTubeByString(self, tubeIx): """ - Returns list of workspace indices of a tube specified by string (may be set of tubes) - It assumes that all the pixels along the tube have consecutive detector IDs and - the tube runs parallel to the Y-axis. + 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 @param tubeIx: index of Tube in specified set Return value: list of indices """ - firstDet, numDet = self.getDetectorInfoFromTube( tubeIx ) + firstDet, numDet, step = self.getDetectorInfoFromTube( tubeIx ) wkIds = [] - #print " First dectector", firstDet," Last detector", firstDet+numDet-1, "Number of detectors", numDet - #print "Histograms", self.ws.getNumberHistograms() + # print " First dectector", firstDet," Last detector", firstDet+numDet-1, "Number of detectors", numDet + # print "Histograms", self.ws.getNumberHistograms() # 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() numDetsPerWkID = len(detids) if( numDetsPerWkID != 1): @@ -228,14 +242,18 @@ class TubeSpec: # Go and get workspace Indices if(self.continuousIndicesInTube): + if(step == -1): + startDet = firstDet - numDet + 1 + else: + startDet = firstDet if( numDet > 0): for i in range (0, self.ws.getNumberHistograms(), numDet): deti = self.ws.getDetector(i) detID = deti.getID() - if (detID >= firstDet and detID < firstDet+numDet): + if (detID >= startDet and detID < startDet+numDet): iPixel = detID - firstDet - wkIds = range( i - iPixel, i - iPixel + numDet) - #print "Workspace indices",i-iPixel,"to",i-iPixel+numDet-1 + wkIds = range( i - iPixel, i - iPixel + step*numDet, step) + # 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). if( numDet > 0):