]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/trigger/AliHLTGlobalTriggerComponent.cxx
temporarily reverting part of r36598
[u/mrichter/AliRoot.git] / HLT / trigger / AliHLTGlobalTriggerComponent.cxx
index cbf13310897c3fa6b940993d389f365b04e9885f..b745eae1e4bb3777cc86a3b77a1077e1b65d5173 100644 (file)
@@ -1,3 +1,4 @@
+// $Id$
 /**************************************************************************
  * This file is property of and copyright by the ALICE HLT Project        *
  * ALICE Experiment at CERN, All rights reserved.                         *
 #include "AliHLTGlobalTrigger.h"
 #include "AliHLTGlobalTriggerConfig.h"
 #include "AliHLTTriggerMenu.h"
+#include "AliHLTCTPData.h"
+#include "AliCDBManager.h"
+#include "AliCDBStorage.h"
+#include "AliCDBEntry.h"
 #include "TUUID.h"
 #include "TROOT.h"
+#include "TSystem.h"
 #include "TRegexp.h"
 #include "TClonesArray.h"
 #include "TObjString.h"
-#include "TSystem.h"
+#include "TString.h"
 #include "TInterpreter.h"
+#include "TDatime.h"
+#include "TClass.h"
 #include <fstream>
 #include <cerrno>
+#include <cassert>
+#include <vector>
+#include <algorithm>
 
 ClassImp(AliHLTGlobalTriggerComponent)
 
+const char* AliHLTGlobalTriggerComponent::fgkTriggerMenuCDBPath = "HLT/ConfigHLT/HLTGlobalTrigger";
+
+
+namespace
+{
+  /**
+   * This method is used as a comparison functor with the STL sort routines.
+   */
+  bool AliHLTDescendingNumbers(UInt_t a, UInt_t b)
+  {
+    return a > b;
+  }
+} // end of namespace
+
 
 AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() :
        AliHLTTrigger(),
        fTrigger(NULL),
-       fDebugMode(false)
+       fDebugMode(false),
+       fRuntimeCompile(true),
+       fSkipCTPCounters(false),
+       fDeleteCodeFile(false),
+       fCodeFileName(),
+       fClassName(),
+       fCTPDecisions(NULL),
+       fBufferSizeConst(2*(sizeof(AliHLTGlobalTriggerDecision) + sizeof(AliHLTReadoutList))),
+       fBufferSizeMultiplier(1.),
+       fIncludePaths(TObjString::Class()),
+       fIncludeFiles(TObjString::Class()),
+       fLibStateAtLoad()
 {
   // Default constructor.
+  
+  ClearInfoForNewEvent(false);
 }
 
 
@@ -54,6 +92,18 @@ AliHLTGlobalTriggerComponent::~AliHLTGlobalTriggerComponent()
   // Default destructor.
   
   if (fTrigger != NULL) delete fTrigger;
+
+  if (fCTPDecisions) {
+    fCTPDecisions->Delete();
+    delete fCTPDecisions;
+  }
+}
+
+
+void AliHLTGlobalTriggerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& list) const
+{
+  // Returns the kAliHLTDataTypeGlobalTrigger type as output.
+  list.push_back(kAliHLTDataTypeGlobalTrigger);
 }
 
 
@@ -61,8 +111,8 @@ void AliHLTGlobalTriggerComponent::GetOutputDataSize(unsigned long& constBase, d
 {
   // Returns the output data size estimate.
 
-  constBase = sizeof(AliHLTGlobalTriggerDecision);
-  inputMultiplier = 1;
+  constBase = fBufferSizeConst;
+  inputMultiplier = fBufferSizeMultiplier;
 }
 
 
@@ -71,11 +121,13 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
   // Initialises the global trigger component.
   
   fDebugMode = false;
+  fClassName = "";
+  fCodeFileName = "";
+  fDeleteCodeFile = false;
   const char* configFileName = NULL;
   const char* codeFileName = NULL;
-  TString classname;
-  TClonesArray includePaths(TObjString::Class());
-  TClonesArray includeFiles(TObjString::Class());
+  fIncludePaths.Clear();
+  fIncludeFiles.Clear();
   
   for (int i = 0; i < argc; i++)
   {
@@ -104,7 +156,7 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
         HLTError("The include path was not specified." );
         return -EINVAL;
       }
-      new (includePaths[includePaths.GetEntriesFast()]) TObjString(argv[i+1]);
+      new (fIncludePaths[fIncludePaths.GetEntriesFast()]) TObjString(argv[i+1]);
       i++;
       continue;
     }
@@ -116,7 +168,7 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
         HLTError("The include file name was not specified." );
         return -EINVAL;
       }
-      new (includeFiles[includeFiles.GetEntriesFast()]) TObjString(argv[i+1]);
+      new (fIncludeFiles[fIncludeFiles.GetEntriesFast()]) TObjString(argv[i+1]);
       i++;
       continue;
     }
@@ -128,7 +180,12 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
         HLTWarning("The debug flag was already specified. Ignoring this instance.");
       }
       fDebugMode = true;
-      i++;
+      continue;
+    }
+    
+    if (strcmp(argv[i], "-cint") == 0)
+    {
+      fRuntimeCompile = false;
       continue;
     }
     
@@ -151,11 +208,18 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
         HLTError("The custom trigger class name was not specified." );
         return -EINVAL;
       }
-      classname = argv[i+2];
+      fClassName = argv[i+2];
       i += 2;
       continue;
     }
-    
+
+    if (strcmp(argv[i], "-skipctp") == 0)
+    {
+      HLTInfo("Skipping CTP counters in trigger decision");
+      fSkipCTPCounters=true;
+      continue;
+    }
+        
     HLTError("Unknown option '%s'.", argv[i]);
     return -EINVAL;
   } // for loop
@@ -169,34 +233,47 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
     menu = AliHLTGlobalTriggerConfig::Menu();
   }
   
+  // Try load the trigger menu from the CDB if it is not loaded yet with the
+  // -config option
+  int result = -ENOENT;
+  if (menu == NULL)
+  {
+    result = LoadTriggerMenu(fgkTriggerMenuCDBPath, menu);
+  }
   if (menu == NULL)
   {
     HLTError("No trigger menu configuration found or specified.");
-    return -ENOENT;
+    return result;
   }
   
-  int result = 0;
   if (codeFileName == NULL)
   {
-    HLTDebug("Generating custom HLT trigger class.");
-    result = GenerateTrigger(menu, classname, includePaths, includeFiles);
+    result = GenerateTrigger(menu, fClassName, fCodeFileName, fIncludePaths, fIncludeFiles);
+    if (result == 0) fDeleteCodeFile = true;
   }
   else
   {
-    HLTDebug("Loading HLT trigger class from file '%s'.", codeFileName);
-    result = LoadTriggerClass(codeFileName, includePaths);
+    result = LoadTriggerClass(codeFileName, fIncludePaths);
+    if (result == 0) fCodeFileName = codeFileName;
   }
   if (result != 0) return result;
   
