Mantid
Loading...
Searching...
No Matches
MCInteractionStatistics.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 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 +
9#include "MantidKernel/V3D.h"
10
11#include <algorithm>
12#include <iomanip>
13
14namespace Mantid {
15using Kernel::V3D;
16
17namespace Algorithms {
18
28 : m_detectorID(detectorID) {
29 const Geometry::SampleEnvironment *env = nullptr;
30 try {
31 env = &sample.getEnvironment();
32 assert(env);
33 if (env->nelements() == 0) {
34 throw std::invalid_argument("MCInteractionStatistics() - Sample enviroment has zero components.");
35 }
36 } catch (std::runtime_error &) {
37 // swallow this as no defined environment from getEnvironment
38 }
39
40 if (env) {
41 for (size_t i = 0; i < env->nelements(); i++) {
42 m_envScatterPoints.push_back({env->getComponent(i).id(), 0, 0});
43 }
44 }
45}
46
55void MCInteractionStatistics::UpdateScatterPointCounts(int componentIndex, bool pointUsed) {
56 ScatterPointStat *StatToUpdate;
57 if (componentIndex == -1) {
58 StatToUpdate = &m_sampleScatterPoints;
59 } else {
60 StatToUpdate = &m_envScatterPoints[componentIndex];
61 }
62 if (pointUsed) {
63 StatToUpdate->usedPointCount++;
64 } else {
65 StatToUpdate->generatedPointCount++;
66 }
67}
68
74void MCInteractionStatistics::UpdateScatterAngleStats(const V3D &toStart, const V3D &scatteredDirec) {
75 double scatterAngleDegrees = scatteredDirec.angle(-toStart) * 180. / M_PI;
76 double delta = scatterAngleDegrees - m_scatterAngleMean;
77 int totalScatterPoints = m_sampleScatterPoints.usedPointCount;
78 std::for_each(m_envScatterPoints.cbegin(), m_envScatterPoints.cend(),
79 [&totalScatterPoints](const auto &stat) { totalScatterPoints += stat.usedPointCount; });
80 m_scatterAngleMean += delta / totalScatterPoints;
81 m_scatterAngleM2 += delta * (scatterAngleDegrees - m_scatterAngleMean);
82 m_scatterAngleSD = sqrt(m_scatterAngleM2 / totalScatterPoints);
83}
84
90
91 std::stringstream scatterPointSummary;
92 scatterPointSummary << std::fixed;
93 scatterPointSummary << std::setprecision(2);
94
95 scatterPointSummary << "Scatter point statistics" << std::endl;
96 scatterPointSummary << "========================" << std::endl;
97
98 scatterPointSummary << "Detector ID: " << m_detectorID << std::endl;
99
100 int totalScatterPointsGenerated = m_sampleScatterPoints.generatedPointCount;
101 int totalScatterPointsUsed = m_sampleScatterPoints.usedPointCount;
102 for (const auto &stat : m_envScatterPoints) {
103 totalScatterPointsGenerated += stat.generatedPointCount;
104 totalScatterPointsUsed += stat.usedPointCount;
105 }
106
107 scatterPointSummary << "Total scatter points generated: " << totalScatterPointsGenerated << std::endl;
108 scatterPointSummary << "Total scatter points used: " << totalScatterPointsUsed << std::endl;
109
110 if (m_envScatterPoints.size() > 0) {
111 double percentage = static_cast<double>(m_sampleScatterPoints.usedPointCount) / totalScatterPointsUsed * 100;
112 scatterPointSummary << "Sample: " << m_sampleScatterPoints.usedPointCount << " (" << percentage << "%)"
113 << std::endl;
114
115 for (std::vector<int>::size_type i = 0; i < m_envScatterPoints.size(); i++) {
116 percentage = static_cast<double>(m_envScatterPoints[i].usedPointCount) / totalScatterPointsUsed * 100;
117 scatterPointSummary << "Environment part " << i << " (" << m_envScatterPoints[i].name
118 << "): " << m_envScatterPoints[i].usedPointCount << " (" << percentage << "%)" << std::endl;
119 }
120 }
121 scatterPointSummary << "Scattering angle mean (degrees)=" << m_scatterAngleMean << std::endl;
122 scatterPointSummary << "Scattering angle sd (degrees)=" << m_scatterAngleSD << std::endl;
123
124 return scatterPointSummary.str();
125}
126
127} // namespace Algorithms
128} // namespace Mantid
This class stores information about the sample used in particular run.
Definition: Sample.h:33
const Geometry::SampleEnvironment & getEnvironment() const
Get a reference to the sample's environment.
Definition: Sample.cpp:136
void UpdateScatterPointCounts(int componentIndex, bool pointUsed)
Update the scatter point counts.
std::string generateScatterPointStats()
Log a debug string summarising which parts of the environment the simulated scatter points occurred i...
void UpdateScatterAngleStats(const Kernel::V3D &toStart, const Kernel::V3D &scatteredDirec)
Update the scattering angle statistics.
MCInteractionStatistics(detid_t detectorID, const API::Sample &sample)
Construct the statistics object.
std::vector< ScatterPointStat > m_envScatterPoints
Defines a single instance of a SampleEnvironment.
const IObject & getComponent(const size_t index) const
Returns the requested IObject.
Class for 3D vectors.
Definition: V3D.h:34
double angle(const V3D &) const
Angle between this and another vector.
Definition: V3D.cpp:165
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21