Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:23:28

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