Skip to content

Commit

Permalink
refs #10384 Main algorithm contents and beginning of unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Oct 27, 2014
1 parent e2e9a1b commit a2eda2f
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 17 deletions.
Expand Up @@ -41,12 +41,30 @@ namespace Algorithms
class DLLExport BackgroundHelper
{
public:
BackgroundHelper():m_emode(0){};
BackgroundHelper();
void initialize(const API::MatrixWorkspace_const_sptr &bkgWS,const API::MatrixWorkspace_sptr &sourceWS,int emode);

void removeBackground(int hist,const MantidVec &XValues,MantidVec &y_data,MantidVec &e_data);
private:
int m_emode;
// pointer to the units conversion class for the working workspace;
Kernel::Unit_sptr m_WSUnit;

// shared pointer to the workspace containing background
API::MatrixWorkspace_const_sptr m_bgWs;
// shared pointer to the workspace where background should be removed
API::MatrixWorkspace_const_sptr m_wkWS;

// if the background workspace is single value workspace
bool m_singleValueBackground;
// the intensity of the background for a given spectra of a background workspace
double m_Jack05;
// energy conversion mode
int m_Emode;
// source-sample distance
double m_L1;
// incident for direct or analysis for indirect energy for units conversion
double m_Efix;
Geometry::IComponent_const_sptr m_Sample;

};

Expand Down
93 changes: 84 additions & 9 deletions Code/Mantid/Framework/Algorithms/src/BackgroundHelper.cpp
Expand Up @@ -3,17 +3,92 @@

namespace Mantid
{
namespace Algorithms
{
namespace Algorithms
{
/// Constructor
BackgroundHelper::BackgroundHelper():
m_singleValueBackground(false),
m_Emode(0),
m_Efix(0),
m_Jack05(0)
{};

/** Initialization method:
@param bkgWS -- shared pointer to the workspace which contains background
@param sourceWS -- shared pointer to the workspace to remove background from
@param emode -- energy conversion mode used during internal units conversion (0 -- elastic, 1-direct, 2 indirect, as defined in
*/
void BackgroundHelper::initialize(const API::MatrixWorkspace_const_sptr &bkgWS,const API::MatrixWorkspace_sptr &sourceWS,int emode)
{
m_bgWs = bkgWS;
m_wkWS = sourceWS;

void BackgroundHelper::initialize(const API::MatrixWorkspace_const_sptr &bkgWS,const API::MatrixWorkspace_sptr &sourceWS,int emode)
{
}
std::string bgUnits = bkgWS->getAxis(0)->unit()->unitID();
if(bgUnits!="TOF")
throw std::invalid_argument(" Background Workspace: "+bkgWS->getName()+" should be in the units of TOF");

void BackgroundHelper::removeBackground(int hist,const MantidVec &XValues,MantidVec &y_data,MantidVec &e_data)
{
}
if(!(bkgWS->getNumberHistograms() == 1 || sourceWS->getNumberHistograms()==bkgWS->getNumberHistograms()))
throw std::invalid_argument(" Background Workspace: "+bkgWS->getName()+" should have the same number of spectra as source workspace or be a single histogram workspace");

m_WSUnit = sourceWS->getAxis(0)->unit();
if(!m_WSUnit)
throw std::invalid_argument(" Source Workspace: "+sourceWS->getName()+" should have units");

Geometry::IComponent_const_sptr source = sourceWS->getInstrument()->getSource();
m_Sample = sourceWS->getInstrument()->getSample();
if ((!source) || (!m_Sample))
throw std::invalid_argument("Instrument on Source workspace:"+sourceWS->getName()+"is not sufficiently defined: failed to get source and/or sample");
m_L1 = source->getDistance(*m_Sample);


m_singleValueBackground = false;
if(bkgWS->getNumberHistograms()==0)
m_singleValueBackground = true;
const MantidVec& dataY = bkgWS->dataY(0);
const MantidVec& dataX = bkgWS->dataX(0);
m_Jack05 = dataY[0]/(dataX[1]-dataX[0]);


}

void BackgroundHelper::removeBackground(int nHist,const MantidVec &XValues,MantidVec &y_data,MantidVec &e_data)
{
double Jack05;
if(m_singleValueBackground)
{
Jack05= m_Jack05;
}
else
{
const MantidVec& dataY = m_bgWs->dataY(nHist);
const MantidVec& dataX = m_bgWs->dataX(nHist);
Jack05 = dataY[0]/(dataX[1]-dataX[0]);
}
try
{
auto detector = m_wkWS->getDetector(nHist);
//
double twoTheta = m_wkWS->detectorTwoTheta(detector);
double L2 = detector->getDistance(*m_Sample);
double delta(std::numeric_limits<double>::quiet_NaN());
//
double tof1 = m_WSUnit->convertSingleToTOF(XValues[0], m_L1, L2,twoTheta, m_Emode, m_Efix,delta);
for(size_t i=1;i<XValues.size();i++)
{
double tof2=m_WSUnit->convertSingleToTOF(XValues[i], m_L1, L2,twoTheta, m_Emode, m_Efix, delta);
double normBkgrnd = (tof1-tof2)*Jack05;
double normErr = std::sqrt(normBkgrnd);
y_data[i-1] -=normBkgrnd;
e_data[i-1] +=normErr;
}

}
catch(...)
{
// no background removal for this detector;
}

}

} // end API
} // end API
} // end Mantid
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/Algorithms/src/Rebin.cpp
Expand Up @@ -112,9 +112,9 @@ namespace Mantid
"but actually useful mainly for removing background while rebinning an event workspace in the units different from TOF.");