-  fTrigger = AliHLTGlobalTrigger::CreateNew(classname.Data());
+  fTrigger = AliHLTGlobalTrigger::CreateNew(fClassName.Data());
   if (fTrigger == NULL)
   {
-    HLTError("Could not create a new instance of '%s'.", classname.Data());
+    HLTError("Could not create a new instance of '%s'.", fClassName.Data());
     return -EIO;
   }
   
   fTrigger->FillFromMenu(*menu);
-  fTrigger->ResetCounters(menu->NumberOfItems());
+  if (fTrigger->CallFailed()) return -EPROTO;
+
+  // setup the CTP accounting in AliHLTComponent
+  SetupCTPData();
+
+  // Set the default values from the trigger menu.
+  SetDescription(menu->DefaultDescription());
+  SetTriggerDomain(menu->DefaultTriggerDomain());
   
   return 0;
 }
@@ -212,6 +289,23 @@ Int_t AliHLTGlobalTriggerComponent::DoDeinit()
     fTrigger = NULL;
   }
   
+  if (fCTPDecisions) {
+    fCTPDecisions->Delete();
+    delete fCTPDecisions;
+  }
+  fCTPDecisions=NULL;
+  
+  Int_t result = UnloadTriggerClass(fCodeFileName);
+  if (result != 0) return result;
+  
+  if (fDeleteCodeFile and !fCodeFileName.IsNull() && gSystem->AccessPathName(fCodeFileName)==0 && !fDebugMode) {
+    fCodeFileName.ReplaceAll(".cxx", "*");
+    TString command="rm "; command+=fCodeFileName;
+    gSystem->Exec(command);
+  }
+  fCodeFileName="";
+  fDeleteCodeFile=false;
+
   return 0;
 }
 
@@ -233,26 +327,70 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
     HLTFatal("Global trigger implementation object is NULL!");
     return -EIO;
   }
+
+  AliHLTUInt32_t eventType=0;
+  if (!IsDataEvent(&eventType)) {
+    if (eventType==gkAliEventTypeEndOfRun) PrintStatistics(fTrigger, kHLTLogImportant, 0);
+    return 0;
+  }
+
+  // Copy the trigger counters in case we need to set them back to their original
+  // value because the PushBack method fails with ENOSPC.
+  TArrayL64 originalCounters = fTrigger->GetCounters();
+  if (fTrigger->CallFailed()) return -EPROTO;
   
   fTrigger->NewEvent();
+  if (fTrigger->CallFailed()) return -EPROTO;
   
   // Fill in the input data.
   const TObject* obj = GetFirstInputObject();
   while (obj != NULL)
   {
     fTrigger->Add(obj, GetDataType(), GetSpecification());
+    if (fTrigger->CallFailed()) return -EPROTO;
     obj = GetNextInputObject();
   }
 
+  // add trigger decisions for every CTP class
+  const AliHLTCTPData* pCTPData=CTPData();
+  if (pCTPData) {
+    AddCTPDecisions(fTrigger, pCTPData, GetTriggerData());
+  }
+
   // Calculate the global trigger result and trigger domain, then create and push
   // back the new global trigger decision object.
   TString description;
-  GetTriggerDomain().Clear();
-  bool triggerResult = fTrigger->CalculateTriggerDecision(GetTriggerDomain(), description);
-  SetDescription(description.Data());
+  AliHLTTriggerDomain triggerDomain;
+  bool triggerResult = fTrigger->CalculateTriggerDecision(triggerDomain, description);
+  if (fTrigger->CallFailed()) return -EPROTO;
+  AliHLTGlobalTriggerDecision decision(
+      triggerResult,
+      // The following will cause the decision to be generated with default values
+      // (set in fTriggerDomain and fDescription) if the trigger result is false.
+      (triggerResult == true) ? triggerDomain : GetTriggerDomain(),
+      (triggerResult == true) ? description.Data() : GetDescription()
+    );
+
+  // mask the readout list according to the CTP trigger
+  // if the classes have been initialized (mask non-zero)
+  if (pCTPData && pCTPData->Mask()) {
+    AliHLTEventDDL eventDDL=pCTPData->ReadoutList(*GetTriggerData());
+    AliHLTReadoutList ctpreadout(eventDDL);
+    ctpreadout.Enable(AliHLTReadoutList::kHLT);
+    AliHLTReadoutList maskedList=decision.ReadoutList();
+    maskedList.AndEq(ctpreadout);
+    decision.ReadoutList(maskedList);
+  }
+
+  decision.SetCounters(fTrigger->GetCounters(), GetEventCount()+1);
+  if (fTrigger->CallFailed()) return -EPROTO;
   
-  AliHLTGlobalTriggerDecision decision(triggerResult, GetTriggerDomain(), GetDescription());
-  decision.SetCounters(fTrigger->Counters());
+  static UInt_t lastTime=0;
+  TDatime time;
+  if (time.Get()-lastTime>5) {
+    lastTime=time.Get();
+    PrintStatistics(fTrigger);
+  }
   
   // Add the input objects used to the global decision.
   obj = GetFirstInputObject();
@@ -268,20 +406,143 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
     }
     obj = GetNextInputObject();
   }
