ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Reader.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Eckhard von Toerne, Jan Therhaag
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Reader *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Reader class to be used in the user application to interpret the trained *
12  * MVAs in an analysis context *
13  * *
14  * Authors (alphabetical order): *
15  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16  * Peter Speckmayer <peter.speckmayer@cern.ch> - CERN, Switzerland *
17  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
18  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
19  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
20  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
21  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
22  * *
23  * Copyright (c) 2005-2011: *
24  * CERN, Switzerland *
25  * U. of Victoria, Canada *
26  * MPI-K Heidelberg, Germany *
27  * U. of Bonn, Germany *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://ttmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 //_______________________________________________________________________
35 //
36 // The Reader class serves to use the MVAs in a specific analysis context.
37 // Within an event loop, a vector is filled that corresponds to the variables
38 // that were used to train the MVA(s) during the training stage. This vector
39 // is transfered to the Reader, who takes care of interpreting the weight
40 // file of the MVA of choice, and to return the MVA's output. This is then
41 // used by the user for further analysis.
42 //
43 // ---------------------------------------------------------------------
44 // Usage:
45 //
46 // // ------ before starting the event loop (eg, in the initialisation step)
47 //
48 // //
49 // // create TMVA::Reader object
50 // //
51 // TMVA::Reader *reader = new TMVA::Reader();
52 //
53 // // create a set of variables and declare them to the reader
54 // // - the variable names must corresponds in name and type to
55 // // those given in the weight file(s) that you use
56 // Float_t var1, var2, var3, var4;
57 // reader->AddVariable( "var1", &var1 );
58 // reader->AddVariable( "var2", &var2 );
59 // reader->AddVariable( "var3", &var3 );
60 // reader->AddVariable( "var4", &var4 );
61 //
62 // // book the MVA of your choice (prior training of these methods, ie,
63 // // existence of the weight files is required)
64 // reader->BookMVA( "Fisher method", "weights/Fisher.weights.txt" );
65 // reader->BookMVA( "MLP method", "weights/MLP.weights.txt" );
66 // // ... etc
67 //
68 // // ------- start your event loop
69 //
70 // for (Long64_t ievt=0; ievt<myTree->GetEntries();ievt++) {
71 //
72 // // fill vector with values of variables computed from those in the tree
73 // var1 = myvar1;
74 // var2 = myvar2;
75 // var3 = myvar3;
76 // var4 = myvar4;
77 //
78 // // retrieve the corresponding MVA output
79 // double mvaFi = reader->EvaluateMVA( "Fisher method" );
80 // double mvaNN = reader->EvaluateMVA( "MLP method" );
81 //
82 // // do something with these ...., e.g., fill them into your ntuple
83 //
84 // } // end of event loop
85 //
86 // delete reader;
87 // ---------------------------------------------------------------------
88 //
89 // An example application of the Reader can be found in TMVA/macros/TMVApplication.C.
90 //_______________________________________________________________________
91 
92 #include "TMVA/Reader.h"
93 
94 #ifndef ROOT_TMVA_Tools
95 #include "TMVA/Tools.h"
96 #endif
97 #include "TMVA/Config.h"
98 #include "TMVA/ClassifierFactory.h"
99 #include "TMVA/DataSetInfo.h"
100 #include "TMVA/DataSetManager.h"
101 #include "TMVA/IMethod.h"
102 #include "TMVA/MethodBase.h"
103 #include "TMVA/MethodCuts.h"
104 #include "TMVA/MethodCategory.h"
105 #include "TMVA/MsgLogger.h"
106 #include "TMVA/Types.h"
107 
108 #include "TTree.h"
109 #include "TLeaf.h"
110 #include "TString.h"
111 #include "TClass.h"
112 #include "TH1D.h"
113 #include "TKey.h"
114 #include "TVector.h"
115 #include "TXMLEngine.h"
116 #include "TMath.h"
117 
118 #include <cstdlib>
119 
120 #include <string>
121 #include <vector>
122 #include <fstream>
123 
124 #include <iostream>
125 
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// constructor
130 
131 TMVA::Reader::Reader( const TString& theOption, Bool_t verbose )
132  : Configurable( theOption ),
133  fDataSetManager( NULL ), // DSMTEST
134  fDataSetInfo(),
135  fVerbose( verbose ),
136  fSilent ( kFALSE ),
137  fColor ( kFALSE ),
138  fCalculateError(kFALSE),
139  fMvaEventError( 0 ),
140  fMvaEventErrorUpper( 0 ),
141  fLogger ( 0 )
142 {
143  fDataSetManager = new DataSetManager( fDataInputHandler );
144  fDataSetManager->AddDataSetInfo(fDataSetInfo);
145  fLogger = new MsgLogger(this);
146  SetConfigName( GetName() );
147  DeclareOptions();
148  ParseOptions();
149 
150  Init();
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// constructor
155 
156 TMVA::Reader::Reader( std::vector<TString>& inputVars, const TString& theOption, Bool_t verbose )
157  : Configurable( theOption ),
158  fDataSetManager( NULL ), // DSMTEST
159  fDataSetInfo(),
160  fVerbose( verbose ),
161  fSilent ( kFALSE ),
162  fColor ( kFALSE ),
163  fCalculateError(kFALSE),
164  fMvaEventError( 0 ),
165  fMvaEventErrorUpper( 0 ), //zjh
166  fLogger ( 0 )
167 {
170  fLogger = new MsgLogger(this);
171  SetConfigName( GetName() );
172  DeclareOptions();
173  ParseOptions();
174 
175  // arguments: names of input variables (vector)
176  // verbose flag
177  for (std::vector<TString>::iterator ivar = inputVars.begin(); ivar != inputVars.end(); ivar++)
178  DataInfo().AddVariable( *ivar );
179 
180  Init();
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// constructor
185 
186 TMVA::Reader::Reader( std::vector<std::string>& inputVars, const TString& theOption, Bool_t verbose )
187  : Configurable( theOption ),
188  fDataSetManager( NULL ), // DSMTEST
189  fDataSetInfo(),
190  fVerbose( verbose ),
191  fSilent ( kFALSE ),
192  fColor ( kFALSE ),
193  fCalculateError(kFALSE),
194  fMvaEventError( 0 ),
195  fMvaEventErrorUpper( 0 ),
196  fLogger ( 0 )
197 {
200  fLogger = new MsgLogger(this);
201  SetConfigName( GetName() );
202  DeclareOptions();
203  ParseOptions();
204 
205  // arguments: names of input variables (vector)
206  // verbose flag
207  for (std::vector<std::string>::iterator ivar = inputVars.begin(); ivar != inputVars.end(); ivar++)
208  DataInfo().AddVariable( ivar->c_str() );
209 
210  Init();
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// constructor
215 
216 TMVA::Reader::Reader( const std::string& varNames, const TString& theOption, Bool_t verbose )
217  : Configurable( theOption ),
218  fDataSetManager( NULL ), // DSMTEST
219  fDataSetInfo(),
220  fVerbose( verbose ),
221  fSilent ( kFALSE ),
222  fColor ( kFALSE ),
223  fCalculateError(kFALSE),
224  fMvaEventError( 0 ),
225  fMvaEventErrorUpper( 0 ),
226  fLogger ( 0 )
227 {
230  fLogger = new MsgLogger(this);
231  SetConfigName( GetName() );
232  DeclareOptions();
233  ParseOptions();
234 
235  // arguments: names of input variables given in form: "name1:name2:name3"
236  // verbose flag
237  DecodeVarNames(varNames);
238  Init();
239 }
240 
241 ////////////////////////////////////////////////////////////////////////////////
242 /// constructor
243 
244 TMVA::Reader::Reader( const TString& varNames, const TString& theOption, Bool_t verbose )
245  : Configurable( theOption ),
246  fDataSetManager( NULL ), // DSMTEST
247  fDataSetInfo(),
248  fVerbose( verbose ),
249  fSilent ( kFALSE ),
250  fColor ( kFALSE ),
251  fCalculateError(kFALSE),
252  fMvaEventError( 0 ),
253  fMvaEventErrorUpper( 0 ),
254  fLogger ( 0 )
255 {
258  fLogger = new MsgLogger(this);
259  SetConfigName( GetName() );
260  DeclareOptions();
261  ParseOptions();
262 
263  // arguments: names of input variables given in form: "name1:name2:name3"
264  // verbose flag
265  DecodeVarNames(varNames);
266  Init();
267 }
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 /// declaration of configuration options
271 
273 {
274  if (gTools().CheckForSilentOption( GetOptions() )) Log().InhibitOutput(); // make sure is silent if wanted to
275 
276  DeclareOptionRef( fVerbose, "V", "Verbose flag" );
277  DeclareOptionRef( fColor, "Color", "Color flag (default True)" );
278  DeclareOptionRef( fSilent, "Silent", "Boolean silent flag (default False)" );
279  DeclareOptionRef( fCalculateError, "Error", "Calculates errors (default False)" );
280 }
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 /// destructor
284 
286 {
287  delete fDataSetManager; // DSMTEST
288 
289  delete fLogger;
290 }
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// default initialisation (no member variables)
294 /// default initialisation (no member variables)
295 
296 void TMVA::Reader::Init( void )
297 {
298  if (Verbose()) fLogger->SetMinType( kVERBOSE );
299 
300  gConfig().SetUseColor( fColor );
301  gConfig().SetSilent ( fSilent );
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 /// Add a float variable or expression to the reader
306 
307 void TMVA::Reader::AddVariable( const TString& expression, Float_t* datalink )
308 {
309  DataInfo().AddVariable( expression, "", "", 0, 0, 'F', kFALSE ,(void*)datalink ); // <= should this be F or rather T?
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 
314 void TMVA::Reader::AddVariable( const TString& expression, Int_t* datalink )
315 {
316  Log() << kFATAL << "Reader::AddVariable( const TString& expression, Int_t* datalink ), this function is deprecated, please provide all variables to the reader as floats" << Endl;
317  // Add an integer variable or expression to the reader
318  Log() << kFATAL << "Reader::AddVariable( const TString& expression, Int_t* datalink ), this function is deprecated, please provide all variables to the reader as floats" << Endl;
319  DataInfo().AddVariable(expression, "", "", 0, 0, 'I', kFALSE, (void*)datalink ); // <= should this be F or rather T?
320 }
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 /// Add a float spectator or expression to the reader
324 
325 void TMVA::Reader::AddSpectator( const TString& expression, Float_t* datalink )
326 {
327  DataInfo().AddSpectator( expression, "", "", 0, 0, 'F', kFALSE ,(void*)datalink );
328 }
329 
330 ////////////////////////////////////////////////////////////////////////////////
331 /// Add an integer spectator or expression to the reader
332 
333 void TMVA::Reader::AddSpectator( const TString& expression, Int_t* datalink )
334 {
335  DataInfo().AddSpectator(expression, "", "", 0, 0, 'I', kFALSE, (void*)datalink );
336 }
337 
338 ////////////////////////////////////////////////////////////////////////////////
339 /// read the method type from the file
340 
342 {
343  std::ifstream fin( filename );
344  if (!fin.good()) { // file not found --> Error
345  Log() << kFATAL << "<BookMVA> fatal error: "
346  << "unable to open input weight file: " << filename << Endl;
347  }
348 
349  TString fullMethodName("");
350  if (filename.EndsWith(".xml")) {
351  fin.close();
352 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,29,0)
353  void* doc = gTools().xmlengine().ParseFile(filename,gTools().xmlenginebuffersize());// the default buffer size in TXMLEngine::ParseFile is 100k. Starting with ROOT 5.29 one can set the buffer size, see: http://savannah.cern.ch/bugs/?78864. This might be necessary for large XML files
354 #else
355  void* doc = gTools().xmlengine().ParseFile(filename);
356 #endif
357  void* rootnode = gTools().xmlengine().DocGetRootElement(doc); // node "MethodSetup"
358  gTools().ReadAttr(rootnode, "Method", fullMethodName);
359  gTools().xmlengine().FreeDoc(doc);
360  }
361  else {
362  char buf[512];
363  fin.getline(buf,512);
364  while (!TString(buf).BeginsWith("Method")) fin.getline(buf,512);
365  fullMethodName = TString(buf);
366  fin.close();
367  }
368  TString methodType = fullMethodName(0,fullMethodName.Index("::"));
369  if (methodType.Contains(" ")) methodType = methodType(methodType.Last(' ')+1,methodType.Length());
370  return methodType;
371 }
372 
373 ////////////////////////////////////////////////////////////////////////////////
374 /// read method name from weight file
375 
376 TMVA::IMethod* TMVA::Reader::BookMVA( const TString& methodTag, const TString& weightfile )
377 {
378  // assert non-existence
379  if (fMethodMap.find( methodTag ) != fMethodMap.end())
380  Log() << kFATAL << "<BookMVA> method tag \"" << methodTag << "\" already exists!" << Endl;
381 
382  TString methodType(GetMethodTypeFromFile(weightfile));
383 
384  Log() << kINFO << "Booking \"" << methodTag << "\" of type \"" << methodType << "\" from " << weightfile << "." << Endl;
385 
386  MethodBase* method = dynamic_cast<MethodBase*>(this->BookMVA( Types::Instance().GetMethodType(methodType),
387  weightfile ) );
388  if( method && method->GetMethodType() == Types::kCategory ){
389  MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method));
390  if( !methCat )
391  Log() << kFATAL << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl;
392  methCat->fDataSetManager = fDataSetManager;
393  }
394 
395  return fMethodMap[methodTag] = method;
396 }
397 
398 ////////////////////////////////////////////////////////////////////////////////
399 /// books MVA method from weightfile
400 
402 {
403  IMethod* im = ClassifierFactory::Instance().Create(std::string(Types::Instance().GetMethodName( methodType )),
404  DataInfo(), weightfile );
405 
406  MethodBase *method = (dynamic_cast<MethodBase*>(im));
407 
408  if (method==0) return im;
409 
410  if( method->GetMethodType() == Types::kCategory ){
411  MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method));
412  if( !methCat )
413  Log() << kERROR << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl;
414  methCat->fDataSetManager = fDataSetManager;
415  }
416 
417  method->SetupMethod();
418 
419  // when reading older weight files, they could include options
420  // that are not supported any longer
421  method->DeclareCompatibilityOptions();
422 
423  // read weight file
424  method->ReadStateFromFile();
425 
426  // check for unused options
427  method->CheckSetup();
428 
429  Log() << kINFO << "Booked classifier \"" << method->GetMethodName()
430  << "\" of type: \"" << method->GetMethodTypeName() << "\"" << Endl;
431 
432  return method;
433 }
434 
435 ////////////////////////////////////////////////////////////////////////////////
436 
437 TMVA::IMethod* TMVA::Reader::BookMVA( TMVA::Types::EMVA methodType, const char* xmlstr )
438 {
439 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,00)
440 
441  // books MVA method from weightfile
442  IMethod* im = ClassifierFactory::Instance().Create(std::string(Types::Instance().GetMethodName( methodType )),
443  DataInfo(), "" );
444 
445  MethodBase *method = (dynamic_cast<MethodBase*>(im));
446 
447  if(!method) return 0;
448 
449  if( method->GetMethodType() == Types::kCategory ){
450  MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method));
451  if( !methCat )
452  Log() << kFATAL << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl;
453  methCat->fDataSetManager = fDataSetManager;
454  }
455 
456  method->SetupMethod();
457 
458  // when reading older weight files, they could include options
459  // that are not supported any longer
460  method->DeclareCompatibilityOptions();
461 
462  // read weight file
463  method->ReadStateFromXMLString( xmlstr );
464 
465  // check for unused options
466  method->CheckSetup();
467 
468  Log() << kINFO << "Booked classifier \"" << method->GetMethodName()
469  << "\" of type: \"" << method->GetMethodTypeName() << "\"" << Endl;
470 
471  return method;
472 #else
473  Log() << kFATAL << "Method Reader::BookMVA(TMVA::Types::EMVA methodType = " << methodType
474  << ", const char* xmlstr = " << xmlstr
475  << " ) is not available for ROOT versions prior to 5.26/00." << Endl;
476  return 0;
477 #endif
478 }
479 
480 ////////////////////////////////////////////////////////////////////////////////
481 /// Evaluate a std::vector<float> of input data for a given method
482 /// The parameter aux is obligatory for the cuts method where it represents the efficiency cutoff
483 
484 Double_t TMVA::Reader::EvaluateMVA( const std::vector<Float_t>& inputVec, const TString& methodTag, Double_t aux )
485 {
486  // create a temporary event from the vector.
487  IMethod* imeth = FindMVA( methodTag );
488  MethodBase* meth = dynamic_cast<TMVA::MethodBase*>(imeth);
489  if(meth==0) return 0;
490 
491 // Event* tmpEvent=new Event(inputVec, 2); // ToDo resolve magic 2 issue
492  Event* tmpEvent=new Event(inputVec, DataInfo().GetNVariables()); // is this the solution?
493  for (UInt_t i=0; i<inputVec.size(); i++){
494  if (TMath::IsNaN(inputVec[i])) {
495  Log() << kERROR << i << "-th variable of the event is NaN --> return MVA value -999, \n that's all I can do, please fix or remove this event." << Endl;
496  delete tmpEvent;
497  return -999;
498  }
499  }
500 
501  if (meth->GetMethodType() == TMVA::Types::kCuts) {
502  TMVA::MethodCuts* mc = dynamic_cast<TMVA::MethodCuts*>(meth);
503  if(mc)
504  mc->SetTestSignalEfficiency( aux );
505  }
506  Double_t val = meth->GetMvaValue( tmpEvent, (fCalculateError?&fMvaEventError:0));
507  delete tmpEvent;
508  return val;
509 }
510 
511 ////////////////////////////////////////////////////////////////////////////////
512 /// Evaluate a std::vector<double> of input data for a given method
513 /// The parameter aux is obligatory for the cuts method where it represents the efficiency cutoff
514 
515 Double_t TMVA::Reader::EvaluateMVA( const std::vector<Double_t>& inputVec, const TString& methodTag, Double_t aux )
516 {
517  // performs a copy to float values which are internally used by all methods
518  if(fTmpEvalVec.size() != inputVec.size())
519  fTmpEvalVec.resize(inputVec.size());
520 
521  for (UInt_t idx=0; idx!=inputVec.size(); idx++ )
522  fTmpEvalVec[idx]=inputVec[idx];
523 
524  return EvaluateMVA( fTmpEvalVec, methodTag, aux );
525 }
526 
527 ////////////////////////////////////////////////////////////////////////////////
528 /// evaluates MVA for given set of input variables
529 
531 {
532  IMethod* method = 0;
533 
534  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
535  if (it == fMethodMap.end()) {
536  Log() << kINFO << "<EvaluateMVA> unknown classifier in map; "
537  << "you looked for \"" << methodTag << "\" within available methods: " << Endl;
538  for (it = fMethodMap.begin(); it!=fMethodMap.end(); it++) Log() << " --> " << it->first << Endl;
539  Log() << "Check calling string" << kFATAL << Endl;
540  }
541 
542  else method = it->second;
543 
544  MethodBase * kl = dynamic_cast<TMVA::MethodBase*>(method);
545 
546  if(kl==0)
547  Log() << kFATAL << methodTag << " is not a method" << Endl;
548 
549  // check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
550  // it is not again checked in each of these subsequet calls..
551  const Event* ev = kl->GetEvent();
552  for (UInt_t i=0; i<ev->GetNVariables(); i++){
553  if (TMath::IsNaN(ev->GetValue(i))) {
554  Log() << kERROR << i << "-th variable of the event is NaN --> return MVA value -999, \n that's all I can do, please fix or remove this event." << Endl;
555  return -999;
556  }
557  }
558  return this->EvaluateMVA( kl, aux );
559 }
560 
561 ////////////////////////////////////////////////////////////////////////////////
562 /// evaluates the MVA
563 
565 {
566  // the aux value is only needed for MethodCuts: it sets the
567  // required signal efficiency
568  if (method->GetMethodType() == TMVA::Types::kCuts) {
569  TMVA::MethodCuts* mc = dynamic_cast<TMVA::MethodCuts*>(method);
570  if(mc)
571  mc->SetTestSignalEfficiency( aux );
572  }
573 
574  return method->GetMvaValue( (fCalculateError?&fMvaEventError:0),
575  (fCalculateError?&fMvaEventErrorUpper:0) );
576 }
577 
578 ////////////////////////////////////////////////////////////////////////////////
579 /// evaluates MVA for given set of input variables
580 
581 const std::vector< Float_t >& TMVA::Reader::EvaluateRegression( const TString& methodTag, Double_t aux )
582 {
583  IMethod* method = 0;
584 
585  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
586  if (it == fMethodMap.end()) {
587  Log() << kINFO << "<EvaluateMVA> unknown method in map; "
588  << "you looked for \"" << methodTag << "\" within available methods: " << Endl;
589  for (it = fMethodMap.begin(); it!=fMethodMap.end(); it++) Log() << " --> " << it->first << Endl;
590  Log() << "Check calling string" << kFATAL << Endl;
591  }
592  else method = it->second;
593 
594  MethodBase * kl = dynamic_cast<TMVA::MethodBase*>(method);
595 
596  if(kl==0)
597  Log() << kFATAL << methodTag << " is not a method" << Endl;
598  // check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
599  // it is not again checked in each of these subsequet calls..
600  const Event* ev = kl->GetEvent();
601  for (UInt_t i=0; i<ev->GetNVariables(); i++){
602  if (TMath::IsNaN(ev->GetValue(i))) {
603  Log() << kERROR << i << "-th variable of the event is NaN, \n regression values might evaluate to .. what do I know. \n sorry this warning is all I can do, please fix or remove this event." << Endl;
604  }
605  }
606 
607  return this->EvaluateRegression( kl, aux );
608 }
609 
610 ////////////////////////////////////////////////////////////////////////////////
611 /// evaluates the regression MVA
612 /// check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
613 /// it is not again checked in each of these subsequet calls..
614 
615 const std::vector< Float_t >& TMVA::Reader::EvaluateRegression( MethodBase* method, Double_t /*aux*/ )
616 {
617  const Event* ev = method->GetEvent();
618  for (UInt_t i=0; i<ev->GetNVariables(); i++){
619  if (TMath::IsNaN(ev->GetValue(i))) {
620  Log() << kERROR << i << "-th variable of the event is NaN, \n regression values might evaluate to .. what do I know. \n sorry this warning is all I can do, please fix or remove this event." << Endl;
621  }
622  }
623  return method->GetRegressionValues();
624 }
625 
626 
627 ////////////////////////////////////////////////////////////////////////////////
628 /// evaluates the regression MVA
629 
631 {
632  try {
633  return EvaluateRegression(methodTag, aux).at(tgtNumber);
634  }
635  catch (std::out_of_range e) {
636  Log() << kWARNING << "Regression could not be evaluated for target-number " << tgtNumber << Endl;
637  return 0;
638  }
639 }
640 
641 
642 
643 ////////////////////////////////////////////////////////////////////////////////
644 /// evaluates MVA for given set of input variables
645 
646 const std::vector< Float_t >& TMVA::Reader::EvaluateMulticlass( const TString& methodTag, Double_t aux )
647 {
648  IMethod* method = 0;
649 
650  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
651  if (it == fMethodMap.end()) {
652  Log() << kINFO << "<EvaluateMVA> unknown method in map; "
653  << "you looked for \"" << methodTag << "\" within available methods: " << Endl;
654  for (it = fMethodMap.begin(); it!=fMethodMap.end(); it++) Log() << " --> " << it->first << Endl;
655  Log() << "Check calling string" << kFATAL << Endl;
656  }
657  else method = it->second;
658 
659  MethodBase * kl = dynamic_cast<TMVA::MethodBase*>(method);
660 
661  if(kl==0)
662  Log() << kFATAL << methodTag << " is not a method" << Endl;
663  // check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
664  // it is not again checked in each of these subsequet calls..
665 
666  const Event* ev = kl->GetEvent();
667  for (UInt_t i=0; i<ev->GetNVariables(); i++){
668  if (TMath::IsNaN(ev->GetValue(i))) {
669  Log() << kERROR << i << "-th variable of the event is NaN, \n regression values might evaluate to .. what do I know. \n sorry this warning is all I can do, please fix or remove this event." << Endl;
670  }
671  }
672 
673  return this->EvaluateMulticlass( kl, aux );
674 }
675 
676 ////////////////////////////////////////////////////////////////////////////////
677 /// evaluates the multiclass MVA
678 /// check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
679 /// it is not again checked in each of these subsequet calls..
680 
681 const std::vector< Float_t >& TMVA::Reader::EvaluateMulticlass( MethodBase* method, Double_t /*aux*/ )
682 {
683  const Event* ev = method->GetEvent();
684  for (UInt_t i=0; i<ev->GetNVariables(); i++){
685  if (TMath::IsNaN(ev->GetValue(i))) {
686  Log() << kERROR << i << "-th variable of the event is NaN, \n regression values might evaluate to .. what do I know. \n sorry this warning is all I can do, please fix or remove this event." << Endl;
687  }
688  }
689  return method->GetMulticlassValues();
690 }
691 
692 
693 ////////////////////////////////////////////////////////////////////////////////
694 /// evaluates the multiclass MVA
695 
697 {
698  try {
699  return EvaluateMulticlass(methodTag, aux).at(clsNumber);
700  }
701  catch (std::out_of_range e) {
702  Log() << kWARNING << "Multiclass could not be evaluated for class-number " << clsNumber << Endl;
703  return 0;
704  }
705 }
706 
707 
708 ////////////////////////////////////////////////////////////////////////////////
709 /// return pointer to method with tag "methodTag"
710 
712 {
713  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
714  if (it != fMethodMap.end()) return it->second;
715  Log() << kERROR << "Method " << methodTag << " not found!" << Endl;
716  return 0;
717 }
718 
719 ////////////////////////////////////////////////////////////////////////////////
720 /// special function for Cuts to avoid dynamic_casts in ROOT macros,
721 /// which are not properly handled by CINT
722 
724 {
725  return dynamic_cast<MethodCuts*>(FindMVA(methodTag));
726 }
727 
728 ////////////////////////////////////////////////////////////////////////////////
729 /// evaluates probability of MVA for given set of input variables
730 
731 Double_t TMVA::Reader::GetProba( const TString& methodTag, Double_t ap_sig, Double_t mvaVal )
732 {
733  IMethod* method = 0;
734  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
735  if (it == fMethodMap.end()) {
736  for (it = fMethodMap.begin(); it!=fMethodMap.end(); it++) Log() << "M" << it->first << Endl;
737  Log() << kFATAL << "<EvaluateMVA> unknown classifier in map: " << method << "; "
738  << "you looked for " << methodTag<< " while the available methods are : " << Endl;
739  }
740  else method = it->second;
741 
742  MethodBase* kl = dynamic_cast<MethodBase*>(method);
743  if(kl==0) return -1;
744  // check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
745  // it is not again checked in each of these subsequet calls..
746  const Event* ev = kl->GetEvent();
747  for (UInt_t i=0; i<ev->GetNVariables(); i++){
748  if (TMath::IsNaN(ev->GetValue(i))) {
749  Log() << kERROR << i << "-th variable of the event is NaN --> return MVA value -999, \n that's all I can do, please fix or remove this event." << Endl;
750  return -999;
751  }
752  }
753 
754  if (mvaVal == -9999999) mvaVal = kl->GetMvaValue();
755 
756  return kl->GetProba( mvaVal, ap_sig );
757 }
758 
759 ////////////////////////////////////////////////////////////////////////////////
760 /// evaluates the MVA's rarity
761 
763 {
764  IMethod* method = 0;
765  std::map<TString, IMethod*>::iterator it = fMethodMap.find( methodTag );
766  if (it == fMethodMap.end()) {
767  for (it = fMethodMap.begin(); it!=fMethodMap.end(); it++) Log() << "M" << it->first << Endl;
768  Log() << kFATAL << "<EvaluateMVA> unknown classifier in map: \"" << method << "\"; "
769  << "you looked for \"" << methodTag<< "\" while the available methods are : " << Endl;
770  }
771  else method = it->second;
772 
773  MethodBase* kl = dynamic_cast<MethodBase*>(method);
774  if(kl==0) return -1;
775  // check for NaN in event data: (note: in the factory, this check was done already at the creation of the datasets, hence
776  // it is not again checked in each of these subsequet calls..
777  const Event* ev = kl->GetEvent();
778  for (UInt_t i=0; i<ev->GetNVariables(); i++){
779  if (TMath::IsNaN(ev->GetValue(i))) {
780  Log() << kERROR << i << "-th variable of the event is NaN --> return MVA value -999, \n that's all I can do, please fix or remove this event." << Endl;
781  return -999;
782  }
783  }
784 
785  if (mvaVal == -9999999) mvaVal = kl->GetMvaValue();
786 
787  return kl->GetRarity( mvaVal );
788 }
789 
790 // ---------------------------------------------------------------------------------------
791 // ----- methods related to the decoding of the input variable names ---------------------
792 // ---------------------------------------------------------------------------------------
793 
794 ////////////////////////////////////////////////////////////////////////////////
795 /// decodes "name1:name2:..." form
796 
797 void TMVA::Reader::DecodeVarNames( const std::string& varNames )
798 {
799  size_t ipos = 0, f = 0;
800  while (f != varNames.length()) {
801  f = varNames.find( ':', ipos );
802  if (f > varNames.length()) f = varNames.length();
803  std::string subs = varNames.substr( ipos, f-ipos ); ipos = f+1;
804  DataInfo().AddVariable( subs.c_str() );
805  }
806 }
807 
808 ////////////////////////////////////////////////////////////////////////////////
809 /// decodes "name1:name2:..." form
810 
811 void TMVA::Reader::DecodeVarNames( const TString& varNames )
812 {
813  TString format;
814  Int_t n = varNames.Length();
815  TString format_obj;
816 
817  for (int i=0; i< n+1 ; i++) {
818  format.Append(varNames(i));
819  if (varNames(i) == ':' || i == n) {
820  format.Chop();
821  format_obj = format;
822  format_obj.ReplaceAll("@","");
823  DataInfo().AddVariable( format_obj );
824  format.Resize(0);
825  }
826  }
827 }
IMethod * Create(const std::string &name, const TString &job, const TString &title, DataSetInfo &dsi, const TString &option)
creates the method if needed based on the method name using the creator function the factory has stor...
static ClassifierFactory & Instance()
access to the ClassifierFactory singleton creates the instance if needed
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:851
TXMLEngine & xmlengine()
Definition: Tools.h:277
MsgLogger * fLogger
Definition: Reader.h:174
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodBase.h:199
const DataSetInfo & DataInfo() const
Definition: Reader.h:130
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Ssiz_t Length() const
Definition: TString.h:390
virtual Double_t GetMvaValue(Double_t *errLower=0, Double_t *errUpper=0)=0
virtual ~Reader(void)
destructor
Definition: Reader.cxx:285
float Float_t
Definition: RtypesCore.h:53
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Definition: Reader.cxx:307
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
void SetTestSignalEfficiency(Double_t effS)
Definition: MethodCuts.h:132
Config & gConfig()
const std::vector< Float_t > & EvaluateRegression(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:581
const std::vector< Float_t > & GetRegressionValues(const TMVA::Event *const ev)
Definition: MethodBase.h:186
static const char * filename()
Double_t GetRarity(const TString &methodTag, Double_t mvaVal=-9999999)
evaluates the MVA's rarity
Definition: Reader.cxx:762
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
static std::string format(double x, double y, int digits, int width)
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
TFile * f
const TString & GetMethodName() const
Definition: MethodBase.h:296
void DecodeVarNames(const std::string &varNames)
decodes "name1:name2:..." form
Definition: Reader.cxx:797
static Types & Instance()
the the single instance of "Types" if existin already, or create it (Signleton)
Definition: Types.cxx:64
Tools & gTools()
Definition: Tools.cxx:79
TString GetMethodTypeFromFile(const TString &filename)
read the method type from the file
Definition: Reader.cxx:341
DataInputHandler fDataInputHandler
Definition: Reader.h:151
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:118
void DeclareOptions()
declaration of configuration options
Definition: Reader.cxx:272
void ReadStateFromFile()
Function to write options and weights to file.
ClassImp(TMVA::Reader) TMVA
constructor
Definition: Reader.cxx:126
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
TString & Append(const char *cs)
Definition: TString.h:492
virtual void ParseOptions()
options parser
virtual const char * GetName() const
Returns name of object.
Definition: Reader.h:126
void SetupMethod()
setup of methods
Definition: MethodBase.cxx:302
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
Definition: Reader.cxx:376
Types::EMVA GetMethodType() const
Definition: MethodBase.h:298
virtual Double_t GetProba(const Event *ev)
DataSetManager * fDataSetManager
Definition: Reader.h:141
UInt_t GetNVariables() const
accessor to the number of variables
Definition: Event.cxx:303
void ReadStateFromXMLString(const char *xmlstr)
for reading from memory
std::string GetMethodName(TCppMethod_t)
Definition: Cppyy.cxx:706
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2207
DataSetInfo & AddDataSetInfo(DataSetInfo &dsi)
stores a copy of the dataset info object
unsigned int UInt_t
Definition: RtypesCore.h:42
const Event * GetEvent() const
Definition: MethodBase.h:667
bool verbose
DataSetManager * fDataSetManager
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:295
Reader(const TString &theOption="", Bool_t verbose=0)
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
Parses content of file and tries to produce xml structures.
virtual void CheckSetup()
check may be overridden by derived class (sometimes, eg, fitters are used which can only be implement...
Definition: MethodBase.cxx:327
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
Definition: Reader.cxx:325
double Double_t
Definition: RtypesCore.h:55
DataSetInfo fDataSetInfo
Definition: Reader.h:149
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:231
RooCmdArg Verbose(Bool_t flag=kTRUE)
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
Definition: Reader.cxx:484
void Init(void)
default initialisation (no member variables) default initialisation (no member variables) ...
Definition: Reader.cxx:296
void SetUseColor(Bool_t uc)
Definition: Config.h:61
void SetConfigName(const char *n)
Definition: Configurable.h:70
IMethod * FindMVA(const TString &methodTag)
return pointer to method with tag "methodTag"
Definition: Reader.cxx:711
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
Definition: MethodBase.cxx:606
void SetSilent(Bool_t s)
Definition: Config.h:64
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
Int_t IsNaN(Double_t x)
Definition: TMath.h:617
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
#define NULL
Definition: Rtypes.h:82
VariableInfo & AddVariable(const TString &expression, const TString &title="", const TString &unit="", Double_t min=0, Double_t max=0, char varType='F', Bool_t normalized=kTRUE, void *external=0)
add a variable (can be a complex expression) to the set of variables used in the MV analysis ...
TString()
TString default ctor.
Definition: TString.cxx:87
virtual Double_t GetRarity(Double_t mvaVal, Types::ESBType reftype=Types::kBackground) const
compute rarity: R(x) = Integrate_[-oo..x] { PDF(x') dx' } where PDF(x) is the PDF of the classifier's...
TString GetMethodTypeName() const
Definition: MethodBase.h:297
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
const Int_t n
Definition: legend1.C:16
Definition: math.cpp:60
Double_t GetProba(const TString &methodTag, Double_t ap_sig=0.5, Double_t mvaVal=-9999999)
evaluates probability of MVA for given set of input variables
Definition: Reader.cxx:731
const std::vector< Float_t > & EvaluateMulticlass(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
Definition: Reader.cxx:646
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition: TString.cxx:1045
TString & Chop()
Definition: TString.h:622
MethodCuts * FindCutsMVA(const TString &methodTag)
special function for Cuts to avoid dynamic_casts in ROOT macros, which are not properly handled by CI...
Definition: Reader.cxx:723