Skip to content

Commit

Permalink
Removed hardcoding of SASdetector element in savecansas1d. re #5268
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders-Markvardsen committed May 29, 2012
1 parent 0ecca1b commit 06f53c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
84 changes: 38 additions & 46 deletions Code/Mantid/Framework/DataHandling/src/SaveCanSAS1D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void SaveCanSAS1D::init()
boost::make_shared<Kernel::StringListValidator>(radiation_source));
declareProperty("Append", false, "If true the output file is not overwritten but appended to");
declareProperty("Process", "", "Text to append to Process section");
declareProperty("DetectorNames","", "Which detectors to store information about, where the name must match the name given for a detector in the instrument definition file");
}
/** Is called when the input workspace was actually a group, it sets the
* for all group members after the first so that the whole group is saved
Expand Down Expand Up @@ -531,61 +532,52 @@ void SaveCanSAS1D::createSASSourceElement(std::string& sasSource )
sasSource+="\n\t\t\t</SASsource>";

}
/** This method creates an XML element named "SASdetector"
* @param sasDet :: string for sasdetector element in the xml
/** This method creates XML elements named "SASdetector". This method
appends ot sasDet.
* @param sasDet :: string for one or more sasdetector elements
*/
void SaveCanSAS1D::createSASDetectorElement(std::string& sasDet)
{
sasDet="\n\t\t\t<SASdetector>";
//outFile<<sasDet;
const std::string detectorNames = getProperty("DetectorNames");

std::string detectorName ;
Geometry::IDetector_const_sptr detgroup;
try
{
// Get the detector object (probably a group) that goes with the result spectrum
detgroup = m_workspace->getDetector(0);
const int id = detgroup->getID(); //id of the first detector in the group
// Now make sure we've got an individual detector object
Geometry::IDetector_const_sptr det = m_workspace->getInstrument()->getDetector(id);
// Get all its ancestors
const std::vector<boost::shared_ptr<const IComponent> > ancs = det->getAncestors();
// The one we want is the penultimate one
// Shouldn't ever happen, but protect against detector having no ancestors
if (ancs.size() > 1)
detectorName = ancs[ancs.size()-2]->getName();
else
detectorName = det->getName();
//look for xml special characters and replace with entity refrence
searchandreplaceSpecialChars(detectorName);
}
catch(Kernel::Exception::NotFoundError&)
{
throw;
}
catch(std::runtime_error& )
if ( detectorNames.empty() )
return;

std::list<std::string> detList;
boost::algorithm::split(detList, detectorNames, std::bind2nd(std::equal_to<char>(), ','));
for( std::list<std::string>::const_iterator itr = detList.begin(); itr != detList.end(); ++itr )
{
throw ;
}
std::string detectorName = *itr;
boost::algorithm::trim(detectorName);

// get first component with name detectorName in IDF
boost::shared_ptr<const IComponent> comp = m_workspace->getInstrument()->getComponentByName(detectorName);
if (comp != boost::shared_ptr<const IComponent>() )
{
sasDet += "\n\t\t\t<SASdetector>";

std::string sasDetname="\n\t\t\t\t<name>";
sasDetname+=detectorName;
sasDetname+="</name>";
sasDet+=sasDetname;
std::string sasDetname="\n\t\t\t\t<name>";
sasDetname+=detectorName;
sasDetname+="</name>";
sasDet+=sasDetname;

//outFile<<sasDetname;
std::string sasDetUnit="\n\t\t\t\t<SDD unit=\"m\">";
std::string sasDetUnit="\n\t\t\t\t<SDD unit=\"m\">";

std::stringstream sdd;
double distance = detgroup->getDistance(*m_workspace->getInstrument()->getSample());
sdd << distance;
std::stringstream sdd;
double distance = comp->getDistance(*m_workspace->getInstrument()->getSample());
sdd << distance;

sasDetUnit+=sdd.str();
sasDetUnit+="</SDD>";
//outFile<<sasDetUnit;
sasDet+=sasDetUnit;
sasDet+="\n\t\t\t</SASdetector>";
//outFile<<sasDet;
sasDetUnit+=sdd.str();
sasDetUnit+="</SDD>";

sasDet+=sasDetUnit;
sasDet+="\n\t\t\t</SASdetector>";
}
else
{
g_log.notice() << "Detector with name " << detectorName << " does not exist in the instrument of the workspace: " << m_workspace->name() << std::endl;
}
}
}

/** This method creates an XML element named "SASprocess"
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/DataHandling/test/SaveCanSAS1dTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class SaveCanSAS1dTest : public CxxTest::TestSuite
TS_ASSERT( savealg.isInitialized() );
savealg.setPropertyValue("InputWorkspace", "SaveCanSAS1DTest_group");
savealg.setPropertyValue("Filename", m_filename);
savealg.setPropertyValue("DetectorNames", "HAB");
TS_ASSERT_THROWS_NOTHING(savealg.execute());
TS_ASSERT( savealg.isExecuted() );

Expand Down
14 changes: 14 additions & 0 deletions Code/Mantid/MantidQt/CustomInterfaces/src/SANSRunWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,20 @@ void SANSRunWindow::handleDefSaveClick()
QString fname = fileBase.endsWith(ext) ? fileBase : fileBase+ext;
if ( (*alg) == "SaveRKH" )
saveCommand += (*alg)+"('"+m_outputWS+"','"+fname+"', Append=False)\n";
else if ( (*alg) == "SaveCanSAS1D" )
{
saveCommand += (*alg)+"('"+m_outputWS+"','"+fname+"', DetectorNames=";
Workspace_sptr workspace_ptr = AnalysisDataService::Instance().retrieve(m_outputWS.toStdString());
MatrixWorkspace_sptr matrix_workspace = boost::dynamic_pointer_cast<MatrixWorkspace>(workspace_ptr);
if ( matrix_workspace )
{
if ( matrix_workspace->getInstrument()->getName() == "SANS2D" )
saveCommand += "'front-detector, rear-detector'";
if ( matrix_workspace->getInstrument()->getName() == "LOQ" )
saveCommand += "'HAB, main-detector-bank'";
}
saveCommand += ")\n";
}
else
saveCommand += (*alg)+"('"+m_outputWS+"','"+fname+"')\n";
}
Expand Down
18 changes: 18 additions & 0 deletions Code/Mantid/MantidQt/MantidWidgets/src/SaveWorkspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ QString SaveWorkspaces::saveList(const QList<QListWidgetItem*> & wspaces, const
saveCommands += ", Append=";
saveCommands += toAppend ? "True" : "False";
}
if ( algorithm == "SaveCanSAS1D" )
{
saveCommands += ", DetectorNames=";
Workspace_sptr workspace_ptr = AnalysisDataService::Instance().retrieve(wspaces[j]->text().toStdString());
MatrixWorkspace_sptr matrix_workspace = boost::dynamic_pointer_cast<MatrixWorkspace>(workspace_ptr);
if ( matrix_workspace )
{
if ( matrix_workspace->getInstrument()->getName() == "SANS2D" )
saveCommands += "'front-detector, rear-detector'";
if ( matrix_workspace->getInstrument()->getName() == "LOQ" )
saveCommands += "'HAB, main-detector-bank'";
}
else
{
//g_log.wa
}

}
//finally finish the algorithm call
saveCommands += ")\n";
}
Expand Down

0 comments on commit 06f53c5

Please sign in to comment.