ROOT  6.06/09
Reference Guide
TParameter.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Maarten Ballintijn 21/06/2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TParameter
13 #define ROOT_TParameter
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TParameter<AParamType> //
19 // //
20 // Named parameter, streamable and storable. //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #ifndef ROOT_Riostream
25 #include "Riostream.h"
26 #endif
27 
28 #ifndef ROOT_TClass
29 #include "TClass.h"
30 #endif
31 
32 #ifndef ROOT_TObject
33 #include "TObject.h"
34 #endif
35 
36 #ifndef ROOT_TCollection
37 #include "TCollection.h"
38 #endif
39 
40 #ifndef ROOT_TString
41 #include "TString.h"
42 #endif
43 
44 #ifndef ROOT_TROOT
45 #include "TROOT.h"
46 #endif
47 
48 template <class AParamType>
49 class TParameter : public TObject {
50 
51 public:
52  // Defines options / status while merging:
53  enum EStatusBits { kMultiply = BIT(16), // Use multiplication
54  kMax = BIT(17), // Take max value
55  kMin = BIT(18), // Take min value
56  kFirst = BIT(19), // Take the first value
57  kLast = BIT(20), // Take the last value
58  kIsConst = BIT(21) // Set if all values are equal
59  };
60 
61 private:
63  AParamType fVal;
64 
67 
68 public:
69  TParameter(): fVal() { Reset(); SetBit(kIsConst); }
70  TParameter(const char *name, const AParamType &val)
71  : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
72  TParameter(const char *name, const AParamType &val, char mergemode)
73  : fName(name), fVal(val) { SetMergeMode(mergemode); SetBit(kIsConst);}
75 
76  const char *GetName() const { return fName; }
77  const AParamType &GetVal() const { return fVal; }
78  Bool_t IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
79  void SetVal(const AParamType &val) { fVal = val; }
80 
81  // Merging modes:
82  // '+' addition ('OR' for booleans) [default]
83  // '*' multiplication ('AND' for booleans)
84  // 'M' maximum ('OR' for booleans)
85  // 'm' minimum ('AND' for booleans)
86  // 'f' first value
87  // 'l' last value
88  void SetMergeMode(char mergemode = '+') {
89  Reset();
90  if (mergemode == '*') {
92  } else if (mergemode == 'M') {
93  SetBit(kMax);
94  } else if (mergemode == 'm') {
95  SetBit(kMin);
96  } else if (mergemode == 'f') {
97  SetBit(kFirst);
98  } else if (mergemode == 'l') {
99  SetBit(kLast);
100  }
101  }
102  virtual ULong_t Hash() const { return fName.Hash(); }
103  virtual Bool_t IsSortable() const { return kTRUE; }
104  virtual Int_t Compare(const TObject *obj) const {
105  // Compare two TParameter objects. Returns 0 when equal, -1 when this is
106  // smaller and +1 when bigger (like strcmp).
107 
108  if (this == obj) return 0;
109  return fName.CompareTo(obj->GetName());
110  }
111 
112  virtual void ls(Option_t *) const {
113  // Print this parameter content
115  std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
116  }
117 
118  virtual void Print(Option_t *) const {
119  // Print this parameter content
121  std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
122  }
123 
124  virtual Int_t Merge(TCollection *in);
125 
126  ClassDef(TParameter,2) //Named templated parameter type
127 };
128 
129 template <class AParamType>
131  // Merge objects in the list.
132  // Returns the number of objects that were in the list.
133  TIter nxo(in);
134  Int_t n = 0;
135  while (TObject *o = nxo()) {
136  TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
137  if (c) {
138  // Check if constant
139  if (fVal != c->GetVal()) ResetBit(kIsConst);
140  if (TestBit(kMultiply)) {
141  // Multiply
142  fVal *= c->GetVal();
143  } else if (TestBit(kMax)) {
144  // Take max
145  if (c->GetVal() > fVal) fVal = c->GetVal();
146  } else if (TestBit(kMin)) {
147  // Take min
148  if (c->GetVal() < fVal) fVal = c->GetVal();
149  } else if (TestBit(kLast)) {
150  // Take the last
151  fVal = c->GetVal();
152  } else if (!TestBit(kFirst)) {
153  // Add, if not asked to take the first
154  fVal += c->GetVal();
155  }
156  n++;
157  }
158  }
159 
160  return n;
161 }
162 
163 // Specialization of Merge for Bool_t
164 template <>
166 {
167  // Merge bool objects in the list.
168  // Returns the number of objects that were in the list.
169  TIter nxo(in);
170  Int_t n = 0;
171  while (TObject *o = nxo()) {
172  TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
173  if (c) {
174  // Check if constant
175  if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
176  if (TestBit(TParameter::kMultiply) || TestBit(kMin)) {
177  // And
178  fVal &= (Bool_t) c->GetVal();
179  } else if (TestBit(kLast)) {
180  // Take the last
181  fVal = (Bool_t) c->GetVal();
182  } else if (!TestBit(kFirst) || TestBit(kMax)) {
183  // Or
184  fVal |= (Bool_t) c->GetVal();
185  }
186  n++;
187  }
188  }
189 
190  return n;
191 }
192 
193 #endif
virtual void ls(Option_t *) const
The ls function lists the contents of a class on stdout.
Definition: TParameter.h:112
const char Option_t
Definition: RtypesCore.h:62
#define BIT(n)
Definition: Rtypes.h:120
TParameter(const char *name, const AParamType &val, char mergemode)
Definition: TParameter.h:72
void SetVal(const AParamType &val)
Definition: TParameter.h:79
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual Int_t Merge(TCollection *in)
Definition: TParameter.h:130
Bool_t IsConst() const
Definition: TParameter.h:78
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TParameter.h:102
#define ClassDef(name, id)
Definition: Rtypes.h:254
TParameter(const char *name, const AParamType &val)
Definition: TParameter.h:70
void SetMergeMode(char mergemode= '+')
Definition: TParameter.h:88
Named parameter, streamable and storable.
Definition: TParameter.h:49
virtual Bool_t IsSortable() const
Definition: TParameter.h:103
const char * GetName() const
Returns name of object.
Definition: TParameter.h:76
Collection abstract base class.
Definition: TCollection.h:48
TClass * IsA() const
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void Print(Option_t *) const
This method must be overridden when a class wants to print itself.
Definition: TParameter.h:118
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
EStatusBits
Definition: TObject.h:76
unsigned long ULong_t
Definition: RtypesCore.h:51
TString fName
Definition: TParameter.h:62
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TParameter.h:104
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
void Reset()
Definition: TParameter.h:65
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2461
void ResetBit(UInt_t f)
Definition: TObject.h:172
const AParamType & GetVal() const
Definition: TParameter.h:77
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
const Int_t n
Definition: legend1.C:16
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:385
AParamType fVal
Definition: TParameter.h:63
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:605