Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-14 23:16:43

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/L1TGlobal
0004 // Class:      TestWriteGlobalObjectMapRecord
0005 //
0006 /**\class edmtest::TestWriteGlobalObjectMapRecord
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/GlobalObjectMap.h"
0019 #include "DataFormats/L1TGlobal/interface/GlobalObjectMapRecord.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/Frameworkfwd.h"
0022 #include "FWCore/Framework/interface/global/EDProducer.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0027 #include "FWCore/Utilities/interface/EDPutToken.h"
0028 #include "FWCore/Utilities/interface/StreamID.h"
0029 
0030 #include <memory>
0031 #include <string>
0032 #include <utility>
0033 #include <vector>
0034 
0035 namespace edmtest {
0036 
0037   class TestWriteGlobalObjectMapRecord : public edm::global::EDProducer<> {
0038   public:
0039     TestWriteGlobalObjectMapRecord(edm::ParameterSet const&);
0040     void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0041     static void fillDescriptions(edm::ConfigurationDescriptions&);
0042 
0043   private:
0044     unsigned int nGlobalObjectMaps_;
0045     std::vector<std::string> algoNames_;
0046     std::vector<int> algoBitNumbers_;
0047     std::vector<int> algoResults_;
0048     std::vector<std::string> tokenNames0_;
0049     std::vector<int> tokenNumbers0_;
0050     std::vector<int> tokenResults0_;
0051     std::vector<std::string> tokenNames3_;
0052     std::vector<int> tokenNumbers3_;
0053     std::vector<int> tokenResults3_;
0054     unsigned int nElements1_;
0055     unsigned int nElements2_;
0056     unsigned int nElements3_;
0057     int firstElement_;
0058     int elementDelta_;
0059     uint bxIndexModulus_;
0060 
0061     edm::EDPutTokenT<GlobalObjectMapRecord> globalObjectMapRecordPutToken_;
0062   };
0063 
0064   TestWriteGlobalObjectMapRecord::TestWriteGlobalObjectMapRecord(edm::ParameterSet const& iPSet)
0065       : nGlobalObjectMaps_(iPSet.getParameter<unsigned int>("nGlobalObjectMaps")),
0066         algoNames_(iPSet.getParameter<std::vector<std::string>>("algoNames")),
0067         algoBitNumbers_(iPSet.getParameter<std::vector<int>>("algoBitNumbers")),
0068         algoResults_(iPSet.getParameter<std::vector<int>>("algoResults")),
0069         tokenNames0_(iPSet.getParameter<std::vector<std::string>>("tokenNames0")),
0070         tokenNumbers0_(iPSet.getParameter<std::vector<int>>("tokenNumbers0")),
0071         tokenResults0_(iPSet.getParameter<std::vector<int>>("tokenResults0")),
0072         tokenNames3_(iPSet.getParameter<std::vector<std::string>>("tokenNames3")),
0073         tokenNumbers3_(iPSet.getParameter<std::vector<int>>("tokenNumbers3")),
0074         tokenResults3_(iPSet.getParameter<std::vector<int>>("tokenResults3")),
0075         nElements1_(iPSet.getParameter<unsigned int>("nElements1")),
0076         nElements2_(iPSet.getParameter<unsigned int>("nElements2")),
0077         nElements3_(iPSet.getParameter<unsigned int>("nElements3")),
0078         firstElement_(iPSet.getParameter<int>("firstElement")),
0079         elementDelta_(iPSet.getParameter<int>("elementDelta")),
0080         bxIndexModulus_(iPSet.getParameter<uint>("bxIndexModulus")),
0081         globalObjectMapRecordPutToken_(produces()) {
0082     if (algoNames_.size() != nGlobalObjectMaps_ || algoBitNumbers_.size() != nGlobalObjectMaps_ ||
0083         algoResults_.size() != nGlobalObjectMaps_ || tokenNames0_.size() != nGlobalObjectMaps_ ||
0084         tokenNumbers0_.size() != nGlobalObjectMaps_ || tokenResults0_.size() != nGlobalObjectMaps_ ||
0085         tokenNames3_.size() != nGlobalObjectMaps_ || tokenNumbers3_.size() != nGlobalObjectMaps_ ||
0086         tokenResults3_.size() != nGlobalObjectMaps_) {
0087       throw cms::Exception("TestFailure")
0088           << "TestWriteGlobalObjectMapRecord, test configuration error: "
0089              "algoNames, algoBitNumbers, algoResults, tokenNames0, tokenNumbers0, tokenResults0, "
0090              "tokenNames3, tokenNumbers3, and tokenResults3 should have size equal to nGlobalObjectMaps";
0091     }
0092   }
0093 
0094   void TestWriteGlobalObjectMapRecord::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0095     // Fill a GlobalObjectMapRecord. Make sure all the containers inside
0096     // of it have something in them (not empty). The values are meaningless.
0097     // We will later check that after writing this object to persistent storage
0098     // and then reading it in a later process we obtain matching values for
0099     // all this content.
0100 
0101     std::vector<GlobalObjectMap> globalObjectMapVector(nGlobalObjectMaps_);
0102     for (unsigned int i = 0; i < nGlobalObjectMaps_; ++i) {
0103       GlobalObjectMap& globalObjectMap = globalObjectMapVector[i];
0104       globalObjectMap.setAlgoName(algoNames_[i]);
0105       globalObjectMap.setAlgoBitNumber(algoBitNumbers_[i]);
0106       globalObjectMap.setAlgoGtlResult(static_cast<bool>(algoResults_[i]));
0107 
0108       std::vector<GlobalLogicParser::OperandToken> tokenVector;
0109       // We will later check elements 0 and 3 after writing to persistent
0110       // storage and then reading (seemed like checking two elements
0111       // would be enough to verify the reading and writing was working properly,
0112       // the selection of 0 and 3 was meaningless and rather arbitrary)
0113       GlobalLogicParser::OperandToken token0{tokenNames0_[i], tokenNumbers0_[i], static_cast<bool>(tokenResults0_[i])};
0114       GlobalLogicParser::OperandToken token3{tokenNames3_[i], tokenNumbers3_[i], static_cast<bool>(tokenResults3_[i])};
0115       tokenVector.push_back(token0);  // We check this element
0116       tokenVector.push_back(token0);
0117       tokenVector.push_back(token0);
0118       tokenVector.push_back(token3);  // we also check this element
0119       globalObjectMap.swapOperandTokenVector(tokenVector);
0120 
0121       // We fill these with an arithmetic sequence of values.
0122       // Again, this is just an arbitrary meaningless test pattern.
0123       // The only purpose is to later check that when
0124       // we read we get values that match what we wrote.
0125       int value = firstElement_;
0126       uint bxCounter = 0;
0127       std::vector<CombinationsWithBxInCond> combinationsInCondVector;
0128       combinationsInCondVector.reserve(nElements1_);
0129       for (unsigned int i = 0; i < nElements1_; ++i) {
0130         CombinationsWithBxInCond combinationsInCond;
0131         combinationsInCond.reserve(nElements2_);
0132         for (unsigned int j = 0; j < nElements2_; ++j) {
0133           SingleCombWithBxInCond singleCombInCond;
0134           singleCombInCond.reserve(nElements3_);
0135           for (unsigned int k = 0; k < nElements3_; ++k) {
0136             L1TObjBxIndexType const bxIdx = bxCounter % bxIndexModulus_;
0137             singleCombInCond.emplace_back(bxIdx, value);
0138             value += elementDelta_;
0139             ++bxCounter;
0140           }
0141           combinationsInCond.push_back(std::move(singleCombInCond));
0142         }
0143         combinationsInCondVector.push_back(combinationsInCond);
0144       }
0145       globalObjectMap.swapCombinationVector(combinationsInCondVector);
0146 
0147       std::vector<L1TObjectTypeInCond> objectTypeVector;
0148       for (unsigned int i = 0; i < nElements1_; ++i) {
0149         L1TObjectTypeInCond globalObjects;
0150         for (unsigned int j = 0; j < nElements2_; ++j) {
0151           globalObjects.push_back(static_cast<l1t::GlobalObject>(value % 28));
0152           value += elementDelta_;
0153         }
0154         objectTypeVector.push_back(std::move(globalObjects));
0155       }
0156       globalObjectMap.swapObjectTypeVector(objectTypeVector);
0157     }
0158 
0159     auto globalObjectMapRecord = std::make_unique<GlobalObjectMapRecord>();
0160     globalObjectMapRecord->swapGtObjectMap(globalObjectMapVector);
0161     iEvent.put(globalObjectMapRecordPutToken_, std::move(globalObjectMapRecord));
0162   }
0163 
0164   void TestWriteGlobalObjectMapRecord::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0165     edm::ParameterSetDescription desc;
0166     desc.add<unsigned int>("nGlobalObjectMaps");
0167     desc.add<std::vector<std::string>>("algoNames");
0168     desc.add<std::vector<int>>("algoBitNumbers");
0169     desc.add<std::vector<int>>("algoResults");
0170 
0171     desc.add<std::vector<std::string>>("tokenNames0");
0172     desc.add<std::vector<int>>("tokenNumbers0");
0173     desc.add<std::vector<int>>("tokenResults0");
0174     desc.add<std::vector<std::string>>("tokenNames3");
0175     desc.add<std::vector<int>>("tokenNumbers3");
0176     desc.add<std::vector<int>>("tokenResults3");
0177 
0178     desc.add<unsigned int>("nElements1");
0179     desc.add<unsigned int>("nElements2");
0180     desc.add<unsigned int>("nElements3");
0181     desc.add<int>("firstElement");
0182     desc.add<int>("elementDelta");
0183     desc.add<uint>("bxIndexModulus");
0184 
0185     descriptions.addDefault(desc);
0186   }
0187 }  // namespace edmtest
0188 
0189 using edmtest::TestWriteGlobalObjectMapRecord;
0190 DEFINE_FWK_MODULE(TestWriteGlobalObjectMapRecord);