CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Book.h
Go to the documentation of this file.
1 #ifndef BOOK_FOR_ROOT_HISTOGRAMS
2 #define BOOK_FOR_ROOT_HISTOGRAMS
3 
4 #include <map>
5 #include <string>
6 #include "poly.h"
7 #include "TDirectory.h"
8 #include "TH1.h"
9 #include "TH1D.h"
10 #include "TH2D.h"
11 #include "TProfile.h"
12 #include "TH3D.h"
13 #include <boost/regex.hpp>
14 #include <boost/iterator/filter_iterator.hpp>
15 
16 class Book {
17  typedef const double double_t;
18  typedef const unsigned long uint_t;
19  typedef const std::string string_t;
20  typedef std::map<std::string, TH1*> book_t;
21 
24  TDirectory* directory;
25 
26  struct match_name {
28  bool operator()(const book_t::const_iterator::value_type& p) { return regex_match(p.first, expression); }
29 
30  private:
31  boost::regex expression;
32  };
33 
34 public:
35  Book() : title_(""), directory(nullptr) {}
36  Book(string_t t) : title_(t), directory(new TDirectory(t.c_str(), t.c_str())) {}
37 
38  string_t& title() const { return title_; }
39  bool empty() const { return book_.empty(); }
40  long size() const { return book_.size(); }
41 
42  TH1* book(string_t name, TH1* const hist) {
43  book_[name] = hist;
44  hist->SetDirectory(directory);
45  if (!hist->GetSumw2N())
46  hist->Sumw2();
47  return hist;
48  }
49  TH1*& operator[](string_t name) { return book_[name]; }
50  const TH1* operator[](string_t name) const {
51  book_t::const_iterator it = book_.find(name);
52  return it == book_.end() ? nullptr : it->second;
53  }
54 
55  typedef boost::filter_iterator<match_name, book_t::iterator> iterator;
56  typedef boost::filter_iterator<match_name, book_t::const_iterator> const_iterator;
57  iterator begin(string_t re = ".*") {
58  book_t::iterator b(book_.begin()), e(book_.end());
59  return boost::make_filter_iterator(match_name(re), b, e);
60  }
61  const_iterator begin(string_t re = ".*") const {
62  book_t::const_iterator b(book_.begin()), e(book_.end());
63  return boost::make_filter_iterator(match_name(re), b, e);
64  }
65  iterator end(string_t re = ".*") {
66  book_t::iterator e(book_.end());
67  return boost::make_filter_iterator(match_name(re), e, e);
68  }
69  const_iterator end(string_t re = ".*") const {
70  book_t::const_iterator e(book_.end());
71  return boost::make_filter_iterator(match_name(re), e, e);
72  }
74  return boost::make_filter_iterator(match_name(re), book_.find(name), book_.end());
75  }
77  return boost::make_filter_iterator(match_name(re), book_.find(name), book_.end());
78  }
79  std::pair<iterator, iterator> filter_range(string_t re = ".*") { return std::make_pair(begin(re), end(re)); }
80  std::pair<const_iterator, const_iterator> filter_range(string_t re = ".*") const {
81  return std::make_pair(begin(re), end(re));
82  }
83 
85  book_t::iterator it = book_.find(name);
86  if (it != book_.end()) {
87  delete it->second;
88  book_.erase(it);
89  }
90  }
91  void erase(iterator it) {
92  delete it->second;
93  book_.erase(it.base());
94  }
95 
96  void fill(double_t X, const char* name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W = 1) {
97  fill(X, std::string(name), NbinsX, Xlow, Xup, W);
98  }
100  for (auto const& name : names) {
101  book_t::const_iterator current = book_.find(name);
102  if (current == book_.end())
103  book(name, new TH1D(name.c_str(), "", NbinsX, Xlow, Xup))->Fill(X, W);
104  else
105  current->second->Fill(X, W);
106  }
107  }
108  void fill(double_t X, double_t Y, const char* name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W = 1) {
109  fill(X, Y, std::string(name), NbinsX, Xlow, Xup, W);
110  }
112  double_t Y,
113  const poly<std::string>& names,
114  uint_t NbinsX,
115  double_t Xlow,
116  double_t Xup,
117  double_t W = 1) {
118  for (auto const& name : names) {
119  book_t::const_iterator current = book_.find(name);
120  if (current == book_.end())
121  static_cast<TProfile*>(book(name, new TProfile(name.c_str(), "", NbinsX, Xlow, Xup)))->Fill(X, Y, W);
122  else
123  static_cast<TProfile*>(current->second)->Fill(X, Y, W);
124  }
125  }
127  double_t Y,
128  const char* name,
129  uint_t NbinsX,
130  double_t Xlow,
131  double_t Xup,
132  uint_t NbinsY,
133  double_t Ylow,
134  double_t Yup,
135  double_t W = 1) {
136  fill(X, Y, std::string(name), NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, W);
137  }
139  double_t Y,
140  const poly<std::string>& names,
141  uint_t NbinsX,
142  double_t Xlow,
143  double_t Xup,
144  uint_t NbinsY,
145  double_t Ylow,
146  double_t Yup,
147  double_t W = 1) {
148  for (auto const& name : names) {
149  book_t::const_iterator current = book_.find(name);
150  if (current == book_.end())
151  static_cast<TH2*>(book(name, new TH2D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup)))->Fill(X, Y, W);
152  else
153  static_cast<TH2*>(current->second)->Fill(X, Y, W);
154  }
155  }
157  double_t Y,
158  double_t Z,
159  const char* name,
160  uint_t NbinsX,
161  double_t Xlow,
162  double_t Xup,
163  uint_t NbinsY,
164  double_t Ylow,
165  double_t Yup,
166  uint_t NbinsZ,
167  double_t Zlow,
168  double_t Zup,
169  double_t W = 1) {
170  fill(X, Y, Z, std::string(name), NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup);
171  }
173  double_t Y,
174  double_t Z,
175  const poly<std::string>& names,
176  uint_t NbinsX,
177  double_t Xlow,
178  double_t Xup,
179  uint_t NbinsY,
180  double_t Ylow,
181  double_t Yup,
182  uint_t NbinsZ,
183  double_t Zlow,
184  double_t Zup,
185  double_t W = 1) {
186  for (auto const& name : names) {
187  book_t::const_iterator current = book_.find(name);
188  if (current == book_.end())
189  static_cast<TH3*>(
190  book(name, new TH3D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup)))
191  ->Fill(X, Y, Z, W);
192  else
193  static_cast<TH3*>(current->second)->Fill(X, Y, Z, W);
194  }
195  }
196 };
197 
198 #endif
Book(string_t t)
Definition: Book.h:36
long size() const
Definition: Book.h:40
string_t & title() const
Definition: Book.h:38
#define X(str)
Definition: MuonsGrabber.cc:38
const_iterator end(string_t re=".*") const
Definition: Book.h:69
void fill(double_t X, const poly< std::string > &names, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:99
const std::string names[nVars_]
const TH1 * operator[](string_t name) const
Definition: Book.h:50
void fill(double_t X, double_t Y, double_t Z, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, uint_t NbinsY, double_t Ylow, double_t Yup, uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1)
Definition: Book.h:156
const std::string string_t
Definition: Book.h:19
void fill(double_t X, double_t Y, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1)
Definition: Book.h:126
std::string title_
Definition: Book.h:23
const_iterator begin(string_t re=".*") const
Definition: Book.h:61
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
Book()
Definition: Book.h:35
void fill(double_t X, double_t Y, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:108
boost::filter_iterator< match_name, book_t::iterator > iterator
Definition: Book.h:55
book_t book_
Definition: Book.h:22
void erase(iterator it)
Definition: Book.h:91
std::map< std::string, TH1 * > book_t
Definition: Book.h:20
void fill(double_t X, double_t Y, const poly< std::string > &names, uint_t NbinsX, double_t Xlow, double_t Xup, uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1)
Definition: Book.h:138
iterator begin(string_t re=".*")
Definition: Book.h:57
Definition: poly.h:11
__shared__ Hist hist
void fill(double_t X, double_t Y, double_t Z, const poly< std::string > &names, uint_t NbinsX, double_t Xlow, double_t Xup, uint_t NbinsY, double_t Ylow, double_t Yup, uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1)
Definition: Book.h:172
TH1 *& operator[](string_t name)
Definition: Book.h:49
void fill(double_t X, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:96
TDirectory * directory
Definition: Book.h:24
std::pair< iterator, iterator > filter_range(string_t re=".*")
Definition: Book.h:79
match_name(string_t re)
Definition: Book.h:27
double b
Definition: hdecay.h:118
void fill(double_t X, double_t Y, const poly< std::string > &names, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:111
const double double_t
Definition: Book.h:17
bool empty() const
Definition: Book.h:39
boost::filter_iterator< match_name, book_t::const_iterator > const_iterator
Definition: Book.h:56
bool operator()(const book_t::const_iterator::value_type &p)
Definition: Book.h:28
Definition: Book.h:16
const unsigned long uint_t
Definition: Book.h:18
boost::regex expression
Definition: Book.h:31
iterator find(string_t name, string_t re=".*")
Definition: Book.h:73
void erase(string_t name)
Definition: Book.h:84
std::pair< const_iterator, const_iterator > filter_range(string_t re=".*") const
Definition: Book.h:80
iterator end(string_t re=".*")
Definition: Book.h:65
TH1 * book(string_t name, TH1 *const hist)
Definition: Book.h:42
const_iterator find(string_t name, string_t re=".*") const
Definition: Book.h:76