Skip to content

Commit

Permalink
Remove workspace iterators from UserAlgorithms
Browse files Browse the repository at this point in the history
Refs #8983
  • Loading branch information
martyngigg committed Mar 3, 2014
1 parent ce04425 commit c9d93ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 78 deletions.
93 changes: 34 additions & 59 deletions Code/Mantid/Framework/UserAlgorithms/ModifyData.cpp
Expand Up @@ -37,8 +37,8 @@ void ModifyData::init()
*/
void ModifyData::exec()
{
// g_log is a reference to the logger. It is used to print out information,
// warning, and error messages
// g_log is a reference to the logger. It is used to print out information,
// warning, and error messages
g_log.information() << "Running algorithm " << name() << " version " << version() << std::endl;

// Get the input workspace
Expand All @@ -47,61 +47,30 @@ void ModifyData::exec()
// make output Workspace the same type and size as the input one
MatrixWorkspace_sptr outputW = WorkspaceFactory::Instance().create(inputW);

bool useVectors = getProperty("UseVectors");

if ( useVectors )
g_log.information() << "Option 1. Original values:" << std::endl;
// Get the count of histograms in the input workspace
size_t histogramCount = inputW->getNumberHistograms();
// Loop over spectra
for (size_t i = 0; i < histogramCount; ++i)
{
g_log.information() << "Option 1. Original values:" << std::endl;
// Get the count of histograms in the input workspace
size_t histogramCount = inputW->getNumberHistograms();
// Loop over spectra
for (size_t i = 0; i < histogramCount; ++i)
{
// Retrieve the data into a vector
const MantidVec& XValues = inputW->readX(i);
const MantidVec& YValues = inputW->readY(i);
const MantidVec& EValues = inputW->readE(i);
MantidVec& newX = outputW->dataX(i);
MantidVec& newY = outputW->dataY(i);
MantidVec& newE = outputW->dataE(i);

// Iterate over i-th spectrum and modify the data
for(size_t j=0; j<inputW->blocksize(); j++)
{
g_log.information() << "Spectrum " << i << " Point " << j << " values: "
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
newX[j] = XValues[j] + static_cast<double>(i + j);
newY[j] = YValues[j]*(2. + 0.1*static_cast<double>(j));
newE[j] = EValues[j]+0.1;
}
}
// Retrieve the data into a vector
MantidVec& newX = outputW->dataX(i);
MantidVec& newY = outputW->dataY(i);
MantidVec& newE = outputW->dataE(i);
const MantidVec& XValues = inputW->readX(i);
const MantidVec& YValues = inputW->readY(i);
const MantidVec& EValues = inputW->readE(i);

// Iterate over i-th spectrum and modify the data
for(size_t j=0; j<inputW->blocksize(); j++)
{
g_log.information() << "Spectrum " << i << " Point " << j << " values: "
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
newX[j] = XValues[j] + static_cast<double>(i + j);
newY[j] = YValues[j]*(2. + 0.1*static_cast<double>(j));
newE[j] = EValues[j]+0.1;
}
}
else
{
g_log.information() << "Option 2. Original values:" << std::endl;
// Iterate over the workspace and modify the data
int count = 0;
MatrixWorkspace::iterator ti_out(*outputW);
for(MatrixWorkspace::const_iterator ti(*inputW); ti != ti.end(); ++ti,++ti_out)
{
// get the spectrum number
size_t i = count / inputW->blocksize();
// get the point number
size_t j = count % inputW->blocksize();
// Get the reference to a data point
LocatedDataRef tr = *ti;
LocatedDataRef tr_out = *ti_out;
g_log.information() << "Spectrum " << i << " Point " << j << " values: "
<< tr.X() << ' ' << tr.Y() << ' ' << tr.E() << std::endl;
tr_out.X() = tr.X() + count;
tr_out.Y() = tr.Y()*2;
tr_out.E() = tr.E()+0.1;

count++;
}

}


// Assign it to the output workspace property
setProperty("OutputWorkspace",outputW);
Expand All @@ -112,14 +81,20 @@ void ModifyData::exec()
// Check the new workspace
g_log.information() << "New values:" << std::endl;
int count = 0;
for(MatrixWorkspace::const_iterator ti(*newW); ti != ti.end(); ++ti)
for(size_t i = 0; i < histogramCount; ++i)
{
const MantidVec& XValues = outputW->readX(i);
const MantidVec& YValues = outputW->readY(i);
const MantidVec& EValues = outputW->readE(i);

for(size_t j = 0; j < outputW->blocksize(); ++j)
{
// Get the reference to a data point
LocatedDataRef tr = *ti;
g_log.information() << "Point number " << count++ << " values: "
<< tr.X() << ' ' << tr.Y() << ' ' << tr.E() << std::endl;
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
}
}

}

}
Expand Down
31 changes: 12 additions & 19 deletions Code/Mantid/Framework/UserAlgorithms/WorkspaceAlgorithm.cpp
Expand Up @@ -31,9 +31,9 @@ void WorkspaceAlgorithm::init()
*/
void WorkspaceAlgorithm::exec()
{
// g_log is a reference to the logger. It is used to print out information,
// warning, and error messages
g_log.information() << "Running algorithm " << name() << " version " << version() << std::endl;
// g_log is a reference to the logger. It is used to print out information,
// warning, and error messages
g_log.information() << "Running algorithm " << name() << " version " << version() << std::endl;

// Get the input workspace
MatrixWorkspace_const_sptr workspace = getProperty("Workspace");
Expand All @@ -42,26 +42,19 @@ void WorkspaceAlgorithm::exec()
g_log.information() << "Number of items = " << workspace->size() << std::endl;

int count = 0;
// Iterate over the workspace
for(MatrixWorkspace::const_iterator ti(*workspace); ti != ti.end(); ++ti)
size_t histogramCount = workspace->getNumberHistograms();
for(size_t i = 0; i < histogramCount; ++i)
{
// Get the reference to a data point
LocatedDataRef tr = *ti;
g_log.information() << "Point number " << count++ << " values: "
<< tr.X() << ' ' << tr.Y() << ' ' << tr.E() << std::endl;
}
const MantidVec& XValues = workspace->readX(i);
const MantidVec& YValues = workspace->readY(i);
const MantidVec& EValues = workspace->readE(i);

count = 0;
int loopCount = 2;
// Do several loops
for(MatrixWorkspace::const_iterator ti(*workspace,loopCount,LoopOrientation::Horizontal); ti != ti.end(); ++ti)
{
// Get the reference to a data point
LocatedDataRef tr = *ti;
for(size_t j = 0; j < workspace->blocksize(); ++j)
{
g_log.information() << "Point number " << count++ << " values: "
<< tr.X() << ' ' << tr.Y() << ' ' << tr.E() << std::endl;
<< XValues[j] << ' ' << YValues[j] << ' ' << EValues[j] << std::endl;
}
}

}

}
Expand Down

0 comments on commit c9d93ec

Please sign in to comment.