3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
10 //* Permission to use, copy, modify and distribute this software and its *
11 //* documentation strictly for non-commercial purposes is hereby granted *
12 //* without fee, provided that the above copyright notice appears in all *
13 //* copies and that both the copyright notice and this permission notice *
14 //* appear in the supporting documentation. The authors make no claims *
15 //* about the suitability of this software for any purpose. It is *
16 //* provided "as is" without express or implied warranty. *
17 //**************************************************************************
19 // @file AliHLTCTPData.cxx
20 // @author Matthias Richter
22 // @brief Container for CTP trigger classes and counters
25 #include "AliHLTCTPData.h"
27 #include "TObjString.h"
29 #include "AliHLTComponent.h"
30 #include "AliHLTCDHWrapper.h"
35 /** ROOT macro for the implementation of ROOT specific class methods */
36 ClassImp(AliHLTCTPData)
38 AliHLTCTPData::AliHLTCTPData()
39 : TNamed("AliHLTCTPData", "HLT counters for the CTP")
43 , fClassIds(AliHLTReadoutList::Class(), gkNCTPTriggerClasses)
44 , fCounters(gkNCTPTriggerClasses)
48 // see header file for class documentation
50 // refer to README to build package
52 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
55 AliHLTCTPData::AliHLTCTPData(const char* parameter)
56 : TNamed("AliHLTCTPData", "HLT counters for the CTP")
60 , fClassIds(AliHLTReadoutList::Class(), gkNCTPTriggerClasses)
61 , fCounters(gkNCTPTriggerClasses)
64 // constructor, init the CTP trigger classes
65 InitCTPTriggerClasses(parameter);
68 AliHLTCTPData::~AliHLTCTPData()
74 AliHLTCTPData::AliHLTCTPData(const AliHLTCTPData& src)
75 : TNamed(src.GetName(), src.GetTitle())
78 , fTriggers(src.fTriggers)
79 , fClassIds(src.fClassIds)
80 , fCounters(src.Counters())
87 AliHLTCTPData& AliHLTCTPData::operator=(const AliHLTCTPData& src)
89 // assignment operator, clone content
91 SetName(src.GetName());
92 SetTitle(src.GetTitle());
95 fClassIds.ExpandCreate(gkNCTPTriggerClasses);
96 for (int i=0; i<gkNCTPTriggerClasses; i++) {
97 if (i>src.fClassIds.GetLast()) break;
98 ((TNamed*)fClassIds.At(i))->SetName(src.fClassIds.At(i)->GetName());
99 ((TNamed*)fClassIds.At(i))->SetTitle(src.fClassIds.At(i)->GetTitle());
101 fCounters=src.Counters();
108 int AliHLTCTPData::Add(const AliHLTCTPData& src, int factor, int &skipped)
110 // see header file for class documentation
113 for (int i=0; i<gkNCTPTriggerClasses; i++) {
115 c=fClassIds.At(i)->GetName();
116 if (c.IsNull()) continue;
117 if (c.CompareTo(src.fClassIds.At(i)->GetName())==0) {
118 fCounters[i]+=factor*src.Counter(i);
126 AliHLTCTPData& AliHLTCTPData::operator += (const AliHLTCTPData& src)
128 // see header file for class documentation
130 int nofInconsistencies=0;
131 Add(src, 1, nofInconsistencies);
132 if (nofInconsistencies>0) {
133 HLTError("Inconsistent operants: skipping %d of %d CTP classes for operation", nofInconsistencies, gkNCTPTriggerClasses);
138 AliHLTCTPData& AliHLTCTPData::operator -= (const AliHLTCTPData& src)
140 // see header file for class documentation
142 int nofInconsistencies=0;
143 Add(src, -1, nofInconsistencies);
144 if (nofInconsistencies>0) {
145 HLTError("Inconsistent operants: skipping %d of %d CTP classes for operation", nofInconsistencies, gkNCTPTriggerClasses);
150 AliHLTCTPData AliHLTCTPData::operator + (const AliHLTCTPData& src) const
152 // see header file for class documentation
154 AliHLTCTPData result(*this);
159 AliHLTCTPData AliHLTCTPData::operator - (const AliHLTCTPData& src) const
161 // see header file for class documentation
163 AliHLTCTPData result(*this);
168 int AliHLTCTPData::InitCTPTriggerClasses(const char* ctpString)
170 // see header file for function documentation
171 if (!ctpString) return -EINVAL;
173 HLTDebug("Parameter: %s", ctpString);
177 fClassIds.ExpandCreate(gkNCTPTriggerClasses);
179 // general format of the CTP_TRIGGER_CLASS parameter
180 // <bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,<bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,...
181 HLTDebug(": %s", ctpString);
182 TString string=ctpString;
183 if (string.BeginsWith("CTP_TRIGGER_CLASS=")) string.ReplaceAll("CTP_TRIGGER_CLASS=", "");
184 TObjArray* classEntries=string.Tokenize(",");
186 enum {kBit=0, kName, kDetectors};
187 for (int i=0; i<classEntries->GetEntriesFast(); i++) {
188 TString entry=((TObjString*)classEntries->At(i))->GetString();
189 TObjArray* entryParams=entry.Tokenize(":");
191 if (entryParams->GetEntriesFast()==3 &&
192 (((TObjString*)entryParams->At(kBit))->GetString()).IsDigit()) {
193 int index=(((TObjString*)entryParams->At(kBit))->GetString()).Atoi();
194 if (index<gkNCTPTriggerClasses) {
195 AliHLTReadoutList* pCTPClass=dynamic_cast<AliHLTReadoutList*>(fClassIds.At(index));
198 pCTPClass->SetTitle("CTP Class");
199 pCTPClass->SetName((((TObjString*)entryParams->At(kName))->GetString()).Data());
200 TObjArray* detectors=(((TObjString*)entryParams->At(kDetectors))->GetString()).Tokenize("-");
202 for (int dix=0; dix<detectors->GetEntriesFast(); dix++) {
203 if (!(((TObjString*)detectors->At(dix))->GetString()).IsDigit()) {
204 HLTError("invalid detector list format: trigger class entry %s", entry.Data());
207 // see AliHLTReadoutList::EDetectorId for defines of detectors
208 pCTPClass->Enable(0x1<<(((TObjString*)detectors->At(dix))->GetString()).Atoi());
215 // the trigger bitfield is fixed to 100 bits (gkNCTPTriggerClasses)
216 HLTError("invalid trigger class entry %s, index width of trigger bitfield exceeded (%d)", entry.Data(), gkNCTPTriggerClasses);
219 HLTError("invalid trigger class entry %s", entry.Data());
233 AliHLTTriggerMask_t AliHLTCTPData::ActiveTriggers(const AliHLTComponentTriggerData& trigData)
235 // extract active triggers from the trigger data
236 AliHLTCDHWrapper cdh;
237 if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL) != 0) return 0x0;
238 if ((cdh.GetL1TriggerMessage() & 0x1) == 0x1) return 0x0; // invalid for software triggers.
240 AliHLTTriggerMask_t triggerLow(cdh.GetTriggerClasses()); //low bits
241 AliHLTTriggerMask_t triggerHigh(cdh.GetTriggerClassesNext50()); // high bits
243 return triggerLow | (triggerHigh << 50);
246 bool AliHLTCTPData::EvaluateCTPTriggerClass(const char* expression, const AliHLTComponentTriggerData& trigData) const
248 // see header file for function documentation
250 AliHLTCDHWrapper cdh;
251 if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true) != 0) return false;
252 if ((cdh.GetL1TriggerMessage() & 0x1) == 0x1) return false; // invalid for software triggers.
254 AliHLTTriggerMask_t triggerMask(cdh.GetTriggerClasses());
255 AliHLTTriggerMask_t triggerHigh(cdh.GetTriggerClassesNext50());
256 triggerMask |= (triggerHigh << 50);
258 if (fMask!=0 && (triggerMask & fMask)==0) {
259 AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
260 HLTWarning("invalid trigger mask %s, unknown CTP trigger, initialized %s",
261 TriggerMaskToString(triggerMask).c_str(), TriggerMaskToString(fMask).c_str() );
262 for (int i=0; i<evtData->fCommonHeaderWordCnt; i++) HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
266 return EvaluateCTPTriggerClass(expression, triggerMask);
269 bool AliHLTCTPData::EvaluateCTPTriggerClass(const char* expression, AliHLTTriggerMask_t triggerMask) const
271 // see header file for function documentation
273 // use a TFormula to interprete the expression
274 // all classname are replaced by '[n]' which means the n'th parameter in the formula
275 // the parameters are set to 0 or 1 depending on the bit in the trigger mask
276 const vector<unsigned> *pMap=&fMap;
277 vector<unsigned> tmp;
278 if (fMap.size()==0 && fClassIds.GetLast()>=0) {
279 // read map into temporary array and use it
282 static bool suppressWarning=false;
283 if (!suppressWarning) HLTWarning("map not yet initialized, creating local map (slow), suppressing further warnings");
284 suppressWarning=true;
286 vector<Double_t> par;
287 TString condition=expression;
288 for (unsigned index=0; index<pMap->size(); index++) {
289 const char* className=Name((*pMap)[index]);
290 if (className && strlen(className)>0) {
291 //HLTDebug("checking trigger class %s", className.Data());
292 if (condition.Contains(className)) {
293 TString replace; replace.Form("[%d]", (int)par.size());
294 //HLTDebug("replacing %s with %s in \"%s\"", className.Data(), replace.Data(), condition.Data());
295 condition.ReplaceAll(className, replace);
296 if ( triggerMask.test((*pMap)[index]) ) par.push_back(1.0);
297 else par.push_back(0.0);
302 TFormula form("trigger expression", condition);
303 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,99,0)
304 if (form.IsValid()!=0) {
306 if (form.Compile()!=0) {
308 HLTError("invalid expression %s", expression);
311 if (form.EvalPar(&par[0], &par[0])>0.5) return true;
315 void AliHLTCTPData::ResetCounters()
317 // see header file for function documentation
318 fCounters.Set(gkNCTPTriggerClasses);
322 int AliHLTCTPData::Index(const char* name) const
324 // see header file for function documentation
325 TObject* obj=fClassIds.FindObject(name);
326 return obj!=NULL?fClassIds.IndexOf(obj):-1;
329 int AliHLTCTPData::CheckTrigger(const char* name) const
331 // check status of a trigger class
332 int index=Index(name);
333 if (index<0) return index;
334 return ( fTriggers.test(index) ? 1 : 0 );
337 void AliHLTCTPData::Increment(const char* classIds)
339 // see header file for function documentation
340 TString string=classIds;
341 TObjArray* classEntries=string.Tokenize(",");
343 for (int i=0; i<classEntries->GetEntriesFast(); i++) {
344 int index=Index(((TObjString*)classEntries->At(i))->GetString().Data());
345 if (index>=0 && index<fCounters.GetSize()) fCounters[index]++;
351 void AliHLTCTPData::Increment(AliHLTTriggerMask_t triggerPattern)
353 // see header file for function documentation
354 AliHLTTriggerMask_t pattern=triggerPattern&fMask;
355 for (int i=0; i<fCounters.GetSize(); i++) {
356 if (!pattern.test(i)) continue;
361 void AliHLTCTPData::Increment(int classIdx)
363 // see header file for function documentation
364 if (classIdx<fCounters.GetSize() &&
365 fMask.test(classIdx)) {
366 fCounters[classIdx]++;
371 int AliHLTCTPData::Increment(AliHLTComponentTriggerData& trigData)
373 // see header file for function documentation
374 AliHLTCDHWrapper cdh;
375 int result = AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true);
376 if (result != 0) return result;
377 if ((cdh.GetL1TriggerMessage() & 0x1) == 0x1) return 0; // invalid for software triggers.
379 AliHLTTriggerMask_t triggerMask(cdh.GetTriggerClasses());
380 AliHLTTriggerMask_t triggerHigh(cdh.GetTriggerClassesNext50());
381 triggerMask |= (triggerHigh << 50);
383 if (fMask.any() && (triggerMask & fMask).none()) {
384 AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
385 HLTWarning("invalid trigger mask %s, unknown CTP trigger, initialized %s",
386 TriggerMaskToString(triggerMask).c_str(), TriggerMaskToString(fMask).c_str());
387 for (int i=0; i<evtData->fCommonHeaderWordCnt; i++)
388 HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
390 Increment(triggerMask);
394 AliHLTUInt64_t AliHLTCTPData::Counter(int index) const
396 // see header file for function documentation
397 if (index>=0 && index<Counters().GetSize()) return Counters()[index];
401 AliHLTUInt64_t AliHLTCTPData::Counter(const char* classId) const
403 // see header file for function documentation
404 return Counter(Index(classId));
407 const char* AliHLTCTPData::Name(int index) const
409 // see header file for function documentation
410 if (index>fClassIds.GetLast()) return NULL;
411 return fClassIds.At(index)->GetName();
414 int AliHLTCTPData::ReadMap(vector<unsigned> &map) const
416 // read the index map for class names
417 // for nested class names (e.g. 'myclass' is contained in
418 // 'myclassA') the longer names is added first to the map.
419 for (int index=0; index<=fClassIds.GetLast(); index++) {
420 vector<unsigned>::iterator element=map.begin();
421 for (; element!=map.end(); element++) {
422 TString name=Name(index);
423 if (name.Contains(Name(*element))) {
424 // current name contains another one already in the map
425 // -> add before and go to next entry
426 element=map.insert(element, index);
431 if (element==map.end()) {
432 // unique class name, append to map
433 map.push_back(index);
440 AliHLTReadoutList AliHLTCTPData::ReadoutList(const AliHLTComponentTriggerData& trigData) const
442 // see header file for function documentation
444 AliHLTCDHWrapper cdh;
445 if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true) != 0) return AliHLTReadoutList();
446 // Check if we are dealing with a software trigger. If so then we need to return
447 // a readout list with everything set because the CTP trigger bits are invalid.
448 // Thus we assume that everything should be read out.
449 if ((cdh.GetL1TriggerMessage() & 0x1) == 0x1) return ~ AliHLTReadoutList();
451 AliHLTTriggerMask_t triggerMask(cdh.GetTriggerClasses());
452 AliHLTTriggerMask_t triggerHigh(cdh.GetTriggerClassesNext50());
453 triggerMask |= (triggerHigh << 50);
455 if (fMask.any() && (triggerMask & fMask).none()) {
456 AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
457 HLTWarning("invalid trigger mask %s, unknown CTP trigger, initialized %s",
458 TriggerMaskToString(triggerMask).c_str(), TriggerMaskToString(fMask).c_str());
459 for (int i=0; i<evtData->fCommonHeaderWordCnt; i++)
460 HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
463 return ReadoutList(triggerMask);
466 AliHLTReadoutList AliHLTCTPData::ReadoutList(AliHLTTriggerMask_t triggerMask) const
468 // take an 'OR' of all active trigger classes
469 AliHLTReadoutList list;
470 for (int i=0; i<gkNCTPTriggerClasses; i++) {
471 if (i>fClassIds.GetLast()) break;
472 if (! triggerMask.test(i)) continue;
473 AliHLTReadoutList* tcrl=(AliHLTReadoutList*)fClassIds.At(i);
481 void AliHLTCTPData::Print(Option_t* /*option*/) const
483 // see header file for function documentation
484 cout << GetTitle() << endl;
485 cout << "\tactive trigger mask: 0x" << hex << fTriggers << dec << endl;
487 for (int i=0; i<gkNCTPTriggerClasses; i++) {
488 if (i>=Counters().GetSize()) break;
489 if (i>fClassIds.GetLast()) break;
490 if (! fMask.test(i)) continue;
492 cout << "\t" << i << "\t" << Name(i) << "\t" << Counter(i) << endl;
494 if (count==0) cout << "\t(none)" << endl;
498 std::string AliHLTCTPData::TriggerMaskToString(AliHLTTriggerMask_t mask) const
500 AliHLTTriggerMask_t max(std::numeric_limits<unsigned long>::max());
501 int digits = std::numeric_limits<unsigned long>::digits;
502 int numberOfWords = (mask.size() + digits - 1)/digits;
503 std::stringstream stream;
505 stream << std::hex << std::right;
506 for(int i=numberOfWords-1; i>=0; --i){
507 stream.width(digits/4);
509 stream << ((mask >> (digits*i)) & max).to_ulong() << " ";