]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/trigger/AliHLTGlobalTriggerComponent.cxx
putting back the lost statistics message at EOR
[u/mrichter/AliRoot.git] / HLT / trigger / AliHLTGlobalTriggerComponent.cxx
index 5dc78fa7e5f7f1d824fff1c8bdd53f3152c96520..aa9b11f21ab6929f3a4c33ca390f5e564d152288 100644 (file)
@@ -44,6 +44,7 @@
 #include "TDatime.h"
 #include "TClass.h"
 #include "TNamed.h"
+#include "TRandom3.h"
 #include <fstream>
 #include <cerrno>
 #include <cassert>
@@ -87,7 +88,8 @@ AliHLTGlobalTriggerComponent::AliHLTGlobalTriggerComponent() :
        fMonitorPeriod(-1),
        fUniqueID(0),
        fSoftwareTrigger(true, "SOFTWARE"),
-       fTotalEventCounter(0)
+       fTotalEventCounter(0),
+       fCDH(NULL)
 {
   // Default constructor.
   
@@ -139,6 +141,9 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
   fIncludeFiles.Clear();
   SetBit(kIncludeInput);
   fDataEventsOnly = true;
+  UInt_t randomSeed = 0;
+  bool randomSeedSet = false;
+  fCDH = NULL;
   
   for (int i = 0; i < argc; i++)
   {
@@ -325,6 +330,31 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
       fMakeSoftwareTriggers = false;
       continue;
     }
+    if (strcmp(argv[i], "-randomseed") == 0)
+    {
+      if (randomSeedSet)
+      {
+        HLTWarning("The random seed was already specified previously with the option -randomseed."
+                   "Will replace the previous value of %d with the new one.",
+                   randomSeed
+        );
+      }
+      if (argc <= i+1)
+      {
+        HLTError("The number to use as the seed was not specified for the -randomseed option." );
+        return -EINVAL;
+      }
+      TString numstr = argv[i+1];
+      if (not numstr.IsDigit())
+      {
+        HLTError("The number specified in the -randomseed option is not a valid decimal integer." );
+        return -EINVAL;
+      }
+      randomSeed = numstr.Atoi();
+      randomSeedSet = true;
+      i++;
+      continue;
+    }
     
     HLTError("Unknown option '%s'.", argv[i]);
     return -EINVAL;
@@ -389,6 +419,14 @@ Int_t AliHLTGlobalTriggerComponent::DoInit(int argc, const char** argv)
   SetDescription(menu->DefaultDescription());
   SetTriggerDomain(menu->DefaultTriggerDomain());
   
+  // Initialise the random number generator seed value.
+  // NOTE: The GenerateTrigger method called above will set the fUniqueID value
+  // with a random value based on a GUID that should be unique across the system.
+  // This is then used as the seed to the random number generator if the -randomseed
+  // option is not used.
+  if (not randomSeedSet) randomSeed = fUniqueID;
+  gRandom->SetSeed(randomSeed);
+  
   fTotalEventCounter = 0;
   return 0;
 }
@@ -409,6 +447,7 @@ Int_t AliHLTGlobalTriggerComponent::DoDeinit()
     delete fCTPDecisions;
   }
   fCTPDecisions=NULL;
+  fCDH = NULL;
   
   Int_t result = UnloadTriggerClass(fCodeFileName);
   if (result != 0) return result;
@@ -454,13 +493,19 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
 
   AliHLTUInt32_t eventType=0;
   if (!IsDataEvent(&eventType)) {
-    if (eventType==gkAliEventTypeEndOfRun) PrintStatistics(fTrigger, kHLTLogImportant, 0);
+    if (eventType==gkAliEventTypeEndOfRun)
+    {
+      PrintStatistics(fTrigger, kHLTLogImportant);
+    }
     if (fDataEventsOnly)
     {
       IgnoreEvent();  // dont generate any trigger decision.
       return 0;
     }
   }
+  
+  fCDH = NULL;  // have to reset this in case ExtractTriggerData fails.
+  ExtractTriggerData(*GetTriggerData(), NULL, NULL, &fCDH, NULL);
 
   // Copy the trigger counters in case we need to set them back to their original
   // value because the PushBack method fails with ENOSPC.
@@ -495,14 +540,15 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
   // back the new global trigger decision object.
   TString description;
   AliHLTTriggerDomain triggerDomain;
