Skip to content

Commit

Permalink
Implement SaveDetectorMasking from SpecialWorkspace2D. Refs #3622.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Nov 17, 2011
1 parent 74f9da6 commit 3cc6caa
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 11 deletions.
136 changes: 126 additions & 10 deletions Code/Mantid/Framework/DataHandling/src/SaveDetectorMasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ This algorithm saves a SpecialWorkspace2D/MaskWorkspace to an XML file.
#include "MantidKernel/System.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/ISpectrum.h"

#include "fstream"
#include "sstream"
#include "algorithm"

#include "Poco/DOM/Document.h"
#include "Poco/DOM/Element.h"
Expand Down Expand Up @@ -66,23 +70,135 @@ namespace DataHandling
void SaveDetectorMasks::exec(){
// TODO This is a dummy prototype

// 1. Create document and root node
// 1. Get input
DataObjects::SpecialWorkspace2D_const_sptr inpWS = this->getProperty("InputWorkspace");
std::string outxmlfilename = this->getPropertyValue("OutputFile");

// 2. Convert Workspace to ...
std::vector<detid_t> detid0s;
for (size_t i = 0; i < inpWS->getNumberHistograms(); i ++){
if (inpWS->dataY(i)[0] < 1.0E-9){
// Basically it is 0
// a) workspace index -> spectrum -> detector ID
const API::ISpectrum *spec = inpWS->getSpectrum(i);
if (!spec){
g_log.error() << "No spectrum corresponds to workspace index " << i << std::endl;
throw std::invalid_argument("Cannot find spectrum");
}

const std::set<detid_t> detids = spec->getDetectorIDs();
if (detids.size() != 1){
g_log.error() << "Impossible Situation! Workspace " << i << " corresponds to #(Det) = " << detids.size() << std::endl;
throw std::invalid_argument("Impossible number of detectors");
}

// b) get detector id
detid_t detid = 0;
std::set<detid_t>::const_iterator it;
for (it=detids.begin(); it!=detids.end(); ++it){
detid = *it;
}

// c) store
detid0s.push_back(detid);

} // if
} // for

// d) sort
g_log.debug() << "Number of detetors to be masked = " << detid0s.size() << std::endl;
std::sort(detid0s.begin(), detid0s.end());

// 3. Count workspace to count 1 and 0
std::vector<detid_t> idx0sts; // starting point of the pair
std::vector<detid_t> idx0eds; // ending point of pair

detid_t i0st = detid0s[0];
detid_t i0ed = detid0s[0];

for (size_t i = 1; i < detid0s.size(); i ++){

if (detid0s[i] == detid0s[i-1]+1){
// If it is continuous: record the current one
i0ed = detid0s[i];
} else {
// If skip: restart everything
// i) record previous result
idx0sts.push_back(i0st);
idx0eds.push_back(i0ed);
// ii) reset the register
i0st = detid0s[i];
i0ed = detid0s[i];
}

} // for

// Complete the registration
idx0sts.push_back(i0st);
idx0eds.push_back(i0ed);

for (size_t i = 0; i < idx0sts.size(); i++){
g_log.notice() << "Section " << i << " : " << idx0sts[i] << " , " << idx0eds[i] << std::endl;
}

// 4. Write out to XML nodes
// a) Create document and root node
AutoPtr<Document> pDoc = new Document;
AutoPtr<Element> pRoot = pDoc->createElement("root");
AutoPtr<Element> pRoot = pDoc->createElement("detector-masking");
pDoc->appendChild(pRoot);

// 2. Append child
AutoPtr<Element> pChild1 = pDoc->createElement("child1");
AutoPtr<Text> pText1 = pDoc->createTextNode("text1");
pChild1->appendChild(pText1);
pRoot->appendChild(pChild1);

// 3. Write
pRoot->setAttribute("default", "use");

// b) Append Group
AutoPtr<Element> pChildGroup = pDoc->createElement("group");
pChildGroup->setAttribute("type", "notuse");
pRoot->appendChild(pChildGroup);

// c) Append detid
// c1. Generate text value
std::stringstream ss;
for (size_t i = 0; i < idx0sts.size(); i ++){
size_t ist = idx0sts[i];
size_t ied = idx0eds[i];

// a-b or a
bool writedata = true;
if (ist < ied){
ss << ist << "-" << ied;
} else if (ist == ied){
ss << ist;
} else {
writedata = false;
}
// add ","
if (writedata && i < idx0sts.size()-1){
ss << ",";
}

} // for
std::string textvalue = ss.str();

// c2. Create element
AutoPtr<Element> pDetid = pDoc->createElement("detids");
AutoPtr<Text> pText1 = pDoc->createTextNode(textvalue);
pDetid->appendChild(pText1);
pChildGroup->appendChild(pDetid);

// 4. Write
DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(XMLWriter::PRETTY_PRINT);

std::ofstream ofs;
ofs.open(outxmlfilename.c_str(), std::fstream::out);

ofs << "<?xml version=\"1.0\"?>\n"
<< "<?xml-stylesheet type=\"text/xsl\" href=\"cansasxml-html.xsl\" ?>\n";

writer.writeNode(std::cout, pDoc);
writer.writeNode(ofs, pDoc);
ofs.close();

return;
}


