27#include "StorableTimeslice.hpp"
29#include "FairMQLogger.h"
30#include "FairMQProgOptions.h"
31#include "FairParGenericSet.h"
32#include "FairRuntimeDb.h"
37#include <boost/archive/binary_iarchive.hpp>
38#include <boost/archive/binary_oarchive.hpp>
41#include <boost/serialization/vector.hpp>
48 using std::runtime_error::runtime_error;
80 , fuNrOfChannelsPerGet4(0)
81 , fuNrOfChannelsPerFee(0)
83 , fuNrOfGet4PerGdpb(0)
84 , fuNrOfChannelsPerGdpb(0)
94 , fulCurrentEpochTime(0.)
103 , fUnpackPar(nullptr)
105 , fdFirstDigiTimeDif(0.)
107 , fhRawTDigEvBmon(nullptr)
108 , fhRawTDigRef0(nullptr)
109 , fhRawTDigRef(nullptr)
110 , fhRawTRefDig0(nullptr)
111 , fhRawTRefDig1(nullptr)
112 , fhRawDigiLastDigi(nullptr)
117 , fhDetChanCoinc(nullptr)
128 , fUnpackerAlgo(nullptr)
147 int noChannel = fChannels.size();
148 LOG(info) <<
"Number of defined channels: " << noChannel;
149 for (
auto const& entry : fChannels) {
150 LOG(info) <<
"Channel name: " << entry.first;
152 if (entry.first ==
"syscmd") {
167 fTofConfig = std::make_shared<CbmTofUnpackConfig>(
"", runid);
171 LOG(info) <<
"Configure Tof Unpacker ";
179 LOG(fatal) <<
"Tof Unpacker not configured ";
181 const Int_t iHeaderSize = 5;
183 for (
int i = 0; i < iHeaderSize; i++)
185 LOG(info) <<
"Read config";
187 fiReqMode = fConfig->GetValue<uint64_t>(
"ReqMode");
188 fiReqTint = fConfig->GetValue<uint64_t>(
"ReqTint");
189 fiReqBeam = fConfig->GetValue<uint64_t>(
"ReqBeam");
190 fiPulserMode = fConfig->GetValue<int64_t>(
"PulserMode");
191 fiPulMulMin = fConfig->GetValue<uint64_t>(
"PulMulMin");
192 fiPulTotMin = fConfig->GetValue<uint64_t>(
"PulTotMin");
193 fiPulTotMax = fConfig->GetValue<uint64_t>(
"PulTotMax");
194 fdToffTof = fConfig->GetValue<double_t>(
"ToffTof");
195 Int_t iRefModType = fConfig->GetValue<int64_t>(
"RefModType");
196 Int_t iRefModId = fConfig->GetValue<int64_t>(
"RefModId");
197 Int_t iRefCtrType = fConfig->GetValue<int64_t>(
"RefCtrType");
198 Int_t iRefCtrId = fConfig->GetValue<int64_t>(
"RefCtrId");
199 if (iRefModType > -1)
201 LOG(info) <<
" Using reference counter address 0x" << std::hex <<
fiAddrRef <<
" and offset shift " << std::dec
208 const Int_t iMaxReq = 50;
210 while (iNReq < iMaxReq) {
211 iReqDet = fConfig->GetValue<uint64_t>(Form(
"ReqDet%d", iNReq));
212 if (iReqDet == 0)
break;
216 LOG(info) <<
"Setup request";
219 for (UInt_t iGbtx = 0; iGbtx <
fviNrOfRpc.size(); iGbtx++) {
225 for (Int_t iRpc = 0; iRpc <
fviNrOfRpc[iGbtx]; iRpc++) {
233 for (Int_t iRpc = 0; iRpc < 2; iRpc++) {
239 for (Int_t iRpc = 0; iRpc < 2; iRpc++) {
246 for (Int_t iRpc = 0; iRpc < 1; iRpc++) {
252 for (Int_t iRpc = 0; iRpc < 8; iRpc++) {
268 LOG(info) << Form(
"ReqBeam 0x%08x", (uint)
fiReqBeam);
271 LOG(error) << e.what();
279 LOG(info) <<
"Inspect " << entry;
280 std::size_t pos1 = channelName.find(entry);
281 if (pos1 != std::string::npos) {
282 const vector<std::string>::const_iterator
pos =
285 LOG(info) <<
"Found " << entry <<
" in " << channelName;
286 LOG(info) <<
"Channel name " << channelName <<
" found in list of allowed channel names at position " << idx;
290 LOG(info) <<
"Channel name " << channelName <<
" not found in list of allowed channel names.";
291 LOG(error) <<
"Stop device.";
297 LOG(info) <<
"Init parameter containers for CbmDeviceUnpackTofCri";
302 std::string message {
"CbmMcbm2018TofPar,111"};
303 LOG(info) <<
"Requesting parameter container CbmMcbm2018TofPar, sending message: " << message;
305 FairMQMessagePtr req(NewSimpleMessage(
"CbmMcbm2018TofPar,111"));
306 FairMQMessagePtr rep(NewMessage());
308 if (Send(req,
"parameters") > 0) {
309 if (Receive(rep,
"parameters") >= 0) {
310 if (rep->GetSize() != 0) {
313 LOG(info) <<
"Received unpack parameter from parmq server: " <<
fUnpackPar;
317 LOG(error) <<
"Received empty reply. Parameter not available";
325 Bool_t initOK = kTRUE;
356 auto reqparvec =
fTofConfig->GetParContainerRequest();
357 if (1 == reqparvec->size() && (reqparvec->at(0).second->IsA() == CbmMcbm2018TofPar::Class())) {
361 LOG(fatal) <<
"CbmDeviceUnpackTofCri::SetParContainers => wronf number of parameters needed for the algo or wrong "
399 LOG(info) <<
"ReInit parameter containers for CbmDeviceUnpackCriTofPar.";
407 for (UInt_t iGbtx = 0; iGbtx < uNrOfGbtx; ++iGbtx) {
418 LOG(info) <<
"create Histos for " <<
fuNrOfGdpbs <<
" gDPBs ";
421 new TH1F(Form(
"Raw_TDig-EvBmon"), Form(
"Raw digi time difference to 1st digi ; time [ns]; cts"), 500, 0, 100.);
425 new TH1F(Form(
"Raw_TDig-Ref0"), Form(
"Raw digi time difference to Ref ; time [ns]; cts"), 6000, -10000, 50000);
429 new TH1F(Form(
"Raw_TDig-Ref"), Form(
"Raw digi time difference to Ref ; time [ns]; cts"), 6000, -1000, 5000);
432 fhRawTRefDig0 =
new TH1F(Form(
"Raw_TRef-Dig0"), Form(
"Raw Ref time difference to last digi ; time [ns]; cts"), 9999,
437 new TH1F(Form(
"Raw_TRef-Dig1"), Form(
"Raw Ref time difference to last digi ; time [ns]; cts"), 9999, -5000, 5000);
441 Form(
"Raw Digi time difference to last digi ; time [ns]; cts"), 9999, -5000, 5000);
448 for (UInt_t uGdpb = 0; uGdpb <
fuNrOfGdpbs; uGdpb++) {
449 fhRawTotCh[uGdpb] =
new TH2F(Form(
"Raw_Tot_gDPB_%02u", uGdpb), Form(
"Raw TOT gDPB %02u; channel; TOT [bin]", uGdpb),
454 new TH1I(Form(
"ChCount_gDPB_%02u", uGdpb), Form(
"Channel counts gDPB %02u; channel; Hits", uGdpb),
469 new TH2F(Form(
"fhChanCoinc_%02u", uGdpb), Form(
"Channels Coincidence %02u; Left; Right", uGdpb),
472 fhDetChanCoinc =
new TH2F(
"fhDetChanCoinc",
"Det Channels Coincidence; Left; Right", 32, 0., 32, 32, 0., 32);
482 LOG(debug) <<
"Received message number " <<
fNumMessages <<
" with size " << msg->GetSize();
484 std::string msgStr(
static_cast<char*
>(msg->GetData()), msg->GetSize());
485 std::istringstream iss(msgStr);
486 boost::archive::binary_iarchive inputArchive(iss);
488 fles::StorableTimeslice component {0};
489 inputArchive >> component;
508 LOG(debug) <<
"Received message number " <<
fNumMessages <<
" with " << parts.Size() <<
" parts";
510 fles::StorableTimeslice ts {0};
511 uint64_t ulTsStartTime = 0;
515 std::string msgStr(
static_cast<char*
>(parts.At(0)->GetData()), (parts.At(0))->GetSize());
516 std::istringstream iss(msgStr);
517 boost::archive::binary_iarchive inputArchive(iss);
520 for (
size_t c {0}; c < ts.num_components(); c++) {
521 auto systemID =
static_cast<int>(ts.descriptor(c, 0).sys_id);
523 LOG(info) <<
"Found systemID: " << std::hex << systemID << std::dec;
526 if (systemID == 0x60 || systemID == 0x90) {
531 ulTsStartTime = ts.start_time();
534 fles::StorableTimeslice component {0};
536 uint ncomp = parts.Size();
537 for (uint i = 0; i < ncomp; i++) {
538 std::string msgStr(
static_cast<char*
>(parts.At(i)->GetData()), (parts.At(i))->GetSize());
539 std::istringstream iss(msgStr);
540 boost::archive::binary_iarchive inputArchive(iss);
542 inputArchive >> component;
546 LOG(debug) <<
"HandleParts message " <<
fNumMessages <<
", component " << i <<
",size "
547 << (parts.At(i))->GetSize();
550 ulTsStartTime = component.start_time();
565 const char* cmd = (
char*) (msg->GetData());
566 const char cmda[4] = {*cmd};
567 LOG(info) <<
"Handle message " << cmd <<
", " << cmd[0];
572 if (strcmp(cmda,
"STOP")) {
589 LOG(debug) <<
"Timeslice " << ts.index() <<
" contains " << ts.num_microslices(component)
590 <<
" microslices of component " << component <<
", NCoreMs: " << ts.num_core_microslices();
593 std::vector<CbmTofDigi> vDigi =
fUnpackerAlgo->Unpack(&ts, component);
595 LOG(debug) <<
"TS " << ts.index() <<
", startTime " << ts.start_time() <<
": insert " << vDigi.size()
596 <<
" digis of component " << component <<
" into DAQ buffer with size " <<
fBuffer->
GetSize();
600 for (
auto digi : vDigi) {
602 if (NULL != fDigi) fDigiLast = fDigi;
610 LOG(debug) <<
"BufferInsert TSRCS "
611 << Form(
"%d%d%d%02d%d", (
int) fDigi->
GetType(), (
int) fDigi->
GetSm(), (
int) fDigi->
GetRpc(),
616 if (NULL != fDigiLast) {
619 LOG(warn) <<
"Successive digis with same time: "
620 << Form(
"%12.3f, TSRCS %d%d%d%02d%d - %d%d%d%02d%d", fDigi->
GetTime(), (Int_t) fDigi->
GetType(),
647 double TSLENGTH = 1.E6;
650 LOG(debug) <<
"BuildTint: Buffer size " <<
fBuffer->
GetSize() <<
", DeltaT "
661 LOG(debug) <<
"BuildTint: refill DAQ buffer ";
669 digi = boost::any_cast<CbmTofDigi*>(data.first);
672 Double_t dTEnd = digi->
GetTime() + fdMaxDeltaT;
673 Double_t dTEndMax = digi->
GetTime() + 2 * fdMaxDeltaT;
674 LOG(debug) << Form(
"Next event at %f until %f, max %f ", digi->
GetTime(), dTEnd, dTEndMax);
676 if (dTEnd > fTimeBufferLast) {
677 LOG(warn) << Form(
"Remaining buffer < %f with %d entries is not "
678 "sufficient for digi ending at %f -> skipped ",
683 LOG(debug) <<
"BuildTint0 with digi " << Form(
"0x%08x at %012.2f", digi->
GetAddress(), digi->
GetTime());
687 for (Int_t j = 0; j < 2; j++)
691 for (Int_t j = 0; j < 2; j++)
693 Bool_t bBeam = kFALSE;
695 std::vector<CbmTofDigi*> vdigi;
698 const Int_t AddrMask = 0x001FFFFF;
699 Bool_t bOut = kFALSE;
703 digi = boost::any_cast<CbmTofDigi*>(data.first);
704 LOG(debug) <<
"GetNextData " << digi <<
", " << data.second <<
", " << Form(
"%f %f", digi->
GetTime(), dTEnd)
705 <<
", Mul " << nDigi;
708 if (nDigi == vdigi.size()) vdigi.resize(nDigi + 100);
709 vdigi[nDigi++] = digi;
712 if (iAddr == 0x00003006 || iAddr == 0x0000b006) {
714 LOG(debug) << Form(
"Event %10lu: BucMul %2d, addr 0x%08x, side %d, strip %2d, rpc %d",
fEventHeader[0], iBucMul,
724 LOG(debug) <<
"Found ReqBeam at index " << nDigi - 1 <<
", req " << i;
731 Int_t str = ((
CbmTofDigi*) digi)->GetChannel();
740 if (str == 0) bPul[i][1] = kFALSE;
742 if (str == 0) bPul[i][0] = kTRUE;
743 if (str == 40) bPul[i][1] = kTRUE;
754 if (str == 31) bPul[i][1] = kFALSE;
765 if (str == 31) bPul[i][0] = kFALSE;
770 if (str == 0) bPul[i][0] = kFALSE;
781 if (dTEnd - digi->
GetTime() < fdMaxDeltaT * 0.5) {
782 if (digi->
GetTime() + fdMaxDeltaT * 0.5 < dTEndMax) dTEnd = digi->
GetTime() + fdMaxDeltaT * 0.5;
790 LOG(debug) << Form(
" %d digis associated to dTEnd = %15.9f", nDigi, dTEnd);
793 for (UInt_t iDigi = 0; iDigi < nDigi; iDigi++)
794 LOG(debug) <<
"B " << iDigi <<
" TSRC " << vdigi[iDigi]->GetType() << vdigi[iDigi]->GetSm()
795 << vdigi[iDigi]->GetRpc() << vdigi[iDigi]->GetChannel() <<
" S " << vdigi[iDigi]->GetSide() <<
" : "
796 << Form(
"T %15.3f, Tot %5.1f", vdigi[iDigi]->GetTime(), vdigi[iDigi]->GetTot());
803 if (bDet[i][0] == kFALSE || bDet[i][1] == kFALSE)
break;
811 if (bDet[i][0] == kTRUE && bDet[i][1] == kTRUE) { iDetMul++; }
812 if (iDetMul >=
fiReqMode) { bOut = kTRUE; }
817 LOG(debug) <<
"Found Req coinc in event with " << nDigi <<
" digis in " << iDetMul
818 <<
" detectors, dTEnd = " << dTEnd;
824 if (bPul[i][0] == kTRUE && bPul[i][1] == kTRUE) iPulMul++;
828 LOG(debug) <<
"@Event " <<
fEventHeader[0] <<
": iPulMul = " << iPulMul;
832 LOG(debug) <<
"Process Ev " <<
fEventHeader[0] <<
" with iDetMul = " << iDetMul <<
", iPulMul = " << iPulMul;
837 if (bBeam) { LOG(debug) <<
"Beam counter is present "; }
839 LOG(debug) <<
"Beam counter is not present";
857 const Int_t NDigiMax = 10000;
858 if (nDigi > NDigiMax) {
859 LOG(warn) <<
"Oversized event " <<
fEventHeader[0] <<
", size " << nDigi <<
" truncated! ";
860 for (UInt_t iDigi = NDigiMax; iDigi < nDigi; iDigi++) {
861 LOG(debug) <<
"Discard digi " << iDigi <<
": " << vdigi[iDigi]->ToString();
867 LOG(debug) <<
"Send " << nDigi <<
" digis to HitBuilder";
870 for (UInt_t iDigi = 0; iDigi < nDigi; iDigi++)
874 LOG(debug) <<
" BuildTint cleanup of " << nDigi <<
" digis";
875 for (UInt_t iDigi = 0; iDigi < nDigi; iDigi++) {
879 LOG(debug) <<
" Digis deleted ";
888 LOG(debug) <<
"Send Digis for event " <<
fNumTint <<
" with size " << vdigi.size() << Form(
" at %p ", &vdigi);
894 std::stringstream ossE;
895 boost::archive::binary_oarchive oaE(ossE);
897 std::string* strMsgE =
new std::string(ossE.str());
899 std::stringstream oss;
900 boost::archive::binary_oarchive oa(oss);
902 std::string* strMsg =
new std::string(oss.str());
905 parts.AddPart(NewMessage(
906 const_cast<char*
>(strMsgE->c_str()),
908 [](
void*,
void*
object) { delete static_cast<std::string*>(object); },
911 parts.AddPart(NewMessage(
912 const_cast<char*
>(strMsg->c_str()),
914 [](
void*,
void*
object) { delete static_cast<std::string*>(object); },
1004 LOG(debug) <<
"Send data to channel " << idx <<
" " <<
fChannelsToSend[idx][0];
1013 LOG(debug) <<
"Sent event # " <<
fNumTint <<
" from " << vdigi.size() <<
" Digis at " << vdigi.data() <<
", "
1028 for (UInt_t i = 0; i < iNReq; i++)
1032 LOG(info) << Form(
"Request Digi Address 0x%08x at index %d", iAddr, iNReq);
static uint fiSelectComponents
std::shared_ptr< CbmTofUnpackConfig > fTofConfig
Baseclass for the TrdR unpacker algorithms.
Configuration class for an unpacker algorithm.
std::vector< Int_t > fviRpcType
Bool_t DoUnpack(const fles::Timeslice &ts, size_t component)
void AddMsComponentToList(size_t component, UShort_t usDetectorId)
Temp until we change from CbmMcbmUnpack to something else.
bool HandleData(FairMQMessagePtr &, int)
std::vector< Bool_t > fvbFirstEpochSeen
std::vector< uint64_t > fEventHeader
std::vector< Int_t > fviRpcSide
std::vector< TH1 * > fhChCount
std::vector< Int_t > fviModuleId
virtual void BuildTint(uint64_t ulTsStartTime, int iMode=0)
std::vector< Bool_t > fvbChanThere
std::vector< ULong64_t > fvulCurrentEpoch
UInt_t fuNrOfChannelsPerGdpb
std::shared_ptr< CbmTofUnpackAlgo > fUnpackerAlgo
Processing algo.
std::vector< Int_t > fviNrOfRpc
void SetIgnoreOverlapMs(Bool_t bFlagIn=kTRUE)
Algo settings setters.
Int_t GetArrayIndex(Int_t gdpbId, Int_t get4Id)
std::vector< std::string > fAllowedChannels
virtual ~CbmDeviceUnpackTofCri()
uint64_t fiSelectComponents
Bool_t ReInitContainers()
CbmMcbm2018TofPar * fUnpackPar
std::vector< Int_t > fviRpcChUId
bool HandleMessage(FairMQMessagePtr &, int)
std::vector< std::vector< std::string > > fChannelsToSend
bool HandleParts(FairMQParts &, int)
std::vector< Int_t > fiReqDigiAddr
bool IsChannelNameAllowed(std::string channelName)
std::vector< TH2 * > fhRawTotCh
virtual bool SendDigis(std::vector< CbmTofDigi * >, int)
std::vector< TH2 * > fhChanCoinc
Int_t GetModuleId(Int_t i)
Int_t GetRpcSide(Int_t i)
Int_t GetNrOfGdpbs()
FIXME: replace with method returning the correspondign constants! see Star2019 parameter.
Int_t GetNrOfRpc(Int_t i)
Int_t GetRpcType(Int_t i)
Singleton buffer class for CBM raw data.
Double_t GetTimeLast() const
Data GetNextData(Double_t time)
std::pair< boost::any, ECbmModuleId > Data
Double_t GetTimeFirst() const
void InsertData(Digi *digi)
static uint32_t GetUniqueAddress(uint32_t Sm, uint32_t Rpc, uint32_t Channel, uint32_t Side=0, uint32_t SmType=0, uint32_t RpcType=0)
Data class for expanded digital TOF information.
double GetSide() const
Channel Side.
double GetChannel() const
Channel .
int32_t GetAddress() const
Inherited from CbmDigi.
double GetTime() const
Inherited from CbmDigi.
double GetType() const
Sm Type .
double GetRpc() const
Detector aka Module aka RPC .
double GetTot() const
Alias for GetCharge.
void SetTime(double time)
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
void LogState(FairMQDevice *device)