Belle II Software  release-06-01-15
SoftwareTriggerResultPrinterModule.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 
9 #include <hlt/softwaretrigger/modules/basics/SoftwareTriggerResultPrinterModule.h>
10 #include <hlt/softwaretrigger/core/FinalTriggerDecisionCalculator.h>
11 #include <mdst/dbobjects/DBRepresentationOfSoftwareTriggerCut.h>
12 #include <framework/database/DBObjPtr.h>
13 
14 #include <TFile.h>
15 #include <TTree.h>
16 
17 #include <boost/algorithm/string/replace.hpp>
18 #include <memory>
19 
20 
21 using namespace Belle2;
22 using namespace SoftwareTrigger;
23 
24 REG_MODULE(SoftwareTriggerResultPrinter)
25 
26 
28  : Module()
29 {
30  setDescription("Write out the software trigger results in an easily accessible summary table to disk.");
31 
32  addParam("outputFileName", m_param_outputFileName, "File path and name of the ROOT "
33  "file, in which the results of the calculation are stored. Please note that already present files will be overridden. ",
34  m_param_outputFileName);
35 }
36 
38 {
39  m_resultStoreObjectPointer.isRequired();
40  m_l1Result.isRequired();
41 }
42 
44 {
45 
46  auto debugOutputFile = std::unique_ptr<TFile>(TFile::Open(m_param_outputFileName.c_str(), "RECREATE"));
47  if (not debugOutputFile) {
48  B2FATAL("Could not open debug output file. Aborting.");
49  }
50  auto debugTTree = std::make_unique<TTree>("software_trigger_results", "software_trigger_results");
51  if (not debugTTree) {
52  B2FATAL("Could not create debug output tree. Aborting.");
53  }
54 
55  bool prescaled;
56  bool accepted;
57  bool cut;
58 
59  debugTTree->Branch("cut", &cut);
60  debugTTree->Branch("prescaled", &prescaled);
61  debugTTree->Branch("accept_or_reject", &accepted);
62  debugTTree->Branch("total_events", &m_numberOfEvents);
63  std::vector<double> value(m_passedEventsPerTrigger.size());
64 
65  cut = true;
66  prescaled = true;
67  accepted = true;
68  unsigned int counter = 0;
69  for (auto& cutResult : m_passedEventsPerTrigger) {
70  std::string cutName = cutResult.first;
71  boost::replace_all(cutName, "&", "_");
72  debugTTree->Branch(cutName.c_str(), &value.at(counter));
73 
74  value[counter] = static_cast<double>(cutResult.second[SoftwareTriggerCutResult::c_accept]);
75  counter++;
76  }
77  debugTTree->Fill();
78 
79  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
80  cut = true;
81  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
82  prescaled = true;
83  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
84  accepted = false;
85  counter = 0;
86  for (auto& cutResult : m_passedEventsPerTrigger) {
87  value[counter] = static_cast<double>(cutResult.second[SoftwareTriggerCutResult::c_reject]);
88  counter++;
89  }
90  debugTTree->Fill();
91 
92  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
93  cut = true;
94  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
95  prescaled = false;
96  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
97  accepted = true;
98  counter = 0;
99  for (auto& cutResult : m_passedEventsPerTrigger) {
100  const auto& cutName = cutResult.first;
102  value[counter] = NAN;
103  } else {
104  value[counter] = static_cast<double>(m_passedEventsPerTriggerNonPrescaled[cutName][SoftwareTriggerCutResult::c_accept]);
105  }
106  counter++;
107  }
108  debugTTree->Fill();
109 
110  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
111  cut = true;
112  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
113  prescaled = false;
114  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
115  accepted = false;
116  counter = 0;
117  for (auto& cutResult : m_passedEventsPerTrigger) {
118  const auto& cutName = cutResult.first;
120  value[counter] = NAN;
121  } else {
122  value[counter] = static_cast<double>(m_passedEventsPerTriggerNonPrescaled[cutName][SoftwareTriggerCutResult::c_reject]);
123  }
124  counter++;
125  }
126  debugTTree->Fill();
127 
128  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
129  cut = false;
130  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
131  prescaled = false;
132  // cppcheck-suppress redundantAssignment; the variable is used in the Fill() method below
133  accepted = false;
134  counter = 0;
135  for (auto& cutResult : m_passedEventsPerTrigger) {
136  const auto& cutName = cutResult.first;
137  if (m_prescales.find(cutName) == m_prescales.end()) {
138  value[counter] = NAN;
139  } else {
140  value[counter] = static_cast<double>(m_prescales[cutName]);
141  }
142  counter++;
143  }
144  debugTTree->Fill();
145 
146  debugOutputFile->cd();
147  debugOutputFile->Write();
148  debugTTree.reset();
149  debugOutputFile.reset();
150 }
151 
153 {
155 
156  for (const auto& [cutName, cutResults] : m_resultStoreObjectPointer->getResultPairs()) {
157  m_passedEventsPerTrigger[cutName][static_cast<SoftwareTriggerCutResult >(cutResults.first)]++;
158 
159  // This does only make sense for non-total results (as they are prescaled)
160  if (cutName.find("total_result") == std::string::npos) {
161  m_passedEventsPerTriggerNonPrescaled[cutName][static_cast<SoftwareTriggerCutResult >(cutResults.second)]++;
162 
163  DBObjPtr<DBRepresentationOfSoftwareTriggerCut> downloadedCut(cutName);
164  if (downloadedCut) {
165  m_prescales[cutName] = downloadedCut->getPreScaleFactor();
166  }
167  }
168  }
169 
171  if (eventAccepted) {
173  } else {
175  }
176 
177  if (m_l1Result.isValid()) {
178  const bool l1Accepted = m_l1Result->test();
179  if (l1Accepted) {
181  } else {
183  }
184  } else {
185  if (m_eventMetaDataPtr)
186  B2WARNING("Uncaught exception encountered: Trying to access StoreObjPtr object 'TRGSummary' (durability: event), which was not created in exp/run/evt: "
187  << LogVar("exp", m_eventMetaDataPtr->getExperiment())
188  << LogVar("run", m_eventMetaDataPtr->getRun())
189  << LogVar("event", m_eventMetaDataPtr->getEvent()));
190  }
191 }
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Base class for Modules.
Definition: Module.h:72
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...
Write out the software trigger results in an easily accessible summary table.
StoreObjPtr< TRGSummary > m_l1Result
Store Object for reading the l1 result.
std::map< std::string, std::map< SoftwareTriggerCutResult, unsigned int > > m_passedEventsPerTriggerNonPrescaled
Internal map of summed results.
void event() override
Write out the cuts if wanted and sum them up.
std::string m_param_outputFileName
Output file name for the debug output.
void terminate() override
Store and delete the ttree if it was created. Print out the summed results.
std::map< std::string, std::map< SoftwareTriggerCutResult, unsigned int > > m_passedEventsPerTrigger
Internal map of summed results.
std::map< std::string, unsigned int > m_prescales
Internal map of prescales.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
EventMetaData is used by processEvent()/processCore().
StoreObjPtr< SoftwareTriggerResult > m_resultStoreObjectPointer
Store Object for reading the trigger decision.
unsigned int m_numberOfEvents
Internal counter for the number of seen events.
Class to store variables with their name which were sent to the logging service.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
SoftwareTriggerCutResult
Enumeration with all possible results of the SoftwareTriggerCut.
@ c_accept
Accept this event.
@ c_reject
Reject this event.
Abstract base class for different kinds of events.