CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Attributes
PhysicsTools::LeastSquares Class Reference

#include <LeastSquares.h>

Public Member Functions

void add (const std::vector< double > &values, double dest, double weight=1.0)
 
void add (const LeastSquares &other, double weight=1.0)
 
void calculate ()
 
const TMatrixDSym & getCoefficients () const
 
double getConstant () const
 
const TMatrixDSym & getCorrelations () const
 
const TMatrixDSym & getCovariance () const
 
std::vector< double > getMeans () const
 
const TMatrixD & getRotation ()
 
unsigned int getSize () const
 
std::vector< double > getWeights () const
 
 LeastSquares (unsigned int n)
 
void load (XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem)
 
XERCES_CPP_NAMESPACE_QUALIFIER
DOMElement * 
save (XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc) const
 
virtual ~LeastSquares ()
 

Static Public Member Functions

static TVectorD solveFisher (const TMatrixDSym &coeffs)
 
static TMatrixD solveRotation (const TMatrixDSym &covar, TVectorD &trace)
 

Private Attributes

TMatrixDSym coeffs
 
TMatrixDSym corr
 
TMatrixDSym covar
 
const unsigned int n
 
TMatrixD rotation
 
TVectorD trace
 
TVectorD variance
 
TVectorD weights
 

Detailed Description

Definition at line 14 of file LeastSquares.h.

Constructor & Destructor Documentation

PhysicsTools::LeastSquares::LeastSquares ( unsigned int  n)

Definition at line 22 of file LeastSquares.cc.

22  :
23  coeffs(n + 2), covar(n + 1), corr(n + 1), rotation(n, n),
24  weights(n + 1), variance(n + 1), trace(n), n(n)
25 {
26 }
const unsigned int n
Definition: LeastSquares.h:51
PhysicsTools::LeastSquares::~LeastSquares ( )
virtual

Definition at line 28 of file LeastSquares.cc.

29 {
30 }

Member Function Documentation

void PhysicsTools::LeastSquares::add ( const std::vector< double > &  values,
double  dest,
double  weight = 1.0 
)

Definition at line 32 of file LeastSquares.cc.

References coeffs, i, j, n, and CommonMethods::weight().

34 {
35  if (values.size() != n)
36  throw cms::Exception("LeastSquares")
37  << "add(): invalid array size!" << std::endl;
38 
39  for(unsigned int i = 0; i < n; i++) {
40  for(unsigned int j = 0; j < n; j++)
41  coeffs(i, j) += values[i] * values[j] * weight;
42 
43  coeffs(n, i) += values[i] * dest * weight;
44  coeffs(i, n) += values[i] * dest * weight;
45  coeffs(n + 1, i) += values[i] * weight;
46  coeffs(i, n + 1) += values[i] * weight;
47  }
48 
49  coeffs(n, n) += dest * dest * weight;
50  coeffs(n + 1, n) += dest * weight;
51  coeffs(n, n + 1) += dest * weight;
52  coeffs(n + 1, n + 1) += weight;
53 }
int i
Definition: DBlmapReader.cc:9
const unsigned int n
Definition: LeastSquares.h:51
int j
Definition: DBlmapReader.cc:9
void PhysicsTools::LeastSquares::add ( const LeastSquares other,
double  weight = 1.0 
)

Definition at line 55 of file LeastSquares.cc.

References coeffs, getSize(), and n.

56 {
57  if (other.getSize() != n)
58  throw cms::Exception("LeastSquares")
59  << "add(): invalid array size!" << std::endl;
60 
61  coeffs += weight * other.coeffs;
62 }
const unsigned int n
Definition: LeastSquares.h:51
void PhysicsTools::LeastSquares::calculate ( )

Definition at line 87 of file LeastSquares.cc.

References trackerHits::c, coeffs, corr, covar, i, j, N, n, rotation, solveFisher(), solveRotation(), mathSSE::sqrt(), trace, v, variance, w(), and weights.

