GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Sorter.cc
Go to the documentation of this file.
1#include "Sorter.hh"
2
3#include <chrono>
4#include <iomanip>
5
6namespace GamR {
7namespace Sort {
8
9Sorter::Sorter(std::vector<std::string> fns, std::string out, Int_t compression)
10 : fInFilename(fns), fChain("EventTree"), fCounter(0), fTotalEvents(0), fStatus(kFALSE), fXbins(4096), fXlow(0),
11 fXhigh(4096), fYbins(4096), fYlow(0), fYhigh(4096), fZbins(512), fZlow(0), fZhigh(512)
12{
13 ROOT::EnableImplicitMT(std::thread::hardware_concurrency());
14
15 for (const auto &fn : fInFilename) {
16 fChain.Add(fn.c_str());
17 }
18 fTotalEvents = fChain.GetEntries();
19
20 // fProcessor = std::make_unique<ROOT::TTreeProcessorMT>(fChain);
21 fProcessor = new ROOT::TTreeProcessorMT(fChain);
22
23 fOutFile = new TFile(out.c_str(), "UPDATE");
24 if (fOutFile->IsZombie() || !fOutFile->IsOpen())
25 throw std::runtime_error("Error opening output file: " + out);
26 fOutFile->SetCompressionSettings(compression);
27}
28
30{
31 fOutFile->Close();
32 delete fProcessor;
33}
34
35void Sorter::Begin()
36{
37 fCounter.store(0);
38 fTimeStart = std::chrono::system_clock::now();
39}
40
42{
43 fStatus.store(kTRUE);
44}
45
46void Sorter::End()
47{
49 Halt();
50}
51
53{
54 if (fMutex.try_lock()) {
55 auto now = std::chrono::system_clock::now();
56 std::cout << "\rProcessed Events: ";
57 std::cout << fCounter << " / " << fTotalEvents;
58
59 std::cout << std::fixed;
60 std::cout << std::setprecision(0);
61 std::cout << std::noshowpoint;
62 std::cout << " [" << 100.0 * fCounter / fTotalEvents << "%] ";
63
64 std::chrono::duration<double> sec = now - fTimeStart;
65 auto speed = fCounter / sec.count();
66 auto remain = static_cast<ULong64_t>((fTotalEvents - fCounter) / speed);
67 std::cout << "Time Remaining: ";
68 std::cout << remain / 3600 << ":";
69 std::cout << std::setw(2) << std::setfill('0') << remain / 60 % 60 << ":";
70 std::cout << std::setw(2) << std::setfill('0') << remain % 60;
71 std::cout << " [" << speed / 1000 << " k/s]";
72 std::cout << std::flush;
73 fMutex.unlock();
74 }
75}
76
78{
79 ++fCounter;
80}
81
83{
84 fSafeSpecs.clear();
85 fSafeMats.clear();
86 fSafeCubes.clear();
87}
88
90{
91 return fStatus.load();
92}
93void Sorter::Write(std::string folder)
94{
95 std::cout << std::endl << "Spectra" << std::endl;
96 for (auto &h : fSafeSpecs) {
97 fOutFile->cd(folder.c_str());
98 auto m = h.second.Merge();
99 m->Write("", TObject::kWriteDelete);
100 std::cout << "\nWritten\t" << m->GetName();
101 std::cout << "\t=>\t" << fOutFile->GetName() << std::endl;
102 }
103
104 std::cout << std::endl << "Matrices" << std::endl;
105 for (auto &h : fSafeMats) {
106 fOutFile->cd(folder.c_str());
107 auto m = h.second.Merge();
108 m->Write("", TObject::kWriteDelete);
109 std::cout << "Written\t" << m->GetName();
110 std::cout << "\t=>\t" << fOutFile->GetName() << std::endl;
111 }
112
113 std::cout << std::endl << "Cubes" << std::endl;
114 for (auto &h : fSafeCubes) {
115 fOutFile->cd(folder.c_str());
116 auto m = h.second.Merge();
117 m->Write("", TObject::kWriteDelete);
118 std::cout << "Written\t" << m->GetName();
119 std::cout << "\t=>\t" << fOutFile->GetName() << std::endl;
120 }
121}
122
123void Sorter::WriteObject(TObject *obj, const char *folder /* = "/"*/, const char *rename /* = "" */, bool quiet)
124{
125 if (fMutex.try_lock()) {
126 if (!fOutFile->GetDirectory(folder)) {
127 fOutFile->cd("/");
128 fOutFile->mkdir(folder);
129 }
130 fOutFile->cd(folder);
131 obj->Write(rename, TObject::kWriteDelete);
132 if (!quiet) {
133 std::cout << std::endl << "Objects" << std::endl;
134 std::cout << "Written\t" << obj->GetName();
135 std::cout << "\t=>\t" << fOutFile->GetName() << ":" << folder << "/" << std::endl;
136 }
137 fOutFile->cd("/");
138 fMutex.unlock();
139 }
140}
141
142void Sorter::SetXsize(ULong64_t bins, Long64_t low, Long64_t high)
143{
144 fXbins = bins;
145 fXlow = low;
146 fXhigh = high;
147}
148
149void Sorter::SetYsize(ULong64_t bins, Long64_t low, Long64_t high)
150{
151 fYbins = bins;
152 fYlow = low;
153 fYhigh = high;
154}
155
156void Sorter::SetZsize(ULong64_t bins, Long64_t low, Long64_t high)
157{
158 fZbins = bins;
159 fZlow = low;
160 fZhigh = high;
161}
162
163std::shared_ptr<TH1D> Sorter::GetSpectrum(std::string s)
164{
165 std::shared_ptr<TH1D> h = fSafeSpecs[s].Get();
166 h->SetName(s.c_str());
167 h->SetTitle(s.c_str());
168 h->SetBins(fXbins, fXlow, fXhigh);
169 std::cout << std::endl << "Spectrum\t" << s << std::endl;
170 return h;
171}
172
173std::shared_ptr<TH2D> Sorter::GetMatrix(std::string s)
174{
175 std::shared_ptr<TH2D> h = fSafeMats[s].Get();
176 h->SetName(s.c_str());
177 h->SetTitle(s.c_str());
178 h->SetBins(fXbins, fXlow, fXhigh, fYbins, fYlow, fYhigh);
179 std::cout << std::endl << "Matrix\t" << s << std::endl;
180 return h;
181}
182
183std::shared_ptr<TH3D> Sorter::GetCube(std::string s)
184{
185 std::shared_ptr<TH3D> h = fSafeCubes[s].Get();
186 h->SetName(s.c_str());
187 h->SetTitle(s.c_str());
188 h->SetBins(fXbins, fXlow, fXhigh, fYbins, fYlow, fYhigh, fZbins, fZlow, fZhigh);
189 std::cout << std::endl << "Cube\t" << s << std::endl;
190 return h;
191}
192
193} // namespace Sort
194} // namespace GamR
void SetZsize(ULong64_t bins, Long64_t low, Long64_t high)
Definition Sorter.cc:156
void PrintStatus()
Definition Sorter.cc:52
void WriteObject(TObject *obj, const char *folder="/", const char *rename="", bool quiet=kFALSE)
Definition Sorter.cc:123
std::shared_ptr< TH3D > GetCube(std::string s)
Definition Sorter.cc:183
void SetXsize(ULong64_t bins, Long64_t low, Long64_t high)
Definition Sorter.cc:142
Sorter(std::vector< std::string > fns, std::string out, Int_t compression=101)
Definition Sorter.cc:9
void Write(std::string folder="/")
Definition Sorter.cc:93
Bool_t Done()
Definition Sorter.cc:89
std::shared_ptr< TH2D > GetMatrix(std::string s)
Definition Sorter.cc:173
std::shared_ptr< TH1D > GetSpectrum(std::string s)
Definition Sorter.cc:163
void SetYsize(ULong64_t bins, Long64_t low, Long64_t high)
Definition Sorter.cc:149
Definition Gain.cc:19