Skip to content

Commit

Permalink
Re #6151 Add normalisation algo and stub for HFIR reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Nov 16, 2012
1 parent e61b019 commit 9713565
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""*WIKI*
Normalise the data to timer or a spectrum, typically a monitor,
with in the workspace. By default the Normalisation is done with
respect to the Instrument's incident monitor
*WIKI*"""
from mantid.api import *
from mantid.kernel import *

class HFIRSANSNormalise(PythonAlgorithm):

def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", "",
direction = Direction.Input))

self.declareProperty("NormalisationType", "Monitor",
StringListValidator(["Monitor", "Timer"]),
doc="Type of Normalisation to use")

self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "",
direction = Direction.Output))

self.declareProperty("OutputMessage", "", direction=Direction.Output)

def PyExec(self):
input_ws = self.getPropertyValue("InputWorkspace")
output_ws = self.getPropertyValue("OutputWorkspace")
Normalisation = self.getPropertyValue("NormalisationType")

# Get the monitor or timer
ws = AnalysisDataService.retrieve(input_ws)
norm_count = ws.getRun().getProperty(Normalisation.lower()).value
print norm_count

if Normalisation=="Monitor":
factor = 1.0e8/norm_count
else:
factor = 1.0/norm_count

alg = AlgorithmManager.create("Scale")
alg.initialize()
alg.setPropertyValue("InputWorkspace", input_ws)
alg.setPropertyValue("OutputWorkspace", output_ws)
alg.setProperty("Factor", factor)
alg.setProperty("Operation", "Multiply")

self.setPropertyValue("OutputWorkspace", output_ws)
self.setProperty("OutputMessage",
"Normalisation by %s: %6.2g" % (Normalisation, norm_count))

#############################################################################################

registerAlgorithm(HFIRSANSNormalise)
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""*WIKI*
*WIKI*"""
from mantid.api import *
from mantid.kernel import *

class HFIRSANSReduction(PythonAlgorithm):

def PyInit(self):
self.declareProperty(FileProperty("Filename", "",
action=FileAction.Load, extensions=['xml']))
self.declareProperty("ReductionProperties", "__sans_reduction_properties",
validator=StringMandatoryValidator(),
doc="Property manager name for the reduction")
self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "",
direction = Direction.Output),
"Reduced workspace")
self.declareProperty("OutputMessage", "",
direction=Direction.Output, doc = "Output message")

def PyExec(self):
filename = self.getProperty("Filename").value
output_ws = self.getPropertyValue("OutputWorkspace")
property_manager_name = self.getProperty("ReductionProperties").value
property_manager = PropertyManagerDataService.retrieve(property_manager_name)

property_list = [p.name for p in property_manager.getProperties()]

# Find the beam center
self.find_beam_center(property_manager)

