lsst.jointcal  14.0-12-gc7bdbdc
Histo2d.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <math.h> /* for floor */
3 #include <string.h> /* for memset*/
4 
5 #include "lsst/log/Log.h"
7 
8 namespace {
9 LOG_LOGGER _log = LOG_GET("jointcal.Histo2d");
10 }
11 
12 namespace lsst {
13 namespace jointcal {
14 
15 Histo2d::Histo2d(int nnx, float mminx, float mmaxx, int nny, float mminy, float mmaxy) {
16  nx = nnx;
17  ny = nny;
18  minx = mminx;
19  miny = mminy;
20  if (mmaxx != mminx)
21  scalex = nx / (mmaxx - mminx);
22  else {
23  LOGL_WARN(_log, "Histo2d: minx = maxx requested");
24  scalex = 1.0;
25  }
26  if (mmaxy != mminy)
27  scaley = ny / (mmaxy - mminy);
28  else {
29  LOGL_WARN(_log, "Histo2d: maxy = miny requested");
30  scaley = 1.0;
31  }
32  data.reset(new float[nx * ny]);
33  memset(data.get(), 0, nx * ny * sizeof(float));
34 }
35 
36 Histo2d::Histo2d(const Histo2d &other) {
37  memcpy(this, &other, sizeof(Histo2d));
38  data.reset(new float[nx * ny]);
39  memcpy((data).get(), other.data.get(), nx * ny * sizeof(float));
40 }
41 
42 bool Histo2d::indices(double x, double y, int &ix, int &iy) const {
43  ix = (int)floor((x - minx) * scalex);
44  if (ix < 0 || ix >= nx) return false;
45  iy = (int)floor((y - miny) * scaley);
46  return (iy >= 0 && iy < ny);
47 }
48 
49 void Histo2d::fill(float x, float y, float weight) {
50  int ix, iy;
51  if (indices(x, y, ix, iy)) data[iy + ny * ix] += weight;
52 }
53 
54 double Histo2d::maxBin(double &x, double &y) const {
55  float *p, *pend;
56  int imax = 0;
57  float valmax = -1e30;
58 
59  for (p = data.get(), pend = p + nx * ny; pend - p; p++) {
60  if (*p > valmax) {
61  valmax = *p;
62  imax = p - (data.get());
63  }
64  }
65  int ix = imax / ny;
66  int iy = imax - ix * ny;
67  x = minx + ((float)ix + 0.5) / scalex;
68  y = miny + ((float)iy + 0.5) / scaley;
69  return valmax;
70 }
71 
72 void Histo2d::zeroBin(double x, double y) {
73  int ix, iy;
74  if (indices(x, y, ix, iy)) data[iy + ny * ix] = 0;
75 }
76 
77 double Histo2d::binContent(double x, double y) const {
78  int ix, iy;
79  if (indices(x, y, ix, iy)) return data[iy + ny * ix];
80  LOGL_WARN(_log, "Histo2D::binContent outside limits requested");
81  return -1e30;
82 }
83 } // namespace jointcal
84 } // namespace lsst
int y
double binContent(double x, double y) const
Definition: Histo2d.cc:77
void fill(float x, float y, float weight=1.)
Definition: Histo2d.cc:49
void zeroBin(double x, double y)
Definition: Histo2d.cc:72
T memset(T... args)
T floor(T... args)
double maxBin(double &x, double &y) const
Definition: Histo2d.cc:54
Class for a simple mapping implementing a generic Gtransfo.
T memcpy(T... args)
T reset(T... args)
#define LOGL_WARN(logger, message...)
T get(T... args)
double x
#define LOG_GET(logger)