Skip to content

Commit

Permalink
Refs #5021: save on memory by converting to events on the fly
Browse files Browse the repository at this point in the history
in the case of Workspace2D input. Also more tests
  • Loading branch information
Janik Zikovsky committed Apr 4, 2012
1 parent 953bd3b commit fe8316b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,17 @@ namespace MDEvents
}
else
{
// ----- Use the Histogram representation ------------
// ----- Workspace2D, or use the Histogram representation of EventWorkspace ------------
// Construct a new event list
EventList el;

// Create the events using the bins
const ISpectrum * inSpec = m_inWS->getSpectrum(workspaceIndex);
el.createFromHistogram(inSpec, true /* Generate zeros */, false /* no multiple events */, 1);
// If OneEventPerBin, generate exactly 1 event per bin, including zeros.
// If !OneEventPerBin, generate up to 10 events per bin, excluding zeros
el.createFromHistogram(inSpec, OneEventPerBin /* Generate zeros */,
!OneEventPerBin /* Multiple events */,
(OneEventPerBin ? 1 : 10) /* Max of this many events per bin */);

// Perform the conversion on this temporary event list
this->convertEventList<WeightedEventNoTime>(workspaceIndex, el);
Expand Down Expand Up @@ -247,8 +251,7 @@ namespace MDEvents
V3D Q_dir = mat * Q_dir_lab_frame;

// For speed we extract the components.
coord_t Q_dir_x = coord_t(Q_dir.X());
coord_t Q_dir_y = coord_t(Q_dir.Y());
coord_t Q_dir_x = coord_t(Q_dir.X()); coord_t Q_dir_y = coord_t(Q_dir.Y());
coord_t Q_dir_z = coord_t(Q_dir.Z());

// For lorentz correction, calculate sin(theta))^2
Expand Down Expand Up @@ -346,28 +349,6 @@ namespace MDEvents
if (m_inWS->getAxis(0)->unit()->unitID() != "TOF")
throw std::invalid_argument("Input event workspace's X axis must be in TOF units.");

if (!m_inEventWS && !OneEventPerBin)
{
// We DONT want 1 event per bin, but we didn't give an EventWorkspace
// So we need to convert to event workspace.
if (inWS2D)
{
// Convert from 2D to Event
IAlgorithm_sptr alg = createSubAlgorithm("ConvertToEventWorkspace", 0.0, 0.1, true);
alg->setProperty("InputWorkspace", inWS2D);
alg->setProperty("GenerateMultipleEvents", false); // One event per bin by default
alg->setPropertyValue("OutputWorkspace", getPropertyValue("InputWorkspace") + "_event");
alg->executeAsSubAlg();
EventWorkspace_sptr eventWS = alg->getProperty("OutputWorkspace");
m_inWS = boost::dynamic_pointer_cast<MatrixWorkspace>(eventWS);
if (!alg->isExecuted() || !m_inWS)
throw std::runtime_error("Error in ConvertToEventWorkspace. Cannot proceed.");
}
else
throw std::invalid_argument("InputWorkspace must be either an EventWorkspace or a Workspace2D (which will get converted to events).");
}


// Try to get the output workspace
IMDEventWorkspace_sptr i_out = getProperty("OutputWorkspace");
ws = boost::dynamic_pointer_cast<MDEventWorkspace<MDLeanEvent<3>,3> >( i_out );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class ConvertToDiffractionMDWorkspaceTest : public CxxTest::TestSuite



void do_test_MINITOPAZ(EventType type, size_t numTimesToAdd = 1, bool OneEventPerBin=false)
void do_test_MINITOPAZ(EventType type, size_t numTimesToAdd = 1,
bool OneEventPerBin=false, bool MakeWorkspace2D = false)
{

int numEventsPer = 100;
Expand All @@ -104,11 +105,11 @@ class ConvertToDiffractionMDWorkspaceTest : public CxxTest::TestSuite

// Rebin the workspace to have a manageable number bins
AnalysisDataService::Instance().addOrReplace("inputWS", in_ws);
if (OneEventPerBin)
FrameworkManager::Instance().exec("Rebin", 6,
"InputWorkspace", "inputWS",
"OutputWorkspace", "inputWS",
"Params", "0, 1e3, 16e3");
FrameworkManager::Instance().exec("Rebin", 8,
"InputWorkspace", "inputWS",
"OutputWorkspace", "inputWS",
"Params", "0, 500, 16e3",
"PreserveEvents", MakeWorkspace2D ? "0" : "1" );

ConvertToDiffractionMDWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
Expand All @@ -125,10 +126,9 @@ class ConvertToDiffractionMDWorkspaceTest : public CxxTest::TestSuite
TS_ASSERT(ws);
if (!ws) return;
size_t npoints = ws->getNPoints();
if (OneEventPerBin)
{ TS_ASSERT_LESS_THAN( 100000, npoints); }// # of points != # of bins exactly because some are off the extents
else
{ TS_ASSERT_LESS_THAN( 100000, npoints); }// Some points are left
// # of points != # of bins exactly because some are off the extents
TS_ASSERT_LESS_THAN( 100000, npoints);

TS_ASSERT_EQUALS( ws->getNumExperimentInfo(), 1);
TSM_ASSERT("ExperimentInfo object is valid", ws->getExperimentInfo(0) );

Expand Down Expand Up @@ -164,16 +164,32 @@ class ConvertToDiffractionMDWorkspaceTest : public CxxTest::TestSuite
do_test_MINITOPAZ(TOF);
}

void test_MINITOPAZ_Weighted()
{
do_test_MINITOPAZ(WEIGHTED);
}

void test_MINITOPAZ_addToExistingWorkspace()
{
do_test_MINITOPAZ(TOF, 2);
}

void test_MINITOPAZ_OneEventPerBin()
void test_MINITOPAZ_OneEventPerBin_fromEventWorkspace()
{
do_test_MINITOPAZ(TOF, 1, true);
do_test_MINITOPAZ(TOF, 1, true, false);
}

void test_MINITOPAZ_OneEventPerBin_fromWorkspace2D()
{
do_test_MINITOPAZ(TOF, 1, true, true);
}

void test_MINITOPAZ_fromWorkspace2D()
{
do_test_MINITOPAZ(TOF, 1, false, true);
}




};
Expand Down

0 comments on commit fe8316b

Please sign in to comment.