-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartdaq.c
662 lines (573 loc) · 29.6 KB
/
startdaq.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*----------------------------------------------------------------------
* Copyright (c) 2017 XIA LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of XIA LLC
* nor the names of its contributors may be used to endorse
* or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*----------------------------------------------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/file.h>
// need to compile with -lm option
#include "PixieNetDefs.h"
#include "PixieNetCommon.h"
#include "PixieNetConfig.h"
int main(int argc, char *argv[]) {
int fd;
void *map_addr;
int size = 4096;
volatile unsigned int *mapped;
int k, ch, tmpS;
FILE * filmca;
FILE * fil;
const char *lm_file = "LMdata.bin";
unsigned int Accept, RunType, SyncT, ReqRunTime, PollTime, CW;
unsigned int SL[NCHANNELS];
//unsigned int SG[NCHANNELS];
float Tau[NCHANNELS], Dgain[NCHANNELS];
unsigned int BLavg[NCHANNELS], BLcut[NCHANNELS], Binfactor[NCHANNELS], TL[NCHANNELS];
double C0[NCHANNELS], C1[NCHANNELS], Cg[NCHANNELS];
double baseline[NCHANNELS] = {0};
double dt, ph, tmpD, bscale;
double elm, q;
double cfdlev;
time_t starttime, currenttime;
unsigned int startTS, w0, w1, tmpI, revsn, scale14B;
//double m, c0, c1, c2, c3;
unsigned int evstats, R1, hit, timeL, timeH, psa0, psa1, cfd0;
unsigned int psa_base, psa_Q0, psa_Q1, psa_ampl, psa_R;
unsigned int cfdout, cfdlow, cfdhigh, cfdticks, cfdfrac, ts_max;
unsigned int lsum, tsum, gsum, energy, bin, binx, biny;
unsigned int mca[NCHANNELS][MAX_MCA_BINS] ={{0}}; // full MCA for end of run
unsigned int wmca[NCHANNELS][WEB_MCA_BINS] ={{0}}; // smaller MCA during run
unsigned int mca2D[NCHANNELS][MCA2D_BINS*MCA2D_BINS] ={{0}}; // 2D MCA for end of run
unsigned int onlinebin;
unsigned int wf[MAX_TL/2]; // two 16bit values per word
unsigned int chaddr, loopcount, eventcount, NumPrevTraceBlks, TraceBlks;
unsigned short buffer1[FILE_HEAD_LENGTH_400] = {0};
unsigned char buffer2[CHAN_HEAD_LENGTH_400*2] = {0};
unsigned int wm = WATERMARK;
unsigned int BLbad[NCHANNELS];
onlinebin=MAX_MCA_BINS/WEB_MCA_BINS;
if (argc==2) lm_file = argv[1];
// ******************* read ini file and fill struct with values ********************
PixieNetFippiConfig fippiconfig; // struct holding the input parameters
const char *defaults_file = "defaults.ini";
int rval = init_PixieNetFippiConfig_from_file( defaults_file, 0, &fippiconfig ); // first load defaults, do not allow missing parameters
if( rval != 0 )
{
printf( "Failed to parse FPGA settings from %s, rval=%d\n", defaults_file, rval );
return rval;
}
const char *settings_file = "settings.ini";
rval = init_PixieNetFippiConfig_from_file( settings_file, 2, &fippiconfig ); // second override with user settings, do allow missing, no warning
if( rval != 0 )
{
printf( "Failed to parse FPGA settings from %s, rval=%d\n", settings_file, rval );
return rval;
}
// assign to local variables, including any rounding/discretization
Accept = fippiconfig.ACCEPT_PATTERN;
RunType = fippiconfig.RUN_TYPE;
SyncT = fippiconfig.SYNC_AT_START;
ReqRunTime = fippiconfig.REQ_RUNTIME;
PollTime = fippiconfig.POLL_TIME;
CW = (int)floor(fippiconfig.COINCIDENCE_WINDOW*FILTER_CLOCK_MHZ); // multiply time in us * # ticks per us = time in ticks
if( (RunType==0x503) || (RunType==0x402) ) { // grouped list mode run (equiv 0x402)
printf( "This function does not support LM runtypes 0x503 or 0x402, use coincdaq or acquire\n");
return(-1);
}
for( k = 0; k < NCHANNELS; k ++ )
{
SL[k] = (int)floor(fippiconfig.ENERGY_RISETIME[k]*FILTER_CLOCK_MHZ); // multiply time in us * # ticks per us = time in ticks
// SG[k] = (int)floor(fippiconfig.ENERGY_FLATTOP[k]*FILTER_CLOCK_MHZ); // multiply time in us * # ticks per us = time in ticks
Dgain[k] = fippiconfig.DIG_GAIN[k];
TL[k] = BLOCKSIZE_400*(int)floor(fippiconfig.TRACE_LENGTH[k]*ADC_CLK_MHZ/BLOCKSIZE_400); // multiply time in us * # ticks per us = time in ticks, multiple of 4
Binfactor[k] = fippiconfig.BINFACTOR[k];
Tau[k] = fippiconfig.TAU[k];
BLcut[k] = fippiconfig.BLCUT[k];
BLavg[k] = 65536 - fippiconfig.BLAVG[k];
if(BLavg[k]<0) BLavg[k] = 0;
if(BLavg[k]==65536) BLavg[k] = 0;
if(BLavg[k]>MAX_BLAVG) BLavg[k] = MAX_BLAVG;
BLbad[k] = MAX_BADBL; // initialize to indicate no good BL found yet
}
// *************** PS/PL IO initialization *********************
// open the device for PD register I/O
fd = open("/dev/uio0", O_RDWR);
if (fd < 0) {
perror("Failed to open devfile");
return -2;
}
//Lock the PL address space so multiple programs cant step on eachother.
if( flock( fd, LOCK_EX | LOCK_NB ) )
{
printf( "Failed to get file lock on /dev/uio0\n" );
return -3;
}
map_addr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map_addr == MAP_FAILED) {
perror("Failed to mmap");
return -4;
}
mapped = (unsigned int *) map_addr;
// --------------------------------------------------------
// ------------------- Main code begins --------------------
// --------------------------------------------------------
// ********************** Compute Coefficients for E Computation **********************
revsn = hwinfo(mapped);
// energy filters for 14bit version derive from 4x larger ADC samples, but should map to same E in MCA, so divide result by 4
if ( (((revsn>>16) & 0xFFFF) == PN_BOARD_VERSION_12_250_A) ||
(((revsn>>16) & 0xFFFF) == PN_BOARD_VERSION_12_250_B) ||
(((revsn>>16) & 0xFFFF) == PN_BOARD_VERSION_12_250_B_PTP) )
{
//printf("Using E scale for 12 bit version");
scale14B = 1;
}
else
{
//printf("Using E scale for 14 bit version");
scale14B = 4;
}
dt = 1.0/FILTER_CLOCK_MHZ;
for( k = 0; k < NCHANNELS; k ++ )
{
q = exp(-1.0*dt/Tau[k]);
elm = exp(-1.0*dt*SL[k]/Tau[k]);
C0[k] = (q-1.0)*elm/(1.0-elm);
Cg[k] = 1.0-q;
C1[k] = (1.0-q)/(1.0-elm);
// printf("%f %f %f\n", C0[k], Cg[k], C1[k]);
C0[k] = C0[k] * Dgain[k] / scale14B;
Cg[k] = Cg[k] * Dgain[k] / scale14B;
C1[k] = C1[k] * Dgain[k] / scale14B;
}
// ********************** Run Start **********************
NumPrevTraceBlks = 0;
loopcount = 0;
eventcount = 0;
starttime = time(NULL); // capture OS start time
currenttime = starttime;
if( (RunType==0x500) || (RunType==0x501) || (RunType==0x502) || (RunType==0x400) ) { // list mode runtypes
if(SyncT) mapped[ARTC_CLR] = 1; // write to reset time counter
mapped[AOUTBLOCK] = OB_RSREG;
usleep(15); // runstats only update very ~10us or so
startTS = mapped[AREALTIME+1]; // need to accommodate the address offset
if(RunType==0x500) { // generic runtype is one value per line
if (argc==2) fil = fopen(lm_file,"w");
else fil = fopen("LMdata.txt","w");
fprintf(fil, "Module:\t%hu\n",0);
fprintf(fil, "Run Type:\t0x%x\n",RunType);
fprintf(fil,"Run Start Time Stamp (ticks) :\t%u\n", startTS); //
fprintf(fil,"Run Start Time (s) :\t%lld\n", (long long)starttime); // this is only precise to a second or so
}
if(RunType==0x501){ // compressed runtype has columns
if (argc==2) fil = fopen(lm_file,"w");
else fil = fopen("LMdata.dat","w");
fprintf(fil,"Module,Run_Type,Run_Start_ticks,Run_Start_sec,Unused1,Unused2\n");
fprintf(fil,"%d,0x%x,%u,%lld,--,--\n",0,RunType,startTS,(long long)(starttime));
fprintf(fil,"No,Ch,Hit,Time_H,Time_L,Energy\n");
}
if(RunType==0x502){ // compressed PSA runtype has more columns
if (argc==2) fil = fopen(lm_file,"w");
else fil = fopen("LMdata.dt2","w");
fprintf(fil,"Module,Run_Type,Run_Start_ticks,Run_Start_sec,Unused1,Unused2\n");
fprintf(fil,"%d,0x%x,%u,%lld,--,--\n",0,RunType,startTS,(long long)(starttime));
fprintf(fil,"Event_No,Channel_No,Hit_Pattern,Event_Time_H,Event_Time_L,Energy,Amplitude,CFD,Base,Q0,Q1,PSAvalue\n");
}
if(RunType==0x400){
// write a 0x400 header
// fwrite is slow so we will write to a buffer, and then to the file.
if (argc==2) fil = fopen(lm_file,"wb");
else fil = fopen("LMdata.b00","wb");
buffer1[0] = BLOCKSIZE_400;
buffer1[1] = 0; // module number (get from settings file?)
buffer1[2] = RunType;
buffer1[3] = CHAN_HEAD_LENGTH_400;
buffer1[4] = fippiconfig.COINCIDENCE_PATTERN;
buffer1[5] = fippiconfig.COINCIDENCE_WINDOW;
buffer1[7] = revsn>>16; // HW revision from EEPROM
buffer1[12] = revsn & 0xFFFF; // serial number from EEPROM
for( ch = 0; ch < NCHANNELS; ch++) {
buffer1[6] +=(int)floor((TL[ch] + CHAN_HEAD_LENGTH_400) / BLOCKSIZE_400); // combined event length, in blocks
buffer1[8+ch] =(int)floor((TL[ch] + CHAN_HEAD_LENGTH_400) / BLOCKSIZE_400); // each channel's event length, in blocks
}
fwrite( buffer1, 2, FILE_HEAD_LENGTH_400, fil ); // write to file
}
}
mapped[ADSP_CLR] = 1; // write to reset DAQ buffers
mapped[ACOUNTER_CLR] = 1; // write to reset RS counters
mapped[ACSRIN] = 1; // set RunEnable bit to start run
mapped[AOUTBLOCK] = OB_EVREG; // read from event registers
// ********************** Run Loop **********************
do {
//----------- Periodically read BL and update average -----------
// this will be moved into the FPGA soon
if(loopcount % BLREADPERIOD == 0) { //|| (loopcount ==0) ) { // sometimes 0 mod N not zero and first few events have wrong E? watch
for( ch=0; ch < NCHANNELS; ch++) {
// read raw BL sums
chaddr = ch*16+16;
lsum = mapped[chaddr+CA_LSUMB];
tsum = mapped[chaddr+CA_TSUMB];
gsum = mapped[chaddr+CA_GSUMB];
if (tsum>0) // tum=0 indicates bad baseline
{
ph = C1[ch]*lsum+Cg[ch]*gsum+C0[ch]*tsum;
//if (ch==0) printf("ph %f, BLcut %d, BLavg %d, baseline %f\n",ph,BLcut[ch],BLavg[ch],baseline[ch] );
if( (BLcut[ch]==0) || (abs(ph-baseline[ch])<BLcut[ch]) || (BLbad[ch] >=MAX_BADBL) ) // only accept "good" baselines < BLcut, or if too many bad in a row (to start over)
{
if( (BLavg[ch]==0) || (BLbad[ch] >=MAX_BADBL) )
{
baseline[ch] = ph;
BLbad[ch] = 0;
} else {
// BL average: // avg = old avg + (new meas - old avg)/2^BLavg
baseline[ch] = baseline[ch] + (ph-baseline[ch])/(1<<BLavg[ch]);
BLbad[ch] = 0;
} // end BL avg
} else {
BLbad[ch] = BLbad[ch]+1;
} // end BLcut check
} // end tsum >0 check
} // end for loop
} // end periodicity check
// -----------poll for events -----------
// if data ready. read out, compute E, increment MCA *********
evstats = mapped[AEVSTATS];
// printf("EVstats 0x%x\n",evstats);
if(evstats) { // if there are events in any channel
for( ch=0; ch < NCHANNELS; ch++)
{
R1 = 1 << ch;
if(evstats & R1) { // check if there is an event in the FIFO
// read hit pattern and status info
chaddr = ch*16+16;
hit = mapped[chaddr+CA_HIT];
// printf("channel %d, hit 0x%x\n",ch,hit);
if(hit & Accept) {
// read data not needed for pure MCA runs
timeL = mapped[chaddr+CA_TSL];
timeH = mapped[chaddr+CA_TSH];
psa0 = mapped[chaddr+CA_PSAA]; // Q0raw/4 | B
psa1 = mapped[chaddr+CA_PSAB]; // M | Q1raw/4
cfd0 = mapped[chaddr+CA_CFDA]; // {ts_max[6:3],cfdticks[3:0],cfdhigh[11:0],cfdlow[11:0]}
//cfd1 = mapped[chaddr+CA_CFDB];
//printf("channel %d, hit 0x%x, timeL %d\n",ch,hit,timeL);
// read raw energy sums
lsum = mapped[chaddr+CA_LSUM]; // leading, larger, "S1", past rising edge
tsum = mapped[chaddr+CA_TSUM]; // trailing, smaller, "S0" before rising edge
gsum = mapped[chaddr+CA_GSUM]; // gap sum, "Sg", during rising edge; also advances FIFO and increments Nout etc
// compute and histogram E
ph = C1[ch]*(double)lsum+Cg[ch]*(double)gsum+C0[ch]*(double)tsum;
// printf("ph %f, BLavg %f, E %f\n",ph,baseline[ch], ph-baseline[ch]);
ph = ph-baseline[ch];
if ((ph<0.0)|| (ph>65536.0)) ph =0.0; // out of range energies -> 0
energy = (int)floor(ph);
if ((hit & (1<< HIT_LOCALHIT))==0) energy =0; // not a local hit -> 0
// histogramming if E< max mcabin
bin = energy >> Binfactor[ch];
if( (bin<MAX_MCA_BINS) && (bin>0) ) {
mca[ch][bin] = mca[ch][bin] + 1; // increment mca
bin = bin >> WEB_LOGEBIN;
if(bin>0) wmca[ch][bin] = wmca[ch][bin] + 1; // increment wmca
// TODO: add split spectrum n.g for 0x502
}
// cfd and psa need some recomputation, not fully implemented yet
// compute PSA results from raw data
// need to subtract baseline in correct scale (1/4) and length (QDC#_LENGTH[ch])
psa_base = psa0 & 0xFFFF; // base only, in same scale as ADC samples
if( fippiconfig.QDC_DIV8[ch])
bscale = 32.0;
else
bscale = 4.0;
tmpI = (psa0 & 0xFFFF0000) >> 16; // raw Q0, scaled by 1/4, not BL corrected
tmpD = (double)tmpI - (double)psa_base/bscale * fippiconfig.QDC0_LENGTH[ch]; // subtract QDCL0 x base/bscale from raw value
if( (tmpD>0) && (tmpD<65535))
psa_Q0 = (int)floor(tmpD);
else
psa_Q0 = 0;
tmpI = (psa1 & 0xFFFF); // raw Q1, scaled by 1/4, not BL corrected
tmpD = (double)tmpI - (double)psa_base/bscale * fippiconfig.QDC1_LENGTH[ch]; // subtract QDCL0 x base/bscale from raw value
if( (tmpD>0) && (tmpD<65535))
psa_Q1 = (int)floor(tmpD);
else
psa_Q1 = 0;
psa_ampl = ((psa1 & 0xFFFF0000) >> 16) - psa_base;
//if(eventcount<10) printf("psa0 0x%x, psa1 0x%x\n",psa0,psa1);
if(psa_Q0!=0)
psa_R = (int)floor(1000.0*(double)psa_Q1/(double)psa_Q0);
else
psa_R = 0;
// compute CFD fraction
// Normally x = dt * (cfd level - cfd low) / (cfd high - cfd low) = time after lower sample
// However, here CFD is latched before time stamp and we need to compute CFD time BEFORE timestamp.
// So we are interested in the time before higher sample = dt-x = dt*(cfd high - cfd level) / (cfd high - cfd low)
// result is in units of 1/256th ns
// compute 1-x
cfdlev = (double)psa_ampl/2.0 + (double)psa_base; // compute 50% level
cfdlow = (cfd0 & 0x00000FFF);
cfdhigh = (cfd0 & 0x00FFF000) >> 12; // limited to 12 bits currently!
if((cfdhigh-cfdlow)>0) {
tmpD = ((cfdhigh-cfdlev)/(cfdhigh-cfdlow)); // in units of clock cycles
} else {
tmpD = 0;
}
cfdfrac = (int)floor(tmpD*4.0*256.0) & 0x3FF; //fraction 0..1 mapped to 0..1023, i.e. in units of 1/256ns
// add offset within 2-sample group and offset to trigger
cfdticks = (cfd0 & 0x0F000000) >> 24; // cfd ticks has the # of 4ns ticks from cfd level to the block of 2 samples that includes the maximum
ts_max = (cfd0 & 0xF0000000) >> 28; // ft ticks has the 4 relevant bits of timestamp at maximum
tmpI = (timeL & 0x7F) >> 3; // 4 relevant bits of trigger time stamp, in 8ns steps
tmpS = ts_max - tmpI;
if(tmpS<0) tmpS = tmpS + 16; // build difference, tmps = time from trigger to max in 8ns steps
tmpS = 2*tmpS - cfdticks; // build difference, tmps = time from trigger to CFD high in 4ns steps
cfdout = (CW - tmpS)*4*256 + cfdfrac; // time from CW end to CFD point in units of 1/256 ns
// cfdout = cfdfrac + (cfdticks<<10);
// printf("ts_max %d, cfdticks %d, trig_to_max %d, trig_to_cfd %d \n",ts_max, cfdticks, tmpS2, tmpS);
// now store list mode data
if(RunType==0x502) {
// 2D spectrum R vs E
binx = (int)floor(ph/fippiconfig.MCA2D_SCALEX[ch]);
biny = (int)floor((double)psa_R/fippiconfig.MCA2D_SCALEY[ch]);
if( (binx<MCA2D_BINS) && (biny<MCA2D_BINS) && (binx>0) && (biny>0) )
{
mca2D[ch][binx+MCA2D_BINS*biny] = mca2D[ch][binx+MCA2D_BINS*biny] +1; // increment 2D MCA
}
// not saving waveforms, events in a table
fprintf(fil,"%u,%d,0x%X,%u,%u,%u,%u,%u,%u,%u,%u,%u\n",eventcount,ch,hit,timeH,timeL,energy,psa_ampl,cfdout,psa_base,psa_Q0,psa_Q1,psa_R );
} // 0x502
if(RunType==0x501) {
// not saving waveforms, events in a table
fprintf(fil,"%u,%d,0x%X,%u,%u,%u\n",eventcount,ch,hit,timeH,timeL,energy);
} // 0x501
if(RunType==0x500) {
// saving 8 headers + waveforms, one entry per line
fprintf(fil,"%u\n%d\n0x%X\n%u\n%u\n%u\n%u\n%u\n",eventcount,ch,hit,timeH,timeL,energy,psa_R,cfdout);
mapped[AOUTBLOCK] = OB_WFREG;
wf[0] = mapped[AWF0+ch]; // dummy read?
for( k=0; k < (TL[ch]/4); k++)
//for( k=0; k < 10; k++)
{
wf[2*k+0] = mapped[AWF0+ch];
wf[2*k+1] = mapped[AWF0+ch];
// re-order 2 sample words from 32bit FIFO
fprintf(fil,"%u\n",(wf[2*k+0] >> 16) );
fprintf(fil,"%u\n",(wf[2*k+0] & 0xFFFF) );
fprintf(fil,"%u\n",(wf[2*k+1] >> 16) );
fprintf(fil,"%u\n",(wf[2*k+1] & 0xFFFF) );
}
mapped[AOUTBLOCK] = OB_EVREG;
} // 0x500
if(RunType==0x400) {
TraceBlks = (int)floor(TL[ch]/BLOCKSIZE_400);
memcpy( buffer2 + 0, &(hit), 4 );
memcpy( buffer2 + 4, &(TraceBlks), 2 );
memcpy( buffer2 + 6, &(NumPrevTraceBlks), 2 );
memcpy( buffer2 + 8, &(timeL), 4 );
memcpy( buffer2 + 12, &(timeH), 4 );
memcpy( buffer2 + 16, &(energy), 2 );
memcpy( buffer2 + 18, &(ch), 2 );
memcpy( buffer2 + 20, &(psa_ampl), 2 );
memcpy( buffer2 + 22, &(cfdout), 2 ); // actually cfd time
memcpy( buffer2 + 24, &(psa_base), 2 );
memcpy( buffer2 + 26, &(psa_Q0), 2 );
memcpy( buffer2 + 28, &(psa_Q1), 2 );
memcpy( buffer2 + 30, &(psa_R), 2 );
memcpy( buffer2 + 32, &(cfdticks), 2 ); // debug
memcpy( buffer2 + 34, &(ts_max), 2 );
memcpy( buffer2 + 36, &(psa0), 4 ); // debug
memcpy( buffer2 + 40, &(psa1), 4 );
memcpy( buffer2 + 44, &(cfdlow), 2 ); // debug
memcpy( buffer2 + 46, &(cfdhigh), 2 );
// no checksum for now
memcpy( buffer2 + 60, &(wm), 4 );
fwrite( buffer2, 1, CHAN_HEAD_LENGTH_400*2, fil );
NumPrevTraceBlks = TraceBlks;
mapped[AOUTBLOCK] = OB_WFREG;
// w0 = mapped[AWF0+ch]; // dummy read?
for( k=0; k < (TL[ch]/4); k++)
{
w0 = mapped[AWF0+ch];
w1 = mapped[AWF0+ch];
// re-order 2 sample words from 32bit FIFO
wf[2*k+1] = (w1 >> 16) + ((w1 & 0xFFFF) << 16);
wf[2*k+0] = (w0 >> 16) + ((w0 & 0xFFFF) << 16);
}
mapped[AOUTBLOCK] = OB_EVREG;
fwrite( wf, TL[ch]/2, 4, fil );
} // 0x400
eventcount++;
}
else { // event not acceptable (piled up )
R1 = mapped[chaddr+CA_REJECT]; // read this register to advance event FIFOs without incrementing Nout etc
}
} // end event in this channel
} //end for ch
} // end event in any channel
// ----------- Periodically save MCA, PSA, and Run Statistics -----------
if(loopcount % PollTime == 0)
{
// 1) Run Statistics
mapped[AOUTBLOCK] = OB_RSREG;
/*
// for debug purposes, print to std out so we see what's going on
k = 3; // no loop for now
{
m = (mapped[ARS0_MOD+k] + (mapped[ARS0_MOD+k+1]*TWOTO32))/1E9;
c0 = (mapped[ARS0_CH0+k] + (mapped[ARS0_CH0+k+1]*TWOTO32))/1E9;
c1 = (mapped[ARS0_CH1+k] + (mapped[ARS0_CH1+k+1]*TWOTO32))/1E9;
c2 = (mapped[ARS0_CH2+k] + (mapped[ARS0_CH2+k+1]*TWOTO32))/1E9;
c3 = (mapped[ARS0_CH3+k] + (mapped[ARS0_CH3+k+1]*TWOTO32))/1E9;
printf(" %s %9.3f, %s %9.3f,%9.3f,%9.3f,%9.3f\n","RUN_TIME",m,"COUNT_TIME",c0,c1,c2,c3);
}
*/
printf(" Time %u s, Events total %u\n",(unsigned int)(currenttime - starttime),eventcount);
// print (small) set of RS to file, visible to web
read_print_runstats(1, 0, mapped);
mapped[AOUTBLOCK] = OB_EVREG; // read from event registers
// 2) MCA
filmca = fopen("MCA.csv","w");
fprintf(filmca,"bin,MCAch0,MCAch1,MCAch2,MCAch3\n");
for( k=0; k <WEB_MCA_BINS; k++) // report the 4K spectra during the run (faster web update)
{
fprintf(filmca,"%d,%u,%u,%u,%u\n ", k*onlinebin,wmca[0][k],wmca[1][k],wmca[2][k],wmca[3][k]);
}
fclose(filmca);
// 3) 2D MCA or PSA
if(RunType==0x502) {
filmca = fopen("psa2D.csv","w");
// title row (x index)
for( ch=0; ch <NCHANNELS; ch++)
{
for( binx=0;binx<MCA2D_BINS;binx++)
{
fprintf(filmca,",%d",binx+MCA2D_BINS*ch);
}
} // channel loop
fprintf(filmca,"\n");
for( biny=0;biny<MCA2D_BINS;biny++)
{
fprintf(filmca, "%d",biny); // beginning of line
for( ch=0; ch <NCHANNELS; ch++)
{
for( binx=0;binx<MCA2D_BINS;binx++)
{
fprintf(filmca,",%d",mca2D[ch][biny+MCA2D_BINS*binx]);
} // binx loop
} // channel loop
fprintf(filmca,"\n"); // end of line
} // biny loop
fclose(filmca);
} // runtype 0x502
}
// ----------- loop housekeeping -----------
loopcount ++;
currenttime = time(NULL);
} while (currenttime <= starttime+ReqRunTime); // run for a fixed time
// } while (eventcount <= 20); // run for a fixed number of events
// ********************** Run Stop **********************
// clear RunEnable bit to stop run
mapped[ACSRIN] = 0;
// todo: there may be events left in the buffers. need to stop, then keep reading until nothing left
printf(" Run ended, events total %u. Finishing up ...\n",eventcount);
// final save MCA and RS
filmca = fopen("MCA.csv","w");
fprintf(filmca,"bin,MCAch0,MCAch1,MCAch2,MCAch3\n");
for( k=0; k <MAX_MCA_BINS; k++)
{
fprintf(filmca,"%d,%u,%u,%u,%u\n ", k,mca[0][k],mca[1][k],mca[2][k],mca[3][k] );
}
fclose(filmca);
mapped[AOUTBLOCK] = OB_RSREG;
read_print_runstats(0, 0, mapped);
mapped[AOUTBLOCK] = OB_IOREG;
// 3) 2D MCA
if(RunType==0x502) {
filmca = fopen("psa2D.csv","w");
// title row (x index)
for( ch=0; ch <NCHANNELS; ch++)
{
for( binx=0;binx<MCA2D_BINS;binx++)
{
fprintf(filmca,",%d",binx+MCA2D_BINS*ch);
}
} // channel loop
fprintf(filmca,"\n");
for( biny=0;biny<MCA2D_BINS;biny++)
{
fprintf(filmca, "%d",biny); // beginning of line
for( ch=0; ch <NCHANNELS; ch++)
{
for( binx=0;binx<MCA2D_BINS;binx++)
{
fprintf(filmca,",%d",mca2D[ch][biny+MCA2D_BINS*binx]);
} // binx loop
} // channel loop
fprintf(filmca,"\n"); // end of line
} // biny loop
fclose(filmca);
} // runtype 0x502
// clean up
if(RunType==0x400) { // write EOR: special hit pattern, all zero except WM
TraceBlks = 0;
hit = EORMARK;
memcpy( buffer2 + 0, &(hit), 4 );
memcpy( buffer2 + 4, &(TraceBlks), 2 );
memcpy( buffer2 + 6, &(NumPrevTraceBlks), 2 );
memcpy( buffer2 + 8, &(TraceBlks), 4 );
memcpy( buffer2 + 12, &(TraceBlks), 4 );
memcpy( buffer2 + 16, &(TraceBlks), 2 );
memcpy( buffer2 + 18, &(TraceBlks), 2 );
memcpy( buffer2 + 20, &(TraceBlks), 4 );
memcpy( buffer2 + 24, &(TraceBlks), 4 );
memcpy( buffer2 + 28, &(TraceBlks), 4 );
memcpy( buffer2 + 32, &(TraceBlks), 4 );
// no checksum for now
memcpy( buffer2 + 60, &(wm), 4 );
fwrite( buffer2, 1, CHAN_HEAD_LENGTH_400*2, fil );
}
if( (RunType==0x500) || (RunType==0x501) || (RunType==0x502) || (RunType==0x400) ) {
if(eventcount>100000) printf(" Closing files, this may take a long time for high rates\n");
fclose(fil);
}
flock( fd, LOCK_UN );
munmap(map_addr, size);
close(fd);
return 0;
}