Skip to content

Commit

Permalink
Re #6198. Fix usages of mapping methods for value return.
Browse files Browse the repository at this point in the history
Notes:
- 4 classes weren't deleting the returned pointer
- more were not exception safe
- some changes rely on the move assignment operator for efficiency.
  • Loading branch information
RussellTaylor authored and gesnerpassos committed Sep 30, 2013
1 parent 419a248 commit 59e1811
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,11 @@ class DLLExport LoadTOFRawNexus : public API::IFileLoader<Kernel::NexusDescripto
void loadSampleData(DataObjects::Workspace2D_sptr, Mantid::NeXus::NXEntry & entry);

void loadBank(const std::string &nexusfilename, const std::string & entry_name,
const std::string &bankName, Mantid::API::MatrixWorkspace_sptr WS);
const std::string &bankName, API::MatrixWorkspace_sptr WS, const detid2index_map& id_to_wi);

/// List of the absolute time of each pulse
std::vector<Kernel::DateAndTime> pulseTimes;

/// Map where key = detector ID, value = workspace index
detid2index_map * id_to_wi;

/// Number of bins
size_t numBins;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Mantid
API::MatrixWorkspace_const_sptr inputWorkspace;

// Map from detector ID to WS index
detid2index_map * map;
detid2index_map map;

// Progress reporting
API::Progress * prog;
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/DataHandling/src/GroupDetectors2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void GroupDetectors2::processXMLFile(std::string fname,
axis->getSpectraIndexMap(specs2index);
}

detid2index_map* detIdToWiMap = workspace->getDetectorIDToWorkspaceIndexMap();
const detid2index_map detIdToWiMap = workspace->getDetectorIDToWorkspaceIndexMap();

