CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/L1TriggerConfig/L1GtConfigProducers/src/L1GtVmeWriterCore.cc

Go to the documentation of this file.
00001 
00019 // this class header
00020 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVmeWriterCore.h"
00021 
00022 // system include files
00023 #include <iostream>
00024 #include <sstream>
00025 #include <bitset>
00026 
00027 // user include files
00028 //   base class
00029 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtXmlParserTags.h"
00030 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlDefinitions.h"
00031 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterCore.h"
00032 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
00033 #include "CondFormats/L1TObjects/interface/L1GtFwd.h"
00034 
00035 #include "CondFormats/L1TObjects/interface/L1GtCaloTemplate.h"
00036 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
00037 #include "CondFormats/L1TObjects/interface/L1GtEnergySumTemplate.h"
00038 #include "CondFormats/L1TObjects/interface/L1GtJetCountsTemplate.h"
00039 
00040 // constructor
00041 L1GtVmeWriterCore::L1GtVmeWriterCore(const std::string& outputDir,
00042         const std::string& vmeXmlFile) :
00043     m_outputDir(outputDir), m_vmeXmlFile(vmeXmlFile)
00044 {
00045 
00046     object2reg_[Mu]=0x00A0000;
00047     object2reg_[NoIsoEG]=0x0020000;
00048     object2reg_[IsoEG]=0x0000000;
00049     object2reg_[ForJet]=0x0080000;
00050     object2reg_[TauJet]=0x0060000;
00051     object2reg_[CenJet]=0x0040000;
00052     object2reg_[HTT]=0x0100000;
00053     object2reg_[ETT]=0x0100000;
00054     object2reg_[ETM]=0x0100000;
00055 
00056     type2reg_[Type1s]=0x000C000;
00057     type2reg_[Type2s]=0x0004000;
00058     type2reg_[Type2wsc]=0x0008000;
00059     type2reg_[Type3s]=0x0010000;
00060     type2reg_[Type4s]=0x0000000;
00061     type2reg_[TypeETM]=0x0004000;
00062     type2reg_[TypeETT]=0x0000000;
00063     type2reg_[TypeHTT]=0x0008000;
00064 
00065     reg2hex_[m_xmlTagEtThreshold+"_1"]=0x0000000;
00066     reg2hex_[m_xmlTagEtThreshold+"_2"]=0x0000002;
00067     reg2hex_[m_xmlTagEtThreshold+"_3"]=0x0000004;
00068     reg2hex_[m_xmlTagEtThreshold+"_4"]=0x0000006;
00069     reg2hex_[m_xmlTagPtHighThreshold+"_1"]=0x0000000;
00070     reg2hex_[m_xmlTagPtHighThreshold+"_2"]=0x0000002;
00071     reg2hex_[m_xmlTagPtHighThreshold+"_3"]=0x0000004;
00072     reg2hex_[m_xmlTagPtHighThreshold+"_4"]=0x0000006;
00073     reg2hex_[m_xmlTagQuality+"_1"]=0x0000050;
00074     reg2hex_[m_xmlTagQuality+"_2"]=0x0000052;
00075     reg2hex_[m_xmlTagQuality+"_3"]=0x0000054;
00076     reg2hex_[m_xmlTagQuality+"_4"]=0x0000056;
00077     reg2hex_[m_xmlTagPtLowThreshold+"_1"]=0x0000058;
00078     reg2hex_[m_xmlTagPtLowThreshold+"_2"]=0x000005A;
00079     reg2hex_[m_xmlTagPtLowThreshold+"_3"]=0x000005C;
00080     reg2hex_[m_xmlTagPtLowThreshold+"_4"]=0x000005E;
00081     reg2hex_[m_xmlTagChargeCorrelation]=0x000008A;
00082     reg2hex_["jet_cnt_threshold"]=0x0000000;
00083     reg2hex_["threshold_lsb"]=0x0000000;
00084     reg2hex_["threshold_msb"]=0x0000002;
00085 
00086     // sepcial maps for jet counts
00087     jetType2reg_[0]=0x0000000;
00088     jetType2reg_[1]=0x0004000;
00089     jetType2reg_[2]=0x0008000;
00090     jetType2reg_[3]=0x000C000;
00091     jetType2reg_[4]=0x0010000;
00092     jetType2reg_[5]=0x0014000;
00093 
00094     jetObj2reg_[0]=0x00C0000;
00095     jetObj2reg_[1]=0x00E0000;
00096     
00097     spacesPerLevel_ = 2;
00098 
00099 }
00100 
00101 // destructor
00102 L1GtVmeWriterCore::~L1GtVmeWriterCore()
00103 {
00104 
00105     // empty now
00106 
00107 }
00108 
00109 int L1GtVmeWriterCore::condIndex2reg(const unsigned int &index)
00110 {
00111     int indexcp = index;
00112 
00113     return (indexcp << 8);
00114 
00115 }
00116 
00117 std::string L1GtVmeWriterCore::calculateJetsAddress(const int &countIndex,
00118         const int &obj, const int &index)
00119 {
00120     int result = jetType2reg_[countIndex] + jetObj2reg_[obj]
00121             + reg2hex_["jet_cnt_threshold"] +condIndex2reg(index);
00122 
00123     std::bitset<24> bs(result);
00124 
00125     std::ostringstream buffer;
00126     buffer<<bs;
00127 
00128     return buffer.str();
00129 }
00130 
00131 std::string L1GtVmeWriterCore::calculateAddress(const L1GtObject &obj,
00132         const L1GtConditionType &type, const std::string &reg, const int &index)
00133 {
00134 
00135     int result = object2reg_[obj]+ type2reg_[type]+ reg2hex_[reg]
00136             +condIndex2reg(index);
00137 
00138     std::bitset<25> bs(result);
00139 
00140     std::ostringstream buffer;
00141     buffer<<bs;
00142     return buffer.str();
00143 
00144 }
00145 
00146 // destructor
00147 std::string L1GtVmeWriterCore::openTag(const std::string &tag)
00148 {
00149     return "<"+tag+">\n";
00150 }
00151 
00152 std::string L1GtVmeWriterCore::closeTag(const std::string &tag)
00153 {
00154     return "</"+tag+">\n";
00155 }
00156 
00157 std::string L1GtVmeWriterCore::vmeAddrValueBlock(const std::string &addr,
00158         const int &val, const int &spaceLevel, const bool setMsb)
00159 {
00160     std::ostringstream buffer;
00161 
00162     std::bitset<8> bsVal(val);
00163 
00164     if (setMsb)
00165         bsVal.set(7);
00166 
00167     buffer << spaces(spaceLevel) << openTag(m_xmlTagVmeAddress)
00168             << spaces(spaceLevel+1) << addr << std::endl
00169             << spaces(spaceLevel+1) << openTag(m_xmlTagValue)
00170             << spaces(spaceLevel+1) << bsVal <<std::endl
00171             << spaces(spaceLevel+1) << closeTag(m_xmlTagValue)
00172             << spaces(spaceLevel) << closeTag(m_xmlTagVmeAddress);
00173 
00174     return buffer.str();
00175 
00176 }
00177 
00178 std::string L1GtVmeWriterCore::spaces(const unsigned int &level)
00179 {
00180     std::ostringstream buffer;
00181 
00182     for (unsigned int i=0; i<(level*spacesPerLevel_); i++)
00183     {
00184         buffer<<" ";
00185     }
00186 
00187     return buffer.str();
00188 
00189 }
00190 
00191 void L1GtVmeWriterCore::writeVME(const std::vector<ConditionMap> &conditionMap,
00192         std::map<std::string,int> cond2intMap, L1GtVhdlTemplateFile header, const int spacesPerLevel)
00193 {
00194 
00195     L1GtVhdlDefinitions maps;
00196     
00197     // define appearance
00198     spacesPerLevel_ = spacesPerLevel;
00199 
00200     std::string fileName = m_outputDir+m_vmeXmlFile;
00201 
00202     // open output file
00203     std::ofstream outputFile(fileName.c_str());
00204 
00205     // begin of VME XML
00206     outputFile << "<" << m_xmlTagVme << ">" << std::endl;
00207 
00208     // insert header
00209     outputFile << spaces(1) << openTag(m_xmlTagHeader);
00210     header.substitute("vhdl_file_name", m_vmeXmlFile);
00211     header.removeEmptyLines();
00212 
00213     std::vector<std::string> lines = header.returnLines();
00214 
00215     for (unsigned int i = 0; i< lines.size(); i++)
00216     {
00217         outputFile << spaces(2)<<lines.at(i);
00218         //if (i!=lines.size()-1)
00219         outputFile<<std::endl;
00220     }
00221 
00222     outputFile << spaces(1) << closeTag(m_xmlTagHeader);
00223 
00224     // loop over chips
00225     for (unsigned int index=1; index<=2; index++)
00226     {
00227         outputFile << spaces(1) <<"<" << m_xmlTagChip << index <<">"
00228                 << std::endl;
00229 
00230         // loop over condition map
00231         for (ConditionMap::const_iterator iterCond = conditionMap.at(index-1).begin(); iterCond != conditionMap.at(index-1).end(); iterCond++)
00232         {
00233 
00234             // open a condition
00235             outputFile << spaces(2) << "<" << iterCond->first << " "
00236                     << m_xmlConditionAttrObject <<"=\"" << maps.obj2str((iterCond->second->objectType())[0])<<"\" " <<m_xmlConditionAttrType <<"=\""
00237                     << maps.type2str(iterCond->second->condType())<< "\">"
00238                     <<std::endl;
00239 
00240             switch ((iterCond->second)->condCategory())
00241             {
00242 
00243             case CondMuon:
00244             {
00245 
00246                 L1GtMuonTemplate* muonTemplate =
00247                         static_cast<L1GtMuonTemplate*>(iterCond->second);
00248                 const std::vector<L1GtMuonTemplate::ObjectParameter>* op =
00249                         muonTemplate->objectParameter();
00250 
00251                 // get the number of objects
00252                 unsigned int nObjects = iterCond->second->nrObjects();
00253 
00254                 for (unsigned int i =0; i<nObjects; i++)
00255                 {
00256 
00257                     std::string opi = L1GtVhdlWriterCore::int2str(i);
00258 
00259                     // ptHighTreshold
00260                     std::string address = calculateAddress(Mu, (iterCond->second)->condType(),
00261                             (m_xmlTagPtHighThreshold+"_"+opi),
00262                             cond2intMap[iterCond->first]);
00263 
00264                     outputFile << spaces(3) << openTag(m_xmlTagPtHighThreshold)
00265                             <<vmeAddrValueBlock(address, (*op).at(i).ptHighThreshold, 4,iterCond->second->condGEq()) << spaces(3)
00266                             << closeTag(m_xmlTagPtHighThreshold);
00267 
00268                     // ptLow Threshold
00269                     address = calculateAddress(Mu, (iterCond->second)->condType(),
00270                             (m_xmlTagPtLowThreshold+"_"+opi),
00271                             cond2intMap[iterCond->first]);
00272 
00273                     outputFile << spaces(3) << openTag(m_xmlTagPtLowThreshold)
00274                             <<vmeAddrValueBlock(address, (*op).at(i).ptLowThreshold, 4, iterCond->second->condGEq()) << spaces(3)
00275                             <<closeTag(m_xmlTagPtLowThreshold);
00276 
00277                     // Quality
00278                     address = calculateAddress(Mu, (iterCond->second)->condType(), (m_xmlTagQuality+"_"
00279                             +opi), cond2intMap[iterCond->first]);
00280 
00281                     outputFile << spaces(3) << openTag(m_xmlTagQuality)
00282                             << vmeAddrValueBlock(address, (*op).at(i).qualityRange, 4) << spaces(3)
00283                             << closeTag(m_xmlTagQuality);
00284 
00285                 }
00286 
00287                 const L1GtMuonTemplate::CorrelationParameter *cp =
00288                         muonTemplate->correlationParameter();
00289 
00290                 // Charage correlation -  occurs only one time
00291                 outputFile << spaces(3)<< openTag(m_xmlTagChargeCorrelation);
00292 
00293                 std::string address = calculateAddress(Mu, (iterCond->second)->condType(),
00294                         (m_xmlTagChargeCorrelation),
00295                         cond2intMap[iterCond->first]);
00296 
00297                 outputFile << vmeAddrValueBlock(address, (*cp).chargeCorrelation, 4) << spaces(3)
00298                         << closeTag(m_xmlTagChargeCorrelation);
00299 
00300             }
00301                 break;
00302 
00303             case CondCalo:
00304             {
00305 
00306                 L1GtCaloTemplate* m_gtCaloTemplate =
00307                         static_cast<L1GtCaloTemplate*>(iterCond->second);
00308                 const std::vector<L1GtCaloTemplate::ObjectParameter>* op =
00309                         m_gtCaloTemplate->objectParameter();
00310 
00311                 // get the number of objects
00312                 unsigned int nObjects = iterCond->second->nrObjects();
00313 
00314                 for (unsigned int i =0; i<nObjects; i++)
00315                 {
00316 
00317                     std::string opi = L1GtVhdlWriterCore::int2str(i);
00318                     std::string address = calculateAddress((iterCond->second->objectType()).at(0), (iterCond->second)->condType(),
00319                             (m_xmlTagPtHighThreshold+"_"+opi),
00320                             cond2intMap[iterCond->first]);
00321 
00322                     // insert Address/Value Tag
00323                     outputFile<<vmeAddrValueBlock(address, (*op).at(i).etThreshold, 3, iterCond->second->condGEq());
00324 
00325                 }
00326 
00327             }
00328                 break;
00329 
00330             case CondEnergySum:
00331             {
00332 
00333                 L1GtEnergySumTemplate* energySumTempl =
00334                         static_cast<L1GtEnergySumTemplate*>(iterCond->second);
00335 
00336                 const std::vector<L1GtEnergySumTemplate::ObjectParameter>* op =
00337                         energySumTempl->objectParameter();
00338 
00339                 // get the number of objects
00340                 unsigned int nObjects = iterCond->second->nrObjects();
00341 
00342                 for (unsigned int i =0; i<nObjects; i++)
00343                 {
00344 
00345                     std::string opi = L1GtVhdlWriterCore::int2str(i);
00346 
00347                     std::string address = calculateAddress((iterCond->second->objectType()).at(0), (iterCond->second)->condType(),
00348                             (m_xmlTagPtHighThreshold+"_"+opi),
00349                             cond2intMap[iterCond->first]);
00350 
00351                     // insert Address/Value Tag
00352                     outputFile<<vmeAddrValueBlock(address, (*op).at(i).etThreshold, 3,iterCond->second->condGEq());
00353 
00354                 }
00355 
00356             }
00357 
00358                 break;
00359 
00360             case CondJetCounts:
00361             {
00362 
00363                 L1GtJetCountsTemplate* jetsTemplate =
00364                         static_cast<L1GtJetCountsTemplate*>(iterCond->second);
00365                 const std::vector<L1GtJetCountsTemplate::ObjectParameter>* op =
00366                         jetsTemplate->objectParameter();
00367 
00368                 // get the number of objects
00369                 // unsigned int nObjects = iterCond->second->nrObjects();
00370 
00371                 // count index
00372                 int ci = (*op)[0].countIndex;
00373 
00374                 // 0 for count index 0-5 and 1 for count index 6 - 11
00375                 int obj;
00376 
00377                 if (ci<=5)
00378                     obj=0;
00379                 else
00380                     obj=1;
00381 
00382                 std::string address = calculateJetsAddress(ci, obj,
00383                         cond2intMap[iterCond->first]);
00384 
00385                 outputFile<<vmeAddrValueBlock(address, (*op).at(0).countThreshold, 3, iterCond->second->condGEq());
00386 
00387             }
00388                 break;
00389             case CondCorrelation:
00390             {
00391                 // empty
00392             }
00393                 break;
00394 
00395             case CondNull:
00396             {
00397                 // empty
00398             }
00399                 break;
00400 
00401             default:
00402             {
00403                 // empty
00404             }
00405                 break;
00406 
00407             }
00408 
00409             // close the condition
00410             outputFile << spaces(2)<< closeTag(iterCond->first);
00411 
00412         }
00413 
00414         outputFile << spaces(1) << "</" << m_xmlTagChip << index<<">"
00415                 <<std::endl;
00416 
00417     }
00418 
00419     // end of vme XML
00420     outputFile <<closeTag(m_xmlTagVme);
00421 
00422     // close output file
00423     outputFile.close();
00424 
00425     std::cout << std::endl <<"*******   VME XML File: "<< m_outputDir
00426             << m_vmeXmlFile << " written sucessfully!  *******"<<std::endl;
00427 
00428 }