Blame doc/ntuple.rst

Packit 67cb25
.. index:: ntuples
Packit 67cb25
Packit 67cb25
********
Packit 67cb25
N-tuples
Packit 67cb25
********
Packit 67cb25
Packit 67cb25
This chapter describes functions for creating and manipulating
Packit 67cb25
*ntuples*, sets of values associated with events.  The ntuples
Packit 67cb25
are stored in files. Their values can be extracted in any combination
Packit 67cb25
and *booked* in a histogram using a selection function.
Packit 67cb25
Packit 67cb25
The values to be stored are held in a user-defined data structure, and
Packit 67cb25
an ntuple is created associating this data structure with a file.  The
Packit 67cb25
values are then written to the file (normally inside a loop) using
Packit 67cb25
the ntuple functions described below.
Packit 67cb25
Packit 67cb25
A histogram can be created from ntuple data by providing a selection
Packit 67cb25
function and a value function.  The selection function specifies whether
Packit 67cb25
an event should be included in the subset to be analyzed or not. The value
Packit 67cb25
function computes the entry to be added to the histogram for each
Packit 67cb25
event.
Packit 67cb25
Packit 67cb25
All the ntuple functions are defined in the header file
Packit 67cb25
:file:`gsl_ntuple.h`.
Packit 67cb25
Packit 67cb25
The ntuple struct
Packit 67cb25
=================
Packit 67cb25
Packit 67cb25
.. type:: gsl_ntuple
Packit 67cb25
Packit 67cb25
   Ntuples are manipulated using the :type:`gsl_ntuple` struct. This struct
Packit 67cb25
   contains information on the file where the ntuple data is stored, a
Packit 67cb25
   pointer to the current ntuple data row and the size of the user-defined
Packit 67cb25
   ntuple data struct::
Packit 67cb25
Packit 67cb25
      typedef struct
Packit 67cb25
        {
Packit 67cb25
          FILE * file;
Packit 67cb25
          void * ntuple_data;
Packit 67cb25
          size_t size;
Packit 67cb25
        } gsl_ntuple;
Packit 67cb25
Packit 67cb25
Creating ntuples
Packit 67cb25
================
Packit 67cb25
Packit 67cb25
.. function:: gsl_ntuple * gsl_ntuple_create (char * filename, void * ntuple_data, size_t size)
Packit 67cb25
Packit 67cb25
   This function creates a new write-only ntuple file :data:`filename` for
Packit 67cb25
   ntuples of size :data:`size` and returns a pointer to the newly created
Packit 67cb25
   ntuple struct.  Any existing file with the same name is truncated to
Packit 67cb25
   zero length and overwritten.  A pointer to memory for the current ntuple
Packit 67cb25
   row :data:`ntuple_data` must be supplied---this is used to copy ntuples
Packit 67cb25
   in and out of the file.
Packit 67cb25
Packit 67cb25
Opening an existing ntuple file
Packit 67cb25
===============================
Packit 67cb25
Packit 67cb25
.. function:: gsl_ntuple * gsl_ntuple_open (char * filename, void * ntuple_data, size_t size)
Packit 67cb25
Packit 67cb25
   This function opens an existing ntuple file :data:`filename` for reading
Packit 67cb25
   and returns a pointer to a corresponding ntuple struct. The ntuples in
Packit 67cb25
   the file must have size :data:`size`.  A pointer to memory for the current
Packit 67cb25
   ntuple row :data:`ntuple_data` must be supplied---this is used to copy
Packit 67cb25
   ntuples in and out of the file.
Packit 67cb25
Packit 67cb25
Writing ntuples
Packit 67cb25
===============
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ntuple_write (gsl_ntuple * ntuple)
Packit 67cb25
Packit 67cb25
   This function writes the current ntuple :code:`ntuple->ntuple_data` of
Packit 67cb25
   size :code:`ntuple->size` to the corresponding file.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ntuple_bookdata (gsl_ntuple * ntuple)
Packit 67cb25
Packit 67cb25
   This function is a synonym for :func:`gsl_ntuple_write`.
Packit 67cb25
Packit 67cb25
Reading ntuples
Packit 67cb25
===============
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ntuple_read (gsl_ntuple * ntuple)
Packit 67cb25
Packit 67cb25
   This function reads the current row of the ntuple file for :data:`ntuple`
Packit 67cb25
   and stores the values in :code:`ntuple->data`.
Packit 67cb25
Packit 67cb25
Closing an ntuple file
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ntuple_close (gsl_ntuple * ntuple)
Packit 67cb25
Packit 67cb25
   This function closes the ntuple file :data:`ntuple` and frees its
