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