+
+  if (!fSkipCTPCounters && CTPData()) decision.AddInputObject(CTPData());
+
+  CreateEventDoneReadoutFilter(decision.TriggerDomain(), 3);
+  CreateEventDoneReadoutFilter(decision.TriggerDomain(), 4);
+  if (TriggerEvent(&decision) == -ENOSPC)
+  {
+    // Increase the estimated buffer space required if the PushBack methods in TriggerEvent
+    // returned the "no buffer space" error code. Also remember to set the trigger counters
+    // back to what they were, otherwise triggers will be double counted when we try to reprocess
+    // this event with larger buffers.
+    fBufferSizeConst += 1024*1024;
+    fBufferSizeMultiplier *= 2.;
+    fTrigger->SetCounters(originalCounters);
+    if (fTrigger->CallFailed()) return -EPROTO;
+    return -ENOSPC;
+  }
+  return 0;
+}
+
+
+int AliHLTGlobalTriggerComponent::Reconfigure(const char* cdbEntry, const char* chainId)
+{
+  // Reconfigure the component by loading the trigger menu and recreating the
+  // trigger logic class.
+  
+  const char* path = fgkTriggerMenuCDBPath;
+  const char* id = "(unknown)";
+  if (cdbEntry != NULL) path = cdbEntry;
+  if (chainId != NULL and chainId[0] != '\0') id = chainId;
+  HLTInfo("Reconfiguring from '%s' for chain component '%s'.", path, id);
+  
+  const AliHLTTriggerMenu* menu = NULL;
+  int result = LoadTriggerMenu(path, menu);
+  if (result != 0) return result;
   
-  TriggerEvent(&decision);
+  TString className;
+  TString codeFileName;
+  result = GenerateTrigger(menu, className, codeFileName, fIncludePaths, fIncludeFiles);
+  if (result != 0) return result;
+  
+  AliHLTGlobalTrigger* trigger = AliHLTGlobalTrigger::CreateNew(className.Data());
+  if (trigger == NULL)
+  {
+    HLTError("Could not create a new instance of '%s'.", className.Data());
+    // Make sure to cleanup after the new code file.
+    UnloadTriggerClass(codeFileName);
+    if (not codeFileName.IsNull() and gSystem->AccessPathName(codeFileName)==0 and not fDebugMode)
+    {
+      codeFileName.ReplaceAll(".cxx", "*");
+      TString command="rm "; command+=codeFileName;
+      gSystem->Exec(command);
+    }
+    return -EIO;
+  }
+  
+  if (fTrigger != NULL)
+  {
+    delete fTrigger;
+    fTrigger = NULL;
+  }
+  
+  fTrigger = trigger;
+  fTrigger->FillFromMenu(*menu);
+  if (fTrigger->CallFailed()) return -EPROTO;
+
+  // Set the default values from the trigger menu.
+  SetDescription(menu->DefaultDescription());
+  SetTriggerDomain(menu->DefaultTriggerDomain());
+  
+  // Cleanup the old class code.
+  UnloadTriggerClass(fCodeFileName);
+  if (fDeleteCodeFile and not fCodeFileName.IsNull() and gSystem->AccessPathName(fCodeFileName)==0 and not fDebugMode)
+  {
+    fCodeFileName.ReplaceAll(".cxx", "*");
+    TString command="rm "; command+=fCodeFileName;
+    gSystem->Exec(command);
+  }
+  fCodeFileName = codeFileName;
+  fDeleteCodeFile = true;  // Since we generated a new class.
+  
+  return 0;
+}
+
+
+int AliHLTGlobalTriggerComponent::LoadTriggerMenu(const char* cdbPath, const AliHLTTriggerMenu*& menu)
+{
+  // Loads the trigger menu object from the CDB path.
+  
+  HLTDebug("Trying to load trigger menu from '%s'.", cdbPath);
+  if (AliCDBManager::Instance() == NULL)
+  {
+    HLTError("CDB manager object not found.");
+    return -EIO;
+  }
+  AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage();
+  if (store == NULL)
+  {
+    HLTError("Could not get the the default storage for the CDB.");
+    return -EIO;
+  }
+  Int_t version = store->GetLatestVersion(cdbPath, GetRunNo());
+  Int_t subVersion = store->GetLatestSubVersion(cdbPath, GetRunNo(), version);
+  AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, GetRunNo(), version, subVersion);
+  if (entry == NULL)
+  {
+    HLTError("Could not get the CDB entry for \"%s\".", cdbPath);
+    return -EIO;
+  }
+  TObject* obj = entry->GetObject();
+  if (obj == NULL)
+  {
+    HLTError("Configuration object for \"%s\" is missing.", cdbPath);
+    return -ENOENT;
+  }
+  if (obj->IsA() != AliHLTTriggerMenu::Class())
+  {
+    HLTError("Wrong type for configuration object in \"%s\". Found a %s but we expect a AliHLTTriggerMenu.",
+             cdbPath, obj->ClassName()
+    );
+    return -EPROTO;
+  }
+  menu = static_cast<AliHLTTriggerMenu*>(obj);
   return 0;
 }
 
 
 int AliHLTGlobalTriggerComponent::GenerateTrigger(
-    const AliHLTTriggerMenu* menu, TString& name,
+    const AliHLTTriggerMenu* menu, TString& name, TString& filename,
     const TClonesArray& includePaths, const TClonesArray& includeFiles
   )
 {
   // Generates the global trigger class that will implement the specified trigger menu.
   // See header for more details.
   
+  HLTDebug("Generating custom HLT trigger class named %s using trigger menu %p.", ((void*)menu), name.Data());
+  
   // Create a new UUID and replace the '-' characters with '_' to make it a valid
   // C++ symbol name.
   TUUID uuid;
@@ -294,7 +555,7 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   // Create the name of the new class.
   name = "AliHLTGlobalTriggerImpl_";
   name += uuidstr;
-  TString filename = name + ".cxx";
+  filename = name + ".cxx";
   
   // Open a text file to write the code and generate the new class.
   fstream code(filename.Data(), ios_base::out | ios_base::trunc);
@@ -308,9 +569,11 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   int result = BuildSymbolList(menu, symbols);
   if (result != 0) return result;
   
+  code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
   code << "#include <cstring>" << endl;
   code << "#include \"TString.h\"" << endl;
   code << "#include \"TClonesArray.h\"" << endl;
+  code << "#include \"AliHLTLogging.h\"" << endl;
   code << "#include \"AliHLTGlobalTrigger.h\"" << endl;
   code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl;
   code << "#include \"AliHLTDomainEntry.h\"" << endl;
@@ -323,15 +586,45 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   // Add any include files that were specified on the command line.
   for (Int_t i = 0; i < includeFiles.GetEntriesFast(); i++)
   {
-    TString file = static_cast<const TObjString*>(includeFiles.UncheckedAt(i))->String();
+    TString file = static_cast<TObjString*>(includeFiles.UncheckedAt(i))->String();
     code << "#include \"" << file.Data() << "\"" << endl;
   }
   
-  code << "class " << name << " : public AliHLTGlobalTrigger" << endl;
+  if (fDebugMode)
+  {
+    code << "#else" << endl;
+    code << "const char* gFunctionName = \"???\";" << endl;
+    code << "#define HLTDebug(msg) if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) SendMessage(kHLTLogDebug, Class_Name(), ::gFunctionName, __FILE__, __LINE__, msg)" << endl;
+  }
+  code << "#endif" << endl;
+  
+  code << "class " << name << " :" << endl;
+  // Add appropriate #ifdef sections since we need to prevent inheritance from
+  // AliHLTGlobalTrigger. CINT does not seem to support multiple inheritance nor
+  // multiple levels of inheritance. Neither of the following schemes worked:
+  //
+  // 1)  class AliHLTGlobalTrigger : public AliHLTLogging {};
+  //     class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger {};
+  //
+  // 2)  class AliHLTGlobalTrigger {};
+  //     class AliHLTGlobalTriggerImpl_xyz : public AliHLTGlobalTrigger, public AliHLTLogging {};
+  //
+  // Thus, we are forced to just inherit from AliHLTLogging when running in the CINT
+  // interpreter. But we anyway have to call the global trigger implementation class
+  // through the AliHLTGlobalTriggerWrapper so this is not such a problem.
+  code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
+  code << "  public AliHLTGlobalTrigger," << endl;
+  code << "#endif" << endl;
+  code << "  public AliHLTLogging" << endl;
   code << "{" << endl;
   code << "public:" << endl;
   
-  code << "  " << name << "() : AliHLTGlobalTrigger()";
+  // Generate constructor method.
+  code << "  " << name << "() :" << endl;
+  code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
+  code << "    AliHLTGlobalTrigger()," << endl;
+  code << "#endif" << endl;
+  code << "    AliHLTLogging()";
   // Write the symbols in the trigger menu in the initialisation list.
   for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
   {
@@ -351,15 +644,21 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   code << endl << "  {" << endl;
   if (fDebugMode)
   {
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"" << name.Data() <<"\";" << endl;
+    code << "#endif" << endl;
     code << "    SetLocalLoggingLevel(kHLTLogAll);" << endl;
-    code << "    HLTInfo(\"Creating new instance at %p.\", this);" << endl;
+    code << "    HLTDebug(Form(\"Creating new instance at %p.\", this));" << endl;
   }
   code << "  }" << endl;
   
   code << "  virtual ~" << name << "() {" << endl;
   if (fDebugMode)
   {
-    code << "    HLTInfo(\"Deleting instance at %p.\", this);" << endl;
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"~" << name.Data() << "\";" << endl;
+    code << "#endif" << endl;
+    code << "    HLTDebug(Form(\"Deleting instance at %p.\", this));" << endl;
   }
   code << "  }" << endl;
   
@@ -367,8 +666,12 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   code << "  virtual void FillFromMenu(const AliHLTTriggerMenu& menu) {" << endl;
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Filling description entries from trigger menu for global trigger %p.\", this);" << endl;
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"FillFromMenu\";" << endl;
+    code << "#endif" << endl;
+    code << "    HLTDebug(Form(\"Filling description entries from trigger menu for global trigger %p.\", this));" << endl;
   }
+  code << "    fCounter.Set(menu.NumberOfItems());" << endl;
   for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
   {
     code << "    fMenuItemDescription" << i << " = (menu.Item(" << i
@@ -376,21 +679,25 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   }
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Finished filling description entries from trigger menu.\");" << endl;
-    code << "    HLTDebug(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this);" << endl;
+    code << "    HLTDebug(Form(\"Finished filling description entries from trigger menu.\"));" << endl;
+    code << "    HLTDebug(Form(\"Filling domain entries from trigger menu symbols for global trigger %p.\", this));" << endl;
   }
   code << "    for (Int_t i = 0; i < menu.SymbolArray().GetEntriesFast(); i++) {" << endl;
