@@ -20,6 +20,9 @@ namespace edm {
2020 void CacheManagerBase::getEntry (TBranch* branch, EntryNumber entryNumber) { branch->GetEntry (entryNumber); }
2121 void CacheManagerBase::getAuxEntry (TBranch* branch, EntryNumber entryNumber) { getEntry (branch, entryNumber); }
2222 void CacheManagerBase::getEntryForAllBranches (EntryNumber entryNumber) const { tree_->GetEntry (entryNumber); }
23+ std::shared_ptr<TTreeCache> CacheManagerBase::createCacheWithSize (unsigned int cacheSize) {
24+ return filePtr_->createCacheWithSize (tree_, cacheSize);
25+ }
2326
2427 // policy with no TTreeCache
2528 class NoCache : public CacheManagerBase {
@@ -52,13 +55,12 @@ namespace edm {
5255 void createPrimaryCache (unsigned int cacheSize) override ;
5356 void setEntryNumber (EntryNumber nextEntryNumber, EntryNumber entryNumber, EntryNumber entries) override ;
5457 void trainCache (char const * branchNames) override ;
55- void resetTraining () override ;
58+ void resetTraining (bool promptRead ) override ;
5659 void getEntry (TBranch* branch, EntryNumber entryNumber) override ;
5760 void getEntryForAllBranches (EntryNumber entryNumber) const override ;
5861
59- private:
60- // SimpleCache does not own the treeCache_
61- TTreeCache* treeCache_;
62+ protected:
63+ std::shared_ptr<TTreeCache> treeCache_;
6264 BranchType branchType_;
6365 unsigned int learningEntries_;
6466 bool enablePrefetching_;
@@ -77,46 +79,105 @@ namespace edm {
7779 if (cachestats && treeCache_) {
7880 treeCache_->Print (" a cachedbranches" );
7981 }
82+ // We own the treeCache_.
83+ // We make sure the treeCache_ is detached from the file,
84+ // so that ROOT does not also delete it.
85+ filePtr_->clearCacheRead (tree_);
86+ treeCache_.reset ();
8087 }
8188
8289 void SimpleCache::setEntryNumber (EntryNumber nextEntryNumber, EntryNumber entryNumber, EntryNumber entries) {
8390 if (nextEntryNumber != entryNumber) {
84- oneapi::tbb::this_task_arena::isolate ([&]() {
85- tree_->LoadTree (nextEntryNumber);
86- treeCache_->FillBuffer ();
87- });
91+ auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
92+ oneapi::tbb::this_task_arena::isolate ([&]() { tree_->LoadTree (nextEntryNumber); });
8893 }
8994 }
9095
9196 void SimpleCache::getEntry (TBranch* branch, EntryNumber entryNumber) {
97+ auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
9298 oneapi::tbb::this_task_arena::isolate ([&]() { branch->GetEntry (entryNumber); });
9399 }
94100
95101 void SimpleCache::getEntryForAllBranches (EntryNumber entryNumber) const {
102+ auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
96103 oneapi::tbb::this_task_arena::isolate ([&]() { tree_->GetEntry (entryNumber); });
97104 }
98105
99106 void SimpleCache::createPrimaryCache (unsigned int cacheSize) {
100- tree_->SetCacheSize (static_cast <Long64_t>(cacheSize));
101- treeCache_ = dynamic_cast <TTreeCache*>(filePtr_->getCacheRead (tree_));
107+ treeCache_ = createCacheWithSize (cacheSize);
102108 assert (treeCache_);
103-
104109 treeCache_->SetEnablePrefetching (enablePrefetching_);
105- treeCache_->SetLearnEntries (learningEntries_);
106110 }
107111
108- void SimpleCache::resetTraining () {
109- trainCache (nullptr );
110- treeCache_->SetLearnEntries (learningEntries_);
112+ void SimpleCache::resetTraining (bool promptRead) {
113+ if (cachestats) {
114+ treeCache_->Print (" a cachedbranches" );
115+ }
116+ const auto addBranches = promptRead ? " *" : nullptr ;
117+ trainCache (addBranches);
111118 }
112119
113120 void SimpleCache::trainCache (char const * branchNames) {
114- tree_->LoadTree (0 );
115121 treeCache_->StartLearningPhase ();
116122 treeCache_->SetEntryRange (0 , tree_->GetEntries ());
117123 if (branchNames) {
124+ treeCache_->SetLearnEntries (0 );
118125 treeCache_->AddBranch (branchNames, kTRUE );
126+ treeCache_->StopLearningPhase ();
127+ } else {
128+ treeCache_->SetLearnEntries (learningEntries_);
129+ auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
130+ tree_->LoadTree (0 );
131+ }
132+ }
133+
134+ class SimpleWithAuxCache : public SimpleCache {
135+ public:
136+ SimpleWithAuxCache (std::shared_ptr<InputFile> filePtr,
137+ unsigned int learningEntries,
138+ bool enablePrefetching,
139+ BranchType const & branchType);
140+ void getAuxEntry (TBranch* branch, EntryNumber entryNumber) override ;
141+ void reset () override ;
142+
143+ private:
144+ TTreeCache* getAuxCache (TBranch* auxBranch);
145+ std::shared_ptr<TTreeCache> auxCache_;
146+ };
147+
148+ SimpleWithAuxCache::SimpleWithAuxCache (std::shared_ptr<InputFile> filePtr,
149+ unsigned int learningEntries,
150+ bool enablePrefetching,
151+ BranchType const & branchType)
152+ : SimpleCache(filePtr, learningEntries, enablePrefetching, branchType) {}
153+
154+ TTreeCache* SimpleWithAuxCache::getAuxCache (TBranch* auxBranch) {
155+ if (not auxCache_) {
156+ auxCache_ = createCacheWithSize (1 * 1024 * 1024 );
157+ if (auxCache_) {
158+ auxCache_->SetEnablePrefetching (enablePrefetching_);
159+ auxCache_->SetLearnEntries (0 );
160+ auxCache_->StartLearningPhase ();
161+ auxCache_->SetEntryRange (0 , tree_->GetEntries ());
162+ auxCache_->AddBranch (auxBranch->GetName (), kTRUE );
163+ auxCache_->StopLearningPhase ();
164+ }
165+ }
166+ return auxCache_.get ();
167+ }
168+
169+ void SimpleWithAuxCache::reset () {
170+ if constexpr (cachestats) {
171+ if (auxCache_)
172+ auxCache_->Print (" a cachedbranches" );
119173 }
174+ auxCache_.reset ();
175+ SimpleCache::reset ();
176+ }
177+
178+ void SimpleWithAuxCache::getAuxEntry (TBranch* branch, EntryNumber entryNumber) {
179+ auto guard = filePtr_->setCacheReadTemporarily (getAuxCache (branch), tree_);
180+ branch->GetEntry (entryNumber);
120181 }
121182
122183 //
@@ -139,7 +200,7 @@ namespace edm {
139200 enableTriggerCache_(branchType_ == InEvent) {}
140201 ~SparseReadCache () override { reset (); }
141202 void createPrimaryCache (unsigned int cacheSize) override ;
142- void resetTraining () override ;
203+ void resetTraining (bool promptRead ) override ;
143204 void reset () override ;
144205 void setEntryNumber (EntryNumber nextEntryNumber, EntryNumber entryNumber, EntryNumber entries) override ;
145206 void trainCache (char const * branchNames) override ;
@@ -152,7 +213,6 @@ namespace edm {
152213 private:
153214 void getEntryUsingCache (TBranch* branch, EntryNumber entryNumber, TTreeCache* cache);
154215 TTreeCache* getAuxCache (TBranch* auxBranch);
155- std::shared_ptr<TTreeCache> createCacheWithSize (unsigned int cacheSize);
156216 TTreeCache* selectCache (TBranch* branch, EntryNumber entryNumber);
157217 TTreeCache* checkTriggerCache (TBranch* branch, EntryNumber entryNumber);
158218 TTreeCache* checkTriggerCacheImpl (TBranch* branch, EntryNumber entryNumber);
@@ -186,10 +246,6 @@ namespace edm {
186246 std::unordered_set<TBranch*> triggerSet_;
187247 };
188248
189- std::shared_ptr<TTreeCache> SparseReadCache::createCacheWithSize (unsigned int cacheSize) {
190- return filePtr_->createCacheWithSize (*tree_, cacheSize);
191- }
192-
193249 void SparseReadCache::createPrimaryCache (unsigned int cacheSize) {
194250 cacheSize_ = cacheSize;
195251 treeCache_ = createCacheWithSize (cacheSize);
@@ -353,10 +409,8 @@ namespace edm {
353409 }
354410
355411 void SparseReadCache::getEntryForAllBranches (EntryNumber entryNumber) const {
356- oneapi::tbb::this_task_arena::isolate ([&]() {
357- auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
358- tree_->GetEntry (entryNumber);
359- });
412+ auto guard = filePtr_->setCacheReadTemporarily (treeCache_.get (), tree_);
413+ oneapi::tbb::this_task_arena::isolate ([&]() { tree_->GetEntry (entryNumber); });
360414 }
361415
362416 void SparseReadCache::startTraining (EntryNumber entryNumber) {
@@ -404,9 +458,13 @@ namespace edm {
404458 rawTreeCache_.reset ();
405459 }
406460
407- void SparseReadCache::resetTraining () { trainNow_ = true ; }
461+ void SparseReadCache::resetTraining (bool promptRead ) { trainNow_ = true ; }
408462
409463 void SparseReadCache::reset () {
464+ // We own the treeCache_.
465+ // We make sure the treeCache_ is detached from the file,
466+ // so that ROOT does not also delete it.
467+ filePtr_->clearCacheRead (tree_);
410468 if constexpr (cachestats) {
411469 if (treeCache_)
412470 treeCache_->Print (" a cachedbranches" );
@@ -479,10 +537,6 @@ namespace edm {
479537 }
480538
481539 void SparseReadCache::trainCache (char const * branchNames) {
482- // We own the treeCache_.
483- // We make sure the treeCache_ is detached from the file,
484- // so that ROOT does not also delete it.
485-
486540 if (cacheSize_ == 0 ) {
487541 return ;
488542 }
@@ -529,12 +583,15 @@ namespace edm {
529583 unsigned int learningEntries,
530584 bool enablePrefetching,
531585 BranchType const & branchType) {
532- if (strategy == CacheStrategy::kSimple ) {
533- return std::make_unique<SimpleCache>(filePtr, learningEntries, enablePrefetching, branchType);
534- } else if (strategy == CacheStrategy::kSparse ) {
535- return std::make_unique<SparseReadCache>(filePtr, learningEntries, enablePrefetching, branchType);
536- } else if (strategy == CacheStrategy::kNone ) {
537- return std::make_unique<NoCache>(filePtr);
586+ switch (strategy) {
587+ case CacheStrategy::kNone :
588+ return std::make_unique<NoCache>(filePtr);
589+ case CacheStrategy::kSimple :
590+ return std::make_unique<SimpleCache>(filePtr, learningEntries, enablePrefetching, branchType);
591+ case CacheStrategy::kSimpleWithAuxCache :
592+ return std::make_unique<SimpleWithAuxCache>(filePtr, learningEntries, enablePrefetching, branchType);
593+ case CacheStrategy::kSparse :
594+ return std::make_unique<SparseReadCache>(filePtr, learningEntries, enablePrefetching, branchType);
538595 }
539596
540597 throw cms::Exception (" BadConfig" ) << " CacheManagerBase: unknown cache strategy requested" ;
0 commit comments