KaliVeda
Toolkit for HIC analysis
KVNamedParameter.cpp
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 
79 KVNamedParameter::KVNamedParameter(const char* nom, const std::string& val)
80  : TNamed(nom, val), fNumber(0.0)
81 {
83 }
84 
85 
86 
88 
89 void KVNamedParameter::Set(const char* name, const std::string& val)
90 {
91  SetNameTitle(name, val.c_str());
93  fNumber = 0.0;
94 }
95 
96 
97 
99 
100 void KVNamedParameter::Set(const std::string& val)
101 {
102  SetTitle(val.c_str());
104  fNumber = 0.0;
105 }
106 
107 
108 
110 
112  : TNamed(nom, "Double_t"), fNumber(val)
113 {
115 }
116 
117 
118 
120 
121 void KVNamedParameter::Set(const char* name, Double_t val)
122 {
123  SetNameTitle(name, "Double_t");
125  fNumber = val;
126 }
127 
128 
129 
131 
133 {
134  SetTitle("Double_t");
136  fNumber = val;
137 }
138 
139 
140 
142 
144  : TNamed(nom, "Int_t"), fNumber(val)
145 {
146  SetType(kIsInt);
147 }
148 
149 
150 
152 
154  : TNamed(nom, "Bool_t"), fNumber(val)
155 {
156  SetType(kIsBool);
157 }
158 
159 
160 
163 
165  : TNamed(nom, p.GetTitle()), fNumber(p.fNumber)
166 {
167  // Create parameter with given name "nom", and the type & value of "p"
168  SetType(p.GetType());
169 }
170 
171 
172 
174 
175 void KVNamedParameter::Set(const char* name, Int_t val)
176 {
177  SetNameTitle(name, "Int_t");
178  SetType(kIsInt);
179  fNumber = val;
180 }
181 
182 
183 
185 
186 void KVNamedParameter::Set(const char* name, Bool_t val)
187 {
188  SetNameTitle(name, "Bool_t");
189  SetType(kIsBool);
190  fNumber = val;
191 }
192 
193 
194 
197 
198 void KVNamedParameter::Set(const char* nom, const KVNamedParameter& p)
199 {
200  // Set parameter name "nom" with the type & value of "p"
201  SetNameTitle(nom, p.GetTitle());
202  fNumber = p.fNumber;
203  SetType(p.GetType());
204 }
205 
206 
207 
212 
214 {
215  // Numerical values: Add the numerical value of "p" to this parameter
216  // Strings: add string to comma-separated list of values
217  // If parameters are not same type, print warning and do nothing
218  if (GetType() != p.GetType()) {
219  Warning("Add", "Parameters are not same type: this->name=%s (type=%d) other->name=%s (type=%d)",
220  GetName(), GetType(), p.GetName(), p.GetType());
221  return;
222  }
223  if (IsString()) {
224  TString w = Get<TString>() + ",";
225  w += p.Get<TString>();
226  Set(w);
227  }
228  else
229  fNumber += p.fNumber;
230 }
231 
232 
233 
235 
237 {
238  SetTitle("Int_t");
239  SetType(kIsInt);
240  fNumber = val;
241 }
242 
243 
244 
246 
248 {
249  SetTitle("Bool_t");
250  SetType(kIsBool);
251  fNumber = val;
252 }
253 
254 
255 //void KVNamedParameter::Set(const KVNamedParameter& p)
256 //{
257 // // Set name, type & value of parameter according to type & value of 'p'
258 
259 // if (p.IsString()) Set(p.GetName(), p.GetString());
260 // else if (p.IsInt()) Set(p.GetName(), p.GetInt());
261 // else if (p.IsDouble()) Set(p.GetName(), p.GetDouble());
262 // else if (p.IsBool()) Set(p.GetName(), p.GetBool());
263 // else Warning("Set(const KVNamedParameter&)", "Unknown type of parameter argument");
264 //}
265 
266 
270 
272 {
273  // Look for value in TEnv with same name as this parameter, or prefixed with "p."
274  // If found, set value according to TEnv
275 
276  TString name = p;
277  if (name != "") name.Append(".");
278  name += GetName();
279  if (IsString()) Set(e->GetValue(name, GetString()));
280  else if (IsInt()) Set(e->GetValue(name, GetInt()));
281  else if (IsDouble()) Set(e->GetValue(name, GetDouble()));
282  else if (IsBool()) Set((Bool_t)e->GetValue(name, GetBool()));
283 }
284 
285 
286 
289 
291 {
292  // Removes the name and any assigned value
293  SetNameTitle("", "");
294  fNumber = 0.0;
295  ResetBits();
296 }
297 
298 
299 
304 
306 {
307  // Returns value of parameter as a string, whatever the type
308  // (integer or floating values are converted to a string,
309  // booleans are "true" or "false")
310 
311  if (IsString()) return GetTitle();
312  static TString convert = "";
313  if (IsDouble())
314  convert.Form("%lf", fNumber);
315  else if (IsBool())
316  convert = (GetBool() ? "true" : "false");
317  else
318  convert.Form("%d", (Int_t)fNumber);
319  return convert.Data();
320 }
321 
322 
323 
327 
329 {
330  // Returns value of parameter as a TString, whatever the type
331  // (integer or floating values are converted to a string)
332 
333  if (IsString()) return fTitle;
335  if (IsDouble())
336  convert.Form("%lf", fNumber);
337  else if (IsBool())
338  convert = (GetBool() ? "true" : "false");
339  else
340  convert.Form("%d", (Int_t)fNumber);
341  return convert;
342 }
343 
344 
345 
349 
351 {
352  // returns double if parameter value is of numerical type
353  // if string, conversion to floating point is attempted
354 
355  if (IsString()) {
356  return fTitle.Atof();
357  }
358  return fNumber;
359 }
360 
361 
362 
366 
368 {
369  // returns integer if parameter value is of numerical type
370  // if string, conversion to integer is attempted
371 
372  if (IsString()) {
373  return fTitle.Atoi();
374  }
375  return (Int_t)fNumber;
376 }
377 
378 
379 
383 
385 {
386  // returns boolean if parameter value is of boolean type
387  // if string, conversion to integer is attempted
388 
389  if (IsString()) {
390  return fTitle.Atoi();
391  }
392  return (Bool_t)fNumber;
393 }
394 
395 
396 
400 
402 {
403  // Test for equality between two parameters
404  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
405 
406  if (!obj->InheritsFrom("KVNamedParameter")) return kFALSE;
407  KVNamedParameter* _obj = (KVNamedParameter*)obj;
408  return ((*this) == (*_obj));
409 }
410 
411 
412 
416 
418 {
419  // Test for equality between two parameters
420  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
421 
422  if ((other.fName != fName)) return kFALSE;
423  return HasSameValueAs(other);
424 }
425 
426 
427 
431 
433 {
434  // Returns kTRUF if the two parameters have the same type and the
435  // same value (don't care about parameter names)
436  if (other.GetType() != GetType()) return kFALSE;
437  switch (GetType()) {
438  case kIsString:
439  if (fTitle == other.fTitle) return kTRUE;
440  break;
441 
442  case kIsInt:
443  if (other.GetInt() == GetInt()) return kTRUE;
444  break;
445 
446  case kIsDouble:
447  return KVBase::AreEqual(other.GetDouble(), GetDouble());
448  break;
449 
450  case kIsBool:
451  if (other.GetBool() == GetBool()) return kTRUE;
452  break;
453 
454  default:
455  return kFALSE;
456  }
457  return kFALSE;
458 }
459 
460 
461 
463 
465 {
466  if (IsString()) {
467  Info("Print", "Name = %s type = string value = %s", GetName(), GetTitle());
468  }
469  else
470  Info("Print", "Name = %s type = %s value = %s", GetName(), GetTitle(), GetString());
471 }
472 
473 
474 
483 
484 void KVNamedParameter::ls(Option_t* option) const
485 {
486  // compact listing of parameter name & value, used by KVNameValueList::Print
487  // option controls what is printed:
488  // "" (default) : all parameters
489  // "int" : only integer parameters
490  // "bool" : only boolean parameters
491  // "double" : only double parameters
492  // "string" : only string parameters
493 
494  Bool_t can_print = kTRUE;
495  if (strcmp(option, "")) {
496  TString opt(option);
497  opt.ToLower();
498  if (opt == "int" && !IsInt()) can_print = kFALSE;
499  else if (opt == "bool" && !IsBool()) can_print = kFALSE;
500  else if (opt == "double" && !IsDouble()) can_print = kFALSE;
501  else if (opt == "string" && !IsString()) can_print = kFALSE;
502  }
504  if (IsString()) {
505  if (can_print) cout << "<" << GetName() << "=" << GetTitle() << ">" << endl;
506  }
507  else {
508  switch (GetType()) {
509  case kIsInt:
510  if (can_print) cout << "<" << GetName() << "=" << dec << GetInt() << ">" << endl;
511  break;
512 
513  case kIsBool:
514  if (can_print) cout << "<" << GetName() << "=" << GetString() << ">" << endl;
515  break;
516 
517  case kIsDouble:
518  if (can_print) cout << "<" << GetName() << "=" << GetDouble() << ">" << endl;
519  break;
520 
521  default:
522  break;
523  }
524  }
525 }
526 
527 
528 
531 
533 {
534  // Compares numerical parameters for sorting lists (such as KVNameValueList)
535  if (!IsNumber()) return 0;
536  const KVNamedParameter* other = dynamic_cast<const KVNamedParameter*>(obj);
537  if (!other || !other->IsNumber()) return 0;
538  // check for equality
539  if ((*other) == (*this)) return 0;
540  return ((other->fNumber) > fNumber ? -1 : 1);
541 }
542 
543 
544 
547 
549 {
550  // Write parameter in TEnv, using optional prefix p as "p.[name]"
551 
552  TString name = p;
553  if (name != "") name.Append(".");
554  name += GetName();
555  if (IsInt()) e->SetValue(name, GetInt());
556  else if (IsDouble()) e->SetValue(name, GetDouble());
557  else e->SetValue(name, GetString());
558 }
559 
560 
561 
565 
567 {
568  // Returns type of parameter for use in SQLite database
569  // "INTEGER", "REAL", or "TEXT"
570 
571  static TString sql_type;
572  switch (GetType()) {
573  case kIsString:
574  sql_type = "TEXT";
575  break;
576  case kIsDouble:
577  sql_type = "REAL";
578  break;
579  case kIsInt:
580  case kIsBool:
581  sql_type = "INTEGER";
582  }
583  return sql_type.Data();
584 }
585 
586 
int Int_t
#define e(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
winID w
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
char name[80]
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:1410
A generic named parameter storing values of different types.
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
Bool_t IsEqual(const TObject *obj) const override
void Print(Option_t *opt="") const override
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
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
Int_t Compare(const TObject *obj) const override
Compares numerical parameters for sorting lists (such as KVNameValueList)
virtual ~KVNamedParameter()
Destructor.
Bool_t GetBool() const
void Clear(Option_t *="") override
Removes the name and any assigned value.
void ls(Option_t *opt="") const override
virtual void SetTitle(const char *title="")
const char * GetName() const override
const char * GetTitle() const override
TString fTitle
TString fName
virtual void SetNameTitle(const char *name, const char *title)
virtual const char * GetName() const
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual Bool_t InheritsFrom(const char *classname) const
virtual const char * GetTitle() const
virtual void Info(const char *method, const char *msgfmt,...) const
static void IndentLevel()
void ToLower()
Int_t Atoi() const
Double_t Atof() const
const char * Data() const
void convert(AxisAngle const &from, EulerAngles &to)
ClassImp(TPyArg)