18#include <FairLogger.h>
19#include <FairRootManager.h>
20#include <FairRunOnline.h>
26#include "ext_data_client.h"
29 const TString& NtupleOptions,
30 const TString& UcesbPath,
38 , fNtupleOptions(NtupleOptions)
39 , fUcesbPath(UcesbPath)
42 , fEventSize(event_size)
44 , fEventHeader(nullptr)
47 , fReaders(new TObjArray())
53 R3BLOG(debug1,
"R3BUcesbSource destructor.");
66 R3BLOG(debug,
"checking whether R3BEventHeader has been defined in FairRun");
67 auto run = FairRun::Instance();
68 auto eventHeader =
dynamic_cast<R3BEventHeader*
>(run->GetEventHeader());
71 R3BLOG(info,
"EventHeader. was defined properly");
76 run->SetEventHeader(eventHeader);
77 R3BLOG(warn,
"EventHeader. has been created from R3BEventHeader");
81 std::ostringstream command;
84 command << fUcesbPath <<
" " << fFileName <<
" " <<
"--ntuple=" << fNtupleOptions <<
",STRUCT,-";
86 if (fLastEventNo != -1)
88 command <<
" --max-events=" << fLastEventNo;
90 LOG(info) <<
"Calling ucesb with command: " << command.str();
93 fFd = popen(command.str().c_str(),
"r");
96 R3BLOG(fatal,
"popen() failed");
101 status = fClient.connect(fileno(fFd));
102 if (kFALSE == status)
104 R3BLOG(error,
"ext_data_clnt::connect() failed");
105 R3BLOG(fatal,
"ucesb error: " << fClient.last_error());
110 fInputFile.open(fInputFileName.Data(), std::fstream::in);
111 if (!fInputFile.is_open())
113 R3BLOG(warn,
"Input file for RunIds could not be open, it is Ok!");
117 R3BLOG(info,
"Input file for RunIds " << fInputFileName.Data() <<
" is open!");
119 fInputFile.seekg(0, std::ios::beg);
128 FairRootManager* frm = FairRootManager::Instance();
129 R3BLOG_IF(fatal, !frm,
"FairRootManager no found");
131 R3BLOG(info,
"Checking the register of R3BEventHeader");
132 fEventHeader =
dynamic_cast<R3BEventHeader*
>(frm->GetObject(
"EventHeader."));
135 R3BLOG(info,
"EventHeader. was defined properly");
139 R3BLOG(error,
"EventHeader. was not defined properly!");
143 for (
int i = 0; i < fReaders->GetEntriesFast(); ++i)
145 if (!(
dynamic_cast<R3BReader*
>(fReaders->At(i)))->Init(&fStructInfo))
147 R3BLOG(fatal,
"UCESB error: " << fClient.last_error());
154 uint32_t struct_map_success = 0;
155 Bool_t status = fClient.setup(NULL, 0, &fStructInfo, &struct_map_success, fEventSize);
159 R3BLOG(error,
"ext_data_clnt::setup() failed");
160 R3BLOG(fatal,
"UCESB error: " << fClient.last_error());
169 uint32_t map_ok = EXT_DATA_ITEM_MAP_OK | EXT_DATA_ITEM_MAP_NO_DEST;
170 if (struct_map_success & ~(map_ok))
172 R3BLOG(warn,
"ext_data_clnt::setup() failed to map all items:");
173 ext_data_struct_info_print_map_success(fStructInfo, stderr, map_ok);
177 if (fFullDataStructureValidation)
180 "ext_data_clnt::setup() mapping failure may "
181 "cause unexpected analysis results due to missing "
182 "data members. Unpacker needs fixing.");
187 "ext_data_clnt::setup() mapping failure may "
188 "cause unexpected analysis results due to missing "
189 "data members. Unpacker needs fixing.");
200 for (
int i = 0; i < fReaders->GetEntriesFast(); ++i)
202 (
dynamic_cast<R3BReader*
>(fReaders->At(i)))->SetParContainers();
209 for (
int i = 0; i < fReaders->GetEntriesFast(); ++i)
211 if (!(
dynamic_cast<R3BReader*
>(fReaders->At(i)))->ReInit())
213 R3BLOG(fatal,
"ReInit of a reader failed.");
221Int_t R3BUcesbSource::ReadIntFromString(
const std::string& wholestr,
const std::string& pattern)
223 std::string tempstr = wholestr;
224 tempstr.replace(0, tempstr.find(pattern) + pattern.length(),
"");
225 tempstr.replace(0, tempstr.find(
'=') + 1,
"");
226 return atoi(tempstr.c_str());
236 R3BLOG(debug1,
"ReadEvent " << fNEvent);
240 if (fNEvent > fEntryMax && fEntryMax != -1 && fInputFile.is_open())
243 R3BLOG(info,
"ReadEvent()");
248 getline(fInputFile, buffer);
249 LOG(info) <<
"read from file: \"" << buffer <<
"\"";
250 if (buffer.find(
"EVENT BEGIN") == 0)
252 fRunId = ReadIntFromString(buffer,
"RUNID");
253 fEventHeader->SetRunId(fRunId);
255 if (buffer.find(
"EVENT") == 0)
257 Int_t fInit = atoi(buffer.c_str());
258 buffer.erase(0, buffer.find(
' ') + 1);
259 fEntryMax = atoi(buffer.c_str());
261 }
while (fInputFile && buffer.compare(
"EVENT END"));
271 ret = fClient.fetch_event(fEvent, fEventSize);
274 LOG(info) <<
"End of input";
279 perror(
"ext_data_clnt::fetch_event()");
280 R3BLOG(error,
"ext_data_clnt::fetch_event() failed");
281 R3BLOG(fatal,
"UCESB error: " << fClient.last_error());
286 ret = fClient.get_raw_data(&raw, &raw_words);
289 perror(
"ext_data_clnt::get_raw_data()");
290 R3BLOG(fatal,
"Failed to get raw data.");
295 for (
int r = 0; r < fReaders->GetEntriesFast(); ++r)
299 LOG(debug1) <<
" Reading reader " << r <<
" (" << reader->GetName() <<
")";
307 const uint32_t* u =
reinterpret_cast<const uint32_t*
>(raw);
309 LOG(info) <<
" Raw data:";
310 for (w = 0; w < raw_words; w += 8)
312 printf(
" RAW%4x:", w);
313 for (j = 0; j < 8 && w + j < raw_words; j++)
314 printf(
" %08x", u[w + j]);
329 ret = fClient.close();
333 R3BLOG(fatal,
"ext_data_clnt::close() failed");
338 status = pclose(fFd);
342 R3BLOG(fatal,
"pclose() failed");
347 if (fInputFile.is_open())
353 for (
int i = 0; i < fReaders->GetEntriesFast(); ++i)
355 (
dynamic_cast<R3BReader*
>(fReaders->At(i)))->Reset();
372 feh->SetRunId(fRunId);
#define R3BLOG(severity, x)
#define R3BLOG_IF(severity, condition, x)
struct EXT_STR_h101_t EXT_STR_h101
virtual Bool_t R3BRead()=0
R3BUcesbSource(const TString &, const TString &, const TString &, EXT_STR_h101 *, size_t)
virtual Bool_t InitUnpackers()
virtual void SetParUnpackers()
virtual void FillEventHeader(FairEventHeader *feh)
virtual Bool_t ReInitUnpackers()
virtual ~R3BUcesbSource()
virtual Bool_t SpecifyRunId()