-  bool triggerResult = fTrigger->CalculateTriggerDecision(triggerDomain, description);
+  bool triggerResult = false;
+  bool matchedItems = fTrigger->CalculateTriggerDecision(triggerResult, 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()
+      (matchedItems == true) ? triggerDomain : GetTriggerDomain(),
+      (matchedItems == true) ? description.Data() : GetDescription()
     );
 
   decision.SetUniqueID(fUniqueID);
@@ -566,11 +612,12 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
   
   static UInt_t lastTime=0;
   TDatime time;
-  if (time.Get()-lastTime>60) {
+  if (time.Get()-lastTime>60)
+  {
     lastTime=time.Get();
     PrintStatistics(fTrigger, kHLTLogImportant);
   }
-
+  
   // add readout filter to event done data
   CreateEventDoneReadoutFilter(decision.TriggerDomain(), 3);
 
@@ -594,6 +641,29 @@ int AliHLTGlobalTriggerComponent::DoTrigger()
     // add monitoring filter list
     CreateEventDoneReadoutFilter(monitoringFilter, 4);
   }
+  
+  // Mask the readout list according to the CTP trigger
+  // if the classes have been initialized (mask non-zero).
+  // If we are dealing with a software trigger on the other hand then
+  // mask with the participating detector list.
+  // In both cases we must make sure that HLT is part of the readout mask.
+  if (CTPData() != NULL and CTPData()->Mask() != 0x0)
+  {
+    AliHLTReadoutList readoutlist = decision.ReadoutList();
+    AliHLTReadoutList ctpreadout = CTPData()->ReadoutList(*GetTriggerData());
+    ctpreadout.Enable(AliHLTReadoutList::kHLT);
+    readoutlist.AndEq(ctpreadout);
+    decision.ReadoutList(readoutlist); // override the readout list with the masked one.
+  }
+  else if (softwareTriggerIsValid)
+  {
+    assert(fCDH != NULL);
+    AliHLTReadoutList readoutlist = decision.ReadoutList();
+    UInt_t detectors = fCDH->GetSubDetectors();
+    AliHLTReadoutList softwareReadout(Int_t(detectors | AliHLTReadoutList::kHLT));
+    readoutlist.AndEq(softwareReadout);
+    decision.ReadoutList(readoutlist); // override the readout list with the masked one.
+  }
 
   if (TriggerEvent(&decision, kAliHLTDataTypeGlobalTrigger) == -ENOSPC)
   {
@@ -703,6 +773,11 @@ int AliHLTGlobalTriggerComponent::LoadTriggerMenu(const char* cdbPath, const Ali
     return -EIO;
   }
   Int_t version = store->GetLatestVersion(cdbPath, GetRunNo());
+  if (version < 0)
+  {
+    HLTError("Could not find an entry in the CDB for \"%s\".", cdbPath);
+    return -EIO;
+  }
   Int_t subVersion = store->GetLatestSubVersion(cdbPath, GetRunNo(), version);
   AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, GetRunNo(), version, subVersion);
   if (entry == NULL)
@@ -733,9 +808,13 @@ void AliHLTGlobalTriggerComponent::GenerateFileName(TString& name, TString& file
   // Creates a unique file name for the generated code.
   
   TUUID guid = GenerateGUID();
-  UChar_t buf[16];
+  union
+  {
+    UChar_t buf[16];
+    UInt_t bufAsInt[4];
+  };
   guid.GetUUID(buf);
-  fUniqueID = *reinterpret_cast<UInt_t*>(buf);
+  fUniqueID = bufAsInt[0];
   TString guidstr = guid.AsString();
   // Replace '-' with '_' in string.
   for (int i = 0; i < guidstr.Length(); ++i)
@@ -786,6 +865,7 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   code << "#include \"TClass.h\"" << endl;
   code << "#include \"TString.h\"" << endl;
   code << "#include \"TClonesArray.h\"" << endl;
+  code << "#include \"TRandom3.h\"" << endl;
   code << "#include \"AliHLTLogging.h\"" << endl;
   code << "#include \"AliHLTGlobalTrigger.h\"" << endl;
   code << "#include \"AliHLTGlobalTriggerDecision.h\"" << endl;
@@ -1101,7 +1181,7 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
   // 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;
+  code << "  virtual bool CalculateTriggerDecision(bool& _trigger_result_, AliHLTTriggerDomain& _domain_, TString& _description_) {" << endl;
   if (fDebugMode)
   {
     code << "#ifdef __CINT__" << endl;
@@ -1205,10 +1285,22 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
       code << "    if (" << triggerCondition << ") {" << endl;
       code << "      ++fCounter[" << i << "];" << endl;
       const char* indentation = "";
-      if (item->PreScalar() != 0)
+      // Generate the code to handle the prescalar and scale-down
+      bool havePrescalar = item->PreScalar() != 0;
+      bool haveScaledown = item->ScaleDown() < 1;
+      if (havePrescalar or haveScaledown) 
       {
         indentation = "  ";
-        code << "      if ((fCounter[" << i << "] % " << item->PreScalar() << ") == 1) {" << endl;
+        code << "      if (";
+        if (havePrescalar) code << "(fCounter[" << i << "] % " << item->PreScalar() << ") == 1";
+        if (havePrescalar and haveScaledown) code << " && ";
+        if (haveScaledown)
+        {
+          std::streamsize oldprecision = code.precision(17);
+          code << "gRandom->Rndm() < " << item->ScaleDown();
+          code.precision(oldprecision);
+        }
+        code << ") {" << endl;
       }
       code << indentation << "      _item_result_ = true;" << endl;
       if (fDebugMode)
@@ -1216,7 +1308,7 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
         code << indentation << "      HLTDebug(Form(\"Matched trigger condition " << i
              << " (Description = '%s').\", fMenuItemDescription" << i << ".Data()));" << endl;
       }
-      if (item->PreScalar() != 0)
+      if (havePrescalar or haveScaledown)
       {
         code << "      }" << endl;
       }
@@ -1295,11 +1387,33 @@ int AliHLTGlobalTriggerComponent::GenerateTrigger(
         code << "      HLTDebug(Form(\"Matched triggers in trigger priority group " << priorities[n] << ".\"));" << endl;
       }
     }
