Skip to content

Commit

Permalink
refs #9862 Typos and the changes to createOrOpenMDWSgroup interface
Browse files Browse the repository at this point in the history
Added return option, which tells if the data group already exists
  • Loading branch information
abuts committed Aug 14, 2014
1 parent 52ab1bc commit 1b576c8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 57 deletions.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/MDAlgorithms/src/MergeMDFiles.cpp
Expand Up @@ -316,7 +316,8 @@ namespace MDAlgorithms
{
g_log.notice() << "Starting SaveMD to update the file back-end." << std::endl;
// create or open WS group and put there additional information about WS and its dimensions
boost::scoped_ptr< ::NeXus::File> file(MDBoxFlatTree::createOrOpenMDWSgroup(outputFile,m_nDims,m_MDEventType,false));
bool old_data_there;
boost::scoped_ptr< ::NeXus::File> file(MDBoxFlatTree::createOrOpenMDWSgroup(outputFile,m_nDims,m_MDEventType,false,old_data_there));
this->progress(0.94, "Saving ws history and dimensions");
MDBoxFlatTree::saveWSGenericInfo(file.get(),m_OutIWS);
// Save each ExperimentInfo to a spot in the file
Expand Down
11 changes: 6 additions & 5 deletions Code/Mantid/Framework/MDAlgorithms/src/SaveMD.cpp
Expand Up @@ -111,18 +111,19 @@ namespace MDAlgorithms
if(update) // workspace has its own file and ignores any changes to the algorithm parameters
{
if(!ws->isFileBacked())
throw std::runtime_error(" attemtp to update non-file backed workspace");
throw std::runtime_error(" attempt to update non-file backed workspace");
filename = bc->getFileIO()->getFileName();
}

//-----------------------------------------------------------------------------------------------------
// create or open WS group and put there additional information about WS and its dimesnions
// create or open WS group and put there additional information about WS and its dimensions
int nDims = static_cast<int>(nd);
auto file = file_holder_type(MDBoxFlatTree::createOrOpenMDWSgroup(filename,nDims,MDE::getTypeName(),false));
bool data_exist;
auto file = file_holder_type(MDBoxFlatTree::createOrOpenMDWSgroup(filename,nDims,MDE::getTypeName(),false,data_exist));

// Save each NEW ExperimentInfo to a spot in the file
MDBoxFlatTree::saveExperimentInfos(file.get(),ws);
if(!update)
if(!update || !data_exist )
{
MDBoxFlatTree::saveWSGenericInfo(file.get(),ws);
}
Expand All @@ -131,7 +132,7 @@ namespace MDAlgorithms

MDBoxFlatTree BoxFlatStruct;
//-----------------------------------------------------------------------------------------------------
if(update) // the workspace is already file backed; We not usually use this mode but want to leave it for compartibility
if(update) // the workspace is already file backed;
{
// remove all boxes from the DiskBuffer. DB will calculate boxes positions on HDD.
bc->getFileIO()->flushCache();
Expand Down
Expand Up @@ -66,7 +66,7 @@ namespace MDEvents
uint64_t restoreBoxTree(std::vector<API::IMDNode *>&Boxes ,API::BoxController_sptr &bc, bool FileBackEnd,bool NoFileInfo=false);

/*** this function tries to set file positions of the boxes to
make data physiclly located close to each otger to be as close as possible on the HDD */
make data physically located close to each other to be as close as possible on the HDD */
void setBoxesFilePositions(bool setFileBacked);

/**Save flat box structure into a file, defined by the file name*/
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace MDEvents
/// shared pointer to multiple experiment info stored within the workspace
boost::shared_ptr<Mantid::API::MultipleExperimentInfos> m_mEI;
public:
static ::NeXus::File * createOrOpenMDWSgroup(const std::string &fileName,int &nDims, const std::string &WSEventType, bool readOnly);
static ::NeXus::File * createOrOpenMDWSgroup(const std::string &fileName,int &nDims, const std::string &WSEventType, bool readOnly,bool &exist);
// save each experiment info into its own NeXus group within an existing opened group
static void saveExperimentInfos(::NeXus::File * const file, API::IMDEventWorkspace_const_sptr ws);
// load experiment infos, previously saved through the the saveExperimentInfo function
Expand Down
13 changes: 8 additions & 5 deletions Code/Mantid/Framework/MDEvents/src/BoxControllerNeXusIO.cpp
Expand Up @@ -55,7 +55,7 @@ namespace MDEvents
}
/** The optional method to set up the event type and the size of the event coordinate
* As save/load operations use void data type, these function allow set up/get the type name provided for the IO operations
* and the size of the data type in bytes (e.g. the class dependant physical meaning of the blockSize and blockPosition used
* and the size of the data type in bytes (e.g. the class dependent physical meaning of the blockSize and blockPosition used
* by save/load operations
* @param blockSize -- size (in bytes) of the blockPosition and blockSize used in save/load operations. 4 and 8 are supported only
e.g. float and double
Expand Down Expand Up @@ -86,7 +86,7 @@ namespace MDEvents
}

/** As save/load operations use void data type, these function allow set up/get the type name provided for the IO operations
* and the size of the data type in bytes (e.g. the class dependant physical meaning of the blockSize and blockPosition used
* and the size of the data type in bytes (e.g. the class dependent physical meaning of the blockSize and blockPosition used
* by save/load operations
*@return CoordSize -- size (in bytes) of the blockPosition and blockSize used in save/load operations
*@return typeName -- the name of the event used in the operations. The name itself defines the size and the format of the event
Expand All @@ -100,7 +100,7 @@ namespace MDEvents

/**Open the file to use in IO operations with events
*
*@param fileName -- the name of the file to open. Search for file perfomed within the Mantid search path.
*@param fileName -- the name of the file to open. Search for file performed within the Mantid search path.
*@param mode -- opening mode (read or read/write)
*
*
Expand Down Expand Up @@ -133,7 +133,10 @@ namespace MDEvents
throw Kernel::Exception::FileError("Can not open file to read ",m_fileName);
}
int nDims = static_cast<int>(this->m_bc->getNDims());
m_File = MDBoxFlatTree::createOrOpenMDWSgroup(m_fileName,nDims, m_EventsTypesSupported[m_EventType],m_ReadOnly);

bool group_exists;
m_File = MDBoxFlatTree::createOrOpenMDWSgroup(m_fileName,nDims, m_EventsTypesSupported[m_EventType],m_ReadOnly,group_exists);

// we are in MD workspace Class group now
std::map<std::string, std::string> groupEntries;
m_File->getEntries(groupEntries);
Expand All @@ -144,7 +147,7 @@ namespace MDEvents
// we are in MDEvent group now (either created or opened)


// read if exist and create if not the group, which is responsible for saving DiskBuffer infornation;
// read if exist and create if not the group, which is responsible for saving DiskBuffer information;
getDiskBufferFileData();

if(m_ReadOnly)
Expand Down
54 changes: 27 additions & 27 deletions Code/Mantid/Framework/MDEvents/src/MDBox.cpp
Expand Up @@ -437,30 +437,30 @@ namespace MDEvents
TMDE(
void MDBox)::calculateCentroid(coord_t * centroid) const
{
for (size_t d=0; d<nd; d++)
centroid[d] = 0;
for (size_t d=0; d<nd; d++)
centroid[d] = 0;

// Signal was calculated before (when adding)
// Keep 0.0 if the signal is null. This avoids dividing by 0.0
if (this->m_signal == 0) return;
// Signal was calculated before (when adding)
// Keep 0.0 if the signal is null. This avoids dividing by 0.0
if (this->m_signal == 0) return;

typename std::vector<MDE>::const_iterator it_end = data.end();
for(typename std::vector<MDE>::const_iterator it = data.begin(); it != it_end; ++it)
{
const MDE & Evnt = *it;
double signal = Evnt.getSignal();
for (size_t d=0; d<nd; d++)
{
// Total up the coordinate weighted by the signal.
centroid[d] += Evnt.getCenter(d) * static_cast<coord_t>(signal);
}
}
typename std::vector<MDE>::const_iterator it_end = data.end();
for(typename std::vector<MDE>::const_iterator it = data.begin(); it != it_end; ++it)
{
const MDE & Evnt = *it;
double signal = Evnt.getSignal();
for (size_t d=0; d<nd; d++)
{
// Total up the coordinate weighted by the signal.
centroid[d] += Evnt.getCenter(d) * static_cast<coord_t>(signal);
}
}

// Normalize by the total signal
for (size_t d=0; d<nd; d++)
{
centroid[d] /= coord_t(this->m_signal);
}
// Normalize by the total signal
for (size_t d=0; d<nd; d++)
{
centroid[d] /= coord_t(this->m_signal);
}

}

Expand Down Expand Up @@ -644,9 +644,9 @@ namespace MDEvents
if (out[0] < radius && std::fabs(out[1]) < 0.5*length)
{
// add event to appropriate y channel
size_t xchannel;
if (out[1] < 0) xchannel = static_cast<int>(out[1] / deltaQ - 0.5) + static_cast<int>(numSteps / 2)-1;
else xchannel = static_cast<int>(out[1] / deltaQ + 0.5) + static_cast<int>(numSteps / 2)-1;
size_t xchannel;
if (out[1] < 0) xchannel = static_cast<int>(out[1] / deltaQ - 0.5) + static_cast<int>(numSteps / 2)-1;
else xchannel = static_cast<int>(out[1] / deltaQ + 0.5) + static_cast<int>(numSteps / 2)-1;
if (xchannel < numSteps ) signal_fit[xchannel] += static_cast<signal_t>(it->getSignal());

signal += static_cast<signal_t>(it->getSignal());
Expand Down Expand Up @@ -850,8 +850,8 @@ namespace MDEvents

/**Make this box file-backed
* @param fileLocation -- the starting position of this box data are/should be located in the direct access file
* @param fileSize -- the size this box data occupy in the file (in the units of the numbner of eventss)
* @param markSaved -- set to true if the data indeed are physically there and one can indedd read then from there
* @param fileSize -- the size this box data occupy in the file (in the units of the number of events)
* @param markSaved -- set to true if the data indeed are physically there and one can indeed read then from there
*/
TMDE(
void MDBox)::setFileBacked(const uint64_t fileLocation,const size_t fileSize, const bool markSaved)
Expand All @@ -869,7 +869,7 @@ namespace MDEvents
this->setFileBacked(UNDEF_UINT64,this->getDataInMemorySize(),false);
}

