Skip to content

Commit

Permalink
Re #9958. Improved invalid spectra list handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Sep 16, 2014
1 parent 7d7c7da commit 2bdb8c2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 29 deletions.
Expand Up @@ -95,11 +95,12 @@ namespace Mantid

/// number of spectra for each time regime
std::vector<int> m_numberOfSpectra;
//int m_numberOfSpectra;

/// total number of spectra
int m_totalNumberOfSpectra;

/// number of bins for each time regime
std::vector<int> m_numberOfBins;
//int m_numberOfBins;

/// list of spectra to read or empty to read all
std::vector<specid_t> m_specList;
Expand All @@ -109,7 +110,6 @@ namespace Mantid

/// Store the bin boundaries for each time regime
std::vector<boost::shared_ptr<MantidVec>> m_bins;
//boost::shared_ptr<MantidVec> m_bins;

/// Detector IDs
std::vector<int> m_detIDs;
Expand Down
12 changes: 6 additions & 6 deletions Code/Mantid/Framework/LiveData/src/FakeISISHistoDAE.cpp
Expand Up @@ -136,17 +136,17 @@ class TestServerConnection: public Poco::Net::TCPServerConnection
{
int period = 0;
int istart = spec;
const int ns1 = m_nSpectra + 1;
const int nb1 = m_nBins + 1;
const int ns1 = m_nSpectra + m_nMonitors + 1;
if ( m_nPeriods > 1 )
{
period = spec / ns1;
istart = spec - period * ns1;
if ( period >= m_nPeriods || istart + nos > ns1 )
{
sendOK();
}
}
if ( period >= m_nPeriods || istart + nos > ns1 )
{
sendOK();
}
const int nb1 = (istart <= m_nSpectra? m_nBins : m_nMonitorBins) + 1;
const int ndata = nos * nb1;
std::vector<int> data( ndata );
for(int i = 0; i < nos; ++i)
Expand Down
28 changes: 18 additions & 10 deletions Code/Mantid/Framework/LiveData/src/ISISHistoDataListener.cpp
Expand Up @@ -50,7 +50,7 @@ namespace LiveData

auto validator = boost::make_shared<Kernel::ArrayBoundedValidator<int>>();
validator->setLower( 1 );
declareProperty(new Kernel::ArrayProperty<int>("Periods",validator),
declareProperty(new Kernel::ArrayProperty<int>("PeriodList",validator),
"An optional list of periods to load. If blank, all available periods will be loaded.");
}

Expand Down Expand Up @@ -99,7 +99,7 @@ namespace LiveData
}

m_numberOfPeriods = getInt("NPER");
g_log.debug() << "Number of periods " << m_numberOfPeriods << std::endl;
g_log.information() << "Number of periods " << m_numberOfPeriods << std::endl;

// Set the spectra list to load
std::vector<specid_t> spectra = getProperty("SpectraList");
Expand All @@ -109,7 +109,7 @@ namespace LiveData
}

// Set the period list to load
std::vector<int> periodList = getProperty("Periods");
std::vector<int> periodList = getProperty("PeriodList");
if ( !periodList.empty() )
{
setPeriods( periodList );
Expand Down Expand Up @@ -393,14 +393,16 @@ namespace LiveData
specid_t next = m_specList[i];
if ( next - m_specList[i-1] > 1 || static_cast<int>(i - i0) >= maxNumberOfSpectra )
{
int n = static_cast<int>( i - i0 );
index.push_back( spec );
count.push_back( static_cast<int>( i - i0 ) );
count.push_back( n );
i0 = i;
spec = next;
}
}
int n = static_cast<int>( m_specList.size() - i0 );
index.push_back( spec );
count.push_back( static_cast<int>( m_specList.size() - i0 ) );
count.push_back( n );
}

}
Expand All @@ -423,7 +425,7 @@ namespace LiveData
dims[0] = count;
dims[1] = numberOfBins + 1;

