Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-04 01:56:55

0001 #ifndef DataFormats_FWLite_Event_h
0002 #define DataFormats_FWLite_Event_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWLite
0006 // Class  :     Event
0007 //
0008 /**\class Event Event.h DataFormats/FWLite/interface/Event.h
0009 
0010    Description: Provide event data access in FWLite
0011 
0012    Usage:
0013    This class is meant to allow one to loop over all events in a TFile and then
0014  read the data in an Event in a manner analogous to how data is read in the full framework.
0015  A typical use would be
0016  \code
0017  TFile f("foo.root");
0018  fwlite::Event ev(&f);
0019  for(ev.toBeing(); ! ev.atEnd(); ++ev) {
0020     fwlite::Handle<std::vector<Foo> > foos;
0021     foos.getByLabel(ev, "myFoos");
0022  }
0023  \endcode
0024  The above example will work for both ROOT and compiled code. However, it is possible to exactly
0025  match the full framework if you only intend to compile your code.  In that case the access
0026  would look like
0027 
0028  \code
0029  TFile f("foo.root");
0030  fwlite::Event ev(&f);
0031 
0032  edm::InputTag fooTag("myFoos");
0033  for(ev.toBeing(); ! ev.atEnd(); ++ev) {
0034     edm::Handle<std::vector<Foo> > foos;
0035     ev.getByLabel(fooTag, foos);
0036  }
0037  \endcode
0038 
0039  NOTE: This class is not safe to use across threads.
0040 */
0041 //
0042 // Original Author:  Chris Jones
0043 //         Created:  Tue May  8 15:01:20 EDT 2007
0044 //
0045 // system include files
0046 #include <typeinfo>
0047 #include <map>
0048 #include <vector>
0049 #include <memory>
0050 #include <cstring>
0051 #include <string>
0052 #include <functional>
0053 
0054 #include "Rtypes.h"
0055 
0056 // user include files
0057 #include "DataFormats/FWLite/interface/EventBase.h"
0058 #include "DataFormats/FWLite/interface/EntryFinder.h"
0059 #include "DataFormats/FWLite/interface/LuminosityBlock.h"
0060 #include "DataFormats/FWLite/interface/Run.h"
0061 #include "DataFormats/FWLite/interface/InternalDataKey.h"
0062 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
0063 #include "DataFormats/Provenance/interface/EventProcessHistoryID.h"
0064 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0065 #include "DataFormats/Provenance/interface/EventID.h"
0066 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0067 
0068 // forward declarations
0069 namespace edm {
0070   class WrapperBase;
0071   class ProductRegistry;
0072   class BranchDescription;
0073   class EDProductGetter;
0074   class EventAux;
0075   class Timestamp;
0076   class TriggerResults;
0077   class TriggerNames;
0078   class TriggerResultsByName;
0079 }  // namespace edm
0080 class TCut;
0081 
0082 namespace fwlite {
0083   class BranchMapReader;
0084   class HistoryGetterBase;
0085   class DataGetterHelper;
0086   class RunFactory;
0087   class ChainEvent;
0088   class MultiChainEvent;
0089 
0090   class Event : public EventBase {
0091   public:
0092     friend class ChainEvent;
0093     friend class MultiChainEvent;
0094 
0095     // NOTE: Does NOT take ownership so iFile must remain around
0096     // at least as long as Event.
0097     // useCache and baFunc (branch-access-function) are passed to
0098     // DataGetterHelper and help with external management of TTreeCache
0099     // associated with the file. By default useCache is true and internal
0100     // DataGetterHelper caching is enabled. When user sets useCache to
0101     // false no cache is created unless user attaches and controls it
0102     // himself.
0103     Event(
0104         TFile* iFile, bool useCache = true, std::function<void(TBranch const&)> baFunc = [](TBranch const&) {});
0105 
0106     Event(Event const&) = delete;  // stop default
0107 
0108     Event const& operator=(Event const&) = delete;  // stop default
0109 
0110     ~Event() override;
0111 
0112     ///Advance to next event in the TFile
0113     Event const& operator++() override;
0114 
0115     ///Find index of given event-id
0116     Long64_t indexFromEventId(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);
0117 
0118     ///Go to the event at index iIndex
0119     bool to(Long64_t iIndex);
0120 
0121     ///Go to event by Run & Event number
0122     bool to(const edm::EventID& id);
0123     bool to(edm::RunNumber_t run, edm::EventNumber_t event);
0124     bool to(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);
0125 
0126     /// Go to the very first Event.
0127     Event const& toBegin() override;
0128 
0129     // ---------- const member functions ---------------------
0130     ///Return the branch name in the TFile which contains the data
0131     std::string const getBranchNameFor(std::type_info const&,
0132                                        char const* iModuleLabel,
0133                                        char const* iProductInstanceLabel,
0134                                        char const* iProcessName) const override;
0135 
0136     template <typename T>
0137     edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
0138       auto bid =
0139           dataHelper_.getBranchIDFor(typeid(T), iTag.label().c_str(), iTag.instance().c_str(), iTag.process().c_str());
0140       if (bid) {
0141         return this->makeTokenUsing<T>(bid.value().id());
0142       }
0143       return {};
0144     }
0145     using fwlite::EventBase::getByLabel;
0146     /// This function should only be called by fwlite::Handle<>
0147     bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*) const override;
0148     //void getByBranchName(std::type_info const&, char const*, void*&) const;
0149 
0150     ///Properly setup for edm::Ref, etc and then call TTree method
0151     void draw(Option_t* opt);
0152     Long64_t draw(char const* varexp,
0153                   const TCut& selection,
0154                   Option_t* option = "",
0155                   Long64_t nentries = 1000000000,
0156                   Long64_t firstentry = 0);
0157     Long64_t draw(char const* varexp,
0158                   char const* selection,
0159                   Option_t* option = "",
0160                   Long64_t nentries = 1000000000,
0161                   Long64_t firstentry = 0);
0162     Long64_t scan(char const* varexp = "",
0163                   char const* selection = "",
0164                   Option_t* option = "",
0165                   Long64_t nentries = 1000000000,
0166                   Long64_t firstentry = 0);
0167 
0168     bool isValid() const;
0169     operator bool() const;
0170     bool atEnd() const override;
0171 
0172     ///Returns number of events in the file
0173     Long64_t size() const;
0174 
0175     edm::EventAuxiliary const& eventAuxiliary() const override;
0176 
0177     std::vector<edm::BranchDescription> const& getBranchDescriptions() const {
0178       return branchMap_.getBranchDescriptions();
0179     }
0180     std::vector<std::string> const& getProcessHistory() const;
0181     TFile* getTFile() const { return branchMap_.getFile(); }
0182 
0183     edm::ParameterSet const* parameterSet(edm::ParameterSetID const& psID) const override;
0184 
0185     edm::WrapperBase const* getByProductID(edm::ProductID const&) const override;
0186     std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(edm::ProductID const& pid,
0187                                                                                        unsigned int key) const;
0188     void getThinnedProducts(edm::ProductID const& pid,
0189                             std::vector<edm::WrapperBase const*>& foundContainers,
0190                             std::vector<unsigned int>& keys) const;
0191     edm::OptionalThinnedKey getThinnedKeyFrom(edm::ProductID const& parent,
0192                                               unsigned int key,
0193                                               edm::ProductID const& thinned) const;
0194 
0195     edm::TriggerNames const& triggerNames(edm::TriggerResults const& triggerResults) const override;
0196 
0197     edm::TriggerResultsByName triggerResultsByName(edm::TriggerResults const& triggerResults) const override;
0198 
0199     edm::ProcessHistory const& processHistory() const override { return history(); }
0200 
0201     fwlite::LuminosityBlock const& getLuminosityBlock() const;
0202     fwlite::Run const& getRun() const;
0203 
0204     // ---------- static member functions --------------------
0205     static void throwProductNotFoundException(std::type_info const&, char const*, char const*, char const*);
0206 
0207   private:
0208     bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;
0209     friend class internal::ProductGetter;
0210     friend class ChainEvent;
0211     friend class EventHistoryGetter;
0212 
0213     edm::ProcessHistory const& history() const;
0214     void updateAux(Long_t eventIndex) const;
0215     void fillParameterSetRegistry() const;
0216     void setGetter(std::shared_ptr<edm::EDProductGetter const> getter) { return dataHelper_.setGetter(getter); }
0217 
0218     // ---------- member data --------------------------------
0219     //This class is not inteded to be used across different threads
0220     CMS_SA_ALLOW mutable TFile* file_;
0221     // TTree* eventTree_;
0222     TTree* eventHistoryTree_;
0223     // Long64_t eventIndex_;
0224     CMS_SA_ALLOW mutable std::shared_ptr<fwlite::LuminosityBlock> lumi_;
0225     CMS_SA_ALLOW mutable std::shared_ptr<fwlite::Run> run_;
0226     CMS_SA_ALLOW mutable fwlite::BranchMapReader branchMap_;
0227 
0228     //takes ownership of the strings used by the DataKey keys in data_
0229     CMS_SA_ALLOW mutable std::vector<char const*> labels_;
0230     CMS_SA_ALLOW mutable edm::ProcessHistoryMap historyMap_;
0231     CMS_SA_ALLOW mutable std::vector<edm::EventProcessHistoryID> eventProcessHistoryIDs_;
0232     CMS_SA_ALLOW mutable std::vector<std::string> procHistoryNames_;
0233     CMS_SA_ALLOW mutable edm::EventAuxiliary aux_;
0234     CMS_SA_ALLOW mutable EntryFinder entryFinder_;
0235     edm::EventAuxiliary const* pAux_;
0236     edm::EventAux const* pOldAux_;
0237     TBranch* auxBranch_;
0238     int fileVersion_;
0239     CMS_SA_ALLOW mutable bool parameterSetRegistryFilled_;
0240 
0241     fwlite::DataGetterHelper dataHelper_;
0242     CMS_SA_ALLOW mutable std::shared_ptr<RunFactory> runFactory_;
0243   };
0244 
0245 }  // namespace fwlite
0246 #endif