236 fCovarianceMatrix(1,1),
258 : fMeanValues(nVariables),
260 fCovarianceMatrix(nVariables,nVariables),
261 fEigenVectors(nVariables,nVariables),
262 fEigenValues(nVariables),
263 fOffDiagonal(nVariables),
266 if (nVariables <= 1) {
267 Error(
"TPrincipal",
"You can't be serious - nVariables == 1!!!");
278 while (strlen(opt) > 0) {
294 Error(
"TPrincipal",
"Couldn't create vector mean values");
296 Error(
"TPrincipal",
"Couldn't create vector sigmas");
298 Error(
"TPrincipal",
"Couldn't create covariance matrix");
300 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
302 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
304 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
309 Error(
"TPrincipal",
"Couldn't create user data vector");
318 fNumberOfDataPoints(pr.fNumberOfDataPoints),
319 fNumberOfVariables(pr.fNumberOfVariables),
320 fMeanValues(pr.fMeanValues),
322 fCovarianceMatrix(pr.fCovarianceMatrix),
323 fEigenVectors(pr.fEigenVectors),
324 fEigenValues(pr.fEigenValues),
325 fOffDiagonal(pr.fOffDiagonal),
326 fUserData(pr.fUserData),
328 fHistograms(pr.fHistograms),
329 fIsNormalised(pr.fIsNormalised),
330 fStoreData(pr.fStoreData)
519 for (j = 0; j < i + 1; j++) {
665 Int_t len = strlen(opt);
667 for (i = 0; i < len; i++) {
694 Warning(
"MakeHistograms",
"Unknown option: %c",opt[i]);
699 if (!makeX && !makeD && !makeP && !makeE && !makeS)
735 hE =
new TH1F(
Form(
"%s_e",name),
"Eigenvalues of Covariance matrix",
742 hS =
new TH1F(
Form(
"%s_s",name),
"E_{N}",
745 hS->
SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
757 hX[i] =
new TH1F(
Form(
"%s_x%03d", name, i),
758 Form(
"Pattern space, variable %d", i),
769 hD[i] =
new TH2F(
Form(
"%s_d%03d", name, i),
770 Form(
"Distance from pattern to "
771 "feature space, variable %d", i),
773 fNumberOfVariables-1,
776 hD[i]->
SetXTitle(
Form(
"|x_{%d} - x'_{%d,N}|/#sigma_{%d}",i,i,i));
789 hP[i] =
new TH1F(
Form(
"%s_p%03d", name, i),
790 Form(
"Feature space, variable %d", i),
801 if (!makeX && !makeP && !makeD && !makeS)
817 if (makeP||makeD||makeS)
821 if (makeD || makeS) {
825 for (j = fNumberOfVariables; j > 0; j--) {
833 hS->
Fill(j,d[k]*d[k]);
837 (hD[k])->Fill(d[k],j);
881 for (j = 0; j <= i; j++)
889 for (j = 0; j <= i; j++) {
977 const char *prefix = (isMethod ?
Form(
"%s::", classname) :
"");
978 const char *cv_qual = (isMethod ?
"" :
"static ");
982 Error(
"MakeRealCode",
"couldn't open output file '%s'",filename);
986 std::cout <<
"Writing on file \"" << filename <<
"\" ... " << std::flush;
991 outFile <<
"// -*- mode: c++ -*-" << std::endl;
993 outFile <<
"// " << std::endl
994 <<
"// File " << filename
995 <<
" generated by TPrincipal::MakeCode" << std::endl;
998 outFile <<
"// on " << date.
AsString() << std::endl;
1000 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
1001 << std::endl <<
"//" << std::endl;
1003 outFile <<
"// This file contains the functions " << std::endl
1004 <<
"//" << std::endl
1005 <<
"// void " << prefix
1006 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
1007 <<
"// void " << prefix
1008 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);"
1009 << std::endl <<
"//" << std::endl
1010 <<
"// The first for transforming original data x in " << std::endl
1011 <<
"// pattern space, to principal components p in " << std::endl
1012 <<
"// feature space. The second function is for the" << std::endl
1013 <<
"// inverse transformation, but using only nTest" << std::endl
1014 <<
"// of the principal components in the expansion" << std::endl
1015 <<
"// " << std::endl
1016 <<
"// See TPrincipal class documentation for more "
1017 <<
"information " << std::endl <<
"// " << std::endl;
1019 outFile <<
"#ifndef __CINT__" << std::endl;
1022 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
1025 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
1027 outFile <<
"#endif" << std::endl << std::endl;
1036 outFile <<
"//" << std::endl
1037 <<
"// Static data variables" << std::endl
1038 <<
"//" << std::endl;
1039 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = "
1046 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
1047 <<
"// Elements are stored row-wise, that is" << std::endl
1048 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
1049 <<
"// where i and j are zero-based" << std::endl;
1050 outFile << cv_qual <<
"Double_t " << prefix
1051 <<
"gEigenVectors[] = {" << std::flush;
1055 Int_t index = i * fNumberOfVariables + j;
1056 outFile << (index != 0 ?
"," :
"" ) << std::endl
1060 outFile <<
"};" << std::endl << std::endl;
1063 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
1064 outFile << cv_qual <<
"Double_t " << prefix
1065 <<
"gEigenValues[] = {" << std::flush;
1067 outFile << (i != 0 ?
"," :
"") << std::endl
1069 outFile << std::endl <<
"};" << std::endl << std::endl;
1072 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
1073 outFile << cv_qual <<
"Double_t " << prefix
1074 <<
"gMeanValues[] = {" << std::flush;
1076 outFile << (i != 0 ?
"," :
"") << std::endl
1078 outFile << std::endl <<
"};" << std::endl << std::endl;
1081 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
1082 outFile << cv_qual <<
"Double_t " << prefix
1083 <<
"gSigmaValues[] = {" << std::flush;
1085 outFile << (i != 0 ?
"," :
"") << std::endl
1088 outFile << std::endl <<
"};" << std::endl << std::endl;
1095 outFile <<
"// " << std::endl
1097 << (isMethod ?
"method " :
"function ")
1098 <<
" void " << prefix
1099 <<
"X2P(Double_t *x, Double_t *p)"
1100 << std::endl <<
"// " << std::endl;
1101 outFile <<
"void " << prefix
1102 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1103 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1104 <<
" p[i] = 0;" << std::endl
1105 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1106 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1107 <<
" * gEigenVectors[j * gNVariables + i] "
1108 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }"
1109 << std::endl <<
"}" << std::endl << std::endl;
1113 outFile <<
"// " << std::endl <<
"// The "
1114 << (isMethod ?
"method " :
"function ")
1115 <<
" void " << prefix
1116 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)"
1117 << std::endl <<
"// " << std::endl;
1118 outFile <<
"void " << prefix
1119 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1120 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1121 <<
" x[i] = gMeanValues[i];" << std::endl
1122 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1123 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1124 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1125 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1128 outFile <<
"// EOF for " << filename << std::endl;
1133 std::cout <<
"done" << std::endl;
1146 for (
Int_t j = 0; j < nTest; j++)
1169 Int_t len = strlen(opt);
1170 for (
Int_t i = 0; i < len; i++) {
1189 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1194 if (printM||printS||printE) {
1195 std::cout <<
" Variable # " << std::flush;
1197 std::cout <<
"| Mean Value " << std::flush;
1199 std::cout <<
"| Sigma " << std::flush;
1201 std::cout <<
"| Eigenvalue" << std::flush;
1202 std::cout << std::endl;
1204 std::cout <<
"-------------" << std::flush;
1206 std::cout <<
"+------------" << std::flush;
1208 std::cout <<
"+------------" << std::flush;
1210 std::cout <<
"+------------" << std::flush;
1211 std::cout << std::endl;
1214 std::cout << std::setw(12) << i <<
" " << std::flush;
1216 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1219 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1220 <<
fSigmas(i) <<
" " << std::flush;
1222 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1224 std::cout << std::endl;
1226 std::cout << std::endl;
1231 std::cout <<
"Eigenvector # " << i << std::flush;
1304 s[i] += (x[j] - xp[j])*(x[j] - xp[j]);
1323 Warning(
"Test",
"Couldn't get histogram of square residuals");
void Add(TObject *obj, const char *name=0, Int_t check=-1)
Add object with name to browser.
const TVectorD & GetEigenValues() const
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Principal Components Analysis (PCA)
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
virtual void Browse(TBrowser *b)
Browse the TPrincipal object in the TBrowser.
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
virtual ~TPrincipal()
destructor
void MakeRealCode(const char *filename, const char *prefix, Option_t *option="")
PRIVATE METHOD: This is the method that actually generates the code for the transformations to and fr...
static Vc_ALWAYS_INLINE float_v trunc(float_v::AsArg v)
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
virtual void P2X(const Double_t *p, Double_t *x, Int_t nTest)
Calculate x as a function of nTest of the most significant principal components p, and return it in x.
virtual void SetName(const char *name)
Change (i.e.
virtual void Print(Option_t *opt="MSE") const
Print the statistics Options are M Print mean values of original data S Print sigma values of origina...
static const char * filename()
1-D histogram with a float per channel (see TH1 documentation)}
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
virtual void SumOfSquareResiduals(const Double_t *x, Double_t *s)
PRIVATE METHOD: Begin_html.
virtual void SetYTitle(const char *title)
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
void MakeNormalised()
PRIVATE METHOD: Normalize the covariance matrix.
void Test(Option_t *option="")
Test the PCA, bye calculating the sum square of residuals (see method SumOfSquareResiduals), and display the histogram.
const char * Data() const
The TNamed class is the base class for all named ROOT classes.
virtual void MakeMethods(const char *classname="PCA", Option_t *option="")
Generate the file PCA.cxx which contains the implementation of two methods: ...
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
Make histograms of the result of the analysis.
virtual void X2P(const Double_t *x, Double_t *p)
Calculate the principal components from the original data vector x, and return it in p...
void Print(Option_t *option="") const
Print the vector as a list of elements.
Using a TBrowser one can browse all ROOT objects.
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
virtual void AddRow(const Double_t *x)
Begin_Html.
virtual void Draw(Option_t *option="")
Draw this histogram with options.
2-D histogram with a float per channel (see TH1 documentation)}
TVectorT< Element > & Zero()
Set vector elements to zero.
char * Form(const char *fmt,...)
virtual const char * GetName() const
Returns name of object.
virtual const Element * GetMatrixArray() const
virtual void MakePrincipals()
Perform the principal components analysis.
Int_t fNumberOfDataPoints
virtual void Clear(Option_t *option="")
Clear the data in Object.
const Double_t * GetRow(Int_t row)
Return a row of the user supplied data.
const TMatrixD & GetEigenVectors() const
TPrincipal & operator=(const TPrincipal &)
assignement operator
virtual void SetXTitle(const char *title)
virtual void Add(TObject *obj)
virtual void MakeCode(const char *filename="pca", Option_t *option="")
Generates the file , with .C appended if it does argument doesn't end in ...
Double_t Sqrt(Double_t x)
const char * AsString() const
Return the date & time as a string (ctime() format).
#define sym(otri1, otri2)
TMatrixD fCovarianceMatrix
TPrincipal()
Empty CTOR, Do not use.
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.