]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTCTPData.cxx
733b0733276c42d94979d6808063f5fc45999fe3
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTCTPData.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
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 //**************************************************************************
18
19 //  @file   AliHLTCTPData.cxx
20 //  @author Matthias Richter
21 //  @date   2009-08-20
22 //  @brief  Container for CTP trigger classes and counters
23 //  @note
24
25 #include "AliHLTCTPData.h"
26 #include "TClass.h"
27 #include "TObjString.h"
28 #include "TFormula.h"
29 #include "AliHLTComponent.h"
30 #include "AliRawDataHeader.h"
31
32 /** ROOT macro for the implementation of ROOT specific class methods */
33 ClassImp(AliHLTCTPData)
34
35 AliHLTCTPData::AliHLTCTPData()
36   : TNamed("AliHLTCTPData", "HLT counters for the CTP")
37   , AliHLTLogging()
38   , fMask(0)
39   , fTriggers(0)
40   , fClassIds(AliHLTReadoutList::Class(), gkNCTPTriggerClasses)
41   , fCounters(gkNCTPTriggerClasses)
42   , fMap()
43 {
44   // constructor
45   // see header file for class documentation
46   // or
47   // refer to README to build package
48   // or
49   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
50 }
51
52 AliHLTCTPData::AliHLTCTPData(const char* parameter)
53   : TNamed("AliHLTCTPData", "HLT counters for the CTP")
54   , AliHLTLogging()
55   , fMask(0)
56   , fTriggers(0)
57   , fClassIds(AliHLTReadoutList::Class(), gkNCTPTriggerClasses)
58   , fCounters(gkNCTPTriggerClasses)
59   , fMap()
60 {
61   // constructor, init the CTP trigger classes
62   InitCTPTriggerClasses(parameter);
63 }
64
65 AliHLTCTPData::~AliHLTCTPData()
66 {
67   // destructor
68   fClassIds.Delete();
69 }
70
71 AliHLTCTPData::AliHLTCTPData(const AliHLTCTPData& src)
72   : TNamed(src.GetName(), src.GetTitle())
73   , AliHLTLogging()
74   , fMask(src.Mask())
75   , fTriggers(src.fTriggers)
76   , fClassIds(src.fClassIds)
77   , fCounters(src.Counters())
78   , fMap()
79 {
80   // copy constructor
81   ReadMap();
82 }
83
84 AliHLTCTPData& AliHLTCTPData::operator=(const AliHLTCTPData& src)
85 {
86   // assignment operator, clone content
87   if (this!=&src) {
88     SetName(src.GetName());
89     SetTitle(src.GetTitle());
90     fMask=src.Mask();
91     fClassIds.Delete();
92     fClassIds.ExpandCreate(gkNCTPTriggerClasses);
93     for (int i=0; i<gkNCTPTriggerClasses; i++) {
94       if (i>src.fClassIds.GetLast()) break;
95       ((TNamed*)fClassIds.At(i))->SetName(src.fClassIds.At(i)->GetName());
96       ((TNamed*)fClassIds.At(i))->SetTitle(src.fClassIds.At(i)->GetTitle());
97     }
98     fCounters=src.Counters();
99   }
100
101   ReadMap();
102   return *this;
103 }
104
105 int AliHLTCTPData::Add(const AliHLTCTPData& src, int factor, int &skipped)
106 {
107   // see header file for class documentation
108   
109   skipped=0;
110   for (int i=0; i<gkNCTPTriggerClasses; i++) {
111     TString c;
112     c=fClassIds.At(i)->GetName();
113     if (c.IsNull()) continue;
114     if (c.CompareTo(src.fClassIds.At(i)->GetName())==0) {
115       fCounters[i]+=factor*src.Counter(i);
116     } else {
117       skipped++;
118     }
119   }
120   return 0;
121 }
122
123 AliHLTCTPData& AliHLTCTPData::operator += (const AliHLTCTPData& src)
124 {
125   // see header file for class documentation
126   
127   int nofInconsistencies=0;
128   Add(src, 1, nofInconsistencies);
129   if (nofInconsistencies>0) {
130     HLTError("Inconsistent operants: skipping %d of %d CTP classes for operation", nofInconsistencies, gkNCTPTriggerClasses);
131   }
132   return *this;
133 }
134
135 AliHLTCTPData& AliHLTCTPData::operator -= (const AliHLTCTPData& src)
136 {
137   // see header file for class documentation
138   
139   int nofInconsistencies=0;
140   Add(src, -1, nofInconsistencies);
141   if (nofInconsistencies>0) {
142     HLTError("Inconsistent operants: skipping %d of %d CTP classes for operation", nofInconsistencies, gkNCTPTriggerClasses);
143   }
144   return *this;
145 }
146
147 AliHLTCTPData AliHLTCTPData::operator + (const AliHLTCTPData& src) const
148 {
149   // see header file for class documentation
150
151   AliHLTCTPData result(*this);
152   result+=src;
153   return result;
154 }
155
156 AliHLTCTPData AliHLTCTPData::operator - (const AliHLTCTPData& src) const
157 {
158   // see header file for class documentation
159
160   AliHLTCTPData result(*this);
161   result-=src;
162   return result;
163 }
164
165 int AliHLTCTPData::InitCTPTriggerClasses(const char* ctpString)
166 {
167   // see header file for function documentation
168   if (!ctpString) return -EINVAL;
169
170   HLTImportant("Parameter: %s", ctpString);
171
172   fMask=0;
173   fClassIds.Delete();
174   fClassIds.ExpandCreate(gkNCTPTriggerClasses);
175
176   // general format of the CTP_TRIGGER_CLASS parameter
177   // <bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,<bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,...
178   HLTDebug(": %s", ctpString);
179   TString string=ctpString;
180   if (string.BeginsWith("CTP_TRIGGER_CLASS=")) string.ReplaceAll("CTP_TRIGGER_CLASS=", "");
181   TObjArray* classEntries=string.Tokenize(",");
182   if (classEntries) {
183     enum {kBit=0, kName, kDetectors};
184     for (int i=0; i<classEntries->GetEntriesFast(); i++) {
185       TString entry=((TObjString*)classEntries->At(i))->GetString();
186       TObjArray* entryParams=entry.Tokenize(":");
187       if (entryParams) {
188         if (entryParams->GetEntriesFast()==3 &&
189             (((TObjString*)entryParams->At(kBit))->GetString()).IsDigit()) {
190           int index=(((TObjString*)entryParams->At(kBit))->GetString()).Atoi();
191           if (index<gkNCTPTriggerClasses) {
192             AliHLTReadoutList* pCTPClass=dynamic_cast<AliHLTReadoutList*>(fClassIds.At(index));
193             if (pCTPClass) {
194               fMask|=(AliHLTUInt64_t)0x1 << index;
195               pCTPClass->SetTitle("CTP Class");
196               pCTPClass->SetName((((TObjString*)entryParams->At(kName))->GetString()).Data());
197               TObjArray* detectors=(((TObjString*)entryParams->At(kDetectors))->GetString()).Tokenize("-");
198               if (detectors) {
199                 for (int dix=0; dix<detectors->GetEntriesFast(); dix++) {
200                   if (!(((TObjString*)detectors->At(dix))->GetString()).IsDigit()) {
201                     HLTError("invalid detector list format: trigger class entry %s", entry.Data());
202                     break;
203                   }
204                   // see AliHLTReadoutList::EDetectorId for defines of detectors
205                   pCTPClass->Enable(0x1<<(((TObjString*)detectors->At(dix))->GetString()).Atoi());
206                 }
207                 delete detectors;
208               }
209             } else {
210             }
211           } else {
212             // the trigger bitfield is fixed to 50 bits (gkNCTPTriggerClasses)
213             HLTError("invalid trigger class entry %s, index width of trigger bitfield exceeded (%d)", entry.Data(), gkNCTPTriggerClasses);
214           }
215         } else {
216           HLTError("invalid trigger class entry %s", entry.Data());
217         }
218         delete entryParams;
219       }
220     }
221     delete classEntries;
222   }
223
224   ResetCounters();
225   ReadMap();
226
227   return 0;
228 }
229
230 AliHLTUInt64_t AliHLTCTPData::ActiveTriggers(const AliHLTComponentTriggerData& trigData)
231 {
232   // extract active triggers from the trigger data
233
234   const AliRawDataHeader* cdh = NULL;
235   if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL) != 0) return (AliHLTUInt64_t)0;
236   // trigger mask is 50 bit wide and is stored in word 5 and 6 of the CDH
237   AliHLTUInt64_t triggerMask = cdh->GetTriggerClasses();
238   return triggerMask;
239 }
240
241 bool AliHLTCTPData::EvaluateCTPTriggerClass(const char* expression, const AliHLTComponentTriggerData& trigData) const
242 {
243   // see header file for function documentation
244   
245   const AliRawDataHeader* cdh = NULL;
246   if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true) != 0) return false;
247   // trigger mask is 50 bit wide and is stored in word 5 and 6 of the CDH
248   AliHLTUInt64_t triggerMask = cdh->GetTriggerClasses();
249
250   if (fMask!=0 && (triggerMask & fMask)==0) {
251     AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
252     HLTWarning("invalid trigger mask 0x%llx, unknown CTP trigger, initialized 0x%llx", triggerMask, fMask);
253     for (int i=0; i<8; i++) HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
254     return false;
255   }
256
257   return EvaluateCTPTriggerClass(expression, triggerMask);
258 }
259
260 bool AliHLTCTPData::EvaluateCTPTriggerClass(const char* expression, AliHLTUInt64_t triggerMask) const
261 {
262   // see header file for function documentation
263
264   // use a TFormula to interprete the expression
265   // all classname are replaced by '[n]' which means the n'th parameter in the formula
266   // the parameters are set to 0 or 1 depending on the bit in the trigger mask
267   const vector<unsigned> *pMap=&fMap;
268   vector<unsigned> tmp;
269   if (fMap.size()==0 && fClassIds.GetLast()>=0) {
270     // read map into temporary array and use it
271     ReadMap(tmp);
272     pMap=&tmp;
273     static bool suppressWarning=false;
274     if (!suppressWarning) HLTWarning("map not yet initialized, creating local map (slow), suppressing further warnings");
275     suppressWarning=true;
276   }
277   vector<Double_t> par;
278   TString condition=expression;
279   for (unsigned index=0; index<pMap->size(); index++) {
280     const char* className=Name((*pMap)[index]);
281     if (className && strlen(className)>0) {
282       //HLTDebug("checking trigger class %s", className.Data());
283       if (condition.Contains(className)) {
284         TString replace; replace.Form("[%d]", par.size());
285         //HLTDebug("replacing %s with %s in \"%s\"", className.Data(), replace.Data(), condition.Data());
286         condition.ReplaceAll(className, replace);
287         if (triggerMask&((AliHLTUInt64_t)0x1<<(*pMap)[index])) par.push_back(1.0);
288         else par.push_back(0.0);
289       }
290     }
291   }
292
293   TFormula form("trigger expression", condition);
294   if (form.Compile()!=0) {
295     HLTError("invalid expression %s", expression);
296     return false;
297   }
298   if (form.EvalPar(&par[0], &par[0])>0.5) return true;
299   return false;
300 }
301
302 void AliHLTCTPData::ResetCounters()
303 {
304   // see header file for function documentation
305   fCounters.Set(gkNCTPTriggerClasses);
306   fCounters.Reset();
307 }
308
309 int AliHLTCTPData::Index(const char* name) const
310 {
311   // see header file for function documentation
312   TObject* obj=fClassIds.FindObject(name);
313   return obj!=NULL?fClassIds.IndexOf(obj):-1;
314 }
315
316 int AliHLTCTPData::CheckTrigger(const char* name) const
317 {
318   // check status of a trigger class
319   int index=Index(name);
320   if (index<0) return index;
321   return (fTriggers&(0x1<<index))>0?1:0;
322 }
323
324 void AliHLTCTPData::Increment(const char* classIds)
325 {
326   // see header file for function documentation
327   TString string=classIds;
328   TObjArray* classEntries=string.Tokenize(",");
329   if (classEntries) {
330     for (int i=0; i<classEntries->GetEntriesFast(); i++) {
331       int index=Index(((TObjString*)classEntries->At(i))->GetString().Data());
332       if (index>=0 && index<fCounters.GetSize()) fCounters[index]++;
333     }
334     delete classEntries;
335   }
336 }
337
338 void AliHLTCTPData::Increment(AliHLTUInt64_t triggerPattern)
339 {
340   // see header file for function documentation
341   AliHLTUInt64_t pattern=triggerPattern&fMask;
342   for (int i=0; i<fCounters.GetSize(); i++) {
343     if ((pattern&((AliHLTUInt64_t)0x1<<i))==0) continue;
344     fCounters[i]++;    
345   }
346 }
347
348 void AliHLTCTPData::Increment(int classIdx)
349 {
350   // see header file for function documentation
351   if (classIdx<fCounters.GetSize() &&
352       (fMask&((AliHLTUInt64_t)0x1<<classIdx))) {
353     fCounters[classIdx]++;    
354   }
355   
356 }
357
358 int AliHLTCTPData::Increment(AliHLTComponentTriggerData& trigData)
359 {
360   // see header file for function documentation
361   const AliRawDataHeader* cdh = NULL;
362   int result = AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true);
363   if (result != 0) return result;
364   // trigger mask is 50 bit wide and is stored in word 5 and 6 of the CDH
365   AliHLTUInt64_t triggerMask = cdh->GetTriggerClasses();
366
367   if (fMask!=0 && (triggerMask & fMask)==0) {
368     AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
369     HLTWarning("invalid trigger mask 0x%llx, unknown CTP trigger, initialized 0x%llx", triggerMask, fMask);
370     for (int i=0; i<8; i++) HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
371   }
372   Increment(triggerMask);
373   return 0;
374 }
375
376 AliHLTUInt64_t AliHLTCTPData::Counter(int index) const
377 {
378   // see header file for function documentation
379   if (index>=0 && index<Counters().GetSize()) return Counters()[index];
380   return 0;
381 }
382
383 AliHLTUInt64_t AliHLTCTPData::Counter(const char* classId) const
384 {
385   // see header file for function documentation
386   return Counter(Index(classId));
387 }
388
389 const char* AliHLTCTPData::Name(int index) const
390 {
391   // see header file for function documentation
392   if (index>fClassIds.GetLast()) return NULL;
393   return fClassIds.At(index)->GetName();
394 }
395
396 int AliHLTCTPData::ReadMap(vector<unsigned> &map) const
397 {
398   // read the index map for class names
399   // for nested class names (e.g. 'myclass' is contained in
400   // 'myclassA') the longer names is added first to the map.
401   for (int index=0; index<=fClassIds.GetLast(); index++) {
402     vector<unsigned>::iterator element=map.begin();
403     for (; element!=map.end(); element++) {
404       TString name=Name(index);
405       if (name.Contains(Name(*element))) {
406         // current name contains another one already in the map
407         // -> add before and go to next entry
408         element=map.insert(element, index);
409         break;
410       }
411     }
412
413     if (element==map.end()) {
414       // unique class name, append to map
415       map.push_back(index);
416     }
417   }
418   return 0;
419 }
420
421
422 AliHLTReadoutList AliHLTCTPData::ReadoutList(const AliHLTComponentTriggerData& trigData) const
423 {
424   // see header file for function documentation
425
426   const AliRawDataHeader* cdh = NULL;
427   if (AliHLTComponent::ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL, true) != 0) return AliHLTReadoutList();
428   // trigger mask is 50 bit wide and is stored in word 5 and 6 of the CDH
429   AliHLTUInt64_t triggerMask = cdh->GetTriggerClasses();
430
431   if (fMask!=0 && (triggerMask & fMask)==0) {
432     AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
433     HLTWarning("invalid trigger mask 0x%llx, unknown CTP trigger, initialized 0x%llx", triggerMask, fMask);
434     for (int i=0; i<8; i++) HLTWarning("\t CDH[%d]=0x%lx", i, evtData->fCommonHeader[i]);
435   }
436
437   return ReadoutList(triggerMask);
438 }
439
440 AliHLTReadoutList AliHLTCTPData::ReadoutList(AliHLTUInt64_t  triggerMask) const
441 {
442   // take an 'OR' of all active trigger classes 
443   AliHLTReadoutList list;
444   for (int i=0; i<gkNCTPTriggerClasses; i++) {
445     if (i>fClassIds.GetLast()) break;
446     if ((triggerMask&((AliHLTUInt64_t)0x1<<i))==0) continue;
447     AliHLTReadoutList* tcrl=(AliHLTReadoutList*)fClassIds.At(i);
448     list.OrEq(*tcrl);
449   }
450
451   return list;
452 }
453
454
455 void AliHLTCTPData::Print(Option_t* /*option*/) const
456 {
457   // see header file for function documentation
458   cout << GetTitle() << endl;
459   cout << "\tactive trigger mask: 0x" << hex << fTriggers << dec << endl;
460   int count=0;
461   for (int i=0; i<gkNCTPTriggerClasses; i++) {
462     if (i>=Counters().GetSize()) break;
463     if (i>fClassIds.GetLast()) break;
464     if ((fMask&((AliHLTUInt64_t)0x1<<i))==0) continue;
465     count++;
466     cout << "\t" << i << "\t" << Name(i) << "\t" << Counter(i) << endl;
467   }
468   if (count==0) cout << "\t(none)" << endl;
469 }