BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnOnlineShmSource.cxx
Go to the documentation of this file.
1
#include "
BmnOnlineShmSource.h
"
2
3
#include "
nlohmann/json.hpp
"
4
5
#include <FairRootManager.h>
6
#include <
TBranchElement.h
>
7
#include <
TMessage.h
>
8
#include <
TSystem.h
>
9
#include <chrono>
10
#include <thread>
11
12
using
json
=
nlohmann::json
;
13
14
const
auto
configFilePath
=
gSystem
->ExpandPathName(
"$VMCWORKDIR/config/online-shm-config.json"
);
15
16
class
BmnTMessage
:
public
TMessage
17
{
18
public
:
19
BmnTMessage
(
void
* buf,
Int_t
len)
20
:
TMessage
(buf, len)
21
{
22
ResetBit
(
kIsOwner
);
23
}
24
};
25
26
BmnOnlineShmSource::BmnOnlineShmSource
()
27
:
FairOnlineSource
()
28
,
fIsRegisterBranches
(
kFALSE
)
29
{}
30
31
BmnOnlineShmSource::~BmnOnlineShmSource
() {}
32
33
Bool_t
BmnOnlineShmSource::Init
()
34
{
35
std::ifstream configFile(
configFilePath
);
36
if
(!configFile.is_open()) {
37
configFile.close();
38
LOG(fatal) <<
"BmnOnlineShmSource::Init(): File "
<<
configFilePath
<<
" not found!"
;
39
}
40
41
json
shmConfig;
42
configFile >> shmConfig;
43
configFile.close();
44
45
try
{
46
fShmName
= shmConfig[
"bmnShmOptions"
][
"shmName"
];
47
fWriteSemName
= shmConfig[
"bmnShmOptions"
][
"writeSemName"
];
48
fReadSemName
= shmConfig[
"bmnShmOptions"
][
"readSemName"
];
49
fMsgName
= shmConfig[
"bmnShmOptions"
][
"msgName"
];
50
}
catch
(
const
json::type_error& e) {
51
LOG(fatal) <<
"BmnOnlineShmSource::Init(): File "
<<
configFilePath
52
<<
" does not contain the required data or the calorimeter type is incorrectly set!"
;
53
}
54
55
while
(
kTRUE
) {
56
try
{
57
fShmSegment
= std::make_unique<bIpc::managed_shared_memory>(bIpc::open_only,
fShmName
.c_str());
58
59
fWriteSem
= std::make_unique<bIpc::named_semaphore>(bIpc::open_only,
fWriteSemName
.c_str());
60
fReadSem
= std::make_unique<bIpc::named_semaphore>(bIpc::open_only,
fReadSemName
.c_str());
61
62
break
;
63
}
catch
(
const
bIpc::interprocess_exception& e) {
64
LOG(error) <<
"BmnOnlineShmSource::Init(): Shared memory objects such as "
<<
fShmName
<<
", "
65
<<
fWriteSemName
<<
" and "
<<
fReadSemName
66
<<
" have not been created, check that BmnOnlineEDSink is running!"
;
67
}
68
69
std::this_thread::sleep_for(std::chrono::seconds(1));
70
}
71
72
return
kTRUE
;
73
}
74
75
void
BmnOnlineShmSource::RegisterBranches
(std::unique_ptr<TTree>& inputTree)
76
{
77
fRegisteredBranches
.clear();
78
79
auto
branches = inputTree->GetListOfBranches();
80
for
(
Long64_t
branchIndex = 0; branchIndex < branches->GetEntriesFast(); branchIndex++) {
81
auto
branch =
static_cast<
TBranchElement
*
>
(branches->At(branchIndex));
82
if
(strcmp(branch->GetClassName(),
"TClonesArray"
) == 0) {
83
fRegisteredBranches
[branch->GetName()] = std::make_unique<TClonesArray>(branch->GetClonesName());
84
}
85
}
86
87
for
(
auto
& branch :
fRegisteredBranches
) {
88
FairRootManager::Instance()->RegisterInputObject(branch.first.c_str(), branch.second.get());
89
}
90
}
91
92
void
BmnOnlineShmSource::UploadData
(std::unique_ptr<TTree>& inputTree)
93
{
94
for
(
auto
& branch :
fRegisteredBranches
) {
95
branch.second->Delete();
96
auto
tmp = branch.second.get();
97
if
(inputTree->SetBranchAddress(branch.first.c_str(), &tmp)) {
98
LOG(error) <<
"BmnOnlineShmSource::UploadData(): branch '"
<< branch.first <<
"' not found!"
;
99
}
100
}
101
102
inputTree->GetEntry(0);
103
}
104
105
Int_t
BmnOnlineShmSource::ReadEvent
(
UInt_t
)
106
{
107
fReadSem
->wait();
108
109
auto
msg =
fShmSegment
->find<
char
>(
fMsgName
.c_str());
110
111
auto
inputMessage = std::make_unique<BmnTMessage>(msg.first, msg.second);
112
auto
inputTree = std::unique_ptr<TTree>(
static_cast<
TTree
*
>
(inputMessage->ReadObjectAny(inputMessage->GetClass())));
113
114
if
(!
fIsRegisterBranches
) {
115
RegisterBranches
(inputTree);
116
fIsRegisterBranches
=
kTRUE
;
117
}
118
119
UploadData
(inputTree);
120
121
inputMessage.reset();
122
inputTree.reset();
123
fShmSegment
->destroy<
char
>(
fMsgName
.c_str());
124
fWriteSem
->post();
125
126
return
0;
127
}
128
129
void
BmnOnlineShmSource::Close
() {}
configFilePath
const auto configFilePath
Definition
BmnOnlineShmSource.cxx:14
json
nlohmann::json json
Definition
BmnOnlineShmSource.cxx:12
Bool_t
bool Bool_t
Int_t
int Int_t
UInt_t
unsigned int UInt_t
kFALSE
constexpr Bool_t kFALSE
Long64_t
long long Long64_t
kTRUE
constexpr Bool_t kTRUE
TBranchElement.h
TMessage.h
TSystem.h
gSystem
R__EXTERN TSystem * gSystem
BmnOnlineShmSource.h
BmnOnlineShmSource::ReadEvent
virtual Int_t ReadEvent(UInt_t=0)
Definition
BmnOnlineShmSource.cxx:105
BmnOnlineShmSource::UploadData
void UploadData(std::unique_ptr< TTree > &inputTree)
Definition
BmnOnlineShmSource.cxx:92
BmnOnlineShmSource::Init
virtual Bool_t Init()
Definition
BmnOnlineShmSource.cxx:33
BmnOnlineShmSource::Close
virtual void Close()
Definition
BmnOnlineShmSource.cxx:129
BmnOnlineShmSource::fRegisteredBranches
std::unordered_map< std::string, std::unique_ptr< TClonesArray > > fRegisteredBranches
Definition
BmnOnlineShmSource.h:39
BmnOnlineShmSource::RegisterBranches
void RegisterBranches(std::unique_ptr< TTree > &inputTree)
Definition
BmnOnlineShmSource.cxx:75
BmnOnlineShmSource::fMsgName
std::string fMsgName
Definition
BmnOnlineShmSource.h:33
BmnOnlineShmSource::~BmnOnlineShmSource
virtual ~BmnOnlineShmSource()
Definition
BmnOnlineShmSource.cxx:31
BmnOnlineShmSource::fIsRegisterBranches
Bool_t fIsRegisterBranches
Definition
BmnOnlineShmSource.h:28
BmnOnlineShmSource::fWriteSemName
std::string fWriteSemName
Definition
BmnOnlineShmSource.h:31
BmnOnlineShmSource::fShmSegment
std::unique_ptr< bIpc::managed_shared_memory > fShmSegment
Definition
BmnOnlineShmSource.h:35
BmnOnlineShmSource::fReadSemName
std::string fReadSemName
Definition
BmnOnlineShmSource.h:32
BmnOnlineShmSource::fReadSem
std::unique_ptr< bIpc::named_semaphore > fReadSem
Definition
BmnOnlineShmSource.h:37
BmnOnlineShmSource::fShmName
std::string fShmName
Definition
BmnOnlineShmSource.h:30
BmnOnlineShmSource::BmnOnlineShmSource
BmnOnlineShmSource()
Definition
BmnOnlineShmSource.cxx:26
BmnOnlineShmSource::fWriteSem
std::unique_ptr< bIpc::named_semaphore > fWriteSem
Definition
BmnOnlineShmSource.h:36
BmnTMessage::BmnTMessage
BmnTMessage(void *buf, Int_t len)
Definition
BmnOnlineShmSource.cxx:19
FairOnlineSource
TBranchElement
TBuffer::kIsOwner
kIsOwner
TMessage::TMessage
TMessage(UInt_t what=kMESS_ANY, Int_t bufsize=TBuffer::kInitialSize)
TObject::ResetBit
void ResetBit(UInt_t f)
TTree
json.hpp
nlohmann::json
basic_json<> json
default specialization
Definition
json.hpp:3365
base
source
BmnOnlineShmSource.cxx
Generated on
for BmnRoot by
1.17.0