CMS 3D CMS Logo

CSCLUTReader.cc
Go to the documentation of this file.
4 
6  : fname_(fname),
7  nrBitsAddress_(0),
8  nrBitsData_(0),
9  addressMask_(0),
10  dataMask_(0),
11  data_(),
12  m_codeInWidth(12),
13  m_outWidth(32) {
14  if (fname != std::string("")) {
15  load(fname);
16  } else {
17  initialize();
18  }
19 }
20 
21 // I/O functions
22 void CSCLUTReader::save(std::ofstream& output) { write(output); }
23 
24 float CSCLUTReader::data(unsigned int address) const {
25  return (address & addressMask_) < data_.size() ? data_[address] : 0;
26 }
27 
28 int CSCLUTReader::load(const std::string& inFileName) {
29  std::ifstream fstream;
30  fstream.open(edm::FileInPath(inFileName.c_str()).fullPath());
31  if (!fstream.good()) {
32  fstream.close();
33  edm::LogError("CSCLUTReader") << "Failed to open LUT file: " << inFileName;
34  }
35  int readCode = read(fstream);
36 
37  m_initialized = true;
38  fstream.close();
39 
40  return readCode;
41 }
42 
43 float CSCLUTReader::lookup(int code) const {
44  if (m_initialized) {
45  return lookupPacked(code);
46  } else {
47  edm::LogError("CSCLUTReader") << "LUT not initialized. " << fname_;
48  return 0;
49  }
50 }
51 
52 float CSCLUTReader::lookupPacked(const int input) const {
53  if (m_initialized) {
54  return data((unsigned int)input);
55  } else {
56  edm::LogError("CSCLUTReader") << "If you're not loading a LUT from file you need to implement lookupPacked.";
57  return 0;
58  }
59 }
60 
62  if (empty()) {
63  std::stringstream stream;
64  stream << "#<header> V1 " << m_codeInWidth << " " << m_outWidth << " </header> " << std::endl;
65  for (int in = 0; in < (1 << m_codeInWidth); ++in) {
66  int out = lookup(in);
67  stream << in << " " << out << std::endl;
68  }
69  read(stream);
70  }
71  m_initialized = true;
72 }
73 
74 unsigned CSCLUTReader::checkedInput(unsigned in, unsigned maxWidth) const {
75  unsigned maxIn = (1 << maxWidth) - 1;
76  return (in < maxIn ? in : maxIn);
77 }
78 
79 int CSCLUTReader::read(std::istream& stream) {
80  data_.clear();
81 
82  int readHeaderCode = readHeader(stream);
83  if (readHeaderCode != SUCCESS) {
84  edm::LogError("CSCLUTReader") << "Failed to read header code. " << fname_;
85  return readHeaderCode;
86  }
87 
88  std::vector<std::pair<unsigned int, float> > entries;
89  unsigned int maxAddress = addressMask_;
91 
92  while (std::getline(stream, line)) {
93  line.erase(std::find(line.begin(), line.end(), '#'), line.end()); //ignore comments
94  std::istringstream lineStream(line);
95  std::pair<unsigned int, float> entry;
96  while (lineStream >> entry.first >> entry.second) {
97  entry.first &= addressMask_;
98  // entry.second &= dataMask_;
99  entries.push_back(entry);
100  if (entry.first > maxAddress || maxAddress == addressMask_)
101  maxAddress = entry.first;
102  }
103  }
104  std::sort(entries.begin(), entries.end());
105  if (entries.empty()) {
106  //log the error we read nothing
107  return NO_ENTRIES;
108  }
109  //this check is redundant as dups are also picked up by the next check but might make for easier debugging
110  if (std::adjacent_find(entries.begin(), entries.end(), [](auto const& a, auto const& b) {
111  return a.first == b.first;
112  }) != entries.end()) {
113  //log the error that we have duplicate addresses once masked
114  return DUP_ENTRIES;
115  }
116  if (entries.front().first != 0 ||
117  std::adjacent_find(entries.begin(), entries.end(), [](auto const& a, auto const& b) {
118  return a.first + 1 != b.first;
119  }) != entries.end()) {
120  //log the error that we have a missing entry
121  return MISS_ENTRIES;
122  }
123 
124  if (maxAddress != std::numeric_limits<unsigned int>::max())
125  data_.resize(maxAddress + 1, 0);
126  else {
127  //log the error that we have more addresses than we can deal with (which is 4gb so something probably has gone wrong anyways)
128  return MAX_ADDRESS_OUTOFRANGE;
129  }
130 
131  std::transform(entries.begin(), entries.end(), data_.begin(), [](auto const& x) { return x.second; });
132  return SUCCESS;
133 }
134 
135 void CSCLUTReader::write(std::ostream& stream) const {
136  stream << "#<header> V1 " << nrBitsAddress_ << " " << nrBitsData_ << " </header> " << std::endl;
137  for (unsigned int address = 0; address < data_.size(); address++) {
138  stream << (address & addressMask_) << " " << data(address) << std::endl;
139  }
140 }
141 
142 unsigned int CSCLUTReader::maxSize() const {
144 }
145 
146 int CSCLUTReader::readHeader(std::istream& stream) {
147  int startPos = stream.tellg(); //we are going to reset to this position before we exit
149  while (std::getline(stream, line)) {
150  if (line.find("#<header>") == 0) { //line
151  std::istringstream lineStream(line);
152 
153  std::string version; //currently not doing anything with this
154  std::string headerField; //currently not doing anything with this
155  if (lineStream >> headerField >> version >> nrBitsAddress_ >> nrBitsData_) {
156  addressMask_ = nrBitsAddress_ != 32 ? (0x1 << nrBitsAddress_) - 1 : ~0x0;
157  dataMask_ = (0x1 << nrBitsData_) - 1;
158  stream.seekg(startPos);
159  return SUCCESS;
160  }
161  }
162  }
163 
164  nrBitsAddress_ = 0;
165  nrBitsData_ = 0;
166  addressMask_ = (0x1 << nrBitsAddress_) - 1;
167  dataMask_ = (0x1 << nrBitsData_) - 1;
168 
169  stream.seekg(startPos);
170  return NO_HEADER;
171 }
input
static const std::string input
Definition: EdmProvDump.cc:48
CSCLUTReader::lookupPacked
float lookupPacked(const int input) const
Definition: CSCLUTReader.cc:52
MessageLogger.h
CSCLUTReader::maxSize
unsigned int maxSize() const
Definition: CSCLUTReader.cc:142
CSCLUTReader::save
void save(std::ofstream &output)
Definition: CSCLUTReader.cc:22
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
CSCLUTReader::addressMask_
unsigned int addressMask_
Definition: CSCLUTReader.h:57
CSCLUTReader::initialize
void initialize()
Definition: CSCLUTReader.cc:61
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
CSCLUTReader::write
void write(std::ostream &stream) const
Definition: CSCLUTReader.cc:135
mps_splice.entry
entry
Definition: mps_splice.py:68
cms::cuda::stream
uint32_t const T *__restrict__ const uint32_t *__restrict__ int32_t int Histo::index_type cudaStream_t stream
Definition: HistoContainer.h:51
CSCLUTReader::data_
std::vector< float > data_
Definition: CSCLUTReader.h:60
DDAxes::x
CSCLUTReader::MISS_ENTRIES
Definition: CSCLUTReader.h:20
CSCLUTReader::readHeader
int readHeader(std::istream &)
Definition: CSCLUTReader.cc:146
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
CSCLUTReader::data
float data(unsigned int address) const
Definition: CSCLUTReader.cc:24
edm::FileInPath
Definition: FileInPath.h:61
CSCLUTReader::MAX_ADDRESS_OUTOFRANGE
Definition: CSCLUTReader.h:21
CSCLUTReader::CSCLUTReader
CSCLUTReader(const std::string &)
Definition: CSCLUTReader.cc:5
CSCLUTReader::dataMask_
unsigned int dataMask_
Definition: CSCLUTReader.h:58
CSCLUTReader::m_initialized
bool m_initialized
Definition: CSCLUTReader.h:64
HcalDetIdTransform::transform
unsigned transform(const HcalDetId &id, unsigned transformCode)
Definition: HcalDetIdTransform.cc:7
CSCLUTReader.h
CSCLUTReader::DUP_ENTRIES
Definition: CSCLUTReader.h:19
b
double b
Definition: hdecay.h:118
CSCLUTReader::lookup
float lookup(int code) const
Definition: CSCLUTReader.cc:43
a
double a
Definition: hdecay.h:119
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
CSCLUTReader::nrBitsAddress_
unsigned int nrBitsAddress_
Definition: CSCLUTReader.h:55
recoMuon::in
Definition: RecoMuonEnumerators.h:6
CSCLUTReader::read
int read(std::istream &stream)
Definition: CSCLUTReader.cc:79
CSCLUTReader::SUCCESS
Definition: CSCLUTReader.h:17
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
CSCLUTReader::m_outWidth
unsigned m_outWidth
Definition: CSCLUTReader.h:63
alignmentValidation.fname
string fname
main script
Definition: alignmentValidation.py:959
CSCLUTReader::checkedInput
unsigned checkedInput(unsigned in, unsigned maxWidth) const
Definition: CSCLUTReader.cc:74
CSCLUTReader::NO_ENTRIES
Definition: CSCLUTReader.h:18
CSCLUTReader::fname_
std::string fname_
Definition: CSCLUTReader.h:52
CSCLUTReader::nrBitsData_
unsigned int nrBitsData_
Definition: CSCLUTReader.h:56
SiStripMonitorCluster_cfi.maxWidth
maxWidth
Definition: SiStripMonitorCluster_cfi.py:143
Exception.h
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
CSCLUTReader::m_codeInWidth
int m_codeInWidth
Definition: CSCLUTReader.h:62
CSCLUTReader::NO_HEADER
Definition: CSCLUTReader.h:22
mps_splice.line
line
Definition: mps_splice.py:76
BeamSplash_cfg.version
version
Definition: BeamSplash_cfg.py:45
CSCLUTReader::load
int load(const std::string &inFileName)
Definition: CSCLUTReader.cc:28
fname_
struct @771 fname_
CSCLUTReader::empty
bool empty() const
Definition: CSCLUTReader.h:49