CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/DQM/SiPixelMonitorClient/src/SiPixelEDAClient.cc

Go to the documentation of this file.
00001 #include "DQM/SiPixelMonitorClient/interface/SiPixelEDAClient.h"
00002 
00003 // Framework
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/LuminosityBlock.h"
00007 #include "FWCore/ServiceRegistry/interface/Service.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 
00011 #include "DataFormats/DetId/interface/DetId.h"
00012 #include "DataFormats/GeometrySurface/interface/Surface.h"
00013 #include "DataFormats/SiPixelDetId/interface/PixelBarrelName.h"
00014 #include "DataFormats/SiPixelDetId/interface/PixelEndcapName.h"
00015 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00016 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00017 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00018 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00019 
00020 #include "CondFormats/SiPixelObjects/interface/DetectorIndex.h"
00021 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameConverter.h"
00022 
00023 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
00024 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00025 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00026 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
00027 #include "Geometry/CommonTopologies/interface/PixelTopology.h"
00028 
00029 #include "DQMServices/Core/interface/DQMStore.h"
00030 #include "DQMServices/Core/interface/MonitorElement.h"
00031 
00032 #include "DQM/SiPixelMonitorClient/interface/SiPixelWebInterface.h"
00033 #include "DQM/SiPixelMonitorClient/interface/SiPixelActionExecutor.h"
00034 #include "DQM/SiPixelMonitorClient/interface/SiPixelInformationExtractor.h"
00035 #include "DQM/SiPixelMonitorClient/interface/SiPixelDataQuality.h"
00036 #include "DQM/SiPixelMonitorClient/interface/SiPixelUtility.h"
00037 
00038 #include "xgi/Method.h"
00039 #include "xgi/Utils.h"
00040 
00041 #include "cgicc/Cgicc.h"
00042 #include "cgicc/FormEntry.h"
00043 #include "cgicc/HTMLClasses.h"
00044 
00045 #include <iostream>
00046 #include <iomanip>
00047 #include <stdio.h>
00048 #include <string>
00049 #include <sstream>
00050 #include <math.h>
00051 
00052 #define BUF_SIZE 256
00053 
00054 using namespace edm;
00055 using namespace std;
00056 //
00057 // -- Constructor
00058 //
00059 SiPixelEDAClient::SiPixelEDAClient(const edm::ParameterSet& ps) :
00060   ModuleWeb("SiPixelEDAClient"){
00061 // cout<<"Entering  SiPixelEDAClient::SiPixelEDAClient: "<<endl;
00062  
00063   edm::LogInfo("SiPixelEDAClient") <<  " Creating SiPixelEDAClient " << "\n" ;
00064   
00065   bei_ = Service<DQMStore>().operator->();
00066 
00067   summaryFrequency_      = ps.getUntrackedParameter<int>("SummaryCreationFrequency",20);
00068   tkMapFrequency_        = ps.getUntrackedParameter<int>("TkMapCreationFrequency",50); 
00069   staticUpdateFrequency_ = ps.getUntrackedParameter<int>("StaticUpdateFrequency",10);
00070   actionOnLumiSec_       = ps.getUntrackedParameter<bool>("ActionOnLumiSection",false); //client
00071   actionOnRunEnd_        = ps.getUntrackedParameter<bool>("ActionOnRunEnd",true); //client
00072   evtOffsetForInit_      = ps.getUntrackedParameter<int>("EventOffsetForInit",10); //client
00073   offlineXMLfile_        = ps.getUntrackedParameter<bool>("UseOfflineXMLFile",false); //client
00074   hiRes_                 = ps.getUntrackedParameter<bool>("HighResolutionOccupancy",false); //client
00075   noiseRate_             = ps.getUntrackedParameter<double>("NoiseRateCutValue",0.001); //client
00076   noiseRateDenominator_  = ps.getUntrackedParameter<int>("NEventsForNoiseCalculation",100000); //client
00077   Tier0Flag_             = ps.getUntrackedParameter<bool>("Tier0Flag",false); //client
00078   doHitEfficiency_       = ps.getUntrackedParameter<bool>("DoHitEfficiency",true); //client
00079   inputSource_           = ps.getUntrackedParameter<string>("inputSource",  "source");
00080   
00081   if(!Tier0Flag_){
00082     string localPath = string("DQM/SiPixelMonitorClient/test/loader.html");
00083     ifstream fin(edm::FileInPath(localPath).fullPath().c_str(), ios::in);
00084     char buf[BUF_SIZE];
00085   
00086     if (!fin) {
00087       cerr << "Input File: loader.html"<< " could not be opened!" << endl;
00088       return;
00089     }
00090 
00091     while (fin.getline(buf, BUF_SIZE, '\n')) { // pops off the newline character 
00092       html_out_ << buf ;
00093     }
00094     fin.close();
00095 
00096   }
00097   
00098   // instantiate web interface
00099   sipixelWebInterface_ = new SiPixelWebInterface(bei_,offlineXMLfile_,Tier0Flag_);
00100   //instantiate the three work horses of the client:
00101   sipixelInformationExtractor_ = new SiPixelInformationExtractor(offlineXMLfile_);
00102   sipixelActionExecutor_ = new SiPixelActionExecutor(offlineXMLfile_, Tier0Flag_);
00103   sipixelDataQuality_ = new SiPixelDataQuality(offlineXMLfile_);
00104   
00105 // cout<<"...leaving  SiPixelEDAClient::SiPixelEDAClient. "<<endl;
00106 }
00107 //
00108 // -- Destructor
00109 //
00110 SiPixelEDAClient::~SiPixelEDAClient(){
00111 //  cout<<"Entering SiPixelEDAClient::~SiPixelEDAClient: "<<endl;
00112   
00113   edm::LogInfo("SiPixelEDAClient") <<  " Deleting SiPixelEDAClient " << "\n" ;
00114   if (sipixelWebInterface_) {
00115      delete sipixelWebInterface_;
00116      sipixelWebInterface_ = 0;
00117   }
00118   if (sipixelInformationExtractor_) {
00119      delete sipixelInformationExtractor_;
00120      sipixelInformationExtractor_ = 0;
00121   }
00122   if (sipixelActionExecutor_) {
00123      delete sipixelActionExecutor_;
00124      sipixelActionExecutor_ = 0;
00125   }
00126   if (sipixelDataQuality_) {
00127      delete sipixelDataQuality_;
00128      sipixelDataQuality_ = 0;
00129   }
00130 
00131 //  cout<<"...leaving SiPixelEDAClient::~SiPixelEDAClient. "<<endl;
00132 }
00133 //
00134 // -- Begin Job
00135 //
00136 void SiPixelEDAClient::beginJob(){
00137   firstRun = true;
00138 }
00139 //
00140 // -- Begin Run
00141 //
00142 void SiPixelEDAClient::beginRun(Run const& run, edm::EventSetup const& eSetup) {
00143   edm::LogInfo ("SiPixelEDAClient") <<"[SiPixelEDAClient]: Begining of Run";
00144 //  cout<<"Entering SiPixelEDAClient::beginRun: "<<endl;
00145 
00146   if(firstRun){
00147   
00148   // Read the summary configuration file
00149   if (!sipixelWebInterface_->readConfiguration(tkMapFrequency_,summaryFrequency_)) {
00150      edm::LogInfo ("SiPixelEDAClient") <<"[SiPixelEDAClient]: Error to read configuration file!! Summary will not be produced!!!";
00151      summaryFrequency_ = -1;
00152      tkMapFrequency_ = -1;
00153      actionOnLumiSec_ = false;
00154      actionOnRunEnd_ = true;
00155      evtOffsetForInit_ = -1;
00156   }
00157   nLumiSecs_ = 0;
00158   nEvents_   = 0;
00159   if(Tier0Flag_) nFEDs_ = 40;
00160   else nFEDs_ = 0;
00161   
00162   bei_->setCurrentFolder("Pixel/");
00163   // Setting up QTests:
00164 //  sipixelActionExecutor_->setupQTests(bei_);
00165   // Creating Summary Histos:
00166   sipixelActionExecutor_->createSummary(bei_);
00167   // Booking Deviation Histos:
00168   if(!Tier0Flag_) sipixelActionExecutor_->bookDeviations(bei_);
00169   // Booking Efficiency Histos:
00170   if(doHitEfficiency_) sipixelActionExecutor_->bookEfficiency(bei_);
00171   // Creating occupancy plots:
00172   sipixelActionExecutor_->bookOccupancyPlots(bei_, hiRes_);
00173   // Booking noisy pixel ME's:
00174   if(noiseRate_>0.) sipixelInformationExtractor_->bookNoisyPixels(bei_, noiseRate_, Tier0Flag_);
00175   // Booking summary report ME's:
00176   sipixelDataQuality_->bookGlobalQualityFlag(bei_, Tier0Flag_, nFEDs_);
00177   // Booking Static Tracker Maps:
00178 //  sipixelActionExecutor_->bookTrackerMaps(bei_, "adc");
00179 //  sipixelActionExecutor_->bookTrackerMaps(bei_, "charge");
00180 //  sipixelActionExecutor_->bookTrackerMaps(bei_, "ndigis");
00181 //  sipixelActionExecutor_->bookTrackerMaps(bei_, "NErrors");
00182   
00183   firstRun = false;
00184   }
00185 
00186 //  cout<<"...leaving SiPixelEDAClient::beginRun. "<<endl;
00187 
00188 }
00189 //
00190 // -- Begin  Luminosity Block
00191 //
00192 void SiPixelEDAClient::beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, 
00193                                             edm::EventSetup const& context) {
00194 //  cout<<"Entering SiPixelEDAClient::beginLuminosityBlock: "<<endl;
00195   
00196   edm::LogInfo ("SiPixelEDAClient") <<"[SiPixelEDAClient]: Begin of LS transition";
00197 
00198   nEvents_lastLS_=0; nErrorsBarrel_lastLS_=0; nErrorsEndcap_lastLS_=0;
00199   MonitorElement * me = bei_->get("Pixel/AdditionalPixelErrors/byLumiErrors");
00200   if(me){
00201     nEvents_lastLS_ = int(me->getBinContent(0));
00202     nErrorsBarrel_lastLS_ = int(me->getBinContent(1));
00203     nErrorsEndcap_lastLS_ = int(me->getBinContent(2));
00204     //std::cout<<"Nevts in lastLS in EDAClient: "<<nEvents_lastLS_<<" "<<nErrorsBarrel_lastLS_<<" "<<nErrorsEndcap_lastLS_<<std::endl;
00205     me->Reset();
00206   }
00207 //  cout<<"...leaving SiPixelEDAClient::beginLuminosityBlock. "<<endl;
00208 }
00209 //
00210 //  -- Analyze 
00211 //
00212 void SiPixelEDAClient::analyze(const edm::Event& e, const edm::EventSetup& eSetup){
00213 //  cout<<"[SiPixelEDAClient::analyze()] "<<endl;
00214   nEvents_++;  
00215   if(!Tier0Flag_){
00216    
00217     if(nEvents_==1){
00218       // check if any Pixel FED is in readout:
00219       edm::Handle<FEDRawDataCollection> rawDataHandle;
00220       e.getByLabel(inputSource_, rawDataHandle);
00221       if(!rawDataHandle.isValid()){
00222         edm::LogInfo("SiPixelEDAClient") << inputSource_ << " is empty";
00223         return;
00224       } 
00225       const FEDRawDataCollection& rawDataCollection = *rawDataHandle;
00226       nFEDs_ = 0;
00227       for(int i = 0; i != 40; i++){
00228         if(rawDataCollection.FEDData(i).size() && rawDataCollection.FEDData(i).data()) nFEDs_++;
00229       }
00230     }
00231     
00232     // This is needed for plotting with the Pixel Expert GUI (interactive client):
00233     sipixelWebInterface_->setActionFlag(SiPixelWebInterface::CreatePlots);
00234     sipixelWebInterface_->performAction();
00235   }
00236   
00237 }
00238 //
00239 // -- End Luminosity Block
00240 //
00241 void SiPixelEDAClient::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
00242   //cout<<"Entering SiPixelEDAClient::endLuminosityBlock: "<<endl;
00243 
00244   edm::LogInfo ("SiPixelEDAClient") <<"[SiPixelEDAClient]: End of LS transition, performing the DQM client operation";
00245 
00246   nLumiSecs_++;
00247   //cout << "nLumiSecs_: "<< nLumiSecs_ << endl;
00248   
00249   edm::LogInfo("SiPixelEDAClient") << "====================================================== " << endl << " ===> Iteration # " << nLumiSecs_ << " " << lumiSeg.luminosityBlock() << endl  << "====================================================== " << endl;
00250   
00251   //if(actionOnLumiSec_ && !Tier0Flag_ && nLumiSecs_ % 1 == 0 ){
00252   if(actionOnLumiSec_ && nLumiSecs_ % 1 == 0 ){
00253     sipixelWebInterface_->setActionFlag(SiPixelWebInterface::Summary);
00254     sipixelWebInterface_->performAction();
00255      //cout << " Updating efficiency plots" << endl;
00256     if(doHitEfficiency_) sipixelActionExecutor_->createEfficiency(bei_);
00257     //cout << " Checking QTest results " << endl;
00258     sipixelWebInterface_->setActionFlag(SiPixelWebInterface::QTestResult);
00259     sipixelWebInterface_->performAction();
00260      //cout << " Updating occupancy plots" << endl;
00261     sipixelActionExecutor_->createOccupancy(bei_);
00262     //cout  << " Checking Pixel quality flags " << endl;;
00263     bei_->cd();
00264     bool init=true;
00265     //sipixelDataQuality_->computeGlobalQualityFlag(bei_,init,nFEDs_,Tier0Flag_);
00266     sipixelDataQuality_->computeGlobalQualityFlagByLumi(bei_,init,nFEDs_,Tier0Flag_,nEvents_lastLS_,nErrorsBarrel_lastLS_,nErrorsEndcap_lastLS_);
00267     init=true;
00268     bei_->cd();
00269     sipixelDataQuality_->fillGlobalQualityPlot(bei_,init,eSetup,nFEDs_,Tier0Flag_,nLumiSecs_);
00270     //cout << " Checking for new noisy pixels " << endl;
00271     init=true;
00272     if(noiseRate_>=0.) sipixelInformationExtractor_->findNoisyPixels(bei_, init, noiseRate_, noiseRateDenominator_, eSetup);
00273     // cout << "*** Creating Tracker Map Histos for End Run ***" << endl;
00274 //    sipixelActionExecutor_->createMaps(bei_, "adc_siPixelDigis", "adc", Mean);
00275 //    sipixelActionExecutor_->createMaps(bei_, "charge_siPixelClusters", "charge", Mean);
00276 //    sipixelActionExecutor_->createMaps(bei_, "ndigis_siPixelDigis", "ndigis", WeightedSum);
00277 //    sipixelActionExecutor_->createMaps(bei_, "NErrors_siPixelDigis", "NErrors", WeightedSum);
00278     // cout << "*** Done with Tracker Map Histos for End Run ***" << endl;
00279   }   
00280          
00281   //cout<<"...leaving SiPixelEDAClient::endLuminosityBlock. "<<endl;
00282 }
00283 //
00284 // -- End Run
00285 //
00286 void SiPixelEDAClient::endRun(edm::Run const& run, edm::EventSetup const& eSetup){
00287   //cout<<"Entering SiPixelEDAClient::endRun: "<<endl;
00288 
00289   //edm::LogVerbatim ("SiPixelEDAClient") <<"[SiPixelEDAClient]: End of Run, saving  DQM output ";
00290   //int iRun = run.run();
00291   
00292   if(actionOnRunEnd_){
00293     //cout << " Updating Summary " << endl;
00294     sipixelWebInterface_->setActionFlag(SiPixelWebInterface::Summary);
00295     sipixelWebInterface_->performAction();
00296      //cout << " Updating efficiency plots" << endl;
00297     if(doHitEfficiency_) sipixelActionExecutor_->createEfficiency(bei_);
00298     //cout << " Checking QTest results " << endl;
00299     sipixelWebInterface_->setActionFlag(SiPixelWebInterface::QTestResult);
00300     sipixelWebInterface_->performAction();
00301     //cout << " Updating occupancy plots" << endl;
00302     sipixelActionExecutor_->createOccupancy(bei_);
00303     //cout  << " Checking Pixel quality flags " << endl;;
00304     bei_->cd();
00305     bool init=true;
00306     sipixelDataQuality_->computeGlobalQualityFlag(bei_,init,nFEDs_,Tier0Flag_);
00307     init=true;
00308     bei_->cd();
00309     //cout  << " Making run end reportSummaryMap: " <<nFEDs_<< endl;;
00310     sipixelDataQuality_->fillGlobalQualityPlot(bei_,init,eSetup,nFEDs_,Tier0Flag_,nLumiSecs_);
00311     //cout << " Checking for new noisy pixels " << endl;
00312     init=true;
00313     if(noiseRate_>=0.) sipixelInformationExtractor_->findNoisyPixels(bei_, init, noiseRate_, noiseRateDenominator_, eSetup);
00314     // cout << "*** Creating Tracker Map Histos for End Run ***" << endl;
00315 //    sipixelActionExecutor_->createMaps(bei_, "adc_siPixelDigis", "adc", Mean);
00316 //    sipixelActionExecutor_->createMaps(bei_, "charge_siPixelClusters", "charge", Mean);
00317 //    sipixelActionExecutor_->createMaps(bei_, "ndigis_siPixelDigis", "ndigis", WeightedSum);
00318 //    sipixelActionExecutor_->createMaps(bei_, "NErrors_siPixelDigis", "NErrors", WeightedSum);
00319     // cout << "*** Done with Tracker Map Histos for End Run ***" << endl;
00320 
00321     // On demand, dump module ID's and stuff on the screen:
00322     //sipixelActionExecutor_->dumpModIds(bei_,eSetup);
00323     // On demand, dump summary histo values for reference on the screen:
00324     //sipixelActionExecutor_->dumpRefValues(bei_,eSetup);
00325   }
00326   
00327 //  cout<<"...leaving SiPixelEDAClient::endRun. "<<endl;
00328 }
00329 
00330 //
00331 // -- End Job
00332 //
00333 void SiPixelEDAClient::endJob(){
00334 //  cout<<"In SiPixelEDAClient::endJob "<<endl;
00335   edm::LogInfo("SiPixelEDAClient") <<"[SiPixelEDAClient]: endjob called!";
00336 
00337 }
00338 //
00339 // -- Create default web page
00340 //
00341 void SiPixelEDAClient::defaultWebPage(xgi::Input *in, xgi::Output *out)
00342 {
00343 //  cout<<"Entering SiPixelEDAClient::defaultWebPage: "<<endl;
00344       
00345   bool isRequest = false;
00346   cgicc::Cgicc cgi(in);
00347   cgicc::CgiEnvironment cgie(in);
00348   //  edm::LogInfo("SiPixelEDAClient") <<"[SiPixelEDAClient]: defaultWebPage "
00349   //             << " query string : " << cgie.getQueryString();
00350   //  if ( xgi::Utils::hasFormElement(cgi,"ClientRequest") ) isRequest = true;
00351   string q_string = cgie.getQueryString();
00352   if (q_string.find("RequestID") != string::npos) isRequest = true;
00353   if (!isRequest) {    
00354     *out << html_out_.str() << std::endl;
00355   }  else {
00356     // Handles all HTTP requests of the form
00357     int iter = nEvents_/100;
00358     sipixelWebInterface_->handleEDARequest(in, out, iter);
00359   }
00360 
00361 //  cout<<"...leaving SiPixelEDAClient::defaultWebPage. "<<endl;
00362 }
00363