-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
executable file
·430 lines (375 loc) · 15.7 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/******************************************************************************
*
* CAEN SpA - Front End Division
* Via Vetraia, 11 - 55049 - Viareggio ITALY
* +390594388398 - www.caen.it
*
***************************************************************************//**
* \note TERMS OF USE:
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation. This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The user relies on the
* software, documentation and results solely at his own risk.
******************************************************************************/
#define DAWrunner_Release "1.0"
#define DAWrunner_Release_Date "May 2017"
#include "CAENDigitizer.h"
#include "CAENDigitizerType.h"
#include "DAWDemoFunc.h"
#include <time.h>
#include <sys/stat.h>
//code added by chengjie
#include <unistd.h>
char path[256];
static long get_time()
{
long time_ms;
#ifdef WIN32
struct _timeb timebuffer;
_ftime(&timebuffer);
time_ms = (long)timebuffer.time * 1000 + (long)timebuffer.millitm;
#else
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
time_ms = (t1.tv_sec) * 1000 + t1.tv_usec / 1000;
#endif
return time_ms;
}
int main(int argc, char *argv[])
{
uint32_t ev ;
int board, chan; // loop indices
int *handle = NULL;// pointer to handles
ERROR_CODES ErrCode = ERR_NONE;
// Data and config structures
CAEN_DGTZ_730_DAW_Event_t **Event = NULL; // as many events as the maximum number obtainable with a BLT transfer access must be allocated
DAWPlot_t PlotVar; // struct containing the plot options
DAWConfig_t ConfigVar; // struct containing the config file options
// File I/O and related variables
FILE **RawFile;
FILE ***WaveFile;
FILE *f_ini = NULL;
int *RawFileIndex;
struct stat buf;
int FileIndex = 0;
uint64_t PrevCheckTime; // printout and plot time
Counter_t *Counter, *CounterOld;
int PrintFlag=0,PlotFlag=0;
uint32_t AllocatedSize,BufferSize;
char *buffer = NULL;
char ConfigFileName[255];
char tmpConfigFileName[512];
uint32_t *NumEvents;
int EventPlotted=-1;
uint32_t BLTn,MaxBLTn=0,MaxBLTnHIndex;
uint32_t MSize, MaxMSize=0;
int MaxMSizeHIndex;
struct stat info;
uint32_t BoardInfo;
// code added by chengjie
printf("argc number : %d\n",argc);
printf("**************************************************************\n");
printf(" X725/X730 DAW Demo %s\n", DAWrunner_Release);
printf("**************************************************************\n");
#ifdef WIN32
sprintf(path, "%s\\DAW_DEMO\\", getenv("USERPROFILE"));
_mkdir(path);
#else
sprintf(path," ");
#endif
// Open and parse configuration file, init parameters
if (argc > 1) strcpy(ConfigFileName, argv[1]);
else {
strcpy(tmpConfigFileName, DEFAULT_CONFIG_FILE);
sprintf(ConfigFileName, "%s%s", path, tmpConfigFileName);
}
ErrCode = OpenConfigFile(&f_ini, ConfigFileName);
// new code added by Chengjie .
int design_time ;
int time_designed = 0 ; // 0 means disable
if (argc > 2)
{
design_time= atoi(argv[2]);
printf("Designed run_time %d s \n",design_time);
time_designed=1;
}
if (ErrCode == ERR_NONE) ErrCode = ParseConfigFile(f_ini, &ConfigVar);
// Parse Config File will calculate the Configvar.Nhandle
if (f_ini!=NULL) fclose(f_ini);
// Allocate space for handles and counters according to the number of boards in the acquisition chain
if (ErrCode == ERR_NONE) {
if (((handle = (int*)calloc(ConfigVar.Nhandle,sizeof(int))) == NULL) ||
((Counter = (Counter_t*)calloc(ConfigVar.Nhandle, sizeof(Counter_t))) == NULL) ||
((CounterOld = (Counter_t*)calloc(ConfigVar.Nhandle, sizeof(Counter_t))) == NULL) ||
((NumEvents = (uint32_t*)calloc(ConfigVar.Nhandle, sizeof(int))) == NULL) ||
((Event = (CAEN_DGTZ_730_DAW_Event_t**)calloc(ConfigVar.Nhandle, sizeof(CAEN_DGTZ_730_DAW_Event_t*))) == NULL) ||
((RawFileIndex = (int*)calloc(ConfigVar.Nhandle, sizeof(int))) == NULL) ||
((RawFile = (FILE**)calloc(ConfigVar.Nhandle, sizeof(FILE*))) == NULL) ||
((WaveFile = (FILE***)calloc(ConfigVar.Nhandle, sizeof(FILE**))) == NULL)
) ErrCode = ERR_MALLOC;
}
// Open the digitizer and read the board information
if (ErrCode == ERR_NONE) {
printf("Open digitizers\n");
if (OpenDigitizer(handle, &ConfigVar)) ErrCode = ERR_DGZ_OPEN;
}
// Print board info and set board-specific parameters
if (ErrCode == ERR_NONE) {
printf("Get board info and set board-specific parameters\n");
for (board = 0; board < ConfigVar.Nhandle; board++) {
if (CAEN_DGTZ_GetInfo(handle[board],&ConfigVar.BoardConfigVar[board]->BoardInfo)) ErrCode = ERR_BOARD_INFO_READ;
else {
printf("***************************************\n");
printf("Connected to CAEN Digitizer Model %s\n", ConfigVar.BoardConfigVar[board]->BoardInfo.ModelName);
printf("Board serial number %d\n", ConfigVar.BoardConfigVar[board]->BoardInfo.SerialNumber);
printf("ROC FPGA Release is %s\n", ConfigVar.BoardConfigVar[board]->BoardInfo.ROC_FirmwareRel);
printf("AMC FPGA Release is %s\n", ConfigVar.BoardConfigVar[board]->BoardInfo.AMC_FirmwareRel);
CAEN_DGTZ_ReadRegister(handle[board], 0x108C, &BoardInfo);
if (((BoardInfo >> 8) & 0xff) != DAW_FW_ID) { ErrCode = ERR_WRONG_FW; break; }
}
}
printf("***************************************\n");
}
// Program the digitizer
if (ErrCode == ERR_NONE) {
printf("Program digitizers\n");
if (ProgramDigitizers(handle, &ConfigVar)) ErrCode = ERR_DGZ_PROGRAM;
for (board = 0; board < ConfigVar.Nhandle; board++) {
// get the handles with the highest values of event size, events/block transfer
CAEN_DGTZ_GetMaxNumEventsBLT(handle[board], &BLTn); if (BLTn > MaxBLTn) { BLTn = MaxBLTn; MaxBLTnHIndex = board; }
if ((MSize = CheckMallocSize(handle[board])) > MaxMSize) { MaxMSize = MSize; MaxMSizeHIndex = board; }
}
}
// Open the output files
if (ErrCode == ERR_NONE) {
if (stat(ConfigVar.OutFilePath, &info) != 0) {
#ifdef WIN32
if (_mkdir(ConfigVar.OutFilePath) != 0) { printf("Output directory %s could not be created. Please verify that the path exists and is writable\n", ConfigVar.OutFilePath); ErrCode = ERR_OUTDIR_OPEN; }
else printf("Output directory %s created\n", ConfigVar.OutFilePath);
#else
if (mkdir(ConfigVar.OutFilePath, 0777) != 0) { printf("Output directory %s could not be created. Please verify that the path exists and is writable\n", ConfigVar.OutFilePath); ErrCode = ERR_OUTDIR_OPEN; }
else printf("Output directory %s created\n", ConfigVar.OutFilePath);
#endif
}
if (ErrCode == ERR_NONE) {
for (board = 0; board < ConfigVar.Nhandle; board++) {
RawFileIndex[board] = 0;
if (ConfigVar.OFRawEnable) { if ((ErrCode = OpenRawFile(RawFile + board, board, RawFileIndex[board], ConfigVar.OutFilePath, ConfigVar.OutFileName)) != ERR_NONE) break; }
if (ConfigVar.OFWaveEnable) {
if ((*(WaveFile + board) = (FILE**)calloc(ConfigVar.BoardConfigVar[board]->BoardInfo.Channels, sizeof(FILE*))) == NULL) { ErrCode = ERR_MALLOC; break; }
if ((ErrCode = OpenWaveFile(WaveFile + board, board, ConfigVar.BoardConfigVar[board], ConfigVar.OutFilePath, ConfigVar.OutFileName)) != ERR_NONE) break;
}
}
}
}
// WARNING: The mallocs must be done after the digitizer programming
// Allocate memory for the readout buffer
if (ErrCode == ERR_NONE) {
printf("Readout buffer malloc\n");
if (CAEN_DGTZ_MallocReadoutBuffer(handle[MaxMSizeHIndex], &buffer, &AllocatedSize)) ErrCode = ERR_MALLOC;
}
// Allocate memory for the events
if (ErrCode == ERR_NONE) {
printf("Event malloc\n");
for (board = 0; board < ConfigVar.Nhandle; board++) {
if (CAEN_DGTZ_MallocDPPEvents(handle[MaxBLTnHIndex], (void**)&Event[board], &AllocatedSize)) ErrCode = ERR_MALLOC;
}
}
// reset counters
if(ErrCode==ERR_NONE) {
for (board = 0; board < ConfigVar.Nhandle; board++) {
ResetCounter(Counter + board);
ResetCounter(CounterOld + board);
}
}
// No memory allocation for waveform required in this firmware
// Open the plotter and configure its options
if (ErrCode == ERR_NONE) {
printf("Open plotter\n");
ErrCode = OpenPlotter(&ConfigVar, &PlotVar);
}
// Readout Loop
if (ErrCode == ERR_NONE) {
printf("[s] start/stop the acquisition, [q] quit, [space key] help\n");
int auto_already=0 ;
int auto_stop=0 ;
int start_time= time((time_t *)NULL);
int useless_count=1;
int useless_set=0;
if (argc>3)
{
useless_set=atoi(argv[3]);
printf("useless_data delete %d\n",useless_set);
}
printf("start acquisition time : %d\n",start_time);
PrevCheckTime = get_time();
while (!ConfigVar.Quit) {
CheckKeyboardCommands(handle, &ConfigVar);
if (UpdateTime(ConfigVar.PlotRefreshTime, &PrevCheckTime) && ConfigVar.AcqRun) PrintFlag = 1;
// if continuous trigger is enabled, send software triggers
if (ConfigVar.ContTrigger) for (board = 0; board < ConfigVar.Nhandle; board++) CAEN_DGTZ_SendSWtrigger(handle[board]);
// Read data from the board
for (board = 0; board < ConfigVar.Nhandle; board++) {
// Nhandle is the number of target boards .
if (CAEN_DGTZ_ReadData(handle[board], CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, buffer, &BufferSize)) {
ErrCode = ERR_READOUT;
break;
}
if (board == ConfigVar.BoardPlotted) EventPlotted = -1;
if (BufferSize > 0) { // The BLT returned data
Counter[board].ByteCnt += BufferSize;
// allocate the buffered data in the struct and return the number of events
if (CAEN_DGTZ_GetDPPEvents(handle[board], buffer, BufferSize / 4, (void**)&Event[board], &NumEvents[board])) {
ErrCode = ERR_EVENT_BUILD;
break;
}
Counter[board].MB_TS = Event[board][NumEvents[board] - 1].timeStamp;
//Counter[board].TrgCnt += NumEvents[board];
if (ConfigVar.OFRawEnable)
{
if (useless_count>useless_set)
{
fwrite(buffer, BufferSize, 1, RawFile[board]);
}
else
{
useless_count=useless_count+1;
}
}
} else {
NumEvents[board] = 0;
continue; // the BLT returned no data, acquisition still on
}
// check file size and open new file if the file size is larger than the value set in the config file
if (RawFile[board] != NULL) {
fstat(fileno(RawFile[board]), &buf);
if ((int)(buf.st_size / MB_SIZE) > ConfigVar.MaxFileSize)
{ RawFileIndex[board]++;
OpenRawFile(&RawFile[board], board, RawFileIndex[board], ConfigVar.OutFilePath, ConfigVar.OutFileName);
}
}
// Analyze data for each event
for (ev = 0; ev < NumEvents[board]; ev++) {
for (chan = 0; chan < MAX_V1730_CHANNEL_SIZE; chan++) {
if (Event[board][ev].chmask & (1 << chan)) {
// add events
Counter[board].TrgCnt[chan]++;
// increase truncate counters if the related header flag is set
if (Event[board][ev].Channel[chan]->truncate) Counter[board].OFCnt[chan]++;
// remember the last event of the buffer where the plotted channel is present
if ((board == ConfigVar.BoardPlotted) && (chan == ConfigVar.EnableTrack)) EventPlotted = ev;
}
}
}
}
// Print event info for the selected board and channel
if ((PrintFlag || ConfigVar.SinglePlot) && ConfigVar.AcqRun) {
// if plotflag is still 1 no event from the channel selected for plotting was found
if(PlotFlag==1 && Counter[ConfigVar.BoardPlotted].ByteCnt!=CounterOld[ConfigVar.BoardPlotted].ByteCnt) {
printf("The channel selected for plotting (board #%d, channel #%d) was not present in data\n", ConfigVar.BoardPlotted, ConfigVar.EnableTrack);
PlotFlag = 0;
}
printf("==========================================\n");
for (board = 0; board < ConfigVar.Nhandle; board++) {
printf("Board %d", board); if (board == ConfigVar.BoardPlotted) printf("(plotted): \n"); else printf(" : \n");
PrintData(Counter + board, CounterOld + board,&ConfigVar);
for (chan = 0; chan < MAX_V1730_CHANNEL_SIZE; chan++) {
if ((ConfigVar.BoardConfigVar[board]->EnableMask & (1 << chan)) && (Counter[board].OFCnt[chan] != CounterOld[board].OFCnt[chan])) {
printf("fraction of truncated events in board %d, channel %d: %.2f%%\n", board, chan, (float)(100 * (Counter[board].OFCnt[chan] - CounterOld[board].OFCnt[chan])) / (float)NumEvents[board]);
}
}
// save waves
if (ConfigVar.OFWaveEnable ) {
if (useless_count>useless_set)
{
OpenWaveFile(WaveFile + board, board, ConfigVar.BoardConfigVar[board], ConfigVar.OutFilePath, ConfigVar.OutFileName);
WaveWrite(*(WaveFile + board), *(Event + board), ConfigVar.BoardConfigVar[board]);
}
else
{
useless_count=useless_count+1;
}
}
}
PrintFlag = 0;
if(ConfigVar.PlotEnable || ConfigVar.SinglePlot) PlotFlag = 1;
ConfigVar.SinglePlot = 0; // reset one-shot plot ("p" key during acquisition)
}
if (PlotFlag==1) { // if no event from the selected channel has been found, PlotFlag remains active
if ((~ConfigVar.BoardConfigVar[ConfigVar.BoardPlotted]->EnableMask) & (1 << ConfigVar.EnableTrack)) {
printf("The channel selected for plotting (board #%d, channel #%d) is not enabled\n", ConfigVar.BoardPlotted, ConfigVar.EnableTrack);
PlotFlag = 0;
} else if (EventPlotted != -1) {
PlotEvent(&ConfigVar, &PlotVar, &Event[ConfigVar.BoardPlotted][EventPlotted]);
PlotFlag = 0;
}
}
if (auto_stop==1)
{
ConfigVar.Quit=1;
}
if (argc>2 && auto_already==0 )
{
// code added by Chengjie
int i ;
ConfigVar.Quit=0;
ConfigVar.AcqRun=1;
sleep(5);
for (i = 0; i < ConfigVar.Nhandle; i++)
{
if(ConfigVar.BoardConfigVar[i]->StartMode == CAEN_DGTZ_SW_CONTROLLED)
{
CAEN_DGTZ_SWStartAcquisition(handle[i]);
}
}
auto_already=1;
usleep(500);
}
// new code added by Chengjie
if (argc>2)
{
int now_time=time((time_t *)NULL);
int i ;
// printf("%d\n",now_time);
if ( now_time > start_time + design_time)
{
for (i = 0; i < ConfigVar.Nhandle; i++)
{
if (ConfigVar.BoardConfigVar[i]->StartMode == CAEN_DGTZ_SW_CONTROLLED)
{CAEN_DGTZ_SWStopAcquisition(handle[i]); }
}
// ConfigVar.Quit=1;
ConfigVar.AcqRun=0;
auto_stop=1;
// We now wait for the next loop to stop
}
}
}
}
if (ErrCode) {
printf("\a%s\n", ErrMsg[ErrCode]);
printf("Press a key to quit\n");
getch();
}
// stop the acquisition
if (ErrCode != ERR_DGZ_OPEN) for (board = 0; board < ConfigVar.Nhandle; board++) CAEN_DGTZ_SWStopAcquisition(handle[board]);
// close the output files (if allocated)
for (board = 0; board < ConfigVar.Nhandle; board++) {
if (RawFile[board] != NULL) fclose(RawFile[board]);
}
// close the plotter
if (PlotVar.plotpipe != NULL) ClosePlotter(&PlotVar.plotpipe);
// free buffers (if allocated)
if (buffer != NULL) CAEN_DGTZ_FreeReadoutBuffer(&buffer);
for (board = 0; board < ConfigVar.Nhandle; board++) {
if (Event[board] != NULL) {
// free events and waveforms (if allocated)
CAEN_DGTZ_FreeDPPEvents(handle[MaxBLTnHIndex],(void**)&Event[board]);
}
}
if (ErrCode != ERR_DGZ_OPEN) for (board = 0; board < ConfigVar.Nhandle; board++) CAEN_DGTZ_CloseDigitizer(handle[board]);
return 0;
}