62 Types::EVariableTransform tf,
68 fBackTransformedEvent(0),
69 fVariableTransform(tf),
73 fTransformName(trfName),
74 fVariableTypesAreCounted(false),
83 for (
UInt_t ivar = 0; ivar < fDsi.GetNVariables(); ivar++) {
84 fVariables.push_back(
VariableInfo( fDsi.GetVariableInfo(ivar) ) );
86 for (
UInt_t itgt = 0; itgt < fDsi.GetNTargets(); itgt++) {
87 fTargets.push_back(
VariableInfo( fDsi.GetTargetInfo(itgt) ) );
89 for (
UInt_t ispct = 0; ispct < fDsi.GetNSpectators(); ispct++) {
90 fTargets.push_back(
VariableInfo( fDsi.GetSpectatorInfo(ispct) ) );
109 TString inputVariables = _inputVariables;
118 typedef std::set<Int_t> SelectedIndices;
120 SelectedIndices varIndices;
121 SelectedIndices tgtIndices;
122 SelectedIndices spctIndices;
124 if (inputVariables ==
"")
126 inputVariables =
"_V_,_T_";
141 if( variables.
Length() == 0 ){
142 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
143 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
144 varIndices.insert( ivar );
149 Log() <<
kFATAL <<
"You selected variable with index : " << idx <<
" of only " << nvars <<
" variables." <<
Endl;
150 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
151 varIndices.insert( idx );
155 if( variables.
Length() == 0 ){
156 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
157 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
158 tgtIndices.insert( itgt );
163 Log() <<
kFATAL <<
"You selected target with index : " << idx <<
" of only " << ntgts <<
" targets." <<
Endl;
164 fGet.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
165 tgtIndices.insert( idx );
169 if( variables.
Length() == 0 ){
170 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
171 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
172 spctIndices.insert( ispct );
177 Log() <<
kFATAL <<
"You selected spectator with index : " << idx <<
" of only " << nspcts <<
" spectators." <<
Endl;
178 fGet.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
179 spctIndices.insert( idx );
184 Log() <<
kINFO <<
"Variable rearrangement set true: Variable order given in transformation option is used for input to transformation!" <<
Endl;
188 Int_t numIndices = varIndices.size()+tgtIndices.size()+spctIndices.size();
189 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
191 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
192 varIndices.insert( ivar );
196 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
198 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
199 tgtIndices.insert( itgt );
203 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
205 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
206 spctIndices.insert( ispct );
210 Int_t numIndicesEndOfLoop = varIndices.size()+tgtIndices.size()+spctIndices.size();
211 if( numIndicesEndOfLoop == numIndices )
212 Log() <<
kWARNING <<
"Error at parsing the options for the variable transformations: Variable/Target/Spectator '" << variables.
Data() <<
"' not found." <<
Endl;
213 numIndices = numIndicesEndOfLoop;
218 if( putIntoVariables ) {
220 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
221 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
224 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
225 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
228 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
229 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
233 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
235 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
237 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
239 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
241 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
243 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
253 Log() <<
kHEADER <<
"Transformation, Variable selection : " <<
Endl;
262 for( ; itGet != itGetEnd; ++itGet ) {
265 Char_t inputType = (*itGet).first;
266 Int_t inputIdx = (*itGet).second;
268 TString inputLabel =
"NOT FOND";
269 if( inputType ==
'v' ) {
271 inputTypeString =
"variable";
273 else if( inputType ==
't' ){
275 inputTypeString =
"target";
277 else if( inputType ==
's' ){
279 inputTypeString =
"spectator";
282 TString outputTypeString =
"?";
284 Char_t outputType = (*itPut).first;
285 Int_t outputIdx = (*itPut).second;
287 TString outputLabel =
"NOT FOUND";
288 if( outputType ==
'v' ) {
290 outputTypeString =
"variable";
292 else if( outputType ==
't' ){
294 outputTypeString =
"target";
296 else if( outputType ==
's' ){
298 outputTypeString =
"spectator";
300 Log() <<
kINFO <<
"Input : " << inputTypeString.
Data() <<
" '" << inputLabel.
Data() <<
"'" <<
" <---> " <<
"Output : " << outputTypeString.
Data() <<
" '" << outputLabel.
Data() <<
"'" <<
Endl;
301 Log() <<
kDEBUG <<
"\t(index=" << inputIdx <<
")." <<
"\t(index=" << outputIdx <<
")." <<
Endl;
320 if( backTransformation && !
fPut.empty() ){
321 itEntry =
fPut.begin();
322 itEntryEnd =
fPut.end();
323 input.reserve(
fPut.size());
326 itEntry =
fGet.begin();
327 itEntryEnd =
fGet.end();
328 input.reserve(
fGet.size() );
333 for( ; itEntry != itEntryEnd; ++itEntry ) {
335 Int_t idx = (*itEntry).second;
340 input.push_back( event->
GetValue(idx) );
343 input.push_back( event->
GetTarget(idx) );
349 Log() <<
kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
353 catch(std::out_of_range& ){
354 input.push_back(0.
f);
355 mask.push_back(
kTRUE);
356 hasMaskedEntries =
kTRUE;
359 return hasMaskedEntries;
367 std::vector<Float_t>::iterator itOutput = output.begin();
368 std::vector<Char_t>::iterator itMask = mask.begin();
371 event->CopyVarValues( *oldEvent );
378 if( backTransformation ||
fPut.empty() ){
379 itEntry =
fGet.begin();
380 itEntryEnd =
fGet.end();
383 itEntry =
fPut.begin();
384 itEntryEnd =
fPut.end();
388 for( ; itEntry != itEntryEnd; ++itEntry ) {
395 Int_t idx = (*itEntry).second;
396 if (itOutput == output.end())
Log() <<
kFATAL <<
"Read beyond array boundaries in VariableTransformBase::SetOutput"<<
Endl;
401 event->SetVal( idx, value );
404 event->SetTarget( idx, value );
407 event->SetSpectator( idx, value );
410 Log() <<
kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
412 if( !(*itMask) ) ++itOutput;
416 }
catch( std::exception&
except ){
417 Log() <<
kFATAL <<
"VariableTransformBase/SetOutput : exception/" << except.what() <<
Endl;
435 nvars = ntgts = nspcts = 0;
451 Log() <<
kFATAL <<
"VariableTransformBase/GetVariableTypeNumbers : unknown type '" << type <<
"'." <<
Endl;
475 UInt_t nevts = events.size();
478 TVectorD x0( nvars+ntgts ); x0 *= 0;
479 TVectorD v0( nvars+ntgts ); v0 *= 0;
482 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
483 const Event* ev = events[ievt];
486 sumOfWeights += weight;
487 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
496 x0(ivar) += x*weight;
497 x2(ivar) += x*x*weight;
499 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
508 x0(nvars+itgt) += x*weight;
509 x2(nvars+itgt) += x*x*weight;
513 if (sumOfWeights <= 0) {
514 Log() <<
kFATAL <<
" the sum of event weights calcualted for your input is == 0" 515 <<
" or exactly: " << sumOfWeights <<
" there is obviously some problem..."<<
Endl;
519 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
520 Double_t mean = x0(ivar)/sumOfWeights;
523 if (
x2(ivar)/sumOfWeights - mean*mean < 0) {
524 Log() <<
kFATAL <<
" the RMS of your input variable " << ivar
525 <<
" evaluates to an imaginary number: sqrt("<<
x2(ivar)/sumOfWeights - mean*mean
526 <<
") .. sometimes related to a problem with outliers and negative event weights" 531 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
532 Double_t mean = x0(nvars+itgt)/sumOfWeights;
533 Targets().at(itgt).SetMean( mean );
534 if (
x2(nvars+itgt)/sumOfWeights - mean*mean < 0) {
535 Log() <<
kFATAL <<
" the RMS of your target variable " << itgt
536 <<
" evaluates to an imaginary number: sqrt(" <<
x2(nvars+itgt)/sumOfWeights - mean*mean
537 <<
") .. sometimes related to a problem with outliers and negative event weights" 543 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
544 const Event* ev = events[ievt];
546 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
549 v0(ivar) += weight*(x-mean)*(x-mean);
551 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
554 v0(nvars+itgt) += weight*(x-mean)*(x-mean);
560 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
561 Double_t variance = v0(ivar)/sumOfWeights;
562 Variables().at(ivar).SetVariance( variance );
563 Log() <<
kINFO <<
"Variable " <<
Variables().at(ivar).GetExpression() <<
" variance = " << variance <<
Endl;
565 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
566 Double_t variance = v0(nvars+itgt)/sumOfWeights;
567 Targets().at(itgt).SetVariance( variance );
568 Log() <<
kINFO <<
"Target " <<
Targets().at(itgt).GetExpression() <<
" variance = " << variance <<
Endl;
572 Log() << std::setprecision(3);
577 Log() << std::setprecision(3);
579 Log() <<
" " <<
Targets().at(itgt).GetInternalName()
580 <<
"\t: [" <<
Targets().at(itgt).GetMin() <<
"\t, " <<
Targets().at(itgt).GetMax() <<
"\t] " <<
Endl;
581 Log() << std::setprecision(5);
591 std::vector<TString>* strVec =
new std::vector<TString>;
593 strVec->push_back(
Variables()[ivar].GetLabel() +
"_[transformed]");
610 if (x <
Targets().at(ivar-nvars).GetMin())
Targets().at(ivar-nvars).SetMin(x);
611 if (x >
Targets().at(ivar-nvars).GetMax())
Targets().at(ivar-nvars).SetMax(x);
629 UInt_t idx = (*itGet).second;
637 typeString =
"Variable";
642 typeString =
"Target";
647 typeString =
"Spectator";
652 Log() <<
kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" << type <<
"'." <<
Endl;
667 UInt_t idx = (*itPut).second;
675 typeString =
"Variable";
680 typeString =
"Target";
685 typeString =
"Spectator";
690 Log() <<
kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" << type <<
"'." <<
Endl;
733 if( typeString ==
"Variable" ){
734 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
737 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
741 }
else if( typeString ==
"Target" ){
742 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
745 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
749 }
else if( typeString ==
"Spectator" ){
750 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
753 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
758 Log() <<
kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
763 assert( nInputs ==
fGet.size() );
781 if( typeString ==
"Variable" ){
782 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
785 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
789 }
else if( typeString ==
"Target" ){
790 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
793 fPut.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
797 }
else if( typeString ==
"Spectator" ){
798 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
801 fPut.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
806 Log() <<
kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
811 assert( nOutputs ==
fPut.size() );
825 fout <<
" // define the indices of the variables which are transformed by this transformation" << std::endl;
826 fout <<
" static std::vector<int> indicesGet;" << std::endl;
827 fout <<
" static std::vector<int> indicesPut;" << std::endl << std::endl;
828 fout <<
" if ( indicesGet.empty() ) { " << std::endl;
829 fout <<
" indicesGet.reserve(fNvars);" << std::endl;
833 Int_t idx = (*itEntry).second;
837 fout <<
" indicesGet.push_back( " << idx <<
");" << std::endl;
840 Log() <<
kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
843 Log() <<
kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
846 Log() <<
kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
849 fout <<
" } " << std::endl;
850 fout <<
" if ( indicesPut.empty() ) { " << std::endl;
851 fout <<
" indicesPut.reserve(fNvars);" << std::endl;
855 Int_t idx = (*itEntry).second;
859 fout <<
" indicesPut.push_back( " << idx <<
");" << std::endl;
862 Log() <<
kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
865 Log() <<
kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
868 Log() <<
kFATAL <<
"VariableTransformBase/PutInput : unknown type '" << type <<
"'." <<
Endl;
872 fout <<
" } " << std::endl;
875 }
else if( part == 1){
UInt_t GetNVariables() const
#define TMVA_VERSION_CODE
MsgLogger & Endl(MsgLogger &ml)
Collectable string class.
const TString & GetLabel() const
const TString & GetExpression() const
static const double x2[5]
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Float_t GetTarget(UInt_t itgt) const
VariableInfo & GetTargetInfo(Int_t i)
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
TString & Remove(Ssiz_t pos)
VariableInfo & GetSpectatorInfo(Int_t i)
VariableInfo & GetVariableInfo(Int_t i)
Mother of all ROOT objects.
Abstract ClassifierFactory template that handles arbitrary types.
Int_t Atoi() const
Return integer value of string.
Double_t Sqrt(Double_t x)
Float_t GetSpectator(UInt_t ivar) const
return spectator content
void variables(TString dataset, TString fin="TMVA.root", TString dirName="InputVariables_Id", TString title="TMVA Input Variables", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
const char * Data() const