Belle II Software development
SoftwareTriggerDBHandler.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 <framework/database/DBImportObjPtr.h>
9#include <hlt/softwaretrigger/core/SoftwareTriggerDBHandler.h>
10
11#include <boost/algorithm/string/predicate.hpp>
12
13namespace Belle2 {
18 namespace SoftwareTrigger {
19 const std::string SoftwareTriggerDBHandler::s_dbPackageIdentifier = "software_trigger_cut";
20 const std::string SoftwareTriggerDBHandler::s_totalResultIdentifier = "total_result";
21
23 {
25 dbCut.getPreScaleFactor(),
26 dbCut.isRejectCut());
27 }
28
29 std::string SoftwareTriggerDBHandler::makeFullCutName(const std::string& baseCutIdentifier,
30 const std::string& cutIdentifier)
31 {
32 assert(baseCutIdentifier.find("&") == std::string::npos);
33 assert(cutIdentifier.find("&") == std::string::npos);
34
35 return s_dbPackageIdentifier + "&" + baseCutIdentifier + "&" + cutIdentifier;
36 }
37
38 std::string SoftwareTriggerDBHandler::makeTotalResultName(const std::string& baseIdentifier)
39 {
40 return makeFullCutName(baseIdentifier, s_totalResultIdentifier);
41 }
42
43 std::string SoftwareTriggerDBHandler::makeFullTriggerMenuName(const std::string& baseIdentifier)
44 {
45 assert(baseIdentifier.find("&") == std::string::npos);
46
47 return s_dbPackageIdentifier + "&" + baseIdentifier;
48 }
49
50 bool SoftwareTriggerDBHandler::hasBaseIdentifier(const std::string& cutName, const std::string& baseIdentifier)
51 {
52 assert(baseIdentifier.find("&") == std::string::npos);
53 assert(std::count(cutName.begin(), cutName.end(), '&') == 2);
54
55 return boost::starts_with(cutName, makeFullCutName(baseIdentifier, ""));
56 }
57
58 void SoftwareTriggerDBHandler::upload(const std::unique_ptr<SoftwareTriggerCut>& cut, const std::string& baseCutIdentifier,
59 const std::string& cutIdentifier, const IntervalOfValidity& iov)
60 {
61 B2ASSERT("The name " << s_totalResultIdentifier << " is already used for the total result of each trigger stage. "
62 "You can not create a cut with the same name.", cutIdentifier != s_totalResultIdentifier);
63 const std::string& fullCutName = makeFullCutName(baseCutIdentifier, cutIdentifier);
65 cutToUpload.construct(cut->getPreScaleFactor(), cut->isRejectCut(), cut->decompile());
66 cutToUpload.import(iov);
67 }
68
69 void SoftwareTriggerDBHandler::uploadTriggerMenu(const std::string& baseCutIdentifier,
70 const std::vector<std::string>& cutIdentifiers,
71 bool acceptMode,
72 const IntervalOfValidity& iov)
73 {
74 const std::string& fullMenuName = makeFullTriggerMenuName(baseCutIdentifier);
75 DBImportObjPtr<SoftwareTriggerMenu> menuToUpload(fullMenuName);
76 menuToUpload.construct(cutIdentifiers, acceptMode);
77 menuToUpload.import(iov);
78 }
79
80 std::unique_ptr<SoftwareTriggerCut> SoftwareTriggerDBHandler::download(const std::string& baseCutIdentifier,
81 const std::string& cutIdentifier)
82 {
83 const std::string& fullCutName = makeFullCutName(baseCutIdentifier, cutIdentifier);
84 DBObjPtr<DBRepresentationOfSoftwareTriggerCut> downloadedCut(fullCutName);
85 if (downloadedCut) {
86 return createCutFromDB(*downloadedCut);
87 } else {
88 return nullptr;
89 }
90 }
91
92 std::unique_ptr<SoftwareTriggerMenu>
93 SoftwareTriggerDBHandler::downloadTriggerMenu(const std::string& baseCutIdentifier)
94 {
95 const std::string& fullMenuName = makeFullTriggerMenuName(baseCutIdentifier);
96 DBObjPtr<SoftwareTriggerMenu> downloadedMenu(fullMenuName);
97 if (downloadedMenu) {
98 return std::make_unique<SoftwareTriggerMenu>(*downloadedMenu);
99 } else {
100 return nullptr;
101 }
102 }
103
105 {
106 // In case the whole trigger menu has changed, we start from scratch and reload all triggers.
107 if (m_softwareTriggerMenu.hasChanged()) {
108 initialize();
109 return;
110 }
111
112 // In all other cases we just check each downloaded cut, if it has changed.
113 for (auto& databaseCutEntry : m_databaseObjects) {
114 if (databaseCutEntry.hasChanged()) {
115 B2ASSERT("The name of the database entry changed! This is not handled properly by the module.",
116 m_cutsWithIdentifier.find(databaseCutEntry.getName()) != m_cutsWithIdentifier.end());
117 m_cutsWithIdentifier[databaseCutEntry.getName()] = createCutFromDB(*databaseCutEntry);
118 }
119 }
120 }
121
123 {
124 B2ASSERT("Could not find a valid trigger name with this "
125 "base identifier (" << m_baseIdentifier << ") in the database.", m_softwareTriggerMenu);
126
127 m_databaseObjects.clear();
128 m_cutsWithIdentifier.clear();
129
130 const auto& cutIdentifiers = m_softwareTriggerMenu->getCutIdentifiers();
131 m_databaseObjects.reserve(cutIdentifiers.size());
132
133 B2DEBUG(20, "Initializing SoftwareTrigger DB with baseIdentifier " << m_baseIdentifier << " and " << cutIdentifiers.size() <<
134 " cutIdentifiers");
135
136 for (const std::string& cutIdentifier : cutIdentifiers) {
137 B2DEBUG(20, "-> with CutIndentifier " << cutIdentifier);
138
139 const std::string& fullIdentifier = makeFullCutName(m_baseIdentifier, cutIdentifier);
140 m_databaseObjects.emplace_back(fullIdentifier);
141 if (m_databaseObjects.back()) {
142 m_cutsWithIdentifier[fullIdentifier] = createCutFromDB(*m_databaseObjects.back());
143 } else {
144 B2FATAL("There is no DB object with the name " << fullIdentifier);
145 }
146 }
147 }
148
150 {
151 return m_softwareTriggerMenu->isAcceptMode();
152 }
153
154 const std::map<std::string, std::unique_ptr<const SoftwareTriggerCut>>& SoftwareTriggerDBHandler::getCutsWithNames() const
155 {
157 };
158 }
160}
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:36
Class for importing a single object to the database.
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Class to handle storing SoftwareTriggerCuts in the database.
const std::string & getCutString() const
Return the cut string stored in this db representation.
A class that describes the interval of experiments/runs for which an object in the database is valid.
bool isRejectCut() const
Returns true, if the cut is a reject cut and false otherwise.
unsigned int getPreScaleFactor() const
Return the list of pre scale factors.
static std::unique_ptr< SoftwareTriggerCut > compile(const std::string &cut_string, const unsigned int prescaleFactor, const bool rejectCut=false)
Compile a new SoftwareTriggerCut from a cut string (by using the GeneralCut::compile function) and an...
static std::unique_ptr< SoftwareTriggerMenu > downloadTriggerMenu(const std::string &baseCutIdentifier)
Download a trigger menu from the database.
static std::string makeFullTriggerMenuName(const std::string &baseIdentifier)
Helper function to compile the full menu identifier from the base name.
void initialize()
Download the trigger menu and afterwards the cuts with the given base name and specific names from th...
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.
const std::map< std::string, std::unique_ptr< const SoftwareTriggerCut > > & getCutsWithNames() const
Get the already downloaded list of constant cuts with their identifiers.
static const std::string s_totalResultIdentifier
Common suffix to identify all total results in the stored results.
static void uploadTriggerMenu(const std::string &baseCutIdentifier, const std::vector< std::string > &cutIdentifiers, bool acceptMode, const IntervalOfValidity &iov)
Upload a new (or replace an old version) trigger menu with the given base and specific names.
bool getAcceptOverridesReject() const
Return true of the trigger menu is in accept mode.
static std::unique_ptr< SoftwareTriggerCut > createCutFromDB(const DBRepresentationOfSoftwareTriggerCut &dbCut)
Helper factory function to generate a unique cut pointer from its representation in the database.
std::map< std::string, std::unique_ptr< const SoftwareTriggerCut > > m_cutsWithIdentifier
Map of cuts with their identifiers, downloaded from the database.
DBObjPtr< SoftwareTriggerMenu > m_softwareTriggerMenu
Database entry of the software trigger menu.
static bool hasBaseIdentifier(const std::string &cutName, const std::string &baseIdentifier)
Check if a given cut name in the form <package_identifier>&<base_name>&<cut_name> has the given base ...
std::vector< DBObjPtr< DBRepresentationOfSoftwareTriggerCut > > m_databaseObjects
Database entries of the cuts, which where created in the initialize function.
void checkForChangedDBEntries()
Helper function to check for changes in the DB of all cuts registered in the initialize function.
static void upload(const std::unique_ptr< SoftwareTriggerCut > &cut, const std::string &baseCutIdentifier, const std::string &cutIdentifier, const IntervalOfValidity &iov)
Upload a new (or replace an old version) cut with the given base and specific 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...
static const std::string s_dbPackageIdentifier
Common prefix to identify all software trigger cuts in the database.
static std::unique_ptr< SoftwareTriggerCut > download(const std::string &baseCutIdentifier, const std::string &cutIdentifier)
Download a cut from the database.
Abstract base class for different kinds of events.