Skip to content

Commit

Permalink
Applied changes to new examples from #6764 re #6752
Browse files Browse the repository at this point in the history
Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
  • Loading branch information
KarlPalmen committed Apr 2, 2013
1 parent 469b50b commit 3e83adf
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#
# TUBE CALIBRATION DEMONSTRATION PROGRAM FOR MAPS
#
# This is a simple example for running calibration for and calibration run of MAPS.
# It uses the CalibrateMaps function

#
from mantid.api import WorkspaceFactory # For table worskspace of calibrations
from mantid.kernel import config # To set default instrument to MAPS
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

def CalibrateMaps( RunNumber ):
'''
RunNumber is the run number for the calibration.
'''

# == Set parameters for calibration ==
previousDefaultInstrument = config['default.instrument']
config['default.instrument']="MAPS"
filename = str(RunNumber) # Name of calibration run
print "Filename",filename
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 Gaussian Peaks (initial value of fit parameter)
ExpectedWidth = 8.0 # Expected width of Gaussian peaks in pixels (initial value of fit parameter)
ExpectedPositions = [4.0, 85.0, 128.0, 165.0, 252.0] #Expected positions of the edges and Gaussian peaks in pixels (initial values of fit parameters)

# Set what we want to calibrate (e.g whole intrument or one door )
CalibratedComponent = 'MAPS' # Calibrate all


