Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 #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 
48 TMVA::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;
81  fInternalName = gTools().ReplaceRegularExpressions( fLabel, "_" );
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// default constructor
86 
87 TMVA::VariableInfo::VariableInfo()
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;
100  fLabel = GetExpression();
101  fTitle = fLabel;
102  fName = fTitle;
103  fUnit = "";
104  fInternalName = gTools().ReplaceRegularExpressions( fLabel, "_" );
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// copy constructor
109 
110 TMVA::VariableInfo::VariableInfo( const VariableInfo& other )
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 
131 TMVA::VariableInfo& TMVA::VariableInfo::operator=(const VariableInfo& rhs)
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 
148 void 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 
164 void 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 
193 void 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[]
204  if (TestBit(DataSetInfo::kIsArrayVariable))
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 
214 void TMVA::VariableInfo::ReadFromXML( void* varnode )
215 {
216  TString type;
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("[]"))
229  SetBit(DataSetInfo::kIsArrayVariable);
230 }