Skip to content

Commit

Permalink
Use the filename in FileBackExperimentInfo
Browse files Browse the repository at this point in the history
It turns out that the NeXus object is not as persistent throughout
the system as was first suspected.
Refs #11220
  • Loading branch information
martyngigg committed Mar 4, 2015
1 parent fecb737 commit 3e342c6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
Expand Up @@ -37,7 +37,7 @@ namespace API {
class MANTID_API_DLL FileBackedExperimentInfo : public ExperimentInfo {
public:
/// Constructor
FileBackedExperimentInfo(::NeXus::File *file, const std::string &path);
FileBackedExperimentInfo(const std::string & filename, const std::string &path);

ExperimentInfo *cloneExperimentInfo() const;

Expand Down Expand Up @@ -99,8 +99,8 @@ class MANTID_API_DLL FileBackedExperimentInfo : public ExperimentInfo {
void populateFromFile() const;

mutable bool m_loaded;
::NeXus::File *m_file;
std::string m_path;
std::string m_filename;
std::string m_nxpath;
};

} // namespace API
Expand Down
20 changes: 12 additions & 8 deletions Code/Mantid/Framework/API/src/FileBackedExperimentInfo.cpp
Expand Up @@ -2,10 +2,13 @@
// Includes
//----------------------------------------------------------------------------------------------
#include "MantidAPI/FileBackedExperimentInfo.h"
#include <nexus/NeXusException.hpp>

#include <sstream>

#include <nexus/NeXusException.hpp>
#include <nexus/NeXusFile.hpp>


namespace Mantid {
namespace API {

Expand All @@ -17,12 +20,12 @@ Kernel::Logger g_log("FileBackedExperimentInfo");

/**
* Create an object based on a NeXus file and path
* @param file A pointer to an open NeXus file object
* @param path Path to the location of the data
* @param filename The full path to the file
* @param nxpath Path to the location of the experiment information
*/
FileBackedExperimentInfo::FileBackedExperimentInfo(::NeXus::File *file,
const std::string &path)
: ExperimentInfo(), m_loaded(false), m_file(file), m_path(path) {}
FileBackedExperimentInfo::FileBackedExperimentInfo(const std::string &filename,
const std::string &nxpath)
: ExperimentInfo(), m_loaded(false), m_filename(filename), m_nxpath(nxpath) {}

/**
* @return A clone of the object
Expand Down Expand Up @@ -288,15 +291,16 @@ void FileBackedExperimentInfo::populateIfNotLoaded() const {
*/
void FileBackedExperimentInfo::populateFromFile() const {
try {
m_file->openPath(m_path);
::NeXus::File nxFile(m_filename);
nxFile.openPath(m_nxpath);
// The loadExperimentInfo calls things such as mutableSample()
// and if m_loaded is not true then this function is
// will be called recursively.
m_loaded = true;

std::string parameterStr;
const_cast<FileBackedExperimentInfo *>(this)
->loadExperimentInfoNexus(m_file, parameterStr);
->loadExperimentInfoNexus(&nxFile, parameterStr);
const_cast<FileBackedExperimentInfo *>(this)
->readParameterMap(parameterStr);
} catch (::NeXus::Exception &exc) {
Expand Down
19 changes: 6 additions & 13 deletions Code/Mantid/Framework/API/test/FileBackedExperimentInfoTest.h
Expand Up @@ -33,10 +33,10 @@ class FileBackedExperimentInfoTest : public CxxTest::TestSuite {
}

m_inMemoryExptInfo = boost::make_shared<ExperimentInfo>();
m_nexusFile = boost::make_shared< ::NeXus::File >(m_filename, NXACC_READ);
m_nexusFile->openGroup("mantid_workspace_1", "NXentry");
::NeXus::File nxFile(m_filename, NXACC_READ);
nxFile.openGroup("mantid_workspace_1", "NXentry");
std::string paramString;
m_inMemoryExptInfo->loadExperimentInfoNexus(m_nexusFile.get(), paramString);
m_inMemoryExptInfo->loadExperimentInfoNexus(&nxFile, paramString);
m_inMemoryExptInfo->readParameterMap(paramString);
}

Expand Down Expand Up @@ -215,27 +215,20 @@ class FileBackedExperimentInfoTest : public CxxTest::TestSuite {
// Failure tests
//------------------------------------------------------------------------------------------------
void test_runtime_error_generated_when_unable_to_load_from_file() {
// Load the file we want to use
::NeXus::File nexusFile(m_filename, NXACC_READ);

// Create the file backed experiment info, shouldn't be loaded yet
FileBackedExperimentInfo fileBacked(&nexusFile, "/not/right/path");
FileBackedExperimentInfo fileBacked(m_filename, "/not/right/path");

TS_ASSERT_THROWS(fileBacked.toString(), std::runtime_error);
}

private:
Mantid::API::ExperimentInfo_sptr createTestObject() {
// Load the file we want to use
::NeXus::File nexusFile(m_filename, NXACC_READ);
// Create the file backed experiment info, shouldn't be loaded yet.
// Manipulate it through
// the interface
return boost::make_shared<FileBackedExperimentInfo>(m_nexusFile.get(),
// Manipulate it through the interface
return boost::make_shared<FileBackedExperimentInfo>(m_filename,
"/mantid_workspace_1");
}

boost::shared_ptr< ::NeXus::File > m_nexusFile;
Mantid::API::ExperimentInfo_sptr m_inMemoryExptInfo;
std::string m_filename;
};
Expand Down

0 comments on commit 3e342c6

Please sign in to comment.