CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/DQM/SiStripMonitorHardware/src/SiStripFEDEmulator.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripMonitorHardware/interface/SiStripSpyEventMatcher.h"
00002 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDEmulator.h"
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 using edm::LogError;
00006 using edm::LogInfo;
00007 using edm::LogWarning;
00008 
00009 namespace sistrip{
00010 
00011   const char* FEDEmulator::messageLabel_ = "SiStripFEDEmulator";
00012 
00013 
00014   FEDEmulator::FEDEmulator() {
00015     byModule_ = false;
00016     minStrip_ = 0;
00017     maxStrip_ = 0;
00018     pedestals_.clear();
00019     noises_.clear();
00020     medians_.clear();
00021 
00022     detId_ = 0;
00023     nPairs_ = 0;
00024     pair_ = 0;
00025 
00026   }
00027 
00028 
00029 
00030   FEDEmulator::~FEDEmulator() {
00031 
00032   }
00033 
00034   void FEDEmulator::initialise(const bool byModule)
00035   {
00036     byModule_ = byModule;
00037     pedestals_.clear();
00038     noises_.clear();
00039     medians_.clear();
00040   }
00041 
00042   void FEDEmulator::initialiseModule(const uint32_t aDetId,
00043                                      const uint32_t aNPairs,
00044                                      const uint32_t aPair)
00045   {
00046     detId_ = aDetId;
00047     nPairs_ = aNPairs;
00048     pair_ = aPair;
00049 
00050     minStrip_ = 0;
00051     maxStrip_ = nPairs_*sistrip::STRIPS_PER_FEDCH;
00052     if (!byModule_) {
00053       minStrip_ = sistrip::STRIPS_PER_FEDCH*pair_;
00054       maxStrip_ = sistrip::STRIPS_PER_FEDCH*(pair_+1);
00055     }
00056 
00057     //need resize because it will not be filled by push_back...
00058     pedestals_.clear();
00059     pedestals_.resize(nPairs_*sistrip::STRIPS_PER_FEDCH);
00060 
00061     noises_.clear();
00062     noises_.resize(nPairs_*sistrip::STRIPS_PER_FEDCH);
00063 
00064     medians_.clear();
00065     medians_.reserve(nPairs_*2);
00066 
00067   }
00068 
00069   void FEDEmulator::retrievePedestals(const edm::ESHandle<SiStripPedestals> & aHandle){
00070     try {
00071       SiStripPedestals::Range pedestalsRange = aHandle->getRange(detId_);
00072       aHandle->allPeds(pedestals_,pedestalsRange);
00073     }
00074     catch(const cms::Exception& e) {
00075       LogError(messageLabel_) << " Something wrong whilst getting pedestals: size = " 
00076                                           << pedestals_.size() << " detId = " 
00077                                           << detId_ << std::endl << e.what();
00078       //throw cms::Exception("ProductNotFound") << "Problem with pedestals..." ;
00079     }
00080   }//end of retrievePedestals method.
00081 
00082   void FEDEmulator::retrieveNoises(const edm::ESHandle<SiStripNoises> & aHandle){
00083     try {
00084       SiStripNoises::Range noisesRange = aHandle->getRange(detId_);
00085       aHandle->allNoises(noises_,noisesRange);
00086     }
00087     catch(const cms::Exception& e) {
00088       LogError(messageLabel_) << " Something wrong whilst getting noises: size = " 
00089                                           << noises_.size() << " detId = " 
00090                                           << detId_ << std::endl << e.what();
00091       //throw cms::Exception("ProductNotFound") << "Problem with noises..." ;
00092     }
00093   }//end of retrieveNoises method.
00094 
00095   void FEDEmulator::subtractPedestals(const edm::DetSetVector<SiStripRawDigi>::const_iterator & inputChannel,
00096                                       std::vector<SiStripRawDigi> & pedsDetSetData,
00097                                       std::vector<SiStripProcessedRawDigi> & noiseDetSetData,
00098                                       std::vector<SiStripRawDigi> & pedSubtrDetSetData,
00099                                       std::vector<uint32_t> & medsDetSetData,
00100                                       const bool fillApvsForCM)
00101   {
00102 
00103     edm::DetSet<SiStripRawDigi>::const_iterator digi_it = inputChannel->begin();
00105     uint32_t lCount = 0;
00106     std::vector<std::vector<uint16_t> > apvs;
00107     apvs.reserve(nPairs_*2);
00108 
00109     //subtract pedestals and fill apv vector 
00110     //for median calculation
00111     //in a module, 256*nPairs, but here we are dealing
00112     //channel by channel, so 256 values at a time in the pedestal object.
00113     //reordered = 0-127 APV0 128-255 APV1
00114 
00115     std::vector<uint16_t> apvVec;;
00116     apvVec.reserve(sistrip::STRIPS_PER_APV);
00117       
00118     for (uint32_t st = minStrip_; st < maxStrip_; st++) {
00119       uint16_t ped = static_cast<uint16_t>(pedestals_[st]);
00120       pedsDetSetData.push_back(SiStripRawDigi(ped));
00121 
00122       float noise = noises_[st];
00123       noiseDetSetData.push_back(SiStripProcessedRawDigi(noise));
00124     
00125       if (digi_it == inputChannel->end()) {
00126         LogError(messageLabel_) << " Error, end of inputchannel reached for detid " << detId_ << "! Processed " << lCount << " strips." << std::endl;
00127         break;
00128       }
00129     
00130       int lVal = digi_it->adc() - ped;
00131       if (lVal < 0) {
00132         if (digi_it->adc()>0) LogDebug(messageLabel_) << " Negative value after pedestal subtraction ! Detid = " << detId_ << ", strip " << st << ", digi = " << digi_it->adc() << ", ped = " << ped << std::endl;
00133         lVal = 0;
00134       }
00135       pedSubtrDetSetData.push_back( SiStripRawDigi(static_cast<uint16_t>(lVal)) );
00136 
00137       
00138       if (fillApvsForCM) {
00139         apvVec.push_back( static_cast<uint16_t>(lVal) );
00140       }
00141       
00142       lCount++;
00143       ++digi_it;
00144       
00145       if (lCount%sistrip::STRIPS_PER_APV == 0 && fillApvsForCM) {
00146         if (apvVec.size()) apvs.push_back(apvVec);
00147         apvVec.clear();
00148         apvVec.reserve(sistrip::STRIPS_PER_APV);
00149       }
00150       
00151     }//end of loop over strips
00152     
00153     if (fillApvsForCM){
00154       if (apvs.size() != nPairs_*2) {
00155         LogError(messageLabel_) << " Error, wrong output size for median calculation for detid " << detId_ << "! Processed " << apvs.size() << " APVs." << std::endl;
00156       }
00157 
00158       //calculate common mode values
00159       for (uint32_t iapv(0); iapv<apvs.size(); iapv++){
00160         std::vector<uint16_t> lVec = apvs[iapv];
00161 
00162         std::vector<uint16_t>::iterator mid = lVec.begin() + (lVec.size()/2 - 1);
00163         std::nth_element(lVec.begin(), mid, lVec.end());
00164 
00165         //std::sort(lVec.begin(),lVec.end());
00166         //get the 64th element
00167         //if (*mid != lVec.at(63)) exit(0);
00168         //medians_.push_back(lVec.at(63));
00169         //medsDetSetData.push_back(lVec.at(63));
00170         medians_.push_back(*mid);
00171         medsDetSetData.push_back(*mid);
00172     
00173       }
00174     }
00175 
00176   }//end of FEDEmulator::subtractPedestals method.
00177 
00178 
00179   void FEDEmulator::subtractCM(const std::vector<SiStripRawDigi> & pedSubtrDetSetData,
00180                                std::vector<SiStripRawDigi> & cmSubtrDetSetData)
00181   {
00182     //subtract Medians
00183     std::vector<SiStripRawDigi>::const_iterator lDigi = pedSubtrDetSetData.begin();
00184     uint32_t lCount = 0;
00185 
00186     for (uint32_t st = minStrip_; st < maxStrip_; st++) {
00187       uint32_t lApvNum = static_cast<uint32_t>(lCount*1./sistrip::STRIPS_PER_APV);
00188       
00189       int value = lDigi->adc() - medians_.at(lApvNum);
00190       if (value < 0) {
00191         //FED doesn't handle negative values
00192         value = 0;
00193       }
00194       cmSubtrDetSetData.push_back(SiStripRawDigi(static_cast<uint16_t>(value)));
00195 
00196       ++lDigi;
00197       lCount++;
00198     }
00199 
00200   }
00201 
00202   void FEDEmulator::zeroSuppress(const std::vector<SiStripRawDigi> & cmSubtrDetSetData,
00203                                  edm::DetSet<SiStripDigi>    & zsDetSetData,
00204                                  const std::auto_ptr<SiStripRawProcessingAlgorithms> & algorithms)
00205   {
00206     //transform the input digis to a vector of integers
00207     std::vector<int16_t> cmSubtrRawDigis;
00208     transform(cmSubtrDetSetData.begin(), 
00209               cmSubtrDetSetData.end(),
00210               back_inserter(cmSubtrRawDigis),
00211               boost::bind(&SiStripRawDigi::adc ,_1)
00212               );
00213     algorithms->suppressor->suppress(cmSubtrRawDigis, 0,
00214                                      zsDetSetData);
00215     
00216   }//end of FEDEmulator::zeroSuppress method.
00217 
00218 
00219   
00220   void FEDEmulator::fillPeds(const edm::DetSetVector<SiStripRawDigi>::const_iterator & peds){
00221     for (edm::DetSet<SiStripRawDigi>::const_iterator iApv = peds->begin(); 
00222          iApv != peds->end(); ++iApv) {
00223       pedestals_.push_back(iApv->adc());
00224     }
00225   }
00226    
00227   void FEDEmulator::fillNoises(const edm::DetSetVector<SiStripProcessedRawDigi>::const_iterator & noises){
00228     for (edm::DetSet<SiStripProcessedRawDigi>::const_iterator iApv = noises->begin(); 
00229          iApv != noises->end(); ++iApv) {
00230       noises_.push_back(iApv->adc());
00231     }
00232   }
00233   
00234   void FEDEmulator::fillMedians(const std::map<uint32_t,std::vector<uint32_t> >::const_iterator & meds){
00235     std::vector<uint32_t>::const_iterator iApv = (meds->second).begin();
00236     for ( ; iApv != (meds->second).end(); ++iApv) {
00237       medians_.push_back(*iApv);
00238     }
00239   }
00240 
00241   void FEDEmulator::print(std::ostream & aOs) {
00242     aOs << "===============================" << std::endl
00243         << " ===== FEDEmulator::print =====" << std::endl
00244         << " === byModule = " << byModule_ << std::endl
00245         << " === minStrip = " << minStrip_ << std::endl
00246         << " === maxStrip = " << maxStrip_ << std::endl
00247         << " === size of pedestals = " << pedestals_.size() << std::endl
00248         << " === size of noises = " << noises_.size() << std::endl
00249         << " === size of medians = " << medians_.size() << std::endl
00250         << " === detId = " << detId_ << std::endl
00251         << " === nPairs = " << nPairs_ << std::endl
00252         << " === pair = " << pair_ << std::endl
00253         << "===============================" << std::endl;
00254   }
00255 
00256 
00257   void FEDEmulator::printPeds(std::ostream & aOs) {
00258     aOs << "=========================================" << std::endl
00259         << " ===== FEDEmulator::printPedestals =====" << std::endl;
00260     for (unsigned int i(0); i<pedestals_.size(); i++){
00261       if (i%32 == 0) aOs << std::endl;
00262       aOs << pedestals_[i] << " " ;
00263     }
00264     aOs << "=========================================" << std::endl;
00265   }
00266 
00267   void FEDEmulator::printNoises(std::ostream & aOs) {
00268     aOs << "=========================================" << std::endl
00269         << " ===== FEDEmulator::printNoises =====" << std::endl;
00270     for (unsigned int i(0); i<noises_.size(); i++){
00271       if (i%32 == 0) aOs << std::endl;
00272       aOs << noises_[i] << " " ;
00273     }
00274     aOs << "=========================================" << std::endl;
00275   }
00276 
00277   void FEDEmulator::printMeds(std::ostream & aOs) {
00278     aOs << "=========================================" << std::endl
00279         << " ===== FEDEmulator::printMedians =====" << std::endl;
00280     for (unsigned int i(0); i<medians_.size(); i++){
00281       if (i%32 == 0) aOs << std::endl;
00282       aOs << medians_[i] << " " ;
00283     }
00284     aOs << "=========================================" << std::endl;
00285   }
00286 
00287 
00288 
00289 
00290 }//namespace sistrip