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 "Chain.hpp"
14#include "Cuts.hpp"
15#include "Matching.hpp"
16#include "Task.hpp"
17
18class TTree;
19class TFile;
20class TChain;
21
22namespace AnalysisTree {
23
24class Configuration;
25
26enum class eBranchWriteMode {
27 kCreateNewTree,
28 kCopyTree,
29 kNone
30};
31
32class TaskManager {
33
34 public:
35 static TaskManager* GetInstance();
36
37 TaskManager(TaskManager& other) = delete;
38 void operator=(const TaskManager&) = delete;
39 virtual ~TaskManager();
40
45 void SetOwnsTasks(bool owns_flag = true) { is_owns_tasks_ = owns_flag; }
46
52 virtual void Init(const std::vector<std::string>& filelists, const std::vector<std::string>& in_trees);
53
57 virtual void Init();
58 virtual void Run(long long nEvents = -1);
59 virtual void Finish();
60
61 void AddTask(Task* task) { tasks_.emplace_back(task); }
62
68 template<class BranchPtr>
69 void AddBranch(BranchPtr*& ptr, const BranchConfig& config) {
70 if (out_tree_ == nullptr) {
71 throw std::runtime_error("No output tree. Probably, TaskManager::Init() was not called or output file/tree names are not set.");
72 }
73
74 if (!ptr) {
75 ptr = new BranchPtr(config.GetId());
76 } else if (ptr->GetId() != config.GetId()) {
77 std::cout << "WARNING! ptr->GetId() != config.GetId()!" << std::endl;
78 ptr = new BranchPtr(config.GetId());
79 }
80 configuration_->AddBranchConfig(config);
81 if (write_mode_ == eBranchWriteMode::kCreateNewTree) {
82 chain_->GetConfiguration()->AddBranchConfig(config);
83 }
84
85 out_tree_->Branch((config.GetName() + ".").c_str(), &ptr);
86 }
87
88 void AddBranch(Branch* branch) {
89 switch (branch->GetBranchType()) {
90 case DetType::kParticle: {
91 AddBranch(branch->GetDataRaw<Particles*>(), branch->GetConfig());
92 break;
93 }
94 case DetType::kTrack: {
95 AddBranch(branch->GetDataRaw<TrackDetector*>(), branch->GetConfig());
96 break;
97 }
98 case DetType::kHit: {
99 AddBranch(branch->GetDataRaw<HitDetector*>(), branch->GetConfig());
100 break;
101 }
102 case DetType::kModule: {
103 AddBranch(branch->GetDataRaw<ModuleDetector*>(), branch->GetConfig());
104 break;
105 }
106 case DetType::kEventHeader: {
107 AddBranch(branch->GetDataRaw<EventHeader*>(), branch->GetConfig());
108 break;
109 }
110 case DetType::kGeneric: {
111 AddBranch(branch->GetDataRaw<GenericDetector*>(), branch->GetConfig());
112 break;
113 }
114 }
115 }
116
123 void AddMatching(const std::string& br1, const std::string& br2, Matching*& match) {
124 assert(!br1.empty() && !br2.empty() && !match);
125 if (out_tree_ == nullptr) {
126 throw std::runtime_error("No output tree. Probably, TaskManager::Init() was not called or output file/tree names are not set.");
127 }
128
129 match = new Matching(configuration_->GetBranchConfig(br1).GetId(),
130 configuration_->GetBranchConfig(br2).GetId());
131
132 configuration_->AddMatch(match);
133 out_tree_->Branch((configuration_->GetMatchName(br1, br2) + ".").c_str(), &match);
134 }
135
136 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetConfig() const { return chain_->GetConfiguration(); }
137 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetDataHeader() const { return chain_->GetDataHeader(); }
138 ANALYSISTREE_ATTR_NODISCARD Chain* GetChain() const { return chain_; }
139
140 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetOutConfig() const { return configuration_; }
141 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetOutDataHeader() const { return data_header_; }
142
143 void SetOutputDataHeader(DataHeader* dh) {
144 data_header_ = dh;
145 chain_->SetDataHeader(dh);// TODO
146 }
147 void FillOutput() { out_tree_->Fill(); }
148
149 void Exec();
150
151 std::vector<Task*>& Tasks() { return tasks_; }
152
153 void SetOutputName(std::string file, std::string tree = "aTree") {
154 out_file_name_ = std::move(file);
155 out_tree_name_ = std::move(tree);
156 fill_out_tree_ = true;
157 }
158
159 void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; }
160 void SetBranchesExclude(std::vector<std::string> brex) { branches_exclude_ = std::move(brex); }
161 void SetVerbosityPeriod(int value) { verbosity_period_ = value; }
162 void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; }
163 void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; }
164
165 void ClearTasks() { tasks_.clear(); }
166
167 protected:
168 TaskManager() = default;
169 static TaskManager* manager_;
170
171 void InitOutChain();
172 void InitTasks();
173 static void WriteCommitInfo();
174 static void PrintCommitInfo();
175
176 // input data members
177 Chain* chain_{nullptr};
178 std::vector<Task*> tasks_{};
179
180 // output data members
181 TFile* out_file_{nullptr};
182 TTree* out_tree_{nullptr};
185 std::string out_tree_name_{"aTree"};
186 std::string out_file_name_{"analysis_tree.root"};
187 std::vector<std::string> branches_exclude_{};
188
189 int verbosity_period_{-1};
190
191 // configuration parameters
192 eBranchWriteMode write_mode_{eBranchWriteMode::kCreateNewTree};
193 bool is_init_{false};
194 bool fill_out_tree_{false};
195 bool is_update_entry_in_exec_{true};
196 bool read_in_tree_{false};
197 bool is_owns_tasks_{true};
198 bool is_write_hash_info_{true};
199
200 ClassDef(TaskManager, 0);
201};
202
203};// namespace AnalysisTree
204
205#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:53
void SetOwnsTasks(bool owns_flag=true)
If TaskManager owns its tasks, they will be deleted on TaskManager' destruction.
Definition TaskManager.hpp:45
void AddBranch(BranchPtr *&ptr, const BranchConfig &config)
Definition TaskManager.hpp:69
Configuration * configuration_
output
Definition TaskManager.hpp:183
void AddMatching(const std::string &br1, const std::string &br2, Matching *&match)
Definition TaskManager.hpp:123
DataHeader * data_header_
output
Definition TaskManager.hpp:184
static TaskManager * GetInstance()
Definition TaskManager.cpp:12
Definition Task.hpp:25
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10