+    bool methodReturnResult = true;
+    if (priorityGroup[n].size() > 0)
+    {
+      const AliHLTTriggerMenuItem* item = menu->Item(priorityGroup[n][0]);
+      methodReturnResult = item->DefaultResult();
+    }
+    // Check to see if the items of the group all have the same default result.
+    // If not then warn the user since only the first item's value will be used.
+    for (size_t m = 1; m < priorityGroup[n].size(); m++)
+    {
+      const AliHLTTriggerMenuItem* item = menu->Item(priorityGroup[n][m]);
+      if (item->DefaultResult() != methodReturnResult)
+      {
+        HLTWarning("Found items with different default results set for priority group %d."
+                   "Will only use the value from the first item.",
+                   item->Priority()
+                  );
+        break;
+      }
+    }
+    code << "      _trigger_result_ = " << (methodReturnResult ? "true" : "false") << ";" << endl;
     code << "      return true;" << endl;
     code << "    }" << endl;
   }
   code << "    _domain_.Clear();" << endl;
   code << "    _description_ = \"\";" << endl;
+  code << "    _trigger_result_ = " << (menu->DefaultResult() ? "true" : "false") << ";" << endl;
   code << "    return false;" << endl;
   code << "  }" << endl;
   
@@ -1526,6 +1640,21 @@ int AliHLTGlobalTriggerComponent::FindSymbol(const char* name, const TClonesArra
 }
 
 
+namespace
+{
+  /**
+   * Helper routine to compare two trigger menu symbols to see if b is a subset of a.
+   * \returns true if b is a subset of a or they are unrelated.
+   */
+  bool AliHLTCheckForContainment(const AliHLTTriggerMenuSymbol* a, const AliHLTTriggerMenuSymbol* b)
+  {
+    TString bstr = b->Name();
+    return bstr.Contains(a->Name());
+  }
+
+} // end of namespace
+
+
 int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu, TClonesArray& list)
 {
   // Builds the list of symbols to use in the custom global trigger menu
@@ -1556,9 +1685,11 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
       return -ENOMEM;
     }
   }
+  Int_t initialEntryCount = list.GetEntriesFast();
   
-  TRegexp exp("[_a-zA-Z][-_a-zA-Z0-9]*");
-  TRegexp hexexp("x[a-fA-F0-9]+");
+  // Note: the \\. must not be the first element in the character class, otherwise
+  // it is interpreted as an "any character" dot symbol.
+  TRegexp exp("[_a-zA-Z][-\\._a-zA-Z0-9]*");
   for (UInt_t i = 0; i < menu->NumberOfItems(); i++)
   {
     const AliHLTTriggerMenuItem* item = menu->Item(i);
@@ -1600,6 +1731,27 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
         // Ignore iso646.h and other keywords.
         continue;
       }
