ROOT logo
//-*- Mode: C++ -*-
// $Id$
#ifndef AliHLTSCALARS_H
#define AliHLTSCALARS_H
/* This file is property of and copyright by the ALICE HLT Project        *
 * ALICE Experiment at CERN, All rights reserved.                         *
 * See cxx source for full Copyright notice                               */

///  @file   AliHLTScalars.h
///  @author Artur Szostak <artursz@iafrica.com>
///  @date   28 Sep 2010
///  @brief  Declares the a base class for named scalar values.

#include "TObject.h"
#include "TNamed.h"
#include "TClonesArray.h"
#include "THashTable.h"

/**
 * @class AliHLTScalars
 * @brief Container for named scalar values.
 *
 * This class contains a list of named scalars for an event as summary information.
 * These can be used by the trigger components to perform event selection or used
 * for monitoring purposes.
 *
 * \ingroup alihlt_base
 */
class AliHLTScalars : public TObject
{
public:
	/**
	 * This class stores a single scalar value and name.
	 */
	class AliScalar : public TNamed
	{
	public:
		/// Default constructor
		AliScalar() : TNamed(), fValue(0) {}
		
		/// Constructor to set the initial value.
		AliScalar(const char* name, const char* description, Double_t value) :
			TNamed(name, description), fValue(value)
		{}
		
		/// Default destructor
		virtual ~AliScalar() {}
		
		/// Inherited from TObject. Compares two scalar names.
		virtual Int_t Compare(const TObject *obj) const
		{
			return fName.CompareTo(obj->GetName());
		}
		
		/// Inherited from TObject. Returns true.
		virtual Bool_t IsSortable() const { return kTRUE; }
		
		/**
		 * Inherited from TObject.
		 * Returns true if the names of the scalars are the same.
		 */
		virtual Bool_t IsEqual(const TObject *obj) const
		{
			return fName == obj->GetName();
		}
		
		/// Resets the scalar value to zero.
		virtual void Clear(Option_t* /*option*/ = "") { fValue = 0; }
	
		/// Inherited from TObject. Performs a deep copy.
		virtual void Copy(TObject& object) const;
		
		/// Returns the value of the scalar.
		Double_t Value() const { return fValue; }

		/// Sets a new value for the scalar.
		void Value(Double_t value) { fValue = value; }

		/**
		 * Increments the scalar by a value of 'count'.
		 * \param count  The number to increment the scalar by. The default is 1.
		 */
		void Increment(UInt_t count = 1) { fValue += count; }
		
		/// Returns the name of the scalar.
		const char* Name() const { return fName.Data(); }
		
		/// Returns the description string for the scalar.
		const char* Description() const { return fTitle.Data(); }
		
		/// Checks if two scalar objects are identical.
		bool operator == (const AliScalar& x) const
		{
			return fValue == x.fValue and fName == x.fName and fTitle == x.fTitle;
		}
		
		/// Checks if two scalar objects are not identical.
		bool operator != (const AliScalar& x) const
		{
			return not (this->operator == (x));
		}
		
		/// Typecast operator for returning the value directly.
		operator Double_t () const { return fValue; }
		
	private:
		Double_t fValue; // The scalar's value.
		
		ClassDef(AliScalar, 1);  // HLT scalar value.
	};
	
	/// Default constructor.
	AliHLTScalars();
	
	/// The copy constructor performs a deep copy.
	AliHLTScalars(const AliHLTScalars& obj);
	
	/// Default destructor.
	virtual ~AliHLTScalars();
	
  /// Needed by schema evolution 
  const TClonesArray* GetScalars() const { return &fScalars; }
	
	/**
	 * Adds a new scalar to the end of the scalars list.
	 * If the scalar already exists then its values are updated instead.
	 * \param name  The name of the scalar.
	 * \param description  A short description of the scalar.
	 * \param value  The value of the new scalar.
	 * \returns true if the scalar already exists and false otherwise.
	 */
	virtual bool Add(const char* name, const char* description = NULL, Double_t value = 0);

	/**
	 * Removes a named scalar from the scalars list.
	 * \param name  The name of the scalar to remove.
	 * \returns true if the scalar existed and false otherwise.
	 * \note The scalars list is compressed so this method will be slow.
	 *    In addition, scalar positions will change if not removing from the end.
	 */
	virtual bool Remove(const char* name);
	
	/// Checks to see if the named scalar exists.
	bool Exists(const char* name) const { return fMap.FindObject(name) != NULL; }

