/
TaskManager.cpp
112 lines (89 loc) · 2.64 KB
/
TaskManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "TaskManager.hpp"
#include <iostream>
namespace AnalysisTree {
TaskManager* TaskManager::manager_ = nullptr;
TaskManager* TaskManager::GetInstance() {
/**
* This is a safer way to create an instance. instance = new Singleton is
* dangeruous in case two instance threads wants to access at the same time
*/
if (manager_ == nullptr) {
manager_ = new TaskManager;
}
return manager_;
}
void TaskManager::Init(const std::vector<std::string>& filelists, const std::vector<std::string>& in_trees) {
assert(!is_init_);
is_init_ = true;
read_in_tree_ = true;
chain_ = new Chain(filelists, in_trees);
std::set<std::string> branch_names{};
for (auto* task : tasks_) {
const auto& br = task->GetInputBranchNames();
branch_names.insert(br.begin(), br.end());
}
chain_->InitPointersToBranches(branch_names);
InitTasks();
}
void TaskManager::InitTasks() {
for (auto* task : tasks_) {
task->PreInit();
task->Init();
}
}
void TaskManager::Init() {
assert(!is_init_);
is_init_ = true;
InitOutChain();
chain_ = new Chain(out_tree_, configuration_, data_header_);
InitTasks();
}
void TaskManager::InitOutChain() {
out_file_ = TFile::Open(out_file_name_.c_str(), "recreate");
out_tree_ = new TTree(out_tree_name_.c_str(), "AnalysisTree");
configuration_ = new Configuration("Configuration");
data_header_ = new DataHeader;
}
void TaskManager::Run(long long nEvents) {
std::cout << "AnalysisTree::Manager::Run" << std::endl;
auto start = std::chrono::system_clock::now();
if (chain_->GetEntries() > 0) {
nEvents = nEvents < 0 || nEvents > chain_->GetEntries() ? chain_->GetEntries() : nEvents;
}
for (long long iEvent = 0; iEvent < nEvents; ++iEvent) {
if (read_in_tree_) {
chain_->GetEntry(iEvent);
}
Exec();
}// Event loop
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "elapsed time: " << elapsed_seconds.count() << ", per event: " << elapsed_seconds.count() / nEvents << "s\n";
}
void TaskManager::Finish() {
if (fill_out_tree_) {
out_file_->cd();
out_tree_->Write();
configuration_->Write("Configuration");
data_header_->Write("DataHeader");
out_file_->Close();
delete out_file_;
}
for (auto* task : tasks_) {
task->Finish();
delete task;
}
tasks_.clear();
// delete chain_;
if (fill_out_tree_) {
delete configuration_;
delete data_header_;
// delete out_tree_;
}
out_tree_name_ = "aTree";
out_file_name_ = "analysis_tree.root";
is_init_ = false;
fill_out_tree_ = false;
read_in_tree_ = false;
}
}// namespace AnalysisTree