CbmRoot
CbmMcbm2018Source.cxx
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 // ----- -----
3 // ----- CbmMcbm2018Source -----
4 // ----- Created 19.01.2018 by P.-A. Loizeau -----
5 // ----- -----
6 // -----------------------------------------------------------------------------
7 
8 #include "CbmMcbm2018Source.h"
9 
10 #include "CbmMcbmUnpack.h"
11 #include "TimesliceMetaData.h"
12 
13 #include "MicrosliceContents.hpp"
14 #include "Timeslice.hpp"
15 #include "TimesliceInputArchive.hpp"
16 #include "TimesliceMultiInputArchive.hpp"
17 #include "TimesliceMultiSubscriber.hpp"
18 #include "TimesliceSubscriber.hpp"
19 
20 #include "FairLogger.h"
21 #include "FairRunOnline.h"
22 
23 #include "TClonesArray.h"
24 #include "TH1.h"
25 #include "THttpServer.h"
26 #include "TProfile.h"
27 
28 #include <fstream>
29 #include <iomanip>
30 #include <iostream>
31 
33  : FairSource()
34  , fFileName("")
35  , fDirName("")
36  , fInputFileList(new TObjString())
37  , fHost("localhost")
38  , fUnpackers()
39  , fUnpackersToRun()
40  , fTSNumber(0)
41  , fTSCounter(0)
42  , fTimer()
43  , fHistoMissedTS(nullptr)
44  , fHistoMissedTSEvo(nullptr)
45  , fNofTSSinceLastTS(0)
46  , fuTsReduction(1)
47  , fSource(nullptr)
48  , fuSubscriberHwm(1)
49  , fbWriteOutput(kFALSE)
50  , fTimeSliceMetaDataArray(nullptr) {}
51 
53 
55  if (0 == fFileName.Length() && 0 == fInputFileList.GetSize()) {
56  // Create a ";" separated string with all host/port combinations
57  fInputFileList.Add(new TObjString(fHost));
58  std::string fileList {""};
59  for (const auto&& obj : fInputFileList) {
60  std::string fileName = dynamic_cast<TObjString*>(obj)->GetString().Data();
61  fileList += fileName;
62  fileList += ";";
63  }
64  fileList.pop_back(); // Remove the last ;
65  fSource.reset(
66  new fles::TimesliceMultiSubscriber(fileList, fuSubscriberHwm));
67 
68  if (!fSource) { LOG(fatal) << "Could not connect to publisher."; }
69  } else {
70  // Create a ";" separated string with all file names
71  std::string fileList {""};
72  for (const auto&& obj : fInputFileList) {
73  std::string fileName = dynamic_cast<TObjString*>(obj)->GetString().Data();
74  fileList += fileName;
75  fileList += ";";
76  }
77  fileList.pop_back(); // Remove the last ;
78  LOG(info) << "Input File String: " << fileList;
79  if (fDirName.Length() > 0) {
80  fSource.reset(
81  new fles::TimesliceMultiInputArchive(fileList, fDirName.Data()));
82  } else {
83  fSource.reset(new fles::TimesliceMultiInputArchive(fileList));
84  }
85  }
86 
88  for (auto it = fUnpackers.begin(); it != fUnpackers.end(); ++it)
89  fUnpackersToRun.insert(it->second);
90 
91  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
92  ++itUnp) {
93  LOG(info) << "Initialize " << (*itUnp)->GetName();
94  (*itUnp)->Init();
95  }
96 
97  THttpServer* server = FairRunOnline::Instance()->GetHttpServer();
98 
99  fHistoMissedTS = new TH1I("Missed_TS", "Missed TS", 2, 0., 2.);
100  fHistoMissedTSEvo = new TProfile(
101  "Missed_TS_Evo", "Missed TS evolution; TS Idx []", 100000, 0., 10000000.);
102 
103  if (server) {
104  server->Register("/Fles", fHistoMissedTS);
105  server->Register("/Fles", fHistoMissedTSEvo);
106  } // if (server)
107 
109  FairRootManager* ioman = FairRootManager::Instance();
110  if (NULL == ioman) { LOG(fatal) << "No FairRootManager instance"; }
111  fTimeSliceMetaDataArray = new TClonesArray("TimesliceMetaData", 10);
112  if (NULL == fTimeSliceMetaDataArray) {
113  LOG(fatal) << "Failed creating the TS meta data TClonesarray ";
114  }
115  ioman->Register("TimesliceMetaData",
116  "TS Meta Data",
118  fbWriteOutput);
119 
120  return kTRUE;
121 }
122 
124  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
125  ++itUnp) {
126  LOG(info) << "Set parameter container " << (*itUnp)->GetName();
127  (*itUnp)->SetParContainers();
128  }
129 }
130 
132  Bool_t result = kTRUE;
133  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
134  ++itUnp) {
135  LOG(info) << "Initialize parameter container " << (*itUnp)->GetName();
136  result = result && (*itUnp)->InitContainers();
137  }
138  return result;
139 }
140 
142  Bool_t result = kTRUE;
143  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
144  ++itUnp) {
145  LOG(info) << "Initialize parameter container " << (*itUnp)->GetName();
146  result = result && (*itUnp)->ReInitContainers();
147  }
148  return result;
149 }
150 
152  Int_t retVal = FillBuffer();
153 
154  if (1 == retVal) { LOG(info) << "No more input"; }
155 
156  return retVal; // no more data; trigger end of run
157 }
158 
160  const fles::MicrosliceDescriptor& mdsc) {
161  LOG(info) << "Header ID: Ox" << std::hex << static_cast<int>(mdsc.hdr_id)
162  << std::dec;
163  LOG(info) << "Header version: Ox" << std::hex
164  << static_cast<int>(mdsc.hdr_ver) << std::dec;
165  LOG(info) << "Equipement ID: " << mdsc.eq_id;
166  LOG(info) << "Flags: " << mdsc.flags;
167  LOG(info) << "Sys ID: Ox" << std::hex << static_cast<int>(mdsc.sys_id)
168  << std::dec;
169  LOG(info) << "Sys version: Ox" << std::hex << static_cast<int>(mdsc.sys_ver)
170  << std::dec;
171  LOG(info) << "Microslice Idx: " << mdsc.idx;
172  LOG(info) << "Checksum: " << mdsc.crc;
173  LOG(info) << "Size: " << mdsc.size;
174  LOG(info) << "Offset: " << mdsc.offset;
175 }
176 
177 Bool_t CbmMcbm2018Source::CheckTimeslice(const fles::Timeslice& ts) {
178  if (0 == ts.num_components()) {
179  LOG(error) << "No Component in TS " << ts.index();
180  return 1;
181  }
182  LOG(info) << "Found " << ts.num_components()
183  << " different components in timeslice";
184  return kTRUE;
185 }
186 
188  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
189  ++itUnp) {
190  LOG(info) << "Finish " << (*itUnp)->GetName();
191  (*itUnp)->Finish();
192  }
193  /*
194  fHistoMissedTS->Write();
195  fHistoMissedTSEvo->Write();
196 */
197 }
198 
200  for (auto it = fUnpackers.begin(); it != fUnpackers.end(); ++it) {
201  it->second->Reset();
202  }
203  fTimeSliceMetaDataArray->Clear();
204 }
205 
207  while (auto timeslice = fSource->get()) {
208  fTSCounter++;
209  if (0 == fTSCounter % 10000) {
210  LOG(info) << "Analyse Event " << fTSCounter;
211  }
212 
213  const fles::Timeslice& ts = *timeslice;
214  auto tsIndex = ts.index();
215  if ((tsIndex != (fTSNumber + 1)) && (fTSNumber != 0)) {
216  LOG(debug) << "Missed Timeslices. Old TS Number was " << fTSNumber
217  << " New TS Number is " << tsIndex;
218  fHistoMissedTS->Fill(1, tsIndex - fTSNumber);
219  fHistoMissedTSEvo->Fill(tsIndex, 1, tsIndex - fTSNumber);
220  fNofTSSinceLastTS = tsIndex - fTSNumber;
221  } else {
222  fHistoMissedTS->Fill(0);
223  fHistoMissedTSEvo->Fill(tsIndex, 0, 1);
224  fNofTSSinceLastTS = 1;
225  }
226  fTSNumber = tsIndex;
227 
228  if (0 == fTSNumber % 1000) {
229  LOG(info) << "Reading Timeslice " << fTSNumber;
230  }
231 
232  if (1 == fTSCounter) {
233  for (size_t c {0}; c < ts.num_components(); c++) {
234  auto systemID = static_cast<int>(ts.descriptor(c, 0).sys_id);
235  LOG(info) << "Found systemID: " << std::hex << systemID << std::dec;
236 
238  auto it_list = fUnpackers.equal_range(systemID);
239  if (it_list.first == it_list.second) {
240  LOG(info) << "Could not find unpacker for system id 0x" << std::hex
241  << systemID << std::dec;
242  } else { // if( it == fUnpackers.end() )
243  for (auto it = it_list.first; it != it_list.second; ++it) {
244  it->second->AddMsComponentToList(c, systemID);
245  it->second->SetNbMsInTs(ts.num_core_microslices(),
246  ts.num_microslices(c)
247  - ts.num_core_microslices());
248  } // for( auto it = it_list.first; it != it_list.second; ++it )
249  } // else of if( it == fUnpackers.end() )
250  } // for (size_t c {0}; c < ts.num_components(); c++)
251  } // if( 1 == fTSCounter )
252 
254  if (0 == tsIndex % fuTsReduction) {
255  for (auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end();
256  ++itUnp) {
257  (*itUnp)->DoUnpack(ts, 0);
258  } // for( auto itUnp = fUnpackersToRun.begin(); itUnp != fUnpackersToRun.end(); ++ itUnp )
259  } // if( 0 == tsIndex % fuTsReduction )
260 
263  new ((*fTimeSliceMetaDataArray)[fTimeSliceMetaDataArray->GetEntriesFast()])
264  TimesliceMetaData(ts.descriptor(0, 0).idx);
265 
266  return 0;
267  }
268  return 1;
269 }
270 
CbmMcbm2018Source::Reset
void Reset()
Definition: CbmMcbm2018Source.cxx:199
CbmMcbm2018Source::fHost
TString fHost
Definition: CbmMcbm2018Source.h:86
CbmMcbm2018Source::fHistoMissedTS
TH1I * fHistoMissedTS
Definition: CbmMcbm2018Source.h:97
CbmMcbm2018Source::fTSCounter
UInt_t fTSCounter
Definition: CbmMcbm2018Source.h:94
CbmMcbmUnpack.h
TimesliceMetaData
Definition: TimesliceMetaData.h:11
CbmMcbm2018Source::Close
void Close()
Definition: CbmMcbm2018Source.cxx:187
CbmMcbm2018Source::fDirName
TString fDirName
Definition: CbmMcbm2018Source.h:84
CbmMcbm2018Source::ReInitUnpackers
virtual Bool_t ReInitUnpackers()
Definition: CbmMcbm2018Source.cxx:141
CbmMcbm2018Source::fSource
std::unique_ptr< fles::TimesliceSource > fSource
Definition: CbmMcbm2018Source.h:103
CbmMcbm2018Source::SetParUnpackers
virtual void SetParUnpackers()
Definition: CbmMcbm2018Source.cxx:123
CbmMcbm2018Source::fHistoMissedTSEvo
TProfile * fHistoMissedTSEvo
Definition: CbmMcbm2018Source.h:98
CbmMcbm2018Source::FillBuffer
Int_t FillBuffer()
Definition: CbmMcbm2018Source.cxx:206
CbmMcbm2018Source::~CbmMcbm2018Source
virtual ~CbmMcbm2018Source()
Definition: CbmMcbm2018Source.cxx:52
CbmMcbm2018Source::Init
Bool_t Init()
Definition: CbmMcbm2018Source.cxx:54
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMcbm2018Source::fuSubscriberHwm
UInt_t fuSubscriberHwm
Definition: CbmMcbm2018Source.h:105
CbmMcbm2018Source::fFileName
TString fFileName
Definition: CbmMcbm2018Source.h:83
CbmMcbm2018Source.h
CbmMcbm2018Source::ReadEvent
Int_t ReadEvent(UInt_t)
Definition: CbmMcbm2018Source.cxx:151
CbmMcbm2018Source::CheckTimeslice
Bool_t CheckTimeslice(const fles::Timeslice &ts)
Definition: CbmMcbm2018Source.cxx:177
CbmMcbm2018Source
Definition: CbmMcbm2018Source.h:34
CbmMcbm2018Source::fuTsReduction
UInt_t fuTsReduction
Definition: CbmMcbm2018Source.h:101
CbmMcbm2018Source::fTSNumber
UInt_t fTSNumber
List of all unpackers for which at least one matching container was found.
Definition: CbmMcbm2018Source.h:93
CbmMcbm2018Source::fTimeSliceMetaDataArray
TClonesArray * fTimeSliceMetaDataArray
If ON the output TClonesArray of meta-data is written to disk.
Definition: CbmMcbm2018Source.h:115
CbmMcbm2018Source::InitUnpackers
virtual Bool_t InitUnpackers()
Definition: CbmMcbm2018Source.cxx:131
CbmMcbm2018Source::fNofTSSinceLastTS
Int_t fNofTSSinceLastTS
Definition: CbmMcbm2018Source.h:99
CbmMcbm2018Source::fInputFileList
TList fInputFileList
List of input files.
Definition: CbmMcbm2018Source.h:85
CbmMcbm2018Source::fUnpackersToRun
std::unordered_set< CbmMcbmUnpack * > fUnpackersToRun
List pairs of system ID and unpacker pointer (unpacker can appear multiple times)
Definition: CbmMcbm2018Source.h:91
CbmMcbm2018Source::fbWriteOutput
Bool_t fbWriteOutput
Output ClonesArray.
Definition: CbmMcbm2018Source.h:114
TimesliceMetaData.h
CbmMcbm2018Source::PrintMicroSliceDescriptor
void PrintMicroSliceDescriptor(const fles::MicrosliceDescriptor &mdsc)
Definition: CbmMcbm2018Source.cxx:159
CbmMcbm2018Source::CbmMcbm2018Source
CbmMcbm2018Source()
Definition: CbmMcbm2018Source.cxx:32
CbmMcbm2018Source::fUnpackers
std::multimap< Int_t, CbmMcbmUnpack * > fUnpackers
Definition: CbmMcbm2018Source.h:89