CbmRoot
CbmDeviceStsHitProducerIdeal.cxx
Go to the documentation of this file.
1 
9 #include "CbmMQDefs.h"
10 
11 #include "CbmTrdParSetGas.h"
12 
13 #include "CbmStsHit.h"
14 #include "CbmStsPoint.h"
15 
16 #include "FairMQLogger.h"
17 #include "FairParGenericSet.h"
18 
19 #include "FairMQProgOptions.h" // device->fConfig
20 
21 #include "FairRunAna.h"
22 
23 #include "TList.h"
24 
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/serialization/vector.hpp>
27 
28 #include <string>
29 #include <vector>
30 
31 #include <stdexcept>
32 struct InitTaskError : std::runtime_error {
33  using std::runtime_error::runtime_error;
34 };
35 
36 //using namespace std;
37 using std::string;
38 
40  : fMaxEvents {0}
41  , fNumMessages {0}
42  , fRunId {"0"}
43  , fvmcworkdir {""}
44  , fTrdGasPar {nullptr} {}
45 
47 
48 
50 
51  fMaxEvents = fConfig->GetValue<uint64_t>("max-events");
52 
53  LOG(info) << "MaxEvents: " << fMaxEvents;
54 
55  // Check if the defined channels from the topology (by name)
56  // are in the list of channels which are allowed
57  // Connnect channels which delivers StsPoints with the proper
58  // function handling the data
59  fChan.CheckChannels(this);
62 
63  for (auto const& entry : fChannels) {
64  LOG(info) << "Channel name: " << entry.first;
65  if (entry.first.compare("StsPoint"))
66  LOG(info) << "Connect Channel " << entry.first
67  << "with data type StsPoint";
68  OnData(entry.first, &CbmDeviceStsHitProducerIdeal::HandleData);
69  }
70  // Initialize the algorithm and get the proper parameter containers
71  fAlgo->Init();
73 
74 } catch (InitTaskError& e) {
75  LOG(ERROR) << e.what();
76  // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
78 }
79 
81  Bool_t initOK {kTRUE};
82 
83  fRunId = fConfig->GetValue<string>("run-id");
84  fvmcworkdir = fConfig->GetValue<string>("vmcworkdir");
85  fMaxEvents = fConfig->GetValue<uint64_t>("max-events");
86 
87  LOG(INFO) << "Init parameter containers for CbmDeviceStsHitProducerIdeal.";
88 
89 
90  TList* fParCList = fAlgo->GetParList();
91 
92  for (int iparC = 0; iparC < fParCList->GetEntries(); iparC++) {
93  FairParGenericSet* tempObj = (FairParGenericSet*) (fParCList->At(iparC));
94  fParCList->Remove(tempObj);
95  std::string paramName {tempObj->GetName()};
96 
97  // NewSimpleMessage create s a copy of the data and takes care of its destruction (after the transfer takes place).
98  // Should only be used for small data because of the cost of an additional copy
99 
100  // Her must come the proper Runid
101  std::string message = paramName + ",111";
102  LOG(info) << "Requesting parameter container " << paramName
103  << ", sending message: " << message;
104 
105  FairMQMessagePtr req(NewSimpleMessage(message));
106  FairMQMessagePtr rep(NewMessage());
107 
108  FairParGenericSet* newObj = nullptr;
109 
110  if (Send(req, "parameters") > 0) {
111  if (Receive(rep, "parameters") >= 0) {
112  if (rep->GetSize() != 0) {
113  CbmMQTMessage tmsg(rep->GetData(), rep->GetSize());
114  newObj =
115  static_cast<FairParGenericSet*>(tmsg.ReadObject(tmsg.GetClass()));
116  LOG(info) << "Received unpack parameter from the server:";
117  newObj->print();
118  } else {
119  LOG(error) << "Received empty reply. Parameter not available";
120  } // if (rep->GetSize() != 0)
121  } // if (Receive(rep, "parameters") >= 0)
122  } // if (Send(req, "parameters") > 0)
123  fParCList->AddAt(newObj, iparC);
124  delete tempObj;
125  } // for ( int iparC = 0; iparC < fParCList->GetEntries(); iparC++ )
126 
127  // NewSimpleMessage creates a copy of the data and takes care of its destruction (after the transfer takes place).
128  // Should only be used for small data because of the cost of an additional copy
129 
130  initOK = fAlgo->InitContainers();
131 
132  return initOK;
133  return true;
134 }
135 
136 
137 // handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
138 bool CbmDeviceStsHitProducerIdeal::HandleData(FairMQMessagePtr& msg,
139  int /*index*/) {
140 
141  fNumMessages++;
142  LOG(DEBUG) << "Received message number " << fNumMessages << " with size "
143  << msg->GetSize();
144 
145 
146  // Unpack the message into a vector of CbmStsPoints
147  std::string msgStr(static_cast<char*>(msg->GetData()), msg->GetSize());
148  std::istringstream iss(msgStr);
149  boost::archive::binary_iarchive inputArchive(iss);
150 
151  std::vector<CbmStsPoint> points;
152  inputArchive >> points;
153 
154  // Pass the vector to the algorithm
155  // Get the vector with the newly created data objects from the algorithm
156  std::vector<CbmStsHit> hits = fAlgo->ProcessInputData(points);
157 
158  // Event summary
159  LOG(info) << "Out of " << points.size() << " StsPoints, " << hits.size()
160  << " Hits created.";
161 
162 
163  if (fNumMessages % 10000 == 0)
164  LOG(INFO) << "Processed " << fNumMessages << " time slices";
165 
166  // Send the data to a consumer
167  SendData();
168 
169  return true;
170 }
171 
172 
174 
175 Bool_t CbmDeviceStsHitProducerIdeal::DoWork() { return true; }
176 
CbmDeviceStsHitProducerIdeal::SendData
bool SendData()
Definition: CbmDeviceStsHitProducerIdeal.cxx:173
CbmDeviceStsHitProducerIdeal::HandleData
bool HandleData(FairMQMessagePtr &, int)
Definition: CbmDeviceStsHitProducerIdeal.cxx:138
CbmDeviceStsHitProducerIdeal::fChan
CbmMQChannels fChan
Definition: CbmDeviceStsHitProducerIdeal.h:46
CbmStsHitProducerIdealAlgo::Init
virtual Bool_t Init()
Definition: CbmStsHitProducerIdealAlgo.cxx:23
CbmStsHitProducerIdealAlgo::InitContainers
Bool_t InitContainers()
Definition: CbmStsHitProducerIdealAlgo.cxx:34
CbmDeviceStsHitProducerIdeal::InitTask
virtual void InitTask()
Definition: CbmDeviceStsHitProducerIdeal.cxx:49
CbmDeviceStsHitProducerIdeal::fvmcworkdir
std::string fvmcworkdir
Definition: CbmDeviceStsHitProducerIdeal.h:37
InitTaskError
CBM headers.
Definition: CbmDeviceEventBuilderEtofStar2019.cxx:36
CbmDeviceStsHitProducerIdeal::fNumMessages
uint64_t fNumMessages
Definition: CbmDeviceStsHitProducerIdeal.h:35
CbmDeviceStsHitProducerIdeal::fRunId
std::string fRunId
Definition: CbmDeviceStsHitProducerIdeal.h:36
CbmMQTMessage
Definition: CbmDeviceEventBuilderEtofStar2019.h:96
CbmDeviceStsHitProducerIdeal::CbmDeviceStsHitProducerIdeal
CbmDeviceStsHitProducerIdeal()
Definition: CbmDeviceStsHitProducerIdeal.cxx:39
CbmMQChannels::CheckChannels
bool CheckChannels(FairMQDevice *device)
Definition: CbmMQChannels.cxx:47
cbm::mq::Transition::ErrorFound
@ ErrorFound
CbmDeviceStsHitProducerIdeal::fAlgo
CbmStsHitProducerIdealAlgo * fAlgo
Definition: CbmDeviceStsHitProducerIdeal.h:48
TrbNetState::DEBUG
@ DEBUG
CbmDeviceStsHitProducerIdeal::~CbmDeviceStsHitProducerIdeal
virtual ~CbmDeviceStsHitProducerIdeal()
Definition: CbmDeviceStsHitProducerIdeal.cxx:46
CbmDeviceStsHitProducerIdeal::Finish
void Finish()
Definition: CbmDeviceStsHitProducerIdeal.cxx:177
CbmStsPoint.h
points
TClonesArray * points
Definition: Analyze_matching.h:18
CbmMQChannels::GetChannelsToSend
std::vector< std::vector< std::string > > GetChannelsToSend()
Definition: CbmMQChannels.h:17
CbmStsHitProducerIdealAlgo::ProcessInputData
virtual std::vector< CbmStsHit > ProcessInputData(const std::vector< CbmStsPoint > &)
Definition: CbmStsHitProducerIdealAlgo.cxx:67
hits
static vector< vector< QAHit > > hits
Definition: CbmTofHitFinderTBQA.cxx:114
CbmDeviceStsHitProducerIdeal::InitContainers
bool InitContainers()
Definition: CbmDeviceStsHitProducerIdeal.cxx:80
CbmDeviceStsHitProducerIdeal.h
CbmDeviceStsHitProducerIdeal::fComponentsToSend
std::vector< int > fComponentsToSend
Definition: CbmDeviceStsHitProducerIdeal.h:44
CbmTrdParSetGas.h
CbmDeviceStsHitProducerIdeal::fChannelsToSend
std::vector< std::vector< std::string > > fChannelsToSend
Definition: CbmDeviceStsHitProducerIdeal.h:43
CbmMQDefs.h
CbmDeviceStsHitProducerIdeal::fMaxEvents
uint64_t fMaxEvents
Definition: CbmDeviceStsHitProducerIdeal.h:34
cbm::mq::ChangeState
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition: CbmMQDefs.h:19
CbmStsHitProducerIdealAlgo::GetParList
TList * GetParList()
Definition: CbmStsHitProducerIdealAlgo.cxx:54
CbmMQChannels::GetComponentsToSend
std::vector< int > GetComponentsToSend()
Definition: CbmMQChannels.h:16
CbmDeviceStsHitProducerIdeal::DoWork
bool DoWork()
Definition: CbmDeviceStsHitProducerIdeal.cxx:175
CbmStsHit.h
Data class for a reconstructed hit in the STS.