Logo ROOT   6.10/09
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 
36 #include "TMVA/Tools.h"
37 
38 #include "TMath.h"
39 #include "TNamed.h"
40 
41 #include <iomanip>
42 #include <sstream>
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// constructor
46 
47 TMVA::VariableInfo::VariableInfo( const TString& expression, const TString& title, const TString& unit,
48  Int_t varCounter,
49  char varType, void* external,
50  Double_t min, Double_t max, Bool_t normalized )
51  : TNamed(title.Data(),title.Data()),
52  fExpression ( expression ),
53  fUnit ( unit ),
54  fVarType ( varType ),
55  fXmeanNorm ( 0 ),
56  fXrmsNorm ( 0 ),
57  fXvarianceNorm( 0 ),
58  fNormalized ( normalized ),
59  fExternalData ( external ),
60  fVarCounter ( varCounter )
61 {
62  if ( TMath::Abs(max - min) <= FLT_MIN ) {
63  fXminNorm = FLT_MAX;
64  fXmaxNorm = -FLT_MAX;
65  }
66  else {
67  fXminNorm = min;
68  fXmaxNorm = max;
69  }
70  // if a label is set, than retrieve the label and the
71  if (expression.Contains(":=")) {
72  Ssiz_t index = expression.Index(":=");
73  fExpression = expression(index+2,expression.Sizeof()-index-2);
74  fLabel = expression(0,index);
75  fLabel = fLabel.ReplaceAll(" ","");
76  }
77  else fLabel = GetExpression();
78 
79  if (fTitle == "") fTitle = fLabel;
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// default constructor
85 
87  : TNamed(),
88  fExpression (""),
89  fVarType ('\0'),
90  fXmeanNorm ( 0 ),
91  fXrmsNorm ( 0 ),
92  fXvarianceNorm( 0 ),
93  fNormalized ( kFALSE ),
94  fExternalData ( 0 ),
95  fVarCounter ( 0 )
96 {
97  fXminNorm = 1e30;
98  fXmaxNorm = -1e30;
100  fTitle = fLabel;
101  fName = fTitle;
102  fUnit = "";
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// copy constructor
108 
110  : TNamed(other),
111  fExpression ( other.fExpression ),
112  fInternalName ( other.fInternalName ),
113  fLabel ( other.fLabel ),
114  fUnit ( other.fUnit ),
115  fVarType ( other.fVarType ),
116  fXminNorm ( other.fXminNorm ),
117  fXmaxNorm ( other.fXmaxNorm ),
118  fXmeanNorm ( other.fXmeanNorm ),
119  fXrmsNorm ( other.fXrmsNorm ),
121  fNormalized ( other.fNormalized ),
122  fExternalData ( other.fExternalData ),
123  fVarCounter ( other.fVarCounter )
124 {
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// comparison operator
129 
131 {
132  if (this !=& rhs) {
133  fExpression = rhs.fExpression;
135  fVarType = rhs.fVarType;
136  fXminNorm = rhs.fXminNorm;
137  fXmaxNorm = rhs.fXmaxNorm;
138  fTitle = rhs.fTitle;
139  fName = rhs.fName;
140  }
141  return *this;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// write VariableInfo to stream
146 
147 void TMVA::VariableInfo::WriteToStream( std::ostream& o ) const
148 {
149  UInt_t nc = TMath::Max( 30, TMath::Max( GetExpression().Length()+1, GetInternalName().Length()+1 ) );
150  TString expBr(Form("\'%s\'",GetExpression().Data()));
151  o << std::setw(nc) << GetExpression();
152  o << std::setw(nc) << GetInternalName();
153  o << std::setw(nc) << GetLabel();
154  o << std::setw(nc) << GetTitle();
155  o << std::setw(nc) << GetUnit();
156  o << " \'" << fVarType << "\' ";
157  o << "[" << std::setprecision(12) << GetMin() << "," << std::setprecision(12) << GetMax() << "]" << std::endl;
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// read VariableInfo from stream
162 
163 void TMVA::VariableInfo::ReadFromStream( std::istream& istr )
164 {
165  // PLEASE do not modify this, it does not have to correspond to WriteToStream
166  // this is needed to stay like this in 397 for backward compatibility
167  TString exp, varname, vartype, minmax, minstr, maxstr;
168  istr >> exp >> varname >> vartype >> minmax;
169  exp.Strip(TString::kBoth, '\'');
170  minmax = minmax.Strip(TString::kLeading, '[');
171  minmax = minmax.Strip(TString::kTrailing, ']');
172  minstr = minmax(0,minmax.First(','));
173  maxstr = minmax(1+minmax.First(','),minmax.Length());
174  Double_t min, max;
175  std::stringstream strmin(minstr.Data());
176  std::stringstream strmax(maxstr.Data());
177  strmin >> min;
178  strmax >> max;
179  SetExpression ( exp );
180  SetInternalVarName( varname );
181  SetLabel ( varname );
182  SetTitle ( varname );
183  SetUnit ( "" );
184  SetVarType ( vartype[1] );
185  SetMin ( min );
186  SetMax ( max );
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// write class to XML
191 
192 void TMVA::VariableInfo::AddToXML( void* varnode )
193 {
194  gTools().AddAttr( varnode, "Expression", GetExpression() );
195  gTools().AddAttr( varnode, "Label", GetLabel() );
196  gTools().AddAttr( varnode, "Title", GetTitle() );
197  gTools().AddAttr( varnode, "Unit", GetUnit() );
198  gTools().AddAttr( varnode, "Internal", GetInternalName() );
199 
200  TString typeStr(" ");
201  typeStr[0] = GetVarType();
202  gTools().AddAttr( varnode, "Type", typeStr );
203  gTools().AddAttr( varnode, "Min", gTools().StringFromDouble(GetMin()) );
204  gTools().AddAttr( varnode, "Max", gTools().StringFromDouble(GetMax()) );
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// read VariableInfo from stream
209 
210 void TMVA::VariableInfo::ReadFromXML( void* varnode )
211 {
212  TString type;
213  gTools().ReadAttr( varnode, "Expression", fExpression );
214  gTools().ReadAttr( varnode, "Label", fLabel );
215  gTools().ReadAttr( varnode, "Title", fTitle );
216  gTools().ReadAttr( varnode, "Unit", fUnit );
217  gTools().ReadAttr( varnode, "Internal", fInternalName );
218  gTools().ReadAttr( varnode, "Type", type );
219  gTools().ReadAttr( varnode, "Min", fXminNorm );
220  gTools().ReadAttr( varnode, "Max", fXmaxNorm );
221 
222  SetVarType(type[0]);
223 }
TString fTitle
Definition: TNamed.h:33
const TString & GetInternalName() const
Definition: VariableInfo.h:58
Double_t GetMin() const
Definition: VariableInfo.h:63
Double_t fXvarianceNorm
Definition: VariableInfo.h:105
void SetUnit(const TString &s)
Definition: VariableInfo.h:91
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:587
VariableInfo & operator=(const TMVA::VariableInfo &rhs)
comparison operator
Basic string class.
Definition: TString.h:129
const TString & GetUnit() const
Definition: VariableInfo.h:60
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const TString & GetLabel() const
Definition: VariableInfo.h:59
void SetLabel(const TString &s)
Definition: VariableInfo.h:90
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition: Tools.h:308
Short_t Abs(Short_t d)
Definition: TMathBase.h:108
const TString & GetExpression() const
Definition: VariableInfo.h:57
char GetVarType() const
Definition: VariableInfo.h:61
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
std::vector< std::vector< double > > Data
void SetMin(Double_t v)
Definition: VariableInfo.h:69
Double_t GetMax() const
Definition: VariableInfo.h:64
void SetInternalVarName(const TString &s)
Definition: VariableInfo.h:92
TString ReplaceRegularExpressions(const TString &s, const TString &replace="+")
replace regular expressions helper function to remove all occurrences "$!%^&()&#39;<>?= " from a string and replace all ::,$,*,/,+,- with M,S,T,D,P,M respectively
Definition: Tools.cxx:809
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
void ReadFromXML(void *varnode)
read VariableInfo from stream
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1080
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:290
Tools & gTools()
VariableInfo()
default constructor
TString fName
Definition: TNamed.h:32
void SetMax(Double_t v)
Definition: VariableInfo.h:70
const Bool_t kFALSE
Definition: RtypesCore.h:92
int Ssiz_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:55
int type
Definition: TGX11.cxx:120
virtual Int_t Sizeof() const
Returns size string will occupy on I/O buffer.
Definition: TString.cxx:1308
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
void SetExpression(const TString &s)
Definition: VariableInfo.h:89
void ReadFromStream(std::istream &istr)
read VariableInfo from stream
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:200
void AddToXML(void *varnode)
write class to XML
void WriteToStream(std::ostream &o) const
write VariableInfo to stream
void SetVarType(char c)
Definition: VariableInfo.h:93
Class for type info of MVA input variable.
Definition: VariableInfo.h:47
double exp(double)
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:347