/**Save the box dataat specific disk position using the class, responsible for the file IO.
/**Save the box data to specific disk position using the class, responsible for the file IO.
*
*@param FileSaver -- the pointer to the class, responsible for File IO operations
*@param position -- the position of the data within the class.
Expand Down
42 changes: 25 additions & 17 deletions Code/Mantid/Framework/MDEvents/src/MDBoxFlatTree.cpp
Expand Up @@ -70,7 +70,7 @@ namespace Mantid
// currently ID is the number of the box, but it may change in a future. TODO: uint64_t
size_t id = Box->getID();
size_t numChildren = Box->getNumChildren();
if (numChildren > 0) // MDGridBox have childred
if (numChildren > 0) // MDGridBox have children
{
// DEBUG:
//// Make sure that all children are ordered. TODO: This might not be needed if the IDs are rigorously done
Expand Down Expand Up @@ -124,7 +124,7 @@ namespace Mantid
}
ic++;
}
// file postion have to be calculated afresh
// file position have to be calculated afresh
if(!filePositionDefined)
{
uint64_t boxPosition(0);
Expand All @@ -140,13 +140,13 @@ namespace Mantid

}
/*** this function tries to set file positions of the boxes to
make data physiclly located close to each otger to be as close as possible on the HDD
make data physically located close to each other to be as close as possible on the HDD
@param setFileBacked -- initiate the boxes to be fileBacked. The boxes assumed not to be saved before.
*/
void MDBoxFlatTree::setBoxesFilePositions(bool setFileBacked)
{
// this will preserve file-backed workspace and information in it as we are not loading old box data and not?
// this would be right for binary axcess but questionable for Nexus --TODO: needs testing
// this would be right for binary access but questionable for Nexus --TODO: needs testing
// Done in INIT--> need check if ID and index in the tree are always the same.
//Kernel::ISaveable::sortObjByFilePos(m_Boxes);
// calculate the box positions in the resulting file and save it on place
Expand All @@ -172,8 +172,8 @@ namespace Mantid
void MDBoxFlatTree::saveBoxStructure(const std::string &fileName)
{
m_FileName = fileName;

auto hFile = file_holder_type(createOrOpenMDWSgroup(fileName,m_nDim,m_Boxes[0]->getEventType(),false));
bool old_group;
auto hFile = file_holder_type(createOrOpenMDWSgroup(fileName,m_nDim,m_Boxes[0]->getEventType(),false,old_group));

//Save box structure;
this->saveBoxStructure(hFile.get());
Expand All @@ -194,7 +194,7 @@ namespace Mantid


bool create(false);
if(groupEntries.find("box_structure")==groupEntries.end()) //dimesnions dataset exist
if(groupEntries.find("box_structure")==groupEntries.end()) //dimensions dataset exist
create = true;

// Start the box data group
Expand Down Expand Up @@ -241,7 +241,7 @@ namespace Mantid
}
else
{
// Update the extendible data sets
// Update the expendable data sets
hFile->writeUpdatedData("box_type", m_BoxType);
hFile->writeUpdatedData("depth", m_Depth);
hFile->writeUpdatedData("inverse_volume", m_InverseVolume);
Expand All @@ -261,8 +261,8 @@ namespace Mantid
@param fileName :: The name of the file with the box information
@param nDim :: number of dimensions the boxes have.
If this number is <=0 on input, method loads existing number of box dimensions from the file, if it is a number, method verifies if
if the number of dimensions provided equal to this number in the file. (leftower from the time when it was templated method)
@param EventType :: "MDEvent" or "MDLeanEvent" -- describe the type of events the workspace contans, similarly to nDim, used to check the data integrity
if the number of dimensions provided equal to this number in the file. (leftover from the time when it was templated method)
@param EventType :: "MDEvent" or "MDLeanEvent" -- describe the type of events the workspace contains, similarly to nDim, used to check the data integrity
@param onlyEventInfo :: load only box controller information and the events locations -- do not restore boxes themselves
@param restoreExperimentInfo :: load also experiment information
*/
Expand All @@ -272,9 +272,13 @@ namespace Mantid
m_FileName = fileName;
m_nDim = nDim;
m_eventType = EventType;


bool old_group;
// open the file and the MD workspace group.
auto hFile = file_holder_type(createOrOpenMDWSgroup(fileName,nDim,m_eventType,true));
auto hFile = file_holder_type(createOrOpenMDWSgroup(fileName,nDim,m_eventType,true,old_group));
if(!old_group)
throw Kernel::Exception::FileError("MD workspace box structure data are not present in the file",fileName);

m_nDim = nDim;


Expand Down Expand Up @@ -473,10 +477,10 @@ namespace Mantid
}

/** Method recovers the interconnected box structure from the plain tree into box tree, recovering both boxes and their connectivity
* does the opposite to the initFlatStructure operation (the class contants remains unchanged)
* does the opposite to the initFlatStructure operation (the class contents remains unchanged)
@param Boxes :: the return vector of pointers to interconnected boxes. All previous pointers found in the vector will be overwritten (beware of memory loss)
@param bc :: shard pointer to the box controller, which each box uses
@param FileBackEnd :: if one should make the data file backed, namely restore/calculate the data, nesessary to obtain events file positions
@param FileBackEnd :: if one should make the data file backed, namely restore/calculate the data, necessary to obtain events file positions
@param BoxStructureOnly :: restore box tree only ignoring information about the box events
Expand Down Expand Up @@ -589,14 +593,16 @@ namespace Mantid
*@param nDims -- number of workspace dimensions;
*@param WSEventType -- the string describing event type
*@param readOnly -- true if the file is opened for read-only access
*@param alreadyExists -- return true, if opened existing group or false if new group has been created.
*@return NeXus pointer to properly opened NeXus data file and group.
*
*@throws if group or its component do not exist and the file is opened read-only or if the existing file parameters are not equal to the
input parameters.
*/
::NeXus::File * MDBoxFlatTree::createOrOpenMDWSgroup(const std::string &fileName,int &nDims, const std::string &WSEventType, bool readOnly)
::NeXus::File * MDBoxFlatTree::createOrOpenMDWSgroup(const std::string &fileName,int &nDims, const std::string &WSEventType, bool readOnly,bool &alreadyExists)
{
alreadyExists = false;
Poco::File oldFile(fileName);
bool fileExists = oldFile.exists();
if (!fileExists && readOnly)
Expand Down Expand Up @@ -626,6 +632,7 @@ namespace Mantid
{
// Open and check ws group -------------------------------------------------------------------------------->>>
hFile->openGroup("MDEventWorkspace", "NXentry");
alreadyExists = true;

std::string eventType;
if(hFile->hasAttr("event_type"))
Expand Down Expand Up @@ -683,6 +690,7 @@ namespace Mantid

try
{
alreadyExists = false;
hFile->makeGroup("MDEventWorkspace", "NXentry", true);
hFile->putAttr("event_type",WSEventType);

Expand All @@ -698,7 +706,7 @@ namespace Mantid
}


/**Save workpace generig info like dimension structure, history, titile dimensions etc.*/
/**Save workspace generic info like dimension structure, history, title dimensions etc.*/
void MDBoxFlatTree::saveWSGenericInfo(::NeXus::File *const file,API::IMDWorkspace_const_sptr ws)
{

Expand All @@ -723,7 +731,7 @@ namespace Mantid
}

/**
* Save the affine matricies to both directional conversions to the
* Save the affine matrices to both directional conversions to the
* data.
* @param file : pointer to the NeXus file
* @param ws : workspace to get matrix from
Expand Down

0 comments on commit 1b576c8

Please sign in to comment.