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