CMS 3D CMS Logo

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