CbmRoot
Loading...
Searching...
No Matches
CbmStar2019EventBuilderEtof.cxx
Go to the documentation of this file.
1/* Copyright (C) 2018-2020 Facility for Antiproton and Ion Research in Europe, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau [committer] */
4
5// -----------------------------------------------------------------------------
6// ----- -----
7// ----- CbmStar2019EventBuilderEtof -----
8// ----- Created 14.11.2018 by P.-A. Loizeau -----
9// ----- -----
10// -----------------------------------------------------------------------------
11
13
15#include "CbmStar2019TofPar.h"
16#include "FairParGenericSet.h"
17#include "FairRootManager.h"
18#include "FairRun.h"
19#include "FairRunOnline.h"
20#include "FairRuntimeDb.h"
21#include "TCanvas.h"
22#include "TH2.h"
23#include "THttpServer.h"
24#include "TProfile.h"
25#include "TROOT.h"
26#include "TString.h"
27
28#include <Logger.h>
29
30#include <TFile.h>
31
32#include <chrono>
33#include <cstdint>
34#include <fstream>
35#include <iomanip>
36#include <iostream>
37
39
42 , fbMonitorMode(kFALSE)
43 , fbDebugMonitorMode(kFALSE)
44 , fbStoreLostEventMsg(kFALSE)
45 , fbAddStatusToEvent(kTRUE)
46 , fbSandboxMode(kFALSE)
47 , fbEventDumpEna(kFALSE)
48 , fParCList(nullptr)
49 , fuMinTotPulser(90)
50 , fuMaxTotPulser(110)
51 , fsHistoFileName("data/eventBuilderMonHist.root")
52 , fulTsCounter(0)
53 , fEventBuilderAlgo(nullptr)
54 , fdMsSizeInNs(-1.0)
55 , fdTsCoreSizeInSec(-1.0)
56 , fTimer()
57 , fdRealTime(0.0)
58 , fdRealTimeMin(1e6)
59 , fdRealTimeMax(0.0)
60 , fdRealTimeAll(0.0)
61 , fdRealTimeMinAll(1e6)
62 , fdRealTimeMaxAll(0.0)
63 , fulNbEvents(0)
65 , fhRealTimeDistr(nullptr)
66 , fhRealTimeEvo(nullptr)
67 , fhMeanRealTimeEvo(nullptr)
68 , fpBinDumpFile(nullptr)
69{
71}
72
74
76{
77 LOG(info) << "CbmStar2019EventBuilderEtof::Init";
78 LOG(info) << "Initializing STAR eTOF 2018 Event Builder";
79
80 FairRootManager* ioman = FairRootManager::Instance();
81 if (NULL == ioman) { LOG(fatal) << "No FairRootManager instance"; }
82
83 return kTRUE;
84}
85
87{
88 LOG(info) << "Setting parameter containers for " << GetName();
89
90 fParCList = fEventBuilderAlgo->GetParList();
91
92 for (Int_t iparC = 0; iparC < fParCList->GetEntries(); ++iparC) {
93 FairParGenericSet* tempObj = (FairParGenericSet*) (fParCList->At(iparC));
94 fParCList->Remove(tempObj);
95
96 std::string sParamName {tempObj->GetName()};
97 FairParGenericSet* newObj =
98 dynamic_cast<FairParGenericSet*>(FairRun::Instance()->GetRuntimeDb()->getContainer(sParamName.data()));
99
100 if (nullptr == newObj) {
101 LOG(error) << "Failed to obtain parameter container " << sParamName << ", for parameter index " << iparC;
102 return;
103 } // if( nullptr == newObj )
104
105 fParCList->AddAt(newObj, iparC);
106 // delete tempObj;
107 } // for( Int_t iparC = 0; iparC < fParCList->GetEntries(); ++iparC )
108}
109
111{
112 LOG(info) << "Init parameter containers for " << GetName();
113
115 CbmStar2019TofPar* pUnpackPar =
116 dynamic_cast<CbmStar2019TofPar*>(FairRun::Instance()->GetRuntimeDb()->getContainer("CbmStar2019TofPar"));
117 if (nullptr == pUnpackPar) {
118 LOG(error) << "Failed to obtain parameter container CbmStar2019TofPar";
119 return kFALSE;
120 } // if( nullptr == pUnpackPar )
121
122 // fbMonitorMode = pUnpackPar->GetMonitorMode();
123 LOG(info) << "Monitor mode: " << (fbMonitorMode ? "ON" : "OFF");
124
125 // fbDebugMonitorMode = pUnpackPar->GetDebugMonitorMode();
126 LOG(info) << "Debug Monitor mode: " << (fbDebugMonitorMode ? "ON" : "OFF");
127
129 fdMsSizeInNs = pUnpackPar->GetSizeMsInNs();
130 LOG(info) << "Timeslice parameters: each MS is " << fdMsSizeInNs << " ns";
131
132 fEventBuilderAlgo->SetMonitorMode(fbMonitorMode);
133 fEventBuilderAlgo->SetDebugMonitorMode(fbDebugMonitorMode);
134 fEventBuilderAlgo->SetStoreLostEventMsg(fbStoreLostEventMsg);
135 fEventBuilderAlgo->SetAddStatusToEvent(fbAddStatusToEvent);
136 fEventBuilderAlgo->SetPulserTotLimits(fuMinTotPulser, fuMaxTotPulser);
137
138 Bool_t initOK = fEventBuilderAlgo->InitContainers();
139
141 if (kTRUE == fbMonitorMode) {
143 initOK &= fEventBuilderAlgo->CreateHistograms();
144
146 std::vector<std::pair<TNamed*, std::string>> vHistos = fEventBuilderAlgo->GetHistoVector();
148 std::vector<std::pair<TCanvas*, std::string>> vCanvases = fEventBuilderAlgo->GetCanvasVector();
149
151 THttpServer* server = FairRunOnline::Instance()->GetHttpServer();
152 for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
153 server->Register(Form("/%s", vHistos[uHisto].second.data()), vHistos[uHisto].first);
154 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
155
156 for (UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv) {
157 // LOG(info) << "Registering " << vCanvases[ uCanv ].first->GetName()
158 // << " in " << vCanvases[ uCanv ].second.data();
159 server->Register(Form("/%s", vCanvases[uCanv].second.data()),
160 gROOT->FindObject((vCanvases[uCanv].first)->GetName()));
161 } // for( UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv )
162
163 server->RegisterCommand("/Reset_EvtBuild_Hist", "bStarEtof2019EventBuilderResetHistos=kTRUE");
164 server->Restrict("/Reset_EvtBuild_Hist", "allow=admin");
165
166 if (fbDebugMonitorMode) {
167 fhRealTimeDistr = new TH1I("hEvtBuildRealTimeDistr",
168 "Realtime for processing a TS in event "
169 "builder; Realtime [ms]; TS nb []",
170 100000, 0.0, 100.0);
171 fhRealTimeEvo = new TH2I("hEvtBuildRealTimeEvo",
172 "Realtime Processing to duration ratio for processing a TS in event "
173 "builder vs TS index; TS []; Realtime ratio []; TS Nb []",
174 1000, 0, 100000, 10000, 0.0, 100.0);
175
176 fhMeanRealTimeEvo = new TProfile("hEvtBuildMeanRealTimeEvo",
177 "Mean Realtime Processing to duration ratio for processing a TS in "
178 "event builder vs TS index; TS []; Mean Realtime ratio []; TS Nb []",
179 1000, 0, 100000);
180
181 server->Register("/EvtBuildTime", fhRealTimeDistr);
182 server->Register("/EvtBuildTime", fhRealTimeEvo);
183 server->Register("/EvtBuildTime", fhMeanRealTimeEvo);
184 } // if( fbDebugMonitorMode )
185
186 } // if( kTRUE == fbMonitorMode )
187
188 return initOK;
189}
190
192{
193 LOG(info) << "ReInit parameter containers for " << GetName();
194 Bool_t initOK = fEventBuilderAlgo->ReInitContainers();
195
196 return initOK;
197}
198
200{
201 if (fbEventDumpEna != bDumpEna) {
202 if (bDumpEna) {
203 LOG(info) << "Enabling event dump to binary file which was disabled. "
204 "File will be opened.";
205
206 std::time_t cTimeCurrent = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
207 char tempBuff[80];
208 std::strftime(tempBuff, 80, "%Y_%m_%d_%H_%M_%S", localtime(&cTimeCurrent));
209 TString sFileName = Form("event_dump_%s.bin", tempBuff);
210 fpBinDumpFile = new std::fstream(sFileName, std::ios::out | std::ios::binary);
211
212 if (NULL == fpBinDumpFile) {
213 LOG(fatal) << "Failed to open new binary file for event dump at " << sFileName;
214 } // if( NULL == fpBinDumpFile )
215 else
216 LOG(info) << "Opened binary dump file at " << sFileName;
217 } // if( bDumpEna )
218 else {
219 LOG(info) << "Disabling event dump to binary file which was enabled. "
220 "File will be closed.";
221
222 if (NULL != fpBinDumpFile) fpBinDumpFile->close();
223 } // else of if( bDumpEna )
224 } // if( fbEventDumpEna != bDumpEna )
225
226 fbEventDumpEna = bDumpEna;
227 if (fbEventDumpEna) LOG(info) << "Event dump to binary file is now ENABLED";
228 else
229 LOG(info) << "Event dump to binary file is now DISABLED";
230}
231
232void CbmStar2019EventBuilderEtof::AddMsComponentToList(size_t component, UShort_t usDetectorId)
233{
234 fEventBuilderAlgo->AddMsComponentToList(component, usDetectorId);
235}
236
237Bool_t CbmStar2019EventBuilderEtof::DoUnpack(const fles::Timeslice& ts, size_t /*component*/)
238{
239 fTimer.Start();
240
242 if (-1.0 == fdTsCoreSizeInSec) {
243 fdTsCoreSizeInSec = fdMsSizeInNs * ts.num_core_microslices() / 1e9;
244 } // if( -1.0 == fdTsCoreSizeInSec )
245
246 if (0 == fulTsCounter) {
247 LOG(info) << "FIXME ===> Jumping 1st TS as corrupted with current FW + "
248 "FLESNET combination";
249 fulTsCounter++;
250 return kTRUE;
251 } // if( 0 == fulTsCounter )
252
254 LOG(info) << "Reset eTOF STAR histos ";
255 fEventBuilderAlgo->ResetHistograms();
257
258 if (fbDebugMonitorMode) {
259 fhRealTimeDistr->Reset();
260 fhRealTimeEvo->Reset();
261 fhMeanRealTimeEvo->Reset();
262 } // if( fbDebugMonitorMode )
263 } // if( fbMonitorMode && bStarEtof2019EventBuilderResetHistos )
264
265 if (kFALSE == fEventBuilderAlgo->ProcessTs(ts)) {
266 LOG(error) << "Failed processing TS " << ts.index() << " in event builder algorithm class";
267 return kTRUE;
268 } // if( kFALSE == fEventBuilderAlgo->ProcessTs( ts ) )
269
270 std::vector<CbmTofStarSubevent2019>& eventBuffer = fEventBuilderAlgo->GetEventBuffer();
271
272 for (UInt_t uEvent = 0; uEvent < eventBuffer.size(); ++uEvent) {
274 Int_t iBuffSzByte = 0;
275 void* pDataBuff = eventBuffer[uEvent].BuildOutput(iBuffSzByte);
276 if (NULL != pDataBuff) {
278 // Bool_t fbSendEventToStar = kFALSE;
279 if (kFALSE == fbSandboxMode) {
280 /*
281 ** Function to send sub-event block to the STAR DAQ system
282 * trg_word received is packed as:
283 *
284 * trg_cmd|daq_cmd|tkn_hi|tkn_mid|tkn_lo
285 */
286 star_rhicf_write(eventBuffer[uEvent].GetTrigger().GetStarTrigerWord(), pDataBuff, iBuffSzByte);
287 } // if( kFALSE == fbSandboxMode )
288
289 LOG(debug) << "Sent STAR event with size " << iBuffSzByte << " Bytes"
290 << " and token " << eventBuffer[uEvent].GetTrigger().GetStarToken();
291
292 if (kTRUE == fbEventDumpEna) {
293 fpBinDumpFile->write(reinterpret_cast<const char*>(&kuBinDumpBegWord), sizeof(UInt_t));
294 fpBinDumpFile->write(reinterpret_cast<const char*>(&iBuffSzByte), sizeof(Int_t));
295 fpBinDumpFile->write(reinterpret_cast<const char*>(pDataBuff), iBuffSzByte);
296 fpBinDumpFile->write(reinterpret_cast<const char*>(&kuBinDumpEndWord), sizeof(UInt_t));
297 } // if( kTRUE == fbEventDumpEna )
298 } // if( NULL != pDataBuff )
299 else
300 LOG(error) << "Invalid STAR SubEvent Output, can only happen if trigger "
301 << " object was not set => Do Nothing more with it!!! ";
302 } // for( UInt_t uEvent = 0; uEvent < eventBuffer.size(); ++uEvent )
303 fTimer.Stop();
304 Double_t dRealT = fTimer.RealTime();
305 if (fbDebugMonitorMode) {
306 LOG(debug2) << Form("Real time TS %12lu, Realtime: %12f ns", static_cast<unsigned long>(fulTsCounter),
307 dRealT * 1e9);
308 fhRealTimeDistr->Fill(dRealT * 1e3);
311 } // if( fbDebugMonitorMode )
312 fdRealTime += dRealT;
313 if (dRealT < fdRealTimeMin) {
315 LOG(debug) << Form("New min Real time TS %12lu, Real time: %9.6f ms Old "
316 "Min %9.6f Diff %.9f",
317 static_cast<unsigned long>(fulTsCounter), dRealT, fdRealTimeMin, fdRealTimeMin - dRealT);
318 fdRealTimeMin = dRealT;
319 } // if( dRealT < fdRealTimeMin )
320 if (fdRealTimeMax < dRealT) {
322 LOG(debug) << Form("New max Real time TS %12lu, Real time: %9.6f ms Old "
323 "Max %9.6f Diff %.9f",
324 static_cast<unsigned long>(fulTsCounter), dRealT, fdRealTimeMax, fdRealTimeMax - dRealT);
325 fdRealTimeMax = dRealT;
326 } // if( fdRealTimeMax < dRealT )
327 fulNbEvents += eventBuffer.size();
328 fulNbEventsSinceLastPrintout += eventBuffer.size();
329
330 if (0 == fulTsCounter % 10000) {
331 fdRealTime /= 10000;
332 LOG(info) << Form("Processed %12lu TS, Real time: %6.3f ms/TS (Min %6.3f, "
333 "Max %6.3f), Events: %12lu (%9lu since last print)",
334 static_cast<unsigned long>(fulTsCounter), fdRealTime, fdRealTimeMin, fdRealTimeMax,
335 static_cast<unsigned long>(fulNbEvents),
336 static_cast<unsigned long>(fulNbEventsSinceLastPrintout));
337 fdRealTime = 0.0;
338 fdRealTimeMin = 1e6;
339 fdRealTimeMax = 0.0;
341 } // if( 0 == fulTsCounter % 10000 )
342 fulTsCounter++;
343
344 return kTRUE;
345}
346
348
350{
351 if (NULL != fpBinDumpFile) {
352 LOG(info) << "Closing binary file used for event dump.";
353 fpBinDumpFile->close();
354 } // if( NULL != fpBinDumpFile )
355
357 if (kTRUE == fbMonitorMode) { SaveHistograms(); } // if( kTRUE == fbMonitorMode )
358}
359
361{
363 std::vector<std::pair<TNamed*, std::string>> vHistos = fEventBuilderAlgo->GetHistoVector();
364 std::vector<std::pair<TCanvas*, std::string>> vCanvas = fEventBuilderAlgo->GetCanvasVector();
365
367 TDirectory* oldDir = NULL;
368 TFile* histoFile = NULL;
369 // Store current directory position to allow restore later
370 oldDir = gDirectory;
371 // open separate histo file in recreate mode
372 histoFile = new TFile(fsHistoFileName, "RECREATE");
373
374 if (nullptr == histoFile) return kFALSE;
375
377 for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
379 TString sFolder = vHistos[uHisto].second.data();
380 if (nullptr == gDirectory->Get(sFolder)) gDirectory->mkdir(sFolder);
381 gDirectory->cd(sFolder);
382
384 vHistos[uHisto].first->Write();
385
386 histoFile->cd();
387 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
388
389 for (UInt_t uCanvas = 0; uCanvas < vCanvas.size(); ++uCanvas) {
391 TString sFolder = vCanvas[uCanvas].second.data();
392 if (nullptr == gDirectory->Get(sFolder)) gDirectory->mkdir(sFolder);
393 gDirectory->cd(sFolder);
394
396 vCanvas[uCanvas].first->Write();
397
398 histoFile->cd();
399 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
400
401 // Restore original directory position
402 oldDir->cd();
403 histoFile->Close();
404
405 return kTRUE;
406}
407
ClassImp(CbmConverterManager)
Bool_t bStarEtof2019EventBuilderResetHistos
int star_rhicf_write(unsigned int trg_word, void *dta, int bytes)
int Int_t
bool Bool_t
Bool_t fbSandboxMode
Switch ON the readout and insertion of STATUS pattern message (default is true)
TStopwatch fTimer
Total size of the core MS in a TS, [seconds].
TH2 * fhRealTimeEvo
Exclude from dictionary because ?!?!
Double_t fdTsCoreSizeInSec
Size of a single MS, [nanoseconds].
TProfile * fhMeanRealTimeEvo
Exclude from dictionary because ?!?!
virtual Bool_t DoUnpack(const fles::Timeslice &ts, size_t component)
Bool_t fbDebugMonitorMode
Switch ON the filling of a minimal set of histograms.
Double_t fdMsSizeInNs
Processing speed watch.
Bool_t fbEventDumpEna
Switch OFF the emission of data toward the STAR DAQ.
std::fstream * fpBinDumpFile
Exclude from dictionary because ?!?!
UInt_t fuMinTotPulser
User settings: Data selection parameters.
CbmStar2019EventBuilderEtofAlgo * fEventBuilderAlgo
Processing algo.
Bool_t fbAddStatusToEvent
Switch ON the insertion of the LostEvent messages from GET4s with the critical errors.
Bool_t fbStoreLostEventMsg
Switch ON the filling of a additional set of histograms.
void SetEventDumpEnable(Bool_t bDumpEna=kTRUE)
TList * fParCList
Switch ON the dumping of the events to a binary file.
void AddMsComponentToList(size_t component, UShort_t usDetectorId)
Temp until we change from CbmMcbmUnpack to something else.
uint64_t fulTsCounter
Statistics & first TS rejection.