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"
33 ClassImp(AliHLTMuonSpectroScalars);
34 ClassImp(AliHLTMuonSpectroScalars::AliScalar);
37 AliHLTMuonSpectroScalars::AliHLTMuonSpectroScalars() :
39 fScalars(AliHLTMuonSpectroScalars::AliScalar::Class(), 128),
43 // Default constructor.
45 fIndex.SetOwner(kFALSE);
49 AliHLTMuonSpectroScalars::AliHLTMuonSpectroScalars(const AliHLTMuonSpectroScalars& obj) :
51 fScalars(obj.fScalars),
53 fIndexValid(obj.fIndexValid)
55 // Copy constructor performs a deep copy.
57 fIndex.SetOwner(kFALSE);
61 AliHLTMuonSpectroScalars::~AliHLTMuonSpectroScalars()
63 // Default destructor.
69 void AliHLTMuonSpectroScalars::Add(const char* name, const char* description, Double_t value)
73 new (fScalars[fScalars.GetEntriesFast()]) AliScalar(value, name, description);
74 fIndexValid = false; // Invalidate the index.
78 bool AliHLTMuonSpectroScalars::Exists(const char* name) const
80 // Checks if the scalar exists or not.
82 if (not fIndexValid) MakeIndex();
83 AliScalar tmpobj(0, name, "");
84 Int_t n = fIndex.BinarySearch(&tmpobj);
85 if (n == -1) return false;
90 AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalarN(UInt_t n)
92 // Fetch the n'th scalar object.
94 if (n >= NumberOfScalars())
96 AliError(Form("Value 'n' is out of bounds. Should be in the range [0..%d].", NumberOfScalars()-1));
99 return static_cast<AliScalar*>( fScalars.UncheckedAt(Int_t(n)) );
103 const AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalarN(UInt_t n) const
105 // Fetch the n'th scalar object.
107 if (n >= NumberOfScalars())
109 AliError(Form("Value 'n' is out of bounds. Should be in the range [0..%d].", NumberOfScalars()-1));
112 return static_cast<const AliScalar*>( fScalars.UncheckedAt(Int_t(n)) );
116 AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalar(const char* name)
118 // Fetch the named scalar object.
120 if (not fIndexValid) MakeIndex();
121 AliScalar tmpobj(0, name, "");
122 Int_t n = fIndex.BinarySearch(&tmpobj);
125 AliError(Form("Scalar '%s' could not be found.", name));
128 return static_cast<AliScalar*>( fIndex.UncheckedAt(n) );
132 const AliHLTMuonSpectroScalars::AliScalar* AliHLTMuonSpectroScalars::GetScalar(const char* name) const
134 // Fetch the named scalar object.
136 if (not fIndexValid) MakeIndex();
137 AliScalar tmpobj(0, name, "");
138 Int_t n = fIndex.BinarySearch(&tmpobj);
141 AliError(Form("Scalar '%s' could not be found.", name));
144 return static_cast<const AliScalar*>( fIndex.UncheckedAt(n) );
148 Double_t AliHLTMuonSpectroScalars::GetN(UInt_t n) const
150 // Fetches the n'th scalar value.
152 const AliScalar* scalar = GetScalarN(n);
153 if (scalar == NULL) return 0;
154 return scalar->Value();
158 Double_t AliHLTMuonSpectroScalars::Get(const char* name) const
160 // Fetches the n'th scalar value.
162 const AliScalar* scalar = GetScalar(name);
163 if (scalar == NULL) return 0;
164 return scalar->Value();
168 bool AliHLTMuonSpectroScalars::SetN(UInt_t n, Double_t value)
170 // Sets the n'th scalar value.
172 AliScalar* scalar = GetScalarN(n);
173 if (scalar == NULL) return false;
174 scalar->Value(value);
179 bool AliHLTMuonSpectroScalars::Set(const char* name, Double_t value)
181 // Sets the named scalar value.
183 AliScalar* scalar = GetScalar(name);
184 if (scalar == NULL) return false;
185 scalar->Value(value);
190 bool AliHLTMuonSpectroScalars::IncrementN(UInt_t n, UInt_t count)
192 // Increments the n'th scalar by a value of 'count'.
194 AliScalar* scalar = GetScalarN(n);
195 if (scalar == NULL) return false;
196 scalar->Increment(count);
201 bool AliHLTMuonSpectroScalars::Increment(const char* name, UInt_t count)
203 // Increments the named scalar by a value of 'count'.
205 AliScalar* scalar = GetScalar(name);
206 if (scalar == NULL) return false;
207 scalar->Increment(count);
212 const char* AliHLTMuonSpectroScalars::Name(UInt_t n) const
214 // Fetches the n'th scalar's name.
216 const AliScalar* scalar = GetScalarN(n);
217 if (scalar == NULL) return NULL;
218 return scalar->Name();
222 const char* AliHLTMuonSpectroScalars::Description(UInt_t n) const
224 // Fetches the n'th scalar's description.
226 const AliScalar* scalar = GetScalarN(n);
227 if (scalar == NULL) return NULL;
228 return scalar->Description();
232 void AliHLTMuonSpectroScalars::Reset()
234 // Sets all the scalar values to zero.
236 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
238 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
244 void AliHLTMuonSpectroScalars::Clear(Option_t* option)
246 // Clears the array of scalars.
248 fScalars.Delete(option);
253 void AliHLTMuonSpectroScalars::Copy(TObject& object) const
255 // Performs a deep copy.
257 if (object.IsA() != AliHLTMuonSpectroScalars::Class())
259 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
262 AliHLTMuonSpectroScalars* obj = static_cast<AliHLTMuonSpectroScalars*>(&object);
267 TObject* AliHLTMuonSpectroScalars::FindObject(const char* name) const
269 // Finds the scalar object by name.
273 AliScalar tmpobj(0, name, "");
274 Int_t n = fIndex.BinarySearch(&tmpobj);
275 if (n != -1) return fIndex.UncheckedAt(n);
279 return fScalars.FindObject(name);
285 TObject* AliHLTMuonSpectroScalars::FindObject(const TObject* obj) const
287 // Finds the scalar object with the same name as obj->GetName().
289 return FindObject(obj->GetName());
293 void AliHLTMuonSpectroScalars::Print(Option_t* option) const
295 // Prints the muon spectrometer's HLT trigger scalars.
297 TString opt = option;
298 if (opt == "compact")
300 if (NumberOfScalars() > 0)
302 for (UInt_t i = 1; i < NumberOfScalars(); ++i) cout << ", " << GetN(i);
307 // Calculate the maximum field width required to keep things aligned.
309 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
311 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
312 int length = strlen(scalar->Description());
313 if (length > fieldwidth) fieldwidth = length;
315 if (fieldwidth > 80) fieldwidth = 80;
317 cout << "HLT muon spectrometer trigger scalars:" << endl;
318 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
320 AliScalar* scalar = static_cast<AliScalar*>( fScalars.UncheckedAt(i) );
321 cout << setw(fieldwidth) << scalar->Description() << setw(0)
322 << " = " << scalar->Value() << endl;
324 if (fScalars.GetEntriesFast() == 0) cout << "(none)" << endl;
328 AliHLTMuonSpectroScalars& AliHLTMuonSpectroScalars::operator = (const AliHLTMuonSpectroScalars& obj)
330 // Performs a deep copy.
332 if (this == &obj) return *this;
334 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
336 AliScalar* scalar = static_cast<AliScalar*>( obj.fScalars.UncheckedAt(i) );
337 Add(scalar->Name(), scalar->Description(), scalar->Value());
344 bool AliHLTMuonSpectroScalars::operator == (const AliHLTMuonSpectroScalars& obj) const
346 // Compares two scalar objects.
348 if (not fIndexValid) MakeIndex();
349 if (fScalars.GetEntriesFast() != obj.fScalars.GetEntriesFast()) return false;
350 for (Int_t i = 0; i < obj.fScalars.GetEntriesFast(); ++i)
352 AliScalar* scalar1 = static_cast<AliScalar*>( obj.fScalars.UncheckedAt(i) );
353 Int_t n = fIndex.BinarySearch(scalar1);
354 if (n == -1) return false;
355 AliScalar* scalar2 = static_cast<AliScalar*>( fIndex.UncheckedAt(n) );
356 if (scalar1->Value() != scalar2->Value()) return false;
362 void AliHLTMuonSpectroScalars::MakeIndex() const
364 // Makes the index fIndex required for faster searching in fScalars.
367 for (Int_t i = 0; i < fScalars.GetEntriesFast(); ++i)
369 fIndex.Add(fScalars.UncheckedAt(i));
376 void AliHLTMuonSpectroScalars::AliScalar::Copy(TObject& object) const
378 // Performs a deep copy.
380 if (object.IsA() != AliHLTMuonSpectroScalars::AliScalar::Class())
382 AliError(Form("Cannot copy to an object of type '%s'.", object.ClassName()));
385 AliHLTMuonSpectroScalars::AliScalar* obj = static_cast<AliHLTMuonSpectroScalars::AliScalar*>(&object);