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 
303 
305 {
306  // \returns value of parameter as a TString, whatever the type
307  // (integer or floating values are converted to a string)
308 
309  if (IsString()) return fTitle;
311  if (IsDouble())
312  convert.Form("%lf", fNumber);
313  else if (IsBool())
314  convert = (GetBool() ? "true" : "false");
315  else
316  convert.Form("%d", (Int_t)fNumber);
317  return convert;
318 }
319 
320 
321 
325 
327 {
328  // \returns value of parameter as a C-string, whatever the type
329  // (integer or floating values are converted to a string)
330 
331  if (IsString()) return fTitle.Data();
332 
333  if (IsDouble())
334  return Form("%lf", fNumber);
335  else if (IsBool())
336  return (GetBool() ? "true" : "false");
337 
338  return Form("%d", (Int_t)fNumber);
339 }
340 
341 
342 
346 
348 {
349  // returns double if parameter value is of numerical type
350  // if string, conversion to floating point is attempted
351 
352  if (IsString()) {
353  return fTitle.Atof();
354  }
355  return fNumber;
356 }
357 
358 
359 
363 
365 {
366  // returns integer if parameter value is of numerical type
367  // if string, conversion to integer is attempted
368 
369  if (IsString()) {
370  return fTitle.Atoi();
371  }
372  return (Int_t)fNumber;
373 }
374 
375 
376 
380 
382 {
383  // returns boolean if parameter value is of boolean type
384  // if string, conversion to integer is attempted
385 
386  if (IsString()) {
387  return fTitle.Atoi();
388  }
389  return (Bool_t)fNumber;
390 }
391 
392 
393 
397 
399 {
400  // Test for equality between two parameters
401  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
402 
403  if (!obj->InheritsFrom("KVNamedParameter")) return kFALSE;
404  KVNamedParameter* _obj = (KVNamedParameter*)obj;
405  return ((*this) == (*_obj));
406 }
407 
408 
409 
413 
415 {
416  // Test for equality between two parameters
417  // Returns kTRUE if both the name, the type, and the value of the parameters are identical
418 
419  if ((other.fName != fName)) return kFALSE;
420  return HasSameValueAs(other);
421 }
422 
423 
424 
428 
430 {
431  // Returns kTRUF if the two parameters have the same type and the
432  // same value (don't care about parameter names)
433  if (other.GetType() != GetType()) return kFALSE;
434  switch (GetType()) {
435  case kIsString:
436  if (fTitle == other.fTitle) return kTRUE;
437  break;
438 
439  case kIsInt:
440  if (other.GetInt() == GetInt()) return kTRUE;
441  break;
442 
443  case kIsDouble:
444  return KVBase::AreEqual(other.GetDouble(), GetDouble());
445  break;
446 
447  case kIsBool:
448  if (other.GetBool() == GetBool()) return kTRUE;
449  break;
450 
451  default:
452  return kFALSE;
453  }
454  return kFALSE;
455 }
456 
457 
458 
460 
462 {
463  if (IsString()) {
464  Info("Print", "Name = %s type = string value = %s", GetName(), GetTitle());
465  }
466  else
467  Info("Print", "Name = %s type = %s value = %s", GetName(), GetTitle(), GetString());
468 }
469 
470 
471 
480 
481 void KVNamedParameter::ls(Option_t* option) const
482 {
483  // compact listing of parameter name & value, used by KVNameValueList::Print
484  // option controls what is printed:
485  // "" (default) : all parameters
486  // "int" : only integer parameters
487  // "bool" : only boolean parameters
488  // "double" : only double parameters
489  // "string" : only string parameters
490 
491  Bool_t can_print = kTRUE;
492  if (strcmp(option, "")) {
493  TString opt(option);
494  opt.ToLower();
495  if (opt == "int" && !IsInt()) can_print = kFALSE;
496  else if (opt == "bool" && !IsBool()) can_print = kFALSE;
497  else if (opt == "double" && !IsDouble()) can_print = kFALSE;
498  else if (opt == "string" && !IsString()) can_print = kFALSE;
499  }
501  if (IsString()) {
502  if (can_print) cout << "<" << GetName() << "=" << GetTitle() << ">" << endl;
503  }
504  else {
505  switch (GetType()) {
506  case kIsInt:
507  if (can_print) cout << "<" << GetName() << "=" << dec << GetInt() << ">" << endl;
508  break;
509 
510  case kIsBool:
511  if (can_print) cout << "<" << GetName() << "=" << GetString() << ">" << endl;
512  break;
513 
514  case kIsDouble:
515  if (can_print) cout << "<" << GetName() << "=" << GetDouble() << ">" << endl;
516  break;
517 
518  default:
519  break;
520  }
521  }
522 }
523 
524 
525 
534 
536 {
537  // compact listing of parameter name & value, used by KVNameValueList::Print
538  // option controls what is printed:
539  // "" (default) : all parameters
540  // "int" : only integer parameters
541  // "bool" : only boolean parameters
542  // "double" : only double parameters
543  // "string" : only string parameters
544 
545  Bool_t can_print = kTRUE;
546  if (strcmp(option, "")) {
547  TString opt(option);
548  opt.ToLower();
549  if (opt == "int" && !IsInt()) can_print = kFALSE;
550  else if (opt == "bool" && !IsBool()) can_print = kFALSE;
551  else if (opt == "double" && !IsDouble()) can_print = kFALSE;
552  else if (opt == "string" && !IsString()) can_print = kFALSE;
553  }
554  if (IsString()) {
555  if (can_print) return Form("%s=%s", GetName(), GetTitle());
556  }
557  else {
558  switch (GetType()) {
559  case kIsInt:
560  if (can_print) return Form("%s=%d", GetName(), GetInt());
561  break;
562 
563  case kIsBool:
564  if (can_print) return Form("%s=%s", GetName(), GetString());
565  break;
566 
567  case kIsDouble:
568  if (can_print) return Form("%s=%f", GetName(), GetDouble());
569  break;
570 
571  default:
572  break;
573  }
574  }
575  return "";
576 }
577 
578 
579 
582 
584 {
585  // Compares numerical parameters for sorting lists (such as KVNameValueList)
586  if (!IsNumber()) return 0;
587  const KVNamedParameter* other = dynamic_cast<const KVNamedParameter*>(obj);
588  if (!other || !other->IsNumber()) return 0;
589  // check for equality
590  if ((*other) == (*this)) return 0;
591  return ((other->fNumber) > fNumber ? -1 : 1);
592 }
593 
594 
595 
598 
600 {
601  // Write parameter in TEnv, using optional prefix p as "p.[name]"
602 
603  TString name = p;
604  if (name != "") name.Append(".");
605  name += GetName();
606  if (IsInt()) e->SetValue(name, GetInt());
607  else if (IsDouble()) e->SetValue(name, GetDouble());
608  else e->SetValue(name, GetString());
609 }
610 
611 
612 
615 
617 {
618  // \returns type of parameter for use in SQLite database: "INTEGER", "REAL", or "TEXT"
619 
620  switch (GetType()) {
621  case kIsString:
622  return "TEXT";
623 
624  case kIsDouble:
625  return "REAL";
626 
627  case kIsInt:
628  case kIsBool:
629  return "INTEGER";
630  }
631  return "";
632 }
633 
634 
635 
640 
642 {
643  // \return selection string which can be used with SQLite database 'NAME = VALUE'
644  //
645  // if parameter is type "TEXT" we enclose the value in single quotes
646 
647  switch (GetType()) {
648  case kIsString:
649  return Form("%s = '%s'", GetName(), GetTitle());
650  break;
651 
652  case kIsInt:
653  case kIsBool:
654  return Form("%s = %d", GetName(), GetInt());
655  break;
656 
657  case kIsDouble:
658  return Form("%s = %f", GetName(), GetDouble());
659  break;
660  }
661  return "";
662 }
663 
664 
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]
char * Form(const char *fmt,...)
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:1434
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
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
TString List(Option_t *opt="") const
Bool_t IsNumber() const
TString GetSQLType() const
TString AsSQLSelection() 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)