forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootIO.cc
279 lines (241 loc) · 7.8 KB
/
RootIO.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celer-g4/RootIO.cc
//---------------------------------------------------------------------------//
#include "RootIO.hh"
#include <cstdio>
#include <regex>
#include <G4Event.hh>
#include <G4RunManager.hh>
#include <G4Threading.hh>
#include <TBranch.h>
#include <TFile.h>
#include <TObject.h>
#include <TROOT.h>
#include <TTree.h>
#include "corecel/Macros.hh"
#include "corecel/io/Logger.hh"
#include "celeritas/ext/GeantUtils.hh"
#include "celeritas/ext/RootFileManager.hh"
#include "accel/ExceptionConverter.hh"
#include "accel/SetupOptions.hh"
#include "GlobalSetup.hh"
#include "SensitiveHit.hh"
#ifdef _WIN32
# include <process.h>
#else
# include <unistd.h>
#endif
namespace celeritas
{
namespace app
{
//---------------------------------------------------------------------------//
/*!
* Create a ROOT output file for each worker thread in MT.
*/
RootIO::RootIO()
{
CELER_VALIDATE(RootFileManager::use_root(),
<< "cannot interface with ROOT (disabled by user "
"environment)");
ROOT::EnableThreadSafety();
file_name_ = std::regex_replace(
GlobalSetup::Instance()->GetSetupOptions()->output_file,
std::regex("\\.json$"),
".root");
if (file_name_.empty())
{
file_name_ = "celer-g4.root";
}
if (file_name_ == "-")
{
file_name_ = "stdout-" + std::to_string(::getpid()) + ".root";
}
if (G4Threading::IsWorkerThread())
{
file_name_ += std::to_string(G4Threading::G4GetThreadId());
}
if (G4Threading::IsWorkerThread()
|| !G4Threading::IsMultithreadedApplication())
{
CELER_LOG_LOCAL(info)
<< "Creating ROOT event output file at '" << file_name_ << "'";
file_.reset(TFile::Open(file_name_.c_str(), "recreate"));
CELER_VALIDATE(file_->IsOpen(), << "failed to open " << file_name_);
tree_.reset(new TTree(
this->TreeName(), "event_hits", this->SplitLevel(), file_.get()));
}
}
//---------------------------------------------------------------------------//
/*!
* Return the static thread local singleton instance.
*/
RootIO* RootIO::Instance()
{
static G4ThreadLocal RootIO instance;
return &instance;
}
//---------------------------------------------------------------------------//
/*!
* Write sensitive hits to output in the form of EventData.
* See celeritas/io/EventData.hh
*/
void RootIO::Write(G4Event const* event)
{
auto const hit_cols = event->GetHCofThisEvent();
if (!hit_cols)
{
return;
}
// Populate EventData using the collections of sensitive hits
EventData event_data;
event_data.hits.resize(detector_name_id_map_.size());
event_data.event_id = event->GetEventID();
for (auto i : celeritas::range(hit_cols->GetNumberOfCollections()))
{
auto const* hc_id = hit_cols->GetHC(i);
std::vector<EventHitData> hits;
hits.resize(hc_id->GetSize());
for (auto j : celeritas::range(hc_id->GetSize()))
{
auto* hit_id = dynamic_cast<SensitiveHit*>(hc_id->GetHit(j));
hits[j] = hit_id->data();
}
auto const iter = detector_name_id_map_.find(hc_id->GetName());
CELER_ASSERT(iter != detector_name_id_map_.end());
event_data.hits[iter->second] = std::move(hits);
}
this->WriteObject(&event_data);
}
//---------------------------------------------------------------------------//
/*!
* Fill event tree with event data.
*
* \note `tree_->Fill()` will import the data from *all* existing TBranches. So
* this code expects to have only one TBranch in this TTree.
*/
void RootIO::WriteObject(EventData* event_data)
{
if (!event_branch_)
{
// TODO: expose as environment variable if needed
int const root_buffer_size{128000};
event_branch_ = tree_->Branch(
"event", &event_data, root_buffer_size, this->SplitLevel());
}
else
{
event_branch_->SetAddress(&event_data);
}
tree_->Fill();
event_branch_->ResetAddress();
}
//---------------------------------------------------------------------------//
/*!
* Map sensitive detectors to contiguous IDs.
*/
void RootIO::AddSensitiveDetector(std::string name)
{
auto&& [iter, inserted]
= detector_name_id_map_.insert({std::move(name), ++detector_id_});
CELER_ASSERT(inserted);
CELER_DISCARD(iter);
}
//---------------------------------------------------------------------------//
/*!
* Write and Close or Merge output.
*/
void RootIO::Close()
{
CELER_EXPECT((file_ && file_->IsOpen())
|| (G4Threading::IsMultithreadedApplication()
&& G4Threading::IsMasterThread()));
if (!G4Threading::IsMultithreadedApplication())
{
CELER_LOG(info) << "Writing hit ROOT output to " << file_name_;
CELER_ASSERT(tree_);
file_->Write("", TObject::kOverwrite);
}
else
{
if (G4Threading::IsMasterThread())
{
// Merge output file on the master thread
this->Merge();
}
else
{
CELER_LOG(debug) << "Writing temporary local ROOT output";
file_->Write("", TObject::kOverwrite);
}
}
event_branch_ = nullptr;
tree_.reset();
file_.reset();
}
//---------------------------------------------------------------------------//
/*!
* Merging output root files from multiple threads using TTree::MergeTrees.
*
* TODO: use TBufferMerger and follow the example described in the ROOT
* tutorials/multicore/mt103_fillNtupleFromMultipleThreads.C which stores
* TBuffer data in memory and writes 32MB compressed output concurrently.
*/
void RootIO::Merge()
{
auto const nthreads = celeritas::get_geant_num_threads();
std::vector<TFile*> files;
std::vector<TTree*> trees;
std::unique_ptr<TList> list(new TList);
CELER_LOG_LOCAL(info) << "Merging hit root files from " << nthreads
<< " threads into \"" << file_name_ << "\"";
for (int i = 0; i < nthreads; ++i)
{
std::string file_name = file_name_ + std::to_string(i);
files.push_back(TFile::Open(file_name.c_str()));
trees.push_back((TTree*)(files[i]->Get(this->TreeName())));
list->Add(trees[i]);
if (i == nthreads - 1)
{
auto* file = TFile::Open(file_name_.c_str(), "recreate");
CELER_VALIDATE(file->IsOpen(), << "failed to open " << file_name_);
auto* tree = TTree::MergeTrees(list.get());
tree->SetName(this->TreeName());
// Store sensitive detector map branch
this->StoreSdMap(file);
// Write both the TFile and TTree meta-data
file->Write();
file->Close();
}
// Delete the merged file
std::remove(file_name.c_str());
}
}
//---------------------------------------------------------------------------//
/*!
* Store TTree with sensitive detector names and their IDs (used by
* EventData).
*/
void RootIO::StoreSdMap(TFile* file)
{
CELER_EXPECT(file && file->IsOpen());
auto tree = new TTree(
"sensitive_detectors", "name_to_id", this->SplitLevel(), file);
std::string name;
unsigned int id;
tree->Branch("name", &name);
tree->Branch("id", &id);
for (auto const& iter : detector_name_id_map_)
{
name = iter.first;
id = iter.second;
tree->Fill();
}
}
//---------------------------------------------------------------------------//
} // namespace app
} // namespace celeritas