CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/EventFilter/SiStripRawToDigi/src/SiStripDigiToRaw.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripDigiToRaw.cc,v 1.41 2009/09/14 14:01:04 nc302 Exp $
00002 
00003 #include "EventFilter/SiStripRawToDigi/interface/SiStripDigiToRaw.h"
00004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00005 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00006 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00007 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00008 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00009 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
00010 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00011 #include "FWCore/Utilities/interface/CRC16.h"
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include <iostream>
00014 #include <sstream>
00015 #include <vector>
00016 
00017 namespace sistrip {
00018 
00019   // -----------------------------------------------------------------------------
00021   DigiToRaw::DigiToRaw( FEDReadoutMode mode, 
00022                         bool useFedKey ) : 
00023     mode_(mode),
00024     useFedKey_(useFedKey),
00025     bufferGenerator_()
00026   {
00027     if ( edm::isDebugEnabled() ) {
00028       LogDebug("DigiToRaw")
00029         << "[sistrip::DigiToRaw::DigiToRaw]"
00030         << " Constructing object...";
00031     }
00032     bufferGenerator_.setReadoutMode(mode_);
00033   }
00034 
00035   // -----------------------------------------------------------------------------
00037   DigiToRaw::~DigiToRaw() {
00038     if ( edm::isDebugEnabled() ) {
00039       LogDebug("DigiToRaw")
00040         << "[sistrip::DigiToRaw::~DigiToRaw]"
00041         << " Destructing object...";
00042     }
00043   }
00044 
00045   // -----------------------------------------------------------------------------
00054   void DigiToRaw::createFedBuffers( edm::Event& event,
00055                                     edm::ESHandle<SiStripFedCabling>& cabling,
00056                                     edm::Handle< edm::DetSetVector<SiStripDigi> >& collection,
00057                                     std::auto_ptr<FEDRawDataCollection>& buffers ) { 
00058     createFedBuffers_(event, cabling, collection, buffers, true);
00059   }
00060   
00061   void DigiToRaw::createFedBuffers( edm::Event& event,
00062                                     edm::ESHandle<SiStripFedCabling>& cabling,
00063                                     edm::Handle< edm::DetSetVector<SiStripRawDigi> >& collection,
00064                                     std::auto_ptr<FEDRawDataCollection>& buffers ) { 
00065     createFedBuffers_(event, cabling, collection, buffers, false);
00066   }
00067   
00068   template<class Digi_t>
00069   void DigiToRaw::createFedBuffers_( edm::Event& event,
00070                                      edm::ESHandle<SiStripFedCabling>& cabling,
00071                                      edm::Handle< edm::DetSetVector<Digi_t> >& collection,
00072                                      std::auto_ptr<FEDRawDataCollection>& buffers,
00073                                      bool zeroSuppressed) {
00074     try {
00075       
00076       //set the L1ID to use in the buffers
00077       bufferGenerator_.setL1ID(0xFFFFFF & event.id().event());
00078       
00079       const std::vector<uint16_t>& fed_ids = cabling->feds();
00080       std::vector<uint16_t>::const_iterator ifed;
00081       
00082       for ( ifed = fed_ids.begin(); ifed != fed_ids.end(); ifed++ ) {
00083         
00084         const std::vector<FedChannelConnection>& conns = cabling->connections(*ifed);
00085         std::vector<FedChannelConnection>::const_iterator iconn = conns.begin();
00086         
00087         FEDStripData fedData(zeroSuppressed);
00088         
00089         for ( ; iconn != conns.end(); iconn++ ) {
00090           
00091           // Determine FED key from cabling
00092           uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
00093         
00094           // Determine whether DetId or FED key should be used to index digi containers
00095           uint32_t key = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? fed_key : iconn->detId();
00096           
00097           // Check key is non-zero and valid
00098           if ( !key || ( key == sistrip::invalid32_ ) ) { continue; }
00099 
00100           // Determine APV pair number (needed only when using DetId)
00101           uint16_t ipair = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? 0 : iconn->apvPairNumber();
00102           
00103           FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
00104 
00105           // Find digis for DetID in collection
00106           typename std::vector< edm::DetSet<Digi_t> >::const_iterator digis = collection->find( key );
00107           if (digis == collection->end()) { continue; } 
00108 
00109           typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
00110           for ( idigi = digis_begin; idigi != digis->data.end(); idigi++ ) {
00111             
00112             if ( STRIP(idigi, digis_begin) < ipair*256 ||
00113                  STRIP(idigi, digis_begin) > ipair*256+255 ) { continue; }
00114             const unsigned short strip = STRIP(idigi, digis_begin) % 256;
00115 
00116             if ( strip >= STRIPS_PER_FEDCH ) {
00117               if ( edm::isDebugEnabled() ) {
00118                 std::stringstream ss;
00119                 ss << "[sistrip::DigiToRaw::createFedBuffers]"
00120                    << " strip >= strips_per_fedCh";
00121                 edm::LogWarning("DigiToRaw") << ss.str();
00122               }
00123               continue;
00124             }
00125           
00126             // check if value already exists
00127             if ( edm::isDebugEnabled() ) {
00128               const uint16_t value = 0;//chanData[strip];
00129               if ( value && value != (*idigi).adc() ) {
00130                 std::stringstream ss; 
00131                 ss << "[sistrip::DigiToRaw::createFedBuffers]" 
00132                    << " Incompatible ADC values in buffer!"
00133                    << "  FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
00134                    << "  DetStrip: " << STRIP(idigi, digis_begin)
00135                    << "  FedChStrip: " << strip
00136                    << "  AdcValue: " << (*idigi).adc()
00137                    << "  RawData[" << strip << "]: " << value;
00138                 edm::LogWarning("DigiToRaw") << ss.str();
00139               }
00140             }
00141 
00142             // Add digi to buffer
00143             chanData[strip] = (*idigi).adc();
00144 
00145           }
00146           // if ((*idigi).strip() >= (ipair+1)*256) break;
00147         }
00148 
00149         //create the buffer
00150         FEDRawData& fedrawdata = buffers->FEDData( *ifed );
00151         bufferGenerator_.generateBuffer(&fedrawdata,fedData,*ifed);
00152         
00153        }
00154     }
00155     catch (const std::exception& e) {
00156       if ( edm::isDebugEnabled() ) {
00157         edm::LogWarning("DigiToRaw") 
00158           << "DigiToRaw::createFedBuffers] " 
00159           << "Exception caught : " << e.what();
00160       }
00161     }
00162   
00163   }
00164 
00165   inline uint16_t DigiToRaw::STRIP(const edm::DetSet<SiStripDigi>::const_iterator& it, const edm::DetSet<SiStripDigi>::const_iterator& begin) const {return it->strip();}
00166   inline uint16_t DigiToRaw::STRIP(const edm::DetSet<SiStripRawDigi>::const_iterator& it, const edm::DetSet<SiStripRawDigi>::const_iterator& begin) const {return it-begin;}
00167 
00168 }
00169