-
Notifications
You must be signed in to change notification settings - Fork 122
/
CalculateChiSquared.cpp
73 lines (55 loc) · 2.32 KB
/
CalculateChiSquared.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "MantidCurveFitting/CalculateChiSquared.h"
namespace Mantid {
namespace CurveFitting {
using namespace Mantid::Kernel;
using namespace Mantid::API;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CalculateChiSquared)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
CalculateChiSquared::CalculateChiSquared() {}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
CalculateChiSquared::~CalculateChiSquared() {}
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string CalculateChiSquared::name() const {
return "CalculateChiSquared";
}
/// Algorithm's version for identification. @see Algorithm::version
int CalculateChiSquared::version() const { return 1; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string CalculateChiSquared::summary() const {
return "Calculate chi squared for a function and a data set in a workspace.";
}
//----------------------------------------------------------------------------------------------
/// Initialize the algorithm's properties.
void CalculateChiSquared::initConcrete() {
declareProperty("ChiSquared", 0.0, "Output value of chi squared.");
}
//----------------------------------------------------------------------------------------------
/// Execute the algorithm.
void CalculateChiSquared::execConcrete() {
// Function may need some preparation.
m_function->setUpForFit();
API::FunctionDomain_sptr domain;
API::FunctionValues_sptr values;
//m_domainCreator->ignoreInvalidData(getProperty("IgnoreInvalidData"));
m_domainCreator->createDomain(domain, values);
// Do something with the function which may depend on workspace.
m_domainCreator->initFunction(m_function);
// Calculate function values.
m_function->function(*domain, *values);
// Calculate the chi squared.
double chiSquared = 0.0;
for(size_t i = 0; i < values->size(); ++i) {
double tmp = values->getFitData(i) - values->getCalculated(i);
chiSquared += tmp * tmp;
}
// Store the result.
setProperty("ChiSquared", chiSquared);
}
} // namespace CurveFitting
} // namespace Mantid