+  // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly.
+  // Have to use the TClass system for extra protection.
+  code << "      if (menu.SymbolArray().UncheckedAt(i) == NULL) continue;" << endl;
+  code << "      if (menu.SymbolArray().UncheckedAt(i)->IsA() != AliHLTTriggerMenuSymbol::Class()) continue;" << endl;
   code << "      const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const"
            " AliHLTTriggerMenuSymbol*>(menu.SymbolArray().UncheckedAt(i));" << endl;
   code << "      if (symbol == NULL) continue;" << endl;
   for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
   {
     AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
-    code << "      if (strcmp(symbol->Name(), \"" << symbol->Name() << "\") == 0) {" << endl;
+    code << "      if (strcmp(symbol->RealName(), \"" << symbol->RealName() << "\") == 0) {" << endl;
     if (fDebugMode)
     {
-      code << "        HLTDebug(\"Assinging domain entry value to match for symbol '%s' to '%s'.\","
-              " symbol->Name(), symbol->BlockType().AsString().Data());" << endl;
+      code << "        HLTDebug(Form(\"Assinging domain entry value corresponding with symbol '%s' to '%s'.\","
+              " symbol->RealName(), symbol->BlockType().AsString().Data()));" << endl;
     }
     code << "        " << symbol->Name() << "DomainEntry = symbol->BlockType();" << endl;
     code << "        continue;" << endl;
@@ -399,7 +706,7 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   code << "    }" << endl;
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Finished filling domain entries from trigger menu symbols.\");" << endl;
+    code << "    HLTDebug(Form(\"Finished filling domain entries from trigger menu symbols.\"));" << endl;
   }
   code << "  }" << endl;
   
@@ -407,13 +714,26 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   code << "  virtual void NewEvent() {" << endl;
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"New event for global trigger object %p, initialising variables to default values.\", this);" << endl;
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"NewEvent\";" << endl;
+    code << "#endif" << endl;
+    code << "    HLTDebug(Form(\"New event for global trigger object %p, initialising variables to default values.\", this));" << endl;
   }
   // Write code to initialise the symbols in the trigger menu to their default values.
   for (Int_t i = 0; i < symbols.GetEntriesFast(); i++)
   {
     AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(i) );
-    code << "    " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl;
+    // CINT has problems with the implicit equals operator for complex types, so if
+    // the type has a equals operater we need to write the operator call explicitly.
+    TClass* clas = TClass::GetClass(symbol->Type());
+    if (clas != NULL and clas->GetMethodAny("operator=") != NULL)
+    {
+      code << "    " << symbol->Name() << ".operator = (" << symbol->DefaultValue() << ");" << endl;
+    }
+    else
+    {
+      code << "    " << symbol->Name() << " = " << symbol->DefaultValue() << ";" << endl;
+    }
     if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
     {
       code << "    " << symbol->Name() << "TriggerDomain.Clear();" << endl;
@@ -421,18 +741,24 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   }
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Finished initialising variables.\");" << endl;
+    code << "    HLTDebug(Form(\"Finished initialising variables.\"));" << endl;
   }
   code << "  }" << endl;
   
   // Generate the Add method.
   code << "  virtual void Add(const TObject* _object_, const AliHLTComponentDataType& _type_, AliHLTUInt32_t _spec_) {" << endl;
+  if (fDebugMode)
+  {
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"Add\";" << endl;
+    code << "#endif" << endl;
+  }
   code << "    AliHLTDomainEntry _type_spec_(_type_, _spec_);" << endl;
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Adding TObject %p, with class name '%s' from data block"
+    code << "    HLTDebug(Form(\"Adding TObject %p, with class name '%s' from data block"
             " '%s', to global trigger object %p\", _object_, _object_->ClassName(),"
-            " _type_spec_.AsString().Data(), this);" << endl;
+            " _type_spec_.AsString().Data(), this));" << endl;
     code << "    _object_->Print();" << endl;
     code << "    bool _object_assigned_ = false;" << endl;
   }
@@ -448,26 +774,29 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
     {
       if (isTrigDecision)
       {
-        code << "    HLTDebug(\"Trying to match input object to class '"
-             << symbol->ObjectClass() << "', trigger name '" << symbol->Name()
+        code << "    HLTDebug(Form(\"Trying to match input object to class '"
+             << symbol->ObjectClass() << "', trigger name '" << symbol->RealName()
              << "' and block type '%s'\", " << symbol->Name()
-             << "DomainEntry.AsString().Data());" << endl;
+             << "DomainEntry.AsString().Data()));" << endl;
       }
       else
       {
-        code << "    HLTDebug(\"Trying to match input object to class '"
+        code << "    HLTDebug(Form(\"Trying to match input object to class '"
              << symbol->ObjectClass() << "' and block type '%s'\", "
-             << symbol->Name() << "DomainEntry.AsString().Data());" << endl;
+             << symbol->Name() << "DomainEntry.AsString().Data()));" << endl;
       }
     }
-    code << "    const " << symbol->ObjectClass() << "* " << symbol->Name()
+    // 30 Oct 2009 - CINT sometimes evaluates the dynamic_cast incorrectly.
+    // Have to use the TClass system for extra protection.
+    code << "    const " << symbol->ObjectClass() << "* " << symbol->Name() << "_object_ = NULL;" << endl;
+    code << "    if (_object_->IsA() == " << symbol->ObjectClass() << "::Class()) " << symbol->Name()
          << "_object_ = dynamic_cast<const " << symbol->ObjectClass()
          << "*>(_object_);" << endl;
