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