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