Demonstrate the use of Statistics; the code's in statistics.cc.
Start by including needed headers and declaring namespace aliases
#include <iostream>
#include <cmath>
using namespace std;
namespace math = lsst::afw::math;
Create an Image
namespace geom = lsst::afw::geom;
int main() {
img = 100000.0;
Create a
Statistics object from that Image, requesting the number of points, the mean, and the standard deviation.
And print the desired quantities. Note that we didn't request that the error in the mean be calculated, so a
NaN is returned.
cout <<
"Mean: " << stats.getValue(
math::MEAN) << endl;
cout <<
"Error in mean: " << stats.getError(
math::MEAN) <<
" (expect NaN)" << endl;
cout <<
"Standard Deviation: " << stats.getValue(
math::STDEV) << endl << endl;
}
Here's another way to do the same thing. We use makeStatistics (cf. std::make_pair) to avoid having to specify what sort of Statistics we're creating (and in C++0X you'll be able to say
which means that we never have to provide information that the compiler has up its sleeve — very convenient for generic template programming)
Print the answers again, but this time return that value and its error as a
std::pair std::pair<double, double> mean = stats.getResult(
math::MEAN);
cout << "Mean: " << mean.first << " error in mean: " << mean.second << endl << endl;
}
Don't ask for things that you didn't request.
{
try {
} catch (lsst::pex::exceptions::InvalidParameterError &e) {
cout << "You didn't ask for the mean, so we caught an exception: " << e.what() << endl;
}
}
be tidy and return success (unnecessary; unlike C, C++ will return this 0 for you automatically)