Logo ROOT   6.08/07
Reference Guide
VariableTransformBase.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : VariableTransformBase *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
16  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
17  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * *
19  * Copyright (c) 2005: *
20  * CERN, Switzerland *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
29 
30 #include "TMVA/Config.h"
31 #include "TMVA/DataSetInfo.h"
32 #include "TMVA/MsgLogger.h"
33 #include "TMVA/Ranking.h"
34 #include "TMVA/Tools.h"
35 #include "TMVA/Types.h"
36 #include "TMVA/VariableInfo.h"
37 #include "TMVA/Version.h"
38 
39 #include "TH1.h"
40 #include "TH2.h"
41 #include "THashTable.h"
42 #include "TList.h"
43 #include "TMath.h"
44 #include "TProfile.h"
45 #include "TVectorD.h"
46 
47 #include <algorithm>
48 #include <cassert>
49 #include <exception>
50 #include <iomanip>
51 #include <stdexcept>
52 #include <set>
53 
54 bool advanced;
55 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// standard constructor
60 
62  Types::EVariableTransform tf,
63  const TString& trfName )
64 : TObject(),
65  fDsi(dsi),
66  fDsiOutput(NULL),
67  fTransformedEvent(0),
68  fBackTransformedEvent(0),
69  fVariableTransform(tf),
70  fEnabled( kTRUE ),
71  fCreated( kFALSE ),
72  fNormalise( kFALSE ),
73  fTransformName(trfName),
74  fVariableTypesAreCounted(false),
75  fNVariables(0),
76  fNTargets(0),
77  fNSpectators(0),
78  fSortGet(kTRUE),
79  fTMVAVersion(TMVA_VERSION_CODE),
80  fLogger( 0 )
81 {
82  fLogger = new MsgLogger(this, kINFO);
83  for (UInt_t ivar = 0; ivar < fDsi.GetNVariables(); ivar++) {
84  fVariables.push_back( VariableInfo( fDsi.GetVariableInfo(ivar) ) );
85  }
86  for (UInt_t itgt = 0; itgt < fDsi.GetNTargets(); itgt++) {
87  fTargets.push_back( VariableInfo( fDsi.GetTargetInfo(itgt) ) );
88  }
89  for (UInt_t ispct = 0; ispct < fDsi.GetNSpectators(); ispct++) {
90  fTargets.push_back( VariableInfo( fDsi.GetSpectatorInfo(ispct) ) );
91  }
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 
97 {
98  if (fTransformedEvent!=0) delete fTransformedEvent;
100  // destructor
101  delete fLogger;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// select the variables/targets/spectators which serve as input to the transformation
106 
107 void TMVA::VariableTransformBase::SelectInput( const TString& _inputVariables, Bool_t putIntoVariables )
108 {
109  TString inputVariables = _inputVariables;
110 
111  // unselect all variables first
112  fGet.clear();
113 
114  UInt_t nvars = GetNVariables();
115  UInt_t ntgts = GetNTargets();
116  UInt_t nspcts = GetNSpectators();
117 
118  typedef std::set<Int_t> SelectedIndices;
119 
120  SelectedIndices varIndices;
121  SelectedIndices tgtIndices;
122  SelectedIndices spctIndices;
123 
124  if (inputVariables == "") // default is all variables and all targets
125  { // (the default can be changed by decorating this member function in the implementations)
126  inputVariables = "_V_,_T_";
127  }
128 
129  TList* inList = gTools().ParseFormatLine( inputVariables, "," );
130  TListIter inIt(inList);
131  while (TObjString* os = (TObjString*)inIt()) {
132 
133  TString variables = os->GetString();
134 
135  if( variables.BeginsWith("_") && variables.EndsWith("_") ) { // special symbol (keyword)
136  variables.Remove( 0,1); // remove first "_"
137  variables.Remove( variables.Length()-1,1 ); // remove last "_"
138 
139  if( variables.BeginsWith("V") ) { // variables
140  variables.Remove(0,1); // remove "V"
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 );
145  }
146  } else {
147  UInt_t idx = variables.Atoi();
148  if( idx >= nvars )
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 );
152  }
153  }else if( variables.BeginsWith("T") ) { // targets
154  variables.Remove(0,1); // remove "T"
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 );
159  }
160  } else {
161  UInt_t idx = variables.Atoi();
162  if( idx >= ntgts )
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 );
166  }
167  }else if( variables.BeginsWith("S") ) { // spectators
168  variables.Remove(0,1); // remove "S"
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 );
173  }
174  } else {
175  UInt_t idx = variables.Atoi();
176  if( idx >= nspcts )
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 );
180  }
181  }else if( TString("REARRANGE").BeginsWith(variables) ) { // toggle rearrange sorting (take sort order given in the options)
183  if( !fSortGet )
184  Log() << kINFO << "Variable rearrangement set true: Variable order given in transformation option is used for input to transformation!" << Endl;
185 
186  }
187  }else{ // no keyword, ... user provided variable labels
188  Int_t numIndices = varIndices.size()+tgtIndices.size()+spctIndices.size();
189  for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { // search all variables
190  if( fDsi.GetVariableInfo( ivar ).GetLabel() == variables ) {
191  fGet.push_back( std::pair<Char_t,UInt_t>('v',ivar) );
192  varIndices.insert( ivar );
193  break;
194  }
195  }
196  for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { // search all targets
197  if( fDsi.GetTargetInfo( itgt ).GetLabel() == variables ) {
198  fGet.push_back( std::pair<Char_t,UInt_t>('t',itgt) );
199  tgtIndices.insert( itgt );
200  break;
201  }
202  }
203  for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { // search all spectators
204  if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == variables ) {
205  fGet.push_back( std::pair<Char_t,UInt_t>('s',ispct) );
206  spctIndices.insert( ispct );
207  break;
208  }
209  }
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;
214  }
215  }
216 
217 
218  if( putIntoVariables ) {
219  Int_t idx = 0;
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) );
222  ++idx;
223  }
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) );
226  ++idx;
227  }
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) );
230  ++idx;
231  }
232  }else {
233  for( SelectedIndices::iterator it = varIndices.begin(), itEnd = varIndices.end(); it != itEnd; ++it ) {
234  Int_t idx = (*it);
235  fPut.push_back( std::pair<Char_t,UInt_t>('v',idx) );
236  }
237  for( SelectedIndices::iterator it = tgtIndices.begin(), itEnd = tgtIndices.end(); it != itEnd; ++it ) {
238  Int_t idx = (*it);
239  fPut.push_back( std::pair<Char_t,UInt_t>('t',idx) );
240  }
241  for( SelectedIndices::iterator it = spctIndices.begin(), itEnd = spctIndices.end(); it != itEnd; ++it ) {
242  Int_t idx = (*it);
243  fPut.push_back( std::pair<Char_t,UInt_t>('s',idx) );
244  }
245 
246  // if sorting is turned on, fGet should have the indices sorted as fPut has them.
247  if( fSortGet ) {
248  fGet.clear();
249  fGet.assign( fPut.begin(), fPut.end() );
250  }
251  }
252 
253  Log() << kHEADER << "Transformation, Variable selection : " << Endl;
254 
255  // choose the new dsi for output if present, if not, take the common one
256  const DataSetInfo* outputDsiPtr = (fDsiOutput? &(*fDsiOutput) : &fDsi );
257 
258 
259 
260  ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end();
261  ItVarTypeIdx itPut = fPut.begin(); // , itPutEnd = fPut.end();
262  for( ; itGet != itGetEnd; ++itGet ) {
263  TString inputTypeString = "?";
264 
265  Char_t inputType = (*itGet).first;
266  Int_t inputIdx = (*itGet).second;
267 
268  TString inputLabel = "NOT FOND";
269  if( inputType == 'v' ) {
270  inputLabel = fDsi.GetVariableInfo( inputIdx ).GetLabel();
271  inputTypeString = "variable";
272  }
273  else if( inputType == 't' ){
274  inputLabel = fDsi.GetTargetInfo( inputIdx ).GetLabel();
275  inputTypeString = "target";
276  }
277  else if( inputType == 's' ){
278  inputLabel = fDsi.GetSpectatorInfo( inputIdx ).GetLabel();
279  inputTypeString = "spectator";
280  }
281 
282  TString outputTypeString = "?";
283 
284  Char_t outputType = (*itPut).first;
285  Int_t outputIdx = (*itPut).second;
286 
287  TString outputLabel = "NOT FOUND";
288  if( outputType == 'v' ) {
289  outputLabel = outputDsiPtr->GetVariableInfo( outputIdx ).GetLabel();
290  outputTypeString = "variable";
291  }
292  else if( outputType == 't' ){
293  outputLabel = outputDsiPtr->GetTargetInfo( outputIdx ).GetLabel();
294  outputTypeString = "target";
295  }
296  else if( outputType == 's' ){
297  outputLabel = outputDsiPtr->GetSpectatorInfo( outputIdx ).GetLabel();
298  outputTypeString = "spectator";
299  }
300  Log() << kINFO << "Input : " << inputTypeString.Data() << " '" << inputLabel.Data() << "'" << " <---> " << "Output : " << outputTypeString.Data() << " '" << outputLabel.Data() << "'" << Endl;
301  Log() << kDEBUG << "\t(index=" << inputIdx << ")." << "\t(index=" << outputIdx << ")." << Endl;
302 
303  ++itPut;
304  }
305  // Log() << kINFO << Endl;
306 }
307 
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// select the values from the event
311 
312 Bool_t TMVA::VariableTransformBase::GetInput( const Event* event, std::vector<Float_t>& input, std::vector<Char_t>& mask, Bool_t backTransformation ) const
313 {
314  ItVarTypeIdxConst itEntry;
315  ItVarTypeIdxConst itEntryEnd;
316 
317  input.clear();
318  mask.clear();
319 
320  if( backTransformation && !fPut.empty() ){
321  itEntry = fPut.begin();
322  itEntryEnd = fPut.end();
323  input.reserve(fPut.size());
324  }
325  else {
326  itEntry = fGet.begin();
327  itEntryEnd = fGet.end();
328  input.reserve(fGet.size() );
329  }
330 
331  Bool_t hasMaskedEntries = kFALSE;
332  // event->Print(std::cout);
333  for( ; itEntry != itEntryEnd; ++itEntry ) {
334  Char_t type = (*itEntry).first;
335  Int_t idx = (*itEntry).second;
336 
337  try{
338  switch( type ) {
339  case 'v':
340  input.push_back( event->GetValue(idx) );
341  break;
342  case 't':
343  input.push_back( event->GetTarget(idx) );
344  break;
345  case 's':
346  input.push_back( event->GetSpectator(idx) );
347  break;
348  default:
349  Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl;
350  }
351  mask.push_back(kFALSE);
352  }
353  catch(std::out_of_range& /* excpt */ ){ // happens when an event is transformed which does not yet have the targets calculated (in the application phase)
354  input.push_back(0.f);
355  mask.push_back(kTRUE);
356  hasMaskedEntries = kTRUE;
357  }
358  }
359  return hasMaskedEntries;
360 }
361 
362 ////////////////////////////////////////////////////////////////////////////////
363 /// select the values from the event
364 
365 void TMVA::VariableTransformBase::SetOutput( Event* event, std::vector<Float_t>& output, std::vector<Char_t>& mask, const Event* oldEvent, Bool_t backTransformation ) const
366 {
367  std::vector<Float_t>::iterator itOutput = output.begin();
368  std::vector<Char_t>::iterator itMask = mask.begin();
369 
370  if( oldEvent )
371  event->CopyVarValues( *oldEvent );
372 
373  try {
374 
375  ItVarTypeIdxConst itEntry;
376  ItVarTypeIdxConst itEntryEnd;
377 
378  if( backTransformation || fPut.empty() ){ // as in GetInput, but the other way round (from fPut for transformation, from fGet for backTransformation)
379  itEntry = fGet.begin();
380  itEntryEnd = fGet.end();
381  }
382  else {
383  itEntry = fPut.begin();
384  itEntryEnd = fPut.end();
385  }
386 
387 
388  for( ; itEntry != itEntryEnd; ++itEntry ) {
389 
390  if( (*itMask) ){ // if the value is masked
391  continue;
392  }
393 
394  Char_t type = (*itEntry).first;
395  Int_t idx = (*itEntry).second;
396  if (itOutput == output.end()) Log() << kFATAL << "Read beyond array boundaries in VariableTransformBase::SetOutput"<<Endl;
397  Float_t value = (*itOutput);
398 
399  switch( type ) {
400  case 'v':
401  event->SetVal( idx, value );
402  break;
403  case 't':
404  event->SetTarget( idx, value );
405  break;
406  case 's':
407  event->SetSpectator( idx, value );
408  break;
409  default:
410  Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl;
411  }
412  if( !(*itMask) ) ++itOutput;
413  ++itMask;
414 
415  }
416  }catch( std::exception& except ){
417  Log() << kFATAL << "VariableTransformBase/SetOutput : exception/" << except.what() << Endl;
418  throw;
419  }
420 }
421 
422 
423 ////////////////////////////////////////////////////////////////////////////////
424 /// count variables, targets and spectators
425 
427 {
429  nvars = fNVariables;
430  ntgts = fNTargets;
431  nspcts = fNSpectators;
432  return;
433  }
434 
435  nvars = ntgts = nspcts = 0;
436 
437  for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
438  Char_t type = (*itEntry).first;
439 
440  switch( type ) {
441  case 'v':
442  nvars++;
443  break;
444  case 't':
445  ntgts++;
446  break;
447  case 's':
448  nspcts++;
449  break;
450  default:
451  Log() << kFATAL << "VariableTransformBase/GetVariableTypeNumbers : unknown type '" << type << "'." << Endl;
452  }
453  }
454 
455  fNVariables = nvars;
456  fNTargets = ntgts;
457  fNSpectators = nspcts;
458 
460 }
461 
462 
463 ////////////////////////////////////////////////////////////////////////////////
464 /// TODO --> adapt to variable,target,spectator selection
465 /// method to calculate minimum, maximum, mean, and RMS for all
466 /// variables used in the MVA
467 
468 void TMVA::VariableTransformBase::CalcNorm( const std::vector<const Event*>& events )
469 {
470  if (!IsCreated()) return;
471 
472  const UInt_t nvars = GetNVariables();
473  const UInt_t ntgts = GetNTargets();
474 
475  UInt_t nevts = events.size();
476 
477  TVectorD x2( nvars+ntgts ); x2 *= 0;
478  TVectorD x0( nvars+ntgts ); x0 *= 0;
479  TVectorD v0( nvars+ntgts ); v0 *= 0;
480 
481  Double_t sumOfWeights = 0;
482  for (UInt_t ievt=0; ievt<nevts; ievt++) {
483  const Event* ev = events[ievt];
484 
485  Double_t weight = ev->GetWeight();
486  sumOfWeights += weight;
487  for (UInt_t ivar=0; ivar<nvars; ivar++) {
488  Double_t x = ev->GetValue(ivar);
489  if (ievt==0) {
490  Variables().at(ivar).SetMin(x);
491  Variables().at(ivar).SetMax(x);
492  }
493  else {
494  UpdateNorm( ivar, x );
495  }
496  x0(ivar) += x*weight;
497  x2(ivar) += x*x*weight;
498  }
499  for (UInt_t itgt=0; itgt<ntgts; itgt++) {
500  Double_t x = ev->GetTarget(itgt);
501  if (ievt==0) {
502  Targets().at(itgt).SetMin(x);
503  Targets().at(itgt).SetMax(x);
504  }
505  else {
506  UpdateNorm( nvars+itgt, x );
507  }
508  x0(nvars+itgt) += x*weight;
509  x2(nvars+itgt) += x*x*weight;
510  }
511  }
512 
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;
516  }
517 
518  // set Mean and RMS
519  for (UInt_t ivar=0; ivar<nvars; ivar++) {
520  Double_t mean = x0(ivar)/sumOfWeights;
521 
522  Variables().at(ivar).SetMean( mean );
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"
527  << Endl;
528  }
529  Variables().at(ivar).SetRMS( TMath::Sqrt( x2(ivar)/sumOfWeights - mean*mean) );
530  }
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"
538  << Endl;
539  }
540  Targets().at(itgt).SetRMS( TMath::Sqrt( x2(nvars+itgt)/sumOfWeights - mean*mean) );
541  }
542  // calculate variance
543  for (UInt_t ievt=0; ievt<nevts; ievt++) {
544  const Event* ev = events[ievt];
545  Double_t weight = ev->GetWeight();
546  for (UInt_t ivar=0; ivar<nvars; ivar++) {
547  Double_t x = ev->GetValue(ivar);
548  Double_t mean = Variables().at(ivar).GetMean();
549  v0(ivar) += weight*(x-mean)*(x-mean);
550  }
551  for (UInt_t itgt=0; itgt<ntgts; itgt++) {
552  Double_t x = ev->GetTarget(itgt);
553  Double_t mean = Targets().at(itgt).GetMean();
554  v0(nvars+itgt) += weight*(x-mean)*(x-mean);
555  }
556 
557  }
558 
559  // set variance
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;
564  }
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;
569  }
570 
571  Log() << kVERBOSE << "Set minNorm/maxNorm for variables to: " << Endl;
572  Log() << std::setprecision(3);
573  for (UInt_t ivar=0; ivar<GetNVariables(); ivar++)
574  Log() << " " << Variables().at(ivar).GetInternalName()
575  << "\t: [" << Variables().at(ivar).GetMin() << "\t, " << Variables().at(ivar).GetMax() << "\t] " << Endl;
576  Log() << kVERBOSE << "Set minNorm/maxNorm for targets to: " << Endl;
577  Log() << std::setprecision(3);
578  for (UInt_t itgt=0; itgt<GetNTargets(); itgt++)
579  Log() << " " << Targets().at(itgt).GetInternalName()
580  << "\t: [" << Targets().at(itgt).GetMin() << "\t, " << Targets().at(itgt).GetMax() << "\t] " << Endl;
581  Log() << std::setprecision(5); // reset to better value
582 }
583 
584 ////////////////////////////////////////////////////////////////////////////////
585 /// TODO --> adapt to variable,target,spectator selection
586 /// default transformation output
587 /// --> only indicate that transformation occurred
588 
589 std::vector<TString>* TMVA::VariableTransformBase::GetTransformationStrings( Int_t /*cls*/ ) const
590 {
591  std::vector<TString>* strVec = new std::vector<TString>;
592  for (UInt_t ivar=0; ivar<GetNVariables(); ivar++) {
593  strVec->push_back( Variables()[ivar].GetLabel() + "_[transformed]");
594  }
595 
596  return strVec;
597 }
598 
599 ////////////////////////////////////////////////////////////////////////////////
600 /// TODO --> adapt to variable,target,spectator selection
601 /// update min and max of a given variable (target) and a given transformation method
602 
604 {
605  Int_t nvars = fDsi.GetNVariables();
606  if( ivar < nvars ){
607  if (x < Variables().at(ivar).GetMin()) Variables().at(ivar).SetMin(x);
608  if (x > Variables().at(ivar).GetMax()) Variables().at(ivar).SetMax(x);
609  }else{
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);
612  }
613 }
614 
615 ////////////////////////////////////////////////////////////////////////////////
616 /// create XML description the transformation (write out info of selected variables)
617 
619 {
620  void* selxml = gTools().AddChild(parent, "Selection");
621 
622  void* inpxml = gTools().AddChild(selxml, "Input");
623  gTools().AddAttr(inpxml, "NInputs", fGet.size() );
624 
625  // choose the new dsi for output if present, if not, take the common one
626  const DataSetInfo* outputDsiPtr = (fDsiOutput? fDsiOutput : &fDsi );
627 
628  for( ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) {
629  UInt_t idx = (*itGet).second;
630  Char_t type = (*itGet).first;
631 
632  TString label = "";
633  TString expression = "";
634  TString typeString = "";
635  switch( type ){
636  case 'v':
637  typeString = "Variable";
638  label = fDsi.GetVariableInfo( idx ).GetLabel();
639  expression = fDsi.GetVariableInfo( idx ).GetExpression();
640  break;
641  case 't':
642  typeString = "Target";
643  label = fDsi.GetTargetInfo( idx ).GetLabel();
644  expression = fDsi.GetTargetInfo( idx ).GetExpression();
645  break;
646  case 's':
647  typeString = "Spectator";
648  label = fDsi.GetSpectatorInfo( idx ).GetLabel();
649  expression = fDsi.GetSpectatorInfo( idx ).GetExpression();
650  break;
651  default:
652  Log() << kFATAL << "VariableTransformBase/AttachXMLTo unknown variable type '" << type << "'." << Endl;
653  }
654 
655  void* idxxml = gTools().AddChild(inpxml, "Input");
656  // gTools().AddAttr(idxxml, "Index", idx);
657  gTools().AddAttr(idxxml, "Type", typeString);
658  gTools().AddAttr(idxxml, "Label", label);
659  gTools().AddAttr(idxxml, "Expression", expression);
660  }
661 
662 
663  void* outxml = gTools().AddChild(selxml, "Output");
664  gTools().AddAttr(outxml, "NOutputs", fPut.size() );
665 
666  for( ItVarTypeIdx itPut = fPut.begin(), itPutEnd = fPut.end(); itPut != itPutEnd; ++itPut ) {
667  UInt_t idx = (*itPut).second;
668  Char_t type = (*itPut).first;
669 
670  TString label = "";
671  TString expression = "";
672  TString typeString = "";
673  switch( type ){
674  case 'v':
675  typeString = "Variable";
676  label = outputDsiPtr->GetVariableInfo( idx ).GetLabel();
677  expression = outputDsiPtr->GetVariableInfo( idx ).GetExpression();
678  break;
679  case 't':
680  typeString = "Target";
681  label = outputDsiPtr->GetTargetInfo( idx ).GetLabel();
682  expression = outputDsiPtr->GetTargetInfo( idx ).GetExpression();
683  break;
684  case 's':
685  typeString = "Spectator";
686  label = outputDsiPtr->GetSpectatorInfo( idx ).GetLabel();
687  expression = outputDsiPtr->GetSpectatorInfo( idx ).GetExpression();
688  break;
689  default:
690  Log() << kFATAL << "VariableTransformBase/AttachXMLTo unknown variable type '" << type << "'." << Endl;
691  }
692 
693  void* idxxml = gTools().AddChild(outxml, "Output");
694  // gTools().AddAttr(idxxml, "Index", idx);
695  gTools().AddAttr(idxxml, "Type", typeString);
696  gTools().AddAttr(idxxml, "Label", label);
697  gTools().AddAttr(idxxml, "Expression", expression);
698  }
699 
700 
701 }
702 
703 ////////////////////////////////////////////////////////////////////////////////
704 /// Read the input variables from the XML node
705 
707 {
708  void* inpnode = gTools().GetChild( selnode );
709  void* outnode = gTools().GetNextChild( inpnode );
710 
711  UInt_t nvars = GetNVariables();
712  UInt_t ntgts = GetNTargets();
713  UInt_t nspcts = GetNSpectators();
714 
715 
716 
717  // read inputs
718  fGet.clear();
719 
720  UInt_t nInputs = 0;
721  gTools().ReadAttr(inpnode, "NInputs", nInputs);
722 
723  void* ch = gTools().GetChild( inpnode );
724  while(ch) {
725  TString typeString = "";
726  TString label = "";
727  TString expression = "";
728 
729  gTools().ReadAttr(ch, "Type", typeString);
730  gTools().ReadAttr(ch, "Label", label);
731  gTools().ReadAttr(ch, "Expression", expression);
732 
733  if( typeString == "Variable" ){
734  for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { // search all variables
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) );
738  break;
739  }
740  }
741  }else if( typeString == "Target" ){
742  for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { // search all targets
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) );
746  break;
747  }
748  }
749  }else if( typeString == "Spectator" ){
750  for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { // search all spectators
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) );
754  break;
755  }
756  }
757  }else{
758  Log() << kFATAL << "VariableTransformationBase/ReadFromXML : unknown type '" << typeString << "'." << Endl;
759  }
760  ch = gTools().GetNextChild( ch );
761  }
762 
763  assert( nInputs == fGet.size() );
764 
765  // read outputs
766  fPut.clear();
767 
768  UInt_t nOutputs = 0;
769  gTools().ReadAttr(outnode, "NOutputs", nOutputs);
770 
771  void* chOut = gTools().GetChild( outnode );
772  while(chOut) {
773  TString typeString = "";
774  TString label = "";
775  TString expression = "";
776 
777  gTools().ReadAttr(chOut, "Type", typeString);
778  gTools().ReadAttr(chOut, "Label", label);
779  gTools().ReadAttr(chOut, "Expression", expression);
780 
781  if( typeString == "Variable" ){
782  for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { // search all variables
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) );
786  break;
787  }
788  }
789  }else if( typeString == "Target" ){
790  for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { // search all targets
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) );
794  break;
795  }
796  }
797  }else if( typeString == "Spectator" ){
798  for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { // search all spectators
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) );
802  break;
803  }
804  }
805  }else{
806  Log() << kFATAL << "VariableTransformationBase/ReadFromXML : unknown type '" << typeString << "'." << Endl;
807  }
808  chOut = gTools().GetNextChild( chOut );
809  }
810 
811  assert( nOutputs == fPut.size() );
812 
813 
814 }
815 
816 
817 ////////////////////////////////////////////////////////////////////////////////
818 /// getinput and setoutput equivalent
819 
820 void TMVA::VariableTransformBase::MakeFunction( std::ostream& fout, const TString& /*fncName*/, Int_t part,
821  UInt_t /*trCounter*/, Int_t /*cls*/ )
822 {
823  if( part == 0 ){ // definitions
824  fout << std::endl;
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;
830 
831  for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) {
832  Char_t type = (*itEntry).first;
833  Int_t idx = (*itEntry).second;
834 
835  switch( type ) {
836  case 'v':
837  fout << " indicesGet.push_back( " << idx << ");" << std::endl;
838  break;
839  case 't':
840  Log() << kWARNING << "MakeClass doesn't work with transformation of targets. The results will be wrong!" << Endl;
841  break;
842  case 's':
843  Log() << kWARNING << "MakeClass doesn't work with transformation of spectators. The results will be wrong!" << Endl;
844  break;
845  default:
846  Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl;
847  }
848  }
849  fout << " } " << std::endl;
850  fout << " if ( indicesPut.empty() ) { " << std::endl;
851  fout << " indicesPut.reserve(fNvars);" << std::endl;
852 
853  for( ItVarTypeIdxConst itEntry = fPut.begin(), itEntryEnd = fPut.end(); itEntry != itEntryEnd; ++itEntry ) {
854  Char_t type = (*itEntry).first;
855  Int_t idx = (*itEntry).second;
856 
857  switch( type ) {
858  case 'v':
859  fout << " indicesPut.push_back( " << idx << ");" << std::endl;
860  break;
861  case 't':
862  Log() << kWARNING << "MakeClass doesn't work with transformation of targets. The results will be wrong!" << Endl;
863  break;
864  case 's':
865  Log() << kWARNING << "MakeClass doesn't work with transformation of spectators. The results will be wrong!" << Endl;
866  break;
867  default:
868  Log() << kFATAL << "VariableTransformBase/PutInput : unknown type '" << type << "'." << Endl;
869  }
870  }
871 
872  fout << " } " << std::endl;
873  fout << std::endl;
874 
875  }else if( part == 1){
876  }
877 }
UInt_t GetNVariables() const
Definition: DataSetInfo.h:128
#define TMVA_VERSION_CODE
Definition: Version.h:47
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Collectable string class.
Definition: TObjString.h:32
float Float_t
Definition: RtypesCore.h:53
virtual void MakeFunction(std::ostream &fout, const TString &fncName, Int_t part, UInt_t trCounter, Int_t cls)=0
getinput and setoutput equivalent
virtual void CountVariableTypes(UInt_t &nvars, UInt_t &ntgts, UInt_t &nspcts) const
count variables, targets and spectators
virtual void AttachXMLTo(void *parent)=0
create XML description the transformation (write out info of selected variables)
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
const TString & GetLabel() const
Definition: VariableInfo.h:67
bool advanced
const std::vector< TMVA::VariableInfo > & Targets() const
const std::vector< TMVA::VariableInfo > & Variables() const
void CalcNorm(const std::vector< const Event *> &)
TODO –> adapt to variable,target,spectator selection method to calculate minimum, maximum, mean, and RMS for all variables used in the MVA.
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:309
void * AddChild(void *parent, const char *childname, const char *content=0, bool isRootNode=false)
add child node
Definition: Tools.cxx:1134
Iterator of linked list.
Definition: TList.h:187
const TString & GetExpression() const
Definition: VariableInfo.h:65
virtual std::vector< TString > * GetTransformationStrings(Int_t cls) const
TODO –> adapt to variable,target,spectator selection default transformation output –> only indicate...
Tools & gTools()
Definition: Tools.cxx:79
static const double x2[5]
virtual Bool_t GetInput(const Event *event, std::vector< Float_t > &input, std::vector< Char_t > &mask, Bool_t backTransform=kFALSE) const
select the values from the event
Double_t x[n]
Definition: legend1.C:17
virtual void SetOutput(Event *event, std::vector< Float_t > &output, std::vector< Char_t > &mask, const Event *oldEvent=0, Bool_t backTransform=kFALSE) const
select the values from the event
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
VectorOfCharAndInt::iterator ItVarTypeIdx
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2221
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Definition: Event.cxx:378
virtual void ReadFromXML(void *trfnode)=0
Read the input variables from the XML node.
A doubly linked list.
Definition: TList.h:47
Float_t GetTarget(UInt_t itgt) const
Definition: Event.h:104
VectorOfCharAndInt::const_iterator ItVarTypeIdxConst
VariableInfo & GetTargetInfo(Int_t i)
Definition: DataSetInfo.h:119
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
unsigned int UInt_t
Definition: RtypesCore.h:42
Ssiz_t Length() const
Definition: TString.h:390
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:296
void UpdateNorm(Int_t ivar, Double_t x)
TODO –> adapt to variable,target,spectator selection update min and max of a given variable (target)...
Float_t GetValue(UInt_t ivar) const
return value of i&#39;th variable
Definition: Event.cxx:233
TString & Remove(Ssiz_t pos)
Definition: TString.h:616
#define ClassImp(name)
Definition: Rtypes.h:279
double f(double x)
double Double_t
Definition: RtypesCore.h:55
int type
Definition: TGX11.cxx:120
void * GetNextChild(void *prevchild, const char *childname=0)
XML helpers.
Definition: Tools.cxx:1170
VariableInfo & GetSpectatorInfo(Int_t i)
Definition: DataSetInfo.h:124
VariableInfo & GetVariableInfo(Int_t i)
Definition: DataSetInfo.h:114
Mother of all ROOT objects.
Definition: TObject.h:37
void ToggleInputSortOrder(Bool_t sortOrder)
char Char_t
Definition: RtypesCore.h:29
Abstract ClassifierFactory template that handles arbitrary types.
TList * ParseFormatLine(TString theString, const char *sep=":")
Parse the string and cut into labels separated by ":".
Definition: Tools.cxx:413
#define NULL
Definition: Rtypes.h:82
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1965
virtual void SelectInput(const TString &inputVariables, Bool_t putIntoVariables=kFALSE)
select the variables/targets/spectators which serve as input to the transformation ...
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Bool_t kTRUE
Definition: Rtypes.h:91
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Definition: Event.cxx:258
const int except
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)
Definition: variables.cxx:10
const char * Data() const
Definition: TString.h:349