	/**
	 * Fetches the specified scalar object.
	 * \param name  The name of the scalar object.
	 * \returns the found scalar object, otherwise an empty sentinel object with
	 *    zeros. One can tell it is a sentinel because the name will be empty.
	 */
	const AliScalar& GetScalar(const char* name) const;

	/**
	 * Fetches the specified scalar object for editing.
	 * \param name  The name of the scalar object.
	 * \returns the found scalar object. If the scalar does not already
	 *     exist then a new one is created and returned.
	 */
	AliScalar& GetScalar(const char* name);

	/// Returns the number of scalar values.
	UInt_t NumberOfScalars() const { return UInt_t(fScalars.GetEntriesFast()); }

	// Note: the following GetScalarN methods do not use the same name as
	// GetScalar above because the parameter type would unfortunately be
	// ambiguous to an ISO c++ compiler.
	
	/**
	 * Fetches the n'th scalar object.
	 * \param n  The number of the scalar object.
	 * \returns the found scalar object, otherwise an empty sentinel object with
	 *    zeros. One can tell it is a sentinel because the name will be empty.
	 */
	const AliScalar& GetScalarN(UInt_t n) const;

	/**
	 * Fetches the n'th scalar object for editing.
	 * \param n  The number of the scalar object.
	 * \returns the found scalar object. If the scalar does not already
	 *     exist then a new one is created and returned.
	 */
	AliScalar& GetScalarN(UInt_t n);
	
	/// Resets all scalar values to zero.
	virtual void Reset();
	
	/**
	 * Removes all the scalars from the internal array.
	 * \param option  This is passed onto the internal Delete method.
	 */
	virtual void Clear(Option_t* option = "");
	
	/// Inherited form TObject. Performs a deep copy.
	virtual void Copy(TObject& object) const;
	
	/// Finds the scalar object by name.
	virtual TObject* FindObject(const char* name) const
	{
		return fMap.FindObject(name);
	}
	
	/// Finds the scalar object with the same name as obj->GetName().
	virtual TObject* FindObject(const TObject* obj) const
	{
		return fMap.FindObject(obj->GetName());
	}
	
	/**
	 * Inherited from TObject, this prints the contents of all the scalars.
	 * \param option  Can be "compact", which will just print all the values on one line.
	 */
	virtual void Print(Option_t* option = "") const;
	
	/**
	 * The assignment operator performs a deep copy.
	 */
	AliHLTScalars& operator = (const AliHLTScalars& obj);
	
	/// Returns the n'th scalar or a zero sentinel if n is out of range.
	const AliScalar& operator [] (UInt_t n) const { return GetScalarN(n); }

	/// Returns the n'th scalar for editing. A new scalar is created if n is out of range.
	AliScalar& operator [] (UInt_t n) { return GetScalarN(n); }

	/// Returns the named scalar or a zero sentinel if no such scalar is found.
	const AliScalar& operator [] (const TString& name) const { return GetScalar(name.Data()); }

	/// Returns the named scalar for editing. A new scalar is created if the named scalar is not found.
	AliScalar& operator [] (const TString& name) { return GetScalar(name.Data()); }

	/**
	 * Inherited from TObject.
	 * Returns true if the names of the two sets of scalars are the same.
	 * \note The actual values are not checked. Use the comparison operator for that.
	 */
	virtual Bool_t IsEqual(const TObject *obj) const;
	
	/**
	 * Comparison operator to check if two sets of scalars have the same values.
	 * \note The description strings are not checked so they could be different
	 *   and the order of the scalars does not matter either.
	 */
	bool operator == (const AliHLTScalars& obj) const;
	
	/**
	 * Comparison operator to check if two sets of scalars are different.
	 * \note The description strings are not checked, only the values are.
	 *   In addition, the order of the scalars does not matter.
	 */
	bool operator != (const AliHLTScalars& obj) const
	{
		return not (this->operator == (obj));
	}

protected:
	
	/**
	 * Constructor that can be used by deriving classes to overload the class stored
	 * in the fScalars TClonesArray.
	 * \param cl  The class to use in the fScalars as passed to the TClonesArray constructor.
	 * \param initSize  The initial approximate number of elements in fScalars. (Default = 128).
	 * \note The class used in <i>cl</i> must derive from AliHLTScalars::AliScalar.
	 */
	AliHLTScalars(const TClass* cl, Int_t initSize = 128);
	
