Newer
Older
Janik Zikovsky
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
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataHandling/LoadCalFile.h"
#include "MantidDataObjects/GroupingWorkspace.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidGeometry/IInstrument.h"
#include "MantidKernel/System.h"
#include <fstream>
#include "MantidDataObjects/SpecialWorkspace2D.h"
using Mantid::Geometry::IInstrument_sptr;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid
{
namespace DataHandling
{
// Get the logger
Kernel::Logger& LoadCalFile::g_log = Kernel::Logger::get("LoadCalFile");
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadCalFile)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
LoadCalFile::LoadCalFile()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
LoadCalFile::~LoadCalFile()
{
}
//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void LoadCalFile::initDocs()
{
this->setWikiSummary("Loads a 5-column ASCII .cal file into up to 3 workspaces: a GroupingWorkspace, OffsetsWorkspace and/or MaskWorkspace.");
this->setOptionalMessage("Loads a 5-column ASCII .cal file into up to 3 workspaces: a GroupingWorkspace, OffsetsWorkspace and/or MaskWorkspace.");
Janik Zikovsky
committed
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
}
//----------------------------------------------------------------------------------------------
/** For use by getInstrument3Ways, initializes the properties
* @param alg :: algorithm to which to add the properties.
* */
void LoadCalFile::getInstrument3WaysInit(Algorithm * alg)
{
alg->declareProperty(new WorkspaceProperty<>("InputWorkspace","",Direction::Input, true),
"Optional: An input workspace with the instrument we want to use.");
alg->declareProperty(new PropertyWithValue<std::string>("InstrumentName","",Direction::Input),
"Optional: Name of the instrument to base the GroupingWorkspace on which to base the GroupingWorkspace.");
alg->declareProperty(new FileProperty("InstrumentFilename", "", FileProperty::OptionalLoad, ".xml"),
"Optional: Path to the instrument definition file on which to base the GroupingWorkspace.");
}
//----------------------------------------------------------------------------------------------
/** Get a pointer to an instrument in one of 3 ways: InputWorkspace, InstrumentName, InstrumentFilename
* @param alg :: algorithm from which to get the property values.
* */
Mantid::Geometry::IInstrument_sptr LoadCalFile::getInstrument3Ways(Algorithm * alg)
{
MatrixWorkspace_sptr inWS = alg->getProperty("InputWorkspace");
std::string InputWorkspace = alg->getPropertyValue("InputWorkspace");
std::string InstrumentName = alg->getPropertyValue("InstrumentName");
std::string InstrumentFilename = alg->getPropertyValue("InstrumentFilename");
// Some validation
int numParams = 0;
if (inWS) numParams++;
if (!InstrumentName.empty()) numParams++;
if (!InstrumentFilename.empty()) numParams++;
if (numParams > 1)
throw std::invalid_argument("You must specify exactly ONE way to get an instrument (workspace, instrument name, or IDF file). You specified more than one.");
if (numParams == 0)
throw std::invalid_argument("You must specify exactly ONE way to get an instrument (workspace, instrument name, or IDF file). You specified none.");
// ---------- Get the instrument one of 3 ways ---------------------------
IInstrument_sptr inst;
if (inWS)
{
inst = inWS->getInstrument();
}
else
{
Algorithm_sptr childAlg = alg->createSubAlgorithm("LoadInstrument",0.0,0.2);
MatrixWorkspace_sptr tempWS(new Workspace2D());
childAlg->setProperty<MatrixWorkspace_sptr>("Workspace", tempWS);
childAlg->setPropertyValue("Filename", InstrumentFilename);
childAlg->setPropertyValue("InstrumentName", InstrumentName);
childAlg->executeAsSubAlg();
inst = tempWS->getInstrument();
}
return inst;
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void LoadCalFile::init()
{
LoadCalFile::getInstrument3WaysInit(this);
declareProperty(new FileProperty("CalFilename", "", FileProperty::Load, ".cal"),
"Path to the old-style .cal grouping/calibration file (multi-column ASCII). You must also specify the instrument.");
declareProperty(new PropertyWithValue<bool>("MakeGroupingWorkspace",true,Direction::Input),
"Set to true to create a GroupingWorkspace with called WorkspaceName_group.");
declareProperty(new PropertyWithValue<bool>("MakeOffsetsWorkspace",true,Direction::Input),
"Set to true to create a OffsetsWorkspace with called WorkspaceName_offsets.");
declareProperty(new PropertyWithValue<bool>("MakeMaskWorkspace",true,Direction::Input),
"Set to true to create a MaskWorkspace with called WorkspaceName_mask.");
Janik Zikovsky
committed
declareProperty(new PropertyWithValue<std::string>("WorkspaceName", "", Direction::Input),
"The base of the output workspace names. Names will have '_group', '_offsets', '_mask' appended to them.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void LoadCalFile::exec()
{
std::string CalFilename = getPropertyValue("CalFilename");
std::string WorkspaceName = getPropertyValue("WorkspaceName");
bool MakeGroupingWorkspace = getProperty("MakeGroupingWorkspace");
bool MakeOffsetsWorkspace = getProperty("MakeOffsetsWorkspace");
bool MakeMaskWorkspace = getProperty("MakeMaskWorkspace");
Janik Zikovsky
committed
if (WorkspaceName.empty())
throw std::invalid_argument("Must specify WorkspaceName.");
IInstrument_sptr inst = LoadCalFile::getInstrument3Ways(this);
GroupingWorkspace_sptr groupWS;
OffsetsWorkspace_sptr offsetsWS;
MatrixWorkspace_sptr maskWS;
// Initialize all required workspaces.
Janik Zikovsky
committed
if (MakeGroupingWorkspace)
{
groupWS = GroupingWorkspace_sptr(new GroupingWorkspace(inst));
declareProperty(new WorkspaceProperty<GroupingWorkspace>("OutputGroupingWorkspace", WorkspaceName + "_group", Direction::Output),
"Set the the output GroupingWorkspace, if any.");
setProperty("OutputGroupingWorkspace", groupWS);
Janik Zikovsky
committed
}
if (MakeOffsetsWorkspace)
{
offsetsWS = OffsetsWorkspace_sptr(new OffsetsWorkspace(inst));
declareProperty(new WorkspaceProperty<OffsetsWorkspace>("OutputOffsetsWorkspace", WorkspaceName + "_offsets", Direction::Output),
"Set the the output OffsetsWorkspace, if any.");
setProperty("OutputOffsetsWorkspace", offsetsWS);
Janik Zikovsky
committed
}
if (MakeMaskWorkspace)
Janik Zikovsky
committed
{
maskWS = MatrixWorkspace_sptr(new SpecialWorkspace2D(inst));
declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputMaskWorkspace", WorkspaceName + "_mask", Direction::Output),
"Set the the output MaskWorkspace, if any.");
setProperty("OutputMaskWorkspace", maskWS);
Janik Zikovsky
committed
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
}
LoadCalFile::readCalFile(CalFilename, groupWS, offsetsWS, maskWS);
}
//-----------------------------------------------------------------------
/** Reads the calibration file.
*
* @param calFileName :: path to the old .cal file
* @param groupWS :: optional, GroupingWorkspace to fill. Must be initialized to the right instrument.
* @param offsetsWS :: optional, OffsetsWorkspace to fill. Must be initialized to the right instrument.
* @param maskWS :: optional, masking-type workspace to fill. Must be initialized to the right instrument.
*/
void LoadCalFile::readCalFile(const std::string& calFileName,
GroupingWorkspace_sptr groupWS, OffsetsWorkspace_sptr offsetsWS, MatrixWorkspace_sptr maskWS)
{
bool doGroup = false;
if (groupWS) doGroup = true;
bool doOffsets = false;
if (offsetsWS) doOffsets = true;
bool doMask = false;
if (maskWS) doMask = true;
if (!doOffsets && !doGroup && !doMask)
throw std::invalid_argument("You must give at least one of the grouping, offsets or masking workspaces.");
std::ifstream grFile(calFileName.c_str());
if (!grFile)
{
throw std::runtime_error("Unable to open calibration file " + calFileName);
}
size_t numErrors = 0;
detid2index_map * detID_to_wi = NULL;
Janik Zikovsky
committed
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
if (doMask)
{
detID_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap( false );
}
std::string str;
while(getline(grFile,str))
{
if (str.empty() || str[0] == '#') continue;
std::istringstream istr(str);
int n,udet,select,group;
double offset;
istr >> n >> udet >> offset >> select >> group;
if (doOffsets)
{
if (offset < -1.) // should never happen
{
std::stringstream msg;
msg << "Encountered offset = " << offset << " at index " << n << " for udet = " << udet
<< ". Offsets must be greater than -1.";
throw std::runtime_error(msg.str());
}
try
{
offsetsWS->setValue(udet, offset);
}
catch (std::invalid_argument &)
Janik Zikovsky
committed
{
numErrors++;
}
}
if (doGroup)
{
try
{
groupWS->setValue(udet, double(group) );
}
catch (std::invalid_argument &)
Janik Zikovsky
committed
{
numErrors++;
}
}
if (doMask)
{
detid2index_map::const_iterator it = detID_to_wi->find(udet);
if (it != detID_to_wi->end())
Janik Zikovsky
committed
{
int wi = it->second;
if (select <= 0)
maskWS->maskWorkspaceIndex(wi, 0.0);
else
maskWS->dataY(wi)[0] = 1.0;
Janik Zikovsky
committed
}
Janik Zikovsky
committed
{
// Could not find the UDET.
Janik Zikovsky
committed
numErrors++;
}
}
}
// Warn about any errors
if (numErrors > 0)
g_log.warning() << numErrors << " errors (invalid Detector ID's) found when reading .cal file '" << calFileName << "'.\n";
if (doMask)
delete detID_to_wi;
}
} // namespace Mantid
} // namespace DataHandling