travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HistContainer.h
Go to the documentation of this file.
1 #ifndef HistContainer_h
2 #define HistContainer_h
3 
4 #include <map>
5 #include <memory>
6 #include <string>
7 
8 #include "TH1.h"
9 #include "TDirectoryFile.h"
10 
11 
12 namespace tvx {
13 
14 typedef std::map<std::string, std::unique_ptr<TH1> > HistMap;
15 
16 
23 class HistContainer : public TDirectoryFile
24 {
25 public:
26 
27  HistContainer(const std::string name, TDirectory* motherDir=nullptr, const std::string option="");
28 
30  void Add(TH1* hist);
31 
34  const HistMap& GetHists() const;
35 
38  const TH1& operator[](const std::string& hist_name) const;
39 
42  const TH1* FindHist(const std::string& hist_name) const;
43 
47  virtual void FillDerivedHists();
48 
51  void SaveAllAs(std::string prefix="./", std::string img_format="png");
52 
53 protected:
54 
56  TH1* h(const std::string& hist_name) const;
57 
58 private:
59 
61  HistMap fHs;
62 };
63 
64 
65 inline const HistMap& HistContainer::GetHists() const
66 {
67  return fHs;
68 }
69 
70 
71 inline const TH1& HistContainer::operator[](const std::string& hist_name) const
72 {
73  return *(fHs.at(hist_name));
74 }
75 
76 
77 inline const TH1* HistContainer::FindHist(const std::string& hist_name) const
78 {
79  return h(hist_name);
80 }
81 
82 
84 
85 
86 inline TH1* HistContainer::h(const std::string& hist_name) const
87 {
88  auto iter = fHs.find(hist_name);
89  return ( iter != fHs.end() ) ? iter->second.get() : nullptr;
90 }
91 
92 }
93 
94 #endif
virtual void FillDerivedHists()
A user implementation of this method can be called when additional histograms need to be built from t...
Definition: HistContainer.h:83
std::map< std::string, std::unique_ptr< TH1 > > HistMap
Definition: HistContainer.h:14
HistContainer(const std::string name, TDirectory *motherDir=nullptr, const std::string option="")
Creates an empty histogram container.
Definition: Event.h:12
A light container to hold and manipulate user histograms.
Definition: HistContainer.h:23
HistMap fHs
A container of unique pointers to TH1 objects indexed by names.
Definition: HistContainer.h:61
TH1 * h(const std::string &hist_name) const
Unrestricted access to stored histograms for friends.
Definition: HistContainer.h:86
const HistMap & GetHists() const
Returns a reference to the internal histogram container for external handling.
Definition: HistContainer.h:65
const TH1 & operator[](const std::string &hist_name) const
Returns a reference to the histogram with name hist_name, throws a std::out_of_range exception if no ...
Definition: HistContainer.h:71
const TH1 * FindHist(const std::string &hist_name) const
Returns a raw pointer to the histogram with hist_name name or nullptr such histogram does not exist...
Definition: HistContainer.h:77
void SaveAllAs(std::string prefix="./", std::string img_format="png")
Saves all histograms from the container as png images in the prefix directory.
void Add(TH1 *hist)
This container assumes the ownership of the histogram and can modify it.