Commit d367d132 authored by Janik Zikovsky's avatar Janik Zikovsky
Browse files

Refs #3400: Forgot to add the files (as usual :P )

parent 3edf5f90
#ifndef MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1D_H_
#define MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1D_H_
#include "MantidKernel/System.h"
#include "MantidAPI/ISpectrum.h"
#include "MantidDataObjects/Histogram1D.h"
//#include "MantidDataObjects/AbsManagedWorkspace2D.h"
//#include "MantidDataObjects/ManagedDataBlock2D.h"
namespace Mantid
{
namespace DataObjects
{
// Forward declaration
class AbsManagedWorkspace2D;
/** A "managed" version of Histogram1D where the
* data is loaded only when required
@author Janik Zikovsky
@date 2011-07-26
Copyright © 2011 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ManagedHistogram1D : public Histogram1D //Mantid::API::ISpectrum
{
public:
friend class ManagedDataBlock2D;
ManagedHistogram1D(AbsManagedWorkspace2D * parentWS, size_t workspaceIndex);
ManagedHistogram1D(const ManagedHistogram1D&);
ManagedHistogram1D& operator=(const ManagedHistogram1D&);
virtual ~ManagedHistogram1D();
//------------------------------------------------------------------------
/** Method that retrieves the data from the disk
* if needed.
*/
void retrieveData() const;
/** Method that clears the data vectors to release
* memory when the spectrum can be released to disk.
*/
void releaseData();
// ------------ Y/E data setters -----------------------------
/// Sets the data.
void setData(const MantidVec& Y)
{ retrieveData(); refY.access()=Y; m_dirty=true; }
/// Sets the data and errors
void setData(const MantidVec& Y, const MantidVec& E)
{ retrieveData(); refY.access()=Y; refE.access()=E; m_dirty=true; }
/// Sets the data.
void setData(const MantidVecPtr& Y)
{ retrieveData(); refY=Y; m_dirty=true; }
/// Sets the data and errors
void setData(const MantidVecPtr& Y, const MantidVecPtr& E)
{ retrieveData(); refY=Y; refE=E; m_dirty=true; }
/// Sets the data.
void setData(const MantidVecPtr::ptr_type& Y)
{ retrieveData(); refY=Y; m_dirty=true; }
/// Sets the data and errors
void setData(const MantidVecPtr::ptr_type& Y, const MantidVecPtr::ptr_type& E)
{ retrieveData(); refY=Y; refE=E; m_dirty=true; }
/// Zero the data (Y&E) in this spectrum
void clearData();
// ------------ Y/E data accessors -----------------------------
// Get the array data
/// Returns the y data const
virtual const MantidVec& dataY() const
{ retrieveData(); return *refY; }
/// Returns the error data const
virtual const MantidVec& dataE() const
{ retrieveData(); return *refE; }
///Returns the y data
virtual MantidVec& dataY()
{ retrieveData(); m_dirty=true; return refY.access(); }
///Returns the error data
virtual MantidVec& dataE()
{ retrieveData(); m_dirty=true; return refE.access(); }
// ------------ X data accessors -----------------------------
virtual void setX(const MantidVec& X)
{ retrieveData(); refX.access()=X; m_dirty = true; }
virtual void setX(const MantidVecPtr& X)
{ retrieveData(); refX=X; m_dirty = true; }
virtual void setX(const MantidVecPtr::ptr_type& X)
{ retrieveData(); refX=X; m_dirty = true; }
virtual MantidVec& dataX()
{ retrieveData(); m_dirty = true;
return refX.access(); }
virtual const MantidVec& dataX() const
{ retrieveData();
return *refX; }
virtual MantidVecPtr ptrX() const
{ retrieveData(); m_dirty = true;
return refX; }
virtual std::size_t size() const
{ retrieveData();
return refY->size();
}
/// Checks for errors
bool isError() const
{ retrieveData();
return refE->empty();
}
/// Gets the memory size of the histogram
size_t getMemorySize() const
{ return ((refX->size()+refY->size()+refE->size())*sizeof(double)); }
//------------------------------------------------------------------------
/// @return true if the data was modified.
bool isDirty() const
{ return m_dirty; }
/** Set the dirty flag
* @param dirty :: bool flag value */
void setDirty(bool dirty)
{ m_dirty = dirty; }
//------------------------------------------------------------------------
/// @return true if the data was loaded from disk
bool isLoaded() const
{ return m_loaded; }
/** Set the loaded flag
* @param loaded :: bool flag value */
void setLoaded(bool loaded)
{ m_loaded = loaded; }
//------------------------------------------------------------------------
/// @return m_workspaceIndex (for debugging mostly)
size_t getWorkspaceIndex() const
{ return m_workspaceIndex; }
protected:
/// Are the data vectors loaded from the disk?
mutable bool m_loaded;
/// Was the data modified?
mutable bool m_dirty;
/// Workspace that owns this histogram
AbsManagedWorkspace2D * m_parentWorkspace;
/// Index in m_parentWorkspace
size_t m_workspaceIndex;
public:
// ------------ Direct data accessors, for use by ManagedDataBlock2D -----------------------------
MantidVec& directDataX() { return refX.access(); }
MantidVec& directDataY() { return refY.access(); }
MantidVec& directDataE() { return refE.access(); }
void directSetX(boost::shared_ptr<MantidVec> newX) { refX = newX; }
};
} // namespace DataObjects
} // namespace Mantid
#endif /* MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1D_H_ */
#include "MantidDataObjects/ManagedHistogram1D.h"
#include "MantidKernel/System.h"
#include "MantidDataObjects/AbsManagedWorkspace2D.h"
#include "MantidDataObjects/ManagedWorkspace2D.h"
namespace Mantid
{
namespace DataObjects
{
//----------------------------------------------------------------------------------------------
/** Constructor
*/
ManagedHistogram1D::ManagedHistogram1D(AbsManagedWorkspace2D * parentWS, size_t workspaceIndex)
: m_loaded(false), m_dirty(false),
m_parentWorkspace(parentWS), m_workspaceIndex(workspaceIndex)
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
ManagedHistogram1D::~ManagedHistogram1D()
{
}
//------------------------------------------------------------------------
/** Clear Y and E vectors */
void ManagedHistogram1D::clearData()
{
retrieveData();
m_dirty = true;
MantidVec & yValues = this->dataY();
std::fill(yValues.begin(), yValues.end(), 0.0);
MantidVec & eValues = this->dataE();
std::fill(eValues.begin(), eValues.end(), 0.0);
}
//------------------------------------------------------------------------
/** Method that retrieves the data from the disk
* if needed.
*/
void ManagedHistogram1D::retrieveData() const
{
// Only load from disk when needed
if (!m_loaded)
{
if (m_parentWorkspace)
{
// This method will read in the data and fill in this (and other nearby) spectra
m_parentWorkspace->readDataBlockIfNeeded(m_workspaceIndex);
}
// OK, we're loaded
m_loaded = true;
// We have not modified anything yet.
m_dirty = false;
}
}
//------------------------------------------------------------------------
/** Method that clears the data vectors to release
* memory when the spectrum can be released to disk.
* The data should have been saved already.
*/
void ManagedHistogram1D::releaseData()
{
if (m_loaded)
{
// Clear all the vectors to release the memory
refX.access().clear();
MantidVec().swap(refX.access());
refY.access().clear();
MantidVec().swap(refY.access());
refE.access().clear();
MantidVec().swap(refE.access());
// Note: we leave DX alone since it is kept in memory always.
// Reset Markers
m_loaded = false;
m_dirty = false;
}
}
} // namespace Mantid
} // namespace DataObjects
#ifndef MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1DTEST_H_
#define MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1DTEST_H_
#include "MantidDataObjects/ManagedHistogram1D.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>
using namespace Mantid;
using namespace Mantid::DataObjects;
using namespace Mantid::API;
class ManagedHistogram1DTest : public CxxTest::TestSuite
{
public:
void test_constructor()
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT_EQUALS( h.getWorkspaceIndex(), 1234);
}
/** Const access does not set the dirty flag */
void test_dirtyFlag_const()
{
const ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
const MantidVec & x = h.dataX();
TS_ASSERT( !h.isDirty() );
const MantidVec & y = h.dataY();
TS_ASSERT( !h.isDirty() );
const MantidVec & e = h.dataE();
TS_ASSERT( !h.isDirty() );
const MantidVec & dx = h.dataDx();
TS_ASSERT( !h.isDirty() );
UNUSED_ARG(x);UNUSED_ARG(dx);UNUSED_ARG(y);UNUSED_ARG(e);
}
/** Non-const access does set the dirty flag (except for Dx)*/
void test_dirtyFlag_isSet()
{
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.dataX();
TS_ASSERT( h.isDirty() );
ManagedHistogram1D * h2 = new ManagedHistogram1D(NULL, 1234);
// Check that the method is properly overridden in ISpectrum
ISpectrum * spec = h2;
TS_ASSERT( !h2->isDirty() );
spec->dataX();
TS_ASSERT( h2->isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.ptrX();
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.dataY();
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.dataE();
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.dataDx();
// Dx does NOT dirty it
TS_ASSERT( !h.isDirty() );
}
}
/** setX or setData() makes it dirty */
void test_dirtyFlag_isSet_whenUsingSetData()
{
MantidVec X, Y, E;
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.setData(Y);
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.setData(Y, E);
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.setX(X);
TS_ASSERT( h.isDirty() );
}
{
ManagedHistogram1D h(NULL, 1234);
TS_ASSERT( !h.isDirty() );
h.setDx(X);
// Dx does NOT dirty it
TS_ASSERT( !h.isDirty() );
}
}
};
#endif /* MANTID_DATAOBJECTS_MANAGEDHISTOGRAM1DTEST_H_ */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment