CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQM/HcalMonitorModule/src/ZDCMonitorModule.cc

Go to the documentation of this file.
00001 #include <DQM/HcalMonitorModule/interface/ZDCMonitorModule.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 ZDCMonitorModule.cc
00043  *
00044  * $Date: 2013/01/05 19:47:01 $
00045  * $Revision: 1.7 $
00046  * \author J Temple
00047  *
00048  *  JAIME!  UPDATE THIS INFO FOR ZDC!
00049  * New version of ZDCMonitorModule stores only a few necessary variables that other tasks need to grab
00050  * (Online_ boolean, subsystem folder name, subdetector presence check, etc.)
00051  * Modeled heavily on EcalBarrelMonitorModule code.
00052 
00053  * 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.
00054 
00055 */
00056 
00057 
00058 //Constructor
00059 
00060 ZDCMonitorModule::ZDCMonitorModule(const edm::ParameterSet& ps)
00061 {  // Set initial values
00062   init_=false; // first event sets up Monitor Elements and sets init_ to true
00063 
00064   // get ps objects
00065   Online_                = ps.getUntrackedParameter<bool>("online",false);
00066   mergeRuns_             = ps.getUntrackedParameter<bool>("mergeRuns",false);
00067   enableCleanup_         = ps.getUntrackedParameter<bool>("enableCleanup",false);
00068   debug_                 = ps.getUntrackedParameter<int>("debug",0);
00069   
00070   FEDRawDataCollection_  = ps.getUntrackedParameter<edm::InputTag>("FEDRawDataCollection");
00071   inputLabelReport_      = ps.getUntrackedParameter<edm::InputTag>("UnpackerReport");
00072   
00073   prefixME_              = ps.getUntrackedParameter<std::string>("subSystemFolder","Hcal/");
00074   if (prefixME_.substr(prefixME_.size()-1,prefixME_.size())!="/")
00075     prefixME_.append("/");
00076   
00077   NLumiBlocks_           = ps.getUntrackedParameter<int>("NLumiBlocks",4000);
00078 
00079 } // ZDCMonitorModule::ZDCMonitorModule
00080 
00081 
00082 //Destructor
00083 ZDCMonitorModule::~ZDCMonitorModule()
00084 {
00085 
00086 } //ZDCMonitorModule::~ZDCMonitorModule()
00087 
00088 
00089 
00090 void ZDCMonitorModule::beginJob(void)
00091 {
00092   if (debug_>0) std::cout <<"ZDCMonitorModule::beginJob()"<<std::endl;
00093   // Get DQM service
00094   dbe_ = edm::Service<DQMStore>().operator->();
00095   // set default values
00096   ievt_=0;
00097   fedsListed_=false;
00098   ZDCpresent_=0;
00099   // Set pointers to null
00100   meCalibType_=0;
00101   meFEDS_=0;
00102   meIevt_=0;
00103   meIevtHist_=0;
00104   meEvtsVsLS_=0;
00105   meProcessedEndLumi_=0;
00106   meZDC_=0;
00107 
00108 }
00109 
00110 void ZDCMonitorModule::beginRun(const edm::Run& r, const edm::EventSetup& c) 
00111 {
00112   if ( debug_>0 ) std::cout << "ZDCMonitorModule: beginRun" << std::endl;
00113   // reset histograms & counters on a new run, unless merging allowed
00114 
00115   if (eMap_==0) //eMap_ not created yet
00116     {
00117       if (debug_>1) std::cout <<"\t<ZDCMonitorModule::beginRun> Getting Emap!"<<std::endl;
00118       edm::ESHandle<HcalDbService> pSetup;
00119       c.get<HcalDbRecord>().get( pSetup );
00120       eMap_=pSetup->getHcalMapping(); 
00121     }
00122   if (mergeRuns_) return;
00123   this->setup();
00124   this->reset();
00125 
00126 } //ZDCMonitorModule::beginRun(....)
00127 
00128 
00129 void ZDCMonitorModule::endRun(const edm::Run& r, const edm::EventSetup& c) {
00130 
00131   if ( debug_>0 ) std::cout << "ZDCMonitorModule: endRun" << std::endl;
00132 
00133   // end-of-run
00134   if ( meStatus_ ) meStatus_->Fill(2);
00135 
00136   if ( meRun_ ) meRun_->Fill(runNumber_);
00137   if ( meEvt_ ) meEvt_->Fill(evtNumber_);
00138 }
00139 
00140 void ZDCMonitorModule::reset(void)
00141 {
00142   if (debug_>0) std::cout <<"ZDCMonitorModule::reset"<<std::endl;
00143   // Call Reset() on all MonitorElement histograms
00144   if (meCalibType_) meCalibType_->Reset();
00145   if (meFEDS_) meFEDS_->Reset();
00146   if (meIevt_) meIevt_->Fill(0);
00147   if (meIevtHist_) meIevtHist_->Reset();
00148   if (meEvtsVsLS_) meEvtsVsLS_->Reset();
00149   ievt_=0;
00150   if (meProcessedEndLumi_) meProcessedEndLumi_->Fill(-1);
00151   if (meZDC_) meZDC_->Fill(-1);
00152   ZDCpresent_=0;
00153   
00154   fedsListed_=false;
00155 } // void ZDCMonitorModule::reset(void)
00156 
00157 void ZDCMonitorModule::setup(void)
00158 {
00159   // Run this on first event in run; set up all necessary monitor elements
00160   if (debug_>0) std::cout <<"ZDCMonitorModule::setup"<<std::endl;
00161   init_=true;
00162   if (dbe_)
00163     {
00164       dbe_->setCurrentFolder(prefixME_+"ZDCInfo");
00165       meStatus_ = dbe_->bookInt("STATUS");
00166       if (meStatus_) meStatus_->Fill(-1);
00167       meRun_ = dbe_->bookInt("RUN");
00168       if (meRun_) meRun_->Fill(-1);
00169       meEvt_ = dbe_->bookInt("EVT");
00170       if (meEvt_) meEvt_->Fill(-1);
00171       meIevt_ = dbe_->bookInt("EventsProcessed");
00172       if (meIevt_) meIevt_->Fill(-1);
00173       meIevtHist_ = dbe_->book1D("EventsInZDCMonitorModule","Events Seen by ZDCMonitorModule",1,0.5,1.5);
00174       meIevtHist_->setBinLabel(1,"Nevents",1);
00175       meEvtsVsLS_ = dbe_->book1D("EventsVsLS","Events vs. Luminosity Section;LS;# events",NLumiBlocks_,0.5,NLumiBlocks_+0.5);
00176       meOnline_ = dbe_->bookInt("Online");
00177       meOnline_->Fill((int)Online_);
00178       meProcessedEndLumi_ = dbe_->bookInt("EndLumiBlock_MonitorModule");
00179       if (meProcessedEndLumi_) meProcessedEndLumi_->Fill(-1);
00180       meCurrentCalibType_= dbe_->bookInt("CURRENT_EVENT_TYPE");
00181       if (meCurrentCalibType_) meCurrentCalibType_->Fill(-1);
00182       
00183       meZDC_ = dbe_->bookInt("ZDCpresent");
00184       if (meZDC_) meZDC_->Fill(-1);
00185 
00186       meFEDS_    = dbe_->book1D("FEDs Unpacked","FEDs Unpacked; Hcal FEDs 700-731",1+(FEDNumbering::MAXHCALFEDID-FEDNumbering::MINHCALFEDID),FEDNumbering::MINHCALFEDID-0.5,FEDNumbering::MAXHCALFEDID+0.5);
00187 
00188       meCalibType_ = dbe_->book1D("CalibrationType","Calibration Type",9,-0.5,8.5);
00189       meCalibType_->setBinLabel(1,"Normal",1);
00190       meCalibType_->setBinLabel(2,"Ped",1);
00191       meCalibType_->setBinLabel(3,"RADDAM",1);
00192       meCalibType_->setBinLabel(4,"HBHEHPD",1);
00193       meCalibType_->setBinLabel(5,"HOHPD",1);
00194       meCalibType_->setBinLabel(6,"HFPMT",1);
00195       meCalibType_->setBinLabel(7,"ZDC",1); // Is this used for ZDC calibraitons??
00196       meCalibType_->setBinLabel(8,"CASTOR",1);
00197 
00198     } // if (dbe_)
00199   return;
00200 } // void ZDCMonitorModule::setup(void)
00201 
00202 
00203 void ZDCMonitorModule::cleanup(void)
00204 {
00205   if (debug_>0) std::cout <<"ZDCMonitorModule::cleanup"<<std::endl;
00206   if (!enableCleanup_) return;
00207   if (dbe_)
00208     {
00209       dbe_->setCurrentFolder(prefixME_+"ZDCInfo");
00210       if ( meStatus_ ) 
00211         dbe_->removeElement( meStatus_->getName() );
00212       meStatus_ = 0;
00213       if ( meRun_ ) 
00214         dbe_->removeElement( meRun_->getName() );
00215       meRun_ = 0;
00216       if ( meEvt_ ) 
00217         dbe_->removeElement( meEvt_->getName() );
00218       meEvt_ = 0;
00219       if (meIevt_) 
00220         dbe_->removeElement(meIevt_->getName());
00221       meIevt_=0;
00222       if (meIevtHist_)
00223         dbe_->removeElement(meIevtHist_->getName());
00224       meIevtHist_=0;
00225       if (meFEDS_) 
00226         dbe_->removeElement(meFEDS_->getName());
00227       meFEDS_ = 0;
00228       if (meCalibType_) 
00229         dbe_->removeElement(meCalibType_->getName());
00230       meCalibType_ = 0;
00231       if (meCurrentCalibType_) 
00232         dbe_->removeElement(meCurrentCalibType_->getName());
00233       meCurrentCalibType_=0;
00234       if (meProcessedEndLumi_) 
00235         dbe_->removeElement(meProcessedEndLumi_->getName());
00236       meProcessedEndLumi_ = 0;
00237       if (meZDC_) 
00238         dbe_->removeElement(meZDC_->getName());
00239        meZDC_=0;  
00240      } // if (dbe_)
00241 
00242   fedsListed_=false;
00243   ZDCpresent_=0;
00244   init_=false;
00245   
00246 } // void ZDCMonitorModule::cleanup(void)
00247 
00248 
00249 
00250 void ZDCMonitorModule::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00251                                                 const edm::EventSetup& c) 
00252 {
00253   if (debug_>0) std::cout <<"ZDCMonitorModule::beginLuminosityBlock"<<std::endl;
00254 }// void ZDCMonitorModule::beginLuminosityBlock(...)
00255 
00256 
00257 
00258 
00259 void ZDCMonitorModule::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00260                                               const edm::EventSetup& c) 
00261 {
00262   if (debug_>0) std::cout <<"ZDCMonitorModule::endLuminosityBlock"<<std::endl;
00263   meProcessedEndLumi_->Fill(lumiSeg.luminosityBlock());
00264 }// void ZDCMonitorModule::endLuminosityBlock(...)
00265 
00266 
00267 
00268 void ZDCMonitorModule::endJob(void)
00269 {
00270   if (debug_>0) std::cout <<"ZDCMonitorModule::endJob()"<<std::endl;
00271   if (dbe_)
00272     {
00273       meStatus_ = dbe_->get(prefixME_ + "/EventInfo/STATUS");
00274       meRun_ = dbe_->get(prefixME_ + "/EventInfo/RUN");
00275       meEvt_ = dbe_->get(prefixME_ + "/EventInfo/EVT");
00276     }
00277   if (meStatus_) meStatus_->Fill(2);
00278   if (meRun_) meRun_->Fill(runNumber_);
00279   if (meEvt_) meEvt_->Fill(evtNumber_);
00280   if (init_) this->cleanup();
00281 } // void ZDCMonitorModule::endJob(void)
00282 
00283 
00284 
00285 void ZDCMonitorModule::analyze(const edm::Event& e, const edm::EventSetup& c)
00286 {
00287   if (!init_) this->setup();
00288   
00289   LogDebug("ZDCMonitorModule")<<"processing event "<<ievt_;
00290   
00291   // Fill Monitor Elements with run, evt, processed event info
00292   ++ievt_;
00293   runNumber_=e.id().run();
00294   evtNumber_=e.id().event();
00295   if (meRun_) meRun_->Fill(runNumber_);
00296   if (meEvt_) meEvt_->Fill(evtNumber_);
00297   if (meIevt_) meIevt_->Fill(ievt_);
00298   if (meIevtHist_) meIevtHist_->Fill(1);
00299   if (meEvtsVsLS_) meEvtsVsLS_->Fill(e.luminosityBlock(),1);
00300   if (ievt_==1)
00301     {
00302       LogDebug("ZDCMonitorModule") << "processing run " << runNumber_;
00303       // begin-of-run
00304       if ( meStatus_ ) meStatus_->Fill(0);
00305     } 
00306   else 
00307     {
00308       // running
00309       if ( meStatus_ ) meStatus_->Fill(1);
00310     }
00311   
00312   // Try to get raw data
00313   edm::Handle<FEDRawDataCollection> rawraw;  
00314   if (!(e.getByLabel(FEDRawDataCollection_,rawraw)))
00315     {
00316       edm::LogWarning("ZDCMonitorModule")<<" raw data with label "<<FEDRawDataCollection_ <<" not available";
00317       return;
00318     }
00319   
00320   // Get Event Calibration Type -- copy of Bryan Dahmes' filter
00321   int calibType=-1;
00322   int numEmptyFEDs = 0 ;
00323   std::vector<int> calibTypeCounter(8,0) ;
00324   for( int i = FEDNumbering::MINHCALFEDID; i <= FEDNumbering::MAXHCALFEDID; i++) 
00325     {
00326       const FEDRawData& fedData = rawraw->FEDData(i) ;
00327       
00328       if ( fedData.size() < 24 ) numEmptyFEDs++ ;
00329       if ( fedData.size() < 24 ) continue;
00330 
00331       int value = (int)((const HcalDCCHeader*)(fedData.data()))->getCalibType() ;
00332       if(value>7) 
00333         {
00334           edm::LogWarning("ZDCMonitorModule::CalibTypeFilter") << "Unexpected Calibration type: "<< value << " in FED: "<<i<<" (should be 0-7). I am bailing out...";
00335           return;
00336         }
00337 
00338       calibTypeCounter.at(value)++ ; // increment the counter for this calib type
00339     } // for (int i = FEDNumbering::MINHCALFEDID; ...)
00340 
00341   int maxCount = 0;
00342   int numberOfFEDIds = FEDNumbering::MAXHCALFEDID  - FEDNumbering::MINHCALFEDID + 1 ;
00343   for (unsigned int i=0; i<calibTypeCounter.size(); i++) {
00344     if ( calibTypeCounter.at(i) > maxCount )
00345       { calibType = i ; maxCount = calibTypeCounter.at(i) ; }
00346     if ( maxCount == numberOfFEDIds ) break ;
00347   }
00348   
00349   if ( maxCount != (numberOfFEDIds-numEmptyFEDs) )
00350     edm::LogWarning("ZDCMonitorModule::CalibTypeFilter") << "Conflicting calibration types found.  Assigning type "
00351                                            << calibType ;
00352   LogDebug("ZDCMonitorModule::CalibTypeFilter") << "Calibration type is: " << calibType ;
00353   // Fill histogram of calibration types, as well as integer to keep track of current value
00354   if (meCalibType_) meCalibType_->Fill(calibType);
00355   if (meCurrentCalibType_) meCurrentCalibType_->Fill(calibType);
00357 
00358   if (debug_>2) std::cout <<"\t<ZDCMonitorModule>  ievt = "<<ievt_<<"  calibration type = "<<calibType<<std::endl;
00359 
00360   // Check to see which subdetectors are present.
00361   // May only need to do this on first event?   Subdets don't appear during a run?
00362   if (ZDCpresent_==0)
00363     CheckSubdetectorStatus(rawraw, *eMap_);
00364 
00365   
00366   //  Here, we do need this information each event
00367   edm::Handle<HcalUnpackerReport> report;  
00368   if (!(e.getByLabel(inputLabelReport_,report)))
00369     {
00370       edm::LogWarning("ZDCMonitorModule")<<" Unpacker Report "<<inputLabelReport_<<" not available";
00371       return;
00372     }
00373 
00374   if (!fedsListed_)
00375     {
00376       const std::vector<int> feds =  (*report).getFedsUnpacked();    
00377       for(unsigned int f=0; f<feds.size(); ++f)
00378         meFEDS_->Fill(feds[f]);    
00379       fedsListed_ = true;
00380     } // if (!fedsListed_)
00381 
00382 } // void ZDCMonitorModule::analyze(...)
00383 
00384 
00385 
00386 void ZDCMonitorModule::CheckSubdetectorStatus(const edm::Handle<FEDRawDataCollection>& rawraw,
00387                                               const HcalElectronicsMap& emap)
00388 {
00389 
00390   std::vector<int> fedUnpackList;
00391   for (int i=FEDNumbering::MINHCALFEDID; 
00392        i<=FEDNumbering::MAXHCALFEDID; 
00393        i++) 
00394     fedUnpackList.push_back(i);
00395 
00396   if (debug_>1) std::cout <<"<ZDCMonitorModule::CheckSubdetectorStatus>  Checking ZDC "<<std::endl;
00397   for (std::vector<int>::const_iterator i=fedUnpackList.begin();
00398        i!=fedUnpackList.end(); 
00399        ++i) 
00400     {
00401       if (debug_>2) std::cout <<"\t<ZDCMonitorModule::CheckSubdetectorStatus>  FED = "<<*i<<std::endl;
00402       const FEDRawData& fed =(*rawraw).FEDData(*i);
00403       if (fed.size()<12) continue; // Was 16. How do such tiny events even get here?
00404       
00405       // get the DCC header
00406       const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(fed.data());
00407       if (!dccHeader) return;
00408       //int dccid=dccHeader->getSourceId();
00409       
00410       // This is how HcalForward data is identified:
00411       //if (subdet == HcalForward && dccid>717 && dccid<724)
00412       // check for ZDC at some point -- how?
00413       // For now, just assume ZDC info is present, with dummy condition
00414       if (1>0)
00415         {
00416           ZDCpresent_=1;
00417           meZDC_->Fill(ZDCpresent_);
00418           break;
00419         }
00420     } //loop over fedUnpack list 
00421 
00422 } // void ZDCMonitorModule::CheckSubdetectorStatus(...)
00423 
00424 DEFINE_FWK_MODULE(ZDCMonitorModule);