-
Notifications
You must be signed in to change notification settings - Fork 1
/
myReadOutDesy.cpp
344 lines (294 loc) · 10.7 KB
/
myReadOutDesy.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
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdint>
#include <csignal>
#include <unistd.h>
#include <sys/time.h>
#include "myModules.h" // VME Module Id.s
#include "myV2718.h" // CAEN V2718 VME Bridge class
#include "myV513.h" // CAEN V513 I/O REG class
#include "myV792AC.h" // CAEN V792AC QDC class
using namespace std;
#define TRIGGER_IN_CHAN 7 // channel 7
#define TRIGGER_MASK 7 // channels 0, 1, 2
#define TRIGGER_DISA_ALL 15 // channel 15
#define TRIGGER_UNLK_CHAN 14 // channel 14
#define SCALER_RESET 13 // channel 13
#define PEDESTAL_VETO 12 // channel 12
#define NSLEEPT 1000 // for sleeping 1000 ns
void dlwait( char* msg )
{
cout << "Press any key to continue ... " << msg;
getchar();
cout << endl;
}
void nsleep ( uint32_t ns )
{
struct timespec ndelay = { 0, ns };
nanosleep ( &ndelay, NULL );
}
inline void enableTriggers( v513& ioreg ) { ioreg.clearOutputBit( TRIGGER_DISA_ALL ); }
inline void disableTriggers( v513& ioreg ) { ioreg.setOutputBit( TRIGGER_DISA_ALL ); }
inline void enablePedestal( v513& ioreg ) { ioreg.clearOutputBit( PEDESTAL_VETO ); }
inline void disablePedestal( v513& ioreg ) { ioreg.setOutputBit( PEDESTAL_VETO ); }
inline uint32_t ioregValue( v513& ioreg ) { return ioreg.readInputRegister(); }
inline bool isTrig( v513& ioreg ) { return (ioreg.readInputRegister() & 1<<TRIGGER_IN_CHAN); }
inline uint32_t getTrigMask( v513& ioreg ) { return (ioreg.readInputRegister() & TRIGGER_MASK); }
inline void resetScaler( v513& ioreg )
{
nsleep(NSLEEPT);
ioreg.setOutputBit( SCALER_RESET );
nsleep(NSLEEPT);
ioreg.clearOutputBit( SCALER_RESET );
}
inline void unlockTrigger( v513& ioreg )
{
nsleep(NSLEEPT);
ioreg.setOutputBit( TRIGGER_UNLK_CHAN );
nsleep(NSLEEPT);
ioreg.clearOutputBit( TRIGGER_UNLK_CHAN );
}
int initV513( v513& ioreg )
{
uint16_t w;
ioreg.read16phys(0xFE, &w);
cout << hex << " V513 FE " << w << endl;
nsleep(NSLEEPT);
ioreg.read16phys(0xFC, &w);
cout << hex << " V513 FC " << w << endl;
nsleep(NSLEEPT);
ioreg.read16phys(0xFA, &w);
cout << hex << " V513 FA " << w << endl;
nsleep(NSLEEPT);
w=0;
ioreg.write16phys(0x48, w);
nsleep(NSLEEPT);
ioreg.write16phys(0x46, w);
nsleep(NSLEEPT);
ioreg.write16phys(0x42, w);
nsleep(NSLEEPT);
ioreg.reset();
nsleep(NSLEEPT);
ioreg.read16phys(0x04, &w);
cout << hex << " V513 0x4 " << w << endl;
nsleep(NSLEEPT);
for (int i=0; i<8; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelInput(i);
nsleep(NSLEEPT);
ioreg.read16phys(reg, &w);
cout << hex << " V513 input ch " << i << " reg " << reg << " " << w << endl;
nsleep(NSLEEPT);
}
for (int i=8; i<16; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelOutput(i);
nsleep(NSLEEPT);
ioreg.read16phys(reg, &w);
cout << hex << " V513 output ch " << i << " reg " << reg << " " << w << endl;
nsleep(NSLEEPT);
}
disableTriggers( ioreg );
return 0;
}
void initV792( v792ac& v792adc )
{
v792adc.print();
v792adc.reset();
v792adc.disableSlide();
v792adc.disableOverflowSupp();
v792adc.disableZeroSupp();
v792adc.clearEventCounter();
v792adc.clearData();
uint16_t ped;
ped = v792adc.getI1();
uint32_t bid = v792adc.id();
cout << "v792ac addr 0x" << hex << bid << dec
<< ": default ped I1 value is " << ped << endl;
ped = 255; // >~ minimum possible Iped (see manual par. 2.1.2.1)
v792adc.setI1(ped); // set I1 current to define pedestal position
ped =v792adc.getI1(); // in the range [0x00-0xFF] (see manual par. 4.34.2)
uint16_t thr = 0x00;
v792adc.setChannelThreshold(thr);
cout << "v792ac addr 0x" << hex << bid << dec << ": now ped I1 value is " << ped << " thr " << thr << endl;
v792adc.enableChannels();
uint16_t reg1 = v792adc.getStatus1();
uint16_t reg2 = v792adc.getControl1();
uint16_t reg3 = v792adc.getStatus2();
uint16_t reg4 = v792adc.getBit1();
uint16_t reg5 = v792adc.getBit2();
uint32_t evc = v792adc.eventCounter();
cout << "v792ac addr 0x" << hex << bid << " status 1 0x" << reg1
<< " control 1 0x" << reg2 << " status 2 0x" << reg3 << " bit 1 0x" << reg4
<< " bit 2 0x" << reg5 << dec << endl;
cout << "v792ac event counter " << evc << endl;
}
volatile bool abort_run(false);
volatile bool pause_run(false);
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;
}
void sigusr1_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"%s sigusr1_handler: sig%d pause_run is %d\n\n", stime, sig, pause_run);
pause_run = not pause_run;
pause_run ? fprintf(stderr,"pausing run\n") : fprintf(stderr,"resuming run\n");
}
uint32_t readEvent ( v792ac& v792adc, uint32_t phEvents, uint32_t* buffer )
{
nsleep(NSLEEPT);
// v792adc.setEvents( phEvents, 0 ); // old version < 15.08.2021
v792adc.setEvents( phEvents );
uint32_t size = v792adc.readEvent( buffer );
return size;
}
int evt_realign ( v513& ion, v792ac& qdc, uint32_t* Tcts, ofstream& ofs, uint32_t& phytr )
{
uint32_t myBuffer[256];
uint32_t tmask = getTrigMask( ion );
cout << " RRR " << dec << phytr << " " << true << " tmask " << hex << tmask << " ioreg " << ioregValue( ion ) << endl;
if (tmask & 1) Tcts[0] ++; // physics triggers
if (tmask & 2) Tcts[1] ++; // pedestal triggers
if (tmask & 4) Tcts[2] ++;
(Tcts[1]*10 < Tcts[0]) ? enablePedestal( ion ) : disablePedestal( ion );
uint32_t size = readEvent ( qdc, phytr, myBuffer );
uint32_t head = myBuffer[0];
uint32_t trail = myBuffer[size-1] & 0xff000000;
uint32_t evnum = myBuffer[size-1] & 0xffffff;
if (head != 0xfa002000) cout << dec << phytr << " Error in header " << hex << head << endl;
if (trail != 0xfc000000) cout << dec << phytr << " Error in trailer " << hex << trail << endl;
if (size != 34) cout << dec << phytr << " Size error " << size << endl;
if (evnum != phytr)
{
cout << dec << phytr << " Error in event number " << evnum << endl;
tmask |= 0xffff0000;
}
ofs << dec << " ev # " << phytr << " tow cts " << Tcts[0] << " " << Tcts[1] << " " << Tcts[2] << " trigger mask " << tmask << " values:";
for (uint32_t j=1; j<size-1; j++)
{
uint32_t chan = (myBuffer[j] >> 16) & 0x1f;
ofs << " " << dec << chan << " " << hex << (myBuffer[j] & 0x3fff);
}
ofs << endl;
phytr ++;
return 0;
}
int main( int argc, char** argv )
{
ofstream ofs;
ifstream ifs;
uint32_t phytr(0);
uint32_t Tcts[3] = { 0, 0, 0 };
uint32_t myBuffer[256];
signal(SIGINT, cntrl_c_handler); // Control-C handler
signal(SIGUSR1, sigusr1_handler); // Control-C handler
v2718 v2718_1("/V2718/cvA24_U_DATA/0"); // VME interface
v513 ion(0xa00000,"/V2718/cvA24_U_DATA/0"); // I/O register
v792ac qdc(0x05000000,"/V2718/cvA32_U_DATA/0"); // V792 QDC
// Init I/O Register and QDC
initV513 ( ion );
initV792 ( qdc );
sched_yield();
disableTriggers( ion );
resetScaler( ion );
unlockTrigger( ion );
// dlwait(" ");
enableTriggers( ion );
time_t stime = time(NULL);
cout << " time is " << stime << endl;
struct tm mytime;
localtime_r( &stime, &mytime );
cout << mytime.tm_year+1900 << "." << mytime.tm_mon+1 << "." << mytime.tm_mday << "." << mytime.tm_hour << ":" << mytime.tm_min << ":" << mytime.tm_sec << endl;
cout << asctime(&mytime) << endl;
uint32_t runnbr;
ifs.open ("/home/dreamtest/working/run.number",std::ofstream::in);
ifs >> runnbr;
ifs.close();
ofs.open ("/home/dreamtest/working/run.number",std::ofstream::out);
ofs << ++runnbr;
ofs.close();
stime -= 1600000000;
string fname = "desydata.run" + to_string(runnbr) + ".txt";
if (argc == 2) fname = argv[1];
cout << " filename is " << fname << endl;
ofs.open (fname.c_str(), std::ofstream::out);
time_t tr0 = time(NULL);
bool running(true);
while (1)
{
if (running and pause_run)
{
disableTriggers(ion);
running = false;
time_t tr = time(NULL) - tr0;
cout << dec << tr << "(sec) " << phytr << "(evt) ioreg " << ioregValue( ion ) << " running was true (now false) and pause_run true" << endl;
}
else if (not running and not pause_run)
{
enableTriggers(ion);
running = true;
time_t tr = time(NULL) - tr0;
cout << dec << tr << "(sec) " << phytr << "(evt) ioreg " << ioregValue( ion ) << " running was false (now true) and pause_run false" << endl;
}
bool isPh = isTrig( ion );
if (isPh)
{
uint32_t tmask = getTrigMask( ion );
if ((phytr % 10000) == 0)
{
time_t tr = time(NULL) - tr0;
double rate = phytr/double(tr);
cout << dec << tr << "(sec) " << phytr << "(evt) " << " rate " << rate << "(evt/sec) isPh " << isPh << " tmask " << hex << tmask << " ioreg " << ioregValue( ion ) << endl;
}
if (tmask & 1) Tcts[0] ++; // physics triggers
if (tmask & 2) Tcts[1] ++; // pedestal triggers
if (tmask & 4) Tcts[2] ++;
(Tcts[1]*10 < Tcts[0]) ? enablePedestal( ion ) : disablePedestal( ion );
uint32_t size = readEvent ( qdc, phytr, myBuffer );
uint32_t head = myBuffer[0];
uint32_t trail = myBuffer[size-1] & 0xff000000;
uint32_t evnum = myBuffer[size-1] & 0xffffff;
if (head != 0xfa002000) cout << dec << phytr << " Error in header " << hex << head << endl;
if (trail != 0xfc000000) cout << dec << phytr << " Error in trailer " << hex << trail << endl;
if (size != 34) cout << dec << phytr << " Size error " << size << endl;
if (evnum != phytr)
{
cout << dec << phytr << " Error in event number " << evnum << endl;
tmask |= 0xffff0000;
}
ofs << dec << " ev # " << phytr << " tow cts " << Tcts[0] << " " << Tcts[1] << " " << Tcts[2] << " trigger mask " << hex << tmask << " values:";
for (uint32_t j=1; j<size-1; j++)
{
uint32_t chan = (myBuffer[j] >> 16) & 0x1f;
ofs << " " << dec << chan << " " << hex << (myBuffer[j] & 0x3fff);
}
ofs << endl;
phytr ++;
// while (phytr <= evnum) phytr ++; // evt_realign ( ion, qdc, Tcts, ofs, phytr );
if (abort_run) disableTriggers( ion );
unlockTrigger( ion );
}
if (abort_run) break;
nsleep( NSLEEPT );
}
ofs.close();
time_t etime = time(NULL);
etime -= 1600000000;
uint32_t tdiff = etime-stime;
double rate = phytr/double(tdiff);
cout << dec << " time is " << etime << " diff(sec) " << (etime-stime) << " events " << phytr
<< " rate(Hz) " << rate << endl;
return 0;
}