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
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 }
111 }
112
119 void AddMatching(const std::string& br1, const std::string& br2, Matching*& match) {
120 assert(!br1.empty() && !br2.empty() && !match);
121 if (out_tree_ == nullptr) {
122 throw std::runtime_error("No output tree. Probably, TaskManager::Init() was not called or output file/tree names are not set.");
123 }
124
125 match = new Matching(configuration_->GetBranchConfig(br1).GetId(),
126 configuration_->GetBranchConfig(br2).GetId());
127
128 configuration_->AddMatch(match);
129 // if (write_mode_ == eBranchWriteMode::kCreateNewTree) {
130 // chain_->GetConfiguration()->AddMatch(match);
131 // }
132 out_tree_->Branch((configuration_->GetMatchName(br1, br2) + ".").c_str(), &match);
133 }
134
135 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetConfig() const { return chain_->GetConfiguration(); }
136 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetDataHeader() const { return chain_->GetDataHeader(); }
137 ANALYSISTREE_ATTR_NODISCARD Chain* GetChain() const { return chain_; }
138
139 ANALYSISTREE_ATTR_NODISCARD const Configuration* GetOutConfig() const { return configuration_; }
140 ANALYSISTREE_ATTR_NODISCARD const DataHeader* GetOutDataHeader() const { return data_header_; }
141
142 void SetOutputDataHeader(DataHeader* dh) {
143 data_header_ = dh;
144 chain_->SetDataHeader(dh);// TODO
145 }
146 void FillOutput() { out_tree_->Fill(); }
147
148 void Exec();
149
150 std::vector<Task*>& Tasks() { return tasks_; }
151
152 void SetOutputName(std::string file, std::string tree = "aTree") {
153 out_file_name_ = std::move(file);
154 out_tree_name_ = std::move(tree);
155 fill_out_tree_ = true;
156 }
157
158 void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; }
159 void SetBranchesExclude(std::vector<std::string> brex) { branches_exclude_ = std::move(brex); }
160 void SetVerbosityPeriod(int value) { verbosity_period_ = value; }
161 void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; }
162 void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; }
163
164 void ClearTasks() { tasks_.clear(); }
165
166 protected:
167 TaskManager() = default;
168 static TaskManager* manager_;
169
170 void InitOutChain();
171 void InitTasks();
172 static void WriteCommitInfo();
173 static void PrintCommitInfo();
174
175 // input data members
176 Chain* chain_{nullptr};
177 std::vector<Task*> tasks_{};
178
179 // output data members
180 TFile* out_file_{nullptr};
181 TTree* out_tree_{nullptr};
184 std::string out_tree_name_{"aTree"};
185 std::string out_file_name_{"analysis_tree.root"};
186 std::vector<std::string> branches_exclude_{};
187
188 int verbosity_period_{-1};
189
190 // configuration parameters
191 eBranchWriteMode write_mode_{eBranchWriteMode::kCreateNewTree};
192 bool is_init_{false};
193 bool fill_out_tree_{false};
194 bool is_update_entry_in_exec_{true};
195 bool read_in_tree_{false};
196 bool is_owns_tasks_{true};
197 bool is_write_hash_info_{true};
198
199 ClassDef(TaskManager, 0);
200};
201
202};// namespace AnalysisTree
203
204#endif//ANALYSISTREE_INFRA_TASKMANANGERNEW_HPP_
A class to store configuration of the Container.
Definition BranchConfig.hpp:118
Definition Branch.hpp:23
A class to store configuration of the whole AnalysisTree object.
Definition Configuration.hpp:58
Definition DataHeader.hpp:19
Definition Detector.hpp:21
Definition Matching.hpp:17
Definition TaskManager.hpp:32
virtual void Init()
Definition TaskManager.cpp:52
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:182
void AddMatching(const std::string &br1, const std::string &br2, Matching *&match)
Definition TaskManager.hpp:119
DataHeader * data_header_
output
Definition TaskManager.hpp:183
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