Alien-libhisto

 view release on metacpan or  search on metacpan

bundled/include/histo/histo2d.h  view on Meta::CPAN

 * @param[in] xmax   Upper bound along X.
 * @param[in] ny     Number of bins along Y.
 * @param[in] yedges Array of (ny + 1) monotonic edge values.
 * @param[in] flags  Feature flags.
 * @return Initialized handle, or NULL on error.
 */
histo2d_t* histo2d_create_uniform_variable(uint32_t nx, double xmin, double xmax,
                                           uint32_t ny, const double *yedges,
                                           uint32_t flags);

/**
 * @brief Creates a Variable-Uniform 2D histogram (Variable X, Uniform Y).
 *
 * @param[in] nx     Number of bins along X.
 * @param[in] xedges Array of (nx + 1) monotonic edge values.
 * @param[in] ny     Number of equidistant Y bins.
 * @param[in] ymin   Lower bound along Y.
 * @param[in] ymax   Upper bound along Y.
 * @param[in] flags  Feature flags.
 * @return Initialized handle, or NULL on error.
 */
histo2d_t* histo2d_create_variable_uniform(uint32_t nx, const double *xedges,
                                           uint32_t ny, double ymin, double ymax,
                                           uint32_t flags);

/**
 * @brief Destroys a 2D histogram and releases all allocated memory.
 * Safe to call with NULL.
 *
 * @param[in,out] h 2D histogram handle to destroy.
 */
void histo2d_destroy(histo2d_t *h);

/**
 * @brief Clones a 2D histogram, optionally zeroing bin contents and moments.
 *
 * @param[in] src   Source 2D histogram to clone.
 * @param[in] empty If true, allocate identical axis geometry with cleared bins.
 * @return Cloned handle, or NULL on error.
 */
histo2d_t* histo2d_clone(const histo2d_t *src, bool empty);

/**
 * @brief Resets all bin contents, guard region counters, and moments to zero.
 *
 * @param[in,out] h 2D histogram handle.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_reset(histo2d_t *h);

/* ========================================================================= */
/* Ingestion / Filling                                                       */
/* ========================================================================= */

/**
 * @brief Fills a single coordinate pair (x, y) with unit weight (1.0).
 *
 * @param[in,out] h 2D histogram handle.
 * @param[in]     x Sample X coordinate.
 * @param[in]     y Sample Y coordinate.
 * @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x or y is NaN/Inf.
 */
histo_status_t histo2d_fill(histo2d_t *h, double x, double y);

/**
 * @brief Fills a single coordinate pair (x, y) with specified weight.
 *
 * @param[in,out] h      2D histogram handle.
 * @param[in]     x      Sample X coordinate.
 * @param[in]     y      Sample Y coordinate.
 * @param[in]     weight Event weight.
 * @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x, y, or weight is NaN/Inf.
 */
histo_status_t histo2d_fill_w(histo2d_t *h, double x, double y, double weight);

/**
 * @brief Batch fills N coordinate pairs with optional weights (SoA format).
 *
 * @param[in,out] h       2D histogram handle.
 * @param[in]     n       Number of samples to ingest.
 * @param[in]     x       Contiguous array of X coordinates.
 * @param[in]     y       Contiguous array of Y coordinates.
 * @param[in]     weights Optional contiguous array of weights (NULL for unit weights).
 * @return HISTO_OK on success, or HISTO_WARN_NON_FINITE if non-finite samples skipped.
 */
histo_status_t histo2d_fill_n(histo2d_t *h, size_t n,
                              const double *x, const double *y,
                              const double *weights);

/**
 * @brief Strided batch fill for Array-of-Structs (AoS) data layouts.
 *
 * @param[in,out] h               2D histogram handle.
 * @param[in]     n               Number of samples.
 * @param[in]     x               Pointer to first sample X coordinate.
 * @param[in]     x_stride_bytes  Byte stride between consecutive X values.
 * @param[in]     y               Pointer to first sample Y coordinate.
 * @param[in]     y_stride_bytes  Byte stride between consecutive Y values.
 * @param[in]     weights         Optional pointer to first weight (NULL for unit weights).
 * @param[in]     w_stride_bytes  Byte stride between consecutive weights.
 * @return HISTO_OK on success, or HISTO_WARN_NON_FINITE if non-finite samples skipped.
 */