# Load the sample data
if "LoadAlgorithm" not in property_list:
raise RuntimeError, "HFIR SANS reduction not set up properly: missing load algorithm"
p=property_manager.getProperty("LoadAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("Filename", filename)
alg.setProperty("OutputWorkspace", output_ws)
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
output_msg = "Loaded %s\n" % filename
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value

# Perform the main corrections on the sample data
output_msg += self.process_data_file(output_ws)

# Sample data transmission correction

# Process background data

# Background transmission correction

# Subtract background

# Absolute scale correction

# Compute I(q)

# Compute I(qx,qy)

# Save data


self.setPropertyValue("OutputWorkspace", output_ws)
self.setProperty("OutputMessage", output_msg)

def find_beam_center(self, property_manager):
pass

def process_data_file(self, workspace):
output_msg = ""
property_manager_name = self.getProperty("ReductionProperties").value
property_manager = PropertyManagerDataService.retrieve(property_manager_name)
property_list = [p.name for p in property_manager.getProperties()]

# Dark current subtraction
if "DarkCurrentAlgorithm" in property_list:
p=property_manager.getProperty("DarkCurrentAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", workspace)
alg.setProperty("OutputWorkspace", workspace)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Normalize
if "NormaliseAlgorithm" in property_list:
p=property_manager.getProperty("NormaliseAlgorithm")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", workspace)
alg.setProperty("OutputWorkspace", workspace)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Mask

# Solid angle correction
if "SANSSolidAngleCorrection" in property_list:
p=property_manager.getProperty("SANSSolidAngleCorrection")
alg=Algorithm.fromString(p.valueAsStr)
alg.setProperty("InputWorkspace", workspace)
alg.setProperty("OutputWorkspace", workspace)
if alg.existsProperty("ReductionProperties"):
alg.setProperty("ReductionProperties", property_manager_name)
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Sensitivity correction
return output_msg


#############################################################################################

registerAlgorithm(HFIRSANSReduction)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ See [http://www.mantidproject.org/Reduction_for_HFIR_SANS SANS Reduction] docume
#include "MantidAPI/PropertyManagerDataService.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidKernel/BoundedValidator.h"

#include "MantidKernel/ListValidator.h"
#include "MantidAPI/FileProperty.h"
#include <boost/algorithm/string/predicate.hpp>
namespace Mantid
{
namespace WorkflowAlgorithms
Expand All @@ -36,8 +38,12 @@ using namespace DataObjects;

void SetupHFIRReduction::init()
{
// Load options
std::string load_grp = "Load Options";

declareProperty("SampleDetectorDistance", EMPTY_DBL(), "Sample to detector distance to use (overrides meta data), in mm");
declareProperty("SampleDetectorDistanceOffset", EMPTY_DBL(), "Offset to the sample to detector distance (use only when using the distance found in the meta data), in mm");
declareProperty("SolidAngleCorrection", true, "If true, the solide angle correction will be applied to the data");

// Optionally, we can specify the wavelength and wavelength spread and overwrite
// the value in the data file (used when the data file is not populated)
Expand All @@ -48,6 +54,12 @@ void SetupHFIRReduction::init()
declareProperty("WavelengthSpread", 0.1, mustBePositive,
"Wavelength spread to use when loading the data file (default 0.0)" );

setPropertyGroup("SampleDetectorDistance", load_grp);
setPropertyGroup("SampleDetectorDistanceOffset", load_grp);
setPropertyGroup("SolidAngleCorrection", load_grp);
setPropertyGroup("Wavelength", load_grp);
setPropertyGroup("WavelengthSpread", load_grp);

// Beam center
std::string center_grp = "Beam Center";
declareProperty("FindBeamCenter", false, "If True, the beam center will be calculated");
Expand All @@ -57,6 +69,19 @@ void SetupHFIRReduction::init()
setPropertyGroup("BeamCenterX", center_grp);
setPropertyGroup("BeamCenterY", center_grp);

// Normalisation
std::vector<std::string> incidentBeamNormOptions;
incidentBeamNormOptions.push_back("None");
incidentBeamNormOptions.push_back("Monitor");
incidentBeamNormOptions.push_back("Time");
this->declareProperty("Normalisation", "Monitor",
boost::make_shared<StringListValidator>(incidentBeamNormOptions),
"Options for data normalisation");

// Dark current
declareProperty(new API::FileProperty("DarkCurrentFile", "", API::FileProperty::OptionalLoad, ".xml"),
"The name of the input data file to load as dark current.");

declareProperty("OutputMessage","",Direction::Output);
declareProperty("ReductionProperties","__sans_reduction_properties", Direction::Input);
}
Expand Down Expand Up @@ -90,8 +115,9 @@ void SetupHFIRReduction::exec()
loadAlg->setProperty("Wavelength", wavelength);
loadAlg->setProperty("WavelengthSpread", wavelengthSpread);
}
reductionManager->declareProperty(new AlgorithmProperty("LoadAlgorithm"));
reductionManager->setProperty("LoadAlgorithm", loadAlg);
AlgorithmProperty *algProp = new AlgorithmProperty("LoadAlgorithm");
algProp->setValue(loadAlg->toString());
reductionManager->declareProperty(algProp);

// Beam center
const double beamCenterX = getProperty("BeamCenterX");
Expand All @@ -103,12 +129,47 @@ void SetupHFIRReduction::exec()
reductionManager->declareProperty(new PropertyWithValue<double>("LatestBeamCenterY", beamCenterY) );
}

// Store dark current algorithm
const std::string darkCurrentFile = getPropertyValue("DarkCurrentFile");
if (darkCurrentFile.size() > 0)
{
IAlgorithm_sptr darkAlg = createSubAlgorithm("HFIRDarkCurrentSubtraction");
darkAlg->setProperty("Filename", darkCurrentFile);
darkAlg->setProperty("OutputDarkCurrentWorkspace", "");
darkAlg->setPropertyValue("ReductionProperties", reductionManagerName);
algProp = new AlgorithmProperty("DarkCurrentAlgorithm");
algProp->setValue(darkAlg->toString());
reductionManager->declareProperty(algProp);
}

// Store default dark current algorithm
IAlgorithm_sptr darkAlg = createSubAlgorithm("HFIRDarkCurrentSubtraction");
darkAlg->setProperty("OutputDarkCurrentWorkspace", "");
darkAlg->setPropertyValue("ReductionProperties", reductionManagerName);
reductionManager->declareProperty(new AlgorithmProperty("DefaultDarkCurrentAlgorithm"));
reductionManager->setProperty("DefaultDarkCurrentAlgorithm", darkAlg);
IAlgorithm_sptr darkDefaultAlg = createSubAlgorithm("HFIRDarkCurrentSubtraction");
darkDefaultAlg->setProperty("OutputDarkCurrentWorkspace", "");
darkDefaultAlg->setPropertyValue("ReductionProperties", reductionManagerName);
algProp = new AlgorithmProperty("DefaultDarkCurrentAlgorithm");
algProp->setValue(darkDefaultAlg->toString());
reductionManager->declareProperty(algProp);

// Solid angle correction
const bool solidAngleCorrection = getProperty("SolidAngleCorrection");
if (solidAngleCorrection)
{
IAlgorithm_sptr solidAlg = createSubAlgorithm("SANSSolidAngleCorrection");
algProp = new AlgorithmProperty("SANSSolidAngleCorrection");
algProp->setValue(solidAlg->toString());
reductionManager->declareProperty(algProp);
}

// Normalization
const std::string normalization = getProperty("Normalisation");
if (!boost::contains(normalization, "None"))
{
IAlgorithm_sptr normAlg = createSubAlgorithm("HFIRSANSNormalise");
normAlg->setProperty("NormalisationType", normalization);
algProp = new AlgorithmProperty("NormaliseAlgorithm");
algProp->setValue(normAlg->toString());
reductionManager->declareProperty(algProp);
}

setPropertyValue("OutputMessage", "HFIR reduction options set");
}
Expand Down

0 comments on commit 9713565

Please sign in to comment.