-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (118 loc) · 4.09 KB
/
main.cpp
File metadata and controls
134 lines (118 loc) · 4.09 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <fstream>
#include <thread>
#include <signal.h>
#include "PhysicalParam/ConfigManager.h"
#include "PhysicalParam/ParticleRange.h"
#include "histogram/RootWriter.h"
#include "Tasks/Tasks.h"
#include "Tasks/XIAReader.h"
#include "Tasks/Calibrator.h"
#include "Tasks/Buffer.h"
#include "Tasks/BufferSE.h"
#include "Tasks/Splitter.h"
#include "Tasks/SplitterSE.h"
#include "Tasks/Trigger.h"
#include "Tasks/Sort.h"
//#include "Tasks/MTSort.h"
#include "Tools/CommandLineInterface.h"
#include "Tools/ProgressUI.h"
#include "ThreadPool.hpp"
#include <TROOT.h>
#include <TFileMerger.h>
std::vector<std::string> RunSort(const CLI::Options &options, ProgressUI &progress)
{
std::ifstream cal_file;
try {
cal_file = std::ifstream(options.CalibrationFile.value());
} catch ( std::exception &e ){
// Pass, do nothing.
std::cerr << "Configuration file is missing." << std::endl;
return {};
}
cal_file.close();
auto cal = OCL::ConfigManager::FromFile(options.CalibrationFile.value().c_str());
ParticleRange particleRange( options.RangeFile.value_or("") );
auto userConfig = OCL::UserConfiguration::FromFile(options.CalibrationFile.value().c_str(),
options.Trigger.value(),
options.sortType.value(), particleRange);
std::string hist_file;
std::string tree_file;
std::string conf_file;
std::vector<std::string> root_files;
if ( options.tree.value() ) {
auto outname = options.output.value();
tree_file = outname;
hist_file = outname;
} else {
hist_file = options.output.value();
}
Task::XIAReader reader(options.input.value(), &progress);
Task::Calibrator calibrator(cal, reader.GetQueue());
Task::BufferSE buffer(calibrator.GetQueue());
Task::SplitterSE splitter(buffer.GetQueue(), options.SplitTime.value());
Task::Trigger trigger(splitter.GetQueue(), options.coincidenceTime.value(),
options.Trigger.value(), options.sortType.value());
const char *user_sort = nullptr;
if ( options.userSort.has_value() )
user_sort = options.userSort->c_str();
Task::Sorter sorter(trigger.GetQueue(), userConfig, ( tree_file.empty() ) ? nullptr : tree_file.c_str(), user_sort);
ThreadPool<std::thread> pool;
pool.AddTask(&reader);
pool.AddTask(&calibrator);
pool.AddTask(&buffer);
pool.AddTask(&splitter);
pool.AddTask(&trigger);
pool.AddTask(&sorter);
try {
pool.Wait();
} catch ( const std::exception &ex ){
std::cerr << "Got exception: " << ex.what() << std::endl;
}
Histograms &hm = sorter.GetHistograms();
if ( options.tree.value() ) {
RootWriter::Write(hm, hist_file.c_str(), nullptr, "UPDATE");
} else {
RootWriter::Write(hm, hist_file.c_str()/*, nullptr, "UPDATE"*/);
}
root_files.push_back(hist_file);
return root_files;
}
void MergeFiles(std::string &output_file, const std::vector<std::string> &files)
{
{
TFileMerger merger;
merger.OutputFile(output_file.c_str());
for (auto &file: files) {
merger.AddFile(file.c_str(), true);
}
merger.Merge();
}
// We can now delete all the files
for ( auto &file : files ){
std::cout << "Deleting file " << file << std::endl;
system(std::string("rm " + file).c_str());
}
}
int main(int argc, char *argv[])
{
ROOT::EnableThreadSafety();
//ROOT::EnableImplicitMT();
CLI::Options options;
try {
options = CLI::ParseCLA(argc, argv);
} catch ( std::exception &e ){
return 1; // Error
}
ProgressUI progress;
auto files = RunSort(options, progress);
if ( files.size() == 1 ) // Do nothing. No files to merge.
return 0;
else if ( files.size() > 1 ) {
auto spinner = progress.FinishSort(options.output.value());
MergeFiles(options.output.value(), files);
spinner.Finish();
} else if ( files.empty() )
return 1;
return 0;
}