AlcapDAQ  1
Functions.c
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * CAEN SpA - Front End Division
4 * Via Vetraia, 11 - 55049 - Viareggio ITALY
5 * +390594388398 - www.caen.it
6 *
7 ***************************************************************************/
17 #include "keyb.h"
18 #include "Functions.h"
19 
20 #include <stdio.h>
21 #ifdef WIN32
22 
23  #include <time.h>
24  #include <sys/timeb.h>
25  #include <conio.h>
26  #include <process.h>
27  #define getch _getch /* redefine POSIX 'deprecated' */
28  #define kbhit _kbhit /* redefine POSIX 'deprecated' */
29 
30 #else
31  #include <unistd.h>
32  #include <stdint.h> /* C99 compliant compilers: uint64_t */
33  #include <ctype.h> /* toupper() */
34  #include <sys/time.h>
35 #endif
36 
37 /* ###########################################################################
38 * Functions
39 * ########################################################################### */
43 long get_time()
44 {
45  long time_ms;
46 #ifdef WIN32
47  struct _timeb timebuffer;
48  _ftime( &timebuffer );
49  time_ms = (long)timebuffer.time * 1000 + (long)timebuffer.millitm;
50 #else
51  struct timeval t1;
52  struct timezone tz;
53  gettimeofday(&t1, &tz);
54  time_ms = (t1.tv_sec) * 1000 + t1.tv_usec / 1000;
55 #endif
56  return time_ms;
57 }
58 
59 /* --------------------------------------------------------------------------------------------------------- */
63 /* --------------------------------------------------------------------------------------------------------- */
64 int DataConsistencyCheck(uint32_t *buff32, int NumWords)
65 {
66  int i, zcnt=0, pnt=0;
67  uint32_t EventSize;
68 
69  if (NumWords == 0)
70  return 0;
71 
72  // Check for events integrity
73  do {
74  EventSize = buff32[pnt] & 0x0FFFFFFF;
75  pnt += EventSize; // Jump to next event
76  } while (pnt<NumWords);
77  if (pnt != NumWords) {
78  printf("Data Error: Event truncation\n");
79  return -1;
80  }
81 
82  // Check for burst of zeroes (more than 2 consecutive zeroes)
83  for(i=0; i<NumWords; i++) {
84  if (buff32[i] == 0) zcnt++;
85  else zcnt=0;
86  if (zcnt > 2) {
87  printf("Data Error: Burst of zeroes\n");
88  return -1;
89  }
90  }
91  return 0;
92 }
93 
94 
95 /* --------------------------------------------------------------------------------------------------------- */
99 /* --------------------------------------------------------------------------------------------------------- */
100 
101 int SaveHistogram(char *basename, int b, int ch, uint32_t *EHisto)
102 {
103  /*
104  This function saves the first 10000 bin of each EHisto[ch] array in separate text files
105  each array value is a number representing the histogram height in the corresponding bin
106  NB: each EHisto[ch] must be a pointer already initialized with at least 10000 values
107  */
108  FILE *fh;
109  int i;
110  char filename[20];
111  sprintf(filename, "%s_%d_%d.txt", basename, b, ch);
112  fh = fopen(filename, "w");
113  if (fh == NULL)
114  return -1;
115  for(i=0; i<(1<<12); i++) {
116  fprintf(fh, "%d\n", EHisto[i]);
117  }
118  fclose(fh);
119  printf("Histograms saved to '%s_<board>_<channel>.txt'\n", basename);
120 
121  return 0;
122 }
123 
124 /* --------------------------------------------------------------------------------------------------------- */
128 /* --------------------------------------------------------------------------------------------------------- */
129 int SaveWaveform(int b, int ch, int trace, int size, int16_t *WaveData)
130 {
131  /*
132  This function saves the waveform in a textfile as a sequence of number representing the wave height
133  */
134  FILE *fh;
135  int i;
136  char filename[20];
137 
138  sprintf(filename, "Waveform_%d_%d_%d.txt", b, ch, trace);
139  fh = fopen(filename, "w");
140  if (fh == NULL)
141  return -1;
142  for(i=0; i<size; i++)
143  fprintf(fh, "%d\n", WaveData[i]); //&((1<<MAXNBITS)-1)
144  fclose(fh);
145  return 0;
146 }
147 
148 /* --------------------------------------------------------------------------------------------------------- */
152 /* --------------------------------------------------------------------------------------------------------- */
153 int SaveDigitalProbe(int b, int ch, int trace, int size, uint8_t *WaveData)
154 {
155  /*
156  This function saves the digital waveform in a textfile as a sequence of number representing the wave height
157  */
158  FILE *fh;
159  int i;
160  char filename[20];
161 
162  sprintf(filename, "DWaveform_%d_%d_%d.txt", b, ch, trace);
163  fh = fopen(filename, "w");
164  if (fh == NULL)
165  return -1;
166  for(i=0; i<size; i++)
167  fprintf(fh, "%d\n", WaveData[i]); //&((1<<MAXNBITS)-1)
168  fclose(fh);
169  return 0;
170 }
171 
172 /* --------------------------------------------------------------------------------------------------------- */
178  printf("\ns ) Start acquisition\n");
179  printf("S ) Stop acquisition\n");
180  printf("r ) Restart acquisition\n");
181  printf("q ) Quit\n");
182  printf("t ) Send a software trigger\n");
183  printf("h ) Save Histograms to file\n");
184  printf("w ) Save waveforms to file\n\n\n");
185 }