3 #ifndef AliHLTSCALARS_H
4 #define AliHLTSCALARS_H
5 /* This file is property of and copyright by the ALICE HLT Project *
6 * ALICE Experiment at CERN, All rights reserved. *
7 * See cxx source for full Copyright notice */
9 /// @file AliHLTScalars.h
10 /// @author Artur Szostak <artursz@iafrica.com>
12 /// @brief Declares the a base class for named scalar values.
16 #include "TTimeStamp.h"
17 #include "TClonesArray.h"
18 #include "THashTable.h"
21 * @class AliHLTScalars
22 * @brief Container for named scalar values.
24 * This class contains a list of named scalars for an event as summary information.
25 * These can be used by the trigger components to perform event selection or used
26 * for monitoring purposes.
28 * \ingroup alihlt_base
30 class AliHLTScalars : public TObject
34 * This class stores a single scalar value and name.
36 class AliScalar : public TNamed
39 /// Default constructor
40 AliScalar() : TNamed(), fValue(0) {}
42 /// Constructor to set the initial value.
43 AliScalar(const char* name, const char* description, Double_t value) :
44 TNamed(name, description), fValue(value)
47 /// Default destructor
48 virtual ~AliScalar() {}
50 /// Inherited from TObject. Compares two scalar names.
51 virtual Int_t Compare(const TObject *obj) const
53 return fName.CompareTo(obj->GetName());
56 /// Inherited from TObject. Returns true.
57 virtual Bool_t IsSortable() const { return kTRUE; }
60 * Inherited from TObject.
61 * Returns true if the names of the scalars are the same.
63 virtual Bool_t IsEqual(const TObject *obj) const
65 return fName == obj->GetName();
68 /// Resets the scalar value to zero.
69 virtual void Clear(Option_t* /*option*/ = "") { fValue = 0; }
71 /// Inherited from TObject. Performs a deep copy.
72 virtual void Copy(TObject& object) const;
74 /// Returns the value of the scalar.
75 Double_t Value() const { return fValue; }
77 /// Sets a new value for the scalar.
78 void Value(Double_t value) { fValue = value; }
81 * Increments the scalar by a value of 'count'.
82 * \param count The number to increment the scalar by. The default is 1.
84 void Increment(UInt_t count = 1) { fValue += count; }
86 /// Returns the name of the scalar.
87 const char* Name() const { return fName.Data(); }
89 /// Returns the description string for the scalar.
90 const char* Description() const { return fTitle.Data(); }
92 /// Checks if two scalar objects are identical.
93 bool operator == (const AliScalar& x) const
95 return fValue == x.fValue and fName == x.fName and fTitle == x.fTitle;
98 /// Checks if two scalar objects are not identical.
99 bool operator != (const AliScalar& x) const
101 return not (this->operator == (x));
104 /// Typecast operator for returning the value directly.
105 operator Double_t () { return fValue; }
108 Double_t fValue; // The scalar's value.
110 ClassDef(AliScalar, 1); // HLT scalar value.
113 /// Default constructor.
116 /// The copy constructor performs a deep copy.
117 AliHLTScalars(const AliHLTScalars& obj);
119 /// Default destructor.
120 virtual ~AliHLTScalars();
123 * Adds a new scalar to the end of the scalars list.
124 * If the scalar already exists then its values are updated instead.
125 * \param name The name of the scalar.
126 * \param description A short description of the scalar.
127 * \param value The value of the new scalar.
128 * \returns true if the scalar already exists and false otherwise.
130 virtual bool Add(const char* name, const char* description = NULL, Double_t value = 0);
133 * Removes a named scalar from the scalars list.
134 * \param name The name of the scalar to remove.
135 * \returns true if the scalar existed and false otherwise.
136 * \note The scalars list is compressed so this method will be slow.
137 * In addition, scalar positions will change if not removing from the end.
139 virtual bool Remove(const char* name);
141 /// Checks to see if the named scalar exists.
142 bool Exists(const char* name) const { return fMap.FindObject(name) != NULL; }
145 * Fetches the specified scalar object.
146 * \param name The name of the scalar object.
147 * \returns the found scalar object, otherwise an empty sentinel object with
148 * zeros. One can tell it is a sentinel because the name will be empty.
150 const AliScalar& GetScalar(const char* name) const;
153 * Fetches the specified scalar object for editing.
154 * \param name The name of the scalar object.
155 * \returns the found scalar object. If the scalar does not already
156 * exist then a new one is created and returned.
158 AliScalar& GetScalar(const char* name);
160 /// Returns the number of scalar values.
161 UInt_t NumberOfScalars() const { return UInt_t(fScalars.GetEntriesFast()); }
163 // Note: the following GetScalarN methods do not use the same name as
164 // GetScalar above because the parameter type would unfortunately be
165 // ambiguous to an ISO c++ compiler.
168 * Fetches the n'th scalar object.
169 * \param n The number of the scalar object.
170 * \returns the found scalar object, otherwise an empty sentinel object with
171 * zeros. One can tell it is a sentinel because the name will be empty.
173 const AliScalar& GetScalarN(UInt_t n) const;
176 * Fetches the n'th scalar object for editing.
177 * \param n The number of the scalar object.
178 * \returns the found scalar object. If the scalar does not already
179 * exist then a new one is created and returned.
181 AliScalar& GetScalarN(UInt_t n);
183 /// Resets all scalar values to zero.
184 virtual void Reset();
187 * Removes all the scalars from the internal array.
188 * \param option This is passed onto the internal Delete method.
190 virtual void Clear(Option_t* option = "");
192 /// Inherited form TObject. Performs a deep copy.
193 virtual void Copy(TObject& object) const;
195 /// Finds the scalar object by name.
196 virtual TObject* FindObject(const char* name) const
198 return fMap.FindObject(name);
201 /// Finds the scalar object with the same name as obj->GetName().
202 virtual TObject* FindObject(const TObject* obj) const
204 return fMap.FindObject(obj->GetName());
208 * Inherited from TObject, this prints the contents of all the scalars.
209 * \param option Can be "compact", which will just print all the values on one line.
211 virtual void Print(Option_t* option = "") const;
214 * The assignment operator performs a deep copy.
216 AliHLTScalars& operator = (const AliHLTScalars& obj);
218 /// Returns the n'th scalar or a zero sentinel if n is out of range.
219 const AliScalar& operator [] (UInt_t n) const { return GetScalarN(n); }
221 /// Returns the n'th scalar for editing. A new scalar is created if n is out of range.
222 AliScalar& operator [] (UInt_t n) { return GetScalarN(n); }
224 /// Returns the named scalar or a zero sentinel if no such scalar is found.
225 const AliScalar& operator [] (const TString& name) const { return GetScalar(name.Data()); }
227 /// Returns the named scalar for editing. A new scalar is created if n is out of range.
228 AliScalar& operator [] (const TString& name) { return GetScalar(name.Data()); }
231 * Inherited from TObject.
232 * Returns true if the names of the two sets of scalars are the same.
233 * \note The actual values are not checked. Use the comparison operator for that.
235 virtual Bool_t IsEqual(const TObject *obj) const;
238 * Comparison operator to check if two sets of scalars have the same values.
239 * \note The description strings are not checked so they could be different
240 * and the order of the scalars does not matter either.
242 bool operator == (const AliHLTScalars& obj) const;
245 * Comparison operator to check if two sets of scalars are different.
246 * \note The description strings are not checked, only the values are.
247 * In addition, the order of the scalars does not matter.
249 bool operator != (const AliHLTScalars& obj) const
251 return not (this->operator == (obj));
257 * Constructor that can be used by deriving classes to overload the class stored
258 * in the fScalars TClonesArray.
259 * \param cl The class to use in the fScalars as passed to the TClonesArray constructor.
260 * \param initSize The initial approximate number of elements in fScalars. (Default = 128).
261 * \note The class used in <i>cl</i> must derive from AliHLTScalars::AliScalar.
263 AliHLTScalars(const TClass* cl, Int_t initSize = 128);
266 * This method creates a new scalar object in the fScalars TClonesArray.
267 * \param i Location of the new object to construct in the TClonesArray.
268 * \param name The name of the new scalar.
269 * \param description The description of the new scalar.
270 * \param value The value of the new scalar.
271 * \returns the pointer to the new object created.
272 * \note This method must be overridden by classes inheriting from this class if
273 * the protected AliHLTScalars(const TClass*, Int_t) constructor is used to
274 * change the class stored in the fScalars TClonesArray.
275 * One should use the method ScalarForConstructor to get the location where
276 * the new scalar object will be constructed.
278 virtual AliScalar* NewScalar(UInt_t i, const char* name, const char* description, Double_t value);
281 * Returns a pointer to the memory where a new scalar object should be constructed.
282 * \param i The position of the new object.
284 TObject*& ScalarForConstructor(UInt_t i) { return fScalars[Int_t(i)]; }
287 * This method should return an empty sentinel object to mark that a scalar was
288 * not found in the list.
289 * \note This method must be overridden by classes inheriting from this class if
290 * the protected AliHLTScalars(const TClass*, Int_t) constructor is used to
291 * change the class stored in the fScalars TClonesArray.
293 virtual const AliScalar& Sentinel() const;
296 * This is an internal Add method which can be faster to use than the public Add method
297 * directly for classes derived from AliHLTScalars.
298 * [out] \param scalar This gets filled with the pointer of the new scalar created or
299 * the existing one found.
300 * [in] \param name The name of the scalar.
301 * [in] \param description A short description of the scalar.
302 * [in] \param value The value of the new scalar.
303 * \returns true if the scalar already exists and false otherwise.
305 bool Add(AliScalar*& scalar, const char* name, const char* description, Double_t value);
308 * Utility method for classes deriving from AliHLTScalars to fetch the i'th scalar
309 * from the TClonesArray without checking that the index is valid.
310 * \param i The index number of the scalar to fetch.
312 AliScalar* ScalarUncheckedAt(UInt_t i) const { return static_cast<AliScalar*>(fScalars.UncheckedAt(Int_t(i))); }
316 TClonesArray fScalars; // List of scalar objects.
317 THashTable fMap; //! Hash table of pointers to the scalars for fast lookup.
319 ClassDef(AliHLTScalars, 1); // Set of HLT scalars.
322 #endif // AliHLTSCALARS_H