Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:44

0001 #ifndef XHistogram_h
0002 #define XHistogram_h
0003 
0004 #include <algorithm>
0005 #include <vector>
0006 #include <iostream>
0007 #include <stdexcept>
0008 #include <memory>
0009 
0010 #include <TH2F.h>
0011 #include <TH2I.h>
0012 
0013 class XHistogram {
0014 public:
0015   typedef TH2I ColorMap;
0016   typedef TH2F Histogram;
0017   typedef std::pair<double, double> Range;
0018 
0019 protected:
0020   double m_minDl;
0021   Range m_xRange;
0022   Range m_yRange;
0023   size_t m_xBins;
0024   size_t m_yBins;
0025   size_t m_size;
0026 
0027   std::vector<std::shared_ptr<Histogram> > m_histograms;
0028   std::shared_ptr<Histogram> m_normalization;
0029   std::shared_ptr<ColorMap> m_colormap;
0030   std::shared_ptr<Histogram> m_dummy;
0031 
0032 public:
0033   /// default CTOR
0034   XHistogram(void)
0035       : m_minDl(0.000001),
0036         m_xRange(),
0037         m_yRange(),
0038         m_xBins(),
0039         m_yBins(),
0040         m_size(),
0041         m_histograms(),
0042         m_normalization(),
0043         m_colormap(),
0044         m_dummy() {}
0045 
0046   // explicit CTOR
0047   XHistogram(size_t size, size_t bins_x, size_t bins_y, Range x, Range y, size_t zones, const std::vector<double>& max)
0048       : m_minDl(0.000001),
0049         m_xRange(x),
0050         m_yRange(y),
0051         m_xBins(bins_x),
0052         m_yBins(bins_y),
0053         m_size(size),
0054         m_histograms(m_size),
0055         m_normalization(),
0056         m_colormap() {
0057     // setup unnamed ROOT histograms
0058     for (size_t i = 0; i < m_size; ++i) {
0059       m_histograms[i].reset(new Histogram(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0060       m_histograms[i]->SetMinimum(0.);
0061       m_histograms[i]->SetMaximum(max[i]);
0062     }
0063     m_normalization.reset(new Histogram(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0064     m_colormap.reset(new ColorMap(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0065     m_colormap->SetMinimum(0);
0066     m_colormap->SetMaximum(zones);
0067     Histogram(nullptr, nullptr, 0, 0., 0., 0, 0., 0.);  // make ROOT "forget" about unnamed histograms
0068   }
0069 
0070   /// fill one point
0071   void fill(double x, double y, const std::vector<double>& weight, double norm);
0072 
0073   /// fill one point and set its color
0074   void fill(double x, double y, const std::vector<double>& weight, double norm, unsigned int colour);
0075 
0076   /// fill one segment, normalizing each bin's weight to the fraction of the segment it contains
0077   void fill(const Range& x, const Range& y, const std::vector<double>& weight, double norm);
0078 
0079   /// fill one segment and set its color, normalizing each bin's weight to the fraction of the segment it contains
0080   void fill(const Range& x, const Range& y, const std::vector<double>& weight, double norm, unsigned int colour);
0081 
0082   /// normalize the histograms
0083   void normalize(void);
0084 
0085   /// access one of the histograms
0086   Histogram* get(size_t h = 0) const {
0087     if (h < m_size)
0088       return (Histogram*)m_histograms[h]->Clone(nullptr);
0089     else
0090       return nullptr;
0091   }
0092 
0093   /// access the normalization
0094   Histogram* normalization(void) const { return (Histogram*)m_normalization->Clone(nullptr); }
0095 
0096   /// access the colormap
0097   ColorMap* colormap(void) const { return (ColorMap*)m_colormap->Clone(nullptr); }
0098 
0099   /// set the minimum length of sub-segment a segment should be split into:
0100   /// when splitting across bin boundaries with splitSegment(...), sub-segments shorter than this are skipped
0101   void setMinDl(double dl) { m_minDl = dl; }
0102 
0103 protected:
0104   struct position {
0105     double f;
0106     double x;
0107     double y;
0108 
0109     position() : f(0), x(0), y(0) {}
0110 
0111     position(double f_, double x_, double y_) : f(f_), x(x_), y(y_) {}
0112 
0113     bool operator<(const position& other) const { return f < other.f; }
0114   };
0115 
0116   /// split a segment into a vector of points
0117   std::vector<position> splitSegment(Range x, Range y) const;
0118 
0119   /// check the weights passed as an std::vector have the correct size
0120   void check_weight(const std::vector<double>& weight) noexcept(false) {
0121     // run time check for vector size
0122     if (weight.size() != m_size)
0123       throw std::invalid_argument("weight: wrong number of elements");
0124   }
0125 };
0126 
0127 #endif  // XHistogram_h