Skip to content

Commit 4f14e9a

Browse files
committed
init root files
1 parent 70e2bab commit 4f14e9a

4 files changed

Lines changed: 59 additions & 24 deletions

File tree

gstreamer/examples/gstreamer_example.cc

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ auto build_event(int
117117
// Multithreaded driver
118118
// Each thread repeatedly grabs the next event number from an atomic counter
119119
// -----------------------------------------------------------------------------
120-
void run_simulation(int nevents,
121-
int nthreads,
122-
const std::shared_ptr<GLogger>& logs,
123-
const std::shared_ptr<GLogger>& loge,
124-
const std::shared_ptr<GLogger>& logt,
125-
const std::unordered_map<std::string, std::shared_ptr<GDynamicDigitization>>& dynamicRoutinesMap,
126-
GOptions* gopt,
127-
std::vector<std::unique_ptr<GEventDataCollection>>& runData,
128-
std::mutex& runDataMtx) {
120+
void run_simulation(int nevents,
121+
int nthreads,
122+
const std::shared_ptr<GLogger>& logs,
123+
const std::shared_ptr<GLogger>& loge,
124+
const std::shared_ptr<GLogger>& logt,
125+
const std::unordered_map<std::string, std::shared_ptr<GDynamicDigitization>>& dynamicRoutinesMap,
126+
const std::vector<std::unordered_map<std::string, std::shared_ptr<GStreamer>>> streamers,
127+
std::vector<std::unique_ptr<GEventDataCollection>>& runData,
128+
std::mutex& runDataMtx) {
129129
// thread-safe integer counter starts at 1.
130130
// fetch_add returns the old value *and* bumps.
131131
// Zero contention: each thread fetches the next free event number.
@@ -155,12 +155,11 @@ void run_simulation(int
155155
logs->info(0, "worker ", tid, " started");
156156

157157
int localCount = 0; // events built by *this* worker
158+
// get streamer for this thread
159+
const auto& streamersMap = streamers[tid]; // get the map of gstreamers for this thread
158160

159-
auto gstreamer_defs = gstreamer::getGStreamerDefinition(gopt); // get the vector of GStreamerDefinition from options
160-
const auto& streamers = gstreamer::gstreamersMap(gstreamer_defs, tid, gopt, logs ); // all gstreamers for this thread
161161

162162
while (true) {
163-
164163
// repeatedly asks the shared atomic counter for “the next unclaimed event
165164
// number,” processes that event, stores the result, and goes back for more.
166165
// memory_order_relaxed: we only need *atomicity*, no ordering
@@ -200,17 +199,11 @@ void run_simulation(int
200199
}
201200

202201

203-
#include "TROOT.h"
204-
#include "TSystem.h"
205202

206203
// notice runData is not really used here, but we keep the code as reference on how to accumulate
207204
// events in a thread-safe way into a shared vector.
208205
int main(int argc, char* argv[]) {
209206

210-
// Trigger interpreter and global dictionary setup
211-
TClass::GetClass("TObject"); // this is a safe no-op that triggers interpreter init
212-
gSystem->Load("libCore"); // optional: explicitly load libraries
213-
214207
// runData holds the finished events. We store them as *unique_ptr* because
215208
// each event is owned by the container and *only* by the container (single
216209
// ownership → choose unique_ptr, not shared_ptr).
@@ -239,9 +232,12 @@ int main(int argc, char* argv[]) {
239232
constexpr int nevents = 200;
240233
constexpr int nthreads = 8;
241234

235+
auto gstreamer_defs = gstreamer::getGStreamerDefinition(gopts); // get the vector of GStreamerDefinition from options
236+
const auto& streamers= gstreamer::gstreamersMapVector(gstreamer_defs, nthreads, gopts, logs); // all gstreamers for this thread
237+
242238
run_simulation(nevents, nthreads, logs, loge, loge,
243239
dynRoutinesConstMap,
244-
gopts,
240+
streamers,
245241
runData,
246242
runDataMtx);
247243

gstreamer/factories/ROOT/gstreamerROOTConnection.cc

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,51 @@
22
#include "gstreamerROOTFactory.h"
33
#include "gstreamerConventions.h"
44

5+
// root
6+
#include "TROOT.h"
7+
#include "TSystem.h"
8+
9+
// c__
10+
#include <mutex> // for once_flag
11+
12+
// Static member definition
13+
std::once_flag GstreamerRootFactory::rootInitFlag;
14+
515
bool GstreamerRootFactory::openConnectionImpl() {
616

17+
// Trigger interpreter and global dictionary setup
18+
// this needs to be called in the main thread before any ROOT objects are created
19+
std::call_once(rootInitFlag, []() {
20+
TClass::GetClass("TObject"); // triggers interpreter
21+
gSystem->Load("libCore"); // optional
22+
// log->info(0, "GstreamerRootFactory: ROOT interpreter initialized");
23+
});
24+
725
rootfile = std::make_unique<TFile>(filename().c_str(), "RECREATE");
826
rootfile->cd();
9-
TDirectory::TContext ctx(rootfile.get()); // thread-local context
27+
TDirectory::TContext ctx(rootfile.get()); // thread-local context
1028

1129
gRootTrees = new std::map<std::string, GRootTree*>;
1230

13-
if (!rootfile->IsOpen()) { log->error(ERR_CANTOPENOUTPUT, "GstreamerRootFactory: could not open file " + filename()); }
14-
else { log->info(0, "GstreamerRootFactory: opened file " + filename()); }
31+
if (!rootfile->IsOpen()) {
32+
log->error(ERR_CANTOPENOUTPUT, "GstreamerRootFactory: could not open file " + filename());
33+
}
34+
else {
35+
log->info(0, "GstreamerRootFactory: opened file " + filename());
36+
}
1537

1638
return true;
1739
}
1840

1941
bool GstreamerRootFactory::closeConnectionImpl() {
20-
TDirectory::TContext ctx(rootfile.get()); // thread-local context
42+
TDirectory::TContext ctx(rootfile.get()); // thread-local context
2143

2244
if (rootfile->IsOpen()) {
2345
rootfile->Write();
2446
rootfile->Close();
2547
}
2648

27-
if ( rootfile->IsOpen() ) { log->error(ERR_CANTOPENOUTPUT, "GstreamerRootFactory: could not close file " + filename()); }
49+
if (rootfile->IsOpen()) { log->error(ERR_CANTOPENOUTPUT, "GstreamerRootFactory: could not close file " + filename()); }
2850

2951
delete gRootTrees;
3052

gstreamer/factories/ROOT/gstreamerROOTFactory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ class GstreamerRootFactory : public GStreamer {
4949
std::string filename() const override {
5050
return gstreamer_definitions.rootname + ".root";
5151
}
52+
53+
static std::once_flag rootInitFlag;
5254
};

gstreamer/gstreamer.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class GStreamer {
155155

156156
};
157157

158+
159+
158160
namespace gstreamer {
159161

160162
inline std::unordered_map<std::string, std::shared_ptr<GStreamer>> gstreamersMap(const std::vector<GStreamerDefinition>& goutput_defs,
@@ -186,5 +188,18 @@ inline std::unordered_map<std::string, std::shared_ptr<GStreamer>> gstreamersMap
186188
return gstreamerConstMap;
187189
}
188190

191+
// returns a vector of identical gstreamersMap the size of the number of threads
192+
inline std::vector<std::unordered_map<std::string, std::shared_ptr<GStreamer>>> gstreamersMapVector(const std::vector<GStreamerDefinition>& goutput_defs,
193+
int nthreads,
194+
GOptions* gopts,
195+
std::shared_ptr<GLogger> log) {
196+
std::vector<std::unordered_map<std::string, std::shared_ptr<GStreamer>>> gstreamersMaps;
197+
198+
for (int tid = 0; tid < nthreads; ++tid) {
199+
gstreamersMaps.emplace_back(gstreamersMap(goutput_defs, tid, gopts, log));
200+
}
201+
202+
return gstreamersMaps;
203+
}
189204

190205
} // namespace gstreamer

0 commit comments

Comments
 (0)