68 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
69 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
70 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
92 if (!IsEnabled() || IsCreated())
return kTRUE;
94 Log() << kINFO <<
"Preparing the Principle Component (PCA) transformation..." <<
Endl;
96 UInt_t inputSize = fGet.size();
98 SetNVariables(inputSize);
101 if (inputSize <= 1) {
102 Log() << kFATAL <<
"Cannot perform PCA transformation for " << inputSize <<
" variable only" <<
Endl;
106 if (inputSize > 200) {
107 Log() << kINFO <<
"----------------------------------------------------------------------------"
110 <<
": More than 200 variables, will not calculate PCA!" <<
Endl;
111 Log() << kINFO <<
"----------------------------------------------------------------------------"
116 CalculatePrincipalComponents( events );
128 if (!IsCreated())
return 0;
144 if (fTransformedEvent==0 ) {
145 fTransformedEvent =
new Event();
148 std::vector<Float_t>
input;
149 std::vector<Char_t>
mask;
158 Log() << kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
161 return fTransformedEvent;
167 return fTransformedEvent;
177 if (!IsCreated())
return 0;
188 if (fBackTransformedEvent==0 ) fBackTransformedEvent =
new Event();
191 std::vector<Char_t>
mask;
192 std::vector<Float_t>
output;
198 return fBackTransformedEvent;
209 if( nvars>0 &&
ntgts>0 )
210 Log() << kFATAL <<
"Variables and targets cannot be mixed in PCA transformation." <<
Endl;
212 const Int_t inputSize = fGet.size();
228 std::vector<Float_t>
input;
229 std::vector<Char_t>
mask;
236 Log() << kWARNING <<
"Print event which triggers an error" <<
Endl;
237 std::ostringstream
oss;
240 Log() << kFATAL <<
"Masked entries found in event read in when calculating the principal components for the PCA transformation." <<
Endl;
256 for (
UInt_t i=0; i<fMeanValues.size(); i++)
if (fMeanValues[i] != 0)
delete fMeanValues[i];
257 for (
UInt_t i=0; i<fEigenVectors.size(); i++)
if (fEigenVectors[i] != 0)
delete fEigenVectors[i];
258 fMeanValues.resize(
maxPCA,0);
259 fEigenVectors.resize(
maxPCA,0);
262 pca.at(i)->MakePrincipals();
265 fMeanValues[i] =
new TVectorD( *(
pca.at(i)->GetMeanValues()) );
266 fEigenVectors[i] =
new TMatrixD( *(
pca.at(i)->GetEigenVectors()) );
317 o <<
"# PCA mean values " << std::endl;
319 o << (
sbType==0 ?
"Signal" :
"Background") <<
" " <<
means->GetNrows() << std::endl;
320 for (
Int_t row = 0; row<
means->GetNrows(); row++) {
321 o << std::setprecision(12) << std::setw(20) << (*means)[row];
325 o <<
"##" << std::endl;
329 o <<
"# PCA eigenvectors " << std::endl;
331 o << (
sbType==0 ?
"Signal" :
"Background") <<
" " <<
mat->GetNrows() <<
" x " <<
mat->GetNcols() << std::endl;
332 for (
Int_t row = 0; row<
mat->GetNrows(); row++) {
333 for (
Int_t col = 0; col<
mat->GetNcols(); col++) {
334 o << std::setprecision(12) << std::setw(20) << (*mat)[row][col] <<
" ";
339 o <<
"##" << std::endl;
359 for (
Int_t row = 0; row<
means->GetNrows(); row++)
373 for (
Int_t row = 0; row<
mat->GetNrows(); row++)
374 for (
Int_t col = 0; col<
mat->GetNcols(); col++)
415 if (fMeanValues.size()<=
clsIdx) fMeanValues.resize(
clsIdx+1,0);
420 std::stringstream s(
gTools().GetContent(ch));
423 else if (
nodeName ==
"Eigenvectors" ) {
430 if (fEigenVectors.size()<=
clsIdx) fEigenVectors.resize(
clsIdx+1,0);
435 std::stringstream s(
gTools().GetContent(ch));
438 s >> (*fEigenVectors[
clsIdx])[row][col];
452 istr.getline(buf,512);
457 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
458 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
459 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
461 fMeanValues.resize(3);
462 fEigenVectors.resize(3);
464 Log() << kINFO <<
"VariablePCATransform::ReadTransformationFromStream(): " <<
Endl;
466 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
468 while (*
p==
' ' || *
p==
'\t')
p++;
469 if (*
p==
'#' || *
p==
'\0') {
470 istr.getline(buf,512);
473 std::stringstream
sstr(buf);
488 istr.getline(buf,512);
492 istr.getline(buf,512);
493 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
495 while(*
p==
' ' || *
p==
'\t')
p++;
496 if (*
p==
'#' || *
p==
'\0') {
497 istr.getline(buf,512);
500 std::stringstream
sstr(buf);
512 for (
Int_t row = 0; row<fEigenVectors[
sbType]->GetNrows(); row++) {
513 for (
Int_t col = 0; col<fEigenVectors[
sbType]->GetNcols(); col++) {
519 istr.getline(buf,512);
533 UInt_t nvar = fEigenVectors[0]->GetNrows();
539 fout <<
" void X2P_"<<
trCounter<<
"( const double*, double*, int ) const;" << std::endl;
541 << fMeanValues[0]->GetNrows() <<
"];" << std::endl;
543 << fEigenVectors[0]->GetNrows() <<
"]["
544 << fEigenVectors[0]->GetNcols() <<
"];" << std::endl;
550 if (fMeanValues[0]->GetNrows() != fMeanValues[1]->GetNrows() ||
551 fEigenVectors[0]->GetNrows() != fEigenVectors[1]->GetNrows() ||
552 fEigenVectors[0]->GetNcols() != fEigenVectors[1]->GetNcols()) {
553 Log() << kFATAL <<
"<MakeFunction> Mismatch in vector/matrix dimensions" <<
Endl;
560 fout <<
"//_______________________________________________________________________" << std::endl;
561 fout <<
"inline void " <<
fcncName <<
"::X2P_"<<
trCounter<<
"( const double* x, double* p, int index ) const" << std::endl;
562 fout <<
"{" << std::endl;
563 fout <<
" // Calculate the principal components from the original data vector" << std::endl;
564 fout <<
" // x, and return it in p (function extracted from TPrincipal::X2P)" << std::endl;
565 fout <<
" // It's the users responsibility to make sure that both x and p are" << std::endl;
566 fout <<
" // of the right size (i.e., memory must be allocated for p)." << std::endl;
567 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
569 fout <<
" for (int i = 0; i < nVar; i++) {" << std::endl;
570 fout <<
" p[i] = 0;" << std::endl;
571 fout <<
" for (int j = 0; j < nVar; j++) p[i] += (x[j] - fMeanValues_"<<
trCounter<<
"[index][j]) * fEigenVectors_"<<
trCounter<<
"[index][j][i];" << std::endl;
572 fout <<
" }" << std::endl;
573 fout <<
"}" << std::endl;
575 fout <<
"//_______________________________________________________________________" << std::endl;
577 fout <<
"{" << std::endl;
578 fout <<
" // PCA transformation, initialisation" << std::endl;
581 fout <<
" // initialise vector of mean values" << std::endl;
582 std::streamsize
dp =
fout.precision();
584 for (
int i=0; i<fMeanValues[
index]->GetNrows(); i++) {
585 fout <<
" fMeanValues_"<<
trCounter<<
"["<<
index<<
"]["<<i<<
"] = " << std::setprecision(12)
586 << (*fMeanValues[
index])(i) <<
";" << std::endl;
592 fout <<
" // initialise matrix of eigenvectors" << std::endl;
594 for (
int i=0; i<fEigenVectors[
index]->GetNrows(); i++) {
595 for (
int j=0;
j<fEigenVectors[
index]->GetNcols();
j++) {
596 fout <<
" fEigenVectors_"<<
trCounter<<
"["<<
index<<
"]["<<i<<
"]["<<
j<<
"] = " << std::setprecision(12)
597 << (*fEigenVectors[
index])(i,
j) <<
";" << std::endl;
601 fout << std::setprecision(
dp);
602 fout <<
"}" << std::endl;
604 fout <<
"//_______________________________________________________________________" << std::endl;
605 fout <<
"inline void " <<
fcncName <<
"::Transform_"<<
trCounter<<
"( std::vector<double>& iv, int cls ) const" << std::endl;
606 fout <<
"{" << std::endl;
607 fout <<
" // PCA transformation" << std::endl;
608 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
609 fout <<
" double *dv = new double[nVar];" << std::endl;
610 fout <<
" double *rv = new double[nVar];" << std::endl;
611 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
612 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
613 fout <<
" else cls = "<<(
numC==1?0:2)<<
";"<< std::endl;
614 fout <<
" }"<< std::endl;
618 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
621 fout <<
" // Perform PCA and put it into PCAed events tree" << std::endl;
622 fout <<
" this->X2P_"<<
trCounter<<
"( dv, rv, cls );" << std::endl;
623 fout <<
" for (int ivar=0; ivar<nVar; ivar++) iv[indicesPut.at(ivar)] = rv[ivar];" << std::endl;
626 fout <<
" delete [] dv;" << std::endl;
627 fout <<
" delete [] rv;" << std::endl;
628 fout <<
"}" << std::endl;
char Char_t
Character 1 byte (char)
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
float Float_t
Float 4 bytes (float)
double Double_t
Double 8 bytes.
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
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 mask
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 value
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
Class that contains all the data information.
Singleton class for Global types used by TMVA.
Principal Components Analysis (PCA)
MsgLogger & Endl(MsgLogger &ml)