Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:54

0001 #ifndef L1GCTLUTFROMFILE_H_
0002 #define L1GCTLUTFROMFILE_H_
0003 
0004 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctLut.h"
0005 
0006 #include <vector>
0007 #include <fstream>
0008 #include <cassert>
0009 
0010 template <int NAddressBits, int NDataBits>
0011 class L1GctLutFromFile : public L1GctLut<NAddressBits, NDataBits> {
0012 public:
0013   static L1GctLutFromFile<NAddressBits, NDataBits>* setupLut(const std::string filename);
0014   virtual ~L1GctLutFromFile();
0015 
0016   void readFromFile(const std::string filename);
0017 
0018 protected:
0019   L1GctLutFromFile();
0020 
0021   virtual uint16_t value(const uint16_t lutAddress) const;
0022 
0023 private:
0024   std::vector<uint16_t> m_lutContents;
0025 };
0026 
0027 template <int NAddressBits, int NDataBits>
0028 L1GctLutFromFile<NAddressBits, NDataBits>* L1GctLutFromFile<NAddressBits, NDataBits>::setupLut(
0029     const std::string filename) {
0030   L1GctLutFromFile<NAddressBits, NDataBits>* newLut = new L1GctLutFromFile<NAddressBits, NDataBits>();
0031   newLut->readFromFile(filename);
0032   return newLut;
0033 }
0034 
0035 template <int NAddressBits, int NDataBits>
0036 L1GctLutFromFile<NAddressBits, NDataBits>::L1GctLutFromFile()
0037     : L1GctLut<NAddressBits, NDataBits>(), m_lutContents(1 << NAddressBits) {}
0038 
0039 template <int NAddressBits, int NDataBits>
0040 L1GctLutFromFile<NAddressBits, NDataBits>::~L1GctLutFromFile() {}
0041 
0042 template <int NAddressBits, int NDataBits>
0043 void L1GctLutFromFile<NAddressBits, NDataBits>::readFromFile(const std::string filename) {
0044   static const unsigned maxAddress = L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK;
0045   static const unsigned rowLength = 16;
0046 
0047   std::ifstream inFile;
0048   std::string strFromFile;
0049   inFile.open(filename.c_str(), std::ios::in);
0050 
0051   // Read input values in hex
0052   inFile >> std::hex;
0053 
0054   // Read and discard the first lines of the file, looking for
0055   // a line entirely composed of '-' and '+' characters
0056   while (std::getline(inFile, strFromFile).good()) {
0057     if (strFromFile.length() > 0 && strFromFile.find_first_not_of("-+") == std::string::npos)
0058       break;
0059   }
0060 
0061   // Now read the lut data
0062   unsigned a = 0;
0063   while (a <= maxAddress) {
0064     unsigned val;
0065     inFile >> val;
0066     assert(val == a);
0067     inFile.ignore();
0068     for (unsigned c = 0; c < rowLength && a <= maxAddress; c++) {
0069       inFile >> val;
0070       m_lutContents.at(a++) = static_cast<uint16_t>(val);
0071     }
0072   }
0073   // All values read
0074   if (inFile.get() == '\n') {
0075     inFile.get();
0076   }
0077   L1GctLut<NAddressBits, NDataBits>::m_setupOk = (inFile.eof() && (m_lutContents.size() == a));
0078   inFile.close();
0079 }
0080 
0081 template <int NAddressBits, int NDataBits>
0082 uint16_t L1GctLutFromFile<NAddressBits, NDataBits>::value(const uint16_t lutAddress) const {
0083   unsigned Address = static_cast<unsigned>(lutAddress & L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK);
0084   assert(Address < m_lutContents.size());
0085   return m_lutContents.at(Address);
0086 }
0087 
0088 #endif /*L1GCTLUTFROMFILE_H_*/