Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef L1GtConfigProducers_L1GtVhdlWriterCore_h
0002 #define L1GtConfigProducers_L1GtVhdlWriterCore_h
0003 
0004 /**
0005  * \class L1GtVhdlWriterCore
0006  *
0007  *
0008  * \Description writes the actual VHDL code and stores global information like the common header.
0009  *  This class is using parameter maps containing the substitution parameters and their content.
0010  *
0011  * Implementation:
0012  *    <TODO: enter implementation details>
0013  *
0014  * \author Philipp Wagner
0015  *
0016  *
0017  */
0018 
0019 // system include files
0020 
0021 #include <iostream>
0022 #include <fstream>
0023 #include <map>
0024 #include <string>
0025 #include <vector>
0026 
0027 // CMSSW headers
0028 #include "L1GtVhdlTemplateFile.h"
0029 
0030 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenuFwd.h"
0031 #include "CondFormats/L1TObjects/interface/L1GtFwd.h"
0032 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetupFwd.h"
0033 
0034 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
0035 #include "CondFormats/L1TObjects/interface/L1GtCaloTemplate.h"
0036 #include "CondFormats/L1TObjects/interface/L1GtEnergySumTemplate.h"
0037 
0038 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlDefinitions.h"
0039 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterBitManager.h"
0040 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlWriterCore.h"
0041 
0042 class L1GtVhdlWriterCore : public L1GtVhdlDefinitions {
0043 public:
0044   /// constructor
0045   L1GtVhdlWriterCore(const std::string &templatesDirectory, const std::string &outputDirectory, const bool &debug);
0046 
0047   /// destructor
0048   ~L1GtVhdlWriterCore() override;
0049 
0050   /// returns all condition of the same class. Conditions belong to one class if they are matching
0051   /// in type (Type1s, Type2s..) category (CondMuon, CondCalo,)and are defined for the same object (Mu, fwdJet..)
0052   /// \param map is the ConditionMap that shall be filtered
0053   /// \param conditionToIntegerMap is the condition is added to this conversion map
0054   /// \param outputMap here the conditions matching the requirements are stored
0055   bool returnConditionsOfOneClass(const L1GtConditionType &type,
0056                                   const L1GtConditionCategory &category,
0057                                   const L1GtObject &object,
0058                                   const ConditionMap &map,
0059                                   ConditionMap &outputMap);
0060 
0061   /// builds a parameter map that can be used by the L1GtVhdlWriterCore class
0062   /// the output is written into the parameter &muonParameters;
0063   /// \param conditionToIntegerMap is the condition is added to this conversion map
0064   /// \param muonParameters here the routine stores the content of the subsitution parameters
0065   /// \param conditionMap containing the input
0066   void getMuonSetupContentFromTriggerMenu(const unsigned short int &condChip,
0067                                           std::map<std::string, std::string> &muonParameters);
0068 
0069   /// builds the parameters particle_common for the cond_chip.vhd's
0070   bool getSubstParamCommonFromTriggerMenu(const unsigned short int &condChip,
0071                                           L1GtVhdlTemplateFile &particle,
0072                                           const L1GtObject &object,
0073                                           const L1GtConditionCategory &category,
0074                                           std::string &parameterStr);
0075 
0076   bool getCaloSetupContentFromTriggerMenu(const unsigned short int &condChip,
0077                                           std::map<std::string, std::string> &caloParameters,
0078                                           const L1GtObject &caloObject);
0079 
0080   /// contains only one subsitution parameter which is stored in reference energySumParameter
0081   bool getEsumsSetupContentFromTriggerMenu(const unsigned short int &condChip,
0082                                            const L1GtObject &object,
0083                                            std::string &energySumParameter);
0084 
0085   /// builds the substitution parameters for the cond_chip.vhd
0086   /// \condChip the condition chip that will be processed
0087   /// \param conditionToIntegerMap this has to be a already FILLED conversion map.
0088   ///  therefore this routine has to be called after all those which are adding information
0089   ///  to the conditionToIntegerMap map (getMuonSetupContentFromTriggerMenu, buildCaloParameterMap...)
0090   /// \param templates in this map the final content for the subsitution parameters is stored in
0091   /// VHDL template file format
0092   bool getCondChipVhdContentFromTriggerMenu(const unsigned short int &condChip,
0093                                             std::map<std::string, L1GtVhdlTemplateFile> &templates,
0094                                             std::map<std::string, std::string> &commonParams);
0095 
0096   std::string retNumberOfConditionsString(const std::string &typeStr, const int &number);
0097 
0098   bool findObjectType(const L1GtObject &object, ConditionMap &map);
0099 
0100   // builds the parameters particle_common for the cond_chip.vhd's
0101   // bool buildCommonParameter(L1GtVhdlTemplateFile &particle,const L1GtObject &object, const L1GtConditionCategory &category, std::string &parameterStr, const ConditionMap &conditionMap);
0102 
0103   /// calculates the integer value for jet counts conditions and furthermore counts
0104   /// how many jet counts of one type are in trigger menu
0105   void addJetCountsToCond2IntMap(const int chip,
0106                                  const std::vector<ConditionMap> &conditionMap,
0107                                  std::map<std::string, int> &conditionToIntegerMap);
0108 
0109   /// processes algorithm map delivered by parser, replaces condition names by types and serial numbers,
0110   /// splits the map in a vector two seperate ones for the two condition chips
0111   bool processAlgorithmMap(std::vector<std::map<int, std::string> > &algoStrings);
0112 
0113   /// builds the common header for all files
0114   void buildCommonHeader(std::map<std::string, std::string> &headerParameters,
0115                          const std::vector<std::string> &connectedChannels);
0116 
0117   /// opens a new template file and inserts the common header
0118   L1GtVhdlTemplateFile openVhdlFileWithCommonHeader(const std::string &filename, const std::string &outputFilename);
0119 
0120   /// returns the common header
0121   L1GtVhdlTemplateFile retrunCommonHeader();
0122 
0123   /// prints the common header
0124   void printCommonHeader();
0125 
0126   /// produces the firmware code
0127   bool makeFirmware(const std::vector<ConditionMap> &conditionMap, const AlgorithmMap &algorithmMap);
0128 
0129   /// builds muon setup files
0130   void writeMuonSetupVhdl(std::map<std::string, std::string> &muonParameters,
0131                           const std::string &particle,
0132                           unsigned short int &condChip);
0133 
0134   /// builds cond_chip.vhds
0135   void writeConditionChipSetup(const std::map<std::string, L1GtVhdlTemplateFile> &templates,
0136                                const std::map<std::string, std::string> &common,
0137                                const unsigned short int &chip);
0138 
0139   /// writes def_val_pkg.vhd
0140   void writeDefValPkg(const std::vector<ConditionMap> &conditionMap, const int &chip);
0141 
0142   /// builds etm setup files
0143   void writeEtmSetup(std::string &etmString, const int &condChip);
0144 
0145   /// builds the prealgo_and_or setup
0146   void writeAlgoSetup(std::vector<std::map<int, std::string> > &algoStrings);
0147 
0148   /// builds the two quartus setup files. This routine is called in buildCommonHeader!
0149   void writeQsfSetupFiles(const std::string &version);
0150 
0151   /// converts a integer into a string
0152   static std::string int2str(const int &integerValue);
0153 
0154   /// checks weather value searchValue exists in a <string,int> map, saves it in &intVal if it exists and returns false if not
0155   bool getIntVal(const std::map<std::string, int> &map, const std::string &searchValue, int &intVal);
0156 
0157   /// returns the templates path
0158   std::string gtTemplatesPath();
0159 
0160   /// adds a string to intern message buffer
0161   void msg(const std::string &message);
0162 
0163   /// returns intern message buffer
0164   std::vector<std::string> getMsgBuf();
0165 
0166   /// builds the index for the cond_chip vhds. (They have to start at 1 so one has to be added
0167   /// to the value in conversion map)
0168   std::string index4CondChipVhd(int intval);
0169 
0170   /// returns conditions of a class in output map and counts the occurance of all conditions of a
0171   /// certain type at the same time with the result being stored in numberOfConditions_ in the
0172   /// following format: CONSTANT nr_muon_3 : integer := 0; Tis is the format for the cond_chip_pkg files.
0173   void countCondsAndAdd2NumberVec(const L1GtConditionType &type,
0174                                   const L1GtConditionCategory &category,
0175                                   const L1GtObject &object,
0176                                   const ConditionMap &map,
0177                                   ConditionMap &outputMap,
0178                                   const int &condChip);
0179 
0180   void initializeDeltaConditions();
0181 
0182   /// produces a control output file for condition to integer conversion
0183   void writeCond2intMap2File();
0184 
0185   /// for debuggin
0186   void printConditionsOfCategory(const L1GtConditionCategory &category, const ConditionMap &map);
0187 
0188   /// gets condition category from object
0189   L1GtConditionCategory getCategoryFromObject(const L1GtObject &object);
0190 
0191   /// builds the string representing a condition in def_val_pkg.vhd. Parameters are condition index and a vector
0192   /// containig default values.
0193   std::string buildDefValString(const int &conditionIndex, const std::vector<int> &values);
0194 
0195   void writeCondChipPkg(const int &chip);
0196 
0197   std::map<std::string, int> getCond2IntMap();
0198 
0199   /// helper, used by writeDefValPkg. Builds default values buffer for each object type.
0200   /// this buffer finally is inserte in the def_val_pkg.vhd template file
0201   bool buildDefValuesBuffer(L1GtVhdlTemplateFile &buffer,
0202                             const std::map<L1GtConditionType, std::string> &typeList,
0203                             const std::vector<std::string> &defValuesList,
0204                             const L1GtObject &object);
0205 
0206   /// this routine extracts default values for def_val_pkg.vhd from trigger menu.
0207   /// it is used from buildDefValuesBuffer.
0208   std::string getDefValsFromTriggerMenu(const L1GtConditionType &type,
0209                                         const L1GtObject &object,
0210                                         const VmeRegister &reg);
0211 
0212   /// converts string name to substiution parameter synthax; name --> $(name)
0213   std::string sp(const std::string &name);
0214 
0215   std::string chip2OutputSubDir(const int &chip);
0216 
0217 private:
0218   /// condition map
0219   const std::vector<ConditionMap> *conditionMap_;
0220 
0221   /// algorithm map
0222   const AlgorithmMap *algorithmMap_;
0223 
0224   /// stores to condition name to integer conversion table
0225   std::map<std::string, int> conditionToIntegerMap_;
0226 
0227   /// converts L1GtConditionType to firmware string
0228   std::map<L1GtConditionType, std::string> condType2Str_;
0229 
0230   /// converts L1GtObject to calo_nr
0231   std::map<L1GtObject, std::string> caloType2Int_;
0232 
0233   /// converts L1GtObject to string
0234   std::map<L1GtObject, std::string> objType2Str_;
0235 
0236   /// list of all possible calo objects
0237   std::vector<L1GtObject> caloObjects_;
0238 
0239   /// list of all possible esums objects
0240   std::vector<L1GtObject> esumObjects_;
0241 
0242   /// templates directory
0243   std::string vhdlDir_;
0244 
0245   /// output directory
0246   std::string outputDir_;
0247 
0248   /// bit manager for bit operations
0249   L1GtVhdlWriterBitManager bm_;
0250 
0251   std::string version_;
0252 
0253   /// common header for all files
0254   L1GtVhdlTemplateFile commonHeader_;
0255 
0256   std::vector<std::string> internMessageBuf_;
0257 
0258   /// vector containing the initialization of all conditions
0259   std::vector<std::vector<std::string> > numberOfConditions_;
0260 
0261   /// class will produce some additional debugging output if set
0262   bool debugMode_;
0263 };
0264 #endif /*L1GtConfigProducers_L1GtVhdlWriterCore_h*/