Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DD4hep_XHistogram_h
0002 #define DD4hep_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 DD4hep_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   DD4hep_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   DD4hep_XHistogram(
0048       size_t size, size_t bins_x, size_t bins_y, Range x, Range y, size_t zones, const std::vector<double>& max)
0049       : m_minDl(0.000001),
0050         m_xRange(x),
0051         m_yRange(y),
0052         m_xBins(bins_x),
0053         m_yBins(bins_y),
0054         m_size(size),
0055         m_histograms(m_size),
0056         m_normalization(),
0057         m_colormap() {
0058     // setup unnamed ROOT histograms
0059     for (size_t i = 0; i < m_size; ++i) {
0060       m_histograms[i].reset(new Histogram(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0061       m_histograms[i]->SetMinimum(0.);
0062       m_histograms[i]->SetMaximum(max[i]);
0063     }
0064     m_normalization.reset(new Histogram(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0065     m_colormap.reset(new ColorMap(nullptr, nullptr, bins_x, x.first, x.second, bins_y, y.first, y.second));
0066     m_colormap->SetMinimum(0);
0067     m_colormap->SetMaximum(zones);
0068     Histogram(nullptr, nullptr, 0, 0., 0., 0, 0., 0.);  // make ROOT "forget" about unnamed histograms
0069   }
0070 
0071   /// fill one point
0072   void fill(double x, double y, const std::vector<double>& weight, double norm);
0073 
0074   /// fill one point and set its color
0075   void fill(double x, double y, const std::vector<double>& weight, double norm, unsigned int colour);
0076 
0077   /// fill one segment, normalizing each bin's weight to the fraction of the segment it contains
0078   void fill(const Range& x, const Range& y, const std::vector<double>& weight, double norm);
0079 
0080   /// fill one segment and set its color, normalizing each bin's weight to the fraction of the segment it contains
0081   void fill(const Range& x, const Range& y, const std::vector<double>& weight, double norm, unsigned int colour);
0082 
0083   /// normalize the histograms
0084   void normalize(void);
0085 
0086   /// access one of the histograms
0087   Histogram* get(size_t h = 0) const {
0088     if (h < m_size)
0089       return (Histogram*)m_histograms[h]->Clone(nullptr);
0090     else
0091       return nullptr;
0092   }
0093 
0094   /// access the normalization
0095   Histogram* normalization(void) const { return (Histogram*)m_normalization->Clone(nullptr); }
0096 
0097   /// access the colormap
0098   ColorMap* colormap(void) const { return (ColorMap*)m_colormap->Clone(nullptr); }
0099 
0100   /// set the minimum length of sub-segment a segment should be split into:
0101   /// when splitting across bin boundaries with splitSegment(...), sub-segments shorter than this are skipped
0102   void setMinDl(double dl) { m_minDl = dl; }
0103 
0104 protected:
0105   struct position {
0106     double f;
0107     double x;
0108     double y;
0109 
0110     position() : f(0), x(0), y(0) {}
0111 
0112     position(const double& f_, const double& x_, const double& y_) : f(f_), x(x_), y(y_) {}
0113 
0114     bool operator<(const position& other) const { return f < other.f; }
0115   };
0116 
0117   /// split a segment into a vector of points
0118   std::vector<position> splitSegment(Range x, Range y) const;
0119 
0120   /// check the weights passed as an std::vector have the correct size
0121   void check_weight(const std::vector<double>& weight) noexcept(false) {
0122     // run time check for vector size
0123     if (weight.size() != m_size)
0124       throw std::invalid_argument("weight: wrong number of elements");
0125   }
0126 };
0127 
0128 #endif  // DD4hep_XHistogram_h