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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
from ../gsl-ref.texi -->
<TITLE>GNU Scientific Library -- Reference Manual - N-tuples</TITLE>
<!-- <LINK rel="stylesheet" title="Default Style Sheet" href="/css/texinfo.css" type="text/css"> -->
<link href="gsl-ref_23.html" rel=Next>
<link href="gsl-ref_21.html" rel=Previous>
<link href="gsl-ref_toc.html" rel=ToC>
</HEAD>
<BODY>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_21.html">previous</A>, <A HREF="gsl-ref_23.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
<P><HR><P>
<H1><A NAME="SEC361" HREF="gsl-ref_toc.html#TOC361">N-tuples</A></H1>
<P>
<A NAME="IDX1825"></A>
</P>
<P>
This chapter describes functions for creating and manipulating
<I>ntuples</I>, sets of values associated with events. The ntuples
are stored in files. Their values can be extracted in any combination
and <I>booked</I> in a histogram using a selection function.
</P>
<P>
The values to be stored are held in a user-defined data structure, and
an ntuple is created associating this data structure with a file. The
values are then written to the file (normally inside a loop) using
the ntuple functions described below.
</P>
<P>
A histogram can be created from ntuple data by providing a selection
function and a value function. The selection function specifies whether
an event should be included in the subset to be analyzed or not. The value
function computes the entry to be added to the histogram for each
event.
</P>
<P>
All the ntuple functions are defined in the header file
<TT>'gsl_ntuple.h'</TT>
</P>
<H2><A NAME="SEC362" HREF="gsl-ref_toc.html#TOC362">The ntuple struct</A></H2>
<P>
Ntuples are manipulated using the <CODE>gsl_ntuple</CODE> struct. This struct
contains information on the file where the ntuple data is stored, a
pointer to the current ntuple data row and the size of the user-defined
ntuple data struct.
</P>
<PRE class="example">
typedef struct {
FILE * file;
void * ntuple_data;
size_t size;
} gsl_ntuple;
</PRE>
<H2><A NAME="SEC363" HREF="gsl-ref_toc.html#TOC363">Creating ntuples</A></H2>
<P>
<DL>
<DT><U>Function:</U> gsl_ntuple * <B>gsl_ntuple_create</B> <I>(char * <VAR>filename</VAR>, void * <VAR>ntuple_data</VAR>, size_t <VAR>size</VAR>)</I>
<DD><A NAME="IDX1826"></A>
This function creates a new write-only ntuple file <VAR>filename</VAR> for
ntuples of size <VAR>size</VAR> and returns a pointer to the newly created
ntuple struct. Any existing file with the same name is truncated to
zero length and overwritten. A pointer to memory for the current ntuple
row <VAR>ntuple_data</VAR> must be supplied--this is used to copy ntuples
in and out of the file.
</DL>
</P>
<H2><A NAME="SEC364" HREF="gsl-ref_toc.html#TOC364">Opening an existing ntuple file</A></H2>
<P>
<DL>
<DT><U>Function:</U> gsl_ntuple * <B>gsl_ntuple_open</B> <I>(char * <VAR>filename</VAR>, void * <VAR>ntuple_data</VAR>, size_t <VAR>size</VAR>)</I>
<DD><A NAME="IDX1827"></A>
This function opens an existing ntuple file <VAR>filename</VAR> for reading
and returns a pointer to a corresponding ntuple struct. The ntuples in
the file must have size <VAR>size</VAR>. A pointer to memory for the current
ntuple row <VAR>ntuple_data</VAR> must be supplied--this is used to copy
ntuples in and out of the file.
</DL>
</P>
<H2><A NAME="SEC365" HREF="gsl-ref_toc.html#TOC365">Writing ntuples</A></H2>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_ntuple_write</B> <I>(gsl_ntuple * <VAR>ntuple</VAR>)</I>
<DD><A NAME="IDX1828"></A>
This function writes the current ntuple <VAR>ntuple->ntuple_data</VAR> of
size <VAR>ntuple->size</VAR> to the corresponding file.
</DL>
</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_ntuple_bookdata</B> <I>(gsl_ntuple * <VAR>ntuple</VAR>)</I>
<DD><A NAME="IDX1829"></A>
This function is a synonym for <CODE>gsl_ntuple_write</CODE>.
</DL>
</P>
<H2><A NAME="SEC366" HREF="gsl-ref_toc.html#TOC366">Reading ntuples</A></H2>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_ntuple_read</B> <I>(gsl_ntuple * <VAR>ntuple</VAR>)</I>
<DD><A NAME="IDX1830"></A>
This function reads the current row of the ntuple file for <VAR>ntuple</VAR>
and stores the values in <VAR>ntuple->data</VAR>.
</DL>
</P>
<H2><A NAME="SEC367" HREF="gsl-ref_toc.html#TOC367">Closing an ntuple file</A></H2>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_ntuple_close</B> <I>(gsl_ntuple * <VAR>ntuple</VAR>)</I>
<DD><A NAME="IDX1831"></A>
This function closes the ntuple file <VAR>ntuple</VAR> and frees its
associated allocated memory.
</DL>
</P>
<H2><A NAME="SEC368" HREF="gsl-ref_toc.html#TOC368">Histogramming ntuple values</A></H2>
<P>
Once an ntuple has been created its contents can be histogrammed in
various ways using the function <CODE>gsl_ntuple_project</CODE>. Two
user-defined functions must be provided, a function to select events and
a function to compute scalar values. The selection function and the
value function both accept the ntuple row as a first argument and other
parameters as a second argument.
</P>
<P>
<A NAME="IDX1832"></A>
The <I>selection function</I> determines which ntuple rows are selected
for histogramming. It is defined by the following struct,
<PRE class="smallexample">
typedef struct {
int (* function) (void * ntuple_data, void * params);
void * params;
} gsl_ntuple_select_fn;
</PRE>
<P>
The struct component <VAR>function</VAR> should return a non-zero value for
each ntuple row that is to be included in the histogram.
</P>
<P>
<A NAME="IDX1833"></A>
The <I>value function</I> computes scalar values for those ntuple rows
selected by the selection function,
<PRE class="smallexample">
typedef struct {
double (* function) (void * ntuple_data, void * params);
void * params;
} gsl_ntuple_value_fn;
</PRE>
<P>
In this case the struct component <VAR>function</VAR> should return the value
to be added to the histogram for the ntuple row.
</P>
<P>
<A NAME="IDX1834"></A>
<A NAME="IDX1835"></A>
<DL>
<DT><U>Function:</U> int <B>gsl_ntuple_project</B> <I>(gsl_histogram * <VAR>h</VAR>, gsl_ntuple * <VAR>ntuple</VAR>, gsl_ntuple_value_fn *<VAR>value_func</VAR>, gsl_ntuple_select_fn *<VAR>select_func</VAR>)</I>
<DD><A NAME="IDX1836"></A>
This function updates the histogram <VAR>h</VAR> from the ntuple <VAR>ntuple</VAR>
using the functions <VAR>value_func</VAR> and <VAR>select_func</VAR>. For each
ntuple row where the selection function <VAR>select_func</VAR> is non-zero the
corresponding value of that row is computed using the function
<VAR>value_func</VAR> and added to the histogram. Those ntuple rows where
<VAR>select_func</VAR> returns zero are ignored. New entries are added to
the histogram, so subsequent calls can be used to accumulate further
data in the same histogram.
</DL>
</P>
<H2><A NAME="SEC369" HREF="gsl-ref_toc.html#TOC369">Examples</A></H2>
<P>
The following example programs demonstrate the use of ntuples in
managing a large dataset. The first program creates a set of 10,000
simulated "events", each with 3 associated values (x,y,z). These
are generated from a gaussian distribution with unit variance, for
demonstration purposes, and written to the ntuple file <TT>'test.dat'</TT>.
</P>
<PRE class="example">
#include <gsl/gsl_ntuple.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
struct data
{
double x;
double y;
double z;
};
int
main (void)
{
const gsl_rng_type * T;
gsl_rng * r;
struct data ntuple_row;
int i;
gsl_ntuple *ntuple
= gsl_ntuple_create ("test.dat", &ntuple_row,
sizeof (ntuple_row));
gsl_rng_env_setup ();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
for (i = 0; i < 10000; i++)
{
ntuple_row.x = gsl_ran_ugaussian (r);
ntuple_row.y = gsl_ran_ugaussian (r);
ntuple_row.z = gsl_ran_ugaussian (r);
gsl_ntuple_write (ntuple);
}
gsl_ntuple_close (ntuple);
return 0;
}
</PRE>
<P>
The next program analyses the ntuple data in the file <TT>'test.dat'</TT>.
The analysis procedure is to compute the squared-magnitude of each
event, E^2=x^2+y^2+z^2, and select only those which exceed a
lower limit of 1.5. The selected events are then histogrammed using
their E^2 values.
</P>
<PRE class="example">
#include <math.h>
#include <gsl/gsl_ntuple.h>
#include <gsl/gsl_histogram.h>
struct data
{
double x;
double y;
double z;
};
int sel_func (void *ntuple_data, void *params);
double val_func (void *ntuple_data, void *params);
int
main (void)
{
struct data ntuple_row;
gsl_ntuple *ntuple
= gsl_ntuple_open ("test.dat", &ntuple_row,
sizeof (ntuple_row));
double lower = 1.5;
gsl_ntuple_select_fn S;
gsl_ntuple_value_fn V;
gsl_histogram *h = gsl_histogram_alloc (100);
gsl_histogram_set_ranges_uniform(h, 0.0, 10.0);
S.function = &sel_func;
S.params = &lower;
V.function = &val_func;
V.params = 0;
gsl_ntuple_project (h, ntuple, &V, &S);
gsl_histogram_fprintf (stdout, h, "%f", "%f");
gsl_histogram_free (h);
gsl_ntuple_close (ntuple);
return 0;
}
int
sel_func (void *ntuple_data, void *params)
{
struct data * data = (struct data *) ntuple_data;
double x, y, z, E2, scale;
scale = *(double *) params;
x = data->x;
y = data->y;
z = data->z;
E2 = x * x + y * y + z * z;
return E2 > scale;
}
double
val_func (void *ntuple_data, void *params)
{
struct data * data = (struct data *) ntuple_data;
double x, y, z;
x = data->x;
y = data->y;
z = data->z;
return x * x + y * y + z * z;
}
</PRE>
<P>
The following plot shows the distribution of the selected events.
Note the cut-off at the lower bound.
</P>
<H2><A NAME="SEC370" HREF="gsl-ref_toc.html#TOC370">References and Further Reading</A></H2>
<P>
<A NAME="IDX1837"></A>
<A NAME="IDX1838"></A>
</P>
<P>
Further information on the use of ntuples can be found in the
documentation for the CERN packages PAW and HBOOK
(available online).
</P>
<P><HR><P>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_21.html">previous</A>, <A HREF="gsl-ref_23.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
</BODY>
</HTML>
|