Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-03-01 03:21:10

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   if (edmCfg.exists("simTracksTag"))
0034     simTracksTag = edmCfg.getParameter<edm::InputTag>("simTracksTag");
0035   else
0036     edm::LogImportant("OMTFReconstruction")
0037         << "EventCapture::EventCapture: no InputTag simTracksTag found" << std::endl;
0038 
0039   //stubsSimHitsMatcher works only with the trackingParticle, because only them are stored in the pilup events
0040   if (this->candidateSimMuonMatcher && edmCfg.exists("trackingParticleTag"))
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 (std::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     LogTrace("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) {
0105         dump = true;
0106 
0107         bool runStubsSimHitsMatcher = false;
0108         if (matchingResult.trackingParticle) {
0109           auto trackingParticle = matchingResult.trackingParticle;
0110           ostr << "trackingParticle: eventId " << trackingParticle->eventId().event() << " pdgId " << std::setw(3)
0111                << trackingParticle->pdgId() << " trackId " << trackingParticle->g4Tracks().at(0).trackId() << " pt "
0112                << std::setw(9) << trackingParticle->pt()  //<<" Beta "<<simMuon->momentum().Beta()
0113                << " eta " << std::setw(9) << trackingParticle->momentum().eta() << " phi " << std::setw(9)
0114                << trackingParticle->momentum().phi() << std::endl;
0115         } else if (matchingResult.simTrack) {
0116           runStubsSimHitsMatcher = true;
0117           ostr << "SimMuon: eventId " << matchingResult.simTrack->eventId().event() << " pdgId " << std::setw(3)
0118                << matchingResult.simTrack->type() << " pt " << std::setw(9)
0119                << matchingResult.simTrack->momentum().pt()  //<<" Beta "<<simMuon->momentum().Beta()
0120                << " eta " << std::setw(9) << matchingResult.simTrack->momentum().eta() << " phi " << std::setw(9)
0121                << matchingResult.simTrack->momentum().phi() << std::endl;
0122         } else {
0123           ostr << "no simMuon ";
0124           runStubsSimHitsMatcher = true;
0125         }
0126         ostr << "matched to: " << std::endl;
0127         auto finalCandidate = matchingResult.muonCand;
0128         ostr << " hwPt " << finalCandidate->hwPt() << " hwUPt " << finalCandidate->hwPtUnconstrained() << " hwSign "
0129              << finalCandidate->hwSign() << " hwQual " << finalCandidate->hwQual() << " hwEta " << std::setw(4)
0130              << finalCandidate->hwEta() << std::setw(4) << " hwPhi " << finalCandidate->hwPhi() << "    eta "
0131              << std::setw(9) << (finalCandidate->hwEta() * 0.010875) << " phi " << std::endl;
0132 
0133         if (stubsSimHitsMatcher && runStubsSimHitsMatcher)
0134           stubsSimHitsMatcher->match(iEvent, matchingResult.muonCand, matchingResult.procMuon, ostr);
0135       }
0136     }
0137   } else if (!simTracksTag.label().empty()) {
0138     dump = false;
0139     bool wasSimMuInOmtfPos = false;
0140     bool wasSimMuInOmtfNeg = false;
0141     for (auto& simMuon : simMuons) {
0142       //TODO choose a condition, to print the desired events
0143       if (simMuon->eventId().event() == 0 && std::abs(simMuon->momentum().eta()) > 0.82 &&
0144           std::abs(simMuon->momentum().eta()) < 1.24 && simMuon->momentum().pt() >= 3.) {
0145         ostr << "SimMuon: eventId " << simMuon->eventId().event() << " pdgId " << std::setw(3) << simMuon->type()
0146              << " pt " << std::setw(9) << simMuon->momentum().pt()  //<<" Beta "<<simMuon->momentum().Beta()
0147              << " eta " << std::setw(9) << simMuon->momentum().eta() << " phi " << std::setw(9)
0148              << simMuon->momentum().phi() << std::endl;
0149 
0150         if (simMuon->momentum().eta() > 0)
0151           wasSimMuInOmtfPos = true;
0152         else
0153           wasSimMuInOmtfNeg = true;
0154       }
0155     }
0156 
0157     bool wasCandInNeg = false;
0158     bool wasCandInPos = false;
0159 
0160     for (auto& finalCandidate : *finalCandidates) {
0161       //TODO choose a condition, to print the desired candidates
0162       if (finalCandidate.trackFinderType() == l1t::tftype::omtf_neg && finalCandidate.hwQual() >= 12 &&
0163           finalCandidate.hwPt() > 20)
0164         wasCandInNeg = true;
0165 
0166       if (finalCandidate.trackFinderType() == l1t::tftype::omtf_pos && finalCandidate.hwQual() >= 12 &&
0167           finalCandidate.hwPt() > 20)
0168         wasCandInPos = true;
0169     }
0170 
0171     if ((wasSimMuInOmtfNeg && wasCandInNeg))  //TODO
0172       dump = true;
0173 
0174     if ((wasSimMuInOmtfPos && wasCandInPos))  //TODO
0175       dump = true;
0176   } else {
0177     //TODO choose a condition, to print the desired candidates
0178     // an example of a simple cut, only on the canidate pt
0179     /*
0180     for (auto& finalCandidate : *finalCandidates) {
0181       if (finalCandidate.hwPt() < 41) {  //  finalCandidate.hwQual() >= 1  41
0182         dump = true;
0183       }
0184     } */
0185     //!!!!!!!!!!!!!!!!!!!!!!!! TODO dumps all events!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0186     dump = true;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
0187   }
0188 
0189   if (!dump)
0190     return;
0191 
0192   ///printing
0193 
0194   edm::LogVerbatim("l1tOmtfEventPrint") << "##################### EventCapture::observeEventEnd - dump of event "
0195                                         << iEvent.id() << " #####################################################"
0196                                         << std::endl;
0197 
0198   edm::LogVerbatim("l1tOmtfEventPrint") << ostr.str() << endl;  //printing sim muons
0199 
0200   edm::LogVerbatim("l1tOmtfEventPrint") << "finalCandidates " << std::endl;
0201   for (int bx = finalCandidates->getFirstBX(); bx <= finalCandidates->getLastBX(); bx++) {
0202     for (auto finalCandidateIt = finalCandidates->begin(bx); finalCandidateIt != finalCandidates->end(bx);
0203          finalCandidateIt++) {
0204       auto& finalCandidate = *finalCandidateIt;
0205       int globHwPhi = (finalCandidate.processor()) * 96 + finalCandidate.hwPhi();
0206       // first processor starts at CMS phi = 15 degrees (24 in int)... Handle wrap-around with %. Add 576 to make sure the number is positive
0207       globHwPhi = (globHwPhi + 600) % 576;
0208 
0209       double globalPhi = globHwPhi * 2. * M_PI / 576;
0210       if (globalPhi > M_PI)
0211         globalPhi = globalPhi - (2. * M_PI);
0212 
0213       int layerHits = (int)finalCandidate.trackAddress().at(0);
0214       std::bitset<18> layerHitBits(layerHits);
0215 
0216       edm::LogVerbatim("l1tOmtfEventPrint")
0217           << " bx " << bx << " hwPt " << finalCandidate.hwPt() << " hwUPt " << finalCandidate.hwPtUnconstrained()
0218           << " hwSign " << finalCandidate.hwSign() << " hwQual " << finalCandidate.hwQual() << " hwEta " << std::setw(4)
0219           << finalCandidate.hwEta() << std::setw(4) << " hwPhi " << finalCandidate.hwPhi() << "    eta " << std::setw(9)
0220           << (finalCandidate.hwEta() * 0.010875) << " phi " << std::setw(9) << globalPhi << " " << layerHitBits
0221           << " processor " << OmtfName(finalCandidate.processor(), finalCandidate.trackFinderType(), omtfConfig)
0222           << std::endl;
0223 
0224       for (auto& trackAddr : finalCandidate.trackAddress()) {
0225         if (trackAddr.first >= 10)
0226           edm::LogVerbatim("l1tOmtfEventPrint")
0227               << "trackAddr first " << trackAddr.first << " second " << trackAddr.second << " ptGeV "
0228               << omtfConfig->hwPtToGev(trackAddr.second);
0229       }
0230     }
0231   }
0232   edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0233 
0234   for (unsigned int iProc = 0; iProc < inputInProcs.size(); iProc++) {
0235     OmtfName board(iProc, omtfConfig);
0236 
0237     std::ostringstream ostrInput;
0238     if (inputInProcs[iProc]) {
0239       auto& omtfInput = *inputInProcs[iProc];
0240       int layersWithStubs = 0;
0241       for (auto& layer : omtfInput.getMuonStubs()) {
0242         for (auto& stub : layer) {
0243           bool layerFired = false;
0244           if (stub && (stub->type != MuonStub::Type::EMPTY)) {
0245             layerFired = true;
0246 
0247             auto globalPhiRad = omtfConfig->procHwPhiToGlobalPhi(
0248                 stub->phiHw, OMTFinputMaker::getProcessorPhiZero(omtfConfig, iProc % omtfConfig->nProcessors()));
0249             ostrInput << (*stub) << " globalPhiRad " << globalPhiRad << std::endl;
0250           }
0251           if (layerFired)
0252             layersWithStubs++;
0253         }
0254       }
0255 
0256       if (layersWithStubs != 0) {
0257         edm::LogVerbatim("l1tOmtfEventPrint") << "\niProcessor " << iProc << " " << board.name()
0258                                               << " **************************************************" << std::endl;
0259         edm::LogVerbatim("l1tOmtfEventPrint") << ostrInput.str() << std::endl;
0260       }
0261 
0262       if (layersWithStubs < 2)
0263         continue;
0264 
0265       edm::LogVerbatim("l1tOmtfEventPrint") << *inputInProcs[iProc] << std::endl;
0266 
0267       edm::LogVerbatim("l1tOmtfEventPrint") << "algoMuons " << std::endl;
0268       //unsigned int procIndx = omtfConfig->getProcIndx(iProcessor, mtfType);
0269       for (auto& algoMuon : algoMuonsInProcs[iProc]) {
0270         if (algoMuon->isValid()) {
0271           edm::LogVerbatim("l1tOmtfEventPrint")
0272               << board.name() << " " << *algoMuon << " RefHitNum " << algoMuon->getRefHitNumber() << std::endl;
0273           edm::LogVerbatim("l1tOmtfEventPrint") << algoMuon->getGpResultConstr();
0274           if (algoMuon->getGpResultUnconstr().isValid())
0275             edm::LogVerbatim("l1tOmtfEventPrint")
0276                 << "GpResultUnconstr " << algoMuon->getGoldenPaternUnconstr()->key() << "\n"
0277                 << algoMuon->getGpResultUnconstr() << std::endl;
0278 
0279           if (goldenPatterns)  //watch out with the golden patterns
0280             for (auto& gp : *goldenPatterns) {
0281               if (gp->key().thePt == 0)
0282                 continue;
0283 
0284               //printing GoldenPatternResult, uncomment if needed
0285               /*auto& gpResult = gp->getResults()[iProc][algoMuon->getRefHitNumber()];
0286             edm::LogVerbatim("l1tOmtfEventPrint") << " "<<gp->key() << "  "
0287               //<< "  refLayer: " << gpResult.getRefLayer() << "\t"
0288               << " Sum over layers: " << gpResult.getPdfSum() << "\t"
0289               << " Number of hits: " << gpResult.getFiredLayerCnt() << "\t"
0290               << std::endl;*/
0291             }
0292           edm::LogVerbatim("l1tOmtfEventPrint") << std::endl << std::endl;
0293         }
0294       }
0295 
0296       edm::LogVerbatim("l1tOmtfEventPrint") << "gbCandidates " << std::endl;
0297       for (auto& gbCandidate : gbCandidatesInProcs[iProc])
0298         if (gbCandidate->isValid())
0299           edm::LogVerbatim("l1tOmtfEventPrint") << board.name() << " " << *gbCandidate << std::endl;
0300 
0301       {
0302         edm::LogVerbatim("l1tOmtfEventPrint") << std::endl << std::endl << "\ngb_test " << board.name() << std::endl;
0303         for (auto& algoMuon : algoMuonsInProcs[iProc]) {
0304           edm::LogVerbatim("l1tOmtfEventPrint")
0305               << "     (" << std::setw(5) << algoMuon->getHwPatternNumConstr() << "," << std::setw(5)
0306               << algoMuon->getHwPatternNumUnconstr() << ","
0307 
0308               << std::setw(5) << algoMuon->getGpResultConstr().getFiredLayerCnt() << "," << std::setw(5)
0309               << algoMuon->getGpResultUnconstr().getFiredLayerCnt()
0310               << ","
0311 
0312               //in the FW there is LSB added for some reason, therefore we multiply by 2 here
0313               << std::setw(6) << algoMuon->getGpResultConstr().getFiredLayerBits() * 2 << "," << std::setw(6)
0314               << algoMuon->getGpResultUnconstr().getFiredLayerBits() * 2 << ","
0315 
0316               << std::setw(5) << algoMuon->getGpResultConstr().getPdfSum() << "," << std::setw(5)
0317               << algoMuon->getGpResultUnconstr().getPdfSumUnconstr() << ","
0318 
0319               << std::setw(5) << algoMuon->getGpResultConstr().getPhi() << "," << std::setw(5)
0320               << algoMuon->getGpResultUnconstr().getPhi()
0321               << ","
0322 
0323               //<<std::setw(5)<<OMTFConfiguration::eta2Bits(std::abs(algoMuon->getEtaHw()))<<", "
0324               << std::setw(5) << algoMuon->getEtaHw()
0325               << ", "
0326 
0327               //<<std::setw(5)<<omtfConfig->getRefToLogicNumber()[algoMuon->getRefLayer()]<<""
0328               << std::setw(5) << algoMuon->getRefLayer() << ""
0329 
0330               << "), " << std::endl;
0331         }
0332         edm::LogVerbatim("l1tOmtfEventPrint") << "\ngbCandidates" << std::endl;
0333         for (auto& gbCandidate : gbCandidatesInProcs[iProc])
0334           edm::LogVerbatim("l1tOmtfEventPrint")
0335               << "     (" << std::setw(5) << gbCandidate->getPtConstr() << "," << std::setw(5)
0336               << gbCandidate->getPtUnconstr() << ","
0337 
0338               << std::setw(5) << gbCandidate->getGpResultConstr().getFiredLayerCnt() << "," << std::setw(5)
0339               << gbCandidate->getGpResultUnconstr().getFiredLayerCnt()
0340               << ","
0341 
0342               //in the FW there is LSB added for some reason, therefore we multiply by 2 here
0343               << std::setw(6) << gbCandidate->getGpResultConstr().getFiredLayerBits() * 2 << "," << std::setw(6)
0344               << gbCandidate->getGpResultUnconstr().getFiredLayerBits() * 2 << ","
0345 
0346               << std::setw(5) << gbCandidate->getGpResultConstr().getPdfSum() << "," << std::setw(5)
0347               << gbCandidate->getGpResultUnconstr().getPdfSumUnconstr() << ","
0348 
0349               << std::setw(5) << gbCandidate->getGpResultConstr().getPhi() << "," << std::setw(5)
0350               << gbCandidate->getGpResultUnconstr().getPhi()
0351               << ","
0352 
0353               //<<std::setw(5)<<OMTFConfiguration::eta2Bits(std::abs(gbCandidate->getEtaHw()))<<", "
0354               << std::setw(5) << gbCandidate->getEtaHw()
0355               << ", "
0356 
0357               //<<std::setw(5)<<omtfConfig->getRefToLogicNumber()[gbCandidate->getRefLayer()]<<""
0358               << std::setw(5) << gbCandidate->getRefLayer() << ""
0359 
0360               << "), "
0361               << " -- getFiredLayerBits " << std::setw(5) << gbCandidate->getGpResultConstr().getFiredLayerBits()
0362               << std::endl;
0363 
0364         edm::LogVerbatim("l1tOmtfEventPrint") << "finalCandidates " << std::endl;
0365 
0366         std::ostringstream ostr;
0367         if (finalCandidates->size(0) > 0) {
0368           int iMu = 1;
0369           for (auto finalCandidateIt = finalCandidates->begin(0); finalCandidateIt != finalCandidates->end(0);
0370                finalCandidateIt++) {
0371             auto& finalCandidate = *finalCandidateIt;
0372 
0373             auto omtfName = OmtfName(finalCandidate.processor(), finalCandidate.trackFinderType(), omtfConfig);
0374 
0375             if (omtfName == board.name()) {
0376               int layerHits = (int)finalCandidate.trackAddress().at(0);
0377               std::bitset<18> layerHitBits(layerHits);
0378 
0379               unsigned int trackAddr = finalCandidate.trackAddress().at(0);
0380               unsigned int uPt = finalCandidate.hwPtUnconstrained();
0381               //if(uPt == 0) uPt = 5; //TODO remove when fixed in the FW
0382               trackAddr = (uPt << 18) + trackAddr;
0383 
0384               ostr << "M" << iMu << ":" << std::setw(4) << finalCandidate.hwPt() << "," << std::setw(4)
0385                    << finalCandidate.hwQual() << "," << std::setw(4) << finalCandidate.hwPhi() << "," << std::setw(4)
0386                    << std::abs(finalCandidate.hwEta())
0387                    << ","
0388                    //<<std::setw(10)<< finalCandidate.trackAddress().at(0)<<""
0389                    << std::setw(10) << trackAddr << "," << std::setw(4) << 0 << ","                 //Halo
0390                    << std::setw(4) << finalCandidate.hwSign() << "," << std::setw(4) << 1 << "; ";  //ChValid
0391               //<< " -- uPt " << std::setw(10) << uPt << " firedLayers " << finalCandidate.trackAddress().at(0);
0392 
0393               //<<std::setw(5)<< finalCandidate.hwPtUnconstrained()<<","
0394 
0395               //<<std::setw(9)<< layerHitBits<<","
0396               //<<std::setw(6)<< layerHits<<","
0397               iMu++;
0398             }
0399           }
0400           for (; iMu <= 3; iMu++)
0401             ostr << "M" << iMu << ":   0,   0,   0,   0,         0,   0,   0,   0; ";
0402           edm::LogVerbatim("l1tOmtfEventPrint") << ostr.str() << std::endl;
0403         }
0404       }
0405 
0406       edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0407     }
0408   }
0409 
0410   edm::LogVerbatim("l1tOmtfEventPrint") << std::endl;
0411 }
0412 
0413 void EventCapture::endJob() {
0414   if (stubsSimHitsMatcher)
0415     stubsSimHitsMatcher->endJob();
0416 }