232 fCovarianceMatrix(1,1),
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");
431 for (j = 0; j < i + 1; j++) {
462 while ((h = (
TH1*)next()))
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",
654 hS =
new TH1F(
Form(
"%s_s",name),
"E_{N}",
657 hS->
SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
669 hX[i] =
new TH1F(
Form(
"%s_x%03d", name, i),
670 Form(
"Pattern space, variable %d", i),
681 hD[i] =
new TH2F(
Form(
"%s_d%03d", name, i),
682 Form(
"Distance from pattern to " 683 "feature space, variable %d", i),
685 fNumberOfVariables-1,
688 hD[i]->
SetXTitle(
Form(
"|x_{%d} - x'_{%d,N}|/#sigma_{%d}",i,i,i));
701 hP[i] =
new TH1F(
Form(
"%s_p%03d", name, i),
702 Form(
"Feature space, variable %d", i),
713 if (!makeX && !makeP && !makeD && !makeS)
729 if (makeP||makeD||makeS)
733 if (makeD || makeS) {
737 for (j = fNumberOfVariables; j > 0; j--) {
745 hS->
Fill(j,d[k]*d[k]);
749 (hD[k])->Fill(d[k],j);
793 for (j = 0; j <= i; j++)
801 for (j = 0; j <= i; j++) {
888 const char *prefix = (isMethod ?
Form(
"%s::", classname) :
"");
889 const char *cv_qual = (isMethod ?
"" :
"static ");
891 std::ofstream outFile(filename,std::ios::out|std::ios::trunc);
893 Error(
"MakeRealCode",
"couldn't open output file '%s'",filename);
897 std::cout <<
"Writing on file \"" << filename <<
"\" ... " << std::flush;
902 outFile <<
"// -*- mode: c++ -*-" << std::endl;
904 outFile <<
"// " << std::endl
905 <<
"// File " << filename
906 <<
" generated by TPrincipal::MakeCode" << std::endl;
909 outFile <<
"// on " << date.
AsString() << std::endl;
911 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
912 << std::endl <<
"//" << std::endl;
914 outFile <<
"// This file contains the functions " << std::endl
916 <<
"// void " << prefix
917 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
918 <<
"// void " << prefix
919 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);" 920 << std::endl <<
"//" << std::endl
921 <<
"// The first for transforming original data x in " << std::endl
922 <<
"// pattern space, to principal components p in " << std::endl
923 <<
"// feature space. The second function is for the" << std::endl
924 <<
"// inverse transformation, but using only nTest" << std::endl
925 <<
"// of the principal components in the expansion" << std::endl
926 <<
"// " << std::endl
927 <<
"// See TPrincipal class documentation for more " 928 <<
"information " << std::endl <<
"// " << std::endl;
930 outFile <<
"#ifndef __CINT__" << std::endl;
933 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
936 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
938 outFile <<
"#endif" << std::endl << std::endl;
947 outFile <<
"//" << std::endl
948 <<
"// Static data variables" << std::endl
949 <<
"//" << std::endl;
950 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = " 957 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
958 <<
"// Elements are stored row-wise, that is" << std::endl
959 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
960 <<
"// where i and j are zero-based" << std::endl;
961 outFile << cv_qual <<
"Double_t " << prefix
962 <<
"gEigenVectors[] = {" << std::flush;
966 Int_t index = i * fNumberOfVariables + j;
967 outFile << (index != 0 ?
"," :
"" ) << std::endl
971 outFile <<
"};" << std::endl << std::endl;
974 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
975 outFile << cv_qual <<
"Double_t " << prefix
976 <<
"gEigenValues[] = {" << std::flush;
978 outFile << (i != 0 ?
"," :
"") << std::endl
980 outFile << std::endl <<
"};" << std::endl << std::endl;
983 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
984 outFile << cv_qual <<
"Double_t " << prefix
985 <<
"gMeanValues[] = {" << std::flush;
987 outFile << (i != 0 ?
"," :
"") << std::endl
989 outFile << std::endl <<
"};" << std::endl << std::endl;
992 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
993 outFile << cv_qual <<
"Double_t " << prefix
994 <<
"gSigmaValues[] = {" << std::flush;
996 outFile << (i != 0 ?
"," :
"") << std::endl
999 outFile << std::endl <<
"};" << std::endl << std::endl;
1006 outFile <<
"// " << std::endl
1008 << (isMethod ?
"method " :
"function ")
1009 <<
" void " << prefix
1010 <<
"X2P(Double_t *x, Double_t *p)" 1011 << std::endl <<
"// " << std::endl;
1012 outFile <<
"void " << prefix
1013 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1014 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1015 <<
" p[i] = 0;" << std::endl
1016 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1017 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1018 <<
" * gEigenVectors[j * gNVariables + i] " 1019 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }" 1020 << std::endl <<
"}" << std::endl << std::endl;
1024 outFile <<
"// " << std::endl <<
"// The " 1025 << (isMethod ?
"method " :
"function ")
1026 <<
" void " << prefix
1027 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)" 1028 << std::endl <<
"// " << std::endl;
1029 outFile <<
"void " << prefix
1030 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1031 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1032 <<
" x[i] = gMeanValues[i];" << std::endl
1033 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1034 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1035 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1036 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1039 outFile <<
"// EOF for " << filename << std::endl;
1044 std::cout <<
"done" << std::endl;
1057 for (
Int_t j = 0; j < nTest; j++)
1080 Int_t len = strlen(opt);
1081 for (
Int_t i = 0; i < len; i++) {
1100 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1105 if (printM||printS||printE) {
1106 std::cout <<
" Variable # " << std::flush;
1108 std::cout <<
"| Mean Value " << std::flush;
1110 std::cout <<
"| Sigma " << std::flush;
1112 std::cout <<
"| Eigenvalue" << std::flush;
1113 std::cout << std::endl;
1115 std::cout <<
"-------------" << std::flush;
1117 std::cout <<
"+------------" << std::flush;
1119 std::cout <<
"+------------" << std::flush;
1121 std::cout <<
"+------------" << std::flush;
1122 std::cout << std::endl;
1125 std::cout << std::setw(12) << i <<
" " << std::flush;
1127 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1130 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1131 <<
fSigmas(i) <<
" " << std::flush;
1133 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1135 std::cout << std::endl;
1137 std::cout << std::endl;
1142 std::cout <<
"Eigenvector # " << i << std::flush;
1175 s[i] += (x[j] - xp[j])*(x[j] - xp[j]);
1194 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.
virtual const char * GetName() const
Returns name of object.
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="")
This is the method that actually generates the code for the transformations to and from feature space...
virtual void Print(Option_t *opt="MSE") const
Print the statistics Options are.
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 const Element * GetMatrixArray() const
virtual void SetName(const char *name)
Set the name of the TNamed.
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
tomato 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)
Calculates the sum of the square residuals, that is.
virtual void SetYTitle(const char *title)
void MakeNormalised()
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.
void Print(Option_t *option="") const
Print the vector as a list of elements.
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
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 <classname>PCA.cxx which contains the implementation of two methods: void <classnam...
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
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...
Using a TBrowser one can browse all ROOT objects.
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
virtual void AddRow(const Double_t *x)
Add a data point and update the covariance matrix.
virtual void Draw(Option_t *option="")
Draw this histogram with options.
tomato 2-D histogram with a float per channel (see TH1 documentation)}
TVectorT< Element > & Zero()
Set vector elements to zero.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
char * Form(const char *fmt,...)
virtual void MakePrincipals()
Perform the principal components analysis.
Int_t fNumberOfDataPoints
const TMatrixD & GetEigenVectors() const
virtual void Clear(Option_t *option="")
Clear the data in Object.
const TVectorD & GetEigenValues() const
const Double_t * GetRow(Int_t row)
Return a row of the user supplied data.
TPrincipal & operator=(const TPrincipal &)
Assignment operator.
const char * AsString() const
Return the date & time as a string (ctime() format).
virtual void SetXTitle(const char *title)
virtual void Add(TObject *obj)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
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 ...
Double_t Sqrt(Double_t x)
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
#define sym(otri1, otri2)
TMatrixD fCovarianceMatrix
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
TPrincipal()
Empty constructor. Do not use.
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
const char * Data() const