38 "Incident spectrum and detector efficiency correction file.");
40 "An output Workspace containing spectra for each detector bank.");
42 getInstrument3WaysInit(
this);
51 const V3D samplePos = inst->getSample()->getPos();
52 const V3D pos = inst->getSource()->getPos() - samplePos;
53 double l1 = pos.
norm();
58 infile.open(spectraFile.c_str());
61 std::vector<std::vector<double>> spectra;
62 std::vector<std::vector<double>> time;
64 for (
int wi = 0; wi < 8; wi++)
69 spectra.resize(a + 1);
73 std::stringstream ss(
STRING);
74 if (
STRING.find(
"Bank") == std::string::npos) {
75 double time0, spectra0;
76 ss >> time0 >> spectra0;
77 time[a].emplace_back(time0);
78 spectra[a].emplace_back(spectra0);
86 std::vector<std::shared_ptr<RectangularDetector>> detList;
87 for (
int i = 0; i < inst->nelements(); i++) {
88 std::shared_ptr<RectangularDetector> det;
89 std::shared_ptr<ICompAssembly> assem;
90 std::shared_ptr<ICompAssembly> assem2;
92 det = std::dynamic_pointer_cast<RectangularDetector>((*inst)[i]);
94 detList.emplace_back(det);
99 assem = std::dynamic_pointer_cast<ICompAssembly>((*inst)[i]);
101 for (
int j = 0; j < assem->nelements(); j++) {
102 det = std::dynamic_pointer_cast<RectangularDetector>((*assem)[j]);
104 detList.emplace_back(det);
110 assem2 = std::dynamic_pointer_cast<ICompAssembly>((*assem)[j]);
112 for (
int k = 0; k < assem2->nelements(); k++) {
113 det = std::dynamic_pointer_cast<RectangularDetector>((*assem2)[k]);
115 detList.emplace_back(det);
125 if (spectra.size() < 1)
126 throw std::runtime_error(
"The number of spectra in the loaded file is zero.");
130 outWS->setInstrument(inst);
131 outWS->getAxis(0)->setUnit(
"TOF");
132 outWS->setYUnit(
"Counts");
133 outWS->setDistribution(
true);
134 outWS->rebuildSpectraMapping(
false);
137 for (
size_t i = 0; i < spectra.size(); i++) {
138 auto &outSpec = outWS->getSpectrum(i);
139 outSpec.clearDetectorIDs();
140 for (
int j = 0; j < detList[i]->xpixels(); j++)
141 for (
int k = 0; k < detList[i]->ypixels(); k++)
142 outSpec.addDetectorID(
static_cast<detid_t>(detList[i]->getDetectorIDAtXY(j, k)));
143 auto &outX = outSpec.mutableX();
144 auto &outY = outSpec.mutableY();
145 auto &outE = outSpec.mutableE();
147 V3D dir = detList[i]->getPos() - samplePos;
150 std::vector<double> xdata(1, 1.0);
151 std::vector<double> ydata;
155 double theta2 = dir.
angle(
V3D(0.0, 0.0, 1.0));
158 unit->toTOF(xdata, ydata, l1, 0,
160 {UnitParams::l2,
l2},
161 {UnitParams::twoTheta, theta2},
163 double one = xdata[0];
164 double spect1 =
spectrumCalc(one, iSpec, time, spectra, i);
166 for (
size_t j = 0; j < spectra[i].size(); j++) {
167 double spect = spectra[i][j];
169 double relSigSpect = std::sqrt((1.0 / spect) + (1.0 / spect1));
172 outX[j] = time[i][j];
174 outE[j] = relSigSpect;
176 throw std::runtime_error(
"Wavelength for normalizing to spectrum is out of range.");
184 convertAlg->execute();
185 outWS = convertAlg->getProperty(
"OutputWorkspace");
191 std::vector<std::vector<double>> spectra,
size_t id) {
196 double T =
TOF / 1000.;
198 double c1 = spectra[id][0];
199 double c2 = spectra[id][1];
200 double c3 = spectra[id][2];
201 double c4 = spectra[id][3];
202 double c5 = spectra[id][4];
203 double c6 = spectra[id][5];
204 double c7 = spectra[id][6];
205 double c8 = spectra[id][7];
206 double c9 = spectra[id][8];
207 double c10 = spectra[id][9];
208 double c11 = spectra[id][10];
210 spect = c1 + c2 * exp(-c3 / std::pow(T, 2)) / std::pow(T, 5) + c4 * exp(-c5 * std::pow(T, 2)) +
211 c6 * exp(-c7 * std::pow(T, 3)) + c8 * exp(-c9 * std::pow(T, 4)) + c10 * exp(-c11 * std::pow(T, 5));
214 for (i = 1; i < spectra[0].size() - 1; ++i)
215 if (
TOF < time[
id][i])
217 spect = spectra[id][i - 1] +
218 (
TOF - time[id][i - 1]) / (time[
id][i] - time[
id][i - 1]) * (spectra[id][i] - spectra[id][i - 1]);
228 std::string grpName(
"Specify the Instrument");
232 "Optional: An input workspace with the instrument we want to use.");
235 "Optional: Name of the instrument to base the "
236 "GroupingWorkspace on which to base the "
237 "GroupingWorkspace.");
240 "Optional: Path to the instrument definition file on "
241 "which to base the GroupingWorkspace.");
256 std::string InstrumentFilename = alg->
getPropertyValue(
"InstrumentFilename");
262 if (!InstrumentName.empty())
264 if (!InstrumentFilename.empty())
268 throw std::invalid_argument(
"You must specify exactly ONE way to get an "
269 "instrument (workspace, instrument name, or "
270 "IDF file). You specified more than one.");
272 throw std::invalid_argument(
"You must specify exactly ONE way to get an "
273 "instrument (workspace, instrument name, or "
274 "IDF file). You specified none.");
279 inst = inWS->getInstrument();
284 childAlg->setPropertyValue(
"Filename", InstrumentFilename);
285 childAlg->setPropertyValue(
"InstrumentName", InstrumentName);
287 childAlg->executeAsChildAlg();
288 inst = tempWS->getInstrument();
#define DECLARE_ALGORITHM(classname)
Base class from which all concrete algorithm classes should be derived.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
@ Load
allowed here which will be passed to the algorithm
A property class for workspaces.
Load incident spectrum and detector efficiency correction file.
double spectrumCalc(double TOF, int iSpec, std::vector< std::vector< double > > time, std::vector< std::vector< double > > spectra, size_t id)
Geometry::Instrument_const_sptr getInstrument3Ways(Algorithm *alg)
Get a pointer to an instrument in one of 3 ways: InputWorkspace, InstrumentName, InstrumentFilename.
void exec() override
Run the algorithm.
void getInstrument3WaysInit(Algorithm *alg)
For use by getInstrument3Ways, initializes the properties.
Concrete workspace implementation.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
double angle(const V3D &) const
Angle between this and another vector.
double norm() const noexcept
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
A namespace containing physical constants that are required by algorithms and unit routines.
int32_t detid_t
Typedef for a detector ID.
@ Input
An input workspace.
@ Output
An output workspace.