Skip to content

Commit

Permalink
Modify codes along with XML file. Refs #4756.
Browse files Browse the repository at this point in the history
Masking XML file's definition is changed.  So thus LoadMaskingFile() and
its unit test to implement the change.
  • Loading branch information
wdzhou committed Feb 7, 2012
1 parent ab736a9 commit 8b0bb0b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 27 deletions.
28 changes: 12 additions & 16 deletions Code/Mantid/Framework/DataHandling/src/LoadMaskingFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,7 @@ namespace DataHandling
/// Initialise the properties
void LoadMaskingFile::init(){

// 1. Setup
/*
std::vector<std::string> instrumentnames;
instrumentnames.push_back("VULCAN");
instrumentnames.push_back("POWGEN");
instrumentnames.push_back("NOMAD");
declareProperty("Instrument", "POWGEN", new ListValidator(instrumentnames),
"Instrument to mask. If InstrumentName is given, algorithm will take InstrumentName. ");
*/

// 2. Declare property

// 1. Declare property
declareProperty("Instrument", "", "Name of instrument to mask.");
declareProperty(new FileProperty("InputFile", "", FileProperty::Load, ".xml"),
"XML file for masking. ");
Expand Down Expand Up @@ -227,7 +216,7 @@ namespace DataHandling

g_log.debug() << "Component name = " << componentnames[i] << std::endl;

// a) get componenet
// a) get component
Geometry::IComponent_const_sptr component = minstrument->getComponentByName(componentnames[i]);
g_log.debug() << "Component ID = " << component->getComponentID() << std::endl;

Expand Down Expand Up @@ -429,6 +418,8 @@ namespace DataHandling
if (pNode->nodeName().compare("group") == 0){
// Node "group"
ingroup = true;
tomask = true;
/*
// get type
Poco::XML::NamedNodeMap* att = pNode->attributes();
Poco::XML::Node* cNode = att->item(0);
Expand All @@ -439,7 +430,9 @@ namespace DataHandling
} else {
g_log.error() << "Type (" << cNode->localName() << ") = " << cNode->getNodeValue() << " is not supported!" << std::endl;
}
g_log.information() << "Node Group: child Node Name = " << cNode->localName() << ": " << cNode->getNodeValue() << std::endl;
g_log.information() << "Node Group: child Node Name = " << cNode->localName() << ": " << cNode->getNodeValue()
<< "(always)"<< std::endl;
*/

} else if (pNode->nodeName().compare("component") == 0){
// Node "component"
Expand Down Expand Up @@ -469,18 +462,21 @@ namespace DataHandling

} else if (pNode->nodeName().compare("detector-masking") == 0){
// Node "detector-masking". Check default value
mDefaultToUse = true;
/*
Poco::XML::NamedNodeMap* att = pNode->attributes();
if (att->length() > 0){
Poco::XML::Node* cNode = att->item(0);
mDefaultToUse = true;
if (cNode->localName().compare("default") == 0){
if (cNode->getNodeValue().compare("use") == 0){
mDefaultToUse = true;
} else {
mDefaultToUse = false;
}
}
} // if - att-length
}
*/
} // END-IF-ELSE: pNode->nodeName()

pNode = it.nextNode();
} // ENDWHILE
Expand Down
142 changes: 133 additions & 9 deletions Code/Mantid/Framework/DataHandling/test/LoadMaskingFileTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "MantidKernel/System.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Poco/File.h"

#include "MantidDataHandling/LoadMaskingFile.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
Expand Down Expand Up @@ -67,36 +69,110 @@ class LoadMaskingFileTest : public CxxTest::TestSuite

} // test_LoadXML

