Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002  * \class L1GtVmeWriterCore
0003  *
0004  *
0005  * Description: core class to write the VME xml file.
0006  *
0007  * Implementation:
0008  *    Core class to write the VME xml file: L1GtVmeWriter is an EDM wrapper for this class.
0009  *    L1GtVmeWriterCore can also be used in L1 Trigger Supervisor framework,  with another
0010  *    wrapper - it must be therefore EDM-framework free.
0011  *
0012  * \author: Vasile Mihai Ghete - HEPHY Vienna
0013  *
0014  *
0015  */
0016 
0017 // this class header
0018 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVmeWriterCore.h"
0019 
0020 // system include files
0021 #include <iostream>
0022 #include <sstream>
0023 #include <bitset>
0024 
0025 // user include files
0026 //   base class
0027 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtXmlParserTags.h"
0028 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlDefinitions.h"
0029 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterCore.h"
0030 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
0031 #include "CondFormats/L1TObjects/interface/L1GtFwd.h"
0032 
0033 #include "CondFormats/L1TObjects/interface/L1GtCaloTemplate.h"
0034 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
0035 #include "CondFormats/L1TObjects/interface/L1GtEnergySumTemplate.h"
0036 #include "CondFormats/L1TObjects/interface/L1GtJetCountsTemplate.h"
0037 
0038 // constructor
0039 L1GtVmeWriterCore::L1GtVmeWriterCore(const std::string &outputDir, const std::string &vmeXmlFile)
0040     : m_outputDir(outputDir), m_vmeXmlFile(vmeXmlFile) {
0041   object2reg_[Mu] = 0x00A0000;
0042   object2reg_[NoIsoEG] = 0x0020000;
0043   object2reg_[IsoEG] = 0x0000000;
0044   object2reg_[ForJet] = 0x0080000;
0045   object2reg_[TauJet] = 0x0060000;
0046   object2reg_[CenJet] = 0x0040000;
0047   object2reg_[HTT] = 0x0100000;
0048   object2reg_[ETT] = 0x0100000;
0049   object2reg_[ETM] = 0x0100000;
0050 
0051   type2reg_[Type1s] = 0x000C000;
0052   type2reg_[Type2s] = 0x0004000;
0053   type2reg_[Type2wsc] = 0x0008000;
0054   type2reg_[Type3s] = 0x0010000;
0055   type2reg_[Type4s] = 0x0000000;
0056   type2reg_[TypeETM] = 0x0004000;
0057   type2reg_[TypeETT] = 0x0000000;
0058   type2reg_[TypeHTT] = 0x0008000;
0059 
0060   reg2hex_[m_xmlTagEtThreshold + "_1"] = 0x0000000;
0061   reg2hex_[m_xmlTagEtThreshold + "_2"] = 0x0000002;
0062   reg2hex_[m_xmlTagEtThreshold + "_3"] = 0x0000004;
0063   reg2hex_[m_xmlTagEtThreshold + "_4"] = 0x0000006;
0064   reg2hex_[m_xmlTagPtHighThreshold + "_1"] = 0x0000000;
0065   reg2hex_[m_xmlTagPtHighThreshold + "_2"] = 0x0000002;
0066   reg2hex_[m_xmlTagPtHighThreshold + "_3"] = 0x0000004;
0067   reg2hex_[m_xmlTagPtHighThreshold + "_4"] = 0x0000006;
0068   reg2hex_[m_xmlTagQuality + "_1"] = 0x0000050;
0069   reg2hex_[m_xmlTagQuality + "_2"] = 0x0000052;
0070   reg2hex_[m_xmlTagQuality + "_3"] = 0x0000054;
0071   reg2hex_[m_xmlTagQuality + "_4"] = 0x0000056;
0072   reg2hex_[m_xmlTagPtLowThreshold + "_1"] = 0x0000058;
0073   reg2hex_[m_xmlTagPtLowThreshold + "_2"] = 0x000005A;
0074   reg2hex_[m_xmlTagPtLowThreshold + "_3"] = 0x000005C;
0075   reg2hex_[m_xmlTagPtLowThreshold + "_4"] = 0x000005E;
0076   reg2hex_[m_xmlTagChargeCorrelation] = 0x000008A;
0077   reg2hex_["jet_cnt_threshold"] = 0x0000000;
0078   reg2hex_["threshold_lsb"] = 0x0000000;
0079   reg2hex_["threshold_msb"] = 0x0000002;
0080 
0081   // sepcial maps for jet counts
0082   jetType2reg_[0] = 0x0000000;
0083   jetType2reg_[1] = 0x0004000;
0084   jetType2reg_[2] = 0x0008000;
0085   jetType2reg_[3] = 0x000C000;
0086   jetType2reg_[4] = 0x0010000;
0087   jetType2reg_[5] = 0x0014000;
0088 
0089   jetObj2reg_[0] = 0x00C0000;
0090   jetObj2reg_[1] = 0x00E0000;
0091 
0092   spacesPerLevel_ = 2;
0093 }
0094 
0095 // destructor
0096 L1GtVmeWriterCore::~L1GtVmeWriterCore() {
0097   // empty now
0098 }
0099 
0100 int L1GtVmeWriterCore::condIndex2reg(const unsigned int &index) {
0101   int indexcp = index;
0102 
0103   return (indexcp << 8);
0104 }
0105 
0106 std::string L1GtVmeWriterCore::calculateJetsAddress(const int &countIndex, const int &obj, const int &index) {
0107   int result = jetType2reg_[countIndex] + jetObj2reg_[obj] + reg2hex_["jet_cnt_threshold"] + condIndex2reg(index);
0108 
0109   std::bitset<24> bs(result);
0110 
0111   std::ostringstream buffer;
0112   buffer << bs;
0113 
0114   return buffer.str();
0115 }
0116 
0117 std::string L1GtVmeWriterCore::calculateAddress(const L1GtObject &obj,
0118                                                 const L1GtConditionType &type,
0119                                                 const std::string &reg,
0120                                                 const int &index) {
0121   int result = object2reg_[obj] + type2reg_[type] + reg2hex_[reg] + condIndex2reg(index);
0122 
0123   std::bitset<25> bs(result);
0124 
0125   std::ostringstream buffer;
0126   buffer << bs;
0127   return buffer.str();
0128 }
0129 
0130 // destructor
0131 std::string L1GtVmeWriterCore::openTag(const std::string &tag) { return "<" + tag + ">\n"; }
0132 
0133 std::string L1GtVmeWriterCore::closeTag(const std::string &tag) { return "</" + tag + ">\n"; }
0134 
0135 std::string L1GtVmeWriterCore::vmeAddrValueBlock(const std::string &addr,
0136                                                  const int &val,
0137                                                  const int &spaceLevel,
0138                                                  const bool setMsb) {
0139   std::ostringstream buffer;
0140 
0141   std::bitset<8> bsVal(val);
0142 
0143   if (setMsb)
0144     bsVal.set(7);
0145 
0146   buffer << spaces(spaceLevel) << openTag(m_xmlTagVmeAddress) << spaces(spaceLevel + 1) << addr << std::endl
0147          << spaces(spaceLevel + 1) << openTag(m_xmlTagValue) << spaces(spaceLevel + 1) << bsVal << std::endl
0148          << spaces(spaceLevel + 1) << closeTag(m_xmlTagValue) << spaces(spaceLevel) << closeTag(m_xmlTagVmeAddress);
0149 
0150   return buffer.str();
0151 }
0152 
0153 std::string L1GtVmeWriterCore::spaces(const unsigned int &level) {
0154   std::ostringstream buffer;
0155 
0156   for (unsigned int i = 0; i < (level * spacesPerLevel_); i++) {
0157     buffer << " ";
0158   }
0159 
0160   return buffer.str();
0161 }
0162 
0163 void L1GtVmeWriterCore::writeVME(const std::vector<ConditionMap> &_conditionMap,
0164                                  const std::map<std::string, int> &_cond2intMap,
0165                                  const L1GtVhdlTemplateFile &_header,
0166                                  const int spacesPerLevel) {
0167   std::vector<ConditionMap> conditionMap = _conditionMap;
0168   std::map<std::string, int> cond2intMap = _cond2intMap;
0169   L1GtVhdlTemplateFile header = _header;
0170   L1GtVhdlDefinitions maps;
0171 
0172   // define appearance
0173   spacesPerLevel_ = spacesPerLevel;
0174 
0175   std::string fileName = m_outputDir + m_vmeXmlFile;
0176 
0177   // open output file
0178   std::ofstream outputFile(fileName.c_str());
0179 
0180   // begin of VME XML
0181   outputFile << "<" << m_xmlTagVme << ">" << std::endl;
0182 
0183   // insert header
0184   outputFile << spaces(1) << openTag(m_xmlTagHeader);
0185   header.substitute("vhdl_file_name", m_vmeXmlFile);
0186   header.removeEmptyLines();
0187 
0188   std::vector<std::string> lines = header.returnLines();
0189 
0190   for (unsigned int i = 0; i < lines.size(); i++) {
0191     outputFile << spaces(2) << lines.at(i);
0192     //if (i!=lines.size()-1)
0193     outputFile << std::endl;
0194   }
0195 
0196   outputFile << spaces(1) << closeTag(m_xmlTagHeader);
0197 
0198   // loop over chips
0199   for (unsigned int index = 1; index <= 2; index++) {
0200     outputFile << spaces(1) << "<" << m_xmlTagChip << index << ">" << std::endl;
0201 
0202     // loop over condition map
0203     for (ConditionMap::iterator iterCond = conditionMap.at(index - 1).begin();
0204          iterCond != conditionMap.at(index - 1).end();
0205          iterCond++) {
0206       // open a condition
0207       outputFile << spaces(2) << "<" << iterCond->first << " " << m_xmlConditionAttrObject << "=\""
0208                  << maps.obj2str((iterCond->second->objectType())[0]) << "\" " << m_xmlConditionAttrType << "=\""
0209                  << maps.type2str(iterCond->second->condType()) << "\">" << std::endl;
0210 
0211       switch ((iterCond->second)->condCategory()) {
0212         case CondMuon: {
0213           L1GtMuonTemplate *muonTemplate = static_cast<L1GtMuonTemplate *>(iterCond->second);
0214           const std::vector<L1GtMuonTemplate::ObjectParameter> *op = muonTemplate->objectParameter();
0215 
0216           // get the number of objects
0217           unsigned int nObjects = iterCond->second->nrObjects();
0218 
0219           for (unsigned int i = 0; i < nObjects; i++) {
0220             std::string opi = L1GtVhdlWriterCore::int2str(i);
0221 
0222             // ptHighTreshold
0223             std::string address = calculateAddress(Mu,
0224                                                    (iterCond->second)->condType(),
0225                                                    (m_xmlTagPtHighThreshold + "_" + opi),
0226                                                    cond2intMap[iterCond->first]);
0227 
0228             outputFile << spaces(3) << openTag(m_xmlTagPtHighThreshold)
0229                        << vmeAddrValueBlock(address, (*op).at(i).ptHighThreshold, 4, iterCond->second->condGEq())
0230                        << spaces(3) << closeTag(m_xmlTagPtHighThreshold);
0231 
0232             // ptLow Threshold
0233             address = calculateAddress(
0234                 Mu, (iterCond->second)->condType(), (m_xmlTagPtLowThreshold + "_" + opi), cond2intMap[iterCond->first]);
0235 
0236             outputFile << spaces(3) << openTag(m_xmlTagPtLowThreshold)
0237                        << vmeAddrValueBlock(address, (*op).at(i).ptLowThreshold, 4, iterCond->second->condGEq())
0238                        << spaces(3) << closeTag(m_xmlTagPtLowThreshold);
0239 
0240             // Quality
0241             address = calculateAddress(
0242                 Mu, (iterCond->second)->condType(), (m_xmlTagQuality + "_" + opi), cond2intMap[iterCond->first]);
0243 
0244             outputFile << spaces(3) << openTag(m_xmlTagQuality)
0245                        << vmeAddrValueBlock(address, (*op).at(i).qualityRange, 4) << spaces(3)
0246                        << closeTag(m_xmlTagQuality);
0247           }
0248 
0249           const L1GtMuonTemplate::CorrelationParameter *cp = muonTemplate->correlationParameter();
0250 
0251           // Charage correlation -  occurs only one time
0252           outputFile << spaces(3) << openTag(m_xmlTagChargeCorrelation);
0253 
0254           std::string address = calculateAddress(
0255               Mu, (iterCond->second)->condType(), (m_xmlTagChargeCorrelation), cond2intMap[iterCond->first]);
0256 
0257           outputFile << vmeAddrValueBlock(address, (*cp).chargeCorrelation, 4) << spaces(3)
0258                      << closeTag(m_xmlTagChargeCorrelation);
0259 
0260         } break;
0261 
0262         case CondCalo: {
0263           L1GtCaloTemplate *m_gtCaloTemplate = static_cast<L1GtCaloTemplate *>(iterCond->second);
0264           const std::vector<L1GtCaloTemplate::ObjectParameter> *op = m_gtCaloTemplate->objectParameter();
0265 
0266           // get the number of objects
0267           unsigned int nObjects = iterCond->second->nrObjects();
0268 
0269           for (unsigned int i = 0; i < nObjects; i++) {
0270             std::string opi = L1GtVhdlWriterCore::int2str(i);
0271             std::string address = calculateAddress((iterCond->second->objectType()).at(0),
0272                                                    (iterCond->second)->condType(),
0273                                                    (m_xmlTagPtHighThreshold + "_" + opi),
0274                                                    cond2intMap[iterCond->first]);
0275 
0276             // insert Address/Value Tag
0277             outputFile << vmeAddrValueBlock(address, (*op).at(i).etThreshold, 3, iterCond->second->condGEq());
0278           }
0279 
0280         } break;
0281 
0282         case CondEnergySum: {
0283           L1GtEnergySumTemplate *energySumTempl = static_cast<L1GtEnergySumTemplate *>(iterCond->second);
0284 
0285           const std::vector<L1GtEnergySumTemplate::ObjectParameter> *op = energySumTempl->objectParameter();
0286 
0287           // get the number of objects
0288           unsigned int nObjects = iterCond->second->nrObjects();
0289 
0290           for (unsigned int i = 0; i < nObjects; i++) {
0291             std::string opi = L1GtVhdlWriterCore::int2str(i);
0292 
0293             std::string address = calculateAddress((iterCond->second->objectType()).at(0),
0294                                                    (iterCond->second)->condType(),
0295                                                    (m_xmlTagPtHighThreshold + "_" + opi),
0296                                                    cond2intMap[iterCond->first]);
0297 
0298             // insert Address/Value Tag
0299             outputFile << vmeAddrValueBlock(address, (*op).at(i).etThreshold, 3, iterCond->second->condGEq());
0300           }
0301 
0302         }
0303 
0304         break;
0305 
0306         case CondJetCounts: {
0307           L1GtJetCountsTemplate *jetsTemplate = static_cast<L1GtJetCountsTemplate *>(iterCond->second);
0308           const std::vector<L1GtJetCountsTemplate::ObjectParameter> *op = jetsTemplate->objectParameter();
0309 
0310           // get the number of objects
0311           // unsigned int nObjects = iterCond->second->nrObjects();
0312 
0313           // count index
0314           int ci = (*op)[0].countIndex;
0315 
0316           // 0 for count index 0-5 and 1 for count index 6 - 11
0317           int obj;
0318 
0319           if (ci <= 5)
0320             obj = 0;
0321           else
0322             obj = 1;
0323 
0324           std::string address = calculateJetsAddress(ci, obj, cond2intMap[iterCond->first]);
0325 
0326           outputFile << vmeAddrValueBlock(address, (*op).at(0).countThreshold, 3, iterCond->second->condGEq());
0327 
0328         } break;
0329         case CondCorrelation: {
0330           // empty
0331         } break;
0332 
0333         case CondNull: {
0334           // empty
0335         } break;
0336 
0337         default: {
0338           // empty
0339         } break;
0340       }
0341 
0342       // close the condition
0343       outputFile << spaces(2) << closeTag(iterCond->first);
0344     }
0345 
0346     outputFile << spaces(1) << "</" << m_xmlTagChip << index << ">" << std::endl;
0347   }
0348 
0349   // end of vme XML
0350   outputFile << closeTag(m_xmlTagVme);
0351 
0352   // close output file
0353   outputFile.close();
0354 
0355   std::cout << std::endl
0356             << "*******   VME XML File: " << m_outputDir << m_vmeXmlFile << " written sucessfully!  *******"
0357             << std::endl;
0358 }