]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTScalars.cxx
Be sure to load mapping when needed
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTScalars.cxx
CommitLineData
63a7ee27 1// $Id$
9cb24db9 2/**************************************************************************
3 * This file is property of and copyright by the ALICE HLT Project *
4 * All rights reserved. *
5 * *
6 * Primary Authors: *
7 * Artur Szostak <artursz@iafrica.com> *
8 * *
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 **************************************************************************/
17
18/// @file AliHLTScalars.cxx
19/// @author Artur Szostak <artursz@iafrica.com>
20/// @date 28 Sep 2010
21/// @brief Implementation of the HLT scalars class.
22///
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
25/// using a hash map.
26
27#include "AliHLTScalars.h"
28#include "TString.h"
1d089207 29#include "AliHLTLogging.h" // HLT logging
30#include "TClass.h" // for Class_Name macro for logging
9cb24db9 31#include "TIterator.h"
32#include "Riostream.h"
33#include <cassert>
34
35ClassImp(AliHLTScalars);
36ClassImp(AliHLTScalars::AliScalar);
37
38
39AliHLTScalars::AliHLTScalars() :
40 TObject(),
41 fScalars(AliHLTScalars::AliScalar::Class(), 128),
42 fMap(TCollection::kInitHashTableCapacity, 2)
43{
44 // Default constructor.
45
46 fMap.SetOwner(kFALSE);
47}
48
49
50AliHLTScalars::AliHLTScalars(const AliHLTScalars& obj) :
51 TObject(obj),
52 fScalars(obj.fScalars),
53 fMap(obj.fMap.GetSize(), obj.fMap.GetRehashLevel())
54{
55 // Copy constructor performs a deep copy.
56
57 fMap.SetOwner(kFALSE);
58 fMap.AddAll(&fScalars);
59}
60
61
62AliHLTScalars::AliHLTScalars(const TClass* cl, Int_t initSize) :
63 TObject(),
64 fScalars(cl, initSize),
65 fMap(TCollection::kInitHashTableCapacity, 2)
66{
67 // Constructor to be able to specify a custom class for the fScalars TClonesArray object.
68
69 fMap.SetOwner(kFALSE);
70}
71
72
73AliHLTScalars::~AliHLTScalars()
74{
75 // Default destructor.
76
77 Clear();
78}
79
80
5df17fc7 81AliHLTScalars::AliScalar* AliHLTScalars::NewScalar(UInt_t i, const char* name, const char* description, Double_t value)
9cb24db9 82{
5df17fc7 83 // Creates a new scalar object.
84
85 return new (fScalars[i]) AliScalar(name, description, value);
86}
87
88
89const AliHLTScalars::AliScalar& AliHLTScalars::Sentinel() const
90{
91 // Returns an empty sentinel object.
92
93 static AliHLTScalars::AliScalar sentinel;
94 return sentinel;
95}
96
9cb24db9 97
5df17fc7 98bool AliHLTScalars::Add(AliScalar*& scalar, const char* name, const char* description, Double_t value)
99{
100 // Adds a new scalar (internal version).
101
102 scalar = static_cast<AliScalar*>( fMap.FindObject(name) );
9cb24db9 103 bool exists = scalar != NULL;
104 if (not exists)
105 {
5df17fc7 106 scalar = NewScalar(fScalars.GetEntriesFast(), name, description, value);
9cb24db9 107 fMap.Add(scalar);
108 }
109 else
110 {
111 scalar->Value(value);
112 }
113 return exists;
114}
115
116
5df17fc7 117bool AliHLTScalars::Add(const char* name, const char* description, Double_t value)
118{
119 // Adds a new scalar.
120
121 AliScalar* scalar = NULL;
122 return Add(scalar, name, description, value);
123}
124
125
9cb24db9 126bool AliHLTScalars::Remove(const char* name)
127{
128 // Removes a scalar from the list.
129
130 TNamed x(name, "");
131 TObject* scalar = fMap.Remove(&x);
132 bool existed = scalar != NULL;
133 if (existed)
134 {
135 fScalars.Remove(scalar);
136 fScalars.Compress();
137 }
138 return existed;
139}
140
141
142const AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name) const
143{
144 // Fetch the named scalar object.
145
146 const TObject* scalar = fMap.FindObject(name);
147 if (scalar != NULL)
148 {
149 return *static_cast<const AliScalar*>(scalar);
150 }
151 else
152 {
5df17fc7 153 return Sentinel();
9cb24db9 154 }
155}
156
157
158AliHLTScalars::AliScalar& AliHLTScalars::GetScalar(const char* name)
159{
160 // Fetch the named scalar object for editing.
161
162 TObject* scalar = fMap.FindObject(name);
163 if (scalar == NULL)
164 {
5df17fc7 165 scalar = NewScalar(fScalars.GetEntriesFast(), name, "", 0);
9cb24db9 166 fMap.Add(scalar);
167 }
168 return *static_cast<AliScalar*>(scalar);
169}
170
171
172const AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n) const
173{
174 // Fetch the n'th scalar object.
175
176 if (n < NumberOfScalars())
177 {
178 const TObject* scalar = fScalars.UncheckedAt(Int_t(n));
179 return *static_cast<const AliScalar*>(scalar);
180 }
181 else
182 {
5df17fc7 183 return Sentinel();
9cb24db9 184 }
185}
186
187
188AliHLTScalars::AliScalar& AliHLTScalars::GetScalarN(UInt_t n)
189{
190 // Fetch the n'th scalar object for editing.
191
192 TObject* scalar = NULL;
193 if (n < NumberOfScalars())
194 {
195 scalar = fScalars.UncheckedAt(Int_t(n));
196 }
197 else
198 {
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)
203 {
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)
208 {
209 UInt_t m = 0;
210 do
211 {
212 nameToUse = Form("Scalar%d_%d", i, m++);
213 }
214 while (FindObject(nameToUse.Data()) != NULL);
215 }
5df17fc7 216 scalar = NewScalar(i, nameToUse.Data(), "", 0);
9cb24db9 217 fMap.Add(scalar);
218 }
219 }
220 return *static_cast<AliScalar*>(scalar);
221}
222
223
224void AliHLTScalars::Reset()
225{
5df17fc7 226 // Sets all the scalar values to zero.
9cb24db9 227
228 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
229 {
230 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
231 scalar->Value(0);
232 }
233}
234
235
236void AliHLTScalars::Clear(Option_t* option)
237{
238 // Clears the array of scalars.
239
240 fMap.Clear();
241 fScalars.Delete(option);
242}
243
244
245void AliHLTScalars::Copy(TObject& object) const
246{
247 // Performs a deep copy.
248
249 if (object.IsA() != AliHLTScalars::Class())
250 {
1d089207 251 AliHLTLogging log;
252 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot copy to an object of type '%s'.", object.ClassName());
9cb24db9 253 return;
254 }
255 AliHLTScalars* obj = static_cast<AliHLTScalars*>(&object);
256 obj->operator = (*this);
257}
258
259
260void AliHLTScalars::Print(Option_t* option) const
261{
262 // Prints the HLT trigger scalars.
263
264 TIter next(&fScalars);
265 const AliScalar* scalar = NULL;
266
267 TString opt = option;
268 if (opt == "compact")
269 {
270 scalar = static_cast<const AliScalar*>(next());
271 if (scalar != NULL) cout << scalar->Value();
272 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
273 {
274 cout << ", " << scalar->Value();
275 }
276 cout << endl;
277 return;
278 }
279
280 // Calculate the maximum field width required to keep things aligned.
281 int fieldwidth = 0;
282 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
283 {
284 int length = strlen(scalar->Name()) + strlen(scalar->Description()) + 3;
285 if (length > fieldwidth) fieldwidth = length;
286 }
287 if (fieldwidth > 80) fieldwidth = 80;
288
289 cout << "HLT scalars:" << endl;
290 next.Reset();
291 while ((scalar = static_cast<const AliScalar*>(next())) != NULL)
292 {
293 TString str = scalar->Description();
294 str += " (";
295 str += scalar->Name();
296 str += ")";
297 cout << setw(fieldwidth) << str.Data() << setw(0)
298 << " = " << scalar->Value() << setw(0)
299 << endl;
300 }
5df17fc7 301 if (fScalars.GetEntriesFast() == 0) cout << "(none)" << endl;
9cb24db9 302}
303
304
305AliHLTScalars& AliHLTScalars::operator = (const AliHLTScalars& obj)
306{
307 // Performs a deep copy.
308
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)
313 {
314 const AliScalar* x = static_cast<const AliScalar*>(obj.fScalars.UncheckedAt(i));
315 new (fScalars[i]) AliScalar(*x);
316 }
317 fMap.AddAll(&fScalars);
318 return *this;
319}
320
321
322Bool_t AliHLTScalars::IsEqual(const TObject *obj) const
323{
324 // Checks if two sets of scalar lists have the same scalars.
325
326 assert(obj != NULL);
327 if (obj->IsA()->GetBaseClass(AliHLTScalars::Class()) == NULL)
328 {
1d089207 329 AliHLTLogging log;
330 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot compare object of type '%s'' with an object of type '%s'.",
9cb24db9 331 this->ClassName(), obj->ClassName()
1d089207 332 );
9cb24db9 333 return kFALSE;
334 }
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)
340 {
341 if (rhs->fScalars.FindObject(scalar->Name()) == NULL) return kFALSE;
342 }
343 return kTRUE;
344}
345
346
347bool AliHLTScalars::operator == (const AliHLTScalars& obj) const
348{
349 // Compares two scalar lists to see that they have the same scalar values.
350
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)
355 {
356 const AliScalar* b = static_cast<const AliScalar*>(
357 obj.fScalars.FindObject(a->Name())
358 );
359 if (b == NULL) return false;
360 if (a->Value() != b->Value()) return false;
361 }
362 return true;
363}
364
365
366void AliHLTScalars::AliScalar::Copy(TObject& object) const
367{
368 // Performs a deep copy.
369
370 if (object.IsA() != AliHLTScalars::AliScalar::Class())
371 {
1d089207 372 AliHLTLogging log;
373 log.LoggingVarargs(kHLTLogError, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , "Cannot copy to an object of type '%s'.", object.ClassName());
9cb24db9 374 return;
375 }
376 AliHLTScalars::AliScalar* obj = static_cast<AliHLTScalars::AliScalar*>(&object);
377 *obj = *this;
378}