FACT++  1.0
shared_ptr<EVT_CTRL2> mBufEvt ( const READ_STRUCT rd,
shared_ptr< RUN_CTRL2 > &  actrun 
)

Definition at line 577 of file EventBuilder.cc.

References actrun, checkRoiConsistency(), factPrintf(), gotNewRun(), READ_STRUCT::H, MessageImp::kError, MessageImp::kInfo, kRequestMaxEvtsReached, kRequestMaxTimeReached, runFinished(), EVT_CTRL2::time, and READ_STRUCT::time.

Referenced by mainloop().

578 {
579  /*
580  checkroi consistence
581  find existing entry
582  if no entry, try to allocate memory
583  if entry and memory, init event structure
584  */
585 
586  uint16_t nRoi[9];
587  if (!checkRoiConsistency(rd, nRoi))
588  return shared_ptr<EVT_CTRL2>();
589 
590  for (auto it=evtCtrl.rbegin(); it!=evtCtrl.rend(); it++)
591  {
592  // A reference is enough because the evtCtrl holds the shared_ptr anyway
593  const shared_ptr<EVT_CTRL2> &evt = *it;
594 
595  // If the run is different, go on searching.
596  // We cannot stop searching if a lower run-id is found as in
597  // the case of the events, because theoretically, there
598  // can be the same run on two different days.
599  if (rd.H.runnumber != evt->runNum)
600  continue;
601 
602  // If the ID of the new event if higher than the last one stored
603  // in that run, we have to assign a new slot (leave the loop)
604  if (rd.H.fad_evt_counter > evt->evNum/* && runID == evtCtrl[k].runNum*/)
605  break;
606 
607  if (rd.H.fad_evt_counter != evt->evNum/* || runID != evtCtrl[k].runNum*/)
608  continue;
609 
610  // We have found an entry with the same runID and evtID
611  // Check if ROI is consistent
612  if (evt->nRoi != nRoi[0] || evt->nRoiTM != nRoi[8])
613  {
614  factPrintf(MessageImp::kError, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
615  evt->nRoi, evt->nRoiTM, nRoi[0], nRoi[8]);
616  return shared_ptr<EVT_CTRL2>();
617  }
618 
619  // It is maybe not likely, but the header of this board might have
620  // arrived earlier. (We could also update the run-info, but
621  // this should not make a difference here)
622  if ((rd.time.tv_sec==evt->time.tv_sec && rd.time.tv_usec<evt->time.tv_usec) ||
623  rd.time.tv_sec<evt->time.tv_sec)
624  evt->time = rd.time;
625 
626  //everything seems fine so far ==> use this slot ....
627  return evt;
628  }
629 
630  if (actrun->runId==rd.H.runnumber && (actrun->roi0 != nRoi[0] || actrun->roi8 != nRoi[8]))
631  {
632  factPrintf(MessageImp::kError, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d (runID=%d, evID=%d)",
633  actrun->roi0, actrun->roi8, nRoi[0], nRoi[8], rd.H.runnumber, rd.H.fad_evt_counter);
634  return shared_ptr<EVT_CTRL2>();
635  }
636 
637  EVT_CTRL2 *evt = new EVT_CTRL2;
638 
639  evt->time = rd.time;
640 
641  evt->runNum = rd.H.runnumber;
642  evt->evNum = rd.H.fad_evt_counter;
643 
644  evt->trgNum = rd.H.trigger_id;
645  evt->trgTyp = rd.H.trigger_type;
646 
647  evt->nRoi = nRoi[0];
648  evt->nRoiTM = nRoi[8];
649 
650  //evt->firstBoard = rd.sockId;
651 
652  const bool newrun = actrun->runId != rd.H.runnumber;
653  if (newrun)
654  {
655  // Since we have started a new run, we know already when to close the
656  // previous run in terms of number of events
657  actrun->maxEvt = actrun->lastEvt;
658 
659  factPrintf(MessageImp::kInfo, "New run %d (evt=%d) registered with roi=%d(%d), prev=%d",
660  rd.H.runnumber, rd.H.fad_evt_counter, nRoi[0], nRoi[8], actrun->runId);
661 
662  // The new run is the active run now
663  actrun = make_shared<RUN_CTRL2>();
664 
665  const time_t &tsec = evt->time.tv_sec;
666 
667  actrun->openTime = tsec;
668  actrun->closeTime = tsec + 3600 * 24; // max time allowed
669  actrun->runId = rd.H.runnumber;
670  actrun->roi0 = nRoi[0]; // FIXME: Make obsolete!
671  actrun->roi8 = nRoi[8]; // FIXME: Make obsolete!
672 
673  // Signal the fadctrl that a new run has been started
674  // Note this is the only place at which we can ensure that
675  // gotnewRun is called only once
676  gotNewRun(*actrun);
677  }
678 
679  // Keep pointer to run of this event
680  evt->runCtrl = actrun;
681 
682  // Increase the number of events we have started to receive in this run
683  actrun->lastTime = evt->time.tv_sec; // Time when the last event was received
684  actrun->lastEvt++;
685 
686  // An event can be the first and the last, but not the last and the first.
687  // Therefore gotNewRun is called before runFinished.
688  // runFinished signals that the last event of a run was just received. Processing
689  // might still be ongoing, but we can start a new run.
690  const bool cond1 = actrun->lastEvt < actrun->maxEvt; // max number of events not reached
691  const bool cond2 = actrun->lastTime < actrun->closeTime; // max time not reached
692  if (!cond1 || !cond2)
693  runFinished();
694 
695  // We don't mind here that this is not common to all events,
696  // because every coming event will fullfil the condition as well.
697  if (!cond1)
698  evt->closeRequest |= kRequestMaxEvtsReached;
699  if (!cond2)
700  evt->closeRequest |= kRequestMaxTimeReached;
701 
702  // Secure access to evtCtrl against access in CloseRunFile
703  // This should be the last... otherwise we can run into threading issues
704  // if the event is accessed before it is fully initialized.
705  evtCtrl.emplace_back(evt);
706  return evtCtrl.back();
707 }
timeval time
void gotNewRun(RUN_CTRL2 &run)
void factPrintf(int severity, const char *fmt,...)
list< shared_ptr< EVT_CTRL2 > > evtCtrl
shared_ptr< RUN_CTRL2 > actrun
PEVNT_HEADER H
An info telling something which can be interesting to know.
Definition: MessageImp.h:17
timeval time
Definition: EventBuilder.h:129
Error, something unexpected happened, but can still be handled by the program.
Definition: MessageImp.h:19
bool checkRoiConsistency(const READ_STRUCT &rd, uint16_t roi[])
void runFinished()

+ Here is the call graph for this function:

+ Here is the caller graph for this function: