AlCaIsolatedBunchSelector

Counters

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
// system include files
#include <atomic>
#include <memory>
#include <cmath>
#include <iostream>
#include <sstream>
#include <fstream>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDFilter.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Common/interface/TriggerNames.h"

#include "DataFormats/Common/interface/Handle.h"
//Triggers
#include "DataFormats/Common/interface/TriggerResults.h"
#include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
//
// class declaration
//

//#define EDM_ML_DEBUG

namespace alCaIsolatedBunchSelector {
  struct Counters {
    Counters() : nAll_(0), nGood_(0) {}
    mutable std::atomic<unsigned int> nAll_, nGood_;
  };
}  // namespace alCaIsolatedBunchSelector

class AlCaIsolatedBunchSelector : public edm::stream::EDFilter<edm::GlobalCache<alCaIsolatedBunchSelector::Counters> > {
public:
  explicit AlCaIsolatedBunchSelector(edm::ParameterSet const&, const alCaIsolatedBunchSelector::Counters* count);
  ~AlCaIsolatedBunchSelector() override = default;

  static std::unique_ptr<alCaIsolatedBunchSelector::Counters> initializeGlobalCache(edm::ParameterSet const& iConfig) {
    return std::make_unique<alCaIsolatedBunchSelector::Counters>();
  }

  bool filter(edm::Event&, edm::EventSetup const&) override;
  void endStream() override;
  static void globalEndJob(const alCaIsolatedBunchSelector::Counters* counters);
  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
  void beginRun(edm::Run const&, edm::EventSetup const&) override;
  void endRun(edm::Run const&, edm::EventSetup const&) override;

  // ----------member data ---------------------------
  HLTConfigProvider hltConfig_;
  unsigned int nRun_, nAll_, nGood_;
  const std::string trigName_;
  const std::string processName_;
  const edm::InputTag theTriggerResultsLabel_;
  const edm::EDGetTokenT<edm::TriggerResults> tok_trigRes_;
};

//
// constructors and destructor
//
AlCaIsolatedBunchSelector::AlCaIsolatedBunchSelector(const edm::ParameterSet& iConfig,
                                                     const alCaIsolatedBunchSelector::Counters* count)
    : nRun_(0),
      nAll_(0),
      nGood_(0),
      trigName_(iConfig.getParameter<std::string>("triggerName")),
      processName_(iConfig.getParameter<std::string>("processName")),
      theTriggerResultsLabel_(iConfig.getParameter<edm::InputTag>("triggerResultLabel")),
      tok_trigRes_(consumes<edm::TriggerResults>(theTriggerResultsLabel_)) {
  edm::LogVerbatim("AlCaIsoBunch") << "Input tag for trigger results " << theTriggerResultsLabel_
                                   << " with trigger name " << trigName_ << " and process " << processName_
                                   << std::endl;
}

//
// member functions
//

// ------------ method called on each new Event  ------------
bool AlCaIsolatedBunchSelector::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
  bool accept(false);
  ++nAll_;
#ifdef EDM_ML_DEBUG
  edm::LogVerbatim("AlCaIsoBunch") << "Run " << iEvent.id().run() << " Event " << iEvent.id().event() << " Luminosity "
                                   << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing() << std::endl;
#endif
  if (trigName_.empty()) {
    accept = true;
  } else {
    //Step1: Find if the event passes the chosen trigger
    auto const& triggerResults = iEvent.getHandle(tok_trigRes_);
    if (triggerResults.isValid()) {
      const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
      const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
      for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
        int hlt = triggerResults->accept(iHLT);
        if (triggerNames_[iHLT].find(trigName_) != std::string::npos) {
          if (hlt > 0) {
            accept = true;
#ifdef EDM_ML_DEBUG
            edm::LogVerbatim("AlCaIsoBunch")
                << triggerNames_[iHLT] << " has got HLT flag " << hlt << ":" << accept << std::endl;
#endif
            break;
          }
        }
      }
    }
  }

  // Step 2:  Return the acceptance flag
  if (accept)
    ++nGood_;
  return accept;

}  // AlCaIsolatedBunchSelector::filter
// ------------ method called once each job just after ending the event loop  ------------
void AlCaIsolatedBunchSelector::endStream() {
  globalCache()->nAll_ += nAll_;
  globalCache()->nGood_ += nGood_;
}

void AlCaIsolatedBunchSelector::globalEndJob(const alCaIsolatedBunchSelector::Counters* count) {
  edm::LogVerbatim("AlCaIsoBunch") << "Selects " << count->nGood_ << " in " << count->nAll_ << " events" << std::endl;
}

// ------------ method called when starting to processes a run  ------------
void AlCaIsolatedBunchSelector::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
  bool changed(false);
  edm::LogVerbatim("AlCaIsoBunch") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init "
                                   << hltConfig_.init(iRun, iSetup, processName_, changed) << std::endl;
}
// ------------ method called when ending the processing of a run  ------------
void AlCaIsolatedBunchSelector::endRun(edm::Run const& iRun, edm::EventSetup const&) {
  ++nRun_;
  edm::LogVerbatim("AlCaIsoBunch") << "endRun[" << nRun_ << "] " << iRun.run() << std::endl;
}

// ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
void AlCaIsolatedBunchSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<edm::InputTag>("triggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
  desc.add<std::string>("processName", "HLT");
  desc.add<std::string>("triggerName", "");
  descriptions.add("alcaIsolatedBunchSelector", desc);
}

//define this as a plug-in
#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(AlCaIsolatedBunchSelector);