Event

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#ifndef DataFormats_FWLite_Event_h
#define DataFormats_FWLite_Event_h
// -*- C++ -*-
//
// Package:     FWLite
// Class  :     Event
//
/**\class Event Event.h DataFormats/FWLite/interface/Event.h

   Description: Provide event data access in FWLite

   Usage:
   This class is meant to allow one to loop over all events in a TFile and then
 read the data in an Event in a manner analogous to how data is read in the full framework.
 A typical use would be
 \code
 TFile f("foo.root");
 fwlite::Event ev(&f);
 for(ev.toBeing(); ! ev.atEnd(); ++ev) {
    fwlite::Handle<std::vector<Foo> > foos;
    foos.getByLabel(ev, "myFoos");
 }
 \endcode
 The above example will work for both ROOT and compiled code. However, it is possible to exactly
 match the full framework if you only intend to compile your code.  In that case the access
 would look like

 \code
 TFile f("foo.root");
 fwlite::Event ev(&f);

 edm::InputTag fooTag("myFoos");
 for(ev.toBeing(); ! ev.atEnd(); ++ev) {
    edm::Handle<std::vector<Foo> > foos;
    ev.getByLabel(fooTag, foos);
 }
 \endcode

 NOTE: This class is not safe to use across threads.
*/
//
// Original Author:  Chris Jones
//         Created:  Tue May  8 15:01:20 EDT 2007
//
// system include files
#include <typeinfo>
#include <map>
#include <vector>
#include <memory>
#include <cstring>
#include <string>
#include <functional>

#include "Rtypes.h"

// user include files
#include "DataFormats/FWLite/interface/EventBase.h"
#include "DataFormats/FWLite/interface/EntryFinder.h"
#include "DataFormats/FWLite/interface/LuminosityBlock.h"
#include "DataFormats/FWLite/interface/Run.h"
#include "DataFormats/FWLite/interface/InternalDataKey.h"
#include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
#include "DataFormats/Provenance/interface/EventProcessHistoryID.h"
#include "DataFormats/Provenance/interface/EventAuxiliary.h"
#include "DataFormats/Provenance/interface/EventID.h"
#include "DataFormats/Provenance/interface/ProductDescriptionFwd.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"

// forward declarations
namespace edm {
  class WrapperBase;
  class ProductRegistry;
  class EDProductGetter;
  class EventAux;
  class Timestamp;
  class TriggerResults;
  class TriggerNames;
  class TriggerResultsByName;
}  // namespace edm
class TCut;

namespace fwlite {
  class BranchMapReader;
  class HistoryGetterBase;
  class DataGetterHelper;
  class RunFactory;
  class ChainEvent;
  class MultiChainEvent;

  class Event : public EventBase {
  public:
    friend class ChainEvent;
    friend class MultiChainEvent;

    // NOTE: Does NOT take ownership so iFile must remain around
    // at least as long as Event.
    // useCache and baFunc (branch-access-function) are passed to
    // DataGetterHelper and help with external management of TTreeCache
    // associated with the file. By default useCache is true and internal
    // DataGetterHelper caching is enabled. When user sets useCache to
    // false no cache is created unless user attaches and controls it
    // himself.
    Event(TFile* iFile, bool useCache = true, std::function<void(TBranch const&)> baFunc = [](TBranch const&) {});

    Event(Event const&) = delete;  // stop default

    Event const& operator=(Event const&) = delete;  // stop default

    ~Event() override;

    ///Advance to next event in the TFile
    Event const& operator++() override;

    ///Find index of given event-id
    Long64_t indexFromEventId(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);

    ///Go to the event at index iIndex
    bool to(Long64_t iIndex);

    ///Go to event by Run & Event number
    bool to(const edm::EventID& id);
    bool to(edm::RunNumber_t run, edm::EventNumber_t event);
    bool to(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event);

    /// Go to the very first Event.
    Event const& toBegin() override;

    // ---------- const member functions ---------------------
    ///Return the branch name in the TFile which contains the data
    std::string const getBranchNameFor(std::type_info const&,
                                       char const* iModuleLabel,
                                       char const* iProductInstanceLabel,
                                       char const* iProcessName) const override;

