MIDAS
Loading...
Searching...
No Matches
fesimdaq.cxx
Go to the documentation of this file.
1/********************************************************************\
2Example period frontend. Exercises certain MIDAS functions, including
3i) db_check_record to ensure that ODB matchs expectation
4ii) deferred transition; frontend waits 5 extra seconds at end of run with deferred transition.
5 Thomas Lindner (TRIUMF)
6\********************************************************************/
7
8
9#include <vector>
10#include <stdio.h>
11#include <algorithm>
12#include <stdlib.h>
13#include "midas.h"
14#include <stdint.h>
15#include <iostream>
16#include <sstream>
17#include <unistd.h>
18
19/* make frontend functions callable from the C framework */
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/*-- Globals -------------------------------------------------------*/
25
26/* The frontend name (client name) as seen by other MIDAS clients */
27char *frontend_name = "fesimdaq";
28/* The frontend file name, don't change it */
30
31/* frontend_loop is called periodically if this variable is TRUE */
33
34/* a frontend status page is displayed with this frequency in ms */
36
37/* maximum event size produced by this frontend */
38INT max_event_size = 3 * 1024 * 1024;
39
40/* maximum event size for fragmented events (EQ_FRAGMENTED) --- not really used here */
41INT max_event_size_frag = 2 * 1024 * 1024;
42
43/* buffer size to hold events */
44INT event_buffer_size = 20 * 1000000;
45 void **info;
46char strin[256];
48
49
50
51
52
53
54/*-- Function declarations -----------------------------------------*/
55
58INT begin_of_run(INT run_number, char *error);
59INT end_of_run(INT run_number, char *error);
60INT pause_run(INT run_number, char *error);
61INT resume_run(INT run_number, char *error);
63
64INT read_trigger_event(char *pevent, INT off);
65INT read_scaler_event(char *pevent, INT off);
66
67
68/*-- Equipment list ------------------------------------------------*/
69
70#undef USE_INT
71
73
74 {"SIMDAQ", /* equipment name */
75 {1, 0, /* event ID, trigger mask */
76 "SYSTEM", /* event buffer */
77#ifdef USE_INT
78 EQ_INTERRUPT, /* equipment type */
79#else
80 EQ_PERIODIC, /* equipment type */
81#endif
82 LAM_SOURCE(0, 0xFFFFFF), /* event source crate 0, all stations */
83 "MIDAS", /* format */
84 TRUE, /* enabled */
85 RO_RUNNING, /* read only when running */
86 1000, /* poll for 1000ms */
87 0, /* stop run after this event limit */
88 0, /* number of sub events */
89 0, /* don't log history */
90 "", "", "",},
91 read_trigger_event, /* readout routine */
92 },
93
94 {""}
95};
96
97#ifdef __cplusplus
98}
99#endif
100
101/********************************************************************\
102 Callback routines for system transitions
103
104 These routines are called whenever a system transition like start/
105 stop of a run occurs. The routines are called on the following
106 occations:
107
108 frontend_init: When the frontend program is started. This routine
109 should initialize the hardware.
110
111 frontend_exit: When the frontend program is shut down. Can be used
112 to releas any locked resources like memory, commu-
113 nications ports etc.
114
115 begin_of_run: When a new run is started. Clear scalers, open
116 rungates, etc.
117
118 end_of_run: Called on a request to stop a run. Can send
119 end-of-run event and close run gates.
120
121 pause_run: When a run is paused. Should disable trigger events.
122
123 resume_run: When a run is resumed. Should enable trigger events.
124\********************************************************************/
125
127
128int nremaining = -1;
129// Deferred transition where we take 5 extra events...
131 {
132 if(first){
133 std::cout << "Starting deferred transition" << std::endl;
134 nremaining = 5;
135 }
136
137 if (finished_readout) {
138 return TRUE;
139 }
140
141 return FALSE;
142 }
143
144
145#define SIMDAQSETTINGS_STR(_name) const char *_name[] = {\
146"[.]",\
147"Descrip = STRING : [256] ",\
148"Parameter AAA = INT : 0",\
149"Parameter BB2 = INT : 0", \
150"Parameter CC22 = INT : 0",\
151"",\
152NULL }
153
155
156/*-- Frontend Init -------------------------------------------------*/
158{
159
160
161 int status = db_check_record(hDB, 0, "/Equipment/SIMDAQ/Settings", strcomb1(simdaqsettings_str).c_str(), TRUE);
162 printf("Status %i\n",status);
163 if (status == DB_STRUCT_MISMATCH) {
164 cm_msg(MERROR, "init_simdaqsettings", "Aborting on mismatching /Equipment/SIMDAQ/Settings");
166 abort();
167 }
168
170 return SUCCESS;
171}
172
173
174
175
176/*-- Frontend Exit -------------------------------------------------*/
177
179{
180 printf("Exiting fedcrc!\n");
181 return SUCCESS;
182}
183
184/*-- Begin of Run --------------------------------------------------*/
185// Upon run stasrt, read ODB settings and write them to DCRC
187{
188
189 nremaining = -1;
191 return SUCCESS;
192}
193
194/*-- End of Run ----------------------------------------------------*/
195
197{
198 return SUCCESS;
199}
200
201/*-- Pause Run -----------------------------------------------------*/
202
204{
205 return SUCCESS;
206}
207
208/*-- Resuem Run ----------------------------------------------------*/
209
211{
212 return SUCCESS;
213}
214
215/*-- Frontend Loop -------------------------------------------------*/
216
218{
219 /* if frontend_call_loop is true, this routine gets called when
220 the frontend is idle or once between every event */
221 usleep(10000);
222 return SUCCESS;
223}
224
225/*------------------------------------------------------------------*/
226
227/********************************************************************\
228
229 Readout routines for different events
230
231\********************************************************************/
232
233/*-- Trigger event routines ----------------------------------------*/
234// Not currently used for DCRC readout
235extern "C" { INT poll_event(INT source, INT count, BOOL test)
236/* Polling routine for events. Returns TRUE if event
237 is available. If test equals TRUE, don't return. The test
238 flag is used to time the polling */
239{
240 int i;
241
242 for (i = 0; i < count; i++) {
243// cam_lam_read(LAM_SOURCE_CRATE(source), &lam);
244
245// if (lam & LAM_SOURCE_STATION(source))
246 if (!test)
247 return 1;
248 }
249
250 usleep(1000);
251 return 0;
252}
253}
254
255/*-- Interrupt configuration ---------------------------------------*/
256// This is not currently used by the DCRC readout
257extern "C" { INT interrupt_configure(INT cmd, INT source, POINTER_T adr)
258{
259 switch (cmd) {
261 break;
263 break;
265 break;
267 break;
268 }
269 return SUCCESS;
270}
271}
272
273#include "math.h" // for RAND, and rand
274double sampleNormal() {
275 double u = ((double) rand() / (RAND_MAX)) * 2 - 1;
276 double v = ((double) rand() / (RAND_MAX)) * 2 - 1;
277 double r = u * u + v * v;
278 if (r == 0 || r > 1) return sampleNormal();
279 double c = sqrt(-2 * log(r) / r);
280 return u * c;
281}
282#include <sys/time.h>
283/*-- Event readout -------------------------------------------------*/
285{
286
287 /* init bank structure */
288 bk_init32(pevent);
289
291 /* create structured ADC0 bank of double words (i.e. 4-byte words) */
292 bk_create(pevent, "ADC0", TID_DWORD, (void **)&pdata32);
293
294 // Add a header, with number of words in event.
295 // Use the top two bits to indicate different control words.
296 // 11 -> 0xcXXXXXXX : overall header
297 // 01 -> 0x4XXXXXXX : trigger header
298 // 00 -> 0x0XXXXXXX : channel header
299 // 10 -> 0x8XXXXXXX : trailer
300
301 // Write the bank header to the bank, containing the number of triggers
302 *pdata32++ = 0xfa000200;
303
304 int sample = floor(sampleNormal()*80)+1200;
305 if((rand() % 100) > 80) sample += 200;
306 *pdata32++ = 0xf800406b + sample;
307
308 int sample2 = floor(sampleNormal()*120)+1000;
309 *pdata32++ = 0xf801405e + sample2;
310
311 int sample3 = floor(sampleNormal()*100)+1000;
312 *pdata32++ = 0xf802405e + sample3;
313
314 int sample4 = floor(sampleNormal()*20)+950;
315 *pdata32++ = 0xf803405e + sample4;
316
317 int sample5 = floor(sampleNormal()*50)+1300;
318 *pdata32++ = 0xf804405e + sample5;
319
320 int sample6 = floor(sampleNormal()*200)+2000;
321 *pdata32++ = 0xf805405e + sample6;
322
323
324
325 *pdata32++ = 0xfcfd57e8;
326
327
328 int size2 = bk_close(pevent, pdata32);
329
330
332 bk_create(pevent, "ADC1", TID_DWORD, (void **)&pdata322);
333
334
335 //for(int i = 0; i < 100000; i++)
336 //*pdata322++ = i;
337
338 size2 = bk_close(pevent, pdata322);
339
341 bk_create(pevent, "W200", TID_DWORD, (void **)&pdata720);
342
343
344 *pdata720++ = 0xa00001f8;
345 *pdata720++ = 0xfff703;
346 *pdata720++ = 0x0;
347 *pdata720++ = 0x13132f;
348
349 int pulse_position = floor(sampleNormal()*80) + 200;
350 for(int j = 0; j < 2; j++){
351
352 if (j==1) pulse_position+= 5;
353 int pulse_height = floor(sampleNormal()*30) +200;
354 pulse_height = pulse_height * (1.0 - ((double)pulse_position)/600.0);
355
356 for(int i = 0; i < 504; i++){
357 uint32_t word = 0;
358 uint32_t sample = 400 + floor(sampleNormal()*2);
359 if(i == pulse_position) sample += 0.2 * pulse_height;
360 if(i == pulse_position+1) sample += 0.8 * pulse_height;
361 if(i == pulse_position+2) sample += 1.0 * pulse_height;
362 if(i == pulse_position+3) sample += 0.9 * pulse_height;
363 if(i == pulse_position+4) sample += 0.5 * pulse_height;
364 if(i == pulse_position+5) sample += 0.3 * pulse_height;
365 if(i == pulse_position+6) sample += 0.1 * pulse_height;
366 //std::cout << i << " " << pulse_position << " " << pulse_height << " " << sample << std::endl;
367 word = sample;
368 i++;
369 sample = 400 + floor(sampleNormal()*2);
370 if(i == pulse_position) sample += 0.2 * pulse_height;
371 if(i == pulse_position+1) sample += 0.8 * pulse_height;
372 if(i == pulse_position+2) sample += 1.0 * pulse_height;
373 if(i == pulse_position+3) sample += 0.9 * pulse_height;
374 if(i == pulse_position+4) sample += 0.5 * pulse_height;
375 if(i == pulse_position+5) sample += 0.3 * pulse_height;
376 if(i == pulse_position+6) sample += 0.1 * pulse_height;
377 //std::cout << j << " " << i << " " << pulse_position << " " << pulse_height << " " << sample << std::endl;
378 word += (sample << 16);
379 *pdata720++ = word;
380 }
381 }
382
383
384
385 size2 = bk_close(pevent, pdata720);
386
387
388
389 struct timeval start,end;
390 gettimeofday(&start,NULL);
391 if(0)printf ("simdaq request: %f\n",start.tv_sec
392 + 0.000001*start.tv_usec);
393 // close the bank
394
395 // handle the deferred transition
396 if(nremaining > 0){ std::cout << "deferring " << nremaining << std::endl; nremaining--;
397 };
398
399 if(nremaining == 0){
401 std::cout << "Finished readout... " << std::endl;
402 }
403
404 /*
405 printf("Start sleep\n");
406 bool done = false;
407 double time;
408 while(!done){
409 sleep(5);
410 gettimeofday(&end,NULL);
411 time = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)*0.000001;
412 if(time > 10) done = true;
413 }
414
415 //}
416 printf("End sleep. time %f\n", time);
417 */
418
419 usleep(10000);
420 return bk_size(pevent);
421}
422
423
424
#define FALSE
Definition cfortran.h:309
INT transition(INT run_number, char *error)
Definition consume.cxx:35
INT bk_close(void *event, void *pdata)
Definition midas.cxx:16780
void bk_init32(void *event)
Definition midas.cxx:16469
void bk_create(void *event, const char *name, WORD type, void **pdata)
Definition midas.cxx:16561
INT bk_size(const void *event)
Definition midas.cxx:16495
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
INT cm_register_deferred_transition(INT transition, BOOL(*func)(INT, BOOL))
Definition midas.cxx:3837
#define DB_STRUCT_MISMATCH
Definition midas.h:654
#define CMD_INTERRUPT_ATTACH
Definition midas.h:822
#define CMD_INTERRUPT_DISABLE
Definition midas.h:821
#define CMD_INTERRUPT_ENABLE
Definition midas.h:820
#define CMD_INTERRUPT_DETACH
Definition midas.h:823
#define SUCCESS
Definition mcstd.h:54
#define EQ_PERIODIC
Definition midas.h:414
#define MERROR
Definition midas.h:559
#define EQ_INTERRUPT
Definition midas.h:416
#define TR_STOP
Definition midas.h:406
#define RO_RUNNING
Definition midas.h:426
#define TID_DWORD
Definition midas.h:336
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
std::string strcomb1(const char **list)
Definition odb.cxx:598
INT db_check_record(HNDLE hDB, HNDLE hKey, const char *keyname, const char *rec_str, BOOL correct)
Definition odb.cxx:12972
BOOL frontend_call_loop
Definition fesimdaq.cxx:28
const char * frontend_file_name
Definition fesimdaq.cxx:25
INT max_event_size
Definition fesimdaq.cxx:34
INT frontend_exit()
Frontend exit.
Definition fesimdaq.cxx:173
HNDLE hSet
Definition fesimdaq.cxx:44
int nremaining
Definition fesimdaq.cxx:121
INT frontend_init()
Frontend initialization.
Definition fesimdaq.cxx:152
INT event_buffer_size
Definition fesimdaq.cxx:40
INT max_event_size_frag
Definition fesimdaq.cxx:37
BOOL wait_end_cycle(int transition, BOOL first)
Definition fesimdaq.cxx:123
INT interrupt_configure(INT cmd, INT source, POINTER_T adr)
Definition fesimdaq.cxx:251
void ** info
Definition fesimdaq.cxx:41
char strin[256]
Definition fesimdaq.cxx:42
INT poll_event(INT source, INT count, BOOL test)
Definition fesimdaq.cxx:230
double sampleNormal()
Definition fesimdaq.cxx:267
INT read_trigger_event(char *pevent, INT off)
Definition fesimdaq.cxx:277
BOOL finished_readout
Definition fesimdaq.cxx:119
EQUIPMENT equipment[]
Definition fesimdaq.cxx:69
#define SIMDAQSETTINGS_STR(_name)
Definition fesimdaq.cxx:140
const char * frontend_name
Definition fesimdaq.cxx:23
INT display_period
Definition fesimdaq.cxx:31
INT begin_of_run(INT run_number, char *error)
Begin of Run.
Definition fesimdaq.cxx:181
INT frontend_loop()
Frontend loop.
Definition fesimdaq.cxx:212
INT end_of_run(INT run_number, char *error)
End of Run.
Definition fesimdaq.cxx:191
INT read_scaler_event(char *pevent, INT off)
Scaler event readout.
Definition ebfe.cxx:295
INT run_number[2]
Definition mana.cxx:246
HNDLE hDB
main ODB handle
Definition mana.cxx:207
double count
Definition mdump.cxx:33
INT i
Definition mdump.cxx:32
INT HNDLE
Definition midas.h:132
DWORD BOOL
Definition midas.h:105
int INT
Definition midas.h:129
#define LAM_SOURCE(c, s)
Definition midas.h:469
#define TRUE
Definition midas.h:182
#define POINTER_T
Definition midas.h:166
#define end
#define resume_run
#define pause_run
program test
Definition miniana.f:6
int gettimeofday(struct timeval *tp, void *tzp)
INT j
Definition odbhist.cxx:40
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
char c
Definition system.cxx:1310