CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 
00017 // this class header
00018 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterCore.h"
00019 
00020 // system include files
00021 #include <iostream>
00022 #include <fstream>
00023 #include <map>
00024 #include <string>
00025 #include <sstream>
00026 #include <vector>
00027 #include <sys/stat.h>
00028 #include <algorithm>
00029 
00030 // CMSSW headers
00031 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
00032 
00033 #include "CondFormats/L1TObjects/interface/L1GtFwd.h"
00034 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenuFwd.h"
00035 
00036 #include "CondFormats/L1TObjects/interface/L1GtCaloTemplate.h"
00037 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
00038 #include "CondFormats/L1TObjects/interface/L1GtEnergySumTemplate.h"
00039 #include "CondFormats/L1TObjects/interface/L1GtJetCountsTemplate.h"
00040 #include "CondFormats/L1TObjects/interface/L1GtStableParameters.h"
00041 
00042 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
00043 
00044 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
00045 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterBitManager.h"
00046 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlDefinitions.h"
00047 
00048 #include "CondFormats/L1TObjects/interface/L1GtStableParameters.h"
00049 
00050 // constructor(s)
00051 L1GtVhdlWriterCore::L1GtVhdlWriterCore(const std::string &templatesDirectory,
00052         const std::string &outputDirectory, const bool &debug)
00053 {
00054 
00055     // set templates directory
00056     vhdlDir_=templatesDirectory;
00057 
00058     // set output directory
00059     outputDir_=outputDirectory;
00060 
00061     // Get maps
00062     L1GtVhdlDefinitions maps;
00063 
00064     objType2Str_=maps.getObj2StrMap();
00065     caloType2Int_=maps.getCalo2IntMap();
00066     condType2Str_=maps.getCond2StrMap();
00067 
00068     // fill calo object vector
00069     caloObjects_.push_back(IsoEG);
00070     caloObjects_.push_back(NoIsoEG);
00071     caloObjects_.push_back(CenJet);
00072     caloObjects_.push_back(ForJet);
00073     caloObjects_.push_back(TauJet);
00074 
00075     esumObjects_.push_back(HTT);
00076     esumObjects_.push_back(ETT);
00077     esumObjects_.push_back(ETM);
00078 
00079     // prepare number of condition vector with two empty string vectors for the two condition chips
00080     std::vector<std::string> temp;
00081     numberOfConditions_.push_back(temp);
00082     numberOfConditions_.push_back(temp);
00083 
00084     // set debug mode
00085     debugMode_= debug;
00086 
00087 }
00088 
00089 // destructor
00090 L1GtVhdlWriterCore::~L1GtVhdlWriterCore()
00091 {
00092     // empty
00093 }
00094 
00095 bool L1GtVhdlWriterCore::returnConditionsOfOneClass(
00096         const L1GtConditionType &type, const L1GtConditionCategory &category,
00097         const L1GtObject &object,
00098         const ConditionMap &map,
00099         ConditionMap &outputMap)
00100 {
00101     bool status = false;
00102 
00103     ConditionMap::const_iterator condIter = map.begin();
00104     while (condIter!=map.end())
00105     {
00106         if (condIter->second->condCategory()==category
00107                 && condIter->second->condType()==type && (condIter->second->objectType())[0]==object)
00108         {
00109             outputMap[condIter->first]=condIter->second;
00110             status = true;
00111         }
00112         condIter++;
00113     }
00114 
00115     return status;
00116 }
00117 
00118 bool L1GtVhdlWriterCore::findObjectType(const L1GtObject &object,
00119         ConditionMap &map)
00120 {
00121     bool status = false;
00122 
00123     ConditionMap::iterator condIter = map.begin();
00124     while (condIter!=map.end())
00125     {
00126         for (unsigned int i=0; i<(condIter->second->objectType()).size(); i++)
00127         {
00128             if (condIter->second->objectType()[i]==object)
00129             {
00130                 status = true;
00131                 break;
00132             }
00133         }
00134 
00135         if (status)
00136             break;
00137         condIter++;
00138     }
00139 
00140     return status;
00141 }
00142 
00143 void L1GtVhdlWriterCore::getMuonSetupContentFromTriggerMenu(
00144         const unsigned short int &condChip,
00145         std::map<std::string,std::string> &muonParameters)
00146 {
00147     // vector containing all relevant types for muon conditions
00148     std::vector<L1GtConditionType> muonConditionTypes;
00149 
00150     muonConditionTypes.push_back(Type1s);
00151     muonConditionTypes.push_back(Type2s);
00152     muonConditionTypes.push_back(Type2wsc);
00153     muonConditionTypes.push_back(Type3s);
00154     muonConditionTypes.push_back(Type4s);
00155 
00156     for (unsigned int i = 0; i<muonConditionTypes.size(); i++)
00157     {
00158         //std::cout<<i<<std::endl;
00159 
00160         ConditionMap MuonConditions1s;
00161 
00162         countCondsAndAdd2NumberVec(muonConditionTypes.at(i), CondMuon, Mu,
00163                 (*conditionMap_).at(condChip-1), MuonConditions1s, condChip);
00164 
00165         /*
00166          // Get the absoloute amount of conditions
00167          std::string initstr="CONSTANT nr_"+objType2Str_[Mu]+"_"+condType2Str_[muonConditionTypes.at(i)]+" : integer := ";
00168 
00169          if (returnConditionsOfOneClass(muonConditionTypes.at(i),CondMuon,Mu,conditionMap_.at(condChip-1), MuonConditions1s))
00170          initstr+=(int2str(MuonConditions1s.size())+";");
00171          else
00172          initstr+=("0;");
00173 
00174          numberOfConditions_.at(condChip-1).push_back(initstr);
00175 
00176          */
00177 
00178         //std::cout<< std::hex << (*myObjectParameter).at(0).etThreshold << std::endl;
00179         unsigned int counter=0;
00180 
00181         for (ConditionMap::const_iterator iterCond = MuonConditions1s.begin(); iterCond
00182                 != MuonConditions1s.end(); iterCond++)
00183         {
00184 
00185             // add this condition to name -> integer conversion map
00186             conditionToIntegerMap_[iterCond->first]=counter;
00187 
00188             L1GtMuonTemplate* m_gtMuonTemplate =
00189                     static_cast<L1GtMuonTemplate*>(iterCond->second);
00190             const std::vector<L1GtMuonTemplate::ObjectParameter>* op =
00191                     m_gtMuonTemplate->objectParameter();
00192 
00193             if (muonConditionTypes.at(i)==Type1s)
00194             {
00195 
00196                 // build eta
00197                 muonParameters["eta_1_s"] += (bm_.buildEtaMuon(op, 1, counter));
00198 
00199                 // add the parameters to parameter map
00200                 muonParameters["phi_h_1_s"]+= bm_.buildPhiMuon(op, 1, counter,
00201                         true);
00202                 muonParameters["phi_l_1_s"]+=bm_.buildPhiMuon(op, 1, counter,
00203                         false);
00204 
00205                 if (debugMode_)
00206                 {
00207                     muonParameters["phi_l_1_s"]+=("--"+iterCond->first+"\n");
00208                     muonParameters["phi_h_1_s"]+=("--"+iterCond->first+"\n");
00209                     muonParameters["eta_1_s"] +=("--"+iterCond->first+"\n");
00210                 }
00211 
00212             } else
00213 
00214             if (muonConditionTypes.at(i)==Type2s)
00215             {
00216 
00217                 // build eta
00218                 muonParameters["eta_2_s"] += (bm_.buildEtaMuon(op, 2, counter));
00219 
00220                 // add the parameters to parameter map
00221                 muonParameters["phi_h_2_s"]+= bm_.buildPhiMuon(op, 2, counter,
00222                         true);
00223                 muonParameters["phi_l_2_s"]+=bm_.buildPhiMuon(op, 2, counter,
00224                         false);
00225 
00226                 if (debugMode_)
00227                 {
00228                     muonParameters["phi_l_2_s"]+=("--"+iterCond->first+"\n");
00229                     muonParameters["phi_h_2_s"]+=("--"+iterCond->first+"\n");
00230                     muonParameters["eta_2_s"] +=("--"+iterCond->first+"\n");
00231                 }
00232 
00233             } else
00234             //m_gtMuonTemplate->print(std::cout);
00235 
00236             if (muonConditionTypes.at(i)==Type3s)
00237             {
00238 
00239                 // build eta
00240                 muonParameters["eta_3"] += (bm_.buildEtaMuon(op, 3, counter));
00241 
00242                 // add the parameters to parameter map
00243                 muonParameters["phi_h_3"]+= bm_.buildPhiMuon(op, 3, counter,
00244                         true);
00245                 muonParameters["phi_l_3"]+=bm_.buildPhiMuon(op, 3, counter,
00246                         false);
00247 
00248                 if (debugMode_)
00249                 {
00250                     muonParameters["phi_l_3"]+=("--"+iterCond->first+"\n");
00251                     muonParameters["phi_h_3"]+=("--"+iterCond->first+"\n");
00252                     muonParameters["eta_3"] +=("--"+iterCond->first+"\n");
00253                 }
00254 
00255             }
00256 
00257             if (muonConditionTypes.at(i)==Type4s)
00258             {
00259 
00260                 // build eta
00261                 muonParameters["eta_4"] += (bm_.buildEtaMuon(op, 4, counter));
00262 
00263                 // add the parameters to parameter map
00264                 muonParameters["phi_h_4"]+= bm_.buildPhiMuon(op, 4, counter,
00265                         true);
00266                 muonParameters["phi_l_4"]+=bm_.buildPhiMuon(op, 4, counter,
00267                         false);
00268 
00269                 if (debugMode_)
00270                 {
00271                     muonParameters["phi_l_4"]+=("--"+iterCond->first+"\n");
00272                     muonParameters["phi_h_4"]+=("--"+iterCond->first+"\n");
00273                     muonParameters["eta_4"] +=("--"+iterCond->first+"\n");
00274                 }
00275 
00276             }
00277 
00278             if (muonConditionTypes.at(i)==Type2wsc)
00279             {
00280                 const L1GtMuonTemplate::CorrelationParameter* cp =
00281                         m_gtMuonTemplate->correlationParameter();
00282 
00283                 // build eta
00284                 muonParameters["eta_2_wsc"]
00285                         += (bm_.buildEtaMuon(op, 2, counter));
00286 
00287                 // build phi
00288                 muonParameters["phi_h_2_wsc"]+= bm_.buildPhiMuon(op, 2,
00289                         counter, true);
00290                 muonParameters["phi_l_2_wsc"]+=bm_.buildPhiMuon(op, 2, counter,
00291                         false);
00292 
00293                 // build delta_eta
00294                 std::ostringstream dEta;
00295                 muonParameters["delta_eta"] += bm_.buildDeltaEtaMuon(cp,
00296                         counter);
00297 
00298                 // build delta_phi
00299                 muonParameters["delta_phi"] += bm_.buildDeltaPhiMuon(cp,
00300                         counter);
00301 
00302                 if (debugMode_)
00303                 {
00304                     muonParameters["eta_2_wsc"]+=("--"+iterCond->first+"\n");
00305                     muonParameters["phi_h_2_wsc"]+=("--"+iterCond->first+"\n");
00306                     muonParameters["phi_l_2_wsc"] +=("--"+iterCond->first+"\n");
00307                     muonParameters["delta_eta"]+=("--"+iterCond->first+"\n");
00308                     muonParameters["delta_phi"] +=("--"+iterCond->first+"\n");
00309                 }
00310 
00311             }
00312             counter++;
00313         }
00314     }
00315 
00316 }
00317 
00318 bool L1GtVhdlWriterCore::getCaloSetupContentFromTriggerMenu(
00319         const unsigned short int &condChip,
00320         std::map<std::string,std::string> &caloParameters,
00321         const L1GtObject &caloObject)
00322 {
00323     // vector containing all relevant types for calo conditions
00324     std::vector<L1GtConditionType> caloConditionTypes;
00325 
00326     caloConditionTypes.push_back(Type1s);
00327     caloConditionTypes.push_back(Type2s);
00328     caloConditionTypes.push_back(Type2wsc);
00329     caloConditionTypes.push_back(Type4s);
00330 
00331     for (unsigned int i = 0; i<caloConditionTypes.size(); i++)
00332     {
00333         unsigned int counter=0;
00334 
00335         ConditionMap caloConditions;
00336 
00337         // stores all conditions of type given in the first three parameters
00338         countCondsAndAdd2NumberVec(caloConditionTypes.at(i), CondCalo,
00339                 caloObject, (*conditionMap_).at(condChip-1), caloConditions,
00340                 condChip);
00341 
00342         for (ConditionMap::const_iterator iterCond = caloConditions.begin(); iterCond
00343                 != caloConditions.end(); iterCond++)
00344         {
00345 
00346             // add this condition to name -> integer conversion map
00347             conditionToIntegerMap_[iterCond->first]=counter;
00348 
00349             L1GtCaloTemplate* m_gtCaloTemplate =
00350                     static_cast<L1GtCaloTemplate*>(iterCond->second);
00351             const std::vector<L1GtCaloTemplate::ObjectParameter>* op =
00352                     m_gtCaloTemplate->objectParameter();
00353 
00354             if (caloConditionTypes.at(i)==Type1s)
00355             {
00356 
00357                 //= static_cast<std::vector<L1GtCaloTemplate::ObjectPaenergySumParameter+=rameter*> >(op);
00358 
00359                 // build eta
00360                 caloParameters["eta_1_s"] += (bm_.buildEtaCalo(op, 1, counter));
00361 
00362                 caloParameters["phi_1_s"]+= bm_.buildPhiCalo(op, 1, counter);
00363 
00364                 if (debugMode_)
00365                 {
00366                     caloParameters["eta_1_s"]+=("--"+iterCond->first+"\n");
00367                     caloParameters["phi_1_s"]+=("--"+iterCond->first+"\n");
00368                 }
00369 
00370             } else if (caloConditionTypes.at(i)==Type2s)
00371             {
00372 
00373                 // build eta
00374                 caloParameters["eta_2_s"] += (bm_.buildEtaCalo(op, 2, counter));
00375 
00376                 // add the parameters to parameter map
00377                 caloParameters["phi_2_s"]+= bm_.buildPhiCalo(op, 2, counter);
00378 
00379                 if (debugMode_)
00380                 {
00381                     caloParameters["eta_2_s"]+=("--"+iterCond->first+"\n");
00382                     caloParameters["phi_2_s"]+=("--"+iterCond->first+"\n");
00383                 }
00384 
00385             } else if (caloConditionTypes.at(i)==Type4s)
00386             {
00387                 // build eta
00388                 caloParameters["eta_4"] += (bm_.buildEtaCalo(op, 4, counter));
00389 
00390                 // add the parameters to parameter map
00391                 caloParameters["phi_4"]+= bm_.buildPhiCalo(op, 4, counter);
00392 
00393                 if (debugMode_)
00394                 {
00395                     caloParameters["eta_4"]+=("--"+iterCond->first+"\n");
00396                     caloParameters["phi_4"]+=("--"+iterCond->first+"\n");
00397                 }
00398 
00399             } else if (caloConditionTypes.at(i)==Type2wsc)
00400             {
00401                 const L1GtCaloTemplate::CorrelationParameter* cp =
00402                         m_gtCaloTemplate->correlationParameter();
00403 
00404                 // build eta
00405                 caloParameters["eta_2_wsc"]
00406                         += (bm_.buildEtaCalo(op, 2, counter));
00407 
00408                 // build phi
00409                 caloParameters["phi_2_wsc"]+= bm_.buildPhiCalo(op, 2, counter);
00410 
00411                 // build delta_eta
00412                 caloParameters["delta_eta"] += bm_.buildDeltaEtaCalo(cp,
00413                         counter);
00414 
00415                 // build delta_phi
00416                 caloParameters["delta_phi"] += bm_.buildDeltaPhiCalo(cp,
00417                         counter);
00418 
00419                 if (debugMode_)
00420                 {
00421                     caloParameters["eta_2_wsc"]+=("--"+iterCond->first+"\n");
00422                     caloParameters["phi_2_wsc"]+=("--"+iterCond->first+"\n");
00423                     caloParameters["delta_eta"]+=("--"+iterCond->first+"\n");
00424                     caloParameters["delta_phi"]+=("--"+iterCond->first+"\n");
00425                 }
00426 
00427             }
00428 
00429             counter++;
00430 
00431         }
00432 
00433     }
00434 
00435     return true;
00436 }
00437 
00438 bool L1GtVhdlWriterCore::getEsumsSetupContentFromTriggerMenu(
00439         const unsigned short int &condChip, const L1GtObject &object,
00440         std::string &energySumParameter)
00441 {
00442 
00443     L1GtConditionType type;
00444 
00445     unsigned int counter=0;
00446 
00447     if (object==HTT)
00448         type= TypeHTT;
00449     else if (object==ETM)
00450         type= TypeETM;
00451     else if (object==ETT)
00452         type= TypeETT;
00453 
00454     ConditionMap esumsConditions;
00455     // stores all conditions of type given in the first three parameters
00456     countCondsAndAdd2NumberVec(type, CondEnergySum, object,
00457             (*conditionMap_).at(condChip-1), esumsConditions, condChip);
00458 
00459     for (ConditionMap::const_iterator iterCond = esumsConditions.begin(); iterCond
00460             != esumsConditions.end(); iterCond++)
00461     {
00462         L1GtEnergySumTemplate* energySumTempl =
00463                 static_cast<L1GtEnergySumTemplate*>(iterCond->second);
00464         const std::vector<L1GtEnergySumTemplate::ObjectParameter>* op =
00465                 energySumTempl->objectParameter();
00466 
00467         if (bm_.buildPhiEnergySum(op, 1, counter)!="")
00468             energySumParameter+=bm_.buildPhiEnergySum(op, 1, counter);
00469 
00470         if (debugMode_)
00471         {
00472             energySumParameter+=("--"+iterCond->first+"\n");
00473         }
00474 
00475         conditionToIntegerMap_[(iterCond->first)]=counter;
00476 
00477         counter++;
00478     }
00479 
00480     return true;
00481 
00482 }
00483 
00484 bool L1GtVhdlWriterCore::getSubstParamCommonFromTriggerMenu(const unsigned short int &condChip, L1GtVhdlTemplateFile &particle,
00485         const L1GtObject &object, const L1GtConditionCategory &category,
00486         std::string &parameterStr)
00487 {
00488 
00489     std::map<L1GtConditionType,std::string> condType2Strcopy = condType2Str_;
00490 
00491     // make a copy since types will deleted later after they are processed
00492     std::map<L1GtConditionType,std::string>::iterator typeIterator =
00493             condType2Strcopy.begin();
00494 
00495     while (typeIterator != condType2Strcopy.end())
00496     {
00497 
00498         ConditionMap outputMap;
00499         
00500         if (returnConditionsOfOneClass((*typeIterator).first, category, object, (*conditionMap_).at(condChip-1), outputMap))
00501         {
00502 
00503             // special treatment for jet counts in buildCodChipParameters
00504             if (object !=JetCounts)
00505             {
00506                 std::string tempStr2 = particle.returnParameterMap()[stringConstantCommon_];
00507                 while (particle.findAndReplaceString(tempStr2,
00508                         sp(substParamParticle_), objType2Str_[object]))
00509                     ;
00510                 while (particle.findAndReplaceString(tempStr2,
00511                         sp(substParamType_), condType2Str_[(*typeIterator).first]))
00512                     ;
00513                 parameterStr+=(tempStr2+"\n\n");
00514 
00515                 //parameterStr+=(tempStr2+"<= '0';\n");
00516 
00517                 // remove this type since it was already processed
00518                 condType2Strcopy.erase(typeIterator);
00519             }
00520 
00521         }
00522 
00523         typeIterator++;
00524 
00525     }
00526 
00527     return true;
00528 }
00529 
00530 bool L1GtVhdlWriterCore::getCondChipVhdContentFromTriggerMenu(
00531         const unsigned short int &condChip,
00532         std::map<std::string, L1GtVhdlTemplateFile> &templates,
00533         std::map<std::string, std::string> &commonParams)
00534 {
00535 
00536     // open the necessary internal templates
00537     L1GtVhdlTemplateFile muon, calo, esums, jet;
00538 
00539     muon.open(vhdlDir_+"InternalTemplates/muon", true);
00540     muon.substitute("particle", "muon");
00541 
00542     calo.open(vhdlDir_+"InternalTemplates/calo", true);
00543     esums.open(vhdlDir_+"InternalTemplates/esums", true);
00544     jet.open(vhdlDir_+"InternalTemplates/jet_cnts", true);
00545 
00546     /*
00547      charge1s.open(vhdlDir_+"InternalTemplates/charge_1s",false);
00548      charge2s.open(vhdlDir_+"InternalTemplates/charge_2s",false);
00549      charge2wsc.open(vhdlDir_+"InternalTemplates/charge_2_wsc",false);
00550      charge3s.open(vhdlDir_+"InternalTemplates/charge_3",false);
00551      charge4s.open(vhdlDir_+"InternalTemplates/charge_4",false);
00552      */
00553 
00554     // only for jet_cnts common parameter relevant since common parameter is build in this routine
00555     std::vector<unsigned int> processedTypes;
00556 
00557     //-------------------------------------build the common parameters-------------------------------------------
00558 
00559     // build $(muon_common)
00560     getSubstParamCommonFromTriggerMenu(condChip,muon, Mu, CondMuon, commonParams["muon_common"]);
00561 
00562     // build $(calo_common) - loop over all calo objects
00563     std::vector<L1GtObject>::iterator caloObjIter = caloObjects_.begin();
00564 
00565     while (caloObjIter != caloObjects_.end())
00566     {
00567 
00568         getSubstParamCommonFromTriggerMenu(condChip, muon, (*caloObjIter), CondCalo,
00569                 commonParams["calo_common"]);
00570         caloObjIter++;
00571 
00572     }
00573 
00574     // build $(esums_common) - loop over all esums objects
00575     std::vector<L1GtObject>::iterator esumObjIter = esumObjects_.begin();
00576 
00577     while (esumObjIter != esumObjects_.end())
00578     {
00579 
00580         getSubstParamCommonFromTriggerMenu(condChip, esums, (*esumObjIter), CondEnergySum,
00581                 commonParams["esums_common"]);
00582         esumObjIter++;
00583 
00584     }
00585 
00586     //--------------------------------------build the parameter maps----------------------------------------------
00587 
00588     // loop over condition map
00589     for (ConditionMap::const_iterator iterCond = (*conditionMap_).at(condChip-1).begin(); iterCond != (*conditionMap_).at(condChip-1).end(); iterCond++)
00590     {
00591 
00592         switch ((iterCond->second)->condCategory())
00593         {
00594 
00595         case CondMuon:
00596         {
00597 
00598             int cond2int;
00599             if (!getIntVal(conditionToIntegerMap_, (iterCond->first), cond2int))
00600             {
00601                 msg("Panik! Condition "+(iterCond->first)
00602                         +" does not have a integer equivalent!");
00603                 break;
00604             }
00605 
00606             std::string intVal = index4CondChipVhd(cond2int);
00607 
00608             L1GtVhdlTemplateFile muoncopy = muon;
00609 
00610             muoncopy.substitute("type", condType2Str_[(iterCond->second)->condType()]);
00611 
00612             muoncopy.substitute("ser_no", intVal);
00613 
00614             muoncopy.substitute("name", iterCond->first);
00615 
00616             if ((iterCond->second)->condType() == Type1s)
00617             {
00618                 muoncopy.substitute(substParamCharge_,
00619                         muon.getInternalParameter(stringConstantCharge1s_));
00620             } else if ((iterCond->second)->condType() == Type2s)
00621             {
00622                 muoncopy.substitute(substParamCharge_,
00623                         muon.getInternalParameter(stringConstantCharge2s_));
00624             } else if ((iterCond->second)->condType() == Type2wsc)
00625             {
00626                 muoncopy.substitute(substParamCharge_,
00627                         muon.getInternalParameter(stringConstantCharge2wsc_));
00628             } else if ((iterCond->second)->condType() == Type3s)
00629             {
00630                 muoncopy.substitute(substParamCharge_,
00631                         muon.getInternalParameter(stringConstantCharge3s_));
00632             } else if ((iterCond->second)->condType() == Type4s)
00633             {
00634                 muoncopy.substitute(substParamCharge_,
00635                         muon.getInternalParameter(stringConstantCharge4s_));
00636             }
00637 
00638             std::string tempStr = muoncopy.returnParameterMap()["PREALGO"];
00639 
00640             muoncopy.findAndReplaceString(tempStr, "$(particle)", "muon");
00641             muoncopy.findAndReplaceString(tempStr, "$(ser_no)", intVal);
00642             muoncopy.findAndReplaceString(tempStr, "$(type)", condType2Str_[(iterCond->second)->condType()]);
00643 
00644             muoncopy.append(tempStr);
00645 
00646             muoncopy.removeEmptyLines();
00647 
00648             // add the processed internal template to parameter map
00649             templates["muon"].append(muoncopy);
00650 
00651         }
00652             break;
00653 
00654         case CondCalo:
00655         {
00656             int cond2int;
00657 
00658             if (!getIntVal(conditionToIntegerMap_, (iterCond->first), cond2int))
00659             {
00660                 msg("Panik! Condition "+(iterCond->first)
00661                         +" does not have a integer equivalent!");
00662                 break;
00663             }
00664 
00665             std::string intVal = index4CondChipVhd(cond2int);
00666 
00667             L1GtVhdlTemplateFile calocopy = calo;
00668 
00669             calocopy.substitute("type", condType2Str_[(iterCond->second)->condType()]);
00670             calocopy.substitute("particle", objType2Str_[((iterCond->second)->objectType()).at(0)]);
00671             calocopy.substitute("name", iterCond->first);
00672             calocopy.substitute("ser_no", intVal);
00673             calocopy.substitute("calo_nr", caloType2Int_[((iterCond->second)->objectType()).at(0)]);
00674 
00675             // builds something like tau_1_s(20));
00676             calocopy.append(objType2Str_[((iterCond->second)->objectType()).at(0)] +"_"+condType2Str_[(iterCond->second)->condType()]+"("+intVal+"));");
00677 
00678             templates["calo"].append(calocopy);
00679 
00680         }
00681             break;
00682 
00683         case CondEnergySum:
00684         {
00685 
00686             int cond2int;
00687 
00688             if (!getIntVal(conditionToIntegerMap_, (iterCond->first), cond2int))
00689             {
00690                 msg("Panik! Condition "+(iterCond->first)
00691                         +" does not have a integer equivalent!");
00692                 break;
00693             }
00694 
00695             std::string intVal = index4CondChipVhd(cond2int);
00696 
00697             L1GtVhdlTemplateFile esumscopy = esums;
00698 
00699             esumscopy.substitute("type", condType2Str_[(iterCond->second)->condType()]);
00700             esumscopy.substitute("particle", objType2Str_[((iterCond->second)->objectType()).at(0)]);
00701             esumscopy.substitute("name", iterCond->first);
00702             esumscopy.substitute("ser_no", intVal);
00703 
00704             if (((iterCond->second)->objectType()).at(0)==ETM)
00705                 esumscopy.substitute("if_etm_then_1_else_0", "1");
00706             else
00707                 esumscopy.substitute("if_etm_then_1_else_0", "0");
00708 
00709             // builds something like htt_cond(4));
00710             esumscopy.append(objType2Str_[((iterCond->second)->objectType()).at(0)] +"_"+condType2Str_[(iterCond->second)->condType()]+"("+ intVal+"));");
00711 
00712             templates["esums"].append(esumscopy);
00713         }
00714 
00715             break;
00716 
00717         case CondJetCounts:
00718         {
00719 
00720             // build the common parameter for the jet counts
00721 
00722             L1GtJetCountsTemplate* jetsTemplate =
00723                     static_cast<L1GtJetCountsTemplate*>(iterCond->second);
00724 
00725             int nObjects = iterCond->second->nrObjects();
00726 
00727             const std::vector<L1GtJetCountsTemplate::ObjectParameter>* op =
00728                     jetsTemplate->objectParameter();
00729 
00730             for (int i = 0; i < nObjects; i++)
00731             {
00732 
00733                 std::vector<unsigned int>::iterator p = find(
00734                         processedTypes.begin(), processedTypes.end(), (*op)[i].countIndex);
00735 
00736                 // check, weather this count index was already processed
00737                 // and process it if not
00738                 if (p==processedTypes.end())
00739                 {
00740                     std::ostringstream indStr;
00741                     indStr<<(*op)[i].countIndex;
00742 
00743                     std::string tempStr2 = jet.returnParameterMap()[stringConstantCommon_];
00744                     while (jet.findAndReplaceString(tempStr2,
00745                             sp(substParamParticle_), objType2Str_[JetCounts]))
00746                         ;
00747                     while (jet.findAndReplaceString(tempStr2,
00748                             sp(substParamType_), indStr.str()))
00749                         ;
00750                     commonParams[substParamJetCntsCommon_]+=tempStr2+"\n"; // +"<= '0';\n");
00751                     processedTypes.push_back((*op)[i].countIndex);
00752                 }
00753 
00754             }
00755 
00756             int cond2int;
00757 
00758             if (!getIntVal(conditionToIntegerMap_, (iterCond->first), cond2int))
00759             {
00760                 msg("Panik! Condition "+(iterCond->first)
00761                         +" does not have a integer equivalent!");
00762                 break;
00763             }
00764 
00765             std::string intVal = index4CondChipVhd(cond2int);
00766 
00767             L1GtVhdlTemplateFile jetcopy = jet;
00768 
00769             jetcopy.substitute("particle", condType2Str_[(iterCond->second)->condType()]);
00770             jetcopy.substitute("type", int2str((*op)[0].countIndex));
00771             jetcopy.substitute("name", iterCond->first);
00772 
00773             jetcopy.substitute("ser_no", intVal);
00774 
00775             // builds the final string
00776             std::string tempStr = jetcopy.returnParameterMap()["PREALGO"];
00777 
00778             jetcopy.findAndReplaceString(tempStr, "$(particle)", "jet_cnt");
00779             jetcopy.findAndReplaceString(tempStr, "$(ser_no)", intVal);
00780             jetcopy.findAndReplaceString(tempStr, "$(type)", int2str((*op)[0].countIndex));
00781 
00782             jetcopy.append(tempStr);
00783             jetcopy.removeEmptyLines();
00784 
00785             templates["jet_cnts"].append(jetcopy);
00786         }
00787 
00788             break;
00789 
00790         case CondCorrelation:
00791         {
00792             //empty
00793         }
00794             break;
00795 
00796         case CondNull:
00797         {
00798             //empty
00799         }
00800             break;
00801 
00802         default:
00803         {
00804             //empty
00805         }
00806             break;
00807         }
00808     }
00809 
00810     // delete superfluous newlines at the end of common parameters
00811     std::map<std::string,std::string>::iterator pIt = commonParams.begin();
00812 
00813     while (pIt!=commonParams.end())
00814     {
00815         if ((pIt->second)[((pIt->second).length()-1)] == '\n')
00816             (pIt->second) = (pIt->second).substr(0, (pIt->second).length()-1);
00817         pIt++;
00818     }
00819 
00820     return true;
00821 
00822 }
00823 
00824 bool L1GtVhdlWriterCore::processAlgorithmMap(std::vector< std::map<int, std::string> > &algoStrings)
00825 {
00826     std::map<int, std::string> algorithmsChip1;
00827     std::map<int, std::string> algorithmsChip2;
00828     
00829     AlgorithmMap::const_iterator algoIter=(*algorithmMap_).begin();
00830 
00831     // loop over algorithm map
00832     while (algoIter!=(*algorithmMap_).end())
00833     {
00834         // msg(algoIter->first);
00835 
00836         L1GtVhdlTemplateFile dummy;
00837 
00838         // get the logical expression
00839         std::string logicalExpr = (algoIter->second).algoLogicalExpression();
00840         std::vector<std::string> conditions;
00841 
00842         dummy.getConditionsFromAlgo(logicalExpr, conditions);
00843 
00844         std::string logicalExprCopy= logicalExpr;
00845 
00846         // loop over all condition names appearing in algorithm and replace them by type and integer value
00847         for (unsigned int i=0; i<conditions.size(); i++)
00848         {
00849             std::ostringstream newExpr;
00850             
00851             // look for the condition on correct chip
00852            
00853             std::vector<ConditionMap> * conditionMapCp = const_cast< std::vector<ConditionMap>* >(conditionMap_);
00854             
00855             L1GtCondition* cond = (*conditionMapCp).at((algoIter->second).algoChipNumber())[conditions.at(i)];
00856 
00857             // check weather condition exists
00858             if (cond!=NULL)
00859             {
00860                 newExpr<<objType2Str_[(cond->objectType()).at(0)];
00861 
00862                 newExpr <<"_" << condType2Str_[cond->condType()] << "(";
00863 
00864                 newExpr << conditionToIntegerMap_[conditions.at(i)] << ")";
00865                 dummy.findAndReplaceString(logicalExpr, conditions.at(i),
00866                         newExpr.str());
00867             } else
00868                 msg("Panik! Didn't find Condition "+conditions.at(i));
00869         }
00870 
00871         // has to be checked!
00872         std::vector<int> orderConditionChip;
00873         orderConditionChip.push_back(2);
00874         orderConditionChip.push_back(1);
00875 
00876         int pin = (algoIter->second).algoOutputPin(2, 96, orderConditionChip);
00877 
00878         //msg(int2str(pin));
00879 
00880         if (pin<0)
00881             pin*=-1;
00882 
00883         if ((algoIter->second).algoChipNumber()==0)
00884         {
00885             algorithmsChip1[pin]=logicalExpr;
00886             if (debugMode_)
00887                 algorithmsChip1[pin]+=("-- "+logicalExprCopy);
00888         } else if ((algoIter->second).algoChipNumber()==1)
00889         {
00890             algorithmsChip2[pin]=logicalExpr;
00891             if (debugMode_)
00892                 algorithmsChip2[pin]+=("-- "+logicalExprCopy);
00893         } else
00894             ;
00895 
00896         algoIter++;
00897     }
00898 
00899     algoStrings.push_back(algorithmsChip1);
00900     algoStrings.push_back(algorithmsChip2);
00901     
00902     return true;
00903 
00904 }
00905 
00906 std::string L1GtVhdlWriterCore::chip2OutputSubDir(const int &chip)
00907 {
00908     if (chip==1)
00909         return outputDir_+"/"+outputSubDir1_+"/";
00910     if (chip==2)
00911         return outputDir_+"/"+outputSubDir2_+"/";
00912     
00913     return "";
00914 }
00915 
00916 
00917 bool L1GtVhdlWriterCore::makeFirmware(const std::vector<ConditionMap> &conditionMap,
00918         const AlgorithmMap &algorithmMap)
00919 {
00920     conditionMap_ = &conditionMap;
00921 
00922     algorithmMap_ = &algorithmMap;
00923     
00924     std::string subDir1 = outputDir_+outputSubDir1_;
00925     std::string subDir2  = outputDir_+outputSubDir2_;
00926     
00927     if (!mkdir(subDir1.c_str(), 666));
00928     if (!mkdir(subDir2.c_str(), 666));
00929     
00930     chmod(subDir1.c_str(), 0777);
00931     chmod(subDir2.c_str(), 0777);
00932     
00933     /*
00934     subDirs_.push_back(subDir1);
00935     subDirs_.push_back(subDir2);
00936     */
00937     
00938     writeQsfSetupFiles(version_);
00939     
00940     
00941     //--------------------------------------build setups-------------------------------------------------------
00942 
00943     // loop over the two condition chips
00944     for (unsigned short int i =1; i<=2; i++)
00945     {
00946         // ----------------------- muon setup -------------------------------------------------------
00947 
00948         std::map<std::string,std::string> muonParameters;
00949         getMuonSetupContentFromTriggerMenu(i, muonParameters);
00950         writeMuonSetupVhdl(muonParameters, "muon", i);
00951 
00952         // ----------------------- calo setup -------------------------------------------------------
00953 
00954         // map containing all calo object types correlated with the strings
00955         // under which they appear in the firmware
00956         std::vector<L1GtObject>::iterator caloObjIter = caloObjects_.begin();
00957 
00958         // loop over all calo objectsbuildMuonSetupVhdl
00959         while (caloObjIter != caloObjects_.end())
00960         {
00961             std::map<std::string,std::string> caloParameters;
00962             getCaloSetupContentFromTriggerMenu(i, caloParameters,
00963                     (*caloObjIter));
00964             writeMuonSetupVhdl(caloParameters, objType2Str_[(*caloObjIter)], i);
00965 
00966             caloObjIter++;
00967         }
00968 
00969         // ----------------------- etm setup ---------------------------------------------------------
00970 
00971         // map containing all calo object types correlated with the strings
00972         // under which they appear in the firmware
00973         std::vector<L1GtObject>::iterator esumObjIter = esumObjects_.begin();
00974 
00975         while (esumObjIter != esumObjects_.end())
00976         {
00977             std::string etmParameter;
00978 
00979             getEsumsSetupContentFromTriggerMenu(i, (*esumObjIter),
00980                     etmParameter);
00981             writeEtmSetup(etmParameter, i);
00982 
00983             esumObjIter++;
00984 
00985         }
00986 
00987         // add jet counts to condition 2 integer map
00988         addJetCountsToCond2IntMap(i, (*conditionMap_), conditionToIntegerMap_);
00989 
00990         // --------------------cond chip setup---------------------------------------------------------
00991         // Important: all other setups have to be build BEFORE this one because it needs a
00992         // complete condition2integer map !!!
00993 
00994         std::map<std::string, L1GtVhdlTemplateFile> templates;
00995         std::map<std::string, std::string> common;
00996         getCondChipVhdContentFromTriggerMenu(i, templates, common);
00997         writeConditionChipSetup(templates, common, i);
00998 
00999         // despite yet not existing they have to appear in cond_chip_pkg vhds
01000         initializeDeltaConditions();
01001 
01002         // --------------------cond chip pkg------------------------------------------------------------
01003 
01004         writeCondChipPkg(i);
01005 
01006         // -----------------------def val pkg -------------------------------------------------
01007 
01008         writeDefValPkg((*conditionMap_), i);
01009 
01010         // debug
01011         if (debugMode_)
01012         {
01013             //printConditionsOfCategory(CondEnergySum, (*conditionMap_).at(i-1));
01014         }
01015 
01016     }
01017 
01018     if (debugMode_)
01019         writeCond2intMap2File();
01020 
01021     //-------------------------------process algorithms----------------------------------------------------------
01022 
01023     std::vector< std::map<int, std::string> > algoStrings;
01024 
01025     processAlgorithmMap(algoStrings);
01026     
01027     writeAlgoSetup(algoStrings);
01028 
01029     return true;
01030 }
01031 
01032 std::string L1GtVhdlWriterCore::gtTemplatesPath()
01033 {
01034     return vhdlDir_;
01035 }
01036 
01037 std::string L1GtVhdlWriterCore::int2str(const int &integerValue)
01038 {
01039     std::ostringstream oss;
01040     oss<<integerValue;
01041     return oss.str();
01042 
01043 }
01044 
01045 void L1GtVhdlWriterCore::buildCommonHeader(
01046         std::map<std::string,std::string> &headerParameters,
01047         const std::vector<std::string> &connectedChannels)
01048 {
01049     commonHeader_.open(vhdlDir_+"InternalTemplates/header");
01050 
01051     std::map<std::string,std::string>::iterator iter = headerParameters.begin();
01052 
01053     // loop over the header parameter map and replace all subsitution
01054     // parameters with their values
01055     while (iter != headerParameters.end() )
01056     {
01057         commonHeader_.substitute((*iter).first, (*iter).second);
01058         iter++;
01059     }
01060 
01061     commonHeader_.insert("$(connected_channels_1)", connectedChannels);
01062     commonHeader_.insert("$(connected_channels_2)", connectedChannels);
01063 
01064     //---------------------build the Quartus configuration files----------------------------------------
01065 
01066     //writeQsfSetupFiles(headerParameters["version"]);
01067     
01068     version_=headerParameters["version"];
01069 
01070     msg("Build Common header and Quartus setup files sucessuflly!");
01071 
01072 }
01073 
01074 L1GtVhdlTemplateFile L1GtVhdlWriterCore::openVhdlFileWithCommonHeader(
01075         const std::string &filename, const std::string &outputFilename)
01076 {
01077     L1GtVhdlTemplateFile commonHeaderCp;
01078     commonHeaderCp = commonHeader_;
01079 
01080     commonHeaderCp.substitute("vhdl_file_name", outputFilename);
01081 
01082     L1GtVhdlTemplateFile myTemplate(vhdlDir_+"Templates/"+filename);
01083     myTemplate.insert("$(header)", commonHeaderCp);
01084 
01085     return myTemplate;
01086 }
01087 
01088 void L1GtVhdlWriterCore::writeMuonSetupVhdl(
01089         std::map<std::string,std::string> &muonParameters,
01090         const std::string &particle, unsigned short int &condChip)
01091 {
01092     std::string filename;
01093 
01094     L1GtVhdlTemplateFile muonTemplate, internalTemplate, internalTemplateCopy;
01095 
01096     // choose the correct template file name
01097     if (particle=="muon")
01098     {
01099         filename = vhdlTemplateMuonSetup_;
01100         internalTemplate.open(vhdlDir_+"InternalTemplates/muonsetup", true);
01101     } else
01102     {
01103         filename = vhdlTemplateCaloSetup_;
01104         internalTemplate.open(vhdlDir_+"InternalTemplates/calosetup", true);
01105     }
01106 
01107     std::string outputFile;
01108     outputFile = filename;
01109 
01110     // modify filename
01111     if (particle!="muon")
01112         muonTemplate.findAndReplaceString(outputFile, "calo", particle);
01113 
01114     // add condition chip index to output filename
01115     //muonTemplate.findAndReplaceString(outputFile, ".", int2str(condChip)+".");
01116 
01117     // open the template file and insert header
01118     muonTemplate = openVhdlFileWithCommonHeader(filename, outputFile);
01119 
01120     std::map<std::string,std::string> parameterMap =
01121             internalTemplate.returnParameterMap();
01122 
01123     std::vector<std::string> substitutionParameters;
01124 
01125     // fills the map with all substitution parameters of the template file
01126     substitutionParameters=muonTemplate.getSubstitutionParametersFromTemplate();
01127 
01128     // insert a emty line at the end of each block
01129     internalTemplate.append("");
01130 
01131     std::map<std::string, std::string>::iterator iter;
01132 
01133     // loop over all substitution parameters that have been extracted from the template file
01134     for (unsigned int i=0; i<substitutionParameters.size(); i++)
01135     {
01136         // make a working copy of the template file
01137         internalTemplateCopy = internalTemplate;
01138 
01139         // choose which of the three constant template strings to take - only for calo!
01140         if (particle!="muon")
01141         {
01142 
01143             if (substitutionParameters.at(i).substr(0, 3) == "eta")
01144                 internalTemplateCopy.substitute("constant", parameterMap["eta"]);
01145             else if (substitutionParameters.at(i).substr(0, 3) == "phi")
01146                 internalTemplateCopy.substitute("constant", parameterMap["phi"]);
01147             else if (substitutionParameters.at(i).substr(0, 3) == "del"/*ta*/)
01148             {
01149                 internalTemplateCopy.substitute("constant",
01150                         parameterMap["delta"]);
01151                 while (internalTemplateCopy.substitute("delta",
01152                         substitutionParameters[i]))
01153                     internalTemplateCopy.substitute("delta",
01154                             substitutionParameters[i]);
01155 
01156             }
01157         }
01158 
01159         if (particle=="muon")
01160         {
01161         // final preparation of the internal template before it is inserted in the actual template file
01162         internalTemplateCopy.substitute("type", substitutionParameters[i]);
01163         } else
01164             
01165         {
01166             internalTemplateCopy.substitute("type", substitutionParameters[i].substr(4)); 
01167         }
01168 
01169         // subsitute the second occurance of type without "_l" and "_h"
01170 
01171         std::string paramCopy = substitutionParameters[i];
01172 
01173         internalTemplateCopy.findAndReplaceString(paramCopy, "_l", "");
01174         internalTemplateCopy.findAndReplaceString(paramCopy, "_h", "");
01175 
01176         internalTemplateCopy.substitute("type2", paramCopy);
01177 
01178         internalTemplateCopy.substitute("others",
01179                 parameterMap[substitutionParameters[i]]);
01180 
01181         // replace all the occurances of "particle"
01182         while (muonTemplate.substitute("particle", particle))
01183             muonTemplate.substitute("particle", particle);
01184 
01185         // remove the the parameter $(content) if its empty
01186         iter=muonParameters.find(substitutionParameters[i]);
01187 
01188         // check weather this parameter exists
01189         if (iter!=muonParameters.end())
01190         {
01191             if ((*iter).second[(*iter).second.length()-1]=='\n')
01192                 (*iter).second[(*iter).second.length()-1]=' ';
01193             internalTemplateCopy.substitute("content", (*iter).second);
01194         } else
01195             internalTemplateCopy.removeLineWithContent("$(content)");
01196 
01197         // insert the processed internal template in the muon template file
01198         muonTemplate.insert("$("+substitutionParameters[i]+")",
01199                 internalTemplateCopy);
01200     }
01201 
01202     // save the muon template file
01203     muonTemplate.save(chip2OutputSubDir(condChip)+outputFile);
01204     chmod((chip2OutputSubDir(condChip)+outputFile).c_str(), 0666);
01205 
01206 }
01207 
01208 void L1GtVhdlWriterCore::writeConditionChipSetup(
01209         std::map<std::string, L1GtVhdlTemplateFile> templates,
01210         const std::map<std::string, std::string> &common,
01211         const unsigned short int &chip)
01212 {
01213 
01214     // get filename 
01215     std::string filename = vhdlTemplateCondChip_;
01216 
01217     // build output filename
01218     std::string outputFileName = filename;
01219     L1GtVhdlTemplateFile::findAndReplaceString(outputFileName, ".",
01220             int2str(chip)+".");
01221 
01222     L1GtVhdlTemplateFile myTemplate = openVhdlFileWithCommonHeader(filename,
01223             outputFileName);
01224 
01225     // map containing the subsitution parameters with their content (as L1GtVhdlTemplateFile file object)
01226     std::map<std::string, L1GtVhdlTemplateFile>::iterator iter=
01227             templates.begin();
01228 
01229     while (iter != templates.end())
01230     {
01231 
01232         myTemplate.insert("$("+(iter->first)+")", iter->second);
01233 
01234         iter++;
01235     }
01236 
01237     // subsitutes common parameters
01238     std::map<std::string, std::string>::const_iterator iter2= common.begin();
01239 
01240     while (iter2 != common.end())
01241     {
01242 
01243         myTemplate.substitute((iter2->first), iter2->second);
01244 
01245         iter2++;
01246     }
01247 
01248     myTemplate.save(chip2OutputSubDir(chip)+filename);
01249     chmod((chip2OutputSubDir(chip)+filename).c_str(), 0666);
01250 
01251 }
01252 
01253 void L1GtVhdlWriterCore::writeAlgoSetup(std::vector< std::map<int, std::string> > &algoStrings)
01254 {
01255     // loop over the two condition chips
01256     for (unsigned int i=1; i<=2; i++)
01257     {
01258         std::string filename=vhdlTemplateAlgoAndOr_;
01259 
01260         // output file name
01261         std::string outputFileName = filename;
01262         L1GtVhdlTemplateFile::findAndReplaceString(outputFileName, ".",
01263                 int2str(i)+".");
01264 
01265         L1GtVhdlTemplateFile templateFile = openVhdlFileWithCommonHeader(
01266                 filename, outputFileName);
01267 
01268         std::ostringstream buffer;
01269         
01270         unsigned int algoPinsPerChip = 96;
01271 
01272         for (unsigned int k=1; k<=algoPinsPerChip; k++)
01273         {
01274 
01275             buffer<< stringConstantAlgo_<<"("<<k<<")";
01276             if (algoStrings.at(i-1)[k]!="")
01277                 buffer<<" <= "<<algoStrings.at(i-1)[k]<<";"<<std::endl;
01278             else
01279                 buffer<<" <= '0';"<<std::endl;
01280         }
01281 
01282         templateFile.substitute(substParamAlgos_, buffer.str());
01283         templateFile.save(chip2OutputSubDir(i)+filename);
01284         chmod((chip2OutputSubDir(i)+filename).c_str(), 0666);
01285     }
01286 
01287 }
01288 
01289 void L1GtVhdlWriterCore::writeEtmSetup(std::string &etmString,
01290         const int &condChip)
01291 {
01292 
01293     // get filename
01294     std::string filename= vhdlTemplateEtmSetup_;
01295     std::string outputFile = filename;
01296 
01297     L1GtVhdlTemplateFile myTemplate;
01298 
01299     // modify output filename
01300     myTemplate.findAndReplaceString(outputFile, ".", int2str(condChip)+".");
01301 
01302     myTemplate = openVhdlFileWithCommonHeader(filename, filename);
01303 
01304     // replace all occurances of $(particle)
01305     while (myTemplate.substitute("particle", "etm"))
01306         while (myTemplate.substitute("particle", "etm"))
01307             ;
01308 
01309     // delete last char if it is \n
01310     if (etmString[etmString.length()-1] == '\n')
01311         etmString = etmString.substr(0, etmString.length()-1);
01312 
01313     myTemplate.substitute("phi", etmString);
01314 
01315     myTemplate.save(chip2OutputSubDir(condChip)+filename);
01316     chmod((chip2OutputSubDir(condChip)+filename).c_str(), 0666);
01317 
01318 }
01319 
01320 void L1GtVhdlWriterCore::printCommonHeader()
01321 {
01322     commonHeader_.print();
01323 }
01324 
01325 L1GtVhdlTemplateFile L1GtVhdlWriterCore::retrunCommonHeader()
01326 {
01327     return commonHeader_;
01328 }
01329 
01330 void L1GtVhdlWriterCore::writeQsfSetupFiles(const std::string &version)
01331 {
01332 
01333     std::vector<std::string> filenames;
01334     filenames.push_back(quartusSetupFileChip1_);
01335     filenames.push_back(quartusSetupFileChip2_);
01336 
01337     for (unsigned int i=0; i<filenames.size(); i++)
01338     {
01339         L1GtVhdlTemplateFile myTemplate;
01340 
01341         myTemplate.open(vhdlDir_+"Templates/"+filenames.at(i));
01342         myTemplate.substitute("version", version);
01343 
01344         std::string tempStr = filenames.at(i);
01345         
01346         if (i==0)
01347             L1GtVhdlTemplateFile::findAndReplaceString(tempStr , "cond1","cond");
01348         if (i==1)
01349             L1GtVhdlTemplateFile::findAndReplaceString(tempStr , "cond2","cond");
01350         
01351         myTemplate.save(chip2OutputSubDir(i+1)+tempStr);
01352         chmod((chip2OutputSubDir(i+1)+tempStr).c_str(), 0666);
01353     }
01354 
01355 }
01356 
01357 void L1GtVhdlWriterCore::msg(const std::string &message)
01358 {
01359     internMessageBuf_.push_back(message);
01360 }
01361 
01362 std::vector<std::string> L1GtVhdlWriterCore::getMsgBuf()
01363 {
01364     return internMessageBuf_;
01365 }
01366 
01367 bool L1GtVhdlWriterCore::getIntVal(const std::map<std::string,int> &map,
01368         const std::string &searchValue, int &intVal)
01369 {
01370     std::map<std::string,int>::const_iterator iter = map.find(searchValue);
01371     if (iter == map.end() )
01372         return false;
01373     intVal = (*iter).second;
01374     return true;
01375 }
01376 
01377 std::string L1GtVhdlWriterCore::index4CondChipVhd(int intval)
01378 {
01379     intval++;
01380     return int2str(intval);
01381 }
01382 
01383 void L1GtVhdlWriterCore::addJetCountsToCond2IntMap(const int chip,
01384         const std::vector<ConditionMap> &conditionMap,
01385         std::map<std::string,int> &conditionToIntegerMap_)
01386 {
01387     ConditionMap jetConditions;
01388 
01389     returnConditionsOfOneClass(TypeJetCounts, CondJetCounts, JetCounts,
01390             conditionMap.at(chip-1), jetConditions);
01391     /*
01392      int counter = 0;
01393 
01394      for (ConditionMap::const_iterator iterCond =  jetConditions.begin(); iterCond !=  jetConditions.end(); iterCond++)
01395      {
01396      conditionToIntegerMap_[iterCond->first]=counter;
01397      counter++;
01398      msg(iterCond->first);
01399      }
01400 
01401      */
01402     
01403     unsigned int maxJetsCountsIndex = 11;
01404     
01405     for (unsigned int i= 0; i<=maxJetsCountsIndex; i++)
01406     {
01407         int counter = 0;
01408 
01409         for (ConditionMap::const_iterator iterCond = jetConditions.begin(); iterCond
01410                 != jetConditions.end(); iterCond++)
01411         {
01412 
01413             L1GtJetCountsTemplate* jetsTemplate =
01414                     static_cast<L1GtJetCountsTemplate*>(iterCond->second);
01415             const std::vector<L1GtJetCountsTemplate::ObjectParameter>* op =
01416                     jetsTemplate->objectParameter();
01417 
01418             conditionToIntegerMap_[iterCond->first]=counter;
01419             if ((*op)[0].countIndex==i)
01420                 counter++;
01421 
01422             //msg(int2str((*op)[0].countIndex));
01423 
01424         }
01425 
01426         numberOfConditions_.at(chip-1).push_back(retNumberOfConditionsString("jet_cnts_"
01427                 +int2str(i)+"_cond", counter));
01428 
01429     }
01430 
01431 }
01432 
01433 void L1GtVhdlWriterCore::writeCond2intMap2File()
01434 {
01435    
01436     
01437     for (unsigned int i=0; i<=1;i++)
01438     {
01439         ConditionMap::const_iterator iterCond =
01440             conditionMap_->at(i).begin();
01441        
01442         std::string filename=chip2OutputSubDir(i+1)+"cond_names_integer.txt";
01443         
01444         std::ofstream outputFile(filename.c_str());
01445         
01446         while (iterCond!=conditionMap_->at(i).end())
01447         {
01448             conditionToIntegerMap_[(*iterCond).first];
01449             
01450             outputFile<<(iterCond->first)<<": "<<conditionToIntegerMap_[(*iterCond).first]<<std::endl;
01451             iterCond++;
01452         }
01453         
01454         outputFile.close();
01455         
01456         chmod(filename.c_str(), 0666);
01457           
01458         
01459     }
01460    
01461     /*
01462     
01463     
01464     const std::vector<ConditionMap> * conditionMap_;
01465     
01466     std::string filename=outputDir_+"cond_names_integer.txt";
01467     std::ofstream outputFile(filename.c_str());
01468 
01469     for (std::map<std::string,int>::const_iterator iterCond =
01470             conditionToIntegerMap_.begin(); iterCond
01471             != conditionToIntegerMap_.end(); iterCond++)
01472     {
01473         outputFile<<(iterCond->first)<<": "<<(iterCond->second)<<std::endl;
01474     }
01475     
01476     */
01477 
01478 }
01479 
01480 std:: string L1GtVhdlWriterCore::buildDefValString(const int &conditionIndex,
01481         const std::vector<int> &values)
01482 {
01483     // has to produce something like 1 =>  ("00000000", "00000000")
01484 
01485     return "1 =>  (\"00000000\", \"00000000\")";
01486 }
01487 
01488 std::string L1GtVhdlWriterCore::getDefValsFromTriggerMenu(
01489         const L1GtConditionType &type, const L1GtObject &object,
01490         const VmeRegister &reg)
01491 {
01492     L1GtConditionCategory category;
01493 
01494     // get condition category from object
01495 
01496     category = getCategoryFromObject(object);
01497 
01498     ConditionMap conditions;
01499     returnConditionsOfOneClass(type, category, object, (*conditionMap_).at(0),
01500             conditions);
01501 
01502     std::string result;
01503 
01504     if (category==CondCalo)
01505     {
01506         for (ConditionMap::const_iterator iterCond =conditions.begin(); iterCond
01507                 != conditions.end(); iterCond++)
01508         {
01509             L1GtCaloTemplate* caloTemplate =
01510                     static_cast<L1GtCaloTemplate*>(iterCond->second);
01511             const std::vector<L1GtCaloTemplate::ObjectParameter>* op =
01512                     caloTemplate->objectParameter();
01513 
01514             unsigned int nObjects = iterCond->second->nrObjects();
01515 
01516             for (unsigned int i =0; i<nObjects; i++)
01517             {
01518                 if (i==0)
01519                     result+="(";
01520                 result +="\"";
01521                 result += int2str((*op).at(i).etThreshold);
01522                 result +="\"";
01523                 if (i!=nObjects-1)
01524                     result +=",";
01525                 else
01526                     result+=")\n";
01527 
01528             }
01529         }
01530     }
01531 
01532     return "-- 0 => (\"00000000\", \"00000000\" ... )";
01533     return result;
01534 }
01535 
01536 void L1GtVhdlWriterCore::writeDefValPkg(
01537         const std::vector<ConditionMap> &conditionMap, const int &chip)
01538 {
01539     // open internal template file
01540     L1GtVhdlTemplateFile internalTemplate;
01541     internalTemplate.open(vhdlDir_+"InternalTemplates/defvalpkg", true);
01542 
01543     // to write the def_value_pkg file it has to be looped over all POSSIBLE types of conditions
01544     // POSSIBLE: calo_3 eg. does not exist and therefore makes no sense..
01545 
01546     // Process muon conditions first
01547 
01548     // temporary buffers for the default value blocks (see internal templates) of the different types.
01549     // in the end those buffers are simply appended to the actual template!
01550     L1GtVhdlTemplateFile muonDefValuesBuffer, caloDefValuesBuffer,
01551             jetCountsDefValuesBuffer, esumsDefValBuffer;
01552 
01553     // Get the default value type strings from internal template
01554     std::vector<std::string> muonDefValTypes;
01555     muonDefValTypes.push_back(internalTemplate.getInternalParameter(stringConstantPtl_));
01556     muonDefValTypes.push_back(internalTemplate.getInternalParameter(stringConstantPth_));
01557     muonDefValTypes.push_back(internalTemplate.getInternalParameter(stringConstantQuality_));
01558     muonDefValTypes.push_back(internalTemplate.getInternalParameter(substParamCharge_));
01559 
01560     int jetCountsMaxIndex = 11;
01561 
01562     // fill jet counts vector (from 1 to 11)
01563     std::vector<std::string> jetCountsDefValTypes;
01564     for (int i = 0; i<=jetCountsMaxIndex; i++)
01565     {
01566         jetCountsDefValTypes.push_back(int2str(i));
01567     }
01568 
01569     // only one default value for calo objects therefore only a empty string
01570     std::vector<std::string> caloDefValTypes;
01571     caloDefValTypes.push_back("");
01572 
01573     // get types of esums defvalues (as firmware strings) from the internal templates
01574     std::vector<std::string> esumsDefValTypes;
01575     esumsDefValTypes.push_back(internalTemplate.getInternalParameter(stringConstantEsumsLow_));
01576     esumsDefValTypes.push_back(internalTemplate.getInternalParameter(stringConstantEsumsHigh_));
01577 
01578     std::map<L1GtConditionType,std::string>::iterator typeIter;
01579 
01580     // prepare a map with all muon relevant types by removing 
01581     // obsolete types from a copy of condType2Str_ map
01582     // should be improved
01583     std::map<L1GtConditionType,std::string> muonTypes = condType2Str_;
01584 
01585     typeIter=muonTypes.find(Type2cor);
01586     muonTypes.erase(typeIter);
01587     typeIter=muonTypes.find(TypeETM);
01588     muonTypes.erase(typeIter);
01589     typeIter=muonTypes.find(TypeETT);
01590     muonTypes.erase(typeIter);
01591     typeIter=muonTypes.find(TypeHTT);
01592     muonTypes.erase(typeIter);
01593     typeIter=muonTypes.find(TypeJetCounts);
01594     muonTypes.erase(typeIter);
01595 
01596     std::map<L1GtConditionType,std::string> caloTypes = muonTypes;
01597     typeIter=caloTypes.find(Type3s);
01598     caloTypes.erase(typeIter);
01599 
01600     // dummy type in order to be able to use the same code as for calo and muon
01601     // this map is also used for esums since there is no difference in treatment 
01602     std::map<L1GtConditionType,std::string> jetCountsTypes;
01603     jetCountsTypes[TypeJetCounts] = "";
01604 
01605     // here the DefValuesBuffer are build (=objects of the class L1GtVhdlTemplateFile
01606     // that are containing all default values and finally can be inserted into the
01607     // def_val_pkg.vhd template
01608 
01609     buildDefValuesBuffer(muonDefValuesBuffer, muonTypes, muonDefValTypes, Mu);
01610 
01611     // loop over all possible calo objects here
01612     for (unsigned int i=0; i<caloObjects_.size(); i++)
01613     {
01614         buildDefValuesBuffer(caloDefValuesBuffer, caloTypes, caloDefValTypes,
01615                 caloObjects_.at(i));
01616     }
01617 
01618     // loop over all possible esums objects here
01619     for (unsigned int i=0; i<esumObjects_.size(); i++)
01620     {
01621         buildDefValuesBuffer(esumsDefValBuffer, jetCountsTypes,
01622                 esumsDefValTypes, esumObjects_.at(i));
01623     }
01624 
01625     // same procedure for jet counts
01626     buildDefValuesBuffer(esumsDefValBuffer, jetCountsTypes,
01627             jetCountsDefValTypes, JetCounts);
01628 
01629     //----------------------In this section the actual output file is written----------------
01630 
01631     // now open the actual template file:
01632 
01633     std::string filename = vhdlTemplateDefValPkg_;
01634 
01635     std::string outputFile = filename;
01636 
01637     // modify output filename
01638     L1GtVhdlTemplateFile::findAndReplaceString(outputFile, ".", int2str(chip)
01639             +".");
01640     L1GtVhdlTemplateFile defValTemplate = openVhdlFileWithCommonHeader(
01641             filename, outputFile);
01642 
01643     // insert the temporary buffers to the template
01644 
01645     // muon default values
01646     defValTemplate.insert(sp(substParamMuonDefVals_), muonDefValuesBuffer);
01647 
01648     // calo default values
01649     defValTemplate.insert(sp(substParamCaloDefVals_), caloDefValuesBuffer);
01650 
01651     // esums default values
01652     defValTemplate.insert(sp(substParamEsumsDefVals_), esumsDefValBuffer);
01653 
01654     // jet counts default values
01655     defValTemplate.insert(sp(substParamJetsDefVals_), jetCountsDefValuesBuffer);
01656 
01657     // close and save the file
01658     
01659     L1GtVhdlTemplateFile::findAndReplaceString(outputFile, "1","");
01660     L1GtVhdlTemplateFile::findAndReplaceString(outputFile, "2","");
01661     
01662     defValTemplate.save(chip2OutputSubDir(chip)+outputFile);
01663     chmod((chip2OutputSubDir(chip)+outputFile).c_str(), 0666);
01664 
01665 }
01666 
01667 bool L1GtVhdlWriterCore::buildDefValuesBuffer(L1GtVhdlTemplateFile &buffer,
01668         const std::map<L1GtConditionType,std::string> &typeList,
01669         const std::vector<std::string> &defValuesList, const L1GtObject &object)
01670 {
01671     // type iterator
01672     std::map<L1GtConditionType,std::string>::const_iterator typeIter;
01673 
01674     for (typeIter=typeList.begin(); typeIter!=typeList.end(); typeIter++)
01675     {
01676 
01677         for (unsigned int i = 0; i<defValuesList.size(); i++)
01678         {
01679 
01680             // open a new internale template
01681             L1GtVhdlTemplateFile internalTemplate;
01682             internalTemplate.open(vhdlDir_+"InternalTemplates/defvalpkg", true);
01683 
01684             std::string
01685                     idString =
01686                             internalTemplate.getInternalParameter(stringConstantDefValId_);
01687 
01688             // The following three steps convert "$(particle)_$(type)_$(defvaltype)_def_val"
01689             // to e.g. "muon_1s_et_def_val"
01690 
01691             // replace the substiution parameter particle with muon - thereby parameterString is a
01692             // reference and therefore changed.
01693             L1GtVhdlTemplateFile::findAndReplaceString(idString,
01694                     sp(substParamParticle_), objType2Str_[object]);
01695 
01696             // replace the substiution parameter particle with muon
01697             L1GtVhdlTemplateFile::findAndReplaceString(idString,
01698                     sp(substParamType_), typeIter->second);
01699 
01700             // replace the substiution parameter defvaltype with muon
01701             L1GtVhdlTemplateFile::findAndReplaceString(idString,
01702                     sp(substParamDefValType_), defValuesList.at(i));
01703 
01704             // usage of the subsitute routine:
01705             // first the substitution parameter, then the content
01706             internalTemplate.substitute(substParamDefValId_, idString);
01707 
01708             // !here the body of the internale template is build - almost done!
01709 
01710             // The body looks as follows $(def_val), $(max_nr), $(content) and
01711             // $(others) have to be subsituted 
01712 
01713             /*
01714              * CONSTANT $(def_val) : $(calo_or_muon)_maxnr$(max_nr)vector8_arr := 
01715              * (
01716              * $(content)
01717              * $(others)
01718              * );
01719              */
01720 
01721             // substitute the particle
01722             internalTemplate.substitute(substParamParticle_,
01723                     objType2Str_[object]);
01724 
01725             // go on with max number - get the correct expression for max number from the internal template
01726             internalTemplate.substitute(substParamMaxNr_,
01727                     internalTemplate.getInternalParameter(substParamMaxNr_+"_"
01728                             +condType2Str_[typeIter->first]));
01729 
01730             // now we have to choose the correct OTHERS string for the
01731             // internal template. The identifier for the OTHERS string
01732             // is the condition type (as firmware string) therefore:
01733 
01734             // we get it as follows
01735             std::string othersString;
01736 
01737             // jet_cnts are a special case
01738             if (object==JetCounts || object==HTT || object==ETM || object==ETT )
01739                 othersString
01740                         = internalTemplate.getInternalParameter(objType2Str_[JetCounts]);
01741             else
01742                 othersString
01743                         = internalTemplate.getInternalParameter(typeIter->second);
01744 
01745             internalTemplate.substitute(substParamOthers_, othersString);
01746 
01747             // the actual content, taken from the trigger menu
01748 
01749             std::string content = getDefValsFromTriggerMenu(typeIter->first,
01750                     object, RegEtThreshold);
01751 
01752             internalTemplate.substitute(substParamContent_, content);
01753 
01754             // Finally the parameter $(calo_or_muon) is left open
01755             std::string caloOrMuonStr = stringConstantCalo_;
01756            
01757             if (object == Mu)
01758                 caloOrMuonStr = objType2Str_[Mu];
01759             
01760             internalTemplate.substitute(substParamCaloOrMuon_, caloOrMuonStr);
01761             
01762             // The internal template has been processed and now can be added to the buffer..
01763             buffer.append(internalTemplate);
01764 
01765         }
01766 
01767     }
01768 
01769     return true;
01770 }
01771 
01772 void L1GtVhdlWriterCore::countCondsAndAdd2NumberVec(
01773         const L1GtConditionType &type, const L1GtConditionCategory &category,
01774         const L1GtObject &object, const ConditionMap &map,
01775         ConditionMap &outputMap, const int &condChip)
01776 {
01777 
01778     // Get the absoloute amount of conditions
01779 
01780     int number;
01781 
01782     if (returnConditionsOfOneClass(type, category, object, map, outputMap))
01783         number=outputMap.size();
01784     else
01785         number=0;
01786 
01787     std::string initstr = retNumberOfConditionsString(objType2Str_[object]+"_"
01788             +condType2Str_[type], number);
01789 
01790     numberOfConditions_.at(condChip-1).push_back(initstr);
01791 
01792 }
01793 
01794 void L1GtVhdlWriterCore::initializeDeltaConditions()
01795 {
01796 
01797     for (unsigned int k=0; k<=1; k++)
01798     {
01799 
01800         // combine muon with calo particles
01801         for (unsigned int i=0; i<caloObjects_.size(); i++)
01802         {
01803             numberOfConditions_.at(k).push_back(retNumberOfConditionsString(objType2Str_[Mu]
01804                     +"_"+objType2Str_[caloObjects_.at(i)], 0));
01805         }
01806 
01807         // combine etm with muon
01808         numberOfConditions_.at(k).push_back(retNumberOfConditionsString(objType2Str_[ETM]+"_"
01809                 +objType2Str_[Mu], 0));
01810 
01811         // combine etm with calo particles
01812         for (unsigned int i=0; i<caloObjects_.size(); i++)
01813         {
01814             numberOfConditions_.at(k).push_back(retNumberOfConditionsString(objType2Str_[ETM]
01815                     +"_"+objType2Str_[caloObjects_.at(i)], 0));
01816         }
01817 
01818         std::vector<L1GtObject> caloObjectsCp = caloObjects_;
01819 
01820         while (caloObjectsCp.size()>0)
01821         {
01822             std::vector<L1GtObject>::iterator iter=caloObjectsCp.begin();
01823             L1GtObject firstPartner = (*iter);
01824             caloObjectsCp.erase(iter);
01825 
01826             iter=caloObjectsCp.begin();
01827             while (iter!=caloObjectsCp.end())
01828             {
01829                 numberOfConditions_.at(k).push_back(retNumberOfConditionsString(
01830                         objType2Str_[firstPartner]+"_"+objType2Str_[(*iter)], 0));
01831                 iter++;
01832             }
01833 
01834         }
01835 
01836     }
01837 }
01838 
01839 L1GtConditionCategory L1GtVhdlWriterCore::getCategoryFromObject(
01840         const L1GtObject &object)
01841 {
01842 
01843     L1GtConditionCategory category;
01844 
01845     if (object==Mu)
01846         category = CondMuon;
01847     else if (object==ETM || object==HTT || object==ETT)
01848         category = CondEnergySum;
01849     else if (object==IsoEG|| object==NoIsoEG || object==CenJet || object
01850             ==ForJet || object==TauJet)
01851         category = CondCalo;
01852     else if (object==IsoEG|| object==NoIsoEG || object==CenJet || object
01853             ==ForJet || object==TauJet)
01854         category = CondCalo;
01855     else if (object==JetCounts)
01856         category = CondJetCounts;
01857     else 
01858         category=CondNull;
01859 
01860     return category;
01861 
01862 }
01863 
01864 void L1GtVhdlWriterCore::printConditionsOfCategory(
01865         const L1GtConditionCategory &category, const ConditionMap &map)
01866 {
01867     int counter =0;
01868     for (ConditionMap::const_iterator iterCond = map.begin(); iterCond
01869             != map.end(); iterCond++)
01870     {
01871         msg(iterCond->first);
01872         counter++;
01873     }
01874 
01875     msg("Total Occurances: "+int2str(counter));
01876 
01877 }
01878 
01879 void L1GtVhdlWriterCore::writeCondChipPkg(const int &chip)
01880 {
01881 
01882     // build filename
01883 
01884     std::string filename;
01885 
01886     if (chip==1)
01887         filename = vhdlTemplateCondChipPkg1_;
01888     else if (chip==2)
01889         filename = vhdlTemplateCondChipPkg2_;
01890 
01891     // write the output
01892     L1GtVhdlTemplateFile myTemplate = openVhdlFileWithCommonHeader(filename,
01893             filename);
01894     
01895     myTemplate.substitute("version",version_);
01896     
01897     myTemplate.insert("$(conditions_nr)", numberOfConditions_.at(chip-1));
01898    
01899     L1GtVhdlTemplateFile::findAndReplaceString(filename, "1","");
01900     L1GtVhdlTemplateFile::findAndReplaceString(filename, "2","");
01901     
01902     myTemplate.save(chip2OutputSubDir(chip)+filename);
01903     chmod((chip2OutputSubDir(chip)+filename).c_str(), 0666);
01904 }
01905 
01906 std::string L1GtVhdlWriterCore::retNumberOfConditionsString(
01907         const std::string &typeStr, const int &number)
01908 {
01909     std::string initstr=stringConstantConstantNr_+typeStr+" : integer := "+int2str(number)
01910             +";";
01911 
01912     return initstr;
01913 }
01914 
01915 std::map<std::string,int> L1GtVhdlWriterCore::getCond2IntMap()
01916 {
01917     return conditionToIntegerMap_;
01918 }
01919 
01920 std::string L1GtVhdlWriterCore::sp(const std::string &name)
01921 {
01922     return "$("+name+")";
01923 }
01924