Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / doc / examples / ntupler.c
1 #include <math.h>
2 #include <gsl/gsl_ntuple.h>
3 #include <gsl/gsl_histogram.h>
4
5 struct data
6 {
7   double x;
8   double y;
9   double z;
10 };
11
12 int sel_func (void *ntuple_data, void *params);
13 double val_func (void *ntuple_data, void *params);
14
15 int
16 main (void)
17 {
18   struct data ntuple_row;
19
20   gsl_ntuple *ntuple 
21     = gsl_ntuple_open ("test.dat", &ntuple_row,
22                        sizeof (ntuple_row));
23   double lower = 1.5;
24
25   gsl_ntuple_select_fn S;
26   gsl_ntuple_value_fn V;
27
28   gsl_histogram *h = gsl_histogram_alloc (100);
29   gsl_histogram_set_ranges_uniform(h, 0.0, 10.0);
30
31   S.function = &sel_func;
32   S.params = &lower;
33
34   V.function = &val_func;
35   V.params = 0;
36
37   gsl_ntuple_project (h, ntuple, &V, &S);
38   gsl_histogram_fprintf (stdout, h, "%f", "%f");
39   gsl_histogram_free (h);
40   gsl_ntuple_close (ntuple);
41
42   return 0;
43 }
44
45 int
46 sel_func (void *ntuple_data, void *params)
47 {
48   struct data * data = (struct data *) ntuple_data;  
49   double x, y, z, E2, scale;
50   scale = *(double *) params;
51   
52   x = data->x;
53   y = data->y;
54   z = data->z;
55
56   E2 = x * x + y * y + z * z;
57
58   return E2 > scale;
59 }
60
61 double
62 val_func (void *ntuple_data, void *params)
63 {
64   struct data * data = (struct data *) ntuple_data;  
65   double x, y, z;
66
67   x = data->x;
68   y = data->y;
69   z = data->z;
70
71   return x * x + y * y + z * z;
72 }