261 if (nVariables <= 1) {
262 Error(
"TPrincipal",
"You can't be serious - nVariables <= 1!!!");
265 if (nVariables > std::numeric_limits<Int_t>::max()) {
266 Error(
"TPrincipal",
"`nVariables` input parameter %lld is larger than the allowed maximum %d", nVariables, std::numeric_limits<Int_t>::max());
277 while (opt && strlen(opt) > 0) {
293 Error(
"TPrincipal",
"Couldn't create vector mean values");
295 Error(
"TPrincipal",
"Couldn't create vector sigmas");
297 Error(
"TPrincipal",
"Couldn't create covariance matrix");
299 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
301 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
303 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
308 Error(
"TPrincipal",
"Couldn't create user data vector");
418 Error(
"AddRow",
"`fNumberOfDataPoints` has reached its allowed maximum %d, cannot add new row.",
fNumberOfDataPoints);
438 meanValues[i] *= cor;
439 meanValues[i] += p[i] * invnp;
440 const Double_t t1 = (p[i] - meanValues[i]) * invnpM1;
443 for (j = 0; j < i + 1; j++) {
445 covMatrix[
index] *= cor;
446 covMatrix[
index] +=
t1 * (p[j] - meanValues[j]);
475 while ((
h = (
TH1*)next()))
476 b->Add(
h,
h->GetName());
483 b->Add(&
fSigmas,
"Sigma value vector");
529 if (
index > std::numeric_limits<Int_t>::max()) {
530 Error(
"GetRow",
"Input parameter `row` %lld x fNumberOfVariables %d goes into overflow (%lld>%d), returning nullptr.", row,
fNumberOfVariables,
index, std::numeric_limits<Int_t>::max());
596 for (i = 0; i <
len; i++) {
623 Warning(
"MakeHistograms",
"Unknown option: %c",opt[i]);
628 if (!makeX && !makeD && !makeP && !makeE && !makeS)
674 hS->
SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
699 TString::Format(
"Distance from pattern to feature space, variable %d", i),
727 if (!makeX && !makeP && !makeD && !makeS) {
750 if (makeP||makeD||makeS)
754 if (makeD || makeS) {
770 (hD[k])->Fill(
d[k],j);
814 for (j = 0; j <= i; j++)
822 for (j = 0; j <= i; j++) {
910 const char *cv_qual = isMethod ?
"" :
"static ";
912 prefix.
Form(
"%s::", classname);
914 std::ofstream outFile(
filename,std::ios::out|std::ios::trunc);
916 Error(
"MakeRealCode",
"couldn't open output file '%s'",
filename);
920 std::cout <<
"Writing on file \"" <<
filename <<
"\" ... " << std::flush;
925 outFile <<
"// -*- mode: c++ -*-" << std::endl;
927 outFile <<
"// " << std::endl
929 <<
" generated by TPrincipal::MakeCode" << std::endl;
932 outFile <<
"// on " << date.
AsString() << std::endl;
934 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
935 << std::endl <<
"//" << std::endl;
937 outFile <<
"// This file contains the functions " << std::endl
939 <<
"// void " << prefix
940 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
941 <<
"// void " << prefix
942 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);"
943 << std::endl <<
"//" << std::endl
944 <<
"// The first for transforming original data x in " << std::endl
945 <<
"// pattern space, to principal components p in " << std::endl
946 <<
"// feature space. The second function is for the" << std::endl
947 <<
"// inverse transformation, but using only nTest" << std::endl
948 <<
"// of the principal components in the expansion" << std::endl
949 <<
"// " << std::endl
950 <<
"// See TPrincipal class documentation for more "
951 <<
"information " << std::endl <<
"// " << std::endl;
955 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
958 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
967 outFile <<
"//" << std::endl
968 <<
"// Static data variables" << std::endl
969 <<
"//" << std::endl;
970 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = "
977 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
978 <<
"// Elements are stored row-wise, that is" << std::endl
979 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
980 <<
"// where i and j are zero-based" << std::endl;
981 outFile << cv_qual <<
"Double_t " << prefix
982 <<
"gEigenVectors[] = {" << std::flush;
987 outFile << (
index != 0 ?
"," :
"" ) << std::endl
991 outFile <<
"};" << std::endl << std::endl;
994 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
995 outFile << cv_qual <<
"Double_t " << prefix
996 <<
"gEigenValues[] = {" << std::flush;
998 outFile << (i != 0 ?
"," :
"") << std::endl
1000 outFile << std::endl <<
"};" << std::endl << std::endl;
1003 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
1004 outFile << cv_qual <<
"Double_t " << prefix
1005 <<
"gMeanValues[] = {" << std::flush;
1007 outFile << (i != 0 ?
"," :
"") << std::endl
1009 outFile << std::endl <<
"};" << std::endl << std::endl;
1012 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
1013 outFile << cv_qual <<
"Double_t " << prefix
1014 <<
"gSigmaValues[] = {" << std::flush;
1016 outFile << (i != 0 ?
"," :
"") << std::endl
1019 outFile << std::endl <<
"};" << std::endl << std::endl;
1026 outFile <<
"// " << std::endl
1028 << (isMethod ?
"method " :
"function ")
1029 <<
" void " << prefix
1030 <<
"X2P(Double_t *x, Double_t *p)"
1031 << std::endl <<
"// " << std::endl;
1032 outFile <<
"void " << prefix
1033 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1034 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1035 <<
" p[i] = 0;" << std::endl
1036 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1037 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1038 <<
" * gEigenVectors[j * gNVariables + i] "
1039 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }"
1040 << std::endl <<
"}" << std::endl << std::endl;
1044 outFile <<
"// " << std::endl <<
"// The "
1045 << (isMethod ?
"method " :
"function ")
1046 <<
" void " << prefix
1047 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)"
1048 << std::endl <<
"// " << std::endl;
1049 outFile <<
"void " << prefix
1050 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1051 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1052 <<
" x[i] = gMeanValues[i];" << std::endl
1053 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1054 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1055 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1056 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1059 outFile <<
"// EOF for " <<
filename << std::endl;
1064 std::cout <<
"done" << std::endl;
1077 for (
Int_t j = 0; j < nTest; j++)
1120 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1125 if (printM||printS||printE) {
1126 std::cout <<
" Variable # " << std::flush;
1128 std::cout <<
"| Mean Value " << std::flush;
1130 std::cout <<
"| Sigma " << std::flush;
1132 std::cout <<
"| Eigenvalue" << std::flush;
1133 std::cout << std::endl;
1135 std::cout <<
"-------------" << std::flush;
1137 std::cout <<
"+------------" << std::flush;
1139 std::cout <<
"+------------" << std::flush;
1141 std::cout <<
"+------------" << std::flush;
1142 std::cout << std::endl;
1145 std::cout << std::setw(12) << i <<
" " << std::flush;
1147 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1150 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1151 <<
fSigmas(i) <<
" " << std::flush;
1153 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1155 std::cout << std::endl;
1157 std::cout << std::endl;
1162 std::cout <<
"Eigenvector # " << i << std::flush;
1195 s[i] += (
x[j] - xp[j])*(
x[j] - xp[j]);
1211 TH1 *pca_s =
nullptr;
1214 Warning(
"Test",
"Couldn't get histogram of square residuals");
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Boolean (0=false, 1=true) (bool)
int Int_t
Signed integer 4 bytes (int)
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
const char Option_t
Option string (const char)
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
TMatrixTSym< Double_t > TMatrixDSym
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
TVectorT< Double_t > TVectorD
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)
TH1 is the base class of all histogram classes in ROOT.
virtual void SetXTitle(const char *title)
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
void Draw(Option_t *option="") override
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)
const TVectorD & GetEigenValues() const
const TMatrixD & GetEigenVectors() const
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
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.
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.
Double_t fTrace
Trace of covarience matrix.
TPrincipal(const TPrincipal &)
Copy constructor.
TPrincipal()
Empty constructor. Do not use.
void Clear(Option_t *option="") override
Clear the data in Object.
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
Make histograms of the result of the analysis.
void Print(Option_t *opt="MSE") const override
Print the statistics Options are.
TVectorD fUserData
Vector of original data points.
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
Covariance matrix.
Int_t fNumberOfVariables
Number of variables.
TVectorD fSigmas
vector of sigmas
TVectorD fOffDiagonal
Elements of the tridiagonal.
TVectorD fEigenValues
Eigenvalue vector of trans.
TVectorD fMeanValues
Mean value over all data points.
TList * fHistograms
List of histograms.
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...
Bool_t fStoreData
Should we store input data?
TMatrixD fEigenVectors
Eigenvector matrix of trans.
~TPrincipal() override
Destructor.
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,...
void Browse(TBrowser *b) override
Browse the TPrincipal object in the TBrowser.
Int_t fNumberOfDataPoints
Number of data points.
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.
const Double_t * GetRow(Long64_t row)
Return a row of the user supplied data.
void Test(Option_t *option="")
Test the PCA, bye calculating the sum square of residuals (see method SumOfSquareResiduals),...
Bool_t fIsNormalised
Normalize matrix?
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
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.