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

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