Mantid
Loading...
Searching...
No Matches
SaveDspacemap.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include "MantidKernel/System.h"
12#include <fstream>
13
14namespace Mantid::DataHandling {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(SaveDspacemap)
18
19using namespace Mantid::Kernel;
20using namespace Mantid::API;
21using namespace Mantid::DataObjects;
22using namespace Mantid::Geometry;
23
24//----------------------------------------------------------------------------------------------
28 declareProperty(std::make_unique<WorkspaceProperty<OffsetsWorkspace>>("InputWorkspace", "", Direction::Input),
29 "An input OffsetsWorkspace to save.");
30
31 declareProperty(std::make_unique<FileProperty>("DspacemapFile", "", FileProperty::Save, ".dat"),
32 "The DspacemapFile on output contains the d-space mapping");
33
34 declareProperty("PadDetID", 300000, "Pad Data to this number of pixels");
35}
36
37//----------------------------------------------------------------------------------------------
41 Mantid::DataObjects::OffsetsWorkspace_sptr offsetsWS = getProperty("InputWorkspace");
42 std::string filename = getPropertyValue("DspacemapFile");
43 CalculateDspaceFromCal(offsetsWS, filename);
44}
45
46//-----------------------------------------------------------------------
55 const std::string &DFileName) {
56 const char *filename = DFileName.c_str();
57 // Get a pointer to the instrument contained in the workspace
58 Instrument_const_sptr instrument = offsetsWS->getInstrument();
59 const auto &detectorInfo = offsetsWS->detectorInfo();
60 double l1;
61 Kernel::V3D beamline, samplePos;
62 double beamline_norm;
63 instrument->getInstrumentParameters(l1, beamline, beamline_norm, samplePos);
64
65 // To get all the detector ID's
66 detid2det_map allDetectors;
67 instrument->getDetectors(allDetectors);
68
69 detid2det_map::const_iterator it;
70 detid_t maxdetID = 0;
71 for (it = allDetectors.begin(); it != allDetectors.end(); ++it) {
72 detid_t detectorID = it->first;
73 if (detectorID > maxdetID)
74 maxdetID = detectorID;
75 }
76
77 detid_t paddetID = detid_t(getProperty("PadDetID"));
78 if (maxdetID < paddetID)
79 maxdetID = paddetID;
80
81 // Now write the POWGEN-style Dspace mapping file
82 std::ofstream fout(filename, std::ios_base::out | std::ios_base::binary);
83 Progress prog(this, 0.0, 1.0, maxdetID);
84
85 for (detid_t i = 0; i != maxdetID; ++i) {
86 // Compute the factor
87 double factor;
89 // Find the detector with that detector id
90 it = allDetectors.find(i);
91 if (it != allDetectors.end()) {
92 det = it->second;
93 const auto detectorIndex = detectorInfo.indexOf(i);
95 l1, detectorInfo.l2(detectorIndex), detectorInfo.twoTheta(detectorIndex), offsetsWS->getValue(i, 0.0));
96 // Factor of 10 between ISAW and Mantid
97 factor *= 0.1;
98 if (factor < 0)
99 factor = 0.0;
100 fout.write(reinterpret_cast<char *>(&factor), sizeof(double));
101 } else {
102 factor = 0;
103 fout.write(reinterpret_cast<char *>(&factor), sizeof(double));
104 }
105 // Report progress
106 prog.report();
107 }
108 fout.close();
109}
110
111} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IntArray detectorIndex
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void CalculateDspaceFromCal(const Mantid::DataObjects::OffsetsWorkspace_sptr &offsetsWS, const std::string &DFileName)
Make a map of the conversion factors between tof and D-spacing for all pixel IDs in a workspace.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< OffsetsWorkspace > OffsetsWorkspace_sptr
shared pointer to the OffsetsWorkspace class
MANTID_GEOMETRY_DLL double tofToDSpacingFactor(const double l1, const double l2, const double twoTheta, const double offset)
Calculate and return conversion factor from tof to d-spacing.
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
Definition: IDetector.h:102
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
std::map< detid_t, Geometry::IDetector_const_sptr > detid2det_map
Typedef of a map from detector ID to detector shared pointer.
Definition: Instrument.h:27
@ Input
An input workspace.
Definition: Property.h:53