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 bool AliHLTScalars::Add(const char* name, const char* description, Double_t value)
84 AliScalar* scalar = static_cast<AliScalar*>( fMap.FindObject(name) );
85 bool exists = scalar != NULL;
88 scalar = new (fScalars[fScalars.GetEntriesFast()]) AliScalar(name, description, value);
99 bool AliHLTScalars::Remove(const char* name)
101 // Removes a scalar from the list.
104 TObject* scalar = fMap.Remove(&x);
105 bool existed = scalar != NULL;
108 fScalars.Remove(scalar);
115 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name) const
117 // Fetch the named scalar object.
119 const TObject* scalar = fMap.FindObject(name);
122 return *static_cast<const AliScalar*>(scalar);
126 static AliHLTScalars::AliScalar sentinel;
132 AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name)
134 // Fetch the named scalar object for editing.
136 TObject* scalar = fMap.FindObject(name);
139 scalar = new (fScalars[fScalars.GetEntriesFast()]) AliScalar(name, "", 0);
142 return *static_cast<AliScalar*>(scalar);
146 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n) const
148 // Fetch the n'th scalar object.
150 if (n < NumberOfScalars())
152 const TObject* scalar = fScalars.UncheckedAt(Int_t(n));
153 return *static_cast<const AliScalar*>(scalar);
157 static AliHLTScalars::AliScalar sentinel;
163 AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n)
165 // Fetch the n'th scalar object for editing.
167 TObject* scalar = NULL;
168 if (n < NumberOfScalars())
170 scalar = fScalars.UncheckedAt(Int_t(n));
174 // We have to create all missing scalars since there cannot
175 // be gaps in the TClonesArray. This can cause segfaults during
176 // ROOT I/O of the class otherwise.
177 for (UInt_t i = NumberOfScalars(); i <= n; ++i)
179 // Make sure the the name of the scalar is not taken.
180 // If it is then find an unused name.
181 TString nameToUse = Form("Scalar%d", i);
182 if (FindObject(nameToUse.Data()) != NULL)
187 nameToUse = Form("Scalar%d_%d", i, m++);
189 while (FindObject(nameToUse.Data()) != NULL);
191 scalar = new (fScalars[i]) AliScalar(nameToUse.Data(), "", 0);
195 return *static_cast<AliScalar*>(scalar);
199 void AliHLTScalars::Reset()
201 // Sets all the scalar values and rates to zero.
203 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
205 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
211 void AliHLTScalars::Clear(Option_t* option)
213 // Clears the array of scalars.
216 fScalars.Delete(option);
220 void AliHLTScalars::Copy(TObject& object) const
222 // Performs a deep copy.
224 if (object.IsA() != AliHLTScalars::Class())
226 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
229 AliHLTScalars* obj = static_cast<AliHLTScalars*>(&object);
230 obj->operator = (*this);
234 void AliHLTScalars::Print(Option_t* option) const
236 // Prints the HLT trigger scalars.
238 TIter next(&fScalars);
239 const AliScalar* scalar = NULL;
241 TString opt = option;
242 if (opt == "compact")
244 scalar = static_cast<const AliScalar*>(next());
245 if (scalar != NULL) cout << scalar->Value();
246 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
248 cout << ", " << scalar->Value();
254 // Calculate the maximum field width required to keep things aligned.
256 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
258 int length = strlen(scalar->Name()) + strlen(scalar->Description()) + 3;
259 if (length > fieldwidth) fieldwidth = length;
261 if (fieldwidth > 80) fieldwidth = 80;
263 cout << "HLT scalars:" << endl;
265 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
267 TString str = scalar->Description();
269 str += scalar->Name();
271 cout << setw(fieldwidth) << str.Data() << setw(0)
272 << " = " << scalar->Value() << setw(0)
275 if (fScalars.GetSize() == 0) cout << "(none)" << endl;
279 AliHLTScalars& AliHLTScalars::operator = (const AliHLTScalars& obj)
281 // Performs a deep copy.
283 if (this == &obj) return *this;
284 Clear(); // Remove existing scalars.
285 TObject::operator = (obj);
286 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
288 const AliScalar* x = static_cast<const AliScalar*>(obj.fScalars.UncheckedAt(i));
289 new (fScalars[i]) AliScalar(*x);
291 fMap.AddAll(&fScalars);
296 Bool_t AliHLTScalars::IsEqual(const TObject *obj) const
298 // Checks if two sets of scalar lists have the same scalars.
301 if (obj->IsA()->GetBaseClass(AliHLTScalars::Class()) == NULL)
303 AliError(Form("Cannot compare object of type '%s'' with an object of type '%s'.",
304 this->ClassName(), obj->ClassName()
308 const AliHLTScalars* rhs = static_cast<const AliHLTScalars*>(obj);
309 if (fScalars.GetEntriesFast() != rhs->fScalars.GetEntriesFast()) return kFALSE;
310 TIter next(&fScalars);
311 const AliScalar* scalar = NULL;
312 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
314 if (rhs->fScalars.FindObject(scalar->Name()) == NULL) return kFALSE;
320 bool AliHLTScalars::operator == (const AliHLTScalars& obj) const
322 // Compares two scalar lists to see that they have the same scalar values.
324 if (fScalars.GetEntriesFast() != obj.fScalars.GetEntriesFast()) return false;
325 TIter next(&fScalars);
326 const AliScalar* a = NULL;
327 while ((a = static_cast<const AliScalar*>(next())) != NULL)
329 const AliScalar* b = static_cast<const AliScalar*>(
330 obj.fScalars.FindObject(a->Name())
332 if (b == NULL) return false;
333 if (a->Value() != b->Value()) return false;
339 void AliHLTScalars::AliScalar::Copy(TObject& object) const
341 // Performs a deep copy.
343 if (object.IsA() != AliHLTScalars::AliScalar::Class())
345 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
348 AliHLTScalars::AliScalar* obj = static_cast<AliHLTScalars::AliScalar*>(&object);