ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/Book.h
Revision: 1.1
Committed: Mon Jul 20 12:21:17 2009 UTC (15 years, 9 months ago) by bbetchar
Content type: text/plain
Branch: MAIN
Log Message:
some tools for writing macros easier, inspiration for Book and poly is Sue Anne Koay's Parang

File Contents

# User Rev Content
1 bbetchar 1.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->Fill(X,W);
47     } else current->second->Fill(X,W);
48     }
49     }
50     void fill( double_t X, double_t Y, const poly<std::string>& names, uint_t NbinsX, double_t Xlow, double_t Xup,
51     uint_t NbinsY, double_t Ylow, double_t Yup, double_t W=1 )
52     {
53     BOOST_FOREACH(std::string name, std::make_pair(names.begin(),names.end())) {
54     book_t::const_iterator current = book.find(name);
55     if( current == book.end() ) {
56     TH1* histogram = book[name] = new TH2D((_title+"/"+name).c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup);
57     histogram->SetDirectory(0);
58     static_cast<TH2*>(histogram)->Fill(X,Y,W);
59     } else static_cast<TH2*>(current->second)->Fill(X,Y,W);
60     }
61     }
62     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,
63     uint_t NbinsY, double_t Ylow, double_t Yup,
64     uint_t NbinsZ, double_t Zlow, double_t Zup, double_t W=1 )
65     {
66     BOOST_FOREACH(std::string name, std::make_pair(names.begin(),names.end())) {
67     book_t::const_iterator current = book.find(name);
68     if( current == book.end() ) {
69     TH1* histogram = book[name] = new TH3D((_title+"/"+name).c_str(), "", NbinsX, Xlow, Xup, NbinsY, Ylow, Yup, NbinsZ, Zlow, Zup);
70     histogram->SetDirectory(0);
71     static_cast<TH3*>(histogram)->Fill(X,Y,Z,W);
72     } else static_cast<TH3*>(current->second)->Fill(X,Y,Z,W);
73     }
74     }
75    
76     class const_iterator
77     : public boost::iterator_facade< const_iterator, TH1* const, boost::bidirectional_traversal_tag, TH1* const> {
78     friend class boost::iterator_core_access;
79    
80     std::map<std::string, TH1*>::const_iterator base, begin, end;
81     boost::regex expression;
82    
83     void increment() { while( ++base!=end && !regex_match( base->first, expression) ); }
84     void decrement() { while( base!=begin && !regex_match((--base)->first, expression) ); }
85     bool equal(const const_iterator& rhs) const { return base==rhs.base; }
86     TH1* const dereference() const { return base->second; }
87    
88     typedef std::map<std::string,TH1*>::const_iterator base_t;
89     public:
90     const_iterator(const base_t& base, const base_t& begin, const base_t& end, const boost::regex& expression )
91     : base(base), begin(begin), end(end), expression(expression) { if(base!=end && !regex_match(base->first, expression) ) increment();}
92     const std::string& name() {return base->first;}
93     typedef TH1* type;
94     };
95    
96     //long count(const std::string& pattern) const;
97     };
98    
99     #endif