Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/L1TGlobal
0004 // Class:      TestReadGlobalObjectMapRecord
0005 //
0006 /**\class edmtest::TestReadGlobalObjectMapRecord
0007   Description: Used as part of tests that ensure the GlobalObjectMapRecord
0008   data format can be persistently written and in a subsequent process
0009   read. First, this is done using the current release version for writing
0010   and reading. In addition, the output file of the write process should
0011   be saved permanently each time the GlobalObjectMapRecord persistent data
0012   format changes. In unit tests, we read each of those saved files to verify
0013   that the current releases can read older versions of the data format.
0014 */
0015 // Original Author:  W. David Dagenhart
0016 //         Created:  3 May 2023
0017 
0018 #include "DataFormats/L1TGlobal/interface/GlobalObject.h"
0019 #include "DataFormats/L1TGlobal/interface/GlobalObjectMapFwd.h"
0020 #include "DataFormats/L1TGlobal/interface/GlobalObjectMapRecord.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0024 #include "FWCore/Framework/interface/MakerMacros.h"
0025 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0028 #include "FWCore/Utilities/interface/EDGetToken.h"
0029 #include "FWCore/Utilities/interface/Exception.h"
0030 #include "FWCore/Utilities/interface/InputTag.h"
0031 #include "FWCore/Utilities/interface/StreamID.h"
0032 
0033 #include <string>
0034 #include <vector>
0035 
0036 namespace edmtest {
0037 
0038   class TestReadGlobalObjectMapRecord : public edm::global::EDAnalyzer<> {
0039   public:
0040     TestReadGlobalObjectMapRecord(edm::ParameterSet const&);
0041     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0042     void throwWithMessage(const char*) const;
0043     static void fillDescriptions(edm::ConfigurationDescriptions&);
0044 
0045   private:
0046     // These expected values are meaningless other than we use them
0047     // to check that values read from persistent storage match the values
0048     // we know were written.
0049     std::vector<std::string> expectedAlgoNames_;
0050     std::vector<int> expectedAlgoBitNumbers_;
0051     std::vector<int> expectedAlgoGtlResults_;
0052     std::vector<std::string> expectedTokenNames0_;
0053     std::vector<int> expectedTokenNumbers0_;
0054     std::vector<int> expectedTokenResults0_;
0055     std::vector<std::string> expectedTokenNames3_;
0056     std::vector<int> expectedTokenNumbers3_;
0057     std::vector<int> expectedTokenResults3_;
0058     int expectedFirstElement_;
0059     int expectedElementDelta_;
0060     int expectedFinalValue_;
0061 
0062     edm::EDGetTokenT<GlobalObjectMapRecord> globalObjectMapRecordToken_;
0063   };
0064 
0065   TestReadGlobalObjectMapRecord::TestReadGlobalObjectMapRecord(edm::ParameterSet const& iPSet)
0066       : expectedAlgoNames_(iPSet.getParameter<std::vector<std::string>>("expectedAlgoNames")),
0067         expectedAlgoBitNumbers_(iPSet.getParameter<std::vector<int>>("expectedAlgoBitNumbers")),
0068         expectedAlgoGtlResults_(iPSet.getParameter<std::vector<int>>("expectedAlgoGtlResults")),
0069         expectedTokenNames0_(iPSet.getParameter<std::vector<std::string>>("expectedTokenNames0")),
0070         expectedTokenNumbers0_(iPSet.getParameter<std::vector<int>>("expectedTokenNumbers0")),
0071         expectedTokenResults0_(iPSet.getParameter<std::vector<int>>("expectedTokenResults0")),
0072         expectedTokenNames3_(iPSet.getParameter<std::vector<std::string>>("expectedTokenNames3")),
0073         expectedTokenNumbers3_(iPSet.getParameter<std::vector<int>>("expectedTokenNumbers3")),
0074         expectedTokenResults3_(iPSet.getParameter<std::vector<int>>("expectedTokenResults3")),
0075         expectedFirstElement_(iPSet.getParameter<int>("expectedFirstElement")),
0076         expectedElementDelta_(iPSet.getParameter<int>("expectedElementDelta")),
0077         expectedFinalValue_(iPSet.getParameter<int>("expectedFinalValue")),
0078 
0079         globalObjectMapRecordToken_(consumes(iPSet.getParameter<edm::InputTag>("globalObjectMapRecordTag"))) {
0080     if (expectedAlgoNames_.size() != expectedAlgoBitNumbers_.size() ||
0081         expectedAlgoNames_.size() != expectedAlgoGtlResults_.size() ||
0082         expectedAlgoNames_.size() != expectedTokenNames0_.size() ||
0083         expectedAlgoNames_.size() != expectedTokenNumbers0_.size() ||
0084         expectedAlgoNames_.size() != expectedTokenResults0_.size() ||
0085         expectedAlgoNames_.size() != expectedTokenNames3_.size() ||
0086         expectedAlgoNames_.size() != expectedTokenNumbers3_.size() ||
0087         expectedAlgoNames_.size() != expectedTokenResults3_.size()) {
0088       throw cms::Exception("TestFailure") << "TestWriteGlobalObjectMapRecord, test configuration error: "
0089                                              "expectedAlgoNames, expectedAlgoBitNumbers, expectedAlgoGtlResults, "
0090                                              "expectedTokenNames0_, expectedTokenNumbers0_, expectedTokenResults0_, "
0091                                              "expectedTokenNames3_, expectedTokenNumbers3_, and expectedTokenResults3_ "
0092                                              "should have the same size.";
0093     }
0094   }
0095 
0096   void TestReadGlobalObjectMapRecord::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0097     auto const& globalObjectMapRecord = iEvent.get(globalObjectMapRecordToken_);
0098 
0099     std::vector<GlobalObjectMap> const& globalObjectMaps = globalObjectMapRecord.gtObjectMap();
0100 
0101     if (expectedAlgoNames_.size() != globalObjectMaps.size()) {
0102       throwWithMessage("globalObjectMaps does not have expected size");
0103     }
0104 
0105     unsigned int index = 0;
0106     for (auto const& globalObjectMap : globalObjectMaps) {
0107       if (globalObjectMap.algoName() != expectedAlgoNames_[index]) {
0108         throwWithMessage("algoName does not have expected value");
0109       }
0110       if (globalObjectMap.algoBitNumber() != expectedAlgoBitNumbers_[index]) {
0111         throwWithMessage("algoBitNumber does not have expected value");
0112       }
0113       if (globalObjectMap.algoGtlResult() != static_cast<bool>(expectedAlgoGtlResults_[index])) {
0114         throwWithMessage("algoGtlResult does not have expected value");
0115       }
0116 
0117       std::vector<GlobalLogicParser::OperandToken> const& operandTokens = globalObjectMap.operandTokenVector();
0118       if (operandTokens[0].tokenName != expectedTokenNames0_[index]) {
0119         throwWithMessage("tokenName0 does not have expected value");
0120       }
0121       if (operandTokens[0].tokenNumber != expectedTokenNumbers0_[index]) {
0122         throwWithMessage("tokenNumber0 does not have expected value");
0123       }
0124       if (operandTokens[0].tokenResult != static_cast<bool>(expectedTokenResults0_[index])) {
0125         throwWithMessage("tokenResult0 does not have expected value");
0126       }
0127       if (operandTokens[3].tokenName != expectedTokenNames3_[index]) {
0128         throwWithMessage("tokenName3 does not have expected value");
0129       }
0130       if (operandTokens[3].tokenNumber != expectedTokenNumbers3_[index]) {
0131         throwWithMessage("tokenNumber3 does not have expected value");
0132       }
0133       if (operandTokens[3].tokenResult != static_cast<bool>(expectedTokenResults3_[index])) {
0134         throwWithMessage("tokenResult3 does not have expected value");
0135       }
0136 
0137       int expectedValue = expectedFirstElement_;
0138       for (auto const& combinationsInCond : globalObjectMap.combinationVector()) {
0139         for (auto const& singleCombInCond : combinationsInCond) {
0140           for (auto const& value : singleCombInCond) {
0141             if (value != expectedValue) {
0142               throwWithMessage("element in inner combination vector does have expected value");
0143             }
0144             expectedValue += expectedElementDelta_;
0145           }
0146         }
0147       }
0148 
0149       for (auto const& l1tObjectTypeInCond : globalObjectMap.objectTypeVector()) {
0150         for (auto const& globalObject : l1tObjectTypeInCond) {
0151           if (static_cast<int>(globalObject) != (expectedValue % 28)) {
0152             throwWithMessage("globalObject does have expected value");
0153           }
0154           expectedValue += expectedElementDelta_;
0155         }
0156       }
0157       if (expectedValue != expectedFinalValue_) {
0158         throw cms::Exception("TestFailure")
0159             << "final value = " << expectedValue << " which does not match the expected value of "
0160             << expectedFinalValue_ << " this might mean the vectors did not contain the expected number of elements";
0161       }
0162       ++index;
0163     }
0164   }
0165 
0166   void TestReadGlobalObjectMapRecord::throwWithMessage(const char* msg) const {
0167     throw cms::Exception("TestFailure") << "TestReadGlobalObjectMapRecord::analyze, " << msg;
0168   }
0169 
0170   void TestReadGlobalObjectMapRecord::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0171     edm::ParameterSetDescription desc;
0172     desc.add<std::vector<std::string>>("expectedAlgoNames");
0173     desc.add<std::vector<int>>("expectedAlgoBitNumbers");
0174     desc.add<std::vector<int>>("expectedAlgoGtlResults");
0175     desc.add<std::vector<std::string>>("expectedTokenNames0");
0176     desc.add<std::vector<int>>("expectedTokenNumbers0");
0177     desc.add<std::vector<int>>("expectedTokenResults0");
0178     desc.add<std::vector<std::string>>("expectedTokenNames3");
0179     desc.add<std::vector<int>>("expectedTokenNumbers3");
0180     desc.add<std::vector<int>>("expectedTokenResults3");
0181     desc.add<int>("expectedFirstElement");
0182     desc.add<int>("expectedElementDelta");
0183     desc.add<int>("expectedFinalValue");
0184     desc.add<edm::InputTag>("globalObjectMapRecordTag");
0185     descriptions.addDefault(desc);
0186   }
0187 }  // namespace edmtest
0188 
0189 using edmtest::TestReadGlobalObjectMapRecord;
0190 DEFINE_FWK_MODULE(TestReadGlobalObjectMapRecord);