54 TMVA::VariableNormalizeTransform::VariableNormalizeTransform( DataSetInfo& dsi )
55 : VariableTransformBase( dsi, Types::kNormalized, "Norm" )
69 UInt_t inputSize = fGet.size();
70 Int_t numC = GetNClasses()+1;
71 if (GetNClasses() <= 1 ) numC = 1;
75 for (
Int_t i=0; i<numC; i++) {
76 fMin.at(i).resize(inputSize);
77 fMax.at(i).resize(inputSize);
78 fMin.at(i).assign(inputSize, 0);
79 fMax.at(i).assign(inputSize, 0);
88 if (!IsEnabled() || IsCreated())
return kTRUE;
90 Log() <<
kINFO <<
"Preparing the transformation." <<
Endl;
94 CalcNormalizationParams( events );
106 if (!IsCreated())
Log() <<
kFATAL <<
"Transformation not yet created" <<
Endl;
115 if (cls < 0 || cls >= (
int) fMin.size()) cls = fMin.size()-1;
120 std::vector<Char_t> mask;
121 GetInput( ev, input, mask );
123 if (fTransformedEvent==0) fTransformedEvent =
new Event();
130 std::vector<Char_t>::iterator itMask = mask.begin();
131 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
141 min = minVector.at(iidx);
142 max = maxVector.at(iidx);
147 output.push_back( valnorm );
153 SetOutput( fTransformedEvent, output, mask, ev );
154 return fTransformedEvent;
162 if (!IsCreated())
Log() <<
kFATAL <<
"Transformation not yet created" <<
Endl;
166 if (cls < 0 || cls > GetNClasses()) {
167 if (GetNClasses() > 1 ) cls = GetNClasses();
173 std::vector<Char_t> mask;
174 GetInput( ev, input, mask,
kTRUE );
176 if (fBackTransformedEvent==0) fBackTransformedEvent =
new Event( *ev );
183 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
186 min = minVector.at(iidx);
187 max = maxVector.at(iidx);
191 Float_t valnorm = offset+((val+1)/(scale * 2));
192 output.push_back( valnorm );
197 SetOutput( fBackTransformedEvent, output, mask, ev,
kTRUE );
199 return fBackTransformedEvent;
207 if (events.size() <= 1)
208 Log() <<
kFATAL <<
"Not enough events (found " << events.size() <<
") to calculate the normalization" <<
Endl;
211 std::vector<Char_t> mask;
213 UInt_t inputSize = fGet.size();
215 const UInt_t nCls = GetNClasses();
223 for (
UInt_t iinp=0; iinp<inputSize; ++iinp) {
224 for (
Int_t ic = 0; ic < numC; ic++) {
225 fMin.at(ic).at(iinp) = FLT_MAX;
226 fMax.at(ic).at(iinp) = -FLT_MAX;
230 std::vector<Event*>::const_iterator evIt = events.begin();
231 for (;evIt!=events.end();evIt++) {
242 GetInput(event,input,mask);
244 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
247 if( minVector.at(iidx) > val ) minVector.at(iidx) = val;
248 if( maxVector.at(iidx) < val ) maxVector.at(iidx) = val;
251 if (minVectorAll.at(iidx) > val) minVectorAll.at(iidx) = val;
252 if (maxVectorAll.at(iidx) < val) maxVectorAll.at(iidx) = val;
269 if (cls < 0 || cls > GetNClasses()) cls = GetNClasses();
272 const UInt_t size = fGet.size();
273 std::vector<TString>* strVec =
new std::vector<TString>(size);
276 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
277 min = fMin.at(cls).at(iinp);
278 max = fMax.at(cls).at(iinp);
281 UInt_t idx = (*itGet).second;
285 VariableInfo& varInfo = (type==
'v'?fDsi.GetVariableInfo(idx):(type==
't'?fDsi.GetTargetInfo(idx):fDsi.GetSpectatorInfo(idx)));
289 (*strVec)[iinp] = str;
302 o <<
"# min max for all variables for all classes one after the other and as a last entry for all classes together" << std::endl;
304 Int_t numC = GetNClasses()+1;
305 if (GetNClasses() <= 1 ) numC = 1;
307 UInt_t nvars = GetNVariables();
308 UInt_t ntgts = GetNTargets();
310 for (
Int_t icls = 0; icls < numC; icls++ ) {
311 o << icls << std::endl;
312 for (
UInt_t ivar=0; ivar<nvars; ivar++)
313 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(ivar) <<
" "
314 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(ivar) << std::endl;
315 for (
UInt_t itgt=0; itgt<ntgts; itgt++)
316 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(nvars+itgt) <<
" "
317 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(nvars+itgt) << std::endl;
319 o <<
"##" << std::endl;
331 Int_t numC = (GetNClasses()<= 1)?1:GetNClasses()+1;
333 for(
Int_t icls=0; icls<numC; icls++ ) {
338 for(
ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
355 void* inpnode =
NULL;
358 if( inpnode !=
NULL )
368 UInt_t size = fGet.size();
377 fMin.resize(classindex+1);
378 fMax.resize(classindex+1);
380 fMin[classindex].resize(size,
Float_t(0));
381 fMax[classindex].resize(size,
Float_t(0));
386 if(nodeName==
"Ranges") {
404 UInt_t classindex, varindex, tgtindex, nvars, ntgts;
411 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
412 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
414 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
415 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
421 fMin.resize(classindex+1);
422 fMax.resize(classindex+1);
423 fMin[classindex].resize(nvars+ntgts,
Float_t(0));
424 fMax[classindex].resize(nvars+ntgts,
Float_t(0));
429 if(nodeName==
"Variables") {
437 }
else if (nodeName==
"Targets") {
441 gTools().
ReadAttr(tgtch,
"Min", fMin[classindex][nvars+tgtindex]);
442 gTools().
ReadAttr(tgtch,
"Max", fMax[classindex][nvars+tgtindex]);
460 UInt_t nvars = GetNVariables();
462 if(var.size() != nvars)
463 Log() <<
kFATAL <<
"<BuildTransformationFromVarInfo> can't build transformation,"
464 <<
" since the number of variables disagree" <<
Endl;
466 UInt_t numC = (GetNClasses()<=1)?1:GetNClasses()+1;
467 fMin.clear();fMin.resize( numC );
468 fMax.clear();fMax.resize( numC );
471 for(
UInt_t cls=0; cls<numC; ++cls) {
472 fMin[cls].resize(nvars+GetNTargets(),0);
473 fMax[cls].resize(nvars+GetNTargets(),0);
475 for(std::vector<TMVA::VariableInfo>::const_iterator
v = var.begin();
v!=var.end(); ++
v, ++vidx) {
476 fMin[cls][vidx] =
v->GetMin();
477 fMax[cls][vidx] =
v->GetMax();
478 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',vidx));
489 UInt_t nvars = GetNVariables();
490 UInt_t ntgts = GetNTargets();
491 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
492 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
494 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
495 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
499 istr.getline(buf,512);
503 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
505 while (*p==
' ' || *p==
'\t') p++;
506 if (*p==
'#' || *p==
'\0') {
507 istr.getline(buf,512);
510 std::stringstream sstr(buf);
512 for (
UInt_t ivar=0;ivar<nvars;ivar++) {
513 istr.getline(buf2,512);
514 std::stringstream sstr2(buf2);
515 sstr2 >> fMin[icls][ivar] >> fMax[icls][ivar];
517 for (
UInt_t itgt=0;itgt<ntgts;itgt++) {
518 istr.getline(buf2,512);
519 std::stringstream sstr2(buf2);
520 sstr2 >> fMin[icls][nvars+itgt] >> fMax[icls][nvars+itgt];
522 istr.getline(buf,512);
532 Int_t nCls = GetNClasses();
534 if (nCls <= 1 ) numC = 1;
535 for (
Int_t icls = 0; icls < numC; icls++ ) {
537 Log() <<
kINFO <<
"Transformation for all classes based on these ranges:" <<
Endl;
539 Log() <<
kINFO <<
"Transformation for class " << icls <<
" based on these ranges:" <<
Endl;
541 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ){
543 UInt_t idx = (*itGet).second;
545 TString typeString = (type==
'v'?
"Variable: ": (type==
't'?
"Target : ":
"Spectator : ") );
546 Log() << typeString.
Data() << std::setw(20) << fMin[icls][idx] << std::setw(20) << fMax[icls][idx] <<
Endl;
560 UInt_t nVar = fGet.size();
561 UInt_t numC = fMin.size();
564 fout <<
" double fMin_"<<trCounter<<
"["<<numC<<
"]["<<nVar<<
"];" << std::endl;
565 fout <<
" double fMax_"<<trCounter<<
"["<<numC<<
"]["<<nVar<<
"];" << std::endl;
570 fout <<
"//_______________________________________________________________________" << std::endl;
571 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
572 fout <<
"{" << std::endl;
573 fout <<
" // Normalization transformation, initialisation" << std::endl;
574 for (
UInt_t ivar=0; ivar<nVar; ivar++) {
575 for (
UInt_t icls = 0; icls < numC; icls++) {
578 fout <<
" fMin_"<<trCounter<<
"["<<icls<<
"]["<<ivar<<
"] = " << std::setprecision(12)
579 << min <<
";" << std::endl;
580 fout <<
" fMax_"<<trCounter<<
"["<<icls<<
"]["<<ivar<<
"] = " << std::setprecision(12)
581 << max <<
";" << std::endl;
584 fout <<
"}" << std::endl;
586 fout <<
"//_______________________________________________________________________" << std::endl;
587 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int cls) const" << std::endl;
588 fout <<
"{" << std::endl;
589 fout <<
" // Normalization transformation" << std::endl;
590 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
591 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
592 fout <<
" else cls = "<<(fMin.size()==1?0:2)<<
";"<< std::endl;
593 fout <<
" }"<< std::endl;
594 fout <<
" const int nVar = " << nVar <<
";" << std::endl << std::endl;
595 fout <<
" // get indices of used variables" << std::endl;
597 fout <<
" static std::vector<double> dv;" << std::endl;
598 fout <<
" dv.resize(nVar);" << std::endl;
599 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
601 fout <<
" for (int ivar=0;ivar<"<<nVar<<
";ivar++) {" << std::endl;
602 fout <<
" double offset = fMin_"<<trCounter<<
"[cls][ivar];" << std::endl;
603 fout <<
" double scale = 1.0/(fMax_"<<trCounter<<
"[cls][ivar]-fMin_"<<trCounter<<
"[cls][ivar]);" << std::endl;
604 fout <<
" iv[indicesPut.at(ivar)] = (dv[ivar]-offset)*scale * 2 - 1;" << std::endl;
605 fout <<
" }" << std::endl;
606 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)
Short_t Max(Short_t a, Short_t b)
const TString & GetLabel() const
static void output(int code)