15 #include <rtsSystems.h>
16 #include <RC_Config.h>
19 #include <daqFormats.h>
20 #include <daq100Decision.h>
21 #include <XMLCFG/SimpleXmlDoc.h>
29 UINT64 dets_in_run_mask;
31 UINT64 tokenZeroTriggers;
35 UINT32 groupdef[EVP_GROUP_MAX][2];
36 float rate[EVP_GROUP_MAX];
42 int cnt[EVP_GROUP_MAX];
47 int configEvp(SimpleXmlDoc *xml,
int divisor=1) {
49 evpCtrs.runStartTime = 0;
50 memset(evpCtrs.cnt, 0,
sizeof(evpCtrs.cnt));
53 evpCfg.policy = xml->getParamI(
"trg_setup.extra.evp_policy");
55 for(
int i=0;i<EVP_GROUP_MAX;i++) {
57 evpCfg.groupdef[i][0] = xml->getParamMask(0,
"trg_setup.evpGroup[%d].definition", i);
59 evpCfg.groupdef[i][1] = xml->getParamMask(1,
"trg_setup.evpGroup[%d].definition", i);
61 evpCfg.rate[i] = xml->getParamI(
"trg_setup.evpGroup[%d].rate", i);
63 LOG(NOTE ,
"Groups[%d].rate = %f : def[0] = 0x%x def[1] = 0x%x",i,evpCfg.rate[i],evpCfg.groupdef[i][0],evpCfg.groupdef[i][1],0);
65 if((evpCfg.rate[i] > 0.0) && (divisor > 1)) {
66 evpCfg.rate[i] /= (float)divisor;
69 if((
int)(evpCfg.rate[i]*1000) > 0) {
70 LOG(NOTE,
"configEvp: rate[%d]*1000 = %d (0x%x-0x%x): divisor=%d",i,(
int)(evpCfg.rate[i]*1000),evpCfg.groupdef[i][0],evpCfg.groupdef[i][1],divisor);
109 int config(SimpleXmlDoc *xml) {
112 ret = configEvp(xml);
114 run_type = xml->getParamI(
"daq_setup.run_type");
115 cl_run = xml->getParamI(
"daq_setup.detectors[%d].cl_done",TPX_ID);
116 raw_write = xml->getParamI(
"daq_setup.detectors[%d].raw_write",TPX_ID);
118 dets_in_run_mask = 0;
120 for(
int i=0;i<MAX_NODES;i++) {
121 int inrun = xml->getParamI(
"subsys_tasks.nodes[%d].inrun",i);
125 UINT32 node = xml->getParamI(
"subsys_tasks.nodes[%d].node",i);
127 LOG(DBG,
"node = 0x%x", node,0,0,0,0);
129 int sys = GET_SYSTEM(node);
131 if(sys == DAQ_SYSTEM)
continue;
132 if(sys == TRG_SYSTEM)
continue;
134 dets_in_run_mask |= (1ll<<sys);
137 tokenZeroTriggers = 0;
139 for(
int i=0;i<TRIGGERS_MAX;i++) {
140 int inrun = xml->getParamI(
"trg_setup.triggers[%d].used", i);
143 if(xml->getParamI(
"trg_setup.triggers[%d].userdata.tokenZero", i)) {
144 tokenZeroTriggers |= (1ll<<i);
148 LOG(NOTE,
"dets_in_run_mask = 0x%llx",dets_in_run_mask,0,0,0,0);
193 UINT32 sec = time(NULL);
194 float currtime = (float)sec;
197 gettimeofday(&tm, NULL);
198 double sec = tm.tv_sec;
199 double usec = tm.tv_usec;
201 double currtime = sec + usec / 1000000.0;
203 LOG(DBG,
"currtime = %lf",currtime);
210 UINT32 evpAssign(UINT32 trg_lo, UINT32 trg_hi)
212 static double resettime=0;
213 static long int seed = 0;
215 double currtime = mygettime();
220 seed = (
long int)(currtime * 1e6);
222 LOG(
"JEFF",
"seed48=%llx %d", seed,
sizeof(seed),0,0,0);
226 if(currtime > 15 + resettime) {
228 resettime = currtime;
230 resettime = currtime + drand48();
232 for(
int i=0;i<EVP_GROUP_MAX;i++) {
237 double et = currtime - resettime;
238 if(et < .01) et = .01;
239 LOG(NOTE,
"currtime=%d resettime=%d et*1000=%d",(
int)currtime,(
int)resettime,(
int)(et*1000),0,0);
243 for(
int i=0;i<EVP_GROUP_MAX;i++) {
244 if((trg_lo & evpCfg.groupdef[i][0]) ||
245 (trg_hi & evpCfg.groupdef[i][1])) {
250 LOG(NOTE,
"Event: et*1000=%d trg_lo=0x%x trg_hi=0x%x grpmask=0x%x",(
int)(et*1000),trg_lo,trg_hi,grpmask,0);
254 for(
int i=0;i<EVP_GROUP_MAX;i++) {
255 if(!(grpmask & (1<<i)))
continue;
257 double r = ((double)evpCtrs.cnt[i]/et);
258 LOG(NOTE,
"group[%d] rate*1000=%d",i, (
int)(r * 1000),0,0,0);
260 if(r < evpCfg.rate[i]) {
263 LOG(NOTE,
"EVP[%d]: r*1000=%d rate*1000=%d, cnt=%d et=%d",i,(
int)(r*1000),(
int)(evpCfg.rate[i]*1000),evpCtrs.cnt[i],(
int)et);
266 LOG(NOTE,
"NOEVP[%d]: r*1000=%d rate*1000=%d, cnt=%d et=%d",i,(
int)(r*1000),(
int)(evpCfg.rate[i]*1000),evpCtrs.cnt[i],(
int)et);
269 if(evpCfg.rate[i] < 0) {
270 LOG(NOTE,
"Set fire mask because of neg rate[%d]*1000 %d?",i,(
int)(evpCfg.rate[i]*1000),0,0,0);
276 if(evpCfg.policy == 1) {
277 LOG(NOTE,
"Set fire mask because of take all",0,0,0,0,0);
281 if(evpCfg.policy == 2) {
282 double r = ((double)evpCtrs.cnt[0]/et);
284 LOG(NOTE,
"Set fire mask because of 10hz",0,0,0,0,0);
292 for(
int i=0;i<EVP_GROUP_MAX;i++) {
293 if(firemask & (1<<i)) evpCtrs.cnt[i]++;
304 int doEvent(
gbPayload *pay,
EvtDescData *evt, UINT32 l1trg_lo, UINT32 l2trg_lo, UINT32 l25abort, UINT32 token, UINT32 eventNumber, UINT32 l1trg_hi=0, UINT32 l2trg_hi=0)
306 pay->gbPayloadVersion = l2h32(GB_PAYLOAD_VERSION);
307 pay->eventNumber = l2h32(eventNumber);
308 pay->token = l2h32(token);
311 if(&pay->EventDescriptor == evt) {
312 LOG(NOTE,
"Event descriptor is already in payload! Fine.",0,0,0,0,0);
315 LOG(NOTE,
"Event descriptor not in payload, copy it! %p %p", &pay->EventDescriptor, evt,0,0,0);
316 memcpy(&pay->EventDescriptor, evt,
sizeof(
EvtDescData));
320 pay->L1summary[0] = l2h32(l1trg_lo);
321 pay->L1summary[1] = l2h32(l1trg_hi);
322 pay->L2summary[0] = l2h32(l2trg_lo);
323 pay->L3summary[0] = l2h32(l2trg_lo);
324 pay->L2summary[1] = l2h32(l2trg_hi);
325 pay->L3summary[1] = l2h32(l2trg_hi);
327 LOG(NOTE,
"l1: 0x%x/0x%x, l2: 0x%x/0x%x evtdesc->dets: 0x%x",
332 evt->actionWdDetectorBitMask);
335 pay->evp = l2h32(evpAssign(l2trg_lo, l2trg_hi));
336 pay->L3summary[3] = pay->evp;
340 clock_gettime(CLOCK_REALTIME, &tm);
341 pay->sec = l2h32(tm.tv_sec);
342 pay->usec = l2h32(tm.tv_nsec * 1000);
345 gettimeofday(&tm, NULL);
346 pay->sec = tm.tv_sec;
347 pay->usec = tm.tv_usec;
350 LOG(DBG,
"Payload: ver=0x%x token=%d trgcmd=%d daqcmd=0x%x",
351 pay->gbPayloadVersion,
352 pay->EventDescriptor.TrgToken,
353 pay->EventDescriptor.actionWdTrgCommand,
354 pay->EventDescriptor.actionWdDaqCommand,0);
356 pay->flags = daq100Decision(l2h32(pay->token), evt->actionWdDaqCommand, run_type, cl_run, raw_write);
359 pay->flags |= EVBFLAG_L25ABORT;
362 pay->flags |= EVBFLAG_L25TIMEOUT;
365 pay->flags = l2h32(pay->flags);
367 UINT32 awdetmask = b2h16(evt->actionWdDetectorBitMask);
368 LOG(NOTE,
"grp_mask = 0x%x",awdetmask,0,0,0,0);
370 UINT64 detmask = grp2rts_mask(awdetmask);
373 LOG(NOTE,
"potential det_mask = 0x%llx dets_in_run_mask 0x%llx",detmask,dets_in_run_mask,0,0,0);
375 detmask &= dets_in_run_mask;
376 detmask |= (1ll<<TRG_SYSTEM);
378 LOG(NOTE,
"final det_mask = 0x%llx",detmask,0,0,0,0);
380 pay->rtsDetMask = l2h64(detmask);
382 LOG(NOTE,
"super final det_mask = 0x%llx",pay->rtsDetMask,0,0,0,0);
384 if(pay->flags & EVBFLAG_L25ABORT) {
385 LOG(DBG,
"Sending L25Abort: token=%d event=%d 1l=0x%x l2=0x%x l2abort=%d",
386 token, eventNumber, l1trg_lo, l2trg_lo, l25abort);
390 LOG(DBG,
"l1: 0x%x/0x%x, l2: 0x%x/0x%x",
394 pay->L2summary[1],0);
399 int prepareTokenZeroPayload(
gbPayload *pay,
int eventNumber)
405 des->TrgToken = b2h16(0);
409 des->actionWdDetectorBitMask = b2h16(0);
411 des->TrgDataFmtVer = FORMAT_VERSION & 0x000000ff;
414 pay->gbPayloadVersion = l2h32(GB_PAYLOAD_VERSION);
415 pay->eventNumber = l2h32(eventNumber);
419 UINT32 tzt_lo = tokenZeroTriggers & 0xffffffff;
420 UINT32 tzt_hi = (tokenZeroTriggers >> 32) & 0xffffffff;
422 pay->L1summary[0] = l2h32(tzt_lo);
423 pay->L1summary[1] = l2h32(tzt_hi);
424 pay->L2summary[0] = l2h32(tzt_lo);
425 pay->L2summary[1] = l2h32(tzt_hi);
426 pay->L3summary[0] = l2h32(tzt_lo);
427 pay->L3summary[1] = l2h32(tzt_hi);
430 pay->L3summary[3] = pay->evp;
434 clock_gettime(CLOCK_REALTIME, &tm);
435 pay->sec = l2h32(tm.tv_sec);
436 pay->usec = l2h32(tm.tv_nsec * 1000);
439 gettimeofday(&tm, NULL);
440 pay->sec = tm.tv_sec;
441 pay->usec = tm.tv_usec;
445 UINT64 detmask = dets_in_run_mask | (1ll <<TRG_ID);
446 pay->rtsDetMask = l2h64(detmask);