BmnRoot
Loading...
Searching...
No Matches
BmnMonProf.cxx
Go to the documentation of this file.
1#include <stdio.h>
2// Auxillary
3#include <boost/circular_buffer.hpp>
4#include <boost/exception/all.hpp>
5#include <boost/property_tree/json_parser.hpp>
6#include <boost/property_tree/ptree.hpp>
7#include <zmq.h>
8// FairSoft
9#include <TBufferFile.h>
10#include <TCanvas.h>
11#include <TH2F.h>
12#include <THttpServer.h>
13#include <TSystem.h>
14#include <root/TH1.h>
15// BmnRoot
16#include "BmnDataReceiver.h"
17#include "BmnProfRawTools.h"
18
19namespace pt = boost::property_tree;
20
21#define PAD_WIDTH 1920
22#define PAD_HEIGHT 2600
23const size_t ASIC_channel = 32; // for the beam profilometer
24const size_t NChannels_run9 = 64; //
25// size_t ASIC_channel = 64; // for the beam tracker
26const string DataHeader = "DAT";
27const string DataTrailer = "SPILLEND";
28
29uint32_t temp_cntr = 0;
30const size_t SpillCount = 10;
31
32vector<boost::circular_buffer<vector<float>>> cb;
33vector<boost::circular_buffer<size_t>> cb_event_count;
34
35vector<TH1*> hVec;
36vector<string> hVecOpt;
37vector<TH1*> hVec2D;
38vector<vector<int>> adc1_word_char;
39vector<vector<vector<int>>> adc1_ch;
40vector<vector<float>> adc1_ch_mean; // mean by spill
41
42vector<vector<bitset<ASIC_channel>>> adc1, adc2;
43vector<bitset<ASIC_channel>> adc1_word, adc2_word;
44// vector<uint32_t> trigger_pside, trigger_nside;
45uint32_t spill_cntr = 0;
46size_t NChars = 0;
47const int NBins = 200;
48const int ADCLimit = 2050;
49const float thr = 0;
50const int NCols = 2; // canvas columns
51const int NGenHists = 4;
52TCanvas* can2d(nullptr);
53TCanvas* can(nullptr);
54
56{
59 int channelLow = 0;
60 int channelHi = 0;
62};
63vector<ProfiMap> channel_maps;
64
65int ReadMap(string name)
66{
67 try {
68 pt::ptree conf;
69 pt::read_json(name, conf);
70 pt::ptree pads = conf.get_child("modules");
71 for (auto v = pads.begin(); v != pads.end(); v++) {
72 cout << (*v).second.get_optional<Int_t>("nAsicChannels") << endl;
73 pt::ptree maps = (*v).second.get_child("channelMapping");
74 for (auto m = maps.begin(); m != maps.end(); m++) {
75 channel_maps.push_back(ProfiMap{.ChannelName = (*m).second.get<char>("adcChName"),
76 .LayerType = (*m).second.get<char>("layerType"),
77 .channelLow = (*m).second.get<int>("chLow"),
78 .channelHi = (*m).second.get<int>("chHigh")});
79 pt::ptree strips = (*m).second.get_child("stripsMapping");
80 for (auto stripNode = strips.begin(); stripNode != strips.end(); stripNode++) {
81 auto it = (*stripNode).second.begin();
82 // cout << it->second.get_value<int>() << " : " <<
83 // (++it)->second.get_value<int>()<< endl;
84 int stripId = (it)->second.get_value<int>();
85 int chanlId = (++it)->second.get_value<int>();
86 channel_maps.back().StripMap[chanlId] = stripId;
87 }
88 }
89 }
90 } catch (boost::exception& e) {
91 cerr << boost::diagnostic_information(e);
92 cout << "Unable to parse the channel map!\n" << endl;
93 return -1;
94 }
95 return 0;
96}
97
98static vector<float> MeanVector(vector<vector<int>>& vec)
99{
100 vector<float> mean_vec(ASIC_channel, 0);
101 for (size_t i = 0; i < vec.size(); i++) {
102 for (size_t j = 0; j < vec[i].size(); j++) {
103 if (vec[i].size() != ASIC_channel)
104 continue;
105 mean_vec[j] += vec[i][j];
106 }
107 }
108 for (size_t i = 0; i < mean_vec.size(); i++) {
109 mean_vec[i] /= static_cast<int>(vec.size());
110 }
111 return mean_vec;
112}
113
114int ProcessBuffer(uint32_t* word, size_t len)
115{
116 uint32_t holdb_temp = 0;
117 uint32_t holdb = 0;
118 string str(reinterpret_cast<char*>(word), 6);
119 // printf("str %s\n", str.c_str());
120 for (uint32_t i = 0; i < len; i++) {
121 uint32_t data = word[i];
122 // printf("data %08X i = %05u\n",data, i);
123 // Check is it data or a trigger:
125 holdb = BmnProfRawTools::holdb_cntr(data); // get holdb counter
126 if (holdb_temp == holdb) {
127 // printf("data %08X\n", data);
128 // Divide ADC0 and ADC1 data
129 if (!BmnProfRawTools::adc_num(data)) { // 1st ADC
130 adc1_word.push_back(bitset<32>(data));
131 } else if (BmnProfRawTools::adc_num(data)) { // 2nd ADC
132 adc2_word.push_back(bitset<32>(data));
133 }
134 } else {
135 if (adc1_word.size() == 6 * ASIC_channel)
136 adc1.push_back(adc1_word);
137 if (adc2_word.size() == 6 * ASIC_channel)
138 adc2.push_back(adc2_word);
139 // printf("adc1_word %lu\n", adc1_word.size());
140 // printf("adc1 %lu\n", adc1.size());
141 adc1_word.clear();
142 adc2_word.clear();
143 // Divide ADC0 and ADC1 data
144 if (!BmnProfRawTools::adc_num(data)) { // 1st ADC
145 adc1_word.push_back(bitset<32>(data));
146 } else { // 2nd ADC
147 adc2_word.push_back(bitset<32>(data));
148 }
149 holdb_temp = holdb;
150 }
151 } else {
152 // spill_cntr++;
153 // if (BmnProfRawTools::trig_psd(data)) {
154 // trigger_pside.push_back(data);
155 // } else if (BmnProfRawTools::trig_nsd(data)) {
156 // trigger_nside.push_back(data);
157 // }
158 }
159 }
160 return 0;
161}
162
164{
165 vector<bitset<32>> temp;
166 // printf("ReorderBits adc1 size %lu\n", adc1.size());
167 for (size_t i = 0; i < adc1.size(); i++) {
168 gSystem->ProcessEvents();
169 // printf("events processed in the reorder func\n");
170 // printf("ReorderBits adc1[%02lu].size() %lu\n", i, adc1.size());
171 for (size_t j = 0; j < adc1[i].size(); j = j + 6) {
172 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++) {
173 adc1_word_char[iChar].push_back(BmnProfRawTools::adc_ch(adc1[i], j, channel_maps[iChar].ChannelName));
174 // printf("arr[%c][%2ld] = %d\n",channel_maps[iChar].ChannelName,j
175 // ,adc1_word_char[iChar][adc1_word_char[iChar].size()-1]);
176 }
177 temp.clear();
178 }
179 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++)
180 adc1_ch[iChar].push_back(move(adc1_word_char[iChar]));
181 }
182 return 0;
183}
184
186{
187 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++) {
188 adc1_ch_mean[iChar] = MeanVector(adc1_ch[iChar]);
189 }
190 return 0;
191}
192
193int GetCluster(vector<int>& input, vector<float>& mean, float& sig)
194{
195 // bool inside(false);
196 size_t start(0);
197 int len(0);
198
199 struct ClusterInfo
200 {
201 int start;
202 int width;
203 };
204 vector<ClusterInfo> clusters;
205 // detect clusters
206 for (size_t iCh = 0; iCh < ASIC_channel; ++iCh) {
207 float dif = input[iCh] - mean[iCh];
208 if (dif > thr) {
209 if (len == 0)
210 start = iCh;
211 ++len;
212 } else {
213 if (len) {
214 printf("new {%2zu, %2d}\n", start, len);
215 clusters.push_back(ClusterInfo{(int)start, len});
216 len = 0;
217 }
218 }
219 }
220 sort(begin(clusters), end(clusters), [](ClusterInfo& a, ClusterInfo& b) -> bool { return a.width < b.width; });
221 if (clusters.size()) {
222 sig = 0;
223 ClusterInfo& max = clusters[clusters.size() - 1];
224 printf("min %d max %d\n", clusters[0].width, max.width);
225 for (int iCh = max.start; iCh < (max.start + max.width); ++iCh)
226 sig += input[iCh] - mean[iCh];
227 sig /= max.width;
228 printf("mean sig %f\n", sig);
229 return (max.start + max.width / 2);
230 }
231 return -1;
232}
233
234inline int MapStrip(size_t& iChar, size_t& iCh)
235{
236 return channel_maps[iChar].StripMap[iCh] + channel_maps[iChar].channelLow; // @TODO
237}
238
240{
241 for (auto& h : hVec) {
242 h->Reset();
243 }
244 for (auto& h : hVec2D) {
245 h->Reset();
246 }
247 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++) {
248 vector<float> cur(ASIC_channel, 0);
249 for (size_t iEvent = 0; iEvent < adc1_ch[iChar].size(); iEvent++) {
250 // float cluster_sig(0);
251 // int cluster_center = GetCluster(adc1_ch[iChar][iEvent], adc1_ch_mean[iChar], cluster_sig);
252 // printf("cluster_center %d\n", cluster_center);
253 for (size_t iCh = 0; iCh < ASIC_channel; iCh++) {
254 float sig = adc1_ch[iChar][iEvent][iCh] - adc1_ch_mean[iChar][iCh];
255 cur[iCh] += sig;
256 hVec[NChars * 3 + iChar]->Fill(iCh, sig);
257 // hVec[NChars * 3 + iChar]->Fill(iCh, adc1_ch[iChar][iEvent][iCh]);
258 if (((sig > thr) && channel_maps[iChar].LayerType == 'p')
259 || ((sig < -thr) && channel_maps[iChar].LayerType == 'n')
260 // abs(sig)>thr
261 )
262 {
263 int strip = MapStrip(iChar, iCh);
264 hVec[NChars * 0 + iChar]->Fill(strip);
265 hVec[NChars * 1 + iChar]->Fill(sig);
266 // hVec[NChars * 1 + iChar]->Fill(abs(sig));
267 }
268 }
269 }
270 cb[iChar].push_front(move(cur));
271 // printf("adc1_ch[%02lu]size %lu\n", iChar, adc1_ch[iChar].size());
272 cb_event_count[iChar].push_front(adc1_ch[iChar].size());
273 size_t event_count = 0;
274 for (size_t& ne : cb_event_count[iChar])
275 event_count += ne;
276 // printf("events[%02lu] %lu\n", iChar, event_count);
277 for (size_t iCh = 0; iCh < ASIC_channel; iCh++) {
278 float sum = 0;
279 for (size_t iSpill = 0; iSpill < cb[iChar].size(); iSpill++) {
280 sum += cb[iChar][iSpill][iCh];
281 }
282 if (event_count) {
283 sum /= (float)event_count;
284 }
285 // printf("sum %5.2f\n", sum);
286 int strip = MapStrip(iChar, iCh);
287 // hVec[3* iChar]->SetBinContent(strip, sum);
288 hVec[NChars * 2 + iChar]->SetBinContent(strip, adc1_ch_mean[iChar][iCh]);
289 }
290 }
291 for (size_t iEvent = 0; iEvent < adc1_ch[0].size(); iEvent++) {
292 bitset<NChannels_run9> x = 0;
293 bitset<NChannels_run9> y = 0;
294 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++) {
295 for (size_t iCh = 0; iCh < ASIC_channel; iCh++) {
296 float sig = adc1_ch[iChar][iEvent][iCh] - adc1_ch_mean[iChar][iCh];
297 if (((sig > thr) && channel_maps[iChar].LayerType == 'p')
298 || ((sig < -thr) && channel_maps[iChar].LayerType == 'n')
299 // abs(sig)>thr
300 )
301 {
302 int strip = MapStrip(iChar, iCh);
303 if (channel_maps[iChar].LayerType == 'p')
304 x[strip] = true;
305 else
306 y[strip] = true;
307 }
308 }
309 }
310 for (size_t iX = 0; iX < NChannels_run9; iX++)
311 for (size_t iY = 0; iY < NChannels_run9; iY++)
312 if (x[iX] && y[iY])
313 hVec2D[0]->Fill(iX, iY);
314 // hVec[2 * NChars]->Fill(iX, iY);
315 }
316 return 0;
317}
318
320{
321 adc1.clear();
322 adc2.clear();
323 adc1_word.clear();
324 adc2_word.clear();
325 for (size_t i = 0; i < NChars; i++) {
326 adc1_word_char[i].clear();
327 adc1_ch[i].clear();
328 adc1_ch_mean[i].clear();
329 }
330}
331
332int Draw(TCanvas* c, vector<TH1*> hv, vector<string> optV)
333{
334 for (size_t i = 0; i < hv.size(); i++) {
335 c->cd(i + 1);
336 hv[i]->Draw(optV[i].data());
337 }
338 c->Update();
339 c->Modified();
340 return 0;
341}
342
343int Draw(TCanvas* c, vector<TH1*> hv)
344{
345 for (size_t i = 0; i < hv.size(); i++) {
346 c->cd(i + 1);
347 hv[i]->Draw("colz");
348 }
349 c->Update();
350 c->Modified();
351 return 0;
352}
353
355{
356 Draw(can, hVec, hVecOpt);
357 Draw(can2d, hVec2D);
358 return 0;
359}
360
361int main(int argc, char** argv)
362{
363 printf("Hi!\n");
364 string name = string(getenv("VMCWORKDIR")) + "/input/" + "Prof_map_run_9.json";
365 ReadMap(name);
366 // webserver init
367 Int_t _webPort = 10000;
368 string TargetBoardId = "board1";
369 string ServerMode = "fastcgi";
370 char* SourceAddr;
371 if (argc > 1)
372 SourceAddr = argv[1];
373 if (argc > 2) {
374 string str(argv[2]);
375 _webPort = stoi(str);
376 }
377 if (argc > 3) {
378 TargetBoardId = argv[3];
379 }
380 if (argc > 4) {
381 ServerMode = argv[4];
382 }
383 TString cgiStr = Form("%s:%d;noglobal", ServerMode.data(), _webPort);
384 THttpServer* fServer = new THttpServer(cgiStr.Data());
385 // hist declaration
386 can = new TCanvas("Profilometer", "Profilometer", PAD_WIDTH, PAD_HEIGHT);
387 fServer->Register("/", can);
388 NChars = channel_maps.size();
389 can->Divide(NChars, NGenHists, 0.002, 0.002);
390 hVec.resize(NGenHists * NChars);
391 hVecOpt.resize(NGenHists * NChars);
392 for (size_t iMap = 0; iMap < NChars; ++iMap) {
393 auto& map = channel_maps[iMap];
394 TH1* h = new TH1F(Form("h%c_%c_strips", map.ChannelName, map.LayerType),
395 Form("%c %c side", map.ChannelName, map.LayerType), ASIC_channel, 0, ASIC_channel);
396 TH1* hMean = new TH1F(Form("h%c_%c_strips_mean", map.ChannelName, map.LayerType),
397 Form("%c %c side mean", map.ChannelName, map.LayerType), ASIC_channel, 0, ASIC_channel);
398 TH1* hSig =
399 new TH1F(Form("h%c_%c_sig", map.ChannelName, map.LayerType),
400 Form("%c %c side signals", map.ChannelName, map.LayerType), 2 * ADCLimit + 1, -ADCLimit, ADCLimit);
401 TH1* hPed = new TH2F(Form("h%c_%c_ped", map.ChannelName, map.LayerType),
402 Form("%c %c side pedestals", map.ChannelName, map.LayerType), ASIC_channel, 0,
404 hVec[NChars * 0 + iMap] = h;
405 hVec[NChars * 1 + iMap] = hSig;
406 hVecOpt[NChars * 1 + iMap] = "logy";
407 hVec[NChars * 2 + iMap] = hMean;
408 hVec[NChars * 3 + iMap] = hPed;
409 hVecOpt[NChars * 3 + iMap] = "colz";
410 }
411 for (auto& h : hVec)
412 h->SetTitleSize(0.1);
413 can2d = new TCanvas("Prof_2D", "Prof_2D", PAD_WIDTH, PAD_HEIGHT);
414 can2d->Divide(1, 1, 0.001, 0.001);
415 fServer->Register("/", can2d);
416 TH1* h2d = new TH2F("h_2D", "Profile 2D", NChannels_run9, 0, NChannels_run9, NChannels_run9, 0, NChannels_run9);
417 hVec2D.push_back(h2d);
418
419 adc1_word_char.resize(NChars);
420 adc1_ch.resize(NChars);
421 adc1_ch_mean.resize(NChars);
422 cb.resize(NChars, boost::circular_buffer<vector<float>>(SpillCount));
423 cb_event_count.resize(NChars, boost::circular_buffer<size_t>(SpillCount));
424 for (size_t iChar = 0; iChar < channel_maps.size(); iChar++) {
425 // printf("init cb[%02lu]size %lu\n", iChar, cb[iChar].size());
426 // printf("init cb_event_count[%02lu]size %lu\n", iChar, cb_event_count[iChar].size());
427 }
428
429 // data socket init
430 void* _ctx = nullptr;
431 void* _rawSocket = nullptr;
432 _ctx = zmq_ctx_new();
433 _rawSocket = zmq_socket(_ctx, ZMQ_SUB);
434 if (_rawSocket == NULL) {
435 DBGERR("zmq socket")
436 return 0;
437 }
438 if (zmq_connect(_rawSocket, SourceAddr) != 0) {
439 DBGERR("zmq connect")
440 return -1;
441 }
442 // if (zmq_connect(_rawSocket, "tcp://127.0.0.1:5602") != 0) {
443 // DBGERR("zmq connect")
444 // return -1;
445 // }
446 if (zmq_setsockopt(_rawSocket, ZMQ_SUBSCRIBE, NULL, 0) == -1) {
447 DBGERR("zmq subscribe")
448 return 0;
449 }
450 printf("MonProf listens to %s\n", SourceAddr);
451
452 DrawAll();
453
454 TBufferFile t(TBuffer::kRead);
455 t.SetReadMode();
456 bool keepWorking = kTRUE;
457 bool isReceiving = kTRUE;
458 const Int_t MaxStrLen = 100;
459 bool isIdFound = kFALSE;
460 bool isHeaderFound = kFALSE;
461 bool isTrailerFound = kFALSE;
462 while (keepWorking) {
463 Int_t recv_more = 0;
464 isIdFound = kFALSE;
465
466 do {
467 gSystem->ProcessEvents();
468 // fServer->ProcessRequests();
469 zmq_msg_t msg;
470 zmq_msg_init(&msg);
471 Int_t frame_size = zmq_msg_recv(&msg, _rawSocket, ZMQ_DONTWAIT); // ZMQ_DONTWAIT
472 // printf("recv %d\n", frame_size);
473 if (frame_size == -1) {
474 // printf("Receive error # %d #%s\n", errno, zmq_strerror(errno));
475 switch (errno) {
476 case EAGAIN:
477 // printf("EAGAIN\n");
478 usleep(50000);
479 break;
480 case EINTR:
481 printf("EINTR\n");
482 isReceiving = kFALSE;
483 keepWorking = kFALSE;
484 printf("Exit!\n");
485 break;
486 case EFAULT:
487 printf("EFAULT\n");
488 zmq_close(_rawSocket);
489 isReceiving = kFALSE;
490 keepWorking = kFALSE;
491 break;
492 default:
493 break;
494 }
495 } else {
496 if (frame_size < MaxStrLen) {
497 string str(static_cast<char*>(zmq_msg_data(&msg)), zmq_msg_size(&msg));
498 printf("str %s\n", str.c_str());
499 if (isIdFound) {
500 if (str == DataTrailer) {
501 isTrailerFound = kTRUE;
502 printf("trailer\n");
503 }
504 if (str == DataHeader) {
505 isHeaderFound = kTRUE;
506 printf("header\n");
507 }
508 } else {
509 if (str == TargetBoardId) {
510 isIdFound = kTRUE;
511 // printf("id\n");
512 }
513 }
514 } else {
515 if (isHeaderFound) {
516 // printf("process buffer\n");
517 ProcessBuffer(static_cast<uint32_t*>(zmq_msg_data(&msg)), zmq_msg_size(&msg) / 4);
518 }
519 }
520 }
521 size_t opt_size = sizeof(recv_more);
522 if (zmq_getsockopt(_rawSocket, ZMQ_RCVMORE, &recv_more, &opt_size) == -1) {
523 printf("ZMQ socket options error #%s\n", zmq_strerror(errno));
524 return -1;
525 }
526 // printf("ZMQ rcvmore = %d\n", recv_more);
527 zmq_msg_close(&msg);
528 } while (recv_more && isReceiving && (!isTrailerFound));
529 // printf("FullReceive\n");
530 if (isTrailerFound) {
531 ReorderBits();
532 // printf("ReorderBits\n");
533 CalcMean();
534 // printf("CalcMean\n");
535 FillSpill();
536 // printf("FillSpill\n");
537 DrawAll();
538 printf("Draw\n\n");
540 isHeaderFound = kFALSE;
541 isTrailerFound = kFALSE;
542 // return 0;
543 }
544 }
545 for (auto h : hVec)
546 delete h;
547 delete can;
548 delete can2d;
549 delete fServer;
550
551 zmq_close(_rawSocket);
552 zmq_ctx_destroy(_ctx);
553 _ctx = NULL;
554}
@ iX
@ iY
uint32_t temp_cntr
const int NCols
int Draw(TCanvas *c, vector< TH1 * > hv, vector< string > optV)
int DrawAll()
const size_t NChannels_run9
int ProcessBuffer(uint32_t *word, size_t len)
#define PAD_WIDTH
size_t NChars
const string DataHeader
vector< string > hVecOpt
const size_t SpillCount
int ReorderBits()
const int ADCLimit
const float thr
void ClearAllVectors()
int GetCluster(vector< int > &input, vector< float > &mean, float &sig)
vector< ProfiMap > channel_maps
vector< boost::circular_buffer< size_t > > cb_event_count
vector< vector< bitset< ASIC_channel > > > adc1
TCanvas * can2d(nullptr)
vector< bitset< ASIC_channel > > adc2_word
const size_t ASIC_channel
const int NGenHists
const string DataTrailer
vector< vector< bitset< ASIC_channel > > > adc2
vector< TH1 * > hVec
vector< bitset< ASIC_channel > > adc1_word
int ReadMap(string name)
vector< boost::circular_buffer< vector< float > > > cb
TCanvas * can(nullptr)
uint32_t spill_cntr
#define PAD_HEIGHT
const int NBins
int CalcMean()
vector< vector< float > > adc1_ch_mean
int FillSpill()
vector< vector< vector< int > > > adc1_ch
int MapStrip(size_t &iChar, size_t &iCh)
vector< vector< int > > adc1_word_char
vector< TH1 * > hVec2D
__m128 v
Definition P4_F32vec4.h:1
int i
Definition P4_F32vec4.h:22
__m128 m
Definition P4_F32vec4.h:27
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
#define DBGERR(a)
Definition BmnMath.h:25
static bool data_or_trig(const uint32_t &word)
static uint32_t holdb_cntr(const uint32_t &word)
static int adc_ch(vector< bitset< 32 > > &adc_word, char channel_name)
static bool adc_num(const bitset< 32 > &word)
vector< Int_t > StripMap
int main()
int channelHi
char ChannelName
int channelLow
char LayerType