source: trunk/FACT++/src/EventBuilder.h@ 18066

Last change on this file since 18066 was 17679, checked in by tbretz, 11 years ago
Fixed a bug which could cause infinite loops when an event with invalid FADhead was checked for timout (the it++ was missing), added a lot of comments
File size: 5.8 KB
Line 
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
10namespace 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*/
26extern int g_reset ; //>0 = reset different levels of eventbuilder
27extern size_t g_maxMem ; //maximum memory allowed for buffer
28extern uint16_t g_evtTimeout; //timeout (sec) for one event
29
30extern FACT_SOCK g_port[NBOARDS] ; // .port = baseport, .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
31
32extern uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
33
34class DrsCalibration;
35
36enum FileStatus_t
37{
38 kFileNotYetOpen,
39 kFileOpen,
40 kFileClosed
41};
42
43enum CloseRequest_t
44{
45 kRequestNone = 0,
46 kRequestManual = 1<<1,
47 kRequestTimeout = 1<<2,
48 kRequestConnectionChange = 1<<3,
49 kRequestEventCheckFailed = 1<<4,
50 kRequestMaxEvtsReached = 1<<5,
51 kRequestMaxTimeReached = 1<<6
52};
53
54
55struct 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
74 FileStatus_t fileStat;
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
98namespace 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
113struct 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
135 int closeRequest;
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 }
160 ~EVT_CTRL2()
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
Note: See TracBrowser for help on using the repository browser.