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
17#include "FairParGenericSet.h"
18#include "FairRootManager.h"
19#include "FairRun.h"
20#include "FairRunOnline.h"
21#include "FairRuntimeDb.h"
22#include <Logger.h>
23
24#include "TCanvas.h"
25#include "TH2.h"
26#include "THttpServer.h"
27#include "TProfile.h"
28#include "TROOT.h"
29#include "TString.h"
30#include <TFile.h>
31
32#include <chrono>
33#include <fstream>
34#include <iomanip>
35#include <iostream>
36
37#include <stdint.h>
38
40
43 , fbMonitorMode(kFALSE)
44 , fbDebugMonitorMode(kFALSE)
45 , fbStoreLostEventMsg(kFALSE)
46 , fbAddStatusToEvent(kTRUE)
47 , fbSandboxMode(kFALSE)
48 , fbEventDumpEna(kFALSE)
49 , fParCList(nullptr)
50 , fuMinTotPulser(90)
51 , fuMaxTotPulser(110)
52 , fsHistoFileName("data/eventBuilderMonHist.root")
53 , fulTsCounter(0)
54 , fEventBuilderAlgo(nullptr)
55 , fdMsSizeInNs(-1.0)
56 , fdTsCoreSizeInSec(-1.0)
57 , fTimer()
58 , fdRealTime(0.0)
59 , fdRealTimeMin(1e6)
60 , fdRealTimeMax(0.0)
61 , fdRealTimeAll(0.0)
62 , fdRealTimeMinAll(1e6)
63 , fdRealTimeMaxAll(0.0)
64 , fulNbEvents(0)
65 , fulNbEventsSinceLastPrintout(0)
66 , fhRealTimeDistr(nullptr)
67 , fhRealTimeEvo(nullptr)
68 , fhMeanRealTimeEvo(nullptr)
69 , fpBinDumpFile(nullptr)
70{
72}
73
75
77{
78 LOG(info) << "CbmStar2019EventBuilderEtof::Init";
79 LOG(info) << "Initializing STAR eTOF 2018 Event Builder";
80
81 FairRootManager* ioman = FairRootManager::Instance();
82 if (NULL == ioman) { LOG(fatal) << "No FairRootManager instance"; }
83
84 return kTRUE;
85}
86
88{
89 LOG(info) << "Setting parameter containers for " << GetName();
90
92
93 for (Int_t iparC = 0; iparC < fParCList->GetEntries(); ++iparC) {
94 FairParGenericSet* tempObj = (FairParGenericSet*) (fParCList->At(iparC));
95 fParCList->Remove(tempObj);
96
97 std::string sParamName {tempObj->GetName()};
98 FairParGenericSet* newObj =
99 dynamic_cast<FairParGenericSet*>(FairRun::Instance()->GetRuntimeDb()->getContainer(sParamName.data()));
100
101 if (nullptr == newObj) {
102 LOG(error) << "Failed to obtain parameter container " << sParamName << ", for parameter index " << iparC;
103 return;
104 } // if( nullptr == newObj )
105
106 fParCList->AddAt(newObj, iparC);
107 // delete tempObj;
108 } // for( Int_t iparC = 0; iparC < fParCList->GetEntries(); ++iparC )
109}
110
112{
113 LOG(info) << "Init parameter containers for " << GetName();
114
116 CbmStar2019TofPar* pUnpackPar =
117 dynamic_cast<CbmStar2019TofPar*>(FairRun::Instance()->GetRuntimeDb()->getContainer("CbmStar2019TofPar"));
118 if (nullptr == pUnpackPar) {
119 LOG(error) << "Failed to obtain parameter container CbmStar2019TofPar";
120 return kFALSE;
121 } // if( nullptr == pUnpackPar )
122
123 // fbMonitorMode = pUnpackPar->GetMonitorMode();
124 LOG(info) << "Monitor mode: " << (fbMonitorMode ? "ON" : "OFF");
125
126 // fbDebugMonitorMode = pUnpackPar->GetDebugMonitorMode();
127 LOG(info) << "Debug Monitor mode: " << (fbDebugMonitorMode ? "ON" : "OFF");
128
130 fdMsSizeInNs = pUnpackPar->GetSizeMsInNs();
131 LOG(info) << "Timeslice parameters: each MS is " << fdMsSizeInNs << " ns";
132
138
139 Bool_t initOK = fEventBuilderAlgo->InitContainers();
140
142 if (kTRUE == fbMonitorMode) {
145
147 std::vector<std::pair<TNamed*, std::string>> vHistos = fEventBuilderAlgo->GetHistoVector();
149 std::vector<std::pair<TCanvas*, std::string>> vCanvases = fEventBuilderAlgo->GetCanvasVector();
150
152 THttpServer* server = FairRunOnline::Instance()->GetHttpServer();
153 for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
154 server->Register(Form("/%s", vHistos[uHisto].second.data()), vHistos[uHisto].first);
155 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
156
157 for (UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv) {
158 // LOG(info) << "Registering " << vCanvases[ uCanv ].first->GetName()
159 // << " in " << vCanvases[ uCanv ].second.data();
160 server->Register(Form("/%s", vCanvases[uCanv].second.data()),
161 gROOT->FindObject((vCanvases[uCanv].first)->GetName()));
162 } // for( UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv )
163
164 server->RegisterCommand("/Reset_EvtBuild_Hist", "bStarEtof2019EventBuilderResetHistos=kTRUE");
165 server->Restrict("/Reset_EvtBuild_Hist", "allow=admin");
166
167 if (fbDebugMonitorMode) {
168 fhRealTimeDistr = new TH1I("hEvtBuildRealTimeDistr",
169 "Realtime for processing a TS in event "
170 "builder; Realtime [ms]; TS nb []",
171 100000, 0.0, 100.0);
172 fhRealTimeEvo = new TH2I("hEvtBuildRealTimeEvo",
173 "Realtime Processing to duration ratio for processing a TS in event "
174 "builder vs TS index; TS []; Realtime ratio []; TS Nb []",
175 1000, 0, 100000, 10000, 0.0, 100.0);
176
177 fhMeanRealTimeEvo = new TProfile("hEvtBuildMeanRealTimeEvo",
178 "Mean Realtime Processing to duration ratio for processing a TS in "
179 "event builder vs TS index; TS []; Mean Realtime ratio []; TS Nb []",
180 1000, 0, 100000);
181
182 server->Register("/EvtBuildTime", fhRealTimeDistr);
183 server->Register("/EvtBuildTime", fhRealTimeEvo);
184 server->Register("/EvtBuildTime", fhMeanRealTimeEvo);
185 } // if( fbDebugMonitorMode )
186
187 } // if( kTRUE == fbMonitorMode )
188
189 return initOK;
190}
191
193{
194 LOG(info) << "ReInit parameter containers for " << GetName();
195 Bool_t initOK = fEventBuilderAlgo->ReInitContainers();
196
197 return initOK;
198}
199
201{
202 if (fbEventDumpEna != bDumpEna) {
203 if (bDumpEna) {
204 LOG(info) << "Enabling event dump to binary file which was disabled. "
205 "File will be opened.";
206
207 std::time_t cTimeCurrent = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
208 char tempBuff[80];
209 std::strftime(tempBuff, 80, "%Y_%m_%d_%H_%M_%S", localtime(&cTimeCurrent));
210 TString sFileName = Form("event_dump_%s.bin", tempBuff);
211 fpBinDumpFile = new std::fstream(sFileName, std::ios::out | std::ios::binary);
212
213 if (NULL == fpBinDumpFile) {
214 LOG(fatal) << "Failed to open new binary file for event dump at " << sFileName;
215 } // if( NULL == fpBinDumpFile )
216 else
217 LOG(info) << "Opened binary dump file at " << sFileName;
218 } // if( bDumpEna )
219 else {
220 LOG(info) << "Disabling event dump to binary file which was enabled. "
221 "File will be closed.";
222
223 if (NULL != fpBinDumpFile) fpBinDumpFile->close();
224 } // else of if( bDumpEna )
225 } // if( fbEventDumpEna != bDumpEna )
226
227 fbEventDumpEna = bDumpEna;
228 if (fbEventDumpEna) LOG(info) << "Event dump to binary file is now ENABLED";
229 else
230 LOG(info) << "Event dump to binary file is now DISABLED";
231}
232
233void CbmStar2019EventBuilderEtof::AddMsComponentToList(size_t component, UShort_t usDetectorId)
234{
235 fEventBuilderAlgo->AddMsComponentToList(component, usDetectorId);
236}
237
238Bool_t CbmStar2019EventBuilderEtof::DoUnpack(const fles::Timeslice& ts, size_t /*component*/)
239{
240 fTimer.Start();
241
243 if (-1.0 == fdTsCoreSizeInSec) {
244 fdTsCoreSizeInSec = fdMsSizeInNs * ts.num_core_microslices() / 1e9;
245 } // if( -1.0 == fdTsCoreSizeInSec )
246
247 if (0 == fulTsCounter) {
248 LOG(info) << "FIXME ===> Jumping 1st TS as corrupted with current FW + "
249 "FLESNET combination";
250 fulTsCounter++;
251 return kTRUE;
252 } // if( 0 == fulTsCounter )
253
255 LOG(info) << "Reset eTOF STAR histos ";
258
259 if (fbDebugMonitorMode) {
260 fhRealTimeDistr->Reset();
261 fhRealTimeEvo->Reset();
262 fhMeanRealTimeEvo->Reset();
263 } // if( fbDebugMonitorMode )
264 } // if( fbMonitorMode && bStarEtof2019EventBuilderResetHistos )
265
266 if (kFALSE == fEventBuilderAlgo->ProcessTs(ts)) {
267 LOG(error) << "Failed processing TS " << ts.index() << " in event builder algorithm class";
268 return kTRUE;
269 } // if( kFALSE == fEventBuilderAlgo->ProcessTs( ts ) )
270
271 std::vector<CbmTofStarSubevent2019>& eventBuffer = fEventBuilderAlgo->GetEventBuffer();
272
273 for (UInt_t uEvent = 0; uEvent < eventBuffer.size(); ++uEvent) {
275 Int_t iBuffSzByte = 0;
276 void* pDataBuff = eventBuffer[uEvent].BuildOutput(iBuffSzByte);
277 if (NULL != pDataBuff) {
279 // Bool_t fbSendEventToStar = kFALSE;
280 if (kFALSE == fbSandboxMode) {
281 /*
282 ** Function to send sub-event block to the STAR DAQ system
283 * trg_word received is packed as:
284 *
285 * trg_cmd|daq_cmd|tkn_hi|tkn_mid|tkn_lo
286 */
287 star_rhicf_write(eventBuffer[uEvent].GetTrigger().GetStarTrigerWord(), pDataBuff, iBuffSzByte);
288 } // if( kFALSE == fbSandboxMode )
289
290 LOG(debug) << "Sent STAR event with size " << iBuffSzByte << " Bytes"
291 << " and token " << eventBuffer[uEvent].GetTrigger().GetStarToken();
292
293 if (kTRUE == fbEventDumpEna) {
294 fpBinDumpFile->write(reinterpret_cast<const char*>(&kuBinDumpBegWord), sizeof(UInt_t));
295 fpBinDumpFile->write(reinterpret_cast<const char*>(&iBuffSzByte), sizeof(Int_t));
296 fpBinDumpFile->write(reinterpret_cast<const char*>(pDataBuff), iBuffSzByte);
297 fpBinDumpFile->write(reinterpret_cast<const char*>(&kuBinDumpEndWord), sizeof(UInt_t));
298 } // if( kTRUE == fbEventDumpEna )
299 } // if( NULL != pDataBuff )
300 else
301 LOG(error) << "Invalid STAR SubEvent Output, can only happen if trigger "
302 << " object was not set => Do Nothing more with it!!! ";
303 } // for( UInt_t uEvent = 0; uEvent < eventBuffer.size(); ++uEvent )
304 fTimer.Stop();
305 Double_t dRealT = fTimer.RealTime();
306 if (fbDebugMonitorMode) {
307 LOG(debug2) << Form("Real time TS %12lu, Realtime: %12f ns", static_cast<unsigned long>(fulTsCounter),
308 dRealT * 1e9);
309 fhRealTimeDistr->Fill(dRealT * 1e3);
312 } // if( fbDebugMonitorMode )
313 fdRealTime += dRealT;
314 if (dRealT < fdRealTimeMin) {
316 LOG(debug) << Form("New min Real time TS %12lu, Real time: %9.6f ms Old "
317 "Min %9.6f Diff %.9f",
318 static_cast<unsigned long>(fulTsCounter), dRealT, fdRealTimeMin, fdRealTimeMin - dRealT);
319 fdRealTimeMin = dRealT;
320 } // if( dRealT < fdRealTimeMin )
321 if (fdRealTimeMax < dRealT) {
323 LOG(debug) << Form("New max Real time TS %12lu, Real time: %9.6f ms Old "
324 "Max %9.6f Diff %.9f",
325 static_cast<unsigned long>(fulTsCounter), dRealT, fdRealTimeMax, fdRealTimeMax - dRealT);
326 fdRealTimeMax = dRealT;
327 } // if( fdRealTimeMax < dRealT )
328 fulNbEvents += eventBuffer.size();
329 fulNbEventsSinceLastPrintout += eventBuffer.size();
330
331 if (0 == fulTsCounter % 10000) {
332 fdRealTime /= 10000;
333 LOG(info) << Form("Processed %12lu TS, Real time: %6.3f ms/TS (Min %6.3f, "
334 "Max %6.3f), Events: %12lu (%9lu since last print)",
335 static_cast<unsigned long>(fulTsCounter), fdRealTime, fdRealTimeMin, fdRealTimeMax,
336 static_cast<unsigned long>(fulNbEvents),
337 static_cast<unsigned long>(fulNbEventsSinceLastPrintout));
338 fdRealTime = 0.0;
339 fdRealTimeMin = 1e6;
340 fdRealTimeMax = 0.0;
342 } // if( 0 == fulTsCounter % 10000 )
343 fulTsCounter++;
344
345 return kTRUE;
346}
347
349
351{
352 if (NULL != fpBinDumpFile) {
353 LOG(info) << "Closing binary file used for event dump.";
354 fpBinDumpFile->close();
355 } // if( NULL != fpBinDumpFile )
356
358 if (kTRUE == fbMonitorMode) { SaveHistograms(); } // if( kTRUE == fbMonitorMode )
359}
360
362{
364 std::vector<std::pair<TNamed*, std::string>> vHistos = fEventBuilderAlgo->GetHistoVector();
365 std::vector<std::pair<TCanvas*, std::string>> vCanvas = fEventBuilderAlgo->GetCanvasVector();
366
368 TDirectory* oldDir = NULL;
369 TFile* histoFile = NULL;
370 // Store current directory position to allow restore later
371 oldDir = gDirectory;
372 // open separate histo file in recreate mode
373 histoFile = new TFile(fsHistoFileName, "RECREATE");
374
375 if (nullptr == histoFile) return kFALSE;
376
378 for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
380 TString sFolder = vHistos[uHisto].second.data();
381 if (nullptr == gDirectory->Get(sFolder)) gDirectory->mkdir(sFolder);
382 gDirectory->cd(sFolder);
383
385 vHistos[uHisto].first->Write();
386
387 histoFile->cd();
388 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
389
390 for (UInt_t uCanvas = 0; uCanvas < vCanvas.size(); ++uCanvas) {
392 TString sFolder = vCanvas[uCanvas].second.data();
393 if (nullptr == gDirectory->Get(sFolder)) gDirectory->mkdir(sFolder);
394 gDirectory->cd(sFolder);
395
397 vCanvas[uCanvas].first->Write();
398
399 histoFile->cd();
400 } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
401
402 // Restore original directory position
403 oldDir->cd();
404 histoFile->Close();
405
406 return kTRUE;
407}
408
ClassImp(CbmConverterManager)
Bool_t bStarEtof2019EventBuilderResetHistos
int star_rhicf_write(unsigned int trg_word, void *dta, int bytes)
std::vector< std::pair< TNamed *, std::string > > GetHistoVector()
std::vector< std::pair< TCanvas *, std::string > > GetCanvasVector()
std::vector< CbmTofStarSubevent2019 > & GetEventBuffer()
void AddMsComponentToList(size_t component, UShort_t usDetectorId)
void SetPulserTotLimits(UInt_t uMin, UInt_t uMax)
Bool_t ProcessTs(const fles::Timeslice &ts)
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.