71 fBackTransformedEvent(0),
72 fVariableTransform(tf),
76 fTransformName(trfName),
77 fVariableTypesAreCounted(false),
101 if (fTransformedEvent!=0)
delete fTransformedEvent;
102 if (fBackTransformedEvent!=0)
delete fBackTransformedEvent;
112 TString inputVariables = _inputVariables;
117 UInt_t nvars = GetNVariables();
118 UInt_t ntgts = GetNTargets();
119 UInt_t nspcts = GetNSpectators();
121 typedef std::set<Int_t> SelectedIndices;
123 SelectedIndices varIndices;
124 SelectedIndices tgtIndices;
125 SelectedIndices spctIndices;
127 if (inputVariables ==
"")
129 inputVariables =
"_V_,_T_";
145 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
146 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
147 varIndices.insert( ivar );
152 Log() << kFATAL <<
"You selected variable with index : " << idx <<
" of only " << nvars <<
" variables." <<
Endl;
153 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
154 varIndices.insert( idx );
159 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
160 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
161 tgtIndices.insert( itgt );
166 Log() << kFATAL <<
"You selected target with index : " << idx <<
" of only " << ntgts <<
" targets." <<
Endl;
167 fGet.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
168 tgtIndices.insert( idx );
173 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
174 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
175 spctIndices.insert( ispct );
180 Log() << kFATAL <<
"You selected spectator with index : " << idx <<
" of only " << nspcts <<
" spectators." <<
Endl;
181 fGet.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
182 spctIndices.insert( idx );
185 ToggleInputSortOrder(
kFALSE );
187 Log() << kINFO <<
"Variable rearrangement set true: Variable order given in transformation option is used for input to transformation!" <<
Endl;
191 Int_t numIndices = varIndices.size()+tgtIndices.size()+spctIndices.size();
192 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
193 if( fDsi.GetVariableInfo( ivar ).GetLabel() ==
variables ) {
194 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
195 varIndices.insert( ivar );
199 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
200 if( fDsi.GetTargetInfo( itgt ).GetLabel() ==
variables ) {
201 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
202 tgtIndices.insert( itgt );
206 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
207 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() ==
variables ) {
208 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
209 spctIndices.insert( ispct );
213 Int_t numIndicesEndOfLoop = varIndices.size()+tgtIndices.size()+spctIndices.size();
214 if( numIndicesEndOfLoop == numIndices )
215 Log() << kWARNING <<
"Error at parsing the options for the variable transformations: Variable/Target/Spectator '" <<
variables.Data() <<
"' not found." <<
Endl;
216 numIndices = numIndicesEndOfLoop;
221 if( putIntoVariables ) {
223 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
224 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
227 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
228 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
231 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
232 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
236 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
238 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
240 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
242 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
244 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
246 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
252 fGet.assign( fPut.begin(), fPut.end() );
256 Log() << kHEADER <<
"Transformation, Variable selection : " <<
Endl;
259 const DataSetInfo* outputDsiPtr = (fDsiOutput? &(*fDsiOutput) : &fDsi );
263 ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end();
265 for( ; itGet != itGetEnd; ++itGet ) {
268 Char_t inputType = (*itGet).first;
269 Int_t inputIdx = (*itGet).second;
271 TString inputLabel =
"NOT FOND";
272 if( inputType ==
'v' ) {
273 inputLabel = fDsi.GetVariableInfo( inputIdx ).GetLabel();
274 inputTypeString =
"variable";
276 else if( inputType ==
't' ){
277 inputLabel = fDsi.GetTargetInfo( inputIdx ).GetLabel();
278 inputTypeString =
"target";
280 else if( inputType ==
's' ){
281 inputLabel = fDsi.GetSpectatorInfo( inputIdx ).GetLabel();
282 inputTypeString =
"spectator";
285 TString outputTypeString =
"?";
287 Char_t outputType = (*itPut).first;
288 Int_t outputIdx = (*itPut).second;
290 TString outputLabel =
"NOT FOUND";
291 if( outputType ==
'v' ) {
293 outputTypeString =
"variable";
295 else if( outputType ==
't' ){
297 outputTypeString =
"target";
299 else if( outputType ==
's' ){
301 outputTypeString =
"spectator";
303 Log() << kINFO <<
"Input : " << inputTypeString.
Data() <<
" '" << inputLabel.
Data() <<
"'" <<
" <---> " <<
"Output : " << outputTypeString.
Data() <<
" '" << outputLabel.
Data() <<
"'" <<
Endl;
304 Log() << kDEBUG <<
"\t(index=" << inputIdx <<
")." <<
"\t(index=" << outputIdx <<
")." <<
Endl;
323 if( backTransformation && !fPut.empty() ){
324 itEntry = fPut.begin();
325 itEntryEnd = fPut.end();
326 input.reserve(fPut.size());
329 itEntry = fGet.begin();
330 itEntryEnd = fGet.end();
331 input.reserve(fGet.size() );
336 for( ; itEntry != itEntryEnd; ++itEntry ) {
338 Int_t idx = (*itEntry).second;
343 input.push_back( event->
GetValue(idx) );
346 input.push_back( event->
GetTarget(idx) );
352 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" <<
type <<
"'." <<
Endl;
356 catch(std::out_of_range& ){
357 input.push_back(0.f);
358 mask.push_back(
kTRUE);
359 hasMaskedEntries =
kTRUE;
362 return hasMaskedEntries;
370 std::vector<Float_t>::iterator itOutput =
output.begin();
371 std::vector<Char_t>::iterator itMask = mask.begin();
374 event->CopyVarValues( *oldEvent );
381 if( backTransformation || fPut.empty() ){
382 itEntry = fGet.begin();
383 itEntryEnd = fGet.end();
386 itEntry = fPut.begin();
387 itEntryEnd = fPut.end();
391 for( ; itEntry != itEntryEnd; ++itEntry ) {
398 Int_t idx = (*itEntry).second;
399 if (itOutput ==
output.end()) Log() << kFATAL <<
"Read beyond array boundaries in VariableTransformBase::SetOutput"<<
Endl;
404 event->SetVal( idx, value );
407 event->SetTarget( idx, value );
410 event->SetSpectator( idx, value );
413 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" <<
type <<
"'." <<
Endl;
415 if( !(*itMask) ) ++itOutput;
419 }
catch( std::exception& except ){
420 Log() << kFATAL <<
"VariableTransformBase/SetOutput : exception/" << except.what() <<
Endl;
431 if( fVariableTypesAreCounted ){
434 nspcts = fNSpectators;
438 nvars = ntgts = nspcts = 0;
440 for(
ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
454 Log() << kFATAL <<
"VariableTransformBase/GetVariableTypeNumbers : unknown type '" <<
type <<
"'." <<
Endl;
460 fNSpectators = nspcts;
462 fVariableTypesAreCounted =
true;
472 if (!IsCreated())
return;
474 const UInt_t nvars = GetNVariables();
475 const UInt_t ntgts = GetNTargets();
477 UInt_t nevts = events.size();
480 TVectorD x0( nvars+ntgts ); x0 *= 0;
484 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
485 const Event* ev = events[ievt];
488 sumOfWeights += weight;
489 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
492 Variables().at(ivar).SetMin(
x);
493 Variables().at(ivar).SetMax(
x);
496 UpdateNorm( ivar,
x );
498 x0(ivar) +=
x*weight;
499 x2(ivar) +=
x*
x*weight;
501 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
504 Targets().at(itgt).SetMin(
x);
505 Targets().at(itgt).SetMax(
x);
508 UpdateNorm( nvars+itgt,
x );
510 x0(nvars+itgt) +=
x*weight;
511 x2(nvars+itgt) +=
x*
x*weight;
515 if (sumOfWeights <= 0) {
516 Log() << kFATAL <<
" the sum of event weights calculated for your input is == 0"
517 <<
" or exactly: " << sumOfWeights <<
" there is obviously some problem..."<<
Endl;
521 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
522 Double_t mean = x0(ivar)/sumOfWeights;
524 Variables().at(ivar).SetMean( mean );
525 if (
x2(ivar)/sumOfWeights - mean*mean < 0) {
526 Log() << kFATAL <<
" the RMS of your input variable " << ivar
527 <<
" evaluates to an imaginary number: sqrt("<<
x2(ivar)/sumOfWeights - mean*mean
528 <<
") .. sometimes related to a problem with outliers and negative event weights"
531 Variables().at(ivar).SetRMS(
TMath::Sqrt(
x2(ivar)/sumOfWeights - mean*mean) );
533 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
534 Double_t mean = x0(nvars+itgt)/sumOfWeights;
535 Targets().at(itgt).SetMean( mean );
536 if (
x2(nvars+itgt)/sumOfWeights - mean*mean < 0) {
537 Log() << kFATAL <<
" the RMS of your target variable " << itgt
538 <<
" evaluates to an imaginary number: sqrt(" <<
x2(nvars+itgt)/sumOfWeights - mean*mean
539 <<
") .. sometimes related to a problem with outliers and negative event weights"
542 Targets().at(itgt).SetRMS(
TMath::Sqrt(
x2(nvars+itgt)/sumOfWeights - mean*mean) );
545 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
546 const Event* ev = events[ievt];
548 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
550 Double_t mean = Variables().at(ivar).GetMean();
551 v0(ivar) += weight*(
x-mean)*(
x-mean);
553 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
555 Double_t mean = Targets().at(itgt).GetMean();
556 v0(nvars+itgt) += weight*(
x-mean)*(
x-mean);
562 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
564 Variables().at(ivar).SetVariance( variance );
565 Log() << kINFO <<
"Variable " << Variables().at(ivar).GetExpression() <<
" variance = " << variance <<
Endl;
567 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
568 Double_t variance =
v0(nvars+itgt)/sumOfWeights;
569 Targets().at(itgt).SetVariance( variance );
570 Log() << kINFO <<
"Target " << Targets().at(itgt).GetExpression() <<
" variance = " << variance <<
Endl;
573 Log() << kVERBOSE <<
"Set minNorm/maxNorm for variables to: " <<
Endl;
574 Log() << std::setprecision(3);
575 for (
UInt_t ivar=0; ivar<GetNVariables(); ivar++)
576 Log() <<
" " << Variables().at(ivar).GetInternalName()
577 <<
"\t: [" << Variables().at(ivar).GetMin() <<
"\t, " << Variables().at(ivar).GetMax() <<
"\t] " <<
Endl;
578 Log() << kVERBOSE <<
"Set minNorm/maxNorm for targets to: " <<
Endl;
579 Log() << std::setprecision(3);
580 for (
UInt_t itgt=0; itgt<GetNTargets(); itgt++)
581 Log() <<
" " << Targets().at(itgt).GetInternalName()
582 <<
"\t: [" << Targets().at(itgt).GetMin() <<
"\t, " << Targets().at(itgt).GetMax() <<
"\t] " <<
Endl;
583 Log() << std::setprecision(5);
593 std::vector<TString>* strVec =
new std::vector<TString>;
594 for (
UInt_t ivar=0; ivar<GetNVariables(); ivar++) {
595 strVec->push_back( Variables()[ivar].GetLabel() +
"_[transformed]");
607 Int_t nvars = fDsi.GetNVariables();
609 if (
x < Variables().at(ivar).GetMin()) Variables().at(ivar).SetMin(
x);
610 if (
x > Variables().at(ivar).GetMax()) Variables().at(ivar).SetMax(
x);
612 if (
x < Targets().at(ivar-nvars).GetMin()) Targets().at(ivar-nvars).SetMin(
x);
613 if (
x > Targets().at(ivar-nvars).GetMax()) Targets().at(ivar-nvars).SetMax(
x);
628 const DataSetInfo* outputDsiPtr = (fDsiOutput? fDsiOutput : &fDsi );
630 for(
ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
631 UInt_t idx = (*itGet).second;
639 typeString =
"Variable";
640 label = fDsi.GetVariableInfo( idx ).GetLabel();
641 expression = fDsi.GetVariableInfo( idx ).GetExpression();
644 typeString =
"Target";
645 label = fDsi.GetTargetInfo( idx ).GetLabel();
646 expression = fDsi.GetTargetInfo( idx ).GetExpression();
649 typeString =
"Spectator";
650 label = fDsi.GetSpectatorInfo( idx ).GetLabel();
651 expression = fDsi.GetSpectatorInfo( idx ).GetExpression();
654 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" <<
type <<
"'." <<
Endl;
668 for(
ItVarTypeIdx itPut = fPut.begin(), itPutEnd = fPut.end(); itPut != itPutEnd; ++itPut ) {
669 UInt_t idx = (*itPut).second;
677 typeString =
"Variable";
682 typeString =
"Target";
687 typeString =
"Spectator";
692 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" <<
type <<
"'." <<
Endl;
713 UInt_t nvars = GetNVariables();
714 UInt_t ntgts = GetNTargets();
715 UInt_t nspcts = GetNSpectators();
733 if( typeString ==
"Variable" ){
734 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
735 if( fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
736 fDsi.GetVariableInfo( ivar ).GetExpression() == expression) {
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 ) {
743 if( fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
744 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
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 ) {
751 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
752 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
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 ) {
783 if( fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
784 fDsi.GetVariableInfo( ivar ).GetExpression() == expression ) {
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 ) {
791 if( fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
792 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
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 ) {
799 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
800 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
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() );
822 fout <<
" // define the indices of the variables which are transformed by this transformation" << std::endl;
823 fout <<
" static std::vector<int> indicesGet;" << std::endl;
824 fout <<
" static std::vector<int> indicesPut;" << std::endl << std::endl;
825 fout <<
" if ( indicesGet.empty() ) {" << std::endl;
826 fout <<
" indicesGet.reserve(fNvars);" << std::endl;
828 for(
ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
830 Int_t idx = (*itEntry).second;
834 fout <<
" indicesGet.push_back( " << idx <<
");" << std::endl;
837 Log() << kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
840 Log() << kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
843 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" <<
type <<
"'." <<
Endl;
846 fout <<
" }" << std::endl;
847 fout <<
" if ( indicesPut.empty() ) {" << std::endl;
848 fout <<
" indicesPut.reserve(fNvars);" << std::endl;
850 for(
ItVarTypeIdxConst itEntry = fPut.begin(), itEntryEnd = fPut.end(); itEntry != itEntryEnd; ++itEntry ) {
852 Int_t idx = (*itEntry).second;
856 fout <<
" indicesPut.push_back( " << idx <<
");" << std::endl;
859 Log() << kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
862 Log() << kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
865 Log() << kFATAL <<
"VariableTransformBase/PutInput : unknown type '" <<
type <<
"'." <<
Endl;
869 fout <<
" }" << std::endl;
872 }
else if( part == 1){
static const double x2[5]
#define TMVA_VERSION_CODE
Class that contains all the data information.
UInt_t GetNVariables() const
UInt_t GetNSpectators(bool all=kTRUE) const
UInt_t GetNTargets() const
VariableInfo & GetVariableInfo(Int_t i)
VariableInfo & GetTargetInfo(Int_t i)
VariableInfo & GetSpectatorInfo(Int_t i)
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Float_t GetTarget(UInt_t itgt) const
ostringstream derivative to redirect and format output
Class for type info of MVA input variable.
const TString & GetLabel() const
const TString & GetExpression() const
Collectable string class.
Mother of all ROOT objects.
const char * Data() const
TString & Remove(Ssiz_t pos)
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)
MsgLogger & Endl(MsgLogger &ml)
Double_t Sqrt(Double_t x)
static void output(int code)