Skip to content

Commit

Permalink
Re #4106. Events have to be spread randomly within bin
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 11, 2011
1 parent 679b2dc commit 2db843f
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The Precount option will count the number of events in each pixel before allocat
#include <fstream>
#include <sstream>
#include <boost/algorithm/string/replace.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real.hpp>
#include <Poco/File.h>
#include <Poco/Path.h>
#include "MantidKernel/VisibleWhenProperty.h"
Expand Down Expand Up @@ -1698,11 +1700,15 @@ void LoadEventNexus::loadTimeOfFlight(const std::string &nexusfilename, DataObje
* Load the time of flight data. file must have open the group containing "time_of_flight" data set.
* @param file :: The nexus file to read from.
* @param WS :: The event workspace to write to.
* @param binsName :: bins name
* @param start_wi :: First workspace index to process
* @param end_wi :: Last workspace index to process
*/
void LoadEventNexus::loadTimeOfFlightData(::NeXus::File& file, DataObjects::EventWorkspace_sptr WS,
const std::string& binsName,size_t start_wi, size_t end_wi)
{
file.openData(binsName);
// time of flights of events
std::vector<float> tof;
file.getData(tof);
// todo: try to find if tof can be reduced to just 3 numbers: start, end and dt
Expand All @@ -1711,31 +1717,47 @@ void LoadEventNexus::loadTimeOfFlightData(::NeXus::File& file, DataObjects::Even
end_wi = WS->getNumberHistograms();
}

// random number generator
boost::mt19937 rand_gen;

// loop over spectra
for(size_t wi = start_wi; wi < end_wi; ++wi)
{
EventList& event_list = WS->getEventList(wi);
// sort the events
event_list.sortTof();
std::vector<TofEvent>& events = event_list.getEvents();
size_t i = 0;
size_t n = tof.size() - 1;
size_t m = 0;
for(std::vector<TofEvent>::iterator ev = events.begin(); ev != events.end(); ++ev)
if (events.empty()) continue;
size_t n = tof.size();
// iterate over the events and time bins
std::vector<TofEvent>::iterator ev = events.begin();
std::vector<TofEvent>::iterator ev_end = events.end();
for(size_t i = 1; i < n; ++i)
{
while(i < n && double(tof[i]) < ev->m_tof)
double right = double(tof[i]);
// find the right boundary for the current event
if( right < ev->m_tof )
{
if (m > 0)
{// m events in this bin
double dx = double(tof[i+1] - tof[i]) / (double(m) + 1);
double x = double(tof[i]);
for(std::vector<TofEvent>::iterator ev1 = ev - m; ev1 != ev; ++ev1, x += dx)
{
ev1->m_tof = x;
}
continue;
}
// count events which have the same right boundary
size_t m = 0;
while(ev != ev_end && ev->m_tof < right)
{
++ev;
++m; // count events in the i-th bin
}

if (m > 0)
{// m events in this bin
boost::uniform_real<> distribution(double(tof[i-1]),right);
// spread the events uniformly inside the bin
for(std::vector<TofEvent>::iterator ev1 = ev - m; ev1 != ev; ++ev1)
{
ev1->m_tof = distribution(rand_gen);
}
m = 0;
++i;
}
++m; // count events in the i-th bin

}
}
file.closeData();
Expand Down

0 comments on commit 2db843f

Please sign in to comment.