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

Last change on this file since 17676 was 17658, checked in by tbretz, 11 years ago
Even if the event is not valid, attach the latest trigger counter to it.
File size: 5.7 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 nBoard;
123 int16_t board[NBOARDS];
124
125 uint16_t nRoi;
126 uint16_t nRoiTM;
127
128 timeval time;
129
130 PEVNT_HEADER *FADhead; // Pointer to the whole allocated memory
131 EVENT *fEvent; // Pointer to the event data itself
132 PEVNT_HEADER *header; // Pointer to a valid header within FADhead
133
134 int closeRequest;
135
136 std::array<uint32_t, 8> triggerCounter; // triggerCounter must only be manipulated in procEvt to keep it thread safe
137
138 std::shared_ptr<RUN_CTRL2> runCtrl;
139
140 // Be carefull with this constructor... writeEvt can seg fault
141 // it gets an empty runCtrl
142 EVT_CTRL2() : nBoard(0), FADhead(0), header(0), closeRequest(kRequestNone)
143 {
144 //flag all boards as unused
145 std::fill(board, board+NBOARDS, -1);
146 }
147 /*
148 EVT_CTRL2(CloseRequest_t req) : nBoard(0), FADhead(0), header(0), reportMem(false), closeRequest(req), runCtrl(new RUN_CTRL2)
149 {
150 //flag all boards as unused
151 std::fill(board, board+NBOARDS, -1);
152 }*/
153
154 EVT_CTRL2(int req, const std::shared_ptr<RUN_CTRL2> &run) : nBoard(0), FADhead(0), header(0), closeRequest(req), runCtrl(run)
155 {
156 //flag all boards as unused
157 std::fill(board, board+NBOARDS, -1);
158 }
159 ~EVT_CTRL2()
160 {
161 Memory::free(FADhead);
162 }
163
164 operator RUN_HEAD() const
165 {
166 RUN_HEAD rh;
167
168 rh.Nroi = nRoi;
169 rh.NroiTM = nRoiTM;
170 rh.RunTime = time.tv_sec;
171 rh.RunUsec = time.tv_usec;
172
173 memcpy(rh.FADhead, FADhead, NBOARDS*sizeof(PEVNT_HEADER));
174
175 return rh;
176 }
177
178 bool valid() const { return header; }
179
180 bool initMemory()
181 {
182 // We have a valid entry, but no memory has yet been allocated
183 if (FADhead)
184 return true;
185
186 FADhead = (PEVNT_HEADER*)Memory::malloc();
187 if (!FADhead)
188 return false;
189
190 fEvent = reinterpret_cast<EVENT*>(FADhead+NBOARDS);
191
192 memset(FADhead, 0, (NPIX+NTMARK)*2*nRoi+NBOARDS*sizeof(PEVNT_HEADER)+sizeof(EVENT));
193
194 //flag all pixels as unused, flag all TMark as unused
195 std::fill(fEvent->StartPix, fEvent->StartPix+NPIX, -1);
196 std::fill(fEvent->StartTM, fEvent->StartTM +NTMARK, -1);
197
198 fEvent->Roi = nRoi;
199 fEvent->RoiTM = nRoiTM;
200 fEvent->EventNum = evNum;
201 fEvent->TriggerNum = trgNum;
202 fEvent->TriggerType = trgTyp;
203
204 return true;
205 }
206};
207
208#endif
Note: See TracBrowser for help on using the repository browser.