Skip to content

Commit

Permalink
SortEvents by PulseTime+TOF. Refs #4255.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Dec 7, 2011
1 parent 6ea22df commit efec7a1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Code/Mantid/Framework/Algorithms/src/SortEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Mantid
std::vector<std::string> propOptions;
propOptions.push_back("X Value");
propOptions.push_back("Pulse Time");
propOptions.push_back("Pulse Time + TOF");
declareProperty("SortBy", "X Value",new ListValidator(propOptions),
"How to sort the events:\n"
" X Value: the x-position of the event in each pixel (typically Time of Flight).\n"
Expand All @@ -70,7 +71,7 @@ namespace Mantid
// Get the input workspace
EventWorkspace_sptr eventW = getProperty("InputWorkspace");
//And other properties
bool sortByTof = (getPropertyValue("SortBy") == "X Value");
std::string sortoption = getPropertyValue("SortBy");

//------- EventWorkspace ---------------------------
const size_t histnumber = eventW->getNumberHistograms();
Expand All @@ -79,7 +80,10 @@ namespace Mantid
Progress prog(this,0.0,1.0, histnumber);

DataObjects::EventSortType sortType = DataObjects::TOF_SORT;
if (!sortByTof) sortType = DataObjects::PULSETIME_SORT;
if (sortoption == "Pulse Time")
sortType = DataObjects::PULSETIME_SORT;
else if (sortoption == "Pulse Time + TOF")
sortType = DataObjects::PULSETIMETOF_SORT;

//This runs the SortEvents algorithm in parallel
eventW->sortAll(sortType, &prog);
Expand Down
36 changes: 36 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/SortEventsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ class SortEventsTest : public CxxTest::TestSuite
}


void testSortByPulseTimeTOF()
{
std::string wsName("test_inEvent4");
EventWorkspace_sptr test_in = WorkspaceCreationHelper::CreateRandomEventWorkspace(NUMBINS, NUMPIXELS);
AnalysisDataService::Instance().add(wsName, test_in);

SortEvents sort;
sort.initialize();
sort.setPropertyValue("InputWorkspace",wsName);
sort.setPropertyValue("SortBy", "Pulse Time + TOF");
TS_ASSERT(sort.execute());
TS_ASSERT(sort.isExecuted());

EventWorkspace_const_sptr outWS = boost::dynamic_pointer_cast<const EventWorkspace>(AnalysisDataService::Instance().retrieve(wsName));
std::vector<TofEvent> ve = outWS->getEventList(0).getEvents();
TS_ASSERT_EQUALS( ve.size(), NUMBINS);
for (size_t i=0; i<ve.size()-1; i++){
bool less = true;
if (ve[i].pulseTime() > ve[i+1].pulseTime()){
less = false;
}
else if ( (ve[i].pulseTime()==ve[i+1].pulseTime()) && (ve[i].tof()>=ve[i+1].tof()) ){
less = false;
}
if (!less){
std::cout << "Event " << i << " is later than Event " << i+1 << std::endl;
std::cout << "Event " << i << ": " << ve[i].pulseTime() << " + " << ve[i].tof() << std::endl;
}
TS_ASSERT(less);
}

AnalysisDataService::Instance().remove(wsName);

}


};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace DataObjects


/// How the event list is sorted.
enum EventSortType {UNSORTED, TOF_SORT, PULSETIME_SORT};
enum EventSortType {UNSORTED, TOF_SORT, PULSETIME_SORT, PULSETIMETOF_SORT};

//==========================================================================================
/** @class Mantid::DataObjects::EventList
Expand Down Expand Up @@ -165,6 +165,7 @@ class DLLExport EventList : public Mantid::API::IEventList
inline void addEventQuickly(const TofEvent &event)
{
this->events.push_back(event);
this->order = UNSORTED;
}

// --------------------------------------------------------------------------
Expand All @@ -174,6 +175,7 @@ class DLLExport EventList : public Mantid::API::IEventList
inline void addEventQuickly(const WeightedEvent &event)
{
this->weightedEvents.push_back(event);
this->order = UNSORTED;
}

// --------------------------------------------------------------------------
Expand All @@ -183,15 +185,14 @@ class DLLExport EventList : public Mantid::API::IEventList
inline void addEventQuickly(const WeightedEventNoTime &event)
{
this->weightedEventsNoTime.push_back(event);
this->order = UNSORTED;
}



Mantid::API::EventType getEventType() const;

void switchTo(Mantid::API::EventType newType);

// bool hasWeights() const;
// bool hasWeights() const;

WeightedEvent getEvent(size_t event_number);

Expand Down Expand Up @@ -227,6 +228,7 @@ class DLLExport EventList : public Mantid::API::IEventList
void sortTof4() const;

void sortPulseTime() const;
void sortPulseTimeTOF() const;

bool isSortedByTof() const;

Expand Down
49 changes: 48 additions & 1 deletion Code/Mantid/Framework/DataObjects/src/EventList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "MantidDataObjects/EventWorkspaceMRU.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include <functional>
#include <limits>
#include <math.h>
Expand All @@ -22,7 +23,6 @@ namespace DataObjects
using Kernel::DateAndTime;
using namespace Mantid::API;


//==========================================================================
/// --------------------- TofEvent Comparators ----------------------------------
//==========================================================================
Expand All @@ -45,6 +45,21 @@ namespace DataObjects
return (e1.pulseTime() < e2.pulseTime());
}

/** Compare two events' FRAME id, return true if e1 should be before e2.
* @param e1 :: first event
* @param e2 :: second event
* */
bool compareEventPulseTimeTOF(const TofEvent& e1, const TofEvent& e2){

if (e1.pulseTime() < e2.pulseTime()){
return true;
}
else if ( (e1.pulseTime() == e2.pulseTime()) && (e1.m_tof < e2.m_tof) ){
return true;
}

return false;
}



Expand Down Expand Up @@ -750,6 +765,9 @@ namespace DataObjects
{
this->sortPulseTime();
}
else if (order == PULSETIMETOF_SORT){
this->sortPulseTimeTOF();
}
else
{
throw runtime_error("Invalid sort type in EventList::sort(EventSortType)");
Expand Down Expand Up @@ -1081,6 +1099,35 @@ namespace DataObjects
this->order = PULSETIME_SORT;
}

/*
* Sort events by pulse time + TOF
* (the absolute time)
*/
void EventList::sortPulseTimeTOF() const
{
if (this->order == PULSETIMETOF_SORT){
// already ordered.
return;
}

switch (eventType)
{
case TOF:
std::sort(events.begin(), events.end(), compareEventPulseTimeTOF);
break;
case WEIGHTED:
std::sort(events.begin(), events.end(), compareEventPulseTimeTOF);
break;
case WEIGHTED_NOTIME:
// Do nothing; there is no time to sort
break;
}

// Save
this->order = PULSETIMETOF_SORT;

}

// --------------------------------------------------------------------------
/** Return true if the event list is sorted by TOF */
bool EventList::isSortedByTof() const
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/EventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,10 @@ namespace DataObjects

//-----------------------------------------------------------------------------

/*
* Review each event list to get the sort type
* If any 2 have different order type, then be unsorted
*/
EventSortType EventWorkspace::getSortType() const
{
size_t size = this->data.size();
Expand Down

0 comments on commit efec7a1

Please sign in to comment.