CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQM/HcalMonitorModule/src/ZDCMonitorModule.cc

Go to the documentation of this file.
00001 #include "DQM/HcalMonitorModule/interface/ZDCMonitorModule.h"
00002 #include "DQM/HcalMonitorTasks/interface/HcalZDCMonitor.h"
00003 
00004 #include "DQMServices/Core/interface/DQMStore.h"
00005 #include "DQMServices/Core/interface/MonitorElement.h"
00006 
00007 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00008 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00009 #include "FWCore/Framework/interface/LuminosityBlock.h"
00010 #include "FWCore/Utilities/interface/CPUTimer.h"
00011 
00012 #include "DataFormats/Provenance/interface/EventID.h"  
00013 #include "DataFormats/HcalDigi/interface/HcalUnpackerReport.h"
00014 #include "DataFormats/HcalDigi/interface/HcalCalibrationEventTypes.h"
00015 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetup.h"
00016 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
00017 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetupFwd.h"
00018 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00019 #include "DataFormats/DetId/interface/DetId.h"
00020 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00021 #include "DQM/HcalMonitorTasks/interface/HcalZDCMonitor.h"
00022 
00023 #include "CondFormats/HcalObjects/interface/HcalChannelStatus.h"
00024 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
00025 #include "CondFormats/HcalObjects/interface/HcalCondObjectContainer.h"
00026 
00027 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00028 
00029 #include <memory>
00030 #include <iostream>
00031 #include <fstream>
00032 #include <vector>
00033 #include <string>
00034 #include <sys/time.h>
00035 
00036 //--------------------------------------------------------
00037 ZDCMonitorModule::ZDCMonitorModule(const edm::ParameterSet& ps){
00038 
00039   irun_=0; ilumisec=0; ievent_=0; itime_=0;
00040 
00041   meStatus_=0;  
00042   meFEDS_=0;
00043   meLatency_=0; meQuality_=0;
00044   fedsListed_ = false;
00045   zdcMon_ = 0;
00046   
00047   // Assumed ZDC is out of the run by default
00048   ZDCpresent_=0;
00049 
00050   inputLabelDigi_        = ps.getParameter<edm::InputTag>("digiLabel");
00051   inputLabelRecHitZDC_   = ps.getParameter<edm::InputTag>("zdcRecHitLabel");
00052   showTiming_ = ps.getUntrackedParameter<bool>("showTiming", false);         //-- show CPU time 
00053   dump2database_   = ps.getUntrackedParameter<bool>("dump2database",false);  //-- dumps output to database file
00054   // Check Online running
00055   Online_                = ps.getUntrackedParameter<bool>("Online",false);
00056   checkZDC_=ps.getUntrackedParameter<bool>("checkZDC", true); 
00057   dbe_ = edm::Service<DQMStore>().operator->();
00058   debug_ = ps.getUntrackedParameter<int>("debug", 0);
00059   //FEDRawDataCollection_ = ps.getUntrackedParameter<edm::InputTag>("FEDRawDataCollection",edm::InputTag("source",""));
00060 
00061   if (checkZDC_)
00062     {
00063       zdcMon_ = new HcalZDCMonitor();
00064       zdcMon_->setup(ps, dbe_);
00065     }
00066 
00067   // set parameters   
00068   prescaleEvt_ = ps.getUntrackedParameter<int>("diagnosticPrescaleEvt", -1);
00069   if(debug_>1) std::cout << "===>ZDCMonitor event prescale = " << prescaleEvt_ << " event(s)"<< std::endl;
00070 
00071   prescaleLS_ = ps.getUntrackedParameter<int>("diagnosticPrescaleLS", -1);
00072   if(debug_>1) std::cout << "===>ZDCMonitor lumi section prescale = " << prescaleLS_ << " lumi section(s)"<< std::endl;
00073   
00074   // Base folder for the contents of this job
00075   std::string subsystemname = ps.getUntrackedParameter<std::string>("subSystemFolder", "ZDC") ;
00076   if(debug_>0) std::cout << "===>ZDCMonitor name = " << subsystemname << std::endl;
00077   rootFolder_ = subsystemname + "/";
00078   
00079   gettimeofday(&psTime_.updateTV,NULL);
00081   psTime_.updateTime = (psTime_.updateTV.tv_sec*1000.0+psTime_.updateTV.tv_usec/1000.0);
00082   psTime_.updateTime /= 1000.0;
00083   psTime_.elapsedTime=0;
00084   psTime_.vetoTime=psTime_.updateTime;
00085 }
00086 
00087 //--------------------------------------------------------
00088 ZDCMonitorModule::~ZDCMonitorModule()
00089 {
00090   if (!checkZDC_) return;
00091   if (dbe_!=0)
00092     {    
00093       if (zdcMon_!=0)   zdcMon_->clearME();
00094       dbe_->setCurrentFolder(rootFolder_);
00095       dbe_->removeContents();
00096     }
00097 
00098 if (zdcMon_!=0)
00099     {
00100       delete zdcMon_; zdcMon_=0;
00101     }
00102 }
00103 
00104 //--------------------------------------------------------
00105 // beginJob no longer needed; trying setup within beginJob won't work !! -- IOV's not loaded
00106 void ZDCMonitorModule::beginJob()
00107 {
00108   if (!checkZDC_) return;
00109   // should we reset these counters at the start of each run?
00110   ievt_ = 0;
00111   ievt_pre_=0;
00112 
00113   // Counters for rawdata, digi, and rechit
00114   ievt_rawdata_=0;
00115   ievt_digi_=0;
00116   ievt_rechit_=0;
00117   return;
00118 }
00119 
00120 //--------------------------------------------------------
00121 void ZDCMonitorModule::beginRun(const edm::Run& run, const edm::EventSetup& c) 
00122 {
00123   if (!checkZDC_) return;
00124   fedsListed_ = false;
00125   ZDCpresent_ = 0;
00126 
00127   reset();
00128 
00129   if ( dbe_ != NULL ){
00130     dbe_->setCurrentFolder(rootFolder_+"DQM Job Status" );
00131 
00132     meIEVTALL_ = dbe_->bookInt("Events Processed");
00133     meIEVTRAW_ = dbe_->bookInt("Events with Raw Data");
00134     meIEVTDIGI_= dbe_->bookInt("Events with Digis");
00135     meIEVTRECHIT_ = dbe_->bookInt("Events with RecHits");
00136     meIEVTALL_->Fill(ievt_);
00137     meIEVTRAW_->Fill(ievt_rawdata_);
00138     meIEVTDIGI_->Fill(ievt_digi_);
00139     meIEVTRECHIT_->Fill(ievt_rechit_);
00140     meStatus_  = dbe_->bookInt("STATUS");
00141    
00142     meFEDS_    = dbe_->book1D("FEDs Unpacked","FEDs Unpacked",1+(FEDNumbering::MAXHCALFEDID-FEDNumbering::MINHCALFEDID),FEDNumbering::MINHCALFEDID-0.5,FEDNumbering::MAXHCALFEDID+0.5);
00143     // process latency was (200,0,1), but that gave overflows
00144     meLatency_ = dbe_->book1D("Process Latency","Process Latency",200,0,10);
00145     meQuality_ = dbe_->book1D("Quality Status","Quality Status",100,0,1);
00146     // Store whether or not subdetectors are present
00147     meZDC_ = dbe_->bookInt("ZDCpresent");
00148 
00149     meStatus_->Fill(0);
00150     // Should fill with 0 to start
00151     meZDC_->Fill(ZDCpresent_);
00152 
00153   }
00154   // Create histograms for individual Tasks
00155   if (zdcMon_)    zdcMon_->beginRun();
00156 
00157   edm::ESHandle<HcalDbService> pSetup;
00158   c.get<HcalDbRecord>().get( pSetup );
00159 
00160   // Not checking ZDC raw data?  In that case, no readoutMap, hcaldetid_, etc. info needed
00161 
00162   
00163   //get conditions
00164   c.get<HcalDbRecord>().get(conditions_);
00165 
00166   // get channel quality -- not yet used for ZDC
00167   /*
00168   edm::ESHandle<HcalChannelQuality> p;
00169   c.get<HcalChannelQualityRcd>().get(p);
00170   chanquality_= new HcalChannelQuality(*p.product());
00171   */
00172   return;
00173 }
00174 
00175 //--------------------------------------------------------
00176 void ZDCMonitorModule::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00177      const edm::EventSetup& context) 
00178 {
00179   /* Don't start a new luminosity block if it is less than the current value
00180      when running online.  This avoids the problem of getting events
00181      from mis-ordered lumi blocks, which screws up our lumi block 
00182      monitoring.
00183   */
00184   if (!checkZDC_) return;
00185   if (Online_ && lumiSeg.luminosityBlock()<ilumisec)
00186     return;
00187 
00188   // Otherwise, run normal startups
00189   ilumisec = lumiSeg.luminosityBlock();
00190   if (zdcMon_!=0)   {  zdcMon_->beginLuminosityBlock(ilumisec);}
00191 }
00192 
00193 
00194 //--------------------------------------------------------
00195 void ZDCMonitorModule::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00196                                            const edm::EventSetup& context) 
00197 {
00198   if (!checkZDC_) return;
00199   // In online running, don't process events that occur before current luminosity block
00200   if (Online_ && lumiSeg.luminosityBlock()<ilumisec)
00201     return; 
00202 
00203   // Call these every luminosity block
00204   if (zdcMon_!=0)   {  zdcMon_->endLuminosityBlock();}
00205   // Call these only if prescale set
00206   if (prescaleLS_>-1 && !prescale())
00207     {
00208     }
00209   return;
00210 }
00211 
00212 //--------------------------------------------------------
00213 void ZDCMonitorModule::endRun(const edm::Run& r, const edm::EventSetup& context)
00214 {
00215   if (!checkZDC_) return;
00216   if (debug_>0)  
00217     std::cout <<"ZDCMonitorModule::endRun(...) ievt = "<<ievt_<<std::endl;
00218 
00219   // These should be unnecessary; call them just in case, so that
00220   // we're sure we get at least one fill per run
00221   if (zdcMon_!=0)   {  zdcMon_->endLuminosityBlock();}
00222 
00223   return;
00224 }
00225 
00226 
00227 //--------------------------------------------------------
00228 void ZDCMonitorModule::endJob(void) 
00229 {
00230   if (!checkZDC_) return;
00231   if ( dbe_ != NULL ){
00232     meStatus_  = dbe_->get(rootFolder_+"DQM Job Status/STATUS");
00233   }
00234   
00235   if ( meStatus_ ) meStatus_->Fill(2);
00236 
00237   return; // All of the rest of the endjob stuff (filling db, etc.) should be done in the client, right?
00238 
00239   if (zdcMon_!=NULL)       zdcMon_->done();
00240 
00241   return;
00242 }
00243 
00244 //--------------------------------------------------------
00245 void ZDCMonitorModule::reset(){
00246   if (!checkZDC_) return;
00247   if (zdcMon_!=NULL) zdcMon_->reset();
00248 
00249 }
00250 
00251 //--------------------------------------------------------
00252 void ZDCMonitorModule::analyze(const edm::Event& e, const edm::EventSetup& eventSetup)
00253 {
00254   if (!checkZDC_) return;
00255   // environment datamembers
00256   irun_     = e.id().run();
00257   ievent_   = e.id().event();
00258   itime_    = e.time().value();
00259   
00260   if (Online_ && e.luminosityBlock()<ilumisec)
00261     return;
00262 
00263   if (debug_>1) std::cout << "ZDCMonitorModule: evts: "<< nevt_ << ", run: " << irun_ << ", LS: " << e.luminosityBlock() << ", evt: " << ievent_ << ", time: " << itime_ << std::endl <<"\t counter = "<<ievt_pre_<<"\t total count = "<<ievt_<<std::endl; 
00264 
00265   if ( meStatus_ ) meStatus_->Fill(1);
00266   meLatency_->Fill(psTime_.elapsedTime);
00267 
00268   
00270   bool rawOK_    = true;
00271   bool digiOK_   = true;
00272   bool zdchitOK_ = true;
00273 
00274   edm::Handle<HcalUnpackerReport> report; 
00275   e.getByLabel(inputLabelDigi_,report);
00276   if (!report.isValid())
00277     {
00278       rawOK_=false;
00279       edm::LogWarning("ZDCMonitorModule")<<" Unpacker Report Digi Collection "<<inputLabelDigi_<<" not available";
00280     }
00281   if (rawOK_)
00282     {
00283       if(!fedsListed_){
00284         const std::vector<int> feds =  (*report).getFedsUnpacked();    
00285         for(unsigned int f=0; f<feds.size(); f++){
00286           meFEDS_->Fill(feds[f]);    
00287         }
00288         fedsListed_ = true;
00289         fedss = feds; //Assign to a non-const holder
00290       }
00291     }
00292   if (rawOK_==true) ++ievt_rawdata_;
00293 
00294   // copy of Bryan Dahmes' calibration filter
00295   /*
00296     // need to get raw data first before running filter!
00297   int calibType=-1;
00298   int dccBCN=-1;
00299 
00300   if (rawOK_==true)
00301     {
00302       // checking FEDs for calibration information
00303       int numEmptyFEDs = 0 ;
00304       std::vector<int> calibTypeCounter(8,0) ;
00305       for( int i = FEDNumbering::MINHCALFEDID; i <= FEDNumbering::MAXHCALFEDID; i++) {
00306         const FEDRawData& fedData = rawraw->FEDData(i) ;
00307         
00308         if ( fedData.size() < 24 ) numEmptyFEDs++ ;
00309         if ( fedData.size() < 24 ) continue;
00310         int value = ((const HcalDCCHeader*)(fedData.data()))->getCalibType() ;
00311         calibTypeCounter.at(value)++ ; // increment the counter for this calib type
00312         // Temporary for Pawel -- get BCN #101
00313         const HcalDCCHeader* dccHeader=(const HcalDCCHeader*)(fedData.data());
00314         dccBCN = dccHeader->getBunchId();
00315       }
00316     int maxCount = 0;
00317     int numberOfFEDIds = FEDNumbering::MAXHCALFEDID  - FEDNumbering::MINHCALFEDID + 1 ;
00318     for (unsigned int i=0; i<calibTypeCounter.size(); i++) {
00319       if ( calibTypeCounter.at(i) > maxCount )
00320         { calibType = i ; maxCount = calibTypeCounter.at(i) ; }
00321       if ( maxCount == numberOfFEDIds ) break ;
00322     }
00323     
00324     if ( maxCount != (numberOfFEDIds-numEmptyFEDs) )
00325       edm::LogWarning("HcalCalibTypeFilter") << "Conflicting calibration types found.  Assigning type "
00326                                              << calibType ;
00327     LogDebug("HcalCalibTypeFilter") << "Calibration type is: " << calibType ;
00328     } // if (rawOK_==true) // calibration loop
00329   */
00330 
00331   // skip this event if we're prescaling...
00332   ++ievt_;
00333   if(prescaleEvt_>0 && prescale()) return;
00334   
00336   // try to get digis
00337   edm::Handle<ZDCDigiCollection> zdc_digi;
00338   e.getByLabel(inputLabelDigi_,zdc_digi);
00339   if (!zdc_digi.isValid())
00340     {
00341       digiOK_=false;
00342       if (debug_>1) std::cout <<"<ZDCMonitorModule> COULDN'T GET ZDC DIGI"<<std::endl;
00343       //edm::LogWarning("ZDCMonitorModule")<< inputLabelDigi_<<" zdc_digi not available";
00344     }
00345   if (digiOK_) ++ievt_digi_;
00346 
00348 
00349   // try to get rechits
00350   edm::Handle<ZDCRecHitCollection> zdc_hits;
00351   e.getByLabel(inputLabelRecHitZDC_,zdc_hits);
00352   if (!zdc_hits.isValid())
00353     {
00354       zdchitOK_=false;
00355       // ZDC Warnings should be suppressed unless debugging is on (since we don't yet normally run zdcreco)
00356       if (debug_>0) 
00357         edm::LogWarning("ZDCMonitorModule")<< inputLabelRecHitZDC_<<" not available"; 
00358     }
00359   if (zdchitOK_) ++ievt_rechit_;
00360 
00361 
00363 
00364   // Run the configured tasks, protect against missing products
00365   meIEVTALL_->Fill(ievt_);
00366   meIEVTRAW_->Fill(ievt_rawdata_);
00367   meIEVTDIGI_->Fill(ievt_digi_);
00368   meIEVTRECHIT_->Fill(ievt_rechit_);
00369 
00370   if (ZDCpresent_==0 && (digiOK_ || zdchitOK_))
00371     {
00372       ZDCpresent_=1;
00373       meZDC_->Fill(ZDCpresent_);
00374     }
00375 
00376   // Data Format monitor task
00377   if (showTiming_)
00378     {
00379       cpu_timer.reset(); cpu_timer.start();
00380     }
00381 
00382   if (zdcMon_!=NULL && zdchitOK_ && digiOK_) 
00383     zdcMon_->processEvent(*zdc_digi,*zdc_hits);
00384  
00385   if (showTiming_)
00386     {
00387       cpu_timer.stop();
00388       if (zdcMon_ !=NULL) std::cout <<"TIMER:: ZDC MONITOR ->"<<cpu_timer.cpuTime()<<std::endl;
00389       cpu_timer.reset(); cpu_timer.start();
00390     }
00391 
00392  // Empty Event/Unsuppressed monitor plots
00393 
00394   if(debug_>0 && ievt_%1000 == 0)
00395     std::cout << "ZDCMonitorModule: processed " << ievt_ << " events" << std::endl;
00396 
00397   if(debug_>1)
00398     {
00399       std::cout << "ZDCMonitorModule: processed " << ievt_ << " events" << std::endl;
00400       std::cout << "    ZDC RAW Data   ==> " << rawOK_<< std::endl;
00401       std::cout << "    ZDC Digis      ==> " << digiOK_<< std::endl;
00402       std::cout << "    ZDC RecHits    ==> " << zdchitOK_<< std::endl;
00403     }
00404   
00405   return;
00406 }
00407 
00408 //--------------------------------------------------------
00409 bool ZDCMonitorModule::prescale()
00410 {
00411   if (!checkZDC_) return true;
00412 
00415   if (debug_>1) std::cout <<"ZDCMonitorModule::prescale:  ievt = "<<ievt_<<std::endl;
00416   // If no prescales are set, return 'false'.  (This means that we should process the event.)
00417   if(prescaleEvt_<=0 && prescaleLS_<=0) return false;
00418 
00419   // Now check whether event should be kept.  Assume that it should not by default
00420   bool keepEvent=false;
00421   
00422   // Keep event if prescaleLS test is met or if prescaleEvt test is met
00423   if(prescaleLS_>0 && (ilumisec%prescaleLS_)==0) keepEvent = true; // check on ls prescale; 
00424   if (prescaleEvt_>0 && (ievt_%prescaleEvt_)==0) keepEvent = true; // 
00425   
00426   // if any criteria wants to keep the event, do so
00427   if (keepEvent) return false;  // event should be kept; don't apply prescale
00428   return true; // apply prescale by default
00429 
00430 } // ZDCMonitorModule::prescale(...)
00431 
00432 
00433 // -------------------------------------------------
00434 
00435 DEFINE_FWK_MODULE(ZDCMonitorModule);