Skip to content

Commit 9330780

Browse files
committed
Changing TTreeCache now requires specifying the TTree
1 parent 7e1b23e commit 9330780

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

DataFormats/FWLite/src/DataGetterHelper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace fwlite {
130130
////END OF WORK AROUND
131131

132132
if (tcUse_) {
133-
TTreeCache* tcache = dynamic_cast<TTreeCache*>(branchMap_->getFile()->GetCacheRead());
133+
TTreeCache* tcache = dynamic_cast<TTreeCache*>(branchMap_->getFile()->GetCacheRead(nullptr));
134134

135135
if (nullptr != tcache) {
136136
if (!tcTrained_) {

IOPool/Input/src/InputFile.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ namespace edm {
4646
static void reportReadBranch(InputType inputType, std::string const& branchname);
4747

4848
TObject* Get(char const* name) { return file_->Get(name); }
49-
TFileCacheRead* GetCacheRead() const { return file_->GetCacheRead(); }
50-
void SetCacheRead(TFileCacheRead* tfcr) { file_->SetCacheRead(tfcr, nullptr, TFile::kDoNotDisconnect); }
49+
TFileCacheRead* GetCacheRead(TObject* iTree) const { return file_->GetCacheRead(iTree); }
50+
void SetCacheRead(TFileCacheRead* tfcr, TObject* iTree) {
51+
file_->SetCacheRead(tfcr, iTree, TFile::kDoNotDisconnect);
52+
}
5153
void logFileAction(char const* msg, char const* fileName) const;
5254

5355
private:

IOPool/Input/src/RootFile.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ namespace edm {
302302
std::unique_ptr<TTreeCache> psetTreeCache =
303303
roottree::trainCache(psetTree.get(), *filePtr_, roottree::defaultNonEventCacheSize, "*");
304304
psetTreeCache->SetEnablePrefetching(false);
305-
filePtr_->SetCacheRead(psetTreeCache.get());
305+
filePtr_->SetCacheRead(psetTreeCache.get(), psetTree.get());
306306
for (Long64_t i = 0; i != psetTree->GetEntries(); ++i) {
307307
psetTree->GetEntry(i);
308308
psetMap.insert(idToBlob);
309309
}
310-
filePtr_->SetCacheRead(nullptr);
310+
filePtr_->SetCacheRead(nullptr, psetTree.get());
311311
}
312312

313313
// backward compatibility

IOPool/Input/src/RootTree.cc

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ namespace edm {
224224
void RootTree::setCacheSize(unsigned int cacheSize) {
225225
cacheSize_ = cacheSize;
226226
tree_->SetCacheSize(static_cast<Long64_t>(cacheSize));
227-
treeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead()));
227+
treeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead(tree_)));
228228
if (treeCache_)
229229
treeCache_->SetEnablePrefetching(enablePrefetching_);
230-
filePtr_->SetCacheRead(nullptr);
230+
filePtr_->SetCacheRead(nullptr, tree_);
231231
rawTreeCache_.reset();
232232
}
233233

@@ -245,7 +245,7 @@ namespace edm {
245245
}
246246

247247
void RootTree::setEntryNumber(EntryNumber theEntryNumber) {
248-
filePtr_->SetCacheRead(treeCache_.get());
248+
filePtr_->SetCacheRead(treeCache_.get(), tree_);
249249

250250
// Detect a backward skip. If the skip is sufficiently large, we roll the dice and reset the treeCache.
251251
// This will cause some amount of over-reading: we pre-fetch all the events in some prior cluster.
@@ -283,7 +283,7 @@ namespace edm {
283283

284284
entryNumber_ = theEntryNumber;
285285
tree_->LoadTree(entryNumber_);
286-
filePtr_->SetCacheRead(nullptr);
286+
filePtr_->SetCacheRead(nullptr, tree_);
287287
if (treeCache_ && trainNow_ && entryNumber_ >= 0) {
288288
startTraining();
289289
trainNow_ = false;
@@ -337,7 +337,7 @@ namespace edm {
337337
// ROOT will automatically expand the cache to fit one cluster; hence, we use
338338
// 5 MB as the cache size below
339339
tree_->SetCacheSize(static_cast<Long64_t>(5 * 1024 * 1024));
340-
rawTriggerTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead()));
340+
rawTriggerTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead(tree_)));
341341
if (rawTriggerTreeCache_)
342342
rawTriggerTreeCache_->SetEnablePrefetching(false);
343343
TObjArray* branches = tree_->GetListOfBranches();
@@ -355,7 +355,7 @@ namespace edm {
355355
}
356356
performedSwitchOver_ = false;
357357
rawTriggerTreeCache_->StopLearningPhase();
358-
filePtr_->SetCacheRead(nullptr);
358+
filePtr_->SetCacheRead(nullptr, tree_);
359359

360360
return rawTriggerTreeCache_.get();
361361
} else if (!performedSwitchOver_ and entryNumber_ < rawTriggerSwitchOverEntry_) {
@@ -370,7 +370,7 @@ namespace edm {
370370

371371
// Train the triggerCache
372372
tree_->SetCacheSize(static_cast<Long64_t>(5 * 1024 * 1024));
373-
triggerTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead()));
373+
triggerTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead(tree_)));
374374
triggerTreeCache_->SetEnablePrefetching(false);
375375
triggerTreeCache_->SetLearnEntries(0);
376376
triggerTreeCache_->SetEntryRange(entryNumber, tree_->GetEntries());
@@ -380,7 +380,7 @@ namespace edm {
380380
triggerTreeCache_->AddBranch(*it, kTRUE);
381381
}
382382
triggerTreeCache_->StopLearningPhase();
383-
filePtr_->SetCacheRead(nullptr);
383+
filePtr_->SetCacheRead(nullptr, tree_);
384384
}
385385
return triggerTreeCache_.get();
386386
}
@@ -412,34 +412,34 @@ namespace edm {
412412
}
413413

