CbmRoot
Loading...
Searching...
No Matches
analysis/common/analysis_tree_converter/steer/Run.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Frederic Linz [committer], Volker Friese, Florian Uhlig */
4
10#include "Run.h"
11
12#include "CbmSetup.h"
13
14#include <FairEventHeader.h>
15#include <FairFileSource.h>
16#include <FairMonitor.h>
17#include <FairParRootFileIo.h>
18#include <FairRootFileSink.h>
19#include <FairRunAna.h>
20#include <FairRuntimeDb.h>
21#include <FairTask.h>
22#include <Logger.h>
23
24#include "TaskFactory.h"
25#include <TFile.h>
26#include <TGeoManager.h>
27#include <TStopwatch.h>
28#include <TString.h>
29#include <TTree.h>
30
31#include <cassert>
32#include <iostream>
33
34#include <sys/stat.h>
35
36namespace cbm::atconverter
37{
38
39
40 // ----- Constructor ----------------------------------------------------
41 Run::Run() : TNamed("Run", "CBM AnalysisTree Converter Run") {}
42 // --------------------------------------------------------------------------
43
44
45 // ----- Destructor -----------------------------------------------------
46 Run::~Run() { LOG(debug) << "Destructing " << fName; }
47 // --------------------------------------------------------------------------
48
49
50 // ----- Add a reconstruction task --------------------------------------
51 void Run::AddTask(FairTask* task)
52 {
53 fRun.AddTask(task);
54 LOG(info) << GetName() << ": Added task " << task->GetName();
55 }
56 // --------------------------------------------------------------------------
57
58
59 // ----- Check existence of a file --------------------------------------
60 bool Run::CheckFile(const char* fileName)
61 {
62 struct stat buffer;
63 return (stat(fileName, &buffer) == 0);
64 }
65 // --------------------------------------------------------------------------
66
67
68 // ----- Check existence of reco branch ---------------------------------
69 void Run::CheckRecoBranch(TTree* tree, ECbmModuleId detector)
70 {
71 TString branchName = ToString(detector);
72 branchName += "Hit";
73 if (tree->GetBranchStatus(branchName.Data())) {
74 LOG(info) << GetName() << ": Found branch " << branchName;
75 fDataPresent.insert(detector);
76 }
77 }
78 // --------------------------------------------------------------------------
79
80
81 // ----- Check which input reco branches are present --------------------
82 void Run::CheckInputBranches(FairFileSource* source)
83 {
84 TFile* inFile = source->GetInFile();
85 if (!inFile) throw std::runtime_error("No input file");
86 auto* inTree = inFile->Get<TTree>("cbmsim");
87 if (!inTree) throw std::runtime_error("No input tree");
88
89 for (ECbmModuleId detector = ECbmModuleId::kMvd; detector != ECbmModuleId::kNofSystems; ++detector)
90 CheckRecoBranch(inTree, detector);
91 }
92 // --------------------------------------------------------------------------
93
94
95 // ----- Create the topology --------------------------------------------
97 {
98
99 TaskFactory fact(this);
100
103 fact.RegisterCaTracking();
104 fact.RegisterTrdPid();
106 }
107 // --------------------------------------------------------------------------
108
109
110 // ----- Execute reconstruction run -------------------------------------
112 {
113
114 // --- Mirror options and configuration
115 LOG(info) << GetName() << ": Output file is " << fOutput;
116 for (auto traFile : fTra) {
117 LOG(info) << GetName() << ": Transport file is " << traFile;
118 if (!CheckFile(traFile.Data())) throw std::runtime_error("Transport file does not exist");
119 }
120 LOG(info) << GetName() << ": Digitization file is " << fRaw;
121 LOG(info) << GetName() << ": Parameter file is " << fPar;
122 LOG(info) << GetName() << ": Reconstruction file is " << fReco;
123 LOG(info) << GetName() << ": Geometry setup is " << fSetupTag;
124 LOG(info) << "Configuration: \n" << fConfig.ToString() << "\n";
125
126 // --- Timer
127 TStopwatch timer;
128 timer.Start();
129
130 // --- Run info
131 fRun.SetGenerateRunInfo(true);
132
133 // --- Logger settings
134 FairLogger::GetLogger()->SetLogScreenLevel(fConfig.f_glb_logLevel.data());
135 FairLogger::GetLogger()->SetLogVerbosityLevel(fConfig.f_glb_logVerbose.data());
136 FairLogger::GetLogger()->SetColoredLog(fConfig.f_glb_logColor.data());
137
138 // --- Check input, output and parameter files
139 if (CheckFile(fOutput.Data()) && !fOverwrite) throw std::runtime_error("Output file already exists");
140 if (!CheckFile(fRaw.Data())) throw std::runtime_error("Digitizazion (raw) file does not exist");
141 if (!CheckFile(fPar.Data())) throw std::runtime_error("Parameter file does not exist");
142 if (!CheckFile(fReco.Data())) throw std::runtime_error("Reconstruction file does not exist");
143
144 // --- Input and output
145 FairFileSource* source = new FairFileSource(fReco);
146 for (auto traFile : fTra)
147 source->AddFriend(traFile);
148 source->AddFriend(fRaw);
149 fRun.SetSource(source);
150 fRun.SetSink(new FairRootFileSink(fOutput));
151
152 // --- Check presence of input (reco) branches
153 LOG(info) << GetName() << ": Checking reco input...";
154 CheckInputBranches(source);
155
156 // --- Geometry setup
157 LOG(info) << GetName() << ": Loading setup " << fSetupTag;
160 // TODO: This CbmSetup business with singleton is not nice. Should be replaced eventually.
161
162 // --- Topology
163 LOG(info) << GetName() << ": Creating topology...";
165
166 // --- Parameter database
167 LOG(info) << GetName() << ": Set runtime DB...";
168 FairRuntimeDb* rtdb = fRun.GetRuntimeDb();
169 FairParRootFileIo* parIo1 = new FairParRootFileIo();
170 parIo1->open(fPar.Data(), "UPDATE");
171 rtdb->setFirstInput(parIo1);
172
173 // --- Initialisation
174 LOG(info) << GetName() << ": Initialise FairRun..." << std::endl;
175 fRun.Init();
176 rtdb->setOutput(parIo1);
177 rtdb->saveOutput();
178 rtdb->print();
179 timer.Stop();
180 double timeInit = timer.RealTime();
181 timer.Start();
182
183 // --- Execute run
184 std::cout << std::endl << std::endl;
185 LOG(info) << GetName() << ": Starting run" << std::endl;
187
188 // --- Finish
189 timer.Stop();
190 double timeExec = timer.RealTime();
191 FairMonitor::GetMonitor()->Print();
192 std::cout << std::endl << std::endl;
193 LOG(info) << GetName() << ": Execution successful";
194 for (auto traFile : fTra)
195 LOG(info) << GetName() << ": Transport file was " << traFile;
196 LOG(info) << GetName() << ": Digitization file was " << fRaw;
197 LOG(info) << GetName() << ": Parameter file was " << fPar;
198 LOG(info) << GetName() << ": Reconstruction file was " << fReco;
199 LOG(info) << GetName() << ": Output file is " << fOutput;
200 LOG(info) << GetName() << ": Execution time: Init " << timeInit << " s, Exec " << timeExec << "s";
201 }
202 // --------------------------------------------------------------------------
203
204
205 // ----- Read configuration from YAML file -------------------------------
206 void Run::LoadConfig(const char* fileName)
207 {
208
209 TString file(fileName);
210 if (file.IsNull()) {
211 file = std::getenv("VMCWORKDIR");
212 file += "/reco/offline/config/RecoConfig_event_ideal.yaml";
213 }
214 LOG(info) << GetName() << ": Loading configuration from " << file;
215 fConfig.LoadYaml(file.Data());
216 }
217 // --------------------------------------------------------------------------
218
219
220 // ----- Set transport input sources -------------------------------------
221 void Run::SetTraFiles(const std::vector<std::string> files)
222 {
223 for (auto file : files) {
224 const char* filename = file.c_str();
225 TString traFile = filename;
226 fTra.push_back(traFile);
227 }
228 }
229 // --------------------------------------------------------------------------
230
231
232} // namespace cbm::atconverter
233
ClassImp(CbmConverterManager)
std::string ToString(ECbmModuleId modId)
Definition CbmDefs.cxx:70
ECbmModuleId
Definition CbmDefs.h:39
@ kMvd
Micro-Vertex Detector.
@ kNofSystems
For loops over active systems.
static CbmSetup * Instance()
Definition CbmSetup.cxx:160
void LoadSetup(const char *setupName)
Definition CbmSetup.h:64
void LoadYaml(const std::string &filename)
Load from YAML file.
void CheckInputBranches(FairFileSource *source)
Check the presence of reco input branches.
bool CheckFile(const char *fileName)
Check existence of a file.
void SetTraFiles(const std::vector< std::string > files)
Set transport input files.
void CheckRecoBranch(TTree *tree, ECbmModuleId detector)
Check and mark presence of reco branches.
void CreateTopology()
Create the reconstruction task topology (chain)
void AddTask(FairTask *task)
Add a task to the run.
void LoadConfig(const char *fileName)
Set configuration file name.
Factory class for the instantiation of CBM analysis tree converter tasks.
void RegisterConverterManager(const TString &outputFile)
AnalysisTree Converter Manager.
void RegisterMCDataManager(const std::vector< TString > &traFiles)
MC data manager for matching.