Belle II Software development
HistogramMapping Class Reference

Utility to store received histograms (hierarchical tree structures) from clients (as an event message), with a function to add multiple histogram trees together. More...

#include <HistogramMapping.h>

Public Member Functions

HistogramMappingoperator= (const HistogramMapping &rhs)=delete
 As this is a heavy object, make sure to not copy.
 
HistogramMappingoperator= (HistogramMapping &&rhs)=default
 Moving is allowed.
 
 HistogramMapping (const HistogramMapping &rhs)=delete
 As this is a heavy object, make sure to not copy.
 
 HistogramMapping (HistogramMapping &&rhs)=default
 Moving is allowed.
 
 HistogramMapping ()=default
 Default constructor needed during summation.
 
 HistogramMapping (std::unique_ptr< Belle2::EvtMessage > msg)
 Constructor via a received event message by deserializing the histograms.
 
void operator+= (const HistogramMapping &rhs)
 Add another histogramm tree instance by merging all histograms with the same name.
 
void write () const
 Write out all stored histograms in the currently selected ROOT gDirectory.
 
void clear ()
 Clear all histograms in the internal map also deleting the pointers.
 
void printMe () const
 Debug function to print out the content as into messages.
 
std::unique_ptr< Belle2::EvtMessagetoMessage () const
 Construct an EvtMessage by serializing the content of the internal histogram storage. Will not invalidate the histograms.
 
bool empty () const
 Check if there are no stored histograms.
 

Private Attributes

std::map< std::string, std::unique_ptr< TH1 > > m_histograms
 Internal storage of the histograms in the form name -> unique TH1 pointer.
 

Detailed Description

Utility to store received histograms (hierarchical tree structures) from clients (as an event message), with a function to add multiple histogram trees together.

Can be converted back into a single event message and is used in the histogram server for merging the received histogram messages. Internally, the tree structure is stored as a mapping name -> TH1 (unique) pointer, where folder structure is mapped via "/" in the name.

Definition at line 30 of file HistogramMapping.h.

Constructor & Destructor Documentation

◆ HistogramMapping()

HistogramMapping ( std::unique_ptr< Belle2::EvtMessage msg)
explicit

Constructor via a received event message by deserializing the histograms.

Definition at line 19 of file HistogramMapping.cc.

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}
std::map< std::string, std::unique_ptr< TH1 > > m_histograms
Internal storage of the histograms in the form name -> unique TH1 pointer.
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

Member Function Documentation

◆ clear()

void clear ( )

Clear all histograms in the internal map also deleting the pointers.

Definition at line 71 of file HistogramMapping.cc.

72{
73 m_histograms.clear();
74}

◆ empty()

bool empty ( ) const

Check if there are no stored histograms.

Definition at line 76 of file HistogramMapping.cc.

77{
78 return m_histograms.empty();
79}

◆ operator+=()

void operator+= ( const HistogramMapping rhs)

Add another histogramm tree instance by merging all histograms with the same name.

Definition at line 46 of file HistogramMapping.cc.

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}

◆ printMe()

void printMe ( ) const

Debug function to print out the content as into messages.

Definition at line 81 of file HistogramMapping.cc.

82{
83 for (const auto& [key, histogram] : m_histograms) {
84 B2INFO(key << ": " << histogram->GetName() << " -> " << histogram->GetEntries());
85 }
86}

◆ toMessage()

std::unique_ptr< Belle2::EvtMessage > toMessage ( ) const

Construct an EvtMessage by serializing the content of the internal histogram storage. Will not invalidate the histograms.

Definition at line 88 of file HistogramMapping.cc.

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}
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

◆ write()

void write ( ) const

Write out all stored histograms in the currently selected ROOT gDirectory.

Definition at line 63 of file HistogramMapping.cc.

64{
65 for (const auto& [_, histogram] : m_histograms) {
66 histogram->SetDirectory(gDirectory);
67 histogram->Write();
68 }
69}

Member Data Documentation

◆ m_histograms

std::map<std::string, std::unique_ptr<TH1> > m_histograms
private

Internal storage of the histograms in the form name -> unique TH1 pointer.

Definition at line 61 of file HistogramMapping.h.


The documentation for this class was generated from the following files: