AnalysisTree
Loading...
Searching...
No Matches
TaskManager.hpp
1/* Copyright (C) 2019-2021 GSI, Universität Tübingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Viktor Klochkov, Ilya Selyuzhenkov */
4#ifndef ANALYSISTREE_INFRA_TASKMANANGERNEW_HPP_
5#define ANALYSISTREE_INFRA_TASKMANANGERNEW_HPP_
6
7#include <chrono>
8#include <iostream>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include <Compression.h>
14
15#include "Chain.hpp"
16#include "Cuts.hpp"
17#include "Matching.hpp"
18#include "Task.hpp"
19
20class TTree;
21class TFile;
22class TChain;
23
24namespace AnalysisTree {
25
26class Configuration;
27
28enum class eBranchWriteMode {
29 kCreateNewTree,
30 kCopyTree,
31 kNone
32};
33
34class TaskManager {
35
36 public:
37 static TaskManager* GetInstance();
38
39 TaskManager(TaskManager& other) = delete;
40 void operator=(const TaskManager&) = delete;
41 virtual ~TaskManager();
42
47 void SetOwnsTasks(bool owns_flag = true) { is_owns_tasks_ = owns_flag; }
48
54 virtual void Init(const std::vector<std::string>& filelists, const std::vector<std::string>& in_trees);
55
59 virtual void Init();
60 void Run(long long nEventFrom, long long nEvents);
61 void Run(long long nEvents = -1);
62 virtual void Finish();
63
64 void AddTask(Task* task) { tasks_.emplace_back(task); }
65
71 template<class BranchPtr>
72 void AddBranch(BranchPtr*& ptr, const BranchConfig& config) {
73 if (out_tree_ == nullptr) {
74 throw std::runtime_error("No output tree. Probably, TaskManager::Init() was not called or output file/tree names are not set.");
75 }
76
77 if (!ptr) {
78 ptr = new BranchPtr(config.GetId());
79 } else if (ptr->GetId() != config.GetId()) {
80 std::cout << "WARNING! ptr->GetId() != config.GetId()!" << std::endl;
81 ptr = new BranchPtr(config.GetId());
82 }
83 configuration_->AddBranchConfig(config);
84 if (write_mode_ == eBranchWriteMode::kCreateNewTree) {
85 chain_->GetConfiguration()->AddBranchConfig(config);
86 }
87
88 out_tree_->Branch((config.GetName() + ".").c_str(), &ptr);
89 }
90
91 void AddBranch(Branch* branch) {
92 switch (branch->GetBranchType()) {
93 case DetType::kParticle: {
94 AddBranch(branch->GetDataRaw<Particles*>(), branch->GetConfig());
95 break;
96 }
97 case DetType::kTrack: {
98 AddBranch(branch->GetDataRaw<TrackDetector*>(), branch->GetConfig());
99 break;
100 }
101 case DetType::kHit: {
102 AddBranch(branch->GetDataRaw<HitDetector*>(), branch->GetConfig());
103 break;
104 }
105 case DetType::kModule: {
106 AddBranch(branch->GetDataRaw<ModuleDetector*>(), branch->GetConfig());
107 break;
108 }
109 case DetType::kEventHeader: {
110 AddBranch(branch->GetDataRaw<EventHeader*>(), branch->GetConfig());
111 break;
112 }
113 case DetType::kGeneric: {
114 AddBranch(branch->GetDataRaw<GenericDetector*>(), branch->GetConfig());
115 break;
116 }
117 }
118 }
119
126 void AddMatching(const std::string& br1, const std::string& br2, Matching*& match) {
127 assert(!br1.empty() && !br2.empty() && !match);
128 if (out_tree_ == nullptr) {
129 throw std::runtime_error("No output tree. Probably, TaskManager::Init() was not called or output file/tree names are not set.");
130 }
131
132 match = new Matching(configuration_->GetBranchConfig(br1).GetId(),
133 configuration_->GetBranchConfig(br2).GetId());
134
135 configuration_->AddMatch(match);
136 out_tree_->Branch((configuration_->GetMatchName(br1, br2) + ".").c_str(), &match);
137 }
138
139 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetConfig() const { return chain_->GetConfiguration(); }
140 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetDataHeader() const { return chain_->GetDataHeader(); }
141 ANALYSISTREE_ATTR_NODISCARD Chain* GetChain() const { return chain_; }
142
143 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetOutConfig() const { return configuration_; }
144 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetOutDataHeader() const { return data_header_; }
145
146 void SetOutputDataHeader(DataHeader* dh) {
147 data_header_ = dh;
148 chain_->SetDataHeader(dh);// TODO
149 }
150 void FillOutput() { out_tree_->Fill(); }
151
152 void Exec();
153
154 std::vector<Task*>& Tasks() { return tasks_; }
155
156 void SetOutputName(std::string file, std::string tree = "aTree") {
157 out_file_name_ = std::move(file);
158 out_tree_name_ = std::move(tree);
159 fill_out_tree_ = true;
160 }
161
162 void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; }
163 void SetBranchesExclude(std::vector<std::string> brex) { branches_exclude_ = std::move(brex); }
164 void SetVerbosityPeriod(int value) { verbosity_period_ = value; }
165 void SetVerbosityFrequency(int value) { verbosity_frequency_ = value; }
166 void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; }
167 void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; }
168
173 void SetCompressionSettings(int settings) { compression_settings_ = settings; }
174
179 void SetAutoFlush(Long64_t value) {
180 auto_flush_ = value;
181 is_set_auto_flush_ = true;
182 }
183
184 void ClearTasks() { tasks_.clear(); }
185
186 protected:
187 TaskManager() = default;
188 static TaskManager* manager_;
189
190 void InitOutChain();
191 void InitTasks();
192 static void WriteCommitInfo();
193 static void PrintCommitInfo();
194
195 // input data members
196 Chain* chain_{nullptr};
197 std::vector<Task*> tasks_{};
198
199 // output data members
200 TFile* out_file_{nullptr};
201 TTree* out_tree_{nullptr};
204 std::string out_tree_name_{"aTree"};
205 std::string out_file_name_{"analysis_tree.root"};
206 std::vector<std::string> branches_exclude_{};
207
208 int verbosity_period_{-1};
209 int verbosity_frequency_{-1};
210
211 int compression_settings_{ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault};
212 Long64_t auto_flush_{0};
213 bool is_set_auto_flush_{false};
214
215 // configuration parameters
216 eBranchWriteMode write_mode_{eBranchWriteMode::kCreateNewTree};
217 bool is_init_{false};
218 bool fill_out_tree_{false};
219 bool is_update_entry_in_exec_{true};
220 bool read_in_tree_{false};
221 bool is_owns_tasks_{true};
222 bool is_write_hash_info_{true};
223
224 ClassDef(TaskManager, 0);
225};
226
227};// namespace AnalysisTree
228
229#endif//ANALYSISTREE_INFRA_TASKMANANGERNEW_HPP_
A class to store configuration of the Container.
Definition BranchConfig.hpp:89
Definition Branch.hpp:23
A class to store configuration of the whole AnalysisTree object.
Definition Configuration.hpp:58
Definition DataHeader.hpp:19
Definition Matching.hpp:17
virtual void Init()
Definition TaskManager.cpp:56
void SetAutoFlush(Long64_t value)
Sets TTree::SetAutoFlush() of the output tree.
Definition TaskManager.hpp:179
void SetOwnsTasks(bool owns_flag=true)
If TaskManager owns its tasks, they will be deleted on TaskManager' destruction.
Definition TaskManager.hpp:47
void SetCompressionSettings(int settings)
Sets compression of the output file and all output branches.
Definition TaskManager.hpp:173
void AddBranch(BranchPtr *&ptr, const BranchConfig &config)
Definition TaskManager.hpp:72
Configuration * configuration_
output
Definition TaskManager.hpp:202
void AddMatching(const std::string &br1, const std::string &br2, Matching *&match)
Definition TaskManager.hpp:126
DataHeader * data_header_
output
Definition TaskManager.hpp:203
static TaskManager * GetInstance()
Definition TaskManager.cpp:15
Definition Task.hpp:25
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10