Logo ROOT   6.16/01
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
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
47TMVA::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 ),
120 fXvarianceNorm( other.fXvarianceNorm ),
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;
134 fInternalName = rhs.fInternalName;
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
147void 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
163void 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
192void 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
211{
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}
int Int_t
Definition: RtypesCore.h:41
int Ssiz_t
Definition: RtypesCore.h:63
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
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:810
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:337
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition: Tools.h:355
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:1081
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:487
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:1309
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