CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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: 2010/07/20 02:58:25 $
00045  * $Revision: 1.165 $
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       int value = (int)((const HcalDCCHeader*)(fedData.data()))->getCalibType() ;
00360       calibTypeCounter.at(value)++ ; // increment the counter for this calib type
00361     } // for (int i = FEDNumbering::MINHCALFEDID; ...)
00362   
00363   int maxCount = 0;
00364   int numberOfFEDIds = FEDNumbering::MAXHCALFEDID  - FEDNumbering::MINHCALFEDID + 1 ;
00365   for (unsigned int i=0; i<calibTypeCounter.size(); i++) {
00366     if ( calibTypeCounter.at(i) > maxCount )
00367       { calibType = i ; maxCount = calibTypeCounter.at(i) ; }
00368     if ( maxCount == numberOfFEDIds ) break ;
00369   }
00370   
00371   if ( maxCount != (numberOfFEDIds-numEmptyFEDs) )
00372     edm::LogWarning("HcalMonitorModule::CalibTypeFilter") << "Conflicting calibration types found.  Assigning type "
00373                                            << calibType ;
00374   LogDebug("HcalMonitorModule::CalibTypeFilter") << "Calibration type is: " << calibType ;
00375   // Fill histogram of calibration types, as well as integer to keep track of current value
00376   if (meCalibType_) meCalibType_->Fill(calibType);
00377   if (meCurrentCalibType_) meCurrentCalibType_->Fill(calibType);
00379 
00380   if (debug_>2) std::cout <<"\t<HcalMonitorModule>  ievt = "<<ievt_<<"  calibration type = "<<calibType<<std::endl;
00381 
00382   // Check to see which subdetectors are present.
00383   // May only need to do this on first event?   Subdets don't appear during a run?
00384   if (HBpresent_==0)
00385     CheckSubdetectorStatus(rawraw, HcalBarrel, *eMap_);
00386   if (HEpresent_==0)
00387     CheckSubdetectorStatus(rawraw, HcalEndcap, *eMap_);
00388   if (HOpresent_==0)
00389     CheckSubdetectorStatus(rawraw, HcalOuter, *eMap_);
00390   if (HFpresent_==0)
00391     CheckSubdetectorStatus(rawraw, HcalForward, *eMap_);
00392   
00393   //  Here, we do need this information each event
00394   edm::Handle<HcalUnpackerReport> report;  
00395   if (!(e.getByLabel(inputLabelReport_,report)))
00396     {
00397       edm::LogWarning("HcalMonitorModule")<<" Unpacker Report "<<inputLabelReport_<<" not available";
00398       return;
00399     }
00400 
00401   if (!fedsListed_)
00402     {
00403       const std::vector<int> feds =  (*report).getFedsUnpacked();    
00404       for(unsigned int f=0; f<feds.size(); ++f)
00405         meFEDS_->Fill(feds[f]);    
00406       fedsListed_ = true;
00407     } // if (!fedsListed_)
00408 
00409 } // void HcalMonitorModule::analyze(...)
00410 
00411 
00412 
00413 void HcalMonitorModule::CheckSubdetectorStatus(const edm::Handle<FEDRawDataCollection>& rawraw,
00414                                                   HcalSubdetector subdet,
00415                                                   const HcalElectronicsMap& emap)
00416 {
00417 
00418   std::vector<int> fedUnpackList;
00419   for (int i=FEDNumbering::MINHCALFEDID; 
00420        i<=FEDNumbering::MAXHCALFEDID; 
00421        i++) 
00422     fedUnpackList.push_back(i);
00423 
00424   if (debug_>1) std::cout <<"<HcalMonitorModule::CheckSubdetectorStatus>  Checking subdetector "<<subdet<<std::endl;
00425   for (std::vector<int>::const_iterator i=fedUnpackList.begin();
00426        i!=fedUnpackList.end(); 
00427        ++i) 
00428     {
00429       if (debug_>2) std::cout <<"\t<HcalMonitorModule::CheckSubdetectorStatus>  FED = "<<*i<<std::endl;
00430       const FEDRawData& fed =(*rawraw).FEDData(*i);
00431       if (fed.size()<12) continue; // Was 16. How do such tiny events even get here?
00432       
00433       // get the DCC header
00434       const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(fed.data());
00435       if (!dccHeader) return;
00436       int dccid=dccHeader->getSourceId();
00437       // check for HF
00438       if (subdet == HcalForward && dccid>717 && dccid<724)
00439         {
00440           HFpresent_=1;
00441           meHF_->Fill(HFpresent_);
00442           return;
00443         }
00444       else if (subdet==HcalOuter && dccid>723)
00445         {
00446           HOpresent_=1;
00447           meHO_->Fill(HOpresent_);
00448           return;
00449         }
00450       else if (dccid<718 && (subdet==HcalBarrel || subdet==HcalEndcap))
00451         {
00452           HcalHTRData htr;  
00453           for (int spigot=0; spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) 
00454             {    
00455               if (!dccHeader->getSpigotPresent(spigot)) continue;
00456               
00457               // Load the given decoder with the pointer and length from this spigot.
00458               dccHeader->getSpigotData(spigot,htr, fed.size()); 
00459               
00460               // check min length, correct wordcount, empty event, or total length if histo event.
00461               if (!htr.check()) continue;
00462               if (htr.isHistogramEvent()) continue;
00463               
00464               int firstFED =  FEDNumbering::MINHCALFEDID;
00465               
00466               // Tease out HB and HE, which share HTRs in HBHE
00467               for(int fchan=0; fchan<3; ++fchan) //0,1,2 are valid
00468                 {
00469                   for(int fib=1; fib<9; ++fib) //1...8 are valid
00470                     {
00471                       HcalElectronicsId eid(fchan,fib,spigot,dccid-firstFED);
00472                       eid.setHTR(htr.readoutVMECrateId(),
00473                                  htr.htrSlot(),htr.htrTopBottom());
00474                       
00475                       DetId did=emap.lookup(eid);
00476                       if (!did.null()) 
00477                         {
00478                           
00479                           if ((HcalSubdetector)did.subdetId()==subdet)
00480                             {
00481                               if (subdet==HcalBarrel)
00482                                 {
00483                                   HBpresent_=1;
00484                                   meHB_->Fill(HBpresent_);
00485                                   return;
00486                                 }
00487                               else if (subdet==HcalEndcap)
00488                               {
00489                                 HEpresent_=1;
00490                                 meHE_->Fill(HEpresent_);
00491                                 return;
00492                               }
00493                             } // if ((HcalSubdetector)did.subdetId()==subdet)
00494                         } // if (!did.null())
00495                     } // for (int fib=1;fib<9;...)
00496                 } // for (int fchan=0; fchan<3;...)
00497             } // for (int spigot=0;spigot<HcalDCCHeader::SPIGOT_COUNT; spigot++) 
00498         } //else if (dcc<718 && (subdet...))
00499   } // loop over fedUnpackList
00500   
00501 
00502 } // void HcalMonitorModule::CheckSubdetectorStatus(...)
00503 
00504 DEFINE_FWK_MODULE(HcalMonitorModule);