Skip to content

Commit

Permalink
Made change to print more information. Refs #8291.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Dec 2, 2013
1 parent 92f6cc1 commit 6342f85
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 83 deletions.
Expand Up @@ -95,7 +95,7 @@ class DLLExport CheckWorkspacesMatch : public API::Algorithm
void doPeaksComparison(API::IPeaksWorkspace_sptr tws1, API::IPeaksWorkspace_sptr tws2);
void doTableComparison(API::ITableWorkspace_const_sptr tws1, API::ITableWorkspace_const_sptr tws2);
void doMDComparison(API::Workspace_sptr w1, API::Workspace_sptr w2);
bool checkEventLists(DataObjects::EventWorkspace_const_sptr ews1, DataObjects::EventWorkspace_const_sptr ews2);
bool compareEventWorkspaces(DataObjects::EventWorkspace_const_sptr ews1, DataObjects::EventWorkspace_const_sptr ews2);
bool checkData(API::MatrixWorkspace_const_sptr ws1, API::MatrixWorkspace_const_sptr ws2);
bool checkAxes(API::MatrixWorkspace_const_sptr ws1, API::MatrixWorkspace_const_sptr ws2);
bool checkSpectraMap(API::MatrixWorkspace_const_sptr ws1, API::MatrixWorkspace_const_sptr ws2);
Expand All @@ -104,8 +104,9 @@ class DLLExport CheckWorkspacesMatch : public API::Algorithm
bool checkSample(const API::Sample& sample1, const API::Sample& sample2);
bool checkRunProperties(const API::Run& run1, const API::Run& run2);

/// Compare 2 EventsList
int compareEventsList(const DataObjects::EventList &el1, const DataObjects::EventList &el2,
double tolTof, double tolPulse, bool printdetail,
double tolTof, double tolWeight, int64_t tolPulse, int compareoption,
size_t& numdiffpulse, size_t& numdifftof, size_t& numdiffboth) const;

std::string m_result; ///< the result string
Expand Down
220 changes: 139 additions & 81 deletions Code/Mantid/Framework/Algorithms/src/CheckWorkspacesMatch.cpp
Expand Up @@ -172,7 +172,7 @@ void CheckWorkspacesMatch::init()
// declareProperty("CheckAllSpectra", false, "If true, all spectra in event workspace will be checked.");
// Should use 'CheckAllData' instead

declareProperty("NumberMismatchedSpectra2Print", 1, "Number of mismatched spectra from lowest to be listed. ");
declareProperty("NumberMismatchedSpectraToPrint", 1, "Number of mismatched spectra from lowest to be listed. ");

declareProperty("WorkspaceIndex", EMPTY_INT(), "Mismatched spectra that will be printed out in details. ");

Expand Down Expand Up @@ -293,7 +293,7 @@ void CheckWorkspacesMatch::doComparison()
prog = new Progress(this, 0.0, 1.0, numhist*5);

// Compare event lists to see whether 2 event workspaces match each other
if ( ! checkEventLists(ews1, ews2) ) return;
if ( ! compareEventWorkspaces(ews1, ews2) ) return;
}
else
{
Expand Down Expand Up @@ -327,29 +327,46 @@ void CheckWorkspacesMatch::doComparison()
return;
}

