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