Expand Down
60 changes: 59 additions & 1 deletion Code/Mantid/Framework/DataHandling/test/SaveDetectorMasksTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <iomanip>

#include "MantidDataHandling/SaveDetectorMasks.h"
#include "MantidDataHandling/LoadMaskingFile.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"

#include "Poco/File.h"

using namespace Mantid;
using namespace Mantid::DataHandling;
Expand All @@ -22,8 +26,62 @@ class SaveDetectorMasksTest : public CxxTest::TestSuite

static void destroySuite( SaveDetectorMasksTest *suite ) { delete suite; }

void test_Something()
void test_Initialize()
{
SaveDetectorMasks savealg;
savealg.initialize();

TS_ASSERT(savealg.isInitialized());
}


void test_SaveFile()
{
// 1. Init SaveDetectorMasking
SaveDetectorMasks savealg;
savealg.initialize();

// 2. Run LoadMaskingFile
LoadMaskingFile loadfile;
loadfile.initialize();

loadfile.setProperty("Instrument", "POWGEN");
loadfile.setProperty("InputFile", "testmasking.xml");
loadfile.setProperty("OutputWorkspace", "PG3Mask");

TS_ASSERT_EQUALS(loadfile.execute(),true);
DataObjects::SpecialWorkspace2D_sptr maskws =
boost::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(AnalysisDataService::Instance().retrieve("PG3Mask"));

// 3. Set property and run
TS_ASSERT(savealg.setProperty("InputWorkspace", maskws));
TS_ASSERT(savealg.setProperty("OutputFile", "maskcopy.xml"));

savealg.execute();
TS_ASSERT(savealg.isExecuted());

// 4. Load the new XML file
LoadMaskingFile loadfile2;
loadfile2.initialize();

loadfile2.setProperty("Instrument", "POWGEN");
loadfile2.setProperty("InputFile", "maskcopy.xml");
loadfile2.setProperty("OutputWorkspace", "PG3MaskCopy");

TS_ASSERT_EQUALS(loadfile2.execute(),true);
DataObjects::SpecialWorkspace2D_sptr maskws2 =
boost::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(AnalysisDataService::Instance().retrieve("PG3MaskCopy"));

// 5. Compare
TS_ASSERT_EQUALS(maskws->getNumberHistograms(), maskws2->getNumberHistograms());
for (size_t i = 0; i < maskws->getNumberHistograms(); i ++){
TS_ASSERT_EQUALS(maskws->dataY(i)[0], maskws2->dataY(i)[0]);
}

// 6. Clean the file
Poco::File cleanfile("maskcopy.xml");
cleanfile.remove(false);

}


Expand Down

0 comments on commit 3cc6caa

Please sign in to comment.