-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveDspacemap.cpp
108 lines (95 loc) · 3.63 KB
/
SaveDspacemap.cpp
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
#include "MantidDataHandling/SaveDspacemap.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include <fstream>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SaveDspacemap)
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SaveDspacemap::init() {
declareProperty(make_unique<WorkspaceProperty<OffsetsWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input OffsetsWorkspace to save.");
declareProperty(make_unique<FileProperty>("DspacemapFile", "",
FileProperty::Save, ".dat"),
"The DspacemapFile on output contains the d-space mapping");
declareProperty("PadDetID", 300000, "Pad Data to this number of pixels");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SaveDspacemap::exec() {
Mantid::DataObjects::OffsetsWorkspace_sptr offsetsWS =
getProperty("InputWorkspace");
std::string filename = getPropertyValue("DspacemapFile");
CalculateDspaceFromCal(offsetsWS, filename);
}
//-----------------------------------------------------------------------
/**
* Make a map of the conversion factors between tof and D-spacing
* for all pixel IDs in a workspace.
*
* @param DFileName name of dspacemap file
* @param offsetsWS :: OffsetsWorkspace with instrument and offsets
*/
void SaveDspacemap::CalculateDspaceFromCal(
Mantid::DataObjects::OffsetsWorkspace_sptr offsetsWS,
std::string DFileName) {
const char *filename = DFileName.c_str();
// Get a pointer to the instrument contained in the workspace
Instrument_const_sptr instrument = offsetsWS->getInstrument();
double l1;
Kernel::V3D beamline, samplePos;
double beamline_norm;
instrument->getInstrumentParameters(l1, beamline, beamline_norm, samplePos);
// To get all the detector ID's
detid2det_map allDetectors;
instrument->getDetectors(allDetectors);
detid2det_map::const_iterator it;
detid_t maxdetID = 0;
for (it = allDetectors.begin(); it != allDetectors.end(); ++it) {
detid_t detectorID = it->first;
if (detectorID > maxdetID)
maxdetID = detectorID;
}
detid_t paddetID = detid_t(getProperty("PadDetID"));
if (maxdetID < paddetID)
maxdetID = paddetID;
// Now write the POWGEN-style Dspace mapping file
std::ofstream fout(filename, std::ios_base::out | std::ios_base::binary);
Progress prog(this, 0.0, 1.0, maxdetID);
for (detid_t i = 0; i != maxdetID; i++) {
// Compute the factor
double factor;
Geometry::IDetector_const_sptr det;
// Find the detector with that detector id
it = allDetectors.find(i);
if (it != allDetectors.end()) {
det = it->second;
factor = Instrument::calcConversion(l1, beamline, beamline_norm,
samplePos, det->getPos(),
offsetsWS->getValue(i, 0.0));
// Factor of 10 between ISAW and Mantid
factor *= 0.1;
if (factor < 0)
factor = 0.0;
fout.write(reinterpret_cast<char *>(&factor), sizeof(double));
} else {
factor = 0;
fout.write(reinterpret_cast<char *>(&factor), sizeof(double));
}
// Report progress
prog.report();
}
fout.close();
}
} // namespace Mantid
} // namespace DataHandling