Belle II Software prerelease-11-00-00a
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
21using namespace Belle2;
22using namespace SoftwareTrigger;
23
24REG_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. ",
35}
36
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 // cppcheck-suppress knownConditionTrueFalse
52 if (not debugTTree) {
53 B2FATAL("Could not create debug output tree. Aborting.");
54 }
55
56 bool prescaled;
57 bool accepted;
58 bool cut;
59
60 debugTTree->Branch("cut", &cut);
61 debugTTree->Branch("prescaled", &prescaled);
62 debugTTree->Branch("accept_or_reject", &accepted);
63 debugTTree->Branch("total_events", &m_numberOfEvents);
64 std::vector<double> value(m_passedEventsPerTrigger.size());
65
66 cut = true;
67 prescaled = true;
68 accepted = true;
69 unsigned int counter = 0;
70 for (const auto& cutResult : m_passedEventsPerTrigger) {
71 std::string cutName = cutResult.first;
72 boost::replace_all(cutName, "&", "_");
73 debugTTree->Branch(cutName.c_str(), &value.at(counter));
74
75 auto it = cutResult.second.find(SoftwareTriggerCutResult::c_accept);
76 value[counter] = (it != cutResult.second.end()) ? static_cast<double>(it->second) : 0;
77 counter++;
78 }
79 debugTTree->Fill();
80
81 cut = true;
82 prescaled = true;
83 accepted = false;
84 counter = 0;
85 for (const auto& cutResult : m_passedEventsPerTrigger) {
86 auto itReject = cutResult.second.find(SoftwareTriggerCutResult::c_reject);
87 // cppcheck-suppress unreadVariable
88 value[counter] = (itReject != cutResult.second.end()) ? static_cast<double>(itReject->second) : 0;
89 counter++;
90 }
91 debugTTree->Fill();
92
93 cut = true;
94 prescaled = false;
95 accepted = true;
96 counter = 0;
97 for (const auto& cutResult : m_passedEventsPerTrigger) {
98 const auto& cutName = cutResult.first;
100 // cppcheck-suppress unreadVariable
101 value[counter] = NAN;
102 } else {
103 // cppcheck-suppress unreadVariable
104 value[counter] = static_cast<double>(m_passedEventsPerTriggerNonPrescaled[cutName][SoftwareTriggerCutResult::c_accept]);
105 }
106 counter++;
107 }
108 debugTTree->Fill();
109
110 cut = true;
111 prescaled = false;
112 accepted = false;
113 counter = 0;
114 for (const auto& cutResult : m_passedEventsPerTrigger) {
115 const auto& cutName = cutResult.first;
117 // cppcheck-suppress unreadVariable
118 value[counter] = NAN;
119 } else {
120 // cppcheck-suppress unreadVariable
121 value[counter] = static_cast<double>(m_passedEventsPerTriggerNonPrescaled[cutName][SoftwareTriggerCutResult::c_reject]);
122 }
123 counter++;
124 }
125 debugTTree->Fill();
126
127 cut = false;
128 prescaled = false;
129 accepted = false;
130 counter = 0;
131 for (const auto& cutResult : m_passedEventsPerTrigger) {
132 const auto& cutName = cutResult.first;
133 if (m_prescales.find(cutName) == m_prescales.end()) {
134 // cppcheck-suppress unreadVariable
135 value[counter] = NAN;
136 } else {
137 // cppcheck-suppress unreadVariable
138 value[counter] = static_cast<double>(m_prescales[cutName]);
139 }
140 counter++;
141 }
142 debugTTree->Fill();
143
144 debugOutputFile->cd();
145 debugOutputFile->Write();
146 debugTTree.reset();
147 debugOutputFile.reset();
148}
149
151{
153
154 for (const auto& [cutName, cutResults] : m_resultStoreObjectPointer->getResultPairs()) {
155 m_passedEventsPerTrigger[cutName][static_cast<SoftwareTriggerCutResult >(cutResults.first)]++;
156
157 // This does only make sense for non-total results (as they are prescaled)
158 if (cutName.find("total_result") == std::string::npos) {
159 m_passedEventsPerTriggerNonPrescaled[cutName][static_cast<SoftwareTriggerCutResult >(cutResults.second)]++;
160
162 if (downloadedCut) {
163 m_prescales[cutName] = downloadedCut->getPreScaleFactor();
164 }
165 }
166 }
167
169 if (eventAccepted) {
171 } else {
173 }
174
175 if (m_l1Result.isValid()) {
176 const bool l1Accepted = m_l1Result->test();
177 if (l1Accepted) {
179 } else {
181 }
182 } else {
184 B2WARNING("Uncaught exception encountered: Trying to access StoreObjPtr object 'TRGSummary' (durability: event), which was not created in exp/run/evt: "
185 << LogVar("exp", m_eventMetaDataPtr->getExperiment())
186 << LogVar("run", m_eventMetaDataPtr->getRun())
187 << LogVar("event", m_eventMetaDataPtr->getEvent()));
188 }
189}
Class for accessing objects in the database.
Definition DBObjPtr.h:21
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
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...
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.
SoftwareTriggerResultPrinterModule()
Create a new module instance and set the parameters.
Class to store variables with their name which were sent to the logging service.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
SoftwareTriggerCutResult
Enumeration with all possible results of the SoftwareTriggerCut.
Abstract base class for different kinds of events.