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