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

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