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

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