Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-11 03:13:32

0001 // author: Florent Lacroix (UIC)
0002 // date: 07/14/2009
0003 
0004 #include "DataFormats/Candidate/interface/Candidate.h"
0005 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0006 #include "DataFormats/Common/interface/Handle.h"
0007 #include "FWCore/Framework/interface/global/EDFilter.h"
0008 #include "FWCore/Framework/interface/ESHandle.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 class PFFilter : public edm::global::EDFilter<> {
0014 public:
0015   explicit PFFilter(const edm::ParameterSet &);
0016   ~PFFilter() override;
0017 
0018   bool filter(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
0019   void beginJob() override;
0020   void endJob() override;
0021   bool checkInput() const;
0022 
0023 private:
0024   std::vector<std::string> collections_;
0025   std::vector<std::string> variables_;
0026   std::vector<double> min_;
0027   std::vector<double> max_;
0028   std::vector<int> doMin_;
0029   std::vector<int> doMax_;
0030 };
0031 
0032 PFFilter::PFFilter(const edm::ParameterSet &iConfig) {
0033   collections_ = iConfig.getParameter<std::vector<std::string>>("Collections");
0034   variables_ = iConfig.getParameter<std::vector<std::string>>("Variables");
0035   min_ = iConfig.getParameter<std::vector<double>>("Mins");
0036   max_ = iConfig.getParameter<std::vector<double>>("Maxs");
0037   doMin_ = iConfig.getParameter<std::vector<int>>("DoMin");
0038   doMax_ = iConfig.getParameter<std::vector<int>>("DoMax");
0039 }
0040 
0041 PFFilter::~PFFilter() {}
0042 
0043 bool PFFilter::checkInput() const {
0044   if (collections_.size() != min_.size()) {
0045     std::cout << "Error: in PFFilter: collections_.size()!=min_.size()" << std::endl;
0046     std::cout << "collections_.size() = " << collections_.size() << std::endl;
0047     std::cout << "min_.size() = " << min_.size() << std::endl;
0048     return false;
0049   }
0050   if (collections_.size() != max_.size()) {
0051     std::cout << "Error: in PFFilter: collections_.size()!=max_.size()" << std::endl;
0052     std::cout << "collections_.size() = " << collections_.size() << std::endl;
0053     std::cout << "max_.size() = " << max_.size() << std::endl;
0054     return false;
0055   }
0056   if (collections_.size() != doMin_.size()) {
0057     std::cout << "Error: in PFFilter: collections_.size()!=min_.size()" << std::endl;
0058     std::cout << "collections_.size() = " << collections_.size() << std::endl;
0059     std::cout << "doMin_.size() = " << doMin_.size() << std::endl;
0060     return false;
0061   }
0062   if (collections_.size() != doMax_.size()) {
0063     std::cout << "Error: in PFFilter: collections_.size()!=min_.size()" << std::endl;
0064     std::cout << "collections_.size() = " << collections_.size() << std::endl;
0065     std::cout << "doMax_.size() = " << doMax_.size() << std::endl;
0066     return false;
0067   }
0068   if (collections_.size() != variables_.size()) {
0069     std::cout << "Error: in PFFilter: collections_.size()!=variables_.size()" << std::endl;
0070     std::cout << "collections_.size() = " << collections_.size() << std::endl;
0071     std::cout << "variables_.size() = " << variables_.size() << std::endl;
0072     return false;
0073   }
0074   return true;
0075 }
0076 
0077 void PFFilter::beginJob() {
0078   // std::cout << "FL: beginJob" << std::endl;
0079 }
0080 
0081 bool PFFilter::filter(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const {
0082   // std::cout << "FL: filter" << std::endl;
0083   // std::cout << "FL: Mins = " << min_ << std::endl;
0084 
0085   if (!checkInput())
0086     return true;  // no filtering !
0087 
0088   bool skip = false;
0089 
0090   for (unsigned int varc = 0; varc < collections_.size(); ++varc) {
0091     // std::cout << "FL: var[" << varc << "] = " << collections_[varc] <<
0092     // std::endl; std::cout << "FL: var[0] = " << collections_[0] << std::endl;
0093 
0094     // if the collection is collection1-collection2:
0095     const unsigned int minuspos = collections_[varc].find('-');
0096     if (minuspos < collections_[varc].size()) {
0097       std::string collection1;
0098       collection1.assign(collections_[varc], 0, minuspos);
0099       // std::cout << "collection1 = " << collection1 << std::endl;
0100       std::string collection2;
0101       collection2.assign(collections_[varc], minuspos + 1, collections_[varc].size());
0102       // std::cout << "collection2 = " << collection2 << std::endl;
0103 
0104       const edm::View<reco::Candidate> *var1;
0105       edm::Handle<edm::View<reco::Candidate>> var1_hnd;
0106       bool isVar1 = iEvent.getByLabel(collection1, var1_hnd);
0107       if (!isVar1) {
0108         std::cout << "Warning : no " << collection1 << " in input !" << std::endl;
0109         return false;
0110       }
0111       var1 = var1_hnd.product();
0112       const reco::Candidate *var10 = &(*var1)[0];
0113       // std::cout << "FL: var10.pt = " << var10->et() << std::endl;
0114       double coll_var1;
0115       if (variables_[varc] == "et")
0116         coll_var1 = var10->et();
0117       else if (variables_[varc] == "phi")
0118         coll_var1 = var10->phi();
0119       else if (variables_[varc] == "eta")
0120         coll_var1 = var10->eta();
0121       else {
0122         std::cout << "Error: PFFilter: variable unknown: " << variables_[varc] << std::endl;
0123         return true;
0124       }
0125       // std::cout << "FL: coll_var1[" << variables_[varc] << "] = " <<
0126       // coll_var1 << std::endl;
0127 
0128       const edm::View<reco::Candidate> *var2;
0129       edm::Handle<edm::View<reco::Candidate>> var2_hnd;
0130       bool isVar2 = iEvent.getByLabel(collection2, var2_hnd);
0131       if (!isVar2) {
0132         std::cout << "Warning : no " << collection2 << " in input !" << std::endl;
0133         return false;
0134       }
0135       var2 = var2_hnd.product();
0136       const reco::Candidate *var20 = &(*var2)[0];
0137       // std::cout << "FL: var20.pt = " << var20->et() << std::endl;
0138       double coll_var2;
0139       if (variables_[varc] == "et")
0140         coll_var2 = var20->et();
0141       else if (variables_[varc] == "phi")
0142         coll_var2 = var20->phi();
0143       else if (variables_[varc] == "eta")
0144         coll_var2 = var20->eta();
0145       else {
0146         std::cout << "Error: PFFilter: variable unknown: " << variables_[varc] << std::endl;
0147         return true;
0148       }
0149       // std::cout << "FL: coll_var2[" << variables_[varc] << "] = " <<
0150       // coll_var2 << std::endl; std::cout << "FL: max_[varc] = " << max_[varc]
0151       // << std::endl; std::cout << "FL: min_[varc] = " << min_[varc] <<
0152       // std::endl;
0153 
0154       // Delta computation
0155       double delta = coll_var1 - coll_var2;
0156       if (variables_[varc] == "phi") {
0157         if (coll_var1 > M_PI)
0158           coll_var1 -= ceil((coll_var1 - M_PI) / (2 * M_PI)) * 2 * M_PI;
0159         if (coll_var1 <= -M_PI)
0160           coll_var1 += ceil((coll_var1 + M_PI) / (-2. * M_PI)) * 2. * M_PI;
0161         if (coll_var2 > M_PI)
0162           coll_var2 -= ceil((coll_var2 - M_PI) / (2 * M_PI)) * 2 * M_PI;
0163         if (coll_var2 <= -M_PI)
0164           coll_var2 += ceil((coll_var2 + M_PI) / (-2. * M_PI)) * 2 * M_PI;
0165 
0166         double deltaphi = -999.0;
0167         if (fabs(coll_var1 - coll_var2) < M_PI) {
0168           deltaphi = (coll_var1 - coll_var2);
0169         } else {
0170           if ((coll_var1 - coll_var2) > 0.0) {
0171             deltaphi = (2 * M_PI - fabs(coll_var1 - coll_var2));
0172           } else {
0173             deltaphi = -(2 * M_PI - fabs(coll_var1 - coll_var2));
0174           }
0175         }
0176         delta = deltaphi;
0177       }
0178 
0179       // cuts
0180       if (doMin_[varc] && doMax_[varc] && max_[varc] < min_[varc]) {
0181         if (delta > max_[varc] && delta < min_[varc])
0182           skip = true;
0183       } else {
0184         if (doMin_[varc] && delta < min_[varc])
0185           skip = true;
0186         if (doMax_[varc] && delta > max_[varc])
0187           skip = true;
0188       }
0189       // std::cout << "skip = " << skip << std::endl;
0190     } else {
0191       // get the variable:
0192       const edm::View<reco::Candidate> *var0;
0193       edm::Handle<edm::View<reco::Candidate>> var0_hnd;
0194       bool isVar0 = iEvent.getByLabel(collections_[varc], var0_hnd);
0195       if (!isVar0) {
0196         std::cout << "Warning : no " << collections_[varc] << " in input !" << std::endl;
0197         return false;
0198       }
0199       var0 = var0_hnd.product();
0200       const reco::Candidate *var00 = &(*var0)[0];
0201       // std::cout << "FL: var00.pt = " << var00->et() << std::endl;
0202       double coll_var;
0203       if (variables_[varc] == "et")
0204         coll_var = var00->et();
0205       else if (variables_[varc] == "phi")
0206         coll_var = var00->phi();
0207       else if (variables_[varc] == "eta")
0208         coll_var = var00->eta();
0209       else {
0210         std::cout << "Error: PFFilter: variable unknown: " << variables_[varc] << std::endl;
0211         return true;
0212       }
0213       // std::cout << "FL: coll_var[" << variables_[varc] << "] = " << coll_var
0214       // << std::endl; std::cout << "FL: max_[varc] = " << max_[varc] <<
0215       // std::endl; std::cout << "FL: min_[varc] = " << min_[varc] << std::endl;
0216 
0217       // cuts
0218       if (doMin_[varc] && doMax_[varc] && max_[varc] < min_[varc]) {
0219         if (coll_var > max_[varc] && coll_var < min_[varc])
0220           skip = true;
0221       } else {
0222         if (doMin_[varc] && coll_var < min_[varc])
0223           skip = true;
0224         if (doMax_[varc] && coll_var > max_[varc])
0225           skip = true;
0226       }
0227       // std::cout << "skip = " << skip << std::endl;
0228     }
0229   }
0230   // std::cout << "final skip = " << skip << std::endl;
0231   return !skip;
0232 }
0233 
0234 void PFFilter::endJob() {
0235   // std::cout << "FL: endJob" << std::endl;
0236 }