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 AliHLTMuonSpectroScalars.cxx
19 /// @author Artur Szostak <artursz@iafrica.com>
21 /// @brief Implementation of the muon spectrometer trigger scalars.
23 /// This implements the trigger scalars for the muon spectrometer that can be used
24 /// in the HLT global trigger and/or added to the AliESDEvent.
26 #include "AliHLTMuonSpectroScalars.h"
28 #include "TObjArray.h"
29 #include "TObjString.h"
31 #include "Riostream.h"
36 ClassImp(AliHLTMuonSpectroScalars);
37 ClassImp(AliHLTMuonSpectroScalars::AliScalar);
40 AliHLTMuonSpectroScalars::AliHLTMuonSpectroScalars() :
42 fScalars(AliHLTMuonSpectroScalars::AliScalar::Class(), 128),
46 // Default constructor.
48 fIndex.SetOwner(kFALSE);
52 AliHLTMuonSpectroScalars::AliHLTMuonSpectroScalars(const AliHLTMuonSpectroScalars& obj) :
54 fScalars(obj.fScalars),
56 fIndexValid(obj.fIndexValid)
58 // Copy constructor performs a deep copy.
60 fIndex.SetOwner(kFALSE);
64 AliHLTMuonSpectroScalars::~AliHLTMuonSpectroScalars()
66 // Default destructor.
72 void AliHLTMuonSpectroScalars::Add(const char* name, const char* description, Double_t value)
76 new (fScalars[fScalars.GetEntriesFast()]) AliScalar(value, name, description);
77 fIndexValid = false; // Invalidate the index.
81 bool AliHLTMuonSpectroScalars::Exists(const char* name) const
83 // Checks if the scalar exists or not.
85 if (not fIndexValid) MakeIndex();
86 AliScalar tmpobj(0, name, "");
87 Int_t n = fIndex.BinarySearch(&tmpobj);
88 if (n == -1) return false;
93 AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalarN(UInt_t n)
95 // Fetch the n'th scalar object.
97 if (n >= NumberOfScalars())
99 AliError(Form("Value 'n' is out of bounds. Should be in the range [0..%d].", NumberOfScalars()-1));
102 return static_cast<AliScalar*>( fScalars.UncheckedAt(Int_t(n)) );
106 const AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalarN(UInt_t n) const
108 // Fetch the n'th scalar object.
110 if (n >= NumberOfScalars())
112 AliError(Form("Value 'n' is out of bounds. Should be in the range [0..%d].", NumberOfScalars()-1));
115 return static_cast<const AliScalar*>( fScalars.UncheckedAt(Int_t(n)) );
119 AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalar(const char* name)
121 // Fetch the named scalar object.
123 if (not fIndexValid) MakeIndex();
124 AliScalar tmpobj(0, name, "");
125 Int_t n = fIndex.BinarySearch(&tmpobj);
128 AliError(Form("Scalar '%s' could not be found.", name));
131 return static_cast<AliScalar*>( fIndex.UncheckedAt(n) );
135 const AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalar(const char* name) const
137 // Fetch the named scalar object.
139 if (not fIndexValid) MakeIndex();
140 AliScalar tmpobj(0, name, "");
141 Int_t n = fIndex.BinarySearch(&tmpobj);
144 AliError(Form("Scalar '%s' could not be found.", name));
147 return static_cast<const AliScalar*>( fIndex.UncheckedAt(n) );
151 Double_t AliHLTMuonSpectroScalars::GetN(UInt_t n) const
153 // Fetches the n'th scalar value.
155 const AliScalar* scalar = GetScalarN(n);
156 if (scalar == NULL) return 0;
157 return scalar->Value();
161 Double_t AliHLTMuonSpectroScalars::Get(const char* name) const
163 // Fetches the n'th scalar value.
165 const AliScalar* scalar = GetScalar(name);
166 if (scalar == NULL) return 0;
167 return scalar->Value();
171 bool AliHLTMuonSpectroScalars::SetN(UInt_t n, Double_t value)
173 // Sets the n'th scalar value.
175 AliScalar* scalar = GetScalarN(n);
176 if (scalar == NULL) return false;
177 scalar->Value(value);
182 bool AliHLTMuonSpectroScalars::Set(const char* name, Double_t value)
184 // Sets the named scalar value.
186 AliScalar* scalar = GetScalar(name);
187 if (scalar == NULL) return false;
188 scalar->Value(value);
193 bool AliHLTMuonSpectroScalars::IncrementN(UInt_t n, UInt_t count)
195 // Increments the n'th scalar by a value of 'count'.
197 AliScalar* scalar = GetScalarN(n);
198 if (scalar == NULL) return false;
199 scalar->Increment(count);
204 bool AliHLTMuonSpectroScalars::Increment(const char* name, UInt_t count)
206 // Increments the named scalar by a value of 'count'.
208 AliScalar* scalar = GetScalar(name);
209 if (scalar == NULL) return false;
210 scalar->Increment(count);
215 const char* AliHLTMuonSpectroScalars::Name(UInt_t n) const
217 // Fetches the n'th scalar's name.
219 const AliScalar* scalar = GetScalarN(n);
220 if (scalar == NULL) return NULL;
221 return scalar->Name();
225 const char* AliHLTMuonSpectroScalars::Description(UInt_t n) const
227 // Fetches the n'th scalar's description.
229 const AliScalar* scalar = GetScalarN(n);
230 if (scalar == NULL) return NULL;
231 return scalar->Description();
235 void AliHLTMuonSpectroScalars::Reset()
237 // Sets all the scalar values to zero.
239 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
241 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
247 void AliHLTMuonSpectroScalars::Clear(Option_t* option)
249 // Clears the array of scalars.
251 fScalars.Delete(option);
256 void AliHLTMuonSpectroScalars::Copy(TObject& object) const
258 // Performs a deep copy.
260 if (object.IsA() != AliHLTMuonSpectroScalars::Class())
262 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
265 AliHLTMuonSpectroScalars* obj = static_cast<AliHLTMuonSpectroScalars*>(&object);
270 TObject* AliHLTMuonSpectroScalars::FindObject(const char* name) const
272 // Finds the scalar object by name.
276 AliScalar tmpobj(0, name, "");
277 Int_t n = fIndex.BinarySearch(&tmpobj);
278 if (n != -1) return fIndex.UncheckedAt(n);
282 return fScalars.FindObject(name);
288 TObject* AliHLTMuonSpectroScalars::FindObject(const TObject* obj) const
290 // Finds the scalar object with the same name as obj->GetName().
292 return FindObject(obj->GetName());
296 void AliHLTMuonSpectroScalars::Print(Option_t* option) const
298 // Prints the muon spectrometer's HLT trigger scalars.
300 TString opt = option;
301 if (opt == "compact")
303 if (NumberOfScalars() > 0)
305 for (UInt_t i = 1; i < NumberOfScalars(); ++i) cout << ", " << GetN(i);
310 // Calculate the maximum field width required to keep things aligned.
312 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
314 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
315 int length = strlen(scalar->Description());
316 if (length > fieldwidth) fieldwidth = length;
318 if (fieldwidth > 80) fieldwidth = 80;
320 cout << "HLT muon spectrometer trigger scalars:" << endl;
321 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
323 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
324 cout << setw(fieldwidth) << scalar->Description() << setw(0)
325 << " = " << scalar->Value() << endl;
327 if (fScalars.GetEntriesFast() == 0) cout << "(none)" << endl;
331 AliHLTMuonSpectroScalars& AliHLTMuonSpectroScalars::operator = (const AliHLTMuonSpectroScalars& obj)
333 // Performs a deep copy.
335 if (this == &obj) return *this;
337 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
339 AliScalar* scalar = static_cast<AliScalar*>( obj.fScalars.UncheckedAt(i) );
340 Add(scalar->Name(), scalar->Description(), scalar->Value());
347 bool AliHLTMuonSpectroScalars::operator == (const AliHLTMuonSpectroScalars& obj) const
349 // Compares two scalar objects.
351 if (not fIndexValid) MakeIndex();
352 if (fScalars.GetEntriesFast() != obj.fScalars.GetEntriesFast()) return false;
353 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
355 AliScalar* scalar1 = static_cast<AliScalar*>( obj.fScalars.UncheckedAt(i) );
356 Int_t n = fIndex.BinarySearch(scalar1);
357 if (n == -1) return false;
358 AliScalar* scalar2 = static_cast<AliScalar*>( fIndex.UncheckedAt(n) );
359 if (scalar1->Value() != scalar2->Value()) return false;
365 void AliHLTMuonSpectroScalars::MakeIndex() const
367 // Makes the index fIndex required for faster searching in fScalars.
370 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
372 fIndex.Add(fScalars.UncheckedAt(i));
379 void AliHLTMuonSpectroScalars::AliScalar::Copy(TObject& object) const
381 // Performs a deep copy.
383 if (object.IsA() != AliHLTMuonSpectroScalars::AliScalar::Class())
385 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
388 AliHLTMuonSpectroScalars::AliScalar* obj = static_cast<AliHLTMuonSpectroScalars::AliScalar*>(&object);