42 #ifndef ROOT_TMVA_MsgLogger 89 Log() <<
kINFO <<
"Preparing the Principle Component (PCA) transformation..." <<
Endl;
97 Log() <<
kFATAL <<
"Cannot perform PCA transformation for " << inputSize <<
" variable only" <<
Endl;
101 if (inputSize > 200) {
102 Log() <<
kINFO <<
"----------------------------------------------------------------------------" 105 <<
": More than 200 variables, will not calculate PCA!" <<
Endl;
106 Log() <<
kINFO <<
"----------------------------------------------------------------------------" 143 std::vector<Float_t> input;
144 std::vector<Char_t> mask;
145 std::vector<Float_t> principalComponents;
149 if( hasMaskedEntries ){
152 if( numMasked>0 && numOK>0 ){
153 Log() <<
kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
159 X2P( principalComponents, input, cls );
185 std::vector<Float_t> principalComponents;
186 std::vector<Char_t> mask;
187 std::vector<Float_t>
output;
190 P2X( output, principalComponents, cls );
202 UInt_t nvars = 0, ntgts = 0, nspcts = 0;
204 if( nvars>0 && ntgts>0 )
205 Log() <<
kFATAL <<
"Variables and targets cannot be mixed in PCA transformation." <<
Endl;
211 const UInt_t maxPCA = (nCls<=1) ? nCls : nCls+1;
214 std::vector<TPrincipal*> pca(maxPCA);
220 Long64_t ievt, entries = events.size();
223 std::vector<Float_t> input;
224 std::vector<Char_t> mask;
225 for (ievt=0; ievt<entries; ievt++) {
226 const Event* ev = events[ievt];
230 if (hasMaskedEntries){
233 Log() <<
kFATAL <<
"Masked entries found in event read in when calculating the principal components for the PCA transformation." <<
Endl;
237 for( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp )
244 pca.at(cls)->AddRow( dvec );
245 if (nCls > 1) pca.at(maxPCA-1)->AddRow( dvec );
254 for (
UInt_t i=0; i<maxPCA; i++ ) {
255 pca.at(i)->MakePrincipals();
262 for (
UInt_t i=0; i<maxPCA; i++)
delete pca.at(i);
274 const Int_t nInput = x.size();
277 for (
Int_t i = 0; i < nInput; i++) {
279 for (
Int_t j = 0; j < nInput; j++)
293 const Int_t nInput = pc.size();
296 for (
Int_t i = 0; i < nInput; i++) {
298 for (
Int_t j = 0; j < nInput; j++)
309 for (
Int_t sbType=0; sbType<2; sbType++) {
310 o <<
"# PCA mean values " << std::endl;
312 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << means->
GetNrows() << std::endl;
314 o << std::setprecision(12) << std::setw(20) << (*means)[row];
318 o <<
"##" << std::endl;
321 for (
Int_t sbType=0; sbType<2; sbType++) {
322 o <<
"# PCA eigenvectors " << std::endl;
324 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << mat->
GetNrows() <<
" x " << mat->
GetNcols() << std::endl;
327 o << std::setprecision(12) << std::setw(20) << (*mat)[row][col] <<
" ";
332 o <<
"##" << std::endl;
348 gTools().
AddAttr( meanxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined")) );
361 gTools().
AddAttr( evxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined") ) );
385 void* inpnode =
NULL;
401 if (nodeName ==
"Statistics") {
413 std::stringstream s(
gTools().GetContent(ch));
416 else if ( nodeName ==
"Eigenvectors" ) {
428 std::stringstream s(
gTools().GetContent(ch));
429 for (
Int_t row = 0; row<nrows; row++)
430 for (
Int_t col = 0; col<ncols; col++)
445 istr.getline(buf,512);
447 Int_t nrows(0), ncols(0);
448 UInt_t classIdx=(classname==
"signal"?0:1);
457 Log() <<
kINFO <<
"VariablePCATransform::ReadTransformationFromStream(): " <<
Endl;
459 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
461 while (*p==
' ' || *p==
'\t') p++;
462 if (*p==
'#' || *p==
'\0') {
463 istr.getline(buf,512);
466 std::stringstream sstr(buf);
468 if (strvar==
"signal" || strvar==
"background") {
471 Int_t sbType = (strvar==
"signal" ? 0 : 1);
481 istr.getline(buf,512);
485 istr.getline(buf,512);
486 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
488 while(*p==
' ' || *p==
'\t') p++;
489 if (*p==
'#' || *p==
'\0') {
490 istr.getline(buf,512);
493 std::stringstream sstr(buf);
495 if (strvar==
"signal" || strvar==
"background") {
498 sstr >> nrows >> dummy >> ncols;
499 Int_t sbType = (strvar==
"signal" ? 0 : 1);
512 istr.getline(buf,512);
532 fout <<
" void X2P_"<<trCounter<<
"( const double*, double*, int ) const;" << std::endl;
533 fout <<
" double fMeanValues_"<<trCounter<<
"["<<numC<<
"][" 534 <<
fMeanValues[0]->GetNrows() <<
"];" << std::endl;
535 fout <<
" double fEigenVectors_"<<trCounter<<
"["<<numC<<
"][" 546 Log() <<
kFATAL <<
"<MakeFunction> Mismatch in vector/matrix dimensions" <<
Endl;
553 fout <<
"//_______________________________________________________________________" << std::endl;
554 fout <<
"inline void " << fcncName <<
"::X2P_"<<trCounter<<
"( const double* x, double* p, int index ) const" << std::endl;
555 fout <<
"{" << std::endl;
556 fout <<
" // Calculate the principal components from the original data vector" << std::endl;
557 fout <<
" // x, and return it in p (function extracted from TPrincipal::X2P)" << std::endl;
558 fout <<
" // It's the users responsibility to make sure that both x and p are" << std::endl;
559 fout <<
" // of the right size (i.e., memory must be allocated for p)." << std::endl;
560 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
562 fout <<
" for (int i = 0; i < nVar; i++) {" << std::endl;
563 fout <<
" p[i] = 0;" << std::endl;
564 fout <<
" for (int j = 0; j < nVar; j++) p[i] += (x[j] - fMeanValues_"<<trCounter<<
"[index][j]) * fEigenVectors_"<<trCounter<<
"[index][j][i];" << std::endl;
565 fout <<
" }" << std::endl;
566 fout <<
"}" << std::endl;
568 fout <<
"//_______________________________________________________________________" << std::endl;
569 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
570 fout <<
"{" << std::endl;
571 fout <<
" // PCA transformation, initialisation" << std::endl;
574 fout <<
" // initialise vector of mean values" << std::endl;
575 std::streamsize dp = fout.precision();
576 for (
UInt_t index=0; index<numC; index++) {
577 for (
int i=0; i<
fMeanValues[index]->GetNrows(); i++) {
578 fout <<
" fMeanValues_"<<trCounter<<
"["<<index<<
"]["<<i<<
"] = " << std::setprecision(12)
585 fout <<
" // initialise matrix of eigenvectors" << std::endl;
586 for (
UInt_t index=0; index<numC; index++) {
589 fout <<
" fEigenVectors_"<<trCounter<<
"["<<index<<
"]["<<i<<
"]["<<j<<
"] = " << std::setprecision(12)
594 fout << std::setprecision(dp);
595 fout <<
"}" << std::endl;
597 fout <<
"//_______________________________________________________________________" << std::endl;
598 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int cls ) const" << std::endl;
599 fout <<
"{" << std::endl;
600 fout <<
" // PCA transformation" << std::endl;
601 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
602 fout <<
" double *dv = new double[nVar];" << std::endl;
603 fout <<
" double *rv = new double[nVar];" << std::endl;
604 fout <<
" if (cls < 0 || cls > "<<
GetNClasses()<<
") {"<< std::endl;
606 fout <<
" else cls = "<<(numC==1?0:2)<<
";"<< std::endl;
607 fout <<
" }"<< std::endl;
611 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
614 fout <<
" // Perform PCA and put it into PCAed events tree" << std::endl;
615 fout <<
" this->X2P_"<<trCounter<<
"( dv, rv, cls );" << std::endl;
616 fout <<
" for (int ivar=0; ivar<nVar; ivar++) iv[indicesPut.at(ivar)] = rv[ivar];" << std::endl;
619 fout <<
" delete [] dv;" << std::endl;
620 fout <<
" delete [] rv;" << std::endl;
621 fout <<
"}" << std::endl;
Principal Components Analysis (PCA)
MsgLogger & Endl(MsgLogger &ml)
TVectorT< Double_t > TVectorD
TMatrixT< Double_t > TMatrixD
void Print(std::ostream &o) const
print method
static RooMathCoreReg dummy
Abstract ClassifierFactory template that handles arbitrary types.
static void output(int code)