Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-06 02:54:10

0001 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/GoldenPatternWithStat.h"
0002 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFConfiguration.h"
0003 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFReconstruction.h"
0004 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/ProcessorBase.h"
0005 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/XMLConfigReader.h"
0006 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/XMLEventWriter.h"
0007 #include "L1Trigger/L1TMuonOverlapPhase1/interface/ProcConfigurationBase.h"
0008 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/CandidateSimMuonMatcher.h"
0009 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/DataROOTDumper2.h"
0010 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/EventCapture.h"
0011 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/PatternGenerator.h"
0012 
0013 #include "CondFormats/DataRecord/interface/L1TMuonOverlapParamsRcd.h"
0014 #include "CondFormats/L1TObjects/interface/L1TMuonOverlapParams.h"
0015 #include "DataFormats/L1Trigger/interface/BXVector.h"
0016 #include "DataFormats/Provenance/interface/EventID.h"
0017 #include "FWCore/Common/interface/EventBase.h"
0018 #include "FWCore/Framework/interface/EventSetupRecord.h"
0019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0020 #include "FWCore/ParameterSet/interface/FileInPath.h"
0021 #include "FWCore/Utilities/interface/Exception.h"
0022 
0023 #include <iostream>
0024 #include <memory>
0025 #include <string>
0026 #include <vector>
0027 /////////////////////////////////////////////////////
0028 /////////////////////////////////////////////////////
0029 OMTFReconstruction::OMTFReconstruction(const edm::ParameterSet& parameterSet, MuStubsInputTokens& muStubsInputTokens)
0030     : edmParameterSet(parameterSet),
0031       muStubsInputTokens(muStubsInputTokens),
0032       omtfConfig(new OMTFConfiguration()),
0033       omtfProc(nullptr),
0034       m_OMTFConfigMaker(nullptr) {
0035   bxMin = edmParameterSet.exists("bxMin") ? edmParameterSet.getParameter<int>("bxMin") : 0;
0036   bxMax = edmParameterSet.exists("bxMax") ? edmParameterSet.getParameter<int>("bxMax") : 0;
0037 
0038   edm::LogVerbatim("OMTFReconstruction") << "running emulation for the bxMin " << bxMin << " - bxMax " << bxMax
0039                                          << std::endl;
0040 }
0041 /////////////////////////////////////////////////////
0042 /////////////////////////////////////////////////////
0043 OMTFReconstruction::~OMTFReconstruction() {}
0044 
0045 /////////////////////////////////////////////////////
0046 /////////////////////////////////////////////////////
0047 void OMTFReconstruction::beginJob() {
0048   inputMaker = std::make_unique<OMTFinputMaker>(
0049       edmParameterSet, muStubsInputTokens, omtfConfig.get(), std::make_unique<OmtfAngleConverter>());
0050 }
0051 /////////////////////////////////////////////////////
0052 /////////////////////////////////////////////////////
0053 void OMTFReconstruction::endJob() {
0054   for (auto& obs : observers) {
0055     obs->endJob();
0056   }
0057 }
0058 
0059 /////////////////////////////////////////////////////
0060 /////////////////////////////////////////////////////
0061 void OMTFReconstruction::beginRun(edm::Run const& run,
0062                                   edm::EventSetup const& eventSetup,
0063                                   edm::ESGetToken<L1TMuonOverlapParams, L1TMuonOverlapParamsRcd>& omtfParamsEsToken,
0064                                   const MuonGeometryTokens& muonGeometryTokens,
0065                                   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord>& magneticFieldEsToken,
0066                                   const edm::ESGetToken<Propagator, TrackingComponentsRecord>& propagatorEsToken) {
0067   const L1TMuonOverlapParams* omtfParams = nullptr;
0068 
0069   std::string processorType = "OMTFProcessor";  //GoldenPatternWithStat GoldenPattern
0070   if (edmParameterSet.exists("processorType")) {
0071     processorType = edmParameterSet.getParameter<std::string>("processorType");
0072   }
0073 
0074   bool buildPatternsFromXml = (edmParameterSet.exists("patternsXMLFile") || edmParameterSet.exists("patternsXMLFiles"));
0075 
0076   edm::LogVerbatim("OMTFReconstruction") << "OMTFReconstruction::beginRun " << run.id()
0077                                          << " buildPatternsFromXml: " << buildPatternsFromXml << std::endl;
0078 
0079   //if the buildPatternsFromXml == false - we are making the omtfConfig and omtfProc for every run,
0080   //as the configuration my change between the runs,
0081   //if buildPatternsFromXml == true - we assume the the entire configuration comes from phyton,
0082   //so we do it only for the first run
0083   if (omtfProc == nullptr || buildPatternsFromXml == false) {
0084     if (omtfParamsRecordWatcher.check(eventSetup)) {
0085       edm::LogVerbatim("OMTFReconstruction") << "retrieving omtfParams from EventSetup" << std::endl;
0086 
0087       omtfParams = &(eventSetup.getData(omtfParamsEsToken));
0088       if (!omtfParams) {
0089         edm::LogError("OMTFReconstruction") << "Could not retrieve parameters from Event Setup" << std::endl;
0090       }
0091       omtfConfig->configure(omtfParams);
0092 
0093       //the parameters can be overwritten from the python config
0094       omtfConfig->configureFromEdmParameterSet(edmParameterSet);
0095 
0096       inputMaker->initialize(edmParameterSet, eventSetup, muonGeometryTokens);
0097 
0098       //patterns from the edm::EventSetup are reloaded every beginRun
0099       if (buildPatternsFromXml == false) {
0100         edm::LogVerbatim("OMTFReconstruction") << "getting patterns from EventSetup" << std::endl;
0101         if (processorType == "OMTFProcessor") {
0102           omtfProc = std::make_unique<OMTFProcessor<GoldenPattern> >(
0103               omtfConfig.get(), edmParameterSet, eventSetup, omtfParams);
0104           omtfProc->printInfo();
0105         }
0106       }
0107     }
0108   }
0109 
0110   //if we read the patterns directly from the xml, we do it only once, at the beginning of the first run, not every run
0111   if (omtfProc == nullptr && buildPatternsFromXml) {
0112     std::vector<std::string> patternsXMLFiles;
0113 
0114     if (edmParameterSet.exists("patternsXMLFile")) {
0115       patternsXMLFiles.push_back(edmParameterSet.getParameter<edm::FileInPath>("patternsXMLFile").fullPath());
0116     } else if (edmParameterSet.exists("patternsXMLFiles")) {
0117       for (const auto& it : edmParameterSet.getParameter<std::vector<edm::ParameterSet> >("patternsXMLFiles")) {
0118         patternsXMLFiles.push_back(it.getParameter<edm::FileInPath>("patternsXMLFile").fullPath());
0119       }
0120     }
0121 
0122     for (auto& patternsXMLFile : patternsXMLFiles)
0123       edm::LogVerbatim("OMTFReconstruction") << "reading patterns from " << patternsXMLFile << std::endl;
0124 
0125     XMLConfigReader xmlReader;
0126 
0127     std::string patternType = "GoldenPattern";  //GoldenPatternWithStat GoldenPattern
0128     if (edmParameterSet.exists("patternType")) {
0129       patternType = edmParameterSet.getParameter<std::string>("patternType");
0130     }
0131 
0132     if (patternType == "GoldenPattern") {
0133       if (processorType == "OMTFProcessor") {
0134         if (omtfParams) {
0135           omtfProc = std::make_unique<OMTFProcessor<GoldenPattern> >(
0136               omtfConfig.get(),
0137               edmParameterSet,
0138               eventSetup,
0139               xmlReader.readPatterns<GoldenPattern>(*omtfParams, patternsXMLFiles, false));
0140         } else {  //in principle should not happen
0141           throw cms::Exception("OMTFReconstruction::beginRun: omtfParams is nullptr");
0142         }
0143       }
0144 
0145       edm::LogVerbatim("OMTFReconstruction") << "OMTFProcessor constructed. processorType " << processorType
0146                                              << ". GoldenPattern type: " << patternType << std::endl;
0147     } else if (patternType == "GoldenPatternWithStat") {
0148       //pattern generation is only possible if the processor is constructed only once per job
0149       //PatternGenerator modifies the patterns!!!
0150       if (processorType == "OMTFProcessor") {
0151         if (omtfParams) {
0152           omtfProc = std::make_unique<OMTFProcessor<GoldenPatternWithStat> >(
0153               omtfConfig.get(),
0154               edmParameterSet,
0155               eventSetup,
0156               xmlReader.readPatterns<GoldenPatternWithStat>(*omtfParams, patternsXMLFiles, false));
0157         } else {  //in principle should not happen
0158           throw cms::Exception("OMTFReconstruction::beginRun: omtfParams is nullptr");
0159         }
0160         auto omtfProcGoldenPat = dynamic_cast<OMTFProcessor<GoldenPatternWithStat>*>(omtfProc.get());
0161 
0162         if (edmParameterSet.exists("generatePatterns") && edmParameterSet.getParameter<bool>("generatePatterns")) {
0163           observers.emplace_back(
0164               std::make_unique<PatternGenerator>(edmParameterSet, omtfConfig.get(), omtfProcGoldenPat->getPatterns()));
0165           edm::LogVerbatim("OMTFReconstruction") << "generatePatterns: true " << std::endl;
0166         }
0167       }
0168     } else {
0169       throw cms::Exception("OMTFReconstruction::beginRun: unknown GoldenPattern type: " + patternType);
0170     }
0171 
0172     omtfProc->printInfo();
0173   }
0174 
0175   addObservers(muonGeometryTokens, magneticFieldEsToken, propagatorEsToken);
0176 
0177   for (auto& obs : observers) {
0178     obs->beginRun(eventSetup);
0179   }
0180 }
0181 
0182 void OMTFReconstruction::addObservers(
0183     const MuonGeometryTokens& muonGeometryTokens,
0184     const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord>& magneticFieldEsToken,
0185     const edm::ESGetToken<Propagator, TrackingComponentsRecord>& propagatorEsToken) {
0186   if (!observers.empty())  //assuring it is done only at the first run
0187     return;
0188 
0189   edm::LogVerbatim("OMTFReconstruction") << "OMTFReconstruction::addObservers " << std::endl;
0190 
0191   //omtfConfig is created at constructor, and is not re-created at the the start of the run, so this is OK
0192   if (edmParameterSet.exists("dumpResultToXML")) {
0193     if (edmParameterSet.getParameter<bool>("dumpResultToXML"))
0194       observers.emplace_back(std::make_unique<XMLEventWriter>(
0195           omtfConfig.get(), edmParameterSet.getParameter<std::string>("XMLDumpFileName")));
0196   }
0197 
0198   CandidateSimMuonMatcher* candidateSimMuonMatcher = nullptr;
0199 
0200   if (edmParameterSet.exists("candidateSimMuonMatcher")) {
0201     if (edmParameterSet.getParameter<bool>("candidateSimMuonMatcher")) {
0202       observers.emplace_back(std::make_unique<CandidateSimMuonMatcher>(
0203           edmParameterSet, omtfConfig.get(), magneticFieldEsToken, propagatorEsToken));
0204       candidateSimMuonMatcher = static_cast<CandidateSimMuonMatcher*>(observers.back().get());
0205     }
0206   }
0207 
0208   auto omtfProcGoldenPat = dynamic_cast<OMTFProcessor<GoldenPattern>*>(omtfProc.get());
0209   if (omtfProcGoldenPat) {
0210     if (edmParameterSet.exists("eventCaptureDebug"))
0211       if (edmParameterSet.getParameter<bool>("eventCaptureDebug")) {
0212         observers.emplace_back(std::make_unique<EventCapture>(
0213             edmParameterSet, omtfConfig.get(), candidateSimMuonMatcher, muonGeometryTokens
0214             //&(omtfProcGoldenPat->getPatterns() ),
0215             //watch out, will crash if the proc is re-constructed from the DB after L1TMuonOverlapParamsRcd change
0216             ));
0217       }
0218 
0219     if (edmParameterSet.exists("dumpHitsToROOT") && edmParameterSet.getParameter<bool>("dumpHitsToROOT")) {
0220       std::string rootFileName = edmParameterSet.getParameter<std::string>("dumpHitsFileName");
0221       observers.emplace_back(std::make_unique<DataROOTDumper2>(edmParameterSet, omtfConfig.get(), rootFileName));
0222     }
0223   }
0224 }
0225 /////////////////////////////////////////////////////
0226 /////////////////////////////////////////////////////
0227 std::unique_ptr<l1t::RegionalMuonCandBxCollection> OMTFReconstruction::reconstruct(const edm::Event& iEvent,
0228                                                                                    const edm::EventSetup& evSetup) {
0229   LogTrace("l1tOmtfEventPrint") << "\n" << __FUNCTION__ << ":" << __LINE__ << " iEvent " << iEvent.id().event() << endl;
0230   inputMaker->loadAndFilterDigis(iEvent);
0231 
0232   for (auto& obs : observers) {
0233     obs->observeEventBegin(iEvent);
0234   }
0235 
0236   std::unique_ptr<l1t::RegionalMuonCandBxCollection> candidates = std::make_unique<l1t::RegionalMuonCandBxCollection>();
0237   candidates->setBXRange(bxMin, bxMax);
0238 
0239   ///The order is important: first put omtf_pos candidates, then omtf_neg.
0240   for (int bx = bxMin; bx <= bxMax; bx++) {
0241     for (unsigned int iProcessor = 0; iProcessor < omtfConfig->nProcessors(); ++iProcessor) {
0242       std::vector<l1t::RegionalMuonCand> candMuons =
0243           omtfProc->run(iProcessor, l1t::tftype::omtf_pos, bx, inputMaker.get(), observers);
0244 
0245       //fill outgoing collection
0246       for (auto& candMuon : candMuons) {
0247         candidates->push_back(bx, candMuon);
0248       }
0249     }
0250 
0251     for (unsigned int iProcessor = 0; iProcessor < omtfConfig->nProcessors(); ++iProcessor) {
0252       std::vector<l1t::RegionalMuonCand> candMuons =
0253           omtfProc->run(iProcessor, l1t::tftype::omtf_neg, bx, inputMaker.get(), observers);
0254 
0255       //fill outgoing collection
0256       for (auto& candMuon : candMuons) {
0257         candidates->push_back(bx, candMuon);
0258       }
0259     }
0260 
0261     //edm::LogInfo("OMTFReconstruction") <<"OMTF:  Number of candidates in BX="<<bx<<": "<<candidates->size(bx) << std::endl;;
0262   }
0263 
0264   for (auto& obs : observers) {
0265     obs->observeEventEnd(iEvent, candidates);
0266   }
0267 
0268   return candidates;
0269 }
0270 
0271 /////////////////////////////////////////////////////
0272 /////////////////////////////////////////////////////