8#include <framework/database/IntervalOfValidity.h>
9#include <framework/database/Database.h>
10#include <framework/database/Configuration.h>
11#include <framework/database/DBObjPtr.h>
12#include <framework/database/DBArray.h>
13#include <framework/database/EventDependency.h>
14#include <framework/database/PayloadFile.h>
15#include <framework/database/DBPointer.h>
16#include <framework/datastore/StoreObjPtr.h>
17#include <framework/dataobjects/EventMetaData.h>
18#include <framework/utilities/TestHelpers.h>
19#include <framework/geometry/BFieldManager.h>
20#include <framework/dbobjects/MagneticField.h>
21#include <framework/dbobjects/MagneticFieldComponentConstant.h>
24#include <TClonesArray.h>
26#include <gtest/gtest.h>
38 class DataBaseTest :
public ::testing::Test {
42 enum EDatabaseType {c_local, c_central, c_chain, c_default};
45 EDatabaseType m_dbType = c_local;
62 c.overrideGlobalTags();
63 c.setMetadataProviders({});
64 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
65 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
68 c.setGlobalTags({
"default"});
69 c.overrideGlobalTags();
72 c.setGlobalTags({
"default"});
73 c.overrideGlobalTags();
74 c.setMetadataProviders({});
75 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
76 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
82 if (m_dbType != c_central) {
83 list<Database::DBImportQuery> query;
84 TClonesArray array(
"TObject");
85 for (
int experiment = 1; experiment <= 5; experiment++) {
88 TString name =
"Experiment ";
92 new (array[experiment - 1]) TObject;
93 array[experiment - 1]->SetUniqueID(experiment);
96 FILE* f = fopen(
"file.xml",
"w");
97 fprintf(f,
"Experiment %d\n", experiment);
101 if (m_dbType != c_chain) {
106 intraRunDep.add(10,
new TNamed(
"B",
"B"));
107 intraRunDep.add(50,
new TNamed(
"C",
"C"));
116 void TearDown()
override
118 if (m_dbType != c_central) std::filesystem::remove_all(
"testPayloads");
149 TEST_F(DataBaseTest, IntervalOfValidityOperations)
183 EXPECT_TRUE(iov1.
empty());
202 EXPECT_TRUE(iov2.
empty());
220 TEST_F(DataBaseTest, getData)
222 EXPECT_TRUE(strcmp(
Database::Instance().getData(
"TNamed", 1, 1)->GetName(),
"Experiment 1") == 0);
223 EXPECT_TRUE(strcmp(
Database::Instance().getData(
"TNamed", 4, 1)->GetName(),
"Experiment 4") == 0);
233 evtPtr->setExperiment(1);
236 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
237 evtPtr->setExperiment(4);
238 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
240 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 4") == 0);
241 evtPtr->setExperiment(7);
255 for (
const auto& o : missing) {
263 evtPtr->setExperiment(1);
265 EXPECT_TRUE(objects);
266 EXPECT_FALSE(missing);
267 EXPECT_EQ(objects.getEntries(), 1);
268 EXPECT_EQ(objects[0]->GetUniqueID(), 1);
269 evtPtr->setExperiment(4);
270 EXPECT_EQ(objects.getEntries(), 1);
272 EXPECT_EQ(objects.getEntries(), 4);
276 for (
const auto& o : objects) {
277 EXPECT_EQ(o.GetUniqueID(), ++i);
285 for (
const auto& o : missing) {
291 evtPtr->setExperiment(7);
293 EXPECT_FALSE(objects);
297 for (
const auto& o : objects) {
298 EXPECT_EQ(o.GetUniqueID(), ++i);
305 TEST_F(DataBaseTest, DBArrayRange)
310 evtPtr->setExperiment(3);
312 EXPECT_THROW(objects[-1], std::out_of_range);
313 EXPECT_THROW(objects[3], std::out_of_range);
317 TEST_F(DataBaseTest, TypeCheck)
327 TEST_F(DataBaseTest, IntraRun)
332 evtPtr->setExperiment(1);
337 EXPECT_TRUE(strcmp(intraRun->GetName(),
"A") == 0);
339 evtPtr->setEvent(10);
341 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
343 evtPtr->setEvent(49);
345 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
349 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
351 evtPtr->setEvent(50);
353 EXPECT_TRUE(strcmp(intraRun->GetName(),
"C") == 0);
357 EXPECT_TRUE(strcmp(intraRun->GetName(),
"X") == 0);
361 TEST_F(DataBaseTest, HasChanged)
364 evtPtr->setExperiment(0);
370 evtPtr->setExperiment(1);
380 evtPtr->setExperiment(5);
384 evtPtr->setExperiment(7);
388 evtPtr->setExperiment(1);
394 EXPECT_TRUE(intraRun.hasChanged());
398 EXPECT_FALSE(intraRun.hasChanged());
400 evtPtr->setEvent(10);
402 EXPECT_TRUE(intraRun.hasChanged());
404 evtPtr->setEvent(1000);
406 EXPECT_TRUE(intraRun.hasChanged());
411 EXPECT_TRUE(intraRun.hasChanged());
420 evtPtr->setExperiment(1);
422 EXPECT_EQ(payload->getContent(),
"Experiment 1\n") << payload->getFileName();
423 evtPtr->setExperiment(4);
425 EXPECT_EQ(payload->getContent(),
"Experiment 4\n") << payload->getFileName();
426 evtPtr->setExperiment(7);
428 EXPECT_FALSE(payload);
432 int callbackCounter = 0;
446 Callback callbackObject;
448 TEST_F(DataBaseTest, Callbacks)
457 EXPECT_EQ(callbackCounter, 0);
459 evtPtr->setExperiment(2);
461 EXPECT_EQ(callbackCounter, 1);
463 evtPtr->setExperiment(4);
465 EXPECT_EQ(callbackCounter, 2);
467 EXPECT_EQ(callbackCounter, 2);
469 evtPtr->setExperiment(6);
471 EXPECT_EQ(callbackCounter, 3);
473 evtPtr->setExperiment(7);
475 EXPECT_EQ(callbackCounter, 3);
478 objects.addCallback(&callback);
480 evtPtr->setExperiment(1);
483 EXPECT_EQ(callbackCounter, 5);
486 intraRun.addCallback(&callbackObject, &Callback::callback);
491 EXPECT_EQ(callbackCounter, 5);
495 EXPECT_EQ(callbackCounter, 5);
497 evtPtr->setEvent(10);
499 EXPECT_EQ(callbackCounter, 6);
503 EXPECT_EQ(callbackCounter, 7);
507 TEST_F(DataBaseTest, KeyAccess)
510 evtPtr->setExperiment(1);
514 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
515 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2),
nullptr);
516 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
false)->GetUniqueID(), 1);
517 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
true),
nullptr);
519 evtPtr->setExperiment(2);
522 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
523 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2)->GetUniqueID(), 2);
530 evtPtr->setExperiment(2);
534 EXPECT_EQ(ptr.key(), 1);
535 EXPECT_TRUE(ptr.isValid());
536 EXPECT_EQ(ptr->GetUniqueID(), 1);
538 EXPECT_EQ(ptr->GetUniqueID(), 2);
540 EXPECT_FALSE(ptr.isValid());
543 TEST_F(DataBaseTest, CleanupReattachDeathtest)
546 evtPtr->setExperiment(2);
570 class DataBaseNoDataStoreTest :
public ::testing::Test {
574 enum EDatabaseType {c_local, c_central, c_chain, c_default};
577 EDatabaseType m_dbType = c_local;
583 DataBaseNoDataStoreTest() : m_event(0, 0, 1) {};
586 void SetUp()
override
592 c.overrideGlobalTags();
593 c.setMetadataProviders({});
594 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
595 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
598 c.setGlobalTags({
"default"});
599 c.overrideGlobalTags();
602 c.setGlobalTags({
"default"});
603 c.overrideGlobalTags();
604 c.setMetadataProviders({});
605 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
606 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
612 if (m_dbType != c_central) {
613 list<Database::DBImportQuery> query;
614 TClonesArray array(
"TObject");
615 for (
int experiment = 1; experiment <= 5; experiment++) {
618 TString name =
"Experiment ";
622 new (array[experiment - 1]) TObject;
623 array[experiment - 1]->SetUniqueID(experiment);
626 FILE* f = fopen(
"file.xml",
"w");
627 fprintf(f,
"Experiment %d\n", experiment);
631 if (m_dbType != c_chain) {
636 intraRunDep.add(10,
new TNamed(
"B",
"B"));
637 intraRunDep.add(50,
new TNamed(
"C",
"C"));
646 void TearDown()
override
648 if (m_dbType != c_central) std::filesystem::remove_all(
"testPayloads");
655 TEST_F(DataBaseNoDataStoreTest,
DBObjPtr)
662 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
664 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
666 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 4") == 0);
673 TEST_F(DataBaseNoDataStoreTest,
DBArray)
681 for (
const auto& o : missing) {
691 EXPECT_TRUE(objects);
692 EXPECT_FALSE(missing);
693 EXPECT_EQ(objects.getEntries(), 1);
694 EXPECT_EQ(objects[0]->GetUniqueID(), 1);
696 EXPECT_EQ(objects.getEntries(), 1);
698 EXPECT_EQ(objects.getEntries(), 4);
702 for (
const auto& o : objects) {
703 EXPECT_EQ(o.GetUniqueID(), ++i);
711 for (
const auto& o : missing) {
719 EXPECT_FALSE(objects);
723 for (
const auto& o : objects) {
724 EXPECT_EQ(o.GetUniqueID(), ++i);
731 TEST_F(DataBaseNoDataStoreTest, DBArrayRange)
737 EXPECT_THROW(objects[-1], std::out_of_range);
738 EXPECT_THROW(objects[3], std::out_of_range);
742 TEST_F(DataBaseNoDataStoreTest, TypeCheck)
752 TEST_F(DataBaseNoDataStoreTest, IntraRun)
761 EXPECT_TRUE(strcmp(intraRun->GetName(),
"A") == 0);
765 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
769 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
773 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
777 EXPECT_TRUE(strcmp(intraRun->GetName(),
"C") == 0);
781 EXPECT_TRUE(strcmp(intraRun->GetName(),
"X") == 0);
785 TEST_F(DataBaseNoDataStoreTest, HasChanged)
817 EXPECT_TRUE(intraRun.hasChanged());
821 EXPECT_FALSE(intraRun.hasChanged());
825 EXPECT_TRUE(intraRun.hasChanged());
829 EXPECT_TRUE(intraRun.hasChanged());
834 EXPECT_TRUE(intraRun.hasChanged());
844 EXPECT_EQ(payload->getContent(),
"Experiment 1\n") << payload->getFileName();
847 EXPECT_EQ(payload->getContent(),
"Experiment 4\n") << payload->getFileName();
850 EXPECT_FALSE(payload);
855 TEST_F(DataBaseNoDataStoreTest, Callbacks)
863 EXPECT_EQ(callbackCounter, 0);
867 EXPECT_EQ(callbackCounter, 1);
871 EXPECT_EQ(callbackCounter, 2);
873 EXPECT_EQ(callbackCounter, 2);
877 EXPECT_EQ(callbackCounter, 3);
881 EXPECT_EQ(callbackCounter, 3);
884 objects.addCallback(&callback);
889 EXPECT_EQ(callbackCounter, 5);
892 intraRun.addCallback(&callbackObject, &Callback::callback);
897 EXPECT_EQ(callbackCounter, 5);
901 EXPECT_EQ(callbackCounter, 5);
905 EXPECT_EQ(callbackCounter, 6);
909 EXPECT_EQ(callbackCounter, 7);
913 TEST_F(DataBaseNoDataStoreTest, KeyAccess)
919 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
920 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2),
nullptr);
921 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
false)->GetUniqueID(), 1);
922 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
true),
nullptr);
927 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
928 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2)->GetUniqueID(), 2);
931 TEST_F(DataBaseNoDataStoreTest,
DBPointer)
937 EXPECT_EQ(ptr.key(), 1);
938 EXPECT_TRUE(ptr.isValid());
939 EXPECT_EQ(ptr->GetUniqueID(), 1);
941 EXPECT_EQ(ptr->GetUniqueID(), 2);
943 EXPECT_FALSE(ptr.isValid());
static ROOT::Math::XYZVector getFieldInTesla(const ROOT::Math::XYZVector &pos)
return the magnetic field at a given position in Tesla.
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
void addCallback(std::function< void(const std::string &)> callback, bool onDestruction=false)
Add a callback method.
bool isValid() const
Check whether a valid object was obtained from the database.
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
Class for accessing arrays of objects in the database.
Class for accessing objects in the database.
Class for pointing to an element in an array stored in the database.
static DataStore & Instance()
Instance of singleton Store.
void setInitializeActive(bool active)
Setter for m_initializeActive.
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
bool addPayload(const std::string &name, const std::string &fileName, const IntervalOfValidity &iov)
Add a payload file to the database.
Class for handling changing conditions as a function of event number.
A class that describes the interval of experiments/runs for which an object in the database is valid.
bool trimOverlap(IntervalOfValidity &iov, bool trimOlder=true)
Remove the overlap between two intervals of validity by shortening one of them.
bool empty() const
Function that checks whether the validity interval is empty.
Describe one component of the Geometry.
A wrapper class for payload files used by the Database and DBStore classes.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
changes working directory into a newly created directory, and removes it (and contents) on destructio...
static const double T
[tesla]
void reset(bool keepEntries=false)
Invalidate all payloads.
static Database & Instance()
Instance of a singleton Database.
static DBStore & Instance()
Instance of a singleton DBStore.
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store an object in the database.
void updateEvent()
Updates all intra-run dependent objects.
void update()
Updates all objects that are outside their interval of validity.
void addConstantOverride(const std::string &name, TObject *obj, bool oneRun=false)
Add constant override payload.
static void getField(const double *pos, double *field)
return the magnetic field at a given position.
static void reset(bool keepConfig=false)
Reset the database instance.
Abstract base class for different kinds of events.
Struct for bulk write queries.