414414
void RootTree::getEntryForAllBranches() const {
415-
filePtr_->SetCacheRead(rawTreeCache_.get());
415+
filePtr_->SetCacheRead(rawTreeCache_.get(), tree_);
416416
auto ptr = filePtr_.get();
417-
auto cleanup = [ptr](TTreeCache* cache) { ptr->SetCacheRead(nullptr); };
417+
auto cleanup = [ptr, tree = tree_](TTreeCache* cache) { ptr->SetCacheRead(nullptr, tree); };
418418
std::unique_ptr<TTreeCache, decltype(cleanup)> cacheGuard(rawTreeCache_.get(), cleanup);
419419
tree_->GetEntry(entryNumber_);
420420
}
421421

422422
void RootTree::getEntry(TBranch* branch, EntryNumber entryNumber) const {
423423
try {
424424
TTreeCache* cache = selectCache(branch, entryNumber);
425-
filePtr_->SetCacheRead(cache);
425+
filePtr_->SetCacheRead(cache, tree_);
426426
branch->GetEntry(entryNumber);
427-
filePtr_->SetCacheRead(nullptr);
427+
filePtr_->SetCacheRead(nullptr, tree_);
428428
} catch (cms::Exception const& e) {
429429
// We make sure the treeCache_ is detached from the file,
430430
// so that ROOT does not also delete it.
431-
filePtr_->SetCacheRead(nullptr);
431+
filePtr_->SetCacheRead(nullptr, tree_);
432432
Exception t(errors::FileReadError, "", e);
433433
t.addContext(std::string("Reading branch ") + branch->GetName());
434434
throw t;
435435
} catch (std::exception const& e) {
436-
filePtr_->SetCacheRead(nullptr);
436+
filePtr_->SetCacheRead(nullptr, tree_);
437437
Exception t(errors::FileReadError);
438438
t << e.what();
439439
t.addContext(std::string("Reading branch ") + branch->GetName());
440440
throw t;
441441
} catch (...) {
442-
filePtr_->SetCacheRead(nullptr);
442+
filePtr_->SetCacheRead(nullptr, tree_);
443443
Exception t(errors::FileReadError);
444444
t << "An exception of unknown type was thrown.";
445445
t.addContext(std::string("Reading branch ") + branch->GetName());
@@ -471,9 +471,8 @@ namespace edm {
471471
assert(!rawTreeCache_);
472472
treeCache_->SetLearnEntries(learningEntries_);
473473
tree_->SetCacheSize(static_cast<Long64_t>(cacheSize_));
474-
rawTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead()));
474+
rawTreeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead(tree_)));
475475
rawTreeCache_->SetEnablePrefetching(false);
476-
filePtr_->SetCacheRead(nullptr);
477476
rawTreeCache_->SetLearnEntries(0);
478477
if (promptRead_) {
479478
switchOverEntry_ = entries_;
@@ -491,6 +490,8 @@ namespace edm {
491490
rawTreeCache_->SetEntryRange(rawStart, rawEnd);
492491
rawTreeCache_->AddBranch("*", kTRUE);
493492
rawTreeCache_->StopLearningPhase();
493+
filePtr_->SetCacheRead(nullptr, tree_);
494+
494495
treeCache_->StartLearningPhase();
495496
treeCache_->SetEntryRange(treeStart, tree_->GetEntries());
496497
// Make sure that 'branchListIndexes' branch exist in input file
@@ -501,12 +502,13 @@ namespace edm {
501502
trainedSet_.clear();
502503
triggerSet_.clear();
503504
assert(treeCache_->GetTree() == tree_);
505+
filePtr_->SetCacheRead(nullptr, tree_);
504506
}
505507

506508
void RootTree::stopTraining() {
507-
filePtr_->SetCacheRead(treeCache_.get());
509+
filePtr_->SetCacheRead(treeCache_.get(), tree_);
508510
treeCache_->StopLearningPhase();
509-
filePtr_->SetCacheRead(nullptr);
511+
filePtr_->SetCacheRead(nullptr, tree_);
510512
rawTreeCache_.reset();
511513
}
512514

@@ -518,7 +520,7 @@ namespace edm {
518520
// We own the treeCache_.
519521
// We make sure the treeCache_ is detached from the file,
520522
// so that ROOT does not also delete it.
521-
filePtr_->SetCacheRead(nullptr);
523+
filePtr_->SetCacheRead(nullptr, tree_);
522524
// We *must* delete the TTreeCache here because the TFilePrefetch object
523525
// references the TFile. If TFile is closed, before the TTreeCache is
524526
// deleted, the TFilePrefetch may continue to do TFile operations, causing
@@ -537,7 +539,7 @@ namespace edm {
537539
}
538540
tree_->LoadTree(0);
539541
assert(treeCache_);
540-
filePtr_->SetCacheRead(treeCache_.get());
542+
filePtr_->SetCacheRead(treeCache_.get(), tree_);
541543
treeCache_->StartLearningPhase();
542544
treeCache_->SetEntryRange(0, tree_->GetEntries());
543545
treeCache_->AddBranch(branchNames, kTRUE);
@@ -546,7 +548,7 @@ namespace edm {
546548
// We own the treeCache_.
547549
// We make sure the treeCache_ is detached from the file,
548550
// so that ROOT does not also delete it.
549-
filePtr_->SetCacheRead(nullptr);
551+
filePtr_->SetCacheRead(nullptr, tree_);
550552

551553
if (branchType_ == InEvent) {
552554
// Must also manually add things to the trained set.
@@ -594,7 +596,7 @@ namespace edm {
594596
char const* branchNames) {
595597
tree->LoadTree(0);
596598
tree->SetCacheSize(cacheSize);
597-
std::unique_ptr<TTreeCache> treeCache(dynamic_cast<TTreeCache*>(file.GetCacheRead()));
599+
std::unique_ptr<TTreeCache> treeCache(dynamic_cast<TTreeCache*>(file.GetCacheRead(tree)));
598600
if (nullptr != treeCache.get()) {
599601
treeCache->StartLearningPhase();
600602
treeCache->SetEntryRange(0, tree->GetEntries());
@@ -604,7 +606,7 @@ namespace edm {
604606
// We own the treeCache_.
605607
// We make sure the treeCache_ is detached from the file,
606608
// so that ROOT does not also delete it.
607-
file.SetCacheRead(nullptr);
609+
file.SetCacheRead(nullptr, tree);
608610
return treeCache;
609611
}
610612
} // namespace roottree

0 commit comments

Comments
 (0)