ThePEG  1.8.0
DataPointSet.h
1 // -*- C++ -*-
2 //
3 // DataPointSet.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef LWH_DataPointSet_H
10 #define LWH_DataPointSet_H
11 //
12 // This is the declaration of the DataPointSet class representing
13 //
14 
15 
16 #include <vector>
17 #include <limits>
18 #include <cmath>
19 #include <algorithm>
20 #include "AIDataPointSet.h"
21 #include "ManagedObject.h"
22 #include "DataPoint.h"
23 
24 namespace LWH {
25 
26 using namespace AIDA;
27 
33 class DataPointSet: public IDataPointSet, public ManagedObject {
34 
35 public:
36 
41  DataPointSet(int D): dim(D) {}
42 
46  virtual ~DataPointSet() {}
47 
51  IAnnotation & annotation() {
52  throw std::runtime_error("LWH cannot handle annotations");
53  }
54 
58  const IAnnotation & annotation() const {
59  throw std::runtime_error("LWH cannot handle annotations");
60  }
61 
66  std::string title() const {
67  return theTitle;
68  }
69 
74  std::string name() const {
75  return theTitle;
76  }
77 
83  bool setTitle(const std::string & title) {
84  theTitle = title;
85  return true;
86  }
87 
93  int dimension() const {
94  return dim;
95  }
96 
101  void clear() {
102  dset.clear();
103  }
104 
110  int size() const {
111  return dset.size();
112  }
113 
119  IDataPoint * point(int index) {
120  return &(dset[index]);
121  }
122 
136  bool setCoordinate(int coord,
137  const std::vector<double> & val,
138  const std::vector<double> & err) {
139  return setCoordinate(coord, val, err, err);
140  }
141 
157  bool setCoordinate(int coord,
158  const std::vector<double> & val,
159  const std::vector<double> & errp,
160  const std::vector<double> & errm) {
161  if ( coord < 0 || coord >= dimension() ) return false;
162  if ( val.size() != dset.size() || errp.size() != dset.size() ||
163  errm.size() != dset.size() ) return false;
164  for ( int i = 0, N = val.size(); i < N; ++i ) {
165  dset[i].coordinate(coord)->setValue(val[i]);
166  dset[i].coordinate(coord)->setErrorPlus(errp[i]);
167  dset[i].coordinate(coord)->setErrorMinus(errm[i]);
168  }
169  return true;
170  }
171 
176  const IDataPoint * point(int index) const {
177  if ( index < 0 || unsigned(index) >= dset.size() ) return 0;
178  return &(dset[index]);
179  }
180 
185  IDataPoint * addPoint() {
186  dset.push_back(DataPoint(dimension()));
187  return &(dset.back());
188  }
189 
196  bool addPoint(const IDataPoint & point) {
197  if ( dimension() && dimension() != point.dimension() ) return false;
198  dset.push_back(DataPoint(point));
199  return true;
200  }
201 
207  bool removePoint(int index) {
208  if ( index < 0 || unsigned(index) >= dset.size() ) return false;
209  dset.erase(dset.begin() + index);
210  return true;
211  }
212 
220  double lowerExtent(int coord) const {
221  if ( dset.empty() ) return std::numeric_limits<double>::quiet_NaN();
222  if ( coord < 0 || coord >= dimension() )
223  return std::numeric_limits<double>::quiet_NaN();
224  double low = dset[0].coordinate(coord)->value();
225  for ( int i = 1, N = dset.size(); i < N; ++i )
226  low = std::min(low, dset[i].coordinate(coord)->value());
227  return low;
228  }
229 
237  double upperExtent(int coord) const {
238  if ( dset.empty() ) return std::numeric_limits<double>::quiet_NaN();
239  if ( coord < 0 || coord >= dimension() )
240  return std::numeric_limits<double>::quiet_NaN();
241  double upp = dset[0].coordinate(coord)->value();
242  for ( int i = 1, N = dset.size(); i < N; ++i )
243  upp = std::max(upp, dset[i].coordinate(coord)->value());
244  return upp;
245  }
246 
253  bool scale(double scale) {
254  for ( int i = 0, N = dset.size(); i < N; ++i )
255  for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
256  IMeasurement * m = dset[i].coordinate(j);
257  m->setValue(m->value()*scale);
258  m->setErrorPlus(m->errorPlus()*scale);
259  m->setErrorMinus(m->errorPlus()*scale);
260  }
261  return true;
262  }
263 
270  bool scaleValues(double scale) {
271  for ( int i = 0, N = dset.size(); i < N; ++i )
272  for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
273  IMeasurement * m = dset[i].coordinate(j);
274  m->setValue(m->value()*scale);
275  }
276  return true;
277  }
278 
285  bool scaleErrors(double scale) {
286  for ( int i = 0, N = dset.size(); i < N; ++i )
287  for ( int j = 0, M = dset[i].dimension(); j < M; ++j ) {
288  IMeasurement * m = dset[i].coordinate(j);
289  m->setErrorPlus(m->errorPlus()*scale);
290  m->setErrorMinus(m->errorPlus()*scale);
291  }
292  return true;
293  }
294 
299  void * cast(const std::string &) const {
300  return 0;
301  }
302 
306  bool writeXML(std::ostream & os, std::string path, std::string name) {
307  os << " <dataPointSet name=\"" << name
308  << "\"\n title=\"" << title()
309  << "\" path=\"" << path
310  << "\" dimension=\"" << dimension() << "\">\n";
311  for ( int d = 0; d < dimension(); ++d )
312  os << " <dimension dim=\"" << d << "\" title=\"unknown\" />\n";
313  for ( int i = 0, N = size(); i < N; ++i ) {
314  os << " <dataPoint>\n";
315  for ( int j = 0, M = dimension(); j < M; ++j )
316  os << " <measurement value=\""
317  << point(i)->coordinate(j)->value()
318  << "\" errorPlus=\""
319  << point(i)->coordinate(j)->errorPlus()
320  << "\" errorMinus=\""
321  << point(i)->coordinate(j)->errorMinus()
322  << "\"/>\n";
323  os << " </dataPoint>\n";
324  }
325  os << " </dataPointSet>" << std::endl;
326  return true;
327  }
328 
334  bool writeFLAT(std::ostream & os, std::string path, std::string name) {
335  os << "# " << path << "/" << name << " " << size()
336  << " \"" << title() << " \" dimension " << dimension() << std::endl;
337  for ( int i = 0, N = size(); i < N; ++i ) {
338  for ( int j = 0, M = dimension(); j < M; ++j )
339  os << point(i)->coordinate(j)->value() << " ";
340  for ( int j = 0, M = dimension(); j < M; ++j )
341  os << point(i)->coordinate(j)->errorPlus() << " ";
342  for ( int j = 0, M = dimension(); j < M; ++j )
343  os << point(i)->coordinate(j)->errorMinus() << " ";
344  os << std::endl;
345  }
346  os << std::endl;
347  return true;
348  }
349 
350 private:
351 
353  std::string theTitle;
354 
358  std::vector<DataPoint> dset;
359 
363  unsigned int dim;
364 
366  IAnnotation * anno;
367 
368 };
369 
370 }
371 
372 #endif /* LWH_DataPointSet_H */