Belle II Software development
HistogramMapping.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 <daq/hbasf2/utils/HistogramMapping.h>
9
10#include <framework/pcore/MsgHandler.h>
11#include <framework/logging/Logger.h>
12
13#include <boost/range/combine.hpp>
14
15#include <TDirectory.h>
16
17using namespace Belle2;
18
19HistogramMapping::HistogramMapping(std::unique_ptr<Belle2::EvtMessage> msg)
20{
21 m_histograms.clear();
22
23 Belle2::MsgHandler msgHandler;
24 std::vector<TObject*> objects;
25 std::vector<std::string> names;
26 msgHandler.decode_msg(msg.get(), objects, names);
27
28 B2ASSERT("Objects and names need to align", names.size() == objects.size());
29
30 for (const auto& keyValue : boost::combine(names, objects)) {
31 std::string key;
32 TObject* object;
33 boost::tie(key, object) = keyValue;
34
35 TH1* histogram = dynamic_cast<TH1*>(object);
36 if (histogram == nullptr) {
37 B2WARNING("Object " << key << " is not a histogram!");
38 continue;
39 }
40
41 m_histograms.insert({key, std::unique_ptr<TH1>(histogram)});
42 m_histograms[key]->SetName(key.c_str());
43 }
44}
45
47{
48 for (auto& keyValue : rhs.m_histograms) {
49 const auto& key = keyValue.first;
50 const auto& histogram = keyValue.second;
51
52 auto lhsIterator = m_histograms.find(key);
53 if (lhsIterator == m_histograms.end()) {
54 B2DEBUG(100, "Creating new histogram with name " << key << ".");
55 auto* copiedHistogram = dynamic_cast<TH1*>(histogram->Clone());
56 m_histograms.insert({key, std::unique_ptr<TH1>(copiedHistogram)});
57 } else {
58 m_histograms[key]->Add(histogram.get());
59 }
60 }
61}
62
64{
65 for (const auto& [_, histogram] : m_histograms) {
66 histogram->SetDirectory(gDirectory);
67 histogram->Write();
68 }
69}
70
72{
73 m_histograms.clear();
74}
75
77{
78 return m_histograms.empty();
79}
80
82{
83 for (const auto& [key, histogram] : m_histograms) {
84 B2INFO(key << ": " << histogram->GetName() << " -> " << histogram->GetEntries());
85 }
86}
87
88std::unique_ptr<Belle2::EvtMessage> HistogramMapping::toMessage() const
89{
90 Belle2::MsgHandler msgHandler;
91
92 int objectCounter = 0;
93 for (const auto& [key, histogram] : m_histograms) {
94 msgHandler.add(histogram.get(), key);
95 objectCounter++;
96 }
97
98 std::unique_ptr<Belle2::EvtMessage> msg(msgHandler.encode_msg(Belle2::ERecordType::MSG_EVENT));
99 (msg->header())->reserved[0] = 0;
100 (msg->header())->reserved[1] = objectCounter;
101
102 return msg;
103}
Utility to store received histograms (hierarchical tree structures) from clients (as an event message...
HistogramMapping()=default
Default constructor needed during summation.
void write() const
Write out all stored histograms in the currently selected ROOT gDirectory.
bool empty() const
Check if there are no stored histograms.
std::map< std::string, std::unique_ptr< TH1 > > m_histograms
Internal storage of the histograms in the form name -> unique TH1 pointer.
void printMe() const
Debug function to print out the content as into messages.
void clear()
Clear all histograms in the internal map also deleting the pointers.
std::unique_ptr< Belle2::EvtMessage > toMessage() const
Construct an EvtMessage by serializing the content of the internal histogram storage....
void operator+=(const HistogramMapping &rhs)
Add another histogramm tree instance by merging all histograms with the same name.
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
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition: MsgHandler.cc:46
virtual EvtMessage * encode_msg(ERecordType rectype)
Stream object list into an EvtMessage.
Definition: MsgHandler.cc:67
Abstract base class for different kinds of events.