FACT++  1.0
EventBuilder.h
Go to the documentation of this file.
1 #ifndef FACT_EventBuilder
2 #define FACT_EventBuilder
3 
4 #include "FAD.h"
5 
6 #include <list>
7 #include <array>
8 #include <forward_list>
9 
10 namespace std
11 {
12  class mutex;
13 }
14 
15 
16 /* global variables;
17  to avoid race canoditions, only one thread is allowed to write
18  the name of the variable defines which process shall write it:
19 
20  g_XXX : main control thread
21  gi_XX : input thread (reading from camera)
22  gw_XX : write thread (writing to disk)
23  qp_XX : processing thread(s) (processing data, eg. soft-trig)
24 
25 */
26 extern int g_reset ; //>0 = reset different levels of eventbuilder
27 extern size_t g_maxMem ; //maximum memory allowed for buffer
28 extern uint16_t g_evtTimeout; //timeout (sec) for one event
29 
30 extern FACT_SOCK g_port[NBOARDS] ; // .port = baseport, .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
31 
32 extern uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
33 
34 class DrsCalibration;
35 
37 {
41 };
42 
44 {
52 };
53 
54 
55 struct RUN_CTRL2
56 {
57  int64_t runId ; // Run number
58 
59  time_t reportMem; // initMemory has reported no memory once (set outside of class)
60 
61  time_t openTime; // Time when first event (first board) was received
62  time_t lastTime; // Time when last event was received (set when first board data received)
63  time_t closeTime; // Time when run should be closed
64  uint32_t night; // night as int as determined for this run
65 
66  uint32_t lastEvt; // number of events received (counted when the first board was received)
67  uint32_t maxEvt; // maximum number which should be written to file
68 
69  uint16_t roi0; // roi for normal pixels
70  uint16_t roi8; // roi for pixels8
71 
72  std::string runType;
73 
75 
76  std::array<uint32_t, 8> triggerCounter; // triggerCounter must only be manipulated in procEvt to keep it thread safe
77 
78  std::shared_ptr<DrsCalibration> calib;
79  std::list<std::array<int16_t,1440>> prevStart; // History for start cells of previous events (for step calibration)
80 
81  RUN_CTRL2() : runId(-1), reportMem(0), lastTime(0), lastEvt(0), maxEvt(1<<31), fileStat(kFileNotYetOpen)
82  {
83  triggerCounter.fill(0);
84 
85  // runId = -1;
86  // fileId = kFileNotYetOpen;
87  // lastEvt = 0; // Number of events partially started to read
88  // actEvt = 0; // Number of written events
89  // maxEvt = 1<<31; // max number events allowed (~2400min @ 250Hz)
90 
91  }
92  //~RUN_CTRL2();
93 };
94 
95 #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
96 #define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
97 
98 namespace Memory
99 {
100  extern uint64_t inuse;
101  extern uint64_t allocated;
102 
103  extern uint64_t max_inuse;
104 
105  extern std::mutex mtx;
106 
107  extern std::forward_list<void*> memory;
108 
109  extern void *malloc();
110  extern void free(void *mem);
111 };
112 
113 struct EVT_CTRL2
114 {
115  uint32_t runNum; // header->runnumber;
116  uint32_t evNum; // header->fad_evt_counter
117 
118  uint32_t trgNum; // header->trigger_id
119  uint32_t trgTyp; // header->trigger_type
120  uint32_t fadLen;
121 
122  //uint16_t firstBoard; // first board from which data was received
123  uint16_t nBoard;
124  int16_t board[NBOARDS];
125 
126  uint16_t nRoi;
127  uint16_t nRoiTM;
128 
129  timeval time;
130 
131  PEVNT_HEADER *FADhead; // Pointer to the whole allocated memory
132  EVENT *fEvent; // Pointer to the event data itself
133  PEVNT_HEADER *header; // Pointer to a valid header within FADhead
134 
136 
137  std::array<uint32_t, 8> triggerCounter; // triggerCounter must only be manipulated in procEvt to keep it thread safe
138 
139  std::shared_ptr<RUN_CTRL2> runCtrl;
140 
141  // Be carefull with this constructor... writeEvt can seg fault
142  // it gets an empty runCtrl
143  EVT_CTRL2() : nBoard(0), FADhead(0), header(0), closeRequest(kRequestNone)
144  {
145  //flag all boards as unused
146  std::fill(board, board+NBOARDS, -1);
147  }
148  /*
149  EVT_CTRL2(CloseRequest_t req) : nBoard(0), FADhead(0), header(0), reportMem(false), closeRequest(req), runCtrl(new RUN_CTRL2)
150  {
151  //flag all boards as unused
152  std::fill(board, board+NBOARDS, -1);
153  }*/
154 
155  EVT_CTRL2(int req, const std::shared_ptr<RUN_CTRL2> &run) : nBoard(0), FADhead(0), header(0), closeRequest(req), runCtrl(run)
156  {
157  //flag all boards as unused
158  std::fill(board, board+NBOARDS, -1);
159  }
161  {
162  Memory::free(FADhead);
163  }
164 
165  operator RUN_HEAD() const
166  {
167  RUN_HEAD rh;
168 
169  rh.Nroi = nRoi;
170  rh.NroiTM = nRoiTM;
171  rh.RunTime = time.tv_sec;
172  rh.RunUsec = time.tv_usec;
173 
174  memcpy(rh.FADhead, FADhead, NBOARDS*sizeof(PEVNT_HEADER));
175 
176  return rh;
177  }
178 
179  bool valid() const { return header; }
180 
181  bool initMemory()
182  {
183  // We have a valid entry, but no memory has yet been allocated
184  if (FADhead)
185  return true;
186 
187  FADhead = (PEVNT_HEADER*)Memory::malloc();
188  if (!FADhead)
189  return false;
190 
191  fEvent = reinterpret_cast<EVENT*>(FADhead+NBOARDS);
192 
193  memset(FADhead, 0, (NPIX+NTMARK)*2*nRoi+NBOARDS*sizeof(PEVNT_HEADER)+sizeof(EVENT));
194 
195  //flag all pixels as unused, flag all TMark as unused
196  std::fill(fEvent->StartPix, fEvent->StartPix+NPIX, -1);
197  std::fill(fEvent->StartTM, fEvent->StartTM +NTMARK, -1);
198 
199  fEvent->Roi = nRoi;
200  fEvent->RoiTM = nRoiTM;
201  fEvent->EventNum = evNum;
202  fEvent->TriggerNum = trgNum;
203  fEvent->TriggerType = trgTyp;
204 
205  return true;
206  }
207 };
208 
209 #endif
uint32_t RunTime
Definition: FAD.h:96
uint32_t lastEvt
Definition: EventBuilder.h:66
std::mutex mtx
Definition: EventBuilder.cc:95
std::string runType
Definition: EventBuilder.h:72
FileStatus_t
Definition: EventBuilder.h:36
uint gi_NumConnect[NBOARDS]
Definition: EventBuilder.cc:82
PEVNT_HEADER * FADhead
Definition: EventBuilder.h:131
uint16_t NroiTM
Definition: FAD.h:102
std::forward_list< void * > memory
Definition: EventBuilder.cc:97
uint32_t evNum
Definition: EventBuilder.h:116
EVENT * fEvent
Definition: EventBuilder.h:132
int closeRequest
Definition: EventBuilder.h:135
Definition: FAD.h:123
int64_t runId
Definition: EventBuilder.h:57
CloseRequest_t
Definition: EventBuilder.h:43
uint32_t runNum
Definition: EventBuilder.h:115
STL namespace.
EVT_CTRL2(int req, const std::shared_ptr< RUN_CTRL2 > &run)
Definition: EventBuilder.h:155
void * malloc()
Definition: EventBuilder.cc:99
uint16_t nRoi
Definition: EventBuilder.h:126
uint16_t nRoiTM
Definition: EventBuilder.h:127
uint16_t g_evtTimeout
Definition: EventBuilder.cc:78
uint16_t roi8
Definition: EventBuilder.h:70
uint32_t trgTyp
Definition: EventBuilder.h:119
FACT_SOCK g_port[NBOARDS]
Definition: EventBuilder.cc:80
uint64_t inuse
Definition: EventBuilder.cc:90
Definition: FAD.h:92
bool valid() const
Definition: EventBuilder.h:179
time_t lastTime
Definition: EventBuilder.h:62
uint32_t maxEvt
Definition: EventBuilder.h:67
#define NPIX
Definition: BasicGlCamera.h:5
uint64_t max_inuse
Definition: EventBuilder.cc:93
bool initMemory()
Definition: EventBuilder.h:181
uint16_t Nroi
Definition: FAD.h:101
time_t reportMem
Definition: EventBuilder.h:59
uint16_t roi0
Definition: EventBuilder.h:69
std::shared_ptr< RUN_CTRL2 > runCtrl
Definition: EventBuilder.h:139
std::array< uint32_t, 8 > triggerCounter
Definition: EventBuilder.h:76
static int run
Definition: dim_fork.cxx:9
uint32_t night
Definition: EventBuilder.h:64
uint32_t evNum
#define NTMARK
Definition: BasicGlCamera.h:6
#define NBOARDS
Definition: BasicGlCamera.h:4
FileStatus_t fileStat
Definition: EventBuilder.h:74
timeval time
Definition: EventBuilder.h:129
void free(void *mem)
int g_reset
Definition: EventBuilder.cc:74
uint32_t trgNum
Definition: EventBuilder.h:118
uint32_t RunUsec
Definition: FAD.h:97
time_t openTime
Definition: EventBuilder.h:61
size_t g_maxMem
Definition: EventBuilder.cc:76
uint16_t nBoard
Definition: EventBuilder.h:123
uint64_t allocated
Definition: EventBuilder.cc:91
std::list< std::array< int16_t, 1440 > > prevStart
Definition: EventBuilder.h:79
std::array< uint32_t, 8 > triggerCounter
Definition: EventBuilder.h:137
std::shared_ptr< DrsCalibration > calib
Definition: EventBuilder.h:78
uint32_t fadLen
Definition: EventBuilder.h:120
RUN_HEAD()
Definition: FAD.h:592
PEVNT_HEADER * header
Definition: EventBuilder.h:133
time_t closeTime
Definition: EventBuilder.h:63
PEVNT_HEADER FADhead[NBOARDS]
Definition: FAD.h:105