CbmRoot
CbmMCPointSource.cxx
Go to the documentation of this file.
1 
8 #include "CbmMCPointSource.h"
9 #include "CbmMQDefs.h"
10 
11 #include "CbmMuchPoint.h"
12 #include "CbmMvdPoint.h"
13 #include "CbmRichPoint.h"
14 #include "CbmStsPoint.h"
15 #include "CbmTofPoint.h"
16 #include "CbmTrdPoint.h"
17 //#include "CbmEcalPoint.h"
18 #include "CbmPsdPoint.h"
19 
20 #include "FairMQLogger.h"
21 #include "FairMQProgOptions.h" // device->fConfig
22 
23 #include "FairFileSource.h"
24 #include "FairRootManager.h"
25 #include "FairRunAna.h"
26 
27 #include "TClonesArray.h"
28 
29 //#include <boost/archive/binary_oarchive.hpp>
30 // include this header to serialize vectors
31 //#include <boost/serialization/vector.hpp>
32 
33 #include <chrono>
34 #include <ctime>
35 #include <stdexcept>
36 #include <stdio.h>
37 #include <thread> // this_thread::sleep_for
38 
39 using namespace std;
40 
41 struct InitTaskError : std::runtime_error {
42  using std::runtime_error::runtime_error;
43 };
44 
45 
47  // Get the values from the command line options (via fConfig)
48  fFileName = fConfig->GetValue<string>("filename");
49  fMaxEvents = fConfig->GetValue<uint64_t>("max-events");
50 
51 
52  LOG(info) << "Filename: " << fFileName;
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  fChan.CheckChannels(this);
58  fComponentsToSend = fChan.GetComponentsToSend();
59  fChannelsToSend = fChan.GetChannelsToSend();
60 
61  for (auto const& value : fComponentsToSend) {
62  if (value > 1) {
63  throw InitTaskError("Sending same data to more than one output channel "
64  "not implemented yet.");
65  }
66  }
67 
68  // Here we need to create an instance of FairRunAna to avoid a crash when creating the FairRootmanager
69  // This is only a workaround since we actually don't need FairRunAna and the underlying problem has to
70  // be fixed in FairRoot. The command ana->SetContainerStatic() is only
71  // used to silence a compiler warning
72  FairRunAna* ana = new FairRunAna();
73  ana->SetContainerStatic();
74  FairRootManager* rootman = FairRootManager::Instance();
75 
76  if (0 != fFileName.size()) {
77  LOG(info) << "Open the ROOT input file " << fFileName;
78  // Check if the input file exist
79  FILE* inputFile = fopen(fFileName.c_str(), "r");
80  if (!inputFile) { throw InitTaskError("Input file doesn't exist."); }
81  fclose(inputFile);
82  FairFileSource* source = new FairFileSource(fFileName);
83  if (!source) { throw InitTaskError("Could not open input file."); }
84  rootman->SetSource(source);
85  rootman->InitSource();
86 
87 
88  for (unsigned i = 0; i < fComponentsToSend.size(); i++) {
89  if (
90  1
91  == fComponentsToSend.at(
92  i)) { // there is a device connected which consumes data of this type
93  std::vector<std::string> channel_name = fChannelsToSend.at(i);
94  LOG(info) << channel_name.at(0);
95  ConnectChannelIfNeeded(i, channel_name.at(0), "MvdPoint", rootman);
96  ConnectChannelIfNeeded(i, channel_name.at(0), "StsPoint", rootman);
97  ConnectChannelIfNeeded(i, channel_name.at(0), "RichPoint", rootman);
98  ConnectChannelIfNeeded(i, channel_name.at(0), "MuchPoint", rootman);
99  ConnectChannelIfNeeded(i, channel_name.at(0), "TrdPoint", rootman);
100  ConnectChannelIfNeeded(i, channel_name.at(0), "TofPoint", rootman);
101  // ConnectChannelIfNeeded(i, channel_name.at(0), "EcalPoint", rootman);
102  ConnectChannelIfNeeded(i, channel_name.at(0), "PsdPoint", rootman);
103  } else {
104  fArrays.at(i) = nullptr;
105  }
106  }
107  } else {
108  throw InitTaskError("No input file specified");
109  }
110 
111  Int_t MaxAllowed = FairRootManager::Instance()->CheckMaxEventNo(fMaxEvents);
112  if (MaxAllowed != -1) {
113  if (fMaxEvents == 0) {
114  fMaxEvents = MaxAllowed;
115  } else {
116  if (static_cast<Int_t>(fMaxEvents) > MaxAllowed) {
117  LOG(warn) << "-------------------Warning---------------------------";
118  LOG(warn) << " File has less events than requested!!";
119  LOG(warn) << " File contains : " << MaxAllowed << " Events";
120  LOG(warn) << " Requested number of events = " << fMaxEvents
121  << " Events";
122  LOG(warn) << " The number of events is set to " << MaxAllowed
123  << " Events";
124  LOG(warn) << "-----------------------------------------------------";
125  fMaxEvents = MaxAllowed;
126  }
127  }
128  LOG(info) << "After checking, the run will run from event 0 "
129  << " to " << fMaxEvents << ".";
130  } else {
131  LOG(info) << "continue running without stop";
132  }
133 
134 
135  fTime = std::chrono::steady_clock::now();
136 } catch (InitTaskError& e) {
137  LOG(error) << e.what();
138  // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
140 }
141 
143  std::string channel_name,
144  std::string branchname,
145  FairRootManager* rootman) {
146  if (0 == channel_name.compare(branchname)) {
147  LOG(info) << "Found expected data type " << branchname;
148  TClonesArray* arr =
149  static_cast<TClonesArray*>(rootman->GetObject(branchname.c_str()));
150  if (!arr) {
151  LOG(info) << "Consuming device connected but no " << branchname
152  << " array in input file!";
153  fComponentsToSend.at(chan_number) =
154  0; // Don't send to connected device since needed data is not in input
155  }
156  fArrays.at(chan_number) = arr;
157  }
158 }
159 
160 
162 
163  Int_t readEventReturn = FairRootManager::Instance()->ReadEvent(fEventCounter);
164  // LOG(info) <<"Return value: " << readEventReturn;
165 
166  if (readEventReturn != 0) {
167  LOG(warn) << "FairRootManager::Instance()->ReadEvent(" << fEventCounter
168  << ") returned " << readEventReturn
169  << ". Breaking the event loop";
170  CalcRuntime();
171  return false;
172  }
173 
174  for (unsigned i = 0; i < fComponentsToSend.size(); i++) {
175  bool result = true;
176  if (1
177  == fComponentsToSend.at(
178  i)) { // there is a device connected which consumes data of this type
179 
180  if (0 == fChannelsToSend.at(i).at(0).compare("MvdPoint")) {
181  result = ConvertAndSend<CbmMvdPoint>(fArrays.at(i), i);
182  }
183  if (0 == fChannelsToSend.at(i).at(0).compare("StsPoint")) {
184  result = ConvertAndSend<CbmStsPoint>(fArrays.at(i), i);
185  }
186  if (0 == fChannelsToSend.at(i).at(0).compare("RichPoint")) {
187  result = ConvertAndSend<CbmRichPoint>(fArrays.at(i), i);
188  }
189  if (0 == fChannelsToSend.at(i).at(0).compare("MuchPoint")) {
190  result = ConvertAndSend<CbmMuchPoint>(fArrays.at(i), i);
191  }
192  if (0 == fChannelsToSend.at(i).at(0).compare("TrdPoint")) {
193  result = ConvertAndSend<CbmTrdPoint>(fArrays.at(i), i);
194  }
195  if (0 == fChannelsToSend.at(i).at(0).compare("TofPoint")) {
196  result = ConvertAndSend<CbmTofPoint>(fArrays.at(i), i);
197  }
198  /*
199  if (0 == fChannelsToSend.at(i).at(0).compare("EcalPoint")) {
200  result = ConvertAndSend<CbmEcalPoint>(fArrays.at(i),i );
201  }
202 */
203  if (0 == fChannelsToSend.at(i).at(0).compare("PsdPoint")) {
204  result = ConvertAndSend<CbmPsdPoint>(fArrays.at(i), i);
205  }
206 
207  if (!result) {
208  LOG(error) << "Problem sending data";
209  return false;
210  }
211  }
212  }
213 
214  if (fEventCounter % 10000 == 0)
215  LOG(info) << "Analyse Event " << fEventCounter;
216  fEventCounter++;
217 
218 
219  // LOG(info) << "Counter: " << fEventCounter << " Events: " << fMaxEvents;
220  if (fEventCounter < fMaxEvents) {
221  return true;
222  } else {
223  CalcRuntime();
224  return false;
225  }
226 }
227 
229 
231  std::chrono::duration<double> run_time =
232  std::chrono::steady_clock::now() - fTime;
233 
234  LOG(info) << "Runtime: " << run_time.count();
235  LOG(info) << "No more input data";
236 }
CbmRichPoint.h
InitTaskError
CBM headers.
Definition: CbmDeviceEventBuilderEtofStar2019.cxx:36
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmMCPointSource::~CbmMCPointSource
virtual ~CbmMCPointSource()
Definition: CbmMCPointSource.cxx:228
cbm::mq::Transition::ErrorFound
@ ErrorFound
CbmMCPointSource::CalcRuntime
void CalcRuntime()
Definition: CbmMCPointSource.cxx:230
CbmMCPointSource::InitTask
virtual void InitTask()
Definition: CbmMCPointSource.cxx:46
CbmMvdPoint.h
CbmMuchPoint.h
CbmStsPoint.h
CbmMCPointSource::ConnectChannelIfNeeded
void ConnectChannelIfNeeded(int, std::string, std::string, FairRootManager *)
Definition: CbmMCPointSource.cxx:142
CbmMCPointSource.h
CbmTofPoint.h
CbmTrdPoint.h
CbmMCPointSource::ConditionalRun
virtual bool ConditionalRun()
Definition: CbmMCPointSource.cxx:161
CbmMQDefs.h
cbm::mq::ChangeState
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition: CbmMQDefs.h:19
CbmPsdPoint.h