Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/8985_muon_load_auto_grou…
Browse files Browse the repository at this point in the history
…ping'
  • Loading branch information
Anders-Markvardsen committed Feb 21, 2014
2 parents d8033fd + fd8ddad commit 73b0320
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 14 deletions.
Expand Up @@ -50,14 +50,17 @@ namespace WorkflowAlgorithms
void init();
void exec();

// We dont' want processGroups to be called
virtual bool checkGroups() { return false; }

/// Returns a workspace for the first period as specified using FirstPeriod property.
MatrixWorkspace_sptr getFirstPeriodWS(WorkspaceGroup_sptr ws);

/// Returns a workspace for the second period as specified using SecondPeriod property.
MatrixWorkspace_sptr getSecondPeriodWS(WorkspaceGroup_sptr ws);

/// Groups specified workspace according to specified DetectorGroupingTable.
MatrixWorkspace_sptr groupWorkspace(MatrixWorkspace_sptr ws);
MatrixWorkspace_sptr groupWorkspace(MatrixWorkspace_sptr ws, TableWorkspace_sptr grouping);

/// Applies dead time correction to the workspace.
MatrixWorkspace_sptr applyDTC(MatrixWorkspace_sptr ws, TableWorkspace_sptr dt);
Expand Down
63 changes: 50 additions & 13 deletions Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoad.cpp
Expand Up @@ -85,10 +85,13 @@ namespace WorkflowAlgorithms
declareProperty("ApplyDeadTimeCorrection", false,
"Whether dead time correction should be applied to loaded workspace");
declareProperty(new WorkspaceProperty<TableWorkspace>("CustomDeadTimeTable", "",Direction::Input,
PropertyMode::Optional), "Table with dead time information. See LoadMuonNexus for format expected.");

declareProperty(new WorkspaceProperty<TableWorkspace>("DetectorGroupingTable","",Direction::Input),
"Table with detector grouping information. See LoadMuonNexus for format expected.");
PropertyMode::Optional),
"Table with dead time information. See LoadMuonNexus for format expected."
"If not specified -- algorithm tries to use dead times stored in the data file.");
declareProperty(new WorkspaceProperty<TableWorkspace>("DetectorGroupingTable","",Direction::Input,
PropertyMode::Optional),
"Table with detector grouping information. See LoadMuonNexus for format expected. "
"If not specified -- algorithm tries to get grouping information from the data file.");

declareProperty("TimeZero", EMPTY_DBL(), "Value used for Time Zero correction.");
declareProperty(new ArrayProperty<double>("RebinParams"),
Expand Down Expand Up @@ -121,10 +124,22 @@ namespace WorkflowAlgorithms
{
const std::string filename = getProperty("Filename");

// Whether Dead Time Correction should be applied
bool applyDtc = getProperty("ApplyDeadTimeCorrection");

// If DetectorGropingTable not specified - use auto-grouping
bool autoGroup = ! static_cast<TableWorkspace_sptr>( getProperty("DetectorGroupingTable") );

// Load the file
IAlgorithm_sptr load = createChildAlgorithm("LoadMuonNexus");
load->setProperty("Filename", filename);
load->setProperty("DeadTimeTable", "__YouDontSeeMeIAmNinja"); // Name is not used (child alg.)

if ( applyDtc ) // Load dead times as well, if needed
load->setProperty("DeadTimeTable", "__NotUsed");

if ( autoGroup ) // Load grouping as well, if needed
load->setProperty("DetectorGroupingTable", "__NotUsed");

load->execute();

Workspace_sptr loadedWS = load->getProperty("OutputWorkspace");
Expand Down Expand Up @@ -154,9 +169,7 @@ namespace WorkflowAlgorithms
throw std::runtime_error("Loaded workspace is of invalid type");
}


// Deal with dead time correction (if required
bool applyDtc = getProperty("ApplyDeadTimeCorrection");
// Deal with dead time correction (if required)
if ( applyDtc )
{
TableWorkspace_sptr deadTimes = getProperty("CustomDeadTimeTable");
Expand All @@ -172,6 +185,7 @@ namespace WorkflowAlgorithms
}
else if ( auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(loadedDeadTimes) )
{
// XXX: using first table only for now. Can use the one for appropriate period if necessary.
deadTimes = boost::dynamic_pointer_cast<TableWorkspace>( group->getItem(0) );
}

Expand All @@ -185,11 +199,35 @@ namespace WorkflowAlgorithms
secondPeriodWS = applyDTC(secondPeriodWS, deadTimes);
}

