Blame ntuple/ntuple.c

Packit 67cb25
/* histogram/ntuple.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 2000 Simone Piccardi
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
/* Jan/2001 Modified by Brian Gough. Minor changes for GSL */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <errno.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_ntuple.h>
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_open:
Packit 67cb25
 * Initialize an ntuple structure and create the related file
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
gsl_ntuple *
Packit 67cb25
gsl_ntuple_create (char *filename, void *ntuple_data, size_t size)
Packit 67cb25
{
Packit 67cb25
  gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
Packit 67cb25
Packit 67cb25
  if (ntuple == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
Packit 67cb25
                     GSL_ENOMEM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  ntuple->ntuple_data = ntuple_data;
Packit 67cb25
  ntuple->size = size;
Packit 67cb25
Packit 67cb25
  ntuple->file = fopen (filename, "wb");
Packit 67cb25
Packit 67cb25
  if (ntuple->file == 0)
Packit 67cb25
    {
Packit 67cb25
      free (ntuple);
Packit 67cb25
      GSL_ERROR_VAL ("unable to create ntuple file", GSL_EFAILED, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return ntuple;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_open:
Packit 67cb25
 * Initialize an ntuple structure and open the related file
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
gsl_ntuple *
Packit 67cb25
gsl_ntuple_open (char *filename, void *ntuple_data, size_t size)
Packit 67cb25
{
Packit 67cb25
  gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
Packit 67cb25
Packit 67cb25
  if (ntuple == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
Packit 67cb25
                     GSL_ENOMEM, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  ntuple->ntuple_data = ntuple_data;
Packit 67cb25
  ntuple->size = size;
Packit 67cb25
Packit 67cb25
  ntuple->file = fopen (filename, "rb");
Packit 67cb25
Packit 67cb25
  if (ntuple->file == 0)
Packit 67cb25
    {
Packit 67cb25
      free (ntuple);
Packit 67cb25
      GSL_ERROR_VAL ("unable to open ntuple file for reading", 
Packit 67cb25
                     GSL_EFAILED, 0);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return ntuple;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_write:
Packit 67cb25
 * write to file a data row, must be used in a loop!
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ntuple_write (gsl_ntuple * ntuple)
Packit 67cb25
{
Packit 67cb25
  size_t nwrite;
Packit 67cb25
Packit 67cb25
  nwrite = fwrite (ntuple->ntuple_data, ntuple->size,
Packit 67cb25
                   1, ntuple->file);
Packit 67cb25
Packit 67cb25
  if (nwrite != 1)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("failed to write ntuple entry to file", GSL_EFAILED);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* the following function is a synonym for gsl_ntuple_write */
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ntuple_bookdata (gsl_ntuple * ntuple)
Packit 67cb25
{
Packit 67cb25
  return gsl_ntuple_write (ntuple);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_read:
Packit 67cb25
 * read form file a data row, must be used in a loop!
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ntuple_read (gsl_ntuple * ntuple)
Packit 67cb25
{
Packit 67cb25
  size_t nread;
Packit 67cb25
Packit 67cb25
  nread = fread (ntuple->ntuple_data, ntuple->size, 1, ntuple->file);
Packit 67cb25
Packit 67cb25
  if (nread == 0 && feof(ntuple->file))
Packit 67cb25
    {
Packit 67cb25
      return GSL_EOF;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (nread != 1)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("failed to read ntuple entry from file", GSL_EFAILED);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_project:
Packit 67cb25
 * fill an histogram with an ntuple file contents, use
Packit 67cb25
 * SelVal and SelFunc user defined functions to get 
Packit 67cb25
 * the value to book and the selection funtion
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#define EVAL(f,x) ((*((f)->function))(x,(f)->params))
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple,
Packit 67cb25
                    gsl_ntuple_value_fn * value_func, 
Packit 67cb25
                    gsl_ntuple_select_fn * select_func)
Packit 67cb25
{
Packit 67cb25
  size_t nread;
Packit 67cb25
Packit 67cb25
  do
Packit 67cb25
    {
Packit 67cb25
      nread = fread (ntuple->ntuple_data, ntuple->size,
Packit 67cb25
                     1, ntuple->file);
Packit 67cb25
Packit 67cb25
      if (nread == 0 && feof(ntuple->file))
Packit 67cb25
        {
Packit 67cb25
          break ;
Packit 67cb25
        }
Packit 67cb25
      
Packit 67cb25
      if (nread != 1) 
Packit 67cb25
        {
Packit 67cb25
          GSL_ERROR ("failed to read ntuple for projection", GSL_EFAILED);
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (EVAL(select_func, ntuple->ntuple_data))
Packit 67cb25
        {
Packit 67cb25
          gsl_histogram_increment (h, EVAL(value_func, ntuple->ntuple_data));
Packit 67cb25
        }
Packit 67cb25
    }
Packit 67cb25
  while (1);
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
/* 
Packit 67cb25
 * gsl_ntuple_close:
Packit 67cb25
 * close the ntuple file and free the memory
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ntuple_close (gsl_ntuple * ntuple)
Packit 67cb25
{
Packit 67cb25
  int status = fclose (ntuple->file);
Packit 67cb25
  
Packit 67cb25
  if (status)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("failed to close ntuple file", GSL_EFAILED);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  free (ntuple);
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}