CMS 3D CMS Logo

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 
18  typedef const double double_t;
19  typedef const unsigned long uint_t;
20  typedef const std::string string_t;
21  typedef std::map<std::string, TH1*> book_t;
22 
23  book_t book_;
25  TDirectory* directory;
26 
27  struct match_name {
28  match_name(string_t re) : expression(re) {}
29  bool operator()(const book_t::const_iterator::value_type& p) { return regex_match( p.first, expression); }
30  private: boost::regex expression;
31  };
32 
33  public:
34 
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) { book_[name]=hist; hist->SetDirectory(directory); if(!hist->GetSumw2N()) hist->Sumw2(); return hist;}
43  TH1*& operator[](string_t name) { return book_[name]; }
44  const TH1* operator[](string_t name) const { book_t::const_iterator it = book_.find(name); return it==book_.end() ? nullptr : it->second;}
45 
46  typedef boost::filter_iterator<match_name,book_t::iterator> iterator;
47  typedef boost::filter_iterator<match_name,book_t::const_iterator> const_iterator;
48  iterator begin(string_t re = ".*") {book_t::iterator b(book_.begin()), e(book_.end()); return boost::make_filter_iterator(match_name(re),b,e);}
49  const_iterator begin(string_t re = ".*") const {book_t::const_iterator b(book_.begin()), e(book_.end()); return boost::make_filter_iterator(match_name(re),b,e);}
50  iterator end( string_t re = ".*") {book_t::iterator e(book_.end()); return boost::make_filter_iterator(match_name(re),e,e);}
51  const_iterator end( string_t re = ".*") const {book_t::const_iterator e(book_.end()); return boost::make_filter_iterator(match_name(re),e,e);}
52  iterator find (string_t name, string_t re = ".*") { return boost::make_filter_iterator(match_name(re),book_.find(name),book_.end()); }
53  const_iterator find (string_t name, string_t re = ".*") const { return boost::make_filter_iterator(match_name(re),book_.find(name),book_.end()); }
54  std::pair<iterator,iterator> filter_range(string_t re = ".*") { return std::make_pair(begin(re), end(re) ); }
55  std::pair<const_iterator,const_iterator> filter_range(string_t re = ".*") const { return std::make_pair(begin(re), end(re) ); }
56 
57  void erase(string_t name) { book_t::iterator it = book_.find(name); if(it!=book_.end()) {delete it->second; book_.erase(it); } }
58  void erase(iterator it) { delete it->second; book_.erase(it.base()); }
59 
60  void fill( double_t X, const char* name,
61  uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1 ) { fill(X,std::string(name),NbinsX,Xlow,Xup,W);}
62  void fill( double_t X, const poly<std::string>& names,
63  uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1 )
64  {
65  for(auto const& name : names) {
66  book_t::const_iterator current = book_.find(name);
67  if( current == book_.end() )
68  book(name, new TH1D(name.c_str(), "", NbinsX, Xlow, Xup))->Fill(X,W);
69  else current->second->Fill(X,W);
70  }
71  }
72  void fill( double_t X, double_t Y, const char* name,
73  uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1 ) {fill(X,Y,std::string(name),NbinsX,Xlow,Xup,W);}
74  void fill( double_t X, double_t Y, const poly<std::string>& names,
75  uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1 )
76  {
77  for(auto const& name : names) {
78  book_t::const_iterator current = book_.find(name);
79  if( current == book_.end() )
80  static_cast<TProfile*>(book(name, new TProfile(name.c_str(), "", NbinsX, Xlow, Xup)))->Fill(X,Y,W);
81  else static_cast<TProfile*>(current->second)->Fill(X,Y,W);
82  }
83  }
84  void fill( double_t X, double_t Y, const char* name,
85  uint_t NbinsX, double_t Xlow, double_t Xup,
86  uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1 ) { fill(X,Y,std::string(name),NbinsX,Xlow,Xup,NbinsY,Ylow,Yup,W);}
87  void fill( double_t X, double_t Y, const poly<std::string>& names,
88  uint_t NbinsX, double_t Xlow, double_t Xup,
89  uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1 )
90  {
91  for(auto const& name : names) {
92  book_t::const_iterator current = book_.find(name);
93  if( current == book_.end() )
94  static_cast<TH2*>(book(name, new TH2D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup)))->Fill(X,Y,W);
95  else static_cast<TH2*>(current->second)->Fill(X,Y,W);
96  }
97  }
98  void fill( double_t X, double_t Y, double_t Z, const char* name,
99  uint_t NbinsX, double_t Xlow, double_t Xup,
100  uint_t NbinsY, double_t Ylow, double_t Yup,
101  uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1 ) {fill(X,Y,Z,std::string(name),NbinsX,Xlow,Xup,NbinsY,Ylow,Yup,NbinsZ,Zlow,Zup);}
102  void fill( double_t X, double_t Y, double_t Z, const poly<std::string>& names,
103  uint_t NbinsX, double_t Xlow, double_t Xup,
104  uint_t NbinsY, double_t Ylow, double_t Yup,
105  uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1 )
106  {
107  for(auto const& name : names) {
108  book_t::const_iterator current = book_.find(name);
109  if( current == book_.end() )
110  static_cast<TH3*>(book(name, new TH3D(name.c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup)))->Fill(X,Y,Z,W);
111  else static_cast<TH3*>(current->second)->Fill(X,Y,Z,W);
112  }
113  }
114 
115 };
116 
117 #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 nullptr
#define X(str)
Definition: MuonsGrabber.cc:48
const_iterator end(string_t re=".*") const
Definition: Book.h:51
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:62
const std::string names[nVars_]
std::pair< const_iterator, const_iterator > filter_range(string_t re=".*") const
Definition: Book.h:55
const TH1 * operator[](string_t name) const
Definition: Book.h:44
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:98
std::pair< iterator, iterator > filter_range(string_t re=".*")
Definition: Book.h:54
const std::string string_t
Definition: Book.h:20
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:84
std::string title_
Definition: Book.h:24
const_iterator begin(string_t re=".*") const
Definition: Book.h:49
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:72
book_t book_
Definition: Book.h:23
void erase(iterator it)
Definition: Book.h:58
std::map< std::string, TH1 * > book_t
Definition: Book.h:21
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:87
iterator begin(string_t re=".*")
Definition: Book.h:48
Definition: poly.h:10
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:102
TH1 *& operator[](string_t name)
Definition: Book.h:43
void fill(double_t X, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:60
TDirectory * directory
Definition: Book.h:25
match_name(string_t re)
Definition: Book.h:28
double b
Definition: hdecay.h:120
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:74
const double double_t
Definition: Book.h:18
bool empty() const
Definition: Book.h:39
boost::filter_iterator< match_name, book_t::const_iterator > const_iterator
Definition: Book.h:47
bool operator()(const book_t::const_iterator::value_type &p)
Definition: Book.h:29
Definition: Book.h:16
const unsigned long uint_t
Definition: Book.h:19
boost::regex expression
Definition: Book.h:30
iterator find(string_t name, string_t re=".*")
Definition: Book.h:52
void erase(string_t name)
Definition: Book.h:57
boost::filter_iterator< match_name, book_t::iterator > iterator
Definition: Book.h:46
iterator end(string_t re=".*")
Definition: Book.h:50
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:53