AlcapDAQ  1
ReadoutTest_DPP_PSD_x720.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 <CAENDigitizer.h>
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 //#define MANUAL_BUFFER_SETTING 0
23 // The following define must be set to the actual number of connected boards
24 #define MAXNB 1
25 // NB: the following define MUST specify the ACTUAL max allowed number of board's channels
26 // it is needed for consistency inside the CAENDigitizer's functions used to allocate the memory
27 #define MaxNChannels 8
28 
29 #define MAXNBITS 12
30 
31 /* include some useful functions from file Functions.h
32 you can find this file in the src directory */
33 #include "Functions.h"
34 
35 /* ###########################################################################
36 * Functions
37 * ########################################################################### */
38 
39 /* --------------------------------------------------------------------------------------------------------- */
43 /* --------------------------------------------------------------------------------------------------------- */
45 {
46  /* This function uses the CAENDigitizer API functions to perform the digitizer's initial configuration */
47  int i, ret = 0;
48 
49  /* Reset the digitizer */
50  ret |= CAEN_DGTZ_Reset(handle);
51 
52  if (ret) {
53  printf("ERROR: can't reset the digitizer.\n");
54  return -1;
55  }
56 
57  /* Set the DPP acquisition mode
58  This setting affects the modes Mixed and List (see CAEN_DGTZ_DPP_AcqMode_t definition for details)
59  CAEN_DGTZ_DPP_SAVE_PARAM_EnergyOnly Only energy (DPP-PHA) or charge (DPP-PSD/DPP-CI v2) is returned
60  CAEN_DGTZ_DPP_SAVE_PARAM_TimeOnly Only time is returned
61  CAEN_DGTZ_DPP_SAVE_PARAM_EnergyAndTime Both energy/charge and time are returned
62  CAEN_DGTZ_DPP_SAVE_PARAM_None No histogram data is returned */
64 
65  // Set the digitizer acquisition mode (CAEN_DGTZ_SW_CONTROLLED or CAEN_DGTZ_S_IN_CONTROLLED)
67 
68  // Set the I/O level (CAEN_DGTZ_IOLevel_NIM or CAEN_DGTZ_IOLevel_TTL)
69  ret |= CAEN_DGTZ_SetIOLevel(handle, Params.IOlev);
70 
71  /* Set the digitizer's behaviour when an external trigger arrives:
72 
73  CAEN_DGTZ_TRGMODE_DISABLED: do nothing
74  CAEN_DGTZ_TRGMODE_EXTOUT_ONLY: generate the Trigger Output signal
75  CAEN_DGTZ_TRGMODE_ACQ_ONLY = generate acquisition trigger
76  CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT = generate both Trigger Output and acquisition trigger
77 
78  see CAENDigitizer user manual, chapter "Trigger configuration" for details */
80 
81  // Set the enabled channels
82  ret |= CAEN_DGTZ_SetChannelEnableMask(handle, Params.ChannelMask);
83 
84  // Set how many events to accumulate in the board memory before being available for readout
85  ret |= CAEN_DGTZ_SetDPPEventAggregation(handle, Params.EventAggr, 0);
86 
87  /* Set the mode used to syncronize the acquisition between different boards.
88  In this example the sync is disabled */
90 
91  // Set the DPP specific parameters for the channels in the given channelMask
92  ret |= CAEN_DGTZ_SetDPPParameters(handle, Params.ChannelMask, &DPPParams);
93 
94  for(i=0; i<MaxNChannels; i++) {
95  if (Params.ChannelMask & (1<<i)) {
96  // Set the number of samples for each waveform (you can set different RL for different channels)
97  ret |= CAEN_DGTZ_SetRecordLength(handle, Params.RecordLength, i);
98 
99  // Set a DC offset to the input signal to adapt it to digitizer's dynamic range
100  ret |= CAEN_DGTZ_SetChannelDCOffset(handle, i, 0x8000);
101 
102  // Set the Pre-Trigger size (in samples)
103  ret |= CAEN_DGTZ_SetDPPPreTriggerSize(handle, i, 80);
104 
105  // Set the polarity for the given channel (CAEN_DGTZ_PulsePolarityPositive or CAEN_DGTZ_PulsePolarityNegative)
106  ret |= CAEN_DGTZ_SetChannelPulsePolarity(handle, i, Params.PulsePolarity);
107  }
108  }
109 
110  /* Set the virtual probes
111 
112  DPP-PSD for x720 boards can save:
113  2 analog waveforms:
114  Analog Trace 1: it is always the input signal;
115  Analog Trace 2: it can be specified with the VIRTUALPROBE parameter
116  4 digital waveforms:
117  Digital Trace 1: it is always the trigger
118  Digital Trace 2: it is always the long gate
119  Digital Trace 3/4: they can be specified with the DIGITALPROBE 1 and 2 parameters
120 
121  DPP-PSD for x751 boards can save:
122  2 analog waveforms:
123  Analog Trace 1: it is always the input signal;
124  Analog Trace 2: it can be specified with the VIRTUALPROBE parameter
125  3 digital waveforms:
126  Digital Trace 1: it is always the trigger
127  Digital Trace 2/3: they can be specified with the DIGITALPROBE 1 and 2 parameters
128 
129  CAEN_DGTZ_DPP_VIRTUALPROBE_SINGLE -> Save only the Input Signal waveform
130  CAEN_DGTZ_DPP_VIRTUALPROBE_DUAL -> Save also the waveform specified in VIRTUALPROBE
131 
132  Virtual Probes types for Trace 2:
133  CAEN_DGTZ_DPP_PSD_VIRTUALPROBE_Baseline -> Save the Baseline waveform (mean on nsbl parameter)
134  ### Virtual Probes only for FW <= 13X.5 ###
135  CAEN_DGTZ_DPP_PSD_VIRTUALPROBE_Threshold -> Save the (Baseline - Threshold) waveform. NOTE: x720 only
136 
137  Digital Probes types for Digital Trace 3(x720)/2(x751):
138  ### Virtual Probes only for FW >= 13X.6 ###
139  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_ExtTrg NOTE: x720 only
140  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_OverThr
141  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_TrigOut
142  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_CoincWin
143  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_PileUp
144  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_Coincidence
145  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_R6_GateLong NOTE: x751 only
146  ### Virtual Probes only for FW <= 13X.5 ###
147  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_Armed NOTE: x720 only
148  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_Trigger NOTE: x720 only
149  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_ChargeReady NOTE: x720 only
150  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_PileUp NOTE: x720 only
151  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_BlOutSafeBand NOTE: x720 only
152  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_BlTimeout NOTE: x720 only
153  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_CoincidenceMet NOTE: x720 only
154  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_Tvaw NOTE: x720 only
155  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_OverThr NOTE: x751 only
156  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_GateShort NOTE: x751 only
157  CAEN_DGTZ_DPP_PSD_DIGITALPROBE1_None NOTE: x751 only
158 
159  Digital Probes types for Digital Trace 4(x720)/3(x751):
160  ### Virtual Probes only for FW >= 13X.6 ###
161  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_GateShort
162  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_OverThr
163  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_TrgVal
164  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_TrgHO
165  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_PileUp
166  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_R6_Coincidence
167  ### Virtual Probes only for FW <= 13X.5 ###
168  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_Armed NOTE: x720 only
169  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_Trigger NOTE: x720 only
170  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_ChargeReady NOTE: x720 only
171  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_PileUp NOTE: x720 only
172  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_BlOutSafeBand NOTE: x720 only
173  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_BlTimeout NOTE: x720 only
174  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_CoincidenceMet NOTE: x720 only
175  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_Tvaw NOTE: x720 only
176  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_GateShort NOTE: x751 only
177  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_GateLong NOTE: x751 only
178  CAEN_DGTZ_DPP_PSD_DIGITALPROBE2_None NOTE: x751 only */
180 
181  if (ret) {
182  printf("Warning: errors found during the programming of the digitizer.\nSome settings may not be executed\n");
183  return ret;
184  } else {
185  return 0;
186  }
187 }
188 
189 /* ########################################################################### */
190 /* MAIN */
191 /* ########################################################################### */
192 int main(int argc, char *argv[])
193 {
194  /* The following variable is the type returned from most of CAENDigitizer
195  library functions and is used to check if there was an error in function
196  execution. For example:
197  ret = CAEN_DGTZ_some_function(some_args);
198  if(ret) printf("Some error"); */
200 
201  /* Buffers to store the data. The memory must be allocated using the appropriate
202  CAENDigitizer API functions (see below), so they must not be initialized here
203  NB: you must use the right type for different DPP analysis (in this case PSD) */
204  char *buffer = NULL; // readout buffer
205  CAEN_DGTZ_DPP_PSD_Event_t *Events[MaxNChannels]; // events buffer
206  CAEN_DGTZ_DPP_PSD_Waveforms_t *Waveform=NULL; // waveforms buffer
207 
208  /* The following variables will store the digitizer configuration parameters */
210  DigitizerParams_t Params[MAXNB];
211 
212  /* Arrays for data analysis */
213  uint64_t PrevTime[MAXNB][MaxNChannels];
214  uint64_t ExtendedTT[MAXNB][MaxNChannels];
215  uint32_t *EHistoShort[MAXNB][MaxNChannels]; // Energy Histograms for short gate charge integration
216  uint32_t *EHistoLong[MAXNB][MaxNChannels]; // Energy Histograms for long gate charge integration
217  float *EHistoRatio[MAXNB][MaxNChannels]; // Energy Histograms for ratio Long/Short
218  int ECnt[MAXNB][MaxNChannels]; // Number-of-Entries Counter for Energy Histograms short and long gate
219  int TrgCnt[MAXNB][MaxNChannels];
220 
221  /* The following variable will be used to get an handler for the digitizer. The
222  handler will be used for most of CAENDigitizer functions to identify the board */
223  int handle[MAXNB];
224 
225  /* Other variables */
226  int i, b, ch, ev;
227  int Quit=0;
228  int AcqRun = 0;
229  uint32_t AllocatedSize, BufferSize;
230  int Nb=0;
231  int DoSaveWave[MAXNB][MaxNChannels];
232  int MajorNumber;
233  int BitMask = 0;
234  uint64_t CurrentTime, PrevRateTime, ElapsedTime;
235  uint32_t NumEvents[MaxNChannels];
236  CAEN_DGTZ_BoardInfo_t BoardInfo;
237 
238  memset(DoSaveWave, 0, MAXNB*MaxNChannels*sizeof(int));
239  for (i=0; i<MAXNBITS; i++)
240  BitMask |= 1<<i; /* Create a bit mask based on number of bits of the board */
241 
242  /* *************************************************************************************** */
243  /* Set Parameters */
244  /* *************************************************************************************** */
245  memset(&Params, 0, MAXNB*sizeof(DigitizerParams_t));
246  memset(&DPPParams, 0, MAXNB*sizeof(CAEN_DGTZ_DPP_PSD_Params_t));
247  for(b=0; b<MAXNB; b++) {
248  for(ch=0; ch<MaxNChannels; ch++) {
249  EHistoShort[b][ch] = NULL; // Set all histograms pointers to NULL (we will allocate them later)
250  EHistoLong[b][ch] = NULL;
251  EHistoRatio[b][ch] = NULL;
252  }
253 
254  /****************************\
255  * Communication Parameters *
256  \****************************/
257  // Direct USB connection
258  //Params[b].LinkType = CAEN_DGTZ_USB; // Link Type
259  //Params[b].VMEBaseAddress = 0; // For direct USB connection, VMEBaseAddress must be 0
260 
261  // Direct optical connection
262  Params[b].LinkType = CAEN_DGTZ_PCI_OpticalLink; // Link Type
263  Params[b].VMEBaseAddress = 0; // For direct CONET connection, VMEBaseAddress must be 0
264 
265  // Optical connection to A2818 (or A3818) and access to the board with VME bus
266  //Params[b].LinkType = CAEN_DGTZ_PCI_OpticalLink; // Link Type (CAEN_DGTZ_PCIE_OpticalLink for A3818)
267  //Params[b].VMEBaseAddress = 0x32100000; // VME Base Address (only for VME bus access; must be 0 for direct connection (CONET or USB)
268 
269  // USB connection to V1718 bridge and access to the board with VME bus
270  //Params[b].LinkType = CAEN_DGTZ_USB; // Link Type (CAEN_DGTZ_PCIE_OpticalLink for A3818)
271  //Params[b].VMEBaseAddress = 0x32110000; // VME Base Address (only for VME bus access; must be 0 for direct connection (CONET or USB)
272 
273  Params[b].IOlev = CAEN_DGTZ_IOLevel_TTL;
274  /****************************\
275  * Acquisition parameters *
276  \****************************/
277  Params[b].AcqMode = CAEN_DGTZ_DPP_ACQ_MODE_Mixed; // CAEN_DGTZ_DPP_ACQ_MODE_List or CAEN_DGTZ_DPP_ACQ_MODE_Oscilloscope
278  Params[b].RecordLength = 12; // Num of samples of the waveforms (only for Oscilloscope mode)
279  Params[b].ChannelMask = 0xF; // Channel enable mask
280  Params[b].EventAggr = 17554; // number of events in one aggregate (0=automatic)
281  Params[b].PulsePolarity = CAEN_DGTZ_PulsePolarityNegative; // Pulse Polarity (this parameter can be individual)
282 
283  /****************************\
284  * DPP parameters *
285  \****************************/
286  for(ch=0; ch<MaxNChannels; ch++) {
287  DPPParams[b].thr[ch] = 50; // Trigger Threshold
288  /* The following parameter is used to specifiy the number of samples for the baseline averaging:
289  0 -> absolute Bl
290  1 -> 4samp
291  2 -> 8samp
292  3 -> 16samp
293  4 -> 32samp
294  5 -> 64samp
295  6 -> 128samp */
296  DPPParams[b].nsbl[ch] = 2;
297  DPPParams[b].lgate[ch] = 32; // Long Gate Width (N*4ns)
298  DPPParams[b].sgate[ch] = 24; // Short Gate Width (N*4ns)
299  DPPParams[b].pgate[ch] = 8; // Pre Gate Width (N*4ns)
300  /* Self Trigger Mode:
301  0 -> Disabled
302  1 -> Enabled */
303  DPPParams[b].selft[ch] = 1;
304  // Trigger configuration:
305  // CAEN_DGTZ_DPP_TriggerConfig_Peak -> trigger on peak. NOTE: Only for FW <= 13X.5
306  // CAEN_DGTZ_DPP_TriggerConfig_Threshold -> trigger on threshold */
307  DPPParams[b].trgc[ch] = CAEN_DGTZ_DPP_TriggerConfig_Threshold;
308  /* Trigger Validation Acquisition Window */
309  DPPParams[b].tvaw[ch] = 50;
310  /* Charge sensibility: 0->40fc/LSB; 1->160fc/LSB; 2->640fc/LSB; 3->2,5pc/LSB */
311  DPPParams[b].csens[ch] = 0;
312  }
313  /* Pile-Up rejection Mode
314  CAEN_DGTZ_DPP_PSD_PUR_DetectOnly -> Only Detect Pile-Up
315  CAEN_DGTZ_DPP_PSD_PUR_Enabled -> Reject Pile-Up */
316  DPPParams[b].purh = CAEN_DGTZ_DPP_PSD_PUR_DetectOnly;
317  DPPParams[b].purgap = 100; // Purity Gap
318  DPPParams[b].blthr = 3; // Baseline Threshold
319  DPPParams[b].bltmo = 100; // Baseline Timeout
320  DPPParams[b].trgho = 8; // Trigger HoldOff
321  }
322 
323  /* *************************************************************************************** */
324  /* Open the digitizer and read board information */
325  /* *************************************************************************************** */
326  /* The following function is used to open the digitizer with the given connection parameters
327  and get the handler to it */
328  for(b=0; b<MAXNB; b++) {
329  /* IMPORTANT: The following function identifies the different boards with a system which may change
330  for different connection methods (USB, Conet, ecc). Refer to CAENDigitizer user manual for more info.
331  Some examples below */
332 
333  /* The following is for b boards connected via b USB direct links
334  in this case you must set Params[b].LinkType = CAEN_DGTZ_USB and Params[b].VMEBaseAddress = 0 */
335  //ret = CAEN_DGTZ_OpenDigitizer(Params[b].LinkType, b, 0, Params[b].VMEBaseAddress, &handle[b]);
336 
337  /* The following is for b boards connected via 1 opticalLink in dasy chain
338  in this case you must set Params[b].LinkType = CAEN_DGTZ_PCI_OpticalLink and Params[b].VMEBaseAddress = 0 */
339  ret = CAEN_DGTZ_OpenDigitizer(Params[b].LinkType, 0, b, Params[b].VMEBaseAddress, &handle[b]);
340 
341  /* The following is for b boards connected to A2818 (or A3818) via opticalLink (or USB with A1718)
342  in this case the boards are accessed throught VME bus, and you must specify the VME address of each board:
343  Params[b].LinkType = CAEN_DGTZ_PCI_OpticalLink (CAEN_DGTZ_PCIE_OpticalLink for A3818 or CAEN_DGTZ_USB for A1718)
344  Params[0].VMEBaseAddress = <0xXXXXXXXX> (address of first board)
345  Params[1].VMEBaseAddress = <0xYYYYYYYY> (address of second board)
346  etc */
347  //ret = CAEN_DGTZ_OpenDigitizer(Params[b].LinkType, 0, 0, Params[b].VMEBaseAddress, &handle[b]);
348  if (ret) {
349  printf("Can't open digitizer\n");
350  goto QuitProgram;
351  }
352 
353  /* Once we have the handler to the digitizer, we use it to call the other functions */
354  ret = CAEN_DGTZ_GetInfo(handle[b], &BoardInfo);
355  if (ret) {
356  printf("Can't read board info\n");
357  goto QuitProgram;
358  }
359  printf("\nConnected to CAEN Digitizer Model %s, recognized as board %d\n", BoardInfo.ModelName, b);
360  printf("ROC FPGA Release is %s\n", BoardInfo.ROC_FirmwareRel);
361  printf("AMC FPGA Release is %s\n", BoardInfo.AMC_FirmwareRel);
362 
363  // Check firmware revision (only DPP firmware can be used with this Demo) */
364  sscanf(BoardInfo.AMC_FirmwareRel, "%d", &MajorNumber);
365  if (MajorNumber != 131 && MajorNumber != 132 ) {
366  printf("This digitizer has not a DPP-PSD firmware\n");
367  goto QuitProgram;
368  }
369  }
370 
371  /* *************************************************************************************** */
372  /* Program the digitizer (see function ProgramDigitizer) */
373  /* *************************************************************************************** */
374  for(b=0; b<MAXNB; b++) {
375  ret = ProgramDigitizer(handle[b], Params[b], DPPParams[b]);
376  if (ret) {
377  printf("Failed to program the digitizer\n");
378  goto QuitProgram;
379  }
380  }
381 
382  /* WARNING: The mallocs MUST be done after the digitizer programming,
383  because the following functions needs to know the digitizer configuration
384  to allocate the right memory amount */
385  /* Allocate memory for the readout buffer */
386  ret = CAEN_DGTZ_MallocReadoutBuffer(handle[0], &buffer, &AllocatedSize);
387  /* Allocate memory for the events */
388  ret |= CAEN_DGTZ_MallocDPPEvents(handle[0], Events, &AllocatedSize);
389  /* Allocate memory for the waveforms */
390  ret |= CAEN_DGTZ_MallocDPPWaveforms(handle[0], &Waveform, &AllocatedSize);
391  if (ret) {
392  printf("Can't allocate memory buffers\n");
393  goto QuitProgram;
394  }
395 
396 
397  /* *************************************************************************************** */
398  /* Readout Loop */
399  /* *************************************************************************************** */
400  // Clear Histograms and counters
401  for(b=0; b<MAXNB; b++) {
402  for(ch=0; ch<MaxNChannels; ch++) {
403  // Allocate Memory for Histos and set them to 0
404  EHistoShort[b][ch] = (uint32_t *)malloc( (1<<MAXNBITS)*sizeof(uint32_t) );
405  memset(EHistoShort[b][ch], 0, (1<<MAXNBITS)*sizeof(uint32_t));
406  EHistoLong[b][ch] = (uint32_t *)malloc( (1<<MAXNBITS)*sizeof(uint32_t) );
407  memset(EHistoLong[b][ch], 0, (1<<MAXNBITS)*sizeof(uint32_t));
408  EHistoRatio[b][ch] = (float *)malloc( (1<<MAXNBITS)*sizeof(float) );
409  memset(EHistoRatio[b][ch], 0, (1<<MAXNBITS)*sizeof(float));
410  TrgCnt[b][ch] = 0;
411  ECnt[b][ch] = 0;
412  PrevTime[b][ch] = 0;
413  ExtendedTT[b][ch] = 0;
414  }
415  }
416  PrevRateTime = get_time();
417  AcqRun = 0;
418  PrintInterface();
419  printf("Type a command: ");
420  while(!Quit) {
421 
422  // Check keyboard
423  if(kbhit()) {
424  char c;
425  c = getch();
426  if (c=='q') Quit = 1;
427  if (c=='t')
428  for(b=0; b<MAXNB; b++)
429  CAEN_DGTZ_SendSWtrigger(handle[b]); /* Send a software trigger to each board */
430  if (c=='h')
431  for(b=0; b<MAXNB; b++)
432  for(ch=0; ch<MaxNChannels; ch++)
433  if( ECnt[b][ch] != 0) {
434  /* Save Histograms to file for each board and channel */
435  SaveHistogram("HistoShort", b, ch, EHistoShort[b][ch]);
436  SaveHistogram("HistoLong", b, ch, EHistoLong[b][ch]);
437  }
438  if (c=='w')
439  for(b=0; b<MAXNB; b++)
440  for(ch=0; ch<MaxNChannels; ch++)
441  DoSaveWave[b][ch] = 1; /* save waveforms to file for each channel for each board (at next trigger) */
442  if (c=='r') {
443  for(b=0; b<MAXNB; b++) {
444  CAEN_DGTZ_SWStopAcquisition(handle[b]);
445  printf("Restarted\n");
446  CAEN_DGTZ_ClearData(handle[b]);
447  CAEN_DGTZ_SWStartAcquisition(handle[b]);
448  }
449  }
450  if (c=='s') {
451  for(b=0; b<MAXNB; b++) {
452  // Start Acquisition
453  // NB: the acquisition for each board starts when the following line is executed
454  // so in general the acquisition does NOT starts syncronously for different boards
455  CAEN_DGTZ_SWStartAcquisition(handle[b]);
456  printf("Acquisition Started for Board %d\n", b);
457  }
458  AcqRun = 1;
459  }
460  if (c=='S') {
461  for(b=0; b<MAXNB; b++) {
462  // Stop Acquisition
463  CAEN_DGTZ_SWStopAcquisition(handle[b]);
464  printf("Acquisition Stopped for Board %d\n", b);
465  }
466  AcqRun = 0;
467  }
468  }
469  if (!AcqRun) {
470  Sleep(10);
471  continue;
472  }
473 
474  /* Calculate throughput and trigger rate (every second) */
475  CurrentTime = get_time();
476  ElapsedTime = CurrentTime - PrevRateTime; /* milliseconds */
477  if (ElapsedTime > 1000) {
478  system(CLEARSCR);
479  PrintInterface();
480  printf("Readout Rate=%.2f MB\n", (float)Nb/((float)ElapsedTime*1048.576f));
481  for(b=0; b<MAXNB; b++) {
482  printf("\nBoard %d:\n",b);
483  for(i=0; i<MaxNChannels; i++) {
484  if (TrgCnt[b][i]>0)
485  printf("\tCh %d:\tTrgRate=%.2f KHz\t%\n", b*8+i, (float)TrgCnt[b][i]/(float)ElapsedTime);
486  else
487  printf("\tCh %d:\tNo Data\n", i);
488  TrgCnt[b][i]=0;
489  }
490  }
491  Nb = 0;
492  PrevRateTime = CurrentTime;
493  printf("\n\n");
494  }
495 
496  /* Read data from the boards */
497  for(b=0; b<MAXNB; b++) {
498  /* Read data from the board */
499  ret = CAEN_DGTZ_ReadData(handle[b], CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, buffer, &BufferSize);
500  if (ret) {
501  printf("Readout Error\n");
502  goto QuitProgram;
503  }
504  if (BufferSize == 0)
505  continue;
506 
507  Nb += BufferSize;
508  //ret = DataConsistencyCheck((uint32_t *)buffer, BufferSize/4);
509  ret |= CAEN_DGTZ_GetDPPEvents(handle[b], buffer, BufferSize, Events, NumEvents);
510  if (ret) {
511  printf("Data Error: %d\n", ret);
512  goto QuitProgram;
513  }
514 
515  /* Analyze data */
516  for(ch=0; ch<MaxNChannels; ch++) {
517  if (!(Params[b].ChannelMask & (1<<ch)))
518  continue;
519 
520  /* Update Histograms */
521  for(ev=0; ev<NumEvents[ch]; ev++) {
522  TrgCnt[b][ch]++;
523  /* Time Tag */
524  if (Events[ch][ev].TimeTag < PrevTime[b][ch])
525  ExtendedTT[b][ch]++;
526  PrevTime[b][ch] = Events[ch][ev].TimeTag;
527  /* Energy */
528  if ( (Events[ch][ev].ChargeLong > 0) && (Events[ch][ev].ChargeShort > 0)) {
529  // Fill the histograms
530  EHistoShort[b][ch][(Events[ch][ev].ChargeShort) & BitMask]++;
531  EHistoLong[b][ch][(Events[ch][ev].ChargeLong) & BitMask]++;
532  ECnt[b][ch]++;
533  }
534 
535  /* Get Waveforms (only from 1st event in the buffer) */
536  if ((Params[b].AcqMode != CAEN_DGTZ_DPP_ACQ_MODE_List) && DoSaveWave[b][ch] && (ev == 0)) {
537  int size;
538  int16_t *WaveLine;
539  uint8_t *DigitalWaveLine;
540  CAEN_DGTZ_DecodeDPPWaveforms(handle[b], &Events[ch][ev], Waveform);
541 
542  // Use waveform data here...
543  size = (int)(Waveform->Ns); // Number of samples
544  WaveLine = Waveform->Trace1; // First trace (for DPP-PSD it is ALWAYS the Input Signal)
545  SaveWaveform(b, ch, 1, size, WaveLine);
546 
547  WaveLine = Waveform->Trace2; // Second Trace (if single trace mode, it is a sequence of zeroes)
548  SaveWaveform(b, ch, 2, size, WaveLine);
549  DoSaveWave[b][ch] = 0;
550 
551  DigitalWaveLine = Waveform->DTrace1; // First Digital Trace (Gate Short)
552  SaveDigitalProbe(b, ch, 1, size, DigitalWaveLine);
553  DoSaveWave[b][ch] = 0;
554 
555  DigitalWaveLine = Waveform->DTrace2; // Second Digital Trace (Gate Long)
556  SaveDigitalProbe(b, ch, 2, size, DigitalWaveLine);
557  DoSaveWave[b][ch] = 0;
558 
559  DigitalWaveLine = Waveform->DTrace3; // Third Digital Trace (DIGITALPROBE1 set with CAEN_DGTZ_SetDPP_PSD_VirtualProbe)
560  SaveDigitalProbe(b, ch, 3, size, DigitalWaveLine);
561  DoSaveWave[b][ch] = 0;
562 
563  DigitalWaveLine = Waveform->DTrace4; // Fourth Digital Trace (DIGITALPROBE2 set with CAEN_DGTZ_SetDPP_PSD_VirtualProbe)
564  SaveDigitalProbe(b, ch, 4, size, DigitalWaveLine);
565  DoSaveWave[b][ch] = 0;
566  printf("Waveforms saved to 'Waveform_<board>_<channel>_<trace>.txt'\n");
567  } // loop to save waves
568  } // loop on events
569  } // loop on channels
570  } // loop on boards
571  } // End of readout loop
572 
573 
574 QuitProgram:
575  /* stop the acquisition, close the device and free the buffers */
576  for(b=0; b<MAXNB; b++) {
577  CAEN_DGTZ_SWStopAcquisition(handle[b]);
578  CAEN_DGTZ_CloseDigitizer(handle[b]);
579  for (ch=0; ch<MaxNChannels; ch++) {
580  free(EHistoShort[b][ch]);
581  free(EHistoLong[b][ch]);
582  free(EHistoRatio[b][ch]);
583  }
584  }
586  CAEN_DGTZ_FreeDPPEvents(handle[0], Events);
587  CAEN_DGTZ_FreeDPPWaveforms(handle[0], Waveform);
588  return ret;
589 }
590