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

Last change on this file since 16542 was 16380, checked in by tbretz, 11 years ago
Added more kRequest enums; moved the request form the run-ctrl (where it would be executed immediately) to the evt-ctrl; moved the definition and handling of the memory here - there is no need for a shared_ptr for fEvent; keep a history of the start-cells for step removal in the calibration; added a pointer to a valid header.
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
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 openTime; // Time when first event (first board) was received
52 time_t lastTime; // Time when last event was received (set when first board data received)
53 time_t closeTime; // Time when run should be closed
54 uint32_t night; // night as int as determined for this run
55
56 uint32_t lastEvt; // number of events received (counted when the first board was received)
57 uint32_t maxEvt; // maximum number which should be written to file
58
59 uint16_t roi0; // roi for normal pixels
60 uint16_t roi8; // roi for pixels8
61
62 std::string runType;
63
64 FileStatus_t fileStat;
65
66 std::shared_ptr<DrsCalibration> calib;
67 std::list<std::array<int16_t,1440>> prevStart; // History for start cells of previous events (for step calibration)
68
69 RUN_CTRL2() : runId(-1), lastTime(0), lastEvt(0), maxEvt(1<<31), fileStat(kFileNotYetOpen)
70 {
71 // runId = -1;
72 // fileId = kFileNotYetOpen;
73 // lastEvt = 0; // Number of events partially started to read
74 // actEvt = 0; // Number of written events
75 // maxEvt = 1<<31; // max number events allowed (~2400min @ 250Hz)
76
77 }
78 //~RUN_CTRL2();
79};
80
81#define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
82#define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
83
84namespace Memory
85{
86 extern uint64_t inuse;
87 extern uint64_t allocated;
88
89 extern uint64_t max_inuse;
90
91 extern std::mutex mtx;
92
93 extern std::forward_list<void*> memory;
94
95 extern void *malloc();
96 extern void free(void *mem);
97};
98
99struct EVT_CTRL2
100{
101 uint32_t runNum; // header->runnumber;
102 uint32_t evNum; // header->fad_evt_counter
103
104 uint32_t trgNum; // header->trigger_id
105 uint32_t trgTyp; // header->trigger_type
106 uint32_t fadLen;
107
108 uint16_t nBoard;
109 int16_t board[NBOARDS];
110
111 uint16_t nRoi;
112 uint16_t nRoiTM;
113
114 timeval time;
115 uint8_t Errors[4];
116
117 PEVNT_HEADER *FADhead; // Pointer to the whole allocated memory
118 EVENT *fEvent; // Pointer to the event data itself
119 PEVNT_HEADER *header; // Pointer to a valid header within FADhead
120
121 bool reportMem; // initMemory has reported no memory once (set outside of class)
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), reportMem(false), 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), reportMem(false), 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(fEvent->Adc_Data, 0, (NPIX+NTMARK)*2*nRoi);
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->SoftTrig = 0;
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.