⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ntuple.texi

📁 开放gsl矩阵运算
💻 TEXI
字号:
@cindex ntuplesThis chapter describes functions for creating and manipulating@dfn{ntuples}, sets of values associated with events.  The ntuplesare stored in files. Their values can be extracted in any combinationand @dfn{booked} in an histogram using a selection function.The values to be stored are held in a user-defined data structure, andan ntuple is created associating this data structure with a file.  Thevalues are then written to the file (normally inside a loop) usingthe ntuple functions described below.A histogram can be created from ntuple data by providing a selectionfunction and a value function.  The selection function specifies whetheran event should be included in the subset to be analyzed or not. The valuefunction computes the entry to be added to the histogram entry for eachevent.All the ntuple functions are defined in the header file@file{gsl_ntuple.h}@menu* The ntuple struct::           * Creating ntuples::            * Opening an existing ntuple file::  * Writing ntuples::             * Reading ntuples ::            * Closing an ntuple file::      * Histogramming ntuple values::  * Example ntuple programs::     * Ntuple References and Further Reading::  @end menu@node The ntuple struct@section The ntuple structNtuples are manipulated using the @code{gsl_ntuple} struct. This structcontains information on the file where the ntuple data is stored, apointer to the current ntuple data row and the size of the user-definedntuple data struct.@exampletypedef struct @{    FILE * file;    void * ntuple_data;    size_t size;@} gsl_ntuple;@end example@node Creating ntuples@section Creating ntuples@deftypefun {gsl_ntuple *} gsl_ntuple_create (char * @var{filename}, void * @var{ntuple_data}, size_t @var{size})This function creates a new write-only ntuple file @var{filename} forntuples of size @var{size} and returns a pointer to the newly createdntuple struct.  Any existing file with the same name is truncated tozero length and overwritten.  A pointer to memory for the current ntuplerow @var{ntuple_data} must be supplied -- this is used to copy ntuplesin and out of the file.@end deftypefun@node Opening an existing ntuple file@section Opening an existing ntuple file@deftypefun {gsl_ntuple *} gsl_ntuple_open (char * @var{filename}, void * @var{ntuple_data}, size_t @var{size})This function opens an existing ntuple file @var{filename} for readingand returns a pointer to a corresponding ntuple struct. The ntuples inthe file must have size @var{size}.  A pointer to memory for the currentntuple row @var{ntuple_data} must be supplied -- this is used to copyntuples in and out of the file.@end deftypefun@node Writing ntuples@section Writing ntuples@deftypefun int gsl_ntuple_write (gsl_ntuple * @var{ntuple})This function writes the current ntuple @var{ntuple->ntuple_data} ofsize @var{ntuple->size} to the corresponding file.@end deftypefun@deftypefun int gsl_ntuple_bookdata (gsl_ntuple * @var{ntuple})This function is a synonym for @code{gsl_ntuple_write}@end deftypefun@node Reading ntuples @section Reading ntuples@deftypefun int gsl_ntuple_read (gsl_ntuple * @var{ntuple})This function reads the current row of the ntuple file for @var{ntuple}and stores the values in @var{ntuple->data}@end deftypefun@node Closing an ntuple file@section Closing an ntuple file@deftypefun int gsl_ntuple_close (gsl_ntuple * @var{ntuple})This function closes the ntuple file @var{ntuple} and frees itsassociated allocated memory.@end deftypefun@node Histogramming ntuple values@section Histogramming ntuple valuesOnce an ntuple has been created its contents can be histogrammed invarious ways using the function @code{gsl_ntuple_project}.  Twouser-defined functions must be provided, a function to select events anda function to compute scalar values. The selection function and thevalue function both accept the ntuple row as a first argument and otherparameters as a second argument.@cindex selection function, ntuplesThe @dfn{selection function} determines which ntuple rows are selectedfor histogramming.  It is defined by the following struct,@smallexampletypedef struct @{  int (* function) (void * ntuple_data, void * params);  void * params;@} gsl_ntuple_select_fn;@end smallexample@noindentThe struct component @var{function} should return a non-zero value foreach ntuple row that is to be included in the histogram.@cindex value function, ntuplesThe @dfn{value function} computes scalar values for those ntuple rowsselected by the selection function,@smallexampletypedef struct @{  double (* function) (void * ntuple_data, void * params);  void * params;@} gsl_ntuple_value_fn;@end smallexample@noindentIn this case the struct component @var{function} should return the valueto be added to the histogram for the ntuple row.  @cindex histogram, from ntuple@cindex projection of ntuples@deftypefun int gsl_ntuple_project (gsl_histogram * @var{h}, gsl_ntuple * @var{ntuple}, gsl_ntuple_value_fn *@var{value_func}, gsl_ntuple_select_fn *@var{select_func})This function updates the histogram @var{h} from the ntuple @var{ntuple}using the functions @var{value_func} and @var{select_func}. For eachntuple row where the selection function @var{select_func} is non-zero thecorresponding value of that row is computed using the function@var{value_func} and added to the histogram.  Those ntuple rows where@var{select_func} returns zero are ignored.  New entries are added tothe histogram, so subsequent calls can be used to accumulate furtherdata in the same histogram.@end deftypefun@node Example ntuple programs@section Example programsThe following example programs demonstrate the use of ntuples inmanaging a large dataset.  The first program creates a set of 100,000simulated "events", each with 3 associated values @math{(x,y,z)}.  Theseare generated from a gaussian distribution with unit variance, fordemonstration purposes, and written to the ntuple file @file{test.dat}.@example#include <config.h>#include <gsl/gsl_ntuple.h>#include <gsl/gsl_rng.h>#include <gsl/gsl_randist.h>struct data@{  double x;  double y;  double z;@};intmain (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;@}@end example@noindentThe next program analyses the ntuple data in the file @file{test.dat}.The analysis procedure is to compute the squared-magnitude of eachevent, @math{E^2=x^2+y^2+z^2}, and select only those which exceed alower limit of 1.5.  The selected events are then histogrammed usingtheir @math{E^2} values.@example#include <config.h>#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);intmain (void)@{  struct data ntuple_row;  int i;  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;@}intsel_func (void *ntuple_data, void *params)@{  double x, y, z, E, scale;  scale = *(double *) params;  x = ((struct data *) ntuple_data)->x;  y = ((struct data *) ntuple_data)->y;  z = ((struct data *) ntuple_data)->z;  E2 = x * x + y * y + z * z;  return E2 > scale;@}doubleval_func (void *ntuple_data, void *params)@{  double x, y, z;  x = ((struct data *) ntuple_data)->x;  y = ((struct data *) ntuple_data)->y;  z = ((struct data *) ntuple_data)->z;  return x * x + y * y + z * z;@}@end exampleThe following plot shows the distribution of the selected events.Note the cut-off at the lower bound.@iftex@sp 1@center @image{ntuple,4in}@end iftex@node Ntuple References and Further Reading@section References and Further Reading@cindex PAW@cindex HBOOK@noindentFurther information on the use of ntuples can be found in thedocumentation for the @sc{cern} packages @sc{paw} and @sc{hbook}(available online).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -