Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  * EventCapture.cpp
0003  *
0004  *  Created on: Oct 23, 2019
0005  *      Author: kbunkow
0006  */
0007 
0008 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Tools/EventCapture.h"
0009 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OmtfName.h"
0010 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFinputMaker.h"
0011 
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0018 
0019 #include <memory>
0020 #include <sstream>
0021 
0022 EventCapture::EventCapture(const edm::ParameterSet& edmCfg,
0023                            const OMTFConfiguration* omtfConfig,
0024                            CandidateSimMuonMatcher* candidateSimMuonMatcher,
0025                            const MuonGeometryTokens& muonGeometryTokens,
0026                            const GoldenPatternVec<GoldenPattern>* gps)
0027     : omtfConfig(omtfConfig),
0028       goldenPatterns(gps),
0029       candidateSimMuonMatcher(candidateSimMuonMatcher),
0030       inputInProcs(omtfConfig->processorCnt()),
0031       algoMuonsInProcs(omtfConfig->processorCnt()),
0032       gbCandidatesInProcs(omtfConfig->processorCnt()) {
0033   //LogTrace("l1tOmtfEventPrint")<<__FUNCTION__<<":"<<__LINE__<<" omtfConfig->nProcessors() "<<omtfConfig->nProcessors()<<std::endl;
0034   if (edmCfg.exists("simTracksTag"))
0035     simTracksTag = edmCfg.getParameter<edm::InputTag>("simTracksTag");
0036   else
0037     edm::LogImportant("OMTFReconstruction")
0038         << "EventCapture::EventCapture: no InputTag simTracksTag found" << std::endl;
0039 
0040   if (this->candidateSimMuonMatcher)
0041     stubsSimHitsMatcher = std::make_unique<StubsSimHitsMatcher>(edmCfg, omtfConfig, muonGeometryTokens);
0042 }
0043 
0044 EventCapture::~EventCapture() {
0045   // TODO Auto-generated destructor stub
0046 }
0047 
0048 void EventCapture::beginRun(edm::EventSetup const& eventSetup) {
0049   if (stubsSimHitsMatcher)
0050     stubsSimHitsMatcher->beginRun(eventSetup);
0051 }
0052 
0053 void EventCapture::observeEventBegin(const edm::Event& event) {
0054   simMuons.clear();
0055 
0056   if (!simTracksTag.label().empty()) {
0057     edm::Handle<edm::SimTrackContainer> simTraksHandle;
0058     event.getByLabel(simTracksTag, simTraksHandle);
0059 
0060     for (unsigned int iSimTrack = 0; iSimTrack != simTraksHandle->size(); iSimTrack++) {
0061       if (abs((*simTraksHandle.product())[iSimTrack].type()) == 13)
0062         simMuons.emplace_back(simTraksHandle, iSimTrack);
0063     }
0064   }
0065 
0066   for (auto& input : inputInProcs)
0067     input.reset();
0068 
0069   for (auto& algoMuonsInProc : algoMuonsInProcs)
0070     algoMuonsInProc.clear();
0071 
0072   for (auto& gbCandidatesInProc : gbCandidatesInProcs)
0073     gbCandidatesInProc.clear();
0074 }
0075 
0076 void EventCapture::observeProcesorEmulation(unsigned int iProcessor,
0077                                             l1t::tftype mtfType,
0078                                             const std::shared_ptr<OMTFinput>& input,
0079                                             const AlgoMuons& algoCandidates,
0080                                             const AlgoMuons& gbCandidates,
0081                                             const std::vector<l1t::RegionalMuonCand>& candMuons) {
0082   unsigned int procIndx = omtfConfig->getProcIndx(iProcessor, mtfType);
0083 
0084   inputInProcs[procIndx] = input;
0085 
0086   algoMuonsInProcs[procIndx] = algoCandidates;
0087   gbCandidatesInProcs[procIndx] = gbCandidates;
0088 }
0089 
0090 void EventCapture::observeEventEnd(const edm::Event& iEvent,
0091                                    std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {
0092   std::ostringstream ostr;
0093   //filtering
0094 
0095   bool dump = false;
0096 
0097   if (candidateSimMuonMatcher) {
0098     std::vector<MatchingResult> matchingResults = candidateSimMuonMatcher->getMatchingResults();
0099     edm::LogVerbatim("l1tOmtfEventPrint") << "matchingResults.size() " << matchingResults.size() << std::endl;
0100 
0101     //candidateSimMuonMatcher should use the  trackingParticles, because the simTracks are not stored for the pile-up events
0102     for (auto& matchingResult : matchingResults) {
0103       //TODO choose a condition, to print the desired candidates
0104       if (matchingResult.muonCand && matchingResult.muonCand->hwQual() >= 12 &&
0105           matchingResult.muonCand->hwPt() > 38) {  //&& matchingResult.genPt < 20
0106         dump = true;
0107 
0108         bool runStubsSimHitsMatcher = false;
0109         if (matchingResult.trackingParticle) {
0110           auto trackingParticle = matchingResult.trackingParticle;
0111           ostr << "trackingParticle: eventId " << trackingParticle->eventId().event() << " pdgId " << std::setw(3)
0112                << trackingParticle->pdgId() << " trackId " << trackingParticle->g4Tracks().at(0).trackId() << " pt "
0113                << std::setw(9) << trackingParticle->pt()  //<<" Beta "<<simMuon->momentum().Beta()
0114                << " eta " << std::setw(9) << trackingParticle->momentum().eta() << " phi " << std::setw(9)
0115                << trackingParticle->momentum().phi() << std::endl;
0116         } else {
0117           ostr << "no simMuon ";
0118           runStubsSimHitsMatcher = true;
0119         }
0120         ostr << "matched to: " << std::endl;
0121         auto finalCandidate = matchingResult.muonCand;
0122         ostr << " hwPt " << finalCandidate->hwPt() << " hwSign " << finalCandidate->hwSign() << " hwQual "
0123              << finalCandidate->hwQual() << " hwEta " << std::setw(4) << finalCandidate->hwEta() << std::setw(4)
0124              << " hwPhi " << finalCandidate->hwPhi() << "    eta " << std::setw(9)
0125              << (finalCandidate->hwEta() * 0.010875) << " phi " << std::endl;
0126 
0127         if (runStubsSimHitsMatcher)
0128           stubsSimHitsMatcher->match(iEvent, matchingResult.muonCand, matchingResult.procMuon, ostr);
0129       }
0130     }
0131   } else if (!simTracksTag.label().empty()) {
0132     dump = false;
0133     bool wasSimMuInOmtfPos = false;
0134     bool wasSimMuInOmtfNeg = false;
0135     for (auto& simMuon : simMuons) {
0136       //TODO choose a condition, to print the desired events
0137       if (simMuon->eventId().event() == 0 && abs(simMuon->momentum().eta()) > 0.82 &&
0138           abs(simMuon->momentum().eta()) < 1.24 && simMuon->momentum().pt() >= 3.) {
0139         ostr << "SimMuon: eventId " << simMuon->eventId().event() << " pdgId " << std::setw(3) << simMuon->type()
0140              << " pt " << std::setw(9) << simMuon->momentum().pt()  //<<" Beta "<<simMuon->momentum().Beta()
0141              << " eta " << std::setw(9) << simMuon->momentum().eta() << " phi " << std::setw(9)
0142              << simMuon->momentum().phi() << std::endl;
0143 
0144         if (simMuon->momentum().eta() > 0)
0145           wasSimMuInOmtfPos = true;
0146         else
0147           wasSimMuInOmtfNeg = true;
0148       }
0149     }
0150 
0151     bool wasCandInNeg = false;
0152     bool wasCandInPos = false;
0153 
0154     for (auto& finalCandidate : *finalCandidates) {
0155       //TODO choose a condition, to print the desired candidates
0156       if (finalCandidate.trackFinderType() == l1t::tftype::omtf_neg && finalCandidate.hwQual() >= 12 &&
0157           finalCandidate.hwPt() > 20)
0158         wasCandInNeg = true;
0159 
0160       if (finalCandidate.trackFinderType() == l1t::tftype::omtf_pos && finalCandidate.hwQual() >= 12 &&
0161           finalCandidate.hwPt() > 20)
0162         wasCandInPos = true;
0163     }
0164 
0165     if ((wasSimMuInOmtfNeg && wasCandInNeg))  //TODO
0166       dump = true;
0167 
0168     if ((wasSimMuInOmtfPos && wasCandInPos))  //TODO
0169       dump = true;
0170   } else {
0171     //TODO choose a condition, to print the desired candidates
0172     // an example of a simple cut, only on the canidate pt
0173     /*
0174     for (auto& finalCandidate : *finalCandidates) {
0175       if (finalCandidate.hwPt() < 41) {  //  finalCandidate.hwQual() >= 1  41
0176         dump = true;
0177       }
0178     } */
0179     //!!!!!!!!!!!!!!!!!!!!!!!! TODO dumps all events!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0180     dump = true;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
0181   }
0182 
0183   if (!dump)
0184     return;
0185 
0186   ///printing
0187 
0188   edm::LogVerbatim("l1tOmtfEventPrint") << "##################### EventCapture::observeEventEnd - dump of event "
0189                                         << iEvent.id() << " #####################################################"
0190                                         << std::endl;
0191 
0192   edm::LogVerbatim("l1tOmtfEventPrint") << ostr.str() << endl;  //printing sim muons
0193 
0194   edm::LogVerbatim("l1tOmtfEventPrint") << "finalCandidates " << std::endl;
0195   for (int bx = finalCandidates->getFirstBX(); bx <= finalCandidates->getLastBX(); bx++) {
0196     for (auto finalCandidateIt = finalCandidates->begin(bx); finalCandidateIt != finalCandidates->end(bx);
0197          finalCandidateIt++) {
0198       auto& finalCandidate = *finalCandidateIt;
0199       int globHwPhi = (finalCandidate.processor()) * 96 + finalCandidate.hwPhi();
0200       // first processor starts at CMS phi = 15 degrees (24 in int)... Handle wrap-around with %. Add 576 to make sure the number is positive
0201       globHwPhi = (globHwPhi + 600) % 576;
0202 
0203       double globalPhi = globHwPhi * 2. * M_PI / 576;
0204       if (globalPhi > M_PI)
0205         globalPhi = globalPhi - (2. * M_PI);
0206 
0207       int layerHits = (int)finalCandidate.trackAddress().at(0);
0208       std::bitset<18> layerHitBits(layerHits);
0209 
0210       edm::LogVerbatim("l1tOmtfEventPrint")
0211           << " bx " << bx << " hwPt " << finalCandidate.hwPt() << " hwSign " << finalCandidate.hwSign() << " hwQual "
0212           << finalCandidate.hwQual() << " hwEta " << std::setw(4) << finalCandidate.hwEta() << std::setw(4) << " hwPhi "
0213           << finalCandidate.hwPhi() << "    eta " << std::setw(9) << (finalCandidate.hwEta() * 0.010875) << " phi "
0214           << std::setw(9) << globalPhi << " " << layerHitBits << " processor "
0215           << OmtfName(finalCandidate.processor(), finalCandidate.trackFinderType()) << std::endl;
0216 
0217       for (auto& trackAddr : finalCandidate.trackAddress()) {
0218         if (trackAddr.first >= 10)
0219           edm::LogVerbatim("l1tOmtfEventPrint")
0220               << "trackAddr first " << trackAddr.first << " second " << trackAddr.second << " ptGeV "
0221               << omtfConfig->hwPtToGev(trackAddr.second);
0222       }
0223     }
0224   }
0225   edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0226 
0227   for (unsigned int iProc = 0; iProc < inputInProcs.size(); iProc++) {
0228     OmtfName board(iProc);
0229 
0230     std::ostringstream ostrInput;
0231     if (inputInProcs[iProc]) {
0232       auto& omtfInput = *inputInProcs[iProc];
0233       int layersWithStubs = 0;
0234       for (auto& layer : omtfInput.getMuonStubs()) {
0235         for (auto& stub : layer) {
0236           bool layerFired = false;
0237           if (stub && (stub->type != MuonStub::Type::EMPTY)) {
0238             layerFired = true;
0239 
0240             auto globalPhiRad = omtfConfig->procHwPhiToGlobalPhi(
0241                 stub->phiHw, OMTFinputMaker::getProcessorPhiZero(omtfConfig, iProc % 6));
0242             ostrInput << (*stub) << " globalPhiRad " << globalPhiRad << std::endl;
0243           }
0244           if (layerFired)
0245             layersWithStubs++;
0246         }
0247       }
0248 
0249       if (layersWithStubs != 0) {
0250         edm::LogVerbatim("l1tOmtfEventPrint") << "\niProcessor " << iProc << " " << board.name()
0251                                               << " **************************************************" << std::endl;
0252         edm::LogVerbatim("l1tOmtfEventPrint") << ostrInput.str() << std::endl;
0253       }
0254 
0255       if (layersWithStubs < 2)
0256         continue;
0257 
0258       edm::LogVerbatim("l1tOmtfEventPrint") << *inputInProcs[iProc] << std::endl;
0259 
0260       edm::LogVerbatim("l1tOmtfEventPrint") << "algoMuons " << std::endl;
0261       //unsigned int procIndx = omtfConfig->getProcIndx(iProcessor, mtfType);
0262       for (auto& algoMuon : algoMuonsInProcs[iProc]) {
0263         if (algoMuon->isValid()) {
0264           edm::LogVerbatim("l1tOmtfEventPrint")
0265               << board.name() << " " << *algoMuon << " RefHitNum " << algoMuon->getRefHitNumber() << std::endl;
0266           edm::LogVerbatim("l1tOmtfEventPrint") << algoMuon->getGpResult() << std::endl;
0267 
0268           if (goldenPatterns)  //watch out with the golden patterns
0269             for (auto& gp : *goldenPatterns) {
0270               if (gp->key().thePt == 0)
0271                 continue;
0272 
0273               //printing GoldenPatternResult, uncomment if needed
0274               /*auto& gpResult = gp->getResults()[iProc][algoMuon->getRefHitNumber()];
0275             edm::LogVerbatim("l1tOmtfEventPrint") << " "<<gp->key() << "  "
0276               //<< "  refLayer: " << gpResult.getRefLayer() << "\t"
0277               << " Sum over layers: " << gpResult.getPdfSum() << "\t"
0278               << " Number of hits: " << gpResult.getFiredLayerCnt() << "\t"
0279               << std::endl;*/
0280             }
0281           edm::LogVerbatim("l1tOmtfEventPrint") << std::endl << std::endl;
0282         }
0283       }
0284 
0285       edm::LogVerbatim("l1tOmtfEventPrint") << "gbCandidates " << std::endl;
0286       for (auto& gbCandidate : gbCandidatesInProcs[iProc])
0287         if (gbCandidate->isValid())
0288           edm::LogVerbatim("l1tOmtfEventPrint") << board.name() << " " << *gbCandidate << std::endl;
0289 
0290       edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0291     }
0292   }
0293 
0294   edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0295 }
0296 
0297 void EventCapture::endJob() {
0298   if (stubsSimHitsMatcher)
0299     stubsSimHitsMatcher->endJob();
0300 }