int spectrumIndex = index + period * (m_numberOfSpectra[m_timeRegime] + 1);
int spectrumIndex = index + period * (m_totalNumberOfSpectra + 1);
if (IDCgetdat(m_daeHandle, spectrumIndex, count, dataBuffer.data(), dims, &ndims) != 0)
{
g_log.error("Unable to read DATA from DAE " + m_daeName);
Expand Down Expand Up @@ -564,7 +566,7 @@ namespace LiveData

for(auto mon = m_monitorSpectra.begin(); mon != m_monitorSpectra.end(); ++mon)
{
g_log.debug() << "Monitor spectrum " << *mon << std::endl;
g_log.information() << "Monitor spectrum " << *mon << std::endl;
}

const std::string detRTCB = rtcbPrefix + "_" + boost::lexical_cast<std::string>( m_monitorSpectra.front() );
Expand All @@ -585,11 +587,16 @@ namespace LiveData
}
}
}
g_log.debug() << "Number of time regimes " << m_bins.size() << std::endl;
for(size_t i = 0; i < m_bins.size(); ++i)
g_log.information() << "Number of time regimes " << m_bins.size() << std::endl;
assert( m_numberOfBins.size() == m_numberOfSpectra.size() );
for(size_t i = 0; i < m_numberOfBins.size(); ++i)
{
g_log.debug() << "Number of bins in time regime " << i + 1 << " is " << m_bins[i]->size()-1 << std::endl;
g_log.information() << "Number of bins in time regime " << i + 1 << " is " << m_numberOfBins[i] << std::endl;
g_log.information() << "Number of spectra in time regime " << i + 1 << " is " << m_numberOfSpectra[i] << std::endl;
}

// find the total number of spectra in all regimes
m_totalNumberOfSpectra = std::accumulate(m_numberOfSpectra.begin(),m_numberOfSpectra.end(),0);
}

