CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/L1TriggerConfig/L1GtConfigProducers/src/L1GtVhdlWriterBitManager.cc

Go to the documentation of this file.
00001 
00018 // this class header
00019 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterBitManager.h"
00020 
00021 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
00022 
00023 #include "CondFormats/L1TObjects/interface/L1GtCaloTemplate.h"
00024 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
00025 #include "CondFormats/L1TObjects/interface/L1GtEnergySumTemplate.h"
00026 
00027 // system include files
00028 #include <iostream>
00029 #include <fstream>
00030 #include <map>
00031 #include <string>
00032 #include <sstream>
00033 #include <vector>
00034 #include <iomanip>
00035 
00036 L1GtVhdlWriterBitManager::L1GtVhdlWriterBitManager()
00037 {
00038         hex2binMap_["0"]="0000";
00039         hex2binMap_["1"]="0001";
00040         hex2binMap_["2"]="0010";
00041         hex2binMap_["3"]="0011";
00042         hex2binMap_["4"]="0100";
00043         hex2binMap_["5"]="0101";
00044         hex2binMap_["6"]="0110";
00045         hex2binMap_["7"]="0111";
00046         hex2binMap_["8"]="1000";
00047         hex2binMap_["9"]="1001";
00048 
00049         hex2binMap_["A"]="1010";
00050         hex2binMap_["B"]="1011";
00051         hex2binMap_["C"]="1100";
00052         hex2binMap_["D"]="1101";
00053         hex2binMap_["E"]="1110";
00054         hex2binMap_["F"]="1111";
00055 
00056         hex2binMap_["a"]="1010";
00057         hex2binMap_["b"]="1011";
00058         hex2binMap_["c"]="1100";
00059         hex2binMap_["d"]="1101";
00060         hex2binMap_["e"]="1110";
00061         hex2binMap_["f"]="1111";
00062 }
00063 
00064 
00065 std::string L1GtVhdlWriterBitManager::readMapInverse(std::map<std::string,std::string> map,std::string value)
00066 {
00067         std::map<std::string,std::string>::iterator iter = map.begin();
00068         while (iter!=map.end())
00069         {
00070                 if ((*iter).second == value)
00071                         return (*iter).first;
00072                 iter++;
00073         }
00074         return "";
00075 }
00076 
00077 
00078 std::string L1GtVhdlWriterBitManager::hex2bin(std::string hexString)
00079 {
00080 
00081         std::string temp;
00082         for (unsigned int i=0; i<hexString.length(); i++)
00083         {
00084                 std::string str;
00085                 str=hexString[i];
00086 
00087                 temp+=hex2binMap_[str];
00088         }
00089 
00090         return temp;
00091 }
00092 
00093 
00094 std::string L1GtVhdlWriterBitManager::bin2hex(std::string binString)
00095 {
00096 
00097         std::string temp;
00098         for (unsigned int i=1; i<=binString.length(); i++)
00099         {
00100                 //std::cout<<i%4<<std::endl;
00101                 if(i%4==0)
00102                 {
00103                         //std::cout<<"I'm here!"<<std::endl;
00104                         std::string str;
00105                         str = binString.substr(i-4,4);
00106                         //std::cout<<str<<std::cout<<std::endl;
00107                         temp+=readMapInverse(hex2binMap_,str);
00108                 }
00109         }
00110 
00111         return temp;
00112 }
00113 
00114 
00115 std::string L1GtVhdlWriterBitManager::mirror(unsigned int offset, std::string hexString, bool hexOutput)
00116 {
00117         std::string temp,binString;
00118 
00119         char digit;
00120         bool hexInput=false;
00121 
00122         // check weather input hex or binary
00123         for(unsigned int i = 0; i<hexString.length(); i++)
00124         {
00125                 if (hexString[i]!='0' || hexString[i]!='1' )
00126                 {
00127                         hexInput=true;
00128                         break;
00129                 }
00130         }
00131 
00132         if (hexInput)
00133                 binString = hex2bin(hexString);
00134         else binString=hexString;
00135 
00136         unsigned int i=0, len=0;
00137         len = binString.length();
00138 
00139         if(offset > len)
00140                 return binString;
00141 
00142         for(i = 0; i < (len - offset)/2; i++)
00143         {
00144                 digit = binString[i + offset];
00145 
00146                 binString[i + offset] = binString[len - 1 - i];
00147                 binString[len - 1 - i] = digit;
00148 
00149         }
00150 
00151         if (hexOutput)
00152                 return bin2hex(binString);
00153         else return binString;
00154 
00155 }
00156 
00157 
00158 std::string L1GtVhdlWriterBitManager::capitalLetters(std::string hexString)
00159 {
00160 
00161         unsigned int i = 0;
00162         while(i<hexString.length())
00163         {
00164                 if(hexString[i] == 'a') hexString[i] = 'A';
00165                 else if(hexString[i] == 'b') hexString[i] = 'B';
00166                 else if(hexString[i] == 'c') hexString[i] = 'C';
00167                 else if(hexString[i] == 'd') hexString[i] = 'D';
00168                 else if(hexString[i] == 'e') hexString[i] = 'E';
00169                 else if(hexString[i] == 'f') hexString[i] = 'F';
00170                 i++;
00171         }
00172 
00173         return hexString;
00174 }
00175 
00176 
00177 std::string L1GtVhdlWriterBitManager::shiftLeft(std::string hexString)
00178 {
00179         std::string binString = hex2bin(hexString);
00180 
00181         binString.erase(0,1);
00182         binString+="0";
00183 
00184         return bin2hex(binString);
00185 }
00186 
00187 
00188 std::string L1GtVhdlWriterBitManager::buildEtaMuon(const std::vector<L1GtMuonTemplate::ObjectParameter>* op, const unsigned int &num, const unsigned int &counter)
00189 {
00190         std::ostringstream ossEta;
00191 
00192         ossEta
00193                 <<counter
00194                 <<" => X\"";
00195 
00196         for (unsigned int i =0; i<num; i++)
00197         {
00198                 std::ostringstream ossEtaRange;
00199                 ossEtaRange << std::hex<< std::setw(16)<<std::setfill('0')<<(*op).at(i).etaRange<<std::dec;
00200 
00201                 /*
00202                 ossEta
00203                         <<mirror(32,ossEtaRange.str());
00204                 */
00205                 
00206                 ossEta << ossEtaRange.str();
00207                 
00208                 if (num>0 && i!=num-1) ossEta <<"_";
00209 
00210         }
00211 
00212         ossEta<<"\",\n";
00213         return ossEta.str();
00214 
00215 }
00216 
00217 
00218 std::string L1GtVhdlWriterBitManager::buildEtaCalo(const std::vector<L1GtCaloTemplate::ObjectParameter>* op, const unsigned int &num, const unsigned int &counter)
00219 {
00220         std::ostringstream ossEta;
00221 
00222         ossEta
00223                 <<counter
00224                 <<" => X\"";
00225 
00226         // loop over all relevant components of object parameters
00227 
00228         for (unsigned int i =0; i<num; i++)
00229         {
00230                 std::ostringstream ossEtaRange;
00231 
00232                 ossEtaRange << std::hex <<std::setw(4)<<std::setfill('0')<< (*op).at(i).etaRange<<std::dec;
00233 
00234                 /*
00235                 std::string tempstr=hex2bin(ossEtaRange.str());
00236                 tempstr[0]='0';
00237                 tempstr[15]='0';
00238                 */
00239                 
00240                 //ossEta<<std::setw(4)<<std::setfill('0')<<mirror(8,bin2hex(tempstr));
00241 
00242                 ossEta << ossEtaRange.str();
00243                 
00244                 if (num>0 && i!=num-1) ossEta <<"_";
00245 
00246         }
00247 
00248         ossEta<<"\",\n";
00249         return ossEta.str();
00250 }
00251 
00252 
00253 std::string L1GtVhdlWriterBitManager::buildPhiCalo(const std::vector<L1GtCaloTemplate::ObjectParameter>* op, const unsigned int &num, const unsigned int &counter)
00254 {
00255         std::ostringstream ossPhi;
00256 
00257         ossPhi
00258                 <<counter
00259                 <<" => X\"";
00260 
00261         for (unsigned int i =0; i<num; i++)
00262         {
00263                 ossPhi << std::hex<< std::setw(8)<<std::setfill('0')<<(*op).at(i).phiRange<<std::dec;
00264 
00265                 if (num>0 && i!=num-1) ossPhi <<"_";
00266         }
00267 
00268         ossPhi<<"\",\n";
00269         return capitalLetters(ossPhi.str());
00270 }
00271 
00272 
00273 std::string L1GtVhdlWriterBitManager::buildPhiEnergySum(const std::vector<L1GtEnergySumTemplate::ObjectParameter>* op, const unsigned int &num, const unsigned int &counter)
00274 {
00275         std::ostringstream ossPhi,count;
00276 
00277         if ((*op).at(0).phiRange0Word!=0)
00278         {
00279                 ossPhi << "000000000000000000"<<std::hex<<(*op).at(0).phiRange1Word<<(*op).at(0).phiRange0Word<<std::dec;
00280 
00281                 count<<counter;
00282 
00283                 return (count.str()+" => X\""+capitalLetters(ossPhi.str())+"\",\n");
00284 
00285         } else
00286         return "";
00287 }
00288 
00289 
00290 std::string L1GtVhdlWriterBitManager::buildPhiMuon(const std::vector<L1GtMuonTemplate::ObjectParameter>* op, const unsigned int &num, const unsigned int &counter,bool high)
00291 {
00292         std::ostringstream ossPhi;
00293 
00294         ossPhi
00295                 <<counter
00296                 <<" => X\"";
00297 
00298         for (unsigned int i =0; i<num; i++)
00299         {
00300                 if (high)
00301                         ossPhi << std::hex<< std::setw(2)<<std::setfill('0')<<(*op).at(i).phiHigh<<std::dec;
00302                 else
00303                         ossPhi << std::hex<< std::setw(2)<<std::setfill('0')<<(*op).at(i).phiLow<<std::dec;
00304 
00305                 if (num>0 && i!=num-1) ossPhi <<"_";
00306         }
00307 
00308         ossPhi<<"\",\n";
00309         return capitalLetters(ossPhi.str());
00310 
00311 }
00312 
00313 
00314 std::string L1GtVhdlWriterBitManager::buildDeltaEtaCalo(const L1GtCaloTemplate::CorrelationParameter* &cp,const unsigned int &counter)
00315 {
00316         std::ostringstream deltaEtaRange, dEta,res;
00317 
00318         //std::cout<<"b:"<<.hex2bin("00383800")<<std::endl;
00319 
00320         deltaEtaRange << std::hex<< std::setw(4)<<std::setfill('0')<<(*cp).deltaEtaRange<<std::dec;
00321 
00322         // mirror deltaEtaRang and shift the mirrored value to the left by one bit;
00323         // add the original value to the calculated value
00324         dEta<<hex2bin(shiftLeft(mirror(0,deltaEtaRange.str())))<<hex2bin(deltaEtaRange.str());
00325 
00326         std::string result = capitalLetters(bin2hex(dEta.str()));
00327 
00328         res<<counter<<" => X\""<<result<<"\",\n";
00329 
00330         return res.str();
00331 
00332 }
00333 
00334 
00335 std::string L1GtVhdlWriterBitManager::buildDeltaEtaMuon(const L1GtMuonTemplate::CorrelationParameter* &cp,const unsigned int &counter)
00336 {
00337         std::ostringstream deltaEtaRange, dEta;
00338         deltaEtaRange << std::hex<< std::setw(16)<<std::setfill('0')<<(*cp).deltaEtaRange<<std::dec;
00339 
00340         // mirror deltaEtaRang and shift the mirrored value to the left by one bit;
00341         // add the original value to the calculated value
00342 
00343         std::string result = capitalLetters((shiftLeft(mirror(0,deltaEtaRange.str()))+deltaEtaRange.str()));
00344 
00345         dEta<<counter<<" => X\""<<result<<"\",\n";
00346 
00347         return dEta.str();
00348 
00349 }
00350 
00351 
00352 std::string L1GtVhdlWriterBitManager::buildDeltaPhiCalo(const L1GtCaloTemplate::CorrelationParameter* &cp,const unsigned int &counter)
00353 {
00354         std::ostringstream dPhi,deltaPhiRange, result;
00355         //std::cout<<.hex2bin("03E0000000000F81")<<std::endl;
00356         //std::cout<<.hex2bin("0080000000000200")<<std::endl;
00357 
00358         deltaPhiRange << std::hex<<std::setw(3)<<std::setfill('0')<<(*cp).deltaPhiRange<<std::dec;
00359 
00360         std::string binString = hex2bin(deltaPhiRange.str());
00361 
00362         //std::cout <<"========================" <<std::endl;
00363 
00364         std::string help2 = binString.substr(2,binString.length()-2);
00365         std::string help1 = help2.substr(1);
00366         help1 = mirror(0,bin2hex(help1),false);
00367 
00368         // here delta phi is built
00369         result<<help1;
00370 
00371         // might be wrong - has to be tested with more reference values!
00372         result<<help2.substr(0,8);
00373         result<<"0";
00374 
00375         result<<"00000000000000000000000000000";
00376 
00377         result<<help1;
00378 
00379         result<<binString.substr(2,binString.length()-2);
00380 
00381         dPhi<<counter<<" => X\""<<bin2hex(result.str())<<"\",\n";
00382 
00383         //std::cout<<result<<std::endl;
00384 
00385         /*
00386          * Code from old GTS:
00387           bm_dphi = bm_dphi.Get(2);
00388           BitMngr help1, help2 = bm_dphi;
00389           help2.SetAt(9, '\0');
00390           help1 = help2.Get(1);
00391           help1.Mirror();
00392           BitMngr nuller;
00393           nuller.InitBin("00 00 00 00 00 00 00 00 00 00 00 00 00 00 0");
00394           BitMngr result;
00395           result = result + help1 + help2 + nuller + help1 + bm_dphi;
00396         dphi = result.GetHex();
00397         */
00398 
00399         return capitalLetters(dPhi.str());
00400 }
00401 
00402 
00403 std::string L1GtVhdlWriterBitManager::buildDeltaPhiMuon(const L1GtMuonTemplate::CorrelationParameter* &cp,const unsigned int &counter)
00404 {
00405         std::ostringstream dPhi,deltaPhiRange0,deltaPhiRange1,temp,result ;
00406         std::string tempstr;
00407 
00408         deltaPhiRange0 << std::hex<< std::setw(16)<<std::setfill('0')<<(*cp).deltaPhiRange0Word<<std::dec;
00409         //deltaPhiRange1  /*<< std::hex<< std::setw(3)<<std::setfill('0')*/<<(*cp).deltaPhiRange1Word<<std::dec;
00410 
00411         //mirror deltaPhiRange, shift the mirrored value left and convert it to binary format
00412         temp<<hex2bin(shiftLeft(mirror(0,deltaPhiRange0.str())));
00413         tempstr=temp.str();
00414         // set last bit of tempstr to one;
00415         tempstr[tempstr.length()-1]='1';
00416 
00417         // build delta eta as stringstreamtau
00418         result
00419                 <<tempstr
00420         // insert 16 ones
00421                 <<hex2bin("FFFF")
00422                 <<hex2bin(deltaPhiRange0.str());
00423 
00424         dPhi<<counter<<" => X\""<<bin2hex(result.str())<<"\",\n";
00425 
00426         return dPhi.str();
00427 
00428 }