Belle II Software  release-05-01-25
stream.cc
1 #include <framework/utilities/Stream.h>
2 #include <framework/dataobjects/RelationContainer.h>
3 #include <framework/dataobjects/RelationElement.h>
4 #include <framework/datastore/DataStore.h>
5 #include <framework/gearbox/Gearbox.h>
6 #include <framework/gearbox/GearDir.h>
7 
8 #include <TVector3.h>
9 
10 #include <fstream>
11 #include <gtest/gtest.h>
12 
13 using namespace std;
14 using namespace Belle2;
15 
16 namespace {
18  RelationContainer* createObject()
19  {
20  auto* relCont = new RelationContainer;
21  relCont->setFromName("a");
22  relCont->setToName("b");
23  relCont->setFromDurability(DataStore::c_Event);
24  relCont->setToDurability(DataStore::c_Event);
25  // cppcheck-suppress constVariable ; cppcheck doesn't understand the weird new placement clonesarry thing
26  TClonesArray& relations = relCont->elements();
27  //let's make this a bit larger (very small objects are never compressed)
28  for (int i = 0; i < 100; i++)
29  new(relations.AddrAt(relations.GetLast() + 1)) RelationElement(0, i + 1, 42.0);
30 
31  return relCont;
32  }
33 
35  void checkObject(const RelationContainer* rel)
36  {
37  ASSERT_NE(rel, nullptr);
38  EXPECT_TRUE(rel->getFromName() == "a");
39  EXPECT_TRUE(rel->getToName() == "b");
40  EXPECT_EQ(rel->getEntries(), 100);
41  for (int i = 0; i < 100; i++) {
42  EXPECT_EQ(rel->getElement(i).getToIndex(0), (unsigned int)(i + 1));
43  EXPECT_DOUBLE_EQ(rel->getElement(i).getWeight(0), 42.0);
44  }
45  }
46 
48  void checkGbContents()
49  {
50  const Gearbox& gb = Gearbox::getInstance();
51 
52  checkObject(dynamic_cast<const RelationContainer*>(gb.getTObject("/A/RelationContainer")));
53 
54  //Access from GearDir should work
55  GearDir detector("/A/");
56  checkObject(dynamic_cast<const RelationContainer*>(detector.getTObject("RelationContainer")));
57  }
58 
60  TEST(StreamTest, raw)
61  {
62  TVector3 v(1.0, 2.0, 3.0);
63  std::string vStr = Stream::serializeAndEncode(&v);
64  //B2INFO(vStr);
65 
66  //restore
67  TObject* obj = Stream::deserializeEncodedRawData(vStr);
68  auto* v2 = dynamic_cast<TVector3*>(obj);
69  ASSERT_NE(v2, nullptr);
70  EXPECT_TRUE(*v2 == v);
71 
72  //something more complex
73  RelationContainer* relCont = createObject();
74 
75  std::string relStr = Stream::serializeAndEncode(relCont);
76  //B2INFO(relStr);
77  obj = Stream::deserializeEncodedRawData(relStr);
78  checkObject(dynamic_cast<const RelationContainer*>(obj));
79 
80  //creating file for next test..
81  /*
82  std::ofstream file("object_base64.xml");
83  file << Stream::escapeXML(relStr);
84  */
85 
86  // Given random input, TBase64 will most likely just crash, this specific truncation seems ok.
87  // Note that the string length needs to be a multiple of 4 because
88  // otherwise TBase64 will definitely access memory it should not as it just
89  // assumes multiple of four and then performs an invalid read of a char
90  // which it casts to unsigned int.
91  std::string truncated = vStr.substr(0, 12);
92  TObject* broken_obj = Stream::deserializeEncodedRawData(truncated);
93  EXPECT_TRUE(broken_obj == nullptr);
94  }
95 
97  TEST(StreamTest, GearboxRaw)
98  {
99  Gearbox& gb = Gearbox::getInstance();
100  vector<string> backends;
101  backends.emplace_back("file:/framework/tests/");
102  gb.setBackends(backends);
103  gb.open("object_base64.xml");
104 
105  checkGbContents();
106  }
107 
108 } // namespace
VXDTFFilterTest::v2
const std::vector< double > v2
MATLAB generated random vector.
Definition: decorrelationMatrix.cc:44
Belle2::Gearbox::getTObject
virtual const TObject * getTObject(const std::string &path) const noexcept(false) override
Get the parameter path as a TObject.
Definition: Gearbox.cc:288
Belle2::RelationContainer::getElement
const RelationElement & getElement(int i) const
Get reference to RelationElement at index i.
Definition: RelationContainer.cc:25
Belle2::RelationContainer
Class to store relations between StoreArrays in the DataStore.
Definition: RelationContainer.h:35
Belle2::RelationContainer::getEntries
int getEntries() const
Get number of elements.
Definition: RelationContainer.h:88
Belle2::RelationElement
Class to store a single element of a relation.
Definition: RelationElement.h:33
Belle2::RelationContainer::setFromName
void setFromName(const std::string &name)
Set name of the StoreArray we relate from.
Definition: RelationContainer.h:54
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Gearbox
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:44
Belle2::GearDir
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:41
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::RelationElement::getToIndex
index_type getToIndex(size_t n=0) const
Get nth index we point to.
Definition: RelationElement.h:87
Belle2::RelationContainer::getFromName
const std::string & getFromName() const
Get name of the StoreArray we relate from.
Definition: RelationContainer.h:42
Belle2::Gearbox::open
void open(const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
Open connection to backend and parse tree.
Definition: Gearbox.cc:126
Belle2::RelationElement::getWeight
weight_type getWeight(size_t n=0) const
Get nth weight we point to.
Definition: RelationElement.h:90
Belle2::RelationContainer::getToName
const std::string & getToName() const
Get name of the StoreArray we relate to.
Definition: RelationContainer.h:48
Belle2::Gearbox::setBackends
void setBackends(const std::vector< std::string > &backends)
Select the backends to use to find resources.
Definition: Gearbox.cc:92