Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-15 04:21:52

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