85 for (
UInt_t ivar = 0; ivar <
fDsi.GetNVariables(); ivar++) {
88 for (
UInt_t itgt = 0; itgt <
fDsi.GetNTargets(); itgt++) {
91 for (
UInt_t ispct = 0; ispct <
fDsi.GetNSpectators(); ispct++) {
111 TString inputVariables = _inputVariables;
120 typedef std::set<Int_t> SelectedIndices;
122 SelectedIndices varIndices;
123 SelectedIndices tgtIndices;
124 SelectedIndices spctIndices;
126 if (inputVariables ==
"")
128 inputVariables =
"_V_,_T_";
144 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
145 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
146 varIndices.insert( ivar );
151 Log() << kFATAL <<
"You selected variable with index : " << idx <<
" of only " << nvars <<
" variables." <<
Endl;
152 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
153 varIndices.insert( idx );
158 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
159 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
160 tgtIndices.insert( itgt );
165 Log() << kFATAL <<
"You selected target with index : " << idx <<
" of only " << ntgts <<
" targets." <<
Endl;
166 fGet.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
167 tgtIndices.insert( idx );
172 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
173 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
174 spctIndices.insert( ispct );
179 Log() << kFATAL <<
"You selected spectator with index : " << idx <<
" of only " << nspcts <<
" spectators." <<
Endl;
180 fGet.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
181 spctIndices.insert( idx );
186 Log() << kINFO <<
"Variable rearrangement set true: Variable order given in transformation option is used for input to transformation!" <<
Endl;
190 Int_t numIndices = varIndices.size()+tgtIndices.size()+spctIndices.size();
191 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
193 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
194 varIndices.insert( ivar );
198 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
200 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
201 tgtIndices.insert( itgt );
205 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
206 if(
fDsi.GetSpectatorInfo( ispct ).GetLabel() ==
variables ) {
207 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
208 spctIndices.insert( ispct );
212 Int_t numIndicesEndOfLoop = varIndices.size()+tgtIndices.size()+spctIndices.size();
213 if( numIndicesEndOfLoop == numIndices )
214 Log() << kWARNING <<
"Error at parsing the options for the variable transformations: Variable/Target/Spectator '" <<
variables.Data() <<
"' not found." <<
Endl;
215 numIndices = numIndicesEndOfLoop;
220 if( putIntoVariables ) {
222 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
223 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
226 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
227 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
230 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
231 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
235 for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
237 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',idx) );
239 for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
241 fPut.push_back( std::pair<Char_t,UInt_t>(
't',idx) );
243 for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
245 fPut.push_back( std::pair<Char_t,UInt_t>(
's',idx) );
255 Log() << kHEADER <<
"Transformation, Variable selection : " <<
Endl;
264 for( ; itGet != itGetEnd; ++itGet ) {
267 Char_t inputType = (*itGet).first;
268 Int_t inputIdx = (*itGet).second;
270 TString inputLabel =
"NOT FOND";
271 if( inputType ==
'v' ) {
272 inputLabel =
fDsi.GetVariableInfo( inputIdx ).GetLabel();
273 inputTypeString =
"variable";
275 else if( inputType ==
't' ){
276 inputLabel =
fDsi.GetTargetInfo( inputIdx ).GetLabel();
277 inputTypeString =
"target";
279 else if( inputType ==
's' ){
280 inputLabel =
fDsi.GetSpectatorInfo( inputIdx ).GetLabel();
281 inputTypeString =
"spectator";
284 TString outputTypeString =
"?";
286 Char_t outputType = (*itPut).first;
287 Int_t outputIdx = (*itPut).second;
289 TString outputLabel =
"NOT FOUND";
290 if( outputType ==
'v' ) {
292 outputTypeString =
"variable";
294 else if( outputType ==
't' ){
296 outputTypeString =
"target";
298 else if( outputType ==
's' ){
300 outputTypeString =
"spectator";
302 Log() << kINFO <<
"Input : " << inputTypeString.
Data() <<
" '" << inputLabel.
Data() <<
"'" <<
" <---> " <<
"Output : " << outputTypeString.
Data() <<
" '" << outputLabel.
Data() <<
"'" <<
Endl;
303 Log() << kDEBUG <<
"\t(index=" << inputIdx <<
")." <<
"\t(index=" << outputIdx <<
")." <<
Endl;
322 if( backTransformation && !
fPut.empty() ){
323 itEntry =
fPut.begin();
324 itEntryEnd =
fPut.end();
325 input.reserve(
fPut.size());
328 itEntry =
fGet.begin();
329 itEntryEnd =
fGet.end();
330 input.reserve(
fGet.size() );
335 for( ; itEntry != itEntryEnd; ++itEntry ) {
336 Char_t type = (*itEntry).first;
337 Int_t idx = (*itEntry).second;
342 input.push_back( event->
GetValue(idx) );
345 input.push_back( event->
GetTarget(idx) );
351 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
355 catch(std::out_of_range& ){
356 input.push_back(0.f);
357 mask.push_back(
kTRUE);
358 hasMaskedEntries =
kTRUE;
361 return hasMaskedEntries;
369 std::vector<Float_t>::iterator itOutput = output.begin();
370 std::vector<Char_t>::iterator itMask = mask.begin();
373 event->CopyVarValues( *oldEvent );
380 if( backTransformation ||
fPut.empty() ){
381 itEntry =
fGet.begin();
382 itEntryEnd =
fGet.end();
385 itEntry =
fPut.begin();
386 itEntryEnd =
fPut.end();
390 for( ; itEntry != itEntryEnd; ++itEntry ) {
396 Char_t type = (*itEntry).first;
397 Int_t idx = (*itEntry).second;
398 if (itOutput == output.end())
Log() << kFATAL <<
"Read beyond array boundaries in VariableTransformBase::SetOutput"<<
Endl;
403 event->SetVal( idx, value );
406 event->SetTarget( idx, value );
409 event->SetSpectator( idx, value );
412 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
414 if( !(*itMask) ) ++itOutput;
418 }
catch( std::exception& except ){
419 Log() << kFATAL <<
"VariableTransformBase/SetOutput : exception/" << except.what() <<
Endl;
437 nvars = ntgts = nspcts = 0;
440 Char_t type = (*itEntry).first;
453 Log() << kFATAL <<
"VariableTransformBase/GetVariableTypeNumbers : unknown type '" << type <<
"'." <<
Endl;
476 UInt_t nevts = events.size();
478 TVectorD x2( nvars+ntgts ); x2 *= 0;
479 TVectorD x0( nvars+ntgts ); x0 *= 0;
483 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
484 const Event* ev = events[ievt];
487 sumOfWeights += weight;
488 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
497 x0(ivar) +=
x*weight;
498 x2(ivar) +=
x*
x*weight;
500 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
509 x0(nvars+itgt) +=
x*weight;
510 x2(nvars+itgt) +=
x*
x*weight;
514 if (sumOfWeights <= 0) {
515 Log() << kFATAL <<
" the sum of event weights calculated for your input is == 0"
516 <<
" or exactly: " << sumOfWeights <<
" there is obviously some problem..."<<
Endl;
520 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
521 Double_t mean = x0(ivar)/sumOfWeights;
524 if (x2(ivar)/sumOfWeights - mean*mean < 0) {
525 Log() << kFATAL <<
" the RMS of your input variable " << ivar
526 <<
" evaluates to an imaginary number: sqrt("<< x2(ivar)/sumOfWeights - mean*mean
527 <<
") .. sometimes related to a problem with outliers and negative event weights"
532 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
533 Double_t mean = x0(nvars+itgt)/sumOfWeights;
534 Targets().at(itgt).SetMean( mean );
535 if (x2(nvars+itgt)/sumOfWeights - mean*mean < 0) {
536 Log() << kFATAL <<
" the RMS of your target variable " << itgt
537 <<
" evaluates to an imaginary number: sqrt(" << x2(nvars+itgt)/sumOfWeights - mean*mean
538 <<
") .. sometimes related to a problem with outliers and negative event weights"
544 for (
UInt_t ievt=0; ievt<nevts; ievt++) {
545 const Event* ev = events[ievt];
547 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
550 v0(ivar) += weight*(
x-mean)*(
x-mean);
552 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
555 v0(nvars+itgt) += weight*(
x-mean)*(
x-mean);
561 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
563 Variables().at(ivar).SetVariance( variance );
564 Log() << kINFO <<
"Variable " <<
Variables().at(ivar).GetExpression() <<
" variance = " << variance <<
Endl;
566 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
567 Double_t variance =
v0(nvars+itgt)/sumOfWeights;
568 Targets().at(itgt).SetVariance( variance );
569 Log() << kINFO <<
"Target " <<
Targets().at(itgt).GetExpression() <<
" variance = " << variance <<
Endl;
572 Log() << kVERBOSE <<
"Set minNorm/maxNorm for variables to: " <<
Endl;
573 Log() << std::setprecision(3);
577 Log() << kVERBOSE <<
"Set minNorm/maxNorm for targets to: " <<
Endl;
578 Log() << std::setprecision(3);
580 Log() <<
" " <<
Targets().at(itgt).GetInternalName()
581 <<
"\t: [" <<
Targets().at(itgt).GetMin() <<
"\t, " <<
Targets().at(itgt).GetMax() <<
"\t] " <<
Endl;
582 Log() << std::setprecision(5);
592 std::vector<TString>* strVec =
new std::vector<TString>;
594 strVec->push_back(
Variables()[ivar].GetLabel() +
"_[transformed]");
611 if (
x <
Targets().at(ivar-nvars).GetMin())
Targets().at(ivar-nvars).SetMin(
x);
612 if (
x >
Targets().at(ivar-nvars).GetMax())
Targets().at(ivar-nvars).SetMax(
x);
630 UInt_t idx = (*itGet).second;
631 Char_t type = (*itGet).first;
638 typeString =
"Variable";
639 label =
fDsi.GetVariableInfo( idx ).GetLabel();
640 expression =
fDsi.GetVariableInfo( idx ).GetExpression();
643 typeString =
"Target";
644 label =
fDsi.GetTargetInfo( idx ).GetLabel();
645 expression =
fDsi.GetTargetInfo( idx ).GetExpression();
648 typeString =
"Spectator";
649 label =
fDsi.GetSpectatorInfo( idx ).GetLabel();
650 expression =
fDsi.GetSpectatorInfo( idx ).GetExpression();
653 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" << type <<
"'." <<
Endl;
668 UInt_t idx = (*itPut).second;
669 Char_t type = (*itPut).first;
676 typeString =
"Variable";
681 typeString =
"Target";
686 typeString =
"Spectator";
691 Log() << kFATAL <<
"VariableTransformBase/AttachXMLTo unknown variable type '" << type <<
"'." <<
Endl;
732 if( typeString ==
"Variable" ){
733 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
734 if(
fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
735 fDsi.GetVariableInfo( ivar ).GetExpression() == expression) {
736 fGet.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
740 }
else if( typeString ==
"Target" ){
741 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
742 if(
fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
743 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
744 fGet.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
748 }
else if( typeString ==
"Spectator" ){
749 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
750 if(
fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
751 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
752 fGet.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
757 Log() << kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
762 assert( nInputs ==
fGet.size() );
780 if( typeString ==
"Variable" ){
781 for(
UInt_t ivar = 0; ivar < nvars; ++ivar ) {
782 if(
fDsi.GetVariableInfo( ivar ).GetLabel() == label ||
783 fDsi.GetVariableInfo( ivar ).GetExpression() == expression ) {
784 fPut.push_back( std::pair<Char_t,UInt_t>(
'v',ivar) );
788 }
else if( typeString ==
"Target" ){
789 for(
UInt_t itgt = 0; itgt < ntgts; ++itgt ) {
790 if(
fDsi.GetTargetInfo( itgt ).GetLabel() == label ||
791 fDsi.GetTargetInfo( itgt ).GetExpression() == expression ) {
792 fPut.push_back( std::pair<Char_t,UInt_t>(
't',itgt) );
796 }
else if( typeString ==
"Spectator" ){
797 for(
UInt_t ispct = 0; ispct < nspcts; ++ispct ) {
798 if(
fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ||
799 fDsi.GetSpectatorInfo( ispct ).GetExpression() == expression ) {
800 fPut.push_back( std::pair<Char_t,UInt_t>(
's',ispct) );
805 Log() << kFATAL <<
"VariableTransformationBase/ReadFromXML : unknown type '" << typeString <<
"'." <<
Endl;
810 assert( nOutputs ==
fPut.size() );
821 fout <<
" // define the indices of the variables which are transformed by this transformation" << std::endl;
822 fout <<
" static std::vector<int> indicesGet;" << std::endl;
823 fout <<
" static std::vector<int> indicesPut;" << std::endl << std::endl;
824 fout <<
" if ( indicesGet.empty() ) {" << std::endl;
825 fout <<
" indicesGet.reserve(fNvars);" << std::endl;
828 Char_t type = (*itEntry).first;
829 Int_t idx = (*itEntry).second;
833 fout <<
" indicesGet.push_back( " << idx <<
");" << std::endl;
836 Log() << kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
839 Log() << kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
842 Log() << kFATAL <<
"VariableTransformBase/GetInput : unknown type '" << type <<
"'." <<
Endl;
845 fout <<
" }" << std::endl;
846 fout <<
" if ( indicesPut.empty() ) {" << std::endl;
847 fout <<
" indicesPut.reserve(fNvars);" << std::endl;
850 Char_t type = (*itEntry).first;
851 Int_t idx = (*itEntry).second;
855 fout <<
" indicesPut.push_back( " << idx <<
");" << std::endl;
858 Log() << kWARNING <<
"MakeClass doesn't work with transformation of targets. The results will be wrong!" <<
Endl;
861 Log() << kWARNING <<
"MakeClass doesn't work with transformation of spectators. The results will be wrong!" <<
Endl;
864 Log() << kFATAL <<
"VariableTransformBase/PutInput : unknown type '" << type <<
"'." <<
Endl;
868 fout <<
" }" << std::endl;
871 }
else if( part == 1){
int Int_t
Signed integer 4 bytes (int).
char Char_t
Character 1 byte (char).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
float Float_t
Float 4 bytes (float).
TVectorT< Double_t > TVectorD
#define TMVA_VERSION_CODE
Class that contains all the data information.
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.
TObject()
TObject constructor.
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)
Returns the square root of x.