bool CheckWorkspacesMatch::checkEventLists(DataObjects::EventWorkspace_const_sptr ews1, DataObjects::EventWorkspace_const_sptr ews2)
//------------------------------------------------------------------------------------------------
/** Check whether 2 event lists are identical
*/
bool CheckWorkspacesMatch::compareEventWorkspaces(DataObjects::EventWorkspace_const_sptr ews1,
DataObjects::EventWorkspace_const_sptr ews2)
{
bool checkallspectra = getProperty("CheckAllData");
int numspec2print = getProperty("NumberMismatchedSpectra2Print");
int numspec2print = getProperty("NumberMismatchedSpectraToPrint");
int wsindex2print = getProperty("WorkspaceIndex");

// Both will end up sorted anyway
ews1->sortAll(PULSETIMETOF_SORT, prog);
ews2->sortAll(PULSETIMETOF_SORT, prog);

// Compare number of spectra
if (ews1->getNumberHistograms() != ews2->getNumberHistograms())
{
m_result = "Mismatched number of histograms.";
return false;
}

// determine the tolerance for "tof" attribute of events
double ToleranceTOF = Tolerance;
// actual time-of flight is 50 nanoseconds
// Both will end up sorted anyway
ews1->sortAll(PULSETIMETOF_SORT, prog);
ews2->sortAll(PULSETIMETOF_SORT, prog);

// Determine the tolerance for "tof" attribute and "weight" of events
double toleranceWeight = Tolerance; // Standard tolerance
int64_t tolerancePulse = 1;
double toleranceTOF = 0.;
if ((ews1->getAxis(0)->unit()->label() == "microsecond")
|| (ews2->getAxis(0)->unit()->label() == "microsecond"))
ToleranceTOF = 0.05;
g_log.notice() << "TOF Tolerance = " << ToleranceTOF << "\n";
&& (ews2->getAxis(0)->unit()->label() == "microsecond"))
{
// actual time-of flight is 50 nanoseconds
toleranceTOF = 0.05;
}
else
{
std::stringstream errss;
errss << "Event workspace has unit as " << ews1->getAxis(0)->unit()->label() << " and "
<< ews2->getAxis(0)->unit()->label() << ". Tolerance of TOF is set to 0.05 still. ";
g_log.warning(errss.str());
toleranceTOF = 0.05;
}
g_log.notice() << "TOF Tolerance = " << toleranceTOF << "\n";

