1 /**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * ALICE Experiment at CERN, All rights reserved. *
5 * Primary Authors: Artur Szostak <artursz@iafrica.com> *
6 * for The ALICE HLT Project. *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
17 /// @file AliHLTGlobalTriggerComponent.cxx
18 /// @author Artur Szostak <artursz@iafrica.com>
20 /// @brief Implementation of the AliHLTGlobalTriggerComponent component class.
22 /// The AliHLTGlobalTriggerComponentComponent class applies the global HLT trigger to all
23 /// trigger information produced by components deriving from AliHLTTrigger.
25 #include "AliHLTGlobalTriggerComponent.h"
26 #include "AliHLTGlobalTriggerDecision.h"
27 #include "AliHLTGlobalTrigger.h"
28 #include "AliHLTGlobalTriggerConfig.h"
29 #include "AliHLTTriggerMenu.h"
33 #include "TClonesArray.h"
34 #include "TObjString.h"
36 #include "TInterpreter.h"
40 ClassImp(AliHLTGlobalTriggerComponent)
43 AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() :
48 // Default constructor.
52 AliHLTGlobalTriggerComponent::~AliHLTGlobalTriggerComponent()
54 // Default destructor.
56 if (fTrigger != NULL) delete fTrigger;
60 void AliHLTGlobalTriggerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
62 // Returns the output data size estimate.
64 constBase = sizeof(AliHLTGlobalTriggerDecision);
69 Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
71 // Initialises the global trigger component.
74 const char* configFileName = NULL;
75 const char* codeFileName = NULL;
77 TClonesArray includePaths(TObjString::Class());
78 TClonesArray includeFiles(TObjString::Class());
80 for (int i = 0; i < argc; i++)
82 if (strcmp(argv[i], "-config") == 0)
84 if (configFileName != NULL)
86 HLTWarning("Trigger configuration macro was already specified."
87 " Will replace previous value given by -config."
92 HLTError("The trigger configuration macro filename was not specified." );
95 configFileName = argv[i+1];
100 if (strcmp(argv[i], "-includepath") == 0)
104 HLTError("The include path was not specified." );
107 new (includePaths[includePaths.GetEntriesFast()]) TObjString(argv[i+1]);
112 if (strcmp(argv[i], "-include") == 0)
116 HLTError("The include file name was not specified." );
119 new (includeFiles[includeFiles.GetEntriesFast()]) TObjString(argv[i+1]);
124 if (strcmp(argv[i], "-debug") == 0)
126 if (fDebugMode == true)
128 HLTWarning("The debug flag was already specified. Ignoring this instance.");
135 if (strcmp(argv[i], "-usecode") == 0)
137 if (codeFileName != NULL)
139 HLTWarning("Custom trigger code file was already specified."
140 " Will replace previous value given by -usecode."
145 HLTError("The custom trigger code filename was not specified." );
148 codeFileName = argv[i+1];
151 HLTError("The custom trigger class name was not specified." );
154 classname = argv[i+2];
159 HLTError("Unknown option '%s'.", argv[i]);
163 const AliHLTTriggerMenu* menu = NULL;
164 if (configFileName != NULL)
167 cmd += configFileName;
168 gROOT->ProcessLine(cmd);
169 menu = AliHLTGlobalTriggerConfig::Menu();
174 HLTError("No trigger menu configuration found or specified.");
179 if (codeFileName == NULL)
181 HLTDebug("Generating custom HLT trigger class.");
182 result = GenerateTrigger(menu, classname, includePaths, includeFiles);
186 HLTDebug("Loading HLT trigger class from file '%s'.", codeFileName);
187 result = LoadTriggerClass(codeFileName, includePaths);
189 if (result != 0) return result;
191 fTrigger = AliHLTGlobalTrigger::CreateNew(classname.Data());
192 if (fTrigger == NULL)
194 HLTError("Could not create a new instance of '%s'.", classname.Data());
198 fTrigger->FillFromMenu(*menu);
199 fTrigger->ResetCounters(menu->NumberOfItems());
205 Int_t AliHLTGlobalTriggerComponent::DoDeinit()
207 // Cleans up the global trigger component.
209 if (fTrigger != NULL)
219 AliHLTComponent* AliHLTGlobalTriggerComponent::Spawn()
221 // Creates a new object instance.
223 return new AliHLTGlobalTriggerComponent;
227 int AliHLTGlobalTriggerComponent::DoTrigger()
229 // This method will apply the global trigger decision.
231 if (fTrigger == NULL)
233 HLTFatal("Global trigger implementation object is NULL!");
237 fTrigger->NewEvent();
239 // Fill in the input data.
240 const TObject* obj = GetFirstInputObject();
243 fTrigger->Add(obj, GetDataType(), GetSpecification());
244 obj = GetNextInputObject();
247 // Calculate the global trigger result and trigger domain, then create and push
248 // back the new global trigger decision object.
250 GetTriggerDomain().Clear();
251 bool triggerResult = fTrigger->CalculateTriggerDecision(GetTriggerDomain(), description);
252 SetDescription(description.Data());
254 AliHLTGlobalTriggerDecision decision(triggerResult, GetTriggerDomain(), GetDescription());
255 decision.SetCounters(fTrigger->Counters());
257 // Add the input objects used to the global decision.
258 obj = GetFirstInputObject();
261 if (obj->IsA() == AliHLTTriggerDecision::Class())
263 decision.AddTriggerInput( *static_cast<const AliHLTTriggerDecision*>(obj) );
267 decision.AddInputObject(obj);
269 obj = GetNextInputObject();
272 TriggerEvent(&decision);
277 int AliHLTGlobalTriggerComponent::GenerateTrigger(
278 const AliHLTTriggerMenu* menu, TString& name,
279 const TClonesArray& includePaths, const TClonesArray& includeFiles
282 // Generates the global trigger class that will implement the specified trigger menu.
283 // See header for more details.
285 // Create a new UUID and replace the '-' characters with '_' to make it a valid
288 TString uuidstr = uuid.AsString();
289 for (Int_t i = 0; i < uuidstr.Length(); i++)
291 if (uuidstr[i] == '-') uuidstr[i] = '_';
294 // Create the name of the new class.
295 name = "AliHLTGlobalTriggerImpl_";
297 TString filename = name + ".cxx";
299 // Open a text file to write the code and generate the new class.
300 fstream code(filename.Data(), ios_base::out | ios_base::trunc);
303 HLTError("Could not open file '%s' for writing.", filename.Data());
307 TClonesArray symbols(AliHLTTriggerMenuSymbol::Class());
308 int result = BuildSymbolList(menu, symbols);
309 if (result != 0) return result;
311 code << "#include <cstring>" << endl;
312 code << "#include \"TString.h\"" << endl;
313 code << "#include \"TClonesArray.h\"" << endl;
314 code << "#include \"AliHLTGlobalTrigger.h\"" << endl;
315 code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl;
316 code << "#include \"AliHLTDomainEntry.h\"" << endl;
317 code << "#include \"AliHLTTriggerDomain.h\"" << endl;
318 code << "#include \"AliHLTReadoutList.h\"" << endl;
319 code << "#include \"AliHLTTriggerMenu.h\"" << endl;
320 code << "#include \"AliHLTTriggerMenuItem.h\"" << endl;
321 code << "#include \"AliHLTTriggerMenuSymbol.h\"" << endl;
323 // Add any include files that were specified on the command line.
324 for (Int_t i = 0; i < includeFiles.GetEntriesFast(); i++)
326 TString file = static_cast<const TObjString*>(includeFiles.UncheckedAt(i))->String();
327 code << "#include \"" << file.Data() << "\"" << endl;
330 code << "class " << name << " : public AliHLTGlobalTrigger" << endl;
332 code << "public:" << endl;
334 code << " " << name << "() : AliHLTGlobalTrigger()";
335 // Write the symbols in the trigger menu in the initialisation list.
336 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
339 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
340 code << " " << symbol->Name() << "()," << endl;
341 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
343 code << " " << symbol->Name() << "TriggerDomain()," << endl;
345 code << " " << symbol->Name() << "DomainEntry(kAliHLTAnyDataType)";
347 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
349 code << "," << endl << " fMenuItemDescription" << i << "()";
351 code << endl << " {" << endl;
354 code << " SetLocalLoggingLevel(kHLTLogAll);" << endl;
355 code << " HLTInfo(\"Creating new instance at %p.\", this);" << endl;
357 code << " }" << endl;
359 code << " virtual ~" << name << "() {" << endl;
362 code << " HLTInfo(\"Deleting instance at %p.\", this);" << endl;
364 code << " }" << endl;
366 // Generate the FillFromMenu method.
367 code << " virtual void FillFromMenu(const AliHLTTriggerMenu& menu) {" << endl;
370 code << " HLTDebug(\"Filling description entries from trigger menu for global trigger %p.\", this);" << endl;
372 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
374 code << " fMenuItemDescription" << i << " = (menu.Item(" << i
375 << ") != NULL) ? menu.Item(" << i << ")->Description() : \"\";" << endl;
379 code << " HLTDebug(\"Finished filling description entries from trigger menu.\");" << endl;
380 code << " HLTDebug(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this);" << endl;
382 code << " for (Int_t i = 0; i < menu.SymbolArray().GetEntriesFast(); i++) {" << endl;
383 code << " const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const"
384 " AliHLTTriggerMenuSymbol*>(menu.SymbolArray().UncheckedAt(i));" << endl;
385 code << " if (symbol == NULL) continue;" << endl;
386 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
388 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
389 code << " if (strcmp(symbol->Name(), \"" << symbol->Name() << "\") == 0) {" << endl;
392 code << " HLTDebug(\"Assinging domain entry value to match for symbol '%s' to '%s'.\","
393 " symbol->Name(), symbol->BlockType().AsString().Data());" << endl;
395 code << " " << symbol->Name() << "DomainEntry = symbol->BlockType();" << endl;
396 code << " continue;" << endl;
397 code << " }" << endl;
399 code << " }" << endl;
402 code << " HLTDebug(\"Finished filling domain entries from trigger menu symbols.\");" << endl;
404 code << " }" << endl;
406 // Generate the NewEvent method.
407 code << " virtual void NewEvent() {" << endl;
410 code << " HLTDebug(\"New event for global trigger object %p, initialising variables to default values.\", this);" << endl;
412 // Write code to initialise the symbols in the trigger menu to their default values.
413 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
415 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
416 code << " " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl;
417 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
419 code << " " << symbol->Name() << "TriggerDomain.Clear();" << endl;
424 code << " HLTDebug(\"Finished initialising variables.\");" << endl;
426 code << " }" << endl;
428 // Generate the Add method.
429 code << " virtual void Add(const TObject* _object_, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl;
430 code << " AliHLTDomainEntry _type_spec_(_type_, _spec_);" << endl;
433 code << " HLTDebug(\"Adding TObject %p, with class name '%s' from data block"
434 " '%s', to global trigger object %p\", _object_, _object_->ClassName(),"
435 " _type_spec_.AsString().Data(), this);" << endl;
436 code << " _object_->Print();" << endl;
437 code << " bool _object_assigned_ = false;" << endl;
439 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
441 // Write code to check if the block type, specification and class name is correct.
442 // Then write code to assign the variable from the input object.
443 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
444 TString expr = symbol->AssignExpression();
445 if (expr == "") continue; // Skip entries that have no assignment expression.
446 bool isTrigDecision = strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0;
451 code << " HLTDebug(\"Trying to match input object to class '"
452 << symbol->ObjectClass() << "', trigger name '" << symbol->Name()
453 << "' and block type '%s'\", " << symbol->Name()
454 << "DomainEntry.AsString().Data());" << endl;
458 code << " HLTDebug(\"Trying to match input object to class '"
459 << symbol->ObjectClass() << "' and block type '%s'\", "
460 << symbol->Name() << "DomainEntry.AsString().Data());" << endl;
463 code << " const " << symbol->ObjectClass() << "* " << symbol->Name()
464 << "_object_ = dynamic_cast<const " << symbol->ObjectClass()
465 << "*>(_object_);" << endl;
466 code << " if (" << symbol->Name() << "_object_ != NULL and ";
469 code << "strcmp(" << symbol->Name() << "_object_->Name(), \""
470 << symbol->Name() << "\") == 0 and ";
472 code << symbol->Name() << "DomainEntry == _type_spec_) {" << endl;
473 TString fullname = symbol->Name();
474 fullname += "_object_";
475 expr.ReplaceAll("this", fullname);
476 code << " this->" << symbol->Name() << " = " << expr.Data() << ";" << endl;
479 code << " this->" << symbol->Name() << "TriggerDomain = "
480 << fullname.Data() << "->TriggerDomain();" << endl;
484 code << " HLTDebug(\"Added TObject %p with class name '%s' to variable "
485 << symbol->Name() << "\", _object_, _object_->ClassName());" << endl;
486 code << " _object_assigned_ = true;" << endl;
488 code << " }" << endl;
492 code << " if (not _object_assigned_) HLTDebug(\"Did not assign TObject %p"
493 " with class name '%s' to any variable.\", _object_, _object_->ClassName());"
496 code << " }" << endl;
498 // Generate the CalculateTriggerDecision method.
499 code << " virtual bool CalculateTriggerDecision(AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl;
502 code << " HLTDebug(\"Calculating global HLT trigger result with trigger object at %p.\", this);" << endl;
504 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
506 const AliHLTTriggerMenuItem* item = menu->Item(i);
507 TString mergeExpr = item->MergeExpression();
508 for (Int_t j = 0; j < symbols.GetEntriesFast(); j++)
510 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) );
511 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") != 0) continue;
512 TString newname = symbol->Name();
513 newname += "TriggerDomain";
514 mergeExpr.ReplaceAll(symbol->Name(), newname);
518 code << " HLTDebug(\"Trying trigger condition " << i
519 << " (Description = '%s').\", fMenuItemDescription" << i << ".Data());"
522 code << " if (" << item->TriggerCondision() << ") {" << endl;
523 code << " IncrementCounter(" << i << ");" << endl;
524 const char* indentation = "";
525 if (item->PreScalar() != 0)
528 code << " if ((GetCounter(" << i << ") % " << item->PreScalar() << ") == 1) {" << endl;
530 code << indentation << " _domain_ = " << mergeExpr.Data() << ";" << endl;
531 code << indentation << " _description_ = fMenuItemDescription" << i << ";" << endl;
534 code << indentation << " HLTDebug(\"Matched trigger condition " << i
535 << " (Description = '%s').\", fMenuItemDescription" << i << ".Data());" << endl;
537 code << indentation << " return true;" << endl;
538 if (item->PreScalar() != 0)
540 code << " }" << endl;
542 code << " }" << endl;
544 code << " return false;" << endl;
545 code << " }" << endl;
547 // Generate the custom Factory class.
548 code << " class FactoryImpl : public AliHLTGlobalTrigger::Factory" << endl;
549 code << " {" << endl;
550 code << " public:" << endl;
551 code << " virtual const char* ClassName() const {" << endl;
552 code << " return \"" << name << "\";" << endl;
553 code << " }" << endl;
554 code << " virtual AliHLTGlobalTrigger* New() const {" << endl;
555 code << " return new " << name << "();" << endl;
556 code << " }" << endl;
557 code << " };" << endl;
559 code << "private:" << endl;
560 // Add the symbols in the trigger menu to the list of private variables.
561 for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
563 AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
564 code << " " << symbol->Type() << " " << symbol->Name() << ";" << endl;
565 if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
567 code << " AliHLTTriggerDomain " << symbol->Name() << "TriggerDomain;" << endl;
569 code << " AliHLTDomainEntry " << symbol->Name() << "DomainEntry;" << endl;
571 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
573 code << " TString fMenuItemDescription" << i << ";" << endl;
575 code << "};" << endl;
577 // Write a global object for the Factory class for automatic registration.
578 code << "namespace {" << endl;
579 code << " const " << name << "::FactoryImpl gkFactoryImpl;" << endl;
580 code << "};" << endl;
584 // Now we need to compile and load the new class.
585 result = LoadTriggerClass(filename, includePaths);
590 int AliHLTGlobalTriggerComponent::LoadTriggerClass(
591 const char* filename, const TClonesArray& includePaths
594 // Loads the code for a custom global trigger class implementation on the fly.
596 TString compiler = gSystem->GetBuildCompilerVersion();
597 if (compiler.Contains("gcc") or compiler.Contains("icc"))
599 TString includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger";
600 // Add any include paths that were specified on the command line.
601 for (Int_t i = 0; i < includePaths.GetEntriesFast(); i++)
603 TString path = static_cast<const TObjString*>(includePaths.UncheckedAt(i))->String();
607 gSystem->SetIncludePath(includePath);
608 gSystem->SetFlagsOpt("-O3 -DNDEBUG");
609 gSystem->SetFlagsDebug("-g3 -DDEBUG -D__DEBUG");
614 result = gSystem->CompileMacro(filename, "g");
618 result = gSystem->CompileMacro(filename, "O");
622 HLTFatal("Could not compile and load global trigger menu implementation.");
628 // If we do not support the compiler then try interpret the class instead.
631 Int_t errorcode = TInterpreter::kNoError;
632 gROOT->ProcessLine(cmd, &errorcode);
633 if (errorcode != TInterpreter::kNoError)
635 HLTFatal("Could not load interpreted global trigger menu implementation"
636 " (Interpreter error code = %d).",
647 int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArray& list)
649 // Searches for the named symbol in the given list.
650 // See header for more details.
652 for (int i = 0; i < list.GetEntriesFast(); i++)
654 const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const AliHLTTriggerMenuSymbol*>( list.UncheckedAt(i) );
655 if (symbol == NULL) continue;
656 if (strcmp(symbol->Name(), name) == 0) return i;
662 int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu, TClonesArray& list)
664 // Builds the list of symbols to use in the custom global trigger menu
665 // implementation class.
666 // See header for more details.
668 for (UInt_t i = 0; i < menu->NumberOfSymbols(); i++)
670 const AliHLTTriggerMenuSymbol* symbol = menu->Symbol(i);
671 if (FindSymbol(symbol->Name(), list) != -1)
673 HLTError("Multiple symbols with the name '%s' defined in the trigger menu.", symbol->Name());
676 new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(*symbol);
679 TRegexp exp("[_a-zA-Z][_a-zA-Z0-9]*");
680 for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
682 const AliHLTTriggerMenuItem* item = menu->Item(i);
683 TString str = item->TriggerCondision();
688 Ssiz_t pos = exp.Index(str, &length, start);
689 if (pos == kNPOS) break;
690 TString s = str(pos, length);
693 if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or
694 s == "compl" or s == "not" or s == "not_eq" or s == "or" or
695 s == "or_eq" or s == "xor" or s == "xor_eq" or s == "true" or
699 // Ignore iso646.h and other keywords.
703 if (FindSymbol(s.Data(), list) == -1)
705 AliHLTTriggerMenuSymbol newSymbol;
706 newSymbol.Name(s.Data());
707 newSymbol.Type("bool");
708 newSymbol.ObjectClass("AliHLTTriggerDecision");
709 newSymbol.AssignExpression("this->Result()");
710 newSymbol.DefaultValue("false");
711 new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(newSymbol);
714 while (start < str.Length());