Skip to content

Commit

Permalink
Refs #8506. Applying correction from data.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Nov 27, 2013
1 parent 80a4803 commit 80ca087
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#ifndef MANTID_WORKFLOWALGORITHMS_MUONLOADCORRECTED_H_
#define MANTID_WORKFLOWALGORITHMS_MUONLOADCORRECTED_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidKernel/System.h"

namespace Mantid
{
namespace WorkflowAlgorithms
{
using namespace Kernel;
using namespace API;
using namespace DataObjects;

/** MuonLoadCorrected : loads Muon data with Dead Time Correction applied.
Expand Down Expand Up @@ -45,6 +49,19 @@ namespace WorkflowAlgorithms
virtual void initDocs();
void init();
void exec();

/// Attempts to load dead time table from given Muon Nexus file
Workspace_sptr loadDeadTimesFromNexus(const std::string& filename, int numPeriods = 1);

/// Applies dead time table to a workspace
Workspace_sptr applyDtc(Workspace_sptr ws, Workspace_sptr dt);

/// Runs ApplyDeadTimeCorre algorithm
MatrixWorkspace_sptr runApplyDtc(MatrixWorkspace_sptr ws, TableWorkspace_sptr dtt);

/// Creates Dead Time Table from the given list of dead times.
TableWorkspace_sptr createDeadTimeTable( std::vector<double>::const_iterator begin,
std::vector<double>::const_iterator end);
};


Expand Down
121 changes: 117 additions & 4 deletions Code/Mantid/Framework/WorkflowAlgorithms/src/MuonLoadCorrected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ Load Muon data with Dead Time Correction applied. Part of the Muon workflow.
#include "MantidWorkflowAlgorithms/MuonLoadCorrected.h"

#include "MantidAPI/FileProperty.h"
#include "MantidAPI/TableRow.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidKernel/ListValidator.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid
{
namespace WorkflowAlgorithms
{
using namespace Kernel;
using namespace API;
using namespace DataObjects;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(MuonLoadCorrected)
Expand Down Expand Up @@ -51,7 +55,8 @@ namespace WorkflowAlgorithms
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
/**
* Initialize the algorithm's properties.
*/
void MuonLoadCorrected::init()
{
Expand All @@ -74,7 +79,8 @@ namespace WorkflowAlgorithms
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
/**
* Execute the algorithm.
*/
void MuonLoadCorrected::exec()
{
Expand All @@ -84,9 +90,116 @@ namespace WorkflowAlgorithms
loadAlg->setPropertyValue("Filename", filename);
loadAlg->executeAsChildAlg();

Workspace_sptr outWS = loadAlg->getProperty("OutputWorkspace");
Workspace_sptr loadedWS = loadAlg->getProperty("OutputWorkspace");

std::string dtcType = getPropertyValue("DtcType");

if ( dtcType == "None" )
{
setProperty("OutputWorkspace", loadedWS);
}
else if ( dtcType == "FromData" )
{
int numPeriods = 1; // For now

Workspace_sptr deadTimes = loadDeadTimesFromNexus(filename, numPeriods);
Workspace_sptr correctedWS = applyDtc(loadedWS, deadTimes);
setProperty("OutputWorkspace", correctedWS);
}
else
{
// TODO
}
}

/**
* Attempts to load dead time table from given Muon Nexus file.
* @param filename :: Path of the Muon Nexus file to load dead times from
* @param numPeriods :: Number of data collection periods
* @return TableWorkspace when one period, otherwise a group of TableWorkspace-s with dead times
*/
Workspace_sptr MuonLoadCorrected::loadDeadTimesFromNexus(const std::string& filename, int numPeriods)
{
NeXus::NXRoot root(filename);

NeXus::NXFloat deadTimes = root.openNXFloat("run/instrument/detector/deadtimes");
deadTimes.load();

std::vector<double> dtVector;
dtVector.reserve( deadTimes.size() );

for (int i = 0; i < deadTimes.size(); i++)
dtVector.push_back(deadTimes[i]);

return createDeadTimeTable( dtVector.begin(), dtVector.end() );
}

/**
* Creates Dead Time Table from the given list of dead times.
*
* @param begin :: Iterator to the first element of the data to use
* @param end :: Iterator to the last element of the data to use
* @return TableWorkspace created using the data
*/
TableWorkspace_sptr MuonLoadCorrected::createDeadTimeTable( std::vector<double>::const_iterator begin,
std::vector<double>::const_iterator end)
{
TableWorkspace_sptr deadTimeTable = boost::dynamic_pointer_cast<TableWorkspace>(
WorkspaceFactory::Instance().createTable("TableWorkspace") );

deadTimeTable->addColumn("int","spectrum");
deadTimeTable->addColumn("double","dead-time");

int s = 1; // Current spectrum

for(auto it = begin; it != end; it++)
{
TableRow row = deadTimeTable->appendRow();
row << s++ << *it;
}

return deadTimeTable;
}

/**
* Applies dead time correction to a workspace.
* @param ws :: Workspace to apply correction to
* @param dt :: Dead Times to use
* @return Corrected workspace
*/
Workspace_sptr MuonLoadCorrected::applyDtc(Workspace_sptr ws, Workspace_sptr dt)
{
if ( auto wsMatrix = boost::dynamic_pointer_cast<MatrixWorkspace>(ws) )
{
if ( auto dtTable = boost::dynamic_pointer_cast<TableWorkspace>(dt) )
{
return runApplyDtc(wsMatrix, dtTable);
}
else
throw std::invalid_argument("Can't apply group of dead time tables to a single workspace");
}

return Workspace_sptr();
}


/**
* Runs ApplyDeadTimeCorr algorithm.
* @param ws :: Workspace to apply correction to
* @param dt :: Dead Times to use
* @return Corrected workspace
*/
MatrixWorkspace_sptr MuonLoadCorrected::runApplyDtc(MatrixWorkspace_sptr ws, TableWorkspace_sptr dt)
{
IAlgorithm_sptr applyDtc = createChildAlgorithm("ApplyDeadTimeCorr");

applyDtc->setProperty<MatrixWorkspace_sptr>("InputWorkspace", ws);
applyDtc->setProperty<ITableWorkspace_sptr>("DeadTimeTable", dt);
applyDtc->execute();

MatrixWorkspace_sptr output = applyDtc->getProperty("OutputWorkspace");

setProperty("OutputWorkspace", outWS);
return output;
}

} // namespace WorkflowAlgorithms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ class MuonLoadCorrectedTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( ws->readE(15)[1000], 2.646, 0.001 );
TS_ASSERT_DELTA( ws->readE(31)[1999], 0, 0.001);
}

void test_singlePeriod_fromData()
{
MuonLoadCorrected alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )

TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "emu00006473.nxs") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("DTCType", "FromData") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", g_outWSName) );

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

MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(g_outWSName) );
TS_ASSERT(ws);
if (!ws) return;

TS_ASSERT_EQUALS( ws->blocksize(), 2000 );
TS_ASSERT_EQUALS( ws->getNumberHistograms(), 32 );

TS_ASSERT_DELTA( ws->readY(0)[0], 52.0007, 0.0001);
TS_ASSERT_DELTA( ws->readY(7)[500], 166.017, 0.001);
TS_ASSERT_DELTA( ws->readY(15)[1000], 6.99998, 0.00001);
TS_ASSERT_DELTA( ws->readY(20)[1500], 1.000002, 0.000001);
TS_ASSERT_EQUALS( ws->readY(31)[1999], 0);

TS_ASSERT_DELTA( ws->readX(0)[0], -0.254, 0.001 );
TS_ASSERT_DELTA( ws->readX(15)[1000], 15.746, 0.001 );
TS_ASSERT_DELTA( ws->readX(31)[2000], 31.741, 0.001 );

TS_ASSERT_DELTA( ws->readE(0)[0], 7.211, 0.001 );
TS_ASSERT_DELTA( ws->readE(15)[1000], 2.646, 0.001 );
TS_ASSERT_DELTA( ws->readE(31)[1999], 0, 0.001);
}

};


Expand Down

0 comments on commit 80ca087

Please sign in to comment.