Skip to content

File radialprojectionrebinner.h

File List > code_source > templat > include > TempLat > lattice > measuringtools > projectionhelpers > radialprojectionrebinner.h

Go to the documentation of this file

#ifndef TEMPLAT_LATTICE_MEASUREMENTS_PROJECTIONHELPERS_RADIALPROJECTIONREBINNER_H
#define TEMPLAT_LATTICE_MEASUREMENTS_PROJECTIONHELPERS_RADIALPROJECTIONREBINNER_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Wessel Valkenburg, Year: 2019

#include <vector>

#include "TempLat/lattice/measuringtools/projectionhelpers/radialbincomputer.h"
#include "TempLat/lattice/measuringtools/projectionhelpers/radialprojectionsinglebinandvalue.h"

namespace TempLat
{
  template <typename T> class RadialProjectionRebinner
  {
  public:
    static std::vector<RadialProjectionSingleBinAndValue<T>>
    rebin(const std::vector<RadialProjectionSingleBinAndValue<T>> &old, device::Idx newNBins,
          std::vector<T> &oldCentralValues, T customRange)
    {
      std::vector<RadialProjectionSingleBinAndValue<T>> result;
      if (newNBins >= (device::Idx)old.size()) {
        result = old;
      } else {
        T minVal = old[0].getBin().minVal;
        T maxVal = old.back().getBin().maxVal;

        // say << maxVal;
        // say << customRange;

        const T effectiveMax = customRange < 0 ? maxVal : customRange;
        T deltaKBin = (effectiveMax - minVal) / (T)newNBins;
        if (!(deltaKBin > 0)) deltaKBin = 1; // guard against zero/negative width (coincident bounds)
        RadialBinComputer pc(minVal, effectiveMax, newNBins, deltaKBin);
        result.clear();
        result.resize(newNBins);
        for (auto &&it : old) {
          /* just take the central value for the bin indexing. */
          result[pc(it.getBin().average)].combineTo(it);
        }
        pc.setCentralBinBounds(oldCentralValues);
      }
      return result;
    }

  private:
    RadialProjectionRebinner() = default;
  };
} // namespace TempLat

#endif