Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/8402_bank_to_workspace'
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Jan 17, 2014
2 parents 5cfc6fb + d18d8c1 commit 3a43141
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace DataHandling
DataObjects::TableWorkspace_sptr genInfoTableWorkspace(std::vector<int> banks);

/// Put parameters into a metrix workspace
void putParametersIntoWorkspace( const API::ITableWorkspace_sptr &tws, API::MatrixWorkspace_sptr ws);
void putParametersIntoWorkspace( size_t wsNumber, const API::ITableWorkspace_sptr &tws, API::MatrixWorkspace_sptr ws);

/// Add an ALFBE parameter
void addALFBEParameter(const API::Column_const_sptr, Poco::XML::Document* mDoc, Poco::XML::Element* parent, const std::string& paramName);
Expand Down
60 changes: 29 additions & 31 deletions Code/Mantid/Framework/DataHandling/src/LoadFullprofResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,24 @@ namespace DataHandling


// If workspace, put parameters there
if(wsg)
{
Workspace_sptr wsi = wsg->getItem(0);
auto workspace = boost::dynamic_pointer_cast<MatrixWorkspace>(wsi);
putParametersIntoWorkspace( outTabWs, workspace );
}
if(wsg) {
// First check that number of workspaces in group matches number of banks
if( wsg->size() != vec_bankids.size() )
{
std::ostringstream mess;
mess << "Number of banks ("<< vec_bankids.size() << ") does not match number of workspaces (" << wsg->size() << ") in group. Parameters not put into workspaces.";
g_log.error( mess.str() );
}
else // Numbers match, so put parameters into workspaces.
{
for (size_t i=0; i < wsg->size(); ++i)
{
Workspace_sptr wsi = wsg->getItem(i);
auto workspace = boost::dynamic_pointer_cast<MatrixWorkspace>(wsi);
putParametersIntoWorkspace( i+1, outTabWs, workspace );
}
}
}
else if( getPropertyValue("OutputTableWorkspace") == "")
{
// We don't know where to output
Expand Down Expand Up @@ -769,13 +781,16 @@ namespace DataHandling
return tablews;
}

void LoadFullprofResolution::putParametersIntoWorkspace( const API::ITableWorkspace_sptr &tws, API::MatrixWorkspace_sptr ws)
void LoadFullprofResolution::putParametersIntoWorkspace( size_t wsNumber, const API::ITableWorkspace_sptr &tws, API::MatrixWorkspace_sptr ws)
{

// Get instrument name from matrix workspace
std::string instrumentName = ws->getInstrument()->getName();

// Convert table workspace into DOM XML document
// Get column from table workspace
API::Column_const_sptr column = tws->getColumn( wsNumber );

// Convert table workspace column into DOM XML document
// Set up writer to Paremeter file
DOMWriter writer;
writer.setNewLine("\n");
Expand All @@ -797,30 +812,13 @@ namespace DataHandling
AutoPtr<Element> instrumentElem = mDoc->createElement("component-link");
instrumentElem->setAttribute("name",instrumentName);
rootElem->appendChild(instrumentElem);
API::Column_const_sptr column1 = tws->getColumn( 1 );
addALFBEParameter( column1, mDoc, instrumentElem, "Alph0");
addALFBEParameter( column1, mDoc, instrumentElem, "Beta0");
addALFBEParameter( column1, mDoc, instrumentElem, "Alph1");
addALFBEParameter( column1, mDoc, instrumentElem, "Beta1");

// Add banks
if(tws->columnCount() < 2){
throw std::runtime_error("No banks found");
}
size_t num_banks = tws->columnCount()-1;
addALFBEParameter( column, mDoc, instrumentElem, "Alph0");
addALFBEParameter( column, mDoc, instrumentElem, "Beta0");
addALFBEParameter( column, mDoc, instrumentElem, "Alph1");
addALFBEParameter( column, mDoc, instrumentElem, "Beta1");
addSigmaParameters( column, mDoc, instrumentElem );
addGammaParameters( column, mDoc, instrumentElem );

for( size_t i=0; i<num_banks; ++i)
{
API::Column_const_sptr column = tws->getColumn( i+1 );
const double bankNumber = column->cell<double>(0);
std::ostringstream bankName;
bankName << "bank" << bankNumber;
AutoPtr<Element> bankElem = mDoc->createElement("component-link");
bankElem->setAttribute("name",bankName.str());
addSigmaParameters( column, mDoc, bankElem );
addGammaParameters( column, mDoc, bankElem );
rootElem->appendChild(bankElem);
}

// Convert DOM XML document into string
std::ostringstream outFile;
Expand Down
110 changes: 96 additions & 14 deletions Code/Mantid/Framework/DataHandling/test/LoadFullprofResolutionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
//----------------------------------------------------------------------------------------------
/** Test import of ALFBE, GAMMA and SIGMA parameters
* and check they are given their expected names.
* ConvertFullprofToXML relies on features tested here.
*/
void test_ags_parameters()
{
Expand Down Expand Up @@ -284,8 +283,8 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
string filename("TestWorskpace.irf");
generate1BankIrfFile(filename);

// Load workspace wsName
load_GEM();
// Load workspace group wsName with one workspace
load_GEM(1);

// Set up algorithm to load into the workspace
LoadFullprofResolution alg;
Expand All @@ -299,7 +298,7 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());

// Check parameters in workspace T
// Check parameters in workspace
// The workspace is a workspace group with one member corresponding to the one bank in the IRF file
WorkspaceGroup_sptr gws;
gws = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(wsName);
Expand Down Expand Up @@ -342,8 +341,7 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
}


