I’m in the process of developing an event processing application, and just wanted to check that my understanding of multithreaded I/O and TTrees is correct and that I’m not doing anything dumb…
The use case is simple:
[ol]
[li] Read a TTree from a TFile, the TTree having a branch holding “Event” objects.[/li]
[li] Read each entry from the TTree, passing the “Event” to a processing step.[/li]
[li] Process each “Event” in a separate thread (Pipeline/Tasks)[/li]
[li] Write processed “Event” and other data to an output TTree/TFile which are different to the input ones.[/li][/ol]
The first, second and last step would always be serial, and as I understand it, the read/access to the File and Tree are thread safe in this case?
The bit I’m a little unsure of is the parallel processing of the “Event” objects and hence how best to read/write them from the input/output Trees. Based on the User Guide for TTrees and the behaviour of SetBranchAddress and GetEntry regarding ownership, my current implementation for reading is:
TFile f("myfile.root","READ");
TTree* t = dynamic_cast<TTree*>(f.Get("MyEventTree"));
MyEvent* eventPtr(0);
t->SetBranchAddress("MyEventBranch", &eventPtr);
for (size_t i(0); i < t->GetEntries(); ++i) {
t->GetEntry(i);
processEvent(eventPtr); // Takes ownership of the instance
eventPtr=0;
}
Is that reasonable? My own testing indicates that it is - in the sense that a new Event instance is created for each entry, and that I can clean these myself without issue.
On the writing side, I have
void processEvent(MyEvent* eventPtr) {
// TFile/Tree created globally for now
outputTree->SetBranchAddress("MyEventOut", &eventPtr);
outputTree->Fill();
delete eventPtr;
}
Is this also o.k.? It does seems o.k. in terms of memory management, validity of data and that the two TFiles/Trees don’t clash with gDirectory.
Apologies if this is obvious or stupid, but I want to make sure I’m on the right track
If there’re parts of Root I’m missing that would help with this pattern, or if the code is reasonable, but could be improved in any way I’d welcome any advice!
Thanks,
Ben.