Belle II Software  release-05-02-19
softwareTriggerDBHandler.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - 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/core/SoftwareTriggerDBHandler.h>
12 #include <framework/database/Configuration.h>
13 #include <framework/database/Database.h>
14 
15 #include <framework/utilities/TestHelpers.h>
16 #include <gtest/gtest.h>
17 
18 using namespace std;
19 
20 namespace Belle2 {
25  namespace SoftwareTrigger {
26 
28  class SoftwareTriggerDBHandlerTest : public ::testing::Test {
30  TestHelpers::TempDirCreator* m_tmpDir = nullptr;
31 
33  void SetUp()
34  {
35  m_tmpDir = new TestHelpers::TempDirCreator;
36 
38  DataStore::Instance().setInitializeActive(true);
39  evtPtr.registerInDataStore();
40  DataStore::Instance().setInitializeActive(false);
41  evtPtr.construct(1, 0, 0);
42 
43  auto& conf = Conditions::Configuration::getInstance();
44  conf.setNewPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
45  conf.prependTestingPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
46  }
47 
49  void TearDown()
50  {
51  Database::reset();
52  DBStore::Instance().reset();
53  DataStore::Instance().reset();
54 
55  delete m_tmpDir;
56  }
57  };
58 
60  TEST_F(SoftwareTriggerDBHandlerTest, downloadAndChanged)
61  {
62  SoftwareTriggerObject preFilledObject;
63 
64  IntervalOfValidity iov(0, 0, -1, -1);
65 
66  // Create a new cut.
67  const auto& cutOne = SoftwareTriggerCut::compile("1 == 1", 1);
68 
69  // Upload the first cut
70  SoftwareTriggerDBHandler::upload(cutOne, "test", "cutOne", iov);
71 
72  // Try to download a missing cut.
73  SoftwareTriggerDBHandler::uploadTriggerMenu("test", {"cutOne", "cutTwo"}, true, iov);
74  EXPECT_B2FATAL(SoftwareTriggerDBHandler("test"));
75 
76  // Download and test the single uploaded cut.
77  SoftwareTriggerDBHandler::uploadTriggerMenu("test", {"cutOne"}, true, iov);
78 
79  SoftwareTriggerDBHandler dbHandler("test");
80  const auto& cutsWithNames = dbHandler.getCutsWithNames();
81  EXPECT_EQ(SoftwareTriggerCutResult::c_accept,
82  cutsWithNames.at("software_trigger_cut&test&cutOne")->checkPreScaled(preFilledObject));
83  EXPECT_THROW(cutsWithNames.at("software_trigger_cut&test&cutTwo"), std::out_of_range);
84 
85  // Create the missing cut
86  const auto& cutTwo = SoftwareTriggerCut::compile("1 == 2", 1);
87 
88  // Upload the second cut
89  SoftwareTriggerDBHandler::upload(cutOne, "test2", "cutOne", iov);
90  SoftwareTriggerDBHandler::upload(cutTwo, "test2", "cutTwo", iov);
91 
92  // Initialize with both cuts uploaded (should not fail)
93  SoftwareTriggerDBHandler::uploadTriggerMenu("test2", {"cutOne", "cutTwo"}, true, iov);
94 
95  SoftwareTriggerDBHandler otherDBHandler("test2");
96  const auto& otherCutsWithNames = otherDBHandler.getCutsWithNames();
97  // Try out both cuts
98  EXPECT_EQ(SoftwareTriggerCutResult::c_accept,
99  otherCutsWithNames.at("software_trigger_cut&test2&cutOne")->checkPreScaled(preFilledObject));
100  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult,
101  otherCutsWithNames.at("software_trigger_cut&test2&cutTwo")->checkPreScaled(preFilledObject));
102 
103  }
104 
106  TEST_F(SoftwareTriggerDBHandlerTest, pythonUpAndDownload)
107  {
108  IntervalOfValidity iov(0, 0, -1, -1);
109 
110  // Create a new cut.
111  auto cutOne = SoftwareTriggerCut::compile("1 == 1", 1);
112 
113  SoftwareTriggerDBHandler::upload(cutOne, "test", "cutOne1", iov);
114  auto downloadedCutOne = SoftwareTriggerDBHandler::download("test", "cutOne1");
115 
116  ASSERT_NE(downloadedCutOne, nullptr);
117 
118  EXPECT_EQ(cutOne->decompile(), downloadedCutOne->decompile());
119  EXPECT_EQ(cutOne->getPreScaleFactor(), downloadedCutOne->getPreScaleFactor());
120  EXPECT_EQ(cutOne->isRejectCut(), downloadedCutOne->isRejectCut());
121 
122  // Create a second cut.
123  const auto& cutTwo = SoftwareTriggerCut::compile("1 == 2", 1, true);
124 
125  SoftwareTriggerDBHandler::upload(cutTwo, "test", "cutTwo2", iov);
126  const auto& downloadedCutTwo = SoftwareTriggerDBHandler::download("test", "cutTwo2");
127 
128  ASSERT_NE(downloadedCutTwo, nullptr);
129 
130  EXPECT_EQ(cutTwo->decompile(), downloadedCutTwo->decompile());
131  EXPECT_EQ(cutTwo->getPreScaleFactor(), downloadedCutTwo->getPreScaleFactor());
132  EXPECT_EQ(cutTwo->isRejectCut(), downloadedCutTwo->isRejectCut());
133  }
134  }
136 }
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::TestHelpers::TempDirCreator
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:57
Belle2::SoftwareTrigger::SoftwareTriggerDBHandlerTest
Class to test the down- and upload of trigger cuts to the DB.
Definition: softwareTriggerDBHandler.cc:28
Belle2::StoreObjPtr::construct
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:128
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
Belle2::SoftwareTrigger::SoftwareTriggerDBHandlerTest::TearDown
void TearDown()
Destroy the DB and the DataStore.
Definition: softwareTriggerDBHandler.cc:49
Belle2::SoftwareTrigger::SoftwareTriggerDBHandlerTest::SetUp
void SetUp()
Setup the local DB and the datastore with the event meta data.
Definition: softwareTriggerDBHandler.cc:33
Belle2::TestHelpers::TempDirCreator::getTempDir
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:53