TableWorkspace_sptr grouping;

if ( autoGroup )
{
Workspace_sptr loadedGrouping = load->getProperty("DetectorGroupingTable");

if ( auto table = boost::dynamic_pointer_cast<TableWorkspace>(loadedGrouping) )
{
grouping = table;
}
else if ( auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(loadedGrouping) )
{
// XXX: using first table only for now. Can use the one for appropriate period if necessary.
grouping = boost::dynamic_pointer_cast<TableWorkspace>( group->getItem(0) );
}

if ( ! grouping )
throw std::runtime_error("File doesn't contain grouping information");
}
else
{
grouping = getProperty("DetectorGroupingTable");
}

// Deal with grouping
firstPeriodWS = groupWorkspace(firstPeriodWS);
firstPeriodWS = groupWorkspace(firstPeriodWS, grouping);

if ( secondPeriodWS )
secondPeriodWS = groupWorkspace(secondPeriodWS);
secondPeriodWS = groupWorkspace(secondPeriodWS, grouping);

// Correct bin values
double loadedTimeZero = load->getProperty("TimeZero");
Expand Down Expand Up @@ -278,12 +316,11 @@ namespace WorkflowAlgorithms
/**
* Groups specified workspace according to specified DetectorGroupingTable.
* @param ws :: Workspace to group
* @param grouping :: Detector grouping table to use
* @return Grouped workspace
*/
MatrixWorkspace_sptr MuonLoad::groupWorkspace(MatrixWorkspace_sptr ws)
MatrixWorkspace_sptr MuonLoad::groupWorkspace(MatrixWorkspace_sptr ws, TableWorkspace_sptr grouping)
{
TableWorkspace_sptr grouping = getProperty("DetectorGroupingTable");

IAlgorithm_sptr group = createChildAlgorithm("MuonGroupDetectors");
group->setProperty("InputWorkspace", ws);
group->setProperty("DetectorGroupingTable", grouping);
Expand Down
36 changes: 36 additions & 0 deletions Code/Mantid/Framework/WorkflowAlgorithms/test/MuonLoadTest.h
Expand Up @@ -262,6 +262,42 @@ class MuonLoadTest : public CxxTest::TestSuite
TS_ASSERT( ! alg.isExecuted() );
}

void test_autoGrouping()
{
ScopedWorkspace output;

try
{
MuonLoad alg;
alg.setRethrows(true);
alg.initialize();
alg.setPropertyValue("Filename", "emu00006473.nxs");
alg.setProperty("OutputType", "GroupCounts");
alg.setProperty("GroupIndex", 0);
alg.setPropertyValue("OutputWorkspace", output.name());
alg.execute();
}
catch(std::exception& e)
{
TS_FAIL(e.what());
return;
}

MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>( output.retrieve() );

TS_ASSERT(ws);
if ( ! ws )
return; // Nothing to check

TS_ASSERT_EQUALS( ws->getNumberHistograms(), 1 );

TS_ASSERT_EQUALS( ws->readY(0)[0], 461 );
TS_ASSERT_EQUALS( ws->readY(0)[1000], 192 );
TS_ASSERT_EQUALS( ws->readY(0)[1998], 1 );
}

private:

TableWorkspace_sptr createGroupingTable(const std::vector<int>& group1, const std::vector<int>& group2)
{
auto t = boost::make_shared<TableWorkspace>();
Expand Down

0 comments on commit 73b0320

Please sign in to comment.