CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DQM/SiStripMonitorHardware/plugins/SiStripCMMonitor.cc

Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 //
00004 // Package:    DQM/SiStripMonitorHardware
00005 // Class:      SiStripCMMonitorPlugin
00006 // 
00011 //
00012 //         Created:  2009/07/22
00013 // $Id: SiStripCMMonitor.cc,v 1.20 2011/10/04 17:25:59 amagnan Exp $
00014 //
00015 
00016 #include <sstream>
00017 #include <memory>
00018 #include <list>
00019 #include <algorithm>
00020 #include <cassert>
00021 
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/EDAnalyzer.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/Framework/interface/EventSetup.h"
00026 #include "FWCore/Framework/interface/ESHandle.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/Utilities/interface/InputTag.h"
00029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00030 #include "FWCore/ServiceRegistry/interface/Service.h"
00031 #include "FWCore/Utilities/interface/Exception.h"
00032 
00033 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00034 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00035 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00036 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00037 #include "DataFormats/DetId/interface/DetId.h"
00038 
00039 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00040 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00041 
00042 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
00043 
00044 #include "DQMServices/Core/interface/DQMStore.h"
00045 #include "DQMServices/Core/interface/MonitorElement.h"
00046 
00047 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
00048 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
00049 
00050 #include "DataFormats/Common/interface/DetSetVector.h"
00051 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00052 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00053 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00054 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00055 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00056 
00057 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
00058 
00059 //
00060 // Class declaration
00061 //
00062 
00063 class SiStripCMMonitorPlugin : public edm::EDAnalyzer
00064 {
00065  public:
00066 
00067   explicit SiStripCMMonitorPlugin(const edm::ParameterSet&);
00068   ~SiStripCMMonitorPlugin();
00069  private:
00070 
00071   struct Statistics {
00072     double Mean;
00073     double Rms;
00074     double Counter;
00075   };
00076 
00077   virtual void beginJob();
00078   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00079   virtual void endJob();
00080 
00081   //update the cabling if necessary
00082   void updateCabling(const edm::EventSetup& eventSetup);
00083 
00084 
00085   void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians);
00086 
00087   //tag of FEDRawData collection
00088   edm::InputTag rawDataTag_;
00089   //folder name for histograms in DQMStore
00090   std::string folderName_;
00091   //vector of fedIDs which will have detailed histograms made
00092   std::vector<unsigned int> fedIdVec_;
00093   //book detailed histograms even if they will be empty (for merging)
00094   bool fillAllDetailedHistograms_;
00095   //do histos vs time with time=event number. Default time = orbit number (s)
00096   bool fillWithEvtNum_;
00097   bool fillWithLocalEvtNum_;
00098   //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
00099   unsigned int printDebug_;
00100   //write the DQMStore to a root file at the end of the job
00101   bool writeDQMStore_;
00102   std::string dqmStoreFileName_;
00103   //the DQMStore
00104   DQMStore* dqm_;
00105   //FED cabling
00106   uint32_t cablingCacheId_;
00107   const SiStripFedCabling* cabling_;
00108 
00109   //add parameter to save computing time if TkHistoMap are not filled
00110   bool doTkHistoMap_;
00111 
00112   CMHistograms cmHists_;
00113 
00114   std::map<unsigned int,Statistics> CommonModes_;
00115   std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
00116 
00117   std::pair<uint16_t,uint16_t> prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED];
00118 
00119   unsigned int evt_;
00120 
00121 };
00122 
00123 
00124 //
00125 // Constructors and destructor
00126 //
00127 
00128 SiStripCMMonitorPlugin::SiStripCMMonitorPlugin(const edm::ParameterSet& iConfig)
00129   : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
00130     folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
00131     fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
00132     fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
00133     fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
00134     fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
00135     printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
00136     writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
00137     dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
00138     dqm_(0),
00139     cablingCacheId_(0)
00140     
00141 {
00142   //print config to debug log
00143   std::ostringstream debugStream;
00144   if (printDebug_>1) {
00145     debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
00146                 << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
00147                 << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
00148                 << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
00149                 << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
00150                 << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
00151                 << "[SiStripCMMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
00152     if (writeDQMStore_) debugStream << "[SiStripCMMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
00153   }
00154     
00155  std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
00156 
00157  cmHists_.initialise(iConfig,pDebugStream);
00158 
00159  doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
00160 
00161  CommonModes_.clear();
00162  CommonModesAPV0minusAPV1_.clear();
00163 
00164  for (unsigned int fedId(FEDNumbering::MINSiStripFEDID); fedId <= FEDNumbering::MAXSiStripFEDID; fedId++){
00165    for (unsigned int iCh(0); iCh<sistrip::FEDCH_PER_FED; iCh++){
00166      prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(0,0);
00167    }
00168  }
00169 
00170 
00171  if (printDebug_) {
00172    LogTrace("SiStripMonitorHardware") << debugStream.str();
00173  }
00174 
00175  evt_ = 0;
00176 
00177 }
00178 
00179 SiStripCMMonitorPlugin::~SiStripCMMonitorPlugin()
00180 {
00181 }
00182 
00183 
00184 //
00185 // Member functions
00186 //
00187 
00188 // ------------ method called to for each event  ------------
00189 void
00190 SiStripCMMonitorPlugin::analyze(const edm::Event& iEvent, 
00191                                  const edm::EventSetup& iSetup)
00192 {
00193 
00194   //static bool firstEvent = true;
00195   //static bool isBeingFilled = false;
00196   //update cabling
00197   updateCabling(iSetup);
00198   
00199   //get raw data
00200   edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
00201   iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
00202   const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
00203   
00204   //FED errors
00205   FEDErrors lFedErrors;
00206 
00207   //loop over siStrip FED IDs
00208   for (unsigned int fedId = FEDNumbering::MINSiStripFEDID; 
00209        fedId <= FEDNumbering::MAXSiStripFEDID; 
00210        fedId++) {//loop over FED IDs
00211     const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
00212 
00213     //create an object to fill all errors
00214     lFedErrors.initialiseFED(fedId,cabling_);
00215 
00216     //Do detailed check
00217     //first check if data exists
00218     bool lDataExist = lFedErrors.checkDataPresent(fedData);
00219     if (!lDataExist) {
00220       continue;
00221     }
00222 
00223     std::auto_ptr<const sistrip::FEDBuffer> buffer;
00224 
00225     if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
00226       continue;
00227     }
00228     else {
00229       //need to construct full object to go any further
00230       buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
00231       bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
00232       bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
00233       bool feLengthsOK = buffer->checkFEUnitLengths();
00234       if ( !channelLengthsOK ||
00235            !channelPacketCodesOK ||
00236            !feLengthsOK ) {
00237         continue;
00238       }
00239     }
00240 
00241     std::ostringstream infoStream;
00242 
00243     
00244     if (printDebug_ > 1) {
00245       infoStream << " --- Processing FED #" << fedId << std::endl;
00246     }
00247 
00248 
00249     std::vector<CMHistograms::CMvalues> values;
00250 
00251     for (unsigned int iCh = 0; 
00252          iCh < sistrip::FEDCH_PER_FED; 
00253          iCh++) {//loop on channels
00254 
00255       const FedChannelConnection & lConnection = cabling_->connection(fedId,iCh);
00256       bool connected = lConnection.isConnected();
00257 
00258       //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
00259 
00260       if (!connected) {
00261         continue;
00262       }
00263 
00264       uint32_t lDetId = lConnection.detId();
00265       unsigned short nChInModule = lConnection.nApvPairs();
00266 
00267       if (!lDetId || lDetId == sistrip::invalid32_) continue;
00268 
00269       bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
00270 
00271       if (lFailUnpackerChannelCheck) {
00272         continue;
00273       }
00274       
00275 
00276       //short lAPVPair = lConnection.apvPairNumber();
00277       //short lSubDet = DetId(lDetId).subdetId();
00278 
00279 //       if (firstEvent){
00280 //      infoStream << "Subdet " << lSubDet << ", " ;
00281 //      if (lSubDet == 3) {
00282 //        TIBDetId lId(lDetId);
00283 //        infoStream << "TIB layer " << lId.layer()  << ", fedID " << fedId << ", channel " << iCh << std::endl;
00284 //      }
00285 //      else if (lSubDet == 4) {
00286 //        TIDDetId lId(lDetId);
00287 //        infoStream << "TID side " << lId.side()  << " wheel " << lId.wheel() << ", ring " << lId.ring() << ", fedID " << fedId << ", channel " << iCh << std::endl;
00288 //      }
00289 //      else if (lSubDet == 5) {
00290 //        TOBDetId lId(lDetId);
00291 //        infoStream << "TOB side " << lId.rod()[0]  << " layer " << lId.layer() << ", rod " << lId.rodNumber() << ", fedID " << fedId << ", channel " << iCh << std::endl;
00292 //      }
00293 //      else if (lSubDet == 6) {
00294 //        TECDetId lId(lDetId);
00295 //        infoStream << "TEC side " << lId.side()  << " wheel " << lId.wheel() << ", petal " << lId.petalNumber() << ", ring " << lId.ring() << ", fedID " << fedId << ", channel " << iCh << std::endl;
00296 //      }
00297 //      isBeingFilled=true;
00298 //       }
00299 
00300       std::ostringstream lMode;
00301       lMode << buffer->readoutMode();
00302       if (printDebug_ > 1) {
00303         static bool lFirst = true;
00304         if (lFirst) {
00305           std::cout << "Readout mode: " << lMode.str() << std::endl;
00306           lFirst = false;
00307         }
00308 
00309       }
00310 
00311       const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
00312       std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
00313 
00314       if (lMode.str().find("Zero suppressed") != lMode.str().npos && lMode.str().find("lite") == lMode.str().npos) medians = std::pair<uint16_t,uint16_t>(lChannel.cmMedian(0),lChannel.cmMedian(1));
00315       
00316       CMHistograms::CMvalues lVal;
00317       lVal.ChannelID = iCh;
00318       lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
00319       lVal.PreviousMedians = prevMedians_[fedId][iCh];
00320 
00321 //       if (medians.second-medians.first > 26){
00322 //      std::ostringstream info;
00323 //      if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
00324 //      else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
00325 //      edm::LogVerbatim("SiStripMonitorHardware") << info.str();
00326 //       }
00327 
00328       if (printDebug_ > 1) {
00329         if (lChannel.length() > 7) {
00330           infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
00331         }
00332       }
00333 
00334       values.push_back(lVal);
00335 
00336       //if (iEvent.id().event() > 1000)
00337       fillMaps(lDetId,nChInModule,medians);
00338 
00339       prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
00340       
00341     }//loop on channels
00342     
00343     float lTime = 0;
00344     if (fillWithEvtNum_) lTime = iEvent.id().event();
00345     else if (fillWithLocalEvtNum_) lTime = evt_;//iEvent.id().event();
00346     else lTime = iEvent.orbitNumber()/11223.;
00347 
00348     cmHists_.fillHistograms(values,lTime,fedId);
00349 
00350     //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
00351  
00352 
00353 
00354   }//loop on FEDs
00355 
00356 
00357   //if (isBeingFilled) 
00358   //firstEvent = false;
00359 
00360   evt_++;
00361 
00362 }//analyze method
00363 
00364 // ------------ method called once each job just before starting event loop  ------------
00365 void 
00366 SiStripCMMonitorPlugin::beginJob()
00367 {
00368   //get DQM store
00369   dqm_ = &(*edm::Service<DQMStore>());
00370   dqm_->setCurrentFolder(folderName_);
00371 
00372   cmHists_.bookTopLevelHistograms(dqm_);
00373 
00374   if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
00375 
00376 }
00377 
00378 // ------------ method called once each job just after ending the event loop  ------------
00379 void 
00380 SiStripCMMonitorPlugin::endJob()
00381 {
00382 
00383   if (doTkHistoMap_) {//if TkHistoMap is enabled
00384     std::map<unsigned int,Statistics>::iterator fracIter;
00385 
00386     //int ele = 0;
00387     //int nBadChannels = 0;
00388     for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
00389       uint32_t detid = fracIter->first;
00390       //if ((fracIter->second).second != 0) {
00391       //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
00392       //nBadChannels++;
00393       //}
00394       float mean = 0;
00395       float rms = 0;
00396       Statistics lStat = fracIter->second;
00397       if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
00398       if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
00399       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
00400       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
00401 
00402       if (printDebug_ > 1) {
00403         std::ostringstream message;
00404         message << "TkHistoMap CM: Detid " << detid << ", mean = " <<  mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
00405         edm::LogVerbatim("SiStripMonitorHardware") << message.str();
00406       }
00407 
00408       //ele++;
00409     }
00410 
00411     for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
00412       uint32_t detid = fracIter->first;
00413       //if ((fracIter->second).second != 0) {
00414       //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
00415       //nBadChannels++;
00416       //}
00417       float mean = 0;
00418       float rms = 0;
00419       Statistics lStat = fracIter->second;
00420       if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
00421       if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
00422       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
00423       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
00424 
00425       if (printDebug_ > 1) {
00426         std::ostringstream message;
00427         message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " <<  mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
00428         edm::LogVerbatim("SiStripMonitorHardware") << message.str();
00429       }
00430 
00431       //ele++;
00432     }
00433 
00434   }//if TkHistoMap is enabled
00435 
00436   if (writeDQMStore_) dqm_->save(dqmStoreFileName_);
00437 }
00438 
00439 void SiStripCMMonitorPlugin::updateCabling(const edm::EventSetup& eventSetup)
00440 {
00441   uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00442   if (cablingCacheId_ != currentCacheId) {
00443     edm::ESHandle<SiStripFedCabling> cablingHandle;
00444     eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
00445     cabling_ = cablingHandle.product();
00446     cablingCacheId_ = currentCacheId;
00447   }
00448 }
00449 
00450 
00451 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
00452 {
00453 
00454   if (doTkHistoMap_){//if TkHistMap is enabled
00455     std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
00456 
00457     Statistics lStat;
00458     lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
00459     lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
00460     lStat.Counter = 1./aChInModule;
00461 
00462     alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
00463     if (!alreadyThere[0].second) {
00464       ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
00465       ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
00466       ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
00467     }
00468 
00469     lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
00470     lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
00471     lStat.Counter = 1./aChInModule;
00472 
00473     alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
00474     if (!alreadyThere[1].second) {
00475       ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
00476       ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
00477       ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
00478     }
00479 
00480   }
00481 
00482 }
00483 
00484 
00485 //
00486 // Define as a plug-in
00487 //
00488 
00489 #include "FWCore/Framework/interface/MakerMacros.h"
00490 DEFINE_FWK_MODULE(SiStripCMMonitorPlugin);