Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:36

0001 // -*- C++ -*-
0002 //
0003 // Package:    ​RecoBTag/​SecondaryVertex
0004 // Class:      DeepNNTagInfoProducer
0005 //
0006 /**\class DeepNNTagInfoProducer DeepNNTagInfoProducer.cc ​RecoBTag/DeepFlavour/plugins/DeepNNTagInfoProducer.cc
0007  *
0008  * Description: EDProducer that produces collection of DeepNNTagInfos
0009  *
0010  * Implementation:
0011  *    A collection of CandIPTagInfo and CandSecondaryVertexTagInfo and a CombinedSVComputer ESHandle is taken as input and a collection of DeepNNTagInfos
0012  *    is produced as output.
0013  */
0014 //
0015 // Original Author:  Mauro Verzetti (U. Rochester)
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 #include "FWCore/Framework/interface/ESHandle.h"
0026 
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/StreamID.h"
0032 #include "FWCore/Utilities/interface/ESGetToken.h"
0033 
0034 #include "FWCore/Framework/interface/makeRefToBaseProdFrom.h"
0035 #include "DataFormats/Common/interface/RefToBase.h"
0036 
0037 #include "DataFormats/Common/interface/View.h"
0038 #include "DataFormats/BTauReco/interface/CandIPTagInfo.h"
0039 #include "DataFormats/BTauReco/interface/CandSecondaryVertexTagInfo.h"
0040 #include "DataFormats/BTauReco/interface/ShallowTagInfo.h"
0041 #include "DataFormats/BTauReco/interface/TaggingVariable.h"
0042 #include "RecoBTag/SecondaryVertex/interface/CombinedSVComputer.h"
0043 #include "RecoBTau/JetTagComputer/interface/JetTagComputer.h"
0044 #include "RecoBTau/JetTagComputer/interface/JetTagComputerRecord.h"
0045 #include "DataFormats/BTauReco/interface/JetTag.h"
0046 
0047 #include <cmath>
0048 #include <map>
0049 
0050 using namespace reco;
0051 
0052 //
0053 // class declaration
0054 //
0055 
0056 inline bool equals(const edm::RefToBase<Jet>& j1, const edm::RefToBase<Jet>& j2) {
0057   return j1.id() == j2.id() && j1.key() == j2.key();
0058 }
0059 
0060 class DeepCMVATagInfoProducer : public edm::stream::EDProducer<> {
0061 public:
0062   explicit DeepCMVATagInfoProducer(const edm::ParameterSet&);
0063   ~DeepCMVATagInfoProducer() override;
0064 
0065   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0066 
0067 private:
0068   void beginStream(edm::StreamID) override {}
0069   void produce(edm::Event&, const edm::EventSetup&) override;
0070   void endStream() override {}
0071 
0072   // ----------member data ---------------------------
0073   const edm::EDGetTokenT<std::vector<reco::ShallowTagInfo> > deepNNSrc_;
0074   const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > ipInfoSrc_;
0075   const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > muInfoSrc_;
0076   const edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > elInfoSrc_;
0077   std::string jpComputer_, jpbComputer_, softmuComputer_, softelComputer_;
0078   double cMVAPtThreshold_;
0079   edm::ESGetToken<JetTagComputer, JetTagComputerRecord> jpComputerToken_;
0080   edm::ESGetToken<JetTagComputer, JetTagComputerRecord> jpbComputerToken_;
0081   edm::ESGetToken<JetTagComputer, JetTagComputerRecord> softmuComputerToken_;
0082   edm::ESGetToken<JetTagComputer, JetTagComputerRecord> softelComputerToken_;
0083 };
0084 
0085 //
0086 // constants, enums and typedefs
0087 //
0088 
0089 //
0090 // static data member definitions
0091 //
0092 
0093 //
0094 // constructors and destructor
0095 //
0096 DeepCMVATagInfoProducer::DeepCMVATagInfoProducer(const edm::ParameterSet& iConfig)
0097     : deepNNSrc_(consumes<std::vector<reco::ShallowTagInfo> >(iConfig.getParameter<edm::InputTag>("deepNNTagInfos"))),
0098       ipInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("ipInfoSrc"))),
0099       muInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("muInfoSrc"))),
0100       elInfoSrc_(consumes<edm::View<reco::BaseTagInfo> >(iConfig.getParameter<edm::InputTag>("elInfoSrc"))),
0101       jpComputer_(iConfig.getParameter<std::string>("jpComputerSrc")),
0102       jpbComputer_(iConfig.getParameter<std::string>("jpbComputerSrc")),
0103       softmuComputer_(iConfig.getParameter<std::string>("softmuComputerSrc")),
0104       softelComputer_(iConfig.getParameter<std::string>("softelComputerSrc")),
0105       cMVAPtThreshold_(iConfig.getParameter<double>("cMVAPtThreshold")),
0106       jpComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", jpComputer_))),
0107       jpbComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", jpbComputer_))),
0108       softmuComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", softmuComputer_))),
0109       softelComputerToken_(esConsumes<JetTagComputer, JetTagComputerRecord>(edm::ESInputTag("", softelComputer_))) {
0110   produces<std::vector<reco::ShallowTagInfo> >();
0111 }
0112 
0113 DeepCMVATagInfoProducer::~DeepCMVATagInfoProducer() {
0114   // do anything here that needs to be done at destruction time
0115   // (e.g. close files, deallocate resources etc.)
0116 }
0117 
0118 //
0119 // member functions
0120 //
0121 
0122 // ------------ method called to produce the data  ------------
0123 void DeepCMVATagInfoProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0124   // get input TagInfos from DeepCSV
0125   edm::Handle<std::vector<reco::ShallowTagInfo> > nnInfos;
0126   iEvent.getByToken(deepNNSrc_, nnInfos);
0127 
0128   // get other Taginfos
0129   edm::Handle<edm::View<BaseTagInfo> > ipInfos;
0130   iEvent.getByToken(ipInfoSrc_, ipInfos);
0131   edm::Handle<edm::View<BaseTagInfo> > muInfos;
0132   iEvent.getByToken(muInfoSrc_, muInfos);
0133   edm::Handle<edm::View<BaseTagInfo> > elInfos;
0134   iEvent.getByToken(elInfoSrc_, elInfos);
0135 
0136   //get computers
0137   edm::ESHandle<JetTagComputer> jp = iSetup.getHandle(jpComputerToken_);
0138   const JetTagComputer* compjp = jp.product();
0139   edm::ESHandle<JetTagComputer> jpb = iSetup.getHandle(jpbComputerToken_);
0140   const JetTagComputer* compjpb = jpb.product();
0141   edm::ESHandle<JetTagComputer> softmu = iSetup.getHandle(softmuComputerToken_);
0142   const JetTagComputer* compsoftmu = softmu.product();
0143   edm::ESHandle<JetTagComputer> softel = iSetup.getHandle(softelComputerToken_);
0144   const JetTagComputer* compsoftel = softel.product();
0145 
0146   // create the output collection
0147   auto tagInfos = std::make_unique<std::vector<reco::ShallowTagInfo> >();
0148 
0149   // loop over TagInfos, assume they are ordered in the same way, check later and throw exception if not
0150   for (size_t idx = 0; idx < nnInfos->size(); ++idx) {
0151     auto& nnInfo = nnInfos->at(idx);
0152     auto& ipInfo = ipInfos->at(idx);
0153     auto& muInfo = muInfos->at(idx);
0154     auto& elInfo = elInfos->at(idx);
0155 
0156     if (!equals(nnInfo.jet(), ipInfo.jet()) || !equals(nnInfo.jet(), muInfo.jet()) ||
0157         !equals(nnInfo.jet(), elInfo.jet())) {
0158       throw cms::Exception("ValueError")
0159           << "DeepNNTagInfoProducer::produce: The tagInfos taken belong to different jets!" << std::endl
0160           << "This could be due to: " << std::endl
0161           << "  - You passed tagInfos computed on different jet collection" << std::endl
0162           << "  - The assumption that the tagInfos are filled in the same order is actually wrong" << std::endl;
0163     }
0164 
0165     // Make vector of BaseTagInfo, needed for TagInfoHelper
0166     std::vector<const BaseTagInfo*> ipBaseInfo;
0167     ipBaseInfo.push_back(&ipInfo);
0168     std::vector<const BaseTagInfo*> muBaseInfo;
0169     muBaseInfo.push_back(&muInfo);
0170     std::vector<const BaseTagInfo*> elBaseInfo;
0171     elBaseInfo.push_back(&elInfo);
0172 
0173     // Copy the DeepNN TaggingVariables + add the other discriminators
0174     TaggingVariableList vars = nnInfo.taggingVariables();
0175     float softmu_discr = (*compsoftmu)(JetTagComputer::TagInfoHelper(muBaseInfo));
0176     float softel_discr = (*compsoftel)(JetTagComputer::TagInfoHelper(elBaseInfo));
0177     float jp_discr = (*compjp)(JetTagComputer::TagInfoHelper(ipBaseInfo));
0178     float jpb_discr = (*compjpb)(JetTagComputer::TagInfoHelper(ipBaseInfo));
0179 
0180     //if jetPt larger than cMVAPtThreshold_ --> default these taggers for easier SF measurements
0181     if ((nnInfo.jet().get())->pt() < cMVAPtThreshold_) {
0182       vars.insert(reco::btau::Jet_SoftMu, !(std::isinf(softmu_discr)) ? softmu_discr : -0.2, true);
0183       vars.insert(reco::btau::Jet_SoftEl, !(std::isinf(softel_discr)) ? softel_discr : -0.2, true);
0184       vars.insert(reco::btau::Jet_JBP, !(std::isinf(jpb_discr)) ? jpb_discr : -0.2, true);
0185       vars.insert(reco::btau::Jet_JP, !(std::isinf(jp_discr)) ? jp_discr : -0.2, true);
0186     }
0187 
0188     vars.finalize();
0189     tagInfos->emplace_back(vars, nnInfo.jet());
0190 
0191     // just to be sure, clear all containers
0192     ipBaseInfo.clear();
0193     muBaseInfo.clear();
0194     elBaseInfo.clear();
0195   }
0196 
0197   // put the output in the event
0198   iEvent.put(std::move(tagInfos));
0199 }
0200 
0201 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0202 void DeepCMVATagInfoProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0203   //The following says we do not know what parameters are allowed so do no validation
0204   // Please change this to state exactly what you do use, even if it is no parameters
0205   edm::ParameterSetDescription desc;
0206   desc.setUnknown();
0207   descriptions.addDefault(desc);
0208 }
0209 
0210 //define this as a plug-in
0211 DEFINE_FWK_MODULE(DeepCMVATagInfoProducer);