00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef LWH_DataPointSet_H
00010 #define LWH_DataPointSet_H
00011
00012
00013
00014
00015
00016 #include <vector>
00017 #include <limits>
00018 #include <cmath>
00019 #include <algorithm>
00020 #include "AIDataPointSet.h"
00021 #include "ManagedObject.h"
00022 #include "DataPoint.h"
00023
00024 namespace LWH {
00025
00026 using namespace AIDA;
00027
00033 class DataPointSet: public IDataPointSet, public ManagedObject {
00034
00035 public:
00036
00041 DataPointSet(int D): dim(D) {}
00042
00046 virtual ~DataPointSet() {}
00047
00051 IAnnotation & annotation() {
00052 throw std::runtime_error("LWH cannot handle annotations");
00053 }
00054
00058 const IAnnotation & annotation() const {
00059 throw std::runtime_error("LWH cannot handle annotations");
00060 }
00061
00066 std::string title() const {
00067 return theTitle;
00068 }
00069
00074 std::string name() const {
00075 return theTitle;
00076 }
00077
00083 bool setTitle(const std::string & title) {
00084 theTitle = title;
00085 return true;
00086 }
00087
00093 int dimension() const {
00094 return dim;
00095 }
00096
00101 void clear() {
00102 dset.clear();
00103 }
00104
00110 int size() const {
00111 return dset.size();
00112 }
00113
00119 IDataPoint * point(int index) {
00120 return &(dset[index]);
00121 }
00122
00136 bool setCoordinate(int coord,
00137 const std::vector<double> & val,
00138 const std::vector<double> & err) {
00139 return setCoordinate(coord, val, err, err);
00140 }
00141
00157 bool setCoordinate(int coord,
00158 const std::vector<double> & val,
00159 const std::vector<double> & errp,
00160 const std::vector<double> & errm) {
00161 if ( coord < 0 || coord >= dimension() ) return false;
00162 if ( val.size() != dset.size() || errp.size() != dset.size() ||
00163 errm.size() != dset.size() ) return false;
00164 for ( int i = 0, N = val.size(); i < N; ++i ) {
00165 dset[i].coordinate(coord)->setValue(val[i]);
00166 dset[i].coordinate(coord)->setErrorPlus(errp[i]);
00167 dset[i].coordinate(coord)->setErrorMinus(errm[i]);
00168 }
00169 return true;
00170 }
00171
00176 const IDataPoint * point(int index) const {
00177 if ( index < 0 || unsigned(index) >= dset.size() ) return 0;
00178 return &(dset[index]);
00179 }
00180
00185 IDataPoint * addPoint() {
00186 dset.push_back(DataPoint(dimension()));
00187 return &(dset.back());
00188 }
00189
00196 bool addPoint(const IDataPoint & point) {
00197 if ( dimension() && dimension() != point.dimension() ) return false;
00198 dset.push_back(DataPoint(point));
00199 return true;
00200 }
00201
00207 bool removePoint(int index) {
00208 if ( index < 0 || unsigned(index) >= dset.size() ) return false;
00209 dset.erase(dset.begin() + index);
00210 return true;
00211 }
00212
00220 double lowerExtent(int coord) const {
00221 if ( dset.empty() ) return std::numeric_limits<double>::quiet_NaN();
00222 if ( coord < 0 || coord >= dimension() )
00223 return std::numeric_limits<double>::quiet_NaN();
00224 double low = dset[0].coordinate(coord)->value();
00225 for ( int i = 1, N = dset.size(); i < N; ++i )
00226 low = std::min(low, dset[i].coordinate(coord)->value());
00227 return low;
00228 }
00229
00237 double upperExtent(int coord) const {
00238 if ( dset.empty() ) return std::numeric_limits<double>::quiet_NaN();
00239 if ( coord < 0 || coord >= dimension() )
00240 return std::numeric_limits<double>::quiet_NaN();
00241 double upp = dset[0].coordinate(coord)->value();
00242 for ( int i = 1, N = dset.size(); i < N; ++i )
00243 upp = std::max(upp, dset[i].coordinate(coord)->value());
00244 return upp;
00245 }
00246
00253 bool scale(double scale) {
00254 for ( int i = 0, N = dset.size(); i < N; ++i )
00255 for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
00256 IMeasurement * m = dset[i].coordinate(j);
00257 m->setValue(m->value()*scale);
00258 m->setErrorPlus(m->errorPlus()*scale);
00259 m->setErrorMinus(m->errorPlus()*scale);
00260 }
00261 return true;
00262 }
00263
00270 bool scaleValues(double scale) {
00271 for ( int i = 0, N = dset.size(); i < N; ++i )
00272 for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
00273 IMeasurement * m = dset[i].coordinate(j);
00274 m->setValue(m->value()*scale);
00275 }
00276 return true;
00277 }
00278
00285 bool scaleErrors(double scale) {
00286 for ( int i = 0, N = dset.size(); i < N; ++i )
00287 for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
00288 IMeasurement * m = dset[i].coordinate(j);
00289 m->setErrorPlus(m->errorPlus()*scale);
00290 m->setErrorMinus(m->errorPlus()*scale);
00291 }
00292 return true;
00293 }
00294
00299 void * cast(const std::string &) const {
00300 return 0;
00301 }
00302
00306 bool writeXML(std::ostream & os, std::string path, std::string name) {
00307 os << " <dataPointSet name=\"" << name
00308 << "\"\n title=\"" << title()
00309 << "\" path=\"" << path
00310 << "\" dimension=\"" << dimension() << "\">\n";
00311 for ( int i = 0, N = size(); i < N; ++i ) {
00312 os << " <dataPoint>\n";
00313 for ( int j = 0, M = dimension(); j < M; ++j )
00314 os << " <measurement value=\""
00315 << point(i)->coordinate(j)->value()
00316 << "\" errorPlus=\""
00317 << point(i)->coordinate(j)->errorPlus()
00318 << "\" errorMinus=\""
00319 << point(i)->coordinate(j)->errorMinus()
00320 << "\"/>\n";
00321 os << " </dataPoint>\n";
00322 }
00323 os << " </dataPointSet>" << std::endl;
00324 return true;
00325 }
00326
00332 bool writeFLAT(std::ostream & os, std::string path, std::string name) {
00333 os << "# " << path << "/" << name << " " << size()
00334 << " \"" << title() << " \" dimension " << dimension() << std::endl;
00335 for ( int i = 0, N = size(); i < N; ++i ) {
00336 for ( int j = 0, M = dimension(); j < M; ++j )
00337 os << point(i)->coordinate(j)->value() << " ";
00338 for ( int j = 0, M = dimension(); j < M; ++j )
00339 os << point(i)->coordinate(j)->errorPlus() << " ";
00340 for ( int j = 0, M = dimension(); j < M; ++j )
00341 os << point(i)->coordinate(j)->errorMinus() << " ";
00342 os << std::endl;
00343 }
00344 os << std::endl;
00345 return true;
00346 }
00347
00348 private:
00349
00351 std::string theTitle;
00352
00356 std::vector<DataPoint> dset;
00357
00361 unsigned int dim;
00362
00364 IAnnotation * anno;
00365
00366 };
00367
00368 }
00369
00370 #endif