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!!!");
266 if (nVariables > std::numeric_limits<Int_t>::max()) {
267 Error(
"TPrincipal",
"`nVariables` input parameter %lld is larger than the allowed maximum %d", nVariables, std::numeric_limits<Int_t>::max());
278 while (opt && strlen(opt) > 0) {
294 Error(
"TPrincipal",
"Couldn't create vector mean values");
296 Error(
"TPrincipal",
"Couldn't create vector sigmas");
298 Error(
"TPrincipal",
"Couldn't create covariance matrix");
300 Error(
"TPrincipal",
"Couldn't create eigenvector matrix");
302 Error(
"TPrincipal",
"Couldn't create eigenvalue vector");
304 Error(
"TPrincipal",
"Couldn't create offdiagonal vector");
309 Error(
"TPrincipal",
"Couldn't create user data vector");
318 fNumberOfDataPoints(pr.fNumberOfDataPoints),
319 fNumberOfVariables(pr.fNumberOfVariables),
320 fMeanValues(pr.fMeanValues),
322 fCovarianceMatrix(pr.fCovarianceMatrix),
323 fEigenVectors(pr.fEigenVectors),
324 fEigenValues(pr.fEigenValues),
325 fOffDiagonal(pr.fOffDiagonal),
326 fUserData(pr.fUserData),
328 fHistograms(pr.fHistograms),
329 fIsNormalised(pr.fIsNormalised),
330 fStoreData(pr.fStoreData)
419 Error(
"AddRow",
"`fNumberOfDataPoints` has reached its allowed maximum %d, cannot add new row.",
fNumberOfDataPoints);
439 meanValues[i] *= cor;
440 meanValues[i] +=
p[i] * invnp;
441 const Double_t t1 = (
p[i] - meanValues[i]) * invnpM1;
444 for (j = 0; j < i + 1; j++) {
446 covMatrix[
index] *= cor;
447 covMatrix[
index] +=
t1 * (
p[j] - meanValues[j]);
476 while ((
h = (
TH1*)next()))
484 b->Add(&
fSigmas,
"Sigma value vector");
530 if (
index > std::numeric_limits<Int_t>::max()) {
531 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());
597 for (i = 0; i <
len; i++) {
624 Warning(
"MakeHistograms",
"Unknown option: %c",opt[i]);
629 if (!makeX && !makeD && !makeP && !makeE && !makeS)
675 hS->SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
700 TString::Format(
"Distance from pattern to feature space, variable %d", i),
728 if (!makeX && !makeP && !makeD && !makeS) {
751 if (makeP||makeD||makeS)
755 if (makeD || makeS) {
767 hS->Fill(j,
d[k]*
d[k]);
771 (hD[k])->Fill(
d[k],j);
815 for (j = 0; j <= i; j++)
823 for (j = 0; j <= i; j++) {
911 const char *cv_qual = isMethod ?
"" :
"static ";
913 prefix.
Form(
"%s::", classname);
915 std::ofstream outFile(
filename,std::ios::out|std::ios::trunc);
917 Error(
"MakeRealCode",
"couldn't open output file '%s'",
filename);
921 std::cout <<
"Writing on file \"" <<
filename <<
"\" ... " << std::flush;
926 outFile <<
"// -*- mode: c++ -*-" << std::endl;
928 outFile <<
"// " << std::endl
930 <<
" generated by TPrincipal::MakeCode" << std::endl;
933 outFile <<
"// on " << date.
AsString() << std::endl;
935 outFile <<
"// ROOT version " <<
gROOT->GetVersion()
936 << std::endl <<
"//" << std::endl;
938 outFile <<
"// This file contains the functions " << std::endl
940 <<
"// void " << prefix
941 <<
"X2P(Double_t *x, Double_t *p); " << std::endl
942 <<
"// void " << prefix
943 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest);"
944 << std::endl <<
"//" << std::endl
945 <<
"// The first for transforming original data x in " << std::endl
946 <<
"// pattern space, to principal components p in " << std::endl
947 <<
"// feature space. The second function is for the" << std::endl
948 <<
"// inverse transformation, but using only nTest" << std::endl
949 <<
"// of the principal components in the expansion" << std::endl
950 <<
"// " << std::endl
951 <<
"// See TPrincipal class documentation for more "
952 <<
"information " << std::endl <<
"// " << std::endl;
956 outFile <<
"#include \"" << classname <<
".h\"" << std::endl;
959 outFile <<
"#include <Rtypes.h> // needed for Double_t etc" << std::endl;
968 outFile <<
"//" << std::endl
969 <<
"// Static data variables" << std::endl
970 <<
"//" << std::endl;
971 outFile << cv_qual <<
"Int_t " << prefix <<
"gNVariables = "
978 outFile << std::endl <<
"// Assignment of eigenvector matrix." << std::endl
979 <<
"// Elements are stored row-wise, that is" << std::endl
980 <<
"// M[i][j] = e[i * nVariables + j] " << std::endl
981 <<
"// where i and j are zero-based" << std::endl;
982 outFile << cv_qual <<
"Double_t " << prefix
983 <<
"gEigenVectors[] = {" << std::flush;
988 outFile << (
index != 0 ?
"," :
"" ) << std::endl
992 outFile <<
"};" << std::endl << std::endl;
995 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
996 outFile << cv_qual <<
"Double_t " << prefix
997 <<
"gEigenValues[] = {" << std::flush;
999 outFile << (i != 0 ?
"," :
"") << std::endl
1001 outFile << std::endl <<
"};" << std::endl << std::endl;
1004 outFile <<
"// Assignment to mean value vector. Zero-based." << std::endl;
1005 outFile << cv_qual <<
"Double_t " << prefix
1006 <<
"gMeanValues[] = {" << std::flush;
1008 outFile << (i != 0 ?
"," :
"") << std::endl
1010 outFile << std::endl <<
"};" << std::endl << std::endl;
1013 outFile <<
"// Assignment to sigma value vector. Zero-based." << std::endl;
1014 outFile << cv_qual <<
"Double_t " << prefix
1015 <<
"gSigmaValues[] = {" << std::flush;
1017 outFile << (i != 0 ?
"," :
"") << std::endl
1020 outFile << std::endl <<
"};" << std::endl << std::endl;
1027 outFile <<
"// " << std::endl
1029 << (isMethod ?
"method " :
"function ")
1030 <<
" void " << prefix
1031 <<
"X2P(Double_t *x, Double_t *p)"
1032 << std::endl <<
"// " << std::endl;
1033 outFile <<
"void " << prefix
1034 <<
"X2P(Double_t *x, Double_t *p) {" << std::endl
1035 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1036 <<
" p[i] = 0;" << std::endl
1037 <<
" for (Int_t j = 0; j < gNVariables; j++)" << std::endl
1038 <<
" p[i] += (x[j] - gMeanValues[j]) " << std::endl
1039 <<
" * gEigenVectors[j * gNVariables + i] "
1040 <<
"/ gSigmaValues[j];" << std::endl << std::endl <<
" }"
1041 << std::endl <<
"}" << std::endl << std::endl;
1045 outFile <<
"// " << std::endl <<
"// The "
1046 << (isMethod ?
"method " :
"function ")
1047 <<
" void " << prefix
1048 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest)"
1049 << std::endl <<
"// " << std::endl;
1050 outFile <<
"void " << prefix
1051 <<
"P2X(Double_t *p, Double_t *x, Int_t nTest) {" << std::endl
1052 <<
" for (Int_t i = 0; i < gNVariables; i++) {" << std::endl
1053 <<
" x[i] = gMeanValues[i];" << std::endl
1054 <<
" for (Int_t j = 0; j < nTest; j++)" << std::endl
1055 <<
" x[i] += p[j] * gSigmaValues[i] " << std::endl
1056 <<
" * gEigenVectors[i * gNVariables + j];" << std::endl
1057 <<
" }" << std::endl <<
"}" << std::endl << std::endl;
1060 outFile <<
"// EOF for " <<
filename << std::endl;
1065 std::cout <<
"done" << std::endl;
1078 for (
Int_t j = 0; j < nTest; j++)
1121 Warning(
"Print",
"Unknown option '%c'",opt[i]);
1126 if (printM||printS||printE) {
1127 std::cout <<
" Variable # " << std::flush;
1129 std::cout <<
"| Mean Value " << std::flush;
1131 std::cout <<
"| Sigma " << std::flush;
1133 std::cout <<
"| Eigenvalue" << std::flush;
1134 std::cout << std::endl;
1136 std::cout <<
"-------------" << std::flush;
1138 std::cout <<
"+------------" << std::flush;
1140 std::cout <<
"+------------" << std::flush;
1142 std::cout <<
"+------------" << std::flush;
1143 std::cout << std::endl;
1146 std::cout << std::setw(12) << i <<
" " << std::flush;
1148 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1151 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1152 <<
fSigmas(i) <<
" " << std::flush;
1154 std::cout <<
"| " << std::setw(10) << std::setprecision(4)
1156 std::cout << std::endl;
1158 std::cout << std::endl;
1163 std::cout <<
"Eigenvector # " << i << std::flush;
1196 s[i] += (
x[j] - xp[j])*(
x[j] - xp[j]);
1212 TH1 *pca_s =
nullptr;
1215 Warning(
"Test",
"Couldn't get histogram of square residuals");
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
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
TMatrixTColumn_const< Double_t > TMatrixDColumn_const
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
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 Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="")
Performs the operation: this = this + c1*f1 if errors are defined (see TH1::Sumw2),...
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)
2-D histogram with a float per channel (see TH1 documentation)
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
void Add(TObject *obj) override
void Delete(Option_t *option="") override
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.
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
const Element * GetMatrixArray() const override
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.
Double_t fTrace
Trace of covarience matrix.
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.
TVectorT< Element > & Zero()
Set vector elements to zero.
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
Element * GetMatrixArray()
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.