Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    PhysicsTools/NanoAOD
0004 // Class:      VIDNestedWPBitmapProducer
0005 //
0006 /**\class VIDNestedWPBitmapProducer VIDNestedWPBitmapProducer.cc PhysicsTools/NanoAOD/plugins/VIDNestedWPBitmapProducer.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Marco Peruzzi
0015 //         Created:  Mon, 04 Sep 2017 22:43:53 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/stream/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/StreamID.h"
0031 
0032 #include "DataFormats/PatCandidates/interface/Electron.h"
0033 #include "DataFormats/PatCandidates/interface/Photon.h"
0034 
0035 #include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
0036 
0037 //
0038 // class declaration
0039 //
0040 
0041 template <typename T>
0042 class VIDNestedWPBitmapProducer : public edm::stream::EDProducer<> {
0043 public:
0044   explicit VIDNestedWPBitmapProducer(const edm::ParameterSet& iConfig)
0045       : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))), isInit_(false) {
0046     auto const& vwp = iConfig.getParameter<std::vector<std::string>>("WorkingPoints");
0047     for (auto const& wp : vwp) {
0048       src_bitmaps_.push_back(consumes<edm::ValueMap<unsigned int>>(edm::InputTag(wp + std::string("Bitmap"))));
0049       src_cutflows_.push_back(consumes<edm::ValueMap<vid::CutFlowResult>>(edm::InputTag(wp)));
0050     }
0051     nWP = src_bitmaps_.size();
0052     produces<edm::ValueMap<int>>();
0053   }
0054 
0055   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0056 
0057 private:
0058   void produce(edm::Event&, const edm::EventSetup&) override;
0059 
0060   // ----------member data ---------------------------
0061 
0062   edm::EDGetTokenT<edm::View<T>> src_;
0063   std::vector<edm::EDGetTokenT<edm::ValueMap<unsigned int>>> src_bitmaps_;
0064   std::vector<edm::EDGetTokenT<edm::ValueMap<vid::CutFlowResult>>> src_cutflows_;
0065 
0066   unsigned int nWP;
0067   unsigned int nBits;
0068   unsigned int nCuts = 0;
0069   std::vector<unsigned int> res_;
0070   bool isInit_;
0071 
0072   void initNCuts(unsigned int);
0073 };
0074 
0075 template <typename T>
0076 void VIDNestedWPBitmapProducer<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0077   edm::Handle<edm::View<T>> src;
0078   iEvent.getByToken(src_, src);
0079   std::vector<edm::Handle<edm::ValueMap<unsigned int>>> src_bitmaps(nWP);
0080   for (unsigned int i = 0; i < nWP; i++)
0081     iEvent.getByToken(src_bitmaps_[i], src_bitmaps[i]);
0082   std::vector<edm::Handle<edm::ValueMap<vid::CutFlowResult>>> src_cutflows(nWP);
0083   for (unsigned int i = 0; i < nWP; i++)
0084     iEvent.getByToken(src_cutflows_[i], src_cutflows[i]);
0085 
0086   std::vector<unsigned int> res;
0087 
0088   for (auto const& obj : src->ptrs()) {
0089     for (unsigned int j = 0; j < nWP; j++) {
0090       auto cutflow = (*(src_cutflows[j]))[obj];
0091       if (!isInit_)
0092         initNCuts(cutflow.cutFlowSize());
0093       if (cutflow.cutFlowSize() != nCuts)
0094         throw cms::Exception("Configuration", "Trying to compress VID bitmaps for cutflows of different size");
0095       auto bitmap = (*(src_bitmaps[j]))[obj];
0096       for (unsigned int k = 0; k < nCuts; k++) {
0097         if (j == 0)
0098           res_[k] = 0;
0099         if (bitmap >> k & 1) {
0100           if (res_[k] != j)
0101             throw cms::Exception(
0102                 "Configuration",
0103                 "Trying to compress VID bitmaps which are not nested in the correct order for all cuts");
0104           res_[k]++;
0105         }
0106       }
0107     }
0108 
0109     int out = 0;
0110     for (unsigned int k = 0; k < nCuts; k++)
0111       out |= (res_[k] << (nBits * k));
0112     res.push_back(out);
0113   }
0114 
0115   auto resV = std::make_unique<edm::ValueMap<int>>();
0116   edm::ValueMap<int>::Filler filler(*resV);
0117   filler.insert(src, res.begin(), res.end());
0118   filler.fill();
0119 
0120   iEvent.put(std::move(resV));
0121 }
0122 
0123 template <typename T>
0124 void VIDNestedWPBitmapProducer<T>::initNCuts(unsigned int n) {
0125   nCuts = n;
0126   nBits = ceil(log2(nWP + 1));
0127   if (nBits * nCuts > sizeof(int) * 8)
0128     throw cms::Exception("Configuration", "Integer cannot contain the compressed VID bitmap information");
0129   res_.resize(nCuts, 0);
0130   isInit_ = true;
0131 }
0132 
0133 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0134 template <typename T>
0135 void VIDNestedWPBitmapProducer<T>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0136   edm::ParameterSetDescription desc;
0137   desc.add<edm::InputTag>("src")->setComment("input physics object collection");
0138   desc.add<std::vector<std::string>>("WorkingPoints")->setComment("working points to be saved in the bitmask");
0139   std::string modname;
0140   if (typeid(T) == typeid(pat::Electron))
0141     modname += "Ele";
0142   else if (typeid(T) == typeid(pat::Photon))
0143     modname += "Pho";
0144   modname += "VIDNestedWPBitmapProducer";
0145   descriptions.add(modname, desc);
0146 }
0147 
0148 typedef VIDNestedWPBitmapProducer<pat::Electron> EleVIDNestedWPBitmapProducer;
0149 typedef VIDNestedWPBitmapProducer<pat::Photon> PhoVIDNestedWPBitmapProducer;
0150 
0151 //define this as a plug-in
0152 DEFINE_FWK_MODULE(EleVIDNestedWPBitmapProducer);
0153 DEFINE_FWK_MODULE(PhoVIDNestedWPBitmapProducer);