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 if (inst->getName() ==
"CORELLI")
372 const auto &componentInfo =
m_ws->componentInfo();
373 const size_t bankIndex = componentInfo.indexOfAny(p.
getBankName());
374 const auto children = componentInfo.children(bankIndex);
375 if (!children.empty()) {
376 det0 = componentInfo.componentID(children[0]);
381 double pathlength = depth / cosA;
382 double eff_R = 1.0 - exp(-
mu * pathlength);
383 double sp_ratio = eff_center / eff_R;
384 double sinsqt = std::pow(
lambda / (2.0 * dsp), 2);
392 std::vector<double> xdata(1, 1.0);
393 std::vector<double> ydata;
395 double l1 = p.
getL1();
398 unit->toTOF(xdata, ydata, l1, 0, {{UnitParams::l2,
l2}, {UnitParams::twoTheta, theta2}});
399 double one = xdata[0];
400 double spect1 =
spectrumCalc(one, iSpec, time, spectra, bank);
401 relSigSpect = std::sqrt((1.0 / spect) + (1.0 / spect1));
405 throw std::runtime_error(
"Wavelength for normalizing to spectrum is out of range.");
407 correc = scaleFactor * sinsqt * cmonx * sp_ratio / (wl4 * spect * transmission);
409 if (inst->hasParameter(
"detScale" + bankName)) {
410 correc *=
static_cast<double>(inst->getNumberParameter(
"detScale" + bankName)[0]);
414 instBkg = 0. * 12.28 / cmonx * scaleFactor;
421 std::pow(relSigSpect * correc * p.
getIntensity(), 2) + instBkg);
424 if (ckIntensity > 99999.985)
426 <<
" is too large for format. Decrease ScalePeaks.\n";
427 out << std::setw(8) << std::fixed << std::setprecision(2) << ckIntensity;
429 out << std::setw(8) << std::fixed << std::setprecision(2) << cksigI;
432 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getIntensity();
434 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getSigmaIntensity();
436 if (type.compare(0, 2,
"Ba") == 0)
437 out << std::setw(4) << bankSequence + 1;
439 out << std::setw(4) << runSequence + 1;
441 out << std::setw(8) << std::fixed << std::setprecision(5) <<
lambda;
442 out << std::setw(8) << std::fixed << std::setprecision(5) << tbar;
447 V3D reverse_incident_cos = ol.
cosFromDir(reverse_incident_dir);
453 for (
int k = 0; k < 3; ++k) {
454 out << std::setw(9) << std::fixed << std::setprecision(5) << reverse_incident_cos[k];
455 out << std::setw(9) << std::fixed << std::setprecision(5) << scattered_cos[k];
459 out << std::setw(6) << runNumber;
462 out << std::setw(7) << seqNum;
464 out << std::setw(7) << wi + 1;
467 out << std::setw(7) << std::fixed << std::setprecision(4) << transmission;
469 out << std::setw(4) << std::right << bank;
471 out << std::setw(9) << std::fixed << std::setprecision(5) << scattering;
473 out << std::setw(8) << std::fixed << std::setprecision(4) << dsp;
476 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getCol());
477 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getRow());
485 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
487 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
491 out << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL)
492 << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL) << std::fixed
493 << std::setprecision(decimalHKL) << 0.0;
496 out <<
" 0.00 0.00 0 0.00000 0.00000"
497 " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000"
498 " 0 0 0.0000 0 0.00000 0.0000 0.00 0.00";
500 out <<
" 0.00 0.00 0 0.00000 0.00000"
501 " 0 0 0.0000 0 0.00000 0.0000";
507 std::vector<int> badPeaks;
508 for (
auto it = banned.crbegin(); it != banned.crend(); ++it) {
509 badPeaks.emplace_back(
static_cast<int>(*it));
511 peaksW->removePeaks(std::move(badPeaks));
514 if (append && std::filesystem::exists(filename)) {
516 load_alg->setPropertyValue(
"Filename", filename);
517 load_alg->setProperty(
"OutputWorkspace",
"peaks");
518 load_alg->executeAsChildAlg();
521 ws2->setInstrument(inst);
524 plus_alg->setProperty(
"LHSWorkspace", peaksW);
525 plus_alg->setProperty(
"RHSWorkspace", ws2);
526 plus_alg->executeAsChildAlg();
528 peaksW = plus_alg->getProperty(
"OutputWorkspace");