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
31Class 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
48TMVA::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;
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
148void 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
164void 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
193void 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[]
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
215{
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}
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
int type
Definition: TGX11.cxx:120
double exp(double)
char * Form(const char *fmt,...)
TString ReplaceRegularExpressions(const TString &s, const TString &replace="+")
replace regular expressions helper function to remove all occurrences "$!%^&()'<>?...
Definition: Tools.cxx:809
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:335
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition: Tools.h:353
Class for type info of MVA input variable.
Definition: VariableInfo.h:47
void ReadFromXML(void *varnode)
read VariableInfo from stream
VariableInfo & operator=(const TMVA::VariableInfo &rhs)
comparison operator
const TString & GetExpression() const
Definition: VariableInfo.h:57
VariableInfo()
default constructor
void ReadFromStream(std::istream &istr)
read VariableInfo from stream
void AddToXML(void *varnode)
write class to XML
void WriteToStream(std::ostream &o) const
write VariableInfo to stream
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString fTitle
Definition: TNamed.h:33
TString fName
Definition: TNamed.h:32
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1106
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:499
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
@ kLeading
Definition: TString.h:262
@ kTrailing
Definition: TString.h:262
@ kBoth
Definition: TString.h:262
virtual Int_t Sizeof() const
Returns size string will occupy on I/O buffer.
Definition: TString.cxx:1334
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:634
Tools & gTools()
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
Short_t Abs(Short_t d)
Definition: TMathBase.h:120