-    code << "    if (" << symbol->Name() << "_object_ != NULL and ";
+    code << "    if (" << symbol->Name() << "_object_ != NULL && ";
     if (isTrigDecision)
     {
       code << "strcmp(" << symbol->Name() << "_object_->Name(), \""
-           << symbol->Name() << "\") == 0 and ";
+           << symbol->RealName() << "\") == 0 && ";
     }
     code << symbol->Name() << "DomainEntry == _type_spec_) {" << endl;
     TString fullname = symbol->Name();
@@ -481,80 +810,245 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
     }
     if (fDebugMode)
     {
-      code << "      HLTDebug(\"Added TObject %p with class name '%s' to variable "
-           << symbol->Name() << "\", _object_, _object_->ClassName());" << endl;
+      code << "      HLTDebug(Form(\"Added TObject %p with class name '%s' to variable "
+           << symbol->Name() << "\", _object_, _object_->ClassName()));" << endl;
       code << "      _object_assigned_ = true;" << endl;
     }
     code << "    }" << endl;
   }
   if (fDebugMode)
   {
-    code << "    if (not _object_assigned_) HLTDebug(\"Did not assign TObject %p"
-            " with class name '%s' to any variable.\", _object_, _object_->ClassName());"
+    code << "    if (! _object_assigned_) {" << endl;
+    code << "      HLTDebug(Form(\"Did not assign TObject %p"
+            " with class name '%s' to any variable.\", _object_, _object_->ClassName()));"
          << endl;
+    code << "    }" << endl;
   }
   code << "  }" << endl;
   
   // Generate the CalculateTriggerDecision method.
+  // This requires code to be generated that checks which items in the trigger menu
+  // have their conditions asserted and then the trigger domain is generated from
+  // those fired items.
+  // The processing will start from the highest priority trigger group and stop
+  // after at least one trigger from the current priority group being processed
+  // is positive. For each priority group all the trigger menu items are checked.
+  // Their combined trigger condition expression must be true for the trigger priority
+  // group to be triggered positive. The full condition expression is formed by
+  // concatenating the individual condition expressions. If no trailing operators are
+  // used in the individual expressions then the default condition operator is placed
+  // between two concatenated condition expressions.
+  // If a trigger priority group has at least one trigger fired then the trigger domain
+  // is calculated such that it will give the same result as the concatenated trigger
+  // domain merging expressions for all the individual trigger menu items with
+  // positive results. Again, if no trailing operators are used in the individual
+  // merging expressions then the default domain operator is placed between two
+  // expression fragments.
   code << "  virtual bool CalculateTriggerDecision(AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl;
   if (fDebugMode)
   {
-    code << "    HLTDebug(\"Calculating global HLT trigger result with trigger object at %p.\", this);" << endl;
+    code << "#ifdef __CINT__" << endl;
+    code << "    gFunctionName = \"CalculateTriggerDecision\";" << endl;
+    code << "#endif" << endl;
+    code << "    HLTDebug(Form(\"Calculating global HLT trigger result with trigger object at %p.\", this));" << endl;
   }
+  
+  // Build a list of priorities used in the trigger menu.
+  std::vector<UInt_t> priorities;
   for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
   {
     const AliHLTTriggerMenuItem* item = menu->Item(i);
-    TString mergeExpr = item->MergeExpression();
-    for (Int_t j = 0; j < symbols.GetEntriesFast(); j++)
+    bool priorityNotInList = std::find(priorities.begin(), priorities.end(), item->Priority()) == priorities.end();
+    if (priorityNotInList) priorities.push_back(item->Priority());
+  }
+  std::sort(priorities.begin(), priorities.end(), AliHLTDescendingNumbers);
+  // From the priority list, build the priority groups in the correct order,
+  // i.e. highest priority first.
+  // The priority group is a list of vectors of integers. The integers are the
+  // index numbers into the trigger menu item list for the items which form part
+  // of the priority group.
+  std::vector<std::vector<Int_t> > priorityGroup;
+  priorityGroup.insert(priorityGroup.begin(), priorities.size(), std::vector<Int_t>());
+  for (size_t n = 0; n < priorities.size(); n++)
+  {
+    UInt_t priority = priorities[n];
+    for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
     {
-      AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) );
-      if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") != 0) continue;
-      TString newname = symbol->Name();
-      newname += "TriggerDomain";
-      mergeExpr.ReplaceAll(symbol->Name(), newname);
+      const AliHLTTriggerMenuItem* item = menu->Item(i);
+      if (item->Priority() == priority) priorityGroup[n].push_back(i);
     }
