Fixing bug caused by UUID not being generated properly.
[u/mrichter/AliRoot.git] / HLT / trigger / AliHLTGlobalTriggerComponent.cxx
CommitLineData
3da1c6d7 1// $Id$
1b9a175e 2/**************************************************************************
3 * This file is property of and copyright by the ALICE HLT Project *
4 * ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Primary Authors: Artur Szostak <artursz@iafrica.com> *
7 * for The ALICE HLT Project. *
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 AliHLTGlobalTriggerComponent.cxx
19/// @author Artur Szostak <artursz@iafrica.com>
20/// @date 26 Nov 2008
21/// @brief Implementation of the AliHLTGlobalTriggerComponent component class.
22///
23/// The AliHLTGlobalTriggerComponentComponent class applies the global HLT trigger to all
24/// trigger information produced by components deriving from AliHLTTrigger.
25
26#include "AliHLTGlobalTriggerComponent.h"
e2bb8ddd 27#include "AliHLTGlobalTriggerDecision.h"
28#include "AliHLTGlobalTrigger.h"
52f67e50 29#include "AliHLTGlobalTriggerConfig.h"
30#include "AliHLTTriggerMenu.h"
474952b2 31#include "AliHLTCTPData.h"
3da1c6d7 32#include "AliCDBManager.h"
33#include "AliCDBStorage.h"
34#include "AliCDBEntry.h"
e2bb8ddd 35#include "TUUID.h"
579a2996 36#include "TMD5.h"
37#include "TRandom3.h"
e2bb8ddd 38#include "TROOT.h"
72bc5ab0 39#include "TSystem.h"
52f67e50 40#include "TRegexp.h"
41#include "TClonesArray.h"
42#include "TObjString.h"
1b820a65 43#include "TString.h"
52f67e50 44#include "TInterpreter.h"
d4ed5215 45#include "TDatime.h"
81d62bb4 46#include "TClass.h"
d543303d 47#include "TNamed.h"
e2bb8ddd 48#include <fstream>
49#include <cerrno>
566a01d0 50#include <cassert>
81d62bb4 51#include <vector>
52#include <algorithm>
1b9a175e 53
54ClassImp(AliHLTGlobalTriggerComponent)
55
3da1c6d7 56const char* AliHLTGlobalTriggerComponent::fgkTriggerMenuCDBPath = "HLT/ConfigHLT/HLTGlobalTrigger";
57
1b9a175e 58
81d62bb4 59namespace
60{
61 /**
62 * This method is used as a comparison functor with the STL sort routines.
63 */
64 bool AliHLTDescendingNumbers(UInt_t a, UInt_t b)
65 {
66 return a > b;
67 }
68} // end of namespace
69
70
1b9a175e 71AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() :
e2bb8ddd 72 AliHLTTrigger(),
52f67e50 73 fTrigger(NULL),
72bc5ab0 74 fDebugMode(false),
566a01d0 75 fRuntimeCompile(true),
81d62bb4 76 fDeleteCodeFile(false),
566a01d0 77 fCodeFileName(),
81d62bb4 78 fClassName(),
79 fCTPDecisions(NULL),
80 fBufferSizeConst(2*(sizeof(AliHLTGlobalTriggerDecision) + sizeof(AliHLTReadoutList))),
81 fBufferSizeMultiplier(1.),
82 fIncludePaths(TObjString::Class()),
1b820a65 83 fIncludeFiles(TObjString::Class()),
6598c82b 84 fLibStateAtLoad(),
3d6ea577 85 fBits(0),
86 fDataEventsOnly(true)
1b9a175e 87{
88 // Default constructor.
acc7214e 89
90 ClearInfoForNewEvent(false);
1b9a175e 91}
92
93
94AliHLTGlobalTriggerComponent::~AliHLTGlobalTriggerComponent()
95{
96 // Default destructor.
e2bb8ddd 97
98 if (fTrigger != NULL) delete fTrigger;
566a01d0 99
100 if (fCTPDecisions) {
101 fCTPDecisions->Delete();
102 delete fCTPDecisions;
103 }
1b9a175e 104}
105
106
025443e0 107void AliHLTGlobalTriggerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& list) const
108{
109 // Returns the kAliHLTDataTypeGlobalTrigger type as output.
110 list.push_back(kAliHLTDataTypeGlobalTrigger);
111}
112
113
1b9a175e 114void AliHLTGlobalTriggerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
115{
116 // Returns the output data size estimate.
117
81d62bb4 118 constBase = fBufferSizeConst;
119 inputMultiplier = fBufferSizeMultiplier;
1b9a175e 120}
121
122
52f67e50 123Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
1b9a175e 124{
e2bb8ddd 125 // Initialises the global trigger component.
126
52f67e50 127 fDebugMode = false;
81d62bb4 128 fClassName = "";
129 fCodeFileName = "";
130 fDeleteCodeFile = false;
52f67e50 131 const char* configFileName = NULL;
132 const char* codeFileName = NULL;
81d62bb4 133 fIncludePaths.Clear();
134 fIncludeFiles.Clear();
6598c82b 135 SetBit(kIncludeInput);
3d6ea577 136 fDataEventsOnly = true;
52f67e50 137
138 for (int i = 0; i < argc; i++)
139 {
140 if (strcmp(argv[i], "-config") == 0)
141 {
142 if (configFileName != NULL)
143 {
144 HLTWarning("Trigger configuration macro was already specified."
145 " Will replace previous value given by -config."
146 );
147 }
148 if (argc <= i+1)
149 {
150 HLTError("The trigger configuration macro filename was not specified." );
151 return -EINVAL;
152 }
153 configFileName = argv[i+1];
154 i++;
155 continue;
156 }
157
158 if (strcmp(argv[i], "-includepath") == 0)
159 {
160 if (argc <= i+1)
161 {
162 HLTError("The include path was not specified." );
163 return -EINVAL;
164 }
dda123c3 165 try
166 {
167 new (fIncludePaths[fIncludePaths.GetEntriesFast()]) TObjString(argv[i+1]);
168 }
169 catch (const std::bad_alloc&)
170 {
171 HLTError("Could not allocate more memory for the fIncludePaths array.");
172 return -ENOMEM;
173 }
52f67e50 174 i++;
175 continue;
176 }
177
178 if (strcmp(argv[i], "-include") == 0)
179 {
180 if (argc <= i+1)
181 {
182 HLTError("The include file name was not specified." );
183 return -EINVAL;
184 }
dda123c3 185 try
186 {
187 new (fIncludeFiles[fIncludeFiles.GetEntriesFast()]) TObjString(argv[i+1]);
188 }
189 catch (const std::bad_alloc&)
190 {
191 HLTError("Could not allocate more memory for the fIncludeFiles array.");
192 return -ENOMEM;
193 }
52f67e50 194 i++;
195 continue;
196 }
197
198 if (strcmp(argv[i], "-debug") == 0)
199 {
200 if (fDebugMode == true)
201 {
202 HLTWarning("The debug flag was already specified. Ignoring this instance.");
203 }
204 fDebugMode = true;
566a01d0 205 continue;
206 }
207
208 if (strcmp(argv[i], "-cint") == 0)
209 {
210 fRuntimeCompile = false;
52f67e50 211 continue;
212 }
213
214 if (strcmp(argv[i], "-usecode") == 0)
215 {
216 if (codeFileName != NULL)
217 {
218 HLTWarning("Custom trigger code file was already specified."
219 " Will replace previous value given by -usecode."
220 );
221 }
222 if (argc <= i+1)
223 {
224 HLTError("The custom trigger code filename was not specified." );
225 return -EINVAL;
226 }
227 codeFileName = argv[i+1];
228 if (argc <= i+2)
229 {
230 HLTError("The custom trigger class name was not specified." );
231 return -EINVAL;
232 }
81d62bb4 233 fClassName = argv[i+2];
52f67e50 234 i += 2;
235 continue;
236 }
474952b2 237
238 if (strcmp(argv[i], "-skipctp") == 0)
239 {
240 HLTInfo("Skipping CTP counters in trigger decision");
6598c82b 241 SetBit(kSkipCTP);
242 continue;
243 }
244
245 if (strcmp(argv[i], "-forward-input") == 0)
246 {
247 HLTInfo("Forwarding input objects and trigger decisions");
248 SetBit(kForwardInput);
249 SetBit(kIncludeShort);
250 SetBit(kIncludeInput, false);
251 continue;
252 }
253
d543303d 254 if (strstr(argv[i], "-include-input") == argv[i])
6598c82b 255 {
256 SetBit(kForwardInput,false);
257 TString param=argv[i];
258 param.ReplaceAll("-include-input", "");
259 if (param.CompareTo("=none")==0)
260 {
261 HLTInfo("skipping objects and trigger decisions");
262 SetBit(kIncludeShort, false);
263 SetBit(kIncludeInput, false);
264 }
265 else if (param.CompareTo("=short")==0)
266 {
d543303d 267 HLTInfo("including short info on objects and trigger decisions");
6598c82b 268 SetBit(kIncludeShort);
269 SetBit(kIncludeInput, false);
270 }
d543303d 271 else if (param.CompareTo("=both")==0)
272 {
273 HLTInfo("including input objects, trigger decisions and short info");
274 SetBit(kIncludeShort);
275 SetBit(kIncludeInput);
276 }
6598c82b 277 else if (param.CompareTo("=objects")==0 || param.IsNull())
278 {
279 HLTInfo("including input objects and trigger decisions");
280 SetBit(kIncludeShort, false);
281 SetBit(kIncludeInput);
282 }
283 else
284 {
285 HLTError("unknown parameter '%s' for argument '-include-input'", param.Data());
286 }
474952b2 287 continue;
288 }
3d6ea577 289
290 if (strcmp(argv[i], "-process-all-events") == 0)
291 {
292 fDataEventsOnly = false;
293 continue;
294 }
295
52f67e50 296 HLTError("Unknown option '%s'.", argv[i]);
297 return -EINVAL;
298 } // for loop
299
300 const AliHLTTriggerMenu* menu = NULL;
301 if (configFileName != NULL)
302 {
303 TString cmd = ".x ";
304 cmd += configFileName;
305 gROOT->ProcessLine(cmd);
306 menu = AliHLTGlobalTriggerConfig::Menu();
307 }
308
3da1c6d7 309 // Try load the trigger menu from the CDB if it is not loaded yet with the
310 // -config option
81d62bb4 311 int result = -ENOENT;
312 if (menu == NULL)
3da1c6d7 313 {
81d62bb4 314 result = LoadTriggerMenu(fgkTriggerMenuCDBPath, menu);
3da1c6d7 315 }
52f67e50 316 if (menu == NULL)
317 {
318 HLTError("No trigger menu configuration found or specified.");
81d62bb4 319 return result;
52f67e50 320 }
321
52f67e50 322 if (codeFileName == NULL)
323 {
81d62bb4 324 result = GenerateTrigger(menu, fClassName, fCodeFileName, fIncludePaths, fIncludeFiles);
325 if (result == 0) fDeleteCodeFile = true;
52f67e50 326 }
327 else
328 {
81d62bb4 329 result = LoadTriggerClass(codeFileName, fIncludePaths);
330 if (result == 0) fCodeFileName = codeFileName;
52f67e50 331 }
e2bb8ddd 332 if (result != 0) return result;
333
dda123c3 334 try
335 {
336 fTrigger = AliHLTGlobalTrigger::CreateNew(fClassName.Data());
337 }
338 catch (const std::bad_alloc&)
339 {
340 HLTError("Could not allocate memory for the AliHLTGlobalTrigger instance.");
341 return -ENOMEM;
342 }
e2bb8ddd 343 if (fTrigger == NULL)
344 {
81d62bb4 345 HLTError("Could not create a new instance of '%s'.", fClassName.Data());
e2bb8ddd 346 return -EIO;
347 }
348
52f67e50 349 fTrigger->FillFromMenu(*menu);
325e5e42 350 if (fTrigger->CallFailed()) return -EPROTO;
474952b2 351
352 // setup the CTP accounting in AliHLTComponent
566a01d0 353 SetupCTPData();
474952b2 354
acc7214e 355 // Set the default values from the trigger menu.
356 SetDescription(menu->DefaultDescription());
357 SetTriggerDomain(menu->DefaultTriggerDomain());
358
e2bb8ddd 359 return 0;
360}
361
1b9a175e 362
e2bb8ddd 363Int_t AliHLTGlobalTriggerComponent::DoDeinit()
364{
365 // Cleans up the global trigger component.
366
367 if (fTrigger != NULL)
368 {
369 delete fTrigger;
370 fTrigger = NULL;
371 }
372
566a01d0 373 if (fCTPDecisions) {
374 fCTPDecisions->Delete();
375 delete fCTPDecisions;
376 }
377 fCTPDecisions=NULL;
81d62bb4 378
379 Int_t result = UnloadTriggerClass(fCodeFileName);
380 if (result != 0) return result;
381
382 if (fDeleteCodeFile and !fCodeFileName.IsNull() && gSystem->AccessPathName(fCodeFileName)==0 && !fDebugMode) {
383 fCodeFileName.ReplaceAll(".cxx", "*");
384 TString command="rm "; command+=fCodeFileName;
385 gSystem->Exec(command);
386 }
387 fCodeFileName="";
388 fDeleteCodeFile=false;
566a01d0 389
1b9a175e 390 return 0;
391}
4f1d6b68 392
393
394AliHLTComponent* AliHLTGlobalTriggerComponent::Spawn()
395{
396 // Creates a new object instance.
dda123c3 397 AliHLTComponent* comp = NULL;
398 try
399 {
400 comp = new AliHLTGlobalTriggerComponent;
401 }
402 catch (const std::bad_alloc&)
403 {
404 HLTError("Could not allocate memory for a new instance of AliHLTGlobalTriggerComponent.");
405 return NULL;
406 }
407 return comp;
4f1d6b68 408}
409
e2bb8ddd 410
411int AliHLTGlobalTriggerComponent::DoTrigger()
412{
413 // This method will apply the global trigger decision.
414
415 if (fTrigger == NULL)
416 {
417 HLTFatal("Global trigger implementation object is NULL!");
418 return -EIO;
419 }
2974f8dc 420
421 AliHLTUInt32_t eventType=0;
422 if (!IsDataEvent(&eventType)) {
423 if (eventType==gkAliEventTypeEndOfRun) PrintStatistics(fTrigger, kHLTLogImportant, 0);
3d6ea577 424 if (fDataEventsOnly)
425 {
2f251ae6 426 IgnoreEvent(); // dont generate any trigger decision.
3d6ea577 427 return 0;
428 }
2974f8dc 429 }
430
81d62bb4 431 // Copy the trigger counters in case we need to set them back to their original
432 // value because the PushBack method fails with ENOSPC.
433 TArrayL64 originalCounters = fTrigger->GetCounters();
325e5e42 434 if (fTrigger->CallFailed()) return -EPROTO;
81d62bb4 435
e2bb8ddd 436 fTrigger->NewEvent();
325e5e42 437 if (fTrigger->CallFailed()) return -EPROTO;
e2bb8ddd 438
439 // Fill in the input data.
440 const TObject* obj = GetFirstInputObject();
441 while (obj != NULL)
442 {
52f67e50 443 fTrigger->Add(obj, GetDataType(), GetSpecification());
325e5e42 444 if (fTrigger->CallFailed()) return -EPROTO;
52f67e50 445 obj = GetNextInputObject();
446 }
447
a489a8dd 448 // add trigger decisions for every CTP class
449 const AliHLTCTPData* pCTPData=CTPData();
450 if (pCTPData) {
566a01d0 451 AddCTPDecisions(fTrigger, pCTPData, GetTriggerData());
a489a8dd 452 }
453
52f67e50 454 // Calculate the global trigger result and trigger domain, then create and push
455 // back the new global trigger decision object.
456 TString description;
acc7214e 457 AliHLTTriggerDomain triggerDomain;
458 bool triggerResult = fTrigger->CalculateTriggerDecision(triggerDomain, description);
325e5e42 459 if (fTrigger->CallFailed()) return -EPROTO;
acc7214e 460 AliHLTGlobalTriggerDecision decision(
461 triggerResult,
462 // The following will cause the decision to be generated with default values
463 // (set in fTriggerDomain and fDescription) if the trigger result is false.
464 (triggerResult == true) ? triggerDomain : GetTriggerDomain(),
465 (triggerResult == true) ? description.Data() : GetDescription()
466 );
48d98db9 467
81d62bb4 468 decision.SetCounters(fTrigger->GetCounters(), GetEventCount()+1);
325e5e42 469 if (fTrigger->CallFailed()) return -EPROTO;
470
2f251ae6 471 TClonesArray shortInfo(TNamed::Class(), GetNumberOfInputBlocks());
52f67e50 472
2f251ae6 473 // Add the input objects used to make the global decision.
52f67e50 474 obj = GetFirstInputObject();
475 while (obj != NULL)
476 {
2f251ae6 477 const AliHLTTriggerDecision* intrig = dynamic_cast<const AliHLTTriggerDecision*>(obj);
478
479 if (TestBit(kForwardInput)) Forward(obj);
480
481 if (TestBit(kIncludeInput))
e2bb8ddd 482 {
2f251ae6 483 if (intrig != NULL)
484 {
485 decision.AddTriggerInput(*intrig);
486 }
487 else
488 {
489 // The const_cast should be safe in this case because the list of inputObjects
490 // will be added to the global decision with AddInputObjectRef, which only
491 // modifies the kCanDelete bit and nothing else.
492 // This is necessary since GetFirstInputObject only returns const objects.
493 decision.AddInputObjectRef( const_cast<TObject*>(obj) );
494 }
e2bb8ddd 495 }
2f251ae6 496
497 if (TestBit(kIncludeShort))
e2bb8ddd 498 {
2f251ae6 499 int entries = shortInfo.GetEntriesFast();
dda123c3 500 try
501 {
2f251ae6 502 new (shortInfo[entries]) TNamed(obj->GetName(), obj->GetTitle());
dda123c3 503 }
504 catch (const std::bad_alloc&)
505 {
506 HLTError("Could not allocate more memory for the short list of input objects.");
dda123c3 507 return -ENOMEM;
508 }
2f251ae6 509 if (intrig != NULL)
510 {
511 shortInfo[entries]->SetBit(BIT(16)); // indicate that this is a trigger decision
512 shortInfo[entries]->SetBit(BIT(15), intrig->Result());
d543303d 513 }
514 }
515
e2bb8ddd 516 obj = GetNextInputObject();
517 }
2f251ae6 518 if (TestBit(kIncludeShort)) decision.AddInputObjectRef(&shortInfo);
519
520 // The const_cast should be safe in this case because AddInputObjectRef just
521 // modifies the kCanDelete bit and nothing else.
522 if (!TestBit(kSkipCTP) && CTPData()) decision.AddInputObjectRef(const_cast<AliHLTCTPData*>(CTPData()));
523
524 static UInt_t lastTime=0;
525 TDatime time;
526 if (time.Get()-lastTime>60) {
527 lastTime=time.Get();
528 PrintStatistics(fTrigger, kHLTLogImportant);
d543303d 529 }
2974f8dc 530
0744f75d 531 // add readout filter to event done data
2974f8dc 532 CreateEventDoneReadoutFilter(decision.TriggerDomain(), 3);
0744f75d 533 // add monitoring filter to event done data
2974f8dc 534 CreateEventDoneReadoutFilter(decision.TriggerDomain(), 4);
0744f75d 535 if (decision.Result()) {
536 // add monitoring event command for triggered events
537 CreateEventDoneReadoutFilter(decision.TriggerDomain(), 5);
538 }
72e31932 539 if (TriggerEvent(&decision) == -ENOSPC)
81d62bb4 540 {
541 // Increase the estimated buffer space required if the PushBack methods in TriggerEvent
542 // returned the "no buffer space" error code. Also remember to set the trigger counters
543 // back to what they were, otherwise triggers will be double counted when we try to reprocess
544 // this event with larger buffers.
545 fBufferSizeConst += 1024*1024;
546 fBufferSizeMultiplier *= 2.;
547 fTrigger->SetCounters(originalCounters);
325e5e42 548 if (fTrigger->CallFailed()) return -EPROTO;
81d62bb4 549 return -ENOSPC;
550 }
551 return 0;
552}
553
554
555int AliHLTGlobalTriggerComponent::Reconfigure(const char* cdbEntry, const char* chainId)
556{
557 // Reconfigure the component by loading the trigger menu and recreating the
558 // trigger logic class.
559
560 const char* path = fgkTriggerMenuCDBPath;
561 const char* id = "(unknown)";
562 if (cdbEntry != NULL) path = cdbEntry;
563 if (chainId != NULL and chainId[0] != '\0') id = chainId;
564 HLTInfo("Reconfiguring from '%s' for chain component '%s'.", path, id);
565
566 const AliHLTTriggerMenu* menu = NULL;
567 int result = LoadTriggerMenu(path, menu);
568 if (result != 0) return result;
569
570 TString className;
571 TString codeFileName;
572 result = GenerateTrigger(menu, className, codeFileName, fIncludePaths, fIncludeFiles);
573 if (result != 0) return result;
574
dda123c3 575 AliHLTGlobalTrigger* trigger = NULL;
576 try
577 {
578 trigger = AliHLTGlobalTrigger::CreateNew(className.Data());
579 }
580 catch (const std::bad_alloc&)
581 {
582 HLTError("Could not allocate memory for the AliHLTGlobalTrigger instance.");
583 return -ENOMEM;
584 }
81d62bb4 585 if (trigger == NULL)
586 {
587 HLTError("Could not create a new instance of '%s'.", className.Data());
588 // Make sure to cleanup after the new code file.
589 UnloadTriggerClass(codeFileName);
590 if (not codeFileName.IsNull() and gSystem->AccessPathName(codeFileName)==0 and not fDebugMode)
591 {
592 codeFileName.ReplaceAll(".cxx", "*");
593 TString command="rm "; command+=codeFileName;
594 gSystem->Exec(command);
595 }
596 return -EIO;
597 }
598
599 if (fTrigger != NULL)
600 {
601 delete fTrigger;
602 fTrigger = NULL;
603 }
604
605 fTrigger = trigger;
606 fTrigger->FillFromMenu(*menu);
325e5e42 607 if (fTrigger->CallFailed()) return -EPROTO;
81d62bb4 608
609 // Set the default values from the trigger menu.
610 SetDescription(menu->DefaultDescription());
611 SetTriggerDomain(menu->DefaultTriggerDomain());
612
613 // Cleanup the old class code.
614 UnloadTriggerClass(fCodeFileName);
615 if (fDeleteCodeFile and not fCodeFileName.IsNull() and gSystem->AccessPathName(fCodeFileName)==0 and not fDebugMode)
616 {
617 fCodeFileName.ReplaceAll(".cxx", "*");
618 TString command="rm "; command+=fCodeFileName;
619 gSystem->Exec(command);
620 }
621 fCodeFileName = codeFileName;
622 fDeleteCodeFile = true; // Since we generated a new class.
623
624 return 0;
625}
626
627
628int AliHLTGlobalTriggerComponent::LoadTriggerMenu(const char* cdbPath, const AliHLTTriggerMenu*& menu)
629{
630 // Loads the trigger menu object from the CDB path.
631
632 HLTDebug("Trying to load trigger menu from '%s'.", cdbPath);
633 if (AliCDBManager::Instance() == NULL)
634 {
635 HLTError("CDB manager object not found.");
636 return -EIO;
637 }
638 AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage();
639 if (store == NULL)
640 {
641 HLTError("Could not get the the default storage for the CDB.");
642 return -EIO;
643 }
644 Int_t version = store->GetLatestVersion(cdbPath, GetRunNo());
645 Int_t subVersion = store->GetLatestSubVersion(cdbPath, GetRunNo(), version);
646 AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, GetRunNo(), version, subVersion);
647 if (entry == NULL)
648 {
649 HLTError("Could not get the CDB entry for \"%s\".", cdbPath);
650 return -EIO;
651 }
652 TObject* obj = entry->GetObject();
653 if (obj == NULL)
654 {
655 HLTError("Configuration object for \"%s\" is missing.", cdbPath);
656 return -ENOENT;
657 }
658 if (obj->IsA() != AliHLTTriggerMenu::Class())
659 {
660 HLTError("Wrong type for configuration object in \"%s\". Found a %s but we expect a AliHLTTriggerMenu.",
661 cdbPath, obj->ClassName()
662 );
663 return -EPROTO;
664 }
665 menu = static_cast<AliHLTTriggerMenu*>(obj);
e2bb8ddd 666 return 0;
667}
668
669
579a2996 670void AliHLTGlobalTriggerComponent::GenerateFileName(TString& name, TString& filename) const
671{
672 // Creates a unique file name for the generated code.
673
674 // Start by creating a new UUID. We cannot use the one automatically generated
675 // by ROOT because the algorithm used will not guarantee unique IDs when generating
676 // these UUIDs at a high rate in parallel.
677 TUUID uuid;
678 // We then use the generated UUID to form part of the random number seeds which
679 // will be used to generate a proper random UUID. For good measure we use a MD5
680 // hash also. Note that we want to use the TUUID class because it will combine the
681 // host address information into the UUID. Using gSystem->GetHostByName() apparently
682 // can cause problems on Windows machines with a firewall, because it always tries
683 // to contact a DNS. The TUUID class handles this case appropriately.
684 union
685 {
686 UChar_t buf[16];
687 UShort_t word[8];
688 UInt_t dword[4];
689 };
690 uuid.GetUUID(buf);
691 TMD5 md5;
692 md5.Update(buf, sizeof(buf));
693 md5.Final(buf);
694 dword[0] += gSystem->GetUid();
695 dword[1] += gSystem->GetGid();
696 dword[2] += gSystem->GetPid();
697 for (int i = 0; i < 4; ++i)
698 {
699 gRandom->SetSeed(dword[i]);
700 dword[i] = gRandom->Integer(0xFFFFFFFF);
701 }
702 md5.Update(buf, sizeof(buf));
703 md5.Final(buf);
704 // To keep to the standard we need to set the version and reserved bits.
705 word[3] = (word[3] & 0x0FFF) | 0x4000;
706 buf[8] = (buf[8] & 0x3F) | 0x80;
707
708 // Create the name of the new class and file.
709 char uuidstr[64];
710 sprintf(uuidstr, "%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x",
711 dword[0], word[2], word[3], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]
712 );
713 name = "AliHLTGlobalTriggerImpl_";
714 name += uuidstr;
715 filename = name + ".cxx";
716
717 // For good measure, check that the file names are not used. If they are then regenerate.
718 fstream file(filename.Data(), ios_base::in);
719 if (file.good())
720 {
721 file.close();
722 GenerateFileName(name, filename);
723 }
724}
725
726
52f67e50 727int AliHLTGlobalTriggerComponent::GenerateTrigger(
81d62bb4 728 const AliHLTTriggerMenu* menu, TString& name, TString& filename,
52f67e50 729 const TClonesArray& includePaths, const TClonesArray& includeFiles
730 )
e2bb8ddd 731{
732 // Generates the global trigger class that will implement the specified trigger menu.
52f67e50 733 // See header for more details.
e2bb8ddd 734
579a2996 735 GenerateFileName(name, filename);
736 HLTDebug("Generating custom HLT trigger class named %s, in file %s, using trigger menu %p.",
737 name.Data(), filename.Data(), ((void*)menu)
738 );
e2bb8ddd 739
52f67e50 740 // Open a text file to write the code and generate the new class.
81d62bb4 741 fstream code(filename.Data(), ios_base::out | ios_base::trunc);
e2bb8ddd 742 if (not code.good())
743 {
81d62bb4 744 HLTError("Could not open file '%s' for writing.", filename.Data());
e2bb8ddd 745 return -EIO;
746 }
747
52f67e50 748 TClonesArray symbols(AliHLTTriggerMenuSymbol::Class());
749 int result = BuildSymbolList(menu, symbols);
750 if (result != 0) return result;
751
81d62bb4 752 code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
52f67e50 753 code << "#include <cstring>" << endl;
2f251ae6 754 code << "#include \"TClass.h\"" << endl;
52f67e50 755 code << "#include \"TString.h\"" << endl;
756 code << "#include \"TClonesArray.h\"" << endl;
81d62bb4 757 code << "#include \"AliHLTLogging.h\"" << endl;
e2bb8ddd 758 code << "#include \"AliHLTGlobalTrigger.h\"" << endl;
759 code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl;
52f67e50 760 code << "#include \"AliHLTDomainEntry.h\"" << endl;
761 code << "#include \"AliHLTTriggerDomain.h\"" << endl;
762 code << "#include \"AliHLTReadoutList.h\"" << endl;
763 code << "#include \"AliHLTTriggerMenu.h\"" << endl;
764 code << "#include \"AliHLTTriggerMenuItem.h\"" << endl;
765 code << "#include \"AliHLTTriggerMenuSymbol.h\"" << endl;
766
767 // Add any include files that were specified on the command line.
768 for (Int_t i = 0; i < includeFiles.GetEntriesFast(); i++)
769 {
7da191b6 770 TString file = static_cast<TObjString*>(includeFiles.UncheckedAt(i))->String();
52f67e50 771 code << "#include \"" << file.Data() << "\"" << endl;
772 }
773
81d62bb4 774 if (fDebugMode)
775 {
776 code << "#else" << endl;
777 code << "const char* gFunctionName = \"???\";" << endl;
778 code << "#define HLTDebug(msg) if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) SendMessage(kHLTLogDebug, Class_Name(), ::gFunctionName, __FILE__, __LINE__, msg)" << endl;
779 }
780 code << "#endif" << endl;
781
782 code << "class " << name << " :" << endl;
783 // Add appropriate #ifdef sections since we need to prevent inheritance from
784 // AliHLTGlobalTrigger. CINT does not seem to support multiple inheritance nor
785 // multiple levels of inheritance. Neither of the following schemes worked:
786 //
787 // 1) class AliHLTGlobalTrigger : public AliHLTLogging {};
788 // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger {};
789 //
790 // 2) class AliHLTGlobalTrigger {};
791 // class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger, public AliHLTLogging {};
792 //
793 // Thus, we are forced to just inherit from AliHLTLogging when running in the CINT
794 // interpreter. But we anyway have to call the global trigger implementation class
795 // through the AliHLTGlobalTriggerWrapper so this is not such a problem.
796 code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
797 code << " public AliHLTGlobalTrigger," << endl;
798 code << "#endif" << endl;
799 code << " public AliHLTLogging" << endl;
e2bb8ddd 800 code << "{" << endl;
801 code << "public:" << endl;
52f67e50 802
81d62bb4 803 // Generate constructor method.
804 code << " " << name << "() :" << endl;
805 code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
806 code << " AliHLTGlobalTrigger()," << endl;
807 code << "#endif" << endl;
808 code << " AliHLTLogging()";
52f67e50 809 // Write the symbols in the trigger menu in the initialisation list.
810 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
811 {
812 code << "," << endl;
813 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
814 code << " " << symbol->Name() << "()," << endl;
815 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
816 {
817 code << " " << symbol->Name() << "TriggerDomain()," << endl;
818 }
819 code << " " << symbol->Name() << "DomainEntry(kAliHLTAnyDataType)";
820 }
821 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
822 {
823 code << "," << endl << " fMenuItemDescription" << i << "()";
824 }
825 code << endl << " {" << endl;
826 if (fDebugMode)
827 {
81d62bb4 828 code << "#ifdef __CINT__" << endl;
829 code << " gFunctionName = \"" << name.Data() <<"\";" << endl;
830 code << "#endif" << endl;
52f67e50 831 code << " SetLocalLoggingLevel(kHLTLogAll);" << endl;
81d62bb4 832 code << " HLTDebug(Form(\"Creating new instance at %p.\", this));" << endl;
52f67e50 833 }
e2bb8ddd 834 code << " }" << endl;
52f67e50 835
e2bb8ddd 836 code << " virtual ~" << name << "() {" << endl;
52f67e50 837 if (fDebugMode)
838 {
81d62bb4 839 code << "#ifdef __CINT__" << endl;
840 code << " gFunctionName = \"~" << name.Data() << "\";" << endl;
841 code << "#endif" << endl;
842 code << " HLTDebug(Form(\"Deleting instance at %p.\", this));" << endl;
52f67e50 843 }
e2bb8ddd 844 code << " }" << endl;
52f67e50 845
846 // Generate the FillFromMenu method.
847 code << " virtual void FillFromMenu(const AliHLTTriggerMenu& menu) {" << endl;
848 if (fDebugMode)
849 {
81d62bb4 850 code << "#ifdef __CINT__" << endl;
851 code << " gFunctionName = \"FillFromMenu\";" << endl;
852 code << "#endif" << endl;
853 code << " HLTDebug(Form(\"Filling description entries from trigger menu for global trigger %p.\", this));" << endl;
52f67e50 854 }
81d62bb4 855 code << " fCounter.Set(menu.NumberOfItems());" << endl;
52f67e50 856 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
857 {
858 code << " fMenuItemDescription" << i << " = (menu.Item(" << i
859 << ") != NULL) ? menu.Item(" << i << ")->Description() : \"\";" << endl;
860 }
861 if (fDebugMode)
862 {
81d62bb4 863 code << " HLTDebug(Form(\"Finished filling description entries from trigger menu.\"));" << endl;
864 code << " HLTDebug(Form(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this));" << endl;
52f67e50 865 }
866 code << " for (Int_t i = 0; i < menu.SymbolArray().GetEntriesFast(); i++) {" << endl;
325e5e42 867 // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly.
868 // Have to use the TClass system for extra protection.
869 code << " if (menu.SymbolArray().UncheckedAt(i) == NULL) continue;" << endl;
870 code << " if (menu.SymbolArray().UncheckedAt(i)->IsA() != AliHLTTriggerMenuSymbol::Class()) continue;" << endl;
52f67e50 871 code << " const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const"
872 " AliHLTTriggerMenuSymbol*>(menu.SymbolArray().UncheckedAt(i));" << endl;
873 code << " if (symbol == NULL) continue;" << endl;
874 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
875 {
876 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
025443e0 877 code << " if (strcmp(symbol->RealName(), \"" << symbol->RealName() << "\") == 0) {" << endl;
52f67e50 878 if (fDebugMode)
879 {
81d62bb4 880 code << " HLTDebug(Form(\"Assinging domain entry value corresponding with symbol '%s' to '%s'.\","
025443e0 881 " symbol->RealName(), symbol->BlockType().AsString().Data()));" << endl;
52f67e50 882 }
883 code << " " << symbol->Name() << "DomainEntry = symbol->BlockType();" << endl;
884 code << " continue;" << endl;
885 code << " }" << endl;
886 }
887 code << " }" << endl;
2f251ae6 888 // The following is an optimisation where symbols without any assignment operators
889 // are treated as constant and only initialised in FillFromMenu rather than reseting
890 // them in the NewEvent method.
891 // Note: we putting this initialisation into the constructor can lead to seg faults
892 // under CINT interpretation. Thus we must put it into the FillFromMenu method instead.
893 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
894 {
895 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
896 if (TString(symbol->AssignExpression()) != "") continue;
897 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0) continue;
898 // CINT has problems with the implicit equals operator for complex types, so if
899 // the type has an equals operater we need to write the operator call explicitly.
900 TClass* clas = TClass::GetClass(symbol->Type());
901 if (clas != NULL and clas->GetMethodAny("operator=") != NULL)
902 {
903 code << " " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl;
904 }
905 else
906 {
907 code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl;
908 }
909 }
52f67e50 910 if (fDebugMode)
911 {
81d62bb4 912 code << " HLTDebug(Form(\"Finished filling domain entries from trigger menu symbols.\"));" << endl;
52f67e50 913 }
e2bb8ddd 914 code << " }" << endl;
52f67e50 915
916 // Generate the NewEvent method.
917 code << " virtual void NewEvent() {" << endl;
918 if (fDebugMode)
919 {
81d62bb4 920 code << "#ifdef __CINT__" << endl;
921 code << " gFunctionName = \"NewEvent\";" << endl;
922 code << "#endif" << endl;
923 code << " HLTDebug(Form(\"New event for global trigger object %p, initialising variables to default values.\", this));" << endl;
52f67e50 924 }
925 // Write code to initialise the symbols in the trigger menu to their default values.
926 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
927 {
928 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
2f251ae6 929 // The following is an optimisation. If the symbol does not have an assignment expression
930 // then it is effectively a constant symbol and can be initialised earlier and only once.
931 // In this case we initialise it in the FillFromMenu method instead.
932 if (TString(symbol->AssignExpression()) == "") continue;
81d62bb4 933 // CINT has problems with the implicit equals operator for complex types, so if
2f251ae6 934 // the type has an equals operater we need to write the operator call explicitly.
81d62bb4 935 TClass* clas = TClass::GetClass(symbol->Type());
936 if (clas != NULL and clas->GetMethodAny("operator=") != NULL)
937 {
938 code << " " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl;
939 }
940 else
941 {
942 code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl;
943 }
52f67e50 944 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
945 {
946 code << " " << symbol->Name() << "TriggerDomain.Clear();" << endl;
947 }
948 }
949 if (fDebugMode)
950 {
81d62bb4 951 code << " HLTDebug(Form(\"Finished initialising variables.\"));" << endl;
52f67e50 952 }
e2bb8ddd 953 code << " }" << endl;
52f67e50 954
955 // Generate the Add method.
8066c87b 956 bool haveAssignments = false;
957 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
958 {
959 // First check if we have any symbols with assignment expressions.
960 // This is needed to get rid of the on the fly compilation warning about '_object_' not being used.
961 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
962 TString expr = symbol->AssignExpression();
963 if (expr == "") continue; // Skip entries that have no assignment expression.
964 haveAssignments = true;
965 break;
966 }
967 if (haveAssignments or fDebugMode)
968 {
969 code << " virtual void Add(const TObject* _object_, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl;
970 }
971 else
972 {
973 code << " virtual void Add(const TObject* /*_object_*/, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl;
974 }
81d62bb4 975 if (fDebugMode)
976 {
977 code << "#ifdef __CINT__" << endl;
978 code << " gFunctionName = \"Add\";" << endl;
979 code << "#endif" << endl;
980 }
52f67e50 981 code << " AliHLTDomainEntry _type_spec_(_type_, _spec_);" << endl;
982 if (fDebugMode)
983 {
81d62bb4 984 code << " HLTDebug(Form(\"Adding TObject %p, with class name '%s' from data block"
52f67e50 985 " '%s', to global trigger object %p\", _object_, _object_->ClassName(),"
81d62bb4 986 " _type_spec_.AsString().Data(), this));" << endl;
52f67e50 987 code << " _object_->Print();" << endl;
988 code << " bool _object_assigned_ = false;" << endl;
989 }
990 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
991 {
992 // Write code to check if the block type, specification and class name is correct.
993 // Then write code to assign the variable from the input object.
994 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
995 TString expr = symbol->AssignExpression();
996 if (expr == "") continue; // Skip entries that have no assignment expression.
997 bool isTrigDecision = strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0;
998 if (fDebugMode)
999 {
1000 if (isTrigDecision)
1001 {
81d62bb4 1002 code << " HLTDebug(Form(\"Trying to match input object to class '"
f48dcb4e 1003 << symbol->ObjectClass() << "', trigger name '" << symbol->RealName()
52f67e50 1004 << "' and block type '%s'\", " << symbol->Name()
81d62bb4 1005 << "DomainEntry.AsString().Data()));" << endl;
52f67e50 1006 }
1007 else
1008 {
81d62bb4 1009 code << " HLTDebug(Form(\"Trying to match input object to class '"
52f67e50 1010 << symbol->ObjectClass() << "' and block type '%s'\", "
81d62bb4 1011 << symbol->Name() << "DomainEntry.AsString().Data()));" << endl;
52f67e50 1012 }
1013 }
325e5e42 1014 // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly.
1015 // Have to use the TClass system for extra protection.
1016 code << " const " << symbol->ObjectClass() << "* " << symbol->Name() << "_object_ = NULL;" << endl;
f1cac1f9 1017 code << " if (_object_->InheritsFrom(" << symbol->ObjectClass() << "::Class())) " << symbol->Name()
52f67e50 1018 << "_object_ = dynamic_cast<const " << symbol->ObjectClass()
1019 << "*>(_object_);" << endl;
81d62bb4 1020 code << " if (" << symbol->Name() << "_object_ != NULL && ";
52f67e50 1021 if (isTrigDecision)
1022 {
1023 code << "strcmp(" << symbol->Name() << "_object_->Name(), \""
e9f09f57 1024 << symbol->RealName() << "\") == 0 && ";
52f67e50 1025 }
1026 code << symbol->Name() << "DomainEntry == _type_spec_) {" << endl;
1027 TString fullname = symbol->Name();
1028 fullname += "_object_";
1029 expr.ReplaceAll("this", fullname);
1030 code << " this->" << symbol->Name() << " = " << expr.Data() << ";" << endl;
1031 if (isTrigDecision)
1032 {
1033 code << " this->" << symbol->Name() << "TriggerDomain = "
1034 << fullname.Data() << "->TriggerDomain();" << endl;
1035 }
1036 if (fDebugMode)
1037 {
81d62bb4 1038 code << " HLTDebug(Form(\"Added TObject %p with class name '%s' to variable "
1039 << symbol->Name() << "\", _object_, _object_->ClassName()));" << endl;
52f67e50 1040 code << " _object_assigned_ = true;" << endl;
1041 }
1042 code << " }" << endl;
1043 }
1044 if (fDebugMode)
1045 {
81d62bb4 1046 code << " if (! _object_assigned_) {" << endl;
1047 code << " HLTDebug(Form(\"Did not assign TObject %p"
1048 " with class name '%s' to any variable.\", _object_, _object_->ClassName()));"
52f67e50 1049 << endl;
81d62bb4 1050 code << " }" << endl;
52f67e50 1051 }
e2bb8ddd 1052 code << " }" << endl;
52f67e50 1053
1054 // Generate the CalculateTriggerDecision method.
81d62bb4 1055 // This requires code to be generated that checks which items in the trigger menu
1056 // have their conditions asserted and then the trigger domain is generated from
1057 // those fired items.
1058 // The processing will start from the highest priority trigger group and stop
1059 // after at least one trigger from the current priority group being processed
1060 // is positive. For each priority group all the trigger menu items are checked.
1061 // Their combined trigger condition expression must be true for the trigger priority
1062 // group to be triggered positive. The full condition expression is formed by
1063 // concatenating the individual condition expressions. If no trailing operators are
1064 // used in the individual expressions then the default condition operator is placed
1065 // between two concatenated condition expressions.
1066 // If a trigger priority group has at least one trigger fired then the trigger domain
1067 // is calculated such that it will give the same result as the concatenated trigger
1068 // domain merging expressions for all the individual trigger menu items with
1069 // positive results. Again, if no trailing operators are used in the individual
1070 // merging expressions then the default domain operator is placed between two
1071 // expression fragments.
52f67e50 1072 code << " virtual bool CalculateTriggerDecision(AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl;
1073 if (fDebugMode)
1074 {
81d62bb4 1075 code << "#ifdef __CINT__" << endl;
1076 code << " gFunctionName = \"CalculateTriggerDecision\";" << endl;
1077 code << "#endif" << endl;
1078 code << " HLTDebug(Form(\"Calculating global HLT trigger result with trigger object at %p.\", this));" << endl;
52f67e50 1079 }
81d62bb4 1080
1081 // Build a list of priorities used in the trigger menu.
1082 std::vector<UInt_t> priorities;
52f67e50 1083 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
1084 {
1085 const AliHLTTriggerMenuItem* item = menu->Item(i);
81d62bb4 1086 bool priorityNotInList = std::find(priorities.begin(), priorities.end(), item->Priority()) == priorities.end();
1087 if (priorityNotInList) priorities.push_back(item->Priority());
1088 }
1089 std::sort(priorities.begin(), priorities.end(), AliHLTDescendingNumbers);
1090 // From the priority list, build the priority groups in the correct order,
1091 // i.e. highest priority first.
1092 // The priority group is a list of vectors of integers. The integers are the
1093 // index numbers into the trigger menu item list for the items which form part
1094 // of the priority group.
1095 std::vector<std::vector<Int_t> > priorityGroup;
1096 priorityGroup.insert(priorityGroup.begin(), priorities.size(), std::vector<Int_t>());
1097 for (size_t n = 0; n < priorities.size(); n++)
1098 {
1099 UInt_t priority = priorities[n];
1100 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
52f67e50 1101 {
81d62bb4 1102 const AliHLTTriggerMenuItem* item = menu->Item(i);
1103 if (item->Priority() == priority) priorityGroup[n].push_back(i);
52f67e50 1104 }
81d62bb4 1105 }
1106
1107 for (size_t n = 0; n < priorityGroup.size(); n++)
1108 {
52f67e50 1109 if (fDebugMode)
1110 {
81d62bb4 1111 code << " HLTDebug(Form(\"Processing trigger priority group " << priorities[n] << "\"));" << endl;
52f67e50 1112 }
81d62bb4 1113 code << " ";
1114 if (n == 0) code << "UInt_t ";
1115 code << "_previous_match_ = 0xFFFFFFFF;" << endl;
1116 code << " ";
1117 if (n == 0) code << "bool ";
1118 code << "_trigger_matched_ = false;" << endl;
1119 code << " ";
1120 if (n == 0) code << "bool ";
1121 code << "_group_result_ = false;" << endl;
1122 std::vector<TString> conditionOperator;
1123 conditionOperator.insert(conditionOperator.begin(), priorityGroup[n].size(), TString(""));
1124 std::vector<TString> domainOperator;
1125 domainOperator.insert(domainOperator.begin(), priorityGroup[n].size(), TString(""));
1126 for (size_t m = 0; m < priorityGroup[n].size(); m++)
52f67e50 1127 {
81d62bb4 1128 UInt_t i = priorityGroup[n][m];
1129 const AliHLTTriggerMenuItem* item = menu->Item(i);
1130 TString triggerCondition = item->TriggerCondition();
1131 TString mergeExpr = item->MergeExpression();
1132 // Replace the symbols found in the trigger condition and merging expressions
1133 // with appropriate compilable versions.
1134 for (Int_t j = 0; j < symbols.GetEntriesFast(); j++)
1135 {
1136 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) );
1137 bool symbolNamesDifferent = strcmp(symbol->RealName(), symbol->Name()) != 0;
1138 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
1139 {
1140 TString newname = symbol->Name();
1141 newname += "TriggerDomain";
1142 mergeExpr.ReplaceAll(symbol->RealName(), newname);
1143 }
1144 else
1145 {
1146 if (symbolNamesDifferent) mergeExpr.ReplaceAll(symbol->RealName(), symbol->Name());
1147 }
1148 if (symbolNamesDifferent) triggerCondition.ReplaceAll(symbol->RealName(), symbol->Name());
1149 }
1150 // We allow the trigger conditions and merging expressions to have trailing operators.
1151 // Thus, we need to extract the operators and cleanup the expressions so that they will
1152 // compile. This means that we silently ignore the trailing operator if not needed.
1153 if (ExtractedOperator(triggerCondition, conditionOperator[m]))
1154 {
1155 // If the trailing operator is the same as the default operator then reset
1156 // the value in the operator list so that the default is used in the generated
1157 // code. This creates more compact code.
1158 if (conditionOperator[m] == menu->DefaultConditionOperator()) conditionOperator[m] = "";
1159 }
1160 if (ExtractedOperator(mergeExpr, domainOperator[m]))
1161 {
1162 if (domainOperator[m] == menu->DefaultDomainOperator()) domainOperator[m] = "";
1163 }
1164 if (fDebugMode)
1165 {
1166 code << " HLTDebug(Form(\"Trying trigger condition " << i
1167 << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));"
1168 << endl;
1169 }
1170 code << " ";
1171 if (n == 0 and m == 0) code << "bool ";
1172 code << "_item_result_ = false;" << endl;
1173 code << " if (" << triggerCondition << ") {" << endl;
1174 code << " ++fCounter[" << i << "];" << endl;
1175 const char* indentation = "";
1176 if (item->PreScalar() != 0)
1177 {
1178 indentation = " ";
1179 code << " if ((fCounter[" << i << "] % " << item->PreScalar() << ") == 1) {" << endl;
1180 }
1181 code << indentation << " _item_result_ = true;" << endl;
1182 if (fDebugMode)
1183 {
1184 code << indentation << " HLTDebug(Form(\"Matched trigger condition " << i
1185 << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" << endl;
1186 }
1187 if (item->PreScalar() != 0)
1188 {
1189 code << " }" << endl;
1190 }
1191 code << " }" << endl;
1192 if (m == 0)
1193 {
1194 // Since this is the first item of the trigger group,
1195 // the generated trigger logic can be simplified a little.
1196 code << " _group_result_ = _item_result_;" << endl;
1197 code << " if (_item_result_) {" << endl;
1198 code << " _domain_ = " << mergeExpr.Data() << ";" << endl;
1199 code << " _description_ = fMenuItemDescription" << i << ";" << endl;
1200 code << " _previous_match_ = " << i << ";" << endl;
1201 code << " _trigger_matched_ = true;" << endl;
1202 code << " }" << endl;
1203 }
1204 else
1205 {
325e5e42 1206 if (conditionOperator[m-1] == "")
81d62bb4 1207 {
1208 code << " _group_result_ = _group_result_ "
1209 << menu->DefaultConditionOperator() << " _item_result_;" << endl;
1210 }
1211 else
1212 {
325e5e42 1213 code << " _group_result_ = _group_result_ "
1214 << conditionOperator[m-1] << " _item_result_;" << endl;
81d62bb4 1215 }
1216 code << " if (_item_result_) {" << endl;
1217 code << " if (_trigger_matched_) {" << endl;
325e5e42 1218 bool switchWillBeEmpty = true;
81d62bb4 1219 for (size_t k = 0; k < m; k++)
1220 {
1221 if (domainOperator[k] == "") continue;
1222 switchWillBeEmpty = false;
1223 }
1224 if (switchWillBeEmpty)
1225 {
1226 code << " _domain_ = _domain_ " << menu->DefaultDomainOperator() << " "
1227 << mergeExpr.Data() << ";" << endl;
1228 }
1229 else
1230 {
1231 code << " switch(_previous_match_) {" << endl;
1232 for (size_t k = 0; k < m; k++)
1233 {
1234 if (domainOperator[k] == "") continue;
1235 code << " case " << k << ": _domain_ = _domain_ "
1236 << domainOperator[k] << " " << mergeExpr.Data() << "; break;" << endl;
1237 }
1238 code << " default: _domain_ = _domain_ "
1239 << menu->DefaultDomainOperator() << " " << mergeExpr.Data() << ";" << endl;
1240 code << " }" << endl;
1241 }
1242 code << " _description_ += \",\";" << endl;
1243 code << " _description_ += fMenuItemDescription" << i << ";" << endl;
1244 code << " } else {" << endl;
1245 code << " _domain_ = " << mergeExpr.Data() << ";" << endl;
1246 code << " _description_ = fMenuItemDescription" << i << ";" << endl;
1247 code << " }" << endl;
1248 code << " _previous_match_ = " << i << ";" << endl;
1249 code << " _trigger_matched_ = true;" << endl;
1250 code << " }" << endl;
1251 }
52f67e50 1252 }
81d62bb4 1253 code << " if (_group_result_) {" << endl;
52f67e50 1254 if (fDebugMode)
1255 {
81d62bb4 1256 if (n < priorities.size() - 1)
1257 {
1258 code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n]
1259 << ". Stopping processing here because all other trigger groups have lower priority.\"));" << endl;
1260 }
1261 else
1262 {
1263 code << " HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] << ".\"));" << endl;
1264 }
52f67e50 1265 }
81d62bb4 1266 code << " return true;" << endl;
52f67e50 1267 code << " }" << endl;
1268 }
81d62bb4 1269 code << " _domain_.Clear();" << endl;
1270 code << " _description_ = \"\";" << endl;
1271 code << " return false;" << endl;
e2bb8ddd 1272 code << " }" << endl;
52f67e50 1273
81d62bb4 1274 // Generate getter and setter methods for the counters.
1275 code << " const TArrayL64& GetCounters() const { return fCounter; }" << endl;
1276 code << " void SetCounters(const TArrayL64& counters) { fCounter = counters; }" << endl;
52f67e50 1277
e2bb8ddd 1278 code << "private:" << endl;
52f67e50 1279 // Add the symbols in the trigger menu to the list of private variables.
1280 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
1281 {
1282 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
1283 code << " " << symbol->Type() << " " << symbol->Name() << ";" << endl;
1284 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
1285 {
1286 code << " AliHLTTriggerDomain " << symbol->Name() << "TriggerDomain;" << endl;
1287 }
1288 code << " AliHLTDomainEntry " << symbol->Name() << "DomainEntry;" << endl;
1289 }
1290 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
1291 {
1292 code << " TString fMenuItemDescription" << i << ";" << endl;
1293 }
81d62bb4 1294 code << " TArrayL64 fCounter;" << endl;
1295 code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
1296 code << " ClassDef(" << name.Data() << ", 0)" << endl;
1297 code << "#else" << endl;
1298 code << " virtual const char* Class_Name() const { return \"" << name.Data() << "\"; }" << endl;
1299 code << "#endif" << endl;
e2bb8ddd 1300 code << "};" << endl;
81d62bb4 1301 code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
1302 code << "ClassImp(" << name.Data() << ")" << endl;
1303 code << "#endif" << endl;
e2bb8ddd 1304
1305 code.close();
1306
52f67e50 1307 // Now we need to compile and load the new class.
81d62bb4 1308 result = LoadTriggerClass(filename, includePaths);
52f67e50 1309 return result;
1310}
1311
1312
1313int AliHLTGlobalTriggerComponent::LoadTriggerClass(
1314 const char* filename, const TClonesArray& includePaths
1315 )
1316{
1317 // Loads the code for a custom global trigger class implementation on the fly.
1318
81d62bb4 1319 HLTDebug("Loading HLT trigger class from file '%s'.", filename);
1320
52f67e50 1321 TString compiler = gSystem->GetBuildCompilerVersion();
566a01d0 1322 if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc")))
1323 {
1324 TString includePath;
1325#if defined(PKGINCLUDEDIR)
1326 // this is especially for the HLT build system where the package is installed
1327 // in a specific directory including proper treatment of include files
1328 includePath.Form("-I%s", PKGINCLUDEDIR);
1329#else
1330 // the default AliRoot behavior, all include files can be found in the
1331 // $ALICE_ROOT subfolders
1332 includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger";
1333#endif
52f67e50 1334 // Add any include paths that were specified on the command line.
1335 for (Int_t i = 0; i < includePaths.GetEntriesFast(); i++)
1336 {
7da191b6 1337 TString path = static_cast<TObjString*>(includePaths.UncheckedAt(i))->String();
81d62bb4 1338 includePath += " -I";
52f67e50 1339 includePath += path;
1340 }
81d62bb4 1341 HLTDebug("using include settings: %s", includePath.Data());
52f67e50 1342 gSystem->SetIncludePath(includePath);
1343 gSystem->SetFlagsOpt("-O3 -DNDEBUG");
1344 gSystem->SetFlagsDebug("-g3 -DDEBUG -D__DEBUG");
1345
1346 int result = kTRUE;
1347 if (fDebugMode)
1348 {
1349 result = gSystem->CompileMacro(filename, "g");
1350 }
1351 else
1352 {
1353 result = gSystem->CompileMacro(filename, "O");
1354 }
1355 if (result != kTRUE)
1356 {
1357 HLTFatal("Could not compile and load global trigger menu implementation.");
1358 return -ENOENT;
1359 }
1360 }
1361 else
1362 {
1b820a65 1363 // Store the library state to be checked later in UnloadTriggerClass.
1364 fLibStateAtLoad = gSystem->GetLibraries();
1365
52f67e50 1366 // If we do not support the compiler then try interpret the class instead.
81d62bb4 1367 TString cmd = ".L ";
52f67e50 1368 cmd += filename;
1369 Int_t errorcode = TInterpreter::kNoError;
1370 gROOT->ProcessLine(cmd, &errorcode);
1371 if (errorcode != TInterpreter::kNoError)
1372 {
1373 HLTFatal("Could not load interpreted global trigger menu implementation"
1374 " (Interpreter error code = %d).",
1375 errorcode
1376 );
1377 return -ENOENT;
1378 }
1379 }
1380
1381 return 0;
1382}
1383
1384
81d62bb4 1385int AliHLTGlobalTriggerComponent::UnloadTriggerClass(const char* filename)
1386{
1387 // Unloads the code previously loaded by LoadTriggerClass.
1388
1389 HLTDebug("Unloading HLT trigger class in file '%s'.", filename);
1390
1391 TString compiler = gSystem->GetBuildCompilerVersion();
1392 if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc")))
1393 {
1b820a65 1394 // Generate the library name.
81d62bb4 1395 TString libname = filename;
1396 Ssiz_t dotpos = libname.Last('.');
1397 if (0 <= dotpos and dotpos < libname.Length()) libname[dotpos] = '_';
1398 libname += ".";
1399 libname += gSystem->GetSoExt();
1400
1b820a65 1401 // This is a workaround for a problem with unloading shared libraries in ROOT.
1402 // If the trigger logic library is loaded before the libAliHLTHOMER.so library
1403 // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile
1404 // call all the subsequent libraries get unloded. This means that any objects created
1405 // from classes implemented in the libAliHLTHOMER.so library will generate segfaults
1406 // since the executable code has been unloaded.
1407 // We need to check if there are any more libraries loaded after the class we
1408 // are unloading and in that case don't unload the class.
1409 TString libstring = gSystem->GetLibraries();
1410 TString token, lastlib;
1411 Ssiz_t from = 0;
1412 Int_t numOfLibs = 0, posOfLib = -1;
1413 while (libstring.Tokenize(token, from, " "))
1414 {
1415 ++numOfLibs;
1416 lastlib = token;
1417 if (token.Contains(libname)) posOfLib = numOfLibs;
1418 }
1419 if (numOfLibs != posOfLib)
1420 {
1421 HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the shared"
1422 " library '%s' since another library '%s' was loaded afterwards. Trying to"
1423 " unload this library will remove the others and lead to serious memory faults.",
1424 libname.Data(), lastlib.Data()
1425 ));
1426 return 0;
1427 }
1428
81d62bb4 1429 char* path = NULL;
1430 int result = 0;
1431 if ((path = gSystem->DynamicPathName(libname)) != NULL)
1432 {
1433 result = gInterpreter->UnloadFile(path);
1434 delete [] path;
1435 }
1436 if (result != TInterpreter::kNoError) return -ENOENT;
1437 }
1438 else
1439 {
1b820a65 1440 // This is again a workaround for the problem with unloading files in ROOT.
1441 // If the trigger logic class is loaded before the libAliHLTHOMER.so library
1442 // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile
1443 // call all the subsequent libraries get unloded.
1444 // We need to check if the list of loaded libraries has changed since the last
1445 // call to LoadTriggerClass. If it has then don't unload the class.
1446 if (fLibStateAtLoad != gSystem->GetLibraries())
1447 {
1448 TString libstring = gSystem->GetLibraries();
1449 TString token;
1450 Ssiz_t from = 0;
1451 while (libstring.Tokenize(token, from, " "))
1452 {
1453 if (not fLibStateAtLoad.Contains(token)) break;
1454 }
1455 HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the file"
1456 " '%s' since another library '%s' was loaded afterwards. Trying to unload"
1457 " this file will remove the other library and lead to serious memory faults.",
1458 filename, token.Data()
1459 ));
1460 return 0;
1461 }
1462
1463 // If we did not compile the trigger logic then remove the interpreted class.
81d62bb4 1464 TString cmd = ".U ";
1465 cmd += filename;
1466 Int_t errorcode = TInterpreter::kNoError;
1467 gROOT->ProcessLine(cmd, &errorcode);
1468 if (errorcode != TInterpreter::kNoError)
1469 {
1470 HLTFatal("Could not unload interpreted global trigger menu implementation"
1471 " (Interpreter error code = %d).",
1472 errorcode
1473 );
1474 return -ENOENT;
1475 }
1476 }
1477
1478 return 0;
1479}
1480
1481
52f67e50 1482int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArray& list)
1483{
1484 // Searches for the named symbol in the given list.
1485 // See header for more details.
1486
1487 for (int i = 0; i < list.GetEntriesFast(); i++)
1488 {
1489 const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const AliHLTTriggerMenuSymbol*>( list.UncheckedAt(i) );
1490 if (symbol == NULL) continue;
1491 if (strcmp(symbol->Name(), name) == 0) return i;
1492 }
1493 return -1;
1494}
1495
1496
1497int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu, TClonesArray& list)
1498{
1499 // Builds the list of symbols to use in the custom global trigger menu
1500 // implementation class.
1501 // See header for more details.
1502
025443e0 1503 // Note: when we build the symbol list we must use the symbol name as returned
1504 // by the Name() method and not the RealName() method when using FindSymbol.
1505 // This is so that we avoid problems with the generated code not compiling
1506 // because names like "abc-xyz" and "abc_xyz" are synonymous.
1507 // Name() returns the converted C++ symbol name as used in the generated code.
1508
52f67e50 1509 for (UInt_t i = 0; i < menu->NumberOfSymbols(); i++)
1510 {
1511 const AliHLTTriggerMenuSymbol* symbol = menu->Symbol(i);
1512 if (FindSymbol(symbol->Name(), list) != -1)
1513 {
1514 HLTError("Multiple symbols with the name '%s' defined in the trigger menu.", symbol->Name());
1515 return -EIO;
1516 }
dda123c3 1517 try
1518 {
1519 new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(*symbol);
1520 }
1521 catch (const std::bad_alloc&)
1522 {
1523 HLTError("Could not allocate more memory for the symbols list when adding a trigger menu symbol.");
1524 return -ENOMEM;
1525 }
52f67e50 1526 }
1527
f48dcb4e 1528 TRegexp exp("[_a-zA-Z][-_a-zA-Z0-9]*");
3da1c6d7 1529 TRegexp hexexp("x[a-fA-F0-9]+");
52f67e50 1530 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
1531 {
1532 const AliHLTTriggerMenuItem* item = menu->Item(i);
81d62bb4 1533 TString str = item->TriggerCondition();
52f67e50 1534 Ssiz_t start = 0;
1535 do
1536 {
1537 Ssiz_t length = 0;
1538 Ssiz_t pos = exp.Index(str, &length, start);
1539 if (pos == kNPOS) break;
52f67e50 1540 start = pos+length;
1541
3da1c6d7 1542 // Check if there is a numerical character before the found
1543 // regular expression. If so, then the symbol is not a valid one
1544 // and should be skipped.
1545 if (pos > 0)
1546 {
1547 bool notValid = false;
1548 switch (str[pos-1])
1549 {
1550 case '0': case '1': case '2': case '3': case '4':
1551 case '5': case '6': case '7': case '8': case '9':
1552 notValid = true;
1553 break;
1554 default:
1555 notValid = false;
1556 break;
1557 }
1558 if (notValid) continue;
1559 }
1560 TString s = str(pos, length);
1561
52f67e50 1562 if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or
1563 s == "compl" or s == "not" or s == "not_eq" or s == "or" or
1564 s == "or_eq" or s == "xor" or s == "xor_eq" or s == "true" or
1565 s == "false"
1566 )
1567 {
1568 // Ignore iso646.h and other keywords.
1569 continue;
1570 }
3da1c6d7 1571
025443e0 1572 // Need to create the symbols first and check if its name is in the list
1573 // before actually adding it to the symbols list.
1574 AliHLTTriggerMenuSymbol newSymbol;
1575 newSymbol.Name(s.Data());
1576 newSymbol.Type("bool");
1577 newSymbol.ObjectClass("AliHLTTriggerDecision");
1578 newSymbol.AssignExpression("this->Result()");
1579 newSymbol.DefaultValue("false");
1580 if (FindSymbol(newSymbol.Name(), list) == -1)
52f67e50 1581 {
dda123c3 1582 try
1583 {
1584 new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(newSymbol);
1585 }
1586 catch (const std::bad_alloc&)
1587 {
1588 HLTError("Could not allocate more memory for the symbols list when adding a trigger name symbol.");
1589 return -ENOMEM;
1590 }
52f67e50 1591 }
1592 }
1593 while (start < str.Length());
1594 }
1595
e2bb8ddd 1596 return 0;
1597}
1598
81d62bb4 1599
1600bool AliHLTGlobalTriggerComponent::ExtractedOperator(TString& expr, TString& op)
1601{
1602 // Extracts the trailing operator from the expression.
1603
1604 Ssiz_t i = 0;
1605 // First skip the trailing whitespace.
1606 bool whitespace = true;
1607 for (i = expr.Length()-1; i >= 0 and whitespace; i--)
1608 {
1609 switch (expr[i])
1610 {
1611 case ' ': case '\t': case '\r': case '\n':
1612 whitespace = true;
1613 break;
1614 default:
1615 whitespace = false;
1616 }
1617 }
1618 if (i < 0 or whitespace) return false;
1619
1620 // Now find the first whitespace character before the trailing symbol.
1621 bool nonwhitespace = true;
1622 for (; i >= 0 and nonwhitespace; i--)
1623 {
1624 switch (expr[i])
1625 {
1626 case ' ': case '\t': case '\r': case '\n':
1627 nonwhitespace = false;
1628 break;
1629 default:
1630 nonwhitespace = true;
1631 }
1632 }
1633 if (i < 0 or nonwhitespace) return false;
1634
1635 // Extract the last symbols and check if it is a valid operator.
1636 TString s = expr;
1637 s.Remove(0, i+2);
1638 if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or
1639 s == "compl" or s == "not" or s == "not_eq" or s == "or" or
1640 s == "or_eq" or s == "xor" or s == "xor_eq" or s == "&&" or
1641 s == "&=" or s == "&" or s == "|" or s == "~" or s == "!" or
1642 s == "!=" or s == "||" or s == "|=" or s == "^" or s == "^=" or
1643 s == "==" or s == "+" or s == "-" or s == "*" or s == "/" or
1644 s == "%" or s == ">" or s == "<" or s == ">=" or s == "<="
1645 )
1646 {
1647 expr.Remove(i+1);
1648 op = s;
1649 return true;
1650 }
1651
1652 return false;
1653}
1654
1655
2974f8dc 1656int AliHLTGlobalTriggerComponent::PrintStatistics(const AliHLTGlobalTrigger* pTrigger, AliHLTComponentLogSeverity level, int offset) const
1657{
1658 // print some statistics
8b29f84f 1659 int totalEvents=GetEventCount()+offset;
325e5e42 1660 const TArrayL64& counters = pTrigger->GetCounters();
1661 if (pTrigger->CallFailed()) return -EPROTO;
1662 for (int i = 0; i < counters.GetSize(); i++) {
1663 ULong64_t count = counters[i];
8b29f84f 1664 float ratio=0;
1665 if (totalEvents>0) ratio=100*(float)count/totalEvents;
81d62bb4 1666 HLTLog(level, "Item %d: total events: %d - counted events: %llu (%.1f%%)", i, totalEvents, count, ratio);
2974f8dc 1667 }
2974f8dc 1668 return 0;
1669}
566a01d0 1670
1671int AliHLTGlobalTriggerComponent::AddCTPDecisions(AliHLTGlobalTrigger* pTrigger, const AliHLTCTPData* pCTPData, const AliHLTComponentTriggerData* trigData)
1672{
1673 // add trigger decisions for the valid CTP classes
1674 if (!pCTPData || !pTrigger) return 0;
1675
1676 AliHLTUInt64_t triggerMask=pCTPData->Mask();
1677 AliHLTUInt64_t bit0=0x1;
1678 if (!fCTPDecisions) {
dda123c3 1679 try
1680 {
1681 fCTPDecisions=new TClonesArray(AliHLTTriggerDecision::Class(), gkNCTPTriggerClasses);
1682 }
1683 catch (const std::bad_alloc&)
1684 {
1685 HLTError("Could not allocate memory for the CTP decisions array.");
1686 return -ENOMEM;
1687 }
566a01d0 1688 if (!fCTPDecisions) return -ENOMEM;
1689
dda123c3 1690 try
1691 {
1692 fCTPDecisions->ExpandCreate(gkNCTPTriggerClasses);
1693 }
1694 catch (const std::bad_alloc&)
1695 {
1696 HLTError("Could not allocate more memory for the CTP decisions array.");
1697 return -ENOMEM;
1698 }
566a01d0 1699 for (int i=0; i<gkNCTPTriggerClasses; i++) {
1700 const char* name=pCTPData->Name(i);
1701 if (triggerMask&(bit0<<i) && name) {
1702 AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i));
1703 assert(pDecision);
1704 if (!pDecision) {
1705 delete fCTPDecisions;
1706 fCTPDecisions=NULL;
1707 return -ENOENT;
1708 }
1709 pDecision->Name(name);
1710 }
1711 }
1712 }
1713
1714 for (int i=0; i<gkNCTPTriggerClasses; i++) {
1715 const char* name=pCTPData->Name(i);
1716 if ((triggerMask&(bit0<<i))==0 || name==NULL) continue;
1717 AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i));
1718 HLTDebug("updating CTP trigger decision %d %s (%p casted %p)", i, name, fCTPDecisions->At(i), pDecision);
1719 if (!pDecision) return -ENOENT;
1720
1721 bool result=false;
2f251ae6 1722 // 13 March 2010 - Optimisation:
1723 // Dont use the EvaluateCTPTriggerClass method, which uses slow TFormula objects.
1724 AliHLTUInt64_t triggers = 0;
1725 if (trigData) triggers = pCTPData->ActiveTriggers(*trigData);
1726 else triggers = pCTPData->Triggers();
1727 result = (triggers&((AliHLTUInt64_t)0x1<<i)) ? true : false;
1728 //if (trigData) result=pCTPData->EvaluateCTPTriggerClass(name, *trigData);
1729 //else result=pCTPData->EvaluateCTPTriggerClass(name);
566a01d0 1730 pDecision->Result(result);
1731 pDecision->TriggerDomain().Clear();
1732 if (trigData) pDecision->TriggerDomain().Add(pCTPData->ReadoutList(*trigData));
1733 else pDecision->TriggerDomain().Add(pCTPData->ReadoutList());
1734
1735 pTrigger->Add(fCTPDecisions->At(i), kAliHLTDataTypeTriggerDecision, kAliHLTVoidDataSpec);
325e5e42 1736 if (pTrigger->CallFailed()) return -EPROTO;
566a01d0 1737 }
1738
1739 return 0;
1740}