CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Attributes

PhysicsTools::LeastSquares Class Reference

#include <LeastSquares.h>

List of all members.

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.

                                         :
        coeffs(n + 2), covar(n + 1), corr(n + 1), rotation(n, n),
        weights(n + 1), variance(n + 1), trace(n), n(n)
{
}
PhysicsTools::LeastSquares::~LeastSquares ( ) [virtual]

Definition at line 28 of file LeastSquares.cc.

{
}

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().

{
        if (values.size() != n)
                throw cms::Exception("LeastSquares")
                        << "add(): invalid array size!" << std::endl;

        for(unsigned int i = 0; i < n; i++) {
                for(unsigned int j = 0; j < n; j++)
                        coeffs(i, j) += values[i] * values[j] * weight;

                coeffs(n, i) += values[i] * dest * weight;
                coeffs(i, n) += values[i] * dest * weight;
                coeffs(n + 1, i) += values[i] * weight;
                coeffs(i, n + 1) += values[i] * weight;
        } 

        coeffs(n, n) += dest * dest * weight;
        coeffs(n + 1, n) += dest * weight;
        coeffs(n, n + 1) += dest * weight;
        coeffs(n + 1, n + 1) += weight;
}
void PhysicsTools::LeastSquares::add ( const LeastSquares other,
double  weight = 1.0 
)

Definition at line 55 of file LeastSquares.cc.

References coeffs, getSize(), and n.

{
        if (other.getSize() != n)
                throw cms::Exception("LeastSquares")
                        << "add(): invalid array size!" << std::endl;

        coeffs += weight * other.coeffs;
}
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.

{
        double N = coeffs(n + 1, n + 1);

        for(unsigned int i = 0; i <= n; i++) {
                double M = coeffs(n + 1, i);
                for(unsigned int j = 0; j <= n; j++)
                        covar(i, j) = coeffs(i, j) * N - M * coeffs(n + 1, j);
        }

        for(unsigned int i = 0; i <= n; i++) {
                double c = covar(i, i);
                variance[i] = c > 0.0 ? std::sqrt(c) : 0.0;
        }

        for(unsigned int i = 0; i <= n; i++) {
                double M = variance[i];
                for(unsigned int j = 0; j <= n; j++) {
                        double v = M * variance[j];
                        double w = covar(i, j);

                        corr(i, j) = (v >= 1.0e-9) ? (w / v) : (i == j);
                }
        }

        weights = solveFisher(coeffs);
        rotation = solveRotation(covar, trace);
}
const TMatrixDSym& PhysicsTools::LeastSquares::getCoefficients ( ) const [inline]

Definition at line 30 of file LeastSquares.h.

References coeffs.

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

Definition at line 139 of file LeastSquares.cc.

References n, and weights.

{
        return weights[n];
}
const TMatrixDSym& PhysicsTools::LeastSquares::getCorrelations ( ) const [inline]

Definition at line 32 of file LeastSquares.h.

References corr.

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

Definition at line 31 of file LeastSquares.h.

References covar.

{ 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.

{
        std::vector<double> results;
        results.reserve(n);

        double N = coeffs(n + 1, n + 1);
        for(unsigned int i = 0; i < n; i++)
                results.push_back(coeffs(n + 1, i) / N);

        return results;
}
const TMatrixD& PhysicsTools::LeastSquares::getRotation ( ) [inline]

Definition at line 33 of file LeastSquares.h.

References rotation.

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

Definition at line 29 of file LeastSquares.h.

References n.

Referenced by add().

{ return n; }
std::vector< double > PhysicsTools::LeastSquares::getWeights ( ) const

Definition at line 116 of file LeastSquares.cc.

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

{
        std::vector<double> results;
        results.reserve(n);
        
        for(unsigned int i = 0; i < n; i++)
                results.push_back(weights[i]);
        
        return results;
}
void PhysicsTools::LeastSquares::load ( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *  elem)
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* PhysicsTools::LeastSquares::save ( XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *  doc) const
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().

{
        unsigned int n = coeffs.GetNrows() - 2;

        TMatrixDSym tmp;
        coeffs.GetSub(0, n, tmp);
        tmp[n] = TVectorD(n + 1, coeffs[n + 1].GetPtr());
        tmp(n, n) = coeffs(n + 1, n + 1);

        TDecompSVD decCoeffs(tmp);
        bool ok;
        return decCoeffs.Solve(TVectorD(n + 1, coeffs[n].GetPtr()), ok);
}
TMatrixD PhysicsTools::LeastSquares::solveRotation ( const TMatrixDSym &  covar,
TVectorD &  trace 
) [static]

Definition at line 78 of file LeastSquares.cc.

References tmp.

Referenced by calculate().

{
        TMatrixDSym tmp;
        covar.GetSub(0, covar.GetNrows() - 2, tmp);
        TDecompSVD decCovar(tmp);
        trace = decCovar.GetSig();
        return decCovar.GetU();
}

Member Data Documentation

TMatrixDSym PhysicsTools::LeastSquares::coeffs [private]
TMatrixDSym PhysicsTools::LeastSquares::corr [private]

Definition at line 46 of file LeastSquares.h.

Referenced by calculate(), getCorrelations(), and TestHistoMgr::save().

TMatrixDSym PhysicsTools::LeastSquares::covar [private]

Definition at line 45 of file LeastSquares.h.

Referenced by calculate(), getCovariance(), and TestHistoMgr::save().

const unsigned int PhysicsTools::LeastSquares::n [private]

Definition at line 47 of file LeastSquares.h.

Referenced by calculate(), getRotation(), and TestHistoMgr::save().

Definition at line 50 of file LeastSquares.h.

Referenced by calculate(), and TestHistoMgr::save().

Definition at line 49 of file LeastSquares.h.

Referenced by calculate(), and TestHistoMgr::save().

Definition at line 48 of file LeastSquares.h.

Referenced by calculate(), getConstant(), getWeights(), and TestHistoMgr::save().