88 {
89  double N = coeffs(n + 1, n + 1);
90 
91  for(unsigned int i = 0; i <= n; i++) {
92  double M = coeffs(n + 1, i);
93  for(unsigned int j = 0; j <= n; j++)
94  covar(i, j) = coeffs(i, j) * N - M * coeffs(n + 1, j);
95  }
96 
97  for(unsigned int i = 0; i <= n; i++) {
98  double c = covar(i, i);
99  variance[i] = c > 0.0 ? std::sqrt(c) : 0.0;
100  }
101 
102  for(unsigned int i = 0; i <= n; i++) {
103  double M = variance[i];
104  for(unsigned int j = 0; j <= n; j++) {
105  double v = M * variance[j];
106  double w = covar(i, j);
107 
108  corr(i, j) = (v >= 1.0e-9) ? (w / v) : (i == j);
109  }
110  }
111 
114 }
int i
Definition: DBlmapReader.cc:9
static TVectorD solveFisher(const TMatrixDSym &coeffs)
Definition: LeastSquares.cc:64
const unsigned int n
Definition: LeastSquares.h:51
T sqrt(T t)
Definition: SSEVec.h:46
int j
Definition: DBlmapReader.cc:9
static TMatrixD solveRotation(const TMatrixDSym &covar, TVectorD &trace)
Definition: LeastSquares.cc:78
#define N
Definition: blowfish.cc:9
mathSSE::Vec4< T > v
T w() const
const TMatrixDSym& PhysicsTools::LeastSquares::getCoefficients ( ) const
inline

Definition at line 30 of file LeastSquares.h.

References coeffs.

30 { return coeffs; }
double PhysicsTools::LeastSquares::getConstant ( ) const

Definition at line 139 of file LeastSquares.cc.

References n, and weights.

140 {
141  return weights[n];
142 }
const unsigned int n
Definition: LeastSquares.h:51
const TMatrixDSym& PhysicsTools::LeastSquares::getCorrelations ( ) const
inline

Definition at line 32 of file LeastSquares.h.

References corr.

32 { return corr; }
const TMatrixDSym& PhysicsTools::LeastSquares::getCovariance ( ) const
inline

Definition at line 31 of file LeastSquares.h.

References covar.

31 { return covar; }
std::vector< double > PhysicsTools::LeastSquares::getMeans ( ) const

Definition at line 127 of file LeastSquares.cc.

References coeffs, i, N, n, and python.entryComment::results.

128 {
129  std::vector<double> results;
130  results.reserve(n);
131 
132  double N = coeffs(n + 1, n + 1);
133  for(unsigned int i = 0; i < n; i++)
134  results.push_back(coeffs(n + 1, i) / N);
135 
136  return results;
137 }
int i
Definition: DBlmapReader.cc:9
const unsigned int n
Definition: LeastSquares.h:51
#define N
Definition: blowfish.cc:9
const TMatrixD& PhysicsTools::LeastSquares::getRotation ( )
inline

Definition at line 33 of file LeastSquares.h.

References rotation.

33 { return rotation; }
unsigned int PhysicsTools::LeastSquares::getSize ( ) const
inline

Definition at line 29 of file LeastSquares.h.

References n.

Referenced by add().

29 { return n; }
const unsigned int n
Definition: LeastSquares.h:51
std::vector< double > PhysicsTools::LeastSquares::getWeights ( ) const

Definition at line 116 of file LeastSquares.cc.

References i, n, python.entryComment::results, and weights.

117 {
118  std::vector<double> results;
119  results.reserve(n);
120 
121  for(unsigned int i = 0; i < n; i++)
122  results.push_back(weights[i]);
123 
124  return results;
125 }
int i
Definition: DBlmapReader.cc:9
const unsigned int n
Definition: LeastSquares.h:51
void PhysicsTools::LeastSquares::load ( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *  elem)

Definition at line 280 of file LeastSquares.cc.

References trackerHits::c, coeffs, corr, covar, HTMLExport::elem(), edm::hlt::Exception, i, PhysicsTools::loadMatrix(), PhysicsTools::loadVector(), N, n, python.Node::node, pos, rotation, mathSSE::sqrt(), tmp, trace, v, variance, BeamSplash_cfg::version, w(), and weights.

