Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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