File indexing completed on 2023-03-17 11:16:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021
0022
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 #include "DataFormats/Common/interface/Ptr.h"
0037
0038
0039
0040
0041
0042 namespace {
0043
0044
0045
0046 template <typename T>
0047 bool equal(const T& lhs, const T& rhs) {
0048 return lhs.superCluster()->seed()->seed().rawId() == rhs.superCluster()->seed()->seed().rawId();
0049 }
0050
0051
0052
0053
0054 template <typename T>
0055 edm::Ptr<T> getObjInColl(edm::Ptr<T> obj, edm::Handle<edm::View<T>> objColl) {
0056 if (objColl.isValid()) {
0057 for (auto& objToMatch : objColl->ptrs()) {
0058 if (equal(*obj, *objToMatch)) {
0059 return objToMatch;
0060 }
0061 }
0062 return edm::Ptr<T>(objColl.id());
0063 }
0064 return obj;
0065 }
0066
0067 }
0068
0069 template <typename T>
0070 class VIDNestedWPBitmapProducer : public edm::stream::EDProducer<> {
0071 public:
0072 explicit VIDNestedWPBitmapProducer(const edm::ParameterSet& iConfig)
0073 : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
0074 srcForIDToken_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcForID"))),
0075 isInit_(false) {
0076 auto const& vwp = iConfig.getParameter<std::vector<std::string>>("WorkingPoints");
0077 for (auto const& wp : vwp) {
0078 src_bitmaps_.push_back(consumes<edm::ValueMap<unsigned int>>(edm::InputTag(wp + std::string("Bitmap"))));
0079 src_cutflows_.push_back(consumes<edm::ValueMap<vid::CutFlowResult>>(edm::InputTag(wp)));
0080 }
0081 nWP = src_bitmaps_.size();
0082 produces<edm::ValueMap<int>>();
0083 }
0084
0085 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0086
0087 private:
0088 void produce(edm::Event&, const edm::EventSetup&) override;
0089
0090
0091
0092 edm::EDGetTokenT<edm::View<T>> src_;
0093 edm::EDGetTokenT<edm::View<T>> srcForIDToken_;
0094 std::vector<edm::EDGetTokenT<edm::ValueMap<unsigned int>>> src_bitmaps_;
0095 std::vector<edm::EDGetTokenT<edm::ValueMap<vid::CutFlowResult>>> src_cutflows_;
0096
0097 unsigned int nWP;
0098 unsigned int nBits;
0099 unsigned int nCuts = 0;
0100 std::vector<unsigned int> res_;
0101 bool isInit_;
0102
0103 void initNCuts(unsigned int);
0104 };
0105
0106 template <typename T>
0107 void VIDNestedWPBitmapProducer<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0108 edm::Handle<edm::View<T>> src;
0109 iEvent.getByToken(src_, src);
0110
0111 auto srcForIDHandle = iEvent.getHandle(srcForIDToken_);
0112
0113 std::vector<edm::Handle<edm::ValueMap<unsigned int>>> src_bitmaps(nWP);
0114 for (unsigned int i = 0; i < nWP; i++)
0115 iEvent.getByToken(src_bitmaps_[i], src_bitmaps[i]);
0116 std::vector<edm::Handle<edm::ValueMap<vid::CutFlowResult>>> src_cutflows(nWP);
0117 for (unsigned int i = 0; i < nWP; i++)
0118 iEvent.getByToken(src_cutflows_[i], src_cutflows[i]);
0119
0120 std::vector<unsigned int> res;
0121
0122 for (auto const& obj : src->ptrs()) {
0123 auto objForID = getObjInColl(obj, srcForIDHandle);
0124 for (unsigned int j = 0; j < nWP; j++) {
0125 auto cutflow = (*(src_cutflows[j]))[objForID];
0126 if (!isInit_)
0127 initNCuts(cutflow.cutFlowSize());
0128 if (cutflow.cutFlowSize() != nCuts)
0129 throw cms::Exception("Configuration", "Trying to compress VID bitmaps for cutflows of different size");
0130 auto bitmap = (*(src_bitmaps[j]))[objForID];
0131 for (unsigned int k = 0; k < nCuts; k++) {
0132 if (j == 0)
0133 res_[k] = 0;
0134 if (bitmap >> k & 1) {
0135 if (res_[k] != j)
0136 throw cms::Exception(
0137 "Configuration",
0138 "Trying to compress VID bitmaps which are not nested in the correct order for all cuts");
0139 res_[k]++;
0140 }
0141 }
0142 }
0143
0144 int out = 0;
0145 for (unsigned int k = 0; k < nCuts; k++)
0146 out |= (res_[k] << (nBits * k));
0147 res.push_back(out);
0148 }
0149
0150 auto resV = std::make_unique<edm::ValueMap<int>>();
0151 edm::ValueMap<int>::Filler filler(*resV);
0152 filler.insert(src, res.begin(), res.end());
0153 filler.fill();
0154
0155 iEvent.put(std::move(resV));
0156 }
0157
0158 template <typename T>
0159 void VIDNestedWPBitmapProducer<T>::initNCuts(unsigned int n) {
0160 nCuts = n;
0161 nBits = ceil(log2(nWP + 1));
0162 if (nBits * nCuts > sizeof(int) * 8)
0163 throw cms::Exception("Configuration", "Integer cannot contain the compressed VID bitmap information");
0164 res_.resize(nCuts, 0);
0165 isInit_ = true;
0166 }
0167
0168
0169 template <typename T>
0170 void VIDNestedWPBitmapProducer<T>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0171 edm::ParameterSetDescription desc;
0172 desc.add<edm::InputTag>("src")->setComment("input physics object collection");
0173 desc.add<edm::InputTag>("srcForID", edm::InputTag())->setComment("physics object collection the ID value maps are ");
0174 desc.add<std::vector<std::string>>("WorkingPoints")->setComment("working points to be saved in the bitmask");
0175 std::string modname;
0176 if (typeid(T) == typeid(reco::GsfElectron))
0177 modname += "Ele";
0178 else if (typeid(T) == typeid(reco::Photon))
0179 modname += "Pho";
0180 modname += "VIDNestedWPBitmapProducer";
0181 descriptions.add(modname, desc);
0182 }
0183
0184 typedef VIDNestedWPBitmapProducer<reco::GsfElectron> EleVIDNestedWPBitmapProducer;
0185 typedef VIDNestedWPBitmapProducer<reco::Photon> PhoVIDNestedWPBitmapProducer;
0186
0187
0188 DEFINE_FWK_MODULE(EleVIDNestedWPBitmapProducer);
0189 DEFINE_FWK_MODULE(PhoVIDNestedWPBitmapProducer);