Skip to content

Commit

Permalink
refs #6667 Small changes allowing to run DirectEnergyConversion for
Browse files Browse the repository at this point in the history
LET
  • Loading branch information
abuts committed Jul 1, 2013
1 parent 49d9311 commit 10064f8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 37 deletions.
119 changes: 84 additions & 35 deletions Code/Mantid/scripts/Inelastic/DirectEnergyConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def diagnose(self, white, **kwargs):
arg = par.lstrip('diag_')
if arg not in kwargs:
kwargs[arg] = getattr(self, arg)

# if input parameter is workspace rather then run, we want to keep it
self._keep_wb_workspace = False
if isinstance(white,str) and white in mtd:
self._keep_wb_workspace = True

# If we have a hard_mask, check the instrument name is defined
if 'hard_mask_file' in kwargs:
if 'instrument_name' not in kwargs:
Expand All @@ -127,7 +133,7 @@ def diagnose(self, white, **kwargs):
if whitews_name in mtd:
DeleteWorkspace(Workspace=whitews_name)
# Load
white_data = self.load_data(white,whitews_name)
white_data = self.load_data(white,whitews_name,self._keep_wb_workspace)

diag_mask= LoadMask(Instrument=self.instr_name,InputFile=kwargs['hard_mask_file'],
OutputWorkspace='hard_mask_ws')
Expand Down Expand Up @@ -222,7 +228,8 @@ def do_white(self, white_run, spectra_masks, map_file,mon_number=None):
if whitews_name in mtd:
DeleteWorkspace(Workspace=whitews_name)
# Load
white_data = self.load_data(white_run)
white_data = self.load_data(white_run,whitews_name,self._keep_wb_workspace)

