CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DQM/HcalMonitorModule/src/HcalMonitorModule.cc

Go to the documentation of this file.
00001 #include <DQM/HcalMonitorModule/interface/HcalMonitorModule.h>
00002 
00003 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
00004 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00005 #include "DataFormats/DetId/interface/DetId.h"
00006 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00007 #include "DataFormats/HcalDigi/interface/HcalCalibrationEventTypes.h"
00008 #include "DataFormats/HcalDigi/interface/HcalUnpackerReport.h"
00009 #include "DataFormats/Provenance/interface/EventID.h"  
00010 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00011 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00012 
00013 #include "FWCore/Framework/interface/MakerMacros.h"
00014 #include "FWCore/Framework/interface/ESHandle.h"
00015 #include "FWCore/Framework/interface/LuminosityBlock.h"
00016 #include "FWCore/Framework/interface/Run.h"
00017 #include "FWCore/ServiceRegistry/interface/Service.h"
00018 
00019 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00020 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00021 
00022 #include "CondFormats/HcalObjects/interface/HcalChannelStatus.h"
00023 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
00024 #include "CondFormats/HcalObjects/interface/HcalCondObjectContainer.h"
00025 
00026 #include "EventFilter/HcalRawToDigi/interface/HcalDCCHeader.h"
00027 #include "EventFilter/HcalRawToDigi/interface/HcalHTRData.h"
00028 
00029 #include "DQMServices/Core/interface/DQMStore.h"
00030 #include "DQMServices/Core/interface/MonitorElement.h"
00031 
00032 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
00033 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00034 
00035 #include <memory>
00036 #include <iostream>
00037 #include <fstream>
00038 #include <vector>
00039 #include <string>
00040 
00041 /*
00042  * \file HcalMonitorModule.cc
00043  *
00044  * $Date: 2012/05/27 11:31:22 $
00045  * $Revision: 1.166 $
00046  * \author J Temple
00047  *
00048  * New version of HcalMonitorModule stores only a few necessary variables that other tasks need to grab
00049  * (Online_ boolean, subsystem folder name, subdetector presence check, etc.)
00050  * Modeled heavily on EcalBarrelMonitorModule code.
00051 
00052  * Its only function during its analyze function is to determine the event type (normal, pedestal, laser, etc.) and to perform checks to see which subdetectors are present.  Heavy lifting will be done by individual tasks.
00053 
00054 */
00055 
00056 
00057 //Constructor
00058 
00059 HcalMonitorModule::HcalMonitorModule(const edm::ParameterSet& ps)
00060 {  // Set initial values
00061   init_=false; // first event sets up Monitor Elements and sets init_ to true
00062 
00063   // get ps objects
00064   Online_                = ps.getUntrackedParameter<bool>("online",false);
00065   mergeRuns_             = ps.getUntrackedParameter<bool>("mergeRuns",false);
00066   enableCleanup_         = ps.getUntrackedParameter<bool>("enableCleanup",false);
00067   debug_                 = ps.getUntrackedParameter<int>("debug",0);
00068   
00069   FEDRawDataCollection_  = ps.getUntrackedParameter<edm::InputTag>("FEDRawDataCollection");
00070   inputLabelReport_      = ps.getUntrackedParameter<edm::InputTag>("UnpackerReport");
00071   
00072   prefixME_              = ps.getUntrackedParameter<std::string>("subSystemFolder","Hcal/");
00073   if (prefixME_.substr(prefixME_.size()-1,prefixME_.size())!="/")
00074     prefixME_.append("/");
00075   
00076   NLumiBlocks_           = ps.getUntrackedParameter<int>("NLumiBlocks",4000);
00077 
00078 } // HcalMonitorModule::HcalMonitorModule
00079 
00080 
00081 //Destructor
00082 HcalMonitorModule::~HcalMonitorModule()
00083 {
00084 
00085 } //HcalMonitorModule::~HcalMonitorModule()
00086 
00087 
00088 
00089 void HcalMonitorModule::beginJob(void)
00090 {
00091   if (debug_>0) std::cout <<"HcalMonitorModule::beginJob()"<<std::endl;
00092   // Get DQM service
00093   dbe_ = edm::Service<DQMStore>().operator->();
00094   // set default values
00095   ievt_=0;
00096   fedsListed_=false;
00097   HBpresent_=0;
00098   HEpresent_=0;
00099   HOpresent_=0;
00100   HFpresent_=0;
00101   // Set pointers to null
00102   meCalibType_=0;
00103   meFEDS_=0;
00104   meIevt_=0;
00105   meIevtHist_=0;
00106   meEvtsVsLS_=0;
00107   meProcessedEndLumi_=0;
00108   meHB_=0;
00109   meHE_=0;
00110   meHO_=0;
00111   meHF_=0;
00112   eMap_=0;
00113 }
00114 
00115 void HcalMonitorModule::beginRun(const edm::Run& r, const edm::EventSetup& c) 
00116 {
00117   if ( debug_>0 ) std::cout << "HcalMonitorModule: beginRun" << std::endl;
00118   // reset histograms & counters on a new run, unless merging allowed
00119 
00120   if (eMap_==0) //eMap_ not created yet
00121     {
00122       if (debug_>1) std::cout <<"\t<HcalMonitorModule::beginRun> Getting Emap!"<<std::endl;
00123       edm::ESHandle<HcalDbService> pSetup;
00124       c.get<HcalDbRecord>().get( pSetup );
00125       eMap_=pSetup->getHcalMapping(); 
00126     }
00127   if (mergeRuns_) return;
00128   this->setup();
00129   this->reset();
00130 
00131 } //HcalMonitorModule::beginRun(....)
00132 
00133 
00134 void HcalMonitorModule::endRun(const edm::Run& r, const edm::EventSetup& c) {
00135 
00136   if ( debug_>0 ) std::cout << "HcalMonitorModule: endRun" << std::endl;
00137 
00138   // end-of-run
00139   if ( meStatus_ ) meStatus_->Fill(2);
00140 
00141   if ( meRun_ ) meRun_->Fill(runNumber_);
00142   if ( meEvt_ ) meEvt_->Fill(evtNumber_);
00143 }
00144 
00145 void HcalMonitorModule::reset(void)
00146 {
00147   if (debug_>0) std::cout <<"HcalMonitorModule::reset"<<std::endl;
00148   // Call Reset() on all MonitorElement histograms
00149   if (meCalibType_) meCalibType_->Reset();
00150   if (meFEDS_) meFEDS_->Reset();
00151   if (meIevt_) meIevt_->Fill(0);
00152   if (meIevtHist_) meIevtHist_->Reset();
00153   if (meEvtsVsLS_) meEvtsVsLS_->Reset();
00154   ievt_=0;
00155   if (meProcessedEndLumi_) meProcessedEndLumi_->Fill(-1);
00156   if (meHB_) meHB_->Fill(-1);
00157   if (meHE_) meHE_->Fill(-1);
00158   if (meHO_) meHO_->Fill(-1);
00159   if (meHF_) meHF_->Fill(-1);
00160   HBpresent_=0;
00161   HEpresent_=0;
00162   HOpresent_=0;
00163   HFpresent_=0;
00164   fedsListed_=false;
00165 } // void HcalMonitorModule::reset(void)
00166 
00167 void HcalMonitorModule::setup(void)
00168 {
00169   // Run this on first event in run; set up all necessary monitor elements
00170   if (debug_>0) std::cout <<"HcalMonitorModule::setup"<<std::endl;
00171   init_=true;
00172   if (dbe_)
00173     {
00174       dbe_->setCurrentFolder(prefixME_+"HcalInfo");
00175       meStatus_ = dbe_->bookInt("STATUS");
00176       if (meStatus_) meStatus_->Fill(-1);
00177       meRun_ = dbe_->bookInt("RUN");
00178       if (meRun_) meRun_->Fill(-1);
00179       meEvt_ = dbe_->bookInt("EVT");
00180       if (meEvt_) meEvt_->Fill(-1);
00181       meIevt_ = dbe_->bookInt("EventsProcessed");
00182       if (meIevt_) meIevt_->Fill(-1);
00183       meIevtHist_ = dbe_->book1D("EventsInHcalMonitorModule","Events Seen by HcalMonitorModule",1,0.5,1.5);
00184       meIevtHist_->setBinLabel(1,"Nevents",1);
00185       meEvtsVsLS_ = dbe_->book1D("EventsVsLS","Events vs. Luminosity Section;LS;# events",NLumiBlocks_,0.5,NLumiBlocks_+0.5);
00186       meOnline_ = dbe_->bookInt("Online");
00187       meOnline_->Fill((int)Online_);
00188       meProcessedEndLumi_ = dbe_->bookInt("EndLumiBlock_MonitorModule");
00189       if (meProcessedEndLumi_) meProcessedEndLumi_->Fill(-1);
00190       meCurrentCalibType_= dbe_->bookInt("CURRENT_EVENT_TYPE");
00191       if (meCurrentCalibType_) meCurrentCalibType_->Fill(-1);
00192       
00193       meHB_ = dbe_->bookInt("HBpresent");
00194       meHE_ = dbe_->bookInt("HEpresent");
00195       meHO_ = dbe_->bookInt("HOpresent");
00196       meHF_ = dbe_->bookInt("HFpresent");
00197       if (meHB_) meHB_->Fill(-1);
00198       if (meHE_) meHE_->Fill(-1);
00199       if (meHO_) meHO_->Fill(-1);
00200       if (meHF_) meHF_->Fill(-1);
00201 
00202       meFEDS_    = dbe_->book1D("FEDs Unpacked","FEDs Unpacked; Hcal FEDs 700-731",1+(FEDNumbering::MAXHCALFEDID-FEDNumbering::MINHCALFEDID),FEDNumbering::MINHCALFEDID-0.5,FEDNumbering::MAXHCALFEDID+0.5);
00203 
00204       meCalibType_ = dbe_->book1D("CalibrationType","Calibration Type",9,-0.5,8.5);
00205       meCalibType_->setBinLabel(1,"Normal",1);
00206       meCalibType_->setBinLabel(2,"Ped",1);
00207       meCalibType_->setBinLabel(3,"RADDAM",1);
00208       meCalibType_->setBinLabel(4,"HBHEHPD",1);
00209       meCalibType_->setBinLabel(5,"HOHPD",1);
00210       meCalibType_->setBinLabel(6,"HFPMT",1);
00211       meCalibType_->setBinLabel(7,"ZDC",1);
00212       meCalibType_->setBinLabel(8,"CASTOR",1);
00213 
00214     } // if (dbe_)
00215   return;
00216 } // void HcalMonitorModule::setup(void)
00217 
00218 
00219 void HcalMonitorModule::cleanup(void)
00220 {
00221   if (debug_>0) std::cout <<"HcalMonitorModule::cleanup"<<std::endl;
00222   if (!enableCleanup_) return;
00223   if (dbe_)
00224     {
00225       dbe_->setCurrentFolder(prefixME_+"HcalInfo");
00226       if ( meStatus_ ) 
00227         dbe_->removeElement( meStatus_->getName() );
00228       meStatus_ = 0;
00229       if ( meRun_ ) 
00230         dbe_->removeElement( meRun_->getName() );
00231       meRun_ = 0;
00232       if ( meEvt_ ) 
00233         dbe_->removeElement( meEvt_->getName() );
00234       meEvt_ = 0;
00235       if (meIevt_) 
00236         dbe_->removeElement(meIevt_->getName());
00237       meIevt_=0;
00238       if (meIevtHist_)
00239         dbe_->removeElement(meIevtHist_->getName());
00240       meIevtHist_=0;
00241       if (meFEDS_) 
00242         dbe_->removeElement(meFEDS_->getName());
00243       meFEDS_ = 0;
00244       if (meCalibType_) 
00245         dbe_->removeElement(meCalibType_->getName());
00246       meCalibType_ = 0;
00247       if (meCurrentCalibType_) 
00248         dbe_->removeElement(meCurrentCalibType_->getName());
00249       meCurrentCalibType_=0;
00250       if (meProcessedEndLumi_) 
00251         dbe_->removeElement(meProcessedEndLumi_->getName());
00252       meProcessedEndLumi_ = 0;
00253       if (meHB_) 
00254         dbe_->removeElement(meHB_->getName());
00255       meHB_=0;  
00256 
00257       if (meHE_) 
00258         dbe_->removeElement(meHE_->getName());
00259       meHE_=0;
00260       if (meHO_) 
00261         dbe_->removeElement(meHO_->getName());
00262       meHO_=0;
00263       if (meHF_) 
00264         dbe_->removeElement(meHF_->getName());
00265       meHF_=0;
00266     } // if (dbe_)
00267 
00268   fedsListed_=false;
00269   HBpresent_=0;
00270   HEpresent_=0;
00271   HOpresent_=0;
00272   HFpresent_=0;
00273   init_=false;
00274 
00275 } // void HcalMonitorModule::cleanup(void)
00276 
00277 
00278 
00279 void HcalMonitorModule::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00280                                                 const edm::EventSetup& c) 
00281 {
00282   if (debug_>0) std::cout <<"HcalMonitorModule::beginLuminosityBlock"<<std::endl;
00283 }// void HcalMonitorModule::beginLuminosityBlock(...)
00284 
00285 
00286 
00287 
00288 void HcalMonitorModule::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00289                                               const edm::EventSetup& c) 
00290 {
00291   if (debug_>0) std::cout <<"HcalMonitorModule::endLuminosityBlock"<<std::endl;
00292   meProcessedEndLumi_->Fill(lumiSeg.luminosityBlock());
00293 }// void HcalMonitorModule::endLuminosityBlock(...)
00294 
00295 
00296 
00297 void HcalMonitorModule::endJob(void)
00298 {
00299   if (debug_>0) std::cout <<"HcalMonitorModule::endJob()"<<std::endl;
00300   if (dbe_)
00301     {
00302       meStatus_ = dbe_->get(prefixME_ + "/EventInfo/STATUS");
00303       meRun_ = dbe_->get(prefixME_ + "/EventInfo/RUN");
00304       meEvt_ = dbe_->get(prefixME_ + "/EventInfo/EVT");
00305     }
00306   if (meStatus_) meStatus_->Fill(2);
00307   if (meRun_) meRun_->Fill(runNumber_);
00308   if (meEvt_) meEvt_->Fill(evtNumber_);
00309   if (init_) this->cleanup();
00310 } // void HcalMonitorModule::endJob(void)
00311 
00312 
00313 
00314 void HcalMonitorModule::analyze(const edm::Event& e, const edm::EventSetup& c)
00315 {
00316   if (!init_) this->setup();
00317   
00318   LogDebug("HcalMonitorModule")<<"processing event "<<ievt_;
00319   
00320   // Fill Monitor Elements with run, evt, processed event info
00321   ++ievt_;
00322   runNumber_=e.id().run();
00323   evtNumber_=e.id().event();
00324   if (meRun_) meRun_->Fill(runNumber_);
00325   if (meEvt_) meEvt_->Fill(evtNumber_);
00326   if (meIevt_) meIevt_->Fill(ievt_);
00327   if (meIevtHist_) meIevtHist_->Fill(1);
00328   if (meEvtsVsLS_) meEvtsVsLS_->Fill(e.luminosityBlock(),1);
00329   if (ievt_==1)
00330     {
00331       LogDebug("HcalMonitorModule") << "processing run " << runNumber_;
00332       // begin-of-run
00333       if ( meStatus_ ) meStatus_->Fill(0);
00334     } 
00335   else 
00336     {
00337       // running
00338       if ( meStatus_ ) meStatus_->Fill(1);
00339     }
00340   
00341   // Try to get raw data
00342   edm::Handle<FEDRawDataCollection> rawraw;  
00343   if (!(e.getByLabel(FEDRawDataCollection_,rawraw)))
00344     {
00345       edm::LogWarning("HcalMonitorModule")<<" raw data with label "<<FEDRawDataCollection_ <<" not available";
00346       return;
00347     }
00348   
00349   // Get Event Calibration Type -- copy of Bryan Dahmes' filter
00350   int calibType=-1;
00351   int numEmptyFEDs = 0 ;
00352   std::vector<int> calibTypeCounter(8,0) ;
00353   for( int i = FEDNumbering::MINHCALFEDID; i <= FEDNumbering::MAXHCALFEDID; i++) 
00354     {
00355       const FEDRawData& fedData = rawraw->FEDData(i) ;
00356       
00357       if ( fedData.size() < 24 ) numEmptyFEDs++ ;
00358       if ( fedData.size() < 24 ) continue;
00359 
00360       int value = (int)((const HcalDCCHeader*)(fedData.data()))->getCalibType() ;
00361       if(value>7) 
00362         {
00363           edm::LogWarning("HcalMonitorModule::CalibTypeFilter") << "Unexpected Calibration type: "<< value << " in FED: "<<i<<" (should be 0-7). I am bailing out...";
00364           return;
00365         }
00366 
00367       calibTypeCounter.at(value)++ ; // increment the counter for this calib type
00368     } // for (int i = FEDNumbering::MINHCALFEDID; ...)
00369 
00370   int maxCount = 0;
00371   int numberOfFEDIds = FEDNumbering::MAXHCALFEDID  - FEDNumbering::MINHCALFEDID + 1 ;
00372   for (unsigned int i=0; i<calibTypeCounter.size(); i++) {
00373     if ( calibTypeCounter.at(i) > maxCount )
00374       { calibType = i ; maxCount = calibTypeCounter.at(i) ; }
00375     if ( maxCount == numberOfFEDIds ) break ;
00376   }
00377   
00378   if ( maxCount != (numberOfFEDIds-numEmptyFEDs) )
00379     edm::LogWarning("HcalMonitorModule::CalibTypeFilter") << "Conflicting calibration types found.  Assigning type "
00380                                            << calibType ;
00381   LogDebug("HcalMonitorModule::CalibTypeFilter") << "Calibration type is: " << calibType ;
00382   // Fill histogram of calibration types, as well as integer to keep track of current value
00383   if (meCalibType_) meCalibType_->Fill(calibType);
00384   if (meCurrentCalibType_) meCurrentCalibType_->Fill(calibType);
00386 
00387   if (debug_>2) std::cout <<"\t<HcalMonitorModule>  ievt = "<<ievt_<<"  calibration type = "<<calibType<<std::endl;
00388 
00389   // Check to see which subdetectors are present.
00390   // May only need to do this on first event?   Subdets don't appear during a run?
00391   if (HBpresent_==0)
00392     CheckSubdetectorStatus(rawraw, HcalBarrel, *eMap_);
00393   if (HEpresent_==0)
00394     CheckSubdetectorStatus(rawraw, HcalEndcap, *eMap_);
00395   if (HOpresent_==0)
00396     CheckSubdetectorStatus(rawraw, HcalOuter, *eMap_);
00397   if (HFpresent_==0)
00398     CheckSubdetectorStatus(rawraw, HcalForward, *eMap_);
00399   
00400   //  Here, we do need this information each event
00401   edm::Handle<HcalUnpackerReport> report;  
00402   if (!(e.getByLabel(inputLabelReport_,report)))
00403     {
00404       edm::LogWarning("HcalMonitorModule")<<" Unpacker Report "<<inputLabelReport_<<" not available";
00405       return;
00406     }
00407 
00408   if (!fedsListed_)
00409     {
00410       const std::vector<int> feds =  (*report).getFedsUnpacked();    
00411       for(unsigned int f=0; f<feds.size(); ++f)
00412         meFEDS_->Fill(feds[f]);    
00413       fedsListed_ = true;
00414     } // if (!fedsListed_)
00415 
00416 } // void HcalMonitorModule::analyze(...)
00417 
00418 
00419 
00420 void HcalMonitorModule::CheckSubdetectorStatus(const edm::Handle<FEDRawDataCollection>& rawraw,
00421                                                   HcalSubdetector subdet,
00422                                                   const HcalElectronicsMap& emap)
00423 {
00424 
00425   std::vector<int> fedUnpackList;
00426   for (int i=FEDNumbering::MINHCALFEDID; 
00427        i<=FEDNumbering::MAXHCALFEDID; 
00428        i++) 
00429     fedUnpackList.push_back(i);
00430 
00431   if (debug_>1) std::cout <<"<HcalMonitorModule::CheckSubdetectorStatus>  Checking subdetector "<<subdet<<std::endl;
00432   for (std::vector<int>::const_iterator i=fedUnpackList.begin();
00433        i!=fedUnpackList.end(); 
00434        ++i) 
00435     {
00436       if (debug_>2) std::cout <<"\t<HcalMonitorModule::CheckSubdetectorStatus>  FED = "<<*i<<std::endl;
00437       const FEDRawData& fed =(*rawraw).FEDData(*i);
00438       if (fed.size()<12) continue; // Was 16. How do such tiny events even get here?
00439       
00440       // get the DCC header
00441       const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(fed.data());
00442       if (!dccHeader) return;
00443       int dccid=dccHeader->getSourceId();
00444       // check for HF
00445       if (subdet == HcalForward && dccid>717 && dccid<724)
00446         {
00447           HFpresent_=1;
00448           meHF_->Fill(HFpresent_);
00449           return;
00450         }
00451       else if (subdet==HcalOuter && dccid>723)
00452         {
00453           HOpresent_=1;
00454           meHO_->Fill(HOpresent_);
00455           return;
00456         }
00457       else if (dccid<718 && (subdet==HcalBarrel || subdet==HcalEndcap))
00458         {
00459           HcalHTRData htr;  
00460           for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) 
00461             {    
00462               if (!dccHeader->getSpigotPresent(spigot)) continue;
00463               
00464               // Load the given decoder with the pointer and length from this spigot.
00465               dccHeader->getSpigotData(spigot,htr, fed.size()); 
00466               
00467               // check min length, correct wordcount, empty event, or total length if histo event.
00468               if (!htr.check()) continue;
00469               if (htr.isHistogramEvent()) continue;
00470               
00471               int firstFED =  FEDNumbering::MINHCALFEDID;
00472               
00473               // Tease out HB and HE, which share HTRs in HBHE
00474               for(int fchan=0; fchan<3; ++fchan) //0,1,2 are valid
00475                 {
00476                   for(int fib=1; fib<9; ++fib) //1...8 are valid
00477                     {
00478                       HcalElectronicsId eid(fchan,fib,spigot,dccid-firstFED);
00479                       eid.setHTR(htr.readoutVMECrateId(),
00480                                  htr.htrSlot(),htr.htrTopBottom());
00481                       
00482                       DetId did=emap.lookup(eid);
00483                       if (!did.null()) 
00484                         {
00485                           
00486                           if ((HcalSubdetector)did.subdetId()==subdet)
00487                             {
00488                               if (subdet==HcalBarrel)
00489                                 {
00490                                   HBpresent_=1;
00491                                   meHB_->Fill(HBpresent_);
00492                                   return;
00493                                 }
00494                               else if (subdet==HcalEndcap)
00495                               {
00496                                 HEpresent_=1;
00497                                 meHE_->Fill(HEpresent_);
00498                                 return;
00499                               }
00500                             } // if ((HcalSubdetector)did.subdetId()==subdet)
00501                         } // if (!did.null())
00502                     } // for (int fib=1;fib<9;...)
00503                 } // for (int fchan=0; fchan<3;...)
00504             } // for (int spigot=0;spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) 
00505         } //else if (dcc<718 && (subdet...))
00506   } // loop over fedUnpackList
00507   
00508 
00509 } // void HcalMonitorModule::CheckSubdetectorStatus(...)
00510 
00511 DEFINE_FWK_MODULE(HcalMonitorModule);