73 UInt_t inputSize = fGet.size();
74 Int_t numC = GetNClasses()+1;
75 if (GetNClasses() <= 1 ) numC = 1;
79 for (
Int_t i=0; i<numC; i++) {
80 fMin.at(i).resize(inputSize);
81 fMax.at(i).resize(inputSize);
82 fMin.at(i).assign(inputSize, 0);
83 fMax.at(i).assign(inputSize, 0);
92 if (!IsEnabled() || IsCreated())
return kTRUE;
94 Log() << kDEBUG <<
"\tPreparing the transformation." <<
Endl;
98 CalcNormalizationParams( events );
110 if (!IsCreated()) Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
119 if (cls < 0 || cls >= (
int) fMin.size()) cls = fMin.size()-1;
124 std::vector<Char_t> mask;
125 GetInput( ev, input, mask );
127 if (fTransformedEvent==0) fTransformedEvent =
new Event();
134 std::vector<Char_t>::iterator itMask = mask.begin();
135 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
145 min = minVector.at(iidx);
146 max = maxVector.at(iidx);
150 Float_t valnorm = (val-offset)*scale * 2 - 1;
151 output.push_back( valnorm );
157 SetOutput( fTransformedEvent,
output, mask, ev );
158 return fTransformedEvent;
166 if (!IsCreated()) Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
170 if (cls < 0 || cls > GetNClasses()) {
171 if (GetNClasses() > 1 ) cls = GetNClasses();
177 std::vector<Char_t> mask;
178 GetInput( ev, input, mask,
kTRUE );
180 if (fBackTransformedEvent==0) fBackTransformedEvent =
new Event( *ev );
187 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
190 min = minVector.at(iidx);
191 max = maxVector.at(iidx);
195 Float_t valnorm = offset+((val+1)/(scale * 2));
196 output.push_back( valnorm );
201 SetOutput( fBackTransformedEvent,
output, mask, ev,
kTRUE );
203 return fBackTransformedEvent;
211 if (events.size() <= 1)
212 Log() << kFATAL <<
"Not enough events (found " << events.size() <<
") to calculate the normalization" <<
Endl;
215 std::vector<Char_t> mask;
217 UInt_t inputSize = fGet.size();
219 const UInt_t nCls = GetNClasses();
227 for (
UInt_t iinp=0; iinp<inputSize; ++iinp) {
228 for (
Int_t ic = 0; ic < numC; ic++) {
229 fMin.at(ic).at(iinp) = FLT_MAX;
230 fMax.at(ic).at(iinp) = -FLT_MAX;
234 std::vector<Event*>::const_iterator evIt = events.begin();
235 for (;evIt!=events.end();++evIt) {
238 UInt_t cls = (*evIt)->GetClass();
246 GetInput(
event,input,mask);
248 for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) {
251 if( minVector.at(iidx) > val ) minVector.at(iidx) = val;
252 if( maxVector.at(iidx) < val ) maxVector.at(iidx) = val;
255 if (minVectorAll.at(iidx) > val) minVectorAll.at(iidx) = val;
256 if (maxVectorAll.at(iidx) < val) maxVectorAll.at(iidx) = val;
273 if (cls < 0 || cls > GetNClasses()) cls = GetNClasses();
277 std::vector<TString>* strVec =
new std::vector<TString>(
size);
280 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
281 min = fMin.at(cls).at(iinp);
282 max = fMax.at(cls).at(iinp);
285 UInt_t idx = (*itGet).second;
289 VariableInfo& varInfo = (
type==
'v'?fDsi.GetVariableInfo(idx):(
type==
't'?fDsi.GetTargetInfo(idx):fDsi.GetSpectatorInfo(idx)));
291 if (offset < 0) str =
Form(
"2*%g*([%s] + %g) - 1", scale, varInfo.
GetLabel().
Data(), -offset );
292 else str =
Form(
"2*%g*([%s] - %g) - 1", scale, varInfo.
GetLabel().
Data(), offset );
293 (*strVec)[iinp] = str;
306 o <<
"# min max for all variables for all classes one after the other and as a last entry for all classes together" << std::endl;
308 Int_t numC = GetNClasses()+1;
309 if (GetNClasses() <= 1 ) numC = 1;
311 UInt_t nvars = GetNVariables();
312 UInt_t ntgts = GetNTargets();
314 for (
Int_t icls = 0; icls < numC; icls++ ) {
315 o << icls << std::endl;
316 for (
UInt_t ivar=0; ivar<nvars; ivar++)
317 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(ivar) <<
" "
318 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(ivar) << std::endl;
319 for (
UInt_t itgt=0; itgt<ntgts; itgt++)
320 o << std::setprecision(12) << std::setw(20) << fMin.at(icls).at(nvars+itgt) <<
" "
321 << std::setprecision(12) << std::setw(20) << fMax.at(icls).at(nvars+itgt) << std::endl;
323 o <<
"##" << std::endl;
335 Int_t numC = (GetNClasses()<= 1)?1:GetNClasses()+1;
337 for(
Int_t icls=0; icls<numC; icls++ ) {
342 for(
ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
359 void* inpnode = NULL;
362 if( inpnode != NULL )
381 fMin.resize(classindex+1);
382 fMax.resize(classindex+1);
390 if(nodeName==
"Ranges") {
408 UInt_t classindex, varindex, tgtindex, nvars, ntgts;
415 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
416 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
418 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
419 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
425 fMin.resize(classindex+1);
426 fMax.resize(classindex+1);
427 fMin[classindex].resize(nvars+ntgts,
Float_t(0));
428 fMax[classindex].resize(nvars+ntgts,
Float_t(0));
433 if(nodeName==
"Variables") {
441 }
else if (nodeName==
"Targets") {
445 gTools().
ReadAttr(tgtch,
"Min", fMin[classindex][nvars+tgtindex]);
446 gTools().
ReadAttr(tgtch,
"Max", fMax[classindex][nvars+tgtindex]);
464 UInt_t nvars = GetNVariables();
466 if(var.size() != nvars)
467 Log() << kFATAL <<
"<BuildTransformationFromVarInfo> can't build transformation,"
468 <<
" since the number of variables disagree" <<
Endl;
470 UInt_t numC = (GetNClasses()<=1)?1:GetNClasses()+1;
471 fMin.clear();fMin.resize( numC );
472 fMax.clear();fMax.resize( numC );
475 for(
UInt_t cls=0; cls<numC; ++cls) {
476 fMin[cls].resize(nvars+GetNTargets(),0);
477 fMax[cls].resize(nvars+GetNTargets(),0);
479 for(std::vector<TMVA::VariableInfo>::const_iterator
v = var.begin();
v!=var.end(); ++
v, ++vidx) {
480 fMin[cls][vidx] =
v->GetMin();
481 fMax[cls][vidx] =
v->GetMax();
482 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',vidx));
493 UInt_t nvars = GetNVariables();
494 UInt_t ntgts = GetNTargets();
495 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ){
496 fGet.push_back(std::pair<Char_t,UInt_t>(
'v',ivar));
498 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ){
499 fGet.push_back(std::pair<Char_t,UInt_t>(
't',itgt));
503 istr.getline(buf,512);
507 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
509 while (*p==
' ' || *p==
'\t') p++;
510 if (*p==
'#' || *p==
'\0') {
511 istr.getline(buf,512);
514 std::stringstream sstr(buf);
516 for (
UInt_t ivar=0;ivar<nvars;ivar++) {
517 istr.getline(buf2,512);
518 std::stringstream sstr2(buf2);
519 sstr2 >> fMin[icls][ivar] >> fMax[icls][ivar];
521 for (
UInt_t itgt=0;itgt<ntgts;itgt++) {
522 istr.getline(buf2,512);
523 std::stringstream sstr2(buf2);
524 sstr2 >> fMin[icls][nvars+itgt] >> fMax[icls][nvars+itgt];
526 istr.getline(buf,512);
536 Int_t nCls = GetNClasses();
538 if (nCls <= 1 ) numC = 1;
539 for (
Int_t icls = 0; icls < numC; icls++ ) {
541 Log() << kINFO <<
"Transformation for all classes based on these ranges:" <<
Endl;
543 Log() << kINFO <<
"Transformation for class " << icls <<
" based on these ranges:" <<
Endl;
545 for(
ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ){
547 UInt_t idx = (*itGet).second;
549 TString typeString = (
type==
'v'?
"Variable: ": (
type==
't'?
"Target : ":
"Spectator : ") );
550 Log() << typeString.
Data() << std::setw(20) << fMin[icls][idx] << std::setw(20) << fMax[icls][idx] <<
Endl;
564 UInt_t nVar = fGet.size();
565 UInt_t numC = fMin.size();
568 fout <<
" double fOff_" << trCounter <<
"[" << numC <<
"][" << nVar <<
"];" << std::endl;
569 fout <<
" double fScal_" << trCounter <<
"[" << numC <<
"][" << nVar <<
"];" << std::endl;
574 fout <<
"//_______________________________________________________________________" << std::endl;
575 fout <<
"inline void " << fcncName <<
"::InitTransform_" << trCounter <<
"()" << std::endl;
576 fout <<
"{" << std::endl;
577 fout <<
" double fMin_" << trCounter <<
"[" << numC <<
"][" << nVar <<
"];" << std::endl;
578 fout <<
" double fMax_" << trCounter <<
"[" << numC <<
"][" << nVar <<
"];" << std::endl;
579 fout <<
" // Normalization transformation, initialisation" << std::endl;
580 for (
UInt_t ivar = 0; ivar < nVar; ivar++) {
581 for (
UInt_t icls = 0; icls < numC; icls++) {
584 fout <<
" fMin_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"] = " << std::setprecision(12) << min
586 fout <<
" fMax_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"] = " << std::setprecision(12) << max
588 fout <<
" fScal_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"] = 2.0/(fMax_" << trCounter <<
"["
589 << icls <<
"][" << ivar <<
"]-fMin_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"]);" << std::endl;
590 fout <<
" fOff_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"] = fMin_" << trCounter <<
"[" << icls
591 <<
"][" << ivar <<
"]*fScal_" << trCounter <<
"[" << icls <<
"][" << ivar <<
"]+1.;" << std::endl;
594 fout <<
"}" << std::endl;
596 fout <<
"//_______________________________________________________________________" << std::endl;
597 fout <<
"inline void " << fcncName <<
"::Transform_" << trCounter <<
"( std::vector<double>& iv, int cls) const"
599 fout <<
"{" << std::endl;
600 fout <<
" // Normalization transformation" << std::endl;
601 fout <<
" if (cls < 0 || cls > " << GetNClasses() <<
") {" << std::endl;
602 fout <<
" if (" << GetNClasses() <<
" > 1 ) cls = " << GetNClasses() <<
";" << std::endl;
603 fout <<
" else cls = " << (fMin.size() == 1 ? 0 : 2) <<
";" << std::endl;
604 fout <<
" }" << std::endl;
605 fout <<
" const int nVar = " << nVar <<
";" << std::endl << std::endl;
606 fout <<
" // get indices of used variables" << std::endl;
608 fout <<
" static std::vector<double> dv;"
610 fout <<
" dv.resize(nVar);" << std::endl;
611 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
613 fout <<
" for (int ivar=0;ivar<" << nVar <<
";ivar++) {" << std::endl;
614 fout <<
" double offset = fOff_" << trCounter <<
"[cls][ivar];" << std::endl;
615 fout <<
" double scale = fScal_" << trCounter <<
"[cls][ivar];" << std::endl;
616 fout <<
" iv[indicesPut.at(ivar)] = scale*dv[ivar]-offset;" << std::endl;
617 fout <<
" }" << std::endl;
618 fout <<
"}" << std::endl;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
char * Form(const char *fmt,...)
Class that contains all the data information.
Singleton class for Global types used by TMVA.
Class for type info of MVA input variable.
const TString & GetLabel() const
const char * Data() const
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Short_t Min(Short_t a, Short_t b)
static void output(int code)