Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:49:50

0001 #ifndef DataFormats_FWLite_MultiChainEvent_h
0002 #define DataFormats_FWLite_MultiChainEvent_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWLite
0006 // Class  :     MultiChainEvent
0007 //
0008 /**\class MultiChainEvent MultiChainEvent.h DataFormats/FWLite/interface/MultiChainEvent.h
0009 
0010  Description: FWLite chain event that is aware of two files at once
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Salvatore Rappoccio
0018 //         Created:  Thu Jul  9 22:05:56 CDT 2009
0019 //
0020 // system include files
0021 #include <memory>
0022 #include <string>
0023 #include <typeinfo>
0024 #include <vector>
0025 
0026 // user include files
0027 #include "DataFormats/FWLite/interface/EventBase.h"
0028 #include "DataFormats/FWLite/interface/ChainEvent.h"
0029 #include "FWCore/Utilities/interface/propagate_const.h"
0030 
0031 // forward declarations
0032 namespace edm {
0033   class WrapperBase;
0034   class ProductRegistry;
0035   class ProcessHistory;
0036   class BranchDescription;
0037   class EDProductGetter;
0038   class EventAux;
0039   class TriggerResults;
0040   class TriggerNames;
0041   class TriggerResultsByName;
0042 }  // namespace edm
0043 
0044 namespace fwlite {
0045 
0046   namespace internal {
0047     class MultiProductGetter;
0048   }
0049 
0050   class MultiChainEvent : public EventBase {
0051   public:
0052     typedef std::map<edm::EventID, Long64_t> sec_file_index_map;
0053     typedef std::pair<edm::EventID, edm::EventID> event_id_range;
0054     typedef std::map<event_id_range, Long64_t> sec_file_range_index_map;
0055 
0056     MultiChainEvent(std::vector<std::string> const& iFileNames1,
0057                     std::vector<std::string> const& iFileNames2,
0058                     bool useSecFileMapSorted = false);
0059     ~MultiChainEvent() override;
0060 
0061     const MultiChainEvent& operator++() override;
0062 
0063     ///Go to the event at index iIndex
0064     bool to(Long64_t iIndex);
0065 
0066     //If lumi is non-zero, Go to event by Run, Lumi, and Event number
0067     //If lumi is zero, Go to event by Run and Event number
0068     bool to(edm::EventID id);
0069     bool to(edm::RunNumber_t run, edm::EventNumber_t event);
0070     bool to(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);
0071 
0072     // Go to the very first Event.
0073     const MultiChainEvent& toBegin() override;
0074 
0075     // ---------- const member functions ---------------------
0076     std::string const getBranchNameFor(std::type_info const&, char const*, char const*, char const*) const override;
0077     template <typename T>
0078     edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
0079       auto t = event1_->consumes<T>(iTag);
0080       if (t) {
0081         return t;
0082       }
0083       return event2_->consumes<T>(iTag);
0084     }
0085 
0086     using fwlite::EventBase::getByLabel;
0087 
0088     /** This function should only be called by fwlite::Handle<>*/
0089     bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*) const override;
0090     //void getByBranchName(std::type_info const&, char const*, void*&) const;
0091 
0092     bool isValid() const;
0093     operator bool() const;
0094     bool atEnd() const override;
0095 
0096     Long64_t size() const;
0097 
0098     edm::EventAuxiliary const& eventAuxiliary() const override;
0099 
0100     std::vector<edm::BranchDescription> const& getBranchDescriptions() const;
0101     std::vector<std::string> const& getProcessHistory() const;
0102     edm::ProcessHistory const& processHistory() const override;
0103     TFile* getTFile() const { return event1_->getTFile(); }
0104     TFile* getTFileSec() const { return event2_->getTFile(); }
0105 
0106     Long64_t eventIndex() const { return event1_->eventIndex(); }
0107     Long64_t eventIndexSec() const { return event2_->eventIndex(); }
0108 
0109     fwlite::LuminosityBlock const& getLuminosityBlock() { return event1_->getLuminosityBlock(); }
0110 
0111     fwlite::Run const& getRun() { return event1_->getRun(); }
0112 
0113     Long64_t fileIndex() const override { return event1_->eventIndex(); }
0114     Long64_t secondaryFileIndex() const override { return event2_->eventIndex(); }
0115 
0116     edm::TriggerNames const& triggerNames(edm::TriggerResults const& triggerResults) const override;
0117     edm::TriggerResultsByName triggerResultsByName(edm::TriggerResults const& triggerResults) const override;
0118 
0119     edm::ParameterSet const* parameterSet(edm::ParameterSetID const& psID) const override;
0120 
0121     // ---------- static member functions --------------------
0122     static void throwProductNotFoundException(std::type_info const&, char const*, char const*, char const*);
0123 
0124     // return the two chain events
0125     ChainEvent const* primary() const { return &*event1_; }
0126     ChainEvent const* secondary() const { return &*event2_; }
0127 
0128     // ---------- member functions ---------------------------
0129 
0130     edm::WrapperBase const* getByProductID(edm::ProductID const&) const override;
0131 
0132     std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(edm::ProductID const& pid,
0133                                                                                        unsigned int key) const;
0134 
0135     void getThinnedProducts(edm::ProductID const& pid,
0136                             std::vector<edm::WrapperBase const*>& foundContainers,
0137                             std::vector<unsigned int>& keys) const;
0138 
0139     edm::OptionalThinnedKey getThinnedKeyFrom(edm::ProductID const& parent,
0140                                               unsigned int key,
0141                                               edm::ProductID const& thinned) const;
0142 
0143   private:
0144     bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;
0145 
0146     MultiChainEvent(Event const&);  // stop default
0147 
0148     const MultiChainEvent& operator=(Event const&);  // stop default
0149 
0150     ///Go to the event from secondary files at index iIndex
0151     bool toSec(Long64_t iIndex);
0152 
0153     //Go to event from secondary files by Run, Lumi (if non-zero), and  Event number
0154     bool toSec(const edm::EventID& id);
0155     bool toSec(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);
0156     bool toSec(edm::RunNumber_t run, edm::EventNumber_t event);
0157 
0158     // ---------- member data --------------------------------
0159 
0160     std::shared_ptr<ChainEvent> event1_;  // primary files
0161     std::shared_ptr<ChainEvent> event2_;  // secondary files
0162     std::shared_ptr<internal::MultiProductGetter const> getter_;
0163 
0164     // speed up secondary file access with a (run range)_1 ---> index_2 map,
0165     // when the files are sorted by run,event within the file.
0166     // in this case, it is sufficient to store only a run-range to index mapping.
0167     // with this, the solution becomes more performant.
0168     bool useSecFileMapSorted_;
0169     sec_file_range_index_map secFileMapSorted_;
0170   };
0171 
0172 }  // namespace fwlite
0173 #endif