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