boost::shared_ptr<const Mantid::Geometry::IComponent> bank = instr->getComponentByName("bank1");
Mantid::Geometry::Parameter_sptr sigmaSqParam = paramMap.get(&(*bank), "SigmaSquared", "fitting");
Mantid::Geometry::Parameter_sptr sigmaSqParam = paramMap.get(&(*instr), "SigmaSquared", "fitting");
TS_ASSERT(sigmaSqParam);
if(sigmaSqParam)
{
Expand All @@ -354,7 +352,7 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( formulaValueCantreAt10, 0.399, 0.0000001);
}

Mantid::Geometry::Parameter_sptr gammaParam = paramMap.get(&(*bank), "Gamma", "fitting");
Mantid::Geometry::Parameter_sptr gammaParam = paramMap.get(&(*instr), "Gamma", "fitting");
TS_ASSERT(gammaParam);
if(gammaParam)
{
Expand All @@ -369,6 +367,80 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
Poco::File("TestWorskpace.irf").remove();
}

//----------------------------------------------------------------------------------------------
/** Test that when the workspace property is used with multiple workspaces
** that parameters are correctly loaded into this workspaces, according to the fullprof banks.
* The GEM instrument is used
*/
void test_multiworkspace()
{
// Generate file
string filename("TestMultiWorskpace.irf");
generate3BankIrfFile(filename);

// Load workspace group wsName with 3 workspaces
load_GEM(3);

// Set up algorithm to load into the workspace
LoadFullprofResolution alg;
alg.initialize();

alg.setProperty("Filename", filename);
alg.setPropertyValue("Banks", "2-4");
alg.setProperty("Workspace", wsName);

// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());

// Check parameters in workspace
// The workspace is a workspace group with three members corresponding to the three banks in the IRF file
WorkspaceGroup_sptr gws;
gws = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(wsName);
// 1st Workspace - bank 2
Workspace_sptr wsi = gws->getItem(0) ;
auto ws1 = boost::dynamic_pointer_cast<MatrixWorkspace>(wsi);
Mantid::Geometry::ParameterMap& paramMap1 = ws1->instrumentParameters();
boost::shared_ptr<const Mantid::Geometry::Instrument> instr1 = ws1->getInstrument();
// 2nd Workspace - bank 3
wsi = gws->getItem(1) ;
auto ws2 = boost::dynamic_pointer_cast<MatrixWorkspace>(wsi);
Mantid::Geometry::ParameterMap& paramMap2 = ws2->instrumentParameters();
boost::shared_ptr<const Mantid::Geometry::Instrument> instr2 = ws2->getInstrument();
// 3rd Workspace - bank 4
wsi = gws->getItem(2) ;
auto ws3 = boost::dynamic_pointer_cast<MatrixWorkspace>(wsi);
Mantid::Geometry::ParameterMap& paramMap3 = ws3->instrumentParameters();
boost::shared_ptr<const Mantid::Geometry::Instrument> instr3 = ws3->getInstrument();


// Check Beta0 parameter in each workspace
Mantid::Geometry::Parameter_sptr beta0Param1 = paramMap1.get(&(*instr1), "Beta0", "fitting");
TS_ASSERT(beta0Param1);
if(beta0Param1)
{
const Mantid::Geometry::FitParameter& fitParam = beta0Param1->value<Mantid::Geometry::FitParameter>();
TS_ASSERT_DELTA( boost::lexical_cast<double>(fitParam.getFormula()), 6.251096, 0.0000001);
}
Mantid::Geometry::Parameter_sptr beta0Param2 = paramMap2.get(&(*instr2), "Beta0", "fitting");
TS_ASSERT(beta0Param2);
if(beta0Param2)
{
const Mantid::Geometry::FitParameter& fitParam = beta0Param2->value<Mantid::Geometry::FitParameter>();
TS_ASSERT_DELTA( boost::lexical_cast<double>(fitParam.getFormula()), 7.251096, 0.0000001);
}
Mantid::Geometry::Parameter_sptr beta0Param3 = paramMap3.get(&(*instr3), "Beta0", "fitting");
TS_ASSERT(beta0Param3);
if(beta0Param3)
{
const Mantid::Geometry::FitParameter& fitParam = beta0Param3->value<Mantid::Geometry::FitParameter>();
TS_ASSERT_DELTA( boost::lexical_cast<double>(fitParam.getFormula()), 3.012, 0.0000001);
}

// Clean
Poco::File("TestMultiWorskpace.irf").remove();
}

