Newer
Older

Zhou, Wenduo
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*WIKI*
This algorithm loads masking file to a SpecialWorkspace2D/MaskWorkspace.
The format can be
* XML
* ... ...
*WIKI*/
#include "MantidDataHandling/LoadMaskingFile.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
#include "MantidKernel/Strings.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/IDTypes.h"
#include <Poco/DOM/Document.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeFilter.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/NamedNodeMap.h>
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <boost/algorithm/string.hpp>
using Poco::XML::DOMParser;
using Poco::XML::Document;
using Poco::XML::Element;
using Poco::XML::Node;
using Poco::XML::NodeList;
using Poco::XML::NodeIterator;
using Poco::XML::NodeFilter;
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace Mantid
{
namespace DataHandling
{
DECLARE_ALGORITHM(LoadMaskingFile)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
LoadMaskingFile::LoadMaskingFile()
{
// mMaskWS = NULL;
// mInstrumentName = "";
pDoc = NULL;
pRootElem = NULL;
return;
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
LoadMaskingFile::~LoadMaskingFile()
{
// TODO Auto-generated destructor stub
}
/// Sets documentation strings for this algorithm
void LoadMaskingFile::initDocs(){
this->setWikiSummary("Loads an XML file or calibration file to generate MaskWorkspace.");
this->setOptionalMessage("");
}
/// 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");
// 2. Declare property
declareProperty("Instrument", "POWGEN", new ListValidator(instrumentnames),
"Instrument to mask");
declareProperty(new FileProperty("InputFile", "", FileProperty::Load, ".xml"),
"XML file for masking");
// declareProperty(new WorkspaceProperty<API::MatrixWorkspace>("OutputWorkspace", "Masking", Direction::Output),
declareProperty(new WorkspaceProperty<DataObjects::SpecialWorkspace2D>("OutputWorkspace", "Masking", Direction::Output),
"Output Masking Workspace");
return;
}
/// Run the algorithm
void LoadMaskingFile::exec(){
// 1. Load Instrument and create output Mask workspace
const std::string instrumentname = getProperty("Instrument");
mInstrumentName = instrumentname;
this->intializeMaskWorkspace();
setProperty("OutputWorkspace",mMaskWS);
// 2. Parse XML File
std::string xmlfilename = getProperty("InputFile");
this->initializeXMLParser(xmlfilename);
this->parseXML();
// 3. Translate and set geometry
g_log.information() << "To Mask: " << std::endl;
std::vector<int32_t> maskdetids;
std::vector<int32_t> maskdetidpairsL;
std::vector<int32_t> maskdetidpairsU;
this->bankToDetectors(mask_bankid_single, maskdetids, maskdetidpairsL, maskdetidpairsU);
this->spectrumToDetectors(mask_specid_single, mask_specid_pair_low, mask_specid_pair_up, maskdetids, maskdetidpairsL, maskdetidpairsU);
this->detectorToDetectors(mask_detid_single, mask_detid_pair_low, mask_detid_pair_up, maskdetids, maskdetidpairsL, maskdetidpairsU);
g_log.information() << "To UnMask: " << std::endl;
std::vector<int32_t> unmaskdetids;
std::vector<int32_t> unmaskdetidpairsL;
std::vector<int32_t> unmaskdetidpairsU;
this->bankToDetectors(unmask_bankid_single, unmaskdetids, unmaskdetidpairsL, unmaskdetidpairsU);
this->spectrumToDetectors(unmask_specid_single, unmask_specid_pair_low, unmask_specid_pair_up, unmaskdetids, unmaskdetidpairsL, unmaskdetidpairsU);
this->detectorToDetectors(unmask_detid_single, unmask_detid_pair_low, unmask_detid_pair_up, unmaskdetids, unmaskdetidpairsL, unmaskdetidpairsU);
// 4. Apply
this->processMaskOnDetectors(true, maskdetids, maskdetidpairsL, maskdetidpairsU);
return;
}
/*
* Mask detectors or Unmask detectors
* params:
* @ tomask: true to mask, false to unmask
*/
void LoadMaskingFile::processMaskOnDetectors(bool tomask, std::vector<int32_t> singledetids, std::vector<int32_t> pairdetids_low,
std::vector<int32_t> pairdetids_up){
// 1. Initialize
if (tomask){
for (size_t i = 0; i < mMaskWS->getNumberHistograms(); i ++){
mMaskWS->dataY(i)[0] = 1;
}
} else {
for (size_t i = 0; i < mMaskWS->getNumberHistograms(); i ++){
mMaskWS->dataY(i)[0] = 0;
}
}
// 2. Mask
detid2index_map* indexmap = mMaskWS->getDetectorIDToWorkspaceIndexMap(true);
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()){
size_t index = it->second;
if (tomask)
mMaskWS->dataY(index)[0] = 0;
else
mMaskWS->dataY(index)[0] = 1;
} else {
g_log.error() << "Pixel w/ ID = " << detid << " Cannot Be Located" << std::endl;
}
}
for (size_t i = 0; i < pairdetids_low.size(); i ++){
g_log.error() << "To Be Implemented Soon!" << std::endl;
}
}
/*
* Convert bank to detectors
*/
void LoadMaskingFile::bankToDetectors(std::vector<std::string> singlebanks,
std::vector<int32_t>& detectors,
std::vector<int32_t>& detectorpairslow, std::vector<int32_t>& detectorpairsup){
for (size_t i = 0; i < singlebanks.size(); i ++){
g_log.information() << "Bank: " << singlebanks[i] << std::endl;
}
Geometry::Instrument_const_sptr minstrument = mMaskWS->getInstrument();
for (size_t ib = 0; ib < singlebanks.size(); ib++){
std::vector<Geometry::IDetector_const_sptr> idetectors;
minstrument->getDetectorsInBank(idetectors, singlebanks[ib]);
g_log.information() << singlebanks[ib] << " has " << idetectors.size() << " detectors" << std::endl;
// a) get information
size_t numdets = idetectors.size();
detid_t detid_first = idetectors[0]->getID();
detid_t detid_last = idetectors[idetectors.size()-1]->getID();
// b) set detectors
if (detid_first+int32_t(numdets) == detid_last+1 && false){
// TODO This save-time method is not used at this stage
g_log.information() << "Using Range of Detectors" << std::endl;
detectorpairslow.push_back(detid_first);
detectorpairsup.push_back(detid_last);
} else {
g_log.information() << "Apply 1 by 1 " << "DetID: " << detid_first << ", " << detid_last << std::endl;
for (size_t i = 0; i < idetectors.size(); i ++){
Geometry::IDetector_const_sptr det = idetectors[i];
int32_t detid = det->getID();
detectors.push_back(detid);
}
} // if-else
} // ENDFOR
return;
}
/*
* Convert spectrum to detectors
*/
void LoadMaskingFile::spectrumToDetectors(std::vector<int32_t> singles, std::vector<int32_t> pairslow, std::vector<int32_t> pairsup,
std::vector<int32_t>& detectors,
std::vector<int32_t>& detectorpairslow, std::vector<int32_t>& detectorpairsup){
g_log.error() << "SpectrumID in XML File (ids) Is Not Supported! Spectrum IDs" << std::endl;
for (size_t i = 0; i < singles.size(); i ++){
g_log.information() << "Not Taking Into Account: Spectrum " << singles[i] << std::endl;
}
for (size_t i = 0; i < pairslow.size(); i ++){
g_log.information() << "Not Taking Into Account: Spectrum " << pairslow[i] << " To " << pairsup[i] << std::endl;
}
}
/*
* Convert spectrum to detectors
*/
void LoadMaskingFile::detectorToDetectors(std::vector<int32_t> singles, std::vector<int32_t> pairslow, std::vector<int32_t> pairsup,
std::vector<int32_t>& detectors,
std::vector<int32_t>& detectorpairslow, std::vector<int32_t>& detectorpairsup){
/*
for (size_t i = 0; i < singles.size(); i ++){
g_log.information() << "Detector " << singles[i] << std::endl;
}
for (size_t i = 0; i < pairslow.size(); i ++){
g_log.information() << "Detector " << pairslow[i] << " To " << pairsup[i] << std::endl;
}
*/
for (size_t i = 0; i < singles.size(); i ++){
detectors.push_back(singles[i]);
}
for (size_t i = 0; i < pairslow.size(); i ++){
for (int32_t j = 0; j < pairsup[i]-pairslow[i]+1; j ++){
int32_t detid = pairslow[i]+j;
detectors.push_back(detid);
}
/*
detectorpairslow.push_back(pairslow[i]);
detectorpairsup.push_back(pairsup[i]);
*/
}
return;
}
/*
* Initalize Poco XML Parser
*/
void LoadMaskingFile::initializeXMLParser(const std::string & filename)
{
// const std::string instName;
const std::string xmlText = Kernel::Strings::loadFile(filename);
// Set up the DOM parser and parse xml file
DOMParser pParser;
try
{
pDoc = pParser.parseString(xmlText);
}
catch(Poco::Exception& exc)
{
throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse File:", filename);
}
catch(...)
{
throw Kernel::Exception::FileError("Unable to parse File:" , filename);
}
// Get pointer to root element
pRootElem = pDoc->documentElement();
if ( !pRootElem->hasChildNodes() )
{
g_log.error("XML file: " + filename + "contains no root element.");
throw Kernel::Exception::InstrumentDefinitionError("No root element in XML instrument file", filename);
}
}
/*
* Parse XML file
*/
void LoadMaskingFile::parseXML()
{
// 0. Check
if (!pDoc)
throw std::runtime_error("Call LoadMaskingFile::initialize() before parseXML.");
// 1. Parse and create a structure
NodeList* pNL_type = pRootElem->getElementsByTagName("type");
g_log.information() << "Node Size = " << pNL_type->length() << std::endl;
Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
Poco::XML::Node* pNode = it.nextNode();
bool tomask = true;
bool ingroup = false;
while (pNode){
const Poco::XML::XMLString value = pNode->innerText();
if (pNode->nodeName().compare("group") == 0){
// Node "group"
ingroup = true;
// get type
Poco::XML::NamedNodeMap* att = pNode->attributes();
Poco::XML::Node* cNode = att->item(0);
if (cNode->getNodeValue().compare("mask") == 0 || cNode->getNodeValue().compare("notuse") == 0){
tomask = true;
} else if (cNode->getNodeValue().compare("unmask") == 0 || cNode->getNodeValue().compare("use") == 0){
tomask = false;
} 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;
} else if (pNode->nodeName().compare("component") == 0){
// Node "component"
if (ingroup){
this->parseBank(value, tomask);
} else {
g_log.error() << "XML File heirachial (component) error!" << std::endl;
}
// g_log.information() << "Component: " << value << std::endl;
} else if (pNode->nodeName().compare("ids") == 0){
// Node "ids"
if (ingroup){
this->parseSpectrumIDs(value, tomask);
} else {
g_log.error() << "XML File (ids) heirachial error!" << " Inner Text = " << pNode->innerText() << std::endl;
}
// g_log.information() << "detids: " << value << std::endl;
} else if (pNode->nodeName().compare("detids") == 0){
// Node "detids"
if (ingroup){
this->parseDetectorIDs(value, tomask);
} else {
g_log.error() << "XML File (detids) heirachial error!" << std::endl;
}
}
pNode = it.nextNode();
} // ENDWHILE
return;
}
/*
* Parse bank IDs (string name)
* Sample: bank2
* params:
* @valutext: must be bank name
*/
void LoadMaskingFile::parseBank(std::string valuetext, bool tomask){
// 1. Parse bank out
std::vector<std::string> values;
this->splitString(valuetext, values, "bank");
if (values.size() <= 1){
g_log.error() << "Bank information format error!" << std::endl;
return;
}
if (tomask){
mask_bankid_single.push_back(valuetext);
} else {
unmask_bankid_single.push_back(valuetext);
}
/*
for (size_t i = 0; i < singles.size(); i ++){
g_log.information() << "Bank " << singles[i] << std::endl;
}
for (size_t i = 0; i < pairs.size()/2; i ++){
g_log.information() << "Bank " << pairs[2*i] << " To " << pairs[2*i+1] << std::endl;
}
*/
return;
}
/*
* Parse input string for spectrum ID
*/
void LoadMaskingFile::parseSpectrumIDs(std::string inputstr, bool tomask){
g_log.error() << "SpectrumID in XML File (ids) Is Not Supported! Spectrum IDs: " << inputstr << std::endl;
// 1. Parse range out
std::vector<int32_t> singles;
std::vector<int32_t> pairs;
this->parseRangeText(inputstr, singles, pairs);
// 2. Set to data storage
if (tomask){
for (size_t i = 0; i < singles.size(); i ++){
mask_specid_single.push_back(singles[i]);
}
for (size_t i = 0; i < pairs.size()/2; i ++){
mask_specid_pair_low.push_back(pairs[2*i]);
mask_specid_pair_up.push_back(pairs[2*i+1]);
}
} else {
for (size_t i = 0; i < singles.size(); i ++){
unmask_specid_single.push_back(singles[i]);
}
for (size_t i = 0; i < pairs.size()/2; i ++){
unmask_specid_pair_low.push_back(pairs[2*i]);
unmask_specid_pair_up.push_back(pairs[2*i+1]);
}
}
return;
}
/*
* Parse input string for spectrum ID
*/
void LoadMaskingFile::parseDetectorIDs(std::string inputstr, bool tomask){
// g_log.information() << "Detector IDs: " << inputstr << std::endl;
// 1. Parse range out
std::vector<int32_t> singles;
std::vector<int32_t> pairs;
this->parseRangeText(inputstr, singles, pairs);
// 2. Set to data storage
if (tomask){
for (size_t i = 0; i < singles.size(); i ++){
mask_detid_single.push_back(singles[i]);
}
for (size_t i = 0; i < pairs.size()/2; i ++){
mask_detid_pair_low.push_back(pairs[2*i]);
mask_detid_pair_up.push_back(pairs[2*i+1]);
}
} else {
for (size_t i = 0; i < singles.size(); i ++){
unmask_detid_single.push_back(singles[i]);
}
for (size_t i = 0; i < pairs.size()/2; i ++){
unmask_detid_pair_low.push_back(pairs[2*i]);
unmask_detid_pair_up.push_back(pairs[2*i+1]);
}
}
return;
}
/*
* Parse index range text to singles and pairs
* Example: 3,4,9-10,33
*/
void LoadMaskingFile::parseRangeText(std::string inputstr, std::vector<int32_t>& singles, std::vector<int32_t>& pairs){
// 1. Split ','
std::vector<std::string> rawstrings;
this->splitString(inputstr, rawstrings, ",");
// 2. Filter
std::vector<std::string> strsingles;
std::vector<std::string> strpairs;
for (size_t i = 0; i < rawstrings.size(); i ++){
// a) Find '-':
bool containto = false;
const char* tempchs = rawstrings[i].c_str();
for (size_t j = 0; j < rawstrings[i].size(); j ++)
if (tempchs[j] == '-'){
containto = true;
break;
}
// b) Rebin
if (containto)
strpairs.push_back(rawstrings[i]);
else
strsingles.push_back(rawstrings[i]);
} // ENDFOR i
// 3. Treat singles
for (size_t i = 0; i < strsingles.size(); i ++){
int32_t itemp = atoi(strsingles[i].c_str());
singles.push_back(itemp);
}
// 4. Treat pairs
for (size_t i = 0; i < strpairs.size(); i ++){
// a) split and check
std::vector<std::string> ptemp;
this->splitString(strpairs[i], ptemp, "-");
if (ptemp.size() != 2){
g_log.error() << "Range string " << strpairs[i] << " has a wrong format!" << std::endl;
throw std::invalid_argument("Wrong format");
}
// b) parse
int32_t intstart = atoi(ptemp[0].c_str());
int32_t intend = atoi(ptemp[1].c_str());
if (intstart >= intend){
g_log.error() << "Range string " << strpairs[i] << " has a reversed order" << std::endl;
throw std::invalid_argument("Wrong format");
}
pairs.push_back(intstart);
pairs.push_back(intend);
}
return;
}
void LoadMaskingFile::splitString(std::string inputstr, std::vector<std::string>& strings, std::string sep){
// std::vector<std::string> SplitVec;
boost::split(strings, inputstr, boost::is_any_of(sep), boost::token_compress_on);
// g_log.information() << "Inside... split size = " << strings.size() << std::endl;
return;
}
/*
* Initialize the Mask Workspace with instrument
*/
void LoadMaskingFile::intializeMaskWorkspace(){
// 1. Execute algorithm LoadInstrument() to a temporary Workspace
API::Algorithm_sptr childAlg = createSubAlgorithm("LoadInstrument");
MatrixWorkspace_sptr tempWS(new DataObjects::Workspace2D());
childAlg->setProperty<MatrixWorkspace_sptr>("Workspace", tempWS);
childAlg->setPropertyValue("InstrumentName", mInstrumentName);
childAlg->setProperty("RewriteSpectraMap", false);
childAlg->executeAsSubAlg();
// 2. Use the instrument in the temp Workspace for new MaskWorkspace
Geometry::Instrument_const_sptr minstrument = tempWS->getInstrument();
API::MatrixWorkspace_sptr mm = API::MatrixWorkspace_sptr(new DataObjects::SpecialWorkspace2D(minstrument));
mMaskWS = mm;
mMaskWS->setTitle("Mask");
return;
}
} // namespace Mantid
} // namespace DataHandling