# Get calibration raw file and integrate it
rawCalibInstWS = Load(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()
# Set positions of where the shadows and ends should be.
# First array gives positions in Metres and second array gives type 1=Gaussian peak 2=edge.
# See http://www.mantidproject.org/IdealTube for details
# An intelligent guess is used here that is not correct for all tubes.
iTube.setPositionsAndForm([-0.50,-0.16,-0.00, 0.16, 0.50 ],[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 )
print "Got calibration (new positions of detectors) "

# == Apply the Calibation ==
ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
print "Applied calibration"


# == Save workspace ==
SaveNexusProcessed( CalibInstWS, 'TubeCalibDemoMapsResult.nxs',"Result of Running MAPS Calibration")
print "saved calibrated workspace (CalibInstWS) into Nexus file TubeCalibDemoMapsResult.nxs"

# == Reset dafault instrument ==
config['default.instrument'] = previousDefaultInstrument

# ==== End of CalibrateMaps() ====

CalibrateMaps( 14919 )
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Set parameters for ideal tube.
Left = 2.0 # Where the left end of tube should be in pixels (target for AP)
Centre = 512.5 # Where the centre of the tube should be in pixels (target for CP)
Right = 1023.0 # Where the right of the tube should be in pxels (target for BP)
Right = 1023.0 # Where the right of the tube should be in pixels (target for BP)
ActiveLength = 2.9 # Active length of tube in Metres

# Set initial parameters for peak finding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#
# TUBE CALIBRATION DEMONSTRATION PROGRAM FOR MERLIN
#
# This is a simple example for running calibration for and calibration run of MERLIN. Execute all.
# This is a simple example for running calibration for and calibration run of MERLIN.
# It uses the CalibrateMerlin Function
#
# Here we run the calibration of MERLIN or selected part of MERLIN
# (excluding short tubes of door 3)
Expand All @@ -10,32 +11,34 @@
# The workspace with calibrated instrument is saved to a Nexus file
#
from mantid.api import WorkspaceFactory # For table worskspace of calibrations
from mantid.kernel import config # To set default instrument to MERLIN
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

def CalibrateMerlin( RunNumber ):
'''
RunNumber is the run number as a string and including any leading zeros that would occur in the raw file name.
RunNumber is the run number of the calibration.
'''

# == Set parameters for calibration ==

filename = 'MER'+str(RunNumber) # Name of calibration run.
previousDefaultInstrument = config['default.instrument']
config['default.instrument']="MERLIN"
filename = str(RunNumber) # Name of calibration run.
rangeLower = 3000 # Integrate counts in each spectra from rangeLower to rangeUpper
rangeUpper = 20000 #

# Set parameters for ideal tube.
Left = 2.0 # Where the left end of tube should be in pixels (target for AP)
Centre = 512.5 # Where the centre of the tube should be in pixels (target for CP)
Right = 1023.0 # Where the right of the tube should be in pxels (target for BP)
Right = 1023.0 # Where the right of the tube should be in pixels (target for BP)
ActiveLength = 2.9 # Active length of tube in Metres

# Set initial parameters for peak finding
ExpectedHeight = 1000.0 # Expected Height of Peaks (initial value of fit parameter)
ExpectedWidth = 32.0 # Expected width of centre peak (initial value of fit parameter)
ExpectedPositions = [35.0, 512.0, 989.0] # Expected positions of the edges and peak (initial values of fit parameters)
ExpectedHeight = 1000.0 # Expected Height of Gaussian Peaks (initial value of fit parameter)
ExpectedWidth = 32.0 # Expected width of centre peak in Pixels (initial value of fit parameter)
ExpectedPositions = [35.0, 512.0, 989.0] # Expected positions of the edges and peak in pixels (initial values of fit parameters)

# Set what we want to calibrate (e.g whole intrument or one door )
CalibratedComponent = 'MERLIN' # Calibrate whole instrument
Expand Down Expand Up @@ -80,6 +83,9 @@ def CalibrateMerlin( RunNumber ):
SaveNexusProcessed( CalibInstWS, 'TubeCalibDemoMerlinResult.nxs',"Result of Running TubeCalibDemoMerlin_Simple.py")
print "saved calibrated workspace (CalibInstWS) into Nexus file TubeCalibDemoMerlinResult.nxs"

# == Reset dafault instrument ==
config['default.instrument'] = previousDefaultInstrument

# ==== End of CalibrateMerlin() ====

CalibrateMerlin( 12024 )
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
iTube.setArray([-0.41,-0.31,-0.21,-0.11,-0.02, 0.09, 0.18, 0.28, 0.39 ])

# Set fitting parameters
eP = [65.0, 113.0, 161.0, 209.0, 257.0, 305.0, 353.0, 401.0, 449.0]
eP = [57.5, 107.0, 156.5, 206.0, 255.5, 305.0, 354.5, 404.0, 453.5]
ExpectedHeight = 2000.0 # Expected Height of Gaussian Peaks (initial value of fit parameter)
ExpectedWidth = 32.0 # Expected width of Gaussian peaks in pixels (initial value of fit parameter)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# TUBE CALIBRATION DEMONSTRATION PROGRAM FOR WISH
#
# Here we run the calibration of WISH panel03 using a simple CalibrateWish function.
#
from mantid.api import WorkspaceFactory # For table worskspace of calibrations
from mantid.kernel import config # To set default instrument to WISH
from ideal_tube import * # for ideal tube
from tube_calib_fit_params import * # To handle fit parameters
from tube_calib import * # For tube calibration functions
from tube_spec import * # For tube specification class

def CalibrateWish( RunNumber, PanelNumber ):
'''
RunNumber is the run number of the calibration.
PanelNumber is a string of two-digit number of the panel being calibrated
'''
# == Set parameters for calibration ==
previousDefaultInstrument = config['default.instrument']
config['default.instrument']="WISH"
filename = str(RunNumber)
CalibratedComponent = 'WISH/panel'+PanelNumber


# Get calibration raw file and integrate it
print "Loading",filename
rawCalibInstWS = Load(filename) #'raw' in 'rawCalibInstWS' means unintegrated.
CalibInstWS = Integration( rawCalibInstWS, RangeLower=1, RangeUpper=20000 )
DeleteWorkspace(rawCalibInstWS)
print "Created workspace (CalibInstWS) with integrated data from run and instrument to calibrate"

#Create Calibration Table
calibrationTable = CreateEmptyTableWorkspace(OutputWorkspace="CalibTable")
calibrationTable.addColumn(type="int",name="Detector ID") # "Detector ID" column required by ApplyCalibration
calibrationTable.addColumn(type="V3D",name="Detector Position") # "Detector Position" column required by ApplyCalibration

# Specify panel of WISH instrument
thisTubeSet = TubeSpec(CalibInstWS)
print "Calibrating",CalibratedComponent
thisTubeSet.setTubeSpecByString(CalibratedComponent)

# Give y-positions of slit points (gotten for converting first tube's slit point to Y)
iTube = IdealTube()
iTube.setArray([-0.41,-0.31,-0.21,-0.11,-0.02, 0.09, 0.18, 0.28, 0.39 ])

# Set fitting parameters
eP = [57.5, 107.0, 156.5, 206.0, 255.5, 305.0, 354.5, 404.0, 453.5]
ExpectedHeight = 2000.0 # Expected Height of Gaussian Peaks (initial value of fit parameter)
ExpectedWidth = 32.0 # Expected width of Gaussian peaks in pixels (initial value of fit parameter)

fitPar = TubeCalibFitParams( eP, ExpectedHeight, ExpectedWidth )


print "Created objects needed for calibration."

# Get the calibration and put it into the calibration table
getCalibration( CalibInstWS, thisTubeSet, calibrationTable, fitPar, iTube)
print "Got calibration (new positions of detectors)"

#Apply the calibration
ApplyCalibration( Workspace=CalibInstWS, PositionTable=calibrationTable)
print "Applied calibration"

# == Save workspace ==
nexusName = "TubeCalibDemoWish"+PanelNumber+"Result.nxs"
SaveNexusProcessed( CalibInstWS, 'TubeCalibDemoWishResult.nxs',"Result of Running TubeCalibWishMerlin_Simple.py")
print "saved calibrated workspace (CalibInstWS) into Nexus file",nexusName

# == Reset dafault instrument ==
config['default.instrument'] = previousDefaultInstrument

# ==== End of CalibrateWish() ====

CalibrateWish( 17701, '03' )

0 comments on commit 3e83adf

Please sign in to comment.