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