100 peaksW =
m_ws->clone();
101 auto inst = peaksW->getInstrument();
102 std::vector<Peak> &peaks = peaksW->getPeaks();
115 if (peaksW->sample().hasOrientedLattice()) {
116 ol = peaksW->sample().getOrientedLattice();
121 std::ifstream in(fileUB.c_str());
123 throw std::runtime_error(
"A file containing the UB matrix must be input into UBFilename.");
126 UB_alg->setProperty(
"PeaksWorkspace", peaksW);
127 UB_alg->setProperty(
"Filename", fileUB);
128 UB_alg->executeAsChildAlg();
133 int bankSequence = 0;
138 std::string convention = ConfigService::Instance().getString(
"Q.convention");
139 if (convention ==
"Crystallography") {
144 out.open(filename.c_str(), std::ios::out);
148 std::string bankPart =
"?";
151 using bankMap_t = std::map<int, std::vector<size_t>>;
152 using runMap_t = std::map<int, bankMap_t>;
153 std::set<int> uniqueBanks;
154 std::set<int> uniqueRuns;
156 for (
size_t i = 0; i < peaks.size(); ++i) {
157 Peak const &p = peaks[i];
161 if (bankName.size() <= 4) {
162 g_log.
information() <<
"Could not interpret bank number of peak " << i <<
"(" << bankName <<
")\n";
167 bankPart = bankName.substr(0, 4);
169 if (bankPart ==
"bank")
170 bankName = bankName.substr(4, bankName.size() - 4);
171 else if (bankPart ==
"WISH")
172 bankName = bankName.substr(9, bankName.size() - 9);
176 if (type.compare(0, 2,
"Ru") == 0)
177 runMap[run][bank].emplace_back(i);
179 runMap[bank][run].emplace_back(i);
181 uniqueBanks.insert(bank);
182 uniqueRuns.insert(run);
185 bool correctPeaks =
getProperty(
"ApplyAnvredCorrections");
186 std::vector<std::vector<double>> spectra;
187 std::vector<std::vector<double>> time;
194 API::Run &run = peaksW->mutableRun();
204 const Material &sampleMaterial = peaksW->sample().getMaterial();
205 const IObject *sampleShape{
nullptr};
214 sphere->setMaterial(
Material(
"SetInSaveHKL", neutron, 1.0));
216 sphere->setMaterial(sampleMaterial);
221 peaksW->mutableSample().setShape(sphere);
222 }
else if (peaksW->sample().getShape().hasValidShape()) {
223 sampleShape = &(peaksW->sample().getShape());
226 if (std::all_of(peaks.cbegin(), peaks.cend(), [](
auto &p) { return p.getAbsorptionWeightedPathLength() == 0; })) {
228 alg->setProperty(
"InputWorkspace", peaksW);
229 alg->setProperty(
"UseSinglePath",
true);
230 alg->executeAsChildAlg();
235 std::ifstream infile;
237 infile.open(spectraFile.c_str());
238 if (infile.is_open()) {
239 std::string ignoreLine;
241 for (
int wi = 0; wi < 8; wi++)
242 getline(infile, ignoreLine);
243 while (!infile.eof()) {
245 spectra.resize(a + 1);
247 getline(infile, line);
248 std::stringstream ss(line);
249 if (line.find(
"Bank") == std::string::npos) {
250 double time0, spectra0;
251 ss >> time0 >> spectra0;
252 time[a].emplace_back(time0);
253 spectra[a].emplace_back(spectra0);
258 ss >> temp >> a0 >> temp >> a;
265 std::set<size_t> banned;
268 if (peaksW->sample().hasOrientedLattice()) {
269 maxOrder = peaksW->sample().getOrientedLattice().getMaxOrder();
272 runMap_t::iterator runMap_it;
273 for (runMap_it = runMap.begin(); runMap_it != runMap.end(); ++runMap_it) {
276 bankMap_t &bankMap = runMap_it->second;
278 bankMap_t::iterator bankMap_it;
279 for (bankMap_it = bankMap.begin(); bankMap_it != bankMap.end(); ++bankMap_it) {
282 std::vector<size_t> &ids = bankMap_it->second;
285 for (
auto wi : ids) {
308 (p.
getCol() < widthBorder || p.
getRow() < widthBorder || p.
getCol() > (nCols - widthBorder) ||
309 p.
getRow() > (nRows - widthBorder))) {
314 bankName.erase(remove_if(bankName.begin(), bankName.end(), std::not_fn(::isdigit)), bankName.end());
323 if (dsp < dMin || lambda < wlMin || lambda > wlMax) {
328 double transmission{0}, tbar{0};
332 }
else if (sampleShape) {
334 transmission = exp(-tbar * 0.01 * sampleShape->material().attenuationCoefficient(
lambda));
352 out << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getH()
353 << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getK()
354 << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << qSign * p.
getL();
356 double correc = scaleFactor;
358 double relSigSpect = 0.0;
359 bankSequence =
static_cast<int>(std::distance(uniqueBanks.begin(), uniqueBanks.find(bank)));
360 runSequence =
static_cast<int>(std::distance(uniqueRuns.begin(), uniqueRuns.find(runNumber)));
363 double mu = (9.614 *
lambda) + 0.266;
365 double eff_center = 1.0 - std::exp(-
mu * depth);
368 std::shared_ptr<const IComponent> det0 = inst->getComponentByName(p.
getBankName());
369 if (inst->getName() ==
"CORELLI")
371 std::vector<Geometry::IComponent_const_sptr> children;
372 std::shared_ptr<const Geometry::ICompAssembly> asmb =
373 std::dynamic_pointer_cast<const Geometry::ICompAssembly>(inst->getComponentByName(p.
getBankName()));
374 asmb->getChildren(children,
false);
378 double cosA = det0->getDistance(*sample) / p.
getL2();
379 double pathlength = depth / cosA;
380 double eff_R = 1.0 - exp(-
mu * pathlength);
381 double sp_ratio = eff_center / eff_R;
382 double sinsqt = std::pow(
lambda / (2.0 * dsp), 2);
390 std::vector<double> xdata(1, 1.0);
391 std::vector<double> ydata;
393 double l1 = p.
getL1();
396 unit->toTOF(xdata, ydata, l1, 0, {{UnitParams::l2,
l2}, {UnitParams::twoTheta, theta2}});
397 double one = xdata[0];
398 double spect1 =
spectrumCalc(one, iSpec, time, spectra, bank);
399 relSigSpect = std::sqrt((1.0 / spect) + (1.0 / spect1));
403 throw std::runtime_error(
"Wavelength for normalizing to spectrum is out of range.");
405 correc = scaleFactor * sinsqt * cmonx * sp_ratio / (wl4 * spect * transmission);
407 if (inst->hasParameter(
"detScale" + bankName)) {
408 correc *=
static_cast<double>(inst->getNumberParameter(
"detScale" + bankName)[0]);
412 instBkg = 0. * 12.28 / cmonx * scaleFactor;
419 std::pow(relSigSpect * correc * p.
getIntensity(), 2) + instBkg);
422 if (ckIntensity > 99999.985)
424 <<
" is too large for format. Decrease ScalePeaks.\n";
425 out << std::setw(8) << std::fixed << std::setprecision(2) << ckIntensity;
427 out << std::setw(8) << std::fixed << std::setprecision(2) << cksigI;
430 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getIntensity();
432 out << std::setw(8) << std::fixed << std::setprecision(2) << p.
getSigmaIntensity();
434 if (type.compare(0, 2,
"Ba") == 0)
435 out << std::setw(4) << bankSequence + 1;
437 out << std::setw(4) << runSequence + 1;
439 out << std::setw(8) << std::fixed << std::setprecision(5) <<
lambda;
440 out << std::setw(8) << std::fixed << std::setprecision(5) << tbar;
445 V3D reverse_incident_cos = ol.
cosFromDir(reverse_incident_dir);
451 for (
int k = 0; k < 3; ++k) {
452 out << std::setw(9) << std::fixed << std::setprecision(5) << reverse_incident_cos[k];
453 out << std::setw(9) << std::fixed << std::setprecision(5) << scattered_cos[k];
457 out << std::setw(6) << runNumber;
460 out << std::setw(7) << seqNum;
462 out << std::setw(7) << wi + 1;
465 out << std::setw(7) << std::fixed << std::setprecision(4) << transmission;
467 out << std::setw(4) << std::right << bank;
469 out << std::setw(9) << std::fixed << std::setprecision(5) << scattering;
471 out << std::setw(8) << std::fixed << std::setprecision(4) << dsp;
474 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getCol());
475 out << std::setw(7) << std::fixed << std::setprecision(2) << static_cast<double>(p.
getRow());
483 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
485 out << std::setw(4) << 0 << std::setw(4) << 0 << std::setw(4) << 0;
489 out << std::setw(5 + decimalHKL) << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL)
490 << std::fixed << std::setprecision(decimalHKL) << 0.0 << std::setw(5 + decimalHKL) << std::fixed
491 << std::setprecision(decimalHKL) << 0.0;
494 out <<
" 0.00 0.00 0 0.00000 0.00000"
495 " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000"
496 " 0 0 0.0000 0 0.00000 0.0000 0.00 0.00";
498 out <<
" 0.00 0.00 0 0.00000 0.00000"
499 " 0 0 0.0000 0 0.00000 0.0000";
505 std::vector<int> badPeaks;
506 for (
auto it = banned.crbegin(); it != banned.crend(); ++it) {
507 badPeaks.emplace_back(
static_cast<int>(*it));
509 peaksW->removePeaks(std::move(badPeaks));
512 if (append && std::filesystem::exists(filename)) {
514 load_alg->setPropertyValue(
"Filename", filename);
515 load_alg->setProperty(
"OutputWorkspace",
"peaks");
516 load_alg->executeAsChildAlg();
519 ws2->setInstrument(inst);
522 plus_alg->setProperty(
"LHSWorkspace", peaksW);
523 plus_alg->setProperty(
"RHSWorkspace", ws2);
524 plus_alg->executeAsChildAlg();
526 peaksW = plus_alg->getProperty(
"OutputWorkspace");