Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / ntuple / ntuple.c
1 /* histogram/ntuple.c
2  * 
3  * Copyright (C) 2000 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19
20 /* Jan/2001 Modified by Brian Gough. Minor changes for GSL */
21
22 #include <config.h>
23 #include <errno.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_ntuple.h>
26
27 /* 
28  * gsl_ntuple_open:
29  * Initialize an ntuple structure and create the related file
30  */
31
32 gsl_ntuple *
33 gsl_ntuple_create (char *filename, void *ntuple_data, size_t size)
34 {
35   gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
36
37   if (ntuple == 0)
38     {
39       GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
40                      GSL_ENOMEM, 0);
41     }
42
43   ntuple->ntuple_data = ntuple_data;
44   ntuple->size = size;
45
46   ntuple->file = fopen (filename, "wb");
47
48   if (ntuple->file == 0)
49     {
50       free (ntuple);
51       GSL_ERROR_VAL ("unable to create ntuple file", GSL_EFAILED, 0);
52     }
53
54   return ntuple;
55 }
56
57 /* 
58  * gsl_ntuple_open:
59  * Initialize an ntuple structure and open the related file
60  */
61
62 gsl_ntuple *
63 gsl_ntuple_open (char *filename, void *ntuple_data, size_t size)
64 {
65   gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
66
67   if (ntuple == 0)
68     {
69       GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
70                      GSL_ENOMEM, 0);
71     }
72
73   ntuple->ntuple_data = ntuple_data;
74   ntuple->size = size;
75
76   ntuple->file = fopen (filename, "rb");
77
78   if (ntuple->file == 0)
79     {
80       free (ntuple);
81       GSL_ERROR_VAL ("unable to open ntuple file for reading", 
82                      GSL_EFAILED, 0);
83     }
84
85   return ntuple;
86 }
87
88 /* 
89  * gsl_ntuple_write:
90  * write to file a data row, must be used in a loop!
91  */
92
93 int
94 gsl_ntuple_write (gsl_ntuple * ntuple)
95 {
96   size_t nwrite;
97
98   nwrite = fwrite (ntuple->ntuple_data, ntuple->size,
99                    1, ntuple->file);
100
101   if (nwrite != 1)
102     {
103       GSL_ERROR ("failed to write ntuple entry to file", GSL_EFAILED);
104     }
105
106   return GSL_SUCCESS;
107 }
108
109 /* the following function is a synonym for gsl_ntuple_write */
110
111 int
112 gsl_ntuple_bookdata (gsl_ntuple * ntuple)
113 {
114   return gsl_ntuple_write (ntuple);
115 }
116
117 /* 
118  * gsl_ntuple_read:
119  * read form file a data row, must be used in a loop!
120  */
121
122 int
123 gsl_ntuple_read (gsl_ntuple * ntuple)
124 {
125   size_t nread;
126
127   nread = fread (ntuple->ntuple_data, ntuple->size, 1, ntuple->file);
128
129   if (nread == 0 && feof(ntuple->file))
130     {
131       return GSL_EOF;
132     }
133
134   if (nread != 1)
135     {
136       GSL_ERROR ("failed to read ntuple entry from file", GSL_EFAILED);
137     }
138
139   return GSL_SUCCESS;
140 }
141
142 /* 
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
147  */
148
149 #define EVAL(f,x) ((*((f)->function))(x,(f)->params))
150
151 int
152 gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple,
153                     gsl_ntuple_value_fn * value_func, 
154                     gsl_ntuple_select_fn * select_func)
155 {
156   size_t nread;
157
158   do
159     {
160       nread = fread (ntuple->ntuple_data, ntuple->size,
161                      1, ntuple->file);
162
163       if (nread == 0 && feof(ntuple->file))
164         {
165           break ;
166         }
167       
168       if (nread != 1) 
169         {
170           GSL_ERROR ("failed to read ntuple for projection", GSL_EFAILED);
171         }
172
173       if (EVAL(select_func, ntuple->ntuple_data))
174         {
175           gsl_histogram_increment (h, EVAL(value_func, ntuple->ntuple_data));
176         }
177     }
178   while (1);
179
180   return GSL_SUCCESS;
181 }
182
183
184 /* 
185  * gsl_ntuple_close:
186  * close the ntuple file and free the memory
187  */
188
189 int
190 gsl_ntuple_close (gsl_ntuple * ntuple)
191 {
192   int status = fclose (ntuple->file);
193   
194   if (status)
195     {
196       GSL_ERROR ("failed to close ntuple file", GSL_EFAILED);
197     }
198
199   free (ntuple);
200
201   return GSL_SUCCESS;
202 }