Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/XMLConfigWriter.h"
0002 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/AlgoMuon.h"
0003 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/GoldenPattern.h"
0004 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/GoldenPatternResult.h"
0005 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/GoldenPatternWithStat.h"
0006 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFConfiguration.h"
0007 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFinput.h"
0008 
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "DataFormats/L1TMuon/interface/RegionalMuonCand.h"
0011 
0012 #include <iostream>
0013 #include <sstream>
0014 #include <cmath>
0015 #include <ctime>
0016 #include <iomanip>
0017 #include <bitset>
0018 
0019 #include "xercesc/framework/StdOutFormatTarget.hpp"
0020 #include "xercesc/framework/LocalFileFormatTarget.hpp"
0021 #include "xercesc/parsers/XercesDOMParser.hpp"
0022 #include "xercesc/dom/DOM.hpp"
0023 #include "xercesc/dom/DOMException.hpp"
0024 #include "xercesc/dom/DOMImplementation.hpp"
0025 #include "xercesc/sax/HandlerBase.hpp"
0026 #include "xercesc/util/XMLString.hpp"
0027 #include "xercesc/util/PlatformUtils.hpp"
0028 #include "xercesc/util/XercesDefs.hpp"
0029 #include "xercesc/util/XercesVersion.hpp"
0030 XERCES_CPP_NAMESPACE_USE
0031 
0032 #if _XERCES_VERSION < 30100
0033 #include "xercesc/dom/DOMWriter.hpp"
0034 #else
0035 #include "xercesc/dom/DOMLSSerializer.hpp"
0036 #include "xercesc/dom/DOMLSOutput.hpp"
0037 #endif
0038 //
0039 
0040 namespace {
0041   unsigned int eta2Bits(unsigned int eta) {
0042     if (eta == 73)
0043       return 0b100000000;
0044     else if (eta == 78)
0045       return 0b010000000;
0046     else if (eta == 85)
0047       return 0b001000000;
0048     else if (eta == 90)
0049       return 0b000100000;
0050     else if (eta == 94)
0051       return 0b000010000;
0052     else if (eta == 99)
0053       return 0b000001000;
0054     else if (eta == 103)
0055       return 0b000000100;
0056     else if (eta == 110)
0057       return 0b000000010;
0058     else if (eta == 75)
0059       return 0b110000000;
0060     else if (eta == 79)
0061       return 0b011000000;
0062     else if (eta == 92)
0063       return 0b000110000;
0064     else if (eta == 115)
0065       return 0b000000001;
0066     else if (eta == 121)
0067       return 0b000000000;
0068     else
0069       return 0b111111111;
0070     ;
0071   }
0072 }  // namespace
0073 
0074 //////////////////////////////////
0075 // XMLConfigWriter
0076 //////////////////////////////////
0077 inline std::string _toString(XMLCh const* toTranscode) {
0078   std::string tmp(xercesc::XMLString::transcode(toTranscode));
0079   return tmp;
0080 }
0081 
0082 inline XMLCh* _toDOMS(std::string temp) {
0083   XMLCh* buff = XMLString::transcode(temp.c_str());
0084   return buff;
0085 }
0086 ////////////////////////////////////
0087 ////////////////////////////////////
0088 XMLConfigWriter::XMLConfigWriter(const OMTFConfiguration* aOMTFConfig, bool writePdfThresholds, bool writeMeanDistPhi1)
0089     : writePdfThresholds(writePdfThresholds), writeMeanDistPhi1(writeMeanDistPhi1) {
0090   XMLPlatformUtils::Initialize();
0091 
0092   ///Initialise XML document
0093   domImpl = DOMImplementationRegistry::getDOMImplementation(_toDOMS("Range"));
0094 
0095   myOMTFConfig = aOMTFConfig;
0096 }
0097 //////////////////////////////////////////////////
0098 //////////////////////////////////////////////////
0099 void XMLConfigWriter::initialiseXMLDocument(const std::string& docName) {
0100   theDoc = domImpl->createDocument(nullptr, _toDOMS(docName), nullptr);
0101   theTopElement = theDoc->getDocumentElement();
0102 
0103   unsigned int version = myOMTFConfig->patternsVersion();
0104   unsigned int mask16bits = 0xFFFF;
0105 
0106   version &= mask16bits;
0107 
0108   std::ostringstream stringStr;
0109   stringStr.str("");
0110   stringStr << "0x" << std::hex << std::setfill('0') << std::setw(4) << version;
0111   theTopElement->setAttribute(_toDOMS("version"), _toDOMS(stringStr.str()));
0112 }
0113 //////////////////////////////////////////////////
0114 //////////////////////////////////////////////////
0115 void XMLConfigWriter::finaliseXMLDocument(const std::string& fName) {
0116   XMLFormatTarget* formTarget = new LocalFileFormatTarget(fName.c_str());
0117 
0118 #if _XERCES_VERSION < 30100
0119   xercesc::DOMWriter* domWriter = (dynamic_cast<DOMImplementation*>(domImpl))->createDOMWriter();
0120   if (domWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true)) {
0121     domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0122   }
0123   domWriter->writeNode(formTarget, *theTopElement);
0124 
0125 #else
0126   xercesc::DOMLSSerializer* theSerializer = (dynamic_cast<DOMImplementation*>(domImpl))->createLSSerializer();
0127   if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0128     theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0129   DOMLSOutput* theOutput = (dynamic_cast<DOMImplementation*>(domImpl))->createLSOutput();
0130   theOutput->setByteStream(formTarget);
0131   theSerializer->write(theTopElement, theOutput);
0132   theOutput->release();
0133   theSerializer->release();
0134 #endif
0135 
0136   delete formTarget;
0137 }
0138 //////////////////////////////////////////////////
0139 //////////////////////////////////////////////////
0140 xercesc::DOMElement* XMLConfigWriter::writeEventHeader(unsigned int eventId, unsigned int mixedEventId) {
0141   unsigned int eventBx = eventId * 2;
0142 
0143   xercesc::DOMElement* aEvent = nullptr;
0144   xercesc::DOMElement* aBx = nullptr;
0145   std::ostringstream stringStr;
0146 
0147   aEvent = theDoc->createElement(_toDOMS("Event"));
0148 
0149   stringStr.str("");
0150   stringStr << eventId;
0151   aEvent->setAttribute(_toDOMS("iEvent"), _toDOMS(stringStr.str()));
0152 
0153   stringStr.str("");
0154   stringStr << mixedEventId;
0155   aEvent->setAttribute(_toDOMS("iMixedEvent"), _toDOMS(stringStr.str()));
0156 
0157   aBx = theDoc->createElement(_toDOMS("bx"));
0158   stringStr.str("");
0159   stringStr << eventBx;
0160   aBx->setAttribute(_toDOMS("iBx"), _toDOMS(stringStr.str()));
0161   aEvent->appendChild(aBx);
0162 
0163   theTopElement->appendChild(aEvent);
0164 
0165   return aBx;
0166 }
0167 //////////////////////////////////////////////////
0168 //////////////////////////////////////////////////
0169 xercesc::DOMElement* XMLConfigWriter::writeEventData(xercesc::DOMElement* aTopElement,
0170                                                      const OmtfName& board,
0171                                                      const OMTFinput& aInput) {
0172   std::ostringstream stringStr;
0173 
0174   xercesc::DOMElement* aProcessor = theDoc->createElement(_toDOMS("Processor"));
0175   aProcessor->setAttribute(_toDOMS("board"), _toDOMS(board.name()));
0176 
0177   unsigned int iProcessor = board.processor();
0178   stringStr.str("");
0179   stringStr << iProcessor;
0180   aProcessor->setAttribute(_toDOMS("iProcessor"), _toDOMS(stringStr.str()));
0181   stringStr.str("");
0182   if (board.position() == 1)
0183     stringStr << "+";
0184   stringStr << board.position();
0185   aProcessor->setAttribute(_toDOMS("position"), _toDOMS(stringStr.str()));
0186 
0187   xercesc::DOMElement *aLayer, *aHit;
0188   for (unsigned int iLayer = 0; iLayer < myOMTFConfig->nLayers(); ++iLayer) {
0189     aLayer = theDoc->createElement(_toDOMS("Layer"));
0190     stringStr.str("");
0191     stringStr << iLayer;
0192     aLayer->setAttribute(_toDOMS("iLayer"), _toDOMS(stringStr.str()));
0193     for (unsigned int iHit = 0; iHit < aInput.getMuonStubs()[iLayer].size(); ++iHit) {
0194       int hitPhi = aInput.getPhiHw(iLayer, iHit);
0195       if (hitPhi >= (int)myOMTFConfig->nPhiBins())
0196         continue;
0197 
0198       aHit = theDoc->createElement(_toDOMS("Hit"));
0199       stringStr.str("");
0200       stringStr << iHit;
0201       aHit->setAttribute(_toDOMS("iInput"), _toDOMS(stringStr.str()));
0202       stringStr.str("");
0203       stringStr << hitPhi;
0204       aHit->setAttribute(_toDOMS("iPhi"), _toDOMS(stringStr.str()));
0205       stringStr.str("");
0206       stringStr << eta2Bits(abs(aInput.getHitEta(iLayer, iHit)));
0207       aHit->setAttribute(_toDOMS("iEta"), _toDOMS(stringStr.str()));
0208 
0209       aLayer->appendChild(aHit);
0210     }
0211     if (aLayer->getChildNodes()->getLength())
0212       aProcessor->appendChild(aLayer);
0213   }
0214 
0215   aTopElement->appendChild(aProcessor);
0216   return aProcessor;
0217 }
0218 //////////////////////////////////////////////////
0219 //////////////////////////////////////////////////
0220 void XMLConfigWriter::writeAlgoMuon(xercesc::DOMElement* aTopElement, const AlgoMuon& aCand) {
0221   xercesc::DOMElement* aResult = theDoc->createElement(_toDOMS("AlgoMuon"));
0222   std::ostringstream stringStr;
0223   stringStr.str("");
0224   stringStr << aCand.getRefHitNumber();
0225   aResult->setAttribute(_toDOMS("iRefHit"), _toDOMS(stringStr.str()));
0226   stringStr.str("");
0227   stringStr << aCand.getPt();
0228   aResult->setAttribute(_toDOMS("ptCode"), _toDOMS(stringStr.str()));
0229   stringStr.str("");
0230   stringStr << aCand.getPhi();
0231   aResult->setAttribute(_toDOMS("phiCode"), _toDOMS(stringStr.str()));
0232   stringStr.str("");
0233   stringStr << eta2Bits(abs(aCand.getEtaHw()));
0234   aResult->setAttribute(_toDOMS("etaCode"), _toDOMS(stringStr.str()));
0235   stringStr.str("");
0236   stringStr << aCand.getCharge();
0237   aResult->setAttribute(_toDOMS("charge"), _toDOMS(stringStr.str()));
0238   stringStr.str("");
0239   stringStr << aCand.getQ();
0240   aResult->setAttribute(_toDOMS("nHits"), _toDOMS(stringStr.str()));
0241   stringStr.str("");
0242   stringStr << aCand.getDisc();
0243   aResult->setAttribute(_toDOMS("disc"), _toDOMS(stringStr.str()));
0244   stringStr.str("");
0245   stringStr << aCand.getRefLayer();
0246   aResult->setAttribute(_toDOMS("iRefLayer"), _toDOMS(stringStr.str()));
0247   stringStr.str("");
0248   stringStr << std::bitset<18>(aCand.getFiredLayerBits());
0249   aResult->setAttribute(_toDOMS("layers"), _toDOMS(stringStr.str()));
0250   stringStr.str("");
0251   stringStr << aCand.getPhiRHit();
0252   aResult->setAttribute(_toDOMS("phiRHit"), _toDOMS(stringStr.str()));
0253   stringStr.str("");
0254   stringStr << aCand.getHwPatternNumber();
0255   aResult->setAttribute(_toDOMS("patNum"), _toDOMS(stringStr.str()));
0256 
0257   aTopElement->appendChild(aResult);
0258 }
0259 //////////////////////////////////////////////////
0260 //////////////////////////////////////////////////
0261 void XMLConfigWriter::writeCandMuon(xercesc::DOMElement* aTopElement, const l1t::RegionalMuonCand& aCand) {
0262   xercesc::DOMElement* aResult = theDoc->createElement(_toDOMS("CandMuon"));
0263   std::ostringstream stringStr;
0264   stringStr.str("");
0265   stringStr << aCand.hwPt();
0266   aResult->setAttribute(_toDOMS("hwPt"), _toDOMS(stringStr.str()));
0267   stringStr.str("");
0268   stringStr << aCand.hwPhi();
0269   aResult->setAttribute(_toDOMS("hwPhi"), _toDOMS(stringStr.str()));
0270   stringStr.str("");
0271   stringStr << aCand.hwEta();
0272   aResult->setAttribute(_toDOMS("hwEta"), _toDOMS(stringStr.str()));
0273   stringStr.str("");
0274   stringStr << aCand.hwSign();
0275   aResult->setAttribute(_toDOMS("hwSign"), _toDOMS(stringStr.str()));
0276   stringStr.str("");
0277   stringStr << aCand.hwSignValid();
0278   aResult->setAttribute(_toDOMS("hwSignValid"), _toDOMS(stringStr.str()));
0279   stringStr.str("");
0280   stringStr << aCand.hwQual();
0281   aResult->setAttribute(_toDOMS("hwQual"), _toDOMS(stringStr.str()));
0282   stringStr.str("");
0283   std::map<int, int> hwAddrMap = aCand.trackAddress();
0284   stringStr << std::bitset<29>(hwAddrMap[0]);
0285   aResult->setAttribute(_toDOMS("hwTrackAddress"), _toDOMS(stringStr.str()));
0286   stringStr.str("");
0287   stringStr << aCand.link();
0288   aResult->setAttribute(_toDOMS("link"), _toDOMS(stringStr.str()));
0289   stringStr.str("");
0290   stringStr << aCand.processor();
0291   aResult->setAttribute(_toDOMS("processor"), _toDOMS(stringStr.str()));
0292   stringStr.str("");
0293   if (aCand.trackFinderType() == l1t::omtf_neg)
0294     stringStr << "OMTF_NEG";
0295   else if (aCand.trackFinderType() == l1t::omtf_pos)
0296     stringStr << "OMTF_POS";
0297   else
0298     stringStr << aCand.trackFinderType();
0299   aResult->setAttribute(_toDOMS("trackFinderType"), _toDOMS(stringStr.str()));
0300   aTopElement->appendChild(aResult);
0301 }
0302 //////////////////////////////////////////////////
0303 //////////////////////////////////////////////////
0304 //////////////////////////////////////////////////
0305 //////////////////////////////////////////////////
0306 void XMLConfigWriter::writeResultsData(xercesc::DOMElement* aTopElement,
0307                                        unsigned int iRegion,
0308                                        const Key& aKey,
0309                                        const GoldenPatternResult& aResult) {
0310   std::ostringstream stringStr;
0311   ///Write GP key parameters
0312   xercesc::DOMElement* aGP = theDoc->createElement(_toDOMS("GP"));
0313   stringStr.str("");
0314   stringStr << aKey.thePt;
0315   aGP->setAttribute(_toDOMS("iPt"), _toDOMS(stringStr.str()));
0316   stringStr.str("");
0317   stringStr << aKey.theEtaCode;
0318   aGP->setAttribute(_toDOMS("iEta"), _toDOMS(stringStr.str()));
0319   stringStr.str("");
0320   stringStr << "0";
0321   aGP->setAttribute(_toDOMS("iPhi"), _toDOMS(stringStr.str()));
0322   stringStr.str("");
0323   stringStr << aKey.theCharge;
0324   aGP->setAttribute(_toDOMS("iCharge"), _toDOMS(stringStr.str()));
0325   /////////////////
0326   ///Write results details for this GP
0327   {
0328     xercesc::DOMElement* aRefLayer = theDoc->createElement(_toDOMS("Result"));
0329     stringStr.str("");
0330     stringStr << aResult.getRefLayer();
0331     aRefLayer->setAttribute(_toDOMS("iRefLayer"), _toDOMS(stringStr.str()));
0332     stringStr.str("");
0333     stringStr << iRegion;
0334     aRefLayer->setAttribute(_toDOMS("iRegion"), _toDOMS(stringStr.str()));
0335     stringStr.str("");
0336     stringStr << myOMTFConfig->getRefToLogicNumber()[aResult.getRefLayer()];
0337     aRefLayer->setAttribute(_toDOMS("iLogicLayer"), _toDOMS(stringStr.str()));
0338     for (unsigned int iLogicLayer = 0; iLogicLayer < myOMTFConfig->nLayers(); ++iLogicLayer) {
0339       xercesc::DOMElement* aLayer = theDoc->createElement(_toDOMS("Layer"));
0340       stringStr.str("");
0341       stringStr << iLogicLayer;
0342       aLayer->setAttribute(_toDOMS("iLayer"), _toDOMS(stringStr.str()));
0343       stringStr.str("");
0344       stringStr << aResult.getStubResults()[iLogicLayer].getPdfVal();
0345       aLayer->setAttribute(_toDOMS("value"), _toDOMS(stringStr.str()));
0346       if (aResult.getStubResults()[iLogicLayer].getPdfVal())
0347         aRefLayer->appendChild(aLayer);
0348     }
0349     if (aRefLayer->getChildNodes()->getLength())
0350       aGP->appendChild(aRefLayer);
0351   }
0352   if (aGP->getChildNodes()->getLength())
0353     aTopElement->appendChild(aGP);
0354 }
0355 //////////////////////////////////////////////////
0356 //////////////////////////////////////////////////
0357 void XMLConfigWriter::writeGPData(GoldenPattern& aGP) {
0358   std::ostringstream stringStr;
0359   xercesc::DOMElement *aLayer = nullptr, *aRefLayer = nullptr, *aPdf = nullptr;
0360 
0361   xercesc::DOMElement* aGPElement = theDoc->createElement(_toDOMS("GP"));
0362   stringStr.str("");
0363   stringStr << aGP.key().thePt;
0364   aGPElement->setAttribute(_toDOMS("iPt"), _toDOMS(stringStr.str()));
0365   stringStr.str("");
0366   //stringStr<<aGP.key().theEtaCode;
0367   stringStr << "0";  //No eta code at the moment
0368   aGPElement->setAttribute(_toDOMS("iEta"), _toDOMS(stringStr.str()));
0369   stringStr.str("");
0370   stringStr << 0;  //No phi code is assigned to GP for the moment.
0371   aGPElement->setAttribute(_toDOMS("iPhi"), _toDOMS(stringStr.str()));
0372   stringStr.str("");
0373   stringStr << aGP.key().theCharge;
0374   aGPElement->setAttribute(_toDOMS("iCharge"), _toDOMS(stringStr.str()));
0375 
0376   for (unsigned int iLayer = 0; iLayer < myOMTFConfig->nLayers(); ++iLayer) {
0377     int nOfPhis = 0;
0378     /////////////////////////////////////
0379     aLayer = theDoc->createElement(_toDOMS("Layer"));
0380     stringStr.str("");
0381     stringStr << iLayer;
0382     //////////////////////////////////
0383     aLayer->setAttribute(_toDOMS("iLayer"), _toDOMS(stringStr.str()));
0384     stringStr.str("");
0385     stringStr << nOfPhis;
0386     aLayer->setAttribute(_toDOMS("nOfPhis"), _toDOMS(stringStr.str()));
0387     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0388       aRefLayer = theDoc->createElement(_toDOMS("RefLayer"));
0389       int meanDistPhi0 = aGP.getMeanDistPhi()[iLayer][iRefLayer][0];
0390       stringStr.str("");
0391       stringStr << meanDistPhi0;
0392       aRefLayer->setAttribute(_toDOMS("meanDistPhi0"), _toDOMS(stringStr.str()));
0393 
0394       int meanDistPhi1 = aGP.getMeanDistPhi()[iLayer][iRefLayer][1];
0395       stringStr.str("");
0396       stringStr << meanDistPhi1;
0397       aRefLayer->setAttribute(_toDOMS("meanDistPhi1"), _toDOMS(stringStr.str()));
0398 
0399       int selDistPhi = 0;
0400       stringStr.str("");
0401       stringStr << selDistPhi;
0402       aRefLayer->setAttribute(_toDOMS("selDistPhi"), _toDOMS(stringStr.str()));
0403 
0404       int selDistPhiShift =
0405           aGP.getDistPhiBitShift(iLayer, iRefLayer);  //TODO check if Wojtek expects it here or on the distMsbPhiShift
0406       stringStr.str("");
0407       stringStr << selDistPhiShift;
0408       aRefLayer->setAttribute(_toDOMS("selDistPhiShift"), _toDOMS(stringStr.str()));
0409 
0410       int distMsbPhiShift = 0;
0411       stringStr.str("");
0412       stringStr << distMsbPhiShift;
0413       aRefLayer->setAttribute(_toDOMS("distMsbPhiShift"), _toDOMS(stringStr.str()));
0414       aLayer->appendChild(aRefLayer);
0415     }
0416     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0417       for (unsigned int iPdf = 0; iPdf < exp2(myOMTFConfig->nPdfAddrBits()); ++iPdf) {
0418         aPdf = theDoc->createElement(_toDOMS("PDF"));
0419         stringStr.str("");
0420         stringStr << aGP.pdfValue(iLayer, iRefLayer, iPdf);
0421         aPdf->setAttribute(_toDOMS("value"), _toDOMS(stringStr.str()));
0422         aLayer->appendChild(aPdf);
0423       }
0424     }
0425     aGPElement->appendChild(aLayer);
0426   }
0427   theTopElement->appendChild(aGPElement);
0428 }
0429 //////////////////////////////////////////////////
0430 //////////////////////////////////////////////////
0431 void XMLConfigWriter::writeGPData(GoldenPattern* aGP1, GoldenPattern* aGP2, GoldenPattern* aGP3, GoldenPattern* aGP4) {
0432   std::ostringstream stringStr;
0433   auto setAttributeInt = [&](xercesc::DOMElement* domElement, std::string name, int value) -> void {
0434     stringStr << value;
0435     domElement->setAttribute(_toDOMS(name), _toDOMS(stringStr.str()));
0436     stringStr.str("");
0437   };
0438 
0439   auto setAttributeFloat = [&](xercesc::DOMElement* domElement, std::string name, float value) -> void {
0440     stringStr << value;
0441     domElement->setAttribute(_toDOMS(name), _toDOMS(stringStr.str()));
0442     stringStr.str("");
0443   };
0444 
0445   //xercesc::DOMElement *aLayer=nullptr, *aRefLayer=nullptr, *aPdf=nullptr;
0446 
0447   xercesc::DOMElement* aGPElement = theDoc->createElement(_toDOMS("GP"));
0448 
0449   setAttributeInt(aGPElement, "iPt1", aGP1->key().thePt);
0450   setAttributeInt(aGPElement, "iPt2", aGP2->key().thePt);
0451   setAttributeInt(aGPElement, "iPt3", aGP3->key().thePt);
0452   setAttributeInt(aGPElement, "iPt4", aGP4->key().thePt);
0453 
0454   if (writePdfThresholds) {
0455     if (dynamic_cast<const GoldenPatternWithThresh*>(aGP1) != nullptr) {
0456       for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0457         //cout<<__FUNCTION__<<":"<<__LINE__<<std::endl;
0458         xercesc::DOMElement* aRefLayerThresh = theDoc->createElement(_toDOMS("RefLayerThresh"));
0459         setAttributeFloat(
0460             aRefLayerThresh, "tresh1", dynamic_cast<const GoldenPatternWithThresh*>(aGP1)->getThreshold(iRefLayer));
0461         setAttributeFloat(
0462             aRefLayerThresh, "tresh2", dynamic_cast<const GoldenPatternWithThresh*>(aGP2)->getThreshold(iRefLayer));
0463         setAttributeFloat(
0464             aRefLayerThresh, "tresh3", dynamic_cast<const GoldenPatternWithThresh*>(aGP3)->getThreshold(iRefLayer));
0465         setAttributeFloat(
0466             aRefLayerThresh, "tresh4", dynamic_cast<const GoldenPatternWithThresh*>(aGP4)->getThreshold(iRefLayer));
0467 
0468         aGPElement->appendChild(aRefLayerThresh);
0469       }
0470     }
0471   }
0472 
0473   setAttributeInt(aGPElement, "iEta", 0);  //aGP1.key().theEtaCode; //No eta code at the moment
0474 
0475   setAttributeInt(aGPElement, "iPhi", 0);  //No phi code is assigned to GP for the moment.
0476 
0477   setAttributeInt(aGPElement, "iCharge", aGP1->key().theCharge);
0478 
0479   for (unsigned int iLayer = 0; iLayer < myOMTFConfig->nLayers(); ++iLayer) {
0480     int nOfPhis = 0;
0481     /////////////////////////////////////
0482     xercesc::DOMElement* aLayer = theDoc->createElement(_toDOMS("Layer"));
0483 
0484     setAttributeInt(aLayer, "iLayer", iLayer);
0485     setAttributeInt(aLayer, "nOfPhis", nOfPhis);
0486 
0487     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0488       xercesc::DOMElement* aRefLayer = theDoc->createElement(_toDOMS("RefLayer"));
0489 
0490       if (writeMeanDistPhi1) {
0491         int meanDistPhi0 = aGP1->getMeanDistPhi()[iLayer][iRefLayer][0];
0492         setAttributeInt(aRefLayer, "meanDistPhi0", meanDistPhi0);
0493 
0494         int meanDistPhi1 = aGP1->getMeanDistPhi()[iLayer][iRefLayer][1];
0495         setAttributeInt(aRefLayer, "meanDistPhi1", meanDistPhi1);
0496       } else {
0497         int meanDistPhi = aGP1->getMeanDistPhi()[iLayer][iRefLayer][0];
0498         setAttributeInt(aRefLayer, "meanDistPhi", meanDistPhi);
0499       }
0500 
0501       int selDistPhi = 0;
0502       setAttributeInt(aRefLayer, "selDistPhi", selDistPhi);
0503 
0504       int selDistPhiShift = aGP1->getDistPhiBitShift(
0505           iLayer, iRefLayer);  //TODO check if Wojtek expects it here or on the distMsbPhiShift;
0506       setAttributeInt(aRefLayer, "selDistPhiShift", selDistPhiShift);
0507 
0508       int distMsbPhiShift = 0;
0509       setAttributeInt(aRefLayer, "distMsbPhiShift", distMsbPhiShift);
0510 
0511       aLayer->appendChild(aRefLayer);
0512     }
0513     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0514       for (unsigned int iPdf = 0; iPdf < exp2(myOMTFConfig->nPdfAddrBits()); ++iPdf) {
0515         xercesc::DOMElement* aPdf = theDoc->createElement(_toDOMS("PDF"));
0516 
0517         setAttributeFloat(aPdf, "value1", aGP1->pdfValue(iLayer, iRefLayer, iPdf));
0518         setAttributeFloat(aPdf, "value2", aGP2->pdfValue(iLayer, iRefLayer, iPdf));
0519         setAttributeFloat(aPdf, "value3", aGP3->pdfValue(iLayer, iRefLayer, iPdf));
0520         setAttributeFloat(aPdf, "value4", aGP4->pdfValue(iLayer, iRefLayer, iPdf));
0521 
0522         aLayer->appendChild(aPdf);
0523       }
0524     }
0525     aGPElement->appendChild(aLayer);
0526   }
0527   theTopElement->appendChild(aGPElement);
0528 }
0529 //////////////////////////////////////////////////
0530 //////////////////////////////////////////////////
0531 template <class GoldenPatternType>
0532 void XMLConfigWriter::writeGPs(const GoldenPatternVec<GoldenPatternType>& goldenPats, std::string fName) {
0533   initialiseXMLDocument("OMTF");
0534   GoldenPattern* dummy = new GoldenPatternWithThresh(Key(0, 0, 0), myOMTFConfig);
0535 
0536   OMTFConfiguration::vector2D mergedPartters = myOMTFConfig->getPatternGroups(goldenPats);
0537   for (unsigned int iGroup = 0; iGroup < mergedPartters.size(); iGroup++) {
0538     std::vector<GoldenPattern*> gps(4, dummy);
0539     for (unsigned int i = 0; i < mergedPartters[iGroup].size(); i++) {
0540       GoldenPattern* gp = dynamic_cast<GoldenPattern*>(goldenPats.at(mergedPartters[iGroup][i]).get());
0541       if (!gp) {
0542         throw cms::Exception("OMTF::XMLConfigWriter::writeGPs: the gps are not GoldenPatterns ");
0543       }
0544       /*cout<<gp->key()<<endl;;
0545       for(unsigned int iLayer = 0; iLayer<myOMTFConfig->nLayers(); ++iLayer) {
0546         for(unsigned int iRefLayer=0; iRefLayer<myOMTFConfig->nRefLayers(); ++iRefLayer) {
0547           if(gp->getPdf()[iLayer][iRefLayer][0] != 0) {
0548             cout<<"iLayer "<<iLayer<<" iRefLayer "<<iRefLayer<<" pdf[0] "<<gp->getPdf()[iLayer][iRefLayer][0]<<"!!!!!!!!!!!!!!!!!!!!\n";
0549           }
0550         }
0551       }*/
0552       gps[i] = gp;
0553     }
0554     writeGPData(gps[0], gps[1], gps[2], gps[3]);
0555   }
0556   finaliseXMLDocument(fName);
0557 }
0558 //////////////////////////////////////////////////
0559 //////////////////////////////////////////////////
0560 void XMLConfigWriter::writeConnectionsData(
0561     const std::vector<std::vector<OMTFConfiguration::vector2D> >& measurements4D) {
0562   std::ostringstream stringStr;
0563 
0564   for (unsigned int iProcessor = 0; iProcessor < 6; ++iProcessor) {
0565     xercesc::DOMElement* aProcessorElement = theDoc->createElement(_toDOMS("Processor"));
0566     stringStr.str("");
0567     stringStr << iProcessor;
0568     aProcessorElement->setAttribute(_toDOMS("iProcessor"), _toDOMS(stringStr.str()));
0569     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0570       xercesc::DOMElement* aRefLayerElement = theDoc->createElement(_toDOMS("RefLayer"));
0571       stringStr.str("");
0572       stringStr << iRefLayer;
0573       aRefLayerElement->setAttribute(_toDOMS("iRefLayer"), _toDOMS(stringStr.str()));
0574       stringStr.str("");
0575       stringStr << myOMTFConfig->getProcessorPhiVsRefLayer()[iProcessor][iRefLayer];
0576       aRefLayerElement->setAttribute(_toDOMS("iGlobalPhiStart"), _toDOMS(stringStr.str()));
0577       aProcessorElement->appendChild(aRefLayerElement);
0578     }
0579     unsigned int iRefHit = 0;
0580     ///////
0581     for (unsigned int iRefLayer = 0; iRefLayer < myOMTFConfig->nRefLayers(); ++iRefLayer) {
0582       for (unsigned int iRegion = 0; iRegion < 6; ++iRegion) {
0583         unsigned int maxHitCount = 0;
0584         for (unsigned int iInput = 0; iInput < 14; ++iInput) {
0585           if ((int)maxHitCount < myOMTFConfig->getMeasurements4Dref()[iProcessor][iRegion][iRefLayer][iInput])
0586             maxHitCount = myOMTFConfig->getMeasurements4Dref()[iProcessor][iRegion][iRefLayer][iInput];
0587         }
0588         for (unsigned int iInput = 0; iInput < 14; ++iInput) {
0589           unsigned int hitCount = myOMTFConfig->getMeasurements4Dref()[iProcessor][iRegion][iRefLayer][iInput];
0590           if (hitCount < maxHitCount * 0.1)
0591             continue;
0592           xercesc::DOMElement* aRefHitElement = theDoc->createElement(_toDOMS("RefHit"));
0593           stringStr.str("");
0594           stringStr << iRefHit;
0595           aRefHitElement->setAttribute(_toDOMS("iRefHit"), _toDOMS(stringStr.str()));
0596           stringStr.str("");
0597           stringStr << iRefLayer;
0598           aRefHitElement->setAttribute(_toDOMS("iRefLayer"), _toDOMS(stringStr.str()));
0599 
0600           stringStr.str("");
0601           stringStr << iRegion;
0602           aRefHitElement->setAttribute(_toDOMS("iRegion"), _toDOMS(stringStr.str()));
0603 
0604           stringStr.str("");
0605           stringStr << iInput;
0606           aRefHitElement->setAttribute(_toDOMS("iInput"), _toDOMS(stringStr.str()));
0607           unsigned int logicRegionSize = 10 / 360.0 * myOMTFConfig->nPhiBins();
0608           int lowScaleEnd = std::pow(2, myOMTFConfig->nPhiBits() - 1);
0609           ///iPhiMin and iPhiMax are expressed in n bit scale -2**n, +2**2-1 used in each processor
0610           int iPhiMin = myOMTFConfig->getProcessorPhiVsRefLayer()[iProcessor][iRefLayer] -
0611                         myOMTFConfig->globalPhiStart(iProcessor) - lowScaleEnd;
0612           int iPhiMax = iPhiMin + logicRegionSize - 1;
0613 
0614           iPhiMin += iRegion * logicRegionSize;
0615           iPhiMax += iRegion * logicRegionSize;
0616 
0617           stringStr.str("");
0618           stringStr << iPhiMin;
0619           aRefHitElement->setAttribute(_toDOMS("iPhiMin"), _toDOMS(stringStr.str()));
0620 
0621           stringStr.str("");
0622           stringStr << iPhiMax;
0623           aRefHitElement->setAttribute(_toDOMS("iPhiMax"), _toDOMS(stringStr.str()));
0624           if (iRefHit < myOMTFConfig->nRefHits())
0625             aProcessorElement->appendChild(aRefHitElement);
0626           ++iRefHit;
0627         }
0628         for (; iRegion == 5 && iRefLayer == 7 && iRefHit < myOMTFConfig->nRefHits(); ++iRefHit) {
0629           xercesc::DOMElement* aRefHitElement = theDoc->createElement(_toDOMS("RefHit"));
0630           stringStr.str("");
0631           stringStr << iRefHit;
0632           aRefHitElement->setAttribute(_toDOMS("iRefHit"), _toDOMS(stringStr.str()));
0633           stringStr.str("");
0634           stringStr << 0;
0635           aRefHitElement->setAttribute(_toDOMS("iRefLayer"), _toDOMS(stringStr.str()));
0636 
0637           stringStr.str("");
0638           stringStr << 0;
0639           aRefHitElement->setAttribute(_toDOMS("iRegion"), _toDOMS(stringStr.str()));
0640 
0641           stringStr.str("");
0642           stringStr << 0;
0643           aRefHitElement->setAttribute(_toDOMS("iInput"), _toDOMS(stringStr.str()));
0644 
0645           int iPhiMin = 0;
0646           int iPhiMax = 1;
0647 
0648           stringStr.str("");
0649           stringStr << iPhiMin;
0650           aRefHitElement->setAttribute(_toDOMS("iPhiMin"), _toDOMS(stringStr.str()));
0651 
0652           stringStr.str("");
0653           stringStr << iPhiMax;
0654           aRefHitElement->setAttribute(_toDOMS("iPhiMax"), _toDOMS(stringStr.str()));
0655 
0656           aProcessorElement->appendChild(aRefHitElement);
0657         }
0658       }
0659     }
0660     ////
0661     for (unsigned int iRegion = 0; iRegion < 6; ++iRegion) {
0662       xercesc::DOMElement* aRegionElement = theDoc->createElement(_toDOMS("LogicRegion"));
0663       stringStr.str("");
0664       stringStr << iRegion;
0665       aRegionElement->setAttribute(_toDOMS("iRegion"), _toDOMS(stringStr.str()));
0666 
0667       for (unsigned int iLogicLayer = 0; iLogicLayer < myOMTFConfig->nLayers(); ++iLogicLayer) {
0668         xercesc::DOMElement* aLayerElement = theDoc->createElement(_toDOMS("Layer"));
0669         stringStr.str("");
0670         stringStr << iLogicLayer;
0671         ////////////////////////////////////////////////
0672         aLayerElement->setAttribute(_toDOMS("iLayer"), _toDOMS(stringStr.str()));
0673         const OMTFConfiguration::vector1D& myCounts =
0674             myOMTFConfig->getMeasurements4D()[iProcessor][iRegion][iLogicLayer];
0675         unsigned int maxInput = findMaxInput(myCounts);
0676         unsigned int begin = 0, end = 0;
0677         if ((int)maxInput - 2 >= 0)
0678           begin = maxInput - 2;
0679         else
0680           begin = maxInput;
0681         end = maxInput + 3;
0682         stringStr.str("");
0683         stringStr << begin;
0684         aLayerElement->setAttribute(_toDOMS("iFirstInput"), _toDOMS(stringStr.str()));
0685         stringStr.str("");
0686         stringStr << end - begin + 1;
0687         aLayerElement->setAttribute(_toDOMS("nInputs"), _toDOMS(stringStr.str()));
0688         aRegionElement->appendChild(aLayerElement);
0689       }
0690       aProcessorElement->appendChild(aRegionElement);
0691     }
0692     theTopElement->appendChild(aProcessorElement);
0693   }
0694 }
0695 //////////////////////////////////////////////////
0696 //////////////////////////////////////////////////
0697 unsigned int XMLConfigWriter::findMaxInput(const OMTFConfiguration::vector1D& myCounts) {
0698   unsigned int max = 0;
0699   unsigned int maxInput = 0;
0700   for (unsigned int iInput = 0; iInput < 14; ++iInput) {
0701     if (myCounts[iInput] > (int)max) {
0702       max = myCounts[iInput];
0703       maxInput = iInput;
0704     }
0705   }
0706   return maxInput;
0707 }
0708 /////////////////////////////////////////////////////////////
0709 /////////////////////////////////////////////////////////////
0710 template void XMLConfigWriter::writeGPs(const GoldenPatternVec<GoldenPattern>& goldenPats, std::string fName);
0711 
0712 template void XMLConfigWriter::writeGPs(const GoldenPatternVec<GoldenPatternWithStat>& goldenPats, std::string fName);
0713 
0714 template void XMLConfigWriter::writeGPs(const GoldenPatternVec<GoldenPatternWithThresh>& goldenPats, std::string fName);