]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTScalars.h
Bug Fix.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTScalars.h
1 //-*- Mode: C++ -*-
2 // $Id: $
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                               */
8
9 ///  @file   AliHLTScalars.h
10 ///  @author Artur Szostak <artursz@iafrica.com>
11 ///  @date   28 Sep 2010
12 ///  @brief  Declares the a base class for named scalar values.
13
14 #include "TObject.h"
15 #include "TNamed.h"
16 #include "TTimeStamp.h"
17 #include "TClonesArray.h"
18 #include "THashTable.h"
19
20 /**
21  * @class AliHLTScalars
22  * @brief Container for named scalar values.
23  *
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.
27  *
28  * \ingroup alihlt_base
29  */
30 class AliHLTScalars : public TObject
31 {
32 public:
33         /**
34          * This class stores a single scalar value and name.
35          */
36         class AliScalar : public TNamed
37         {
38         public:
39                 /// Default constructor
40                 AliScalar() : TNamed(), fValue(0) {}
41                 
42                 /// Constructor to set the initial value.
43                 AliScalar(const char* name, const char* description, Double_t value) :
44                         TNamed(name, description), fValue(value)
45                 {}
46                 
47                 /// Default destructor
48                 virtual ~AliScalar() {}
49                 
50                 /// Inherited from TObject. Compares two scalar names.
51                 virtual Int_t Compare(const TObject *obj) const
52                 {
53                         return fName.CompareTo(obj->GetName());
54                 }
55                 
56                 /// Inherited from TObject. Returns true.
57                 virtual Bool_t IsSortable() const { return kTRUE; }
58                 
59                 /**
60                  * Inherited from TObject.
61                  * Returns true if the names of the scalars are the same.
62                  */
63                 virtual Bool_t IsEqual(const TObject *obj) const
64                 {
65                         return fName == obj->GetName();
66                 }
67                 
68                 /// Resets the scalar value to zero.
69                 virtual void Clear(Option_t* /*option*/ = "") { fValue = 0; }
70         
71                 /// Inherited from TObject. Performs a deep copy.
72                 virtual void Copy(TObject& object) const;
73                 
74                 /// Returns the value of the scalar.
75                 Double_t Value() const { return fValue; }
76
77                 /// Sets a new value for the scalar.
78                 void Value(Double_t value) { fValue = value; }
79
80                 /**
81                  * Increments the scalar by a value of 'count'.
82                  * \param count  The number to increment the scalar by. The default is 1.
83                  */
84                 void Increment(UInt_t count = 1) { fValue += count; }
85                 
86                 /// Returns the name of the scalar.
87                 const char* Name() const { return fName.Data(); }
88                 
89                 /// Returns the description string for the scalar.
90                 const char* Description() const { return fTitle.Data(); }
91                 
92                 /// Checks if two scalar objects are identical.
93                 bool operator == (const AliScalar& x) const
94                 {
95                         return fValue == x.fValue and fName == x.fName and fTitle == x.fTitle;
96                 }
97                 
98                 /// Checks if two scalar objects are not identical.
99                 bool operator != (const AliScalar& x) const
100                 {
101                         return not (this->operator == (x));
102                 }
103                 
104                 /// Typecast operator for returning the value directly.
105                 operator Double_t () { return fValue; }
106                 
107         private:
108                 Double_t fValue; // The scalar's value.
109                 
110                 ClassDef(AliScalar, 1);  // HLT scalar value.
111         };
112         
113         /// Default constructor.
114         AliHLTScalars();
115         
116         /// The copy constructor performs a deep copy.
117         AliHLTScalars(const AliHLTScalars& obj);
118         
119         /// Default destructor.
120         virtual ~AliHLTScalars();
121         
122         /**
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.
129          */
130         virtual bool Add(const char* name, const char* description = NULL, Double_t value = 0);
131
132         /**
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.
138          */
139         virtual bool Remove(const char* name);
140         
141         /// Checks to see if the named scalar exists.
142         bool Exists(const char* name) const { return fMap.FindObject(name) != NULL; }
143
144         /**
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.
149          */
150         const AliScalar& GetScalar(const char* name) const;
151
152         /**
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.
157          */
158         AliScalar& GetScalar(const char* name);
159
160         /// Returns the number of scalar values.
161         UInt_t NumberOfScalars() const { return UInt_t(fScalars.GetEntriesFast()); }
162
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.
166         
167         /**
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.
172          */
173         const AliScalar& GetScalarN(UInt_t n) const;
174
175         /**
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.
180          */
181         AliScalar& GetScalarN(UInt_t n);
182         
183         /// Resets all scalar values to zero.
184         virtual void Reset();
185         
186         /**
187          * Removes all the scalars from the internal array.
188          * \param option  This is passed onto the internal Delete method.
189          */
190         virtual void Clear(Option_t* option = "");
191         
192         /// Inherited form TObject. Performs a deep copy.
193         virtual void Copy(TObject& object) const;
194         
195         /// Finds the scalar object by name.
196         virtual TObject* FindObject(const char* name) const
197         {
198                 return fMap.FindObject(name);
199         }
200         
201         /// Finds the scalar object with the same name as obj->GetName().
202         virtual TObject* FindObject(const TObject* obj) const
203         {
204                 return fMap.FindObject(obj->GetName());
205         }
206         
207         /**
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.
210          */
211         virtual void Print(Option_t* option = "") const;
212         
213         /**
214          * The assignment operator performs a deep copy.
215          */
216         AliHLTScalars& operator = (const AliHLTScalars& obj);
217         
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); }
220
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); }
223
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()); }
226
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()); }
229
230         /**
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.
234          */
235         virtual Bool_t IsEqual(const TObject *obj) const;
236         
237         /**
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.
241          */
242         bool operator == (const AliHLTScalars& obj) const;
243         
244         /**
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.
248          */
249         bool operator != (const AliHLTScalars& obj) const
250         {
251                 return not (this->operator == (obj));
252         }
253
254 protected:
255         
256         /**
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.
262          */
263         AliHLTScalars(const TClass* cl, Int_t initSize = 128);
264         
265         /**
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.
277          */
278         virtual AliScalar* NewScalar(UInt_t i, const char* name, const char* description, Double_t value);
279         
280         /**
281          * Returns a pointer to the memory where a new scalar object should be constructed.
282          * \param i  The position of the new object.
283          */
284         TObject*& ScalarForConstructor(UInt_t i) { return fScalars[Int_t(i)]; }
285         
286         /**
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.
292          */
293         virtual const AliScalar& Sentinel() const;
294         
295         /**
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.
304          */
305         bool Add(AliScalar*& scalar, const char* name, const char* description, Double_t value);
306         
307         /**
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.
311          */
312         AliScalar* ScalarUncheckedAt(UInt_t i) const { return static_cast<AliScalar*>(fScalars.UncheckedAt(Int_t(i))); }
313         
314 private:
315         
316         TClonesArray fScalars;  // List of scalar objects.
317         THashTable fMap;        //! Hash table of pointers to the scalars for fast lookup.
318         
319         ClassDef(AliHLTScalars, 1);  // Set of HLT scalars.
320 };
321
322 #endif // AliHLTSCALARS_H