Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:10

0001 #ifndef PhysicsTools_SelectorUtils_VersionedIdProducer_h
0002 #define PhysicsTools_SelectorUtils_VersionedIdProducer_h
0003 // system include files
0004 #include <memory>
0005 
0006 // user include files
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/stream/EDProducer.h"
0009 
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 
0015 #include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
0016 #include "DataFormats/PatCandidates/interface/UserData.h"
0017 
0018 #include "DataFormats/Common/interface/View.h"
0019 #include "DataFormats/Common/interface/RefToPtr.h"
0020 
0021 #include "PhysicsTools/SelectorUtils/interface/VersionedSelector.h"
0022 
0023 #include <memory>
0024 #include <sstream>
0025 
0026 template <class PhysicsObjectPtr, class SelectorType = VersionedSelector<PhysicsObjectPtr>>
0027 class VersionedIdProducer : public edm::stream::EDProducer<> {
0028 public:
0029   using PhysicsObjectType = typename PhysicsObjectPtr::value_type;
0030 
0031   using Collection = edm::View<PhysicsObjectType>;
0032   using TokenType = edm::EDGetTokenT<Collection>;
0033 
0034   explicit VersionedIdProducer(const edm::ParameterSet&);
0035   ~VersionedIdProducer() override {}
0036 
0037   void produce(edm::Event&, const edm::EventSetup&) override;
0038 
0039 private:
0040   // ----------member data ---------------------------
0041   bool verbose_;
0042   TokenType physicsObjectSrc_;
0043 
0044   std::vector<std::unique_ptr<SelectorType>> ids_;
0045 };
0046 
0047 //
0048 // constants, enums and typedefs
0049 //
0050 
0051 //
0052 // static data member definitions
0053 //
0054 
0055 //
0056 // constructors and destructor
0057 //
0058 template <class PhysicsObjectPtr, class SelectorType>
0059 VersionedIdProducer<PhysicsObjectPtr, SelectorType>::VersionedIdProducer(const edm::ParameterSet& iConfig) {
0060   constexpr char bitmap_label[] = "Bitmap";
0061 
0062   verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
0063 
0064   physicsObjectSrc_ = consumes<Collection>(iConfig.getParameter<edm::InputTag>("physicsObjectSrc"));
0065 
0066   const std::vector<edm::ParameterSet>& ids = iConfig.getParameterSetVector("physicsObjectIDs");
0067   for (const auto& id : ids) {
0068     const std::string& idMD5 = id.getParameter<std::string>("idMD5");
0069     const edm::ParameterSet& the_id = id.getParameterSet("idDefinition");
0070     const std::string& idname = the_id.getParameter<std::string>("idName");
0071     std::string calculated_md5;
0072     ids_.emplace_back(new SelectorType(the_id));
0073     calculated_md5 = ids_.back()->md5String();
0074     ids_.back()->setConsumes(consumesCollector());
0075     if (ids_.back()->cutFlowSize() == 0) {
0076       throw cms::Exception("InvalidCutFlow") << "Post-processing cutflow size is zero! You may have configured"
0077                                              << " the python incorrectly!";
0078     }
0079 
0080     if (idMD5 != calculated_md5) {
0081       edm::LogInfo("IdConfigurationNotValidated") << "ID: " << ids_.back()->name() << "\n"
0082                                                   << "The expected md5: " << idMD5 << " does not match the md5\n"
0083                                                   << "calculated by the ID: " << calculated_md5 << " please\n"
0084                                                   << "update your python configuration or determine the source\n"
0085                                                   << "of transcription error!";
0086     }
0087 
0088     std::stringstream idmsg;
0089 
0090     // dump whatever information about the ID we have
0091     idmsg << "Instantiated ID: " << idname << std::endl << "with MD5 hash: " << idMD5 << std::endl;
0092     const bool isPOGApproved = id.getUntrackedParameter<bool>("isPOGApproved", false);
0093     if (isPOGApproved) {
0094       idmsg << "This ID is POG approved!" << std::endl;
0095     } else {
0096       idmsg << "This ID is not POG approved and likely under development!!!\n"
0097             << "Please make sure to report your progress with this ID "
0098             << "at the next relevant POG meeting." << std::endl;
0099     }
0100 
0101     if (!isPOGApproved) {
0102       edm::LogWarning("IdInformation") << idmsg.str();
0103     } else {
0104       edm::LogInfo("IdInformation") << idmsg.str();
0105     }
0106 
0107     produces<std::string>(idname);
0108     produces<edm::ValueMap<bool>>(idname);
0109     produces<edm::ValueMap<float>>(idname);  // for PAT
0110     produces<edm::ValueMap<unsigned>>(idname);
0111     produces<edm::ValueMap<unsigned>>(idname + std::string(bitmap_label));
0112     produces<edm::ValueMap<vid::CutFlowResult>>(idname);
0113     produces<pat::UserDataCollection>(idname);
0114     produces<edm::ValueMap<edm::Ptr<pat::UserData>>>(idname);
0115   }
0116 }
0117 
0118 template <class PhysicsObjectPtr, class SelectorType>
0119 void VersionedIdProducer<PhysicsObjectPtr, SelectorType>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120   constexpr char bitmap_label[] = "Bitmap";
0121 
0122   edm::Handle<Collection> physicsObjectsHandle;
0123   iEvent.getByToken(physicsObjectSrc_, physicsObjectsHandle);
0124 
0125   const Collection& physicsobjects = *physicsObjectsHandle;
0126 
0127   for (const auto& id : ids_) {
0128     auto outPass = std::make_unique<edm::ValueMap<bool>>();
0129     auto outPassf = std::make_unique<edm::ValueMap<float>>();
0130     auto outHowFar = std::make_unique<edm::ValueMap<unsigned>>();
0131     auto outBitmap = std::make_unique<edm::ValueMap<unsigned>>();
0132     auto out_cfrs = std::make_unique<edm::ValueMap<vid::CutFlowResult>>();
0133     auto out_usrd = std::make_unique<pat::UserDataCollection>();
0134 
0135     std::vector<bool> passfail;
0136     std::vector<float> passfailf;
0137     std::vector<unsigned> howfar;
0138     std::vector<unsigned> bitmap;
0139     std::vector<vid::CutFlowResult> cfrs;
0140 
0141     for (size_t i = 0; i < physicsobjects.size(); ++i) {
0142       auto po = physicsobjects.ptrAt(i);
0143       passfail.push_back((*id)(po, iEvent));
0144       passfailf.push_back(passfail.back());
0145       howfar.push_back(id->howFarInCutFlow());
0146       bitmap.push_back(id->bitMap());
0147       cfrs.push_back(id->cutFlowResult());
0148       out_usrd->push_back(pat::UserData::make(cfrs.back(), false));
0149     }
0150 
0151     edm::ValueMap<bool>::Filler fillerpassfail(*outPass);
0152     fillerpassfail.insert(physicsObjectsHandle, passfail.begin(), passfail.end());
0153     fillerpassfail.fill();
0154 
0155     edm::ValueMap<float>::Filler fillerpassfailf(*outPassf);
0156     fillerpassfailf.insert(physicsObjectsHandle, passfailf.begin(), passfailf.end());
0157     fillerpassfailf.fill();
0158 
0159     edm::ValueMap<unsigned>::Filler fillerhowfar(*outHowFar);
0160     fillerhowfar.insert(physicsObjectsHandle, howfar.begin(), howfar.end());
0161     fillerhowfar.fill();
0162 
0163     edm::ValueMap<unsigned>::Filler fillerbitmap(*outBitmap);
0164     fillerbitmap.insert(physicsObjectsHandle, bitmap.begin(), bitmap.end());
0165     fillerbitmap.fill();
0166 
0167     edm::ValueMap<vid::CutFlowResult>::Filler fillercfr(*out_cfrs);
0168     fillercfr.insert(physicsObjectsHandle, cfrs.begin(), cfrs.end());
0169     fillercfr.fill();
0170 
0171     iEvent.put(std::move(outPass), id->name());
0172     iEvent.put(std::move(outPassf), id->name());
0173     iEvent.put(std::move(outHowFar), id->name());
0174     iEvent.put(std::move(outBitmap), id->name() + std::string(bitmap_label));
0175     iEvent.put(std::move(out_cfrs), id->name());
0176     iEvent.put(std::make_unique<std::string>(id->md5String()), id->name());
0177     auto usrd_handle = iEvent.put(std::move(out_usrd), id->name());
0178     //now add the value map of ptrs to user datas
0179     auto out_usrdptrs = std::make_unique<edm::ValueMap<edm::Ptr<pat::UserData>>>();
0180     std::vector<edm::Ptr<pat::UserData>> usrdptrs;
0181     for (unsigned i = 0; i < usrd_handle->size(); ++i) {
0182       usrdptrs.push_back(edm::Ptr<pat::UserData>(usrd_handle, i));
0183     }
0184 
0185     edm::ValueMap<edm::Ptr<pat::UserData>>::Filler fillerusrdptrs(*out_usrdptrs);
0186     fillerusrdptrs.insert(physicsObjectsHandle, usrdptrs.begin(), usrdptrs.end());
0187     fillerusrdptrs.fill();
0188 
0189     iEvent.put(std::move(out_usrdptrs), id->name());
0190   }
0191 }
0192 
0193 #endif