49 return "Creates a randomly simulated workspace by sampling from the probability distribution of input data.";
56 auto mustBePositive = std::make_shared<Mantid::Kernel::BoundedValidator<int>>();
57 mustBePositive->setLower(0);
60 "Input Workspace containing data to be simulated");
62 "Integer seed that initialises the random-number generator, for reproducibility");
64 "Number of Monte Carlo events to simulate. Defaults to integral of input workspace if 0.");
66 "Name of output workspace.");
76 Mantid::HistogramData::HistogramY outputY(cdf.size(), 0.0);
77 std::mt19937 gen(seedInput);
78 std::uniform_real_distribution<> dis(0.0, 1.0);
80 int progressInterval = std::max(1, numIterations / 100);
82 for (
int i = 0; i < numIterations; ++i) {
83 double randomNum = dis(gen);
84 auto it = std::lower_bound(cdf.begin(), cdf.end(), randomNum);
85 size_t index = std::distance(cdf.begin(), it);
87 if (
index < outputY.size()) {
88 outputY[
index] += 1.0;
91 if (i % progressInterval == 0) {
92 progress.report(
"Generating random data...");
102 std::vector<double> cdf(yData.size());
103 std::partial_sum(yData.begin(), yData.end(), cdf.begin());
104 double totalCounts = cdf.back();
106 if (totalCounts > 0.0) {
108 std::transform(cdf.begin(), cdf.end(), cdf.begin(), [totalCounts](
double val) { return val / totalCounts; });
110 g_log.
warning(
"Total counts are zero; normalization skipped.");
120 double totalCounts = std::accumulate(yData.begin(), yData.end(), 0.0);
121 int iterations =
static_cast<int>(std::round(totalCounts));
123 if (iterations == 0) {
124 g_log.
warning(
"Total counts in the input workspace round to 0. No Monte Carlo events will be generated.");
135 int userMCEvents =
getProperty(
"MonteCarloEvents");
137 const auto &originalYData = inputWs->y(0);
139 int numIterations = (userMCEvents > 0) ? userMCEvents :
integrateYData(originalYData);
142 progress.report(
"Computing normalized CDF...");
146 outputWs->setSharedX(0, inputWs->sharedX(0));
150 outputWs->mutableY(0) = outputY;
153 Mantid::HistogramData::HistogramE outputE(outputY.size());
154 std::transform(outputY.begin(), outputY.end(), outputE.begin(), [](
double count) { return std::sqrt(count); });
155 outputWs->mutableE(0) = std::move(outputE);
157 g_log.
warning(
"Only the first spectrum is being plotted.");
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Helper class for reporting progress from algorithms.
A property class for workspaces.
CreateMonteCarloWorkspace : The algorithm generates a simulated workspace by sampling from the probab...
std::vector< double > computeNormalizedCDF(const Mantid::HistogramData::HistogramY &yData)
Compute a normalized CDF [0..1] from the given histogram data.
int version() const override
Algorithm's version for identification.
const string summary() const override
Algorithm's summary for use in the GUI and help.
void exec() override
Execute the algorithm.
Mantid::HistogramData::HistogramY fillHistogramWithRandomData(const std::vector< double > &cdf, int numIterations, int seedInput, API::Progress &progress)
void init() override
Initialize the algorithm's properties.
const string category() const override
Algorithm's category for identification.
int integrateYData(const Mantid::HistogramData::HistogramY &yData)
Determine how many iterations to use for MC sampling.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The Logger class is in charge of the publishing messages from the framework through various channels.
void warning(const std::string &msg)
Logs at warning level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Kernel::Logger g_log("DetermineSpinStateOrder")
Helper class which provides the Collimation Length for SANS instruments.
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.
@ Output
An output workspace.