69 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
70 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
71 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
93 if (!IsEnabled() || IsCreated())
return kTRUE;
95 Log() << kINFO <<
"Preparing the Principle Component (PCA) transformation..." <<
Endl;
97 UInt_t inputSize = fGet.size();
99 SetNVariables(inputSize);
102 if (inputSize <= 1) {
103 Log() << kFATAL <<
"Cannot perform PCA transformation for " << inputSize <<
" variable only" <<
Endl;
107 if (inputSize > 200) {
108 Log() << kINFO <<
"----------------------------------------------------------------------------"
111 <<
": More than 200 variables, will not calculate PCA!" <<
Endl;
112 Log() << kINFO <<
"----------------------------------------------------------------------------"
117 CalculatePrincipalComponents( events );
129 if (!IsCreated())
return 0;
145 if (fTransformedEvent==0 ) {
146 fTransformedEvent =
new Event();
149 std::vector<Float_t>
input;
150 std::vector<Char_t>
mask;
159 Log() << kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
162 return fTransformedEvent;
168 return fTransformedEvent;
178 if (!IsCreated())
return 0;
189 if (fBackTransformedEvent==0 ) fBackTransformedEvent =
new Event();
192 std::vector<Char_t>
mask;
193 std::vector<Float_t>
output;
199 return fBackTransformedEvent;
210 if( nvars>0 &&
ntgts>0 )
211 Log() << kFATAL <<
"Variables and targets cannot be mixed in PCA transformation." <<
Endl;
213 const Int_t inputSize = fGet.size();
229 std::vector<Float_t>
input;
230 std::vector<Char_t>
mask;
237 Log() << kWARNING <<
"Print event which triggers an error" <<
Endl;
238 std::ostringstream
oss;
241 Log() << kFATAL <<
"Masked entries found in event read in when calculating the principal components for the PCA transformation." <<
Endl;
257 for (
UInt_t i=0; i<fMeanValues.size(); i++)
if (fMeanValues[i] != 0)
delete fMeanValues[i];
258 for (
UInt_t i=0; i<fEigenVectors.size(); i++)
if (fEigenVectors[i] != 0)
delete fEigenVectors[i];
259 fMeanValues.resize(
maxPCA,0);
260 fEigenVectors.resize(
maxPCA,0);
263 pca.at(i)->MakePrincipals();
266 fMeanValues[i] =
new TVectorD( *(
pca.at(i)->GetMeanValues()) );
267 fEigenVectors[i] =
new TMatrixD( *(
pca.at(i)->GetEigenVectors()) );
318 o <<
"# PCA mean values " << std::endl;
320 o << (
sbType==0 ?
"Signal" :
"Background") <<
" " <<
means->GetNrows() << std::endl;
321 for (
Int_t row = 0; row<
means->GetNrows(); row++) {
322 o << std::setprecision(12) << std::setw(20) << (*means)[row];
326 o <<
"##" << std::endl;
330 o <<
"# PCA eigenvectors " << std::endl;
332 o << (
sbType==0 ?
"Signal" :
"Background") <<
" " <<
mat->GetNrows() <<
" x " <<
mat->GetNcols() << std::endl;
333 for (
Int_t row = 0; row<
mat->GetNrows(); row++) {
334 for (
Int_t col = 0; col<
mat->GetNcols(); col++) {
335 o << std::setprecision(12) << std::setw(20) << (*mat)[row][col] <<
" ";
340 o <<
"##" << std::endl;
360 for (
Int_t row = 0; row<
means->GetNrows(); row++)
374 for (
Int_t row = 0; row<
mat->GetNrows(); row++)
375 for (
Int_t col = 0; col<
mat->GetNcols(); col++)
416 if (fMeanValues.size()<=
clsIdx) fMeanValues.resize(
clsIdx+1,0);
421 std::stringstream s(
gTools().GetContent(ch));
424 else if (
nodeName ==
"Eigenvectors" ) {
431 if (fEigenVectors.size()<=
clsIdx) fEigenVectors.resize(
clsIdx+1,0);
436 std::stringstream s(
gTools().GetContent(ch));
439 s >> (*fEigenVectors[
clsIdx])[row][col];
453 istr.getline(buf,512);
458 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
459 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
460 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
462 fMeanValues.resize(3);
463 fEigenVectors.resize(3);
465 Log() << kINFO <<
"VariablePCATransform::ReadTransformationFromStream(): " <<
Endl;
467 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
469 while (*
p==
' ' || *
p==
'\t')
p++;
470 if (*
p==
'#' || *
p==
'\0') {
471 istr.getline(buf,512);
474 std::stringstream
sstr(buf);
489 istr.getline(buf,512);
493 istr.getline(buf,512);
494 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
496 while(*
p==
' ' || *
p==
'\t')
p++;
497 if (*
p==
'#' || *
p==
'\0') {
498 istr.getline(buf,512);
501 std::stringstream
sstr(buf);
513 for (
Int_t row = 0; row<fEigenVectors[
sbType]->GetNrows(); row++) {
514 for (
Int_t col = 0; col<fEigenVectors[
sbType]->GetNcols(); col++) {
520 istr.getline(buf,512);
534 UInt_t nvar = fEigenVectors[0]->GetNrows();
540 fout <<
" void X2P_"<<
trCounter<<
"( const double*, double*, int ) const;" << std::endl;
542 << fMeanValues[0]->GetNrows() <<
"];" << std::endl;
544 << fEigenVectors[0]->GetNrows() <<
"]["
545 << fEigenVectors[0]->GetNcols() <<
"];" << std::endl;
551 if (fMeanValues[0]->GetNrows() != fMeanValues[1]->GetNrows() ||
552 fEigenVectors[0]->GetNrows() != fEigenVectors[1]->GetNrows() ||
553 fEigenVectors[0]->GetNcols() != fEigenVectors[1]->GetNcols()) {
554 Log() << kFATAL <<
"<MakeFunction> Mismatch in vector/matrix dimensions" <<
Endl;
561 fout <<
"//_______________________________________________________________________" << std::endl;
562 fout <<
"inline void " <<
fcncName <<
"::X2P_"<<
trCounter<<
"( const double* x, double* p, int index ) const" << std::endl;
563 fout <<
"{" << std::endl;
564 fout <<
" // Calculate the principal components from the original data vector" << std::endl;
565 fout <<
" // x, and return it in p (function extracted from TPrincipal::X2P)" << std::endl;
566 fout <<
" // It's the users responsibility to make sure that both x and p are" << std::endl;
567 fout <<
" // of the right size (i.e., memory must be allocated for p)." << std::endl;
568 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
570 fout <<
" for (int i = 0; i < nVar; i++) {" << std::endl;
571 fout <<
" p[i] = 0;" << std::endl;
572 fout <<
" for (int j = 0; j < nVar; j++) p[i] += (x[j] - fMeanValues_"<<
trCounter<<
"[index][j]) * fEigenVectors_"<<
trCounter<<
"[index][j][i];" << std::endl;
573 fout <<
" }" << std::endl;
574 fout <<
"}" << std::endl;
576 fout <<
"//_______________________________________________________________________" << std::endl;
578 fout <<
"{" << std::endl;
579 fout <<
" // PCA transformation, initialisation" << std::endl;
582 fout <<
" // initialise vector of mean values" << std::endl;
583 std::streamsize
dp =
fout.precision();
585 for (
int i=0; i<fMeanValues[
index]->GetNrows(); i++) {
586 fout <<
" fMeanValues_"<<
trCounter<<
"["<<
index<<
"]["<<i<<
"] = " << std::setprecision(12)
587 << (*fMeanValues[
index])(i) <<
";" << std::endl;
593 fout <<
" // initialise matrix of eigenvectors" << std::endl;
595 for (
int i=0; i<fEigenVectors[
index]->GetNrows(); i++) {
596 for (
int j=0;
j<fEigenVectors[
index]->GetNcols();
j++) {
597 fout <<
" fEigenVectors_"<<
trCounter<<
"["<<
index<<
"]["<<i<<
"]["<<
j<<
"] = " << std::setprecision(12)
598 << (*fEigenVectors[
index])(i,
j) <<
";" << std::endl;
602 fout << std::setprecision(
dp);
603 fout <<
"}" << std::endl;
605 fout <<
"//_______________________________________________________________________" << std::endl;
606 fout <<
"inline void " <<
fcncName <<
"::Transform_"<<
trCounter<<
"( std::vector<double>& iv, int cls ) const" << std::endl;
607 fout <<
"{" << std::endl;
608 fout <<
" // PCA transformation" << std::endl;
609 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
610 fout <<
" double *dv = new double[nVar];" << std::endl;
611 fout <<
" double *rv = new double[nVar];" << std::endl;
612 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
613 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
614 fout <<
" else cls = "<<(
numC==1?0:2)<<
";"<< std::endl;
615 fout <<
" }"<< std::endl;
619 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
622 fout <<
" // Perform PCA and put it into PCAed events tree" << std::endl;
623 fout <<
" this->X2P_"<<
trCounter<<
"( dv, rv, cls );" << std::endl;
624 fout <<
" for (int ivar=0; ivar<nVar; ivar++) iv[indicesPut.at(ivar)] = rv[ivar];" << std::endl;
627 fout <<
" delete [] dv;" << std::endl;
628 fout <<
" delete [] rv;" << std::endl;
629 fout <<
"}" << std::endl;
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)