281 {
282  if (std::strcmp(XMLSimpleStr(elem->getNodeName()),
283  "LinearAnalysis") != 0)
284  throw cms::Exception("LeastSquares")
285  << "Expected LinearAnalysis in data file."
286  << std::endl;
287 
288  unsigned int version = XMLDocument::readAttribute<unsigned int>(
289  elem, "version", 1);
290 
291  enum Position {
292  POS_COEFFS, POS_COVAR, POS_CORR, POS_ROTATION,
293  POS_SUMS, POS_WEIGHTS, POS_VARIANCE, POS_TRACE, POS_DONE
294  } pos = POS_COEFFS;
295 
296  for(DOMNode *node = elem->getFirstChild();
297  node; node = node->getNextSibling()) {
298  if (node->getNodeType() != DOMNode::ELEMENT_NODE)
299  continue;
300 
301  DOMElement *elem = static_cast<DOMElement*>(node);
302 
303  switch(pos) {
304  case POS_COEFFS:
305  if (version < 2) {
306  loadMatrix(elem, n + 1, coeffs);
307  coeffs.ResizeTo(n + 2, n + 2);
308  for(unsigned int i = 0; i <= n; i++) {
309  coeffs(n + 1, i) = coeffs(n, i);
310  coeffs(i, n + 1) = coeffs(i, n);
311  }
312  coeffs(n + 1, n + 1) = coeffs(n + 1, n);
313  } else
314  loadMatrix(elem, n + 2, coeffs);
315  break;
316  case POS_COVAR:
317  if (version < 2)
318  loadMatrix(elem, n, covar);
319  else
320  loadMatrix(elem, n + 1, covar);
321  break;
322  case POS_CORR:
323  if (version < 2)
324  loadMatrix(elem, n, corr);
325  else
326  loadMatrix(elem, n + 1, corr);
327  break;
328  case POS_ROTATION:
329  loadMatrix(elem, n, rotation);
330  break;
331  case POS_SUMS:
332  if (version < 2) {
333  TVectorD tmp(n + 1);
334  loadVector(elem, n + 1, tmp);
335 
336  double M = coeffs(n + 1, n);
337  double N = coeffs(n + 1, n + 1);
338 
339  for(unsigned int i = 0; i <= n; i++) {
340  double v = coeffs(n, i) * N -
341  M * coeffs(n + 1, i);
342  double w = coeffs(n, i) * N - v;
343 
344  covar(n, i) = w;
345  covar(i, n) = w;
346  }
347 
348  break;
349  } else
350  pos = (Position)(pos + 1);
351  case POS_WEIGHTS:
352  loadVector(elem, n + 1, weights);
353  break;
354  case POS_VARIANCE:
355  if (version < 2) {
356  loadVector(elem, n, variance);
357 
358  double M = covar(n, n);
359  M = M > 0.0 ? std::sqrt(M) : 0.0;
360  variance[n] = M;
361 
362  for(unsigned int i = 0; i <= n; i++) {
363  double v = M * variance[i];
364  double w = covar(n, i);
365  double c = (v >= 1.0e-9)
366  ? (w / v) : (i == n);
367 
368  corr(n, i) = c;
369  corr(i, n) = c;
370  }
371  } else
372  loadVector(elem, n + 1, variance);
373  break;
374  case POS_TRACE:
375  loadVector(elem, n, trace);
376  break;
377  default:
378  throw cms::Exception("LeastSquares")
379  << "Superfluous content in data file."
380  << std::endl;
381  }
382 
383  pos = (Position)(pos + 1);
384  }
385 
386  if (pos != POS_DONE)
387  throw cms::Exception("LeastSquares")
388  << "Missing objects in data file."
389  << std::endl;
390 }
int i
Definition: DBlmapReader.cc:9
static void loadMatrix(DOMElement *elem, unsigned int n, TMatrixDBase &matrix)
tuple node
Definition: Node.py:50
static void loadVector(DOMElement *elem, unsigned int n, TVectorD &vector)
const unsigned int n
Definition: LeastSquares.h:51
T sqrt(T t)
Definition: SSEVec.h:46
#define N
Definition: blowfish.cc:9
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
mathSSE::Vec4< T > v
T w() const
DOMElement * PhysicsTools::LeastSquares::save ( XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *  doc) const

