Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:19:38

0001 /*
0002  * This file is part of KFParticle package
0003  * Copyright (C) 2007-2019 FIAS Frankfurt Institute for Advanced Studies
0004  *               2007-2019 Goethe University of Frankfurt
0005  *               2007-2019 Ivan Kisel <I.Kisel@compeng.uni-frankfurt.de>
0006  *               2007-2019 Maksym Zyzak
0007  *
0008  * KFParticle is free software: you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation, either version 3 of the License, or
0011  * (at your option) any later version.
0012  *
0013  * KFParticle is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  * GNU General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU General Public License
0019  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0020  */
0021 
0022 #ifndef KFPHISTOGRAM1D
0023 #define KFPHISTOGRAM1D
0024 
0025 #include <vector>
0026 #include <string>
0027 #include <cmath>
0028 #include <iostream>
0029 #include <cstdlib>
0030 
0031 /** @class KFPHistogram1D
0032  ** @brief One-dimensional histogram.
0033  ** @author  M.Zyzak, I.Kisel
0034  ** @date 05.02.2019
0035  ** @version 1.0
0036  **
0037  ** The class is used to collect one-dimensional histograms in the environment,
0038  ** where ROOT is not available, for example at Intel Xeon Phi cards.
0039  ** It contains the histogram itself, number of bins, its name,
0040  ** minimum and maximum value of the axis. The histogram memory is allocated externally
0041  ** (by KFPHistogram) for better performance.
0042  **/
0043 
0044 class KFPHistogram1D
0045 {
0046  public:
0047    
0048   KFPHistogram1D(): fHistogram(0), fSize(0), fName(""), fMinBin(0), fMaxBin(0) {}
0049   KFPHistogram1D(std::string name, int nBins, float minX, float maxX):
0050     fHistogram(0), fSize(nBins+2), fName(name), fMinBin(minX), fMaxBin(maxX) {} ///< Constructor with user-defined parameters.
0051   ~KFPHistogram1D() {}
0052   
0053   int* GetHistogram() const { return fHistogram; } ///< Returns a pointer to the histogram data.
0054   std::string Name()  const { return fName; }      ///< Returns name of the histogram.
0055   float MinBin()      const { return fMinBin; }    ///< returns minimum of the X axis.
0056   float MaxBin()      const { return fMaxBin; }    ///< Returns maximum of the X axis.
0057   int   NBins()       const { return (fSize-2); }  ///< Returns number of bins.
0058   int   DataSize()    const { return fSize; }      ///< Returns number of bins plus underflow and overflow bins.
0059   int   Size()        const { return fSize; }      ///< Returns number of bins plus underflow and overflow bins.
0060   
0061   inline void SetBinContent(int iBin, int value) { fHistogram[iBin] = value; } ///< Sets the value of the bin "iBin".
0062   inline void SetHistogramMemory(int* pointer)   { fHistogram = pointer; }     ///< Sets the pointer to the memory with the histogram.
0063   
0064   /** Adds "value" to the histogram: calculates the corresponding bin and adds one there. */
0065   void Fill(float value)
0066   {
0067     int iBin = floor(float(value - fMinBin)/float(fMaxBin - fMinBin) * float(fSize-2)) + 1;
0068     
0069     if(iBin > fSize-1)
0070       iBin = fSize-1;
0071     if(iBin < 1)
0072       iBin = 0;
0073     
0074     if( !(iBin==iBin) || !(std::isfinite(iBin)) ) iBin = 0;
0075     
0076     fHistogram[iBin]++;    
0077   }
0078   
0079   /** Adds histogram "h" to the current histogram bin-by-bin. */
0080   inline void operator += ( const KFPHistogram1D &h )
0081   {
0082     if( fSize != h.fSize )
0083     {
0084       std::cout << "Size of 1D histogram " << fName << " is incorrect. Stop the program." << std::endl;
0085     }
0086     else
0087     {
0088       for(int i=0; i<fSize; i++)
0089         fHistogram[i] += h.fHistogram[i];
0090     }
0091   }
0092   
0093   /** The copy-constructor. Memory for the fHistogram is not allocated, only the pointer is copied.*/
0094   KFPHistogram1D(const KFPHistogram1D& h): fHistogram(h.fHistogram), fSize(h.fSize), fName(h.fName), fMinBin(h.fMinBin), fMaxBin(h.fMaxBin)
0095   {
0096   }
0097   
0098   /** Copies object "h" to the current object. Memory for the fHistogram is not allocated, only the pointer is copied. Returns the current object.  */
0099   const KFPHistogram1D& operator=(const KFPHistogram1D& h)
0100   {
0101     fHistogram = h.fHistogram;
0102     fSize = h.fSize;
0103     fName = h.fName;
0104     fMinBin = h.fMinBin;
0105     fMaxBin = h.fMaxBin;
0106     
0107     return *this;
0108   }
0109   
0110  private:
0111   int* fHistogram;   ///< Pointer to the array with the values of the histogram.
0112   int fSize;         ///< Number of bins +2, additional two bins are reserved for the underflow and overflow.
0113   
0114   std::string fName; ///< Name of the histogram.
0115   float fMinBin;     ///< Minimum value at the X axis.
0116   float fMaxBin;     ///< Maximum value at the X axis.
0117 };
0118 
0119 #endif