histo_status_t histo2d_fill_strided(histo2d_t *h, size_t n,
                                    const double *x, size_t x_stride_bytes,
                                    const double *y, size_t y_stride_bytes,
                                    const double *weights, size_t w_stride_bytes);

/**
 * @brief Directly accumulates weight into a specific 2D bin cell (ix, iy).
 *
 * @param[in,out] h      2D histogram handle.
 * @param[in]     ix     X bin index [0, nx - 1].
 * @param[in]     iy     Y bin index [0, ny - 1].
 * @param[in]     weight Weight to accumulate.
 * @return HISTO_OK on success, or HISTO_ERR_OUT_OF_RANGE if indices out of bounds.
 */
histo_status_t histo2d_fill_bin(histo2d_t *h, uint32_t ix, uint32_t iy, double weight);

/* ========================================================================= */
/* Geometry & Bin Query                                                      */
/* ========================================================================= */

/** @brief Returns the number of X bins */
uint32_t histo2d_nbins_x(const histo2d_t *h);

/** @brief Returns the number of Y bins */
uint32_t histo2d_nbins_y(const histo2d_t *h);

/** @brief Retrieves X axis description */
histo_status_t histo2d_axis_x(const histo2d_t *h, histo2d_axis_t *out_axis);

/** @brief Retrieves Y axis description */
histo_status_t histo2d_axis_y(const histo2d_t *h, histo2d_axis_t *out_axis);

/**
 * @brief Locates the (ix, iy) bin indices for coordinate pair (x, y).
 *
 * @param[in]  h      2D histogram handle.
 * @param[in]  x      X coordinate.
 * @param[in]  y      Y coordinate.
 * @param[out] out_ix Output X bin index ([0, nx-1] on hit, -1 underflow, nx overflow).
 * @param[out] out_iy Output Y bin index ([0, ny-1] on hit, -1 underflow, ny overflow).
 * @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x or y is NaN.
 */
histo_status_t histo2d_find_bin(const histo2d_t *h, double x, double y,
                                int64_t *out_ix, int64_t *out_iy);

/**
 * @brief Identifies which of the 9 geometric regions (x, y) belongs to.
 *
 * @param[in]  h          2D histogram handle.
 * @param[in]  x          X coordinate.
 * @param[in]  y          Y coordinate.
 * @param[out] out_region Output region classification enum.
 * @return HISTO_OK on success, or HISTO_ERR_NON_FINITE if x or y is NaN.
 */
histo_status_t histo2d_find_region(const histo2d_t *h, double x, double y,
                                   histo2d_region_t *out_region);

/**
 * @brief Retrieves the accumulated weight in cell (ix, iy).
 *
 * @param[in]  h          2D histogram handle.
 * @param[in]  ix         X bin index [0, nx - 1].
 * @param[in]  iy         Y bin index [0, ny - 1].
 * @param[out] out_weight Output pointer for accumulated weight.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_bin_content(const histo2d_t *h, uint32_t ix, uint32_t iy,
                                   double *out_weight);

/**
 * @brief Retrieves statistical uncertainty (standard error) in cell (ix, iy).
 *
 * @param[in]  h         2D histogram handle.
 * @param[in]  ix        X bin index [0, nx - 1].
 * @param[in]  iy        Y bin index [0, ny - 1].
 * @param[out] out_error Output pointer for uncertainty.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_bin_error(const histo2d_t *h, uint32_t ix, uint32_t iy,
                                 double *out_error);

/**
 * @brief Retrieves the sum of squared weights sum(w^2) in cell (ix, iy).
 *
 * @param[in]  h          2D histogram handle.
 * @param[in]  ix         X bin index [0, nx - 1].
 * @param[in]  iy         Y bin index [0, ny - 1].
 * @param[out] out_sum_w2 Output pointer for sum_w2.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_bin_sum_w2(const histo2d_t *h, uint32_t ix, uint32_t iy,
                                  double *out_sum_w2);

/**
 * @brief Retrieves the 2D bounding box [xmin, xmax] x [ymin, ymax] of cell (ix, iy).
 *
 * @param[in]  h        2D histogram handle.
 * @param[in]  ix       X bin index [0, nx - 1].
 * @param[in]  iy       Y bin index [0, ny - 1].
 * @param[out] out_xmin Output pointer for X lower edge.
 * @param[out] out_xmax Output pointer for X upper edge.
 * @param[out] out_ymin Output pointer for Y lower edge.
 * @param[out] out_ymax Output pointer for Y upper edge.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_bin_bounds(const histo2d_t *h, uint32_t ix, uint32_t iy,
                                  double *out_xmin, double *out_xmax,
                                  double *out_ymin, double *out_ymax);

/**
 * @brief Retrieves the midpoint center (cx, cy) of cell (ix, iy).
 *
 * @param[in]  h      2D histogram handle.
 * @param[in]  ix     X bin index [0, nx - 1].
 * @param[in]  iy     Y bin index [0, ny - 1].
 * @param[out] out_cx Output pointer for X center.
 * @param[out] out_cy Output pointer for Y center.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_bin_center(const histo2d_t *h, uint32_t ix, uint32_t iy,
                                  double *out_cx, double *out_cy);

/**
 * @brief Retrieves the accumulated weight and count for a specific guard region.
 *
 * @param[in]  h          2D histogram handle.
 * @param[in]  region     Target region enum.
 * @param[out] out_weight Output pointer for accumulated weight.
 * @param[out] out_count  Output pointer for fill count.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_region_content(const histo2d_t *h, histo2d_region_t region,
                                      double *out_weight, uint64_t *out_count);

/**
 * @brief Retrieves accumulated squared weights (sum of w^2) for a specific guard region.
 *
 * @param[in] h      2D histogram handle.
 * @param[in] region Target region enum.
 * @return Region sum_w2, or 0.0 if h is NULL, region is invalid, or sum_w2 is not tracked.
 */
