00001 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDRawDataCheck.h"
00002 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00003 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00004 #include "DataFormats/Common/interface/Handle.h"
00005 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00006 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00007 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00008 #include "EventFilter/SiStripRawToDigi/interface/SiStripRawToDigiUnpacker.h"
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "Fed9UUtils.hh"
00012 #include "ICException.hh"
00013 #include <sstream>
00014 #include <vector>
00015 #include <map>
00016
00017 using namespace std;
00018 using namespace sistrip;
00019
00020
00021
00022 SiStripFEDRawDataCheck::SiStripFEDRawDataCheck( const edm::ParameterSet& pset )
00023 : label_( pset.getUntrackedParameter<string>("ProductLabel","source") ),
00024 instance_( pset.getUntrackedParameter<string>("ProductInstance","") ),
00025 unpacker_( new SiStripRawToDigiUnpacker(0,0,0,0,0) )
00026 {
00027 if ( edm::isDebugEnabled() ) {
00028 LogTrace(mlRawToDigi_)
00029 << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00030 << " Constructing object...";
00031 }
00032 }
00033
00034
00035
00036 SiStripFEDRawDataCheck::~SiStripFEDRawDataCheck()
00037 {
00038 if ( edm::isDebugEnabled() ) {
00039 LogTrace(mlRawToDigi_)
00040 << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00041 << " Destructing object...";
00042 }
00043 if ( unpacker_ ) { delete unpacker_; }
00044 }
00045
00046
00047
00048 void SiStripFEDRawDataCheck::analyze( const edm::Event& event,
00049 const edm::EventSetup& setup ) {
00050
00051
00052 edm::ESHandle<SiStripFedCabling> cabling;
00053 setup.get<SiStripFedCablingRcd>().get( cabling );
00054
00055
00056 edm::Handle<FEDRawDataCollection> buffers;
00057 event.getByLabel( label_, instance_, buffers );
00058
00059
00060 vector<uint16_t> fed_ids = cabling->feds();
00061
00062
00063 typedef pair<uint16_t,uint16_t> Fed;
00064 typedef vector<Fed> Feds;
00065 Feds trg_feds;
00066 Feds trk_feds;
00067 Feds non_trk;
00068 Feds in_data;
00069 Feds in_cabl;
00070
00071 for ( uint16_t ifed = 0; ifed <= sistrip::CMS_FED_ID_MAX; ++ifed ) {
00072 const FEDRawData& fed = buffers->FEDData( static_cast<int>(ifed) );
00073 uint8_t* data = const_cast<uint8_t*>( fed.data() );
00074 FEDTrailer* fed_trailer = reinterpret_cast<FEDTrailer*>( data + fed.size() - sizeof(FEDTrailer) );
00075 if ( fed.size() && fed_trailer->check() ) { trg_feds.push_back(Fed(ifed,fed.size())); }
00076 else {
00077 if ( ifed < sistrip::FED_ID_MIN ||
00078 ifed > sistrip::FED_ID_MAX ) {
00079 if ( fed.size() ) { non_trk.push_back(Fed(ifed,fed.size())); }
00080 } else {
00081 bool found = find( fed_ids.begin(), fed_ids.end(), ifed ) != fed_ids.end();
00082 if ( fed.size() ) {
00083 if ( found ) { trk_feds.push_back(Fed(ifed,fed.size())); }
00084 else { in_data.push_back(Fed(ifed,fed.size())); }
00085 }
00086 if ( found && fed.size() == 0 ) { in_cabl.push_back(Fed(ifed,0)); }
00087 }
00088 }
00089 }
00090
00091
00092 if ( edm::isDebugEnabled() ) {
00093 stringstream ss;
00094 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00095 << " Found " << trg_feds.size()
00096 << " trigger FEDs";
00097 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00098 ss << " with ids (and buffer sizes [bytes]): ";
00099 for ( uint16_t ifed = 0; ifed < trg_feds.size(); ++ifed ) {
00100 ss << trg_feds[ifed].first << "(" << trg_feds[ifed].second << ") ";
00101 }
00102 LogTrace(mlRawToDigi_) << ss.str();
00103 }
00104
00105
00106 if ( edm::isDebugEnabled() ) {
00107 stringstream ss;
00108 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00109 << " Found " << non_trk.size()
00110 << " non-tracker FEDs from other sub-dets";
00111 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00112 ss << " with ids (and buffer sizes [bytes]): ";
00113 for ( uint16_t ifed = 0; ifed < non_trk.size(); ++ifed ) {
00114 ss << non_trk[ifed].first << "(" << non_trk[ifed].second << ") ";
00115 }
00116 LogTrace(mlRawToDigi_) << ss.str();
00117 }
00118
00119
00120 if ( edm::isDebugEnabled() ) {
00121 stringstream ss;
00122 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00123 << " Found " << trk_feds.size()
00124 << " strip tracker FEDs";
00125 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00126 ss << " with ids (and buffer sizes [bytes]): ";
00127 for ( uint16_t ifed = 0; ifed < trk_feds.size(); ++ifed ) {
00128 ss << trk_feds[ifed].first << "(" << trk_feds[ifed].second << ") ";
00129 }
00130 LogTrace(mlRawToDigi_) << ss.str();
00131 }
00132
00133
00134 if ( edm::isDebugEnabled() ) {
00135 stringstream ss;
00136 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00137 << " Found " << in_data.size()
00138 << " FEDs in data but missing from cabling";
00139 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00140 ss << " with ids (and buffer sizes [bytes]): ";
00141 for ( uint16_t ifed = 0; ifed < in_data.size(); ++ifed ) {
00142 ss << in_data[ifed].first << "(" << in_data[ifed].second << ") ";
00143 }
00144 LogTrace(mlRawToDigi_) << ss.str();
00145 }
00146
00147
00148 if ( edm::isDebugEnabled() ) {
00149 stringstream ss;
00150 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00151 << " Found " << in_cabl.size()
00152 << " FEDs in cabling but missing from data";
00153 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00154 ss << " with ids (and zero buffer size): ";
00155 for ( uint16_t ifed = 0; ifed < in_cabl.size(); ++ifed ) {
00156 ss << in_cabl[ifed].first << " ";
00157 }
00158 LogTrace(mlRawToDigi_) << ss.str();
00159 }
00160
00161
00162 typedef vector<uint16_t> Channels;
00163 typedef map<uint16_t,Channels> ChannelsMap;
00164 ChannelsMap construct;
00165 ChannelsMap check;
00166 ChannelsMap address;
00167 ChannelsMap channels_with_data;
00168 ChannelsMap channels_missing_data;
00169 ChannelsMap cabling_missing_channels;
00170 ChannelsMap connected;
00171
00172
00173 Fed9U::Fed9UEvent* fed_event = new Fed9U::Fed9UEvent();
00174 for ( uint16_t ii = 0; ii < trk_feds.size(); ++ii ) {
00175 uint16_t ifed = trk_feds[ii].first;
00176 const FEDRawData& fed = buffers->FEDData( static_cast<int>(ifed) );
00177
00178 FEDRawData output;
00179 if ( unpacker_ ) { unpacker_->locateStartOfFedBuffer( ifed, fed, output ); }
00180
00181 Fed9U::u32* data_u32 = reinterpret_cast<Fed9U::u32*>( const_cast<unsigned char*>( output.data() ) );
00182 Fed9U::u32 size_u32 = static_cast<Fed9U::u32>( output.size() / 4 );
00183
00184 try {
00185 fed_event->Init( data_u32, 0, size_u32 );
00186 } catch ( const ICUtils::ICException& e ) {
00187 construct[ifed].push_back(0);
00188 continue;
00189 }
00190
00191 try {
00192 fed_event->checkEvent();
00193 } catch ( const ICUtils::ICException& e ) {
00194 check[ifed].push_back(0);
00195 continue;
00196 }
00197
00198 vector<FedChannelConnection> channels = cabling->connections(ifed);
00199 for ( uint16_t chan = 0; chan < channels.size(); ++chan ) {
00200 if ( channels[chan].isConnected() ) { connected[ifed].push_back(channels[chan].fedCh()); }
00201 }
00202
00203 for ( uint16_t iunit = 0; iunit < 8; ++iunit ) {
00204 for ( uint16_t ichan = 0; ichan < 12; ++ichan ) {
00205 Fed9U::Fed9UAddress addr;
00206
00207
00208 try {
00209 addr.setAddress( static_cast<unsigned char>( iunit ),
00210 static_cast<unsigned char>( ichan ) );
00211 } catch ( const ICUtils::ICException& e ) {
00212 address[ifed].push_back(addr.getFedChannel());
00213 continue;
00214 }
00215
00216 try {
00217 fed_event->channel( addr );
00218 } catch ( const ICUtils::ICException& e ) {
00219
00220 bool found = find( connected[ifed].begin(),
00221 connected[ifed].end(),
00222 addr.getFedChannel() ) != connected[ifed].end();
00223 if ( found ) { channels_missing_data[ifed].push_back(addr.getFedChannel()); }
00224 continue;
00225 }
00226
00227
00228 channels_with_data[ifed].push_back(addr.getFedChannel());
00229
00230 }
00231 }
00232
00233
00234 uint16_t chan = 0;
00235 bool found = true;
00236 while ( chan < connected[ifed].size() && found ) {
00237 Channels::iterator iter = channels_with_data[ifed].begin();
00238 while ( iter < channels_with_data[ifed].end() ) {
00239 if ( *iter == connected[ifed][chan] ) { break; }
00240 iter++;
00241 }
00242 found = iter != channels_with_data[ifed].end();
00243 if ( !found ) {
00244 cabling_missing_channels[ifed].push_back(connected[ifed][chan]);
00245
00246 }
00247 chan++;
00248 }
00249
00250 }
00251 if ( fed_event ) { delete fed_event; }
00252
00253
00254
00255 if ( edm::isDebugEnabled() ) {
00256 stringstream ss;
00257 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00258 << " Found " << construct.size()
00259 << " FED buffers that fail on construction of a Fed9UEvent";
00260 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00261 ss << " with ids: ";
00262 for ( ChannelsMap::iterator iter = construct.begin(); iter != construct.end(); ++iter ) {
00263 ss << iter->first << " ";
00264 }
00265 LogTrace(mlRawToDigi_) << ss.str();
00266 }
00267
00268
00269 if ( edm::isDebugEnabled() ) {
00270 stringstream ss;
00271 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00272 << " Found " << check.size()
00273 << " FED buffers that fail on Fed9UEvent::checkEvent()";
00274 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00275 ss << " with ids: ";
00276 for ( ChannelsMap::iterator iter = check.begin(); iter != check.end(); ++iter ) {
00277 ss << iter->first << " ";
00278 }
00279 LogTrace(mlRawToDigi_) << ss.str();
00280 }
00281
00282
00283 if ( edm::isDebugEnabled() ) {
00284 stringstream ss;
00285 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00286 << " Found " << address.size()
00287 << " FED buffers that throw Fed9UAddress exceptions";
00288 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00289 ss << " with ids (and number of exceptions): ";
00290 for ( ChannelsMap::iterator iter = address.begin(); iter != address.end(); ++iter ) {
00291 ss << iter->first << "("
00292 << iter->second.size() << ") ";
00293 }
00294 LogTrace(mlRawToDigi_) << ss.str();
00295 }
00296
00297
00298 if ( edm::isDebugEnabled() ) {
00299 stringstream ss;
00300 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00301 << " Found " << channels_with_data.size()
00302 << " FED buffers with data in connected channels";
00303 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00304 ss << " with ids (and number of connected channels with data): ";
00305 for ( ChannelsMap::iterator iter = channels_with_data.begin(); iter != channels_with_data.end(); ++iter ) {
00306 ss << iter->first << "("
00307 << iter->second.size() << ") ";
00308 }
00309 LogTrace(mlRawToDigi_) << ss.str();
00310 }
00311
00312
00313 if ( edm::isDebugEnabled() ) {
00314 stringstream ss;
00315 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00316 << " Found " << channels_missing_data.size()
00317 << " FED buffers with connected channels and missing data";
00318 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00319 ss << " with ids (and number of connected channels with missing data): ";
00320 for ( ChannelsMap::iterator iter = channels_missing_data.begin(); iter != channels_missing_data.end(); ++iter ) {
00321 ss << iter->first << "("
00322 << iter->second.size() << ") ";
00323 }
00324 LogTrace(mlRawToDigi_) << ss.str();
00325 }
00326
00327
00328 if ( edm::isDebugEnabled() ) {
00329 stringstream ss;
00330 ss << "[SiStripFEDRawDataCheck::" << __func__ << "]"
00331 << " Found " << cabling_missing_channels.size()
00332 << " FED buffers with data in channels that are missing from cabling";
00333 edm::LogVerbatim(mlRawToDigi_) << ss.str();
00334 ss << " with ids (number of channels containing data that are missing from cabling, channel number): ";
00335 for ( ChannelsMap::iterator iter = cabling_missing_channels.begin(); iter != cabling_missing_channels.end(); ++iter ) {
00336 ss << iter->first << "("
00337 << iter->second.size() << ":";
00338 for ( Channels::iterator jter = iter->second.begin(); jter != iter->second.end(); ++jter ) {
00339 ss << *jter << ",";
00340 }
00341 ss << ") ";
00342 }
00343 LogTrace(mlRawToDigi_) << ss.str();
00344 }
00345
00346 }