Packit 67cb25
   associated allocated memory.
Packit 67cb25
Packit 67cb25
Histogramming ntuple values
Packit 67cb25
===========================
Packit 67cb25
Packit 67cb25
Once an ntuple has been created its contents can be histogrammed in
Packit 67cb25
various ways using the function :func:`gsl_ntuple_project`.  Two
Packit 67cb25
user-defined functions must be provided, a function to select events and
Packit 67cb25
a function to compute scalar values. The selection function and the
Packit 67cb25
value function both accept the ntuple row as a first argument and other
Packit 67cb25
parameters as a second argument.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: selection function, ntuples
Packit 67cb25
Packit 67cb25
.. type:: gsl_ntuple_select_fn
Packit 67cb25
Packit 67cb25
   The *selection function* determines which ntuple rows are selected
Packit 67cb25
   for histogramming.  It is defined by the following struct::
Packit 67cb25
Packit 67cb25
      typedef struct
Packit 67cb25
        {
Packit 67cb25
          int (* function) (void * ntuple_data, void * params);
Packit 67cb25
          void * params;
Packit 67cb25
        } gsl_ntuple_select_fn;
Packit 67cb25
Packit 67cb25
   The struct component :data:`function` should return a non-zero value for
Packit 67cb25
   each ntuple row that is to be included in the histogram.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: value function, ntuples
Packit 67cb25
Packit 67cb25
.. type:: gsl_ntuple_value_fn
Packit 67cb25
Packit 67cb25
   The *value function* computes scalar values for those ntuple rows
Packit 67cb25
   selected by the selection function::
Packit 67cb25
Packit 67cb25
      typedef struct
Packit 67cb25
        {
Packit 67cb25
          double (* function) (void * ntuple_data, void * params);
Packit 67cb25
          void * params;
Packit 67cb25
        } gsl_ntuple_value_fn;
Packit 67cb25
Packit 67cb25
   In this case the struct component :data:`function` should return the value
Packit 67cb25
   to be added to the histogram for the ntuple row.  
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: histogram, from ntuple
Packit 67cb25
   single: projection of ntuples
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple, gsl_ntuple_value_fn * value_func, gsl_ntuple_select_fn * select_func)
Packit 67cb25
Packit 67cb25
   This function updates the histogram :data:`h` from the ntuple :data:`ntuple`
Packit 67cb25
   using the functions :data:`value_func` and :data:`select_func`. For each
Packit 67cb25
   ntuple row where the selection function :data:`select_func` is non-zero the
Packit 67cb25
   corresponding value of that row is computed using the function
Packit 67cb25
   :data:`value_func` and added to the histogram.  Those ntuple rows where
Packit 67cb25
   :data:`select_func` returns zero are ignored.  New entries are added to
Packit 67cb25
   the histogram, so subsequent calls can be used to accumulate further
Packit 67cb25
   data in the same histogram.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The following example programs demonstrate the use of ntuples in
Packit 67cb25
managing a large dataset.  The first program creates a set of 10,000
Packit 67cb25
simulated "events", each with 3 associated values :math:`(x,y,z)`.  These
Packit 67cb25
are generated from a Gaussian distribution with unit variance, for
Packit 67cb25
demonstration purposes, and written to the ntuple file :file:`test.dat`.
Packit 67cb25
Packit 67cb25
.. include:: examples/ntuplew.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The next program analyses the ntuple data in the file :file:`test.dat`.
Packit 67cb25
The analysis procedure is to compute the squared-magnitude of each
Packit 67cb25
event, :math:`E^2=x^2+y^2+z^2`, and select only those which exceed a
Packit 67cb25
lower limit of 1.5.  The selected events are then histogrammed using
Packit 67cb25
their :math:`E^2` values.
Packit 67cb25
Packit 67cb25
.. include:: examples/ntupler.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
:numref:`fig_ntuples` shows the distribution of the selected events.
Packit 67cb25
Note the cut-off at the lower bound.
Packit 67cb25
Packit 67cb25
.. _fig_ntuples:
Packit 67cb25
Packit 67cb25
.. figure:: /images/ntuple.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Distribution of selected events
Packit 67cb25
Packit 67cb25
References and Further Reading
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
.. index:: PAW, HBOOK
Packit 67cb25
Packit 67cb25
Further information on the use of ntuples can be found in the
Packit 67cb25
documentation for the CERN packages PAW and HBOOK
Packit 67cb25
(available online).