Belle II Software  release-08-01-10
SoftwareTriggerHLTDQMModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <hlt/softwaretrigger/modules/dqm/SoftwareTriggerHLTDQMModule.h>
9 
10 #include <TDirectory.h>
11 
12 #include <hlt/softwaretrigger/core/SoftwareTriggerDBHandler.h>
13 #include <hlt/softwaretrigger/core/FinalTriggerDecisionCalculator.h>
14 #include <hlt/utilities/Units.h>
15 
16 #include <framework/core/ModuleParam.templateDetails.h>
17 
18 #include <algorithm>
19 #include <fstream>
20 #include <stdexcept>
21 
22 using namespace Belle2;
23 using namespace SoftwareTrigger;
24 
25 REG_MODULE(SoftwareTriggerHLTDQM);
26 
28 {
29  setDescription("Monitor Physics Trigger");
31 
32  // Fill in the default values of the module parameters
34 
35  m_param_cutResultIdentifiers["filter"]["filter"] = {"total_result"};
36 
37  addParam("cutResultIdentifiers", m_param_cutResultIdentifiers,
38  "Which cuts should be reported? Please remember to include the total_result also, if wanted.",
40 
41  addParam("cutResultIdentifiersIgnored", m_param_cutResultIdentifiersIgnored,
42  "Which cuts should be ignored? This will display cleaner trigger lines, e.g. to clear them from bhabha contamination. "
43  "Vetoes on skims do not apply in filter plot and vice versa.",
45 
46  addParam("cutResultIdentifiersPerUnit", m_param_cutResultIdentifiersPerUnit,
47  "Which cuts should be reported per unit?",
49 
50  addParam("variableIdentifiers", m_param_variableIdentifiers,
51  "Which variables should be reported?",
53 
54  addParam("l1Identifiers", m_param_l1Identifiers,
55  "Which l1 identifiers to report?",
57 
58  addParam("additionalL1Identifiers", m_param_additionalL1Identifiers,
59  "Which additional l1 identifiers to be added to the l1 total result plot?",
61 
62  addParam("createTotalResultHistograms", m_param_create_total_result_histograms,
63  "Create total result histogram?",
64  true);
65 
66  addParam("createExpRunEventHistograms", m_param_create_exp_run_event_histograms,
67  "Create exp/run/event histograms?",
68  true);
69 
70  addParam("createHLTUnitHistograms", m_param_create_hlt_unit_histograms,
71  "Create HLT unit histograms?",
72  false);
73 
74  addParam("createErrorFlagHistograms", m_param_create_error_flag_histograms,
75  "Create Error Flag histograms?",
76  false);
77 
78  addParam("histogramDirectoryName", m_param_histogramDirectoryName,
79  "SoftwareTrigger DQM histograms will be put into this directory", m_param_histogramDirectoryName);
80 
81  addParam("pathLocation", m_param_pathLocation,
82  "Location of the module in the path: before filter or after filter", m_param_pathLocation);
83 }
84 
86 {
87  TDirectory* oldDirectory = nullptr;
88 
89  if (!m_param_histogramDirectoryName.empty()) {
90  oldDirectory = gDirectory;
91  TDirectory* histDir = oldDirectory->mkdir(m_param_histogramDirectoryName.c_str());
92  histDir->cd();
93  }
94 
95  for (auto const& variable : m_param_variableIdentifiers) {
96  // todo: make correct range
97  const unsigned int numberOfBins = 50;
98  const double lowerX = 0;
99  const double upperX = 50;
100  m_triggerVariablesHistograms.emplace(variable, new TH1F(variable.c_str(), variable.c_str(), numberOfBins, lowerX, upperX));
101  m_triggerVariablesHistograms[variable]->SetXTitle(("SoftwareTriggerVariable " + variable).c_str());
102  }
103 
104  for (const auto& cutIdentifier : m_param_cutResultIdentifiers) {
105 
106  const std::string& title = cutIdentifier.first;
107  const auto& mapVal = *(m_param_cutResultIdentifiers[title].begin());
108  const std::string& baseIdentifier = mapVal.first;
109  const auto& cuts = mapVal.second;
110  const int numberOfFlags = cuts.size();
111 
112  if (m_param_histogramDirectoryName == "softwaretrigger_skim_nobhabha") {
113  if (title == baseIdentifier)
114  m_cutResultHistograms.emplace(title,
115  new TH1F((title + "_nobhabha").c_str(), ("Events triggered in HLT " + baseIdentifier).c_str(),
116  numberOfFlags, 0,
117  numberOfFlags));
118  else
119  m_cutResultHistograms.emplace(title,
120  new TH1F((baseIdentifier + "_" + title + "_nobhabha").c_str(),
121  ("Events triggered in HLT " + baseIdentifier + " : " + title).c_str(),
122  numberOfFlags, 0,
123  numberOfFlags));
124  } else {
125  if (title == baseIdentifier)
126  m_cutResultHistograms.emplace(title,
127  new TH1F(title.c_str(), ("Events triggered in HLT " + baseIdentifier).c_str(),
128  numberOfFlags, 0,
129  numberOfFlags));
130  else
131  m_cutResultHistograms.emplace(title,
132  new TH1F((baseIdentifier + "_" + title).c_str(), ("Events triggered in HLT " + baseIdentifier + " : " + title).c_str(),
133  numberOfFlags, 0,
134  numberOfFlags));
135  }
136  m_cutResultHistograms[title]->SetXTitle("");
137  m_cutResultHistograms[title]->SetOption("hist");
138  m_cutResultHistograms[title]->SetStats(false);
139  m_cutResultHistograms[title]->SetMinimum(0);
140 
141  // Set bin labels
142  int index = 0;
143  for (const std::string& cutTitle : cuts) {
144  index++;
145  m_cutResultHistograms[title]->GetXaxis()->SetBinLabel(index, cutTitle.c_str());
146  }
147  }
148 
149  // We add one for the total result
151  m_cutResultHistograms.emplace("total_result",
152  new TH1F("total_result", "Total Result of HLT (absolute numbers)", 1, 0, 0));
153  m_cutResultHistograms["total_result"]->SetXTitle("Total Cut Result");
154  m_cutResultHistograms["total_result"]->SetOption("hist");
155  m_cutResultHistograms["total_result"]->SetStats(false);
156  m_cutResultHistograms["total_result"]->SetMinimum(0);
157  }
158 
159  for (const std::string& trigger : m_param_l1Identifiers) {
160  m_l1Histograms.emplace(trigger, new TH1F(trigger.c_str(), ("Events triggered in L1 " + trigger).c_str(), 1, 0, 0));
161  m_l1Histograms[trigger]->SetXTitle("");
162  m_l1Histograms[trigger]->SetOption("hist");
163  m_l1Histograms[trigger]->SetStats(false);
164  m_l1Histograms[trigger]->SetMinimum(0);
165  }
166 
167  // And also one for the total numbers
169  m_l1Histograms.emplace("l1_total_result",
170  new TH1F("l1_total_result", "Events triggered in L1 (total results)", 1, 0, 0));
171  m_l1Histograms["l1_total_result"]->SetXTitle("Total L1 Cut Result");
172  m_l1Histograms["l1_total_result"]->SetOption("hist");
173  m_l1Histograms["l1_total_result"]->SetStats(false);
174  m_l1Histograms["l1_total_result"]->SetMinimum(0);
175 
176  const int numberOfL1Flags = m_param_l1Identifiers.size() + m_param_additionalL1Identifiers.size();
177  m_l1Histograms["l1_total_result"]->SetBins(numberOfL1Flags, 0, numberOfL1Flags);
178  // Set bin labels
179  int l1Index = 0;
180  for (const std::string& l1Trigger : m_param_l1Identifiers) {
181  l1Index++;
182  m_l1Histograms["l1_total_result"]->GetXaxis()->SetBinLabel(l1Index, l1Trigger.c_str());
183  }
184  for (const std::string& l1Trigger : m_param_additionalL1Identifiers) {
185  l1Index++;
186  m_l1Histograms["l1_total_result"]->GetXaxis()->SetBinLabel(l1Index, l1Trigger.c_str());
187  }
188  }
189 
191  m_runInfoHistograms.emplace("run_number", new TH1D("run_number", "Run Number", 100, 0, 10000));
192  m_runInfoHistograms.emplace("event_number", new TH1D("event_number", "Event Number", 100, 0, 1'000'000));
193  m_runInfoHistograms.emplace("experiment_number", new TH1D("experiment_number", "Experiment Number", 50, 0, 50));
194  }
195 
197  if (m_param_histogramDirectoryName != "softwaretrigger_before_filter") {
198  m_runInfoHistograms.emplace("hlt_unit_number", new TH1D("hlt_unit_number_after_filter",
199  ("Number of events per HLT unit " + m_param_pathLocation).c_str(), HLTUnits::max_hlt_units + 1, 0,
200  HLTUnits::max_hlt_units + 1));
201  } else {
202  m_runInfoHistograms.emplace("hlt_unit_number", new TH1D("hlt_unit_number",
203  ("Number of events per HLT unit " + m_param_pathLocation).c_str(), HLTUnits::max_hlt_units + 1, 0,
204  HLTUnits::max_hlt_units + 1));
205  }
206  m_runInfoHistograms["hlt_unit_number"]->SetMinimum(0);
207 
208  for (const auto& cutIdentifierPerUnit : m_param_cutResultIdentifiersPerUnit) {
209  m_cutResultPerUnitHistograms.emplace(cutIdentifierPerUnit, new TH1F((cutIdentifierPerUnit + "_per_unit").c_str(),
210  ("Events triggered per unit in HLT : " + cutIdentifierPerUnit).c_str(), HLTUnits::max_hlt_units + 1, 0,
211  HLTUnits::max_hlt_units + 1));
212  m_cutResultPerUnitHistograms[cutIdentifierPerUnit]->SetXTitle("HLT unit number");
213  m_cutResultPerUnitHistograms[cutIdentifierPerUnit]->SetOption("histe");
214  m_cutResultPerUnitHistograms[cutIdentifierPerUnit]->SetMinimum(0);
215  }
216 
217  }
218 
220  m_runInfoHistograms.emplace("error_flag", new TH1D("error_flag", "Error Flag", 4, 0, 4));
221  m_runInfoHistograms["error_flag"]->SetStats(false);
222  m_runInfoHistograms["error_flag"]->SetOption("hist");
223  m_runInfoHistograms["error_flag"]->SetMinimum(0);
224  }
225 
226  if (oldDirectory) {
227  oldDirectory->cd();
228  }
229 }
230 
232 {
233  // Register histograms (calls back defineHisto)
234  REG_HISTOGRAM
235 
237  std::ifstream file;
238  file.open(HLTUnits::hlt_unit_file);
239  if (file.good()) {
240  std::string host;
241  getline(file, host);
242  m_hlt_unit = atoi(host.substr(3, 2).c_str());
243  file.close();
244  } else {
245  B2WARNING("HLT unit number not found");
246  }
247  }
248 }
249 
251 {
252  // this might be pre-scaled for performance reasons in the final configuration, therefore this structure
253  // might not be filled in every event
254  if (m_variables.isValid()) {
255  for (auto& variableNameAndTH1F : m_triggerVariablesHistograms) {
256  const std::string& variable = variableNameAndTH1F.first;
257  TH1F* histogram = variableNameAndTH1F.second;
258 
259  // try to load this variable from the computed trigger variables
260  if (not m_variables->has(variable)) {
261  B2ERROR("Variable " << variable << " configured for SoftwareTriggerDQM plotting is not available");
262  } else {
263  const double value = m_variables->getVariable(variable);
264  histogram->Fill(value);
265  }
266  }
267  }
268 
269  if (m_triggerResult.isValid()) {
270  const auto results = m_triggerResult->getResults();
271 
272  for (auto const& cutIdentifier : m_param_cutResultIdentifiers) {
273  const std::string& title = cutIdentifier.first;
274  const auto& mapVal = *(m_param_cutResultIdentifiers[title].begin());
275  const std::string& baseIdentifier = mapVal.first;
276  const auto& cuts = mapVal.second;
277 
278  // check if we want to ignore it
279  bool skip = false;
280  const auto& cutsIgnored = m_param_cutResultIdentifiersIgnored[baseIdentifier];
281 
282  for (const std::string& cutTitleIgnored : cutsIgnored) {
283  const std::string& cutNameIgnored = cutTitleIgnored.substr(0, cutTitleIgnored.find("\\"));
284  const std::string& fullCutIdentifierIgnored = SoftwareTriggerDBHandler::makeFullCutName(baseIdentifier, cutNameIgnored);
285 
286  auto const cutEntryIgnored = results.find(fullCutIdentifierIgnored);
287 
288  if (cutEntryIgnored != results.end()) {
289  if (cutEntryIgnored->second > 0) skip = true;
290  }
291  }
292 
293  float index = 0;
294  for (const std::string& cutTitle : cuts) {
295  index++;
296  const std::string& cutName = cutTitle.substr(0, cutTitle.find("\\"));
297  const std::string& fullCutIdentifier = SoftwareTriggerDBHandler::makeFullCutName(baseIdentifier, cutName);
298 
299  // check if the cutResult is in the list, be graceful when not available
300  // Create results object instead of calling .find() on a temporary object. This will cause undefined behaviour
301  // when checking again the .end() pointer, when the .end() pointer is also created from a temporary object.
302  auto const cutEntry = results.find(fullCutIdentifier);
303 
304  if (cutEntry != results.end()) {
305  const int cutResult = cutEntry->second;
306  if (cutResult > 0 and not skip) {
307  m_cutResultHistograms[title]->Fill(index - 0.5);
308  }
309  }
310  }
311 
313  if (title == baseIdentifier) {
314  const std::string& totalCutIdentifier = SoftwareTriggerDBHandler::makeTotalResultName(baseIdentifier);
315  const int cutResult = static_cast<int>(m_triggerResult->getResult(totalCutIdentifier));
316 
317  m_cutResultHistograms["total_result"]->Fill(totalCutIdentifier.c_str(), cutResult > 0);
318  }
319  }
320  }
321 
324  m_cutResultHistograms["total_result"]->Fill("total_result", totalResult > 0);
325  }
326 
328  for (const std::string& cutIdentifierPerUnit : m_param_cutResultIdentifiersPerUnit) {
329  const std::string& cutName = cutIdentifierPerUnit.substr(0, cutIdentifierPerUnit.find("\\"));
330  const std::string& fullCutIdentifier = SoftwareTriggerDBHandler::makeFullCutName("filter", cutName);
331 
332  // check if the cutResult is in the list, be graceful when not available
333  auto const cutEntry = results.find(fullCutIdentifier);
334 
335  if (cutEntry != results.end()) {
336  const int cutResult = cutEntry->second;
337  if (cutResult > 0) {
338  m_cutResultPerUnitHistograms[cutIdentifierPerUnit]->Fill(m_hlt_unit);
339  }
340  }
341  }
342  }
343 
344  if (m_l1TriggerResult.isValid() and m_l1NameLookup.isValid()) {
345  float l1Index = 0;
346  for (const std::string& l1Trigger : m_param_l1Identifiers) {
347  l1Index++;
348  const int triggerBit = m_l1NameLookup->getoutbitnum(l1Trigger.c_str());
349  if (triggerBit < 0) {
350  B2WARNING("Could not find"
351  << LogVar("L1 trigger line", l1Trigger));
352  continue;
353  }
354  bool triggerResult;
355  try {
356  triggerResult = m_l1TriggerResult->testPsnm(triggerBit);
357  } catch (const std::exception&) {
358  triggerResult = false;
359  }
361  if (triggerResult) {
362  m_l1Histograms["l1_total_result"]->Fill(l1Index - 0.5);
363  }
364  }
365 
366  if (not triggerResult) {
367  continue;
368  }
369 
370  for (auto const& cutIdentifier : m_param_cutResultIdentifiers) {
371  const std::string& title = cutIdentifier.first;
372  const auto& mapVal = *(m_param_cutResultIdentifiers[title].begin());
373  const std::string& baseIdentifier = mapVal.first;
374  const auto& cuts = mapVal.second;
375 
376  if (title == baseIdentifier) {
377  for (const std::string& cutTitle : cuts) {
378  const std::string& cutName = cutTitle.substr(0, cutTitle.find("\\"));
379  const std::string& fullCutIdentifier = SoftwareTriggerDBHandler::makeFullCutName(baseIdentifier, cutName);
380 
381  // check if the cutResult is in the list, be graceful when not available
382  auto const cutEntry = results.find(fullCutIdentifier);
383 
384  if (cutEntry != results.end()) {
385  const int cutResult = cutEntry->second;
386  m_l1Histograms[l1Trigger]->Fill(cutTitle.c_str(), cutResult > 0);
387  }
388  }
389  }
390  }
391 
393  m_l1Histograms[l1Trigger]->Fill("hlt_result", totalResult > 0);
394  m_l1Histograms[l1Trigger]->LabelsDeflate("X");
395  }
397  for (const std::string& l1Trigger : m_param_additionalL1Identifiers) {
398  l1Index++;
399  const int triggerBit = m_l1NameLookup->getoutbitnum(l1Trigger.c_str());
400  if (triggerBit < 0) {
401  B2WARNING("Could not find"
402  << LogVar("L1 trigger line", l1Trigger));
403  continue;
404  }
405  bool triggerResult;
406  try {
407  triggerResult = m_l1TriggerResult->testPsnm(triggerBit);
408  } catch (const std::exception&) {
409  triggerResult = false;
410  }
411  if (triggerResult) {
412  m_l1Histograms["l1_total_result"]->Fill(l1Index - 0.5);
413  }
414  }
415  }
416  }
417  }
418 
420  m_runInfoHistograms["run_number"]->Fill(m_eventMetaData->getRun());
421  m_runInfoHistograms["event_number"]->Fill(m_eventMetaData->getEvent());
422  m_runInfoHistograms["experiment_number"]->Fill(m_eventMetaData->getExperiment());
423  }
424 
426  m_runInfoHistograms["error_flag"]->Fill("B2LinkPacketCRCError",
427  (bool)(m_eventMetaData->getErrorFlag() & EventMetaData::EventErrorFlag::c_B2LinkPacketCRCError));
428  m_runInfoHistograms["error_flag"]->Fill("B2LinkEventCRCError",
429  (bool)(m_eventMetaData->getErrorFlag() & EventMetaData::EventErrorFlag::c_B2LinkEventCRCError));
430  m_runInfoHistograms["error_flag"]->Fill("HLTCrash",
431  (bool)(m_eventMetaData->getErrorFlag() & EventMetaData::EventErrorFlag::c_HLTCrash));
432  m_runInfoHistograms["error_flag"]->Fill("ReconstructionAbort",
433  (bool)(m_eventMetaData->getErrorFlag() & EventMetaData::EventErrorFlag::c_ReconstructionAbort));
434  }
435 
437  m_runInfoHistograms["hlt_unit_number"]->Fill(m_hlt_unit);
438  }
439 }
440 
442 {
443  std::for_each(m_cutResultHistograms.begin(), m_cutResultHistograms.end(),
444  [](auto & it) { it.second->Reset(); });
445  std::for_each(m_cutResultPerUnitHistograms.begin(), m_cutResultPerUnitHistograms.end(),
446  [](auto & it) { it.second->Reset(); });
447  std::for_each(m_triggerVariablesHistograms.begin(), m_triggerVariablesHistograms.end(),
448  [](auto & it) { it.second->Reset(); });
449  std::for_each(m_l1Histograms.begin(), m_l1Histograms.end(),
450  [](auto & it) { it.second->Reset(); });
451  std::for_each(m_runInfoHistograms.begin(), m_runInfoHistograms.end(),
452  [](auto & it) { it.second->Reset(); });
453 }
454 
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
static bool getFinalTriggerDecision(const SoftwareTriggerResult &result, bool forgetTotalResult=false)
Calculate the final cut decision using all "total_results" of all sub triggers in the software trigge...
static std::string makeFullCutName(const std::string &baseCutIdentifier, const std::string &cutIdentifier)
Helper function to compile the full identifier from the base and the specific cut name.
static std::string makeTotalResultName(const std::string &baseIdentifier="all")
Handy function to create the name related to the total result of a specific trigger stage (either fil...
std::map< std::string, TH1D * > m_runInfoHistograms
histograms with the run information
std::map< std::string, std::map< std::string, std::vector< std::string > > > m_param_cutResultIdentifiers
Which cuts should be reported? Please remember to include the total_result also, if wanted.
std::map< std::string, TH1F * > m_l1Histograms
histogram with the L1 information
void initialize() override
Module functions to be called from main process.
void event() override
Module functions to be called from event process.
StoreObjPtr< SoftwareTriggerResult > m_triggerResult
STM cut results.
std::vector< std::string > m_param_l1Identifiers
Which L1 cuts should be reported?
std::map< std::string, TH1F * > m_cutResultPerUnitHistograms
histograms for the final sw trigger decisions for each base identifier per unit
std::map< std::string, TH1F * > m_triggerVariablesHistograms
histograms for the software trigger variables in all calculators (although maybe not filled)
std::map< std::string, std::vector< std::string > > m_param_cutResultIdentifiersIgnored
Which cuts should be ignored? This can be used to clear trigger lines from e.g. bhabha contamination.
std::string m_param_pathLocation
Location of the module in the path: before filter or after filter.
void beginRun() override
Reset all histogram entries for a new run.
std::map< std::string, TH1F * > m_cutResultHistograms
histograms for the final sw trigger decisions for each base identifier
bool m_param_create_error_flag_histograms
Create error flag histograms?
std::vector< std::string > m_param_additionalL1Identifiers
Which additional L1 cuts should be added to the L1 total result plot?
bool m_param_create_hlt_unit_histograms
Create HLT unit number histograms?
StoreObjPtr< TRGSummary > m_l1TriggerResult
L1 cut results.
std::vector< std::string > m_param_cutResultIdentifiersPerUnit
Which cuts should be reported per unit?
bool m_param_create_total_result_histograms
Create total result histogram?
std::vector< std::string > m_param_variableIdentifiers
Which variables should be reported?
bool m_param_create_exp_run_event_histograms
Create exp/run/event number histograms?
std::string m_param_histogramDirectoryName
Directory to put the generated histograms.
StoreObjPtr< SoftwareTriggerVariables > m_variables
STM cut variables.
Class to store variables with their name which were sent to the logging service.
REG_MODULE(arichBtest)
Register the Module.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
Abstract base class for different kinds of events.