BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnOnlineShmSource.cxx
Go to the documentation of this file.
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
13
14const auto configFilePath = gSystem->ExpandPathName("$VMCWORKDIR/config/online-shm-config.json");
15
16class BmnTMessage : public TMessage
17{
18 public:
19 BmnTMessage(void* buf, Int_t len)
20 : TMessage(buf, len)
21 {
23 }
24};
25
30
32
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
75void 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
92void 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
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);
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
const auto configFilePath
nlohmann::json json
bool Bool_t
int Int_t
unsigned int UInt_t
constexpr Bool_t kFALSE
long long Long64_t
constexpr Bool_t kTRUE
R__EXTERN TSystem * gSystem
virtual Int_t ReadEvent(UInt_t=0)
void UploadData(std::unique_ptr< TTree > &inputTree)
std::unordered_map< std::string, std::unique_ptr< TClonesArray > > fRegisteredBranches
void RegisterBranches(std::unique_ptr< TTree > &inputTree)
std::unique_ptr< bIpc::managed_shared_memory > fShmSegment
std::unique_ptr< bIpc::named_semaphore > fReadSem
std::unique_ptr< bIpc::named_semaphore > fWriteSem
BmnTMessage(void *buf, Int_t len)
TMessage(UInt_t what=kMESS_ANY, Int_t bufsize=TBuffer::kInitialSize)
void ResetBit(UInt_t f)
basic_json<> json
default specialization
Definition json.hpp:3365