232 fCovarianceMatrix(1,1),
254 : fMeanValues(nVariables),
256 fCovarianceMatrix(nVariables,nVariables),
257 fEigenVectors(nVariables,nVariables),
258 fEigenValues(nVariables),
259 fOffDiagonal(nVariables),
262 if (nVariables <= 1) {
263 Error(
"TPrincipal",
"You can't be serious - nVariables == 1!!!");
274 while (strlen(opt) > 0) {
290 Error(
"TPrincipal",
"Couldn't create vector mean values");
292 Error(
"TPrincipal",
"Couldn't create vector sigmas");
294 Error(
"TPrincipal",
"Couldn't create covariance matrix");
296 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
298 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
300 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
305 Error(
"TPrincipal",
"Couldn't create user data vector");
314 fNumberOfDataPoints(pr.fNumberOfDataPoints),
315 fNumberOfVariables(pr.fNumberOfVariables),
316 fMeanValues(pr.fMeanValues),
318 fCovarianceMatrix(pr.fCovarianceMatrix),
319 fEigenVectors(pr.fEigenVectors),
320 fEigenValues(pr.fEigenValues),
321 fOffDiagonal(pr.fOffDiagonal),
322 fUserData(pr.fUserData),
324 fHistograms(pr.fHistograms),
325 fIsNormalised(pr.fIsNormalised),
326 fStoreData(pr.fStoreData)
431 for (j = 0; j < i + 1; j++) {
462 while ((
h = (
TH1*)next()))
463 b->Add(
h,
h->GetName());
470 b->Add(&
fSigmas,
"Sigma value vector");
577 Int_t len = strlen(opt);
579 for (i = 0; i < len; i++) {
606 Warning(
"MakeHistograms",
"Unknown option: %c",opt[i]);
611 if (!makeX && !makeD && !makeP && !makeE && !makeS)
647 hE =
new TH1F(
Form(
"%s_e",
name),
"Eigenvalues of Covariance matrix",
657 hS->
SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
670 Form(
"Pattern space, variable %d", i),
682 Form(
"Distance from pattern to "
683 "feature space, variable %d", i),
688 hD[i]->
SetXTitle(
Form(
"|x_{%d} - x'_{%d,N}|/#sigma_{%d}",i,i,i));
702 Form(
"Feature space, variable %d", i),
713 if (!makeX && !makeP && !makeD && !makeS) {
736 if (makeP||makeD||makeS)
740 if (makeD || makeS) {
756 (hD[k])->Fill(
d[k],j);
800 for (j = 0; j <= i; j++)
808 for (j = 0; j <= i; j++) {
895 const char *prefix = (isMethod ?
Form(
"%s::", classname) :
"");
896 const char *cv_qual = (isMethod ?
"" :
"static ");
898 std::ofstream outFile(filename,std::ios::out|std::ios::trunc);
900 Error(
"MakeRealCode",
"couldn't open output file '%s'",filename);
904 std::cout <<
"Writing on file \"" << filename <<
"\" ... " << std::flush;
909 outFile <<
"// -*- mode: c++ -*-" << std::endl;
911 outFile <<
"// " << std::endl
912 <<
"// File " << filename
913 <<
" generated by TPrincipal::MakeCode" << std::endl;
916 outFile <<
"// on " << date.
AsString() << std::endl;
918 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
919 << std::endl <<
"//" << std::endl;
921 outFile <<
"// This file contains the functions " << std::endl
923 <<
"// void " << prefix
924 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
925 <<
"// void " << prefix
926 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);"
927 << std::endl <<
"//" << std::endl
928 <<
"// The first for transforming original data x in " << std::endl
929 <<
"// pattern space, to principal components p in " << std::endl
930 <<
"// feature space. The second function is for the" << std::endl
931 <<
"// inverse transformation, but using only nTest" << std::endl
932 <<
"// of the principal components in the expansion" << std::endl
933 <<
"// " << std::endl
934 <<
"// See TPrincipal class documentation for more "
935 <<
"information " << std::endl <<
"// " << std::endl;
937 outFile <<
"#ifndef __CINT__" << std::endl;
940 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
943 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
945 outFile <<
"#endif" << std::endl << std::endl;
954 outFile <<
"//" << std::endl
955 <<
"// Static data variables" << std::endl
956 <<
"//" << std::endl;
957 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = "
964 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
965 <<
"// Elements are stored row-wise, that is" << std::endl
966 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
967 <<
"// where i and j are zero-based" << std::endl;
968 outFile << cv_qual <<
"Double_t " << prefix
969 <<
"gEigenVectors[] = {" << std::flush;
974 outFile << (index != 0 ?
"," :
"" ) << std::endl
978 outFile <<
"};" << std::endl << std::endl;
981 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
982 outFile << cv_qual <<
"Double_t " << prefix
983 <<
"gEigenValues[] = {" << std::flush;
985 outFile << (i != 0 ?
"," :
"") << std::endl
987 outFile << std::endl <<
"};" << std::endl << std::endl;
990 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
991 outFile << cv_qual <<
"Double_t " << prefix
992 <<
"gMeanValues[] = {" << std::flush;
994 outFile << (i != 0 ?
"," :
"") << std::endl
996 outFile << std::endl <<
"};" << std::endl << std::endl;
999 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
1000 outFile << cv_qual <<
"Double_t " << prefix
1001 <<
"gSigmaValues[] = {" << std::flush;
1003 outFile << (i != 0 ?
"," :
"") << std::endl
1006 outFile << std::endl <<
"};" << std::endl << std::endl;
1013 outFile <<
"// " << std::endl
1015 << (isMethod ?
"method " :
"function ")
1016 <<
" void " << prefix
1017 <<
"X2P(Double_t *x, Double_t *p)"
1018 << std::endl <<
"// " << std::endl;
1019 outFile <<
"void " << prefix
1020 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1021 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1022 <<
" p[i] = 0;" << std::endl
1023 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1024 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1025 <<
" * gEigenVectors[j * gNVariables + i] "
1026 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }"
1027 << std::endl <<
"}" << std::endl << std::endl;
1031 outFile <<
"// " << std::endl <<
"// The "
1032 << (isMethod ?
"method " :
"function ")
1033 <<
" void " << prefix
1034 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)"
1035 << std::endl <<
"// " << std::endl;
1036 outFile <<
"void " << prefix
1037 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1038 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1039 <<
" x[i] = gMeanValues[i];" << std::endl
1040 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1041 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1042 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1043 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1046 outFile <<
"// EOF for " << filename << std::endl;
1051 std::cout <<
"done" << std::endl;
1064 for (
Int_t j = 0; j < nTest; j++)
1087 Int_t len = strlen(opt);
1088 for (
Int_t i = 0; i < len; i++) {
1107 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1112 if (printM||printS||printE) {
1113 std::cout <<
" Variable # " << std::flush;
1115 std::cout <<
"| Mean Value " << std::flush;
1117 std::cout <<
"| Sigma " << std::flush;
1119 std::cout <<
"| Eigenvalue" << std::flush;
1120 std::cout << std::endl;
1122 std::cout <<
"-------------" << std::flush;
1124 std::cout <<
"+------------" << std::flush;
1126 std::cout <<
"+------------" << std::flush;
1128 std::cout <<
"+------------" << std::flush;
1129 std::cout << std::endl;
1132 std::cout << std::setw(12) << i <<
" " << std::flush;
1134 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1137 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1138 <<
fSigmas(i) <<
" " << std::flush;
1140 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1142 std::cout << std::endl;
1144 std::cout << std::endl;
1149 std::cout <<
"Eigenvector # " << i << std::flush;
1182 s[i] += (
x[j] - xp[j])*(
x[j] - xp[j]);
1201 Warning(
"Test",
"Couldn't get histogram of square residuals");
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
char * Form(const char *fmt,...)
Using a TBrowser one can browse all ROOT objects.
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
const char * AsString() const
Return the date & time as a string (ctime() format).
1-D histogram with a float per channel (see TH1 documentation)}
virtual void SetXTitle(const char *title)
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
virtual void Draw(Option_t *option="")
Draw this histogram with options.
virtual void SetYTitle(const char *title)
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
2-D histogram with a float per channel (see TH1 documentation)}
virtual void Add(TObject *obj)
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
const TVectorD & GetEigenValues() const
const TMatrixD & GetEigenVectors() const
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
virtual const Element * GetMatrixArray() const
The TNamed class is the base class for all named ROOT classes.
virtual void SetName(const char *name)
Set the name of the TNamed.
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Principal Components Analysis (PCA)
virtual void MakeMethods(const char *classname="PCA", Option_t *option="")
Generate the file <classname>PCA.cxx which contains the implementation of two methods:
virtual void AddRow(const Double_t *x)
Add a data point and update the covariance matrix.
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.
virtual void Print(Option_t *opt="MSE") const
Print the statistics Options are.
const Double_t * GetRow(Int_t row)
Return a row of the user supplied data.
TPrincipal()
Empty constructor. Do not use.
virtual void Browse(TBrowser *b)
Browse the TPrincipal object in the TBrowser.
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
Make histograms of the result of the analysis.
virtual void MakeCode(const char *filename="pca", Option_t *option="")
Generates the file <filename>, with .C appended if it does argument doesn't end in ....
TMatrixD fCovarianceMatrix
virtual ~TPrincipal()
Destructor.
void MakeRealCode(const char *filename, const char *prefix, Option_t *option="")
This is the method that actually generates the code for the transformations to and from feature space...
virtual void Clear(Option_t *option="")
Clear the data in Object.
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,...
Int_t fNumberOfDataPoints
void MakeNormalised()
Normalize the covariance matrix.
virtual void MakePrincipals()
Perform the principal components analysis.
virtual void SumOfSquareResiduals(const Double_t *x, Double_t *s)
Calculates the sum of the square residuals, that is.
void Test(Option_t *option="")
Test the PCA, bye calculating the sum square of residuals (see method SumOfSquareResiduals),...
TPrincipal & operator=(const TPrincipal &)
Assignment operator.
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
const char * Data() const
TVectorT< Element > & Zero()
Set vector elements to zero.
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
static constexpr double s
Double_t Sqrt(Double_t x)
#define sym(otri1, otri2)