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