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