MINT2
HyperPointSet.cpp
Go to the documentation of this file.
1 #include "Mint/HyperPointSet.h"
2 #include <math.h>
3 
6 HyperPointSet::HyperPointSet(int dimension) :
7  _dimension(dimension)
8 {
9  WELCOME_LOG << "Hello from the HyperPointSet() Constructor";
10 }
11 
15  _dimension(-1)
16 {
17 
18  load(path);
19 
20 }
21 
24 HyperPointSet::HyperPointSet(int npoints, const HyperPoint& point) :
25  _dimension(point.getDimension())
26 {
27 
28  for (int i = 0; i < npoints; i++) push_back(point);
29 
30 }
31 
32 
36  _dimension(point.getDimension())
37 {
38  push_back(point);
39 }
40 
43 HyperPointSet::HyperPointSet(const HyperPoint& point1, const HyperPoint& point2) :
44  _dimension(point1.getDimension())
45 {
46  push_back(point1);
47  push_back(point2);
48 }
49 
52 HyperPointSet::HyperPointSet(const HyperPoint& point1, const HyperPoint& point2, const HyperPoint& point3) :
53  _dimension(point1.getDimension())
54 {
55  push_back(point1);
56  push_back(point2);
57  push_back(point3);
58 }
59 
62 bool HyperPointSet::compatible(const HyperPoint& other, bool printError) const{
63 
64  if (_dimension == other.getDimension()) return true;
65 
66  if (printError) ERROR_LOG << "This HyperPoint is NOT compatible with this HyperPointSet";
67  return false;
68 }
69 
72 const HyperPoint& HyperPointSet::at(int i) const {
73  return _points.at(i);
74 }
75 
79  return _points.at(i);
80 }
81 
84 unsigned int HyperPointSet::size() const {
85  return _points.size();
86 }
87 
88 
92  if (compatible(point) == false) return;
93  _points.push_back(point);
94 }
95 
96 
99 double HyperPointSet::getSumW() const{
100 
101  double sumW = 0.0;
102 
103  for(unsigned i = 0; i < size(); i++){
104  sumW += at(i).getWeight();
105  }
106  return sumW;
107 }
108 
111 double HyperPointSet::getSumW2() const{
112 
113  double sumW2 = 0.0;
114  double w = 0.0;
115  for(unsigned i = 0; i < size(); i++){
116  w = at(i).getWeight();
117  sumW2 += w*w;
118  }
119  return sumW2;
120 
121 }
122 
128 
129  int nPoints = size();
130  if ( nPoints > getDimension() ) nPoints = getDimension();
131 
132  if (linearlyIndependant() == 0) {
133  ERROR_LOG << "Trying to perform the Gram Schmidt Process on a set of L.D. vectors";
134  }
135 
136  HyperPointSet orthogonalPoints(at(0));
137 
138  for (int i = 1; i < nPoints; i++){
139  HyperPoint nextPoint = at(i);
140  for (int j = 0; j < i; j++){
141  nextPoint = nextPoint - orthogonalPoints.at(j).project(at(i));
142  }
143  orthogonalPoints.push_back(nextPoint);
144  }
145 
146  orthogonalPoints.normalise();
147  return orthogonalPoints;
148 
149 }
150 
154  //need to implement this...
155  return 1;
156 }
157 
161 
162  for(unsigned i = 0; i < size(); i++){
163  at(i) = at(i)/at(i).norm();
164  }
165 
166 }
167 
172 
173  std::sort(_points.begin(), _points.end());
174 
175 }
176 
180 
181  //INFO_LOG << "Removing all duplicates from the HyperPointSet - did you remember to call sort() first?!?";
182 
183  HyperPointSet hyperPointSet(getDimension());
184 
185  HyperPoint currentHyperPoint = _points.at(0);
186  hyperPointSet.push_back(currentHyperPoint);
187 
188  for(unsigned int i = 1; i < size(); i++){
189  if ( _points.at(i) != currentHyperPoint ) {
190  currentHyperPoint = _points.at(i);
191  hyperPointSet.push_back(currentHyperPoint);
192  }
193  }
194 
195  _points.clear();
196  *this = hyperPointSet;
197 
198 }
199 
203 
204  if (_dimension != other._dimension){
205  ERROR_LOG << "Removing all duplicates from the HyperPointSet - did you remember to call sort() first?!?";
206 
207  }
208 
209  for (unsigned int i = 0; i < other.size(); i++){
210  this->push_back(other.at(i));
211  }
212 
213 }
214 
217 double HyperPointSet::getCorrelation(int i, int j) const{
218 
219  WidthFinder statsi;
220  WidthFinder statsj;
221 
222  for (unsigned evt = 0; evt < size(); evt++){
223  statsi.add(at(evt).at(i));
224  statsj.add(at(evt).at(j));
225  }
226 
227  double meani = statsi.mean();
228  double meanj = statsj.mean();
229  double widthi = statsi.width();
230  double widthj = statsj.width();
231 
232  double correlation = 0.0;
233 
234  for (unsigned evt = 0; evt < size(); evt++){
235  correlation += (at(evt).at(i) - meani)*(at(evt).at(j) - meanj);
236  }
237 
238  correlation = correlation / (widthi * widthj * size());
239 
240  return correlation;
241 }
242 
245 double HyperPointSet::getCovarience(int i, int j) const{
246 
247  MeanFinder statsi;
248  MeanFinder statsj;
249 
250  for (unsigned evt = 0; evt < size(); evt++){
251  statsi.add(at(evt).at(i));
252  statsj.add(at(evt).at(j));
253  }
254 
255  double meani = statsi.mean();
256  double meanj = statsj.mean();
257 
258  double correlation = 0.0;
259 
260  for (unsigned evt = 0; evt < size(); evt++){
261  correlation += (at(evt).at(i) - meani)*(at(evt).at(j) - meanj);
262  }
263 
264  correlation = correlation / size();
265 
266  return correlation;
267 }
268 
272 
273  TMatrixD matrix(getDimension(), getDimension());
274 
275  for (int i = 0; i < getDimension(); i++){
276  for (int j = 0; j < getDimension(); j++){
277  matrix(i,j) = getCovarience(i,j);
278  }
279  }
280 
281  return matrix;
282 }
283 
287 
288  TMatrixD matrix(getDimension(), getDimension());
289 
290  for (int i = 0; i < getDimension(); i++){
291  for (int j = 0; j < getDimension(); j++){
292  matrix(i,j) = getCorrelation(i,j);
293  }
294  }
295 
296  return matrix;
297 }
298 
301 void HyperPointSet::print(std::ostream& os) const{
302 
303  os << "---------- HyperPointSet ------------" << std::endl;
304  for (unsigned i = 0; i < size(); i++){
305  os << "HyperPoint " << i << ": ";
306  at(i).print(os, 0);
307  os << std::endl;
308  }
309 
310 }
311 
314 void HyperPointSet::save(TString path){
315 
316  TFile* file = new TFile(path, "RECREATE");
317 
318  if (file == 0) {
319  ERROR_LOG << "Cannot open file at " << path;
320  return;
321  }
322 
323  save();
324 
325  file->Write();
326  file->Close();
327 
328 }
329 
333 
334  TTree* tree = new TTree("HyperPointSet", "HyperPointSet");
335 
336  if (tree == 0) {
337  ERROR_LOG << "Cannot create tree";
338  return;
339  }
340 
341  std::vector<double> values;
342  std::vector<double> weights;
343 
344  tree->Branch("values" , &values );
345  tree->Branch("weights", &weights);
346 
347  for(unsigned i = 0; i < size(); i++ ){
348  weights = at(i).getWeightsVector();
349  values = at(i).getVector();
350  tree->Fill();
351  }
352 
353  tree->ResetBranchAddresses();
354 
355  tree->Write();
356 
357 }
358 
361 //HyperPointSet, so multiple files can be loaded
362 void HyperPointSet::load(TString path){
363 
364  TFile* file = new TFile(path, "READ");
365  if (file == 0) {
366  ERROR_LOG << "Cannot open file at " << path;
367  return;
368  }
369 
370  TTree* tree = (TTree*)file->Get("HyperPointSet");
371  if (tree == 0) {
372  ERROR_LOG << "Cannot open tree";
373  return;
374  }
375 
376  std::vector<double>* values = 0;
377  std::vector<double>* weights = 0;
378 
379  tree->SetBranchAddress("values" , &values );
380  tree->SetBranchAddress("weights", &weights);
381 
382  int nEntries = tree->GetEntries();
383 
384  for(int i = 0; i < nEntries; i++ ){
385  tree->GetEntry(i);
386 
387  if (_dimension == -1) _dimension = values->size();
388  if ( int(values->size()) != _dimension) ERROR_LOG << "You are loading HyperPoints that do not have the correct dimensionality for this HyperPointSet";
389 
390  HyperPoint point(*values);
391  for(unsigned w = 0; w < weights->size(); w++){
392  point.addWeight(weights->at(w));
393  }
394  this->push_back(point);
395  }
396 
397  tree->ResetBranchAddresses();
398  file->Close();
399 
400 }
401 
403 //HyperCuboid HyperPointSet::getLimits() const{
404 //
405 // HyperCuboid limits(getDimension());
406 //
407 // for (int i = 0; i < getDimension(); i++){
408 // MinMaxFinder minMax;
409 // for (unsigned j = 0; j < size(); j++){
410 // minMax.add( this->at(j).at(i) );
411 // }
412 // limits.getLowCorner() .at(i) = minMax.getMin();
413 // limits.getHighCorner().at(i) = minMax.getMax();
414 // }
415 //
416 // return limits;
417 //}
418 
421  HyperPoint val(getDimension());
422 
423  for (int i = 0; i < getDimension(); i++){
424  MinMaxFinder minMax;
425  for (unsigned j = 0; j < size(); j++){
426  minMax.add( this->at(j).at(i) );
427  }
428  val.at(i) = minMax.getMin();
429  }
430  return val;
431 }
432 
435  HyperPoint val(getDimension());
436 
437  for (int i = 0; i < getDimension(); i++){
438  MinMaxFinder minMax;
439  for (unsigned j = 0; j < size(); j++){
440  minMax.add( this->at(j).at(i) );
441  }
442  val.at(i) = minMax.getMax();
443  }
444  return val;
445 }
446 
447 
451  HyperPoint temp(getDimension());
452  for (unsigned i = 0; i < size(); i++){
453  temp = temp + at(i);
454  }
455  return temp/double(size());
456 }
457 
461 
462  HyperPoint geoMean(getDimension());
463  for (unsigned i = 0; i < size(); i++){
464  HyperPoint temp(getDimension());
465  for (int j= 0; j < getDimension(); j++){
466  temp.at(j) = log( this->at(i).at(j) );
467  }
468  geoMean = geoMean + temp;
469  }
470 
471  geoMean = geoMean/double(size());
472 
473  for (int j= 0; j < getDimension(); j++){
474  geoMean.at(j) = exp( geoMean.at(j) );
475  }
476 
477  return geoMean;
478 
479 }
480 
484 
485  HyperPoint geoMean(getDimension());
486  for (unsigned i = 0; i < size(); i++){
487  HyperPoint temp(getDimension());
488  for (int j= 0; j < getDimension(); j++){
489  temp.at(j) = 1.0 / this->at(i).at(j);
490  }
491  geoMean = geoMean + temp;
492  }
493 
494  geoMean = geoMean/double(size());
495 
496  for (int j= 0; j < getDimension(); j++){
497  geoMean.at(j) = 1.0 / geoMean.at(j);
498  }
499 
500  return geoMean;
501 
502 }
503 
507  GOODBYE_LOG << "Goodbye from the HyperPointSet() Destructor";
508 }
bool linearlyIndependant() const
double getCorrelation(int i, int j) const
void load(TString path)
const std::vector< double > & getVector()
Definition: HyperPoint.h:48
HyperPoint getMin() const
Get a HyperCuboid that surrounds the points.
virtual void print(std::ostream &os=std::cout, int endline=1) const
Definition: HyperPoint.cpp:453
HyperPoint geometricMean() const
double getSumW2() const
void addWeight(const double &weight)
Definition: Weights.cpp:71
#define ERROR_LOG
HyperPoint harmonicMean() const
void add(const double &x, const double &weight=1.0)
const double & getMin() const
TMatrixD getCorrelationMatrix() const
HyperPoint mean() const
const std::vector< double > & getWeightsVector()
Definition: Weights.h:36
virtual ~HyperPointSet()
const HyperPoint & at(int i) const
TMatrixD getCovarienceMatrix() const
HyperPointSet(int dimension)
std::vector< HyperPoint > _points
Definition: HyperPointSet.h:34
#define GOODBYE_LOG
HyperPointSet gramSchmidtProcess() const
double width() const
const double & at(int i) const
Definition: HyperPoint.cpp:433
const int & getDimension() const
Definition: HyperPointSet.h:40
double getCovarience(int i, int j) const
unsigned int size() const
#define WELCOME_LOG
HyperPoint project(const HyperPoint &other) const
Definition: HyperPoint.cpp:164
void print(std::ostream &os=std::cout) const
double getWeight(int i=0) const
Definition: Weights.cpp:40
bool compatible(const HyperPoint &other, bool printError=true) const
void addHyperPointSet(const HyperPointSet &other)
const double & getMax() const
HyperPoint getMax() const
Get a HyperPoint that gives the maximum in each dim.
int getDimension() const
Definition: HyperPoint.h:99
double mean() const
void removeDuplicates()
void push_back(const HyperPoint &point)
double norm() const
Definition: HyperPoint.cpp:131
double getSumW() const