Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:19

0001 // -*- C++ -*-
0002 //
0003 // VariAxis.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef LWH_VariAxis_H
0010 #define LWH_VariAxis_H
0011 //
0012 // This is the declaration of the VariAxis class representing
0013 //
0014 
0015 
0016 #include <limits>
0017 #include <cmath>
0018 #include <algorithm>
0019 #include <map>
0020 #include "AIAxis.h"
0021 
0022 namespace LWH {
0023 
0024 using namespace AIDA;
0025 
0026 /**
0027  * An VariAxis represents a binned histogram axis. A 1D Histogram would have
0028  * one VariAxis representing the X axis, while a 2D Histogram would have two
0029  * axes representing the X and Y VariAxis.
0030  */
0031 class VariAxis: public IAxis {
0032 
0033 public:
0034 
0035   /**
0036    * Standard constructor.
0037    */
0038   VariAxis(const std::vector<double> & edges) {
0039     for ( int i = 0, N = edges.size(); i < N; ++i ) binco[edges[i]] = 0;
0040     std::map<double,int>::iterator it = binco.begin();
0041     for ( int i = 0, N = edges.size(); i < N; ++i ) (it++)->second = i;
0042   }
0043 
0044   /**
0045    * Copy constructor.
0046    */
0047   VariAxis(const VariAxis & a)
0048     : IAxis(a), binco(a.binco) {}
0049 
0050   /// Destructor.
0051   virtual ~VariAxis() { }
0052 
0053   /**
0054    * Check if the IAxis has fixed binning, i.e. if all the bins have
0055    * the same width.  @return <code>true</code> if the binning is
0056    * fixed, <code>false</code> otherwise.
0057    *
0058    */
0059   bool isFixedBinning() const {return false; }
0060 
0061   /**
0062    * Get the lower edge of the IAxis.
0063    * @return The IAxis's lower edge.
0064    *
0065    */
0066   double lowerEdge() const {
0067     if ( binco.size() ) return binco.begin()->first;
0068     return 0;
0069   }
0070 
0071   /**
0072    * Get the upper edge of the IAxis.
0073    * @return The IAxis's upper edge.
0074    *
0075    */
0076   double upperEdge() const {
0077     if ( !binco.size() ) return 0;
0078     std::map<double,int>::const_iterator last = binco.end();
0079     return (--last)->first;
0080   }
0081 
0082   /** 
0083    * The number of bins (excluding underflow and overflow) on the IAxis.
0084    * @return The IAxis's number of bins.
0085    *
0086    */
0087   int bins() const { return binco.size() - 1; }
0088 
0089   /**
0090    * Get the lower edge of the specified bin.
0091    * @param index The bin number: 0 to bins()-1 for the in-range bins
0092    * or OVERFLOW or UNDERFLOW.
0093    * @return The lower edge of the corresponding bin; for the
0094    * underflow bin this is <tt>Double.NEGATIVE_INFINITY</tt>.
0095    *
0096    */
0097   std::pair<double,double> binEdges(int index) const {
0098     std::pair<double,double> edges(0.0, 0.0);
0099     if ( !binco.size() ) return edges;
0100     std::map<double,int>::const_iterator lo = binco.end();
0101     std::map<double,int>::const_iterator up = binco.begin();
0102     if ( index >= 0 ) while ( index-- >= 0 && up != binco.end() ) lo = up++;
0103     edges.first = ( lo == binco.end() )? -std::numeric_limits<double>::max():
0104                                          lo->first;
0105     edges.second = ( up == binco.end() )? std::numeric_limits<double>::max():
0106                                          up->first;
0107     return edges;
0108   }
0109 
0110   /**
0111    * Get the lower edge of the specified bin.
0112    * @param index The bin number: 0 to bins()-1 for the in-range bins
0113    * or OVERFLOW or UNDERFLOW.
0114    * @return The lower edge of the corresponding bin; for the
0115    * underflow bin this is <tt>Double.NEGATIVE_INFINITY</tt>.
0116    *
0117    */
0118   double binLowerEdge(int index) const {
0119     return binEdges(index).first;
0120   }
0121 
0122   /**
0123    * Get the upper edge of the specified bin.
0124    * @param index The bin number: 0 to bins()-1 for the in-range bins
0125    * or OVERFLOW or UNDERFLOW.
0126    * @return The upper edge of the corresponding bin; for the overflow
0127    * bin this is <tt>Double.POSITIVE_INFINITY</tt>.
0128    *
0129    */ 
0130   double binUpperEdge(int index) const {
0131     return binEdges(index).second;
0132   }
0133 
0134   /**
0135    * Get the width of the specified bin.
0136    * @param index The bin number: 0 to bins()-1) for the in-range bins
0137    * or OVERFLOW or UNDERFLOW.
0138    * @return      The width of the corresponding bin.
0139    *
0140    */ 
0141   double binWidth(int index) const {
0142     std::pair<double,double> edges = binEdges(index);
0143     return edges.second - edges.first;
0144   }
0145 
0146   /**
0147    * Convert a coordinate on the axis to a bin number.  If the
0148    * coordinate is less than the lowerEdge UNDERFLOW is returned; if
0149    * the coordinate is greater or equal to the upperEdge OVERFLOW is
0150    * returned.
0151    * @param coord The coordinate to be converted.
0152    * @return      The corresponding bin number.
0153    *
0154    */
0155   int coordToIndex(double coord) const {
0156     std::map<double,int>::const_iterator up = binco.upper_bound(coord);
0157     if ( up == binco.begin() ) return UNDERFLOW_BIN;
0158     else if ( up == binco.end() ) return OVERFLOW_BIN;
0159     else return up->second - 1;
0160   }
0161 
0162   /**
0163    * Return the midpoint of the specified bin. No checking is
0164    * performed to ensure the argument is a valid bin.
0165    */
0166   double binMidPoint(int index) const {
0167     std::pair<double,double> edges = binEdges(index);
0168     return (edges.second + edges.first)/2.0;
0169   }
0170 
0171 private:
0172 
0173   /**
0174    * A map relating the lower edge of a bin to the corresponding bin
0175    * number.
0176    */
0177   std::map<double,int> binco;
0178 
0179 };
0180 
0181 }
0182 
0183 #endif /* LWH_VariAxis_H */