1 | /********************************************************************\
|
---|
2 |
|
---|
3 | Main class of FADCtrl
|
---|
4 |
|
---|
5 | If outputting text with PrintMessage(), a '\r' is used on the console automatically
|
---|
6 | if the given text ends with '\n'.
|
---|
7 |
|
---|
8 | Comment 19/10/2010: It is assumed that boolean access is an atomic operation.
|
---|
9 |
|
---|
10 | \********************************************************************/
|
---|
11 |
|
---|
12 | #include "FAD.h"
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | static const struct CL_Struct { const char *Name;
|
---|
16 | void (FAD::*CommandPointer)();
|
---|
17 | bool NeedIdle;
|
---|
18 | unsigned int MinNumParameter;
|
---|
19 | const char *Parameters;
|
---|
20 | const char *Help;
|
---|
21 | } CommandList[] =
|
---|
22 | {{"board", &FAD::cmd_board, true, 1, "[+|-]<range>" ,"Activate or deactivate board(s)"},
|
---|
23 | {"status", &FAD::cmd_status, false, 0, "[range]", "Show board status information"},
|
---|
24 | {"domino", &FAD::cmd_domino, true, 1, "<on|off>", "Switch Domino wave"},
|
---|
25 | {"dwrite", &FAD::cmd_dwrite, true, 1, "<on|off>", "Set DWRITE"},
|
---|
26 | {"phase", &FAD::cmd_phase, true, 1, "<phase>", "Adjust ADC phase (in 'steps')"},
|
---|
27 | {"srclk", &FAD::cmd_srclk, true, 1, "<on|off>", "Set SRCLK"},
|
---|
28 | {"sclk", &FAD::cmd_sclk, true, 1, "<on|off>", "Set SCLK"},
|
---|
29 | {"trigger", &FAD::cmd_trigger, false, 0, "[n|cont [rate]|stop|enable|disable]", "Issue software triggers"},
|
---|
30 | {"reset_trigger", &FAD::cmd_reset_trigger, true, 0, "", "Reset internal trigger counter"},
|
---|
31 | {"runnumber", &FAD::cmd_runnumber, true, 1, "<n>", "Set runnumber"},
|
---|
32 | {"roi", &FAD::cmd_roi, true, 2, "<channel range> <value>", "Set region-of-interest to value"},
|
---|
33 | {"dac", &FAD::cmd_dac, true, 2, "<range> <value>", "Set DAC numbers in range to value"},
|
---|
34 | {"address", &FAD::cmd_address, true, 2, "<range> <value>", "Set addresses in range to value"},
|
---|
35 | {"send", &FAD::cmd_send, true, 1, "<value>", "Send arbitrary data to board"},
|
---|
36 | {"take", &FAD::cmd_take, true, 1, "<n> <dir>", "Start run with n events, write to directory"},
|
---|
37 | {"acalib", &FAD::cmd_acalib, true, 0, "[n|invalidate|file]", "Perform or read amplitude calibration (n events)"},
|
---|
38 | //{"wmode", &FAD::cmd_wmode, 0, "<run|stop>", "Domino wave running or stopped during read out"},
|
---|
39 | //{"rmode", &FAD::cmd_rmode, 0, "<first|stop>", "Readout start at first bin or stop position (DRS4)"},
|
---|
40 | //{"dmode", &FAD::cmd_dmode, 0, "<single|continuous>", "Domino wave single shot or continuous"},
|
---|
41 | {"stop", &FAD::cmd_stop, false, 0, "", "Stop current operation/run"},
|
---|
42 | {"update", &FAD::cmd_update, false, 1, "<sec>", "Minimum delay between updates to DIM event service"},
|
---|
43 | {"socketmode", &FAD::cmd_socketmode, true, 1, "<com|daq>", "Choose which Sockets are used for data transmission"},
|
---|
44 | {"exit", &FAD::cmd_exit, false, 0, "", "Exit program"},
|
---|
45 | {"help", &FAD::cmd_help, false, 0, "", "Print help"}};
|
---|
46 |
|
---|
47 |
|
---|
48 | // ------------------------------------
|
---|
49 | // ***** Constructor/Destructor *****
|
---|
50 | // ------------------------------------
|
---|
51 |
|
---|
52 | //
|
---|
53 | // Constructor
|
---|
54 | //
|
---|
55 | FAD::FAD(std::vector<std::string> List): EvidenceServer(SERVER_NAME) {
|
---|
56 |
|
---|
57 | // Initialization
|
---|
58 | ConsoleText = NULL;
|
---|
59 | MainThread = pthread_self();
|
---|
60 | Mode = idle;
|
---|
61 | Datafile = -1;
|
---|
62 | EventUpdateDelay = atof(GetConfig("EventUpdateDelay", "0.5").c_str());
|
---|
63 |
|
---|
64 | // Create pipe for data exchange
|
---|
65 | if (pipe(Pipe) == -1) Message(FATAL, "pipe() failed in FAD::FAD() (%s)", strerror(errno));
|
---|
66 |
|
---|
67 | // DIM console service used in PrintMessage()
|
---|
68 | ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
|
---|
69 |
|
---|
70 | // Initialise configuration information (later non-blocking access in commandHandler())
|
---|
71 | GetConfig("CalibTempDiffWarn", "0");
|
---|
72 | GetConfig("CalibFreqDiffWarn", "0");
|
---|
73 |
|
---|
74 | // Construct boards
|
---|
75 | if (List.empty()) BoardList = Tokenize(GetConfig("BoardList"));
|
---|
76 | else BoardList = List;
|
---|
77 |
|
---|
78 | for (unsigned int i=0; i<BoardList.size(); i++) {
|
---|
79 | Boards.push_back(new class FADBoard(BoardList[i], PORT, this, i));
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Create DIM event service thread
|
---|
83 | int Ret;
|
---|
84 | if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchEventThread, (void *) this)) != 0) {
|
---|
85 | Message(FATAL, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret));
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Install DIM command (after all initialized)
|
---|
89 | Command = new DimCommand((char *) SERVER_NAME"/Command", (char *) "C", this);
|
---|
90 |
|
---|
91 | // Initialise boards
|
---|
92 | vector<string> Init = Tokenize(GetConfig("InitSequence", ""), ";");
|
---|
93 |
|
---|
94 | for (unsigned int i=0; i<Init.size(); i++) {
|
---|
95 | DimClient::sendCommand(SERVER_NAME"/Command", Init[i].c_str());
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | //
|
---|
100 | // Destructor
|
---|
101 | //
|
---|
102 | FAD::~FAD() {
|
---|
103 |
|
---|
104 | int Ret;
|
---|
105 |
|
---|
106 | // Close pipe (will make read() on pipe in DIM service thread return)
|
---|
107 | if (close(Pipe[0]) == -1) Message(ERROR, "close() on Pipe[0] failed in FAD::~FAD() (%s)", strerror(errno));
|
---|
108 | if (close(Pipe[1]) == -1) Message(ERROR, "close() on Pipe[1] failed in FAD::~FAD() (%s)", strerror(errno));
|
---|
109 |
|
---|
110 | // Wait for DIM service thread to quit
|
---|
111 | if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret));
|
---|
112 |
|
---|
113 | // Delete all boards (cancels threads automatically)
|
---|
114 | for (unsigned int i=0; i<Boards.size(); i++) delete Boards[i];
|
---|
115 |
|
---|
116 | delete Command;
|
---|
117 | delete ConsoleOut;
|
---|
118 | free(ConsoleText);
|
---|
119 | }
|
---|
120 |
|
---|
121 | // ------------------------------
|
---|
122 | // ***** Command handling *****
|
---|
123 | // ------------------------------
|
---|
124 |
|
---|
125 | //
|
---|
126 | // DIM command handler (non-blocking, otherwise a DIM rpc would dead-lock)
|
---|
127 | //
|
---|
128 | void FAD::commandHandler() {
|
---|
129 |
|
---|
130 | char *Command = getCommand()->getString(), *Start;
|
---|
131 |
|
---|
132 | // Ignore empty or illegal strings
|
---|
133 | if (getCommand()->getSize() == 0) return;
|
---|
134 | if( *(Command+getCommand()->getSize()-1) != '\0' || strlen(Command) == 0) return;
|
---|
135 |
|
---|
136 | // Shell command
|
---|
137 | if (*Command == '.') {
|
---|
138 | system(Command+1);
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Parse command into tokens
|
---|
143 | Parameter.clear();
|
---|
144 | while(true) {
|
---|
145 | while (isspace(*Command)) Command++; // Ignore initial white spaces
|
---|
146 | if(*Command=='\0') break;
|
---|
147 | if (*Command == '\"') {
|
---|
148 | Start = ++Command;
|
---|
149 | while(*Command!='\"' && *Command!='\0') Command++;
|
---|
150 | }
|
---|
151 | else {
|
---|
152 | Start = Command;
|
---|
153 | while(!isspace(*Command) && *Command!='\0') Command++;
|
---|
154 | }
|
---|
155 | if(*Command != '\0') *Command++ = '\0';
|
---|
156 | Parameter.push_back(Start);
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Search for command in command list
|
---|
160 | for(unsigned int n=0; n<sizeof(CommandList)/sizeof(CL_Struct); n++) {
|
---|
161 | if (Match(Parameter[0], CommandList[n].Name)) {
|
---|
162 | // Check if number of parameters
|
---|
163 | if(Parameter.size()-1 < CommandList[n].MinNumParameter) {
|
---|
164 | PrintMessage("Usage: %s %s\n", CommandList[n].Name, CommandList[n].Parameters);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | // Check if idle mode required
|
---|
168 | if (CommandList[n].NeedIdle && Mode != idle) {
|
---|
169 | PrintMessage("Current mode is not idle ('stop' will stop current operation)\n");
|
---|
170 | return;
|
---|
171 | }
|
---|
172 | // Jump to command function
|
---|
173 | (this->*CommandList[n].CommandPointer)();
|
---|
174 | return;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Command not found
|
---|
179 | PrintMessage("Unknown command '%s'\n", Parameter[0].c_str());
|
---|
180 | }
|
---|
181 |
|
---|
182 | //
|
---|
183 | // Switch SRCLK
|
---|
184 | //
|
---|
185 | void FAD::cmd_srclk() {
|
---|
186 |
|
---|
187 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
188 | if (Match(Parameter[1],"on")) Boards[i]->Send(CMD_SRCLK_ON);
|
---|
189 | else if (Match(Parameter[1],"off")) Boards[i]->Send(CMD_SRCLK_OFF);
|
---|
190 | else {
|
---|
191 | PrintUsage();
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | //
|
---|
198 | // Reset internal trigger
|
---|
199 | //
|
---|
200 |
|
---|
201 | void FAD::cmd_reset_trigger() {
|
---|
202 |
|
---|
203 | for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->Send(CMD_RESET_TRIGGER_ID);
|
---|
204 | }
|
---|
205 |
|
---|
206 | //
|
---|
207 | // Set run number
|
---|
208 | //
|
---|
209 | void FAD::cmd_runnumber() {
|
---|
210 |
|
---|
211 | int Num;
|
---|
212 |
|
---|
213 | if (!ConvertToInt(Parameter[1], &Num)) {
|
---|
214 | PrintMessage("Error, incorrect parameter for run number\n");
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
219 | Boards[i]->Send(CMD_Write | ADDR_RUNNUMBER );
|
---|
220 | Boards[i]->Send((unsigned short) (Num>>16)); // Write the HIGH-word first
|
---|
221 | Boards[i]->Send(CMD_Write | (ADDR_RUNNUMBER+1) );
|
---|
222 | Boards[i]->Send((unsigned short) Num); // now write the LOW-word
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | //
|
---|
227 | // Switch socket mode
|
---|
228 | // "com" - command mode (only socket 0 is used)
|
---|
229 | // "daq" - daq mode (only sockets 1 - 7 are used)
|
---|
230 | //
|
---|
231 | // Note that socket 0 is always used to issue commands
|
---|
232 | //
|
---|
233 | void FAD::cmd_socketmode() {
|
---|
234 |
|
---|
235 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
236 | if (Match(Parameter[1],"com")) Boards[i]->Send(CMD_mode_command);
|
---|
237 | else if (Match(Parameter[1],"daq")) Boards[i]->Send(CMD_mode_all_sockets);
|
---|
238 | else {
|
---|
239 | PrintUsage();
|
---|
240 | return;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | //
|
---|
246 | // Switch SCLK
|
---|
247 | //
|
---|
248 | void FAD::cmd_sclk() {
|
---|
249 |
|
---|
250 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
251 | if (Match(Parameter[1],"on")) Boards[i]->Send(CMD_SCLK_ON);
|
---|
252 | else if (Match(Parameter[1],"off")) Boards[i]->Send(CMD_SCLK_OFF);
|
---|
253 | else {
|
---|
254 | PrintUsage();
|
---|
255 | return;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | //
|
---|
261 | // Switch Domino wave
|
---|
262 | //
|
---|
263 | void FAD::cmd_domino() {
|
---|
264 |
|
---|
265 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
266 | if (Match(Parameter[1],"on")) Boards[i]->Send(CMD_DENABLE);
|
---|
267 | else if (Match(Parameter[1],"off")) Boards[i]->Send(CMD_DDISABLE);
|
---|
268 | else {
|
---|
269 | PrintUsage();
|
---|
270 | return;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | //
|
---|
276 | // Switch DWRITE
|
---|
277 | //
|
---|
278 | void FAD::cmd_dwrite() {
|
---|
279 |
|
---|
280 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
281 | if (Match(Parameter[1],"on")) Boards[i]->Send(CMD_DWRITE_RUN);
|
---|
282 | else if (Match(Parameter[1],"off")) Boards[i]->Send(CMD_DWRITE_STOP);
|
---|
283 | else {
|
---|
284 | PrintUsage();
|
---|
285 | return;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | //
|
---|
291 | // Issue soft trigger
|
---|
292 | //
|
---|
293 | void FAD::cmd_trigger() {
|
---|
294 |
|
---|
295 | int Num;
|
---|
296 |
|
---|
297 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
298 | if (Parameter.size() == 1) Boards[i]->Send(CMD_Trigger);
|
---|
299 | else if (ConvertToInt(Parameter[1], &Num)) {
|
---|
300 | for (int j=0; j<Num; j++) Boards[i]->Send(CMD_Trigger);
|
---|
301 | }
|
---|
302 | else if (Match(Parameter[1],"continuous")) {
|
---|
303 | Boards[i]->Send(CMD_Trigger_C);
|
---|
304 | if (Parameter.size() == 3 && ConvertToInt(Parameter[2], &Num)) {
|
---|
305 | if (Num == 0)
|
---|
306 | Boards[i]->Send(CMD_Trigger_S);
|
---|
307 | else {
|
---|
308 | //Boards[i]->Send(0x2100 + (unsigned char) (1000.0/Num/12.5));
|
---|
309 | Boards[i]->Send( CMD_Write | BADDR_CONT_TRIGGER_TIME );
|
---|
310 | Boards[i]->Send((unsigned short) (1000.0/Num/12.5));
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | else if (Match(Parameter[1],"stop")) Boards[i]->Send(CMD_Trigger_S);
|
---|
315 | else if (Match(Parameter[1],"enable")) Boards[i]->Send(CMD_TRIGGERS_ON);
|
---|
316 | else if (Match(Parameter[1],"disable")) Boards[i]->Send(CMD_TRIGGERS_OFF);
|
---|
317 | else {
|
---|
318 | PrintUsage();
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | //
|
---|
325 | // Set DAC
|
---|
326 | //
|
---|
327 | void FAD::cmd_dac() {
|
---|
328 |
|
---|
329 | int Value;
|
---|
330 | struct Range R = {0, NDAC-1};
|
---|
331 | unsigned short Buffer[2*NDAC] = {0};
|
---|
332 |
|
---|
333 | // Check ranges
|
---|
334 | if(!ConvertToRange(Parameter[1], R)) {
|
---|
335 | PrintMessage("Error, DAC number out of range.\n");
|
---|
336 | return;
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (!ConvertToInt(Parameter[2], &Value) || Value<0 || Value>MAX_DACVAL) {
|
---|
340 | PrintMessage("Error, DAC value out of range.\n");
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | // Prepare command buffer
|
---|
345 | for (int i=R.Min; i<=R.Max; i++) {
|
---|
346 | Buffer[2*i] = htons(CMD_Write | (BADDR_DAC + i));
|
---|
347 | Buffer[2*i+1] = htons(Value);
|
---|
348 | }
|
---|
349 |
|
---|
350 | // Send command buffer
|
---|
351 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
352 | Boards[i]->Send(Buffer, sizeof(Buffer));
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | //
|
---|
357 | // Set region-of-interest
|
---|
358 | //
|
---|
359 | void FAD::cmd_roi() {
|
---|
360 |
|
---|
361 | int Value;
|
---|
362 | struct Range R = {0, NChips*NChannels-1};
|
---|
363 | unsigned short Buffer[2*NChips*NChannels] = {0};
|
---|
364 |
|
---|
365 | // Check ranges
|
---|
366 | if (!ConvertToRange(Parameter[1], R)) {
|
---|
367 | PrintMessage("Error, ROI number out of range.\n");
|
---|
368 | return;
|
---|
369 | }
|
---|
370 |
|
---|
371 | if (!ConvertToInt(Parameter[2], &Value) || Value<0 || Value>MAX_ROIVAL) {
|
---|
372 | PrintMessage("Error, ROI value out of range.\n");
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | // Prepare command buffer
|
---|
377 | for (int i=R.Min; i<=R.Max; i++) {
|
---|
378 | Buffer[2*i] = htons(CMD_Write | (BADDR_ROI + i));
|
---|
379 | Buffer[2*i+1] = htons(Value);
|
---|
380 | }
|
---|
381 |
|
---|
382 | // Send command buffer
|
---|
383 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
384 | Boards[i]->Send(Buffer, sizeof(Buffer));
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | //
|
---|
389 | // Set addresses to value
|
---|
390 | //
|
---|
391 | void FAD::cmd_address() {
|
---|
392 |
|
---|
393 | int Value;
|
---|
394 | struct Range R = {0, MAX_ADDR};
|
---|
395 | unsigned short Buffer[2*MAX_ADDR] = {0};
|
---|
396 |
|
---|
397 | // Check ranges
|
---|
398 | if (!ConvertToRange(Parameter[1], R)) {
|
---|
399 | PrintMessage("Error, address out of range.\n");
|
---|
400 | return;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (!ConvertToInt(Parameter[2], &Value) || Value<0 || Value>MAX_VAL) {
|
---|
404 | PrintMessage("Error, value out of range.\n");
|
---|
405 | return;
|
---|
406 | }
|
---|
407 |
|
---|
408 | // Prepare command buffer
|
---|
409 | for (int i=R.Min; i<=R.Max; i++) {
|
---|
410 | Buffer[2*i] = htons(CMD_Write | i);
|
---|
411 | Buffer[2*i+1] = htons(Value);
|
---|
412 | }
|
---|
413 |
|
---|
414 | // Send command buffer
|
---|
415 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
416 | Boards[i]->Send(Buffer, 2*(R.Max-R.Min+1)*sizeof(unsigned short));
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | //
|
---|
421 | // Set ADC phase
|
---|
422 | //
|
---|
423 | void FAD::cmd_phase() {
|
---|
424 |
|
---|
425 | int Value;
|
---|
426 |
|
---|
427 | if (!ConvertToInt(Parameter[1], &Value)) {
|
---|
428 | PrintMessage("Error, illegal phase value\n");
|
---|
429 | return;
|
---|
430 | }
|
---|
431 |
|
---|
432 | // Prepare command buffer
|
---|
433 | unsigned short *Buffer = new unsigned short [abs(Value)];
|
---|
434 | for (int i=0; i<abs(Value); i++) Buffer[i] = htons(CMD_PS_DO);
|
---|
435 |
|
---|
436 | // Execute phase setting
|
---|
437 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
438 | Boards[i]->Send(CMD_PS_RESET);
|
---|
439 | if (Value < 0) Boards[i]->Send(CMD_PS_DIRDEC);
|
---|
440 | else Boards[i]->Send(CMD_PS_DIRINC);
|
---|
441 | Boards[i]->Send(Buffer, abs(Value)*sizeof(unsigned short));
|
---|
442 | }
|
---|
443 |
|
---|
444 | delete[] Buffer;
|
---|
445 | }
|
---|
446 |
|
---|
447 | //
|
---|
448 | // Send arbitrary data to board
|
---|
449 | //
|
---|
450 | void FAD::cmd_send() {
|
---|
451 |
|
---|
452 | int Value;
|
---|
453 |
|
---|
454 | if (!ConvertToInt(Parameter[1], &Value) || Value<0 || Value>MAX_VAL) {
|
---|
455 | PrintMessage("Error, illegal value\n");
|
---|
456 | return;
|
---|
457 | }
|
---|
458 |
|
---|
459 | for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->Send(Value);
|
---|
460 | }
|
---|
461 |
|
---|
462 | /*
|
---|
463 | // Set Domino mode
|
---|
464 | void FAD::cmd_dmode() {
|
---|
465 | if (Match(Param[1],"continuous")) SetDOMINOMode(1);
|
---|
466 | else if (Match(Param[1],"single")) SetDOMINOMode(0);
|
---|
467 | else PrintUsage();
|
---|
468 | }
|
---|
469 |
|
---|
470 | // Set Domino readout mode
|
---|
471 | void FAD::cmd_rmode() {
|
---|
472 | if (Match(Param[1],"first")) SetDOMINOReadMode(0);
|
---|
473 | else if (Match(Param[1],"stop")) SetDOMINOReadMode(1);
|
---|
474 | else PrintUsage();
|
---|
475 | }
|
---|
476 |
|
---|
477 | // Set Domino wave mode
|
---|
478 | void FAD::cmd_wmode() {
|
---|
479 | if (Match(Param[1],"run")) SetDOMINOWaveMode(1);
|
---|
480 | else if (Match(Param[1],"stop")) SetDOMINOWaveMode(0);
|
---|
481 | else PrintUsage();
|
---|
482 | }
|
---|
483 | */
|
---|
484 |
|
---|
485 | //
|
---|
486 | // Start data run
|
---|
487 | //
|
---|
488 | void FAD::cmd_take() {
|
---|
489 |
|
---|
490 | time_t Time = time(NULL);
|
---|
491 | struct tm *T = localtime(&Time);
|
---|
492 | char Filename[500];
|
---|
493 | double Temp;
|
---|
494 |
|
---|
495 | // Set number of requested events
|
---|
496 | NumEventsRequested = atoi(Parameter[1].c_str());
|
---|
497 | NumEvents = 0;
|
---|
498 |
|
---|
499 | // Open file with rwx right for owner and group, never overwrite file
|
---|
500 | snprintf(Filename, sizeof(Filename),"%s/%d%02d%02dT%02d%02d%02d.raw", Parameter[2].c_str(), T->tm_year+1900, T->tm_mon+1, T->tm_mday, T->tm_hour, T->tm_min, T->tm_sec);
|
---|
501 |
|
---|
502 | Datafile = open(Filename,O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
|
---|
503 | if(Datafile == -1) {
|
---|
504 | PrintMessage("Error: Could not open file \"%s\" (%s)\n", Filename, strerror(errno));
|
---|
505 | return;
|
---|
506 | }
|
---|
507 |
|
---|
508 | // Check conditions for run of all active boards
|
---|
509 | float MaxTempDiff = atof(GetConfig("CalibTempDiffWarn").c_str());
|
---|
510 | float MaxFreqDiff = atof(GetConfig("CalibFreqDiffWarn").c_str());
|
---|
511 |
|
---|
512 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
513 | if (!Boards[i]->Active) continue;
|
---|
514 |
|
---|
515 | if (Boards[i]->ACalib.Time == -1) PrintMessage("Warning: Amplitude calibration missing for board %d\n", i);
|
---|
516 | else {
|
---|
517 | Temp = 0;
|
---|
518 | for (unsigned int j=0; j<NTemp; j++) Temp += Boards[i]->GetStatus().Temp[j] / NTemp;
|
---|
519 | if (fabs(Boards[i]->ACalib.Temp-Temp) > MaxTempDiff) PrintMessage("Warning: Calibration to current temperature difference larger than %.1f K for board %d\n", MaxTempDiff, i);
|
---|
520 | if (fabs(Boards[i]->ACalib.Frequency-Boards[i]->GetStatus().Frequency) > MaxFreqDiff) PrintMessage("Warning: Calibration to current frequency difference larger than %.1f GHz for board %d\n", MaxFreqDiff, i);
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | // Start run
|
---|
525 | Mode = datarun;
|
---|
526 | Message(INFO, "Starting run with %d events, filename '%s'", NumEventsRequested, Filename);
|
---|
527 | }
|
---|
528 |
|
---|
529 | //
|
---|
530 | // Amplitude calibration
|
---|
531 | //
|
---|
532 | void FAD::cmd_acalib() {
|
---|
533 |
|
---|
534 | // Invalidate calibration?
|
---|
535 | if (Parameter.size() == 2 && Match(Parameter[1], "invalidate")) {
|
---|
536 | for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->ACalib.Time = -1;
|
---|
537 | return;
|
---|
538 | }
|
---|
539 |
|
---|
540 | // Read calibration data from file?
|
---|
541 | if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumEventsRequested)) {
|
---|
542 | FILE *File;
|
---|
543 | struct FADBoard::CalibData Data;
|
---|
544 |
|
---|
545 | // Open file
|
---|
546 | if ((File = fopen(Parameter[1].c_str(), "r")) == NULL) {
|
---|
547 | PrintMessage("Error opening file '%s'\n", Parameter[1].c_str());
|
---|
548 | return;
|
---|
549 | }
|
---|
550 | // Read data and check if it applies to any board
|
---|
551 | while (fread(&Data, sizeof(Data), 1, File) == 1) {
|
---|
552 | for (unsigned int i=0; i<Boards.size(); i++) if (Data.DNA == Boards[i]->GetStatus().DNA) {
|
---|
553 | PrintMessage("Found calibration for board %d - %s", i, ctime(&Data.Time));
|
---|
554 | Boards[i]->ACalib = Data;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | //Close file
|
---|
558 | if (fclose(File) != 0) PrintMessage("Could not close file '%s'\n", Parameter[1].c_str());
|
---|
559 | return;
|
---|
560 | } // Reading calibration from file
|
---|
561 |
|
---|
562 | // Set number of events required for calibration
|
---|
563 | if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumEventsRequested) || NumEventsRequested<=0) {
|
---|
564 | NumEventsRequested = DEFAULT_NUM_CALIB_EVENTS;
|
---|
565 | }
|
---|
566 |
|
---|
567 | // Start calibration by setting mode
|
---|
568 | Mode = acalib;
|
---|
569 | Message(INFO, "Starting amplitude calibration run with 3x%d events", NumEventsRequested);
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | //
|
---|
574 | // Print status
|
---|
575 | //
|
---|
576 | void FAD::cmd_status() {
|
---|
577 |
|
---|
578 | int MinCount = std::numeric_limits<int>::max();
|
---|
579 | unsigned int SlowestBoard = 0;
|
---|
580 |
|
---|
581 | // ==== Print board overview ====
|
---|
582 | if (Parameter.size() == 1) {
|
---|
583 |
|
---|
584 | // Count active board
|
---|
585 | unsigned int Count = 0, Error = 0;
|
---|
586 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
587 | if (Boards[i]->Active) Count++;
|
---|
588 | if (!Boards[i]->CommOK) Error++;
|
---|
589 | if (Boards[i]->Active && Boards[i]->Count < MinCount) {
|
---|
590 | MinCount = Boards[i]->Count;
|
---|
591 | SlowestBoard = i;
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | PrintMessage("\rTotal boards: %d (%d with communication errors)\n", Boards.size(), Error);
|
---|
596 |
|
---|
597 | // Print list of active boards
|
---|
598 | PrintMessage("Active are %d boards(s) ", Count);
|
---|
599 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
600 | if (Boards[i]->Active) PrintMessage(" %d%s", i, Boards[i]->CommOK ? "":"!");
|
---|
601 | }
|
---|
602 | PrintMessage("\n");
|
---|
603 |
|
---|
604 | // Current mode
|
---|
605 | if (Mode == idle) PrintMessage("Current mode is IDLE\n");
|
---|
606 | else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events, slowest board %d has %d events)\n", NumEventsRequested, SlowestBoard, MinCount);
|
---|
607 | else if (Mode == datarun) PrintMessage("Current mode is DATARUN (%d events requested, %d events taken)\n", NumEventsRequested, NumEvents);
|
---|
608 |
|
---|
609 | return;
|
---|
610 | }
|
---|
611 |
|
---|
612 | // ==== Print details for given range ====
|
---|
613 | struct Range R = {0, Boards.size()};
|
---|
614 |
|
---|
615 | if (!ConvertToRange(Parameter[1], R)) {
|
---|
616 | PrintMessage("Error, out of range.\n");
|
---|
617 | return;
|
---|
618 | }
|
---|
619 |
|
---|
620 | for (int i=0; i<(int) Boards.size(); i++) {
|
---|
621 | if (i<R.Min || i > R.Max) continue;
|
---|
622 |
|
---|
623 | PrintMessage("\nBOARD #%d (%sactive) IP %s Communication %s\n", i, Boards[i]->Active ? "":"in", Boards[i]->Name, Boards[i]->CommOK ? "OK":"ERROR");
|
---|
624 |
|
---|
625 | // Calibration information
|
---|
626 | if (Boards[i]->ACalib.Time == -1) PrintMessage("No amplitude calibration available\n");
|
---|
627 | else PrintMessage("Calibration data: Temperature %.1f Frequency %.2f Time %s" , Boards[i]->ACalib.Temp, Boards[i]->ACalib.Frequency, ctime(&Boards[i]->ACalib.Time));
|
---|
628 |
|
---|
629 | // Status information
|
---|
630 | struct FADBoard::BoardStatus S = Boards[i]->GetStatus();
|
---|
631 |
|
---|
632 | PrintMessage("Status: %s\n", S.Message);
|
---|
633 |
|
---|
634 | if (S.Update.tv_sec == -1) {
|
---|
635 | PrintMessage("No event received yet, no further status information available\n");
|
---|
636 | continue;
|
---|
637 | }
|
---|
638 | PrintMessage("Event rate %.1f Hz Last event received %s", S.Rate, ctime(&S.Update.tv_sec));
|
---|
639 |
|
---|
640 | // Board identification
|
---|
641 | PrintMessage("Board ID %.4x Firmware revision %.4x Serial %llx\n", S.BoardID, S.FirmwareRevision, S.DNA);
|
---|
642 | PrintMessage("Board time %g s Event counter %d\n", S.BoardTime/1.0e4 , S.EventCounter);
|
---|
643 |
|
---|
644 | // Other data
|
---|
645 | PrintMessage("Frequency %.2f GHz Phase shift %d PLL lock %d %d %d %d\n", S.Frequency, S.PhaseShift, S.Lock[0], S.Lock[1], S.Lock[2], S.Lock[3]);
|
---|
646 | PrintMessage("DENABLE %d DWRITE %d SPI_clk %d DCM_lock %d DCM_ready %d\n", S.denable, S.dwrite, S.spi_clk, S.DCM_lock, S.DCM_ready);
|
---|
647 | PrintMessage("DAC %d %d %d %d %d %d %d %d\n", S.DAC[0], S.DAC[1], S.DAC[2], S.DAC[3], S.DAC[4], S.DAC[5], S.DAC[6], S.DAC[7] );
|
---|
648 | PrintMessage("Temperature %.2f %.2f %.2f %.2f", S.Temp[0], S.Temp[1], S.Temp[2], S.Temp[3]);
|
---|
649 |
|
---|
650 | for (unsigned int j=0; j<NChips*NChannels; j++) {
|
---|
651 | if (j%NChannels == 0) PrintMessage("\nROI %2d-%2d: ", j, j+NChannels-1);
|
---|
652 | PrintMessage("%4d ", S.ROI[j/NChannels][j%NChannels]);
|
---|
653 | }
|
---|
654 |
|
---|
655 | PrintMessage("\nTrigger ID: Num %d Type %d CRC %d Run number %d\n", S.TriggerNum, S.TriggerType, S.TriggerCRC, S.Runnumber);
|
---|
656 | } // for()
|
---|
657 | }
|
---|
658 |
|
---|
659 | //
|
---|
660 | // Adress FAD boards
|
---|
661 | //
|
---|
662 | void FAD::cmd_board() {
|
---|
663 |
|
---|
664 | struct Range R = {0, Boards.size()};
|
---|
665 | int Mode = 0;
|
---|
666 |
|
---|
667 | // Check if given boards should be enabled or disabled
|
---|
668 | if (Parameter[1].size() >= 1) {
|
---|
669 | if (Parameter[1][0] == '+') Mode = 1;
|
---|
670 | if (Parameter[1][0] == '-') Mode = -1;
|
---|
671 | }
|
---|
672 | if (Mode != 0) Parameter[1][0] = ' ';
|
---|
673 |
|
---|
674 | // Evaluate given range
|
---|
675 | if (!ConvertToRange(Parameter[1], R)) {
|
---|
676 | PrintMessage("Error, out of range.\n");
|
---|
677 | return;
|
---|
678 | }
|
---|
679 |
|
---|
680 | // Enable or disable boards
|
---|
681 | for (int i=0; i<(int) Boards.size(); i++) {
|
---|
682 | if (Mode == 0) Boards[i]->Active = false;
|
---|
683 | if (i >= R.Min && i <= R.Max) {
|
---|
684 | if (Mode != -1) Boards[i]->Active = true;
|
---|
685 | else Boards[i]->Active = false;
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | //
|
---|
691 | // Set DIM event update delay
|
---|
692 | //
|
---|
693 | void FAD::cmd_update() {
|
---|
694 |
|
---|
695 | double Delay;
|
---|
696 |
|
---|
697 | if (Parameter.size()==2 && ConvertToDouble(Parameter[1], &Delay) && Delay>=0) EventUpdateDelay = Delay;
|
---|
698 | else PrintUsage();
|
---|
699 | }
|
---|
700 |
|
---|
701 | //
|
---|
702 | // Print help
|
---|
703 | //
|
---|
704 | void FAD::cmd_help() {
|
---|
705 |
|
---|
706 | char *Buffer;
|
---|
707 |
|
---|
708 | for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
|
---|
709 | if (asprintf(&Buffer, "%s %s", CommandList[i].Name, CommandList[i].Parameters) == -1) {
|
---|
710 | PrintMessage("Error printing help, asprintf() failed\n");
|
---|
711 | break;
|
---|
712 | }
|
---|
713 | else {
|
---|
714 | if (strlen(Buffer) < 25) PrintMessage("%-25s%s\n", Buffer, CommandList[i].Help);
|
---|
715 | else PrintMessage("%s\n%-25s%s\n", Buffer, "", CommandList[i].Help);
|
---|
716 | }
|
---|
717 | free(Buffer);
|
---|
718 | }
|
---|
719 | PrintMessage(".<command> Execute shell command\n\n"
|
---|
720 | "Items in <> are mandatory, in [] optional, | indicates mutual exclusive.\n"
|
---|
721 | "Strings containing spaces have to be enclosed in \"double quotes\".\n"
|
---|
722 | "Ranges can be given as 'all', a single number or in the form 'a-b'.\n");
|
---|
723 | }
|
---|
724 |
|
---|
725 | //
|
---|
726 | // Stop current operation
|
---|
727 | //
|
---|
728 | void FAD::cmd_stop() {
|
---|
729 |
|
---|
730 | static char Stop[] = "stop\n";
|
---|
731 |
|
---|
732 | if (Mode == idle) {
|
---|
733 | PrintMessage("Nothing to stop\n");
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (Mode == acalib) {
|
---|
738 | Mode = idle;
|
---|
739 | Message(INFO, "Mode set to IDLE");
|
---|
740 | }
|
---|
741 |
|
---|
742 | if (Mode == datarun) {
|
---|
743 | // Inform event thread to stop run in case datarun active
|
---|
744 | if (write(Pipe[1], Stop, strlen(Stop)) == -1) {
|
---|
745 | Message(ERROR, "write() to Pipe[1] failed in FAD::cmd_cancel() (%s)", strerror(errno));
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | PrintMessage("Requested stopping of current operation\n");
|
---|
750 | }
|
---|
751 |
|
---|
752 | //
|
---|
753 | // Exit programm
|
---|
754 | // SIGTERM makes readline() return (in case command came over network)
|
---|
755 | //
|
---|
756 | void FAD::cmd_exit() {
|
---|
757 |
|
---|
758 | if (Mode != idle) cmd_stop();
|
---|
759 |
|
---|
760 | ExitRequest = true;
|
---|
761 |
|
---|
762 | // Wait to allow console input to arrive at readline()
|
---|
763 | usleep(10000);
|
---|
764 | pthread_kill(MainThread, SIGTERM);
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | // -----------------------------
|
---|
769 | // ***** Other functions *****
|
---|
770 | // -----------------------------
|
---|
771 |
|
---|
772 | //
|
---|
773 | // DIM exit handler (overwriting handler in Evidence class)
|
---|
774 | //
|
---|
775 | void FAD::exitHandler(int Code) {
|
---|
776 |
|
---|
777 | Message(INFO, "Exit handler called (DIM exit code %d)", Code);
|
---|
778 | cmd_exit();
|
---|
779 | }
|
---|
780 |
|
---|
781 | //
|
---|
782 | // Save amplitude calibration data to file
|
---|
783 | //
|
---|
784 | void FAD::SaveAmplitudeCalibration() {
|
---|
785 |
|
---|
786 | // Open calibration data file
|
---|
787 | string Filename = string(getenv("HOME"))+"/FAD_ACal";
|
---|
788 | FILE *File = fopen(Filename.c_str(), "wb");
|
---|
789 |
|
---|
790 | if (File == NULL) {
|
---|
791 | PrintMessage("Could not open calibration data file '%s'\n", Filename.c_str());
|
---|
792 | return;
|
---|
793 | }
|
---|
794 |
|
---|
795 | // Write valid calibration information for active boards
|
---|
796 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
797 | if (!Boards[i]->Active || Boards[i]->ACalib.Time == -1) continue;
|
---|
798 |
|
---|
799 | if (fwrite(&Boards[i]->ACalib, sizeof(Boards[i]->ACalib), 1, File) != 1) {
|
---|
800 | PrintMessage("Could not write to calibration file '%s'\n", Filename.c_str());
|
---|
801 | break;
|
---|
802 | }
|
---|
803 | }
|
---|
804 |
|
---|
805 | // Close file
|
---|
806 | if (fclose(File) != 0) PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
|
---|
807 |
|
---|
808 | PrintMessage("Wrote amplitude calibration to file '%s'\n", Filename.c_str());
|
---|
809 | }
|
---|
810 |
|
---|
811 | //
|
---|
812 | // Event thread (publishes/writes M0 format)
|
---|
813 | //
|
---|
814 | void FAD::EventThread() {
|
---|
815 |
|
---|
816 | struct timeval Time, RunStart;
|
---|
817 | struct timeval LastUpdate;
|
---|
818 | struct FADBoard::BoardStatus S;
|
---|
819 | vector<unsigned long> EventNumbers(Boards.size());
|
---|
820 | vector<bool> AcalibDone(Boards.size());
|
---|
821 | double Temp;
|
---|
822 | string IDString;
|
---|
823 | char Buffer[100];
|
---|
824 | int Ret;
|
---|
825 | unsigned long long FileSize = 0;
|
---|
826 |
|
---|
827 | gettimeofday(&LastUpdate, NULL);
|
---|
828 | RunStart = LastUpdate; // only to avoid 'uninitialized' warning from compiler
|
---|
829 |
|
---|
830 | // Create DIM event data and number services
|
---|
831 | int EventSize = sizeof(RunHeader)+ Boards.size()*sizeof(BoardStructure)+sizeof(EventHeader) + Boards.size()*(NChips*NChannels*NBins*sizeof(short) + NChips*sizeof(int));
|
---|
832 | char *EventData = new char [EventSize];
|
---|
833 |
|
---|
834 | memset(EventData, 0, EventSize);
|
---|
835 |
|
---|
836 | DimService EventService(SERVER_NAME"/EventData", (char *) "C", NULL, 0);
|
---|
837 | DimService EventNumService(SERVER_NAME"/EventNumber", NumEvents);
|
---|
838 |
|
---|
839 | // Calculate pointers to EventData array
|
---|
840 | RunHeader *RHeader = (RunHeader *) EventData;
|
---|
841 | BoardStructure **BStruct = new BoardStructure * [Boards.size()];
|
---|
842 | for (unsigned int i=0; i<Boards.size(); i++) BStruct[i] = ((BoardStructure *) (RHeader + 1)) + i;
|
---|
843 | EventHeader *EHeader = (EventHeader *) ((char *) (RHeader + 1) + Boards.size()*sizeof(BoardStructure));
|
---|
844 | int *TriggerCell = (int *) (EHeader + 1);
|
---|
845 | short *Data = (short *) (TriggerCell + NChips*Boards.size());
|
---|
846 |
|
---|
847 | // M0 RunHeader
|
---|
848 | RHeader->DataFormat = DATA_FORMAT;
|
---|
849 | RHeader->RunHeaderSize = sizeof(RunHeader);
|
---|
850 | RHeader->EventHeaderSize = sizeof(EventHeader);
|
---|
851 | RHeader->BoardStructureSize = sizeof(BoardStructure);
|
---|
852 | RHeader->SoftwareRevision = atoi(REVISION) * (strchr(REVISION, 'M')==NULL ? 1:-1);
|
---|
853 | RHeader->Identification = 0;
|
---|
854 |
|
---|
855 | RHeader->Type = 0; // Run type: 0=data, 1=pedestal, 3=test
|
---|
856 |
|
---|
857 | RHeader->RunNumber = -1;
|
---|
858 | RHeader->FileNumber = 0;
|
---|
859 | snprintf(RHeader->Description, sizeof(RHeader->Description), "FADctrl");
|
---|
860 |
|
---|
861 | RHeader->NBoards = Boards.size();
|
---|
862 | RHeader->NChips = NChips;
|
---|
863 | RHeader->NChannels = NChannels;
|
---|
864 | RHeader->Samples = NBins; // Always full pipeline
|
---|
865 | RHeader->Offset = 0;
|
---|
866 | RHeader->NBytes = sizeof(short);
|
---|
867 |
|
---|
868 | // M0 EventHeader
|
---|
869 | EHeader->EventSize = Boards.size()*(NChips*NChannels*NBins*sizeof(short) + NChips*sizeof(int));
|
---|
870 |
|
---|
871 | // Update loop
|
---|
872 | while (!ExitRequest) {
|
---|
873 | // Removed processed data from IDString
|
---|
874 | size_t LastLF = IDString.find_last_of("\n");
|
---|
875 | if (LastLF != string::npos) IDString = IDString.substr(LastLF+1);
|
---|
876 |
|
---|
877 | // Wait for data from TCP/IP reading threads
|
---|
878 | if ((Ret=read(Pipe[0], Buffer, sizeof(Buffer))) == -1) Message(FATAL, "read() from Pipe[0] failed in FAD::EventThread() (%s)", strerror(errno));
|
---|
879 |
|
---|
880 | // Check if pipe closed
|
---|
881 | if (Ret == 0) break;
|
---|
882 |
|
---|
883 | IDString.append(string(Buffer, Ret));
|
---|
884 |
|
---|
885 | // If amplitude calibration mode, check if board finished procedure
|
---|
886 | if (Mode == acalib) {
|
---|
887 | bool Done = true;
|
---|
888 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
889 | if (IDString.find(string("ACALIBDONE")+Boards[i]->Name) != string::npos) AcalibDone[i] = true;
|
---|
890 | if (!AcalibDone[i] && Boards[i]->Active) Done = false;
|
---|
891 | }
|
---|
892 | // Amplitude calibration finished?
|
---|
893 | if (Done) {
|
---|
894 | SaveAmplitudeCalibration();
|
---|
895 | Mode = idle;
|
---|
896 | Message(INFO, "Amplitude calibration done, mode set to IDLE");
|
---|
897 | }
|
---|
898 | }
|
---|
899 | else for (unsigned int i=0; i<Boards.size(); i++) AcalibDone[i] = false;
|
---|
900 |
|
---|
901 | // Update run and event header with current time
|
---|
902 | gettimeofday(&Time, NULL);
|
---|
903 |
|
---|
904 | RHeader->MagicNum = MAGICNUM_CLOSED;
|
---|
905 | RHeader->EndSecond = Time.tv_sec;
|
---|
906 | RHeader->EndMicrosecond = Time.tv_usec;
|
---|
907 |
|
---|
908 | EHeader->Second = Time.tv_sec;
|
---|
909 | EHeader->Microsecond = Time.tv_usec;
|
---|
910 |
|
---|
911 | // Close data file if requested or requested number of events reached
|
---|
912 | if((IDString.find("stop")!=string::npos || NumEvents==NumEventsRequested) && Mode==datarun) {
|
---|
913 |
|
---|
914 | // Update run header
|
---|
915 | RHeader->Events = NumEvents;
|
---|
916 | RHeader->StartSecond = RunStart.tv_sec;
|
---|
917 | RHeader->StartMicrosecond = RunStart.tv_usec;
|
---|
918 |
|
---|
919 | if (lseek(Datafile, 0, SEEK_SET) == -1) {
|
---|
920 | Message(ERROR, "Could not rewind file to write updated run header (%s)", strerror(errno));
|
---|
921 | }
|
---|
922 | else if (write(Datafile, RHeader, sizeof(RunHeader)) != sizeof(RunHeader)) {
|
---|
923 | Message(ERROR, "Could not write updated run header (%s)", strerror(errno));
|
---|
924 | }
|
---|
925 |
|
---|
926 | // Close data file and terminate run
|
---|
927 | if(close(Datafile) == -1) Message(ERROR, "Could not close data file (%s)", strerror(errno));
|
---|
928 | else PrintMessage("Data file closed (size %.1f MByte).\n", FileSize/1024.0/1024);
|
---|
929 |
|
---|
930 | Datafile = -1;
|
---|
931 | Mode = idle;
|
---|
932 | Message(INFO, "Data run ended, mode set to IDLE");
|
---|
933 | }
|
---|
934 |
|
---|
935 | // These values might have changed while close file
|
---|
936 | RHeader->StartSecond = Time.tv_sec;
|
---|
937 | RHeader->StartMicrosecond = Time.tv_usec;
|
---|
938 | RHeader->Events = 1;
|
---|
939 |
|
---|
940 | // Check all boards that have new data
|
---|
941 | for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
|
---|
942 | // Identify board
|
---|
943 | if (IDString.find(string("EVENT")+Boards[Brd]->Name) == string::npos) continue;
|
---|
944 |
|
---|
945 | // Fill M0 BoardStructure
|
---|
946 | S = Boards[Brd]->GetStatus();
|
---|
947 | BStruct[Brd]->SerialNo = S.BoardID;
|
---|
948 | BStruct[Brd]->NomFreq = S.Frequency;
|
---|
949 | BStruct[Brd]->BoardTemp = 0;
|
---|
950 | for (unsigned int i=0; i<NTemp; i++) BStruct[Brd]->BoardTemp += S.Temp[i]/NTemp;
|
---|
951 | BStruct[Brd]->ScaleFactor = 1/2.048;
|
---|
952 |
|
---|
953 | // Update event header with ID and Type of current board
|
---|
954 | EHeader->EventNumber = S.TriggerNum;
|
---|
955 | EHeader->TriggerType = S.TriggerType;
|
---|
956 |
|
---|
957 | // Register event number for data writing below
|
---|
958 | EventNumbers[Brd] = S.TriggerNum;
|
---|
959 |
|
---|
960 | // Write trigger cells
|
---|
961 | for(unsigned int i=0; i<NChips; i++) TriggerCell[Brd*NChips+i] = (int) S.TriggerCell[i];
|
---|
962 |
|
---|
963 | // Write channel data (12 bit signed two's complement with out-of-range-bit and leading zeroes)
|
---|
964 | int Count = Brd*NChips*NChannels*NBins;
|
---|
965 | memset(Data+Count, 0, NChips*NChannels*NBins*sizeof(short));
|
---|
966 |
|
---|
967 | Boards[Brd]->Lock();
|
---|
968 | for (unsigned int Chip=0; Chip<NChips; Chip++) for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
---|
969 | for (int i=0; i<S.ROI[Chip][Chan]; i++) {
|
---|
970 | if (Boards[Brd]->ACalib.Time == -1) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i];
|
---|
971 | else {
|
---|
972 | Temp = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->ACalib.Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]);
|
---|
973 | Temp *= Boards[Brd]->ACalib.Gain[Chip][Chan][0]/Boards[Brd]->ACalib.Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins];
|
---|
974 | //Temp -= Boards[Brd]->ACalib.Secondary[Chip][Chan][i];
|
---|
975 | Data[Count++] = (short) Temp;
|
---|
976 | }
|
---|
977 | }
|
---|
978 | Count += NBins - S.ROI[Chip][Chan];
|
---|
979 | }
|
---|
980 |
|
---|
981 | // Inform TCP/IP thread that data has been processed
|
---|
982 | Boards[Brd]->Continue = true;
|
---|
983 | Boards[Brd]->Unlock();
|
---|
984 |
|
---|
985 | if ((Ret = pthread_cond_signal(&Boards[Brd]->CondVar)) != 0) {
|
---|
986 | Message(FATAL, "pthread_cond_signal() failed (%s)", strerror(Ret));
|
---|
987 | }
|
---|
988 | } // Loop over boards
|
---|
989 |
|
---|
990 | // Check if DIM service should be updated
|
---|
991 | if ((Time.tv_sec-LastUpdate.tv_sec)*1e6 + Time.tv_usec-LastUpdate.tv_usec > EventUpdateDelay*1e6) {
|
---|
992 | gettimeofday(&LastUpdate, NULL);
|
---|
993 | EventService.updateService(EventData, EventSize);
|
---|
994 | EventNumService.updateService();
|
---|
995 | }
|
---|
996 |
|
---|
997 | // ===== Data writing ===
|
---|
998 |
|
---|
999 | if (Mode != datarun) continue;
|
---|
1000 |
|
---|
1001 | // Check if event numbers of all active boards are the same
|
---|
1002 | unsigned long CommonEventNum = numeric_limits<unsigned long>::max();
|
---|
1003 |
|
---|
1004 | for (unsigned int i=0; i<Boards.size(); i++) if (Boards[i]->Active) {
|
---|
1005 | if (CommonEventNum == numeric_limits<unsigned long>::max()) CommonEventNum = EventNumbers[i];
|
---|
1006 | if (CommonEventNum != EventNumbers[i]) {
|
---|
1007 | CommonEventNum = numeric_limits<unsigned long>::max();
|
---|
1008 | break;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 | if (CommonEventNum == numeric_limits<unsigned long>::max()) continue;
|
---|
1012 |
|
---|
1013 | // Write also run header if this is the first event
|
---|
1014 | int Offset;
|
---|
1015 | if (NumEvents == 0) {
|
---|
1016 | RHeader->MagicNum = MAGICNUM_OPEN;
|
---|
1017 | RunStart = Time;
|
---|
1018 | Offset = 0;
|
---|
1019 | FileSize = 0;
|
---|
1020 | }
|
---|
1021 | else Offset = sizeof(RunHeader) + Boards.size()*sizeof(BoardStructure);
|
---|
1022 |
|
---|
1023 | // Write data to file
|
---|
1024 | if(write(Datafile, EventData+Offset, EventSize-Offset) != (ssize_t) EventSize-Offset) {
|
---|
1025 | Message(ERROR, "Could not write all data to file, terminating run, setting mode to IDLE (%s)", strerror(errno));
|
---|
1026 |
|
---|
1027 | // Close file if error
|
---|
1028 | if (close(Datafile) == -1) Message(ERROR, "Could not close data file (%s)", strerror(errno));
|
---|
1029 | Datafile = -1;
|
---|
1030 | Mode = idle;
|
---|
1031 | continue;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | NumEvents++;
|
---|
1035 | FileSize += EventSize-Offset;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | delete[] BStruct;
|
---|
1039 | delete[] EventData;
|
---|
1040 |
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | // Launch event thread inside class
|
---|
1044 | void FAD::LaunchEventThread(class FAD *m) {
|
---|
1045 |
|
---|
1046 | m->EventThread();
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 |
|
---|
1050 | /*
|
---|
1051 | // Set DOMINO mode
|
---|
1052 | void FAD::SetDOMINOMode(int mode) {
|
---|
1053 |
|
---|
1054 | for (int i=FirstBoard; i<=LastBoard; i++) {
|
---|
1055 | GetBoard(i)->SetDominoMode(mode==1 ? 1:0);
|
---|
1056 | PrintMessage("Domino mode of board %d switched to %s.\n",i,mode==1 ? "continuous":"single shot");
|
---|
1057 | }
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | // Set DOMINO readout mode
|
---|
1061 | void FAD::SetDOMINOReadMode(int mode) {
|
---|
1062 |
|
---|
1063 | for (int i=FirstBoard; i<=LastBoard; i++) {
|
---|
1064 | GetBoard(i)->SetReadoutMode(mode);
|
---|
1065 | PrintMessage("Start readout of board %d from %s.\n",i,mode==0 ? "first bin":"stop position");
|
---|
1066 | }
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | // Set DOMINO wave mode
|
---|
1070 | void FAD::SetDOMINOWaveMode(int mode) {
|
---|
1071 |
|
---|
1072 | for (int i=FirstBoard; i<=LastBoard; i++) {
|
---|
1073 | GetBoard(i)->SetDominoActive(mode);
|
---|
1074 | PrintMessage("Domino wave of board %d is %s during readout\n",i,mode==1 ? "running":"stopped");
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | */
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | //
|
---|
1082 | // Print usage text for command
|
---|
1083 | //
|
---|
1084 | void FAD::PrintUsage() {
|
---|
1085 |
|
---|
1086 | for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
|
---|
1087 | if (Match(Parameter[0], CommandList[i].Name)) {
|
---|
1088 | PrintMessage("Usage: %s %s\n", CommandList[i].Name, CommandList[i].Parameters);
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | //
|
---|
1094 | // Print message to console
|
---|
1095 | //
|
---|
1096 | void FAD::PrintMessage(const char *Format, ...) {
|
---|
1097 |
|
---|
1098 | static char Error[] = "vasprintf() failed in PrintMessage()";
|
---|
1099 | char *Text;
|
---|
1100 |
|
---|
1101 | // Evaluate arguments
|
---|
1102 | va_list ArgumentPointer;
|
---|
1103 | va_start(ArgumentPointer, Format);
|
---|
1104 | if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
|
---|
1105 | va_end(ArgumentPointer);
|
---|
1106 |
|
---|
1107 | if (strlen(Text) == 0) return;
|
---|
1108 |
|
---|
1109 | // Print to console
|
---|
1110 | if (Text[strlen(Text)-1] == '\n') printf("\r"); // Overwrite prompt
|
---|
1111 | printf("%s", Text);
|
---|
1112 | fflush(stdout);
|
---|
1113 | if (Text[strlen(Text)-1]=='\n') rl_on_new_line(); // New prompt
|
---|
1114 |
|
---|
1115 | // Send to DIM text service
|
---|
1116 | ConsoleOut->updateService(Text);
|
---|
1117 |
|
---|
1118 | // Free old text
|
---|
1119 | if (ConsoleText != Error) free(ConsoleText);
|
---|
1120 | ConsoleText = Text;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | //
|
---|
1124 | // Check if two strings match (min 1 character must match)
|
---|
1125 | //
|
---|
1126 | bool FAD::Match(string str, const char *cmd) {
|
---|
1127 |
|
---|
1128 | return strncasecmp(str.c_str(),cmd,strlen(str.c_str())==0 ? 1:strlen(str.c_str())) ? false:true;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | //
|
---|
1132 | // Conversion function from string to double, int or range
|
---|
1133 | //
|
---|
1134 | // Return false if conversion did not stop on whitespace or EOL character
|
---|
1135 | bool FAD::ConvertToDouble(string String, double *Result) {
|
---|
1136 |
|
---|
1137 | char *EndPointer;
|
---|
1138 |
|
---|
1139 | *Result = strtod(String.c_str(), &EndPointer);
|
---|
1140 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
---|
1141 | return true;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | bool FAD::ConvertToInt(string String, int *Result) {
|
---|
1145 |
|
---|
1146 | char *EndPointer;
|
---|
1147 |
|
---|
1148 | *Result = (int) strtol(String.c_str(), &EndPointer, 0);
|
---|
1149 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
---|
1150 | return true;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | bool FAD::ConvertToRange(string String, struct FAD::Range &R) {
|
---|
1154 |
|
---|
1155 | int N, M;
|
---|
1156 |
|
---|
1157 | // Full range
|
---|
1158 | if (Match(String, "all")) return true;
|
---|
1159 |
|
---|
1160 | // Single number
|
---|
1161 | if (ConvertToInt(String, &N)) {
|
---|
1162 | if (N>= R.Min && N<=R.Max) {
|
---|
1163 | R.Max = R.Min = N;
|
---|
1164 | return true;
|
---|
1165 | }
|
---|
1166 | return false;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | // Range a-b
|
---|
1170 | vector<string> V = EvidenceServer::Tokenize(String, "-");
|
---|
1171 | if (V.size()==2 && ConvertToInt(V[0], &N) && ConvertToInt(V[1], &M) && N>=R.Min && M<=R.Max) {
|
---|
1172 | R.Min = N;
|
---|
1173 | R.Max = M;
|
---|
1174 | return true;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | return false;
|
---|
1178 | }
|
---|