KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVNamedParameter.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Wed Nov 30 13:54:07 2011
2 //Author: John Frankland,,,
3 
4 #include "KVNamedParameter.h"
5 #include "KVBase.h"
6 #include "Riostream.h"
7 #include "TROOT.h"
8 
9 using namespace std;
10 
12 
13 
14 
18 {
19  // Default constructor
20  ResetBits();
21 }
22 
23 
24 
27 
29 {
30  // Destructor
31 }
32 
33 
34 
36 
38  : TNamed(nom, ""), fNumber(0.0)
39 {
40  ResetBits();
41 }
42 
43 
44 
46 
47 KVNamedParameter::KVNamedParameter(const char* nom, const char* val)
48  : TNamed(nom, val), fNumber(0.0)
49 {
51 }
52 
53 
54 
56 
57 void KVNamedParameter::Set(const char* name, const char* val)
58 {
59  SetNameTitle(name, val);
61  fNumber = 0.0;
62 }
63 
64 
65 
67 
68 void KVNamedParameter::Set(const char* val)
69 {
70  SetTitle(val);
72  fNumber = 0.0;
73 }
74 
75 
76 
78 
80  : TNamed(nom, "Double_t"), fNumber(val)
81 {
83 }
84 
85 
86 
88 
89 void KVNamedParameter::Set(const char* name, Double_t val)
90 {
91  SetNameTitle(name, "Double_t");
93  fNumber = val;
94 }
95 
96 
97 
99 
101 {
102  SetTitle("Double_t");
104  fNumber = val;
105 }
106 
107 
108 
110 
112  : TNamed(nom, "Int_t"), fNumber(val)
113 {
114  SetType(kIsInt);
115 }
116 
117 
118 
120 
122  : TNamed(nom, "Bool_t"), fNumber(val)
123 {
124  SetType(kIsBool);
125 }
126 
127 
128 
131 
133  : TNamed(nom, p.GetTitle()), fNumber(p.fNumber)
134 {
135  // Create parameter with given name "nom", and the type & value of "p"
136  SetType(p.GetType());
137 }
138 
139 
140 
142 
143 void KVNamedParameter::Set(const char* name, Int_t val)
144 {
145  SetNameTitle(name, "Int_t");
146  SetType(kIsInt);
147  fNumber = val;
148 }
149 
150 
151 
153 
154 void KVNamedParameter::Set(const char* name, Bool_t val)
155 {
156  SetNameTitle(name, "Bool_t");
157  SetType(kIsBool);
158  fNumber = val;
159 }
160 
161 
162 
165 
166 void KVNamedParameter::Set(const char* nom, const KVNamedParameter& p)
167 {
168  // Set parameter name "nom" with the type & value of "p"
169  SetNameTitle(nom, p.GetTitle());
170  fNumber = p.fNumber;
171  SetType(p.GetType());
172 }
173 
174 
175 
180 
182 {
183  // Numerical values: Add the numerical value of "p" to this parameter
184  // Strings: add string to comma-separated list of values
185  // If parameters are not same type, print warning and do nothing
186 
187  if (GetType() != p.GetType()) {
188  Warning("Add", "Parameters are not same type: this->name=%s (type=%d) other->name=%s (type=%d)",
189  GetName(), GetType(), p.GetName(), p.GetType());
190  return;
191  }
192  if (IsString()) {
193  TString w = Get<TString>() + ",";
194  w += p.Get<TString>();
195  Set(w);
196  }
197  else
198  fNumber += p.fNumber;
199 }
200 
201 
202 
204 
206 {
207  SetTitle("Int_t");
208  SetType(kIsInt);
209  fNumber = val;
210 }
211 
212 
213 
215 
217 {
218  SetTitle("Bool_t");
219  SetType(kIsBool);
220  fNumber = val;
221 }
222 
223 
224 //void KVNamedParameter::Set(const KVNamedParameter& p)
225 //{
226 // // Set name, type & value of parameter according to type & value of 'p'
227 
228 // if (p.IsString()) Set(p.GetName(), p.GetString());
229 // else if (p.IsInt()) Set(p.GetName(), p.GetInt());
230 // else if (p.IsDouble()) Set(p.GetName(), p.GetDouble());
231 // else if (p.IsBool()) Set(p.GetName(), p.GetBool());
232 // else Warning("Set(const KVNamedParameter&)", "Unknown type of parameter argument");
233 //}
234 
235 
239 
241 {
242  // Look for value in TEnv with same name as this parameter, or prefixed with "p."
243  // If found, set value according to TEnv
244 
245  TString name = p;
246  if (name != "") name.Append(".");
247  name += GetName();
248  if (IsString()) Set(e->GetValue(name, GetString()));
249  else if (IsInt()) Set(e->GetValue(name, GetInt()));
250  else if (IsDouble()) Set(e->GetValue(name, GetDouble()));
251  else if (IsBool()) Set((Bool_t)e->GetValue(name, GetBool()));
252 }
253 
254 
255 
258 
260 {
261  // Removes the name and any assigned value
262  SetNameTitle("", "");
263  fNumber = 0.0;
264  ResetBits();
265 }
266 
267 
268 
273 
275 {
276  // Returns value of parameter as a string, whatever the type
277  // (integer or floating values are converted to a string,
278  // booleans are "true" or "false")
279 
280  if (IsString()) return GetTitle();
281  static TString convert = "";
282  if (IsDouble())
283  convert.Form("%lf", fNumber);
284  else if (IsBool())
285  convert = (GetBool() ? "true" : "false");
286  else
287  convert.Form("%d", (Int_t)fNumber);
288  return convert.Data();
289 }
290 
291 
292 
296 
298 {
299  // Returns value of parameter as a TString, whatever the type
300  // (integer or floating values are converted to a string)
301 
302  if (IsString()) return fTitle;
304  if (IsDouble())
305  convert.Form("%lf", fNumber);
306  else if (IsBool())
307  convert = (GetBool() ? "true" : "false");
308  else
309  convert.Form("%d", (Int_t)fNumber);
310  return convert;
311 }
312 
313 
314 
318 
320 {
321  // returns double if parameter value is of numerical type
322  // if string, conversion to floating point is attempted
323 
324  if (IsString()) {
325  return fTitle.Atof();
326  }
327  return fNumber;
328 }
329 
330 
331 
335 
337 {
338  // returns integer if parameter value is of numerical type
339  // if string, conversion to integer is attempted
340 
341  if (IsString()) {
342  return fTitle.Atoi();
343  }
344  return (Int_t)fNumber;
345 }
346 
347 
348 
352 
354 {
355  // returns boolean if parameter value is of boolean type
356  // if string, conversion to integer is attempted
357 
358  if (IsString()) {
359  return fTitle.Atoi();
360  }
361  return (Bool_t)fNumber;
362 }
363 
364 
365 
369 
371 {
372  // Test for equality between two parameters
373  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
374 
375  if (!obj->InheritsFrom("KVNamedParameter")) return kFALSE;
376  KVNamedParameter* _obj = (KVNamedParameter*)obj;
377  return ((*this) == (*_obj));
378 }
379 
380 
381 
385 
387 {
388  // Test for equality between two parameters
389  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
390 
391  if ((other.fName != fName)) return kFALSE;
392  return HasSameValueAs(other);
393 }
394 
395 
396 
400 
402 {
403  // Returns kTRUF if the two parameters have the same type and the
404  // same value (don't care about parameter names)
405  if (other.GetType() != GetType()) return kFALSE;
406  switch (GetType()) {
407  case kIsString:
408  if (fTitle == other.fTitle) return kTRUE;
409  break;
410 
411  case kIsInt:
412  if (other.GetInt() == GetInt()) return kTRUE;
413  break;
414 
415  case kIsDouble:
416  return KVBase::AreEqual(other.GetDouble(), GetDouble());
417  break;
418 
419  case kIsBool:
420  if (other.GetBool() == GetBool()) return kTRUE;
421  break;
422 
423  default:
424  return kFALSE;
425  }
426  return kFALSE;
427 }
428 
429 
430 
432 
434 {
435  if (IsString()) {
436  Info("Print", "Name = %s type = string value = %s", GetName(), GetTitle());
437  }
438  else
439  Info("Print", "Name = %s type = %s value = %s", GetName(), GetTitle(), GetString());
440 }
441 
442 
443 
452 
453 void KVNamedParameter::ls(Option_t* option) const
454 {
455  // compact listing of parameter name & value, used by KVNameValueList::Print
456  // option controls what is printed:
457  // "" (default) : all parameters
458  // "int" : only integer parameters
459  // "bool" : only boolean parameters
460  // "double" : only double parameters
461  // "string" : only string parameters
462 
463  Bool_t can_print = kTRUE;
464  if (strcmp(option, "")) {
465  TString opt(option);
466  opt.ToLower();
467  if (opt == "int" && !IsInt()) can_print = kFALSE;
468  else if (opt == "bool" && !IsBool()) can_print = kFALSE;
469  else if (opt == "double" && !IsDouble()) can_print = kFALSE;
470  else if (opt == "string" && !IsString()) can_print = kFALSE;
471  }
473  if (IsString()) {
474  if (can_print) cout << "<" << GetName() << "=" << GetTitle() << ">" << endl;
475  }
476  else {
477  switch (GetType()) {
478  case kIsInt:
479  if (can_print) cout << "<" << GetName() << "=" << dec << GetInt() << ">" << endl;
480  break;
481 
482  case kIsBool:
483  if (can_print) cout << "<" << GetName() << "=" << GetString() << ">" << endl;
484  break;
485 
486  case kIsDouble:
487  if (can_print) cout << "<" << GetName() << "=" << GetDouble() << ">" << endl;
488  break;
489 
490  default:
491  break;
492  }
493  }
494 }
495 
496 
497 
500 
502 {
503  // Compares numerical parameters for sorting lists (such as KVNameValueList)
504  if (!IsNumber()) return 0;
505  const KVNamedParameter* other = dynamic_cast<const KVNamedParameter*>(obj);
506  if (!other || !other->IsNumber()) return 0;
507  // check for equality
508  if ((*other) == (*this)) return 0;
509  return ((other->fNumber) > fNumber ? -1 : 1);
510 }
511 
512 
513 
516 
518 {
519  // Write parameter in TEnv, using optional prefix p as "p.[name]"
520 
521  TString name = p;
522  if (name != "") name.Append(".");
523  name += GetName();
524  if (IsInt()) e->SetValue(name, GetInt());
525  else if (IsDouble()) e->SetValue(name, GetDouble());
526  else e->SetValue(name, GetString());
527 }
528 
529 
530 
534 
536 {
537  // Returns type of parameter for use in SQLite database
538  // "INTEGER", "REAL", or "TEXT"
539 
540  static TString sql_type;
541  switch (GetType()) {
542  case kIsString:
543  sql_type = "TEXT";
544  break;
545  case kIsDouble:
546  sql_type = "REAL";
547  break;
548  case kIsInt:
549  case kIsBool:
550  sql_type = "INTEGER";
551  }
552  return sql_type.Data();
553 }
554 
555 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define e(i)
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
const char Option_t
static Bool_t AreEqual(Double_t x, Double_t y, Long64_t maxdif=1)
Comparison between two 64-bit floating-point values.
Definition: KVBase.cpp:1442
A generic named parameter storing values of different types.
Bool_t IsEqual(const TObject *obj) const
Int_t GetType() const
Bool_t HasSameValueAs(const KVNamedParameter &) const
Bool_t IsDouble() const
const Char_t * GetString() const
void SetType(UInt_t f)
KVNamedParameter()
Default constructor.
void Set(const char *, const char *)
Int_t GetInt() const
Bool_t IsInt() const
virtual void Print(Option_t *opt="") const
Bool_t IsBool() const
void Add(const KVNamedParameter &p)
Bool_t operator==(const KVNamedParameter &) const
Bool_t IsString() const
const Char_t * GetSQLType() const
Double_t GetDouble() const
virtual void Clear(Option_t *="")
Removes the name and any assigned value.
virtual void ls(Option_t *opt="") const
Int_t Compare(const TObject *obj) const
Compares numerical parameters for sorting lists (such as KVNameValueList)
void WriteToEnv(TEnv *, const TString &p="")
Write parameter in TEnv, using optional prefix p as "p.[name]".
Double_t fNumber
used to store numerical (integer or floating-point) values
TString GetTString() const
Bool_t IsNumber() const
virtual ~KVNamedParameter()
Destructor.
Bool_t GetBool() const
virtual const char * GetName() const
virtual void SetTitle(const char *title="")
virtual const char * GetTitle() const
TString fTitle
TString fName
virtual void SetNameTitle(const char *name, const char *title)
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual Bool_t InheritsFrom(const char *classname) const
virtual void Info(const char *method, const char *msgfmt,...) const
static void IndentLevel()
void ToLower()
Int_t Atoi() const
Double_t Atof() const
TString & Append(char c, Ssiz_t rep=1)
const char * Data() const
void convert(AxisAngle const &from, EulerAngles &to)