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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
// -*- C++ -*-
//
// Package:     FWLite
// Class  :     ChainEvent
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Sat Jun 16 06:48:39 EDT 2007
//

// system include files

// user include files
#include "DataFormats/FWLite/interface/ChainEvent.h"
#include "DataFormats/Provenance/interface/BranchType.h"
#include "DataFormats/Provenance/interface/ProcessHistory.h"
#include "TFile.h"
#include "TTree.h"
#include "TROOT.h"

namespace fwlite {
  //
  // constants, enums and typedefs
  //

  //
  // static data member definitions
  //

  //
  // constructors and destructor
  //
  ChainEvent::ChainEvent(std::vector<std::string> const& iFileNames)
      : fileNames_(), file_(), event_(), eventIndex_(0), accumulatedSize_() {
    Long64_t summedSize = 0;
    accumulatedSize_.reserve(iFileNames.size() + 1);
    fileNames_.reserve(iFileNames.size());

    for (auto const& fileName : iFileNames) {
      TFile* tfilePtr = TFile::Open(fileName.c_str());
      if (nullptr == tfilePtr) {
        throw cms::Exception("FileOpenFailed") << "TFile::Open of " << fileName << " failed";
      }
      file_ = std::shared_ptr<TFile>(tfilePtr);
      gROOT->GetListOfFiles()->Remove(tfilePtr);
      TTree* tree = dynamic_cast<TTree*>(file_->Get(edm::poolNames::eventTreeName().c_str()));
      if (nullptr == tree) {
        throw cms::Exception("NotEdmFile")
            << "The file " << fileName << " has no 'Events' TTree and therefore is not an EDM ROOT file";
      }
      Long64_t nEvents = tree->GetEntries();
      if (nEvents > 0) {  // skip empty files
        fileNames_.push_back(fileName);
        // accumulatedSize_ is the entry # at the beginning of this file
        accumulatedSize_.push_back(summedSize);
        summedSize += nEvents;
      }
    }
    // total accumulated size (last enry + 1) at the end of last file
    accumulatedSize_.push_back(summedSize);

    if (!fileNames_.empty())
      switchToFile(0);
  }

  // ChainEvent::ChainEvent(ChainEvent const& rhs)
  // {
  //    // do actual copying here;
  // }

  ChainEvent::~ChainEvent() {}

  //
  // assignment operators
  //
  // ChainEvent const& ChainEvent::operator=(ChainEvent const& rhs)
  // {
  //   //An exception safe implementation is
  //   ChainEvent temp(rhs);
  //   swap(rhs);
  //
  //   return *this;
  // }

  //
  // member functions
  //

  ChainEvent const& ChainEvent::operator++() {
    if (eventIndex_ != static_cast<Long64_t>(fileNames_.size()) - 1) {
      ++(*event_);
      if (event_->atEnd()) {
        switchToFile(++eventIndex_);
      }
    } else {
      if (*event_) {
        ++(*event_);
      }
    }
    return *this;
  }

  ///Go to the event at index iIndex
  bool ChainEvent::to(Long64_t iIndex) {
    if (iIndex >= accumulatedSize_.back()) {
      // if we're here, then iIndex was not valid
      return false;
    }

    Long64_t offsetIndex = eventIndex_;

    // we're going backwards, so start from the beginning
    if (iIndex < accumulatedSize_[offsetIndex]) {
      offsetIndex = 0;
    }

    // is it past the end of this file?
    while (iIndex >= accumulatedSize_[offsetIndex + 1]) {
      ++offsetIndex;
    }

    if (offsetIndex != eventIndex_) {
      switchToFile(eventIndex_ = offsetIndex);
    }

    // adjust to entry # in this file
    return event_->to(iIndex - accumulatedSize_[offsetIndex]);
  }

  ///Go to event with event id "id"
  bool ChainEvent::to(const edm::EventID& id) { return to(id.run(), id.luminosityBlock(), id.event()); }

  ///If lumi is non-zero, go to event with given run, lumi, and event number
  ///If lumi is zero, go to event with given run and event number
  bool ChainEvent::to(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event) {
    // First try this file
    if (event_->to(run, lumi, event)) {
      // found it, return
      return true;
    } else {
      // Did not find it, try the other files sequentially.
      // Someday I can make this smarter. For now... we get something working.
      Long64_t thisFile = eventIndex_;
      std::vector<std::string>::const_iterator filesBegin = fileNames_.begin(), filesEnd = fileNames_.end(),
                                               ifile = filesBegin;
      for (; ifile != filesEnd; ++ifile) {
        // skip the "first" file that we tried
        if (ifile - filesBegin != thisFile) {
          // switch to the next file
          switchToFile(ifile - filesBegin);
          // check that tree for the desired event
          if (event_->to(run, lumi, event)) {
            // found it, return
            return true;
          }
        }  // end ignore "first" file that we tried
      }  // end loop over files

      // did not find the event with id "id".
      return false;
    }  // end if we did not find event id in "first" file
  }

