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 <boost/regex.hpp>
|
8 |
#include "TH1.h"
|
9 |
#include "TH1D.h"
|
10 |
#include "TH2D.h"
|
11 |
#include "TH3D.h"
|
12 |
|
13 |
class Book {
|
14 |
|
15 |
typedef std::map<std::string, TH1*> book_t;
|
16 |
book_t book;
|
17 |
const std::string _title;
|
18 |
|
19 |
typedef const double& double_t;
|
20 |
typedef const unsigned long& uint_t;
|
21 |
|
22 |
public:
|
23 |
|
24 |
Book() : _title("") {}
|
25 |
Book(const std::string& t) : _title(t) {}
|
26 |
|
27 |
const std::string& title() const {return _title;}
|
28 |
TH1* operator()(const std::string& name) { return book[name]; }
|
29 |
TH1* operator()(const std::string& name) const { return book.find(name)->second; }
|
30 |
bool contains (const std::string& name) const { return book.find(name) != book.end(); }
|
31 |
|
32 |
bool empty() const { return book.empty(); }
|
33 |
long size () const { return book.size(); }
|
34 |
|
35 |
class const_iterator;
|
36 |
const_iterator begin(const std::string& re=".*") const {return const_iterator( book.begin(), book.begin(), book.end(), boost::regex(re) ); }
|
37 |
const_iterator end(const std::string& re=".*") const {return const_iterator( book.end(), book.begin(), book.end(), boost::regex(re) ); }
|
38 |
|
39 |
void fill( double_t X, const poly<std::string>& names, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1 )
|
40 |
{
|
41 |
BOOST_FOREACH(std::string name, std::make_pair(names.begin(),names.end())) {
|
42 |
book_t::const_iterator current = book.find(name);
|
43 |
if( current == book.end() ) {
|
44 |
TH1* histogram = book[name] = new TH1D((_title+"/"+name).c_str(), "", NbinsX, Xlow, Xup);
|
45 |
histogram->SetDirectory(0);
|
46 |
histogram->Sumw2();
|
47 |
histogram->Fill(X,W);
|
48 |
} else current->second->Fill(X,W);
|
49 |
}
|
50 |
}
|
51 |
void fill( double_t X, double_t Y, const poly<std::string>& names, uint_t NbinsX, double_t Xlow, double_t Xup,
|
52 |
uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1 )
|
53 |
{
|
54 |
BOOST_FOREACH(std::string name, std::make_pair(names.begin(),names.end())) {
|
55 |
book_t::const_iterator current = book.find(name);
|
56 |
if( current == book.end() ) {
|
57 |
TH1* histogram = book[name] = new TH2D((_title+"/"+name).c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup);
|
58 |
histogram->SetDirectory(0);
|
59 |
histogram->Sumw2();
|
60 |
static_cast<TH2*>(histogram)->Fill(X,Y,W);
|
61 |
} else static_cast<TH2*>(current->second)->Fill(X,Y,W);
|
62 |
}
|
63 |
}
|
64 |
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,
|
65 |
uint_t NbinsY, double_t Ylow, double_t Yup,
|
66 |
uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1 )
|
67 |
{
|
68 |
BOOST_FOREACH(std::string name, std::make_pair(names.begin(),names.end())) {
|
69 |
book_t::const_iterator current = book.find(name);
|
70 |
if( current == book.end() ) {
|
71 |
TH1* histogram = book[name] = new TH3D((_title+"/"+name).c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup);
|
72 |
histogram->SetDirectory(0);
|
73 |
histogram->Sumw2();
|
74 |
static_cast<TH3*>(histogram)->Fill(X,Y,Z,W);
|
75 |
} else static_cast<TH3*>(current->second)->Fill(X,Y,Z,W);
|
76 |
}
|
77 |
}
|
78 |
|
79 |
class const_iterator
|
80 |
: public boost::iterator_facade< const_iterator, TH1* const, boost::bidirectional_traversal_tag, TH1* const> {
|
81 |
friend class boost::iterator_core_access;
|
82 |
|
83 |
std::map<std::string, TH1*>::const_iterator base, begin, end;
|
84 |
boost::regex expression;
|
85 |
|
86 |
void increment() { while( ++base!=end && !regex_match( base->first, expression) ); }
|
87 |
void decrement() { while( base!=begin && !regex_match((--base)->first, expression) ); }
|
88 |
bool equal(const const_iterator& rhs) const { return base==rhs.base; }
|
89 |
TH1* const dereference() const { return base->second; }
|
90 |
|
91 |
typedef std::map<std::string,TH1*>::const_iterator base_t;
|
92 |
public:
|
93 |
const_iterator(const base_t& base, const base_t& begin, const base_t& end, const boost::regex& expression )
|
94 |
: base(base), begin(begin), end(end), expression(expression) { if(base!=end && !regex_match(base->first, expression) ) increment();}
|
95 |
const std::string& name() {return base->first;}
|
96 |
typedef TH1* type;
|
97 |
};
|
98 |
|
99 |
//long count(const std::string& pattern) const;
|
100 |
};
|
101 |
|
102 |
#endif
|