CbmRoot
CbmDeviceMonitorT0.cxx
Go to the documentation of this file.
1 
8 #include "CbmDeviceMonitorT0.h"
9 #include "CbmMQDefs.h"
10 
11 #include "CbmFlesCanvasTools.h"
13 
14 #include "StorableTimeslice.hpp"
15 
16 #include "BoostSerializer.h"
17 #include "FairMQLogger.h"
18 #include "FairMQProgOptions.h" // device->fConfig
19 #include "FairParGenericSet.h"
20 #include "RootSerializer.h"
21 
22 #include "TCanvas.h"
23 #include "TFile.h"
24 #include "TH1.h"
25 #include "TList.h"
26 #include "TNamed.h"
27 
28 #include <array>
29 #include <iomanip>
30 #include <string>
31 
32 #include <boost/archive/binary_iarchive.hpp>
33 #include <boost/serialization/utility.hpp>
34 
35 #include <stdexcept>
36 struct InitTaskError : std::runtime_error {
37  using std::runtime_error::runtime_error;
38 };
39 
40 using namespace std;
41 
43 
45  : fbIgnoreOverlapMs {false}
46  , fsChannelNameDataInput {"t0component"}
47  , fsChannelNameHistosInput {"histogram-in"}
48  , fsChannelNameHistosConfig {"histo-conf"}
49  , fsChannelNameCanvasConfig {"canvas-conf"}
50  , fuHistoryHistoSize {3600}
51  , fuMinTotPulser {185}
52  , fuMaxTotPulser {195}
53  , fuOffSpillCountLimit {1000}
54  , fuPublishFreqTs {100}
55  , fdMinPublishTime {0.5}
56  , fdMaxPublishTime {5.0}
57  , fsAllowedChannels {fsChannelNameDataInput}
58  , fParCList {nullptr}
59  , fulNumMessages {0}
60  , fulTsCounter {0}
61  , fLastPublishTime {std::chrono::system_clock::now()}
62  , fMonitorAlgo {new CbmMcbm2018MonitorAlgoT0()}
63  , fArrayHisto {}
64  , fvpsHistosFolder {}
65  , fvpsCanvasConfig {} {}
66 
69  LOG(info) << "Init options for CbmMqStarHistoServer.";
70  fbIgnoreOverlapMs = fConfig->GetValue<bool>("IgnOverMs");
71  fuHistoryHistoSize = fConfig->GetValue<uint32_t>("HistEvoSz");
72  fuMinTotPulser = fConfig->GetValue<uint32_t>("PulsTotMin");
73  fuMaxTotPulser = fConfig->GetValue<uint32_t>("PulsTotMax");
74  fuOffSpillCountLimit = fConfig->GetValue<uint32_t>("SpillThr");
75  fuPublishFreqTs = fConfig->GetValue<uint32_t>("PubFreqTs");
76  fdMinPublishTime = fConfig->GetValue<double_t>("PubTimeMin");
77  fdMaxPublishTime = fConfig->GetValue<double_t>("PubTimeMax");
78  fsChannelNameDataInput = fConfig->GetValue<std::string>("TsNameIn");
79  fsChannelNameHistosInput = fConfig->GetValue<std::string>("ChNameIn");
80  fsChannelNameHistosConfig = fConfig->GetValue<std::string>("ChNameHistCfg");
81  fsChannelNameCanvasConfig = fConfig->GetValue<std::string>("ChNameCanvCfg");
83 
84  LOG(info) << "Histograms publication frequency in TS: " << fuPublishFreqTs;
85  LOG(info) << "Histograms publication min. interval in s: "
87  LOG(info) << "Histograms publication max. interval in s: "
89 
90  // Get the information about created channels from the device
91  // Check if the defined channels from the topology (by name)
92  // are in the list of channels which are possible/allowed
93  // for the device
94  // The idea is to check at initilization if the devices are
95  // properly connected. For the time beeing this is done with a
96  // nameing convention. It is not avoided that someone sends other
97  // data on this channel.
98  //logger::SetLogLevel("INFO");
99 
100  int noChannel = fChannels.size();
101  LOG(info) << "Number of defined channels: " << noChannel;
102  for (auto const& entry : fChannels) {
103  LOG(info) << "Channel name: " << entry.first;
104  if (std::string::npos != entry.first.find(fsChannelNameDataInput)) {
105  if (!IsChannelNameAllowed(entry.first))
106  throw InitTaskError("Channel name does not match.");
107  OnData(entry.first, &CbmDeviceMonitorT0::HandleData);
108  } // if( entry.first.find( "ts" )
109  } // for( auto const &entry : fChannels )
110  InitContainers();
111 } catch (InitTaskError& e) {
112  LOG(error) << e.what();
113  // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
115 }
116 
117 bool CbmDeviceMonitorT0::IsChannelNameAllowed(std::string channelName) {
118  for (auto const& entry : fsAllowedChannels) {
119  std::size_t pos1 = channelName.find(entry);
120  if (pos1 != std::string::npos) {
121  const vector<std::string>::const_iterator pos =
122  std::find(fsAllowedChannels.begin(), fsAllowedChannels.end(), entry);
123  const vector<std::string>::size_type idx =
124  pos - fsAllowedChannels.begin();
125  LOG(info) << "Found " << entry << " in " << channelName;
126  LOG(info) << "Channel name " << channelName
127  << " found in list of allowed channel names at position "
128  << idx;
129  return true;
130  } // if (pos1!=std::string::npos)
131  } // for(auto const &entry : fsAllowedChannels)
132  LOG(info) << "Channel name " << channelName
133  << " not found in list of allowed channel names.";
134  LOG(error) << "Stop device.";
135  return false;
136 }
137 
139  LOG(info) << "Init parameter containers for CbmDeviceMonitorT0.";
140 
142 
143  for (int iparC = 0; iparC < fParCList->GetEntries(); iparC++) {
144  FairParGenericSet* tempObj = (FairParGenericSet*) (fParCList->At(iparC));
145  fParCList->Remove(tempObj);
146  std::string paramName {tempObj->GetName()};
147  // NewSimpleMessage creates a copy of the data and takes care of its destruction (after the transfer takes place).
148  // Should only be used for small data because of the cost of an additional copy
149 
150  // Her must come the proper Runid
151  std::string message = paramName + ",111";
152  LOG(info) << "Requesting parameter container " << paramName
153  << ", sending message: " << message;
154 
155  FairMQMessagePtr req(NewSimpleMessage(message));
156  FairMQMessagePtr rep(NewMessage());
157 
158  FairParGenericSet* newObj = nullptr;
159 
160  if (Send(req, "parameters") > 0) {
161  if (Receive(rep, "parameters") >= 0) {
162  if (rep->GetSize() != 0) {
163  CbmMQTMessage tmsg(rep->GetData(), rep->GetSize());
164  newObj =
165  static_cast<FairParGenericSet*>(tmsg.ReadObject(tmsg.GetClass()));
166  LOG(info) << "Received unpack parameter from the server:";
167  newObj->print();
168  } else {
169  LOG(error) << "Received empty reply. Parameter not available";
170  } // if (rep->GetSize() != 0)
171  } // if (Receive(rep, "parameters") >= 0)
172  } // if (Send(req, "parameters") > 0)
173  fParCList->AddAt(newObj, iparC);
174  delete tempObj;
175  } // for ( int iparC = 0; iparC < fParCList->GetEntries(); iparC++ )
176 
183  // fMonitorAlgo->AddMsComponentToList(0, 0x90);
184 
185  Bool_t initOK = fMonitorAlgo->InitContainers();
186 
187  // Bool_t initOK = fMonitorAlgo->ReInitContainers();
188 
191  initOK &= fMonitorAlgo->CreateHistograms();
192 
194  std::vector<std::pair<TNamed*, std::string>> vHistos =
197  std::vector<std::pair<TCanvas*, std::string>> vCanvases =
199 
204  for (UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto) {
205  // LOG(info) << "Registering " << vHistos[ uHisto ].first->GetName()
206  // << " in " << vHistos[ uHisto ].second.data()
207  // ;
208  fArrayHisto.Add(vHistos[uHisto].first);
209  std::pair<std::string, std::string> psHistoConfig(
210  vHistos[uHisto].first->GetName(), vHistos[uHisto].second);
211  fvpsHistosFolder.push_back(psHistoConfig);
212 
214  FairMQMessagePtr messageHist(NewMessage());
215  Serialize<BoostSerializer<std::pair<std::string, std::string>>>(
216  *messageHist, psHistoConfig);
217 
219  if (Send(messageHist, fsChannelNameHistosConfig) < 0) {
220  LOG(error) << "Problem sending histo config";
221  return false;
222  } // if( Send( messageHist, fsChannelNameHistosConfig ) < 0 )
223 
224  LOG(info) << "Config of hist " << psHistoConfig.first.data()
225  << " in folder " << psHistoConfig.second.data();
226  } // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
227 
231  for (UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv) {
232  // LOG(info) << "Registering " << vCanvases[ uCanv ].first->GetName()
233  // << " in " << vCanvases[ uCanv ].second.data();
234  std::string sCanvName = (vCanvases[uCanv].first)->GetName();
235  std::string sCanvConf = GenerateCanvasConfigString(vCanvases[uCanv].first);
236 
237  std::pair<std::string, std::string> psCanvConfig(sCanvName, sCanvConf);
238 
239  fvpsCanvasConfig.push_back(psCanvConfig);
240 
242  FairMQMessagePtr messageCan(NewMessage());
243  Serialize<BoostSerializer<std::pair<std::string, std::string>>>(
244  *messageCan, psCanvConfig);
245 
247  if (Send(messageCan, fsChannelNameCanvasConfig) < 0) {
248  LOG(error) << "Problem sending canvas config";
249  return false;
250  } // if( Send( messageCan, fsChannelNameCanvasConfig ) < 0 )
251 
252  LOG(info) << "Config string of Canvas " << psCanvConfig.first.data()
253  << " is " << psCanvConfig.second.data();
254  } // for( UInt_t uCanv = 0; uCanv < vCanvases.size(); ++uCanv )
255 
256  return initOK;
257 }
258 
259 
260 // handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
261 bool CbmDeviceMonitorT0::HandleData(FairMQMessagePtr& msg, int /*index*/) {
262  fulNumMessages++;
263  LOG(debug) << "Received message number " << fulNumMessages << " with size "
264  << msg->GetSize();
265 
266  if (0 == fulNumMessages % 10000)
267  LOG(info) << "Received " << fulNumMessages << " messages";
268 
269  std::string msgStr(static_cast<char*>(msg->GetData()), msg->GetSize());
270  std::istringstream iss(msgStr);
271  boost::archive::binary_iarchive inputArchive(iss);
272 
274  fles::StorableTimeslice component {0};
275  inputArchive >> component;
276 
278  DoUnpack(component, 0);
279 
283  std::chrono::system_clock::time_point currentTime =
284  std::chrono::system_clock::now();
285  std::chrono::duration<double_t> elapsedSeconds =
286  currentTime - fLastPublishTime;
287  if ((fdMaxPublishTime < elapsedSeconds.count())
289  && fdMinPublishTime < elapsedSeconds.count())) {
290  SendHistograms();
291  fLastPublishTime = std::chrono::system_clock::now();
292  } // if( ( fdMaxPublishTime < elapsedSeconds.count() ) || ( 0 == fulNumMessages % fuPublishFreqTs && fdMinPublishTime < elapsedSeconds.count() ) )
293 
294  return true;
295 }
296 
299  FairMQMessagePtr message(NewMessage());
300  Serialize<RootSerializer>(*message, &fArrayHisto);
301 
302  // test code to check if deserialization works
303  /*
304  TObject* tempObject = nullptr;
305  Deserialize<RootDeserializer>(*message, tempObject);
306 
307  if (TString(tempObject->ClassName()).EqualTo("TObjArray")) {
308  TObjArray* arrayHisto = static_cast<TObjArray*>(tempObject);
309  LOG(info) << "Array contains " << arrayHisto->GetEntriesFast()
310  << " entries";
311  for (Int_t i = 0; i < arrayHisto->GetEntriesFast(); i++) {
312  TObject* obj = arrayHisto->At(i);
313  LOG(info) << obj->GetName();
314  TH1* histogram = static_cast<TH1*>(obj);
315  LOG(info) << histogram->GetNbinsX();
316  }
317  }
318 */
319 
321  if (Send(message, fsChannelNameHistosInput) < 0) {
322  LOG(error) << "Problem sending data";
323  return false;
324  } // if( Send( message, fsChannelNameHistosInput ) < 0 )
325 
327  fMonitorAlgo->ResetHistograms(kFALSE);
328 
329  return true;
330 }
331 
332 
334 
335 
336 Bool_t CbmDeviceMonitorT0::DoUnpack(const fles::Timeslice& ts,
337  size_t /*component*/) {
338  fulTsCounter++;
339 
340  if (kFALSE == fbComponentsAddedToList) {
341  for (uint32_t uCompIdx = 0; uCompIdx < ts.num_components(); ++uCompIdx) {
342  if (kusSysId == ts.descriptor(uCompIdx, 0).sys_id) {
344  } // if( kusSysId == ts.descriptor( uCompIdx, 0 ).sys_id )
345  } // for( uint32_t uComp = 0; uComp < ts.num_components(); ++uComp )
346  fbComponentsAddedToList = kTRUE;
347  } // if( kFALSE == fbComponentsAddedToList )
348 
349  if (/* fbMonitorMode && */ bMcbm2018MonitorTaskT0ResetHistos) {
350  LOG(info) << "Reset T0 Monitor histos ";
353  } // if( fbMonitorMode && bMcbm2018MonitorTaskT0ResetHistos )
354 
355  if (kFALSE == fMonitorAlgo->ProcessTs(ts)) {
356  LOG(error) << "Failed processing TS " << ts.index()
357  << " in unpacker algorithm class";
358  return kTRUE;
359  } // if( kFALSE == fMonitorAlgo->ProcessTs( ts ) )
360 
363 
364  if (0 == fulTsCounter % 10000)
365  LOG(info) << "Processed " << fulTsCounter << " time slices";
366 
367  return kTRUE;
368 }
369 
CbmDeviceMonitorT0::fvpsHistosFolder
std::vector< std::pair< std::string, std::string > > fvpsHistosFolder
Vector of string pairs with ( HistoName, FolderPath ) to send to the histogram server.
Definition: CbmDeviceMonitorT0.h:74
CbmDeviceMonitorT0::SendHistograms
bool SendHistograms()
Definition: CbmDeviceMonitorT0.cxx:297
CbmDeviceMonitorT0::fsChannelNameHistosConfig
std::string fsChannelNameHistosConfig
Definition: CbmDeviceMonitorT0.h:47
CbmDeviceMonitorT0::fbComponentsAddedToList
Bool_t fbComponentsAddedToList
Ignore Overlap Ms: all fuOverlapMsNb MS at the end of timeslice.
Definition: CbmDeviceMonitorT0.h:42
CbmDeviceMonitorT0::IsChannelNameAllowed
bool IsChannelNameAllowed(std::string channelName)
Definition: CbmDeviceMonitorT0.cxx:117
CbmDeviceMonitorT0::fdMaxPublishTime
double_t fdMaxPublishTime
Definition: CbmDeviceMonitorT0.h:55
CbmStar2019Algo::GetCanvasVector
std::vector< std::pair< TCanvas *, std::string > > GetCanvasVector()
Definition: CbmStar2019Algo.h:91
CbmDeviceMonitorT0::fbIgnoreOverlapMs
Bool_t fbIgnoreOverlapMs
Control flags.
Definition: CbmDeviceMonitorT0.h:41
CbmStar2019Algo::GetHistoVector
std::vector< std::pair< TNamed *, std::string > > GetHistoVector()
Definition: CbmStar2019Algo.h:84
CbmDeviceMonitorT0::fvpsCanvasConfig
std::vector< std::pair< std::string, std::string > > fvpsCanvasConfig
Definition: CbmDeviceMonitorT0.h:78
CbmDeviceMonitorT0::fParCList
TList * fParCList
Parameters management.
Definition: CbmDeviceMonitorT0.h:61
CbmDeviceMonitorT0::fsChannelNameCanvasConfig
std::string fsChannelNameCanvasConfig
Definition: CbmDeviceMonitorT0.h:48
InitTaskError
CBM headers.
Definition: CbmDeviceEventBuilderEtofStar2019.cxx:36
CbmDeviceMonitorT0::fsChannelNameDataInput
std::string fsChannelNameDataInput
User settings parameters.
Definition: CbmDeviceMonitorT0.h:45
CbmDeviceMonitorT0::fulTsCounter
uint64_t fulTsCounter
Definition: CbmDeviceMonitorT0.h:65
CbmFlesCanvasTools.h
CbmDeviceMonitorT0::InitContainers
Bool_t InitContainers()
Definition: CbmDeviceMonitorT0.cxx:138
CbmMcbm2018MonitorAlgoT0::SetSpillThreshold
void SetSpillThreshold(UInt_t uCntLimit)
Definition: CbmMcbm2018MonitorAlgoT0.h:72
CbmMcbm2018MonitorAlgoT0::ResetHistograms
Bool_t ResetHistograms(Bool_t bResetTime=kTRUE)
Definition: CbmMcbm2018MonitorAlgoT0.cxx:1339
CbmDeviceMonitorT0::fsChannelNameHistosInput
std::string fsChannelNameHistosInput
Definition: CbmDeviceMonitorT0.h:46
CbmDeviceMonitorT0::fuOffSpillCountLimit
uint32_t fuOffSpillCountLimit
Definition: CbmDeviceMonitorT0.h:52
CbmMQTMessage
Definition: CbmDeviceEventBuilderEtofStar2019.h:96
CbmDeviceMonitorT0::~CbmDeviceMonitorT0
virtual ~CbmDeviceMonitorT0()
Definition: CbmDeviceMonitorT0.cxx:333
CbmMcbm2018MonitorAlgoT0::SetHistoryHistoSize
void SetHistoryHistoSize(UInt_t inHistorySizeSec=1800)
Definition: CbmMcbm2018MonitorAlgoT0.h:65
cbm::mq::Transition::ErrorFound
@ ErrorFound
CbmDeviceMonitorT0::fMonitorAlgo
CbmMcbm2018MonitorAlgoT0 * fMonitorAlgo
Processing algo.
Definition: CbmDeviceMonitorT0.h:69
CbmDeviceMonitorT0::HandleData
bool HandleData(FairMQMessagePtr &, int)
Definition: CbmDeviceMonitorT0.cxx:261
CbmMcbm2018MonitorAlgoT0::InitContainers
Bool_t InitContainers()
Definition: CbmMcbm2018MonitorAlgoT0.cxx:61
CbmDeviceMonitorT0::fdMinPublishTime
double_t fdMinPublishTime
Definition: CbmDeviceMonitorT0.h:54
CbmDeviceMonitorT0::fuHistoryHistoSize
uint32_t fuHistoryHistoSize
Definition: CbmDeviceMonitorT0.h:49
CbmMcbm2018MonitorAlgoT0
Definition: CbmMcbm2018MonitorAlgoT0.h:35
CbmDeviceMonitorT0::fuMinTotPulser
uint32_t fuMinTotPulser
Definition: CbmDeviceMonitorT0.h:50
CbmMcbm2018MonitorAlgoT0::AddMsComponentToList
void AddMsComponentToList(size_t component, UShort_t usDetectorId)
Definition: CbmMcbm2018MonitorAlgoT0.cxx:130
CbmDeviceMonitorT0.h
CbmMcbm2018MonitorAlgoT0::ProcessTs
Bool_t ProcessTs(const fles::Timeslice &ts)
Definition: CbmMcbm2018MonitorAlgoT0.cxx:145
first
bool first
Definition: LKFMinuit.cxx:143
CbmDeviceMonitorT0::InitTask
virtual void InitTask()
Definition: CbmDeviceMonitorT0.cxx:67
CbmDeviceMonitorT0::fArrayHisto
TObjArray fArrayHisto
Array of histograms to send to the histogram server.
Definition: CbmDeviceMonitorT0.h:72
CbmMcbm2018MonitorAlgoT0::SetPulserTotLimits
void SetPulserTotLimits(UInt_t uMin, UInt_t uMax)
Definition: CbmMcbm2018MonitorAlgoT0.h:68
CbmDeviceMonitorT0::fulNumMessages
uint64_t fulNumMessages
Statistics & first TS rejection.
Definition: CbmDeviceMonitorT0.h:64
GenerateCanvasConfigString
std::string GenerateCanvasConfigString(TCanvas *pCanv)
Definition: CbmFlesCanvasTools.cxx:296
CbmDeviceMonitorT0::fuMaxTotPulser
uint32_t fuMaxTotPulser
Definition: CbmDeviceMonitorT0.h:51
CbmDeviceMonitorT0::Finish
void Finish()
Definition: CbmDeviceMonitorT0.cxx:370
CbmDeviceMonitorT0::CbmDeviceMonitorT0
CbmDeviceMonitorT0()
Definition: CbmDeviceMonitorT0.cxx:44
pos
TVector3 pos
Definition: CbmMvdSensorDigiToHitTask.cxx:60
CbmDeviceMonitorT0::fLastPublishTime
std::chrono::system_clock::time_point fLastPublishTime
Definition: CbmDeviceMonitorT0.h:66
CbmDeviceMonitorT0::fuPublishFreqTs
uint32_t fuPublishFreqTs
Definition: CbmDeviceMonitorT0.h:53
CbmDeviceMonitorT0::kusSysId
static const uint16_t kusSysId
Constants.
Definition: CbmDeviceMonitorT0.h:37
CbmMcbm2018MonitorAlgoT0::SetMonitorMode
void SetMonitorMode(Bool_t bFlagIn=kTRUE)
Definition: CbmMcbm2018MonitorAlgoT0.h:62
CbmMQDefs.h
bMcbm2018MonitorTaskT0ResetHistos
Bool_t bMcbm2018MonitorTaskT0ResetHistos
Definition: CbmDeviceMonitorT0.cxx:42
cbm::mq::ChangeState
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition: CbmMQDefs.h:19
CbmStar2019Algo::ClearVector
void ClearVector()
For unpacker algos.
Definition: CbmStar2019Algo.h:96
CbmMcbm2018MonitorAlgoT0::GetParList
TList * GetParList()
Definition: CbmMcbm2018MonitorAlgoT0.cxx:78
CbmMcbm2018MonitorAlgoT0::CreateHistograms
Bool_t CreateHistograms()
Definition: CbmMcbm2018MonitorAlgoT0.cxx:753
CbmDeviceMonitorT0::fsAllowedChannels
std::vector< std::string > fsAllowedChannels
List of MQ channels names.
Definition: CbmDeviceMonitorT0.h:58
CbmDeviceMonitorT0::DoUnpack
Bool_t DoUnpack(const fles::Timeslice &ts, size_t component)
Definition: CbmDeviceMonitorT0.cxx:336
CbmStar2019Algo::SetIgnoreOverlapMs
void SetIgnoreOverlapMs(Bool_t bFlagIn=kTRUE)
Control flags.
Definition: CbmStar2019Algo.h:102
CbmMcbm2018MonitorAlgoT0.h