-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyTestReadout.cpp
415 lines (337 loc) · 9.85 KB
/
myTestReadout.cpp
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
/*****************************************
myReadOut.cpp
-------------
Dream DAQ program.
Version 2014: 25/11/2014
*****************************************/
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <stdint.h>
#include <cmath>
#include <ctime>
#include <climits>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#define DRS4_FULL_EV_SIZE 13836
#define DRS4_HALF_EV_SIZE 6920
using namespace std;
bool run_closing(false);
uint32_t maxevts(0), maxpedevts(0), trignr(0), events(0), pedevents(0), doubletrig(0), totevents(0), spills(0), totprevious(0);
uint32_t bsum(0);
bool ped_enabled(true);
double phypedratio(0);
string tprint()
{
time_t tt(time(NULL));
char tbuf[26];
ctime_r( &tt, tbuf);
tbuf[24] = '\0';
return string(tbuf);
}
time_t t0(0);
void set_t0()
{
struct timeval t1;
gettimeofday(&t1, NULL);
t0 = t1.tv_sec;
return;
}
double get_time()
{
double time_us;
struct timeval t1;
gettimeofday(&t1, NULL);
t1.tv_sec -= t0;
time_us = (t1.tv_sec) * 1000000.0f + t1.tv_usec;
return time_us;
}
time_t tlast(0);
time_t tdiff(0);
time_t period1(0), period2(0);
time_t spillStart(0), spillEnd(0), totDuration(0);
void loginfo(double occ=0, double pedocc=0)
{
uint32_t neededsp(0);
if ((spills != 0) && (!run_closing))
{
time_t tnow(time(NULL));
if (tlast != 0)
{
time_t p = tnow-tlast;
if ((period1 != 0) && (period2 != 0) && (abs(period1-p)>1) && (abs(period2-p)>1))
{
cout << spills << " *** spill period was " << period1 << " " << period2 << " now is " << p << endl;
period2 = period1;
period1 = p;
}
else if (period1 == 0)
{
period1 = p;
}
else if (period2 == 0)
{
period2 = period1;
period1 = p;
}
}
tlast = tnow;
if ((events) && (maxevts > events))
{
double ratio = maxevts/double(events)-1.;
neededsp=uint32_t(ratio*spills+1);
}
}
// if (abort_run) break;
time_t spillDuration = spillEnd-spillStart;
totDuration += spillDuration;
uint32_t takenNow = totevents-totprevious;
cout << spills << " : " << takenNow
<< " Events taken -- Total " << totevents
<< " -- Triggers " << trignr
<< " -- Beam " << events
<< " ("<< occ*100 << "% occ.)"
<< " -- Pedestals " << pedevents
<< " ("<< pedocc*100 << "% occ.)"
<< " -- Both " << doubletrig
<< " -- End in ~ " << neededsp
<< " spills -- Spill length " << spillDuration << " sec -- Rate "
<< ((spillDuration > 0) ? takenNow/spillDuration : 0)
<< " Hz -- " + tprint() + '\n' << endl;
totprevious = totevents;
sync();
}
/****************************************/
#include "sigHandler.h"
#include "myModules.h"
#include "myVme.h" // VME Base class
#include "myV262.h" // CAEN V262 I/O REG
#include "myV513.h" // CAEN V513 I/O REG
//#define USE_DRS_V1742
/****************************************/
// Declare all the hardware
/****************************************/
v262 io(0x400000,"/V2718/cvA24_U_DATA/1");
v513 ion(0xa00000,"/V2718/cvA24_U_DATA/1");
/****************************************/
volatile bool abort_run=false;
uint32_t rdtim;
uint32_t evinfo[2]={0,0};
void cntrl_c_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"%s cntrl_c_handler: sig%d\n\n", stime, sig);
fprintf(stderr,"aborting run\n");
abort_run = true;
}
double t_istrigger(1), t_rdoarm(0), t_readev(0), t_mtrigger(0), t_trghandler(0);
uint64_t n_loop(0);
int initV513( v513& ioreg )
{
uint16_t w;
ioreg.read16phys(0xFE, &w);
cout << hex << " V513 FE " << w << endl;
usleep(1);
ioreg.read16phys(0xFC, &w);
cout << hex << " V513 FC " << w << endl;
usleep(1);
ioreg.read16phys(0xFA, &w);
cout << hex << " V513 FA " << w << endl;
usleep(1);
w=0;
ioreg.write16phys(0x48, w);
usleep(1);
ioreg.write16phys(0x46, w);
usleep(1);
ioreg.write16phys(0x42, w);
usleep(1);
ioreg.reset();
usleep(1);
ioreg.read16phys(0x04, &w);
cout << hex << " V513 0x4 " << w << endl;
usleep(1);
for (int i=0; i<8; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelInput(i);
usleep(1);
ioreg.read16phys(reg, &w);
cout << hex << " V513 " << reg << " " << w << endl;
usleep(1);
}
for (int i=8; i<16; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelOutput(i);
usleep(1);
ioreg.read16phys(reg, &w);
cout << hex << " V513 " << reg << " " << w << endl;
usleep(1);
}
cout << dec;
return 0;
}
#define SET_IO_PORT(_port_nr_) io.enableNIMOut(_port_nr_)
#define CLEAR_IO_PORT(_port_nr_) io.disableNIMOut(_port_nr_)
inline void enableTriggers() { ion.clearOutputBit(15); } // { CLEAR_IO_PORT(0); } // to enable triggers clear (=0) out 0
inline void disableTriggers() { ion.setOutputBit(15); } // { SET_IO_PORT(0); } // to disable triggers set (=1) out 0
inline void enablePedTriggers() { ion.clearOutputBit(14); } // { CLEAR_IO_PORT(3); } // to enable pedestal triggers clear (=0) out 3
inline void disablePedTriggers() { ion.setOutputBit(14); } // { SET_IO_PORT(3); } // to disable pedestal triggers set (=1) out 3
inline void send_signal() { io.NIMPulse(2); };
inline void unlockTrigger() { io.NIMPulse(0); io.NIMPulse(3); } // to re-enable the trigger logic that goes auto-vetoed after any trigger
inline void resetNIMScaler() { io.NIMPulse(1); } // to reset the NIM scaler
inline bool isT1T2Trig() { return (ion.readInputRegister() & 1<<7); /* io.getNIMIn(0); */ }
inline bool isHWBusy() { return (ion.readInputRegister() & 1<<4); /* io.getNIMIn(1); */ } // return (v2718m.getInputRegister() & 1);
inline bool isInSpill() { return (ion.readInputRegister() & 1<<5); /* io.getNIMIn(2); */ }
inline bool isPedTrig() { return (ion.readInputRegister() & 1<<6); /* io.getNIMIn(3); */ }
uint32_t nSpill(0);
uint32_t nBeam(0);
uint32_t nPed(0);
uint32_t nTrig(0);
uint32_t nBoth(0);
int32_t getEvent (uint32_t isPhysEvent){
double tref = get_time();
while (isHWBusy() && !abort_run) {}
if(abort_run) return 1;
t_mtrigger += get_time()-tref;
return 0;
}
bool isTriggerPresent (uint32_t * trigmask)
{
double tref = get_time();
bool physT1T2 = isT1T2Trig();
bool ped = isPedTrig();
if (physT1T2 || ped) while (isHWBusy()) {}
if (ped) physT1T2 = isT1T2Trig();
if (ped || physT1T2) t_istrigger += get_time()-tref;
*trigmask = (ped<<1) | physT1T2;
return physT1T2 || ped;
}
void handle_trigger (uint32_t trigmask,
uint32_t maxpedevts,
uint32_t * bsum,
bool * ped_enabled)
{
double tref = get_time();
do
{
if (!getEvent(trigmask & 0x1)) {
if(trigmask & 1){
events++;
if (trigmask & 2) doubletrig++;
}else{
pedevents++;
if(pedevents>=maxpedevts){
disablePedTriggers();
*ped_enabled=false;
}
}
totevents ++;
}
}
while (0);
t_trghandler += get_time()-tref;
}
void check_and_handle_event()
{
uint32_t trigmask;
double tref = get_time();
if (isTriggerPresent(&trigmask)) {
trignr ++;
// send_signal();
handle_trigger(trigmask, maxpedevts, &bsum, &ped_enabled);
if (trigmask & 1) {
if (ped_enabled && (events>=phypedratio*pedevents)) enablePedTriggers();
} else {
if (events<phypedratio*pedevents) disablePedTriggers();
}
unlockTrigger();
rdtim += get_time()-tref;
} // if (isTriggerPresent(&trigmask))
}
/****************************************/
int32_t main(int32_t argc, char** argv)
/****************************************/
{
int32_t runnr;
struct tms tmsbuf;
uint32_t ticks_per_sec;
double time0, cputime, tottime, percent;
time0 = times(&tmsbuf);
ticks_per_sec = sysconf(_SC_CLK_TCK);
signal(SIGINT, cntrl_c_handler); // Control-C handler
cout.precision(4);
//Maximum number of events
maxevts=10000000;
phypedratio=10;
double dmaxp = double(maxevts)/phypedratio;
maxpedevts = lrint(dmaxp);
cout << " maxevts " << maxevts << " maxpedevts " << maxpedevts << " phypedratio " << phypedratio << endl;
int32_t xrunnr(0);
while (--argc > 0)
{
++argv;
if ((strcmp(*argv, "-runnr") == 0) && (argc > 1))
{
--argc; ++argv;
xrunnr = strtol(*argv, NULL, 10);
}
}
runnr = xrunnr;
if (runnr <= 0)
{
cerr << "****************** ERROR ****************** run number not found "
<< runnr << " exiting now\n" << endl;
exit(1);
}
cout << tprint() << " Run " << runnr << " starting -> going to acquire " << maxevts << " events with trigger T1*T2*Vbar" << endl;
//Init I/O Registers
io.reset(); initV513( ion );
//Set vetoes
disableTriggers();
disablePedTriggers();
//Start of run setting up
events=0;
spills=0;
unlockTrigger();
resetNIMScaler();
rdtim=0;
cout << tprint() << " Waiting for a spill ... " << endl;
//Wait for a spill before to start
while(!isInSpill() && !abort_run) {}
cout << "\n Got ... start data taking at " << tprint() << endl;
tlast = time(NULL);
//Remove the veto and start
enableTriggers();
spillStart = time(NULL);
do { // daq loop ... exit on ctrl-c or when (events>=maxevts)
check_and_handle_event();
if ((events>=maxevts) || abort_run) break;
if(!isInSpill()){
disableTriggers();
spillEnd = time(NULL);
check_and_handle_event();
spills++;
loginfo();
while(!isInSpill() && !abort_run) { m_usleep(10); }
if (abort_run) break;
enableTriggers();
spillStart = time(NULL);
} // if(!isInSpill())
} while(1); // daq loop ... exit on ctrl-c or when (events>=maxevts)
run_closing = true;
cout << tprint() << " Ending run ... setting Trigger veto" << endl;
disableTriggers();
check_and_handle_event();
spillStart = 0; spillEnd = totDuration; totprevious = 0;
return 0;
}