+      
+      // We need to handle the special case where the symbol contains a dot.
+      // In C++ this is a dereferencing operator. So we need to check if the
+      // current symbol we are handling starts with the same string as any of
+      // the existing symbols defined manually in the symbols table.
+      // If we do find such a case then revert to treating the dot as an operator
+      // rather than part of the symbol name. i.e. skip adding the automatic symbol.
+      bool dereferencedSymbol = false;
+      for (int j = 0; j < initialEntryCount; j++)
+      {
+        const AliHLTTriggerMenuSymbol* symbol = dynamic_cast<const AliHLTTriggerMenuSymbol*>( list.UncheckedAt(j) );
+        if (symbol == NULL) continue;
+        TString symstr = symbol->Name();
+        symstr += ".";
+        if (s.BeginsWith(symstr))
+        {
+          dereferencedSymbol = true;
+          break;
+        }
+      }
+      if (dereferencedSymbol) continue;
 
       // Need to create the symbols first and check if its name is in the list
       // before actually adding it to the symbols list.
@@ -1625,6 +1777,30 @@ int AliHLTGlobalTriggerComponent::BuildSymbolList(const AliHLTTriggerMenu* menu,
     while (start < str.Length());
   }
   
+  // This last part is necessary to make sure that symbols are replaced in the
+  // trigger condition and domain merging expressions in a greedy manner.
+  // I.e. we need to make sure that if one symbol's string representation is
+  // contained inside another (string subset) that the longer symbol name is
+  // always first in the symbols list.
+  // This will work because the symbol table is traversed from first to last
+  // element and TString::ReplaceAll is used to replace the substrings inside
+  // the AliHLTGlobalTriggerComponent::GenerateTrigger method.
+  std::vector<AliHLTTriggerMenuSymbol*> orderedList;
+  for (Int_t i = 0; i < list.GetEntriesFast(); i++)
+  {
+    orderedList.push_back( static_cast<AliHLTTriggerMenuSymbol*>(list.UncheckedAt(i)) );
+  }
+  std::sort(orderedList.begin(), orderedList.end(), AliHLTCheckForContainment);
+  //std::sort(orderedList.begin(), orderedList.end());
+  // Now swap values around according to the orderedList.
+  for (Int_t i = 0; i < list.GetEntriesFast(); i++)
+  {
+    AliHLTTriggerMenuSymbol* target = static_cast<AliHLTTriggerMenuSymbol*>(list.UncheckedAt(i));
+    AliHLTTriggerMenuSymbol tmp = *target;
+    *target = *orderedList[i];
+    *orderedList[i] = tmp;
+  }
+  
   return 0;
 }
 
@@ -1688,9 +1864,9 @@ bool AliHLTGlobalTriggerComponent::ExtractedOperator(TString& expr, TString& op)
 bool AliHLTGlobalTriggerComponent::FillSoftwareTrigger()
 {
   // Fills the fSoftwareTrigger structure.
-  const AliRawDataHeader* cdh;
-  if (ExtractTriggerData(*GetTriggerData(), NULL, NULL, &cdh, NULL) != 0) return false;
-  UChar_t l1msg = cdh->GetL1TriggerMessage();
+  
+  if (fCDH == NULL) return false;
+  UChar_t l1msg = fCDH->GetL1TriggerMessage();
   if ((l1msg & 0x1) == 0x0) return false;  // skip physics events.
   // From here on everything must be a software trigger.
   if (((l1msg >> 2) & 0xF) == 0xE)
@@ -1713,7 +1889,7 @@ bool AliHLTGlobalTriggerComponent::FillSoftwareTrigger()
     fSoftwareTrigger.Name("SOFTWARE");
     fSoftwareTrigger.Description("Generated internal software trigger.");
   }
-  UInt_t detectors = cdh->GetSubDetectors();
+  UInt_t detectors = fCDH->GetSubDetectors();
   fSoftwareTrigger.ReadoutList( AliHLTReadoutList(Int_t(detectors)) );
   return true;
 }
@@ -1722,7 +1898,7 @@ bool AliHLTGlobalTriggerComponent::FillSoftwareTrigger()
 int AliHLTGlobalTriggerComponent::PrintStatistics(const AliHLTGlobalTrigger* pTrigger, AliHLTComponentLogSeverity level, int offset) const
 {
   // print some statistics
-  int totalEvents=GetEventCount()+offset;
+  int totalEvents=fTotalEventCounter+offset;
   const TArrayL64& counters = pTrigger->GetCounters();
   if (pTrigger->CallFailed()) return -EPROTO;
   for (int i = 0; i < counters.GetSize(); i++) {