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