Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:09

0001 // @(#)root/hist:$Id$
0002 // Author: Lorenzo MOneta 11/2020
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_THistRange
0013 #define ROOT_THistRange
0014 
0015 //////////////////////////////////////////////////////////////////////////
0016 //                                                                      //
0017 // THistRange                                                           //
0018 //                                                                      //
0019 // Class defining a generic range of the histogram                      //
0020 // Used to iterated between bins                                        //
0021 //////////////////////////////////////////////////////////////////////////
0022 
0023 #include "TError.h"  // for R__ASSERT
0024 
0025 class TH1;
0026 
0027 
0028 
0029 class TBinIterator {
0030 private:
0031    int fBin;   ///< Global bin number used to advanced
0032    int fXbin;  ///< Bin X number
0033    int fYbin;  ///< Bin y number
0034    int fZbin;  ///< Bin Z number
0035 
0036    int fNx;    ///< Total x size (nbins+2)
0037    int fNy;    ///< y size
0038    int fNz;    ///< z size
0039    int fXmin;  ///< Min x value
0040    int fXmax;  ///< Max x value
0041    int fYmin;  ///< Min y value
0042    int fYmax;  ///< Max y value
0043    int fZmin;  ///< Min z value
0044    int fZmax;  ///< Max z value
0045 
0046    int fDim;   ///< Histogram dimension
0047 
0048    /// Compute global bin number given x,y,x bin numbers
0049    void SetGlobalBin()
0050    {
0051       if (fDim == 1)
0052          fBin = fXbin;
0053       else if (fDim == 2)
0054          fBin = fXbin + fNx * fYbin;
0055       else
0056          fBin = fXbin + fNx * (fYbin + fNy * fZbin);
0057    }
0058 
0059    // private default ctor (used by THistRange)
0060    TBinIterator()
0061       : fBin(0), fXbin(0), fYbin(0), fZbin(0), fNx(0), fNy(0), fNz(0), fXmin(0), fXmax(0), fYmin(0), fYmax(0), fZmin(0),
0062         fZmax(0), fDim(0)
0063    {
0064    }
0065 
0066 public:
0067    friend class THistRange;
0068 
0069    /// enum defining option range type:
0070    enum ERangeType {
0071       kHistRange, ///< use range provided by histogram
0072       kAxisBins,  ///< use allbins within axis limits (no underflow/overflows)
0073       kAllBins,   ///< use all bins including underflows/overflows
0074       kUnOfBins   ///< collection of all underflow/overflow bins
0075    };
0076 
0077    TBinIterator(const TH1 *h, ERangeType type);
0078    // TBinIterator(TBinIterator &rhs) = default;
0079    // TBinIterator &operator=(const TBinIterator &) = default;
0080 
0081    // implement end
0082    static TBinIterator End()
0083    {
0084       TBinIterator end;
0085       end.fBin = -1;
0086       return end;
0087    }
0088 
0089    // keep inline to be faster
0090    TBinIterator &operator++()
0091    {
0092       if (fXbin < fXmax)
0093          fXbin++;
0094       else if (fDim > 1) {
0095          fXbin = fXmin;
0096          if (fYbin < fYmax)
0097             fYbin++;
0098          else if (fDim > 2) {
0099             fYbin = fYmin;
0100             if (fZbin < fZmax)
0101                fZbin++;
0102             else {
0103                fBin = -1;
0104                return *this;
0105             }
0106          } else {
0107             R__ASSERT(fDim == 2);
0108             fBin = -1;
0109             return *this;
0110          }
0111       } else {
0112          R__ASSERT(fDim == 1);
0113          fBin = -1;
0114          return *this;
0115       }
0116       // fXbin can be incremented to zero only in case of TH2Poly
0117       // where you can start from negative bin numbers
0118       // In that case fXbin = 0 will be excluded
0119       if (fXbin == 0) {
0120          R__ASSERT(fXmin < 0 && fDim == 1);
0121          fXbin = 1; // this happens in case of TH2Poly
0122       }
0123       SetGlobalBin();
0124       return *this;
0125    }
0126 
0127    TBinIterator operator++(int)
0128    {
0129       TBinIterator tmp(*this);
0130       operator++();
0131       return tmp;
0132    }
0133 
0134    bool operator==(const TBinIterator &rhs) const { return fBin == rhs.fBin; }
0135    bool operator!=(const TBinIterator &rhs) const { return fBin != rhs.fBin; }
0136    int &operator*() { return fBin; }
0137 };
0138 
0139 class THistRange {
0140 
0141 public:
0142    typedef TBinIterator iterator;
0143    typedef TBinIterator::ERangeType RangeType;
0144 
0145    iterator begin() { return fBegin; }
0146 
0147    iterator end() { return fEnd; }
0148 
0149    THistRange(const TH1 *h1, TBinIterator::ERangeType type = TBinIterator::kHistRange);
0150 
0151 protected:
0152 
0153    TBinIterator fBegin;
0154    TBinIterator fEnd;
0155 };
0156 
0157 
0158 #endif