Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-23 02:05:06

0001 /*----------------------------------------------------------------------
0002 ----------------------------------------------------------------------*/
0003 
0004 #include "RootDelayedReader.h"
0005 #include "InputFile.h"
0006 #include "DataFormats/Common/interface/EDProductGetter.h"
0007 #include "DataFormats/Common/interface/RefCoreStreamer.h"
0008 
0009 #include "FWCore/Framework/interface/SharedResourcesAcquirer.h"
0010 #include "FWCore/Framework/interface/SharedResourcesRegistry.h"
0011 
0012 #include "IOPool/Common/interface/getWrapperBasePtr.h"
0013 
0014 #include "FWCore/Utilities/interface/EDMException.h"
0015 
0016 #include "TBranch.h"
0017 #include "TClass.h"
0018 
0019 #include <cassert>
0020 
0021 namespace edm {
0022 
0023   RootDelayedReader::RootDelayedReader(RootTree const& tree, std::shared_ptr<InputFile> filePtr, InputType inputType)
0024       : tree_(tree), filePtr_(filePtr), nextReader_(), inputType_(inputType) {
0025     if (inputType == InputType::Primary) {
0026       auto resources = SharedResourcesRegistry::instance()->createAcquirerForSourceDelayedReader();
0027       resourceAcquirer_ = std::make_unique<SharedResourcesAcquirer>(std::move(resources.first));
0028       mutex_ = resources.second;
0029     }
0030   }
0031 
0032   RootDelayedReader::~RootDelayedReader() {}
0033 
0034   std::pair<SharedResourcesAcquirer*, std::recursive_mutex*> RootDelayedReader::sharedResources_() const {
0035     return std::make_pair(resourceAcquirer_.get(), mutex_.get());
0036   }
0037 
0038   void RootDelayedReader::readAllProductsNow(EDProductGetter const* ep) {}
0039 
0040   std::shared_ptr<WrapperBase> RootDelayedReader::getProduct_(BranchID const& k, EDProductGetter const* ep) {
0041     if (lastException_) {
0042       try {
0043         std::rethrow_exception(lastException_);
0044       } catch (edm::Exception const& e) {
0045         //avoid growing the context each time the exception is rethrown.
0046         auto copy = e;
0047         copy.addContext("Rethrowing an exception that happened on a different read request.");
0048         throw copy;
0049       } catch (cms::Exception& e) {
0050         //If we do anything here to 'copy', we would lose the actual type of the exception.
0051         e.addContext("Rethrowing an exception that happened on a different read request.");
0052         throw;
0053       }
0054     }
0055     auto branchInfo = getBranchInfo(k);
0056     if (not branchInfo) {
0057       if (nextReader_) {
0058         return nextReader_->getProduct(k, ep);
0059       } else {
0060         return std::shared_ptr<WrapperBase>();
0061       }
0062     }
0063     TBranch* br = branchInfo->productBranch_;
0064     if (br == nullptr) {
0065       if (nextReader_) {
0066         return nextReader_->getProduct(k, ep);
0067       } else {
0068         return std::shared_ptr<WrapperBase>();
0069       }
0070     }
0071 
0072     RefCoreStreamerGuard guard(ep);
0073     std::unique_ptr<WrapperBase> edp = branchInfo->newWrapper();
0074     void* edpPtr = edp.get();
0075     branchInfo->productBranch_->SetAddress(&edpPtr);
0076 
0077     try {
0078       //Run, Lumi, and ProcessBlock only have 1 entry number, which is index 0
0079       tree_.getEntry(br, tree_.entryNumberForIndex(tree_.branchType() == InEvent ? ep->transitionIndex() : 0));
0080     } catch (...) {
0081       lastException_ = std::current_exception();
0082       std::rethrow_exception(lastException_);
0083     }
0084     if (tree_.branchType() == InEvent) {
0085       // CMS-THREADING For the primary input source calls to this function need to be serialized
0086       InputFile::reportReadBranch(inputType_, std::string(br->GetName()));
0087     }
0088     return edp;
0089   }
0090 }  // namespace edm