Skip to content
Snippets Groups Projects
Commit 4d150d52 authored by Savici, Andrei T.'s avatar Savici, Andrei T.
Browse files

AverageLogData, test is just a placeholder. Refs #460

parent 05bb6e20
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ set ( SRC_FILES
src/ApplyDeadTimeCorr.cpp
src/ApplyDetailedBalance.cpp
src/ApplyTransmissionCorrection.cpp
src/AverageLogData.cpp
src/BinaryOperateMasks.cpp
src/BinaryOperation.cpp
src/CalMuonDeadTime.cpp
......@@ -217,6 +218,7 @@ set ( INC_FILES
inc/MantidAlgorithms/ApplyDeadTimeCorr.h
inc/MantidAlgorithms/ApplyDetailedBalance.h
inc/MantidAlgorithms/ApplyTransmissionCorrection.h
inc/MantidAlgorithms/AverageLogData.h
inc/MantidAlgorithms/BinaryOperateMasks.h
inc/MantidAlgorithms/BinaryOperation.h
inc/MantidAlgorithms/CalMuonDeadTime.h
......@@ -419,6 +421,7 @@ set ( TEST_FILES
ApplyDeadTimeCorrTest.h
ApplyDetailedBalanceTest.h
ApplyTransmissionCorrectionTest.h
AverageLogDataTest.h
BinaryOperateMasksTest.h
BinaryOperationTest.h
CalculateEfficiencyTest.h
......
#ifndef MANTID_ALGORITHMS_AVERAGELOGDATA_H_
#define MANTID_ALGORITHMS_AVERAGELOGDATA_H_
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
namespace Mantid
{
namespace Algorithms
{
/** AverageLogData : TODO: DESCRIPTION
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport AverageLogData : public API::Algorithm
{
public:
AverageLogData();
virtual ~AverageLogData();
virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;
private:
virtual void initDocs();
void init();
void exec();
};
} // namespace Algorithms
} // namespace Mantid
#endif /* MANTID_ALGORITHMS_AVERAGELOGDATA_H_ */
/*WIKI*
TODO: Enter a full wiki-markup description of your algorithm here. You can then use the Build/wiki_maker.py script to generate your full wiki page.
*WIKI*/
#include "MantidAlgorithms/AverageLogData.h"
#include "MantidKernel/TimeSeriesProperty.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AverageLogData)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
AverageLogData::AverageLogData()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
AverageLogData::~AverageLogData()
{
}
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string AverageLogData::name() const { return "AverageLogData";};
/// Algorithm's version for identification. @see Algorithm::version
int AverageLogData::version() const { return 1;};
/// Algorithm's category for identification. @see Algorithm::category
const std::string AverageLogData::category() const { return "DataHandling\\Logs";}
//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void AverageLogData::initDocs()
{
std::string summary = "Computes the proton charge averaged value of a given log.";
this->setWikiSummary(summary);
this->setOptionalMessage(summary);
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void AverageLogData::init()
{
declareProperty(new WorkspaceProperty<API::MatrixWorkspace>("InputWorkspace","",Direction::Input), "An input workspace that contains a Sample log property, and a proton charge property.");
declareProperty("LogName", "", "Name of the log to be averaged");
declareProperty("FixZero", true, "If checked, the proton charge and the log value time series are assumed to start at the same moment.");
declareProperty("Average", EMPTY_DBL(),"", Direction::Output);
declareProperty("Error", EMPTY_DBL(),"", Direction::Output);
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void AverageLogData::exec()
{
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
std::string logname = this->getProperty("LogName");
if (logname.empty())
{
throw std::runtime_error("Failed to supply a LogName");
}
if (!inputWS->run().hasProperty(logname))
{
throw std::runtime_error("There is no property "+logname+" in the workspace.");
}
Kernel::TimeSeriesProperty<double> * slog = dynamic_cast<Kernel::TimeSeriesProperty<double> *>( inputWS->run().getLogData(logname) );
if(!slog)
{
throw std::runtime_error("Problem reading property "+logname);
}
Kernel::TimeSeriesProperty<double> * pclog = dynamic_cast<Kernel::TimeSeriesProperty<double> *>( inputWS->run().getLogData("proton_charge") );
if(!pclog)
{
throw std::runtime_error("Problem reading the proton charge property");
}
double average(0),error(0),protoncharge(0);
double diffSeconds=static_cast<double>((slog->firstTime()-pclog->firstTime()).total_nanoseconds())*1e-9;
if (getProperty("FixZero"))
{
diffSeconds=0.;
}
std::vector<double> stime=slog->timesAsVectorSeconds();
std::vector<double> svalue=slog->valuesAsVector();
std::vector<double> pctime=pclog->timesAsVectorSeconds();
std::vector<double> pcvalue=pclog->valuesAsVector();
stime.push_back(EMPTY_DBL());
svalue.push_back(0.0);
pctime.push_back(EMPTY_DBL()*1.1);//larger than stime
pcvalue.push_back(0.0);
std::vector<double>::iterator istime=stime.begin(),isvalue=svalue.begin(), ipctime=pctime.begin(),ipcvalue=pcvalue.begin();
for(;istime<(--stime.end());++istime)
{
//ignore all proton pulses before the lowest time for the log
while((*ipctime)<(*istime)+diffSeconds)
{
++ipctime;
++ipcvalue;
}
// add together proton pulses before the current log time and the next log time
while((*ipctime)<(*(istime+1))+diffSeconds)
{
protoncharge+=(*ipcvalue);
average+=(*ipcvalue)*(*isvalue);
error+=(*ipcvalue)*(*isvalue)*(*isvalue);
++ipctime;
++ipcvalue;
}
++isvalue;
}
if(protoncharge!=0)
{
g_log.warning()<<"Proton charge is 0. Average and standard deviations are NANs"<<std::endl;
}
g_log.debug()<<"Sum = "<<average<<std::endl<<"Sum squares = "<<error<<std::endl<<"PC = "<<protoncharge<<std::endl;
average/=protoncharge;
error/=protoncharge;
error=std::sqrt(std::fabs(error-average*average));
g_log.information()<<"Average value of "<<logname<<" is "<<average<<" +/- "<<error<<std::endl;
setProperty("Average",average);
setProperty("Error",error);
}
} // namespace Algorithms
} // namespace Mantid
#ifndef MANTID_ALGORITHMS_AverageLogDATATEST_H_
#define MANTID_ALGORITHMS_AverageLogDATATEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/AverageLogData.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
using Mantid::Algorithms::AverageLogData;
class AverageLogDataTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static AverageLogDataTest *createSuite() { return new AverageLogDataTest(); }
static void destroySuite( AverageLogDataTest *suite ) { delete suite; }
void test_Init()
{
AverageLogData alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
// Name of the output workspace.
/* std::string WSName("AverageLogDataTest_OutputWS");
AverageLogData alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("REPLACE_PROPERTY_NAME_HERE!!!!", "value") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
/*
// Retrieve the workspace from data service. TODO: Change to your desired type
Workspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace>(outWSName) );
TS_ASSERT(ws);
if (!ws) return;
// TODO: Check the results
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);*/
}
};
#endif /* MANTID_ALGORITHMS_AverageLogDATATEST_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment