CbmRoot
Loading...
Searching...
No Matches
CbmTaskUnpack.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer] */
4
5
6#include "CbmTaskUnpack.h"
7
8#include "CbmDefs.h"
9#include "CbmDigiBranchBase.h"
10#include "CbmDigiEvent.h"
11#include "CbmDigiManager.h"
12#include "CbmDigiTimeslice.h"
13#include "CbmSourceTs.h"
14#include "CbmTimeSlice.h"
15#include "CbmTrdParFasp.h"
16#include "CbmTrdParModAsic.h"
17#include "CbmTrdParModDigi.h"
18#include "CbmTrdParSetAsic.h"
19#include "CbmTrdParSetDigi.h"
20#include "CbmTrdParSpadic.h"
21#include "CbmTsEventHeader.h"
22#include "MicrosliceDescriptor.hpp"
23#include "ParFiles.h"
24#include "System.hpp"
25#include "bmon/ReadoutConfig.h"
26#include "sts/ChannelMaskSet.h"
27#include "tof/ReadoutConfig.h"
28#include "yaml/Yaml.h"
29
30#include <FairParAsciiFileIo.h>
31#include <FairParamList.h>
32#include <FairRunOnline.h>
33#include <Logger.h>
34
35#include <TStopwatch.h>
36
37#include <algorithm>
38#include <cassert>
39#include <cstdint>
40#include <iomanip>
41#include <memory>
42#include <sstream>
43#include <vector>
44
45using namespace std;
46using namespace cbm::algo;
47
48
49// ----- Constructor -----------------------------------------------------
50CbmTaskUnpack::CbmTaskUnpack() : FairTask("Unpack")
51{
52 L_(fatal) << "Instantiated CbmTaskUnpack() without arguments (needs path to parameters and run ID).";
53}
54
55// ----- Constructor -----------------------------------------------------
56CbmTaskUnpack::CbmTaskUnpack(fs::path paramsDir, uint32_t runId) : FairTask("Unpack")
57{
58 ParFiles parFiles(runId);
59 L_(info) << "Using parameter files for setup " << parFiles.setup;
60
62 auto chanMask = yaml::ReadFromFile<sts::ChannelMaskSet>(paramsDir / parFiles.sts.chanMask);
63 auto walkMap = yaml::ReadFromFile<sts::WalkMap>(paramsDir / parFiles.sts.walkMap);
64 sts::ReadoutConfig readout{stsSetup, chanMask};
65 sts::Unpack::Config stsCfg{.readout = readout, .walkMap = walkMap, .bCollectAuxData = false};
66 fStsUnpack = std::make_unique<sts::Unpack>(stsCfg);
67
69 tof::ReadoutConfig tofCfg{tofSetup};
70 fTofUnpack = std::make_unique<tof::Unpack>(tofCfg);
71
73 bmon::ReadoutConfig bmonCfg{bmonSetup};
74 fBmonUnpack = std::make_unique<bmon::Unpack>(bmonCfg);
75
76 auto trdCfg = yaml::ReadFromFile<trd::ReadoutConfig>(paramsDir / parFiles.trd.readout);
77 fTrdUnpack = std::make_unique<trd::Unpack>(trdCfg);
78
79 auto trd2dCfg = yaml::ReadFromFile<trd2d::ReadoutConfig>(paramsDir / parFiles.trd.readout2d);
80 fTrd2dUnpack = std::make_unique<trd2d::Unpack>(trd2dCfg);
81
82 much::ReadoutConfig muchCfg{};
83 fMuchUnpack = std::make_unique<much::Unpack>(muchCfg);
84
85 rich::ReadoutConfig richCfg{};
86 fRichUnpack = std::make_unique<rich::Unpack>(richCfg);
87}
88// ---------------------------------------------------------------------------
89
90
91// ----- Destructor ------------------------------------------------------
93{
95
97 // Clear output vectors
98 fBmonDigis->clear();
99 fStsDigis->clear();
100 fMuchDigis->clear();
101 fTrdDigis->clear();
102 fTofDigis->clear();
103 fRichDigis->clear();
104 }
105}
106// ---------------------------------------------------------------------------
107
108
109// ----- Execution -------------------------------------------------------
110void CbmTaskUnpack::Exec(Option_t*)
111{
112 // --- Get FLES timeslice
113 assert(fSource);
114 fles::Timeslice* timeslice = fSource->GetTimeslice();
115 assert(timeslice);
116
117 // --- Timer and counters
118 TStopwatch timer;
119 timer.Start();
120
121 // --- Unpack the timeslice
122 DigiData digis;
123 Monitor monitor;
124
125 digis.fBmon = RunUnpacker(fBmonUnpack, *timeslice, monitor);
126 digis.fMuch = RunUnpacker(fMuchUnpack, *timeslice, monitor);
127 digis.fRich = RunUnpacker(fRichUnpack, *timeslice, monitor);
128 digis.fSts = RunUnpacker(fStsUnpack, *timeslice, monitor);
129 digis.fTof = RunUnpacker(fTofUnpack, *timeslice, monitor);
130 digis.fTrd = RunUnpacker(fTrdUnpack, *timeslice, monitor);
131 digis.fTrd2d = RunUnpacker(fTrd2dUnpack, *timeslice, monitor);
132
134 // Clear output vectors
135 fBmonDigis->clear();
136 fStsDigis->clear();
137 fMuchDigis->clear();
138 fTrdDigis->clear();
139 fTofDigis->clear();
140 fRichDigis->clear();
141
142 fTsEventHeader->SetTsIndex(timeslice->index());
143 fTsEventHeader->SetTsStartTime(timeslice->start_time());
144
145 fTimeslice->SetStartTime(timeslice->start_time());
146
147 std::move(digis.fBmon.begin(), digis.fBmon.end(), std::back_inserter(*fBmonDigis));
148 std::move(digis.fSts.begin(), digis.fSts.end(), std::back_inserter(*fStsDigis));
149 std::move(digis.fMuch.begin(), digis.fMuch.end(), std::back_inserter(*fMuchDigis));
150 std::move(digis.fTrd2d.begin(), digis.fTrd2d.end(), std::back_inserter(*fTrdDigis));
151 std::move(digis.fTrd.begin(), digis.fTrd.end(), std::back_inserter(*fTrdDigis));
152 std::move(digis.fTof.begin(), digis.fTof.end(), std::back_inserter(*fTofDigis));
153 std::move(digis.fRich.begin(), digis.fRich.end(), std::back_inserter(*fRichDigis));
154
155 // Time-sort the TRD vector as we merged TRD1D and TRD2D (needed for compatibility with legacy unpackers)
157 }
158 else {
159 // --- Reset output branch (CbmDigiTimeslice)
161
162 // Use lines below to combine TRD 1D and 2D
163 //auto& digis1d = digis.fTrd;
164 //auto& digis2d = digis.fTrd2d;
165 //std::copy(digis2d.begin(), digis2d.end(), std::back_inserter(digis1d));
166
168 }
169
170 // --- Timeslice log
171 timer.Stop();
172 stringstream logOut;
173 logOut << setw(15) << left << GetName() << " [";
174 logOut << fixed << setw(8) << setprecision(1) << right << timer.RealTime() * 1000. << " ms] ";
175 logOut << "TS " << fNumTs << " (index " << timeslice->index() << ")";
176 logOut << ", components " << monitor.numCompUsed << " / " << timeslice->num_components();
177 logOut << ", microslices " << monitor.numMs;
178 logOut << ", input rate " << double(monitor.numBytes) / timer.RealTime() / 1.e6 << " MB/s";
179 logOut << ", digis " << monitor.numDigis;
180 LOG(info) << logOut.str();
181
182#if !defined(__CLING__) && !defined(__ROOTCLING__)
183 if (fMonitor) {
184 fMonitor->QueueMetric(GetName(), {{"host", fHostname}},
185 {{"realtime", timer.RealTime()},
186 {"cputime", timer.CpuTime()},
187 {"input_size", monitor.numBytes},
188 {"input_rate", double(monitor.numBytes) / timer.RealTime()},
189 {"digis", monitor.numDigis}});
190 }
191#endif
192
193 // --- Run statistics
194 fNumTs++;
195 fNumMs += monitor.numMs;
196 fNumBytes += monitor.numBytes;
197 fNumDigis += monitor.numDigis;
198 fTime += timer.RealTime();
199}
200// ----------------------------------------------------------------------------
201
202
203// ----- End-of-run action ------------------------------------------------
205{
206 double timePerTs = 1000. * fTime / double(fNumTs); // in ms
207 double rate = fNumBytes / 1.e6 / fTime; // in MB/s
208 LOG(info) << "=====================================";
209 LOG(info) << GetName() << ": Run summary";
210 LOG(info) << "Timeslices : " << fNumTs;
211 LOG(info) << "Microslices : " << fNumMs;
212 LOG(info) << "Digis : " << fNumDigis;
213 LOG(info) << "Av. input rate : " << fixed << setprecision(2) << rate << " MB/s";
214 LOG(info) << "Time / TS : " << fixed << setprecision(2) << timePerTs << " ms";
215 LOG(info) << "=====================================";
216}
217// ----------------------------------------------------------------------------
218
219
220// ----- Initialisation ---------------------------------------------------
221template<typename TVecobj>
222Bool_t CbmTaskUnpack::RegisterVector(FairRootManager* ioman, std::vector<TVecobj>*& vec)
223{
224 if (ioman->GetObject(TVecobj::GetBranchName())) {
225 LOG(fatal) << GetName() << ": Branch " << TVecobj::GetBranchName() << " already exists!";
226 return kFALSE;
227 }
228
229 ioman->RegisterAny(TVecobj::GetBranchName(), vec, kTRUE);
230 LOG(info) << GetName() << ": Registered branch " << TVecobj::GetBranchName() << " at " << vec;
231
232 return kTRUE;
233}
234
236{
237 LOG(info) << "==================================================";
238 LOG(info) << GetName() << ": Initialising...";
239
240 // --- Get hostname
241 fHostname = fles::system::current_hostname();
242
243 // --- Get source instance
244 fSource = dynamic_cast<CbmSourceTs*>(FairRunOnline::Instance()->GetSource());
245 if (fSource == nullptr) {
246 LOG(error) << GetName() << ": No valid source class registered!";
247 return kFATAL;
248 }
249 LOG(info) << "--- Found CbmSourceTs instance";
250
251 // --- Get FairRootManager instance
252 FairRootManager* ioman = FairRootManager::Instance();
253 assert(ioman);
254
255 // --- Register output array (CbmDigiTimeslice)
256 if (ioman->GetObject("DigiTimeslice")) {
257 LOG(fatal) << GetName() << ": Branch DigiTimeslice already exists!";
258 return kFATAL;
259 }
260
262
263 if (!(fTsEventHeader = dynamic_cast<CbmTsEventHeader*>(FairRun::Instance()->GetEventHeader()))) {
264 LOG(fatal) << "CbmSourceDigiTimeslice::Init() no CbmTsEventHeader was added to the run. "
265 "Without it, we can not store the UTC of the Timeslices correctly. "
266 "Hence, this causes a fatal. Please add it to the Run in the steering macro.";
267 return kFATAL;
268 }
269
270 // TimeSlice. branch initialization
271 if (ioman->GetObject("TimeSlice.")) {
272 LOG(fatal) << "Source: Branch TimeSlice. already exists!";
273 return kFATAL;
274 }
275 else {
276 // NOTE: the max time of timeslice is 1.28e8, taken from CbmRecoUnpack.cxx
277 fTimeslice = new CbmTimeSlice(0., 1.28e8 + 1.28e6);
278 ioman->Register("TimeSlice.", "DAQ", fTimeslice, kTRUE);
279 }
280
281 fBmonDigis = new std::vector<CbmBmonDigi>();
282 if (kFALSE == RegisterVector<CbmBmonDigi>(ioman, fBmonDigis)) {
283 return kFATAL;
284 }
285
286
287 fStsDigis = new std::vector<CbmStsDigi>();
288 if (kFALSE == RegisterVector<CbmStsDigi>(ioman, fStsDigis)) {
289 return kFATAL;
290 }
291
292
293 fMuchDigis = new std::vector<CbmMuchDigi>();
294 if (kFALSE == RegisterVector<CbmMuchDigi>(ioman, fMuchDigis)) {
295 return kFATAL;
296 }
297
298
299 fTrdDigis = new std::vector<CbmTrdDigi>();
300 if (kFALSE == RegisterVector<CbmTrdDigi>(ioman, fTrdDigis)) {
301 return kFATAL;
302 }
303
304
305 fTofDigis = new std::vector<CbmTofDigi>();
306 if (kFALSE == RegisterVector<CbmTofDigi>(ioman, fTofDigis)) {
307 return kFATAL;
308 }
309
310
311 fRichDigis = new std::vector<CbmRichDigi>();
312 if (kFALSE == RegisterVector<CbmRichDigi>(ioman, fRichDigis)) {
313 return kFATAL;
314 }
315 }
316 else {
318 ioman->RegisterAny("DigiTimeslice.", fDigiTimeslice, IsOutputBranchPersistent("DigiTimeslice."));
319 LOG(info) << "--- Registered branch DigiTimeslice.";
320 }
321
322 return kSUCCESS;
323}
324// ----------------------------------------------------------------------------
325
326
327template<class Unpacker>
328auto CbmTaskUnpack::RunUnpacker(const std::unique_ptr<Unpacker>& unpacker, const fles::Timeslice& timeslice,
330{
331 auto [digis, detmon, detaux] = (*unpacker)(timeslice);
332 monitor.numCompUsed += detmon.numComponents;
333 monitor.numMs += detmon.numMs;
334 monitor.numBytes += detmon.sizeBytesIn;
335 monitor.numDigis += digis.size();
336 return digis;
337}
338
#define L_(level)
ClassImp(CbmConverterManager)
This file contains the definition of the ParFiles class.
Collection of digis from all detector systems within one timeslice.
CbmDigiData fData
Timeslice data.
void Clear()
Clear content.
Source class for reading from archived time slice data.
Definition CbmSourceTs.h:26
fles::Timeslice * GetTimeslice()
Pointer to current FLES timeslice.
Definition CbmSourceTs.h:81
Task class for associating digis to events.
Bool_t RegisterVector(FairRootManager *ioman, std::vector< TVecobj > *&vec)
virtual ~CbmTaskUnpack()
Destructor.
virtual void Exec(Option_t *opt)
Task execution.
CbmTaskUnpack()
Constructor.
bool fCbmrootFormatOutput
std::vector< CbmMuchDigi > * fMuchDigis
std::unique_ptr< cbm::algo::sts::Unpack > fStsUnpack
std::unique_ptr< cbm::algo::trd::Unpack > fTrdUnpack
std::string fHostname
std::unique_ptr< cbm::algo::trd2d::Unpack > fTrd2dUnpack
CbmTimeSlice * fTimeslice
=> Time-slice header (old version, class about to be deprecated? one should use only CbmTsEventHeader...
std::vector< CbmTofDigi > * fTofDigis
std::vector< CbmStsDigi > * fStsDigis
CbmTsEventHeader * fTsEventHeader
Time-slice event header.
std::vector< CbmBmonDigi > * fBmonDigis
=> Branch vectors of Digis
virtual InitStatus Init()
Task initialisation.
cbm::Monitor * fMonitor
std::unique_ptr< cbm::algo::rich::Unpack > fRichUnpack
virtual void Finish()
Finish timeslice.
std::vector< CbmRichDigi > * fRichDigis
CbmSourceTs * fSource
std::unique_ptr< cbm::algo::bmon::Unpack > fBmonUnpack
std::unique_ptr< cbm::algo::tof::Unpack > fTofUnpack
std::vector< CbmTrdDigi > * fTrdDigis
auto RunUnpacker(const std::unique_ptr< Unpacker > &unpacker, const fles::Timeslice &ts, Monitor &monitor) -> cbm::algo::algo_traits::Output_t< Unpacker >
std::enable_if< std::is_member_function_pointer< decltype(&TVecobj::GetTime)>::value, void >::type Timesort(std::vector< TVecobj > *vec=nullptr)
CbmDigiTimeslice * fDigiTimeslice
Output data if writing root files "as if rra".
std::unique_ptr< cbm::algo::much::Unpack > fMuchUnpack
Bookkeeping of time-slice content.
void SetStartTime(double time)
Set start time.
void SetTsStartTime(uint64_t value)
Set the Ts Start Time.
void SetTsIndex(uint64_t value)
Set the Ts Start Time.
Provides the hardware-to-software address mapping for the CBM-RICH.
Provides the hardware-to-software address mapping for the CBM-STS.
typename std::tuple_element< 0, ResultOf_t< Algo > >::type Output_t
Type alias for the output type produced by an algorithm.
Definition AlgoTraits.h:53
T ReadFromFile(fs::path path)
Definition Yaml.h:51
Hash for CbmL1LinkKey.
Collection of digis from all detector systems.
Definition DigiData.h:31
PODVector< CbmRichDigi > fRich
Unpacked RICH digis.
Definition DigiData.h:38
PODVector< CbmTrdDigi > fTrd
Unpacked TRD digis.
Definition DigiData.h:36
PODVector< CbmStsDigi > fSts
Unpacked STS digis.
Definition DigiData.h:32
PODVector< CbmTrdDigi > fTrd2d
Unpacked TRD2D digis.
Definition DigiData.h:37
PODVector< CbmTofDigi > fTof
Unpacked TOF digis.
Definition DigiData.h:34
CbmDigiData ToStorable() const
Convert to CbmDigiData for file storage.
Definition DigiData.cxx:56
PODVector< CbmMuchDigi > fMuch
Unpacked MUCH digis.
Definition DigiData.h:33
PODVector< CbmBmonDigi > fBmon
Unpacked Bmon digis.
Definition DigiData.h:35
Class to hold the paths to the parameter files for the different detectors.
Definition ParFiles.h:21
struct cbm::algo::ParFiles::@2 tof
struct cbm::algo::ParFiles::@0 bmon
struct cbm::algo::ParFiles::@1 sts
struct cbm::algo::ParFiles::@3 trd
fs::path readout2d
Definition ParFiles.h:46
fs::path chanMask
Definition ParFiles.h:33
Readout setup / Hardware cabling for BMon Used to create the hardware mapping for the BMon unpacker.
Readout setup / Hardware cabling for STS Used to create the hardware mapping for the STS unpacker.
Readout setup / Hardware cabling for TOF Used to create the hardware mapping for the TOF unpacker.