gsl_ntuple__ntuple.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* Jan/2001 Modified by Brian Gough. Minor changes for GSL */
  20. #include "gsl__config.h"
  21. #include <errno.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_ntuple.h"
  24. /*
  25. * gsl_ntuple_open:
  26. * Initialize an ntuple structure and create the related file
  27. */
  28. gsl_ntuple *
  29. gsl_ntuple_create (char *filename, void *ntuple_data, size_t size)
  30. {
  31. gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
  32. if (ntuple == 0)
  33. {
  34. GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
  35. GSL_ENOMEM, 0);
  36. }
  37. ntuple->ntuple_data = ntuple_data;
  38. ntuple->size = size;
  39. ntuple->file = fopen (filename, "wb");
  40. if (ntuple->file == 0)
  41. {
  42. free (ntuple);
  43. GSL_ERROR_VAL ("unable to create ntuple file", GSL_EFAILED, 0);
  44. }
  45. return ntuple;
  46. }
  47. /*
  48. * gsl_ntuple_open:
  49. * Initialize an ntuple structure and open the related file
  50. */
  51. gsl_ntuple *
  52. gsl_ntuple_open (char *filename, void *ntuple_data, size_t size)
  53. {
  54. gsl_ntuple *ntuple = (gsl_ntuple *)malloc (sizeof (gsl_ntuple));
  55. if (ntuple == 0)
  56. {
  57. GSL_ERROR_VAL ("failed to allocate space for ntuple struct",
  58. GSL_ENOMEM, 0);
  59. }
  60. ntuple->ntuple_data = ntuple_data;
  61. ntuple->size = size;
  62. ntuple->file = fopen (filename, "rb");
  63. if (ntuple->file == 0)
  64. {
  65. free (ntuple);
  66. GSL_ERROR_VAL ("unable to open ntuple file for reading",
  67. GSL_EFAILED, 0);
  68. }
  69. return ntuple;
  70. }
  71. /*
  72. * gsl_ntuple_write:
  73. * write to file a data row, must be used in a loop!
  74. */
  75. int
  76. gsl_ntuple_write (gsl_ntuple * ntuple)
  77. {
  78. size_t nwrite;
  79. nwrite = fwrite (ntuple->ntuple_data, ntuple->size,
  80. 1, ntuple->file);
  81. if (nwrite != 1)
  82. {
  83. GSL_ERROR ("failed to write ntuple entry to file", GSL_EFAILED);
  84. }
  85. return GSL_SUCCESS;
  86. }
  87. /* the following function is a synonym for gsl_ntuple_write */
  88. int
  89. gsl_ntuple_bookdata (gsl_ntuple * ntuple)
  90. {
  91. return gsl_ntuple_write (ntuple);
  92. }
  93. /*
  94. * gsl_ntuple_read:
  95. * read form file a data row, must be used in a loop!
  96. */
  97. int
  98. gsl_ntuple_read (gsl_ntuple * ntuple)
  99. {
  100. size_t nread;
  101. nread = fread (ntuple->ntuple_data, ntuple->size, 1, ntuple->file);
  102. if (nread == 0 && feof(ntuple->file))
  103. {
  104. return GSL_EOF;
  105. }
  106. if (nread != 1)
  107. {
  108. GSL_ERROR ("failed to read ntuple entry from file", GSL_EFAILED);
  109. }
  110. return GSL_SUCCESS;
  111. }
  112. /*
  113. * gsl_ntuple_project:
  114. * fill an histogram with an ntuple file contents, use
  115. * SelVal and SelFunc user defined functions to get
  116. * the value to book and the selection funtion
  117. */
  118. #define EVAL(f,x) ((*((f)->function))(x,(f)->params))
  119. int
  120. gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple,
  121. gsl_ntuple_value_fn * value_func,
  122. gsl_ntuple_select_fn * select_func)
  123. {
  124. size_t nread;
  125. do
  126. {
  127. nread = fread (ntuple->ntuple_data, ntuple->size,
  128. 1, ntuple->file);
  129. if (nread == 0 && feof(ntuple->file))
  130. {
  131. break ;
  132. }
  133. if (nread != 1)
  134. {
  135. GSL_ERROR ("failed to read ntuple for projection", GSL_EFAILED);
  136. }
  137. if (EVAL(select_func, ntuple->ntuple_data))
  138. {
  139. gsl_histogram_increment (h, EVAL(value_func, ntuple->ntuple_data));
  140. }
  141. }
  142. while (1);
  143. return GSL_SUCCESS;
  144. }
  145. /*
  146. * gsl_ntuple_close:
  147. * close the ntuple file and free the memory
  148. */
  149. int
  150. gsl_ntuple_close (gsl_ntuple * ntuple)
  151. {
  152. int status = fclose (ntuple->file);
  153. if (status)
  154. {
  155. GSL_ERROR ("failed to close ntuple file", GSL_EFAILED);
  156. }
  157. free (ntuple);
  158. return GSL_SUCCESS;
  159. }