bool mismatchedEvent = false;
int mismatchedEventWI = 0;
Expand All @@ -362,10 +379,11 @@ bool CheckWorkspacesMatch::checkEventLists(DataObjects::EventWorkspace_const_spt
size_t numunequalbothevents = 0;

std::vector<int> vec_mismatchedwsindex;
//PARALLEL_FOR2(ews1, ews2)

PARALLEL_FOR2(ews1, ews2)
for (int i=0; i<static_cast<int>(ews1->getNumberHistograms()); i++)
{
// PARALLEL_START_INTERUPT_REGION
{
PARALLEL_START_INTERUPT_REGION
prog->report("EventLists");
if (!mismatchedEvent || checkallspectra) // This guard will avoid checking unnecessarily
{
Expand All @@ -377,73 +395,80 @@ bool CheckWorkspacesMatch::checkEventLists(DataObjects::EventWorkspace_const_spt
g_log.information() << "Spectrum " << i << " is set to print out in details. " << "\n";
}

if (!el1.equals(el2, ToleranceTOF, Tolerance, 1))
if (!el1.equals(el2, toleranceTOF, toleranceWeight, tolerancePulse))
{
size_t tempnumtof, tempnumpulse, tempnumboth;

int tempnumunequal;

if (printdetail)
{
// Comparison with detailed output
// FIXME - What is Tolerance and print-detail?
tempnumunequal = compareEventsList(el1, el2, ToleranceTOF, Tolerance, true, tempnumtof, tempnumpulse,
tempnumboth);
}
int compareoption;
if (checkallspectra && (printdetail))
compareoption = 1;
else if (checkallspectra)
compareoption = 0;
else
{
// Quick comparison:
// tempnumunequal = el1.compare(el2, ToleranceTOF, Tolerance, false, tempnumtof, tempnumpulse, tempnumboth);
tempnumunequal = compareEventsList(el1, el2, ToleranceTOF, Tolerance, false, tempnumtof, tempnumpulse,
tempnumboth);
}
compareoption = -1;

if (tempnumunequal == -1)
{
// 2 spectra have different number of events
++ numunequalnumeventsspectra;
}
else if (tempnumunequal == -2)
{
// 2 spectra have different type of events
++ numunequaltoftypespectra;
}
else if (tempnumunequal > 0)
{
// 2 spectra have some events different to each other
numunequalevents += static_cast<size_t>(tempnumunequal);
numunequaltofevents += tempnumtof;
numunequalpulseevents += tempnumpulse;
numunequalbothevents += tempnumboth;
}
tempnumunequal = compareEventsList(el1, el2, toleranceTOF, toleranceWeight, tolerancePulse, compareoption,
tempnumpulse, tempnumtof, tempnumboth);

mismatchedEvent = true;
mismatchedEventWI = i;
vec_mismatchedwsindex.push_back(i);
PARALLEL_CRITICAL(CheckWorkspacesMatch)
{
bool unequal = false;
if (tempnumunequal == -1)
{
// 2 spectra have different number of events
++ numunequalnumeventsspectra;
unequal = true;
}
else if (tempnumunequal == -2)
{
// 2 spectra have different type of events
++ numunequaltoftypespectra;
unequal = true;
}
else if (tempnumunequal > 0)
{
// 2 spectra have some events different to each other
numunequalevents += static_cast<size_t>(tempnumunequal);
numunequaltofevents += tempnumtof;
numunequalpulseevents += tempnumpulse;
numunequalbothevents += tempnumboth;
unequal = true;
}

if (unequal)
vec_mismatchedwsindex.push_back(i);
} // Parallel critical region

}
}
// PARALLEL_END_INTERUPT_REGION
PARALLEL_END_INTERUPT_REGION
}
// PARALLEL_CHECK_INTERUPT_REGION
PARALLEL_CHECK_INTERUPT_REGION

bool wsmatch;
if ( mismatchedEvent)
{
std::ostringstream mess;
mess << "Total " << numunequalevents << " unequal events between 2 workspaces. " << "\n";
if (numunequalnumeventsspectra > 0)
mess << "Total " << numunequalnumeventsspectra << " spectra have different number of events. "
<< "\n";
if (numunequaltoftypespectra > 0)
mess << "Total " << numunequaltoftypespectra << " spectra have different type of TOF. " << "\n";

mess << "Total " << numunequalevents << " (in " << ews1->getNumberEvents() << ") events are differrent. "
<< numunequaltofevents << " have different TOF; " << numunequalpulseevents << " have different pulse time; "
<< numunequalbothevents << " have different in both TOF and pulse time. " << "\n";

if (checkallspectra)
{
if (numunequalnumeventsspectra > 0)
mess << "Total " << numunequalnumeventsspectra << " spectra have different number of events. "
<< "\n";
if (numunequaltoftypespectra > 0)
mess << "Total " << numunequaltoftypespectra << " spectra have different type of TOF. " << "\n";

mess << "Total " << numunequalevents << " (in " << ews1->getNumberEvents() << ") events are differrent. "
<< numunequaltofevents << " have different TOF; " << numunequalpulseevents << " have different pulse time; "
<< numunequalbothevents << " have different in both TOF and pulse time. " << "\n";

mess << "Mismatched event lists include " << vec_mismatchedwsindex.size() << " of "
<< "total " << ews1->getNumberHistograms() << " spectra. " << "\n";

std::sort(vec_mismatchedwsindex.begin(), vec_mismatchedwsindex.end());
if (static_cast<int>(vec_mismatchedwsindex.size()) < numspec2print)
numspec2print = static_cast<int>(vec_mismatchedwsindex.size());
for (int i = 0; i < numspec2print; ++i)
Expand All @@ -455,13 +480,19 @@ bool CheckWorkspacesMatch::checkEventLists(DataObjects::EventWorkspace_const_spt
}
else
{
mess << "Mismatched event list at workspace index " << mismatchedEventWI;
mess << "Quick comparison shows 2 workspaces do not match. "
<< "First found mismatched event list is at workspace index " << mismatchedEventWI;
}
m_result = mess.str();
return false;
wsmatch = false;
}
else
{
m_result = "Success!";
wsmatch = true;
}

return true;
return wsmatch;
}

/** Checks that the data matches
Expand Down Expand Up @@ -845,21 +876,28 @@ bool CheckWorkspacesMatch::checkRunProperties(const API::Run& run1, const API::R
}

//------------------------------------------------------------------------------------------------
/** Compare 2 different events list with detailed information output
/** Compare 2 different events list with detailed information output (Linear)
* el1 :: event list 1
* el2 :: event list 2
* tolfTOF :: tolerance of Time-of-flight (in micro-second)
* tolWeight :: tolerance of weight for weighted neutron events
* tolPulse :: tolerance of pulse time (in nanosecond)
* compareoption :: option for comparing. -1: simple, 0: full but no print, 1: full with print
* @return :: int. -1: different number of events; -2: different type of events; N > 0 : some
* events are not same
*/
int CheckWorkspacesMatch::compareEventsList(const EventList& el1, const EventList& el2,
double tolTof, double tolPulse, bool printdetail,
double tolTof, double tolWeight, int64_t tolPulse, int compareoption,
size_t& numdiffpulse, size_t& numdifftof, size_t& numdiffboth) const
{
// Initialize
numdiffpulse = 0;
numdifftof = 0;
numdiffboth = 0;
size_t numdiffweight = 0;

// Compar
int returnint = 1;
int returnint = 0;
if (el1.getNumberEvents() != el2.getNumberEvents())
{
// Number of events are different
Expand All @@ -870,13 +908,13 @@ int CheckWorkspacesMatch::compareEventsList(const EventList& el1, const EventLis
// Types of events are different
returnint = -2;
}
else if (printdetail)
else if (compareoption >= 0)
{
// Compare event by event
// Compare event by event including all events
const std::vector<TofEvent>& events1 = el1.getEvents();
const std::vector<TofEvent>& events2 = el2.getEvents();

int64_t tolpulsens = static_cast<int64_t>(tolPulse*1.E9);
EventType etype = el1.getEventType();

size_t numevents = events1.size();
for (size_t i = 0; i < numevents; ++i)
Expand All @@ -887,7 +925,7 @@ int CheckWorkspacesMatch::compareEventsList(const EventList& el1, const EventLis

bool diffpulse = false;
bool difftof = false;
if (labs(e1.pulseTime().totalNanoseconds() - e2.pulseTime().totalNanoseconds()) > tolpulsens)
if (labs(e1.pulseTime().totalNanoseconds() - e2.pulseTime().totalNanoseconds()) > tolPulse)
{
diffpulse = true;
++ numdiffpulse;
Expand All @@ -900,21 +938,41 @@ int CheckWorkspacesMatch::compareEventsList(const EventList& el1, const EventLis
if (diffpulse & difftof)
++ numdiffboth;

if (etype == WEIGHTED)
{
if (fabs(e1.weight() - e2.weight()) > tolWeight)
++ numdiffweight;
}

bool same = (!diffpulse) && (!difftof);
if (!same)
{
returnint += 1;
std::stringstream outss;
outss << "Spectrum ? Event " << i << ": ";
if (diffpulse)
outss << "Diff-Pulse: " << e1.pulseTime() << " vs. " << e2.pulseTime() << "; ";
if (difftof)
outss << "Diff-TOF: " << e1.tof() << " vs. " << e2.tof() << ";";

g_log.information(outss.str());
if (compareoption > 0)
{
std::stringstream outss;
outss << "Spectrum ? Event " << i << ": ";
if (diffpulse)
outss << "Diff-Pulse: " << e1.pulseTime() << " vs. " << e2.pulseTime() << "; ";
if (difftof)
outss << "Diff-TOF: " << e1.tof() << " vs. " << e2.tof() << ";";

g_log.information(outss.str());
}
}
else if (numdiffweight > 0)
{
throw std::runtime_error("Detected mismatched events in weight. Implement this branch ASAP.");
}
}
}
else
{
// Simple compare i.e., stopping comparing after first unmatched.
bool beequal = el1.equals(el2, tolTof, tolWeight, tolPulse);
if (!beequal)
returnint = 1;
}


#if 0
Expand Down

0 comments on commit 6342f85

Please sign in to comment.