CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondFormats/SiStripObjects/src/SiStripLatency.cc

Go to the documentation of this file.
00001 #include "CondFormats/SiStripObjects/interface/SiStripLatency.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 #include <algorithm>
00006 #include <iterator>
00007 #include <iostream>
00008 #include <sstream>
00009 
00010 bool SiStripLatency::put( const uint32_t detId, const uint16_t apv, const uint16_t latency, const uint16_t mode )
00011 {
00012   if( detId > 536870911 ) {
00013     std::stringstream error;
00014     error << "ERROR: the detId = " << detId << " is bigger than the maximum acceptable value = 2^(29) - 1 = " << 536870911 << std::endl;
00015     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;
00016     error << "of the writing of this class was 47017836 as defined in CalibTracker/SiStripCommon/data/SiStripDetInfo.dat." << std::endl;
00017     error << "If the maximum value has changed a revision of this calss is needed, possibly changing the detIdAndApv value from" << std::endl;
00018     error << "from uint32_t to uint64_t." << std::endl;
00019     edm::LogError("SiStripLatency::put") << error;
00020     throw cms::Exception("InsertFailure");
00021   }
00022 
00023   // Store all the values in the vectors
00024   uint32_t detIdAndApv = (detId << 3) | apv;
00025   latIt pos = lower_bound(latencies_.begin(), latencies_.end(), detIdAndApv, OrderByDetIdAndApv());
00026 
00027   if( pos != latencies_.end() && pos->detIdAndApv == detIdAndApv ) {
00028     std::cout << "Value already inserted, skipping insertion" << std::endl;
00029     return false;
00030   }
00031   // std::cout << "Filling with: latency = " << latency << ", mode = " << mode << std::endl;
00032   latencies_.insert(pos, Latency(detIdAndApv, latency, mode));
00033 
00034   return true;
00035 }
00036 
00037 void SiStripLatency::compress()
00038 {
00039   // std::cout << "Starting compression" << std::endl;
00040   // std::cout << "Total number of elements before compression = " << latencies_.size() << std::endl;
00041 
00042   // int i = 0;
00043   // for( latIt it = latencies_.begin(); it != latencies_.end(); ++it, ++i ) {
00044   //   std::cout << "latency["<<i<<"] = " << it->latency << ", mode["<<i<<"] = " << (int)it->mode << " for detIdAndApv = " << it->detIdAndApv << std::endl;;
00045   // }
00046   // Remove latency duplicates. Note that unique is stable.
00047   // CANNOT USE THIS: it will leave one element, but you do not know which one.
00048   // unique(latencies_.begin(), latencies_.end(), EqualByLatency());
00049   // Cannot use lower_bound or upper_bound with latencies because the vector is sorted in detIdAndApvs and not in latencies
00050   // For the same reason cannot use equal_range.
00051 
00052   // Go through the elements one by one and remove the current one if it has the same latency as the next one
00053   // for( latIt lat = latencies_.begin(); lat != latencies_.end(); ++lat ) {
00054   latIt lat = latencies_.begin();
00055   while( lat != latencies_.end() ) {
00056     // If it is not the last and it has the same latency and mode as the next one remove it
00057     if( ((lat+1) != latencies_.end()) && ((lat+1)->mode == lat->mode) && ((lat+1)->latency == lat->latency) ) {
00058       lat = latencies_.erase(lat);
00059     }
00060     else {
00061       ++lat;
00062     }
00063   }
00064   // std::cout << "Total number of elements after compression = " << latencies_.size() << std::endl;
00065   // i = 0;
00066   // for( latIt it = latencies_.begin(); it != latencies_.end(); ++it, ++i ) {
00067   //   std::cout << "latency["<<i<<"] = " << it->latency << ", mode["<<i<<"] = " << (int)it->mode  << ", for detIdAndApv = " << it->detIdAndApv << std::endl;;
00068   // }
00069 }
00070 
00071 // const latConstIt SiStripLatency::position(const uint32_t detId, const uint16_t apv) const
00072 // {
00073 //   if( latencies_.empty() ) {
00074 //     // std::cout << "SiStripLatency: Error, range is empty" << std::endl;
00075 //     return latencies_.end();
00076 //   }
00077 //   uint32_t detIdAndApv = (detId << 2) | apv;
00078 //   latConstIt pos = lower_bound(latencies_.begin(), latencies_.end(), detIdAndApv, OrderByDetIdAndApv());
00079 //   return pos;
00080 // }
00081 
00082 uint16_t SiStripLatency::latency(const uint32_t detId, const uint16_t apv) const
00083 {
00084   const latConstIt & pos = position(detId, apv);
00085   if( pos == latencies_.end() ) {
00086     return 255;
00087   }
00088   return pos->latency;
00089 }
00090 
00091 uint16_t SiStripLatency::mode(const uint32_t detId, const uint16_t apv) const
00092 {
00093   const latConstIt & pos = position(detId, apv);
00094   if( pos == latencies_.end() ) {
00095     return 0;
00096   }
00097   return pos->mode;
00098 }
00099 
00100 std::pair<uint16_t, uint16_t> SiStripLatency::latencyAndMode(const uint32_t detId, const uint16_t apv) const
00101 {
00102   const latConstIt & pos = position(detId, apv);
00103   if( pos == latencies_.end() ) {
00104     return std::make_pair(255, 0);
00105   }
00106   return std::make_pair(pos->latency, pos->mode);
00107 }
00108 
00109 uint16_t SiStripLatency::singleLatency() const
00110 {
00111   if( latencies_.size() == 1 ) {
00112     return latencies_[0].latency;
00113   }
00114   int differentLatenciesNum = 0;
00115   // Count the number of different latencies
00116   for( latConstIt it = latencies_.begin(); it != latencies_.end()-1; ++it ) {
00117     if( it->latency != (it+1)->latency ) {
00118       ++differentLatenciesNum;
00119     }
00120   }
00121   if( differentLatenciesNum == 0 ) {
00122     return latencies_[0].latency;
00123   }
00124   return 255;
00125 }
00126 
00127 uint16_t SiStripLatency::singleMode() const
00128 {
00129   if( latencies_.size() == 1 ) {
00130     return latencies_[0].mode;
00131   }
00132   int differentModesNum = 0;
00133   // Count the number of different modes
00134   for( latConstIt it = latencies_.begin(); it != latencies_.end()-1; ++it ) {
00135     if( it->mode != (it+1)->mode ) {
00136       ++differentModesNum;
00137     }
00138   }
00139   if( differentModesNum == 0 ) {
00140     return latencies_[0].mode;
00141   }
00142   return 0;
00143 }
00144 
00145 void SiStripLatency::allModes(std::vector<uint16_t> & allModesVector) const
00146 {
00147   for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
00148     allModesVector.push_back(it->mode);
00149   }
00150   // The Latencies are sorted by DetIdAndApv, we need to sort the modes again and then remove duplicates
00151   sort( allModesVector.begin(), allModesVector.end() );
00152   allModesVector.erase( unique( allModesVector.begin(), allModesVector.end() ), allModesVector.end() );
00153 }
00154 
00155 int16_t SiStripLatency::singleReadOutMode() const
00156 {
00157   uint16_t mode = singleMode();
00158   if(mode != 0 ) {
00159     if( (mode & READMODEMASK) == READMODEMASK ) return 1;
00160     if( (mode & READMODEMASK) == 0 ) return 0;
00161   }
00162   else {
00163     // If we are here the Tracker is not in single mode. Check if it is in single Read-out mode.
00164     bool allInPeakMode = true;
00165     bool allInDecoMode = true;
00166     std::vector<uint16_t> allModesVector;
00167     allModes(allModesVector);
00168     std::vector<uint16_t>::const_iterator it = allModesVector.begin();
00169     for( ; it != allModesVector.end(); ++it ) {
00170       if( ((*it) & READMODEMASK) == READMODEMASK ) allInDecoMode = false;
00171       if( ((*it) & READMODEMASK) == 0 ) allInPeakMode = false;
00172     }
00173     if( allInPeakMode ) return 1;
00174     if( allInDecoMode ) return 0;
00175   }
00176   return -1;
00177 }
00178 
00179 // bool SiStripLatency::allPeak() const
00180 // {
00181 //   if( (singleMode() & 8) == 8 ) return true;
00182 //   // If we are here the Tracker is not in single mode. Check if it is in single Read-out mode.
00183 //   bool allInPeakMode = true;
00184 //   std::vector<uint16_t> allModesVector;
00185 //   allModes(allModesVector);
00186 //   std::vector<uint16_t>::const_iterator it = allModesVector.begin();
00187 //   for( ; it != allModesVector.end(); ++it ) {
00188 //     if( ((*it) & 8) == 0 ) allInPeakMode = false;
00189 //   }
00190 //   return allInPeakMode;
00191 // }
00192 
00193 void SiStripLatency::allLatencies(std::vector<uint16_t> & allLatenciesVector) const
00194 {
00195 //   if( !(latencies_.empty()) ) {
00196 //     allLatenciesVector.push_back(latencies_[0].latency);
00197 //     if( latencies_.size() > 1 ) {
00198 //       for( latConstIt it = latencies_.begin()+1; it != latencies_.end(); ++it ) {
00199 //         if( it->latency != (it-1)->latency) {
00200 //           allLatenciesVector.push_back(it->latency);
00201 //           std::cout << "Saved latency = " << short(it->latency) << std::endl;
00202 //         }
00203 //       }
00204 //       // The Latencies are sorted by DetIdAndApv, we need to sort the latencies again
00205 //       std::sort( allLatenciesVector.begin(), allLatenciesVector.end() );
00206 //       allLatenciesVector.erase( unique( allLatenciesVector.begin(), allLatenciesVector.end() ) );
00207 //     }
00208 //   }
00209 
00210   for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
00211     allLatenciesVector.push_back(it->latency);
00212   }
00213   // The Latencies are sorted by DetIdAndApv, we need to sort the latencies again and then remove duplicates
00214   sort( allLatenciesVector.begin(), allLatenciesVector.end() );
00215   allLatenciesVector.erase( unique( allLatenciesVector.begin(), allLatenciesVector.end() ), allLatenciesVector.end() );
00216 }
00217 
00218 // pair<uint16_t, uint16_t> SiStripLatency::singleLatencyAndMode() const
00219 // {
00220 //   if( latencies_.size() == 1 ) {
00221 //     return make_pair(latencies_[0].latency, latencies_[0].mode);
00222 //   }
00223 //   return make_pair(-1, 0);
00224 // }
00225 
00226 std::vector<SiStripLatency::Latency> SiStripLatency::allUniqueLatencyAndModes()
00227 {
00228   std::vector<Latency> latencyCopy(latencies_);
00229   sort( latencyCopy.begin(), latencyCopy.end(), OrderByLatencyAndMode() );
00230   latencyCopy.erase( unique( latencyCopy.begin(), latencyCopy.end(), SiStripLatency::EqualByLatencyAndMode() ), latencyCopy.end() );
00231   return latencyCopy;
00232 }
00233 
00234 void SiStripLatency::printSummary(std::stringstream & ss) const
00235 {
00236   uint16_t lat = singleLatency();
00237   uint16_t mode = singleMode();
00238   if( lat != 255 ) {
00239     ss << "All the Tracker has the same latency = " << lat << std::endl;
00240   }
00241   else {
00242     std::vector<uint16_t> allLatenciesVector;
00243     allLatencies(allLatenciesVector);
00244     if( allLatenciesVector.size() > 1 ) {
00245       ss << "There is more than one latency value in the Tracker" << std::endl;
00246     }
00247     else {
00248       ss << "Latency value is " << lat << " that means invalid" << std::endl;
00249     }
00250   }
00251 
00252   if( mode != 0 ) {
00253     ss << "All the Tracker has the same mode = " << mode << std::endl;
00254   }
00255   else {
00256     std::vector<uint16_t> allModesVector;
00257     allModes(allModesVector);
00258     if( allModesVector.size() > 1 ) {
00259       ss << "There is more than one mode in the Tracker" << std::endl;
00260     }
00261     else {
00262       ss << "Mode value is " << mode << " that means invalid" << std::endl;
00263     }
00264   }
00265 
00266   ss << "Total number of ranges = " << latencies_.size() << std::endl;
00267 }
00268 
00269 void SiStripLatency::printDebug(std::stringstream & ss) const
00270 {
00271   ss << "List of all the latencies and modes for the " << latencies_.size() << " ranges in the object:" << std::endl;
00272   for( latConstIt it = latencies_.begin(); it != latencies_.end(); ++it ) {
00273     int detId = it->detIdAndApv >> 3;
00274     int apv = it->detIdAndApv & 7; // 7 is 0...0111
00275     ss << "for detId = " << detId << " and apv pair = " << apv << " latency = " << int(it->latency) << " and mode = " << int(it->mode) << std::endl;
00276   }
00277 }
00278 
00279 #undef READMODEMASK