74 fBackTransformedEvent(0),
75 fVariableTransform(tf),
79 fTransformName(trfName),
80 fVariableTypesAreCounted(false),
104 if (fTransformedEvent!=0)
delete fTransformedEvent;
105 if (fBackTransformedEvent!=0)
delete fBackTransformedEvent;
115 TString inputVariables = _inputVariables;
120 UInt_t nvars = GetNVariables();
121 UInt_t ntgts = GetNTargets();
122 UInt_t nspcts = GetNSpectators();
124 typedef std::set<Int_t> SelectedIndices;
126 SelectedIndices varIndices;
127 SelectedIndices tgtIndices;
128 SelectedIndices spctIndices;
130 if (inputVariables ==
"")
132 inputVariables =
"_V_,_T_";
148 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
149 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
150 varIndices.insert( ivar );
155 Log() << kFATAL <<
"You selected variable with index : " << idx <<
" of only " << nvars <<
" variables." <<
Endl;
156 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
157 varIndices.insert( idx );
162 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
163 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
164 tgtIndices.insert( itgt );
169 Log() << kFATAL <<
"You selected target with index : " << idx <<
" of only " << ntgts <<
" targets." <<
Endl;
170 fGet.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
171 tgtIndices.insert( idx );
176 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
177 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
178 spctIndices.insert( ispct );
183 Log() << kFATAL <<
"You selected spectator with index : " << idx <<
" of only " << nspcts <<
" spectators." <<
Endl;
184 fGet.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
185 spctIndices.insert( idx );
188 ToggleInputSortOrder(
kFALSE );
190 Log() << kINFO <<
"Variable rearrangement set true: Variable order given in transformation option is used for input to transformation!" <<
Endl;
194 Int_t numIndices = varIndices.size()+tgtIndices.size()+spctIndices.size();
195 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
196 if( fDsi.GetVariableInfo( ivar ).GetLabel() ==
variables ) {
197 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
198 varIndices.insert( ivar );
202 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
203 if( fDsi.GetTargetInfo( itgt ).GetLabel() ==
variables ) {
204 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
205 tgtIndices.insert( itgt );
209 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
210 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() ==
variables ) {
211 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
212 spctIndices.insert( ispct );
216 Int_t numIndicesEndOfLoop = varIndices.size()+tgtIndices.size()+spctIndices.size();
217 if( numIndicesEndOfLoop == numIndices )
218 Log() << kWARNING <<
"Error at parsing the options for the variable transformations: Variable/Target/Spectator '" <<
variables.Data() <<
"' not found." <<
Endl;
219 numIndices = numIndicesEndOfLoop;
224 if( putIntoVariables ) {
226 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
227 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
230 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
231 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
234 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
235 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
239 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
241 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
243 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
245 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
247 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
249 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
255 fGet.assign( fPut.begin(), fPut.end() );
259 Log() << kHEADER <<
"Transformation, Variable selection : " <<
Endl;
262 const DataSetInfo* outputDsiPtr = (fDsiOutput? &(*fDsiOutput) : &fDsi );
266 ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end();
268 for( ; itGet != itGetEnd; ++itGet ) {
271 Char_t inputType = (*itGet).first;
272 Int_t inputIdx = (*itGet).second;
274 TString inputLabel =
"NOT FOND";
275 if( inputType ==
'v' ) {
276 inputLabel = fDsi.GetVariableInfo( inputIdx ).GetLabel();
277 inputTypeString =
"variable";
279 else if( inputType ==
't' ){
280 inputLabel = fDsi.GetTargetInfo( inputIdx ).GetLabel();
281 inputTypeString =
"target";
283 else if( inputType ==
's' ){
284 inputLabel = fDsi.GetSpectatorInfo( inputIdx ).GetLabel();
285 inputTypeString =
"spectator";
288 TString outputTypeString =
"?";
290 Char_t outputType = (*itPut).first;
291 Int_t outputIdx = (*itPut).second;
293 TString outputLabel =
"NOT FOUND";
294 if( outputType ==
'v' ) {
296 outputTypeString =
"variable";
298 else if( outputType ==
't' ){
300 outputTypeString =
"target";
302 else if( outputType ==
's' ){
304 outputTypeString =
"spectator";
306 Log() << kINFO <<
"Input : " << inputTypeString.
Data() <<
" '" << inputLabel.
Data() <<
"'" <<
" <---> " <<
"Output : " << outputTypeString.
Data() <<
" '" << outputLabel.
Data() <<
"'" <<
Endl;
307 Log() << kDEBUG <<
"\t(index=" << inputIdx <<
")." <<
"\t(index=" << outputIdx <<
")." <<
Endl;
326 if( backTransformation && !fPut.empty() ){
327 itEntry = fPut.begin();
328 itEntryEnd = fPut.end();
329 input.reserve(fPut.size());
332 itEntry = fGet.begin();
333 itEntryEnd = fGet.end();
334 input.reserve(fGet.size() );
339 for( ; itEntry != itEntryEnd; ++itEntry ) {
341 Int_t idx = (*itEntry).second;
346 input.push_back( event->
GetValue(idx) );
349 input.push_back( event->
GetTarget(idx) );
355 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" <<
type <<
"'." <<
Endl;
359 catch(std::out_of_range& ){
360 input.push_back(0.f);
361 mask.push_back(
kTRUE);
362 hasMaskedEntries =
kTRUE;
365 return hasMaskedEntries;
373 std::vector<Float_t>::iterator itOutput =
output.begin();
374 std::vector<Char_t>::iterator itMask = mask.begin();
377 event->CopyVarValues( *oldEvent );
384 if( backTransformation || fPut.empty() ){
385 itEntry = fGet.begin();
386 itEntryEnd = fGet.end();
389 itEntry = fPut.begin();
390 itEntryEnd = fPut.end();
394 for( ; itEntry != itEntryEnd; ++itEntry ) {
401 Int_t idx = (*itEntry).second;
402 if (itOutput ==
output.end())
Log() << kFATAL <<
"Read beyond array boundaries in VariableTransformBase::SetOutput"<<
Endl;
407 event->SetVal( idx, value );
410 event->SetTarget( idx, value );
413 event->SetSpectator( idx, value );
416 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" <<
type <<
"'." <<
Endl;
418 if( !(*itMask) ) ++itOutput;
422 }
catch( std::exception& except ){
423 Log() << kFATAL <<
"VariableTransformBase/SetOutput : exception/" << except.what() <<
Endl;
434 if( fVariableTypesAreCounted ){
437 nspcts = fNSpectators;
441 nvars = ntgts = nspcts = 0;
443 for(
ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
457 Log() << kFATAL <<
"VariableTransformBase/GetVariableTypeNumbers : unknown type '" <<
type <<
"'." <<
Endl;
463 fNSpectators = nspcts;
465 fVariableTypesAreCounted =
true;
475 if (!IsCreated())
return;
477 const UInt_t nvars = GetNVariables();
478 const UInt_t ntgts = GetNTargets();
480 UInt_t nevts = events.size();
483 TVectorD x0( nvars+ntgts ); x0 *= 0;
487 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
488 const Event* ev = events[ievt];
491 sumOfWeights += weight;
492 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
495 Variables().at(ivar).SetMin(
x);
496 Variables().at(ivar).SetMax(
x);
499 UpdateNorm( ivar,
x );
501 x0(ivar) +=
x*weight;
502 x2(ivar) +=
x*
x*weight;
504 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
507 Targets().at(itgt).SetMin(
x);
508 Targets().at(itgt).SetMax(
x);
511 UpdateNorm( nvars+itgt,
x );
513 x0(nvars+itgt) +=
x*weight;
514 x2(nvars+itgt) +=
x*
x*weight;
518 if (sumOfWeights <= 0) {
519 Log() << kFATAL <<
" the sum of event weights calculated for your input is == 0"
520 <<
" or exactly: " << sumOfWeights <<
" there is obviously some problem..."<<
Endl;
524 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
525 Double_t mean = x0(ivar)/sumOfWeights;
527 Variables().at(ivar).SetMean( mean );
528 if (
x2(ivar)/sumOfWeights - mean*mean < 0) {
529 Log() << kFATAL <<
" the RMS of your input variable " << ivar
530 <<
" evaluates to an imaginary number: sqrt("<<
x2(ivar)/sumOfWeights - mean*mean
531 <<
") .. sometimes related to a problem with outliers and negative event weights"
534 Variables().at(ivar).SetRMS(
TMath::Sqrt(
x2(ivar)/sumOfWeights - mean*mean) );
536 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
537 Double_t mean = x0(nvars+itgt)/sumOfWeights;
538 Targets().at(itgt).SetMean( mean );
539 if (
x2(nvars+itgt)/sumOfWeights - mean*mean < 0) {
540 Log() << kFATAL <<
" the RMS of your target variable " << itgt
541 <<
" evaluates to an imaginary number: sqrt(" <<
x2(nvars+itgt)/sumOfWeights - mean*mean
542 <<
") .. sometimes related to a problem with outliers and negative event weights"
545 Targets().at(itgt).SetRMS(
TMath::Sqrt(
x2(nvars+itgt)/sumOfWeights - mean*mean) );
548 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
549 const Event* ev = events[ievt];
551 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
553 Double_t mean = Variables().at(ivar).GetMean();
554 v0(ivar) += weight*(
x-mean)*(
x-mean);
556 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
558 Double_t mean = Targets().at(itgt).GetMean();
559 v0(nvars+itgt) += weight*(
x-mean)*(
x-mean);
565 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
567 Variables().at(ivar).SetVariance( variance );
568 Log() << kINFO <<
"Variable " << Variables().at(ivar).GetExpression() <<
" variance = " << variance <<
Endl;
570 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
571 Double_t variance =
v0(nvars+itgt)/sumOfWeights;
572 Targets().at(itgt).SetVariance( variance );
573 Log() << kINFO <<
"Target " << Targets().at(itgt).GetExpression() <<
" variance = " << variance <<
Endl;
576 Log() << kVERBOSE <<
"Set minNorm/maxNorm for variables to: " <<
Endl;
577 Log() << std::setprecision(3);
578 for (
UInt_t ivar=0; ivar<GetNVariables(); ivar++)
579 Log() <<
" " << Variables().at(ivar).GetInternalName()
580 <<
"\t: [" << Variables().at(ivar).GetMin() <<
"\t, " << Variables().at(ivar).GetMax() <<
"\t] " <<
Endl;
581 Log() << kVERBOSE <<
"Set minNorm/maxNorm for targets to: " <<
Endl;
582 Log() << std::setprecision(3);
583 for (
UInt_t itgt=0; itgt<GetNTargets(); itgt++)
584 Log() <<
" " << Targets().at(itgt).GetInternalName()
585 <<
"\t: [" << Targets().at(itgt).GetMin() <<
"\t, " << Targets().at(itgt).GetMax() <<
"\t] " <<
Endl;
586 Log() << std::setprecision(5);
596 std::vector<TString>* strVec =
new std::vector<TString>;
597 for (
UInt_t ivar=0; ivar<GetNVariables(); ivar++) {
598 strVec->push_back( Variables()[ivar].GetLabel() +
"_[transformed]");
610 Int_t nvars = fDsi.GetNVariables();
612 if (
x < Variables().at(ivar).GetMin()) Variables().at(ivar).SetMin(
x);
613 if (
x > Variables().at(ivar).GetMax()) Variables().at(ivar).SetMax(
x);
615 if (
x < Targets().at(ivar-nvars).GetMin()) Targets().at(ivar-nvars).SetMin(
x);
616 if (
x > Targets().at(ivar-nvars).GetMax()) Targets().at(ivar-nvars).SetMax(
x);
631 const DataSetInfo* outputDsiPtr = (fDsiOutput? fDsiOutput : &fDsi );
633 for(
ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
634 UInt_t idx = (*itGet).second;
642 typeString =
"Variable";
643 label = fDsi.GetVariableInfo( idx ).GetLabel();
644 expression = fDsi.GetVariableInfo( idx ).GetExpression();
647 typeString =
"Target";
648 label = fDsi.GetTargetInfo( idx ).GetLabel();
649 expression = fDsi.GetTargetInfo( idx ).GetExpression();
652 typeString =
"Spectator";
653 label = fDsi.GetSpectatorInfo( idx ).GetLabel();
654 expression = fDsi.GetSpectatorInfo( idx ).GetExpression();
657 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" <<
type <<
"'." <<
Endl;
671 for(
ItVarTypeIdx itPut = fPut.begin(), itPutEnd = fPut.end(); itPut != itPutEnd; ++itPut ) {
672 UInt_t idx = (*itPut).second;
680 typeString =
"Variable";
685 typeString =
"Target";
690 typeString =
"Spectator";
695 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" <<
type <<
"'." <<
Endl;
716 UInt_t nvars = GetNVariables();
717 UInt_t ntgts = GetNTargets();
718 UInt_t nspcts = GetNSpectators();
736 if( typeString ==
"Variable" ){
737 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
738 if( fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
739 fDsi.GetVariableInfo( ivar ).GetExpression() == expression) {
740 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
744 }
else if( typeString ==
"Target" ){
745 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
746 if( fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
747 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
748 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
752 }
else if( typeString ==
"Spectator" ){
753 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
754 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
755 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
756 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
761 Log() << kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
766 assert( nInputs == fGet.size() );
784 if( typeString ==
"Variable" ){
785 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
786 if( fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
787 fDsi.GetVariableInfo( ivar ).GetExpression() == expression ) {
788 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
792 }
else if( typeString ==
"Target" ){
793 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
794 if( fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
795 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
796 fPut.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
800 }
else if( typeString ==
"Spectator" ){
801 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
802 if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
803 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
804 fPut.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
809 Log() << kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
814 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;
831 for(
ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
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;
853 for(
ItVarTypeIdxConst itEntry = fPut.begin(), itEntryEnd = fPut.end(); itEntry != itEntryEnd; ++itEntry ) {
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){
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
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)