	/**
	 * This method creates a new scalar object in the fScalars TClonesArray.
	 * \param i  Location of the new object to construct in the TClonesArray.
	 * \param name  The name of the new scalar.
	 * \param description  The description of the new scalar.
	 * \param value  The value of the new scalar.
	 * \returns the pointer to the new object created.
	 * \note This method must be overridden by classes inheriting from this class if
	 *    the protected AliHLTScalars(const TClass*, Int_t) constructor is used to
	 *    change the class stored in the fScalars TClonesArray.
	 *    One should use the method ScalarForConstructor to get the location where
	 *    the new scalar object will be constructed.
	 */
	virtual AliScalar* NewScalar(UInt_t i, const char* name, const char* description, Double_t value);
	
	/**
	 * Returns a pointer to the memory where a new scalar object should be constructed.
	 * \param i  The position of the new object.
	 */
	TObject*& ScalarForConstructor(UInt_t i) { return fScalars[Int_t(i)]; }
	
	/**
	 * This method should return an empty sentinel object to mark that a scalar was
	 * not found in the list.
	 * \note This method must be overridden by classes inheriting from this class if
	 *    the protected AliHLTScalars(const TClass*, Int_t) constructor is used to
	 *    change the class stored in the fScalars TClonesArray.
	 */
	virtual const AliScalar& Sentinel() const;
	
	/**
	 * This is an internal Add method which can be faster to use than the public Add method
	 * directly for classes derived from AliHLTScalars.
	 * \param [out] scalar This gets filled with the pointer of the new scalar created or
	 *    the existing one found.
	 * \param [in] name The name of the scalar.
	 * \param [in] description  A short description of the scalar.
	 * \param [in] value  The value of the new scalar.
	 * \returns true if the scalar already exists and false otherwise.
	 */
	bool Add(AliScalar*& scalar, const char* name, const char* description, Double_t value);
	
	/**
	 * Utility method for classes deriving from AliHLTScalars to fetch the i'th scalar
	 * from the TClonesArray without checking that the index is valid.
	 * \param i  The index number of the scalar to fetch.
	 */
	AliScalar* ScalarUncheckedAt(UInt_t i) const { return static_cast<AliScalar*>(fScalars.UncheckedAt(Int_t(i))); }
	
private:
	
	TClonesArray fScalars;  // List of scalar objects.
	THashTable fMap;        //! Hash table of pointers to the scalars for fast lookup.
	
	ClassDef(AliHLTScalars, 1);  // Set of HLT scalars.
};

