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:      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 
0060     edm::EDPutTokenT<GlobalObjectMapRecord> globalObjectMapRecordPutToken_;
0061   };
0062 
0063   TestWriteGlobalObjectMapRecord::TestWriteGlobalObjectMapRecord(edm::ParameterSet const& iPSet)
0064       : nGlobalObjectMaps_(iPSet.getParameter<unsigned int>("nGlobalObjectMaps")),
0065         algoNames_(iPSet.getParameter<std::vector<std::string>>("algoNames")),
0066         algoBitNumbers_(iPSet.getParameter<std::vector<int>>("algoBitNumbers")),
0067         algoResults_(iPSet.getParameter<std::vector<int>>("algoResults")),
0068         tokenNames0_(iPSet.getParameter<std::vector<std::string>>("tokenNames0")),
0069         tokenNumbers0_(iPSet.getParameter<std::vector<int>>("tokenNumbers0")),
0070         tokenResults0_(iPSet.getParameter<std::vector<int>>("tokenResults0")),
0071         tokenNames3_(iPSet.getParameter<std::vector<std::string>>("tokenNames3")),
0072         tokenNumbers3_(iPSet.getParameter<std::vector<int>>("tokenNumbers3")),
0073         tokenResults3_(iPSet.getParameter<std::vector<int>>("tokenResults3")),
0074         nElements1_(iPSet.getParameter<unsigned int>("nElements1")),
0075         nElements2_(iPSet.getParameter<unsigned int>("nElements2")),
0076         nElements3_(iPSet.getParameter<unsigned int>("nElements3")),
0077         firstElement_(iPSet.getParameter<int>("firstElement")),
0078         elementDelta_(iPSet.getParameter<int>("elementDelta")),
0079         globalObjectMapRecordPutToken_(produces()) {
0080     if (algoNames_.size() != nGlobalObjectMaps_ || algoBitNumbers_.size() != nGlobalObjectMaps_ ||
0081         algoResults_.size() != nGlobalObjectMaps_ || tokenNames0_.size() != nGlobalObjectMaps_ ||
0082         tokenNumbers0_.size() != nGlobalObjectMaps_ || tokenResults0_.size() != nGlobalObjectMaps_ ||
0083         tokenNames3_.size() != nGlobalObjectMaps_ || tokenNumbers3_.size() != nGlobalObjectMaps_ ||
0084         tokenResults3_.size() != nGlobalObjectMaps_) {
0085       throw cms::Exception("TestFailure")
0086           << "TestWriteGlobalObjectMapRecord, test configuration error: "
0087              "algoNames, algoBitNumbers, algoResults, tokenNames0, tokenNumbers0, tokenResults0, "
0088              "tokenNames3, tokenNumbers3, and tokenResults3 should have size equal to nGlobalObjectMaps";
0089     }
0090   }
0091 
0092   void TestWriteGlobalObjectMapRecord::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0093     // Fill a GlobalObjectMapRecord. Make sure all the containers inside
0094     // of it have something in them (not empty). The values are meaningless.
0095     // We will later check that after writing this object to persistent storage
0096     // and then reading it in a later process we obtain matching values for
0097     // all this content.
0098 
0099     std::vector<GlobalObjectMap> globalObjectMapVector(nGlobalObjectMaps_);
0100     for (unsigned int i = 0; i < nGlobalObjectMaps_; ++i) {
0101       GlobalObjectMap& globalObjectMap = globalObjectMapVector[i];
0102       globalObjectMap.setAlgoName(algoNames_[i]);
0103       globalObjectMap.setAlgoBitNumber(algoBitNumbers_[i]);
0104       globalObjectMap.setAlgoGtlResult(static_cast<bool>(algoResults_[i]));
0105 
0106       std::vector<GlobalLogicParser::OperandToken> tokenVector;
0107       // We will later check elements 0 and 3 after writing to persistent
0108       // storage and then reading (seemed like checking two elements
0109       // would be enough to verify the reading and writing was working properly,
0110       // the selection of 0 and 3 was meaningless and rather arbitrary)
0111       GlobalLogicParser::OperandToken token0{tokenNames0_[i], tokenNumbers0_[i], static_cast<bool>(tokenResults0_[i])};
0112       GlobalLogicParser::OperandToken token3{tokenNames3_[i], tokenNumbers3_[i], static_cast<bool>(tokenResults3_[i])};
0113       tokenVector.push_back(token0);  // We check this element
0114       tokenVector.push_back(token0);
0115       tokenVector.push_back(token0);
0116       tokenVector.push_back(token3);  // we also check this element
0117       globalObjectMap.swapOperandTokenVector(tokenVector);
0118 
0119       // We fill these with an arithmetic sequence of values.
0120       // Again, this is just an arbitrary meaningless test pattern.
0121       // The only purpose is to later check that when
0122       // we read we get values that match what we wrote.
0123       int value = firstElement_;
0124       std::vector<CombinationsInCond> combinationsInCondVector;
0125       for (unsigned int i = 0; i < nElements1_; ++i) {
0126         CombinationsInCond combinationsInCond;
0127         for (unsigned int j = 0; j < nElements2_; ++j) {
0128           SingleCombInCond singleCombInCond;
0129           for (unsigned int k = 0; k < nElements3_; ++k) {
0130             singleCombInCond.push_back(value);
0131             value += elementDelta_;
0132           }
0133           combinationsInCond.push_back(std::move(singleCombInCond));
0134         }
0135         combinationsInCondVector.push_back(combinationsInCond);
0136       }
0137       globalObjectMap.swapCombinationVector(combinationsInCondVector);
0138 
0139       std::vector<L1TObjectTypeInCond> objectTypeVector;
0140       for (unsigned int i = 0; i < nElements1_; ++i) {
0141         L1TObjectTypeInCond globalObjects;
0142         for (unsigned int j = 0; j < nElements2_; ++j) {
0143           globalObjects.push_back(static_cast<l1t::GlobalObject>(value % 28));
0144           value += elementDelta_;
0145         }
0146         objectTypeVector.push_back(std::move(globalObjects));
0147       }
0148       globalObjectMap.swapObjectTypeVector(objectTypeVector);
0149     }
0150 
0151     auto globalObjectMapRecord = std::make_unique<GlobalObjectMapRecord>();
0152     globalObjectMapRecord->swapGtObjectMap(globalObjectMapVector);
0153     iEvent.put(globalObjectMapRecordPutToken_, std::move(globalObjectMapRecord));
0154   }
0155 
0156   void TestWriteGlobalObjectMapRecord::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0157     edm::ParameterSetDescription desc;
0158     desc.add<unsigned int>("nGlobalObjectMaps");
0159     desc.add<std::vector<std::string>>("algoNames");
0160     desc.add<std::vector<int>>("algoBitNumbers");
0161     desc.add<std::vector<int>>("algoResults");
0162 
0163     desc.add<std::vector<std::string>>("tokenNames0");
0164     desc.add<std::vector<int>>("tokenNumbers0");
0165     desc.add<std::vector<int>>("tokenResults0");
0166     desc.add<std::vector<std::string>>("tokenNames3");
0167     desc.add<std::vector<int>>("tokenNumbers3");
0168     desc.add<std::vector<int>>("tokenResults3");
0169 
0170     desc.add<unsigned int>("nElements1");
0171     desc.add<unsigned int>("nElements2");
0172     desc.add<unsigned int>("nElements3");
0173     desc.add<int>("firstElement");
0174     desc.add<int>("elementDelta");
0175 
0176     descriptions.addDefault(desc);
0177   }
0178 }  // namespace edmtest
0179 
0180 using edmtest::TestWriteGlobalObjectMapRecord;
0181 DEFINE_FWK_MODULE(TestWriteGlobalObjectMapRecord);