ATLAS Offline Software
LWBinUtils.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Header file for class LWBinUtils //
9 // //
10 // Description: Helper methods for binning related //
11 // calculations. //
12 // //
13 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
14 // Initial version: July 2009 //
15 // //
17 
18 #ifndef LWBINUTILS_H
19 #define LWBINUTILS_H
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <cmath>
24 #include "LWHistBitUtils.h"
25 #ifndef NDEBUG
26 #include <iostream>
27 #include <climits>
28 #endif
29 
30 class LWBinUtils {
31 public:
32 
33  static unsigned valueToBin( const double& x, const float*varBinnings,
34  const double& invDelta,
35  const double& xmin,const double& xmax,
36  unsigned nbinsplus1 );
37  static double getBinCenter( int bin, const float*varBinnings,
38  const double& invDelta, const double& xmin,
39  unsigned nbinsplus1);
40 
41  static unsigned internal_bin(unsigned binx, unsigned biny, unsigned nbinsxPlus2);
42  static void unpack_internal_bin(unsigned internalbin, unsigned& binx, unsigned& biny, unsigned nbinsxPlus2);
43 
44 private:
45 
46  //We don't inline this, since we want to keep the inlined valueToBin
47  //code-size small:
48  static unsigned valueToVarBin( const double&x,
49  const float*varBinnings,
50  unsigned nbinsplus1 );
51 
54 };
55 
57 // INLINES //
59 
60 inline unsigned LWBinUtils::valueToBin( const double& x, const float*varBinnings,
61  const double& invDelta,const double& xmin,const double& xmax,
62  unsigned nbinsplus1 ) {
63 #ifdef LW_STRICT_ROOT_BEHAVIOUR
64  //In ROOT, NaN's go in overflow bin.
65  if (x!=x)
66  return nbinsplus1;
67 #endif
68 #ifndef NDEBUG
69  if (std::isnan(x)) {
70  std::cout<<"LWHisto ERROR: Saw NaN in input axis position"<<std::endl;
71  return USHRT_MAX;
72  }
73 #endif
74  assert(!(x!=x));
75  if (x<xmin)
76  return 0;
77  if (x>xmax)
78  return nbinsplus1;
79  if (!varBinnings)
80  return static_cast<unsigned>(LWHistBitUtils::minZero(std::min<int>(nbinsplus1,int(1+invDelta*(x-xmin)))));
81  return valueToVarBin(x,varBinnings,nbinsplus1);
82 }
83 
84 
85 inline double LWBinUtils::getBinCenter( int bin, const float*varBinnings,
86  const double& invDelta, const double& xmin,
87  unsigned nbinsplus1)
88 {
89  if (!varBinnings || bin<1 || bin>=int(nbinsplus1))
90  return xmin + (bin-0.5)/invDelta;
91  else
92  return 0.5*(varBinnings[bin-1] + varBinnings[bin]);
93 }
94 
95 inline unsigned LWBinUtils::internal_bin( unsigned binx,
96  unsigned biny,
97  unsigned nbinsxPlus2 )
98 {
99  //Same mapping as in ROOT (mapping is important for copyContents):
100  assert(binx<nbinsxPlus2);
101  return biny * (nbinsxPlus2) + binx;
102 }
103 
104 inline void LWBinUtils::unpack_internal_bin( unsigned internalbin,
105  unsigned& binx,
106  unsigned& biny,
107  unsigned nbinsxPlus2 )
108 {
109  binx = internalbin % nbinsxPlus2;
110  biny = (internalbin-binx) / nbinsxPlus2;
111  assert(internal_bin(binx,biny,nbinsxPlus2)==internalbin);
112 }
113 
114 #endif
LWHistBitUtils.h
bin
Definition: BinsDiffFromStripMedian.h:43
LWBinUtils::unpack_internal_bin
static void unpack_internal_bin(unsigned internalbin, unsigned &binx, unsigned &biny, unsigned nbinsxPlus2)
Definition: LWBinUtils.h:104
x
#define x
LWBinUtils::valueToVarBin
static unsigned valueToVarBin(const double &x, const float *varBinnings, unsigned nbinsplus1)
Definition: LWBinUtils.cxx:19
xmin
double xmin
Definition: listroot.cxx:60
LWBinUtils::valueToBin
static unsigned valueToBin(const double &x, const float *varBinnings, const double &invDelta, const double &xmin, const double &xmax, unsigned nbinsplus1)
Definition: LWBinUtils.h:60
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
LWBinUtils::getBinCenter
static double getBinCenter(int bin, const float *varBinnings, const double &invDelta, const double &xmin, unsigned nbinsplus1)
Definition: LWBinUtils.h:85
LWHistBitUtils::minZero
static int minZero(int x)
Definition: LWHistBitUtils.h:49
xmax
double xmax
Definition: listroot.cxx:61
LWBinUtils::~LWBinUtils
~LWBinUtils()
LWBinUtils
Definition: LWBinUtils.h:30
LWBinUtils::LWBinUtils
LWBinUtils()
LWBinUtils::internal_bin
static unsigned internal_bin(unsigned binx, unsigned biny, unsigned nbinsxPlus2)
Definition: LWBinUtils.h:95