231 fCovarianceMatrix(1,1),
238 fHistograms =
nullptr;
240 fNumberOfDataPoints = 0;
241 fNumberOfVariables = 0;
253 : fMeanValues(nVariables),
255 fCovarianceMatrix(nVariables,nVariables),
256 fEigenVectors(nVariables,nVariables),
257 fEigenValues(nVariables),
258 fOffDiagonal(nVariables),
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());
273 fHistograms =
nullptr;
275 fNumberOfDataPoints = 0;
276 fNumberOfVariables = nVariables;
277 while (opt && strlen(opt) > 0) {
281 fIsNormalised =
kTRUE;
292 if (!fMeanValues.IsValid())
293 Error(
"TPrincipal",
"Couldn't create vector mean values");
294 if (!fSigmas.IsValid())
295 Error(
"TPrincipal",
"Couldn't create vector sigmas");
296 if (!fCovarianceMatrix.IsValid())
297 Error(
"TPrincipal",
"Couldn't create covariance matrix");
298 if (!fEigenVectors.IsValid())
299 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
300 if (!fEigenValues.IsValid())
301 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
302 if (!fOffDiagonal.IsValid())
303 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
305 fUserData.ResizeTo(nVariables*1000);
307 if (!fUserData.IsValid())
308 Error(
"TPrincipal",
"Couldn't create user data vector");
317 fNumberOfDataPoints(pr.fNumberOfDataPoints),
318 fNumberOfVariables(pr.fNumberOfVariables),
319 fMeanValues(pr.fMeanValues),
321 fCovarianceMatrix(pr.fCovarianceMatrix),
322 fEigenVectors(pr.fEigenVectors),
323 fEigenValues(pr.fEigenValues),
324 fOffDiagonal(pr.fOffDiagonal),
325 fUserData(pr.fUserData),
327 fHistograms(pr.fHistograms),
328 fIsNormalised(pr.fIsNormalised),
329 fStoreData(pr.fStoreData)
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());
563 TString outName(filename);
564 if (!outName.EndsWith(
".C") && !outName.EndsWith(
".cxx"))
594 Int_t len = opt ? strlen(opt) : 0;
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++) {
889 TMatrixDSymEigen eigen(sym);
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
928 <<
"// File " << filename
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++)
1100 Int_t len = opt ? strlen(opt) : 0;
1101 for (
Int_t i = 0; i < len; i++) {
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
int Int_t
Signed integer 4 bytes (int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
const char Option_t
Option string (const char).
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
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.
const char * AsString() const
Return the date & time as a string (ctime() format).
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.
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
The TNamed class is the base class for all named ROOT classes.
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 MakePrincipals()
Double_t fTrace
Trace of covarience matrix.
virtual void AddRow(const Double_t *x)
void Clear(Option_t *option="") override
Set name and title to empty strings ("").
void Print(Option_t *opt="MSE") const override
Print TNamed name and title.
TVectorD fUserData
Vector of original data points.
TMatrixD fCovarianceMatrix
Covariance matrix.
Int_t fNumberOfVariables
Number of variables.
TVectorD fSigmas
vector of sigmas
TVectorD fOffDiagonal
Elements of the tridiagonal.
virtual void SumOfSquareResiduals(const Double_t *x, Double_t *s)
TVectorD fEigenValues
Eigenvalue vector of trans.
TVectorD fMeanValues
Mean value over all data points.
TList * fHistograms
List of histograms.
virtual void MakeCode(const char *filename="pca", Option_t *option="")
void MakeRealCode(const char *filename, const char *prefix, Option_t *option="")
Bool_t fStoreData
Should we store input data?
TMatrixD fEigenVectors
Eigenvector matrix of trans.
virtual void MakeMethods(const char *classname="PCA", Option_t *option="")
virtual void P2X(const Double_t *p, Double_t *x, Int_t nTest)
void Browse(TBrowser *b) override
Browse object. May be overridden for another default action.
Int_t fNumberOfDataPoints
Number of data points.
virtual void MakeHistograms(const char *name="pca", Option_t *option="epsdx")
const Double_t * GetRow(Long64_t row)
void Test(Option_t *option="")
virtual void X2P(const Double_t *x, Double_t *p)
Bool_t fIsNormalised
Normalize matrix?
TPrincipal & operator=(const TPrincipal &)
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.
void Fill(float *output, float value, int size)
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.
const double xbins[xbins_n]