52 TMVA::VariableDecorrTransform::VariableDecorrTransform( DataSetInfo& dsi )
63 if ((*it) != 0)
delete (*it);
81 if (!IsEnabled() || IsCreated())
return kTRUE;
83 Log() <<
kINFO <<
"Preparing the Decorrelation transformation..." <<
Endl;
85 Int_t inputSize = fGet.size();
86 SetNVariables(inputSize);
88 if (inputSize > 200) {
89 Log() <<
kINFO <<
"----------------------------------------------------------------------------"
92 <<
": More than 200 variables, will not calculate decorrelation matrix "
94 Log() <<
kINFO <<
"----------------------------------------------------------------------------"
99 CalcSQRMats( events, GetNClasses() );
111 Int_t whichMatrix = cls;
115 if (cls < 0 || cls > GetNClasses()) whichMatrix = GetNClasses();
117 TMatrixD*
m = fDecorrMatrices.at(whichMatrix);
119 if (whichMatrix == GetNClasses() )
120 Log() <<
kFATAL <<
"Transformation matrix all classes is not defined"
123 Log() <<
kFATAL <<
"Transformation matrix for class " << whichMatrix <<
" is not defined"
127 const Int_t nvar = fGet.size();
128 std::vector<TString>* strVec =
new std::vector<TString>;
131 for (
Int_t ivar=0; ivar<nvar; ivar++) {
133 for (
Int_t jvar=0; jvar<nvar; jvar++) {
134 str += ((*m)(ivar,jvar) > 0) ?
" + " :
" - ";
137 Int_t idx = fGet.at(jvar).second;
141 str +=
Form(
"%10.5g*[%s]",
TMath::Abs((*m)(ivar,jvar)), Variables()[idx].GetLabel().
Data() );
144 str +=
Form(
"%10.5g*[%s]",
TMath::Abs((*m)(ivar,jvar)), Targets()[idx].GetLabel().
Data() );
147 str +=
Form(
"%10.5g*[%s]",
TMath::Abs((*m)(ivar,jvar)), Spectators()[idx].GetLabel().
Data() );
150 Log() <<
kFATAL <<
"VariableDecorrTransform::GetTransformationStrings : unknown type '" << type <<
"'." <<
Endl;
153 strVec->push_back( str );
165 Log() <<
kFATAL <<
"Transformation matrix not yet created"
168 Int_t whichMatrix = cls;
171 if (cls < 0 || cls >= (
int) fDecorrMatrices.size()) whichMatrix = fDecorrMatrices.size()-1;
178 TMatrixD*
m = fDecorrMatrices.at(whichMatrix);
180 if (whichMatrix == GetNClasses() )
181 Log() <<
kFATAL <<
"Transformation matrix all classes is not defined"
184 Log() <<
kFATAL <<
"Transformation matrix for class " << whichMatrix <<
" is not defined"
188 if (fTransformedEvent==0 || fTransformedEvent->GetNVariables()!=ev->
GetNVariables()) {
189 if (fTransformedEvent!=0) {
delete fTransformedEvent; fTransformedEvent = 0; }
190 fTransformedEvent =
new Event();
194 const Int_t nvar = fGet.size();
196 std::vector<Float_t> input;
197 std::vector<Char_t> mask;
198 Bool_t hasMaskedEntries = GetInput( ev, input, mask );
200 if( hasMaskedEntries ){
203 if( numMasked>0 && numOK>0 ){
204 Log() <<
kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
206 SetOutput( fTransformedEvent, input, mask, ev );
207 return fTransformedEvent;
211 for (
Int_t ivar=0; ivar<nvar; ivar++) vec(ivar) = input.at(ivar);
217 for (
Int_t ivar=0; ivar<nvar; ivar++) input.push_back( vec(ivar) );
219 SetOutput( fTransformedEvent, input, mask, ev );
221 return fTransformedEvent;
230 Log() <<
kFATAL <<
"Inverse transformation for decorrelation transformation not yet implemented. Hence, this transformation cannot be applied together with regression if targets should be transformed. Please contact the authors if necessary." <<
Endl;
233 return fBackTransformedEvent;
243 for (std::vector<TMatrixD*>::iterator it = fDecorrMatrices.begin();
244 it != fDecorrMatrices.end(); it++)
245 if (0 != (*it) ) {
delete (*it); *it=0; }
249 const UInt_t matNum = (maxCls<=1)?maxCls:maxCls+1;
250 fDecorrMatrices.resize( matNum, (
TMatrixD*) 0 );
255 for (
UInt_t cls=0; cls<matNum; cls++) {
258 Log() <<
kFATAL <<
"<GetSQRMats> Zero pointer returned for SQR matrix" <<
Endl;
259 fDecorrMatrices[cls] = sqrMat;
260 delete (*covMat)[cls];
271 Int_t dp = o.precision();
272 for (std::vector<TMatrixD*>::const_iterator itm = fDecorrMatrices.begin(); itm != fDecorrMatrices.end(); itm++) {
273 o <<
"# correlation matrix " << std::endl;
275 o << cls <<
" " << mat->
GetNrows() <<
" x " << mat->
GetNcols() << std::endl;
278 o << std::setprecision(12) << std::setw(20) << (*mat)[
row][col] <<
" ";
284 o <<
"##" << std::endl;
285 o << std::setprecision(dp);
298 for (std::vector<TMatrixD*>::const_iterator itm = fDecorrMatrices.begin(); itm != fDecorrMatrices.end(); itm++) {
321 for( std::vector<TMatrixD*>::iterator it = fDecorrMatrices.begin(); it != fDecorrMatrices.end(); it++ )
322 if( (*it) != 0 )
delete (*it);
323 fDecorrMatrices.clear();
327 void* inpnode =
NULL;
350 std::stringstream s(content);
352 for (
Int_t col = 0; col<ncols; col++) {
353 s >> (*mat)[
row][col];
356 fDecorrMatrices.push_back(mat);
369 istr.getline(buf,512);
371 Int_t nrows(0), ncols(0);
373 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
375 while (*p==
' ' || *p==
'\t') p++;
376 if (*p==
'#' || *p==
'\0') {
377 istr.getline(buf,512);
380 std::stringstream sstr(buf);
383 if (strvar==
"signal" || strvar==
"background") {
385 if(strvar==
"background") cls=1;
386 if(strvar==classname) classIdx = cls;
388 sstr >> nrows >> dummy >> ncols;
389 if (fDecorrMatrices.size() <= cls ) fDecorrMatrices.resize(cls+1);
390 if (fDecorrMatrices.at(cls) != 0)
delete fDecorrMatrices.at(cls);
395 istr >> (*mat)[
row][col];
399 istr.getline(buf,512);
402 fDecorrMatrices.push_back(
new TMatrixD(*fDecorrMatrices[classIdx]) );
413 for (std::vector<TMatrixD*>::iterator itm = fDecorrMatrices.begin(); itm != fDecorrMatrices.end(); itm++) {
414 Log() <<
kINFO <<
"Transformation matrix "<< cls <<
":" <<
Endl;
424 Int_t dp = fout.precision();
426 UInt_t numC = fDecorrMatrices.size();
429 TMatrixD* mat = fDecorrMatrices.at(0);
431 fout <<
" double fDecTF_"<<trCounter<<
"["<<numC<<
"]["<<mat->
GetNrows()<<
"]["<<mat->
GetNcols()<<
"];" << std::endl;
436 fout <<
"//_______________________________________________________________________" << std::endl;
437 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
438 fout <<
"{" << std::endl;
439 fout <<
" // Decorrelation transformation, initialisation" << std::endl;
440 for (
UInt_t icls = 0; icls < numC; icls++){
441 TMatrixD* matx = fDecorrMatrices.at(icls);
442 for (
int i=0; i<matx->
GetNrows(); i++) {
443 for (
int j=0; j<matx->
GetNcols(); j++) {
444 fout <<
" fDecTF_"<<trCounter<<
"["<<icls<<
"]["<<i<<
"]["<<j<<
"] = " << std::setprecision(12) << (*matx)[i][j] <<
";" << std::endl;
448 fout <<
"}" << std::endl;
450 TMatrixD* matx = fDecorrMatrices.at(0);
451 fout <<
"//_______________________________________________________________________" << std::endl;
452 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int cls) const" << std::endl;
453 fout <<
"{" << std::endl;
454 fout <<
" // Decorrelation transformation" << std::endl;
455 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
456 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
457 fout <<
" else cls = "<<(fDecorrMatrices.size()==1?0:2)<<
";"<< std::endl;
458 fout <<
" }"<< std::endl;
462 fout <<
" std::vector<double> tv;" << std::endl;
463 fout <<
" for (int i=0; i<"<<matx->
GetNrows()<<
";i++) {" << std::endl;
464 fout <<
" double v = 0;" << std::endl;
465 fout <<
" for (int j=0; j<"<<matx->
GetNcols()<<
"; j++)" << std::endl;
466 fout <<
" v += iv[indicesGet.at(j)] * fDecTF_"<<trCounter<<
"[cls][i][j];" << std::endl;
467 fout <<
" tv.push_back(v);" << std::endl;
468 fout <<
" }" << std::endl;
469 fout <<
" for (int i=0; i<"<<matx->
GetNrows()<<
";i++) iv[indicesPut.at(i)] = tv[i];" << std::endl;
470 fout <<
"}" << std::endl;
473 fout << std::setprecision(dp);
MsgLogger & Endl(MsgLogger &ml)
std::vector< std::vector< double > > Data
UInt_t GetNVariables() const
accessor to the number of variables
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
TMatrixT< Double_t > TMatrixD
void Initialize(Bool_t useTMVAStyle=kTRUE)
char * Form(const char *fmt,...)
static RooMathCoreReg dummy