    template <typename T>
    edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
      auto bid =
          dataHelper_.getBranchIDFor(typeid(T), iTag.label().c_str(), iTag.instance().c_str(), iTag.process().c_str());
      if (bid) {
        return this->makeTokenUsing<T>(bid.value().id());
      }
      return {};
    }
    using fwlite::EventBase::getByLabel;
    /// This function should only be called by fwlite::Handle<>
    bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*) const override;
    //void getByBranchName(std::type_info const&, char const*, void*&) const;

    ///Properly setup for edm::Ref, etc and then call TTree method
    void draw(Option_t* opt);
    Long64_t draw(char const* varexp,
                  const TCut& selection,
                  Option_t* option = "",
                  Long64_t nentries = 1000000000,
                  Long64_t firstentry = 0);
    Long64_t draw(char const* varexp,
                  char const* selection,
                  Option_t* option = "",
                  Long64_t nentries = 1000000000,
                  Long64_t firstentry = 0);
    Long64_t scan(char const* varexp = "",
                  char const* selection = "",
                  Option_t* option = "",
                  Long64_t nentries = 1000000000,
                  Long64_t firstentry = 0);

    bool isValid() const;
    operator bool() const;
    bool atEnd() const override;

    ///Returns number of events in the file
    Long64_t size() const;

    edm::EventAuxiliary const& eventAuxiliary() const override;

    std::vector<edm::ProductDescription> const& getProductDescriptions() const {
      return branchMap_.getProductDescriptions();
    }
    std::vector<std::string> const& getProcessHistory() const;
    TFile* getTFile() const { return branchMap_.getFile(); }

    edm::ParameterSet const* parameterSet(edm::ParameterSetID const& psID) const override;

    edm::WrapperBase const* getByProductID(edm::ProductID const&) const override;
    std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(edm::ProductID const& pid,
                                                                                       unsigned int key) const;
    void getThinnedProducts(edm::ProductID const& pid,
                            std::vector<edm::WrapperBase const*>& foundContainers,
                            std::vector<unsigned int>& keys) const;
    edm::OptionalThinnedKey getThinnedKeyFrom(edm::ProductID const& parent,
                                              unsigned int key,
                                              edm::ProductID const& thinned) const;

    edm::TriggerNames const& triggerNames(edm::TriggerResults const& triggerResults) const override;

    edm::TriggerResultsByName triggerResultsByName(edm::TriggerResults const& triggerResults) const override;

    edm::ProcessHistory const& processHistory() const override { return history(); }

    fwlite::LuminosityBlock const& getLuminosityBlock() const;
    fwlite::Run const& getRun() const;

    // ---------- static member functions --------------------
    static void throwProductNotFoundException(std::type_info const&, char const*, char const*, char const*);

  private:
    bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;
    friend class internal::ProductGetter;
    friend class ChainEvent;
    friend class EventHistoryGetter;

    edm::ProcessHistory const& history() const;
    void updateAux(Long_t eventIndex) const;
    void fillParameterSetRegistry() const;
    void setGetter(std::shared_ptr<edm::EDProductGetter const> getter) { return dataHelper_.setGetter(getter); }

    // ---------- member data --------------------------------
    //This class is not inteded to be used across different threads
    CMS_SA_ALLOW mutable TFile* file_;
    // TTree* eventTree_;
    TTree* eventHistoryTree_;
    // Long64_t eventIndex_;
    CMS_SA_ALLOW mutable std::shared_ptr<fwlite::LuminosityBlock> lumi_;
    CMS_SA_ALLOW mutable std::shared_ptr<fwlite::Run> run_;
    CMS_SA_ALLOW mutable fwlite::BranchMapReader branchMap_;

    //takes ownership of the strings used by the DataKey keys in data_
    CMS_SA_ALLOW mutable std::vector<char const*> labels_;
    CMS_SA_ALLOW mutable edm::ProcessHistoryMap historyMap_;
    CMS_SA_ALLOW mutable std::vector<edm::EventProcessHistoryID> eventProcessHistoryIDs_;
    CMS_SA_ALLOW mutable std::vector<std::string> procHistoryNames_;
    CMS_SA_ALLOW mutable edm::EventAuxiliary aux_;
    CMS_SA_ALLOW mutable EntryFinder entryFinder_;
    edm::EventAuxiliary const* pAux_;
    edm::EventAux const* pOldAux_;
    TBranch* auxBranch_;
    int fileVersion_;
    CMS_SA_ALLOW mutable bool parameterSetRegistryFilled_;

    fwlite::DataGetterHelper dataHelper_;
    CMS_SA_ALLOW mutable std::shared_ptr<RunFactory> runFactory_;
  };

}  // namespace fwlite
#endif