CbmRoot
CbmMCPointSource.h
Go to the documentation of this file.
1 
8 #ifndef CBMMCPOINTSOURCE_H_
9 #define CBMMCPOINTSOURCE_H_
10 
11 #include "FairMQDevice.h"
12 
13 #include "CbmMQChannels.h"
14 
15 #include "TClonesArray.h"
16 
17 #include <ctime>
18 #include <string>
19 #include <vector>
20 
21 #include <boost/archive/binary_oarchive.hpp>
22 // include this header to serialize vectors
23 #include <boost/serialization/vector.hpp>
24 
25 class CbmMCPoint;
26 class FairRootManager;
27 class TClonesArray;
28 
29 class CbmMCPointSource : public FairMQDevice {
30 public:
31  CbmMCPointSource() = default;
32  virtual ~CbmMCPointSource();
33 
34 protected:
35  uint64_t fMaxEvents {0};
36 
37  std::string fFileName {""};
38  std::vector<std::string> fInputFileList {};
39  uint64_t fFileCounter {};
40 
41  uint64_t fEventNumber {0};
42  uint64_t fEventCounter {0};
43  uint64_t fMessageCounter {0};
44 
45  int fMaxMemory {0};
46 
47  virtual void InitTask();
48  virtual bool ConditionalRun();
49 
50 private:
51  bool SendData();
52  void CalcRuntime();
53  void ConnectChannelIfNeeded(int, std::string, std::string, FairRootManager*);
54 
55  template<class T>
56  void PrintMCPoint(TClonesArray* arr) {
57 
58  Int_t entries = arr->GetEntriesFast();
59  if (entries > 0) {
60  T* point = static_cast<T*>(arr->At(0));
61  LOG(info) << "Entries in TCA for data type " << point->GetName() << ": "
62  << entries;
63  }
64  for (int i = 0; i < entries; ++i) {
65  T* point = static_cast<T*>(arr->At(i));
66  point->Print("");
67  }
68  }
69 
70  template<class T>
71  std::vector<T> Convert(TClonesArray* arr) {
72 
73  std::vector<T> vec;
74  Int_t entries = arr->GetEntriesFast();
75  if (entries > 0) {
76  T* point = static_cast<T*>(arr->At(0));
77  LOG(info) << "Entries in TCA for data type " << point->GetName() << ": "
78  << entries;
79  }
80  for (int i = 0; i < entries; ++i) {
81  T* point = static_cast<T*>(arr->At(i));
82  vec.emplace_back(*point);
83  }
84  return vec;
85  }
86 
87 
88  template<class T>
89  bool ConvertAndSend(TClonesArray* arr, int i) {
90 
91  std::vector<T> vec;
92  Int_t entries = arr->GetEntriesFast();
93  if (entries > 0) {
94  T* point = static_cast<T*>(arr->At(0));
95  LOG(info) << "Entries in TCA for data type " << point->GetName() << ": "
96  << entries;
97  }
98  for (int iEntries = 0; iEntries < entries; ++iEntries) {
99  T* point = static_cast<T*>(arr->At(iEntries));
100  vec.emplace_back(*point);
101  }
102 
103 
104  std::stringstream oss;
105  boost::archive::binary_oarchive oa(oss);
106  oa << vec;
107  std::string* strMsg = new std::string(oss.str());
108 
109 
110  FairMQMessagePtr msg(NewMessage(
111  const_cast<char*>(strMsg->c_str()), // data
112  strMsg->length(), // size
113  [](void* /*data*/, void* object) {
114  delete static_cast<std::string*>(object);
115  },
116  strMsg)); // object that manages the data
117 
118  // TODO: Implement sending same data to more than one channel
119  // Need to create new message (copy message??)
120  if (fComponentsToSend.at(i) > 1) {
121  LOG(info) << "Need to copy FairMessage";
122  }
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",
140  "StsPoint",
141  "RichPoint",
142  "MuchPoint",
143  "Trdpoint",
144  "TofPoint",
145  "PsdPoint"};
146 
147  /*
148  std::vector<std::string> fAllowedChannels
149  = {"MvdPoint", "StsPoint", "RichPoint", "MuchPoint",
150  "Trdpoint", "TofPoint", "EcalPoint", "PsdPoint"};
151 */
152 
154 
155  std::vector<int> fComponentsToSend {};
156  std::vector<std::vector<std::string>> fChannelsToSend {{}};
157  std::vector<TClonesArray*> fArrays {fAllowedChannels.size(), nullptr};
158 };
159 
160 #endif /* CBMMCPOINTSOURCE_H_ */
CbmMCPointSource::fFileName
std::string fFileName
Definition: CbmMCPointSource.h:37
CbmMQChannels.h
CbmMCPointSource::ConvertAndSend
bool ConvertAndSend(TClonesArray *arr, int i)
Definition: CbmMCPointSource.h:89
CbmMCPointSource::fFileCounter
uint64_t fFileCounter
Definition: CbmMCPointSource.h:39
CbmMCPointSource::fTime
std::chrono::steady_clock::time_point fTime
Definition: CbmMCPointSource.h:137
CbmMCPointSource::CbmMCPointSource
CbmMCPointSource()=default
CbmMCPointSource::fAllowedChannels
std::vector< std::string > fAllowedChannels
Definition: CbmMCPointSource.h:139
CbmMCPointSource::fInputFileList
std::vector< std::string > fInputFileList
List of input files.
Definition: CbmMCPointSource.h:38
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmMCPointSource::fEventCounter
uint64_t fEventCounter
Definition: CbmMCPointSource.h:42
CbmMCPointSource::~CbmMCPointSource
virtual ~CbmMCPointSource()
Definition: CbmMCPointSource.cxx:228
CbmMCPointSource::PrintMCPoint
void PrintMCPoint(TClonesArray *arr)
Definition: CbmMCPointSource.h:56
CbmMCPointSource::fMaxEvents
uint64_t fMaxEvents
Definition: CbmMCPointSource.h:35
CbmMCPointSource::fChannelsToSend
std::vector< std::vector< std::string > > fChannelsToSend
Definition: CbmMCPointSource.h:156
CbmMCPointSource::CalcRuntime
void CalcRuntime()
Definition: CbmMCPointSource.cxx:230
CbmMCPointSource::fChan
CbmMQChannels fChan
Definition: CbmMCPointSource.h:153
CbmMCPointSource::InitTask
virtual void InitTask()
Definition: CbmMCPointSource.cxx:46
CbmMCPointSource::Convert
std::vector< T > Convert(TClonesArray *arr)
Definition: CbmMCPointSource.h:71
CbmMCPointSource::fComponentsToSend
std::vector< int > fComponentsToSend
Definition: CbmMCPointSource.h:155
CbmMCPointSource::ConnectChannelIfNeeded
void ConnectChannelIfNeeded(int, std::string, std::string, FairRootManager *)
Definition: CbmMCPointSource.cxx:142
CbmMCPointSource
Definition: CbmMCPointSource.h:29
CbmMCPointSource::SendData
bool SendData()
CbmMCPointSource::ConditionalRun
virtual bool ConditionalRun()
Definition: CbmMCPointSource.cxx:161
CbmMCPointSource::fEventNumber
uint64_t fEventNumber
Definition: CbmMCPointSource.h:41
CbmMCPointSource::fMessageCounter
uint64_t fMessageCounter
Definition: CbmMCPointSource.h:43
CbmMCPointSource::fArrays
std::vector< TClonesArray * > fArrays
Definition: CbmMCPointSource.h:157
CbmMCPointSource::fMaxMemory
int fMaxMemory
Definition: CbmMCPointSource.h:45
CbmMQChannels
Definition: CbmMQChannels.h:9