+  }
+  
+  for (size_t n = 0; n < priorityGroup.size(); n++)
+  {
     if (fDebugMode)
     {
-      code << "    HLTDebug(\"Trying trigger condition " << i
-           << " (Description = '%s').\", fMenuItemDescription" << i << ".Data());"
-           << endl;
+      code << "    HLTDebug(Form(\"Processing trigger priority group " << priorities[n] << "\"));" << endl;
     }
-    code << "    if (" << item->TriggerCondision() << ") {" << endl;
-    code << "      IncrementCounter(" << i << ");" << endl;
-    const char* indentation = "";
-    if (item->PreScalar() != 0)
+    code << "    ";
+    if (n == 0) code << "UInt_t ";
+    code << "_previous_match_ = 0xFFFFFFFF;" << endl;
+    code << "    ";
+    if (n == 0) code << "bool ";
+    code << "_trigger_matched_ = false;" << endl;
+    code << "    ";
+    if (n == 0) code << "bool ";
+    code << "_group_result_ = false;" << endl;
+    std::vector<TString> conditionOperator;
+    conditionOperator.insert(conditionOperator.begin(), priorityGroup[n].size(), TString(""));
+    std::vector<TString> domainOperator;
+    domainOperator.insert(domainOperator.begin(), priorityGroup[n].size(), TString(""));
+    for (size_t m = 0; m < priorityGroup[n].size(); m++)
     {
-      indentation = "  ";
-      code << "      if ((GetCounter(" << i << ") % " << item->PreScalar() << ") == 1) {" << endl;
+      UInt_t i = priorityGroup[n][m];
+      const AliHLTTriggerMenuItem* item = menu->Item(i);
+      TString triggerCondition = item->TriggerCondition();
+      TString mergeExpr = item->MergeExpression();
+      // Replace the symbols found in the trigger condition and merging expressions
+      // with appropriate compilable versions.
+      for (Int_t j = 0; j < symbols.GetEntriesFast(); j++)
+      {
+        AliHLTTriggerMenuSymbol* symbol = static_cast<AliHLTTriggerMenuSymbol*>( symbols.UncheckedAt(j) );
+        bool symbolNamesDifferent = strcmp(symbol->RealName(), symbol->Name()) != 0;
+        if (strcmp(symbol->ObjectClass(), "AliHLTTriggerDecision") == 0)
+        {
+          TString newname = symbol->Name();
+          newname += "TriggerDomain";
+          mergeExpr.ReplaceAll(symbol->RealName(), newname);
+        }
+        else
+        {
+          if (symbolNamesDifferent) mergeExpr.ReplaceAll(symbol->RealName(), symbol->Name());
+        }
+        if (symbolNamesDifferent) triggerCondition.ReplaceAll(symbol->RealName(), symbol->Name());
+      }
+      // We allow the trigger conditions and merging expressions to have trailing operators.
+      // Thus, we need to extract the operators and cleanup the expressions so that they will
+      // compile. This means that we silently ignore the trailing operator if not needed.
+      if (ExtractedOperator(triggerCondition, conditionOperator[m]))
+      {
+        // If the trailing operator is the same as the default operator then reset
+        // the value in the operator list so that the default is used in the generated
+        // code. This creates more compact code.
+        if (conditionOperator[m] == menu->DefaultConditionOperator()) conditionOperator[m] = "";
+      }
+      if (ExtractedOperator(mergeExpr, domainOperator[m]))
+      {
+        if (domainOperator[m] == menu->DefaultDomainOperator()) domainOperator[m] = "";
+      }
+      if (fDebugMode)
+      {
+        code << "    HLTDebug(Form(\"Trying trigger condition " << i
+             << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));"
+             << endl;
+      }
+      code << "    ";
+      if (n == 0 and m == 0) code << "bool ";
+      code << "_item_result_ = false;" << endl;
+      code << "    if (" << triggerCondition << ") {" << endl;
+      code << "      ++fCounter[" << i << "];" << endl;
+      const char* indentation = "";
+      if (item->PreScalar() != 0)
+      {
+        indentation = "  ";
+        code << "      if ((fCounter[" << i << "] % " << item->PreScalar() << ") == 1) {" << endl;
+      }
+      code << indentation << "      _item_result_ = true;" << endl;
+      if (fDebugMode)
+      {
+        code << indentation << "      HLTDebug(Form(\"Matched trigger condition " << i
+             << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" << endl;
+      }
+      if (item->PreScalar() != 0)
+      {
+        code << "      }" << endl;
+      }
+      code << "    }" << endl;
+      if (m == 0)
+      {
+        // Since this is the first item of the trigger group,
+        // the generated trigger logic can be simplified a little.
+        code << "    _group_result_ = _item_result_;" << endl;
+        code << "    if (_item_result_) {" << endl;
+        code << "      _domain_ = " << mergeExpr.Data() << ";" << endl;
+        code << "      _description_ = fMenuItemDescription" << i << ";" << endl;
+        code << "      _previous_match_ = " << i << ";" << endl;
+        code << "      _trigger_matched_ = true;" << endl;
+        code << "    }" << endl;
+      }
+      else
+      {
+        if (conditionOperator[m-1] == "")
+        {
+          code << "    _group_result_ = _group_result_ "
+               << menu->DefaultConditionOperator() << " _item_result_;" << endl;
+        }
+        else
+        {
+          code << "    _group_result_ = _group_result_ "
+               << conditionOperator[m-1] << " _item_result_;" << endl;
+        }
+        code << "    if (_item_result_) {" << endl;
+        code << "      if (_trigger_matched_) {" << endl;
+        bool switchWillBeEmpty = true;
+        for (size_t k = 0; k < m; k++)
+        {
+          if (domainOperator[k] == "") continue;
+          switchWillBeEmpty = false;
+        }
+        if (switchWillBeEmpty)
+        {
+          code << "        _domain_ = _domain_ " << menu->DefaultDomainOperator() << " "
+               << mergeExpr.Data() << ";" << endl;
+        }
+        else
+        {
+          code << "        switch(_previous_match_) {" << endl;
+          for (size_t k = 0; k < m; k++)
+          {
+            if (domainOperator[k] == "") continue;
+            code << "        case " << k << ": _domain_ = _domain_ "
+                 << domainOperator[k] << " " << mergeExpr.Data() << "; break;" << endl;
+          }
+          code << "        default: _domain_ = _domain_ "
+               << menu->DefaultDomainOperator() << " " << mergeExpr.Data() << ";" << endl;
+          code << "        }" << endl;
+        }
+        code << "        _description_ += \",\";" << endl;
+        code << "        _description_ += fMenuItemDescription" << i << ";" << endl;
+        code << "      } else {" << endl;
+        code << "        _domain_ = " << mergeExpr.Data() << ";" << endl;
+        code << "        _description_ = fMenuItemDescription" << i << ";" << endl;
+        code << "      }" << endl;
+        code << "      _previous_match_ = " << i << ";" << endl;
+        code << "      _trigger_matched_ = true;" << endl;
+        code << "    }" << endl;
+      }
     }
-    code << indentation << "      _domain_ = " << mergeExpr.Data() << ";" << endl;
-    code << indentation << "      _description_ = fMenuItemDescription" << i << ";" << endl;
+    code << "    if (_group_result_) {" << endl;
     if (fDebugMode)
     {
-      code << indentation << "      HLTDebug(\"Matched trigger condition " << i
-           << " (Description = '%s').\", fMenuItemDescription" << i << ".Data());" << endl;
-    }
-    code << indentation << "      return true;" << endl;
-    if (item->PreScalar() != 0)
-    {
-      code << "      }" << endl;
+      if (n < priorities.size() - 1)
+      {
+        code << "      HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n]
+             << ". Stopping processing here because all other trigger groups have lower priority.\"));" << endl;
+      }
+      else
+      {
+        code << "      HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] << ".\"));" << endl;
+      }
     }
+    code << "      return true;" << endl;
     code << "    }" << endl;
   }
+  code << "    _domain_.Clear();" << endl;
+  code << "    _description_ = \"\";" << endl;
   code << "    return false;" << endl;
   code << "  }" << endl;
   
-  // Generate the custom Factory class.
-  code << "  class FactoryImpl : public AliHLTGlobalTrigger::Factory" << endl;
-  code << "  {" << endl;
-  code << "  public:" << endl;
-  code << "    virtual const char* ClassName() const {" << endl;
-  code << "      return \"" << name << "\";" << endl;
-  code << "    }" << endl;
-  code << "    virtual AliHLTGlobalTrigger* New() const {" << endl;
-  code << "      return new " << name << "();" << endl;
-  code << "    }" << endl;
-  code << "  };" << endl;
+  // Generate getter and setter methods for the counters.
+  code << "  const TArrayL64& GetCounters() const { return fCounter; }" << endl;
+  code << "  void SetCounters(const TArrayL64& counters) { fCounter = counters; }" << endl;
   
   code << "private:" << endl;
   // Add the symbols in the trigger menu to the list of private variables.
@@ -572,12 +1066,16 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   {
     code << "  TString fMenuItemDescription" << i << ";" << endl;
   }
+  code << "  TArrayL64 fCounter;" << endl;
+  code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
+  code << "  ClassDef(" << name.Data() << ", 0)" << endl;
+  code << "#else" << endl;
+  code << "  virtual const char* Class_Name() const { return \"" << name.Data() << "\"; }" << endl;
+  code << "#endif" << endl;
   code << "};" << endl;
-  
-  // Write a global object for the Factory class for automatic registration.
-  code << "namespace {" << endl;
-  code << "  const " << name << "::FactoryImpl gkFactoryImpl;" << endl;
-  code << "};" << endl;
+  code << "#if !defined(__CINT__) || defined(__MAKECINT__)" << endl;
+  code << "ClassImp(" << name.Data() << ")" << endl;
+  code << "#endif" << endl;
   
   code.close();
   
