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