R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BUcesbSource.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include <iostream>
15#include <sstream>
16#include <string>
17
18#include <FairLogger.h>
19#include <FairRootManager.h>
20#include <FairRunOnline.h>
21
22#include "R3BEventHeader.h"
23#include "R3BLogger.h"
24#include "R3BUcesbSource.h"
25
26#include "ext_data_client.h"
27
28R3BUcesbSource::R3BUcesbSource(const TString& FileName,
29 const TString& NtupleOptions,
30 const TString& UcesbPath,
31 EXT_STR_h101* event,
32 size_t event_size)
33 : FairSource()
34 , fFd(nullptr)
35 , fClient()
36 , fStructInfo()
37 , fFileName(FileName)
38 , fNtupleOptions(NtupleOptions)
39 , fUcesbPath(UcesbPath)
40 , fNEvent(0)
41 , fEvent(event)
42 , fEventSize(event_size)
43 , fLastEventNo(-1)
44 , fEventHeader(nullptr)
45 , fInputFile()
46 , fEntryMax(0)
47 , fReaders(new TObjArray())
48{
49}
50
52{
53 R3BLOG(debug1, "R3BUcesbSource destructor.");
54 if (fReaders)
55 {
56 fReaders->Delete();
57 delete fReaders;
58 }
60}
61
63{
64 // Register of R3BEventHeader in the output root file
65
66 R3BLOG(debug, "checking whether R3BEventHeader has been defined in FairRun");
67 auto run = FairRun::Instance();
68 auto eventHeader = dynamic_cast<R3BEventHeader*>(run->GetEventHeader());
69 if (eventHeader)
70 {
71 R3BLOG(info, "EventHeader. was defined properly");
72 }
73 else
74 {
75 eventHeader = new R3BEventHeader();
76 run->SetEventHeader(eventHeader); // Implicit conversion and transfer ownership to FairRun
77 R3BLOG(warn, "EventHeader. has been created from R3BEventHeader");
78 }
79
80 Bool_t status;
81 std::ostringstream command;
82
83 /* Call ucesb with this command */
84 command << fUcesbPath << " " << fFileName << " " << "--ntuple=" << fNtupleOptions << ",STRUCT,-";
85
86 if (fLastEventNo != -1)
87 {
88 command << " --max-events=" << fLastEventNo;
89 }
90 LOG(info) << "Calling ucesb with command: " << command.str();
91
92 /* Fork off ucesb (calls fork() and pipe()) */
93 fFd = popen(command.str().c_str(), "r");
94 if (nullptr == fFd)
95 {
96 R3BLOG(fatal, "popen() failed");
97 return kFALSE;
98 }
99
100 /* Connect to forked instance */
101 status = fClient.connect(fileno(fFd));
102 if (kFALSE == status)
103 {
104 R3BLOG(error, "ext_data_clnt::connect() failed");
105 R3BLOG(fatal, "ucesb error: " << fClient.last_error());
106 return kFALSE;
107 }
108
109 // Open configuration file with runid values if needed in this step
110 fInputFile.open(fInputFileName.Data(), std::fstream::in);
111 if (!fInputFile.is_open())
112 {
113 R3BLOG(warn, "Input file for RunIds could not be open, it is Ok!");
114 }
115 else
116 {
117 R3BLOG(info, "Input file for RunIds " << fInputFileName.Data() << " is open!");
118 fInputFile.clear();
119 fInputFile.seekg(0, std::ios::beg);
120 }
121
122 return kTRUE;
123}
124
126{
127 // Register of R3BEventHeader in the output root file
128 FairRootManager* frm = FairRootManager::Instance();
129 R3BLOG_IF(fatal, !frm, "FairRootManager no found");
130
131 R3BLOG(info, "Checking the register of R3BEventHeader");
132 fEventHeader = dynamic_cast<R3BEventHeader*>(frm->GetObject("EventHeader."));
133 if (fEventHeader)
134 {
135 R3BLOG(info, "EventHeader. was defined properly");
136 }
137 else
138 {
139 R3BLOG(error, "EventHeader. was not defined properly!");
140 }
141
142 /* Initialize all readers */
143 for (int i = 0; i < fReaders->GetEntriesFast(); ++i)
144 {
145 if (!(dynamic_cast<R3BReader*>(fReaders->At(i)))->Init(&fStructInfo))
146 {
147 R3BLOG(fatal, "UCESB error: " << fClient.last_error());
148 return kFALSE;
149 }
150 }
151
152 /* Setup client */
153 /* this is the version for ucesb setup with extended mapping info */
154 uint32_t struct_map_success = 0;
155 Bool_t status = fClient.setup(NULL, 0, &fStructInfo, &struct_map_success, fEventSize);
156 if (status != 0)
157 {
158 // perror("ext_data_clnt::setup()");
159 R3BLOG(error, "ext_data_clnt::setup() failed");
160 R3BLOG(fatal, "UCESB error: " << fClient.last_error());
161 return kFALSE;
162 }
163 /*
164 * It is not needed, that *all* items are matched.
165 * However, mapping should fail, if items are requested that don't exist
166 * on the server, or if items are requested with wrong parameters.
167 * See ucesb/hbook/ext_data_client.h for more information.
168 */
169 uint32_t map_ok = EXT_DATA_ITEM_MAP_OK | EXT_DATA_ITEM_MAP_NO_DEST;
170 if (struct_map_success & ~(map_ok))
171 {
172 R3BLOG(warn, "ext_data_clnt::setup() failed to map all items:");
173 ext_data_struct_info_print_map_success(fStructInfo, stderr, map_ok);
174 /* FairRunOnline::Init() ignores the return value from
175 * GetSource()->InitUnpackers(); so do a FATAL error.
176 */
177 if (fFullDataStructureValidation)
178 {
179 R3BLOG(fatal,
180 "ext_data_clnt::setup() mapping failure may "
181 "cause unexpected analysis results due to missing "
182 "data members. Unpacker needs fixing.");
183 }
184 else
185 {
186 R3BLOG(error,
187 "ext_data_clnt::setup() mapping failure may "
188 "cause unexpected analysis results due to missing "
189 "data members. Unpacker needs fixing.");
190 }
191
192 return kFALSE;
193 }
194
195 return kTRUE;
196}
197
199{
200 for (int i = 0; i < fReaders->GetEntriesFast(); ++i)
201 {
202 (dynamic_cast<R3BReader*>(fReaders->At(i)))->SetParContainers();
203 }
204}
205
207{
208 /* Initialize all readers */
209 for (int i = 0; i < fReaders->GetEntriesFast(); ++i)
210 {
211 if (!(dynamic_cast<R3BReader*>(fReaders->At(i)))->ReInit())
212 {
213 R3BLOG(fatal, "ReInit of a reader failed.");
214 return kFALSE;
215 }
216 }
217
218 return kTRUE;
219}
220
221Int_t R3BUcesbSource::ReadIntFromString(const std::string& wholestr, const std::string& pattern)
222{
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());
227}
228
230{
231 const void* raw;
232 ssize_t raw_words;
233 int ret;
234 (void)i; /* Why is i not used? Outer loop seems not to use it. */
235
236 R3BLOG(debug1, "ReadEvent " << fNEvent);
237
238 fNEvent++;
239
240 if (fNEvent > fEntryMax && fEntryMax != -1 && fInputFile.is_open())
241 {
242
243 R3BLOG(info, "ReadEvent()");
244
245 std::string buffer;
246 do
247 {
248 getline(fInputFile, buffer);
249 LOG(info) << "read from file: \"" << buffer << "\"";
250 if (buffer.find("EVENT BEGIN") == 0)
251 {
252 fRunId = ReadIntFromString(buffer, "RUNID");
253 fEventHeader->SetRunId(fRunId);
254 }
255 if (buffer.find("EVENT") == 0)
256 continue;
257 Int_t fInit = atoi(buffer.c_str());
258 buffer.erase(0, buffer.find(' ') + 1);
259 fEntryMax = atoi(buffer.c_str());
260
261 } while (fInputFile && buffer.compare("EVENT END"));
262 }
263
264 /* Need to initialize first */
265 if (nullptr == fFd)
266 {
267 Init();
268 }
269
270 /* Fetch data */
271 ret = fClient.fetch_event(fEvent, fEventSize);
272 if (0 == ret)
273 {
274 LOG(info) << "End of input";
275 return 1;
276 }
277 if (-1 == ret)
278 {
279 perror("ext_data_clnt::fetch_event()");
280 R3BLOG(error, "ext_data_clnt::fetch_event() failed");
281 R3BLOG(fatal, "UCESB error: " << fClient.last_error());
282 return 0;
283 }
284
285 /* Get raw data, if any */
286 ret = fClient.get_raw_data(&raw, &raw_words);
287 if (0 != ret)
288 {
289 perror("ext_data_clnt::get_raw_data()");
290 R3BLOG(fatal, "Failed to get raw data.");
291 return 0;
292 }
293
294 /* Run detector specific readers */
295 for (int r = 0; r < fReaders->GetEntriesFast(); ++r)
296 {
297 R3BReader* reader = dynamic_cast<R3BReader*>(fReaders->At(r));
298
299 LOG(debug1) << " Reading reader " << r << " (" << reader->GetName() << ")";
300 reader->R3BRead();
301 }
302
303 /* Display raw data */
304 if (raw)
305 {
306 int w, j;
307 const uint32_t* u = reinterpret_cast<const uint32_t*>(raw);
308
309 LOG(info) << " Raw data:";
310 for (w = 0; w < raw_words; w += 8)
311 {
312 printf(" RAW%4x:", w);
313 for (j = 0; j < 8 && w + j < raw_words; j++)
314 printf(" %08x", u[w + j]);
315 printf("\n");
316 }
317 }
318
319 return 0;
320}
321
323{
324 int ret;
325
326 if (!fFd) // not open
327 return;
328 /* Close client connection */
329 ret = fClient.close();
330 if (0 != ret)
331 {
332 // perror("ext_data_clnt::close()");
333 R3BLOG(fatal, "ext_data_clnt::close() failed");
334 }
335
336 /* Close pipe */
337 int status;
338 status = pclose(fFd);
339 if (-1 == status)
340 {
341 // perror("pclose()");
342 R3BLOG(fatal, "pclose() failed");
343 abort();
344 }
345 fFd = nullptr;
346
347 if (fInputFile.is_open())
348 fInputFile.close();
349}
350
352{
353 for (int i = 0; i < fReaders->GetEntriesFast(); ++i)
354 {
355 (dynamic_cast<R3BReader*>(fReaders->At(i)))->Reset();
356 }
357}
358
360{
361 if (ReadEvent(0) == 0)
362 return true;
363 return false;
364}
365
366//_____________________________________________________________________________
367void R3BUcesbSource::FillEventHeader(FairEventHeader* feh)
368{
369 // note: as of 2023-02-09, FairRootManager will always pass us a pointer to
370 // FairEventHeader, never to R3BEventHeader. Investigation is under way.
371 // --Philipp
372 feh->SetRunId(fRunId); // we can still set the run ID, though.
373}
374
#define R3BLOG(severity, x)
Definition R3BLogger.h:35
#define R3BLOG_IF(severity, condition, x)
Definition R3BLogger.h:46
ClassImp(R3BUcesbSource)
struct EXT_STR_h101_t EXT_STR_h101
virtual Bool_t R3BRead()=0
Int_t ReadEvent(UInt_t)
R3BUcesbSource(const TString &, const TString &, const TString &, EXT_STR_h101 *, size_t)
virtual Bool_t Init()
virtual Bool_t InitUnpackers()
virtual void SetParUnpackers()
virtual void Reset()
virtual void FillEventHeader(FairEventHeader *feh)
virtual Bool_t ReInitUnpackers()
virtual ~R3BUcesbSource()
virtual Bool_t SpecifyRunId()
virtual void Close()