/*
* Test mask by detector ID
* For VULCAN:
* workspaceindex: detector ID
* 34 : 26284
* 1000 : 27250
* 2000 : 28268
*/
void test_DetectorIDs()
{
// 1. Generate masking files
std::vector<int> banks1;
std::vector<int> detids;
detids.push_back(26284);
detids.push_back(27250);
detids.push_back(28268);
std::string maskfname1("maskingdet.xml");
genMaskingFile(maskfname1, detids, banks1);

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

loadfile.setProperty("Instrument", "VULCAN");
loadfile.setProperty("InputFile", maskfname1);
loadfile.setProperty("OutputWorkspace", "VULCAN_Mask_Detectors");

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

// 3. Check
for (size_t iws=0; iws<maskws->getNumberHistograms(); iws++)
{
double y = maskws->dataY(iws)[0];
if (iws==34 || iws==1000 || iws==2000)
{
// These 3 workspace index are masked
TS_ASSERT_DELTA(y, 1.0, 1.0E-5);
}
else
{
// Unmasked
TS_ASSERT_DELTA(y, 0.0, 1.0E-5);
}
}

// 4. Clean
Poco::File cleanfile(maskfname1);
cleanfile.remove(false);

return;
}



/*
* Load "testingmasking.xml" and "regionofinterest.xml"
* These two xml files will generate two opposite Workspaces, i.e.,
* Number(masked detectors of WS1) = Number(unmasked detectors of WS2)
*
* by BinaryOperation
*/
void test_BinaryOperation()
void test_Banks()
{
// 0. Generate masking files
std::vector<int> banks1;
banks1.push_back(21);
banks1.push_back(22);
std::vector<int> detids;
std::string maskfname1("masking01.xml");
genMaskingFile(maskfname1, detids, banks1);

std::vector<int> banks2;
banks2.push_back(23);
banks2.push_back(26);
banks2.push_back(27);
banks2.push_back(28);
std::string maskfname2("masking02.xml");
genMaskingFile(maskfname2, detids, banks2);

// 1. Generate Mask Workspace
LoadMaskingFile loadfile;
loadfile.initialize();

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

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

// 2. Generate Region of Interest Workspace
LoadMaskingFile loadfile2;
loadfile2.initialize();

loadfile2.setProperty("Instrument", "POWGEN");
loadfile2.setProperty("InputFile", "regionofinterest.xml");
loadfile2.setProperty("OutputWorkspace", "PG3Interest");
loadfile2.setProperty("Instrument", "VULCAN");
loadfile2.setProperty("InputFile", "masking02.xml");
loadfile2.setProperty("OutputWorkspace", "VULCAN_Mask2");

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

// 3. Check
size_t sizemask = maskws->getNumberHistograms();
Expand Down Expand Up @@ -126,8 +202,56 @@ class LoadMaskingFileTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(number1-number0, 0);
}

// 4. Delete
Poco::File cleanfile1(maskfname1);
cleanfile1.remove(false);

Poco::File cleanfile2(maskfname2);
cleanfile2.remove(false);

return;
} // test_Openfile

/*
* Create a masking file
*/
void genMaskingFile(std::string maskfilename, std::vector<int> detids, std::vector<int> banks)
{

std::ofstream ofs;
ofs.open(maskfilename.c_str(), std::ios::out);

// 1. Header
ofs << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << std::endl;
ofs << " <detector-masking>" << std::endl;
ofs << " <group>" << std::endl;

// 2. "detids" & component
if (detids.size() > 0)
{
ofs << " <detids>";
for (size_t i=0; i < detids.size(); i++)
{
if (i < detids.size()-1)
ofs << detids[i] << ",";
else
ofs << detids[i];
}
ofs << "</detids>" << std::endl;
}

for (size_t i=0; i < banks.size(); i++)
{
ofs << "<component>bank" << banks[i] << "</component>" << std::endl;
}

// 4. End of file
ofs << " </group>" << std::endl << "</detector-masking>" << std::endl;

ofs.close();

return;
}
};


Expand Down
4 changes: 2 additions & 2 deletions Test/AutoTestData/testmasking.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<detector-masking default="use">
<group type="notuse">
<detector-masking>
<group>
<detids>1,2,17,32</detids>
<component>bank123</component>
<component>bank124</component>
Expand Down

0 comments on commit 8b0bb0b

Please sign in to comment.