3 * Copyright (C) 2000 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Jan/2001 Modified by Brian Gough. Minor changes for GSL */
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_ntuple.h>
29 * Initialize an ntuple structure and create the related file
33 gsl_ntuple_create (char *filename, void *ntuple_data, size_t size)
35 gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
39 GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
43 ntuple->ntuple_data = ntuple_data;
46 ntuple->file = fopen (filename, "wb");
48 if (ntuple->file == 0)
51 GSL_ERROR_VAL ("unable to create ntuple file", GSL_EFAILED, 0);
59 * Initialize an ntuple structure and open the related file
63 gsl_ntuple_open (char *filename, void *ntuple_data, size_t size)
65 gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
69 GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
73 ntuple->ntuple_data = ntuple_data;
76 ntuple->file = fopen (filename, "rb");
78 if (ntuple->file == 0)
81 GSL_ERROR_VAL ("unable to open ntuple file for reading",
90 * write to file a data row, must be used in a loop!
94 gsl_ntuple_write (gsl_ntuple * ntuple)
98 nwrite = fwrite (ntuple->ntuple_data, ntuple->size,
103 GSL_ERROR ("failed to write ntuple entry to file", GSL_EFAILED);
109 /* the following function is a synonym for gsl_ntuple_write */
112 gsl_ntuple_bookdata (gsl_ntuple * ntuple)
114 return gsl_ntuple_write (ntuple);
119 * read form file a data row, must be used in a loop!
123 gsl_ntuple_read (gsl_ntuple * ntuple)
127 nread = fread (ntuple->ntuple_data, ntuple->size, 1, ntuple->file);
129 if (nread == 0 && feof(ntuple->file))
136 GSL_ERROR ("failed to read ntuple entry from file", GSL_EFAILED);
143 * gsl_ntuple_project:
144 * fill an histogram with an ntuple file contents, use
145 * SelVal and SelFunc user defined functions to get
146 * the value to book and the selection funtion
149 #define EVAL(f,x) ((*((f)->function))(x,(f)->params))
152 gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple,
153 gsl_ntuple_value_fn * value_func,
154 gsl_ntuple_select_fn * select_func)
160 nread = fread (ntuple->ntuple_data, ntuple->size,
163 if (nread == 0 && feof(ntuple->file))
170 GSL_ERROR ("failed to read ntuple for projection", GSL_EFAILED);
173 if (EVAL(select_func, ntuple->ntuple_data))
175 gsl_histogram_increment (h, EVAL(value_func, ntuple->ntuple_data));
186 * close the ntuple file and free the memory
190 gsl_ntuple_close (gsl_ntuple * ntuple)
192 int status = fclose (ntuple->file);
196 GSL_ERROR ("failed to close ntuple file", GSL_EFAILED);