101 peaksW =
m_ws->clone();
102 auto inst = peaksW->getInstrument();
103 std::vector<Peak> &peaks = peaksW->getPeaks();
116 if (peaksW->sample().hasOrientedLattice()) {
117 ol = peaksW->sample().getOrientedLattice();
122 std::ifstream in(fileUB.c_str());
124 throw std::runtime_error(
"A file containing the UB matrix must be input into UBFilename.");
127 UB_alg->setProperty(
"PeaksWorkspace", peaksW);
128 UB_alg->setProperty(
"Filename", fileUB);
129 UB_alg->executeAsChildAlg();
134 int bankSequence = 0;
139 std::string convention = ConfigService::Instance().getString(
"Q.convention");
140 if (convention ==
"Crystallography") {
145 out.open(filename.c_str(), std::ios::out);
149 std::string bankPart =
"?";
152 using bankMap_t = std::map<int, std::vector<size_t>>;
153 using runMap_t = std::map<int, bankMap_t>;
154 std::set<int> uniqueBanks;
155 std::set<int> uniqueRuns;
157 for (
size_t i = 0; i < peaks.size(); ++i) {
158 Peak const &p = peaks[i];
162 if (bankName.size() <= 4) {
163 g_log.
information() <<
"Could not interpret bank number of peak " << i <<
"(" << bankName <<
")\n";
168 bankPart = bankName.substr(0, 4);
170 if (bankPart ==
"bank")
171 bankName = bankName.substr(4, bankName.size() - 4);
172 else if (bankPart ==
"WISH")
173 bankName = bankName.substr(9, bankName.size() - 9);
177 if (type.compare(0, 2,
"Ru") == 0)
178 runMap[run][bank].emplace_back(i);
180 runMap[bank][run].emplace_back(i);
182 uniqueBanks.insert(bank);
183 uniqueRuns.insert(run);
186 bool correctPeaks =
getProperty(
"ApplyAnvredCorrections");
187 std::vector<std::vector<double>> spectra;
188 std::vector<std::vector<double>> time;
195 API::Run &run = peaksW->mutableRun();
205 const Material &sampleMaterial = peaksW->sample().getMaterial();
206 const IObject *sampleShape{
nullptr};
215 sphere->setMaterial(
Material(
"SetInSaveHKL", neutron, 1.0));
217 sphere->setMaterial(sampleMaterial);
222 peaksW->mutableSample().setShape(sphere);
223 }
else if (peaksW->sample().getShape().hasValidShape()) {
224 sampleShape = &(peaksW->sample().getShape());
227 if (std::all_of(peaks.cbegin(), peaks.cend(), [](
auto &p) { return p.getAbsorptionWeightedPathLength() == 0; })) {
229 alg->setProperty(
"InputWorkspace", peaksW);
230 alg->setProperty(
"UseSinglePath",
true);
231 alg->executeAsChildAlg();
236 std::ifstream infile;
238 infile.open(spectraFile.c_str());
239 if (infile.is_open()) {
240 std::string ignoreLine;
242 for (
int wi = 0; wi < 8; wi++)
243 getline(infile, ignoreLine);
244 while (!infile.eof()) {
246 spectra.resize(a + 1);
248 getline(infile, line);
249 std::stringstream ss(line);
250 if (line.find(
"Bank") == std::string::npos) {
251 double time0, spectra0;
252 ss >> time0 >> spectra0;
253 time[a].emplace_back(time0);
254 spectra[a].emplace_back(spectra0);
259 ss >> temp >> a0 >> temp >> a;
266 std::set<size_t> banned;
269 if (peaksW->sample().hasOrientedLattice()) {
270 maxOrder = peaksW->sample().getOrientedLattice().getMaxOrder();
273 runMap_t::iterator runMap_it;
274 for (runMap_it = runMap.begin(); runMap_it != runMap.end(); ++runMap_it) {
277 bankMap_t &bankMap = runMap_it->second;
279 bankMap_t::iterator bankMap_it;
280 for (bankMap_it = bankMap.begin(); bankMap_it != bankMap.end(); ++bankMap_it) {
283 std::vector<size_t> &ids = bankMap_it->second;
286 for (
auto wi : ids) {
309 (p.
getCol() < widthBorder || p.
getRow() < widthBorder || p.
getCol() > (nCols - widthBorder) ||
310 p.
getRow() > (nRows - widthBorder))) {
315 bankName.erase(remove_if(bankName.begin(), bankName.end(), std::not_fn(::isdigit)), bankName.end());
324 if (dsp < dMin || lambda < wlMin || lambda > wlMax) {
329 double transmission{0}, tbar{0};
333 }
else if (sampleShape) {
335 transmission = exp(-tbar * 0.01 * sampleShape->material().attenuationCoefficient(
lambda));
353 out << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getH()
354 << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getK()
355 << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getL();
357 double correc = scaleFactor;
359 double relSigSpect = 0.0;
360 bankSequence =
static_cast<int>(std::distance(uniqueBanks.begin(), uniqueBanks.find(bank)));
361 runSequence =
static_cast<int>(std::distance(uniqueRuns.begin(), uniqueRuns.find(runNumber)));
364 double mu = (9.614 *
lambda) + 0.266;
366 double eff_center = 1.0 - std::exp(-
mu * depth);
370 double eff_R = 1.0 - exp(-
mu * pathlength);
371 double sp_ratio = eff_center / eff_R;
372 double sinsqt = std::pow(
lambda / (2.0 * dsp), 2);
380 std::vector<double> xdata(1, 1.0);
381 std::vector<double> ydata;
383 double l1 = p.
getL1();
386 unit->toTOF(xdata, ydata, l1, 0, {{UnitParams::l2,
l2}, {UnitParams::twoTheta, theta2}});
387 double one = xdata[0];
388 double spect1 =
spectrumCalc(one, iSpec, time, spectra, bank);
389 relSigSpect = std::sqrt((1.0 / spect) + (1.0 / spect1));
393 throw std::runtime_error(
"Wavelength for normalizing to spectrum is out of range.");
395 correc = scaleFactor * sinsqt * cmonx * sp_ratio / (wl4 * spect * transmission);
397 if (inst->hasParameter(
"detScale" + bankName)) {
398 correc *=
static_cast<double>(inst->getNumberParameter(
"detScale" + bankName)[0]);
402 instBkg = 0. * 12.28 / cmonx * scaleFactor;
409 std::pow(relSigSpect * correc * p.
getIntensity(), 2) + instBkg);
412 if (ckIntensity > 99999.985)
414 <<
" is too large for format. Decrease ScalePeaks.\n";
415 out << std::setw(8) << std::fixed << std::setprecision(2) << ckIntensity;
417 out << std::setw(8) << std::fixed << std::setprecision(2) << cksigI;
420 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getIntensity();
422 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getSigmaIntensity();
424 if (type.compare(0, 2,
"Ba") == 0)
425 out << std::setw(4) << bankSequence + 1;
427 out << std::setw(4) << runSequence + 1;
429 out << std::setw(8) << std::fixed << std::setprecision(5) <<
lambda;
430 out << std::setw(8) << std::fixed << std::setprecision(5) << tbar;
435 V3D reverse_incident_cos = ol.
cosFromDir(reverse_incident_dir);
441 for (
int k = 0; k < 3; ++k) {
442 out << std::setw(9) << std::fixed << std::setprecision(5) << reverse_incident_cos[k];
443 out << std::setw(9) << std::fixed << std::setprecision(5) << scattered_cos[k];
447 out << std::setw(6) << runNumber;
450 out << std::setw(7) << seqNum;
452 out << std::setw(7) << wi + 1;
455 out << std::setw(7) << std::fixed << std::setprecision(4) << transmission;
457 out << std::setw(4) << std::right << bank;
459 out << std::setw(9) << std::fixed << std::setprecision(5) << scattering;
461 out << std::setw(8) << std::fixed << std::setprecision(4) << dsp;
464 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getCol());
465 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getRow());
473 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
475 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
479 out << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL)
480 << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL) << std::fixed
481 << std::setprecision(decimalHKL) << 0.0;
484 out <<
" 0.00 0.00 0 0.00000 0.00000"
485 " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000"
486 " 0 0 0.0000 0 0.00000 0.0000 0.00 0.00";
488 out <<
" 0.00 0.00 0 0.00000 0.00000"
489 " 0 0 0.0000 0 0.00000 0.0000";
495 std::vector<int> badPeaks;
496 for (
auto it = banned.crbegin(); it != banned.crend(); ++it) {
497 badPeaks.emplace_back(
static_cast<int>(*it));
499 peaksW->removePeaks(std::move(badPeaks));
502 if (append && std::filesystem::exists(filename)) {
504 load_alg->setPropertyValue(
"Filename", filename);
505 load_alg->setProperty(
"OutputWorkspace",
"peaks");
506 load_alg->executeAsChildAlg();
509 ws2->setInstrument(inst);
512 plus_alg->setProperty(
"LHSWorkspace", peaksW);
513 plus_alg->setProperty(
"RHSWorkspace", ws2);
514 plus_alg->executeAsChildAlg();
516 peaksW = plus_alg->getProperty(
"OutputWorkspace");