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