std::vector<std::string> dE_modes = Kernel::DeltaEMode().availableTypes();
declareProperty("dEAnalysisMode",dE_modes[Kernel::DeltaEMode::Direct],boost::make_shared<Kernel::StringListValidator>(dE_modes),
declareProperty("EMode",dE_modes[Kernel::DeltaEMode::Direct],boost::make_shared<Kernel::StringListValidator>(dE_modes),
"If FlatBkgWorkspace, this property is used to define the units conversion from TOF to the units of the InputWorkspace",Direction::Input);
setPropertySettings("dEAnalysisMode",
setPropertySettings("EMode",
new Kernel::VisibleWhenProperty("FlatBkgWorkspace", IS_NOT_EQUAL_TO, ""));


Expand Down Expand Up @@ -395,7 +395,7 @@ namespace Mantid
}


const std::string emodeStr = getProperty("dEAnalysisMode");
const std::string emodeStr = getProperty("EMode");
eMode = static_cast<int>(Kernel::DeltaEMode().fromString(emodeStr));

return bkgWksp;
Expand Down
68 changes: 66 additions & 2 deletions Code/Mantid/Framework/Algorithms/test/BackgroundHelperTest.h
Expand Up @@ -4,8 +4,13 @@
#include <cxxtest/TestSuite.h>

#include "MantidAlgorithms/BackgroundHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

#include "MantidAlgorithms/Rebin.h"
#include "MantidAlgorithms/ConvertUnits.h"
#include "MantidAlgorithms/CalculateFlatBackground.h"

using namespace Mantid;

class BackgroundHelperTest : public CxxTest::TestSuite
{
Expand All @@ -18,18 +23,77 @@ class BackgroundHelperTest : public CxxTest::TestSuite

BackgroundHelperTest()
{

DataObjects::Workspace2D_sptr theWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(1, 15000);

Algorithms::Rebin rebinner;
std::string wsName = theWS->getName();
if(wsName.empty())
{
wsName = "sourceWS";
}
// Add incident energy necessary for unit conversion
theWS->mutableRun().addProperty("Ei",13.,"meV",true);

API::AnalysisDataService::Instance().addOrReplace(wsName,theWS);

rebinner.initialize();
rebinner.setPropertyValue("InputWorkspace",theWS->getName());
rebinner.setPropertyValue("OutputWorkspace","Background");
rebinner.setPropertyValue("Params","10000,5000,15000");

rebinner.execute();

BgWS = API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>("Background");

Algorithms::ConvertUnits unitsConv;
unitsConv.initialize();
unitsConv.setPropertyValue("InputWorkspace",theWS->getName());
unitsConv.setPropertyValue("OutputWorkspace","sourceWSdE");
unitsConv.setPropertyValue("Target","DeltaE");
unitsConv.setPropertyValue("EMode","Direct");

unitsConv.execute();

Algorithms::CalculateFlatBackground bgRemoval;

bgRemoval.initialize();
bgRemoval.setPropertyValue("InputWorkspace",theWS->getName());
bgRemoval.setPropertyValue("OutputWorkspace",theWS->getName());
bgRemoval.setPropertyValue("StartX","10000");
bgRemoval.setPropertyValue("EndX","15000");
bgRemoval.setPropertyValue("Mode","Mean");

bgRemoval.execute();

unitsConv.setPropertyValue("InputWorkspace",theWS->getName());
unitsConv.setPropertyValue("OutputWorkspace","sampleWSdE");
unitsConv.setPropertyValue("Target","DeltaE");
unitsConv.setPropertyValue("EMode","Direct");

unitsConv.execute();


}

~BackgroundHelperTest()
{
BgWS.reset();
}

void testBackgroundInit()
void testBackgroundHelper()
{
Algorithms::BackgroundHelper bgRemoval;

auto SourceWS=API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>("sourceWSdE");
int emode = static_cast<int>(Kernel::DeltaEMode().fromString("Direct"));

bgRemoval.initialize(BgWS,SourceWS,emode);

}

private:

API::MatrixWorkspace_sptr BgWS;

};

Expand Down
Expand Up @@ -291,7 +291,7 @@ namespace Mantid
}
case(CnvrtToMD::ConvertByTOF):
{
double delta(std::numeric_limits<double>::quiet_NaN());
double delta(std::numeric_limits<double>::quiet_NaN());
m_TwoTheta = (*m_pTwoThetas)[i];
m_L2 = (*m_pL2s)[i];
double Efix = m_Efix;
Expand Down

0 comments on commit a2eda2f

Please sign in to comment.