CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LUT.cc
Go to the documentation of this file.
2 
3 #include <sstream>
4 #include <string>
5 #include <functional>
6 #include <algorithm>
7 
8 template <class T1,class T2,typename Comp=std::less<T1> > struct PairSortBy1st : public std::binary_function<std::pair<T1,T2>,std::pair<T1,T2>,bool> {
9  Comp comp;
10  PairSortBy1st(const Comp& iComp):comp(iComp){}
12  bool operator()(const std::pair<T1,T2>& lhs,const std::pair<T1,T2>&rhs)const{return comp(lhs.first,rhs.first);}
13  bool operator()(const T1& lhs,const std::pair<T1,T2>&rhs)const{return comp(lhs,rhs.first);}
14  bool operator()(const std::pair<T1,T2>& lhs,const T1 &rhs)const{return comp(lhs.first,rhs);}
15  bool operator()(const T1& lhs,const T1 &rhs)const{return comp(lhs,rhs);}
16 };
17 
18 template <class T1,typename Comp=std::less<T1> > struct Adjacent : public std::binary_function<T1,T1,bool> {
19  Comp comp;
21  Adjacent(const Comp& iComp):comp(iComp){}
22  //check this, works for now but may be a little buggy...
23  bool operator()(const T1& lhs,const T1& rhs)const{return std::abs(static_cast<int>(lhs-rhs))==1 && comp(lhs,rhs);}
24 
25 };
26 
27 template <class T1,class T2> struct Pair2nd : public std::unary_function<std::pair<T1,T2>,bool> {
28  T2 operator()(const std::pair<T1,T2>& val)const{return val.second;}
29 };
30 
31 //reads in the file
32 //the format is "address payload"
33 //all commments are ignored (start with '#') except for the header comment (starts with #<header>)
34 //currently ignores anything else on the line after the "address payload" and assumes they come first
35 int l1t::LUT::read(std::istream& stream)
36 {
37  data_.clear();
38 
39  int readHeaderCode = readHeader_(stream);
40  if(readHeaderCode!=SUCCESS) return readHeaderCode;
41 
42  std::vector<std::pair<unsigned int,int> > entries;
43  unsigned int maxAddress=addressMask_;
45 
46  while(std::getline(stream,line)){
47  line.erase( std::find( line.begin(), line.end(), '#' ), line.end() ); //ignore comments
48  std::istringstream lineStream(line);
49  std::pair<unsigned int,int> entry;
50  while(lineStream >> entry.first >> entry.second ){
51  entry.first&=addressMask_;
52  entry.second&=dataMask_;
53  entries.push_back(entry);
54  if(entry.first>maxAddress || maxAddress==addressMask_) maxAddress=entry.first;
55  }
56  }
57  std::sort(entries.begin(),entries.end(),PairSortBy1st<unsigned int,int>());
58  if(entries.empty()){
59  //log the error we read nothing
60  return NO_ENTRIES;
61  }
62  //this check is redundant as dups are also picked up by the next check but might make for easier debugging
63  if(std::adjacent_find(entries.begin(),entries.end(),PairSortBy1st<unsigned int,int,std::equal_to<unsigned int> >())!=entries.end()){
64  //log the error that we have duplicate addresses once masked
65  return DUP_ENTRIES;
66  }
67  if(entries.front().first!=0 ||
68  std::adjacent_find(entries.begin(),entries.end(),
69  PairSortBy1st<unsigned int,int,std::binary_negate<Adjacent<unsigned int> > >(std::binary_negate<Adjacent<unsigned int> >(Adjacent<unsigned int>())))!=entries.end()){ //not a great way, must be a better one...
70  //log the error that we have a missing entry
71  return MISS_ENTRIES;
72  }
73 
74 
75  if(maxAddress!=std::numeric_limits<unsigned int>::max()) data_.resize(maxAddress+1,0);
76  else{
77  //log the error that we have more addresses than we can deal with (which is 4gb so something probably has gone wrong anyways)
79  }
80 
81  std::transform(entries.begin(),entries.end(),data_.begin(),Pair2nd<unsigned int,int>());
82  return SUCCESS;
83 
84 }
85 
86 void l1t::LUT::write(std::ostream& stream)const
87 {
88  stream <<"#<header> V1 "<<nrBitsAddress_<<" "<<nrBitsData_<<" </header> "<<std::endl;
89  for(unsigned int address=0;address<data_.size();address++){
90  stream << (address&addressMask_)<<" "<<data(address)<<std::endl;
91  }
92 }
93 
94 int l1t::LUT::readHeader_(std::istream& stream)
95 {
96 
97  int startPos=stream.tellg(); //we are going to reset to this position before we exit
99  while(std::getline(stream,line)){
100  if(line.find("#<header>")==0){ //line
101  std::istringstream lineStream(line);
102 
103  std::string version; //currently not doing anything with this
104  std::string headerField; //currently not doing anything with this
105  if(lineStream >> headerField >> version >> nrBitsAddress_ >> nrBitsData_){
106  addressMask_ = nrBitsAddress_!=32 ? (0x1<<nrBitsAddress_)-1 : ~0x0;
107  dataMask_ = (0x1<<nrBitsData_)-1;
108  stream.seekg(startPos);
109  return SUCCESS;
110  }
111  }
112  }
113 
114  nrBitsAddress_ =0;
115  nrBitsData_ = 0;
116  addressMask_ = (0x1<<nrBitsAddress_)-1;
117  dataMask_ = (0x1<<nrBitsData_)-1;
118 
119  stream.seekg(startPos);
120  return NO_HEADER;
121 }
122 
std::vector< int > data_
Definition: LUT.h:65
void write(std::ostream &stream) const
Definition: LUT.cc:86
Adjacent()
Definition: LUT.cc:20
Comp comp
Definition: LUT.cc:9
Definition: LUT.cc:27
Comp comp
Definition: LUT.cc:19
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
bool operator()(const T1 &lhs, const T1 &rhs) const
Definition: LUT.cc:23
T2 operator()(const std::pair< T1, T2 > &val) const
Definition: LUT.cc:28
int read(std::istream &stream)
Definition: LUT.cc:35
Adjacent(const Comp &iComp)
Definition: LUT.cc:21
bool operator()(const std::pair< T1, T2 > &lhs, const std::pair< T1, T2 > &rhs) const
Definition: LUT.cc:12
unsigned int addressMask_
Definition: LUT.h:62
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool operator()(const T1 &lhs, const std::pair< T1, T2 > &rhs) const
Definition: LUT.cc:13
int readHeader_(std::istream &)
Definition: LUT.cc:94
PairSortBy1st(const Comp &iComp)
Definition: LUT.cc:10
bool operator()(const T1 &lhs, const T1 &rhs) const
Definition: LUT.cc:15
PairSortBy1st()
Definition: LUT.cc:11
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Definition: LUT.cc:18
bool operator()(const std::pair< T1, T2 > &lhs, const T1 &rhs) const
Definition: LUT.cc:14
unsigned int dataMask_
Definition: LUT.h:63