#endif // AliHLTSCALARS_H
 AliHLTScalars.h:1
 AliHLTScalars.h:2
 AliHLTScalars.h:3
 AliHLTScalars.h:4
 AliHLTScalars.h:5
 AliHLTScalars.h:6
 AliHLTScalars.h:7
 AliHLTScalars.h:8
 AliHLTScalars.h:9
 AliHLTScalars.h:10
 AliHLTScalars.h:11
 AliHLTScalars.h:12
 AliHLTScalars.h:13
 AliHLTScalars.h:14
 AliHLTScalars.h:15
 AliHLTScalars.h:16
 AliHLTScalars.h:17
 AliHLTScalars.h:18
 AliHLTScalars.h:19
 AliHLTScalars.h:20
 AliHLTScalars.h:21
 AliHLTScalars.h:22
 AliHLTScalars.h:23
 AliHLTScalars.h:24
 AliHLTScalars.h:25
 AliHLTScalars.h:26
 AliHLTScalars.h:27
 AliHLTScalars.h:28
 AliHLTScalars.h:29
 AliHLTScalars.h:30
 AliHLTScalars.h:31
 AliHLTScalars.h:32
 AliHLTScalars.h:33
 AliHLTScalars.h:34
 AliHLTScalars.h:35
 AliHLTScalars.h:36
 AliHLTScalars.h:37
 AliHLTScalars.h:38
 AliHLTScalars.h:39
 AliHLTScalars.h:40
 AliHLTScalars.h:41
 AliHLTScalars.h:42
 AliHLTScalars.h:43
 AliHLTScalars.h:44
 AliHLTScalars.h:45
 AliHLTScalars.h:46
 AliHLTScalars.h:47
 AliHLTScalars.h:48
 AliHLTScalars.h:49
 AliHLTScalars.h:50
 AliHLTScalars.h:51
 AliHLTScalars.h:52
 AliHLTScalars.h:53
 AliHLTScalars.h:54
 AliHLTScalars.h:55
 AliHLTScalars.h:56
 AliHLTScalars.h:57
 AliHLTScalars.h:58
 AliHLTScalars.h:59
 AliHLTScalars.h:60
 AliHLTScalars.h:61
 AliHLTScalars.h:62
 AliHLTScalars.h:63
 AliHLTScalars.h:64
 AliHLTScalars.h:65
 AliHLTScalars.h:66
 AliHLTScalars.h:67
 AliHLTScalars.h:68
 AliHLTScalars.h:69
 AliHLTScalars.h:70
 AliHLTScalars.h:71
 AliHLTScalars.h:72
 AliHLTScalars.h:73
 AliHLTScalars.h:74
 AliHLTScalars.h:75
 AliHLTScalars.h:76
 AliHLTScalars.h:77
 AliHLTScalars.h:78
 AliHLTScalars.h:79
 AliHLTScalars.h:80
 AliHLTScalars.h:81
 AliHLTScalars.h:82
 AliHLTScalars.h:83
 AliHLTScalars.h:84
 AliHLTScalars.h:85
 AliHLTScalars.h:86
 AliHLTScalars.h:87
 AliHLTScalars.h:88
 AliHLTScalars.h:89
 AliHLTScalars.h:90
 AliHLTScalars.h:91
 AliHLTScalars.h:92
 AliHLTScalars.h:93
 AliHLTScalars.h:94
 AliHLTScalars.h:95
 AliHLTScalars.h:96
 AliHLTScalars.h:97
 AliHLTScalars.h:98
 AliHLTScalars.h:99
 AliHLTScalars.h:100
 AliHLTScalars.h:101
 AliHLTScalars.h:102
 AliHLTScalars.h:103
 AliHLTScalars.h:104
 AliHLTScalars.h:105
 AliHLTScalars.h:106
 AliHLTScalars.h:107
 AliHLTScalars.h:108
 AliHLTScalars.h:109
 AliHLTScalars.h:110
 AliHLTScalars.h:111
 AliHLTScalars.h:112
 AliHLTScalars.h:113
 AliHLTScalars.h:114
 AliHLTScalars.h:115
 AliHLTScalars.h:116
 AliHLTScalars.h:117
 AliHLTScalars.h:118
 AliHLTScalars.h:119
 AliHLTScalars.h:120
 AliHLTScalars.h:121
 AliHLTScalars.h:122
 AliHLTScalars.h:123
 AliHLTScalars.h:124
 AliHLTScalars.h:125
 AliHLTScalars.h:126
 AliHLTScalars.h:127
 AliHLTScalars.h:128
 AliHLTScalars.h:129
 AliHLTScalars.h:130
 AliHLTScalars.h:131
 AliHLTScalars.h:132
 AliHLTScalars.h:133
 AliHLTScalars.h:134
 AliHLTScalars.h:135
 AliHLTScalars.h:136
 AliHLTScalars.h:137
 AliHLTScalars.h:138
 AliHLTScalars.h:139
 AliHLTScalars.h:140
 AliHLTScalars.h:141
 AliHLTScalars.h:142
 AliHLTScalars.h:143
 AliHLTScalars.h:144
 AliHLTScalars.h:145
 AliHLTScalars.h:146
 AliHLTScalars.h:147
 AliHLTScalars.h:148
 AliHLTScalars.h:149
 AliHLTScalars.h:150
 AliHLTScalars.h:151
 AliHLTScalars.h:152
 AliHLTScalars.h:153
 AliHLTScalars.h:154
 AliHLTScalars.h:155
 AliHLTScalars.h:156
 AliHLTScalars.h:157
 AliHLTScalars.h:158
 AliHLTScalars.h:159
 AliHLTScalars.h:160
 AliHLTScalars.h:161
 AliHLTScalars.h:162
 AliHLTScalars.h:163
 AliHLTScalars.h:164
 AliHLTScalars.h:165
 AliHLTScalars.h:166
 AliHLTScalars.h:167
 AliHLTScalars.h:168
 AliHLTScalars.h:169
 AliHLTScalars.h:170
 AliHLTScalars.h:171
 AliHLTScalars.h:172
 AliHLTScalars.h:173
 AliHLTScalars.h:174
 AliHLTScalars.h:175
 AliHLTScalars.h:176
 AliHLTScalars.h:177
 AliHLTScalars.h:178
 AliHLTScalars.h:179
 AliHLTScalars.h:180
 AliHLTScalars.h:181
 AliHLTScalars.h:182
 AliHLTScalars.h:183
 AliHLTScalars.h:184
 AliHLTScalars.h:185
 AliHLTScalars.h:186
 AliHLTScalars.h:187
 AliHLTScalars.h:188
 AliHLTScalars.h:189
 AliHLTScalars.h:190
 AliHLTScalars.h:191
 AliHLTScalars.h:192
 AliHLTScalars.h:193
 AliHLTScalars.h:194
 AliHLTScalars.h:195
 AliHLTScalars.h:196
 AliHLTScalars.h:197
 AliHLTScalars.h:198
 AliHLTScalars.h:199
 AliHLTScalars.h:200
 AliHLTScalars.h:201
 AliHLTScalars.h:202
 AliHLTScalars.h:203
 AliHLTScalars.h:204
 AliHLTScalars.h:205
 AliHLTScalars.h:206
 AliHLTScalars.h:207
 AliHLTScalars.h:208
 AliHLTScalars.h:209
 AliHLTScalars.h:210
 AliHLTScalars.h:211
 AliHLTScalars.h:212
 AliHLTScalars.h:213
 AliHLTScalars.h:214
 AliHLTScalars.h:215
 AliHLTScalars.h:216
 AliHLTScalars.h:217
 AliHLTScalars.h:218
 AliHLTScalars.h:219
 AliHLTScalars.h:220
 AliHLTScalars.h:221
 AliHLTScalars.h:222
 AliHLTScalars.h:223
 AliHLTScalars.h:224
 AliHLTScalars.h:225
 AliHLTScalars.h:226
 AliHLTScalars.h:227
 AliHLTScalars.h:228
 AliHLTScalars.h:229
 AliHLTScalars.h:230
 AliHLTScalars.h:231
 AliHLTScalars.h:232
 AliHLTScalars.h:233
 AliHLTScalars.h:234
 AliHLTScalars.h:235
 AliHLTScalars.h:236
 AliHLTScalars.h:237
 AliHLTScalars.h:238
 AliHLTScalars.h:239
 AliHLTScalars.h:240
 AliHLTScalars.h:241
 AliHLTScalars.h:242
 AliHLTScalars.h:243
 AliHLTScalars.h:244
 AliHLTScalars.h:245
 AliHLTScalars.h:246
 AliHLTScalars.h:247
 AliHLTScalars.h:248
 AliHLTScalars.h:249
 AliHLTScalars.h:250
 AliHLTScalars.h:251
 AliHLTScalars.h:252
 AliHLTScalars.h:253
 AliHLTScalars.h:254
 AliHLTScalars.h:255
 AliHLTScalars.h:256
 AliHLTScalars.h:257
 AliHLTScalars.h:258
 AliHLTScalars.h:259
 AliHLTScalars.h:260
 AliHLTScalars.h:261
 AliHLTScalars.h:262
 AliHLTScalars.h:263
 AliHLTScalars.h:264
 AliHLTScalars.h:265
 AliHLTScalars.h:266
 AliHLTScalars.h:267
 AliHLTScalars.h:268
 AliHLTScalars.h:269
 AliHLTScalars.h:270
 AliHLTScalars.h:271
 AliHLTScalars.h:272
 AliHLTScalars.h:273
 AliHLTScalars.h:274
 AliHLTScalars.h:275
 AliHLTScalars.h:276
 AliHLTScalars.h:277
 AliHLTScalars.h:278
 AliHLTScalars.h:279
 AliHLTScalars.h:280
 AliHLTScalars.h:281
 AliHLTScalars.h:282
 AliHLTScalars.h:283
 AliHLTScalars.h:284
 AliHLTScalars.h:285
 AliHLTScalars.h:286
 AliHLTScalars.h:287
 AliHLTScalars.h:288
 AliHLTScalars.h:289
 AliHLTScalars.h:290
 AliHLTScalars.h:291
 AliHLTScalars.h:292
 AliHLTScalars.h:293
 AliHLTScalars.h:294
 AliHLTScalars.h:295
 AliHLTScalars.h:296
 AliHLTScalars.h:297
 AliHLTScalars.h:298
 AliHLTScalars.h:299
 AliHLTScalars.h:300
 AliHLTScalars.h:301
 AliHLTScalars.h:302
 AliHLTScalars.h:303
 AliHLTScalars.h:304
 AliHLTScalars.h:305
 AliHLTScalars.h:306
 AliHLTScalars.h:307
 AliHLTScalars.h:308
 AliHLTScalars.h:309
 AliHLTScalars.h:310
 AliHLTScalars.h:311
 AliHLTScalars.h:312
 AliHLTScalars.h:313
 AliHLTScalars.h:314
 AliHLTScalars.h:315
 AliHLTScalars.h:316
 AliHLTScalars.h:317
 AliHLTScalars.h:318
 AliHLTScalars.h:319
 AliHLTScalars.h:320
 AliHLTScalars.h:321
 AliHLTScalars.h:322
 AliHLTScalars.h:323
 AliHLTScalars.h:324