Logo ROOT  
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#include "Riostream.h"
25
26#include "TObject.h"
27
28#include "TCollection.h"
29
30#include "TString.h"
31
32#include "TROOT.h"
33
34template <class AParamType>
35class TParameter : public TObject {
36
37public:
38 // Defines options / status while merging:
39 enum EStatusBits { kMultiply = BIT(16), // Use multiplication
40 kMax = BIT(17), // Take max value
41 kMin = BIT(18), // Take min value
42 kFirst = BIT(19), // Take the first value
43 kLast = BIT(20), // Take the last value
44 kIsConst = BIT(21) // Set if all values are equal
45 };
46
47private:
49 AParamType fVal;
50
53
54public:
56 TParameter(const char *name, const AParamType &val)
57 : fName(name), fVal(val) { Reset(); SetBit(kIsConst);}
58 TParameter(const char *name, const AParamType &val, char mergemode)
59 : fName(name), fVal(val) { SetMergeMode(mergemode); SetBit(kIsConst);}
60 virtual ~TParameter()
61 {
62 // Required since we overload TObject::Hash.
64 }
65
66 const char *GetName() const override { return fName; }
67 const AParamType &GetVal() const { return fVal; }
68 Bool_t IsConst() const { return (TestBit(kIsConst) ? kTRUE : kFALSE); }
69 void SetVal(const AParamType &val) { fVal = val; }
70
71 // Merging modes:
72 // '+' addition ('OR' for booleans) [default]
73 // '*' multiplication ('AND' for booleans)
74 // 'M' maximum ('OR' for booleans)
75 // 'm' minimum ('AND' for booleans)
76 // 'f' first value
77 // 'l' last value
78 void SetMergeMode(char mergemode = '+') {
79 Reset();
80 if (mergemode == '*') {
82 } else if (mergemode == 'M') {
83 SetBit(kMax);
84 } else if (mergemode == 'm') {
85 SetBit(kMin);
86 } else if (mergemode == 'f') {
88 } else if (mergemode == 'l') {
90 }
91 }
92 ULong_t Hash() const override { return fName.Hash(); }
93 Bool_t IsSortable() const override { return kTRUE; }
94 Int_t Compare(const TObject *obj) const override
95 {
96 // Compare two TParameter objects. Returns 0 when equal, -1 when this is
97 // smaller and +1 when bigger (like strcmp).
98
99 if (this == obj) return 0;
100 return fName.CompareTo(obj->GetName());
101 }
102
103 void ls(Option_t *) const override
104 {
105 // Print this parameter content
107 std::cout << "OBJ: " << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
108 }
109
110 void Print(Option_t *) const override
111 {
112 // Print this parameter content
114 std::cout << IsA()->GetName() << "\t" << fName << " = " << fVal << std::endl;
115 }
116
117 virtual Int_t Merge(TCollection *in);
118
119 ClassDefOverride(TParameter,2) //Named templated parameter type
120};
121
122template <class AParamType>
124 // Merge objects in the list.
125 // Returns the number of objects that were in the list.
126 TIter nxo(in);
127 Int_t n = 0;
128 while (TObject *o = nxo()) {
129 TParameter<AParamType> *c = dynamic_cast<TParameter<AParamType> *>(o);
130 if (c) {
131 // Check if constant
132 if (fVal != c->GetVal()) ResetBit(kIsConst);
133 if (TestBit(kMultiply)) {
134 // Multiply
135 fVal *= c->GetVal();
136 } else if (TestBit(kMax)) {
137 // Take max
138 if (c->GetVal() > fVal) fVal = c->GetVal();
139 } else if (TestBit(kMin)) {
140 // Take min
141 if (c->GetVal() < fVal) fVal = c->GetVal();
142 } else if (TestBit(kLast)) {
143 // Take the last
144 fVal = c->GetVal();
145 } else if (!TestBit(kFirst)) {
146 // Add, if not asked to take the first
147 fVal += c->GetVal();
148 }
149 n++;
150 }
151 }
152
153 return n;
154}
155
156// Specialization of Merge for Bool_t
157template <>
159{
160 // Merge bool objects in the list.
161 // Returns the number of objects that were in the list.
162 TIter nxo(in);
163 Int_t n = 0;
164 while (TObject *o = nxo()) {
165 TParameter<Bool_t> *c = dynamic_cast<TParameter<Bool_t> *>(o);
166 if (c) {
167 // Check if constant
168 if (fVal != (Bool_t) c->GetVal()) ResetBit(kIsConst);
169 if (TestBit(TParameter::kMultiply) || TestBit(kMin)) {
170 // And
171 fVal &= (Bool_t) c->GetVal();
172 } else if (TestBit(kLast)) {
173 // Take the last
174 fVal = (Bool_t) c->GetVal();
175 } else if (!TestBit(kFirst) || TestBit(kMax)) {
176 // Or
177 fVal |= (Bool_t) c->GetVal();
178 }
179 n++;
180 }
181 }
182
183 return n;
184}
185
186// FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
187// When building with -fmodules, it instantiates all pending instantiations,
188// instead of delaying them until the end of the translation unit.
189// We 'got away with' probably because the use and the definition of the
190// explicit specialization do not occur in the same TU.
191//
192// In case we are building with -fmodules, we need to forward declare the
193// specialization in order to compile the dictionary G__Core.cxx.
194template <> void TParameter<Long64_t>::Streamer(TBuffer &R__b);
196
197#endif
#define c(i)
Definition: RSha256.hxx:101
bool Bool_t
Definition: RtypesCore.h:63
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
unsigned long ULong_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:66
#define BIT(n)
Definition: Rtypes.h:85
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
@ kMultiply
Definition: TAttMarker.h:49
char name[80]
Definition: TGX11.cxx:110
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:81
Collection abstract base class.
Definition: TCollection.h:65
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:433
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:201
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:768
void ResetBit(UInt_t f)
Definition: TObject.h:200
EStatusBits
Definition: TObject.h:61
Named parameter, streamable and storable.
Definition: TParameter.h:35
static TClass * Class()
virtual ~TParameter()
Definition: TParameter.h:60
void Streamer(TBuffer &) override
Stream an object of class TObject.
ULong_t Hash() const override
Return hash value for this object.
Definition: TParameter.h:92
TParameter(const char *name, const AParamType &val, char mergemode)
Definition: TParameter.h:58
const char * GetName() const override
Returns name of object.
Definition: TParameter.h:66
virtual Int_t Merge(TCollection *in)
Definition: TParameter.h:123
TClass * IsA() const override
Definition: TParameter.h:119
void ls(Option_t *) const override
The ls function lists the contents of a class on stdout.
Definition: TParameter.h:103
Bool_t IsConst() const
Definition: TParameter.h:68
void Print(Option_t *) const override
This method must be overridden when a class wants to print itself.
Definition: TParameter.h:110
AParamType fVal
Definition: TParameter.h:49
void SetMergeMode(char mergemode='+')
Definition: TParameter.h:78
TString fName
Definition: TParameter.h:48
void SetVal(const AParamType &val)
Definition: TParameter.h:69
const AParamType & GetVal() const
Definition: TParameter.h:67
Int_t Compare(const TObject *obj) const override
Compare abstract method.
Definition: TParameter.h:94
void Reset()
Definition: TParameter.h:51
TParameter(const char *name, const AParamType &val)
Definition: TParameter.h:56
Bool_t IsSortable() const override
Definition: TParameter.h:93
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2813
Basic string class.
Definition: TString.h:136
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:447
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:667
const Int_t n
Definition: legend1.C:16
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition: TROOT.h:393