Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:09

0001 /*
0002  * XMLEventWriter.cc
0003  *
0004  *  Created on: Oct 12, 2017
0005  *      Author: kbunkow
0006  */
0007 
0008 #include "DataFormats/Provenance/interface/EventID.h"
0009 #include "FWCore/Common/interface/EventBase.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OmtfName.h"
0012 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFinput.h"
0013 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/XMLEventWriter.h"
0014 
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include <boost/property_tree/xml_parser.hpp>
0018 
0019 #include <bitset>
0020 
0021 XMLEventWriter::XMLEventWriter(const OMTFConfiguration* aOMTFConfig, std::string fName)
0022     : omtfConfig(aOMTFConfig), fName(fName) {
0023   //std::string fName = "OMTF";
0024   eventNum = 0;
0025 
0026   unsigned int version = aOMTFConfig->patternsVersion();
0027   const unsigned int mask16bits = 0xFFFF;
0028 
0029   version &= mask16bits;
0030 
0031   std::ostringstream stringStr;
0032   stringStr.str("");
0033   stringStr << "0x" << std::hex << std::setfill('0') << std::setw(4) << version;
0034 
0035   tree.put("OMTF.<xmlattr>.version", stringStr.str());
0036 };
0037 
0038 XMLEventWriter::~XMLEventWriter() {}
0039 
0040 void XMLEventWriter::observeProcesorBegin(unsigned int iProcessor, l1t::tftype mtfType) {
0041   if (eventNum > 5000)
0042     return;
0043 
0044   procTree.clear();
0045 
0046   int endcap = (mtfType == l1t::omtf_neg) ? -1 : ((mtfType == l1t::omtf_pos) ? +1 : 0);
0047   OmtfName board(iProcessor, endcap, omtfConfig);
0048   procTree.add("<xmlattr>.board", board.name());
0049   procTree.add("<xmlattr>.iProcessor", iProcessor);
0050 
0051   std::ostringstream stringStr;
0052   stringStr << (board.position() == 1 ? "+" : "") << board.position();
0053   procTree.add("<xmlattr>.position", stringStr.str());
0054 }
0055 
0056 void XMLEventWriter::observeProcesorEmulation(unsigned int iProcessor,
0057                                               l1t::tftype mtfType,
0058                                               const std::shared_ptr<OMTFinput>& input,
0059                                               const AlgoMuons& algoCandidates,
0060                                               const AlgoMuons& gbCandidates,
0061                                               const std::vector<l1t::RegionalMuonCand>& candMuons) {
0062   if (eventNum > 5000)
0063     return;
0064 
0065   int endcap = (mtfType == l1t::omtf_neg) ? -1 : ((mtfType == l1t::omtf_pos) ? +1 : 0);
0066   OmtfName board(iProcessor, endcap, omtfConfig);
0067 
0068   if (candMuons.empty())
0069     return;
0070 
0071   for (unsigned int iLayer = 0; iLayer < omtfConfig->nLayers(); ++iLayer) {
0072     boost::property_tree::ptree layerTree;
0073 
0074     for (unsigned int iHit = 0; iHit < input->getMuonStubs()[iLayer].size(); ++iHit) {
0075       int hitPhi = input->getPhiHw(iLayer, iHit);
0076       if (hitPhi >= (int)omtfConfig->nPhiBins())
0077         continue;
0078 
0079       auto& hitTree = layerTree.add("Hit", "");
0080 
0081       hitTree.add("<xmlattr>.iInput", iHit);
0082       hitTree.add("<xmlattr>.iEta", input->getHitEta(iLayer, iHit));
0083       hitTree.add("<xmlattr>.iPhi", hitPhi);
0084 
0085       //in the firmware the hit quality is taken from link data only for the DT stubs
0086       //for the CSC and RPC 1 means the hit is valid, 0 - not.
0087       //in the input it is still worth to have the actual quality of the CSC and RPC
0088       //Because it might be used in the neural network
0089       if (iLayer >= 6)
0090         hitTree.add("<xmlattr>.iQual", 1);
0091       else
0092         hitTree.add("<xmlattr>.iQual", input->getHitQual(iLayer, iHit));
0093     }
0094 
0095     if (!layerTree.empty()) {
0096       layerTree.add("<xmlattr>.iLayer", iLayer);
0097       procTree.add_child("Layer", layerTree);
0098     }
0099   }
0100 
0101   for (auto& algoCand : algoCandidates) {
0102     ///Dump only regions, where a candidate was found
0103     if (algoCand->isValid()) {
0104       auto& algoMuonTree = procTree.add("AlgoMuon", "");
0105       algoMuonTree.add("<xmlattr>.charge", algoCand->getChargeConstr());
0106       algoMuonTree.add("<xmlattr>.disc", algoCand->getDisc());
0107       algoMuonTree.add("<xmlattr>.pdfSumConstr", algoCand->getGpResultConstr().getPdfSum());
0108       algoMuonTree.add("<xmlattr>.pdfSumUnconstr", algoCand->getGpResultUnconstr().getPdfSumUnconstr());
0109       algoMuonTree.add("<xmlattr>.etaCode", algoCand->getEtaHw());
0110       algoMuonTree.add("<xmlattr>.iRefHit", algoCand->getRefHitNumber());
0111       algoMuonTree.add("<xmlattr>.iRefLayer", algoCand->getRefLayer());
0112 
0113       //algoMuonTree.add("<xmlattr>.layers", std::bitset<18>(algoCand->getFiredLayerBits()));
0114 
0115       algoMuonTree.add("<xmlattr>.layersConstr", std::bitset<18>(algoCand->getGpResultConstr().getFiredLayerBits()));
0116       algoMuonTree.add("<xmlattr>.layersUnconstr",
0117                        std::bitset<18>(algoCand->getGpResultUnconstr().getFiredLayerBits()));
0118 
0119       algoMuonTree.add("<xmlattr>.nHits", algoCand->getQ());
0120 
0121       algoMuonTree.add("<xmlattr>.firedCntConstr", algoCand->getGpResultConstr().getFiredLayerCnt());
0122       algoMuonTree.add("<xmlattr>.firedCntUnconstr", algoCand->getGpResultUnconstr().getFiredLayerCnt());
0123 
0124       algoMuonTree.add("<xmlattr>.patNumConstr", algoCand->getHwPatternNumConstr());
0125       algoMuonTree.add("<xmlattr>.patNumUnconstr", algoCand->getHwPatternNumUnconstr());
0126 
0127       algoMuonTree.add("<xmlattr>.phiCode", algoCand->getPhi());
0128 
0129       algoMuonTree.add("<xmlattr>.phiConstr", algoCand->getGpResultConstr().getPhi());
0130       algoMuonTree.add("<xmlattr>.phiUnConstr", algoCand->getGpResultUnconstr().getPhi());
0131 
0132       algoMuonTree.add("<xmlattr>.phiRHit", algoCand->getPhiRHit());
0133 
0134       //in the firmware, the algoMuon has no pt nor upt yet,
0135       //only the pattern number, which is converted to the hwpt in the ghostbuster
0136       algoMuonTree.add("<xmlattr>.ptCodeConstr", algoCand->getPtConstr());
0137       algoMuonTree.add("<xmlattr>.ptCodeUnconstr", algoCand->getPtUnconstr());
0138 
0139       auto& gpResultTree = algoMuonTree.add("gpResultConstr", "");
0140       auto& gpResultConstr = algoCand->getGpResultConstr();
0141 
0142       gpResultTree.add("<xmlattr>.patNum", algoCand->getHwPatternNumConstr());
0143       gpResultTree.add("<xmlattr>.pdfSum", gpResultConstr.getPdfSum());
0144 
0145       for (unsigned int iLogicLayer = 0; iLogicLayer < gpResultConstr.getStubResults().size(); ++iLogicLayer) {
0146         auto& layerTree = gpResultTree.add("layer", "");
0147         layerTree.add("<xmlattr>.num", iLogicLayer);
0148         auto pdfBin = gpResultConstr.getStubResults()[iLogicLayer].getPdfBin();
0149         if (pdfBin == 5400)
0150           pdfBin = 0;
0151         layerTree.add("<xmlattr>.pdfBin", pdfBin);
0152         layerTree.add("<xmlattr>.pdfVal", gpResultConstr.getStubResults()[iLogicLayer].getPdfVal());
0153         layerTree.add("<xmlattr>.fired", gpResultConstr.isLayerFired(iLogicLayer));
0154       }
0155 
0156       if (algoCand->getGpResultUnconstr().isValid()) {
0157         auto& gpResultTree = algoMuonTree.add("gpResultUnconstr", "");
0158         auto& gpResult = algoCand->getGpResultUnconstr();
0159 
0160         gpResultTree.add("<xmlattr>.patNum", algoCand->getHwPatternNumUnconstr());
0161         gpResultTree.add("<xmlattr>.pdfSum", gpResult.getPdfSumUnconstr());
0162 
0163         for (unsigned int iLogicLayer = 0; iLogicLayer < gpResult.getStubResults().size(); ++iLogicLayer) {
0164           auto& layerTree = gpResultTree.add("layer", "");
0165           layerTree.add("<xmlattr>.num", iLogicLayer);
0166           auto pdfBin = gpResult.getStubResults()[iLogicLayer].getPdfBin();
0167           if (pdfBin == 5400)
0168             pdfBin = 0;
0169           layerTree.add("<xmlattr>.pdfBin", pdfBin);
0170           layerTree.add("<xmlattr>.pdfVal", gpResult.getStubResults()[iLogicLayer].getPdfVal());
0171           layerTree.add("<xmlattr>.fired", gpResult.isLayerFired(iLogicLayer));
0172         }
0173       }
0174     }
0175   }
0176 
0177   for (auto& candMuon : candMuons) {
0178     auto& candMuonTree = procTree.add("CandMuon", "");
0179     candMuonTree.add("<xmlattr>.hwEta", candMuon.hwEta());
0180     candMuonTree.add("<xmlattr>.hwPhi", candMuon.hwPhi());
0181     candMuonTree.add("<xmlattr>.hwPt", candMuon.hwPt());
0182     candMuonTree.add("<xmlattr>.hwUPt", candMuon.hwPtUnconstrained());
0183     candMuonTree.add("<xmlattr>.hwQual", candMuon.hwQual());
0184     candMuonTree.add("<xmlattr>.hwSign", candMuon.hwSign());
0185     candMuonTree.add("<xmlattr>.hwSignValid", candMuon.hwSignValid());
0186     candMuonTree.add("<xmlattr>.hwTrackAddress", std::bitset<29>(candMuon.trackAddress().at(0)));
0187     candMuonTree.add("<xmlattr>.link", candMuon.link());
0188     candMuonTree.add("<xmlattr>.processor", candMuon.processor());
0189 
0190     std::ostringstream stringStr;
0191     if (candMuon.trackFinderType() == l1t::omtf_neg)
0192       stringStr << "OMTF_NEG";
0193     else if (candMuon.trackFinderType() == l1t::omtf_pos)
0194       stringStr << "OMTF_POS";
0195     else
0196       stringStr << candMuon.trackFinderType();
0197     candMuonTree.add("<xmlattr>.trackFinderType", stringStr.str());
0198   }
0199 
0200   if (!procTree.empty())
0201     eventTree->add_child("Processor", procTree);
0202 }
0203 
0204 void XMLEventWriter::observeEventBegin(const edm::Event& iEvent) {
0205   eventNum++;
0206   if (eventNum > 5000)
0207     //due to some bug if more events is written the memory consumption s very big and program crashes
0208     return;
0209   //currentElement = xmlWriter.writeEventHeader(iEvent.id().event());
0210   eventId = iEvent.id().event();
0211 
0212   eventTree = &(tree.add("OMTF.Event", ""));
0213   eventTree->add("<xmlattr>.iEvent", eventId);
0214 
0215   eventTree = &(eventTree->add("bx", ""));
0216   eventTree->add("<xmlattr>.iBx", 2 * eventId);
0217 }
0218 
0219 void XMLEventWriter::observeEventEnd(const edm::Event& iEvent,
0220                                      std::unique_ptr<l1t::RegionalMuonCandBxCollection>& finalCandidates) {}
0221 
0222 void XMLEventWriter::endJob() {
0223   edm::LogInfo("l1tOmtfEventPrint") << "XMLEventWriter::endJob() - writing the data to the xml - starting";
0224   boost::property_tree::write_xml(
0225       fName, tree, std::locale(), boost::property_tree::xml_parser::xml_writer_make_settings<std::string>(' ', 2));
0226   edm::LogInfo("l1tOmtfEventPrint") << "XMLEventWriter::endJob() - writing the data to the xml - done";
0227 }