//----------------------------------------------------------------------------------------------
/** Test that algorithm does not run,
* if neither the OutputTableWorkspace nor Workspace
Expand Down Expand Up @@ -510,20 +582,30 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite


//----------------------------------------------------------------------------------------------
/** Generate a GEM workspace group with one member
/** Generate a GEM workspace group with specified number of workspaces.
*/
void load_GEM()
void load_GEM( size_t numberOfWorkspaces)
{
LoadInstrument loaderGEM;

TS_ASSERT_THROWS_NOTHING(loaderGEM.initialize());

//create a workspace with some sample data
wsName = "LoadFullprofResolutionWorkspace";
Workspace_sptr ws = WorkspaceFactory::Instance().create("Workspace2D",1,1,1);
Workspace2D_sptr ws2D = boost::dynamic_pointer_cast<Workspace2D>(ws);
WorkspaceGroup_sptr gws(new API::WorkspaceGroup);
gws->addWorkspace( ws2D );
if(numberOfWorkspaces == 1)
{
wsName = "LoadFullprofResolutionWorkspace";
}
else
{
wsName = "LoadFullprofResolutionMultiWorkspace";
}
for (size_t i=0; i < numberOfWorkspaces; ++i)
{
Workspace_sptr ws = WorkspaceFactory::Instance().create("Workspace2D",1,1,1);
Workspace2D_sptr ws2D = boost::dynamic_pointer_cast<Workspace2D>(ws);
gws->addWorkspace( ws2D );
}

//put this workspace in the data service
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().add(wsName, gws));
Expand Down Expand Up @@ -689,7 +771,7 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
ofile << "! Gam-2 Gam-1 Gam-0 \n";
ofile << "GAMMA 0.000 0.000 0.000 \n";
ofile << "! alph0 beta0 alph1 beta1 \n";
ofile << "ALFBE 0.000008 6.251096 0.000000 0.000000 \n";
ofile << "ALFBE 0.000008 7.251096 0.000000 0.000000 \n";
ofile << "! alph0t beta0t alph1t beta1t \n";
ofile << "ALFBT 0.010156 85.918922 0.000000 0.000000 \n";
ofile << "END \n";
Expand Down

0 comments on commit 3a43141

Please sign in to comment.