38 #ifndef ROOT_TMVA_MsgLogger
41 #ifndef ROOT_TMVA_VariableNormalizeTransform
44 #ifndef ROOT_TMVA_Tools
47 #ifndef ROOT_TMVA_DataSet
56 TMVA::VariableNormalizeTransform::VariableNormalizeTransform( DataSetInfo& dsi )
57 : VariableTransformBase( dsi, Types::kNormalized, "Norm" )
71 UInt_t inputSize = fGet.size();
72 Int_t numC = GetNClasses()+1;
73 if (GetNClasses() <= 1 ) numC = 1;
77 for (
Int_t i=0; i<numC; i++) {
78 fMin.at(i).resize(inputSize);
79 fMax.at(i).resize(inputSize);
80 fMin.at(i).assign(inputSize, 0);
81 fMax.at(i).assign(inputSize, 0);
90 if (!IsEnabled() || IsCreated())
return kTRUE;
92 Log() <<
kINFO <<
"Preparing the transformation." <<
Endl;
96 CalcNormalizationParams( events );
108 if (!IsCreated())
Log() <<
kFATAL <<
"Transformation not yet created" <<
Endl;
117 if (cls < 0 || cls >= (
int) fMin.size()) cls = fMin.size()-1;
122 std::vector<Char_t> mask;
123 GetInput( ev, input, mask );
125 if (fTransformedEvent==0) fTransformedEvent =
new Event();
132 std::vector<Char_t>::iterator itMask = mask.begin();
133 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
143 min = minVector.at(iidx);
144 max = maxVector.at(iidx);
148 Float_t valnorm = (val-offset)*scale * 2 - 1;
149 output.push_back( valnorm );
155 SetOutput( fTransformedEvent, output, mask, ev );
156 return fTransformedEvent;
164 if (!IsCreated())
Log() <<
kFATAL <<
"Transformation not yet created" <<
Endl;
168 if (cls < 0 || cls > GetNClasses()) {
169 if (GetNClasses() > 1 ) cls = GetNClasses();
175 std::vector<Char_t> mask;
176 GetInput( ev, input, mask,
kTRUE );
178 if (fBackTransformedEvent==0) fBackTransformedEvent =
new Event( *ev );
185 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
188 min = minVector.at(iidx);
189 max = maxVector.at(iidx);
193 Float_t valnorm = offset+((val+1)/(scale * 2));
194 output.push_back( valnorm );
199 SetOutput( fBackTransformedEvent, output, mask, ev,
kTRUE );
201 return fBackTransformedEvent;
209 if (events.size() <= 1)
210 Log() <<
kFATAL <<
"Not enough events (found " << events.size() <<
") to calculate the normalization" <<
Endl;
213 std::vector<Char_t> mask;
215 UInt_t inputSize = fGet.size();
217 const UInt_t nCls = GetNClasses();
225 for (
UInt_t iinp=0; iinp<inputSize; ++iinp) {
226 for (
Int_t ic = 0; ic < numC; ic++) {
227 fMin.at(ic).at(iinp) = FLT_MAX;
228 fMax.at(ic).at(iinp) = -FLT_MAX;
232 std::vector<Event*>::const_iterator evIt = events.begin();
233 for (;evIt!=events.end();evIt++) {
244 GetInput(event,input,mask);
246 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
249 if( minVector.at(iidx) > val ) minVector.at(iidx) = val;
250 if( maxVector.at(iidx) < val ) maxVector.at(iidx) = val;
253 if (minVectorAll.at(iidx) > val) minVectorAll.at(iidx) = val;
254 if (maxVectorAll.at(iidx) < val) maxVectorAll.at(iidx) = val;
271 if (cls < 0 || cls > GetNClasses()) cls = GetNClasses();
274 const UInt_t size = fGet.size();
275 std::vector<TString>* strVec =
new std::vector<TString>(size);
278 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
279 min = fMin.at(cls).at(iinp);
280 max = fMax.at(cls).at(iinp);
283 UInt_t idx = (*itGet).second;
287 VariableInfo& varInfo = (type==
'v'?fDsi.GetVariableInfo(idx):(type==
't'?fDsi.GetTargetInfo(idx):fDsi.GetSpectatorInfo(idx)));
289 if (offset < 0) str =
Form(
"2*%g*([%s] + %g) - 1", scale, varInfo.
GetLabel().
Data(), -offset );
290 else str =
Form(
"2*%g*([%s] - %g) - 1", scale, varInfo.
GetLabel().
Data(), offset );
291 (*strVec)[iinp] = str;
304 o <<
"# min max for all variables for all classes one after the other and as a last entry for all classes together" << std::endl;
306 Int_t numC = GetNClasses()+1;
307 if (GetNClasses() <= 1 ) numC = 1;
309 UInt_t nvars = GetNVariables();
310 UInt_t ntgts = GetNTargets();
312 for (
Int_t icls = 0; icls < numC; icls++ ) {
313 o << icls << std::endl;
314 for (
UInt_t ivar=0; ivar<nvars; ivar++)
315 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(ivar) <<
" "
316 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(ivar) << std::endl;
317 for (
UInt_t itgt=0; itgt<ntgts; itgt++)
318 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(nvars+itgt) <<
" "
319 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(nvars+itgt) << std::endl;
321 o <<
"##" << std::endl;
333 Int_t numC = (GetNClasses()<= 1)?1:GetNClasses()+1;
335 for(
Int_t icls=0; icls<numC; icls++ ) {
340 for(
ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
357 void* inpnode =
NULL;
360 if( inpnode !=
NULL )
370 UInt_t size = fGet.size();
379 fMin.resize(classindex+1);
380 fMax.resize(classindex+1);
382 fMin[classindex].resize(size,
Float_t(0));
383 fMax[classindex].resize(size,
Float_t(0));
388 if(nodeName==
"Ranges") {
406 UInt_t classindex, varindex, tgtindex, nvars, ntgts;
413 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
414 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
416 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
417 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
423 fMin.resize(classindex+1);
424 fMax.resize(classindex+1);
425 fMin[classindex].resize(nvars+ntgts,
Float_t(0));
426 fMax[classindex].resize(nvars+ntgts,
Float_t(0));
431 if(nodeName==
"Variables") {
439 }
else if (nodeName==
"Targets") {
443 gTools().
ReadAttr(tgtch,
"Min", fMin[classindex][nvars+tgtindex]);
444 gTools().
ReadAttr(tgtch,
"Max", fMax[classindex][nvars+tgtindex]);
462 UInt_t nvars = GetNVariables();
464 if(var.size() != nvars)
465 Log() <<
kFATAL <<
"<BuildTransformationFromVarInfo> can't build transformation,"
466 <<
" since the number of variables disagree" <<
Endl;
468 UInt_t numC = (GetNClasses()<=1)?1:GetNClasses()+1;
469 fMin.clear();fMin.resize( numC );
470 fMax.clear();fMax.resize( numC );
473 for(
UInt_t cls=0; cls<numC; ++cls) {
474 fMin[cls].resize(nvars+GetNTargets(),0);
475 fMax[cls].resize(nvars+GetNTargets(),0);
477 for(std::vector<TMVA::VariableInfo>::const_iterator
v = var.begin();
v!=var.end(); ++
v, ++vidx) {
478 fMin[cls][vidx] =
v->GetMin();
479 fMax[cls][vidx] =
v->GetMax();
480 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',vidx));
491 UInt_t nvars = GetNVariables();
492 UInt_t ntgts = GetNTargets();
493 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
494 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
496 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
497 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
501 istr.getline(buf,512);
505 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
507 while (*p==
' ' || *p==
'\t') p++;
508 if (*p==
'#' || *p==
'\0') {
509 istr.getline(buf,512);
512 std::stringstream sstr(buf);
514 for (
UInt_t ivar=0;ivar<nvars;ivar++) {
515 istr.getline(buf2,512);
516 std::stringstream sstr2(buf2);
517 sstr2 >> fMin[icls][ivar] >> fMax[icls][ivar];
519 for (
UInt_t itgt=0;itgt<ntgts;itgt++) {
520 istr.getline(buf2,512);
521 std::stringstream sstr2(buf2);
522 sstr2 >> fMin[icls][nvars+itgt] >> fMax[icls][nvars+itgt];
524 istr.getline(buf,512);
534 Int_t nCls = GetNClasses();
536 if (nCls <= 1 ) numC = 1;
537 for (
Int_t icls = 0; icls < numC; icls++ ) {
539 Log() <<
kINFO <<
"Transformation for all classes based on these ranges:" <<
Endl;
541 Log() <<
kINFO <<
"Transformation for class " << icls <<
" based on these ranges:" <<
Endl;
543 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ){
545 UInt_t idx = (*itGet).second;
547 TString typeString = (type==
'v'?
"Variable: ": (type==
't'?
"Target : ":
"Spectator : ") );
548 Log() << typeString.
Data() << std::setw(20) << fMin[icls][idx] << std::setw(20) << fMax[icls][idx] <<
Endl;
562 UInt_t nVar = fGet.size();
563 UInt_t numC = fMin.size();
566 fout <<
" double fMin_"<<trCounter<<
"["<<numC<<
"]["<<nVar<<
"];" << std::endl;
567 fout <<
" double fMax_"<<trCounter<<
"["<<numC<<
"]["<<nVar<<
"];" << std::endl;
572 fout <<
"//_______________________________________________________________________" << std::endl;
573 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
574 fout <<
"{" << std::endl;
575 fout <<
" // Normalization transformation, initialisation" << std::endl;
576 for (
UInt_t ivar=0; ivar<nVar; ivar++) {
577 for (
UInt_t icls = 0; icls < numC; icls++) {
580 fout <<
" fMin_"<<trCounter<<
"["<<icls<<
"]["<<ivar<<
"] = " << std::setprecision(12)
581 << min <<
";" << std::endl;
582 fout <<
" fMax_"<<trCounter<<
"["<<icls<<
"]["<<ivar<<
"] = " << std::setprecision(12)
583 << max <<
";" << std::endl;
586 fout <<
"}" << std::endl;
588 fout <<
"//_______________________________________________________________________" << std::endl;
589 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int cls) const" << std::endl;
590 fout <<
"{" << std::endl;
591 fout <<
" // Normalization transformation" << std::endl;
592 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
593 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
594 fout <<
" else cls = "<<(fMin.size()==1?0:2)<<
";"<< std::endl;
595 fout <<
" }"<< std::endl;
596 fout <<
" const int nVar = " << nVar <<
";" << std::endl << std::endl;
597 fout <<
" // get indices of used variables" << std::endl;
599 fout <<
" static std::vector<double> dv;" << std::endl;
600 fout <<
" dv.resize(nVar);" << std::endl;
601 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
603 fout <<
" for (int ivar=0;ivar<"<<nVar<<
";ivar++) {" << std::endl;
604 fout <<
" double offset = fMin_"<<trCounter<<
"[cls][ivar];" << std::endl;
605 fout <<
" double scale = 1.0/(fMax_"<<trCounter<<
"[cls][ivar]-fMin_"<<trCounter<<
"[cls][ivar]);" << std::endl;
606 fout <<
" iv[indicesPut.at(ivar)] = (dv[ivar]-offset)*scale * 2 - 1;" << std::endl;
607 fout <<
" }" << std::endl;
608 fout <<
"}" << std::endl;
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
MsgLogger & Endl(MsgLogger &ml)
Short_t Min(Short_t a, Short_t b)
const char * Data() const
void Initialize(Bool_t useTMVAStyle=kTRUE)
char * Form(const char *fmt,...)
static RooMathCoreReg dummy
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Abstract ClassifierFactory template that handles arbitrary types.
Short_t Max(Short_t a, Short_t b)
const TString & GetLabel() const
static void output(int code)