Skip to content

Commit

Permalink
Continue to refactor events filtering. Refs #5056.
Browse files Browse the repository at this point in the history
(1) Using GenerateEventsFilter() and FilterEvents() to refactor
FilterByTime.
(2) Clean the codes of GenerateEventsFilter and FilterEvents.
  • Loading branch information
wdzhou committed Apr 27, 2012
1 parent d8d5f79 commit 6056279
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 35 deletions.
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/Algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set ( SRC_FILES
src/FilterBadPulses.cpp
src/FilterByLogValue.cpp
src/FilterByTime.cpp
src/FilterByTime2.cpp
src/FilterEvents.cpp
src/FilterEventsHighFrequency.cpp
src/FindCenterOfMassPosition.cpp
Expand Down Expand Up @@ -277,6 +278,7 @@ set ( INC_FILES
inc/MantidAlgorithms/FilterBadPulses.h
inc/MantidAlgorithms/FilterByLogValue.h
inc/MantidAlgorithms/FilterByTime.h
inc/MantidAlgorithms/FilterByTime2.h
inc/MantidAlgorithms/FilterEvents.h
inc/MantidAlgorithms/FilterEventsHighFrequency.h
inc/MantidAlgorithms/FindCenterOfMassPosition.h
Expand Down Expand Up @@ -462,6 +464,7 @@ set ( TEST_FILES
test/FFTTest.h
test/FilterBadPulsesTest.h
test/FilterByLogValueTest.h
test/FilterByTime2Test.h
test/FilterByTimeTest.h
test/FilterEventsHighFrequencyTest.h
test/FilterEventsTest.h
Expand Down Expand Up @@ -539,8 +542,8 @@ set ( TEST_FILES
test/SmoothDataTest.h
test/SmoothNeighboursTest.h
test/SofQW2Test.h
test/SofQW3Test.h
test/SofQWTest.h
test/SofQW3Test.h
test/SolidAngleTest.h
test/SortEventsTest.h
test/SpatialGroupingTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef MANTID_ALGORITHMS_FILTERBYTIME2_H_
#define MANTID_ALGORITHMS_FILTERBYTIME2_H_

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


namespace Mantid
{
namespace Algorithms
{

/** FilterByTime2 : TODO: DESCRIPTION
@date 2012-04-25
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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport FilterByTime2 : public API::Algorithm
{
public:
FilterByTime2();
virtual ~FilterByTime2();

/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "FilterByTime";};
/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return 2;};
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Events\\EventFiltering";}

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
// Implement abstract Algorithm methods
void init();
void exec();

};


} // namespace Algorithms
} // namespace Mantid

#endif /* MANTID_ALGORITHMS_FILTERBYTIME2_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ namespace Algorithms
size_t searchValue(std::vector<double> dataranges, double value);

DataObjects::EventWorkspace_const_sptr mEventWS;
// DataObjects::SplittersWorkspace_sptr mSplitters;
API::ISplittersWorkspace_sptr mSplitters;
API::ITableWorkspace_sptr mFilterInfoWS;

Expand Down
176 changes: 176 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/FilterByTime2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include "MantidAlgorithms/FilterByTime2.h"
#include "MantidKernel/System.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidDataObjects/SplittersWorkspace.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;

namespace Mantid
{
namespace Algorithms
{


//----------------------------------------------------------------------------------------------
/** Constructor
*/
FilterByTime2::FilterByTime2()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
FilterByTime2::~FilterByTime2()
{
}

void FilterByTime2::initDocs()
{

}

//-----------------------------------------------------------------------
void FilterByTime2::init()
{
std::string commonHelp("\nYou can only specify the relative or absolute start/stop times, not both.");

declareProperty(
new WorkspaceProperty<DataObjects::EventWorkspace>("InputWorkspace","",Direction::Input),
"An input event workspace" );

declareProperty(
new WorkspaceProperty<DataObjects::EventWorkspace>("OutputWorkspace","",Direction::Output),
"The name to use for the output workspace" );

auto min = boost::make_shared<BoundedValidator<double> >();
min->setLower(0.0);
declareProperty("StartTime", 0.0, min,
"The start time, in seconds, since the start of the run. Events before this time are filtered out. \nThe time of the first pulse (i.e. the first entry in the ProtonCharge sample log) is used as the zero. " + commonHelp);

declareProperty("StopTime", 0.0,
"The stop time, in seconds, since the start of the run. Events at or after this time are filtered out. \nThe time of the first pulse (i.e. the first entry in the ProtonCharge sample log) is used as the zero. " + commonHelp);

std::string absoluteHelp("Specify date and UTC time in ISO8601 format, e.g. 2010-09-14T04:20:12." + commonHelp);
declareProperty("AbsoluteStartTime", "",
"Absolute start time; events before this time are filtered out. " + absoluteHelp );

declareProperty("AbsoluteStopTime", "",
"Absolute stop time; events at of after this time are filtered out. " + absoluteHelp );

}

//-----------------------------------------------------------------------
/** Executes the algorithm
*/
void FilterByTime2::exec()
{
DataObjects::EventWorkspace_const_sptr inWS = this->getProperty("InputWorkspace");
if (!inWS)
{
g_log.error() << "Input is not EventWorkspace" << std::endl;
throw std::invalid_argument("Input is not EventWorksapce");
}
else
{
g_log.debug() << "DB5244 InputWorkspace Name = " << inWS->getName() << std::endl;
}

double starttime = this->getProperty("StartTime");
double stoptime = this->getProperty("StopTime");
std::string absstarttime = this->getProperty("AbsoluteStartTime");
std::string absstoptime = this->getProperty("AbsoluteStopTime");

std::string start, stop;
if ( (absstarttime != "") && (absstoptime != "") && (starttime <= 0.0) && (stoptime <= 0.0) )
{
// Use the absolute string
start = absstarttime;
stop = absstoptime;
}
else if ( (absstarttime != "" || absstoptime != "") && (starttime > 0.0 || stoptime > 0.0) )
{
throw std::invalid_argument("It is not allowed to provide both absolute time and relative time.");
}
else
{
// Use second
std::stringstream ss;
ss << starttime;
start = ss.str();
std::stringstream ss2;
ss2 << stoptime;
stop = ss2.str();
}

// 1. Generate Filters
g_log.debug() << "\nDB441: About to generate Filter. StartTime = " << starttime <<
" StopTime = " << stoptime << std::endl;

API::Algorithm_sptr genfilter = createSubAlgorithm("GenerateEventsFilter", 0.0, 20.0, true, 1);
genfilter->initialize();
genfilter->setPropertyValue("InputWorkspace", inWS->getName());
genfilter->setPropertyValue("OutputWorkspace", "FilterWS");
// genfilter->setPropertyValue("SplittersInformationWorkspace", "InfoWS");
genfilter->setProperty("StartTime", start);
genfilter->setProperty("StopTime", stop);
genfilter->setProperty("TimeType", "Seconds");

bool sucgen = genfilter->execute();
if (!sucgen)
{
g_log.error() << "Unable to generate event filters" << std::endl;
throw std::runtime_error("Unable to generate event filters");
}
else
{
g_log.debug() << "Filters are generated. " << std::endl;
}

DataObjects::SplittersWorkspace_sptr filterWS = genfilter->getProperty("OutputWorkspace");
if (!filterWS)
{
g_log.error() << "Unable to retrieve generated SplittersWorkspace object from AnalysisDataService." << std::endl;
throw std::runtime_error("Unable to retrieve Splittersworkspace. ");
}
// AnalysisDataService::Instance().addOrReplace("FilterWS", filterWS);

// 2. Filter events
g_log.debug() << "\nAbout to filter events. " << std::endl;

API::Algorithm_sptr filter = createSubAlgorithm("FilterEvents", 20.0, 100.0, true, 1);
filter->initialize();
filter->setPropertyValue("InputWorkspace", inWS->getName());
filter->setPropertyValue("OutputWorkspaceBaseName", "ResultWS");
// filter->setPropertyValue("InputSplittersWorkspace", "FilterWS");
filter->setProperty("InputSplittersWorkspace", filterWS);
filter->setProperty("FilterByPulseTime", true);

bool sucfilt = filter->execute();
if (!sucfilt)
{
g_log.error() << "Unable to filter events" << std::endl;
throw std::runtime_error("Unable to filter events");
}
else
{
g_log.debug() << "Filter events is successful. " << std::endl;
}

DataObjects::EventWorkspace_sptr optws = filter->getProperty("OutputWorkspace_0");

/*
DataObjects::EventWorkspace_sptr optws =
boost::dynamic_pointer_cast<DataObjects::EventWorkspace>(AnalysisDataService::Instance().retrieve("ResultWS_0"));
*/

this->setProperty("OutputWorkspace", optws);

}


} // namespace Mantid
} // namespace Algorithms
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/Algorithms/src/FilterEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ namespace Algorithms
this->declareProperty(new API::WorkspaceProperty<DataObjects::EventWorkspace>(parname.str(), wsname.str(), Direction::Output), "Output");
this->setProperty(parname.str(), optws);

std::cout << "DB9141 Output Workspace: Group = " << wsgroup << " Property Name = " << wsname.str() << std::endl;
g_log.debug() << "DB9141 Output Workspace: Group = " << wsgroup << " Property Name = " << parname.str() <<
" Workspace name = " << wsname.str() <<
" with Number of events = " << optws->getNumberEvents() << std::endl;
} // ENDFOR


Expand Down

0 comments on commit 6056279

Please sign in to comment.