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