CbmRoot
Loading...
Searching...
No Matches
CbmReco.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer] */
4
5#include "CbmReco.h"
6
7#include "CbmSourceTs.h"
10#include "CbmTaskTriggerDigi.h"
11#include "CbmTaskUnpack.h"
12#include "CbmTsEventHeader.h"
13#include "DigiEventSelector.h"
15
16#include <FairFileSource.h>
17#include <FairRootFileSink.h>
18#include <FairRunAna.h>
19#include <FairRunOnline.h>
20#include <Logger.h>
21
22#include <THttpServer.h>
23#include <TRootSniffer.h>
24
25#include <memory>
26#include <string>
27
28#include <yaml-cpp/yaml.h>
29
30using std::make_unique;
31using std::string;
32
33
34// ----- Constructor from single source -----------------------------------
35CbmReco::CbmReco(Config config, string source, string outFile, string configFile, int32_t numTs, uint16_t port,
36 cbm::Monitor* monitor)
37 : fConfig(config)
38 , fSourceNames{source}
39 , fOutputFileName(outFile)
40 , fConfigFileName(configFile)
41 , fNumTs(numTs)
42 , fHttpServerPort(port)
43 , fMonitor(monitor)
44{
45}
46// ----------------------------------------------------------------------------
47
48
49// ----- Constructor from multiple sources --------------------------------
50CbmReco::CbmReco(Config config, std::vector<string> sources, string outFile, string configFile, int32_t numTs,
51 uint16_t port, cbm::Monitor* monitor)
52 : fConfig(config)
53 , fSourceNames(sources)
54 , fOutputFileName(outFile)
55 , fConfigFileName(configFile)
56 , fNumTs(numTs)
57 , fHttpServerPort(port)
58 , fMonitor(monitor)
59{
60}
61// ----------------------------------------------------------------------------
62
63
64// ----- List of source names ---------------------------------------------
65std::string CbmReco::ListSources() const
66{
67 std::string result = "{";
68 for (auto& source : fSourceNames) {
69 result += source + ", ";
70 }
71 result += "}";
72 return result;
73}
74// ----------------------------------------------------------------------------
75
76
77// ----- Configure and execute run ----------------------------------------
78int32_t CbmReco::Run()
79{
80
81 // --- Timing
82 TStopwatch timer;
83 timer.Start();
84
85 // --- Check if the input is a ROOT file. In that case, digis are already present and
86 // --- the unpacking stage must be skipped. The digis are in direct branches of the ROOT
87 // --- tree when coming from simulation, or in form of CbmDigiTimeslice if produced
88 // --- by a previous unpacking run. This variety is caught by the tasks and need not be
89 // --- considered here.
90 bool isRootInput =
91 fSourceNames.size() == 1 && fSourceNames.at(0).compare(fSourceNames.at(0).size() - 5, 5, ".root") == 0;
92
93 // --- Run instance
94 FairRunOnline run;
95
96 // --- Input source
97 if (isRootInput) {
98 auto source = make_unique<FairFileSource>(fSourceNames.at(0));
99 LOG(info) << "Reco: Using ROOT input " << fSourceNames.at(0);
100 run.SetSource(source.release());
101 }
102 else {
103 auto source = make_unique<CbmSourceTs>(fSourceNames);
104 if (source)
105 LOG(info) << "Reco: Using sources " << ListSources();
106 else {
107 LOG(error) << "Reco: Could not open sources " << ListSources() << "; aborting.";
108 return -1;
109 }
110 run.SetSource(source.release());
111 }
112
113 // --- Output file
114 auto sink = make_unique<FairRootFileSink>(fOutputFileName);
115 if (sink)
116 LOG(info) << "Reco: Using output file " << fOutputFileName;
117 else {
118 LOG(error) << "Reco: Could not open output " << fOutputFileName << "; aborting.";
119 return -1;
120 }
121 run.SetSink(sink.release());
122
123 // --- Event header
124 auto header = make_unique<CbmTsEventHeader>();
125 run.SetEventHeader(header.release());
126
127 // --- Unpacking
128 if (!isRootInput) {
129 CbmTaskUnpack::Config unpackConfig;
130 unpackConfig.dumpSetup = fConfig.dumpSetup;
131 //auto unpack = make_unique<CbmTaskUnpack>(unpackConfig);
132 // TO DO: This call no longer works. Parameters directory and run ID is needed!
133 auto unpack = make_unique<CbmTaskUnpack>();
134 unpack->SetMonitor(fMonitor);
135 unpack->SetOutputBranchPersistent("DigiTimeslice.", false);
136 run.AddTask(unpack.release());
137 }
138
139 // --- Event building configuration
140 cbm::algo::evbuild::Config evbuildConfig(YAML::LoadFile(fConfigFileName));
141
142 // --- Digi trigger
143 auto trigger = make_unique<CbmTaskTriggerDigi>();
144 trigger->AddSystem(evbuildConfig.fDigiTrigger.Detector());
145 trigger->SetConfig(evbuildConfig.fDigiTrigger);
146 trigger->SetOutputBranchPersistent("Trigger", false);
147 run.AddTask(trigger.release());
148
149 // --- Event selector parameters
150 cbm::algo::evbuild::DigiEventSelectorConfig selectConfig = evbuildConfig.fSelector;
151
152 // --- Event building
153 auto evtBuild = make_unique<CbmTaskBuildEvents>();
154 evtBuild->SetConfig(evbuildConfig.fBuilder);
155 evtBuild->SetOutputBranchPersistent("DigiEvent", true);
156 evtBuild->SetDigiEventSelector(selectConfig);
157 run.AddTask(evtBuild.release());
158
159 // --- Event QA
160 auto evtQa = make_unique<CbmTaskDigiEventQa>();
161 evtQa->Config(evbuildConfig);
162 run.AddTask(evtQa.release());
163
164 // ----- HttpServer for online monitoring
165 if (fHttpServerPort) {
166 run.ActivateHttpServer(100, fHttpServerPort);
167 run.GetHttpServer()->GetSniffer()->SetScanGlobalDir(kFALSE);
168 }
169
170 // --- Initialise and start run
171 timer.Stop();
172 double timeSetup = timer.RealTime();
173 timer.Start();
174 LOG(info) << "Reco: Initialising...";
175 run.Init();
176 timer.Stop();
177 double timeInit = timer.RealTime();
178
179 // --- Start run
180 timer.Start();
181 run.Run(0, fNumTs);
182 timer.Stop();
183 double timeRun = timer.RealTime();
184
185 // --- Run log
186 size_t numTs = 1;
187 if (!isRootInput) {
188 auto src = dynamic_cast<CbmSourceTs*>(run.GetSource());
189 assert(src);
190 numTs = src->GetNumTs();
191 }
192 // TODO: Don't know how to get the number of processed timeslices for ROOT input.
193 double timeTotal = timeSetup + timeInit + timeRun;
194 LOG(info) << "=====================================";
195 LOG(info) << "Reco: Run summary";
196 LOG(info) << "Timeslices : " << numTs;
197 LOG(info) << "Time setup : " << timeSetup << " s";
198 LOG(info) << "Time init : " << timeInit << " s";
199 LOG(info) << "Time run : " << timeRun << " s";
200 LOG(info) << "Time total : " << timeTotal << " s"
201 << " (" << timeTotal / numTs << " s/ts)";
202 LOG(info) << "Output file : " << fOutputFileName;
203 LOG(info) << "=====================================";
204
205 return numTs;
206}
207// ----------------------------------------------------------------------------
208
ClassImp(CbmConverterManager)
Main steering class of reconstruction in CBM.
Definition CbmReco.h:44
cbm::Monitor * fMonitor
Definition CbmReco.h:102
std::string ListSources() const
List all entries in the input vector.
Definition CbmReco.cxx:65
int32_t Run()
Configure and execute run.
Definition CbmReco.cxx:78
Config fConfig
Configuration.
Definition CbmReco.h:96
std::string fOutputFileName
Output file (ROOT)
Definition CbmReco.h:98
int32_t fNumTs
Number of timeslices to process.
Definition CbmReco.h:100
CbmReco(Config config, std::string source, std::string outFile, std::string configFile, int32_t numTs=std::numeric_limits< int32_t >::max(), uint16_t port=0, cbm::Monitor *monitor=nullptr)
Default constructor.
std::vector< std::string > fSourceNames
Sources (input files or stream)
Definition CbmReco.h:97
std::string fConfigFileName
Configuration file (YAML)
Definition CbmReco.h:99
uint16_t fHttpServerPort
Definition CbmReco.h:101
Source class for reading from archived time slice data.
Definition CbmSourceTs.h:26
Configuration of digi event building.
DigiTriggerConfig fDigiTrigger
Digi trigger configuration.
EventBuilderConfig fBuilder
Event builder configuration.
DigiEventSelectorConfig fSelector
Event selector configuration.
Configuration of the DigiEventSelector class.
ECbmModuleId Detector() const
Trigger detector.