95 Log() << kINFO <<
"Preparing the Principle Component (PCA) transformation..." <<
Endl;
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 <<
"----------------------------------------------------------------------------" 149 std::vector<Float_t> input;
150 std::vector<Char_t> mask;
151 std::vector<Float_t> principalComponents;
155 if( hasMaskedEntries ){
158 if( numMasked>0 && numOK>0 ){
159 Log() << kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
165 X2P( principalComponents, input, cls );
191 std::vector<Float_t> principalComponents;
192 std::vector<Char_t> mask;
193 std::vector<Float_t>
output;
196 P2X( output, principalComponents, cls );
208 UInt_t nvars = 0, ntgts = 0, nspcts = 0;
210 if( nvars>0 && ntgts>0 )
211 Log() << kFATAL <<
"Variables and targets cannot be mixed in PCA transformation." <<
Endl;
217 const UInt_t maxPCA = (nCls<=1) ? nCls : nCls+1;
220 std::vector<TPrincipal*> pca(maxPCA);
226 Long64_t ievt, entries = events.size();
229 std::vector<Float_t> input;
230 std::vector<Char_t> mask;
231 for (ievt=0; ievt<entries; ievt++) {
232 const Event* ev = events[ievt];
236 if (hasMaskedEntries){
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;
245 for( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp )
252 pca.at(cls)->AddRow( dvec );
253 if (nCls > 1) pca.at(maxPCA-1)->AddRow( dvec );
262 for (
UInt_t i=0; i<maxPCA; i++ ) {
263 pca.at(i)->MakePrincipals();
270 for (
UInt_t i=0; i<maxPCA; i++)
delete pca.at(i);
282 const Int_t nInput = x.size();
285 for (
Int_t i = 0; i < nInput; i++) {
287 for (
Int_t j = 0; j < nInput; j++)
301 const Int_t nInput = pc.size();
304 for (
Int_t i = 0; i < nInput; i++) {
306 for (
Int_t j = 0; j < nInput; j++)
317 for (
Int_t sbType=0; sbType<2; sbType++) {
318 o <<
"# PCA mean values " << std::endl;
320 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << means->
GetNrows() << std::endl;
322 o << std::setprecision(12) << std::setw(20) << (*means)[row];
326 o <<
"##" << std::endl;
329 for (
Int_t sbType=0; sbType<2; sbType++) {
330 o <<
"# PCA eigenvectors " << std::endl;
332 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << mat->
GetNrows() <<
" x " << mat->
GetNcols() << std::endl;
335 o << std::setprecision(12) << std::setw(20) << (*mat)[row][col] <<
" ";
340 o <<
"##" << std::endl;
356 gTools().
AddAttr( meanxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined")) );
369 gTools().
AddAttr( evxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined") ) );
393 void* inpnode =
NULL;
409 if (nodeName ==
"Statistics") {
421 std::stringstream s(
gTools().GetContent(ch));
424 else if ( nodeName ==
"Eigenvectors" ) {
436 std::stringstream s(
gTools().GetContent(ch));
437 for (
Int_t row = 0; row<nrows; row++)
438 for (
Int_t col = 0; col<ncols; col++)
453 istr.getline(buf,512);
455 Int_t nrows(0), ncols(0);
456 UInt_t classIdx=(classname==
"signal"?0:1);
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);
476 if (strvar==
"signal" || strvar==
"background") {
479 Int_t sbType = (strvar==
"signal" ? 0 : 1);
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);
503 if (strvar==
"signal" || strvar==
"background") {
506 sstr >> nrows >> dummy >> ncols;
507 Int_t sbType = (strvar==
"signal" ? 0 : 1);
520 istr.getline(buf,512);
540 fout <<
" void X2P_"<<trCounter<<
"( const double*, double*, int ) const;" << std::endl;
541 fout <<
" double fMeanValues_"<<trCounter<<
"["<<numC<<
"][" 542 <<
fMeanValues[0]->GetNrows() <<
"];" << std::endl;
543 fout <<
" double fEigenVectors_"<<trCounter<<
"["<<numC<<
"][" 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;
577 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << 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();
584 for (
UInt_t index=0; index<numC; index++) {
585 for (
int i=0; i<
fMeanValues[index]->GetNrows(); i++) {
586 fout <<
" fMeanValues_"<<trCounter<<
"["<<index<<
"]["<<i<<
"] = " << std::setprecision(12)
593 fout <<
" // initialise matrix of eigenvectors" << std::endl;
594 for (
UInt_t index=0; index<numC; index++) {
597 fout <<
" fEigenVectors_"<<trCounter<<
"["<<index<<
"]["<<i<<
"]["<<j<<
"] = " << std::setprecision(12)
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;
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;
Principal Components Analysis (PCA)
MsgLogger & Endl(MsgLogger &ml)
Singleton class for Global types used by TMVA.
Class that contains all the data information.
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.