Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:35

0001 #include "DataFormats/Common/interface/Handle.h"
0002 #include "DataFormats/Provenance/interface/Provenance.h"
0003 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0004 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/ParameterSet/interface/Registry.h"
0009 #include "FWCore/Utilities/interface/EDGetToken.h"
0010 #include "FWCore/Utilities/interface/InputTag.h"
0011 
0012 namespace edm {
0013   class EventSetup;
0014   class StreamID;
0015 }  // namespace edm
0016 
0017 namespace edmtest {
0018   class SwitchProducerProvenanceAnalyzer : public edm::global::EDAnalyzer<> {
0019   public:
0020     explicit SwitchProducerProvenanceAnalyzer(edm::ParameterSet const& iConfig);
0021     void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const& iSetup) const override;
0022 
0023   private:
0024     void testProduct(edm::Handle<IntProduct> const& prod, int mode, edm::Event const& iEvent) const;
0025 
0026     edm::EDGetTokenT<IntProduct> inputToken1_;
0027     edm::EDGetTokenT<IntProduct> inputToken2_;
0028     std::string producerPrefix_;
0029     bool const aliasMode_;
0030   };
0031 
0032   SwitchProducerProvenanceAnalyzer::SwitchProducerProvenanceAnalyzer(edm::ParameterSet const& iConfig)
0033       : inputToken1_(consumes(iConfig.getParameter<edm::InputTag>("src1"))),
0034         inputToken2_(consumes(iConfig.getParameter<edm::InputTag>("src2"))),
0035         producerPrefix_(iConfig.getParameter<std::string>("producerPrefix")),
0036         aliasMode_(iConfig.getParameter<bool>("aliasMode")) {}
0037 
0038   void SwitchProducerProvenanceAnalyzer::analyze(edm::StreamID,
0039                                                  edm::Event const& iEvent,
0040                                                  edm::EventSetup const& iSetup) const {
0041     edm::Handle<IntProduct> h;
0042     iEvent.getByToken(inputToken1_, h);
0043     testProduct(h, iEvent.id().luminosityBlock(), iEvent);
0044 
0045     iEvent.getByToken(inputToken2_, h);
0046     testProduct(h, iEvent.id().luminosityBlock(), iEvent);
0047   }
0048 
0049   void SwitchProducerProvenanceAnalyzer::testProduct(edm::Handle<IntProduct> const& prod,
0050                                                      int mode,
0051                                                      edm::Event const& iEvent) const {
0052     assert(prod->value == mode);
0053 
0054     edm::Provenance const* provenance = prod.provenance();
0055     assert(provenance != nullptr);
0056     auto const* productProvenance = provenance->productProvenance();
0057     assert(productProvenance != nullptr);
0058     auto const& processHistory = iEvent.processHistory();
0059 
0060     edm::pset::Registry const* psetRegistry = edm::pset::Registry::instance();
0061     assert(psetRegistry != nullptr);
0062 
0063     auto const& moduleLabel = provenance->moduleLabel();
0064 
0065     // Switch output should not look like an alias
0066     assert(productProvenance->branchID() == provenance->branchID());
0067 
0068     // Check that the provenance of the Switch itself is recorded correctly
0069     for (edm::ProcessConfiguration const& pc : processHistory) {
0070       if (pc.processName() == provenance->processName()) {
0071         edm::ParameterSetID const& psetID = pc.parameterSetID();
0072         edm::ParameterSet const* processPSet = psetRegistry->getMapped(psetID);
0073         assert(processPSet);
0074         auto const& modPSet = processPSet->getParameterSet(moduleLabel);
0075         assert(modPSet.getParameter<std::string>("@module_edm_type") == "EDProducer");
0076         assert(modPSet.getParameter<std::string>("@module_type") == "SwitchProducer");
0077         assert(modPSet.getParameter<std::string>("@module_label") == moduleLabel);
0078         auto const& allCases = modPSet.getParameter<std::vector<std::string>>("@all_cases");
0079         assert(allCases.size() == 2);
0080         assert(allCases[0] == moduleLabel + "@test1");
0081         assert(allCases[1] == moduleLabel + "@test2");
0082         assert(modPSet.exists("@chosen_case") == false);
0083       }
0084     }
0085 
0086     // Check the parentage (foo -> foo@case -> possible input)
0087     auto const& parent = productProvenance->parentage();
0088     // Here is where Switch differs from a normal EDProducer: each Switch output branch has exactly one parent
0089     assert(parent.parents().size() == 1);
0090     auto const& parentProvenance = iEvent.getProvenance(parent.parents()[0]);
0091     edm::ProductProvenance const* parentProductProvenance = nullptr;
0092     if (not(aliasMode_ and mode == 2)) {
0093       // If parent is EDAlias, it is skipped in the provenance, so in
0094       // that case the normal grandparent can be found on the place of
0095       // the parent.
0096 
0097       assert(parentProvenance.branchDescription().moduleLabel() == moduleLabel + "@test" + std::to_string(mode));
0098 
0099       // Check grandparent as well
0100       parentProductProvenance = parentProvenance.productProvenance();
0101       assert(parentProductProvenance != nullptr);
0102     }
0103     auto const& grandParent = parentProductProvenance ? parentProductProvenance->parentage() : parent;
0104     //auto const& grandParent = parentProductProvenance->parentage();
0105     assert(grandParent.parents().size() == 1);  // behaviour of the AddIntsProducer
0106     auto const& grandParentProvenance = iEvent.getProvenance(grandParent.parents()[0]);
0107     int postfix = mode;
0108     if (aliasMode_ and mode == 2) {
0109       postfix = 3;
0110     }
0111     assert(grandParentProvenance.branchDescription().moduleLabel() == producerPrefix_ + std::to_string(postfix));
0112   }
0113 }  // namespace edmtest
0114 using edmtest::SwitchProducerProvenanceAnalyzer;
0115 DEFINE_FWK_MODULE(SwitchProducerProvenanceAnalyzer);