CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimTracker/TrackerMaterialAnalysis/plugins/XHistogram.h

Go to the documentation of this file.
00001 #ifndef XHistogram_h
00002 #define XHistogram_h
00003 
00004 #include <algorithm>
00005 #include <vector>
00006 #include <iostream>
00007 #include <stdexcept>
00008 #include <boost/shared_ptr.hpp>
00009 
00010 #include <TH2F.h>
00011 #include <TH2I.h>
00012 
00013 class XHistogram 
00014 {
00015 public:
00016   typedef TH2I                      ColorMap;
00017   typedef TH2F                      Histogram;
00018   typedef std::pair<double, double> Range;
00019 
00020 protected:
00021   double m_minDl;
00022   Range  m_xRange;
00023   Range  m_yRange;
00024   size_t m_xBins;
00025   size_t m_yBins;
00026   size_t m_size;
00027 
00028   std::vector< boost::shared_ptr<Histogram> > m_histograms;
00029   boost::shared_ptr<Histogram> m_normalization;
00030   boost::shared_ptr<ColorMap>  m_colormap;
00031   boost::shared_ptr<Histogram> m_dummy;
00032 
00033 public:
00035   XHistogram(void) :
00036     m_minDl( 0.000001 ),
00037     m_xRange(),
00038     m_yRange(),
00039     m_xBins(),
00040     m_yBins(),
00041     m_size(),
00042     m_histograms(),
00043     m_normalization(),
00044     m_colormap(),
00045     m_dummy()
00046   { }
00047  
00048   // explicit CTOR
00049   XHistogram( size_t size, size_t bins_x, size_t bins_y, Range x, Range y, size_t zones, std::vector<double> max ) :
00050     m_minDl( 0.000001 ),
00051     m_xRange( x ),
00052     m_yRange( y ),
00053     m_xBins( bins_x ),
00054     m_yBins( bins_y ),
00055     m_size( size ),
00056     m_histograms( m_size ),
00057     m_normalization(),
00058     m_colormap()
00059   {
00060     // setup unnamed ROOT histograms
00061     for (size_t i = 0; i < m_size; ++i) {
00062       m_histograms[i].reset(new Histogram( 0, 0, bins_x, x.first, x.second, bins_y, y.first, y.second ));
00063       m_histograms[i]->SetMinimum( 0. );
00064       m_histograms[i]->SetMaximum( max[i] );
00065     }
00066     m_normalization.reset(new Histogram( 0, 0, bins_x, x.first, x.second, bins_y, y.first, y.second ));
00067     m_colormap.reset(new ColorMap( 0, 0, bins_x, x.first, x.second, bins_y, y.first, y.second ));
00068     m_colormap->SetMinimum( 0 );
00069     m_colormap->SetMaximum( zones );
00070     Histogram( 0, 0, 0, 0., 0., 0, 0., 0. );        // make ROOT "forget" about unnamed histograms
00071   }
00072 
00074   void fill( double x, double y, const std::vector<double> & weight, double norm );
00075  
00077   void fill( double x, double y, const std::vector<double> & weight, double norm, unsigned int colour );
00078     
00080   void fill( const Range& x, const Range& y, const std::vector<double> & weight, double norm );
00081     
00083   void fill( const Range& x, const Range& y, const std::vector<double> & weight, double norm, unsigned int colour );
00084 
00086   void normalize(void);
00087 
00089   Histogram * get(size_t h = 0) const
00090   {
00091     if (h < m_size)
00092       return (Histogram *) m_histograms[h]->Clone(0);
00093     else
00094       return 0;
00095   }
00096 
00098   Histogram * normalization(void) const
00099   {
00100     return (Histogram *) m_normalization->Clone(0);
00101   }
00102 
00104   ColorMap * colormap(void) const
00105   {
00106     return (ColorMap *) m_colormap->Clone(0);
00107   }
00108 
00111   void setMinDl( double dl )
00112   {
00113     m_minDl = dl;
00114   }
00115  
00116 protected:
00117   struct position {
00118     double f;
00119     double x;
00120     double y;
00121 
00122     position() : f(0), x(0), y(0) { }
00123 
00124     position(double f_, double x_, double y_) : f(f_), x(x_), y(y_) { }
00125 
00126     bool operator<(const position& other) const {
00127       return f < other.f;
00128     }
00129   };
00130 
00132   std::vector<position> splitSegment( Range x, Range y ) const;
00133 
00135   void check_weight(const std::vector<double> & weight) throw (std::invalid_argument)
00136   {
00137     // run time check for vector size
00138     if (weight.size() != m_size)
00139        throw std::invalid_argument("weight: wrong number of elements");
00140   }
00141 
00142 };
00143 
00144 #endif // XHistogram_h