10#include <framework/logging/Logger.h>
11#include <framework/pcore/EvtMessage.h>
12#include <framework/pcore/MsgHandler.h>
16#include <TCollection.h>
17#include <TDirectory.h>
40 unsigned int streamHistograms(TDirectory* directory,
Belle2::MsgHandler& msgHandler,
const std::string& directoryName =
"")
42 TList* keylist{directory->GetListOfKeys()};
43 TIter nextkey{keylist};
45 unsigned int counter{0};
46 while ((key =
dynamic_cast<TKey*
>(nextkey()))) {
47 TObject*
object{directory->Get(key->GetName())};
48 TClass* objectClass{
object->IsA()};
49 std::string objectName{directoryName};
50 if (not objectName.empty()) {
53 objectName +=
object->GetName();
54 if (objectClass->InheritsFrom(TH1::Class())) {
55 TH1* histogram{
dynamic_cast<TH1*
>(object)};
56 msgHandler.
add(histogram, objectName);
58 }
else if (objectClass->InheritsFrom(TDirectory::Class())) {
59 TDirectory* subDirectory{
dynamic_cast<TDirectory*
>(object)};
62 counter += streamHistograms(subDirectory, msgHandler, object->GetName());
69int main(
int argc,
char* argv[])
71 if (argc == 1 or std::string(argv[1]) ==
"--help" or std::string(argv[1]) ==
"-h") {
72 std::cout <<
"Usage: " << argv[0] <<
" INPUT_FILE\n\n"
73 " This tool checks if the online systems can handle the size of the DQM histograms\n"
74 " by compressing and decompressing them with LZ4 as done within our ZeroMQ framework.\n";
77 std::string inputFileName{argv[1]};
78 if (inputFileName.find(
".root") == std::string::npos) {
79 B2ERROR(
"The input file is not a .root file!");
82 if (gSystem->AccessPathName(inputFileName.c_str())) {
83 B2ERROR(
"The input file does not exist!");
86 std::unique_ptr<TFile> inputFile{
87 std::unique_ptr<TFile>(TFile::Open(inputFileName.c_str(),
"READ"))
89 if (!inputFile or !inputFile->IsOpen() or inputFile->IsZombie()) {
90 B2ERROR(
"The input file is not working!");
94 unsigned int streamedHistograms{streamHistograms(gDirectory, msgHandler)};
95 std::unique_ptr<Belle2::EvtMessage> evtMessage{
96 std::unique_ptr<Belle2::EvtMessage>(msgHandler.
encode_msg(Belle2::ERecordType::MSG_EVENT))
98 size_t maximalCompressedSize{100000000};
99 std::vector<char> compressedBuffer;
100 compressedBuffer.resize(maximalCompressedSize, 0);
102 LZ4_compress_default(evtMessage->buffer(), &compressedBuffer[0],
103 evtMessage->size(), maximalCompressedSize)
105 if (compressedSize <= 0) {
106 B2ERROR(
"LZ4_compress_default failed"
107 <<
LogVar(
"file name", inputFileName)
108 <<
LogVar(
"streamed histograms", streamedHistograms)
109 <<
LogVar(
"original size", evtMessage->size())
110 <<
LogVar(
"compressed size", compressedSize));
114 zmq::message_t message{&compressedBuffer[0],
static_cast<size_t>(compressedSize)};
115 size_t maximalUncompressedSize{128000000};
116 std::vector<char> uncompressedBuffer;
117 uncompressedBuffer.reserve(maximalUncompressedSize);
118 int uncompressedSize{
119 LZ4_decompress_safe(message.data<
char>(), &uncompressedBuffer[0],
120 message.size(), maximalUncompressedSize)
122 if (uncompressedSize <= 0) {
123 B2ERROR(
"LZ4_decompress_safe failed"
124 <<
LogVar(
"file name", inputFileName)
125 <<
LogVar(
"streamed histograms", streamedHistograms)
126 <<
LogVar(
"original size", evtMessage->size())
127 <<
LogVar(
"compressed size", compressedSize)
128 <<
LogVar(
"uncompressed size", uncompressedSize));
132 if (evtMessage->size() != uncompressedSize) {
133 B2ERROR(
"Original size and decompressed size differ"
134 <<
LogVar(
"file name", inputFileName)
135 <<
LogVar(
"streamed histograms", streamedHistograms)
136 <<
LogVar(
"original size", evtMessage->size())
137 <<
LogVar(
"compressed size", compressedSize)
138 <<
LogVar(
"uncompressed size", uncompressedSize));
142 B2INFO(
"The compression/decompression cycle with LZ4 is successfully completed"
143 <<
LogVar(
"file name", inputFileName)
144 <<
LogVar(
"streamed histograms", streamedHistograms)
145 <<
LogVar(
"original size", evtMessage->size())
146 <<
LogVar(
"compressed size", compressedSize)
147 <<
LogVar(
"uncompressed size", uncompressedSize)
148 <<
LogVar(
"compressed/uncompressed ratio", (compressedSize * 1.) / uncompressedSize));
A class to encode/decode an EvtMessage.
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
virtual EvtMessage * encode_msg(ERecordType rectype)
Stream object list into an EvtMessage.
Class to store variables with their name which were sent to the logging service.