Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:06

0001 // -*- C++ -*-
0002 //
0003 // Package:   Alignment/CommonAlignment
0004 // Class:     FilterOutLowPt
0005 //
0006 //
0007 // Original Author:  Marco Musich
0008 
0009 #include <memory>
0010 #include <vector>
0011 #include <map>
0012 #include <set>
0013 #include <utility>  // std::pair
0014 #include "TMath.h"
0015 
0016 // user include files
0017 
0018 #include "FWCore/Utilities/interface/InputTag.h"
0019 #include "FWCore/Framework/interface/Frameworkfwd.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/stream/EDFilter.h"
0022 #include "DataFormats/TrackReco/interface/Track.h"
0023 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0024 #include "FWCore/Utilities/interface/EDGetToken.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/Framework/interface/ESHandle.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 
0030 class FilterOutLowPt : public edm::stream::EDFilter<> {
0031 public:
0032   explicit FilterOutLowPt(const edm::ParameterSet&);
0033   ~FilterOutLowPt() override;
0034 
0035   static void fillDescriptions(edm::ConfigurationDescriptions&);
0036 
0037 private:
0038   virtual void beginJob();
0039   bool filter(edm::Event&, const edm::EventSetup&) override;
0040   virtual void endJob();
0041 
0042   // ----------member data --------
0043   const bool applyfilter;
0044   const bool debugOn;
0045   const bool runControl_;
0046   const double ptmin;
0047   const double thresh;
0048   const unsigned int numtrack;
0049 
0050   double trials;
0051   double passes;
0052 
0053   const std::vector<unsigned int> runControlNumbers_;
0054   std::map<unsigned int, std::pair<int, int>> eventsInRun_;
0055 
0056   reco::TrackBase::TrackQuality _trackQuality;
0057   edm::EDGetTokenT<reco::TrackCollection> theTrackCollectionToken;
0058 };
0059 
0060 FilterOutLowPt::FilterOutLowPt(const edm::ParameterSet& iConfig)
0061     : applyfilter(iConfig.getUntrackedParameter<bool>("applyfilter")),
0062       debugOn(iConfig.getUntrackedParameter<bool>("debugOn")),
0063       runControl_(iConfig.getUntrackedParameter<bool>("runControl")),
0064       ptmin(iConfig.getUntrackedParameter<double>("ptmin")),
0065       thresh(iConfig.getUntrackedParameter<int>("thresh")),
0066       numtrack(iConfig.getUntrackedParameter<unsigned int>("numtrack")),
0067       runControlNumbers_(iConfig.getUntrackedParameter<std::vector<unsigned int>>("runControlNumber")),
0068       theTrackCollectionToken(consumes<reco::TrackCollection>(iConfig.getUntrackedParameter<edm::InputTag>("src"))) {}
0069 
0070 FilterOutLowPt::~FilterOutLowPt() {}
0071 
0072 void FilterOutLowPt::beginJob() {
0073   trials = 0;
0074   passes = 0;
0075 }
0076 
0077 bool FilterOutLowPt::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0078   bool passesRunControl = false;
0079 
0080   if (runControl_) {
0081     if (debugOn) {
0082       for (unsigned int runControlNumber : runControlNumbers_) {
0083         edm::LogInfo("FilterOutLowPt") << "run number:" << iEvent.id().run() << " keeping runs:" << runControlNumber
0084                                        << std::endl;
0085       }
0086     }
0087 
0088     for (unsigned int runControlNumber : runControlNumbers_) {
0089       if (iEvent.eventAuxiliary().run() == runControlNumber) {
0090         if (debugOn) {
0091           edm::LogInfo("FilterOutLowPt") << "run number" << runControlNumber << " match!" << std::endl;
0092         }
0093         passesRunControl = true;
0094         break;
0095       }
0096     }
0097     if (!passesRunControl)
0098       return false;
0099   }
0100 
0101   trials++;
0102 
0103   bool accepted = false;
0104   float fraction = 0;
0105   // get GeneralTracks collection
0106 
0107   edm::Handle<reco::TrackCollection> tkRef;
0108   iEvent.getByToken(theTrackCollectionToken, tkRef);
0109   const reco::TrackCollection* tkColl = tkRef.product();
0110 
0111   int numhighpurity = 0;
0112   _trackQuality = reco::TrackBase::qualityByName("highPurity");
0113 
0114   if (tkColl->size() > numtrack) {
0115     reco::TrackCollection::const_iterator itk = tkColl->begin();
0116     reco::TrackCollection::const_iterator itk_e = tkColl->end();
0117     for (; itk != itk_e; ++itk) {
0118       if (itk->quality(_trackQuality) && (itk->pt() >= ptmin))
0119         numhighpurity++;
0120     }
0121     fraction = numhighpurity;  //(float)tkColl->size();
0122     if (fraction >= thresh)
0123       accepted = true;
0124   }
0125 
0126   if (debugOn) {
0127     int ievt = iEvent.id().event();
0128     int irun = iEvent.id().run();
0129     int ils = iEvent.luminosityBlock();
0130     int bx = iEvent.bunchCrossing();
0131 
0132     edm::LogInfo("FilterOutLowPt") << " Run " << irun << " Event " << ievt << " Lumi Block " << ils
0133                                    << " Bunch Crossing " << bx << " Fraction " << fraction << " NTracks "
0134                                    << tkColl->size() << " Accepted " << accepted << std::endl;
0135   }
0136 
0137   // count the trials and passes
0138   unsigned int iRun = iEvent.id().run();
0139   if (eventsInRun_.count(iRun) > 0) {
0140     eventsInRun_[iRun].first += 1;
0141     if (accepted)
0142       eventsInRun_[iRun].second += 1;
0143   } else {
0144     std::pair<int, int> mypass = std::make_pair(1, 0);
0145     if (accepted)
0146       mypass.second = 1;
0147     eventsInRun_[iRun] = mypass;
0148   }
0149 
0150   if (applyfilter) {
0151     if (accepted)
0152       passes++;
0153     return accepted;
0154   } else
0155     return true;
0156 }
0157 
0158 void FilterOutLowPt::endJob() {
0159   double eff = passes / trials;
0160   double eff_err = std::sqrt((eff * (1 - eff)) / trials);
0161 
0162   edm::LogVerbatim("FilterOutLowPt") << "###################################### \n"
0163                                      << "# FilterOutLowPt::endJob() report \n"
0164                                      << "# Number of analyzed events: " << trials << " \n"
0165                                      << "# Number of accpeted events: " << passes << " \n"
0166                                      << "# Efficiency: " << eff * 100 << " +/- " << eff_err * 100 << " %\n"
0167                                      << "######################################";
0168 
0169   edm::LogVerbatim("FilterOutLowPt") << "###################################### \n"
0170                                      << "# Filter Summary events accepted by run";
0171   for (auto& it : eventsInRun_)
0172     edm::LogVerbatim("FilterOutLowPt") << "# run:" << it.first << " => events tested: " << (it.second).first
0173                                        << " | events passed: " << (it.second).second;
0174   edm::LogVerbatim("FilterOutLowPt") << "###################################### \n";
0175 }
0176 
0177 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0178 void FilterOutLowPt::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0179   std::vector<unsigned int> defaultRuns;
0180   defaultRuns.push_back(0);
0181 
0182   edm::ParameterSetDescription desc;
0183   desc.setComment("Filters out soft events without at least `numtrack` tracks with pT> `ptmin`");
0184   desc.addUntracked<bool>("applyfilter", true);
0185   desc.addUntracked<bool>("debugOn", false);
0186   desc.addUntracked<bool>("runControl", false);
0187   desc.addUntracked<unsigned int>("numtrack", 0);
0188   desc.addUntracked<double>("ptmin", 3.);
0189   desc.addUntracked<int>("thresh", 1);
0190   desc.addUntracked<edm::InputTag>("src", edm::InputTag("generalTracks"));
0191   desc.addUntracked<std::vector<unsigned int>>("runControlNumber", defaultRuns);
0192   descriptions.add("filterOutLowPt", desc);
0193 }
0194 
0195 //define this as a plug-in
0196 DEFINE_FWK_MODULE(FilterOutLowPt);