CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripLatency.cc
Go to the documentation of this file.
4 
5 #include <algorithm>
6 #include <iterator>
7 #include <iostream>
8 #include <sstream>
9 
10 bool SiStripLatency::put( const uint32_t detId, const uint16_t apv, const uint16_t latency, const uint16_t mode )
11 {
12  if( detId > 536870911 ) {
13  std::stringstream error;
14  error << "ERROR: the detId = " << detId << " is bigger than the maximum acceptable value = 2^(29) - 1 = " << 536870911 << std::endl;
15  error << "Since we are using 29 bits for the detId and 3 bits for the apv value. The maximum tracker detId at the moment" << std::endl;
16  error << "of the writing of this class was 47017836 as defined in CalibTracker/SiStripCommon/data/SiStripDetInfo.dat." << std::endl;
17  error << "If the maximum value has changed a revision of this calss is needed, possibly changing the detIdAndApv value from" << std::endl;
18  error << "from uint32_t to uint64_t." << std::endl;
19  edm::LogError("SiStripLatency::put") << error;
20  throw cms::Exception("InsertFailure");
21  }
22 
23  // Store all the values in the vectors
24  uint32_t detIdAndApv = (detId << 3) | apv;
25  latIt pos = lower_bound(latencies_.begin(), latencies_.end(), detIdAndApv, OrderByDetIdAndApv());
26 
27  if( pos != latencies_.end() && pos->detIdAndApv == detIdAndApv ) {
28  std::cout << "Value already inserted, skipping insertion" << std::endl;
29  return false;
30  }
31  // std::cout << "Filling with: latency = " << latency << ", mode = " << mode << std::endl;
32  latencies_.insert(pos, Latency(detIdAndApv, latency, mode));
33 
34  return true;
35 }
36 
38 {
39  // std::cout << "Starting compression" << std::endl;
40  // std::cout << "Total number of elements before compression = " << latencies_.size() << std::endl;
41 
42  // int i = 0;
43  // for( latIt it = latencies_.begin(); it != latencies_.end(); ++it, ++i ) {
44  // std::cout << "latency["<<i<<"] = " << it->latency << ", mode["<<i<<"] = " << (int)it->mode << " for detIdAndApv = " << it->detIdAndApv << std::endl;;
45  // }
46  // Remove latency duplicates. Note that unique is stable.
47  // CANNOT USE THIS: it will leave one element, but you do not know which one.
48  // unique(latencies_.begin(), latencies_.end(), EqualByLatency());
49  // Cannot use lower_bound or upper_bound with latencies because the vector is sorted in detIdAndApvs and not in latencies
50  // For the same reason cannot use equal_range.
51 
52  // Go through the elements one by one and remove the current one if it has the same latency as the next one
53  // for( latIt lat = latencies_.begin(); lat != latencies_.end(); ++lat ) {
54  latIt lat = latencies_.begin();
55  while( lat != latencies_.end() ) {
56  // If it is not the last and it has the same latency and mode as the next one remove it
57  if( ((lat+1) != latencies_.end()) && ((lat+1)->mode == lat->mode) && ((lat+1)->latency == lat->latency) ) {
58  lat = latencies_.erase(lat);
59  }
60  else {
61  ++lat;
62  }
63  }
64  // std::cout << "Total number of elements after compression = " << latencies_.size() << std::endl;
65  // i = 0;
66  // for( latIt it = latencies_.begin(); it != latencies_.end(); ++it, ++i ) {
67  // std::cout << "latency["<<i<<"] = " << it->latency << ", mode["<<i<<"] = " << (int)it->mode << ", for detIdAndApv = " << it->detIdAndApv << std::endl;;
68  // }
69 }
70 
71 // const latConstIt SiStripLatency::position(const uint32_t detId, const uint16_t apv) const
72 // {
73 // if( latencies_.empty() ) {
74 // // std::cout << "SiStripLatency: Error, range is empty" << std::endl;
75 // return latencies_.end();
76 // }
77 // uint32_t detIdAndApv = (detId << 2) | apv;
78 // latConstIt pos = lower_bound(latencies_.begin(), latencies_.end(), detIdAndApv, OrderByDetIdAndApv());
79 // return pos;
80 // }
81 
82 uint16_t SiStripLatency::latency(const uint32_t detId, const uint16_t apv) const
83 {
84  const latConstIt & pos = position(detId, apv);
85  if( pos == latencies_.end() ) {
86  return 255;
87  }
88  return pos->latency;
89 }
90 
91 uint16_t SiStripLatency::mode(const uint32_t detId, const uint16_t apv) const
92 {
93  const latConstIt & pos = position(detId, apv);
94  if( pos == latencies_.end() ) {
95  return 0;
96  }
97  return pos->mode;
98 }
99 
100 std::pair<uint16_t, uint16_t> SiStripLatency::latencyAndMode(const uint32_t detId, const uint16_t apv) const
101 {
102  const latConstIt & pos = position(detId, apv);
103  if( pos == latencies_.end() ) {
104  return std::make_pair(255, 0);
105  }
106  return std::make_pair(pos->latency, pos->mode);
107 }
108 
110 {
111  if( latencies_.size() == 1 ) {
112  return latencies_[0].latency;
113  }
114  int differentLatenciesNum = 0;
115  // Count the number of different latencies
116  for( latConstIt it = latencies_.begin(); it != latencies_.end()-1; ++it ) {
117  if( it->latency != (it+1)->latency ) {
118  ++differentLatenciesNum;
119  }
120  }
121  if( differentLatenciesNum == 0 ) {
122  return latencies_[0].latency;
123  }
124  return 255;
125 }
126 
128 {
129  if( latencies_.size() == 1 ) {
130  return latencies_[0].mode;
131  }
132  int differentModesNum = 0;
133  // Count the number of different modes
134  for( latConstIt it = latencies_.begin(); it != latencies_.end()-1; ++it ) {
135  if( it->mode != (it+1)->mode ) {
136  ++differentModesNum;
137  }
138  }
139  if( differentModesNum == 0 ) {
140  return latencies_[0].mode;
141  }
142  return 0;
143 }
144 
145 void SiStripLatency::allModes(std::vector<uint16_t> & allModesVector) const
146 {
147  for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
148  allModesVector.push_back(it->mode);
149  }
150  // The Latencies are sorted by DetIdAndApv, we need to sort the modes again and then remove duplicates
151  sort( allModesVector.begin(), allModesVector.end() );
152  allModesVector.erase( unique( allModesVector.begin(), allModesVector.end() ), allModesVector.end() );
153 }
154 
156 {
157  uint16_t mode = singleMode();
158  if(mode != 0 ) {
159  if( (mode & READMODEMASK) == READMODEMASK ) return 1;
160  if( (mode & READMODEMASK) == 0 ) return 0;
161  }
162  else {
163  // If we are here the Tracker is not in single mode. Check if it is in single Read-out mode.
164  bool allInPeakMode = true;
165  bool allInDecoMode = true;
166  std::vector<uint16_t> allModesVector;
167  allModes(allModesVector);
168  std::vector<uint16_t>::const_iterator it = allModesVector.begin();
169  for( ; it != allModesVector.end(); ++it ) {
170  if( ((*it) & READMODEMASK) == READMODEMASK ) allInDecoMode = false;
171  if( ((*it) & READMODEMASK) == 0 ) allInPeakMode = false;
172  }
173  if( allInPeakMode ) return 1;
174  if( allInDecoMode ) return 0;
175  }
176  return -1;
177 }
178 
179 // bool SiStripLatency::allPeak() const
180 // {
181 // if( (singleMode() & 8) == 8 ) return true;
182 // // If we are here the Tracker is not in single mode. Check if it is in single Read-out mode.
183 // bool allInPeakMode = true;
184 // std::vector<uint16_t> allModesVector;
185 // allModes(allModesVector);
186 // std::vector<uint16_t>::const_iterator it = allModesVector.begin();
187 // for( ; it != allModesVector.end(); ++it ) {
188 // if( ((*it) & 8) == 0 ) allInPeakMode = false;
189 // }
190 // return allInPeakMode;
191 // }
192 
193 void SiStripLatency::allLatencies(std::vector<uint16_t> & allLatenciesVector) const
194 {
195 // if( !(latencies_.empty()) ) {
196 // allLatenciesVector.push_back(latencies_[0].latency);
197 // if( latencies_.size() > 1 ) {
198 // for( latConstIt it = latencies_.begin()+1; it != latencies_.end(); ++it ) {
199 // if( it->latency != (it-1)->latency) {
200 // allLatenciesVector.push_back(it->latency);
201 // std::cout << "Saved latency = " << short(it->latency) << std::endl;
202 // }
203 // }
204 // // The Latencies are sorted by DetIdAndApv, we need to sort the latencies again
205 // std::sort( allLatenciesVector.begin(), allLatenciesVector.end() );
206 // allLatenciesVector.erase( unique( allLatenciesVector.begin(), allLatenciesVector.end() ) );
207 // }
208 // }
209 
210  for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
211  allLatenciesVector.push_back(it->latency);
212  }
213  // The Latencies are sorted by DetIdAndApv, we need to sort the latencies again and then remove duplicates
214  sort( allLatenciesVector.begin(), allLatenciesVector.end() );
215  allLatenciesVector.erase( unique( allLatenciesVector.begin(), allLatenciesVector.end() ), allLatenciesVector.end() );
216 }
217 
218 // pair<uint16_t, uint16_t> SiStripLatency::singleLatencyAndMode() const
219 // {
220 // if( latencies_.size() == 1 ) {
221 // return make_pair(latencies_[0].latency, latencies_[0].mode);
222 // }
223 // return make_pair(-1, 0);
224 // }
225 
226 std::vector<SiStripLatency::Latency> SiStripLatency::allUniqueLatencyAndModes()
227 {
228  std::vector<Latency> latencyCopy(latencies_);
229  sort( latencyCopy.begin(), latencyCopy.end(), OrderByLatencyAndMode() );
230  latencyCopy.erase( unique( latencyCopy.begin(), latencyCopy.end(), SiStripLatency::EqualByLatencyAndMode() ), latencyCopy.end() );
231  return latencyCopy;
232 }
233 
234 void SiStripLatency::printSummary(std::stringstream & ss) const
235 {
236  uint16_t lat = singleLatency();
237  uint16_t mode = singleMode();
238  if( lat != 255 ) {
239  ss << "All the Tracker has the same latency = " << lat << std::endl;
240  }
241  else {
242  std::vector<uint16_t> allLatenciesVector;
243  allLatencies(allLatenciesVector);
244  if( allLatenciesVector.size() > 1 ) {
245  ss << "There is more than one latency value in the Tracker" << std::endl;
246  }
247  else {
248  ss << "Latency value is " << lat << " that means invalid" << std::endl;
249  }
250  }
251 
252  if( mode != 0 ) {
253  ss << "All the Tracker has the same mode = " << mode << std::endl;
254  }
255  else {
256  std::vector<uint16_t> allModesVector;
257  allModes(allModesVector);
258  if( allModesVector.size() > 1 ) {
259  ss << "There is more than one mode in the Tracker" << std::endl;
260  }
261  else {
262  ss << "Mode value is " << mode << " that means invalid" << std::endl;
263  }
264  }
265 
266  ss << "Total number of ranges = " << latencies_.size() << std::endl;
267 }
268 
269 void SiStripLatency::printDebug(std::stringstream & ss) const
270 {
271  ss << "List of all the latencies and modes for the " << latencies_.size() << " ranges in the object:" << std::endl;
272  for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
273  int detId = it->detIdAndApv >> 3;
274  int apv = it->detIdAndApv & 7; // 7 is 0...0111
275  ss << "for detId = " << detId << " and apv pair = " << apv << " latency = " << int(it->latency) << " and mode = " << int(it->mode) << std::endl;
276  }
277 }
278 
279 #undef READMODEMASK
bool put(const uint32_t detId, const uint16_t apv, const uint16_t latency, const uint16_t mode)
std::vector< Latency > allUniqueLatencyAndModes()
int16_t singleReadOutMode() const
std::vector< Latency >::iterator latIt
uint16_t mode(const uint32_t detId, const uint16_t apv) const
void allLatencies(std::vector< uint16_t > &allLatenciesVector) const
Fills the passed vector with all the possible latencies in the Tracker.
uint16_t singleLatency() const
If all the latency values stored are equal return that value, otherwise return -1.
void allModes(std::vector< uint16_t > &allModesVector) const
Fills the passed vector with all the possible modes in the Tracker.
void printSummary(std::stringstream &ss) const
Prints the number of ranges as well as the value of singleLatency and singleMode. ...
uint16_t singleMode() const
const latConstIt position(const uint32_t detId, const uint16_t apv) const
Used to compute the position with the lower_bound binary search.
uint16_t latency(const uint32_t detId, const uint16_t apv) const
std::vector< Latency > latencies_
int mode
Definition: AMPTWrapper.h:139
void printDebug(std::stringstream &ss) const
Prints the full list of all ranges and corresponding values of latency and mode.
std::vector< Latency >::const_iterator latConstIt
tuple cout
Definition: gather_cfg.py:41
std::pair< uint16_t, uint16_t > latencyAndMode(const uint32_t detId, const uint16_t apv) const
#define READMODEMASK
Definition: SiStripLatency.h:9