3#include <FairRootManager.h>
4#include <TBranchElement.h>
11#include "nlohmann/json.hpp"
15const auto configFilePath = gSystem->ExpandPathName(
"$VMCWORKDIR/config/online-shm-config.json");
19 BmnTMessage(
void *buf, Int_t len) : TMessage(buf, len) { ResetBit(kIsOwner); }
28 if (!configFile.is_open()) {
30 LOG(fatal) <<
"BmnOnlineShmSource::Init(): File " <<
configFilePath <<
" not found!";
34 configFile >> shmConfig;
38 fShmName = shmConfig[
"bmnShmOptions"][
"shmName"];
39 fWriteSemName = shmConfig[
"bmnShmOptions"][
"writeSemName"];
40 fReadSemName = shmConfig[
"bmnShmOptions"][
"readSemName"];
41 fMsgName = shmConfig[
"bmnShmOptions"][
"msgName"];
43 LOG(fatal) <<
"BmnOnlineShmSource::Init(): File " <<
configFilePath
44 <<
" does not contain the required data or the calorimeter type is incorrectly set!";
49 fShmSegment = std::make_unique<bIpc::managed_shared_memory>(bIpc::open_only, fShmName.c_str());
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());
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!";
61 std::this_thread::sleep_for(std::chrono::seconds(1));
67void BmnOnlineShmSource::RegisterBranches(std::unique_ptr<TTree> &inputTree) {
68 fRegisteredBranches.clear();
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());
78 for (
auto &branch : fRegisteredBranches) {
79 FairRootManager::Instance()->RegisterInputObject(branch.first.c_str(), branch.second.get());
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!";
92 inputTree->GetEntry(0);
98 auto msg = fShmSegment->find<
char>(fMsgName.c_str());
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())));
103 if (!fIsRegisterBranches) {
104 RegisterBranches(inputTree);
105 fIsRegisterBranches = kTRUE;
108 UploadData(inputTree);
110 inputMessage.reset();
112 fShmSegment->destroy<
char>(fMsgName.c_str());
const auto configFilePath
virtual Int_t ReadEvent(UInt_t=0)
virtual ~BmnOnlineShmSource()
BmnTMessage(void *buf, Int_t len)
a class to store JSON values
exception indicating executing a member function with a wrong type
basic_json<> json
default specialization