BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnProfilometerSource.cxx
Go to the documentation of this file.
2
3BmnProfilometerSource::BmnProfilometerSource(vector<string> addr, vector<string> boardIds)
7 , fRunId(0)
8 , fPeriodId(8)
9 , fVerbosity(0)
10 , iEventNumber(0)
11 , isSpillStart(true)
12 , fArr(nullptr)
13 , fArrTemp(nullptr)
14 , fAddrs(move(addr))
15 , fBoardIds(move(boardIds))
16{}
17
19{
20 for (auto& it : fBoardsMap)
21 delete it.second;
22}
23
25{
26 printf("BmnProfilometerSource::Init()\n");
27 _ctx = zmq_ctx_new();
28 // _rawSocket = zmq_socket(_ctx, ZMQ_SUB);
29 for (string& addr : fAddrs) {
30 void* raw_socket = zmq_socket(_ctx, ZMQ_SUB);
31 fRawSockets.push_back(raw_socket);
32 if (zmq_connect(raw_socket, addr.c_str()) != 0) {
33 // DBGERR("zmq connect")
34 fprintf(stderr, "Error connecting to ZMQ socket: %s\n", strerror(errno));
35 return kFALSE;
36 } else {
37 printf("connected to %s\n", addr.c_str());
38 if (zmq_setsockopt(raw_socket, ZMQ_SUBSCRIBE, NULL, 0) == -1) {
39 // DBGERR("zmq subscribe")
40 fprintf(stderr, "Error subscribing to ZMQ socket: %s\n", strerror(errno));
41 return kFALSE;
42 }
43 }
44 }
45
46 FairRootManager* ioman = FairRootManager::Instance();
48 fArrTemp = new TClonesArray(fArr->GetClass());
49 ioman->RegisterInputObject("ADC192ASIC", fArr);
51 ioman->RegisterInputObject("BmnEventHeader.", fEventHead);
52 TPRegexp re("\\w+(\\d+)");
53 for (string& id : fBoardIds) {
54 ProfBoard* b = new ProfBoard();
55 TString str(id);
56 re.Substitute(str, "$1");
57 b->board_id = str.Atoi();
58 fBoardsMap.insert(make_pair(id, b));
59 }
60 return kTRUE;
61}
62
64{
65 for (void* rs : fRawSockets)
66 zmq_close(rs);
67 zmq_ctx_destroy(_ctx);
68 _ctx = NULL;
69}
70
72{
73 fArr->Delete();
74 if (fArrTemp->GetEntriesFast() == 0) {
75 if (fVerbosity)
76 printf("Empty\n");
78 isSpillStart = true;
79 iEventNumber = 0;
80 }
81 // printf("entries before %d\n", fArrTemp->GetEntriesFast());
82 fArr->AbsorbObjects(fArrTemp, 0, 0);
84 fEventHead->SetEventId(iEventNumber);
85 fEventHead->SetSpillStart(isSpillStart);
86 if (isSpillStart)
87 isSpillStart = false;
88 // printf("entries after %d\n", fArrTemp->GetEntriesFast());
89 return 0;
90}
91
96
97BmnStatus BmnProfilometerSource::ProcessBuffer(uint32_t* word, size_t len, ProfBoard* board)
98{
99 uint32_t holdb_temp = 0;
100 uint32_t holdb = 0;
101 // string str(reinterpret_cast<char*> (word), 6);
102 // printf("str %s\n", str.c_str());
103 for (uint32_t i = 0; i < len; i++) {
104 uint32_t data = word[i];
105 // printf("data %08X i = %05u\n",data, i);
106 // Check is it data or a trigger:
108 holdb = BmnProfRawTools::holdb_cntr(data); // get holdb counter
109 if (holdb_temp == holdb) {
110 // printf("data %08X\n", data);
111 // Divide ADC0 and ADC1 data
112 if (!BmnProfRawTools::adc_num(data)) { // 1st ADC
113 board->adc1_word.push_back(data);
114 } else if (BmnProfRawTools::adc_num(data)) { // 2nd ADC
115 board->adc2_word.push_back(data);
116 }
117 } else {
118 if (board->adc1_word.size() == BmnProfRawTools::ChannelDigitCnt())
119 new ((*fArrTemp)[fArrTemp->GetEntriesFast()])
120 BmnADCDigit<uint16_t>(board->board_id, 0, std::move(board->adc1_word));
121 // if (board->adc2_word.size() == BmnProfRawTools::ChannelDigitCnt())
122 // new((*fArr)[fArr->GetEntriesFast()]) BmnADCDigit(board->board_id, 1,
123 // BmnProfRawTools::ChannelDigitCnt(), board->adc2_word);
124 // printf("adc1_word %lu\n", adc1_word.size());
125 // printf("adc1 %lu\n", adc1.size());
126 board->adc1_word.clear();
127 board->adc2_word.clear();
128 // Divide ADC0 and ADC1 data
129 if (!BmnProfRawTools::adc_num(data)) { // 1st ADC
130 board->adc1_word.push_back(data);
131 } else { // 2nd ADC
132 board->adc2_word.push_back(data);
133 }
134 holdb_temp = holdb;
135 }
136 } else {
137 // if (BmnProfRawTools::trig_psd(data)) {
138 // trigger_pside.push_back(data);
139 // } else if (BmnProfRawTools::trig_nsd(data)) {
140 // trigger_nside.push_back(data);
141 // }
142 }
143 }
144 return kBMNSUCCESS;
145}
146
148{
149 const Int_t MaxStrLen = 100;
150 bool isIdFound = kFALSE;
151 bool isHeaderFound = kFALSE;
152 bool isTrailerFound = kFALSE;
153 string BoardName;
154 ProfBoard* Board = nullptr;
155 do {
156 Int_t recv_more = 0;
157 isIdFound = kFALSE;
158 do {
159 // gSystem->ProcessEvents();
160 // fServer->ProcessRequests();
161 for (void* rs : fRawSockets) {
162 zmq_msg_t msg;
163 zmq_msg_init(&msg);
164 Int_t frame_size = zmq_msg_recv(&msg, rs, ZMQ_DONTWAIT); // ZMQ_DONTWAIT
165 // printf("recv %d\n", frame_size);
166 if (frame_size == -1) {
167 // printf("Receive error # %d #%s\n", errno, zmq_strerror(errno));
168 switch (errno) {
169 case EAGAIN:
170 // printf("EAGAIN\n");
171 usleep(50000);
172 break;
173 case EINTR:
174 if (fVerbosity)
175 printf("EINTR\n");
178 if (fVerbosity)
179 printf("Exit!\n");
180 break;
181 case EFAULT:
182 if (fVerbosity)
183 printf("EFAULT\n");
184 zmq_close(rs);
187 break;
188 default:
189 break;
190 }
191 } else {
192 if (frame_size < MaxStrLen) {
193 string str(static_cast<char*>(zmq_msg_data(&msg)), zmq_msg_size(&msg));
194 // printf("str %s\n", str.c_str());
195 if (isIdFound) {
196 if (str == DataTrailer) {
197 isTrailerFound = kTRUE;
198 if (fVerbosity)
199 printf("trailer\n");
201 }
202 if (str == DataHeader) {
203 isHeaderFound = kTRUE;
204 if (fVerbosity)
205 printf("header\n");
206 }
207 } else {
208 // if (str == TargetBoardId) {
209 BoardName = str;
210 auto it = fBoardsMap.find(BoardName);
211 if (it != fBoardsMap.end())
212 Board = it->second;
213 isIdFound = kTRUE;
214 if (fVerbosity)
215 printf("id %s\n", str.c_str());
216 // }
217 }
218 } else {
219 if (isHeaderFound && Board) {
220 ProcessBuffer(static_cast<uint32_t*>(zmq_msg_data(&msg)), zmq_msg_size(&msg) / 4, Board);
221 }
222 }
223 }
224 size_t opt_size = sizeof(recv_more);
225 if (zmq_getsockopt(rs, ZMQ_RCVMORE, &recv_more, &opt_size) == -1) {
226 printf("ZMQ socket options error #%s\n", zmq_strerror(errno));
227 return kBMNERROR;
228 }
229 // printf("ZMQ rcvmore = %d\n", recv_more);
230 zmq_msg_close(&msg);
231 }
232 } while (recv_more && isReceiving && (!isTrailerFound));
233 } while (keepWorking);
234 if (fVerbosity)
235 printf("FullReceive\n");
236 if (isTrailerFound) {
237 if (fVerbosity)
238 printf("SPILLEND for %s\n", BoardName.c_str());
239 isHeaderFound = kFALSE;
240 isTrailerFound = kFALSE;
241 }
242 return kBMNSUCCESS;
243}
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
BmnStatus
Define status code.
Definition BmnEnums.h:93
@ kBMNERROR
Error.
Definition BmnEnums.h:95
@ kBMNSUCCESS
Success.
Definition BmnEnums.h:94
static bool data_or_trig(const uint32_t &word)
static constexpr const size_t ChannelDigitCnt()
static uint32_t holdb_cntr(const uint32_t &word)
static bool adc_num(const bitset< 32 > &word)
BmnProfilometerSource(vector< string > addr={"tcp://159.93.49.126:5601", "tcp://159.93.49.126:5602"}, vector< string > boardIds={"board1", "board2"})
void FillEventHeader(FairEventHeader *feh)
map< string, ProfBoard * > fBoardsMap
BmnStatus ProcessBuffer(uint32_t *word, size_t len, ProfBoard *board)
static TClass * Class()
Int_t Substitute(TString &s, const TString &replace, const TString &mods="", Int_t start=0, Int_t nMatchMax=10)
vector< uint16_t > adc2_word
vector< uint16_t > adc1_word