/**
Expand All @@ -608,6 +615,7 @@ namespace LiveData
for( auto specIt = m_specList.begin(); specIt != m_specList.end(); ++specIt )
{
bool isMonitor = std::find(m_monitorSpectra.begin(),m_monitorSpectra.end(), *specIt) != m_monitorSpectra.end();
if ( !isMonitor && *specIt > m_totalNumberOfSpectra ) throw std::invalid_argument("Invalid spectra index is found: " + boost::lexical_cast<std::string>(*specIt));
int specRegime = isMonitor? 1 : 0;
if ( regime < 0 )
{
Expand Down
107 changes: 97 additions & 10 deletions Code/Mantid/Framework/LiveData/test/ISISHistoDataListenerTest.h
Expand Up @@ -250,15 +250,16 @@ class ISISHistoDataListenerTest : public CxxTest::TestSuite

FakeISISHistoDAE dae;
dae.initialize();
dae.setProperty("NSpectra",30);
dae.setProperty("NPeriods",4);
auto res = dae.executeAsync();

Kernel::PropertyManager props;
props.declareProperty(new Kernel::ArrayProperty<int>("Periods"));
props.declareProperty(new Kernel::ArrayProperty<int>("PeriodList"));
std::vector<int> periods(2);
periods[0] = 2;
periods[1] = 3;
props.setProperty( "Periods", periods );
props.setProperty( "PeriodList", periods );

auto listener = Mantid::API::LiveListenerFactory::Instance().create("TESTHISTOLISTENER",true,&props);
TS_ASSERT( listener );
Expand All @@ -269,17 +270,17 @@ class ISISHistoDataListenerTest : public CxxTest::TestSuite
auto group = boost::dynamic_pointer_cast<WorkspaceGroup>( outWS );
TS_ASSERT( group );
TS_ASSERT_EQUALS( group->size(), 2 );
auto ws1 = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(0) );
TS_ASSERT( ws1 );
auto ws2 = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(1) );
TS_ASSERT( ws2 );

auto y = ws1->readY( 2 );
auto ws = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(0) );
TS_ASSERT( ws );
auto y = ws->readY( 2 );
TS_ASSERT_EQUALS( y[0], 1003 );
TS_ASSERT_EQUALS( y[5], 1003 );
TS_ASSERT_EQUALS( y[29], 1003 );

y = ws2->readY( 2 );
ws = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(1) );
TS_ASSERT( ws );
y = ws->readY( 2 );
TS_ASSERT_EQUALS( y[0], 2003 );
TS_ASSERT_EQUALS( y[5], 2003 );
TS_ASSERT_EQUALS( y[29], 2003 );
Expand All @@ -291,6 +292,92 @@ class ISISHistoDataListenerTest : public CxxTest::TestSuite
#endif
}

void test_Receiving_selected_monitors()
{
#ifdef _WIN32
FacilityHelper::ScopedFacilities loadTESTFacility("IDFs_for_UNIT_TESTING/UnitTestFacilities.xml", "TEST");

FakeISISHistoDAE dae;
dae.initialize();
dae.setProperty("NSpectra",10);
dae.setProperty("NPeriods",4);
dae.setProperty("NBins",20);
auto res = dae.executeAsync();

Kernel::PropertyManager props;
props.declareProperty(new Kernel::ArrayProperty<int>("SpectraList"));
props.declareProperty(new Kernel::ArrayProperty<int>("PeriodList"));
props.setProperty( "PeriodList", "1,3" );
// FakeISISHistoDAE has 3 monitors with spectra numbers NSpectra+1, NSpectra+2, NSpectra+2
props.setProperty( "SpectraList", "11-13" );

auto listener = Mantid::API::LiveListenerFactory::Instance().create("TESTHISTOLISTENER",true,&props);
TS_ASSERT( listener );
TSM_ASSERT("Listener has failed to connect", listener->isConnected() );
if (!listener->isConnected()) return;

auto outWS = listener->extractData();
auto group = boost::dynamic_pointer_cast<WorkspaceGroup>( outWS );
TS_ASSERT( group );
TS_ASSERT_EQUALS( group->size(), 2 );

auto ws = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(0) );
TS_ASSERT( ws );
auto y = ws->readY( 2 );
// monitors in FakeISISHistoDAE have twice the number of bins of normal spectra
TS_ASSERT_EQUALS( y.size(), 40 );
TS_ASSERT_EQUALS( y[0], 13 );
TS_ASSERT_EQUALS( y[5], 13 );
TS_ASSERT_EQUALS( y[29], 13 );

ws = boost::dynamic_pointer_cast<MatrixWorkspace>( group->getItem(1) );
TS_ASSERT( ws );
y = ws->readY( 2 );
TS_ASSERT_EQUALS( y.size(), 40 );
TS_ASSERT_EQUALS( y[0], 2013 );
TS_ASSERT_EQUALS( y[5], 2013 );
TS_ASSERT_EQUALS( y[29], 2013 );

dae.cancel();
res.wait();
#else
TS_ASSERT( true );
#endif
}

void test_invalid_spectra_numbers()
{
#ifdef _WIN32
FacilityHelper::ScopedFacilities loadTESTFacility("IDFs_for_UNIT_TESTING/UnitTestFacilities.xml", "TEST");

FakeISISHistoDAE dae;
dae.initialize();
dae.setProperty("NSpectra",10);
dae.setProperty("NPeriods",4);
dae.setProperty("NBins",20);
auto res = dae.executeAsync();

Kernel::PropertyManager props;
props.declareProperty(new Kernel::ArrayProperty<int>("SpectraList"));
props.declareProperty(new Kernel::ArrayProperty<int>("PeriodList"));
props.setProperty( "PeriodList", "1,3" );
// FakeISISHistoDAE has 3 monitors with spectra numbers NSpectra+1, NSpectra+2, NSpectra+2
props.setProperty( "SpectraList", "14-17" );

auto listener = Mantid::API::LiveListenerFactory::Instance().create("TESTHISTOLISTENER",true,&props);
TS_ASSERT( listener );
TSM_ASSERT("Listener has failed to connect", listener->isConnected() );
if (!listener->isConnected()) return;

TS_ASSERT_THROWS( auto outWS = listener->extractData(), std::invalid_argument );

dae.cancel();
res.wait();
#else
TS_ASSERT( true );
#endif
}


void test_no_period()
{
Expand All @@ -303,11 +390,11 @@ class ISISHistoDataListenerTest : public CxxTest::TestSuite
auto res = dae.executeAsync();

Kernel::PropertyManager props;
props.declareProperty(new Kernel::ArrayProperty<int>("Periods"));
props.declareProperty(new Kernel::ArrayProperty<int>("PeriodList"));
std::vector<int> periods(2);
periods[0] = 2;
periods[1] = 5; // this period doesn't exist in dae
props.setProperty( "Periods", periods );
props.setProperty( "PeriodList", periods );

TS_ASSERT_THROWS( auto listener = Mantid::API::LiveListenerFactory::Instance().create("TESTHISTOLISTENER",true,&props), std::invalid_argument );

Expand Down

0 comments on commit 2bdb8c2

Please sign in to comment.