CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/EventFilter/SiStripRawToDigi/src/SiStripRawToClustersLazyUnpacker.cc

Go to the documentation of this file.
00001 #include "EventFilter/SiStripRawToDigi/interface/SiStripRawToClustersLazyUnpacker.h"
00002 #include <sstream>
00003 #include <iostream>
00004 
00005 
00006 namespace sistrip { 
00007   
00008   RawToClustersLazyUnpacker::RawToClustersLazyUnpacker(const SiStripRegionCabling& regioncabling, StripClusterizerAlgorithm& clustalgo, SiStripRawProcessingAlgorithms& rpAlgos, const FEDRawDataCollection& data, bool dump) :
00009 
00010     raw_(&data),
00011     regions_(&(regioncabling.getRegionCabling())),
00012     clusterizer_(&clustalgo),
00013     rawAlgos_(&rpAlgos),
00014     buffers_(),
00015     rawToDigi_(0,0,0,0,0,0,0,0),
00016     dump_(dump),
00017     doAPVEmulatorCheck_(true)
00018   {
00019     buffers_.assign(1024,static_cast<sistrip::FEDBuffer*>(0));
00020   }
00021 
00022   RawToClustersLazyUnpacker::~RawToClustersLazyUnpacker() {
00023 
00024     std::vector< sistrip::FEDBuffer*>::iterator ibuffer = buffers_.begin();
00025     for (; ibuffer!=buffers_.end(); ibuffer++) {
00026       if (*ibuffer) delete *ibuffer;
00027     }
00028   }
00029 
00030   void RawToClustersLazyUnpacker::fill(const uint32_t& index, record_type& record) {
00031 
00032     // Get region, subdet and layer from element-index
00033     uint32_t region = SiStripRegionCabling::region(index);
00034     uint32_t subdet = static_cast<uint32_t>(SiStripRegionCabling::subdet(index));
00035     uint32_t layer = SiStripRegionCabling::layer(index);
00036  
00037     // Retrieve cabling for element
00038     const SiStripRegionCabling::ElementCabling& element = (*regions_)[region][subdet][layer];
00039   
00040     // Loop dets
00041     SiStripRegionCabling::ElementCabling::const_iterator idet = element.begin();
00042     for (;idet!=element.end();idet++) {
00043     
00044       // If det id is null or invalid continue.
00045       if ( !(idet->first) || (idet->first == sistrip::invalid32_) || !clusterizer_->stripByStripBegin(idet->first)) { continue; }
00046     
00047       // Loop over apv-pairs of det
00048       std::vector<FedChannelConnection>::const_iterator iconn = idet->second.begin();
00049       for (;iconn!=idet->second.end();iconn++) {
00050         const uint16_t fedId = iconn->fedId();
00051       
00052         // If fed id is null or connection is invalid continue
00053         if ( !fedId || !iconn->isConnected() ) { continue; }    
00054       
00055         // If Fed hasnt already been initialised, extract data and initialise
00056         FEDBuffer* buffer = buffers_[fedId];
00057         if (!buffer) {
00058         
00059           // Retrieve FED raw data for given FED
00060           const FEDRawData& rawData = raw_->FEDData( static_cast<int>(fedId) );
00061         
00062           // Check on FEDRawData pointer
00063           if ( !rawData.data() ) {
00064             if (edm::isDebugEnabled()) {
00065               edm::LogWarning(sistrip::mlRawToCluster_)
00066                 << "[sistrip::RawToClustersLazyGetter::" 
00067                 << __func__ 
00068                 << "]"
00069                 << " NULL pointer to FEDRawData for FED id " 
00070                 << fedId;
00071             }
00072             continue;
00073           }     
00074         
00075           // Check on FEDRawData size
00076           if ( !rawData.size() ) {
00077             if (edm::isDebugEnabled()) {
00078               edm::LogWarning(sistrip::mlRawToCluster_)
00079                 << "[sistrip::RawToClustersLazyGetter::" 
00080                 << __func__ << "]"
00081                 << " FEDRawData has zero size for FED id " 
00082                 << fedId;
00083             }
00084             continue;
00085           }
00086         
00087           // construct FEDBuffer
00088           try {
00089             buffers_[fedId] = buffer = new sistrip::FEDBuffer(rawData.data(),rawData.size());
00090             if (!buffer->doChecks()) throw cms::Exception("FEDBuffer") << "FED Buffer check fails for FED ID" << fedId << ".";
00091           }
00092           catch (const cms::Exception& e) { 
00093             if (edm::isDebugEnabled()) {
00094               edm::LogWarning(sistrip::mlRawToCluster_) 
00095                 << "Exception caught when creating FEDBuffer object for FED " << fedId << ": " << e.what();
00096             }
00097             if ( buffer ) { delete buffer; }
00098             buffers_[fedId] = 0;
00099             continue;
00100           }
00101 
00102           // dump of FEDRawData to stdout
00103           if ( dump_ ) {
00104             std::stringstream ss;
00105             rawToDigi_.dumpRawData( fedId, rawData, ss );
00106             LogTrace(mlRawToDigi_) 
00107               << ss.str();
00108           }
00109         }
00110 
00111         // check channel
00112         const uint8_t fedCh = iconn->fedCh();
00113 
00114         if (!buffer->channelGood(fedCh,doAPVEmulatorCheck_)) {
00115           if (edm::isDebugEnabled()) {
00116             std::ostringstream ss;
00117             ss << "Problem unpacking channel " << fedCh << " on FED " << fedId;
00118             edm::LogWarning(sistrip::mlRawToCluster_) << ss.str();
00119           }
00120           continue;
00121         }
00122       
00123         // Determine APV std::pair number
00124         uint16_t ipair = iconn->apvPairNumber();
00125 
00126         const sistrip::FEDReadoutMode mode = buffer->readoutMode();
00127         if (mode == sistrip::READOUT_MODE_ZERO_SUPPRESSED ) { 
00128         
00129           try {
00130             // create unpacker
00131             sistrip::FEDZSChannelUnpacker unpacker = sistrip::FEDZSChannelUnpacker::zeroSuppressedModeUnpacker(buffer->channel(fedCh));
00132             
00133             // unpack
00134             while (unpacker.hasData()) {
00135               clusterizer_->stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
00136               unpacker++;
00137             }
00138           } catch (const cms::Exception& e) {
00139             if (edm::isDebugEnabled()) {
00140               std::ostringstream ss;
00141               ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
00142               edm::LogWarning(sistrip::mlRawToCluster_) << ss.str();
00143             }
00144             continue;
00145           }
00146         }
00147 
00148         else if (mode == sistrip::READOUT_MODE_ZERO_SUPPRESSED_LITE ) { 
00149         
00150           try {
00151             // create unpacker
00152             sistrip::FEDZSChannelUnpacker unpacker = sistrip::FEDZSChannelUnpacker::zeroSuppressedLiteModeUnpacker(buffer->channel(fedCh));
00153 
00154             // unpack
00155             while (unpacker.hasData()) {
00156               clusterizer_->stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
00157               unpacker++;
00158             }
00159           } catch (const cms::Exception& e) {
00160             if (edm::isDebugEnabled()) {
00161               std::ostringstream ss;
00162               ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
00163               edm::LogWarning(sistrip::mlRawToCluster_) << ss.str();
00164             }                                               
00165             continue;
00166           }
00167         }
00168 
00169         else if (mode == sistrip::READOUT_MODE_VIRGIN_RAW ) {
00170 
00171           // create unpacker
00172           sistrip::FEDRawChannelUnpacker unpacker = sistrip::FEDRawChannelUnpacker::virginRawModeUnpacker(buffer->channel(fedCh));
00173 
00174           // unpack
00175           std::vector<int16_t> digis;
00176           while (unpacker.hasData()) {
00177             digis.push_back(unpacker.adc());
00178             unpacker++;
00179           }
00180 
00181           //process raw
00182           uint32_t id = iconn->detId();
00183           edm::DetSet<SiStripDigi> zsdigis(id);
00184           //rawAlgos_->subtractorPed->subtract( id, ipair*256, digis);
00185           //rawAlgos_->subtractorCMN->subtract( id, digis);
00186           //rawAlgos_->suppressor->suppress( digis, zsdigis);
00187           uint16_t firstAPV = ipair*2;
00188           rawAlgos_->SuppressVirginRawData(id, firstAPV,digis, zsdigis);  
00189          for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
00190             clusterizer_->stripByStripAdd( it->strip(), it->adc(), record);
00191           }
00192         }
00193 
00194         else if (mode == sistrip::READOUT_MODE_PROC_RAW ) {
00195 
00196           // create unpacker
00197           sistrip::FEDRawChannelUnpacker unpacker = sistrip::FEDRawChannelUnpacker::procRawModeUnpacker(buffer->channel(fedCh));
00198 
00199           // unpack
00200           std::vector<int16_t> digis;
00201           while (unpacker.hasData()) {
00202             digis.push_back(unpacker.adc());
00203             unpacker++;
00204           }
00205 
00206           //process raw
00207           uint32_t id = iconn->detId();
00208           edm::DetSet<SiStripDigi> zsdigis(id);
00209           //rawAlgos_->subtractorCMN->subtract( id, digis);
00210           //rawAlgos_->suppressor->suppress( digis, zsdigis);
00211            uint16_t firstAPV = ipair*2;
00212           rawAlgos_->SuppressProcessedRawData(id, firstAPV,digis, zsdigis); 
00213           for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
00214             clusterizer_->stripByStripAdd( it->strip(), it->adc(), record);
00215           }
00216         }
00217 
00218         else {
00219           edm::LogWarning(sistrip::mlRawToCluster_)
00220             << "[sistrip::RawToClustersLazyGetter::" 
00221             << __func__ << "]"
00222             << " FEDRawData readout mode "
00223             << mode
00224             << " from FED id "
00225             << fedId 
00226             << " not supported."; 
00227           continue;
00228         }
00229       }
00230       clusterizer_->stripByStripEnd(record);
00231     }
00232   }
00233 
00234   
00235 }