21 #include "BoostSerializer.h"
22 #include "FairMQLogger.h"
23 #include "FairMQProgOptions.h"
24 #include "FairParGenericSet.h"
25 #include "FairRunOnline.h"
26 #include "RootSerializer.h"
34 #include <boost/archive/binary_iarchive.hpp>
35 #include <boost/serialization/utility.hpp>
44 using std::runtime_error::runtime_error;
57 LOG(info) <<
"Init options for CbmDeviceMcbmEventBuilderWin.";
58 fbFillHistos = fConfig->GetValue<
bool>(
"FillHistos");
59 fbIgnoreTsOverlap = fConfig->GetValue<
bool>(
"IgnOverMs");
61 fsEvtOverMode = fConfig->GetValue<std::string>(
"EvtOverMode");
62 fsRefDet = fConfig->GetValue<std::string>(
"RefDet");
63 fvsAddDet = fConfig->GetValue<std::vector<std::string>>(
"AddDet");
64 fvsDelDet = fConfig->GetValue<std::vector<std::string>>(
"DelDet");
65 fvsSetTrigWin = fConfig->GetValue<std::vector<std::string>>(
"SetTrigWin");
66 fvsSetTrigMinNb = fConfig->GetValue<std::vector<std::string>>(
"SetTrigMinNb");
68 fsChannelNameDataInput = fConfig->GetValue<std::string>(
"TsNameIn");
69 fsChannelNameDataOutput = fConfig->GetValue<std::string>(
"EvtNameOut");
70 fsChannelNameHistosInput = fConfig->GetValue<std::string>(
"ChNameIn");
71 fsChannelNameHistosConfig = fConfig->GetValue<std::string>(
"ChNameHistCfg");
72 fsChannelNameCanvasConfig = fConfig->GetValue<std::string>(
"ChNameCanvCfg");
73 fsAllowedChannels[0] = fsChannelNameDataInput;
75 fuPublishFreqTs = fConfig->GetValue<uint32_t>(
"PubFreqTs");
76 fdMinPublishTime = fConfig->GetValue<double_t>(
"PubTimeMin");
77 fdMaxPublishTime = fConfig->GetValue<double_t>(
"PubTimeMax");
88 int noChannel = fChannels.size();
89 LOG(info) <<
"Number of defined channels: " << noChannel;
90 for (
auto const& entry : fChannels) {
91 LOG(info) <<
"Channel name: " << entry.first;
92 if (std::string::npos != entry.first.find(fsChannelNameDataInput)) {
93 if (!IsChannelNameAllowed(entry.first))
105 fpAlgo->SetFillHistos(fbFillHistos);
106 fpAlgo->SetIgnoreTsOverlap(fbIgnoreTsOverlap);
113 fpAlgo->SetEventOverlapMode(mode);
124 fpAlgo->SetReferenceDetector(refDet);
127 LOG(info) <<
"CbmDeviceMcbmEventBuilderWin::InitTask => Trying to change "
128 "reference to unsupported detector, ignored! "
133 for (std::vector<std::string>::iterator itStrAdd = fvsAddDet.begin();
134 itStrAdd != fvsAddDet.end();
145 fpAlgo->AddDetector(addDet);
148 LOG(info) <<
"CbmDeviceMcbmEventBuilderWin::InitTask => Trying to add "
149 "unsupported detector, ignored! "
156 for (std::vector<std::string>::iterator itStrRem = fvsDelDet.begin();
157 itStrRem != fvsDelDet.end();
168 fpAlgo->RemoveDetector(remDet);
171 LOG(info) <<
"CbmDeviceMcbmEventBuilderWin::InitTask => Trying to remove "
172 "unsupported detector, ignored! "
178 for (std::vector<std::string>::iterator itStrTrigWin = fvsSetTrigWin.begin();
179 itStrTrigWin != fvsSetTrigWin.end();
181 size_t charPosDel = (*itStrTrigWin).find(
',');
182 if (std::string::npos == charPosDel) {
184 <<
"CbmDeviceMcbmEventBuilderWin::InitTask => "
185 <<
"Trying to set trigger window with invalid option pattern, ignored! "
186 <<
" (Should be ECbmModuleId,dWinBeg,dWinEnd but instead found "
187 << (*itStrTrigWin) <<
" )";
192 std::string sSelDet = (*itStrTrigWin).substr(0, charPosDel);
203 <<
"CbmDeviceMcbmEventBuilderWin::InitTask => "
204 <<
"Trying to set trigger window for unsupported detector, ignored! "
211 std::string sNext = (*itStrTrigWin).substr(charPosDel);
212 charPosDel = sNext.find(
',');
213 if (std::string::npos == charPosDel) {
215 <<
"CbmDeviceMcbmEventBuilderWin::InitTask => "
216 <<
"Trying to set trigger window with invalid option pattern, ignored! "
217 <<
" (Should be ECbmModuleId,dWinBeg,dWinEnd but instead found "
218 << (*itStrTrigWin) <<
" )";
221 Double_t dWinBeg = std::stod(sNext.substr(0, charPosDel));
225 Double_t dWinEnd = std::stod(sNext.substr(charPosDel));
227 fpAlgo->SetTriggerWindow(selDet, dWinBeg, dWinEnd);
230 for (std::vector<std::string>::iterator itStrMinNb = fvsSetTrigMinNb.begin();
231 itStrMinNb != fvsSetTrigMinNb.end();
233 size_t charPosDel = (*itStrMinNb).find(
',');
234 if (std::string::npos == charPosDel) {
236 <<
"CbmDeviceMcbmEventBuilderWin::InitTask => "
237 <<
"Trying to set trigger min Nb with invalid option pattern, ignored! "
238 <<
" (Should be ECbmModuleId,uMinNb but instead found " << (*itStrMinNb)
244 std::string sSelDet = (*itStrMinNb).substr(0, charPosDel);
255 <<
"CbmDeviceMcbmEventBuilderWin::InitTask => "
256 <<
"Trying to set trigger min Nb for unsupported detector, ignored! "
263 UInt_t uMinNb = std::stoul((*itStrMinNb).substr(charPosDel));
265 fpAlgo->SetTriggerMinNumber(selDet, uMinNb);
272 fvDigiT0 =
new std::vector<CbmTofDigi>();
273 fvDigiSts =
new std::vector<CbmStsDigi>();
274 fvDigiMuch =
new std::vector<CbmMuchBeamTimeDigi>();
275 fvDigiTrd =
new std::vector<CbmTrdDigi>();
276 fvDigiTof =
new std::vector<CbmTofDigi>();
277 fvDigiRich =
new std::vector<CbmRichDigi>();
278 fvDigiPsd =
new std::vector<CbmPsdDigi>();
281 fpRun =
new FairRunOnline(0);
282 FairRootManager* ioman =
nullptr;
283 ioman = FairRootManager::Instance();
284 if (NULL == ioman) {
throw InitTaskError(
"No FairRootManager instance"); }
285 fTimeSliceMetaDataArray =
new TClonesArray(
"TimesliceMetaData", 1);
286 if (NULL == fTimeSliceMetaDataArray) {
287 throw InitTaskError(
"Failed creating the TS meta data TClonesarray ");
290 "TimesliceMetaData",
"TS Meta Data", fTimeSliceMetaDataArray, kFALSE);
292 ioman->RegisterAny(
"T0Digi", fvDigiT0, kFALSE);
293 ioman->RegisterAny(
"StsDigi", fvDigiSts, kFALSE);
294 ioman->RegisterAny(
"MuchBeamTimeDigi", fvDigiMuch, kFALSE);
295 ioman->RegisterAny(
"TrdDigi", fvDigiTrd, kFALSE);
296 ioman->RegisterAny(
"TofDigi", fvDigiTof, kFALSE);
297 ioman->RegisterAny(
"RichDigi", fvDigiRich, kFALSE);
298 ioman->RegisterAny(
"PsdDigi", fvDigiPsd, kFALSE);
301 std::vector<CbmMvdDigi>* pMvdDigi =
new std::vector<CbmMvdDigi>();
302 ioman->RegisterAny(
"MvdDigi", pMvdDigi, kFALSE);
303 std::vector<CbmMatch>* pFakeMatch =
new std::vector<CbmMatch>();
304 ioman->RegisterAny(
"MvdDigiMatch", pFakeMatch, kFALSE);
305 ioman->RegisterAny(
"StsDigiMatch", pFakeMatch, kFALSE);
306 ioman->RegisterAny(
"MuchBeamTimeDigiMatch", pFakeMatch, kFALSE);
307 ioman->RegisterAny(
"TrdDigiMatch", pFakeMatch, kFALSE);
308 ioman->RegisterAny(
"TofDigiMatch", pFakeMatch, kFALSE);
309 ioman->RegisterAny(
"RichDigiMatch", pFakeMatch, kFALSE);
310 ioman->RegisterAny(
"PsdDigiMatch", pFakeMatch, kFALSE);
314 fEvents =
new TClonesArray(
"CbmEvent", 500);
317 if (kFALSE == fpAlgo->InitAlgo()) {
318 throw InitTaskError(
"Failed to initilize the algorithm class.");
322 if (kTRUE == fbFillHistos) {
324 std::vector<std::pair<TNamed*, std::string>> vHistos =
325 fpAlgo->GetHistoVector();
327 std::vector<std::pair<TCanvas*, std::string>> vCanvases =
328 fpAlgo->GetCanvasVector();
334 for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
338 fArrayHisto.Add(vHistos[uHisto].
first);
339 std::pair<std::string, std::string> psHistoConfig(
340 vHistos[uHisto].
first->GetName(), vHistos[uHisto].second);
341 fvpsHistosFolder.push_back(psHistoConfig);
344 FairMQMessagePtr messageHist(NewMessage());
345 Serialize<BoostSerializer<std::pair<std::string, std::string>>>(
346 *messageHist, psHistoConfig);
349 if (Send(messageHist, fsChannelNameHistosConfig) < 0) {
353 LOG(info) <<
"Config of hist " << psHistoConfig.first.data()
354 <<
" in folder " << psHistoConfig.second.data();
360 for (UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv) {
363 std::string sCanvName = (vCanvases[uCanv].first)->GetName();
364 std::string sCanvConf =
367 std::pair<std::string, std::string> psCanvConfig(sCanvName, sCanvConf);
369 fvpsCanvasConfig.push_back(psCanvConfig);
372 FairMQMessagePtr messageCan(NewMessage());
373 Serialize<BoostSerializer<std::pair<std::string, std::string>>>(
374 *messageCan, psCanvConfig);
377 if (Send(messageCan, fsChannelNameCanvasConfig) < 0) {
381 LOG(info) <<
"Config string of Canvas " << psCanvConfig.first.data()
382 <<
" is " << psCanvConfig.second.data();
387 LOG(error) << e.what();
393 std::string channelName) {
394 for (
auto const& entry : fsAllowedChannels) {
395 std::size_t pos1 = channelName.find(entry);
396 if (pos1 != std::string::npos) {
397 const vector<std::string>::const_iterator
pos =
398 std::find(fsAllowedChannels.begin(), fsAllowedChannels.end(), entry);
399 const vector<std::string>::size_type idx =
400 pos - fsAllowedChannels.begin();
401 LOG(info) <<
"Found " << entry <<
" in " << channelName;
402 LOG(info) <<
"Channel name " << channelName
403 <<
" found in list of allowed channel names at position "
408 LOG(info) <<
"Channel name " << channelName
409 <<
" not found in list of allowed channel names.";
410 LOG(error) <<
"Stop device.";
479 LOG(debug) <<
"Received message number " << fulNumMessages <<
" with "
480 << parts.Size() <<
" parts"
481 <<
", size0: " << parts.At(0)->GetSize();
483 if (0 == fulNumMessages % 10000)
484 LOG(info) <<
"Received " << fulNumMessages <<
" messages";
487 uint32_t uPartIdx = 0;
498 Deserialize<RootSerializer>(*parts.At(uPartIdx), fTsMetaData);
501 (*fTimeSliceMetaDataArray)
502 [fTimeSliceMetaDataArray->GetEntriesFast()
508 std::string msgStrT0(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
509 (parts.At(uPartIdx))->GetSize());
510 std::istringstream issT0(msgStrT0);
511 boost::archive::binary_iarchive inputArchiveT0(issT0);
512 inputArchiveT0 >> *fvDigiT0;
516 std::string msgStrSts(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
517 (parts.At(uPartIdx))->GetSize());
518 std::istringstream issSts(msgStrSts);
519 boost::archive::binary_iarchive inputArchiveSts(issSts);
520 inputArchiveSts >> *fvDigiSts;
524 std::string msgStrMuch(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
525 (parts.At(uPartIdx))->GetSize());
526 std::istringstream issMuch(msgStrMuch);
527 boost::archive::binary_iarchive inputArchiveMuch(issMuch);
528 inputArchiveMuch >> *fvDigiMuch;
532 std::string msgStrTrd(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
533 (parts.At(uPartIdx))->GetSize());
534 std::istringstream issTrd(msgStrTrd);
535 boost::archive::binary_iarchive inputArchiveTrd(issTrd);
536 inputArchiveTrd >> *fvDigiTrd;
540 std::string msgStrTof(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
541 (parts.At(uPartIdx))->GetSize());
542 std::istringstream issTof(msgStrTof);
543 boost::archive::binary_iarchive inputArchiveTof(issTof);
544 inputArchiveTof >> *fvDigiTof;
548 std::string msgStrRich(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
549 (parts.At(uPartIdx))->GetSize());
550 std::istringstream issRich(msgStrRich);
551 boost::archive::binary_iarchive inputArchiveRich(issRich);
552 inputArchiveRich >> *fvDigiRich;
556 std::string msgStrPsd(
static_cast<char*
>(parts.At(uPartIdx)->GetData()),
557 (parts.At(uPartIdx))->GetSize());
558 std::istringstream issPsd(msgStrPsd);
559 boost::archive::binary_iarchive inputArchivePsd(issPsd);
560 inputArchivePsd >> *fvDigiPsd;
567 if (!SendEvents(parts))
return false;
570 fTimeSliceMetaDataArray->Clear();
583 fpAlgo->ClearEventVector();
588 if (kTRUE == fbFillHistos) {
592 std::chrono::system_clock::time_point currentTime =
593 std::chrono::system_clock::now();
594 std::chrono::duration<double_t> elapsedSeconds =
595 currentTime - fLastPublishTime;
596 if ((fdMaxPublishTime < elapsedSeconds.count())
597 || (0 == fulNumMessages % fuPublishFreqTs
598 && fdMinPublishTime < elapsedSeconds.count())) {
600 fLastPublishTime = std::chrono::system_clock::now();
613 std::vector<CbmEvent*> vEvents = fpAlgo->GetEventVector();
617 LOG(debug) <<
"Vector: " <<
event->ToString();
618 new ((*fEvents)[fEvents->GetEntriesFast()])
CbmEvent(std::move(*event));
620 LOG(debug) <<
"TClonesArray: "
622 fEvents->At(fEvents->GetEntriesFast() - 1))
627 FairMQMessagePtr message(NewMessage());
628 Serialize<RootSerializer>(*message, fEvents);
632 FairMQParts partsOut(std::move(partsIn));
633 partsOut.AddPart(std::move(message));
652 if (Send(partsOut, fsChannelNameDataOutput) < 0) {
653 LOG(error) <<
"Problem sending data to " << fsChannelNameDataOutput;
662 FairMQMessagePtr message(NewMessage());
663 Serialize<RootSerializer>(*message, &fArrayHisto);
666 if (Send(message, fsChannelNameHistosInput) < 0) {
667 LOG(error) <<
"Problem sending data";
672 fpAlgo->ResetHistograms(kFALSE);
679 fTimeSliceMetaDataArray->Clear();
696 delete fTimeSliceMetaDataArray;