231 fCovarianceMatrix(1,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");
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);
443 for (
j = 0;
j < i + 1;
j++) {
475 while ((
h = (
TH1*)next()))
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]);
666 hE->SetXTitle(
"Eigenvalue");
674 hS->SetYTitle(
"#sum_{i=1}^{M} (x_{i} - x'_{N,i})^{2}");
689 hX[i]->SetXTitle(
Form(
"x_{%d}",i));
699 TString::Format(
"Distance from pattern to feature space, variable %d", i),
703 hD[i]->SetYTitle(
"N");
766 hS->Fill(
j,
d[k]*
d[k]);
814 for (
j = 0;
j <= i;
j++)
822 for (
j = 0;
j <= i;
j++) {
912 prefix.
Form(
"%s::", classname);
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;
929 <<
" generated by TPrincipal::MakeCode" << std::endl;
932 outFile <<
"// on " <<
date.AsString() << std::endl;
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;
968 <<
"// Static data variables" << std::endl
969 <<
"//" << std::endl;
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;
982 <<
"gEigenVectors[] = {" << std::flush;
991 outFile <<
"};" << std::endl << std::endl;
994 outFile <<
"// Assignment to eigen value vector. Zero-based." << std::endl;
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;
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;
1014 <<
"gSigmaValues[] = {" << std::flush;
1016 outFile << (i != 0 ?
"," :
"") << std::endl
1019 outFile << std::endl <<
"};" << std::endl << std::endl;
1028 << (
isMethod ?
"method " :
"function ")
1029 <<
" void " << prefix
1030 <<
"X2P(Double_t *x, Double_t *p)"
1031 << std::endl <<
"// " << std::endl;
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;
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;
1064 std::cout <<
"done" << std::endl;
1120 Warning(
"Print",
"Unknown option '%c'",opt[i]);
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;
1214 Warning(
"Test",
"Couldn't get histogram of square residuals");
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
const char Option_t
Option string (const char)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
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
Int_t Fill(Double_t x) override
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...
1-D histogram with a float per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
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),...
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.
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.
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.