2 /**************************************************************************
3 * This file is property of and copyright by the ALICE HLT Project *
4 * All rights reserved. *
7 * Artur Szostak <artursz@iafrica.com> *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
18 /// @file AliHLTScalars.cxx
19 /// @author Artur Szostak <artursz@iafrica.com>
21 /// @brief Implementation of the HLT scalars class.
23 /// This implements the scalars class for the HLT, which is a collection of
24 /// named scalar values. Searching for a named scalar is optimised by using
27 #include "AliHLTScalars.h"
30 #include "TIterator.h"
31 #include "Riostream.h"
34 ClassImp(AliHLTScalars);
35 ClassImp(AliHLTScalars::AliScalar);
38 AliHLTScalars::AliHLTScalars() :
40 fScalars(AliHLTScalars::AliScalar::Class(), 128),
41 fMap(TCollection::kInitHashTableCapacity, 2)
43 // Default constructor.
45 fMap.SetOwner(kFALSE);
49 AliHLTScalars::AliHLTScalars(const AliHLTScalars& obj) :
51 fScalars(obj.fScalars),
52 fMap(obj.fMap.GetSize(), obj.fMap.GetRehashLevel())
54 // Copy constructor performs a deep copy.
56 fMap.SetOwner(kFALSE);
57 fMap.AddAll(&fScalars);
61 AliHLTScalars::AliHLTScalars(const TClass* cl, Int_t initSize) :
63 fScalars(cl, initSize),
64 fMap(TCollection::kInitHashTableCapacity, 2)
66 // Constructor to be able to specify a custom class for the fScalars TClonesArray object.
68 fMap.SetOwner(kFALSE);
72 AliHLTScalars::~AliHLTScalars()
74 // Default destructor.
80 AliHLTScalars::AliScalar* AliHLTScalars::NewScalar(UInt_t i, const char* name, const char* description, Double_t value)
82 // Creates a new scalar object.
84 return new (fScalars[i]) AliScalar(name, description, value);
88 const AliHLTScalars::AliScalar& AliHLTScalars::Sentinel() const
90 // Returns an empty sentinel object.
92 static AliHLTScalars::AliScalar sentinel;
97 bool AliHLTScalars::Add(AliScalar*& scalar, const char* name, const char* description, Double_t value)
99 // Adds a new scalar (internal version).
101 scalar = static_cast<AliScalar*>( fMap.FindObject(name) );
102 bool exists = scalar != NULL;
105 scalar = NewScalar(fScalars.GetEntriesFast(), name, description, value);
110 scalar->Value(value);
116 bool AliHLTScalars::Add(const char* name, const char* description, Double_t value)
118 // Adds a new scalar.
120 AliScalar* scalar = NULL;
121 return Add(scalar, name, description, value);
125 bool AliHLTScalars::Remove(const char* name)
127 // Removes a scalar from the list.
130 TObject* scalar = fMap.Remove(&x);
131 bool existed = scalar != NULL;
134 fScalars.Remove(scalar);
141 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name) const
143 // Fetch the named scalar object.
145 const TObject* scalar = fMap.FindObject(name);
148 return *static_cast<const AliScalar*>(scalar);
157 AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name)
159 // Fetch the named scalar object for editing.
161 TObject* scalar = fMap.FindObject(name);
164 scalar = NewScalar(fScalars.GetEntriesFast(), name, "", 0);
167 return *static_cast<AliScalar*>(scalar);
171 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n) const
173 // Fetch the n'th scalar object.
175 if (n < NumberOfScalars())
177 const TObject* scalar = fScalars.UncheckedAt(Int_t(n));
178 return *static_cast<const AliScalar*>(scalar);
187 AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n)
189 // Fetch the n'th scalar object for editing.
191 TObject* scalar = NULL;
192 if (n < NumberOfScalars())
194 scalar = fScalars.UncheckedAt(Int_t(n));
198 // We have to create all missing scalars since there cannot
199 // be gaps in the TClonesArray. This can cause segfaults during
200 // ROOT I/O of the class otherwise.
201 for (UInt_t i = NumberOfScalars(); i <= n; ++i)
203 // Make sure the the name of the scalar is not taken.
204 // If it is then find an unused name.
205 TString nameToUse = Form("Scalar%d", i);
206 if (FindObject(nameToUse.Data()) != NULL)
211 nameToUse = Form("Scalar%d_%d", i, m++);
213 while (FindObject(nameToUse.Data()) != NULL);
215 scalar = NewScalar(i, nameToUse.Data(), "", 0);
219 return *static_cast<AliScalar*>(scalar);
223 void AliHLTScalars::Reset()
225 // Sets all the scalar values to zero.
227 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
229 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
235 void AliHLTScalars::Clear(Option_t* option)
237 // Clears the array of scalars.
240 fScalars.Delete(option);
244 void AliHLTScalars::Copy(TObject& object) const
246 // Performs a deep copy.
248 if (object.IsA() != AliHLTScalars::Class())
250 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
253 AliHLTScalars* obj = static_cast<AliHLTScalars*>(&object);
254 obj->operator = (*this);
258 void AliHLTScalars::Print(Option_t* option) const
260 // Prints the HLT trigger scalars.
262 TIter next(&fScalars);
263 const AliScalar* scalar = NULL;
265 TString opt = option;
266 if (opt == "compact")
268 scalar = static_cast<const AliScalar*>(next());
269 if (scalar != NULL) cout << scalar->Value();
270 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
272 cout << ", " << scalar->Value();
278 // Calculate the maximum field width required to keep things aligned.
280 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
282 int length = strlen(scalar->Name()) + strlen(scalar->Description()) + 3;
283 if (length > fieldwidth) fieldwidth = length;
285 if (fieldwidth > 80) fieldwidth = 80;
287 cout << "HLT scalars:" << endl;
289 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
291 TString str = scalar->Description();
293 str += scalar->Name();
295 cout << setw(fieldwidth) << str.Data() << setw(0)
296 << " = " << scalar->Value() << setw(0)
299 if (fScalars.GetEntriesFast() == 0) cout << "(none)" << endl;
303 AliHLTScalars& AliHLTScalars::operator = (const AliHLTScalars& obj)
305 // Performs a deep copy.
307 if (this == &obj) return *this;
308 Clear(); // Remove existing scalars.
309 TObject::operator = (obj);
310 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
312 const AliScalar* x = static_cast<const AliScalar*>(obj.fScalars.UncheckedAt(i));
313 new (fScalars[i]) AliScalar(*x);
315 fMap.AddAll(&fScalars);
320 Bool_t AliHLTScalars::IsEqual(const TObject *obj) const
322 // Checks if two sets of scalar lists have the same scalars.
325 if (obj->IsA()->GetBaseClass(AliHLTScalars::Class()) == NULL)
327 AliError(Form("Cannot compare object of type '%s'' with an object of type '%s'.",
328 this->ClassName(), obj->ClassName()
332 const AliHLTScalars* rhs = static_cast<const AliHLTScalars*>(obj);
333 if (fScalars.GetEntriesFast() != rhs->fScalars.GetEntriesFast()) return kFALSE;
334 TIter next(&fScalars);
335 const AliScalar* scalar = NULL;
336 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
338 if (rhs->fScalars.FindObject(scalar->Name()) == NULL) return kFALSE;
344 bool AliHLTScalars::operator == (const AliHLTScalars& obj) const
346 // Compares two scalar lists to see that they have the same scalar values.
348 if (fScalars.GetEntriesFast() != obj.fScalars.GetEntriesFast()) return false;
349 TIter next(&fScalars);
350 const AliScalar* a = NULL;
351 while ((a = static_cast<const AliScalar*>(next())) != NULL)
353 const AliScalar* b = static_cast<const AliScalar*>(
354 obj.fScalars.FindObject(a->Name())
356 if (b == NULL) return false;
357 if (a->Value() != b->Value()) return false;
363 void AliHLTScalars::AliScalar::Copy(TObject& object) const
365 // Performs a deep copy.
367 if (object.IsA() != AliHLTScalars::AliScalar::Class())
369 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
372 AliHLTScalars::AliScalar* obj = static_cast<AliHLTScalars::AliScalar*>(&object);