CbmRoot
Loading...
Searching...
No Matches
CbmDeviceStsLocalReco.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer] */
4
13
14#include "CbmBsField.h"
15#include "CbmFieldConst.h"
16#include "CbmFieldMap.h"
18#include "CbmFieldMapSym1.h"
19#include "CbmFieldMapSym2.h"
20#include "CbmFieldMapSym3.h"
21#include "CbmFieldPar.h"
22#include "CbmMQDefs.h"
23#include "CbmStsDigitizeParameters.h"
24
25#include "FairField.h"
26#include "FairGeoParSet.h"
27#include "FairMQLogger.h"
28#include "FairMQProgOptions.h" // device->fConfig
29#include "FairRunAna.h"
30
31//#include "FairParGenericSet.h"
32//#include "RootSerializer.h"
33
34#include "TGeoManager.h"
35#include "TSystem.h"
36
37/*
38#include "TCanvas.h"
39#include "TFile.h"
40#include "TH1.h"
41#include "TList.h"
42#include "TNamed.h"
43*/
44
45#include <boost/archive/binary_iarchive.hpp>
46
47#include <array>
48#include <iomanip>
49#include <stdexcept>
50#include <string>
51struct InitTaskError : std::runtime_error {
52 using std::runtime_error::runtime_error;
53};
54
55//using namespace std;
56using std::string;
57
59 : fMaxTimeslices {0}
60 , fNumMessages {0}
61 , fRunId {"0"}
62 , fvmcworkdir {""}
63 , fDigiPar {nullptr}
64 , fGeoPar {nullptr}
65 , fFieldPar {nullptr} // , fParCList{nullptr}
66{
67}
68
70{
71 if (gGeoManager) {
72 gGeoManager->GetListOfVolumes()->Delete();
73 gGeoManager->GetListOfShapes()->Delete();
74 }
75}
76
77
79try {
80 fMaxTimeslices = fConfig->GetValue<uint64_t>("max-timeslices");
81 // Get the information about created channels from the device
82 // Check if the defined channels from the topology (by name)
83 // are in the list of channels which are possible/allowed
84 // for the device
85 // The idea is to check at initilization if the devices are
86 // properly connected. For the time beeing this is done with a
87 // nameing convention. It is not avoided that someone sends other
88 // data on this channel.
89 //logger::SetLogLevel("INFO");
90
91 int noChannel = fChannels.size();
92 LOG(info) << "Number of defined channels: " << noChannel;
93 for (auto const& entry : fChannels) {
94 LOG(info) << "Channel name: " << entry.first;
95 if (!IsChannelNameAllowed(entry.first)) throw InitTaskError("Channel name does not match.");
96 OnData(entry.first, &CbmDeviceStsLocalReco::HandleData);
97 }
99}
100catch (InitTaskError& e) {
101 LOG(error) << e.what();
102 // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
104}
105
106
108{
109 for (auto const& entry : fAllowedChannels) {
110 std::size_t pos1 = channelName.find(entry);
111 if (pos1 != std::string::npos) {
112 const std::vector<std::string>::const_iterator pos =
113 std::find(fAllowedChannels.begin(), fAllowedChannels.end(), entry);
114 const std::vector<std::string>::size_type idx = pos - fAllowedChannels.begin();
115 LOG(info) << "Found " << entry << " in " << channelName;
116 LOG(info) << "Channel name " << channelName << " found in list of allowed channel names at position " << idx;
117 return true;
118 }
119 }
120 LOG(info) << "Channel name " << channelName << " not found in list of allowed channel names.";
121 LOG(error) << "Stop device.";
122 return false;
123}
124
126{
127 Bool_t initOK {kTRUE};
128
129
130 fRunId = fConfig->GetValue<string>("run-id");
131 fvmcworkdir = fConfig->GetValue<string>("vmcworkdir");
132 fMaxTimeslices = fConfig->GetValue<uint64_t>("max-timeslices");
133
134 LOG(info) << "Init parameter containers for CbmDeviceStsLocalReco.";
135
136 // NewSimpleMessage creates a copy of the data and takes care of its destruction (after the transfer takes place).
137 // Should only be used for small data because of the cost of an additional copy
138
139 std::string message {"CbmStsDigitizeParameters,"};
140 message += fRunId;
141 LOG(info) << "Requesting parameter container CbmStsDigitizeParameters, "
142 "sending message: "
143 << message;
144
145 FairMQMessagePtr req(NewSimpleMessage(message));
146 FairMQMessagePtr rep(NewMessage());
147
148 if (Send(req, "parameters") > 0) {
149 if (Receive(rep, "parameters") >= 0) {
150 if (rep->GetSize() != 0) {
151 CbmMqTMessage tmsg(rep->GetData(), rep->GetSize());
152 fDigiPar = dynamic_cast<CbmStsDigitizeParameters*>(tmsg.ReadObject(tmsg.GetClass()));
153 LOG(info) << "Received unpack parameter from parmq server: " << fDigiPar;
154 // TODO: check if fDigiPar is properly initialized from the file
155 fDigiPar->Print();
156 LOG(info) << fDigiPar->ToString();
157 }
158 else {
159 throw InitTaskError("Received empty reply. Parameter not available");
160 }
161 }
162 }
163
164 std::string message1 {"FairGeoParSet,"};
165 message1 += fRunId;
166 LOG(info) << "Requesting parameter container FairGeoParSet, sending message: " << message1;
167
168 FairMQMessagePtr req1(NewSimpleMessage(message1));
169 FairMQMessagePtr rep1(NewMessage());
170
171 if (Send(req1, "parameters") > 0) {
172 if (Receive(rep1, "parameters") >= 0) {
173 if (rep1->GetSize() != 0) {
174 CbmMqTMessage tmsg(rep1->GetData(), rep1->GetSize());
175 fGeoPar = static_cast<FairGeoParSet*>(tmsg.ReadObject(tmsg.GetClass()));
176 LOG(info) << "Received unpack parameter from parmq server: " << fGeoPar;
177 fGeoPar->Print();
178 if (!gGeoManager) { throw InitTaskError("No gGeoManager found in FairGeoParSet"); }
179 else {
180 gGeoManager->Print();
181 }
182 }
183 else {
184 throw InitTaskError("Received empty reply. Parameter not available");
185 }
186 }
187 }
188
189 std::string message2 {"CbmFieldPar,"};
190 message2 += fRunId;
191 LOG(info) << "Requesting parameter container CbmFieldPar, sending message: " << message2;
192
193 FairMQMessagePtr req2(NewSimpleMessage(message2));
194 FairMQMessagePtr rep2(NewMessage());
195
196 if (Send(req2, "parameters") > 0) {
197 if (Receive(rep2, "parameters") >= 0) {
198 if (rep2->GetSize() != 0) {
199 CbmMqTMessage tmsg(rep2->GetData(), rep2->GetSize());
200 fFieldPar = static_cast<CbmFieldPar*>(tmsg.ReadObject(tmsg.GetClass()));
201 LOG(info) << "Received unpack parameter from parmq server: " << fGeoPar;
202 if (-1 == fFieldPar->GetType()) { throw InitTaskError("No field parameters available!"); }
203 else {
204 fFieldPar->Print();
205 LOG(info) << "Before creating the field";
206 FairField* field = createField();
207 LOG(info) << "After creating the field";
208 FairRunAna* run = new FairRunAna();
209 run->SetField(field);
210 }
211 }
212 else {
213 LOG(error) << "Received empty reply. Parameter not available";
214 }
215 }
216 }
217
218 return initOK;
219 return true;
220}
221
222
224{
225 FairField* fMagneticField {nullptr};
226
227 // Instantiate correct field type
228 Int_t fType = fFieldPar->GetType();
229 gSystem->Setenv("VMCWORKDIR", fvmcworkdir.c_str());
230 if (fType == 0) fMagneticField = new CbmFieldConst(fFieldPar);
231 else if (fType == 1)
232 fMagneticField = new CbmFieldMap(fFieldPar);
233 else if (fType == 2)
234 fMagneticField = new CbmFieldMapSym2(fFieldPar);
235 else if (fType == 3)
236 fMagneticField = new CbmFieldMapSym3(fFieldPar);
237 else if (fType == 4)
238 fMagneticField = new CbmFieldMapDistorted(fFieldPar);
239 else if (fType == 5)
240 fMagneticField = new CbmFieldMapSym1(fFieldPar);
241 else if (fType == 6)
242 fMagneticField = new CbmBsField(fFieldPar);
243 else {
244 std::stringstream ss;
245 ss << "Unknown field type " << fType;
246 throw InitTaskError(ss.str());
247 }
248 LOG(info) << "New field at " << fMagneticField << ", type " << fType;
249
250 // Initialise field
251 if (fMagneticField) {
252 fMagneticField->Init();
253 fMagneticField->Print("");
254 }
255
256 LOG(info) << "Before return";
257 return fMagneticField;
258}
259
260
261// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
262bool CbmDeviceStsLocalReco::HandleData(FairMQMessagePtr& msg, int /*index*/)
263{
264 // Don't do anything with the data
265 // Maybe add an message counter which counts the incomming messages and add
266 // an output
267 fNumMessages++;
268 LOG(debug) << "Received message number " << fNumMessages << " with size " << msg->GetSize();
269
270 std::string msgStr(static_cast<char*>(msg->GetData()), msg->GetSize());
271 std::istringstream iss(msgStr);
272 boost::archive::binary_iarchive inputArchive(iss);
273
274 DoWork();
275
276 if (fNumMessages % 10000 == 0) LOG(info) << "Processed " << fNumMessages << " time slices";
277
278 SendData();
279
280 return true;
281}
282
283
284bool CbmDeviceStsLocalReco::SendData() { return true; }
285
286Bool_t CbmDeviceStsLocalReco::DoWork() { return true; }
287
CbmStsDigitizeParameters * fDigiPar
std::vector< std::string > fAllowedChannels
bool HandleData(FairMQMessagePtr &, int)
bool IsChannelNameAllowed(std::string channelName)
Int_t GetType() const
Definition CbmFieldPar.h:61
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition CbmMQDefs.h:26