1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* histogram/ntuple.c
*
* Copyright (C) 2000 Simone Piccardi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Jan/2001 Modified by Brian Gough. Minor changes for GSL */
#include <config.h>
#include <errno.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_ntuple.h>
/*
* gsl_ntuple_open:
* Initialize an ntuple structure and create the related file
*/
gsl_ntuple *
gsl_ntuple_create (char *filename, void *ntuple_data, size_t size)
{
gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
if (ntuple == 0)
{
GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
GSL_ENOMEM, 0);
}
ntuple->ntuple_data = ntuple_data;
ntuple->size = size;
ntuple->file = fopen (filename, "wb");
if (ntuple->file == 0)
{
free (ntuple);
GSL_ERROR_VAL ("unable to create ntuple file", GSL_EFAILED, 0);
}
return ntuple;
}
/*
* gsl_ntuple_open:
* Initialize an ntuple structure and open the related file
*/
gsl_ntuple *
gsl_ntuple_open (char *filename, void *ntuple_data, size_t size)
{
gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
if (ntuple == 0)
{
GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
GSL_ENOMEM, 0);
}
ntuple->ntuple_data = ntuple_data;
ntuple->size = size;
ntuple->file = fopen (filename, "rb");
if (ntuple->file == 0)
{
free (ntuple);
GSL_ERROR_VAL ("unable to open ntuple file for reading",
GSL_EFAILED, 0);
}
return ntuple;
}
/*
* gsl_ntuple_write:
* write to file a data row, must be used in a loop!
*/
int
gsl_ntuple_write (gsl_ntuple * ntuple)
{
size_t nwrite;
nwrite = fwrite (ntuple->ntuple_data, ntuple->size,
1, ntuple->file);
if (nwrite != 1)
{
GSL_ERROR ("failed to write ntuple entry to file", GSL_EFAILED);
}
return GSL_SUCCESS;
}
/* the following function is a synonym for gsl_ntuple_write */
int
gsl_ntuple_bookdata (gsl_ntuple * ntuple)
{
return gsl_ntuple_write (ntuple);
}
/*
* gsl_ntuple_read:
* read form file a data row, must be used in a loop!
*/
int
gsl_ntuple_read (gsl_ntuple * ntuple)
{
size_t nread;
nread = fread (ntuple->ntuple_data, ntuple->size, 1, ntuple->file);
if (nread == 0 && feof(ntuple->file))
{
return GSL_EOF;
}
if (nread != 1)
{
GSL_ERROR ("failed to read ntuple entry from file", GSL_EFAILED);
}
return GSL_SUCCESS;
}
/*
* gsl_ntuple_project:
* fill an histogram with an ntuple file contents, use
* SelVal and SelFunc user defined functions to get
* the value to book and the selection funtion
*/
#define EVAL(f,x) ((*((f)->function))(x,(f)->params))
int
gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple,
gsl_ntuple_value_fn * value_func,
gsl_ntuple_select_fn * select_func)
{
size_t nread;
do
{
nread = fread (ntuple->ntuple_data, ntuple->size,
1, ntuple->file);
if (nread == 0 && feof(ntuple->file))
{
break ;
}
if (nread != 1)
{
GSL_ERROR ("failed to read ntuple for projection", GSL_EFAILED);
}
if (EVAL(select_func, ntuple->ntuple_data))
{
gsl_histogram_increment (h, EVAL(value_func, ntuple->ntuple_data));
}
}
while (1);
return GSL_SUCCESS;
}
/*
* gsl_ntuple_close:
* close the ntuple file and free the memory
*/
int
gsl_ntuple_close (gsl_ntuple * ntuple)
{
int status = fclose (ntuple->file);
if (status)
{
GSL_ERROR ("failed to close ntuple file", GSL_EFAILED);
}
free (ntuple);
return GSL_SUCCESS;
}
|