CbmRoot
Loading...
Searching...
No Matches
CbmDevTrigger.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 Facility for Antiproton and Ion Research in Europe, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau[committer], Dominik Smith */
4
5#include "CbmDevTrigger.h"
6
8#include "CbmDigiTimeslice.h"
9#include "CbmMQDefs.h"
10
12#include "BoostSerializer.h"
13#include "FairMQLogger.h"
14#include "FairMQProgOptions.h"
15#include "RootSerializer.h"
16
18#include "TimesliceMetaData.h"
19
20#include <boost/archive/binary_iarchive.hpp>
21#include <boost/serialization/utility.hpp>
22
24#include <array>
25#include <iomanip>
26#include <stdexcept>
27#include <string>
28struct InitTaskError : std::runtime_error {
29 using std::runtime_error::runtime_error;
30};
31
32using namespace std;
33
35
37try {
39 LOG(info) << "Init options for CbmDevTrigger.";
40
41 // Trigger algorithm params
42 const std::string sTriggerDet = fConfig->GetValue<std::string>("TriggerDet");
43 fTriggerWindow = fConfig->GetValue<double>("TriggerWin");
44 fMinNumDigis = fConfig->GetValue<int32_t>("TriggerMinDigis");
45 fDeadTime = fConfig->GetValue<double>("TriggerDeadTime");
46
47 fChannelNameDataInput = fConfig->GetValue<std::string>("TsNameIn");
48 fChannelNameDataOutput = fConfig->GetValue<std::string>("TriggerNameOut");
49
51
53 fTriggerDet = GetDetectorId(sTriggerDet);
54
56 throw InitTaskError("CbmDevTrigger: Trigger detector not set.");
57 }
58}
59catch (InitTaskError& e) {
60 LOG(error) << e.what();
61 // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
63}
64
66{
68 /* clang-format off */
69 ECbmModuleId detId = ("Bmon" == detName ? ECbmModuleId::kBmon
70 : ("Sts" == detName ? ECbmModuleId::kSts
71 : ("Much" == detName ? ECbmModuleId::kMuch
72 : ("Trd" == detName ? ECbmModuleId::kTrd
73 : ("Tof" == detName ? ECbmModuleId::kTof
74 : ("Rich" == detName ? ECbmModuleId::kRich
75 : ("Psd" == detName ? ECbmModuleId::kPsd
76 : ("Fsd" == detName ? ECbmModuleId::kFsd
78 return detId;
80 /* clang-format on */
81}
82
83// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
84bool CbmDevTrigger::HandleData(FairMQParts& parts, int /*index*/)
85{
87 LOG(info) << "Received message number " << fNumMessages << " with " << parts.Size() << " parts"
88 << ", size0: " << parts.At(0)->GetSize();
89
90 if (0 == fNumMessages % 10000) LOG(info) << "Received " << fNumMessages << " messages";
91
94
95 std::string msgStrTS(static_cast<char*>(parts.At(0)->GetData()), (parts.At(0))->GetSize());
96 std::istringstream issTS(msgStrTS);
97 boost::archive::binary_iarchive inputArchiveTS(issTS);
98 inputArchiveTS >> ts;
99
100 LOG(debug) << "Bmon Vector size: " << ts.fData.fBmon.fDigis.size();
101 LOG(debug) << "STS Vector size: " << ts.fData.fSts.fDigis.size();
102 LOG(debug) << "MUCH Vector size: " << ts.fData.fMuch.fDigis.size();
103 LOG(debug) << "TRD Vector size: " << ts.fData.fTrd.fDigis.size();
104 LOG(debug) << "TOF Vector size: " << ts.fData.fTof.fDigis.size();
105 LOG(debug) << "RICH Vector size: " << ts.fData.fRich.fDigis.size();
106 LOG(debug) << "PSD Vector size: " << ts.fData.fPsd.fDigis.size();
107 LOG(debug) << "FSD Vector size: " << ts.fData.fFsd.fDigis.size();
108
109 const std::vector<double> triggers = GetTriggerTimes(ts);
110 LOG(debug) << "triggers: " << triggers.size();
111
113 if (!SendTriggers(triggers, parts)) {
114 return false;
115 }
116
117 return true;
118}
119
121{
122 std::vector<double> vDigiTimes;
123 switch (fTriggerDet) {
124 case ECbmModuleId::kMuch: {
126 break;
127 }
128 case ECbmModuleId::kSts: {
129 vDigiTimes = GetDigiTimes<CbmStsDigi>(ts.fData.fSts.fDigis);
130 break;
131 }
132 case ECbmModuleId::kTof: {
133 vDigiTimes = GetDigiTimes<CbmTofDigi>(ts.fData.fTof.fDigis);
134 break;
135 }
136 case ECbmModuleId::kTrd: {
137 vDigiTimes = GetDigiTimes<CbmTrdDigi>(ts.fData.fTrd.fDigis);
138 break;
139 }
140 case ECbmModuleId::kRich: {
142 break;
143 }
144 case ECbmModuleId::kPsd: {
145 vDigiTimes = GetDigiTimes<CbmPsdDigi>(ts.fData.fPsd.fDigis);
146 break;
147 }
148 case ECbmModuleId::kFsd: {
149 vDigiTimes = GetDigiTimes(ts.fData.fFsd.fDigis);
150 break;
151 }
152 case ECbmModuleId::kBmon: {
154 break;
155 }
156 default: LOG(fatal) << "CbmDevTrigger::GetTriggerTimes(): Reading digis from unknown detector type!";
157 }
158 LOG(debug) << "CbmDevTrigger::GetTriggerTimes(): Building triggers from " << vDigiTimes.size() << " digis.";
159 return (*fTriggerAlgo)(vDigiTimes).first;
160}
161
162bool CbmDevTrigger::SendTriggers(const std::vector<double>& vTriggers, FairMQParts& partsIn)
163{
164 LOG(debug) << "Vector size: " << vTriggers.size();
165
166 FairMQParts partsOut;
167 partsOut.AddPart(std::move(partsIn.At(0))); // DigiTimeslice
168 partsOut.AddPart(std::move(partsIn.At(1))); // TsMetaData
169
170 // Prepare trigger vector.
171 std::stringstream ossTrig;
172 boost::archive::binary_oarchive oaTrig(ossTrig);
173 oaTrig << vTriggers;
174 std::string* strMsgTrig = new std::string(ossTrig.str());
175
176 partsOut.AddPart(NewMessage(
177 const_cast<char*>(strMsgTrig->c_str()), // data
178 strMsgTrig->length(), // size
179 [](void*, void* object) { delete static_cast<std::string*>(object); },
180 strMsgTrig)); // object that manages the data
181
182 if (Send(partsOut, fChannelNameDataOutput) < 0) {
183 LOG(error) << "Problem sending data to " << fChannelNameDataOutput;
184 return false;
185 }
186 return true;
187}
ECbmModuleId
Definition CbmDefs.h:39
@ kTrd
Transition Radiation Detector.
@ kTof
Time-of-flight Detector.
@ kNotExist
If not found.
@ kPsd
Projectile spectator detector.
@ kSts
Silicon Tracking System.
@ kMuch
Muon detection system.
@ kFsd
Forward spectator detector.
@ kRich
Ring-Imaging Cherenkov Detector.
bool first
std::vector< CbmBmonDigi > fDigis
Data vector.
std::string fChannelNameDataOutput
std::vector< double > GetTriggerTimes(const CbmDigiTimeslice &ts)
ECbmModuleId GetDetectorId(std::string detName)
std::unique_ptr< cbm::algo::evbuild::TimeClusterTrigger > fTriggerAlgo
Processing algos.
virtual void InitTask()
uint64_t fNumMessages
Statistics.
bool HandleData(FairMQParts &, int)
bool SendTriggers(const std::vector< double > &vTriggers, FairMQParts &partsIn)
ECbmModuleId fTriggerDet
int32_t fMinNumDigis
std::string fChannelNameDataInput
message queues
double fTriggerWindow
std::vector< double > GetDigiTimes(gsl::span< const TDigi > digiVec)
CbmPsdDigiData fPsd
PSD data.
Definition CbmDigiData.h:42
CbmTrdDigiData fTrd
TRD data.
Definition CbmDigiData.h:39
CbmTofDigiData fTof
TOF data.
Definition CbmDigiData.h:41
CbmStsDigiData fSts
STS data.
Definition CbmDigiData.h:36
CbmFsdDigiData fFsd
FSD data.
Definition CbmDigiData.h:43
CbmRichDigiData fRich
RICH data.
Definition CbmDigiData.h:38
CbmMuchDigiData fMuch
MUCH data.
Definition CbmDigiData.h:37
CbmBmonDigiData fBmon
Beam monitor data.
Definition CbmDigiData.h:35
Collection of digis from all detector systems within one timeslice.
CbmDigiData fData
Timeslice data.
std::vector< CbmFsdDigi > fDigis
Data vector.
std::vector< CbmMuchDigi > fDigis
Data vector.
std::vector< CbmPsdDigi > fDigis
Data vector.
std::vector< CbmRichDigi > fDigis
Data vector.
std::vector< CbmStsDigi > fDigis
Data vector.
std::vector< CbmTofDigi > fDigis
Data vector.
std::vector< CbmTrdDigi > fDigis
Data vector.
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition CbmMQDefs.h:26
Hash for CbmL1LinkKey.