Belle II Software development
msghandler.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/pcore/MsgHandler.h>
9
10#include <TVector3.h>
11#include <TClonesArray.h>
12#include <TNamed.h>
13
14#include <gtest/gtest.h>
15
16using namespace std;
17using namespace Belle2;
18
19namespace {
20 TEST(MsgHandlerTest, conversion)
21 {
22 MsgHandler handler;
23 TVector3 a(1.0, 2.0, 3.0);
24 handler.add(&a, "vector");
25 TVector3 b(42.0, 7.0, -20.0);
26 handler.add(&b, "vectorb");
27 TNamed c("abc", "def");
28 handler.add(&c, "named");
29 EvtMessage* msg = handler.encode_msg(MSG_EVENT);
30
31 {
32 MsgHandler handler2;
33 vector<TObject*> objs;
34 vector<string> names;
35 handler2.decode_msg(msg, objs, names);
36 ASSERT_EQ(3, objs.size());
37 ASSERT_EQ(3, names.size());
38 ASSERT_EQ("vector", names[0]);
39 ASSERT_EQ("vectorb", names[1]);
40 ASSERT_EQ("named", names[2]);
41
42 auto a2 = dynamic_cast<TVector3*>(objs[0]);
43 ASSERT_NE(a2, nullptr);
44 EXPECT_TRUE(*a2 == a);
45
46 auto b2 = dynamic_cast<TVector3*>(objs[1]);
47 ASSERT_NE(b2, nullptr);
48 EXPECT_TRUE(*b2 == b);
49
50 auto c2 = dynamic_cast<TNamed*>(objs[2]);
51 ASSERT_NE(c2, nullptr);
52 EXPECT_TRUE(std::string(c2->GetName()) == c.GetName());
53 EXPECT_TRUE(std::string(c2->GetTitle()) == c.GetTitle());
54 }
55 delete msg;
56 }
57
58 TEST(MsgHandlerTest, compression)
59 {
60 TClonesArray longarray("Belle2::EventMetaData");
61 longarray.ExpandCreate(1000);
62 for (int algo : {0, 1, 2}) {
63 for (int level = 0; level < 10; ++level) {
64 MsgHandler handler(algo * 100 + level);
65 handler.add(&longarray, "mcparticles");
66 EvtMessage* msg = handler.encode_msg(MSG_EVENT);
67 {
68 MsgHandler handler2;
69 vector<TObject*> objs;
70 vector<string> names;
71 handler2.decode_msg(msg, objs, names);
72 ASSERT_EQ(1, objs.size());
73 ASSERT_EQ(1, names.size());
74 ASSERT_EQ(names[0], "mcparticles");
75 for (auto o : objs) delete o;
76 }
77 }
78 }
79 }
80} // namespace
Class to manage streamed object.
Definition: EvtMessage.h:59
A class to encode/decode an EvtMessage.
Definition: MsgHandler.h:103
virtual void decode_msg(EvtMessage *msg, std::vector< TObject * > &objlist, std::vector< std::string > &namelist)
Decode an EvtMessage into a vector list of objects with names.
Definition: MsgHandler.cc:106
Abstract base class for different kinds of events.
STL namespace.