X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/0c708ebeb7aec487326d89207d96b9e61bd541ad..67fd3c920d4a69ab05834f90603e2115e67e36c0:/obs.hpp diff --git a/obs.hpp b/obs.hpp index ae544e4..4d20bd6 100644 --- a/obs.hpp +++ b/obs.hpp @@ -1,7 +1,7 @@ #ifndef OBS_HPP #define OBS_HPP -#include +#include #include #include #include @@ -10,61 +10,92 @@ using namespace std; template class obs -{ - struct result{ - double val; - double err; - }; - +{ public: - void addMeas(obstype val[], int valsize); void addMeas(const obstype& val); + void addMeas(obstype val[], int valsize); + + void computeEasy(const string& compid="one", const int& ival=0) { mean(compid, &measurements, ival); } + + void computeJack(const string& compid, double (*func)(vector< vector > *vals, void *para)=NULL, void *para=NULL); + void computeJack(double (*func)(vector< vector > *vals, void *para)=NULL, void *para=NULL) { computeJack("one",func,para); } - void computeMean(const string& compid) { mean(compid, &measurements); } - void computeJack(const string& compid, double (*func)(list *vals, void *para), void *para=NULL); + double getMean(const string& compid="one") { return computations[compid].val; } + double getErr(const string& compid="one") { return computations[compid].err; } - double getMean(const string& compid) { return computations[compid].val; } - double getErr(const string& compid) { return computations[compid].err; } + void reset(); private: - list measurements; + struct result{ + double val; + double err; + }; + + vector< vector > measurements; map computations; - void mean(const string& compid, list *meas); + void mean(const string& compid, vector< vector > *meas, const int& ival); + void mean(const string& compid, vector< vector > *meas, const int& ival); }; -template -void obs::addMeas(obstype val[], int valsize) + +template +void obs::reset() { - for(int i=0; i void obs::addMeas(const obstype& val) { - measurements.push_back(val); + measurements.push_back( vector(1,val) ); } template -void obs::mean(const string& compid, list *meas) +void obs::addMeas(obstype val[], int valsize) +{ + vector tmpvec; + for(int i=0; i +void obs::mean(const string& compid, vector< vector > *meas, const int& ival) { computations[compid].val = 0; computations[compid].err = 0; int nmeas = meas->size(); - for(list::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].val += *measIt; + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].val += (*measIt)[ival]; computations[compid].val /= nmeas; - for(list::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].err += pow( *measIt - computations[compid].val, 2 ); - computations[compid].err /= nmeas*(nmeas-1); - computations[compid].err = sqrt( computations[compid].err ); + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].err += pow( (*measIt)[ival] - computations[compid].val, 2 ); + computations[compid].err = sqrt( computations[compid].err ) / nmeas; +} + +template +void obs::mean(const string& compid, vector< vector > *meas, const int& ival) +{ + computations[compid].val = 0; + computations[compid].err = 0; + + int nmeas = meas->size(); + + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].val += (*measIt)[ival]; + computations[compid].val /= nmeas; + + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].err += pow( (*measIt)[ival] - computations[compid].val, 2 ); + computations[compid].err = sqrt( computations[compid].err ) / nmeas; } template -void obs::computeJack(const string& compid, double (*func)(list *vals, void *para), void *para) +void obs::computeJack(const string& compid, double (*func)(vector< vector > *vals, void *para), void *para) { int nmeas=measurements.size(); double manymeans[nmeas]; @@ -73,26 +104,23 @@ void obs::computeJack(const string& compid, double (*func)(list::iterator removedIt = measurements.begin(); removedIt != measurements.end(); imeas++) + for(typename vector< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); ++removedIt, imeas++) { - obstype removed = *removedIt; + vector removed = *removedIt; - typename list::iterator nextAfterIt = removedIt; - ++nextAfterIt; + *removedIt = measurements.back(); + measurements.pop_back(); - measurements.erase(removedIt); - manymeans[imeas] = func(&measurements, para); computations[compid].val += manymeans[imeas]; - - measurements.insert(nextAfterIt, removed); - - removedIt = nextAfterIt; + + measurements.push_back( *removedIt ); + *removedIt = removed; } computations[compid].val /= nmeas; for(int imean=0; imean