# Normalise
white_ws = self.normalise(white_data, whitews_name, self.normalise_method,0.0,mon_number)
# Units conversion
Expand Down Expand Up @@ -359,7 +366,7 @@ def _do_mono(self, data_ws, monitor_ws, result_name, ei_guess,
bin_offset = -mon1_peak

# For event mode, we are going to histogram in energy first, then go back to TOF
if (self.facility == "SNS"):
if (self.__facility == "SNS"):
if self.background == True:
# Extract the time range for the background determination before we throw it away
background_bins = "%s,%s,%s" % (self.bkgd_range[0] + bin_offset, (self.bkgd_range[1]-self.bkgd_range[0]), self.bkgd_range[1] + bin_offset)
Expand Down Expand Up @@ -402,7 +409,7 @@ def _do_mono(self, data_ws, monitor_ws, result_name, ei_guess,
if self.background == True:
# Remove the count rate seen in the regions of the histograms defined as the background regions, if the user defined such region
ConvertToDistribution(Workspace=result_name)
if (self.facility == "SNS"):
if (self.__facility == "SNS"):
FlatBackground(InputWorkspace="background_origin_ws",OutputWorkspace= "background_ws",
StartX= self.bkgd_range[0] + bin_offset,EndX= self.bkgd_range[1] + bin_offset,
WorkspaceIndexList= '',Mode= 'Mean',OutputMode= 'Return Background')
Expand Down Expand Up @@ -438,7 +445,7 @@ def _do_mono(self, data_ws, monitor_ws, result_name, ei_guess,
Rebin(InputWorkspace=result_name,OutputWorkspace=result_name,Params= self.energy_bins,PreserveEvents=False)

if self.apply_detector_eff:
if (self.facility == "SNS"):
if (self.__facility == "SNS"):
# Need to be in lambda for detector efficiency correction
ConvertUnits(InputWorkspace=result_name,OutputWorkspace= result_name, Target="Wavelength", EMode="Direct", EFixed=ei_value)
He3TubeEfficiency(InputWorkspace=result_name,OutputWorkspace=result_name)
Expand Down Expand Up @@ -513,7 +520,7 @@ def convert_to_energy(self, mono_run, ei, white_run=None, mono_van=None,\

#calculate psi from sample environment motor and offset

if self.facility == 'ISIS' :
if self.__facility == 'ISIS' :
default_offset=float('NaN')
if not (motor is None) and sample_wkspace.getRun().hasProperty(motor):
default_offset = 0
Expand Down Expand Up @@ -740,14 +747,18 @@ def save_results(self, workspace, save_path, formats = None):


#-------------------------------------------------------------------------------
def load_data(self, runs,new_ws_name=None):
def load_data(self, runs,new_ws_name=None,keep_previous_ws=False):
"""
Load a run or list of runs. If a list of runs is given then
they are summed into one.
"""
result_ws = common.load_runs(runs, sum=True)
if new_ws_name != None :
result_ws = RenameWorkspace(InputWorkspace=result_ws,OutputWorkspace=new_ws_name)
if keep_previous_ws:
result_ws = CloneWorkspace(InputWorkspace = result_ws,OutputWorkspace = new_ws_name)
else:
result_ws = RenameWorkspace(InputWorkspace=result_ws,OutputWorkspace=new_ws_name)

self.setup_mtd_instrument(result_ws)
return result_ws

Expand All @@ -762,41 +773,65 @@ def __init__(self, instr_name=None):
self._to_stdout = True
self._log_to_mantid = False
self._idf_values_read = False
self.instr_name=None
if not (instr_name is None or len(instr_name)==0) :
self._keep_wb_workspace=False # when input data for reducer is wb workspace rather then run number, we want to keep this workspace. But usually not

self.instr_name = instr_name
if not (instr_name is None or len(instr_name)==0) : # first time run or empty run
self.initialise(instr_name)

#----------------------------------------------------------------------------------
# Complex setters/getters
#----------------------------------------------------------------------------------
@property
def instr_name(self):
return _instr_name
return self._instr_name
@instr_name.setter
def instr_name(self,new_name):

if not hasattr(self,'instr_name') :
self._instr_name=None

if new_name is None:
self._instr_name = None
return

# Instrument name might be a prefix, query Mantid for the full name
new_name = config.getFacility().instrument(instr_name).shortName()
if len(new_name)==0 :
raise KeyError(" Can not find/set-up the instrument: "+instr_name)

short_name=''
try :
short_name = config.getFacility().instrument(new_name).shortName()
except:
# it is possible to have wrong facility:
facilities = config.getString('supported.facilities')
facilities = facilities.split(';')
for fac_name in facilities:
#config.setFacility(fac_name);
config.setString('default.facility',fac_name)
try :
short_name = config.getFacility().instrument(new_name).shortName()
if len(short_name)>0 :
break
except:
pass
if len(short_name)==0 :
raise KeyError(" Can not find/set-up the instrument: "+new_name+' in any supported facility')

new_name = short_name
self.__facility = str(config.getFacility())
if new_name == self.instr_name:
return


self._instr_name = new_name

config['default.instrument'] = self.instr_name
config['default.instrument'] = new_name

if self.instrument.getName() != instr_name :


if not hasattr(self,'instument') or self.instrument.getName() != instr_name :
# Load an empty instrument if one isn't already there
idf_dir = config.getString('instrumentDefinition.directory')
try:
idf_file=api.ExperimentInfo.getInstrumentFilename(self.instr_name)
tmp_ws_name = '__empty_' + self.instr_name
idf_file=api.ExperimentInfo.getInstrumentFilename(new_name)
tmp_ws_name = '__empty_' + new_name
if not mtd.doesExist(tmp_ws_name):
LoadEmptyInstrument(Filename=idf_file,OutputWorkspace=tmp_ws_name)
self.instrument = mtd[tmp_ws_name].getInstrument()
Expand All @@ -807,7 +842,7 @@ def instr_name(self,new_name):


# Initialise other IDF parameters
self.init_idf_params(reload_instrument)
self.init_idf_params(True)



Expand All @@ -821,6 +856,10 @@ def van_rmm(self):
def van_rmm(self):
pass

@property
def facility(self):
return self.__facility

# integration range for monochromatic vanadium,
@property
def monovan_integr_range(self):
Expand Down Expand Up @@ -913,7 +952,7 @@ def relocate_dets(self) :

def initialise(self, instr_name,reload_instrument=False):
"""
Initialise the attributes of the class
Initialise the private attributes of the class and the nullify the attributes which expected to be always set-up from a calling script
"""

# Instrument and default parameter setup
Expand All @@ -935,7 +974,7 @@ def initialise(self, instr_name,reload_instrument=False):
# list of the parameters which should usually be changed by user and if not, user should be warn about it.
self.__abs_units_par_to_change=['sample_mass','sample_rmm']


# set/reset instument name in case if this function is called separately. May call lengthy procedure of changing instrument
self.instr_name = instr_name

def setup_mtd_instrument(self, workspace = None,reload_instrument=False):
Expand All @@ -957,7 +996,7 @@ def init_idf_params(self, reload_instrument=False):
specify some parameters which may be not in IDF Parameters file
"""
# mandatrory command line parameter
# mandatrory command line parameter. Real value can not vbe negative
self.incident_energy= -666
# mandatrory command line parameter
self.energy_bins = None
Expand All @@ -966,21 +1005,15 @@ def init_idf_params(self, reload_instrument=False):
self.__det_cal_file_ws = None

# should come from Mantid
if (self.instr_name == "CNCS" or self.instr_name == "ARCS" or self.instr_name == "SEQUOIA" or self.instr_name == "HYSPEC"):
self.facility = "SNS"
self.normalise_method = 'current'
else:
self.facility = str(config.getFacility())

# Motor names-- SNS stuff -- psi used by nxspe file
# These should be reconsidered on moving into _Parameters.xml
self.monitor_workspace = None # looks like unused parameter
self.motor = None
self.motor_offset = None
self.psi = float('NaN')



if self.__facility == 'SNS':
self.normalise_method = 'current'



Expand Down Expand Up @@ -1330,9 +1363,25 @@ def test_build_subst_dictionary(self):
# params = ["];

def test_init_reducer(self):
self.reducer.initialise("MAP",True);
self.assertTrue(self.reducer._idf_values_read)
self.assertEqual(self.reducer.instr_name,"MAP")
tReducer = self.reducer
self.assertTrue(tReducer._idf_values_read)

tReducer.initialise("MAP",True);
self.assertEqual(tReducer.instr_name,"MAP")

#config.setFacility("SNS")
#config.setString('default.facility','SNS')
tReducer.instr_name = 'SEQ'
self.assertTrue(tReducer._idf_values_read)
self.assertEqual(tReducer.instr_name,"SEQ")
self.assertEqual(tReducer.facility,'SNS')

tReducer.instr_name = 'MER'
self.assertEqual(tReducer.instr_name,"MER")
self.assertEqual(tReducer.facility,'ISIS')

self.assertRaises(KeyError,setattr,tReducer,'instr_name','NonExistingInstrument')


def test_set_non_default_wrong_value(self):
tReducer = self.reducer
Expand Down
6 changes: 4 additions & 2 deletions Code/Mantid/scripts/Inelastic/dgreduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file='default',monovan_run=No
# --------------------------------------------------------------------------------------------------------
if isinstance(sample_run,int):
Reducer.log('DGreduce run for: '+Reducer.instr_name+' Run number: '+str(sample_run))

else:
Reducer.log('DGreduce run for: '+Reducer.instr_name+' Run for workspace name: '+str(sample_run))


try:
n,r=funcreturns.lhs_info('both')
wksp_out=r[0]
Expand Down Expand Up @@ -264,7 +266,7 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file='default',monovan_run=No
# diag the sample and detector vanadium. It will deal with hard mask only if it is set that way
if not masks_done:
masking = Reducer.diagnose(wb_run,sample = mask_run,
second_white = None,variation=1.1,print_results=True)
second_white = None,print_results=True)
header = "Diag Processed "


Expand All @@ -276,7 +278,7 @@ def arb_units(wb_run,sample_run,ei_guess,rebin,map_file='default',monovan_run=No
else:
print '########### Run diagnose for monochromatic vanadium run ##############'
masking2 = Reducer.diagnose(wb_for_monovanadium,sample=monovan_run,
second_white = None,variation=1.1,print_results=True)
second_white = None,rint_results=True)
if not Reducer.use_hard_mask_only : # in this case the masking2 is different but points to the same workspace Should be better soulution for that.
masking += masking2
DeleteWorkspace(masking2)
Expand Down

0 comments on commit 10064f8

Please sign in to comment.