double histo2d_region_sum_w2(const histo2d_t *h, histo2d_region_t region);

/**
 * @brief Retrieves the feature configuration flags bitmask for this 2D histogram.
 *
 * @param[in] h 2D histogram handle.
 * @return Bitmask of HISTO_FLAG_* flags, or 0 if h is NULL.
 */
uint32_t histo2d_flags(const histo2d_t *h);

/**
 * @brief Retrieves total number of rejected non-finite (NaN/Inf) fill samples.
 *
 * @param[in] h 2D histogram handle.
 * @return Count of non-finite events.
 */
uint64_t histo2d_nan_count(const histo2d_t *h);

/**
 * @brief Retrieves total in-range accumulated weight.
 *
 * @param[in] h 2D histogram handle.
 * @return Total weight across all in-range cells.
 */
double histo2d_total_weight(const histo2d_t *h);

/**
 * @brief Retrieves total in-range fill entries.
 *
 * @param[in] h 2D histogram handle.
 * @return In-range entry count.
 */
uint64_t histo2d_num_entries(const histo2d_t *h);

/* ========================================================================= */
/* Statistical Moments & Analysis                                            */
/* ========================================================================= */

/** @brief Retrieves the mean along the X axis */
histo_status_t histo2d_mean_x(const histo2d_t *h, double *out_mean_x);

/** @brief Retrieves the mean along the Y axis */
histo_status_t histo2d_mean_y(const histo2d_t *h, double *out_mean_y);

/** @brief Retrieves the sample variance along the X axis */
histo_status_t histo2d_variance_x(const histo2d_t *h, double *out_var_x);

/** @brief Retrieves the sample variance along the Y axis */
histo_status_t histo2d_variance_y(const histo2d_t *h, double *out_var_y);

/** @brief Retrieves the sample standard deviation along the X axis */
histo_status_t histo2d_std_dev_x(const histo2d_t *h, double *out_std_x);

/** @brief Retrieves the sample standard deviation along the Y axis */
histo_status_t histo2d_std_dev_y(const histo2d_t *h, double *out_std_y);

/**
 * @brief Retrieves sample covariance Cov(X, Y).
 *
 * @param[in]  h       2D histogram handle.
 * @param[out] out_cov Output pointer for covariance.
 * @return HISTO_OK on success, or error code.
 */
histo_status_t histo2d_covariance(const histo2d_t *h, double *out_cov);

/**
 * @brief Retrieves Pearson correlation coefficient rho_xy = Cov(X,Y)/(sigma_x * sigma_y).
 *
 * @param[in]  h       2D histogram handle.
 * @param[out] out_rho Output pointer for correlation coefficient.
 * @return HISTO_OK on success, or error code.
 */



( run in 1.010 second using v1.01-cache-2.11-cpan-60884a8df76 )