Definition at line 392 of file LeastSquares.cc.

References coeffs, corr, covar, n, dbtoconf::root, rotation, PhysicsTools::saveMatrix(), PhysicsTools::saveVector(), trace, variance, and weights.

393 {
394  DOMElement *root = doc->createElement(XMLUniStr("LinearAnalysis"));
395  XMLDocument::writeAttribute<unsigned int>(root, "version", 2);
396  XMLDocument::writeAttribute<unsigned int>(root, "size", n);
397 
398  root->appendChild(saveMatrix(doc, n + 2, coeffs));
399  root->appendChild(saveMatrix(doc, n + 1, covar));
400  root->appendChild(saveMatrix(doc, n + 1, corr));
401  root->appendChild(saveMatrix(doc, n, rotation));
402  root->appendChild(saveVector(doc, n + 1, weights));
403  root->appendChild(saveVector(doc, n + 1, variance));
404  root->appendChild(saveVector(doc, n, trace));
405 
406  return root;
407 }
const unsigned int n
Definition: LeastSquares.h:51
tuple doc
Definition: asciidump.py:381
static DOMElement * saveVector(DOMDocument *doc, unsigned int n, const TVectorD &vector)
static DOMElement * saveMatrix(DOMDocument *doc, unsigned int n, const TMatrixDBase &matrix)
string root
initialization
Definition: dbtoconf.py:70
TVectorD PhysicsTools::LeastSquares::solveFisher ( const TMatrixDSym &  coeffs)
static

Definition at line 64 of file LeastSquares.cc.

References coeffs, n, convertSQLiteXML::ok, and tmp.

Referenced by calculate().

65 {
66  unsigned int n = coeffs.GetNrows() - 2;
67 
68  TMatrixDSym tmp;
69  coeffs.GetSub(0, n, tmp);
70  tmp[n] = TVectorD(n + 1, coeffs[n + 1].GetPtr());
71  tmp(n, n) = coeffs(n + 1, n + 1);
72 
73  TDecompSVD decCoeffs(tmp);
74  bool ok;
75  return decCoeffs.Solve(TVectorD(n + 1, coeffs[n].GetPtr()), ok);
76 }
const unsigned int n
Definition: LeastSquares.h:51
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
TMatrixD PhysicsTools::LeastSquares::solveRotation ( const TMatrixDSym &  covar,
TVectorD &  trace 
)
static

Definition at line 78 of file LeastSquares.cc.

References tmp.

Referenced by calculate().

79 {
80  TMatrixDSym tmp;
81  covar.GetSub(0, covar.GetNrows() - 2, tmp);
82  TDecompSVD decCovar(tmp);
83  trace = decCovar.GetSig();
84  return decCovar.GetU();
85 }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100

Member Data Documentation

TMatrixDSym PhysicsTools::LeastSquares::coeffs
private

Definition at line 44 of file LeastSquares.h.

Referenced by add(), calculate(), getCoefficients(), getMeans(), load(), save(), and solveFisher().

TMatrixDSym PhysicsTools::LeastSquares::corr
private

Definition at line 46 of file LeastSquares.h.

Referenced by calculate(), getCorrelations(), load(), and save().

TMatrixDSym PhysicsTools::LeastSquares::covar
private

Definition at line 45 of file LeastSquares.h.

Referenced by calculate(), getCovariance(), load(), and save().

const unsigned int PhysicsTools::LeastSquares::n
private
TMatrixD PhysicsTools::LeastSquares::rotation
private

Definition at line 47 of file LeastSquares.h.

Referenced by calculate(), getRotation(), load(), and save().

TVectorD PhysicsTools::LeastSquares::trace
private

Definition at line 50 of file LeastSquares.h.

Referenced by calculate(), load(), and save().

TVectorD PhysicsTools::LeastSquares::variance
private

Definition at line 49 of file LeastSquares.h.

Referenced by calculate(), load(), and save().

TVectorD PhysicsTools::LeastSquares::weights
private

Definition at line 48 of file LeastSquares.h.

Referenced by calculate(), getConstant(), getWeights(), load(), and save().