CMS 3D CMS Logo

LUT.cc
Go to the documentation of this file.
2 
3 #include <sstream>
4 #include <string>
5 #include <algorithm>
6 
7 //reads in the file
8 //the format is "address payload"
9 //all commments are ignored (start with '#') except for the header comment (starts with #<header>)
10 //currently ignores anything else on the line after the "address payload" and assumes they come first
11 int l1t::LUT::read(std::istream& stream)
12 {
13  data_.clear();
14 
15  int readHeaderCode = readHeader_(stream);
16  if(readHeaderCode!=SUCCESS) return readHeaderCode;
17 
18  std::vector<std::pair<unsigned int,int> > entries;
19  unsigned int maxAddress=addressMask_;
21 
22  while(std::getline(stream,line)){
23  line.erase( std::find( line.begin(), line.end(), '#' ), line.end() ); //ignore comments
24  std::istringstream lineStream(line);
25  std::pair<unsigned int,int> entry;
26  while(lineStream >> entry.first >> entry.second ){
27  entry.first&=addressMask_;
28  entry.second&=dataMask_;
29  entries.push_back(entry);
30  if(entry.first>maxAddress || maxAddress==addressMask_) maxAddress=entry.first;
31  }
32  }
33  std::sort(entries.begin(),entries.end());
34  if(entries.empty()){
35  //log the error we read nothing
36  return NO_ENTRIES;
37  }
38  //this check is redundant as dups are also picked up by the next check but might make for easier debugging
39  if(std::adjacent_find(entries.begin(),entries.end(),
40  [](auto const& a, auto const& b) {
41  return a.first == b.first;
42  }) != entries.end()) {
43  //log the error that we have duplicate addresses once masked
44  return DUP_ENTRIES;
45  }
46  if(entries.front().first!=0 ||
47  std::adjacent_find(entries.begin(),entries.end(),
48  [](auto const& a, auto const& b) {
49  return a.first + 1 != b.first;
50  }) !=entries.end()) {
51  //log the error that we have a missing entry
52  return MISS_ENTRIES;
53  }
54 
55 
56  if(maxAddress!=std::numeric_limits<unsigned int>::max()) data_.resize(maxAddress+1,0);
57  else{
58  //log the error that we have more addresses than we can deal with (which is 4gb so something probably has gone wrong anyways)
60  }
61 
62  std::transform(entries.begin(),entries.end(),data_.begin(), [](auto const& x){return x.second; });
63  return SUCCESS;
64 
65 }
66 
67 void l1t::LUT::write(std::ostream& stream)const
68 {
69  stream <<"#<header> V1 "<<nrBitsAddress_<<" "<<nrBitsData_<<" </header> "<<std::endl;
70  for(unsigned int address=0;address<data_.size();address++){
71  stream << (address&addressMask_)<<" "<<data(address)<<std::endl;
72  }
73 }
74 
75 int l1t::LUT::readHeader_(std::istream& stream)
76 {
77 
78  int startPos=stream.tellg(); //we are going to reset to this position before we exit
80  while(std::getline(stream,line)){
81  if(line.find("#<header>")==0){ //line
82  std::istringstream lineStream(line);
83 
84  std::string version; //currently not doing anything with this
85  std::string headerField; //currently not doing anything with this
86  if(lineStream >> headerField >> version >> nrBitsAddress_ >> nrBitsData_){
87  addressMask_ = nrBitsAddress_!=32 ? (0x1<<nrBitsAddress_)-1 : ~0x0;
88  dataMask_ = (0x1<<nrBitsData_)-1;
89  stream.seekg(startPos);
90  return SUCCESS;
91  }
92  }
93  }
94 
95  nrBitsAddress_ =0;
96  nrBitsData_ = 0;
97  addressMask_ = (0x1<<nrBitsAddress_)-1;
98  dataMask_ = (0x1<<nrBitsData_)-1;
99 
100  stream.seekg(startPos);
101  return NO_HEADER;
102 }
103 
std::vector< int > data_
Definition: LUT.h:65
void write(std::ostream &stream) const
Definition: LUT.cc:67
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
unsigned int nrBitsData_
Definition: LUT.h:61
int read(std::istream &stream)
Definition: LUT.cc:11
unsigned int addressMask_
Definition: LUT.h:62
int readHeader_(std::istream &)
Definition: LUT.cc:75
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
int data(unsigned int address) const
Definition: LUT.h:46
unsigned int nrBitsAddress_
Definition: LUT.h:60
unsigned int dataMask_
Definition: LUT.h:63