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) {
736 if (makeP||makeD||makeS)
740 if (makeD || makeS) {
744 for (j = fNumberOfVariables; j > 0; j--) {
752 hS->
Fill(j,d[k]*d[k]);
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;
973 Int_t index = i * fNumberOfVariables + j;
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");
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
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
Delete a TObjLink object.
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.
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.
static constexpr double s
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