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