FilterOutLowPt

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
// -*- C++ -*-
//
// Package:   Alignment/CommonAlignment
// Class:     FilterOutLowPt
//
//
// Original Author:  Marco Musich

#include <memory>
#include <vector>
#include <map>
#include <set>
#include <utility>  // std::pair
#include "TMath.h"

// user include files

#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/stream/EDFilter.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

class FilterOutLowPt : public edm::stream::EDFilter<> {
public:
  explicit FilterOutLowPt(const edm::ParameterSet&);
  ~FilterOutLowPt() override;

  static void fillDescriptions(edm::ConfigurationDescriptions&);

private:
  virtual void beginJob();
  bool filter(edm::Event&, const edm::EventSetup&) override;
  virtual void endJob();

  // ----------member data --------
  const bool applyfilter;
  const bool debugOn;
  const bool runControl_;
  const double ptmin;
  const double thresh;
  const unsigned int numtrack;

  double trials;
  double passes;

  const std::vector<unsigned int> runControlNumbers_;
  std::map<unsigned int, std::pair<int, int>> eventsInRun_;

  reco::TrackBase::TrackQuality _trackQuality;
  edm::EDGetTokenT<reco::TrackCollection> theTrackCollectionToken;
};

FilterOutLowPt::FilterOutLowPt(const edm::ParameterSet& iConfig)
    : applyfilter(iConfig.getUntrackedParameter<bool>("applyfilter")),
      debugOn(iConfig.getUntrackedParameter<bool>("debugOn")),
      runControl_(iConfig.getUntrackedParameter<bool>("runControl")),
      ptmin(iConfig.getUntrackedParameter<double>("ptmin")),
      thresh(iConfig.getUntrackedParameter<int>("thresh")),
      numtrack(iConfig.getUntrackedParameter<unsigned int>("numtrack")),
      runControlNumbers_(iConfig.getUntrackedParameter<std::vector<unsigned int>>("runControlNumber")),
      theTrackCollectionToken(consumes<reco::TrackCollection>(iConfig.getUntrackedParameter<edm::InputTag>("src"))) {}

FilterOutLowPt::~FilterOutLowPt() {}

void FilterOutLowPt::beginJob() {
  trials = 0;
  passes = 0;
}

bool FilterOutLowPt::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
  bool passesRunControl = false;

  if (runControl_) {
    if (debugOn) {
      for (unsigned int runControlNumber : runControlNumbers_) {
        edm::LogInfo("FilterOutLowPt") << "run number:" << iEvent.id().run() << " keeping runs:" << runControlNumber
                                       << std::endl;
      }
    }

    for (unsigned int runControlNumber : runControlNumbers_) {
      if (iEvent.eventAuxiliary().run() == runControlNumber) {
        if (debugOn) {
          edm::LogInfo("FilterOutLowPt") << "run number" << runControlNumber << " match!" << std::endl;
        }
        passesRunControl = true;
        break;
      }
    }
    if (!passesRunControl)
      return false;
  }

  trials++;

  bool accepted = false;
  float fraction = 0;
  // get GeneralTracks collection

  edm::Handle<reco::TrackCollection> tkRef;
  iEvent.getByToken(theTrackCollectionToken, tkRef);
  const reco::TrackCollection* tkColl = tkRef.product();

  int numhighpurity = 0;
  _trackQuality = reco::TrackBase::qualityByName("highPurity");

  if (tkColl->size() > numtrack) {
    reco::TrackCollection::const_iterator itk = tkColl->begin();
    reco::TrackCollection::const_iterator itk_e = tkColl->end();
    for (; itk != itk_e; ++itk) {
      if (itk->quality(_trackQuality) && (itk->pt() >= ptmin))
        numhighpurity++;
    }
    fraction = numhighpurity;  //(float)tkColl->size();
    if (fraction >= thresh)
      accepted = true;
  }

  if (debugOn) {
    int ievt = iEvent.id().event();
    int irun = iEvent.id().run();
    int ils = iEvent.luminosityBlock();
    int bx = iEvent.bunchCrossing();

    edm::LogInfo("FilterOutLowPt") << " Run " << irun << " Event " << ievt << " Lumi Block " << ils
                                   << " Bunch Crossing " << bx << " Fraction " << fraction << " NTracks "
                                   << tkColl->size() << " Accepted " << accepted << std::endl;
  }

  // count the trials and passes
  unsigned int iRun = iEvent.id().run();
  if (eventsInRun_.count(iRun) > 0) {
    eventsInRun_[iRun].first += 1;
    if (accepted)
      eventsInRun_[iRun].second += 1;
  } else {
    std::pair<int, int> mypass = std::make_pair(1, 0);
    if (accepted)
      mypass.second = 1;
    eventsInRun_[iRun] = mypass;
  }

  if (applyfilter) {
    if (accepted)
      passes++;
    return accepted;
  } else
    return true;
}

void FilterOutLowPt::endJob() {
  double eff = passes / trials;
  double eff_err = std::sqrt((eff * (1 - eff)) / trials);

  edm::LogVerbatim("FilterOutLowPt") << "###################################### \n"
                                     << "# FilterOutLowPt::endJob() report \n"
                                     << "# Number of analyzed events: " << trials << " \n"
                                     << "# Number of accpeted events: " << passes << " \n"
                                     << "# Efficiency: " << eff * 100 << " +/- " << eff_err * 100 << " %\n"
                                     << "######################################";

  edm::LogVerbatim("FilterOutLowPt") << "###################################### \n"
                                     << "# Filter Summary events accepted by run";
  for (auto& it : eventsInRun_)
    edm::LogVerbatim("FilterOutLowPt") << "# run:" << it.first << " => events tested: " << (it.second).first
                                       << " | events passed: " << (it.second).second;
  edm::LogVerbatim("FilterOutLowPt") << "###################################### \n";
}

// ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
void FilterOutLowPt::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  std::vector<unsigned int> defaultRuns;
  defaultRuns.push_back(0);

  edm::ParameterSetDescription desc;
  desc.setComment("Filters out soft events without at least `numtrack` tracks with pT> `ptmin`");
  desc.addUntracked<bool>("applyfilter", true);
  desc.addUntracked<bool>("debugOn", false);
  desc.addUntracked<bool>("runControl", false);
  desc.addUntracked<unsigned int>("numtrack", 0);
  desc.addUntracked<double>("ptmin", 3.);
  desc.addUntracked<int>("thresh", 1);
  desc.addUntracked<edm::InputTag>("src", edm::InputTag("generalTracks"));
  desc.addUntracked<std::vector<unsigned int>>("runControlNumber", defaultRuns);
  descriptions.add("filterOutLowPt", desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(FilterOutLowPt);