CbmRoot
CbmStsDigiSource.cxx
Go to the documentation of this file.
1 
9 #include "CbmStsDigiSource.h"
10 #include "CbmMQDefs.h"
11 
12 #include "CbmDigiManager.h"
13 #include "CbmStsDigi.h"
14 
15 #include "FairMQLogger.h"
16 #include "FairMQProgOptions.h" // device->fConfig
17 
18 #include "FairFileSource.h"
19 #include "FairRootManager.h"
20 #include "FairRunAna.h"
21 
22 
23 #include <boost/archive/binary_oarchive.hpp>
24 
25 #include <chrono>
26 #include <ctime>
27 #include <stdexcept>
28 #include <stdio.h>
29 #include <thread> // this_thread::sleep_for
30 
31 using namespace std;
32 
33 struct InitTaskError : std::runtime_error {
34  using std::runtime_error::runtime_error;
35 };
36 
37 
39  : FairMQDevice()
40  , fMaxEvents(0)
41  , fFileName("")
42  , fInputFileList()
43  , fFileCounter(0)
44  , fEventNumber(0)
45  , fEventCounter(0)
46  , fMessageCounter(0)
47  , fTime() {}
48 
50  // Get the values from the command line options (via fConfig)
51  fFileName = fConfig->GetValue<string>("filename");
52  fMaxEvents = fConfig->GetValue<uint64_t>("max-events");
53 
54 
55  LOG(info) << "Filename: " << fFileName;
56  LOG(info) << "MaxEvents: " << fMaxEvents;
57 
58  // Get the information about created channels from the device
59  // Check if the defined channels from the topology (by name)
60  // are in the list of channels which are possible/allowed
61  // for the device
62  // The idea is to check at initilization if the devices are
63  // properly connected. For the time beeing this is done with a
64  // nameing convention. It is not avoided that someone sends other
65  // data on this channel.
66  int noChannel = fChannels.size();
67  LOG(info) << "Number of defined output channels: " << noChannel;
68  for (auto const& entry : fChannels) {
69  LOG(info) << "Channel name: " << entry.first;
70  if (!IsChannelNameAllowed(entry.first))
71  throw InitTaskError("Channel name does not match.");
72  }
73 
74  FairRootManager* rootman = FairRootManager::Instance();
75 
76 
77  if (0 != fFileName.size()) {
78  LOG(info) << "Open the ROOT input file " << fFileName;
79  // Check if the input file exist
80  FILE* inputFile = fopen(fFileName.c_str(), "r");
81  if (!inputFile) { throw InitTaskError("Input file doesn't exist."); }
82  fclose(inputFile);
83  FairFileSource* source = new FairFileSource(fFileName);
84  if (!source) { throw InitTaskError("Could not open input file."); }
85  rootman->SetSource(source);
86  rootman->InitSource();
88  digiMan->Init();
89  if (!digiMan->IsPresent(ECbmModuleId::kSts)) {
90  throw InitTaskError("No StsDigi branch in input!");
91  }
92  } else {
93  throw InitTaskError("No input file specified");
94  }
95 
96 
97  Int_t MaxAllowed = FairRootManager::Instance()->CheckMaxEventNo(fMaxEvents);
98  if (MaxAllowed != -1) {
99  if (fMaxEvents == 0) {
100  fMaxEvents = MaxAllowed;
101  } else {
102  if (static_cast<Int_t>(fMaxEvents) > MaxAllowed) {
103  LOG(warn) << "-------------------Warning---------------------------";
104  LOG(warn) << " File has less events than requested!!";
105  LOG(warn) << " File contains : " << MaxAllowed << " Events";
106  LOG(warn) << " Requested number of events = " << fMaxEvents
107  << " Events";
108  LOG(warn) << " The number of events is set to " << MaxAllowed
109  << " Events";
110  LOG(warn) << "-----------------------------------------------------";
111  fMaxEvents = MaxAllowed;
112  }
113  }
114  LOG(info) << "After checking, the run will run from event 0 "
115  << " to " << fMaxEvents << ".";
116  } else {
117  LOG(info) << "continue running without stop";
118  }
119 
120 
121  fTime = std::chrono::steady_clock::now();
122 } catch (InitTaskError& e) {
123  LOG(error) << e.what();
124  // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
126 }
127 
128 bool CbmStsDigiSource::IsChannelNameAllowed(std::string channelName) {
129  if (std::find(fAllowedChannels.begin(), fAllowedChannels.end(), channelName)
130  != fAllowedChannels.end()) {
131  LOG(info) << "Channel name " << channelName
132  << " found in list of allowed channel names.";
133  return true;
134  } else {
135  LOG(info) << "Channel name " << channelName
136  << " not found in list of allowed channel names.";
137  LOG(error) << "Stop device.";
138  return false;
139  }
140 }
141 
143 
144  Int_t readEventReturn = FairRootManager::Instance()->ReadEvent(fEventCounter);
145  LOG(info) << "Return value: " << readEventReturn;
146 
147  if (readEventReturn != 0) {
148  LOG(warn) << "FairRootManager::Instance()->ReadEvent(" << fEventCounter
149  << ") returned " << readEventReturn
150  << ". Breaking the event loop";
151  CalcRuntime();
152  return false;
153  }
154 
155  for (Int_t index = 0;
157  index++) {
158  const CbmStsDigi* stsDigi =
160  PrintStsDigi(stsDigi);
161  }
162 
163  if (fEventCounter % 10000 == 0)
164  LOG(info) << "Analyse Event " << fEventCounter;
165  fEventCounter++;
166 
167 
168  LOG(info) << "Counter: " << fEventCounter << " Events: " << fMaxEvents;
169  if (fEventCounter < fMaxEvents) {
170  return true;
171  } else {
172  CalcRuntime();
173  return false;
174  }
175 }
176 
177 
179 
181  std::chrono::duration<double> run_time =
182  std::chrono::steady_clock::now() - fTime;
183 
184  LOG(info) << "Runtime: " << run_time.count();
185  LOG(info) << "No more input data";
186 }
187 
188 
190  LOG(info) << digi->ToString();
191 }
CbmStsDigiSource::~CbmStsDigiSource
virtual ~CbmStsDigiSource()
Definition: CbmStsDigiSource.cxx:178
CbmStsDigiSource::InitTask
virtual void InitTask()
Definition: CbmStsDigiSource.cxx:49
CbmDigiManager::Init
InitStatus Init()
Initialisation.
Definition: CbmDigiManager.cxx:71
InitTaskError
CBM headers.
Definition: CbmDeviceEventBuilderEtofStar2019.cxx:36
CbmStsDigiSource::fEventCounter
uint64_t fEventCounter
Definition: CbmStsDigiSource.h:31
CbmStsDigiSource::CalcRuntime
void CalcRuntime()
Definition: CbmStsDigiSource.cxx:180
CbmDigiManager::GetNofDigis
static Int_t GetNofDigis(ECbmModuleId systemId)
Definition: CbmDigiManager.cxx:62
CbmStsDigi::ToString
std::string ToString() const
Definition: CbmStsDigi.cxx:15
CbmStsDigiSource::IsChannelNameAllowed
bool IsChannelNameAllowed(std::string)
Definition: CbmStsDigiSource.cxx:128
CbmDigiManager::IsPresent
static Bool_t IsPresent(ECbmModuleId systemId)
Presence of a digi branch.
Definition: CbmDigiManager.cxx:112
cbm::mq::Transition::ErrorFound
@ ErrorFound
CbmStsDigiSource::ConditionalRun
virtual bool ConditionalRun()
Definition: CbmStsDigiSource.cxx:142
CbmDigiManager::Instance
static CbmDigiManager * Instance()
Static instance.
Definition: CbmDigiManager.h:93
CbmStsDigiSource::fMaxEvents
uint64_t fMaxEvents
Definition: CbmStsDigiSource.h:24
CbmStsDigi.h
CbmDigiManager::Get
const Digi * Get(Int_t index) const
Get a digi object.
Definition: CbmDigiManager.h:52
CbmStsDigiSource::fFileName
std::string fFileName
Definition: CbmStsDigiSource.h:26
CbmStsDigiSource::fAllowedChannels
std::vector< std::string > fAllowedChannels
Definition: CbmStsDigiSource.h:47
CbmDigiManager
CbmDigiManager.
Definition: CbmDigiManager.h:37
CbmStsDigi
Data class for a single-channel message in the STS.
Definition: CbmStsDigi.h:29
CbmStsDigiSource::fTime
std::chrono::steady_clock::time_point fTime
Definition: CbmStsDigiSource.h:45
CbmStsDigiSource::CbmStsDigiSource
CbmStsDigiSource()
Definition: CbmStsDigiSource.cxx:38
CbmStsDigiSource.h
CbmDigiManager.h
CbmStsDigiSource::PrintStsDigi
void PrintStsDigi(const CbmStsDigi *)
Definition: CbmStsDigiSource.cxx:189
CbmMQDefs.h
cbm::mq::ChangeState
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition: CbmMQDefs.h:19
ECbmModuleId::kSts
@ kSts
Silicon Tracking System.