// 2. Load XML file
DataHandling::LoadGroupXMLFile loader;
Expand Down Expand Up @@ -518,8 +518,8 @@ void GroupDetectors2::processXMLFile(std::string fname,
for (size_t i = 0; i < detids.size(); i++)
{
detid_t detid = detids[i];
detid2index_map::iterator ind =detIdToWiMap->find(detid);
if ( ind != detIdToWiMap->end() )
detid2index_map::const_iterator ind =detIdToWiMap.find(detid);
if ( ind != detIdToWiMap.end() )
{
size_t wsid = ind->second;
wsindexes.push_back(wsid);
Expand Down
10 changes: 3 additions & 7 deletions Code/Mantid/Framework/DataHandling/src/LoadCalFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ namespace DataHandling

size_t numErrors = 0;

detid2index_map * detID_to_wi = NULL;
detid2index_map detID_to_wi;
if (doMask)
{
detID_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap();
Expand Down Expand Up @@ -305,8 +305,8 @@ namespace DataHandling

if (doMask)
{
detid2index_map::const_iterator it = detID_to_wi->find(udet);
if (it != detID_to_wi->end())
detid2index_map::const_iterator it = detID_to_wi.find(udet);
if (it != detID_to_wi.end())
{
size_t wi = it->second;

Expand Down Expand Up @@ -340,10 +340,6 @@ namespace DataHandling
g_log.warning() << "'" << calFileName << "' has no spectra grouped\n";
if (doMask && (!hasUnmasked))
g_log.warning() << "'" << calFileName << "' masks all spectra\n";

if (doMask)
delete detID_to_wi;

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ namespace DataHandling
}

// 1. Prepare
detid2index_map* indexmap = mGroupWS->getDetectorIDToWorkspaceIndexMap(true);
const detid2index_map indexmap = mGroupWS->getDetectorIDToWorkspaceIndexMap(true);

// 2. Set
for (std::map<int, std::vector<std::string> >::iterator it = mGroupComponentsMap.begin();
Expand Down Expand Up @@ -331,8 +331,8 @@ namespace DataHandling
if (det){
// Component is DETECTOR:
int32_t detid = det->getID();
detid2index_map::iterator itx = indexmap->find(detid);
if (itx != indexmap->end()){
detid2index_map::const_iterator itx = indexmap.find(detid);
if (itx != indexmap.end()){
size_t wsindex = itx->second;
mGroupWS->dataY(wsindex)[0] = it->first;
}
Expand All @@ -347,9 +347,6 @@ namespace DataHandling

} // ENDFOR GroupID

// 3. Clear
delete indexmap;

return;
}

Expand Down Expand Up @@ -379,7 +376,7 @@ namespace DataHandling
}

// 1. Prepare
detid2index_map* indexmap = mGroupWS->getDetectorIDToWorkspaceIndexMap(true);
const detid2index_map indexmap = mGroupWS->getDetectorIDToWorkspaceIndexMap(true);

// 2. Set GroupingWorkspace
for (std::map<int, std::vector<detid_t> >::iterator it = mGroupDetectorsMap.begin();
Expand All @@ -389,9 +386,9 @@ namespace DataHandling

for (size_t i = 0; i < it->second.size(); i ++){
detid_t detid = it->second[i];
detid2index_map::iterator itx = indexmap->find(detid);
detid2index_map::const_iterator itx = indexmap.find(detid);

if (itx != indexmap->end())
if (itx != indexmap.end())
{
size_t wsindex = itx->second;
mGroupWS->dataY(wsindex)[0] = it->first;
Expand All @@ -401,9 +398,6 @@ namespace DataHandling
} // ENDFOR detid (in range)
} // ENDFOR each group ID

// 3. Clear
delete indexmap;

return;
}

Expand All @@ -413,8 +407,8 @@ namespace DataHandling
void LoadDetectorsGroupingFile::setBySpectrumIDs()
{
// 1. Get map
spec2index_map* s2imap = mGroupWS->getSpectrumToWorkspaceIndexMap();
spec2index_map::iterator s2iter;
const spec2index_map s2imap = mGroupWS->getSpectrumToWorkspaceIndexMap();
spec2index_map::const_iterator s2iter;

// 2. Locate in loop
// std::map<int, std::vector<int> > mGroupSpectraMap;
Expand All @@ -425,8 +419,8 @@ namespace DataHandling
for (size_t isp=0; isp<gsiter->second.size(); isp++)
{
int specid = gsiter->second[isp];
s2iter = s2imap->find(specid);
if (s2iter == s2imap->end())
s2iter = s2imap.find(specid);
if (s2iter == s2imap.end())
{
g_log.error() << "Spectrum " << specid << " does not have an entry in GroupWorkspace's spec2index map" << std::endl;
throw std::runtime_error("Logic error");
Expand All @@ -449,9 +443,6 @@ namespace DataHandling
} // FOR: each spectrum ID
} // FOR: each group ID

// 3. Clean
delete s2imap;

return;
}

Expand Down
20 changes: 8 additions & 12 deletions Code/Mantid/Framework/DataHandling/src/LoadMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ namespace DataHandling
std::vector<int32_t> pairdetids_up)
{
// 1. Get index map
detid2index_map* indexmap = mMaskWS->getDetectorIDToWorkspaceIndexMap(true);
const detid2index_map indexmap = mMaskWS->getDetectorIDToWorkspaceIndexMap(true);

// 2. Mask
g_log.debug() << "Mask = " << tomask << " Final Single IDs Size = " << singledetids.size() << std::endl;

for (size_t i = 0; i < singledetids.size(); i ++)
{
detid_t detid = singledetids[i];
detid2index_map::iterator it;
it = indexmap->find(detid);
if (it != indexmap->end())
detid2index_map::const_iterator it;
it = indexmap.find(detid);
if (it != indexmap.end())
{
size_t index = it->second;
if (tomask)
Expand All @@ -255,9 +255,6 @@ namespace DataHandling
g_log.error() << "To Be Implemented Soon For Pair (" << pairdetids_low[i] << ", " << pairdetids_up[i] << "!" << std::endl;
}

// 4. Clear
delete indexmap;

return;
}

Expand Down Expand Up @@ -394,9 +391,8 @@ namespace DataHandling
}

// 2. Get Map
// 1. Get map
spec2index_map* s2imap = mMaskWS->getSpectrumToWorkspaceIndexMap();
spec2index_map::iterator s2iter;
const spec2index_map s2imap = mMaskWS->getSpectrumToWorkspaceIndexMap();
spec2index_map::const_iterator s2iter;

// 3. Set mask
for (size_t i = 0; i < pairslow.size(); i ++)
Expand All @@ -406,8 +402,8 @@ namespace DataHandling

for (int32_t specid=pairslow[i]; specid<=pairsup[i]; specid++)
{
s2iter = s2imap->find(specid);
if (s2iter == s2imap->end())
s2iter = s2imap.find(specid);
if (s2iter == s2imap.end())
{
// spectrum not found. bad brach
g_log.error() << "Spectrum " << specid << " does not have an entry in GroupWorkspace's spec2index map" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ namespace DataHandling
double longestTOF(0.0);

// populate workspace with McStas events
detid2index_map* detIDtoWSindex_map = eventWS->getDetectorIDToWorkspaceIndexMap(true);
const detid2index_map detIDtoWSindex_map = eventWS->getDetectorIDToWorkspaceIndexMap(true);
for (size_t in = 0; in < nNeutrons; in++)
{
const int detectorID = static_cast<int>(data[4+numberOfDataColumn*in]);
Expand All @@ -262,7 +262,7 @@ namespace DataHandling
longestTOF = detector_time;
}

size_t workspaceIndex = (*detIDtoWSindex_map)[detectorID];
const size_t workspaceIndex = detIDtoWSindex_map.find(detectorID)->second;
int64_t pulse_time = 0;
//eventWS->getEventList(workspaceIndex) += TofEvent(detector_time,pulse_time);
//eventWS->getEventList(workspaceIndex) += TofEvent(detector_time);
Expand Down
13 changes: 6 additions & 7 deletions Code/Mantid/Framework/DataHandling/src/LoadTOFRawNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void LoadTOFRawNexus::countPixels(const std::string &nexusfilename, const std::s
* @param WS :: workspace to modify
*/
void LoadTOFRawNexus::loadBank(const std::string &nexusfilename, const std::string & entry_name,
const std::string &bankName, Mantid::API::MatrixWorkspace_sptr WS)
const std::string &bankName, API::MatrixWorkspace_sptr WS, const detid2index_map& id_to_wi)
{
g_log.debug() << "Loading bank " << bankName << std::endl;
// To avoid segfaults on RHEL5/6 and Fedora
Expand Down Expand Up @@ -392,9 +392,9 @@ void LoadTOFRawNexus::loadBank(const std::string &nexusfilename, const std::stri
if ( m_spec_max != Mantid::EMPTY_INT())
{
uint32_t ifirst = pixel_id[0];
range_check out_range(m_spec_min, m_spec_max, *id_to_wi);
range_check out_range(m_spec_min, m_spec_max, id_to_wi);
std::vector<uint32_t>::iterator newEnd =
std::remove_if (pixel_id.begin(), pixel_id.end(), out_range);
std::remove_if (pixel_id.begin(), pixel_id.end(), out_range);
pixel_id.erase(newEnd, pixel_id.end());
// check if beginning or end of array was erased
if(ifirst != pixel_id[0]) iPart = numPixels - pixel_id.size();
Expand Down Expand Up @@ -454,7 +454,7 @@ void LoadTOFRawNexus::loadBank(const std::string &nexusfilename, const std::stri
{
// Find the workspace index for this detector
detid_t pixelID = pixel_id[i-iPart];
size_t wi = (*id_to_wi)[pixelID];
size_t wi = id_to_wi.find(pixelID)->second;

// Set the basic info of that spectrum
ISpectrum * spec = WS->getSpectrum(wi);
Expand Down Expand Up @@ -571,7 +571,7 @@ void LoadTOFRawNexus::exec()
WS->rebuildSpectraMapping(false);
// And map ID to WI
g_log.debug() << "Mapping ID to WI" << std::endl;
id_to_wi = WS->getDetectorIDToWorkspaceIndexMap();
const auto id_to_wi = WS->getDetectorIDToWorkspaceIndexMap();

// Load each bank sequentially
//PARALLEL_FOR1(WS)
Expand All @@ -581,7 +581,7 @@ void LoadTOFRawNexus::exec()
std::string bankName = bankNames[i];
prog->report("Loading bank " + bankName);
g_log.debug() << "Loading bank " << bankName << std::endl;
loadBank(filename, entry_name, bankName, WS);
loadBank(filename, entry_name, bankName, WS, id_to_wi);
// PARALLEL_END_INTERUPT_REGION
}
// PARALLEL_CHECK_INTERUPT_REGION
Expand All @@ -600,7 +600,6 @@ void LoadTOFRawNexus::exec()
setProperty("OutputWorkspace", WS);

delete prog;
delete id_to_wi;
}

} // namespace DataHandling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ namespace DataHandling
size_t wi = 0;
try
{
wi = (*map)[ det->getAtXY(x,y)->getID() ];
wi = map.find( det->getAtXY(x,y)->getID() )->second;
}
catch (...)
{
Expand Down Expand Up @@ -711,9 +711,6 @@ namespace DataHandling
if (ret == NX_ERROR)
throw std::runtime_error("Nexus error while copying the file.");

// Free map memory
delete map;

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ namespace Mantid

Geometry::Instrument_const_sptr inst = m_workspace->getInstrument();
// Throws for multiple detectors
boost::scoped_ptr<spec2index_map> specToIndex(m_workspace->getSpectrumToWorkspaceIndexMap());
const spec2index_map specToIndex(m_workspace->getSpectrumToWorkspaceIndexMap());

std::ifstream datfile(filename.c_str(), std::ios_base::in);

Expand All @@ -284,8 +284,8 @@ namespace Mantid
{
if(isSpectrum)
{
auto it = specToIndex->find(detOrSpec);
if(it != specToIndex->end())
auto it = specToIndex.find(detOrSpec);
if(it != specToIndex.end())
{
const size_t wsIndex = it->second;
det = m_workspace->getDetector(wsIndex);
Expand Down

0 comments on commit 59e1811

Please sign in to comment.