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"
29 #include "AliHLTLogging.h" // HLT logging
30 #include "TClass.h" // for Class_Name macro for logging
31 #include "TIterator.h"
32 #include "Riostream.h"
35 ClassImp(AliHLTScalars);
36 ClassImp(AliHLTScalars::AliScalar);
39 AliHLTScalars::AliHLTScalars() :
41 fScalars(AliHLTScalars::AliScalar::Class(), 128),
42 fMap(TCollection::kInitHashTableCapacity, 2)
44 // Default constructor.
46 fMap.SetOwner(kFALSE);
50 AliHLTScalars::AliHLTScalars(const AliHLTScalars& obj) :
52 fScalars(obj.fScalars),
53 fMap(obj.fMap.GetSize(), obj.fMap.GetRehashLevel())
55 // Copy constructor performs a deep copy.
57 fMap.SetOwner(kFALSE);
58 fMap.AddAll(&fScalars);
62 AliHLTScalars::AliHLTScalars(const TClass* cl, Int_t initSize) :
64 fScalars(cl, initSize),
65 fMap(TCollection::kInitHashTableCapacity, 2)
67 // Constructor to be able to specify a custom class for the fScalars TClonesArray object.
69 fMap.SetOwner(kFALSE);
73 AliHLTScalars::~AliHLTScalars()
75 // Default destructor.
81 AliHLTScalars::AliScalar* AliHLTScalars::NewScalar(UInt_t i, const char* name, const char* description, Double_t value)
83 // Creates a new scalar object.
85 return new (fScalars[i]) AliScalar(name, description, value);
89 const AliHLTScalars::AliScalar& AliHLTScalars::Sentinel() const
91 // Returns an empty sentinel object.
93 static AliHLTScalars::AliScalar sentinel;
98 bool AliHLTScalars::Add(AliScalar*& scalar, const char* name, const char* description, Double_t value)
100 // Adds a new scalar (internal version).
102 scalar = static_cast<AliScalar*>( fMap.FindObject(name) );
103 bool exists = scalar != NULL;
106 scalar = NewScalar(fScalars.GetEntriesFast(), name, description, value);
111 scalar->Value(value);
117 bool AliHLTScalars::Add(const char* name, const char* description, Double_t value)
119 // Adds a new scalar.
121 AliScalar* scalar = NULL;
122 return Add(scalar, name, description, value);
126 bool AliHLTScalars::Remove(const char* name)
128 // Removes a scalar from the list.
131 TObject* scalar = fMap.Remove(&x);
132 bool existed = scalar != NULL;
135 fScalars.Remove(scalar);
142 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name) const
144 // Fetch the named scalar object.
146 const TObject* scalar = fMap.FindObject(name);
149 return *static_cast<const AliScalar*>(scalar);
158 AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name)
160 // Fetch the named scalar object for editing.
162 TObject* scalar = fMap.FindObject(name);
165 scalar = NewScalar(fScalars.GetEntriesFast(), name, "", 0);
168 return *static_cast<AliScalar*>(scalar);
172 const AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n) const
174 // Fetch the n'th scalar object.
176 if (n < NumberOfScalars())
178 const TObject* scalar = fScalars.UncheckedAt(Int_t(n));
179 return *static_cast<const AliScalar*>(scalar);
188 AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n)
190 // Fetch the n'th scalar object for editing.
192 TObject* scalar = NULL;
193 if (n < NumberOfScalars())
195 scalar = fScalars.UncheckedAt(Int_t(n));
199 // We have to create all missing scalars since there cannot
200 // be gaps in the TClonesArray. This can cause segfaults during
201 // ROOT I/O of the class otherwise.
202 for (UInt_t i = NumberOfScalars(); i <= n; ++i)
204 // Make sure the the name of the scalar is not taken.
205 // If it is then find an unused name.
206 TString nameToUse = Form("Scalar%d", i);
207 if (FindObject(nameToUse.Data()) != NULL)
212 nameToUse = Form("Scalar%d_%d", i, m++);
214 while (FindObject(nameToUse.Data()) != NULL);
216 scalar = NewScalar(i, nameToUse.Data(), "", 0);
220 return *static_cast<AliScalar*>(scalar);
224 void AliHLTScalars::Reset()
226 // Sets all the scalar values to zero.
228 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
230 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
236 void AliHLTScalars::Clear(Option_t* option)
238 // Clears the array of scalars.
241 fScalars.Delete(option);
245 void AliHLTScalars::Copy(TObject& object) const
247 // Performs a deep copy.
249 if (object.IsA() != AliHLTScalars::Class())
252 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot copy to an object of type '%s'.", object.ClassName());
255 AliHLTScalars* obj = static_cast<AliHLTScalars*>(&object);
256 obj->operator = (*this);
260 void AliHLTScalars::Print(Option_t* option) const
262 // Prints the HLT trigger scalars.
264 TIter next(&fScalars);
265 const AliScalar* scalar = NULL;
267 TString opt = option;
268 if (opt == "compact")
270 scalar = static_cast<const AliScalar*>(next());
271 if (scalar != NULL) cout << scalar->Value();
272 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
274 cout << ", " << scalar->Value();
280 // Calculate the maximum field width required to keep things aligned.
282 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
284 int length = strlen(scalar->Name()) + strlen(scalar->Description()) + 3;
285 if (length > fieldwidth) fieldwidth = length;
287 if (fieldwidth > 80) fieldwidth = 80;
289 cout << "HLT scalars:" << endl;
291 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
293 TString str = scalar->Description();
295 str += scalar->Name();
297 cout << setw(fieldwidth) << str.Data() << setw(0)
298 << " = " << scalar->Value() << setw(0)
301 if (fScalars.GetEntriesFast() == 0) cout << "(none)" << endl;
305 AliHLTScalars& AliHLTScalars::operator = (const AliHLTScalars& obj)
307 // Performs a deep copy.
309 if (this == &obj) return *this;
310 Clear(); // Remove existing scalars.
311 TObject::operator = (obj);
312 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
314 const AliScalar* x = static_cast<const AliScalar*>(obj.fScalars.UncheckedAt(i));
315 new (fScalars[i]) AliScalar(*x);
317 fMap.AddAll(&fScalars);
322 Bool_t AliHLTScalars::IsEqual(const TObject *obj) const
324 // Checks if two sets of scalar lists have the same scalars.
327 if (obj->IsA()->GetBaseClass(AliHLTScalars::Class()) == NULL)
330 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot compare object of type '%s'' with an object of type '%s'.",
331 this->ClassName(), obj->ClassName()
335 const AliHLTScalars* rhs = static_cast<const AliHLTScalars*>(obj);
336 if (fScalars.GetEntriesFast() != rhs->fScalars.GetEntriesFast()) return kFALSE;
337 TIter next(&fScalars);
338 const AliScalar* scalar = NULL;
339 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
341 if (rhs->fScalars.FindObject(scalar->Name()) == NULL) return kFALSE;
347 bool AliHLTScalars::operator == (const AliHLTScalars& obj) const
349 // Compares two scalar lists to see that they have the same scalar values.
351 if (fScalars.GetEntriesFast() != obj.fScalars.GetEntriesFast()) return false;
352 TIter next(&fScalars);
353 const AliScalar* a = NULL;
354 while ((a = static_cast<const AliScalar*>(next())) != NULL)
356 const AliScalar* b = static_cast<const AliScalar*>(
357 obj.fScalars.FindObject(a->Name())
359 if (b == NULL) return false;
360 if (a->Value() != b->Value()) return false;
366 void AliHLTScalars::AliScalar::Copy(TObject& object) const
368 // Performs a deep copy.
370 if (object.IsA() != AliHLTScalars::AliScalar::Class())
373 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot copy to an object of type '%s'.", object.ClassName());
376 AliHLTScalars::AliScalar* obj = static_cast<AliHLTScalars::AliScalar*>(&object);