  bool ChainEvent::to(edm::RunNumber_t run, edm::EventNumber_t event) { return to(run, 0U, event); }

  /** Go to the very first Event*/

  ChainEvent const& ChainEvent::toBegin() {
    if (!size())
      return *this;
    if (eventIndex_ != 0) {
      switchToFile(0);
    }
    event_->toBegin();
    return *this;
  }

  void ChainEvent::switchToFile(Long64_t iIndex) {
    eventIndex_ = iIndex;
    TFile* tfilePtr = TFile::Open(fileNames_[iIndex].c_str());
    file_ = std::shared_ptr<TFile>(tfilePtr);
    gROOT->GetListOfFiles()->Remove(tfilePtr);
    event_ = std::make_shared<Event>(file_.get());
  }

  //
  // const member functions
  //
  std::string const ChainEvent::getBranchNameFor(std::type_info const& iType,
                                                 char const* iModule,
                                                 char const* iInstance,
                                                 char const* iProcess) const {
    return event_->getBranchNameFor(iType, iModule, iInstance, iProcess);
  }

  std::vector<edm::ProductDescription> const& ChainEvent::getProductDescriptions() const {
    return event_->getProductDescriptions();
  }

  std::vector<std::string> const& ChainEvent::getProcessHistory() const { return event_->getProcessHistory(); }

  edm::ProcessHistory const& ChainEvent::processHistory() const { return event_->processHistory(); }

  edm::EventAuxiliary const& ChainEvent::eventAuxiliary() const { return event_->eventAuxiliary(); }

  fwlite::LuminosityBlock const& ChainEvent::getLuminosityBlock() { return event_->getLuminosityBlock(); }

  fwlite::Run const& ChainEvent::getRun() { return event_->getRun(); }

  bool ChainEvent::getByLabel(std::type_info const& iType,
                              char const* iModule,
                              char const* iInstance,
                              char const* iProcess,
                              void* iValue) const {
    return event_->getByLabel(iType, iModule, iInstance, iProcess, iValue);
  }

  bool ChainEvent::getByTokenImp(edm::EDGetToken iToken, edm::WrapperBase const*& iValue) const {
    return event_->getByTokenImp(iToken, iValue);
  }

  edm::WrapperBase const* ChainEvent::getByProductID(edm::ProductID const& iID) const {
    return event_->getByProductID(iID);
  }

  std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> ChainEvent::getThinnedProduct(
      edm::ProductID const& pid, unsigned int key) const {
    return event_->getThinnedProduct(pid, key);
  }

  void ChainEvent::getThinnedProducts(edm::ProductID const& pid,
                                      std::vector<edm::WrapperBase const*>& foundContainers,
                                      std::vector<unsigned int>& keys) const {
    event_->getThinnedProducts(pid, foundContainers, keys);
  }

  edm::OptionalThinnedKey ChainEvent::getThinnedKeyFrom(edm::ProductID const& parent,
                                                        unsigned int key,
                                                        edm::ProductID const& thinned) const {
    return event_->getThinnedKeyFrom(parent, key, thinned);
  }

  bool ChainEvent::isValid() const { return event_->isValid(); }
  ChainEvent::operator bool() const { return *event_; }

  bool ChainEvent::atEnd() const {
    if (!size())
      return true;
    if (eventIndex_ == static_cast<Long64_t>(fileNames_.size()) - 1) {
      return event_->atEnd();
    }
    return false;
  }

  Long64_t ChainEvent::size() const { return accumulatedSize_.empty() ? 0 : accumulatedSize_.back(); }

  edm::TriggerNames const& ChainEvent::triggerNames(edm::TriggerResults const& triggerResults) const {
    return event_->triggerNames(triggerResults);
  }

  edm::ParameterSet const* ChainEvent::parameterSet(edm::ParameterSetID const& psID) const {
    return event_->parameterSet(psID);
  }

  void ChainEvent::fillParameterSetRegistry() const { event_->fillParameterSetRegistry(); }

  edm::TriggerResultsByName ChainEvent::triggerResultsByName(edm::TriggerResults const& triggerResults) const {
    return event_->triggerResultsByName(triggerResults);
  }

  //
  // static member functions
  //
  void ChainEvent::throwProductNotFoundException(std::type_info const& iType,
                                                 char const* iModule,
                                                 char const* iInstance,
                                                 char const* iProcess) {
    Event::throwProductNotFoundException(iType, iModule, iInstance, iProcess);
  }
}  // namespace fwlite