@@ -593,17 +1091,29 @@ int AliHLTGlobalTriggerComponent::LoadTriggerClass(
 {
   // Loads the code for a custom global trigger class implementation on the fly.
   
+  HLTDebug("Loading HLT trigger class from file '%s'.", filename);
+  
   TString compiler = gSystem->GetBuildCompilerVersion();
-  if (compiler.Contains("gcc") or compiler.Contains("icc"))
+  if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc")))
   {
-    TString includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger";
+    TString includePath;
+#if defined(PKGINCLUDEDIR)
+    // this is especially for the HLT build system where the package is installed
+    // in a specific directory including proper treatment of include files
+    includePath.Form("-I%s", PKGINCLUDEDIR);
+#else
+    // the default AliRoot behavior, all include files can be found in the
+    // $ALICE_ROOT subfolders
+    includePath = "-I${ALICE_ROOT}/include -I${ALICE_ROOT}/HLT/BASE -I${ALICE_ROOT}/HLT/trigger";
+#endif
     // Add any include paths that were specified on the command line.
     for (Int_t i = 0; i < includePaths.GetEntriesFast(); i++)
     {
-      TString path = static_cast<const TObjString*>(includePaths.UncheckedAt(i))->String();
-      includePath += " ";
+      TString path = static_cast<TObjString*>(includePaths.UncheckedAt(i))->String();
+      includePath += " -I";
       includePath += path;
     }
+    HLTDebug("using include settings: %s", includePath.Data());
     gSystem->SetIncludePath(includePath);
     gSystem->SetFlagsOpt("-O3 -DNDEBUG");
     gSystem->SetFlagsDebug("-g3 -DDEBUG -D__DEBUG");
@@ -625,6 +1135,9 @@ int AliHLTGlobalTriggerComponent::LoadTriggerClass(
   }
   else
   {
+    // Store the library state to be checked later in UnloadTriggerClass.
+    fLibStateAtLoad = gSystem->GetLibraries();
+    
     // If we do not support the compiler then try interpret the class instead.
     TString cmd = ".L ";
     cmd += filename;
@@ -644,6 +1157,103 @@ int AliHLTGlobalTriggerComponent::LoadTriggerClass(
 }
 
 
+int AliHLTGlobalTriggerComponent::UnloadTriggerClass(const char* filename)
+{
+  // Unloads the code previously loaded by LoadTriggerClass.
+  
+  HLTDebug("Unloading HLT trigger class in file '%s'.", filename);
+  
+  TString compiler = gSystem->GetBuildCompilerVersion();
+  if (fRuntimeCompile && (compiler.Contains("gcc") or compiler.Contains("icc")))
+  {
+    // Generate the library name.
+    TString libname = filename;
+    Ssiz_t dotpos = libname.Last('.');
+    if (0 <= dotpos and dotpos < libname.Length()) libname[dotpos] = '_';
+    libname += ".";
+    libname += gSystem->GetSoExt();
+    
+    // This is a workaround for a problem with unloading shared libraries in ROOT.
+    // If the trigger logic library is loaded before the libAliHLTHOMER.so library
+    // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile
+    // call all the subsequent libraries get unloded. This means that any objects created
+    // from classes implemented in the libAliHLTHOMER.so library will generate segfaults
+    // since the executable code has been unloaded.
+    // We need to check if there are any more libraries loaded after the class we
+    // are unloading and in that case don't unload the class.
+    TString libstring = gSystem->GetLibraries();
+    TString token, lastlib;
+    Ssiz_t from = 0;
+    Int_t numOfLibs = 0, posOfLib = -1;
+    while (libstring.Tokenize(token, from, " "))
+    {
+      ++numOfLibs;
+      lastlib = token;
+      if (token.Contains(libname)) posOfLib = numOfLibs;
+    }
+    if (numOfLibs != posOfLib)
+    {
+      HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the shared"
+          " library '%s' since another library '%s' was loaded afterwards. Trying to"
+          " unload this library will remove the others and lead to serious memory faults.",
+          libname.Data(), lastlib.Data()
+      ));
+      return 0;
+    }
+    
+    char* path = NULL;
+    int result = 0;
+    if ((path = gSystem->DynamicPathName(libname)) != NULL)
+    {
+      result = gInterpreter->UnloadFile(path);
+      delete [] path;
+    }
+    if (result != TInterpreter::kNoError) return -ENOENT;
+  }
+  else
+  {
+    // This is again a workaround for the problem with unloading files in ROOT.
+    // If the trigger logic class is loaded before the libAliHLTHOMER.so library
+    // or any other library is loaded afterwards, then during the gInterpreter->UnloadFile
+    // call all the subsequent libraries get unloded.
+    // We need to check if the list of loaded libraries has changed since the last
+    // call to LoadTriggerClass. If it has then don't unload the class.
+    if (fLibStateAtLoad != gSystem->GetLibraries())
+    {
+      TString libstring = gSystem->GetLibraries();
+      TString token;
+      Ssiz_t from = 0;
+      while (libstring.Tokenize(token, from, " "))
+      {
+        if (not fLibStateAtLoad.Contains(token)) break;
+      }
+      HLTWarning(Form("ROOT limitation! Cannot properly cleanup and unload the file"
+          " '%s' since another library '%s' was loaded afterwards. Trying to unload"
+          " this file will remove the other library and lead to serious memory faults.",
+          filename, token.Data()
+      ));
+      return 0;
+    }
+  
+    // If we did not compile the trigger logic then remove the interpreted class.
+    TString cmd = ".U ";
+    cmd += filename;
+    Int_t errorcode = TInterpreter::kNoError;
+    gROOT->ProcessLine(cmd, &errorcode);
+    if (errorcode != TInterpreter::kNoError)
+    {
+      HLTFatal("Could not unload interpreted global trigger menu implementation"
+               " (Interpreter error code = %d).",
+               errorcode
+      );
+      return -ENOENT;
+    }
+  }
+  
+  return 0;
+}
+
+
 int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArray& list)
 {
   // Searches for the named symbol in the given list.
@@ -665,6 +1275,12 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
   // implementation class.
   // See header for more details.
   
+  // Note: when we build the symbol list we must use the symbol name as returned
+  // by the Name() method and not the RealName() method when using FindSymbol.
+  // This is so that we avoid problems with the generated code not compiling
+  // because names like "abc-xyz" and "abc_xyz" are synonymous.
+  // Name() returns the converted C++ symbol name as used in the generated code.
+  
   for (UInt_t i = 0; i < menu->NumberOfSymbols(); i++)
   {
     const AliHLTTriggerMenuSymbol* symbol = menu->Symbol(i);
@@ -676,20 +1292,40 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
     new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(*symbol);
   }
   
-  TRegexp exp("[_a-zA-Z][_a-zA-Z0-9]*");
+  TRegexp exp("[_a-zA-Z][-_a-zA-Z0-9]*");
+  TRegexp hexexp("x[a-fA-F0-9]+");
   for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
   {
     const AliHLTTriggerMenuItem* item = menu->Item(i);
-    TString str = item->TriggerCondision();
+    TString str = item->TriggerCondition();
     Ssiz_t start = 0;
     do
     {
       Ssiz_t length = 0;
       Ssiz_t pos = exp.Index(str, &length, start);
       if (pos == kNPOS) break;
-      TString s = str(pos, length);
       start = pos+length;
       
+      // Check if there is a numerical character before the found
+      // regular expression. If so, then the symbol is not a valid one
+      // and should be skipped.
+      if (pos > 0)
+      {
+        bool notValid = false;
+        switch (str[pos-1])
+        {
+        case '0': case '1': case '2': case '3': case '4':
+        case '5': case '6': case '7': case '8': case '9':
+          notValid = true;
+          break;
+        default:
+          notValid = false;
+          break;
+        }
+        if (notValid) continue;
+      }
+      TString s = str(pos, length);
+      
       if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or
           s == "compl" or s == "not" or s == "not_eq" or s == "or" or
           s == "or_eq" or s == "xor" or s == "xor_eq" or s == "true" or
@@ -699,15 +1335,17 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
         // Ignore iso646.h and other keywords.
         continue;
       }
-      
-      if (FindSymbol(s.Data(), list) == -1)
+
+      // Need to create the symbols first and check if its name is in the list
+      // before actually adding it to the symbols list.
+      AliHLTTriggerMenuSymbol newSymbol;
+      newSymbol.Name(s.Data());
+      newSymbol.Type("bool");
+      newSymbol.ObjectClass("AliHLTTriggerDecision");
+      newSymbol.AssignExpression("this->Result()");
+      newSymbol.DefaultValue("false");
+      if (FindSymbol(newSymbol.Name(), list) == -1)
       {
-        AliHLTTriggerMenuSymbol newSymbol;
-        newSymbol.Name(s.Data());
-        newSymbol.Type("bool");
-        newSymbol.ObjectClass("AliHLTTriggerDecision");
-        newSymbol.AssignExpression("this->Result()");
-        newSymbol.DefaultValue("false");
         new (list[list.GetEntriesFast()]) AliHLTTriggerMenuSymbol(newSymbol);
       }
     }
@@ -717,3 +1355,123 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
   return 0;
 }
 
+
+bool AliHLTGlobalTriggerComponent::ExtractedOperator(TString& expr, TString& op)
+{
+  // Extracts the trailing operator from the expression.
+  
+  Ssiz_t i = 0;
+  // First skip the trailing whitespace.
+  bool whitespace = true;
+  for (i = expr.Length()-1; i >= 0 and whitespace; i--)
+  {
+    switch (expr[i])
+    {
+    case ' ': case '\t': case '\r': case '\n':
+      whitespace = true;
+      break;
+    default:
+      whitespace = false;
+    }
+  }
+  if (i < 0 or whitespace) return false;
+  
+  // Now find the first whitespace character before the trailing symbol.
+  bool nonwhitespace = true;
+  for (; i >= 0 and nonwhitespace; i--)
+  {
+    switch (expr[i])
+    {
+    case ' ': case '\t': case '\r': case '\n':
+      nonwhitespace = false;
+      break;
+    default:
+      nonwhitespace = true;
+    }
+  }
+  if (i < 0 or nonwhitespace) return false;
+  
+  // Extract the last symbols and check if it is a valid operator.
+  TString s = expr;
+  s.Remove(0, i+2);
+  if (s == "and" or s == "and_eq" or s == "bitand" or s == "bitor" or
+      s == "compl" or s == "not" or s == "not_eq" or s == "or" or
+      s == "or_eq" or s == "xor" or s == "xor_eq" or s == "&&" or
+      s == "&=" or s == "&" or s == "|" or s == "~" or s == "!" or
+      s == "!=" or s == "||" or s == "|=" or s == "^" or s == "^=" or
+      s == "==" or s == "+" or s == "-" or s == "*" or s == "/" or
+      s == "%" or s == ">" or s == "<" or s == ">=" or s == "<="
+     )
+  {
+    expr.Remove(i+1);
+    op = s;
+    return true;
+  }
+  
+  return false;
+}
+
+
+int AliHLTGlobalTriggerComponent::PrintStatistics(const AliHLTGlobalTrigger* pTrigger, AliHLTComponentLogSeverity level, int offset) const
+{
+  // print some statistics
+  int totalEvents=GetEventCount()+offset;
+  const TArrayL64& counters = pTrigger->GetCounters();
+  if (pTrigger->CallFailed()) return -EPROTO;
+  for (int i = 0; i < counters.GetSize(); i++) {
+    ULong64_t count = counters[i];
+    float ratio=0;
+    if (totalEvents>0) ratio=100*(float)count/totalEvents;
+    HLTLog(level, "Item %d: total events: %d - counted events: %llu (%.1f%%)", i, totalEvents, count, ratio);
+  }
+  return 0;
+}
+
+int AliHLTGlobalTriggerComponent::AddCTPDecisions(AliHLTGlobalTrigger* pTrigger, const AliHLTCTPData* pCTPData, const AliHLTComponentTriggerData* trigData)
+{
+  // add trigger decisions for the valid CTP classes
+  if (!pCTPData || !pTrigger) return 0;
+
+  AliHLTUInt64_t triggerMask=pCTPData->Mask();
+  AliHLTUInt64_t bit0=0x1;
+  if (!fCTPDecisions) {
+    fCTPDecisions=new TClonesArray(AliHLTTriggerDecision::Class(), gkNCTPTriggerClasses);
+    if (!fCTPDecisions) return -ENOMEM;
+
+    fCTPDecisions->ExpandCreate(gkNCTPTriggerClasses);
+    for (int i=0; i<gkNCTPTriggerClasses; i++) {
+      const char* name=pCTPData->Name(i);
+      if (triggerMask&(bit0<<i) && name) {
+       AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i));
+       assert(pDecision);
+       if (!pDecision) {
+         delete fCTPDecisions;
+         fCTPDecisions=NULL;
+         return -ENOENT;
+       }
+       pDecision->Name(name);
+      }
+    }
+  }
+
+  for (int i=0; i<gkNCTPTriggerClasses; i++) {
+    const char* name=pCTPData->Name(i);
+    if ((triggerMask&(bit0<<i))==0 || name==NULL) continue;
+    AliHLTTriggerDecision* pDecision=dynamic_cast<AliHLTTriggerDecision*>(fCTPDecisions->At(i));
+    HLTDebug("updating CTP trigger decision %d %s (%p casted %p)", i, name, fCTPDecisions->At(i), pDecision);
+    if (!pDecision) return -ENOENT;
+
+    bool result=false;
+    if (trigData) result=pCTPData->EvaluateCTPTriggerClass(name, *trigData);
+    else result=pCTPData->EvaluateCTPTriggerClass(name);
+    pDecision->Result(result);
+    pDecision->TriggerDomain().Clear();
+    if (trigData) pDecision->TriggerDomain().Add(pCTPData->ReadoutList(*trigData));
+    else pDecision->TriggerDomain().Add(pCTPData->ReadoutList());
+
+    pTrigger->Add(fCTPDecisions->At(i), kAliHLTDataTypeTriggerDecision, kAliHLTVoidDataSpec);
+    if (pTrigger->CallFailed()) return -EPROTO;
+  }
+
+  return 0;
+}