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