CbmRoot
Loading...
Searching...
No Matches
CbmMCPointSource.h
Go to the documentation of this file.
1/* Copyright (C) 2019-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer] */
4
12#ifndef CBMMCPOINTSOURCE_H_
13#define CBMMCPOINTSOURCE_H_
14
15#include "CbmMQChannels.h"
16
17#include "FairMQDevice.h"
18
19#include "TClonesArray.h"
20
21#include <boost/archive/binary_oarchive.hpp>
22
23#include <ctime>
24#include <string>
25#include <vector>
26// include this header to serialize vectors
27#include <boost/serialization/vector.hpp>
28
29class CbmMCPoint;
30class FairRootManager;
31class TClonesArray;
32
33class CbmMCPointSource : public FairMQDevice {
34public:
35 CbmMCPointSource() = default;
36 virtual ~CbmMCPointSource();
37
38protected:
39 uint64_t fMaxEvents {0};
40
41 std::string fFileName {""};
42 std::vector<std::string> fInputFileList {};
43 uint64_t fFileCounter {};
44
45 uint64_t fEventNumber {0};
46 uint64_t fEventCounter {0};
47 uint64_t fMessageCounter {0};
48
49 int fMaxMemory {0};
50
51 virtual void InitTask();
52 virtual bool ConditionalRun();
53
54private:
55 bool SendData();
56 void CalcRuntime();
57 void ConnectChannelIfNeeded(int, std::string, std::string, FairRootManager*);
58
59 template<class T>
60 void PrintMCPoint(TClonesArray* arr)
61 {
62
63 Int_t entries = arr->GetEntriesFast();
64 if (entries > 0) {
65 T* point = static_cast<T*>(arr->At(0));
66 LOG(info) << "Entries in TCA for data type " << point->GetName() << ": " << entries;
67 }
68 for (int i = 0; i < entries; ++i) {
69 T* point = static_cast<T*>(arr->At(i));
70 point->Print("");
71 }
72 }
73
74 template<class T>
75 std::vector<T> Convert(TClonesArray* arr)
76 {
77
78 std::vector<T> vec;
79 Int_t entries = arr->GetEntriesFast();
80 if (entries > 0) {
81 T* point = static_cast<T*>(arr->At(0));
82 LOG(info) << "Entries in TCA for data type " << point->GetName() << ": " << entries;
83 }
84 for (int i = 0; i < entries; ++i) {
85 T* point = static_cast<T*>(arr->At(i));
86 vec.emplace_back(*point);
87 }
88 return vec;
89 }
90
91
92 template<class T>
93 bool ConvertAndSend(TClonesArray* arr, int i)
94 {
95
96 std::vector<T> vec;
97 Int_t entries = arr->GetEntriesFast();
98 if (entries > 0) {
99 T* point = static_cast<T*>(arr->At(0));
100 LOG(info) << "Entries in TCA for data type " << point->GetName() << ": " << entries;
101 }
102 for (int iEntries = 0; iEntries < entries; ++iEntries) {
103 T* point = static_cast<T*>(arr->At(iEntries));
104 vec.emplace_back(*point);
105 }
106
107
108 std::stringstream oss;
109 boost::archive::binary_oarchive oa(oss);
110 oa << vec;
111 std::string* strMsg = new std::string(oss.str());
112
113
114 FairMQMessagePtr msg(NewMessage(
115 const_cast<char*>(strMsg->c_str()), // data
116 strMsg->length(), // size
117 [](void* /*data*/, void* object) { delete static_cast<std::string*>(object); },
118 strMsg)); // object that manages the data
119
120 // TODO: Implement sending same data to more than one channel
121 // Need to create new message (copy message??)
122 if (fComponentsToSend.at(i) > 1) { LOG(info) << "Need to copy FairMessage"; }
123
124 // in case of error or transfer interruption,
125 // return false to go to IDLE state
126 // successfull transfer will return number of bytes
127 // transfered (can be 0 if sending an empty message).
128 LOG(info) << "Send data to channel " << fChannelsToSend.at(i).at(0);
129 if (Send(msg, fChannelsToSend.at(i).at(0)) < 0) {
130 LOG(error) << "Problem sending data";
131 return false;
132 }
133
134 return true;
135 }
136
137 std::chrono::steady_clock::time_point fTime {};
138
139 std::vector<std::string> fAllowedChannels = {"MvdPoint", "StsPoint", "RichPoint", "MuchPoint",
140 "Trdpoint", "TofPoint", "PsdPoint"};
141
142 /*
143 std::vector<std::string> fAllowedChannels
144 = {"MvdPoint", "StsPoint", "RichPoint", "MuchPoint",
145 "Trdpoint", "TofPoint", "EcalPoint", "PsdPoint"};
146*/
147
149
150 std::vector<int> fComponentsToSend {};
151 std::vector<std::vector<std::string>> fChannelsToSend {{}};
152 std::vector<TClonesArray*> fArrays {fAllowedChannels.size(), nullptr};
153};
154
155#endif /* CBMMCPOINTSOURCE_H_ */
CbmMCPointSource()=default
CbmMQChannels fChan
virtual bool ConditionalRun()
void PrintMCPoint(TClonesArray *arr)
std::string fFileName
std::chrono::steady_clock::time_point fTime
bool ConvertAndSend(TClonesArray *arr, int i)
virtual void InitTask()
std::vector< T > Convert(TClonesArray *arr)
std::vector< std::string > fInputFileList
List of input files.
void ConnectChannelIfNeeded(int, std::string, std::string, FairRootManager *)
std::vector< int > fComponentsToSend
std::vector< TClonesArray * > fArrays
std::vector< std::vector< std::string > > fChannelsToSend
std::vector< std::string > fAllowedChannels