Logo ROOT  
Reference Guide
VariableInfo.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 : VariableInfo *
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  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * *
18  * Copyright (c) 2005: *
19  * CERN, Switzerland *
20  * U. of Victoria, Canada *
21  * MPI-K Heidelberg, Germany *
22  * LAPP, Annecy, France *
23  * *
24  * Redistribution and use in source and binary forms, with or without *
25  * modification, are permitted according to the terms listed in LICENSE *
26  * (http://mva.sourceforge.net/license.txt) *
27  **********************************************************************************/
28 
29 /*! \class TMVA::VariableInfo
30 \ingroup TMVA
31 Class for type info of MVA input variable
32 */
33 
34 #include "TMVA/VariableInfo.h"
35 #include "TMVA/DataSetInfo.h"
36 
37 #include "TMVA/Tools.h"
38 
39 #include "TMath.h"
40 #include "TNamed.h"
41 
42 #include <iomanip>
43 #include <sstream>
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// constructor
47 
48 TMVA::VariableInfo::VariableInfo( const TString& expression, const TString& title, const TString& unit,
49  Int_t varCounter,
50  char varType, void* external,
51  Double_t min, Double_t max, Bool_t normalized )
52  : TNamed(title.Data(),title.Data()),
53  fExpression ( expression ),
54  fUnit ( unit ),
55  fVarType ( varType ),
56  fXmeanNorm ( 0 ),
57  fXrmsNorm ( 0 ),
58  fXvarianceNorm( 0 ),
59  fNormalized ( normalized ),
60  fExternalData ( external ),
61  fVarCounter ( varCounter )
62 {
63  if ( TMath::Abs(max - min) <= FLT_MIN ) {
64  fXminNorm = FLT_MAX;
65  fXmaxNorm = -FLT_MAX;
66  }
67  else {
68  fXminNorm = min;
69  fXmaxNorm = max;
70  }
71  // if a label is set, than retrieve the label and the
72  if (expression.Contains(":=")) {
73  Ssiz_t index = expression.Index(":=");
74  fExpression = expression(index+2,expression.Sizeof()-index-2);
75  fLabel = expression(0,index);
76  fLabel = fLabel.ReplaceAll(" ","");
77  }
78  else fLabel = GetExpression();
79 
80  if (fTitle == "") fTitle = fLabel;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// default constructor
86 
88  : TNamed(),
89  fExpression (""),
90  fVarType ('\0'),
91  fXmeanNorm ( 0 ),
92  fXrmsNorm ( 0 ),
93  fXvarianceNorm( 0 ),
94  fNormalized ( kFALSE ),
95  fExternalData ( 0 ),
96  fVarCounter ( 0 )
97 {
98  fXminNorm = 1e30;
99  fXmaxNorm = -1e30;
100  fLabel = GetExpression();
101  fTitle = fLabel;
102  fName = fTitle;
103  fUnit = "";
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// copy constructor
109 
111  : TNamed(other),
112  fExpression ( other.fExpression ),
113  fInternalName ( other.fInternalName ),
114  fLabel ( other.fLabel ),
115  fUnit ( other.fUnit ),
116  fVarType ( other.fVarType ),
117  fXminNorm ( other.fXminNorm ),
118  fXmaxNorm ( other.fXmaxNorm ),
119  fXmeanNorm ( other.fXmeanNorm ),
120  fXrmsNorm ( other.fXrmsNorm ),
121  fXvarianceNorm( other.fXvarianceNorm ),
122  fNormalized ( other.fNormalized ),
123  fExternalData ( other.fExternalData ),
124  fVarCounter ( other.fVarCounter )
125 {
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// comparison operator
130 
132 {
133  if (this !=& rhs) {
134  fExpression = rhs.fExpression;
135  fInternalName = rhs.fInternalName;
136  fVarType = rhs.fVarType;
137  fXminNorm = rhs.fXminNorm;
138  fXmaxNorm = rhs.fXmaxNorm;
139  fTitle = rhs.fTitle;
140  fName = rhs.fName;
141  }
142  return *this;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// write VariableInfo to stream
147 
148 void TMVA::VariableInfo::WriteToStream( std::ostream& o ) const
149 {
150  UInt_t nc = TMath::Max( 30, TMath::Max( GetExpression().Length()+1, GetInternalName().Length()+1 ) );
151  TString expBr(Form("\'%s\'",GetExpression().Data()));
152  o << std::setw(nc) << GetExpression();
153  o << std::setw(nc) << GetInternalName();
154  o << std::setw(nc) << GetLabel();
155  o << std::setw(nc) << GetTitle();
156  o << std::setw(nc) << GetUnit();
157  o << " \'" << fVarType << "\' ";
158  o << "[" << std::setprecision(12) << GetMin() << "," << std::setprecision(12) << GetMax() << "]" << std::endl;
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// read VariableInfo from stream
163 
164 void TMVA::VariableInfo::ReadFromStream( std::istream& istr )
165 {
166  // PLEASE do not modify this, it does not have to correspond to WriteToStream
167  // this is needed to stay like this in 397 for backward compatibility
168  TString exp, varname, vartype, minmax, minstr, maxstr;
169  istr >> exp >> varname >> vartype >> minmax;
170  exp.Strip(TString::kBoth, '\'');
171  minmax = minmax.Strip(TString::kLeading, '[');
172  minmax = minmax.Strip(TString::kTrailing, ']');
173  minstr = minmax(0,minmax.First(','));
174  maxstr = minmax(1+minmax.First(','),minmax.Length());
175  Double_t min, max;
176  std::stringstream strmin(minstr.Data());
177  std::stringstream strmax(maxstr.Data());
178  strmin >> min;
179  strmax >> max;
180  SetExpression ( exp );
181  SetInternalVarName( varname );
182  SetLabel ( varname );
183  SetTitle ( varname );
184  SetUnit ( "" );
185  SetVarType ( vartype[1] );
186  SetMin ( min );
187  SetMax ( max );
188 }
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// write class to XML
192 
193 void TMVA::VariableInfo::AddToXML( void* varnode )
194 {
195  gTools().AddAttr( varnode, "Expression", GetExpression() );
196  gTools().AddAttr( varnode, "Label", GetLabel() );
197  gTools().AddAttr( varnode, "Title", GetTitle() );
198  gTools().AddAttr( varnode, "Unit", GetUnit() );
199  gTools().AddAttr( varnode, "Internal", GetInternalName() );
200 
201  TString typeStr(" ");
202  typeStr[0] = GetVarType();
203  // in case of array variables, add "[]" to the type string: e.g. F[]
204  if (TestBit(DataSetInfo::kIsArrayVariable))
205  typeStr += "[]";
206  gTools().AddAttr(varnode, "Type", typeStr);
207  gTools().AddAttr( varnode, "Min", gTools().StringFromDouble(GetMin()) );
208  gTools().AddAttr( varnode, "Max", gTools().StringFromDouble(GetMax()) );
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// read VariableInfo from stream
213 
214 void TMVA::VariableInfo::ReadFromXML( void* varnode )
215 {
216  TString type;
217  gTools().ReadAttr( varnode, "Expression", fExpression );
218  gTools().ReadAttr( varnode, "Label", fLabel );
219  gTools().ReadAttr( varnode, "Title", fTitle );
220  gTools().ReadAttr( varnode, "Unit", fUnit );
221  gTools().ReadAttr( varnode, "Internal", fInternalName );
222  gTools().ReadAttr( varnode, "Type", type );
223  gTools().ReadAttr( varnode, "Min", fXminNorm );
224  gTools().ReadAttr( varnode, "Max", fXmaxNorm );
225 
226  SetVarType(type[0]);
227  // detect variables from array
228  if (type.Contains("[]"))
230 }
TMath::Max
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
TMVA::VariableInfo::operator=
VariableInfo & operator=(const TMVA::VariableInfo &rhs)
comparison operator
Definition: VariableInfo.cxx:131
TString::Data
const char * Data() const
Definition: TString.h:369
DataSetInfo.h
Form
char * Form(const char *fmt,...)
TNamed.h
TMVA::VariableInfo::WriteToStream
void WriteToStream(std::ostream &o) const
write VariableInfo to stream
Definition: VariableInfo.cxx:148
exp
double exp(double)
VariableInfo.h
TNamed::fName
TString fName
Definition: TNamed.h:32
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TString
Basic string class.
Definition: TString.h:136
bool
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TNamed::fTitle
TString fTitle
Definition: TNamed.h:33
TString::kBoth
@ kBoth
Definition: TString.h:267
TMVA::VariableInfo::AddToXML
void AddToXML(void *varnode)
write class to XML
Definition: VariableInfo.cxx:193
TMVA::Tools::AddAttr
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition: Tools.h:353
TMVA::Tools::ReplaceRegularExpressions
TString ReplaceRegularExpressions(const TString &s, const TString &replace="+")
replace regular expressions helper function to remove all occurrences "$!%^&()'<>?...
Definition: Tools.cxx:810
TMVA::VariableInfo::fInternalName
TString fInternalName
Definition: VariableInfo.h:98
TMVA::VariableInfo::fVarType
Char_t fVarType
Definition: VariableInfo.h:102
TMVA::VariableInfo::ReadFromXML
void ReadFromXML(void *varnode)
read VariableInfo from stream
Definition: VariableInfo.cxx:214
TMVA::VariableInfo::ReadFromStream
void ReadFromStream(std::istream &istr)
read VariableInfo from stream
Definition: VariableInfo.cxx:164
TString::kTrailing
@ kTrailing
Definition: TString.h:267
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMVA::VariableInfo
Class for type info of MVA input variable.
Definition: VariableInfo.h:47
TMVA::Tools::ReadAttr
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:335
TMVA::DataSetInfo::kIsArrayVariable
@ kIsArrayVariable
Definition: DataSetInfo.h:66
unsigned int
TString::Index
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:639
TString::Sizeof
virtual Int_t Sizeof() const
Returns size string will occupy on I/O buffer.
Definition: TString.cxx:1334
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::VariableInfo::GetExpression
const TString & GetExpression() const
Definition: VariableInfo.h:57
TString::kLeading
@ kLeading
Definition: TString.h:267
TMVA::VariableInfo::VariableInfo
VariableInfo()
default constructor
Definition: VariableInfo.cxx:87
Tools.h
TMVA::VariableInfo::fXminNorm
Double_t fXminNorm
Definition: VariableInfo.h:103
type
int type
Definition: TGX11.cxx:121
TMVA::gTools
Tools & gTools()
TMVA::VariableInfo::fXmaxNorm
Double_t fXmaxNorm
Definition: VariableInfo.h:104
TMVA::VariableInfo::fLabel
TString fLabel
Definition: VariableInfo.h:99
TMath.h
int
TMVA::VariableInfo::fExpression
TString fExpression
Definition: VariableInfo.h:97
TMVA::VariableInfo::fUnit
TString fUnit
Definition: VariableInfo.h:101