Belle II Software  release-05-02-19
SoftwareTriggerModule.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/SoftwareTriggerModule.h>
12 #include <hlt/softwaretrigger/core/utilities.h>
13 #include <hlt/softwaretrigger/calculations/FilterCalculator.h>
14 #include <hlt/softwaretrigger/calculations/SkimSampleCalculator.h>
15 #include <hlt/softwaretrigger/core/FinalTriggerDecisionCalculator.h>
16 #include <TFile.h>
17 
18 using namespace Belle2;
19 using namespace SoftwareTrigger;
20 
21 REG_MODULE(SoftwareTrigger)
22 
23 
24 SoftwareTriggerModule::SoftwareTriggerModule() : Module(), m_resultStoreObjectPointer("", DataStore::c_Event)
26 {
27  setDescription("Module to perform cuts on various variables in the event. The cuts can be defined\n"
28  "by elements loaded from the database. Each cut is executed and its result stored.\n"
29  "The return value of this module is a bool, which is either true (accept the event) or false (reject it).\n"
30  "It is defined from the results of the cuts in the given trigger menu, which are all evaluated\n"
31  "and the trigger mode (accept mode or not).\n"
32  "if not in accept mode (= reject mode):\n"
33  "* 1: if one of the accept cuts has a true result and none of the reject cuts is false ( = accepted)\n"
34  "* 0: if neither one of the accept cuts is true nor one of the reject cuts false ( = don't know) or\n"
35  "* if one of the reject cuts is false ( = rejected)\n"
36  "In short: event accepted <=> (#true accept cuts > 0) && (#false reject cuts == 0)\n"
37  "Please note that the reject cuts override the accept cuts decision in this case!\n"
38  "if in accept mode\n"
39  "* 1: if one of the accept cuts has a true result. ( = accepted) or\n"
40  "* if neither one of the accept cuts is true nor one of the reject cuts false ( = don't know)\n"
41  "* 0: if one of the reject cuts is false and none of the accept cuts is true ( = rejected)\n"
42  "Please note that the accept cuts override the reject cuts decision in this case!\n"
43  "In short: event accepted <=> (#true accept cuts > 0) || (#false reject cuts == 0)");
44 
46 
47  addParam("baseIdentifier", m_param_baseIdentifier, "Base identifier for all cuts downloaded from database. "
48  "The full db name of the cuts will be <base_identifier>/<cut_identifier>. You can only choose one identifier "
49  "to make clear that all chosen cuts belong together (and should be calculated together).",
50  m_param_baseIdentifier);
51 
52  addParam("resultStoreArrayName", m_param_resultStoreArrayName, "Store Object Pointer name for storing the "
53  "trigger decision.", m_param_resultStoreArrayName);
54 
55  addParam("storeDebugOutputToROOTFile", m_param_storeDebugOutputToROOTFile, "Flag to save the results of the calculations leading "
56  "to the trigger decisions into a ROOT file. The file path and name of this file can be handled by the "
57  "debugOutputFileName parameter. Not supported during parallel processing.", m_param_storeDebugOutputToROOTFile);
58 
59  addParam("preScaleStoreDebugOutputToDataStore", m_param_preScaleStoreDebugOutputToDataStore,
60  "Prescale with which to save the results of the calculations leading "
61  "to the trigger decisions into the DataStore. Leave to zero, to not store them at all.",
62  m_param_preScaleStoreDebugOutputToDataStore);
63 
64  addParam("debugOutputFileName", m_param_debugOutputFileName, "File path and name of the ROOT "
65  "file, in which the results of the calculation are stored, if storeDebugOutput is "
66  "turned on. Please note that already present files will be overridden. "
67  "ATTENTION: This file debugging mode does not work in parallel processing.", m_param_debugOutputFileName);
68  addParam("useRandomNumbersForPreScale", m_param_useRandomNumbersForPreScale, "Flag to use random numbers (True) "
69  "or a counter (False) for applying the prescale.", m_param_useRandomNumbersForPreScale);
70 }
71 
73 {
76 
79 }
80 
82 {
83  m_dbHandler->checkForChangedDBEntries();
84  // Initialize always the internal counters at the beginning of each run.
87  }
88 }
89 
91 {
92  if (m_debugTTree) {
93  m_debugOutputFile->cd();
94  m_debugOutputFile->Write();
95  m_debugTTree.reset();
96  m_debugOutputFile.reset();
97  }
98 }
99 
102 {
103  if (not m_resultStoreObjectPointer.isValid()) {
104  m_resultStoreObjectPointer.construct();
105  }
106 
108  m_debugOutputStoreObject.construct();
109  }
110 
111  B2DEBUG(20, "Doing the calculation...");
112  const SoftwareTriggerObject& prefilledObject = m_calculation->fillInCalculations();
113  B2DEBUG(20, "Successfully finished the calculation.");
114 
115  makeCut(prefilledObject);
116  makeDebugOutput();
117 }
118 
120 {
121  if (m_param_baseIdentifier == "filter") {
122  m_calculation.reset(new FilterCalculator());
123  } else if (m_param_baseIdentifier == "skim") {
124  m_calculation.reset(new SkimSampleCalculator());
125  } else {
126  B2FATAL("You gave an invalid base identifier " << m_param_baseIdentifier << ".");
127  }
128 
129  m_calculation->requireStoreArrays();
130 }
131 
133 {
135  m_debugOutputFile.reset(TFile::Open(m_param_debugOutputFileName.c_str(), "RECREATE"));
136  if (not m_debugOutputFile) {
137  B2FATAL("Could not open debug output file. Aborting.");
138  }
139  m_debugTTree.reset(new TTree("software_trigger_results", "software_trigger_results"));
140  if (not m_debugTTree) {
141  B2FATAL("Could not create debug output tree. Aborting.");
142  }
143  }
144 
147  }
148 }
149 
151 {
152  // Clear the vector of counters...
153  m_counters.clear();
154  // ... and then initialize it with "numberOfCuts" 0s.
155  size_t numberOfCuts = (m_dbHandler->getCutsWithNames()).size();
156  m_counters.assign(numberOfCuts, 0);
157 }
158 
159 void SoftwareTriggerModule::makeCut(const SoftwareTriggerObject& prefilledObject)
160 {
161  // Define the pointer to the counter and the index of each counter in m_counters.
162  uint32_t* counter{nullptr};
163  size_t counterIndex{0};
164  // Check all cuts with the prefilled object and write them back into the data store.
165  for (const auto& cutWithName : m_dbHandler->getCutsWithNames()) {
166  const std::string& cutIdentifier = cutWithName.first;
167  const auto& cut = cutWithName.second;
168  B2DEBUG(20, "Next processing cut " << cutIdentifier << " (" << cut->decompile() << ")");
170  counter = &m_counters.at(counterIndex++);
171  }
172  const auto& [prescaledCutResult, nonPrescaledCutResult] = cut->check(prefilledObject, counter);
173  m_resultStoreObjectPointer->addResult(cutIdentifier, prescaledCutResult, nonPrescaledCutResult);
174  }
175 
176  // Also add the module result ( = the result of all cuts with this basename) for later reference.
177  const SoftwareTriggerCutResult& moduleResult =
179  m_dbHandler->getAcceptOverridesReject());
180  const std::string& moduleResultIdentifier = SoftwareTriggerDBHandler::makeTotalResultName(m_param_baseIdentifier);
181  m_resultStoreObjectPointer->addResult(moduleResultIdentifier, moduleResult);
182 
183  // Return the trigger decision up to here
185  const std::string& totalResultIdentifier = SoftwareTriggerDBHandler::makeTotalResultName();
186  if (totalResult) {
187  m_resultStoreObjectPointer->addResult(totalResultIdentifier, SoftwareTriggerCutResult::c_accept);
188  } else {
189  m_resultStoreObjectPointer->addResult(totalResultIdentifier, SoftwareTriggerCutResult::c_reject);
190  }
191  setReturnValue(totalResult);
192 }
193 
195 {
197  B2DEBUG(20, "Storing debug output to file as requested.");
198  m_calculation->writeDebugOutput(m_debugTTree);
199  B2DEBUG(20, "Finished storing the debug output to file.");
200  }
201 
203  B2DEBUG(20, "Storing debug output to DataStore as requested.");
205  }
206 }
Belle2::SoftwareTrigger::SoftwareTriggerModule::beginRun
void beginRun() override
Check if the cut representations in the database have changed and download newer ones if needed.
Definition: SoftwareTriggerModule.cc:81
Belle2::SoftwareTrigger::SoftwareTriggerModule::initialize
void initialize() override
Initialize/Require the DB object pointers and any needed store arrays.
Definition: SoftwareTriggerModule.cc:72
Belle2::SoftwareTrigger::SoftwareTriggerModule::terminate
void terminate() override
Store and delete the ttree if it was created.
Definition: SoftwareTriggerModule.cc:90
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_debugTTree
std::unique_ptr< TTree > m_debugTTree
TTree to store the debug output (or a nullptr if we do not save the debug output).
Definition: SoftwareTriggerModule.h:116
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module::c_ParallelProcessingCertified
@ 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:82
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_resultStoreObjectPointer
StoreObjPtr< SoftwareTriggerResult > m_resultStoreObjectPointer
Store Object for storing the trigger decision.
Definition: SoftwareTriggerModule.h:108
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::SoftwareTrigger::SoftwareTriggerDBHandler
Helper class for performing up- and downloads of SoftwareTriggerCuts from the database.
Definition: SoftwareTriggerDBHandler.h:48
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_preScaleStoreDebugOutputToDataStore
unsigned int m_param_preScaleStoreDebugOutputToDataStore
Prescale with which to save the results of the calculations into the DataStore.
Definition: SoftwareTriggerModule.h:98
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::SoftwareTrigger::FilterCalculator
Implementation of a calculator used in the SoftwareTriggerModule to fill a SoftwareTriggerObject for ...
Definition: FilterCalculator.h:40
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_resultStoreArrayName
std::string m_param_resultStoreArrayName
Store Object Pointer name for storing the trigger decision.
Definition: SoftwareTriggerModule.h:94
Belle2::SoftwareTrigger::SoftwareTriggerModule::event
void event() override
Run over all cuts and check them. If one of the cuts yields true, give a positive return value of the...
Definition: SoftwareTriggerModule.cc:101
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SoftwareTrigger::SoftwareTriggerModule::initializeCalculation
void initializeCalculation()
Helper function to initialize the calculation by creating a new calculation object and requiring all ...
Definition: SoftwareTriggerModule.cc:119
Belle2::SoftwareTrigger::SoftwareTriggerModule::SoftwareTriggerModule
SoftwareTriggerModule()
Create a new module instance and set the parameters.
Definition: SoftwareTriggerModule.cc:25
Belle2::SoftwareTrigger::SoftwareTriggerModule::initializeCounters
void initializeCounters()
Helper function to initialize the internal counters used for each cut identifier.
Definition: SoftwareTriggerModule.cc:150
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_storeDebugOutputToROOTFile
bool m_param_storeDebugOutputToROOTFile
Flag to also store the result of the calculations into a root file.
Definition: SoftwareTriggerModule.h:96
Belle2::SoftwareTriggerCutResult::c_reject
@ c_reject
Reject this event.
Belle2::Module::setReturnValue
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:222
Belle2::SoftwareTrigger::SoftwareTriggerDBHandler::makeTotalResultName
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...
Definition: SoftwareTriggerDBHandler.cc:48
Belle2::Module::addParam
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:562
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_debugOutputFileName
std::string m_param_debugOutputFileName
Output file name for the debug output. Is only used if debug is turned on.
Definition: SoftwareTriggerModule.h:100
Belle2::SoftwareTrigger::SoftwareTriggerModule::initializeDebugOutput
void initializeDebugOutput()
Helper function to initliaze debug output creation by creating a TTree and an object in the data stor...
Definition: SoftwareTriggerModule.cc:132
Belle2::SoftwareTrigger::SkimSampleCalculator
Implementation of a calculator used in the SoftwareTriggerModule to fill a SoftwareTriggerObject for ...
Definition: SkimSampleCalculator.h:38
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_debugOutputFile
std::unique_ptr< TFile > m_debugOutputFile
TFile to store the debug TTree (or a nullptr if we do not save the debug output).
Definition: SoftwareTriggerModule.h:114
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_baseIdentifier
std::string m_param_baseIdentifier
Base identifier for all cuts downloaded from database.
Definition: SoftwareTriggerModule.h:92
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_debugOutputStoreObject
StoreObjPtr< SoftwareTriggerVariables > m_debugOutputStoreObject
TTree living in the datastore for debug reasons.
Definition: SoftwareTriggerModule.h:118
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_dbHandler
std::unique_ptr< SoftwareTriggerDBHandler > m_dbHandler
Internal handler object for the DB interface.
Definition: SoftwareTriggerModule.h:110
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_calculation
std::unique_ptr< SoftwareTriggerCalculation > m_calculation
Internal handler for the Calculations (will be set in initialize to the correct one).
Definition: SoftwareTriggerModule.h:112
Belle2::SoftwareTrigger::FinalTriggerDecisionCalculator::getModuleResult
static SoftwareTriggerCutResult getModuleResult(const SoftwareTriggerResult &result, const std::string &baseIdentifier, bool acceptOverridesReject)
Calculate the "total_result" for a given base identifier by looping through all results with the give...
Definition: FinalTriggerDecisionCalculator.cc:72
Belle2::SoftwareTrigger::SoftwareTriggerModule::makeDebugOutput
void makeDebugOutput()
Helper function to store the calculated variables from the calculation either in the TTree or in the ...
Definition: SoftwareTriggerModule.cc:194
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_debugOutputStoreObjName
std::string m_param_debugOutputStoreObjName
Output store object name for the debug output. Is only used if debug is turned on.
Definition: SoftwareTriggerModule.h:102
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_param_useRandomNumbersForPreScale
bool m_param_useRandomNumbersForPreScale
Flag to use random numbers or a counter for applying a prescale.
Definition: SoftwareTriggerModule.h:104
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53
Belle2::SoftwareTriggerCutResult
SoftwareTriggerCutResult
Enumeration with all possible results of the SoftwareTriggerCut.
Definition: SoftwareTriggerResult.h:31
Belle2::SoftwareTrigger::SoftwareTriggerModule::m_counters
std::vector< uint32_t > m_counters
Vector of the internal counters used to apply a prescale.
Definition: SoftwareTriggerModule.h:121
Belle2::SoftwareTrigger::SoftwareTriggerModule::makeCut
void makeCut(const SoftwareTriggerObject &prefilledObject)
Helper function to perform the actual cut on the prefilled object and set the return value of the mod...
Definition: SoftwareTriggerModule.cc:159