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