60 ClassImp(TMVA::Configurable);
64 #pragma warning ( disable : 4355 )
70 TMVA::Configurable::Configurable(
const TString& theOption)
71 : TNamed(
"Configurable",
"Configurable"),
72 fOptions ( theOption ),
73 fLooseOptionCheckingEnabled ( kTRUE ),
74 fLastDeclaredOption ( 0 ),
75 fConfigDescription (
"No description" ),
76 fReferenceFile (
"None" ),
77 fLogger ( new MsgLogger(this) )
79 fListOfOptions.SetOwner();
82 if (gTools().CheckForVerboseOption( theOption )) Log().SetMinType( kVERBOSE );
88 TMVA::Configurable::~Configurable()
96 void TMVA::Configurable::SplitOptions(
const TString& theOpt, TList& loo)
const
98 TString splitOpt(theOpt);
100 while (splitOpt.Length()>0) {
101 if (!splitOpt.Contains(
':')) {
102 loo.Add(
new TObjString(splitOpt));
106 TString toSave = splitOpt(0,splitOpt.First(
':'));
107 loo.Add(
new TObjString(toSave.Data()));
108 splitOpt = splitOpt(splitOpt.First(
':')+1,splitOpt.Length());
117 void TMVA::Configurable::ResetSetFlag()
119 TListIter decOptIt(&fListOfOptions);
120 while (OptionBase* decOpt = (OptionBase*) decOptIt()) {
121 decOpt->fIsSet = kFALSE;
128 void TMVA::Configurable::ParseOptions()
130 Log() << kVERBOSE <<
"Parsing option string: " << Endl;
131 TString optionsWithoutTilde(fOptions);
132 optionsWithoutTilde.ReplaceAll(TString(
"~"),TString(
""));
133 Log() << kVERBOSE <<
"... \"" << optionsWithoutTilde <<
"\"" << Endl;
137 fOptions = fOptions.Strip(TString::kLeading,
':');
140 SplitOptions(fOptions, loo);
144 std::map<TString, std::vector<std::pair<Int_t, TString> > > arrayTypeOptions;
146 TListIter decOptIt(&fListOfOptions);
147 TListIter setOptIt(&loo);
148 while (TObjString * os = (TObjString*) setOptIt()) {
150 TString s = os->GetString();
159 Bool_t preserveTilde = s.BeginsWith(
'~');
160 s = s.Strip(TString::kLeading,
'~');
162 Bool_t paramParsed = kFALSE;
163 if (s.Contains(
'=')) {
164 TString optname = s(0,s.First(
'=')); optname.ToLower();
165 TString optval = s(s.First(
'=')+1,s.Length());
172 OptionBase* decOpt = (OptionBase *)fListOfOptions.FindObject(optname);
173 if (decOpt==0 && optname.Contains(
'[')) {
176 TString st = optname(optname.First(
'[')+1,100);
177 st.Remove(st.First(
']'));
178 std::stringstream str(st.Data());
180 optname.Remove(optname.First(
'['));
181 decOpt = (OptionBase *)fListOfOptions.FindObject(optname);
184 TListIter optIt(&fListOfOptions);
187 Log() << kWARNING <<
"Value for option " << decOpt->GetName()
188 <<
" was previously set to " << decOpt->GetValue() << Endl;
190 if (!decOpt->HasPreDefinedVal() || (decOpt->HasPreDefinedVal() && decOpt->IsPreDefinedVal(optval)) ) {
191 if (decOpt->IsArrayOpt()) {
194 decOpt->SetValue(optval);
198 if (!decOpt->SetValue(optval, idx))
199 Log() << kFATAL <<
"Index " << idx <<
" too large for option " << decOpt->TheName()
200 <<
", allowed range is [0," << decOpt->GetArraySize()-1 <<
"]" << Endl;
205 Log() << kFATAL <<
"Option " << decOpt->TheName()
206 <<
" is not an array, but you specified an index" << Endl;
207 decOpt->SetValue(optval);
211 else Log() << kFATAL <<
"Option " << decOpt->TheName()
212 <<
" does not have predefined value: \"" << optval <<
"\"" << Endl;
218 Bool_t preserveNotSign = kFALSE;
220 Bool_t hasNotSign = kFALSE;
221 if (s.BeginsWith(
"!")) { s.Remove(0,1); preserveNotSign = hasNotSign = kTRUE; }
222 TString optname(s); optname.ToLower();
223 OptionBase* decOpt = 0;
224 Bool_t optionExists = kFALSE;
225 TListIter optIt(&fListOfOptions);
226 while ((decOpt = (OptionBase*)optIt()) !=0) {
227 TString predOptName(decOpt->GetName());
228 predOptName.ToLower();
229 if (predOptName == optname) optionExists = kTRUE;
230 if (
dynamic_cast<Option<bool>*
>(decOpt)==0)
continue;
231 if (predOptName == optname)
break;
236 decOpt->SetValue( hasNotSign ?
"0" :
"1" );
240 if (optionExists && hasNotSign) {
241 Log() << kFATAL <<
"Negating a non-boolean variable " << optname
242 <<
", please check the options for method: " << GetName() << Endl;
248 if (!paramParsed && LooseOptionCheckingEnabled()) {
252 while (OptionBase* decOpt = (OptionBase*) decOptIt()) {
253 if (decOpt->HasPreDefinedVal() && decOpt->IsPreDefinedVal(s) ) {
254 paramParsed = decOpt->SetValue(s);
260 if (fOptions!=
"") fOptions +=
":";
261 if (paramParsed || preserveTilde) fOptions +=
'~';
262 if (preserveNotSign) fOptions +=
'!';
268 if (gConfig().WriteOptionsReference()) WriteOptionsReferenceToFile();
274 void TMVA::Configurable::CheckForUnusedOptions()
const
276 TString theOpt(fOptions);
277 theOpt = theOpt.Strip(TString::kLeading,
':');
281 SplitOptions(theOpt, loo);
283 TListIter setOptIt(&loo);
284 TString unusedOptions(
"");
285 while (TObjString * os = (TObjString*) setOptIt()) {
287 TString s = os->GetString();
288 if (!s.BeginsWith(
'~')) {
289 if (unusedOptions !=
"") unusedOptions +=
':';
293 if (unusedOptions !=
"") {
295 <<
"The following options were specified, but could not be interpreted: \'"
296 << unusedOptions <<
"\', please check!" << Endl;
303 void TMVA::Configurable::PrintOptions()
const
305 Log() << kVERBOSE <<
"The following options are set:" << Endl;
307 TListIter optIt( &fListOfOptions );
308 Log() << kVERBOSE <<
"- By User:" << Endl;
309 Bool_t found = kFALSE;
310 while (OptionBase* opt = (OptionBase *) optIt()) {
312 Log() << kVERBOSE <<
" ";
313 std::ostringstream oss;
316 Log() << Endl; found = kTRUE; }
318 if (!found) Log() << kVERBOSE <<
" <none>" << Endl;
321 Log() << kVERBOSE <<
"- Default:" << Endl;
323 while (OptionBase* opt = (OptionBase *) optIt()) {
325 Log() << kVERBOSE <<
" ";
326 std::ostringstream oss;
329 Log() << Endl; found = kTRUE; }
331 if (!found) Log() << kVERBOSE <<
" <none>" << Endl;
337 void TMVA::Configurable::WriteOptionsToStream( std::ostream& o,
const TString& prefix )
const
339 TListIter optIt( &fListOfOptions );
340 o << prefix <<
"# Set by User:" << std::endl;
341 while (OptionBase * opt = (OptionBase *) optIt())
342 if (opt->IsSet()) { o << prefix; opt->Print(o); o << std::endl; }
344 o << prefix <<
"# Default:" << std::endl;
345 while (OptionBase * opt = (OptionBase *) optIt())
346 if (!opt->IsSet()) { o << prefix; opt->Print(o); o << std::endl; }
347 o << prefix <<
"##" << std::endl;
353 void TMVA::Configurable::AddOptionsXMLTo(
void* parent )
const
356 void* opts = gTools().AddChild(parent,
"Options");
357 TListIter optIt( &fListOfOptions );
358 while (OptionBase * opt = (OptionBase *) optIt()) {
360 if (opt->IsArrayOpt()) {
361 std::stringstream s(
"");
363 for(Int_t i=0; i<opt->GetArraySize(); i++) {
365 s << std::scientific << opt->GetValue(i);
367 optnode = gTools().AddChild(opts,
"Option",s.str().c_str());
370 optnode = gTools().AddChild(opts,
"Option", opt->GetValue());
372 gTools().AddAttr(optnode,
"name", opt->TheName());
373 if (opt->IsArrayOpt()) {
374 gTools().AddAttr(optnode,
"size", opt->GetArraySize());
376 gTools().AddAttr(optnode,
"modified", (opt->IsSet()?
"Yes":
"No") );
382 void TMVA::Configurable::ReadOptionsFromXML(
void* node )
384 void* opt = gTools().GetChild(node);
385 TString optName, optValue;
388 if (fOptions.Length()!=0) fOptions +=
":";
389 gTools().ReadAttr(opt,
"name", optName);
390 optValue = TString( gTools().GetContent(opt) );
391 std::stringstream s(
"");
393 if (gTools().HasAttr(opt,
"size")) {
395 gTools().ReadAttr(opt,
"size", size);
396 std::vector<TString> values = gTools().SplitString(optValue,
' ');
397 for(UInt_t i=0; i<size; i++) {
399 s << std::scientific << optName <<
"[" << i <<
"]=" << values[i];
403 s << std::scientific << optName <<
"=" << optValue;
405 fOptions += s.str().c_str();
406 opt = gTools().GetNextChild(opt);
413 void TMVA::Configurable::WriteOptionsReferenceToFile()
415 TString dir = gConfig().GetIONames().fOptionsReferenceFileDir;
416 gSystem->MakeDirectory( dir );
417 fReferenceFile = dir +
"/" + GetConfigName() +
"_optionsRef.txt";
418 std::ofstream o( fReferenceFile );
420 Log() << kFATAL <<
"<WriteOptionsToInfoFile> Unable to open output file: " << fReferenceFile << Endl;
423 TListIter optIt( &fListOfOptions );
424 o <<
"# List of options:" << std::endl;
425 o <<
"# Configurable: " << GetConfigName() << std::endl;
426 o <<
"# Description: " << GetConfigDescription() << std::endl;
427 while (OptionBase * opt = (OptionBase *) optIt()) {
429 o << std::endl <<
"# ------------------------------------------------" << std::endl;
433 Log() << kVERBOSE <<
"Wrote options reference file: \"" << fReferenceFile <<
"\"" << Endl;
439 void TMVA::Configurable::ReadOptionsFromStream(std::istream& istr)
447 istr.getline(buf,512);
448 TString stropt, strval;
449 while (istr.good() && !istr.eof() && !(buf[0]==
'#' && buf[1]==
'#')) {
451 while (*p==
' ' || *p==
'\t') p++;
452 if (*p==
'#' || *p==
'\0') {
453 istr.getline(buf,512);
456 std::stringstream sstr(buf);
457 sstr >> stropt >> strval;
458 stropt.ReplaceAll(
':',
'=');
459 strval.ReplaceAll(
"\"",
"");
460 if (fOptions.Length()!=0) fOptions +=
":";
463 istr.getline(buf,512);