File indexing completed on 2024-04-06 11:59:46
0001 #ifndef BOOK_FOR_ROOT_HISTOGRAMS
0002 #define BOOK_FOR_ROOT_HISTOGRAMS
0003
0004 #include <map>
0005 #include <string>
0006 #include "poly.h"
0007 #include "TDirectory.h"
0008 #include "TH1.h"
0009 #include "TH1D.h"
0010 #include "TH2D.h"
0011 #include "TProfile.h"
0012 #include "TH3D.h"
0013 #include <boost/regex.hpp>
0014 #include <boost/iterator/filter_iterator.hpp>
0015
0016 class Book {
0017 typedef const double double_t;
0018 typedef const unsigned long uint_t;
0019 typedef const std::string string_t;
0020 typedef std::map<std::string, TH1*> book_t;
0021
0022 book_t book_;
0023 std::string title_;
0024 TDirectory* directory;
0025
0026 struct match_name {
0027 match_name(string_t re) : expression(re) {}
0028 bool operator()(const book_t::const_iterator::value_type& p) { return regex_match(p.first, expression); }
0029
0030 private:
0031 boost::regex expression;
0032 };
0033
0034 public:
0035 Book() : title_(""), directory(nullptr) {}
0036 Book(string_t t) : title_(t), directory(new TDirectory(t.c_str(), t.c_str())) {}
0037
0038 string_t& title() const { return title_; }
0039 bool empty() const { return book_.empty(); }
0040 long size() const { return book_.size(); }
0041
0042 TH1* book(string_t name, TH1* const hist) {
0043 book_[name] = hist;
0044 hist->SetDirectory(directory);
0045 if (!hist->GetSumw2N())
0046 hist->Sumw2();
0047 return hist;
0048 }
0049 TH1*& operator[](string_t name) { return book_[name]; }
0050 const TH1* operator[](string_t name) const {
0051 book_t::const_iterator it = book_.find(name);
0052 return it == book_.end() ? nullptr : it->second;
0053 }
0054
0055 typedef boost::filter_iterator<match_name, book_t::iterator> iterator;
0056 typedef boost::filter_iterator<match_name, book_t::const_iterator> const_iterator;
0057 iterator begin(string_t re = ".*") {
0058 book_t::iterator b(book_.begin()), e(book_.end());
0059 return boost::make_filter_iterator(match_name(re), b, e);
0060 }
0061 const_iterator begin(string_t re = ".*") const {
0062 book_t::const_iterator b(book_.begin()), e(book_.end());
0063 return boost::make_filter_iterator(match_name(re), b, e);
0064 }
0065 iterator end(string_t re = ".*") {
0066 book_t::iterator e(book_.end());
0067 return boost::make_filter_iterator(match_name(re), e, e);
0068 }
0069 const_iterator end(string_t re = ".*") const {
0070 book_t::const_iterator e(book_.end());
0071 return boost::make_filter_iterator(match_name(re), e, e);
0072 }
0073 iterator find(string_t name, string_t re = ".*") {
0074 return boost::make_filter_iterator(match_name(re), book_.find(name), book_.end());
0075 }
0076 const_iterator find(string_t name, string_t re = ".*") const {
0077 return boost::make_filter_iterator(match_name(re), book_.find(name), book_.end());
0078 }
0079 std::pair<iterator, iterator> filter_range(string_t re = ".*") { return std::make_pair(begin(re), end(re)); }
0080 std::pair<const_iterator, const_iterator> filter_range(string_t re = ".*") const {
0081 return std::make_pair(begin(re), end(re));
0082 }
0083
0084 void erase(string_t name) {
0085 book_t::iterator it = book_.find(name);
0086 if (it != book_.end()) {
0087 delete it->second;
0088 book_.erase(it);
0089 }
0090 }
0091 void erase(iterator it) {
0092 delete it->second;
0093 book_.erase(it.base());
0094 }
0095
0096 void fill(double_t X, const char* name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W = 1) {
0097 fill(X, std::string(name), NbinsX, Xlow, Xup, W);
0098 }
0099 void fill(double_t X, const poly<std::string>& names, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W = 1) {
0100 for (auto const& name : names) {
0101 book_t::const_iterator current = book_.find(name);
0102 if (current == book_.end())
0103 book(name, new TH1D(name.c_str(), "", NbinsX, Xlow, Xup))->Fill(X, W);
0104 else
0105 current->second->Fill(X, W);
0106 }
0107 }
0108 void fill(double_t X, double_t Y, const char* name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W = 1) {
0109 fill(X, Y, std::string(name), NbinsX, Xlow, Xup, W);
0110 }
0111 void fill(double_t X,
0112 double_t Y,
0113 const poly<std::string>& names,
0114 uint_t NbinsX,
0115 double_t Xlow,
0116 double_t Xup,
0117 double_t W = 1) {
0118 for (auto const& name : names) {
0119 book_t::const_iterator current = book_.find(name);
0120 if (current == book_.end())
0121 static_cast<TProfile*>(book(name, new TProfile(name.c_str(), "", NbinsX, Xlow, Xup)))->Fill(X, Y, W);
0122 else
0123 static_cast<TProfile*>(current->second)->Fill(X, Y, W);
0124 }
0125 }
0126 void fill(double_t X,
0127 double_t Y,
0128 const char* name,
0129 uint_t NbinsX,
0130 double_t Xlow,
0131 double_t Xup,
0132 uint_t NbinsY,
0133 double_t Ylow,
0134 double_t Yup,
0135 double_t W = 1) {
0136 fill(X, Y, std::string(name), NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, W);
0137 }
0138 void fill(double_t X,
0139 double_t Y,
0140 const poly<std::string>& names,
0141 uint_t NbinsX,
0142 double_t Xlow,
0143 double_t Xup,
0144 uint_t NbinsY,
0145 double_t Ylow,
0146 double_t Yup,
0147 double_t W = 1) {
0148 for (auto const& name : names) {
0149 book_t::const_iterator current = book_.find(name);
0150 if (current == book_.end())
0151 static_cast<TH2*>(book(name, new TH2D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup)))->Fill(X, Y, W);
0152 else
0153 static_cast<TH2*>(current->second)->Fill(X, Y, W);
0154 }
0155 }
0156 void fill(double_t X,
0157 double_t Y,
0158 double_t Z,
0159 const char* name,
0160 uint_t NbinsX,
0161 double_t Xlow,
0162 double_t Xup,
0163 uint_t NbinsY,
0164 double_t Ylow,
0165 double_t Yup,
0166 uint_t NbinsZ,
0167 double_t Zlow,
0168 double_t Zup,
0169 double_t W = 1) {
0170 fill(X, Y, Z, std::string(name), NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup);
0171 }
0172 void fill(double_t X,
0173 double_t Y,
0174 double_t Z,
0175 const poly<std::string>& names,
0176 uint_t NbinsX,
0177 double_t Xlow,
0178 double_t Xup,
0179 uint_t NbinsY,
0180 double_t Ylow,
0181 double_t Yup,
0182 uint_t NbinsZ,
0183 double_t Zlow,
0184 double_t Zup,
0185 double_t W = 1) {
0186 for (auto const& name : names) {
0187 book_t::const_iterator current = book_.find(name);
0188 if (current == book_.end())
0189 static_cast<TH3*>(
0190 book(name, new TH3D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup)))
0191 ->Fill(X, Y, Z, W);
0192 else
0193 static_cast<TH3*>(current->second)->Fill(X, Y, Z, W);
0194 }
0195 }
0196 };
0197
0198 #endif