CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/HcalMonitorClient/src/HcalMonitorClient.cc

Go to the documentation of this file.
00001 /*
00002  * \file HcalMonitorClient.cc
00003  * 
00004  * $Date: 2010/12/09 15:58:26 $
00005  * $Revision: 1.100 $
00006  * \author J. Temple
00007  * 
00008  */
00009 
00010 #include "DQM/HcalMonitorClient/interface/HcalMonitorClient.h"
00011 #include "DQM/HcalMonitorClient/interface/HcalDeadCellClient.h"
00012 #include "DQM/HcalMonitorClient/interface/HcalHotCellClient.h"
00013 #include "DQM/HcalMonitorClient/interface/HcalRecHitClient.h"
00014 #include "DQM/HcalMonitorClient/interface/HcalRawDataClient.h"
00015 #include "DQM/HcalMonitorClient/interface/HcalDigiClient.h"
00016 #include "DQM/HcalMonitorClient/interface/HcalTrigPrimClient.h"
00017 #include "DQM/HcalMonitorClient/interface/HcalBeamClient.h"
00018 #include "DQM/HcalMonitorClient/interface/HcalNZSClient.h"
00019 #include "DQM/HcalMonitorClient/interface/HcalSummaryClient.h"
00020 #include "DQM/HcalMonitorClient/interface/HcalDetDiagPedestalClient.h"
00021 #include "DQM/HcalMonitorClient/interface/HcalDetDiagLaserClient.h"
00022 #include "DQM/HcalMonitorClient/interface/HcalDetDiagLEDClient.h"
00023 #include "DQM/HcalMonitorClient/interface/HcalDetDiagNoiseMonitorClient.h"
00024 #include "DQM/HcalMonitorClient/interface/HcalDetDiagTimingClient.h"
00025 #include "DQM/HcalMonitorClient/interface/HcalCoarsePedestalClient.h"
00026 
00027 #include "FWCore/Framework/interface/Run.h"
00028 #include "FWCore/Framework/interface/LuminosityBlock.h"
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/MakerMacros.h"
00031 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 #include "FWCore/Framework/interface/ESHandle.h"
00034 
00035 #include "DQMServices/Core/interface/MonitorElement.h"
00036 #include "DQMServices/Core/interface/DQMStore.h"
00037 
00038 #include "CondFormats/HcalObjects/interface/HcalChannelStatus.h"
00039 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
00040 #include "CondFormats/HcalObjects/interface/HcalCondObjectContainer.h"
00041 #include "CondFormats/DataRecord/interface/HcalChannelQualityRcd.h"
00042 
00043 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00044 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00045 
00046 #include <iostream>
00047 #include <iomanip>
00048 #include <fstream>
00049 
00050 #include "TROOT.h"
00051 #include "TH1.h"
00052 
00053 //'using' declarations should only be used within classes/functions, and 'using namespace std;' should not be used,
00054 // according to Bill Tanenbaum  (DQM development hypernews, 25 March 2010)
00055 
00056 HcalMonitorClient::HcalMonitorClient(const edm::ParameterSet& ps)
00057 {
00058   debug_ = ps.getUntrackedParameter<int>("debug",0);
00059   inputFile_ = ps.getUntrackedParameter<std::string>("inputFile","");
00060   mergeRuns_ = ps.getUntrackedParameter<bool>("mergeRuns", false);
00061   cloneME_ = ps.getUntrackedParameter<bool>("cloneME", true);
00062   prescaleFactor_ = ps.getUntrackedParameter<int>("prescaleFactor", -1);
00063   prefixME_ = ps.getUntrackedParameter<std::string>("subSystemFolder", "Hcal/");
00064   if (prefixME_.substr(prefixME_.size()-1,prefixME_.size())!="/")
00065     prefixME_.append("/");
00066   enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup", false);
00067   enabledClients_ = ps.getUntrackedParameter<std::vector<std::string> >("enabledClients", enabledClients_);
00068 
00069   updateTime_ = ps.getUntrackedParameter<int>("UpdateTime",0);
00070   baseHtmlDir_ = ps.getUntrackedParameter<std::string>("baseHtmlDir", "");
00071   htmlUpdateTime_ = ps.getUntrackedParameter<int>("htmlUpdateTime", 0);
00072   htmlFirstUpdate_ = ps.getUntrackedParameter<int>("htmlFirstUpdate",20);
00073   databasedir_   = ps.getUntrackedParameter<std::string>("databaseDir","");
00074   databaseUpdateTime_ = ps.getUntrackedParameter<int>("databaseUpdateTime",0);
00075   databaseFirstUpdate_ = ps.getUntrackedParameter<int>("databaseFirstUpdate",10);
00076 
00077   saveByLumiSection_  = ps.getUntrackedParameter<bool>("saveByLumiSection",false);
00078 
00079   if (debug_>0)
00080     {
00081       std::cout <<"HcalMonitorClient:: The following clients are enabled:"<<std::endl;
00082       for (unsigned int i=0;i<enabledClients_.size();++i)
00083           std::cout <<enabledClients_[i]<<std::endl;
00084     } // if (debug_>0)
00085 
00086   // Set all EtaPhiHists pointers to 0 to start
00087   ChannelStatus=0; 
00088   ADC_PedestalFromDBByDepth=0;
00089   ADC_WidthFromDBByDepth=0;
00090   fC_PedestalFromDBByDepth=0;
00091   fC_WidthFromDBByDepth=0;
00092 
00093   // Add all relevant clients
00094   clients_.clear();
00095   clients_.reserve(14); // any reason to reserve ahead of time?
00096   summaryClient_=0;
00097 
00098   clients_.push_back(new HcalBaseDQClient((std::string)"HcalMonitorModule",ps));
00099   if (find(enabledClients_.begin(), enabledClients_.end(),"DeadCellMonitor")!=enabledClients_.end())
00100     clients_.push_back(new HcalDeadCellClient((std::string)"DeadCellMonitor",ps));
00101   if (find(enabledClients_.begin(), enabledClients_.end(),"HotCellMonitor")!=enabledClients_.end())
00102     clients_.push_back(new HcalHotCellClient((std::string)"HotCellMonitor",ps));
00103   if (find(enabledClients_.begin(), enabledClients_.end(),"RecHitMonitor")!=enabledClients_.end())
00104     clients_.push_back(new HcalRecHitClient((std::string)"RecHitMonitor",ps));
00105   if (find(enabledClients_.begin(), enabledClients_.end(),"DigiMonitor")!=enabledClients_.end())
00106     clients_.push_back(new HcalDigiClient((std::string)"DigiMonitor",ps));
00107   if (find(enabledClients_.begin(), enabledClients_.end(),"RawDataMonitor")!=enabledClients_.end())
00108     clients_.push_back(new HcalRawDataClient((std::string)"RawDataMonitor",ps));
00109   if (find(enabledClients_.begin(), enabledClients_.end(),"TrigPrimMonitor")!=enabledClients_.end())
00110     clients_.push_back(new HcalTrigPrimClient((std::string)"TrigPrimMonitor",ps));
00111   if (find(enabledClients_.begin(), enabledClients_.end(),"NZSMonitor")!=enabledClients_.end())
00112     clients_.push_back(new HcalNZSClient((std::string)"NZSMonitor",ps));
00113   if (find(enabledClients_.begin(), enabledClients_.end(),"BeamMonitor")!=enabledClients_.end())
00114     clients_.push_back(new HcalBeamClient((std::string)"BeamMonitor",ps));
00115   if (find(enabledClients_.begin(), enabledClients_.end(),"DetDiagPedestalMonitor")!=enabledClients_.end())
00116     clients_.push_back(new HcalDetDiagPedestalClient((std::string)"DetDiagPedestalMonitor",ps));
00117   if (find(enabledClients_.begin(), enabledClients_.end(),"DetDiagLaserMonitor")!=enabledClients_.end())
00118     clients_.push_back(new HcalDetDiagLaserClient((std::string)"DetDiagLaserMonitor",ps));
00119   if (find(enabledClients_.begin(), enabledClients_.end(),"DetDiagLEDMonitor")!=enabledClients_.end())
00120     clients_.push_back(new HcalDetDiagLEDClient((std::string)"DetDiagLEDMonitor",ps));
00121   if (find(enabledClients_.begin(), enabledClients_.end(),"DetDiagNoiseMonitor")!=enabledClients_.end())
00122     clients_.push_back(new HcalDetDiagNoiseMonitorClient((std::string)"DetDiagNoiseMonitor",ps));
00123   if (find(enabledClients_.begin(), enabledClients_.end(),"DetDiagTimingMonitor")!=enabledClients_.end())
00124     clients_.push_back(new HcalDetDiagTimingClient((std::string)"DetDiagTimingMonitor",ps));
00125  if (find(enabledClients_.begin(), enabledClients_.end(),"CoarsePedestalMonitor")!=enabledClients_.end())
00126     clients_.push_back(new HcalCoarsePedestalClient((std::string)"CoarsePedestalMonitor",ps));
00127 
00128   if (find(enabledClients_.begin(), enabledClients_.end(),"Summary")!=enabledClients_.end())
00129     summaryClient_ = new HcalSummaryClient((std::string)"ReportSummaryClient",ps);
00130   
00131 } // HcalMonitorClient constructor
00132 
00133 
00134 HcalMonitorClient::~HcalMonitorClient()
00135 {
00136   if (debug_>0) std::cout <<"<HcalMonitorClient>  Exiting..."<<std::endl;
00137   for (unsigned int i=0;i<clients_.size();++i)
00138     delete clients_[i];
00139   //if (summaryClient_) delete summaryClient_;
00140 
00141 }
00142 
00143 void HcalMonitorClient::beginJob(void)
00144 {
00145 
00146   begin_run_ = false;
00147   end_run_   = false;
00148 
00149   run_=-1;
00150   evt_=-1;
00151   ievt_=0;
00152   jevt_=0;
00153 
00154   current_time_ = time(NULL);
00155   last_time_html_ = 0; 
00156   last_time_db_ = 0;   
00157 
00158   // get hold of back-end interface
00159 
00160   dqmStore_ = edm::Service<DQMStore>().operator->();
00161 
00162   if ( inputFile_.size() != 0 ) 
00163     {
00164       if ( dqmStore_ )    dqmStore_->open(inputFile_);
00165     }
00166 
00167   for ( unsigned int i=0; i<clients_.size();++i ) 
00168     clients_[i]->beginJob();
00169 
00170   if ( summaryClient_ ) summaryClient_->beginJob();
00171   
00172 
00173 } // void HcalMonitorClient::beginJob(void)
00174 
00175 
00176 void HcalMonitorClient::beginRun(const edm::Run& r, const edm::EventSetup& c) 
00177 {
00178   if (debug_>0) std::cout <<"<HcalMonitorClient::beginRun(r,c)>"<<std::endl;
00179   begin_run_ = true;
00180   end_run_   = false;
00181 
00182   run_=r.id().run();
00183   evt_=0;
00184   jevt_=0;
00185   htmlcounter_=0;
00186 
00187   // Store list of bad channels and their values
00188   std::map <HcalDetId, unsigned int> badchannelmap; 
00189   badchannelmap.clear();
00190 
00191   // Let's get the channel status quality
00192   edm::ESHandle<HcalChannelQuality> p;
00193   c.get<HcalChannelQualityRcd>().get(p);
00194   chanquality_= new HcalChannelQuality(*p.product());
00195  
00196   if (dqmStore_ && ChannelStatus==0)
00197     {
00198       dqmStore_->setCurrentFolder(prefixME_+"HcalInfo/ChannelStatus");
00199       ChannelStatus=new EtaPhiHists;
00200       ChannelStatus->setup(dqmStore_,"ChannelStatus");
00201       std::stringstream x;
00202       for (unsigned int d=0;d<ChannelStatus->depth.size();++d)
00203         {
00204           ChannelStatus->depth[d]->Reset();
00205           x<<"1+log2(status) for HCAL depth "<<d+1;
00206           if (ChannelStatus->depth[d]) ChannelStatus->depth[d]->setTitle(x.str().c_str());
00207           x.str("");
00208         }
00209     }
00210 
00211   edm::ESHandle<HcalDbService> conditions;
00212   c.get<HcalDbRecord>().get(conditions);
00213   // Now let's setup pedestals
00214   if (dqmStore_ )
00215     {
00216       dqmStore_->setCurrentFolder(prefixME_+"HcalInfo/PedestalsFromCondDB");
00217       if (ADC_PedestalFromDBByDepth==0)
00218         {
00219           ADC_PedestalFromDBByDepth = new EtaPhiHists;
00220           ADC_PedestalFromDBByDepth->setup(dqmStore_,"ADC Pedestals From Conditions DB");
00221         }
00222       if (ADC_WidthFromDBByDepth==0)
00223         {
00224           ADC_WidthFromDBByDepth = new EtaPhiHists;
00225           ADC_WidthFromDBByDepth->setup(dqmStore_,"ADC Widths From Conditions DB");
00226         }
00227       if (fC_PedestalFromDBByDepth==0)
00228         {
00229           fC_PedestalFromDBByDepth = new EtaPhiHists;
00230           fC_PedestalFromDBByDepth->setup(dqmStore_,"fC Pedestals From Conditions DB");
00231         }
00232       if (fC_WidthFromDBByDepth==0)
00233         {
00234           fC_WidthFromDBByDepth = new EtaPhiHists;
00235           fC_WidthFromDBByDepth->setup(dqmStore_,"fC Widths From Conditions DB");
00236         }
00237       PlotPedestalValues(*conditions);
00238     }
00239 
00240   // Find only channels with non-zero quality, and add them to badchannelmap
00241   std::vector<DetId> mydetids = chanquality_->getAllChannels();
00242   for (std::vector<DetId>::const_iterator i = mydetids.begin();i!=mydetids.end();++i)
00243     {
00244       if (i->det()!=DetId::Hcal) continue; // not an hcal cell
00245       HcalDetId id=HcalDetId(*i);
00246       int status=(chanquality_->getValues(id))->getValue();
00247       //if (status!=status) status=-1;  // protects against NaN values
00248       // The above line doesn't seem to work in identifying NaNs;  ints for bad values come back as negative numbers (at least in run 146501)
00249       if (status==0) continue;
00250       badchannelmap[id]=status;
00251 
00252       // Fill Channel Status histogram
00253       if (dqmStore_==0) continue;
00254       int depth=id.depth();
00255       if (depth<1 || depth>4) continue;
00256       int ieta=id.ieta();
00257       int iphi=id.iphi();
00258       if (id.subdet()==HcalForward)
00259         ieta>0 ? ++ieta: --ieta;
00260 
00261       double logstatus = 0;
00262       // Fill ChannelStatus value with '-1' when a 'NaN' occurs
00263       if (status<0)
00264         logstatus=-1*(log2(-1.*status)+1);
00265       else
00266         logstatus=log2(1.*status)+1;
00267       if (ChannelStatus->depth[depth-1]) ChannelStatus->depth[depth-1]->Fill(ieta,iphi,logstatus);
00268     }
00269     
00270   for (unsigned int i=0;i<clients_.size();++i)
00271     {
00272       if (clients_[i]->name()=="RawDataMonitor") clients_[i]->setEventSetup(c);
00273       clients_[i]->beginRun();
00274       clients_[i]->setStatusMap(badchannelmap);
00275     }
00276   
00277   if (summaryClient_!=0)
00278     {
00279       summaryClient_->getFriends(clients_);
00280       summaryClient_->beginRun();
00281     }
00282 
00283 } // void HcalMonitorClient::beginRun(const Run& r, const EventSetup& c)
00284 
00285 void HcalMonitorClient::beginRun()
00286 {
00287   // What is the difference between this and beginRun above?
00288   // When would this be called?
00289   begin_run_ = true;
00290   end_run_   = false;
00291   jevt_ = 0;
00292   htmlcounter_=0;
00293 
00294   if (dqmStore_==0 || ChannelStatus!=0) return;
00295   dqmStore_->setCurrentFolder(prefixME_+"HcalInfo");
00296   ChannelStatus=new EtaPhiHists;
00297   ChannelStatus->setup(dqmStore_,"ChannelStatus");
00298   std::stringstream x;
00299   for (unsigned int d=0;d<ChannelStatus->depth.size();++d)
00300     {
00301       x<<"1+log2(status) for HCAL depth "<<d+1;
00302       if (ChannelStatus->depth[d]) ChannelStatus->depth[d]->setTitle(x.str().c_str());
00303       x.str("");
00304     }
00305 } // void HcalMonitorClient::beginRun()
00306 
00307 void HcalMonitorClient::setup(void)
00308 {
00309   // no setup required
00310 }
00311 
00312 void HcalMonitorClient::beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &c) 
00313 {
00314   if (debug_>0) std::cout <<"<HcalMonitorClient::beginLuminosityBlock>"<<std::endl;
00315 } // void HcalMonitorClient::beginLuminosityBlock
00316 
00317 void HcalMonitorClient::analyze(const edm::Event & e, const edm::EventSetup & c)
00318 {
00319   if (debug_>4) 
00320     std::cout <<"HcalMonitorClient::analyze(const edm::Event&, const edm::EventSetup&) ievt_ = "<<ievt_<<std::endl;
00321   ievt_++;
00322   jevt_++;
00323 
00324   run_=e.id().run();
00325   evt_=e.id().event();
00326   if (prescaleFactor_>0 && jevt_%prescaleFactor_==0) this->analyze(e.luminosityBlock());
00327 
00328 } // void HcalMonitorClient::analyze(const edm::Event & e, const edm::EventSetup & c)
00329 
00330 void HcalMonitorClient::analyze(int LS)
00331 {
00332   if (debug_>0)
00333     std::cout <<"HcalMonitorClient::analyze() "<<std::endl;
00334   current_time_ = time(NULL);
00335   // no ievt_, jevt_ counters needed here:  this function gets called at endlumiblock, after default analyze function runs
00336   for (unsigned int i=0;i<clients_.size();++i)
00337     clients_[i]->analyze();
00338   if (summaryClient_!=0)
00339     {
00340       // Always call basic analyze to form histograms for each task
00341       summaryClient_->analyze(LS);
00342       // Call this if LS-by-LS enabling is set to true
00343       if (saveByLumiSection_==true)
00344         summaryClient_->fillReportSummaryLSbyLS(LS);
00345     }
00346 } // void HcalMonitorClient::analyze()
00347 
00348 
00349 void HcalMonitorClient::endLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &c) 
00350 {
00351   if (debug_>0) std::cout <<"<HcalMonitorClient::endLuminosityBlock>"<<std::endl;
00352   current_time_ = time(NULL);
00353   if (updateTime_>0)
00354     {
00355       if ((current_time_-last_time_update_)<60*updateTime_)
00356         return;
00357       last_time_update_ = current_time_;
00358     }
00359   this->analyze(l.luminosityBlock());
00360 
00361   if (databaseUpdateTime_>0)
00362     {
00363       if (
00364           // first update occurs at after databaseFirstUpdate_ minutes
00365           (last_time_db_==0 && (current_time_-last_time_db_)>=60*databaseFirstUpdate_)
00366           ||
00367           // following updates follow once every databaseUpdateTime_ minutes
00368           ((current_time_-last_time_db_)>=60*databaseUpdateTime_)
00369           )
00370         {
00371           this->writeChannelStatus();
00372           last_time_db_=current_time_;
00373         }
00374     }
00375 
00376   if (htmlUpdateTime_>0)
00377     {
00378       if (
00379           (last_time_html_==0 && (current_time_-last_time_html_)>=60*htmlFirstUpdate_)
00380           // 
00381           ||((current_time_-last_time_html_)>=60*htmlUpdateTime_)
00382           ) // htmlUpdateTime_ in minutes
00383         {
00384           this->writeHtml();
00385           last_time_html_=current_time_;
00386         }
00387     }
00388 
00389 } // void HcalMonitorClient::endLuminosityBlock
00390 
00391 void HcalMonitorClient::endRun(void)
00392 {
00393   begin_run_ = false;
00394   end_run_   = true;
00395 
00396   // Always fill summaryClient at end of run (as opposed to the end-lumi fills, which may just contain info for a single LS)
00397   // At the end of this run, set LS=-1  (LS-based plotting in doesn't work yet anyway)
00398   if (summaryClient_)
00399     summaryClient_->analyze(-1);
00400 
00401   if (databasedir_.size()>0)
00402     this->writeChannelStatus();
00403   // writeHtml takes longer; run it last 
00404   // Also, don't run it if htmlUpdateTime_>0 -- it should have already been run
00405   if (baseHtmlDir_.size()>0 && htmlUpdateTime_==0)
00406     this->writeHtml();
00407 }
00408 
00409 void HcalMonitorClient::endRun(const edm::Run& r, const edm::EventSetup& c) 
00410 {
00411   this->analyze();
00412   this->endRun();
00413 }
00414 
00415 void HcalMonitorClient::endJob(void)
00416 {
00417   if (! end_run_)
00418     {
00419       this->analyze();
00420       this->endRun();
00421     }
00422   this->cleanup();
00423 
00424   for ( unsigned int i=0; i<clients_.size(); i++ ) 
00425     clients_[i]->endJob();
00426   //if ( summaryClient_ ) summaryClient_->endJob();
00427 
00428 } // void HcalMonitorClient::endJob(void)
00429 
00430 void HcalMonitorClient::cleanup(void)
00431 {
00432   if (!enableCleanup_) return;
00433   // other cleanup?
00434 } // void HcalMonitorClient::cleanup(void)
00435 
00436 
00437 void HcalMonitorClient::writeHtml()
00438 {
00439   if (debug_>0) std::cout << "Preparing HcalMonitorClient html output ..." << std::endl;
00440   
00441 
00442   // global ROOT style
00443   gStyle->Reset("Default");
00444   gStyle->SetCanvasColor(0);
00445   gStyle->SetPadColor(0);
00446   gStyle->SetFillColor(0);
00447   gStyle->SetTitleFillColor(10);
00448   //  gStyle->SetOptStat(0);
00449   gStyle->SetOptStat("ouemr");
00450   gStyle->SetPalette(1);
00451 
00452   char tmp[20];
00453 
00454   if(run_!=-1) sprintf(tmp, "DQM_%s_R%09d_%i", prefixME_.substr(0,prefixME_.size()-1).c_str(),run_,htmlcounter_);
00455   else sprintf(tmp, "DQM_%s_R%09d_%i", prefixME_.substr(0,prefixME_.size()-1).c_str(),0,htmlcounter_);
00456   std::string htmlDir = baseHtmlDir_ + "/" + tmp + "/";
00457   system(("/bin/mkdir -p " + htmlDir).c_str());
00458 
00459   ++htmlcounter_;
00460 
00461   ofstream htmlFile;
00462   htmlFile.open((htmlDir + "index.html").c_str());
00463 
00464   // html page header
00465   htmlFile << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">  " << std::endl;
00466   htmlFile << "<html>  " << std::endl;
00467   htmlFile << "<head>  " << std::endl;
00468   htmlFile << "  <meta content=\"text/html; charset=ISO-8859-1\"  " << std::endl;
00469   htmlFile << " https-equiv=\"content-type\">  " << std::endl;
00470   htmlFile << "  <title>Hcal Data Quality Monitor</title> " << std::endl;
00471   htmlFile << "</head>  " << std::endl;
00472   htmlFile << "<body>  " << std::endl;
00473   htmlFile << "<br>  " << std::endl;
00474  htmlFile << "<center><h1>Hcal Data Quality Monitor</h1></center>" << std::endl;
00475   htmlFile << "<h2>Run Number:&nbsp;&nbsp;&nbsp;" << std::endl;
00476   htmlFile << "<span style=\"color: rgb(0, 0, 153);\">" << run_ <<"</span></h2> " << std::endl;
00477   htmlFile << "<h2>Events processed:&nbsp;&nbsp;&nbsp;" << std::endl;
00478   htmlFile << "<span style=\"color: rgb(0, 0, 153);\">" << ievt_ <<"</span></h2> " << std::endl;
00479   htmlFile << "<hr>" << std::endl;
00480   htmlFile << "<ul>" << std::endl;
00481 
00482   for (unsigned int i=0;i<clients_.size();++i)
00483     {
00484       if (clients_[i]->validHtmlOutput()==true)
00485         {
00486           clients_[i]->htmlOutput(htmlDir);
00487           // Always print this out?  Or only when validHtmlOutput is true? 
00488           htmlFile << "<table border=0 WIDTH=\"50%\"><tr>" << std::endl;
00489           htmlFile << "<td WIDTH=\"35%\"><a href=\"" << clients_[i]->name_ << ".html"<<"\">"<<clients_[i]->name_<<"</a></td>" << std::endl;
00490           if(clients_[i]->hasErrors_Temp()) htmlFile << "<td bgcolor=red align=center>This monitor task has errors.</td>" << std::endl;
00491           else if(clients_[i]->hasWarnings_Temp()) htmlFile << "<td bgcolor=yellow align=center>This monitor task has warnings.</td>" << std::endl;
00492           else if(clients_[i]->hasOther_Temp()) htmlFile << "<td bgcolor=aqua align=center>This monitor task has messages.</td>" << std::endl;
00493           else htmlFile << "<td bgcolor=lime align=center>This monitor task has no problems</td>" << std::endl;
00494           htmlFile << "</tr></table>" << std::endl;
00495         }
00496     }
00497 
00498   // Add call to reportSummary html output
00499   if (summaryClient_)
00500     {
00501       summaryClient_->htmlOutput(htmlDir);
00502       htmlFile << "<table border=0 WIDTH=\"50%\"><tr>" << std::endl;
00503       htmlFile << "<td WIDTH=\"35%\"><a href=\"" << summaryClient_->name_ << ".html"<<"\">"<<summaryClient_->name_<<"</a></td>" << std::endl;
00504       if(summaryClient_->hasErrors_Temp()) htmlFile << "<td bgcolor=red align=center>This monitor task has errors.</td>" << std::endl;
00505       else if(summaryClient_->hasWarnings_Temp()) htmlFile << "<td bgcolor=yellow align=center>This monitor task has warnings.</td>" << std::endl;
00506       else if(summaryClient_->hasOther_Temp()) htmlFile << "<td bgcolor=aqua align=center>This monitor task has messages.</td>" << std::endl;
00507       else htmlFile << "<td bgcolor=lime align=center>This monitor task has no problems</td>" << std::endl;
00508       htmlFile << "</tr></table>" << std::endl;
00509     }
00510 
00511   htmlFile << "</ul>" << std::endl;
00512 
00513   // html page footer
00514   htmlFile << "</body> " << std::endl;
00515   htmlFile << "</html> " << std::endl;
00516 
00517   htmlFile.close();
00518   if (debug_>0) std::cout << "HcalMonitorClient html output done..." << std::endl;
00519   
00520 } // void HcalMonitorClient::writeHtml()
00521 
00522 void HcalMonitorClient::writeChannelStatus()
00523 {
00524   if (databasedir_.size()==0) return;
00525   if (debug_>0) std::cout <<"<HcalMonitorClient::writeDBfile>  Writing file for database"<<std::endl;
00526 
00527   std::map<HcalDetId, unsigned int> myquality; //map of quality flags as reported by each client
00528   // Get status from all channels (we need to store all channels in case a bad channel suddenly becomes good)
00529   for (std::vector<HcalBaseDQClient*>::size_type i=0;i<clients_.size();++i)
00530     clients_[i]->updateChannelStatus(myquality);
00531 
00532   if (debug_>0) std::cout <<"<HcalMonitorClient::writeChannelStatus()>  myquality size = "<<myquality.size()<<std::endl;
00533 
00534   std::vector<DetId> mydetids = chanquality_->getAllChannels();
00535   HcalChannelQuality* newChanQual = new HcalChannelQuality();
00536 
00537   for (unsigned int i=0;i<mydetids.size();++i)
00538     {
00539       if (mydetids[i].det()!=DetId::Hcal) continue; // not hcal
00540       
00541       HcalDetId id=mydetids[i];
00542       // get original channel status item
00543       const HcalChannelStatus* origstatus=chanquality_->getValues(mydetids[i]);
00544       // make copy of status
00545       HcalChannelStatus* mystatus=new HcalChannelStatus(origstatus->rawId(),origstatus->getValue());
00546       // loop over myquality flags
00547       if (myquality.find(id)!=myquality.end())
00548         {
00549           
00550           // check dead cells
00551           if ((myquality[id]>>HcalChannelStatus::HcalCellDead)&0x1)
00552             mystatus->setBit(HcalChannelStatus::HcalCellDead);
00553           else
00554             mystatus->unsetBit(HcalChannelStatus::HcalCellDead);
00555           // check hot cells
00556           if ((myquality[id]>>HcalChannelStatus::HcalCellHot)&0x1)
00557             mystatus->setBit(HcalChannelStatus::HcalCellHot);
00558           else
00559             mystatus->unsetBit(HcalChannelStatus::HcalCellHot);
00560         } // if (myquality.find_...)
00561       newChanQual->addValues(*mystatus);
00562     } // for (unsigned int i=0;...)
00563   
00564   //Now dump out to text file
00565   std::ostringstream file;
00566   databasedir_=databasedir_+"/"; // add extra slash, just in case
00567   //file <<databasedir_<<"HcalDQMstatus_"<<run_<<".txt";
00568   file <<databasedir_<<"HcalDQMstatus.txt";
00569   std::ofstream outStream(file.str().c_str());
00570   outStream<<"###  Run # "<<run_<<std::endl;
00571   HcalDbASCIIIO::dumpObject (outStream, (*newChanQual));
00572   return;
00573 } // void HcalMonitorClient::writeChannelStatus()
00574 
00575 
00576 void HcalMonitorClient::PlotPedestalValues(const HcalDbService& cond)
00577 {
00578   const HcalQIEShape* shape_ = cond.getHcalShape(); // this one is generic
00579 
00580   double ADC_ped=0;
00581   double ADC_width=0;
00582   double fC_ped=0;
00583   double fC_width=0;
00584   double temp_ADC=0;
00585   double temp_fC=0;
00586 
00587   int ieta=-9999;
00588   int iphi=-9999;
00589   HcalCalibrations calibs_;
00590 
00591   ADC_PedestalFromDBByDepth->Reset();
00592   ADC_WidthFromDBByDepth->Reset();
00593   fC_PedestalFromDBByDepth->Reset();
00594   fC_WidthFromDBByDepth->Reset();
00595 
00596 
00597   for (int subdet=1; subdet<=4;++subdet)
00598     {
00599       for (int depth=0;depth<4;++depth)
00600         {
00601           int etabins= ADC_PedestalFromDBByDepth->depth[depth]->getNbinsX();
00602           int phibins = ADC_PedestalFromDBByDepth->depth[depth]->getNbinsY();
00603           for (int eta=0;eta<etabins;++eta)
00604             {
00605               ieta=CalcIeta(subdet,eta,depth+1);
00606               if (ieta==-9999) continue;
00607               for (int phi=0;phi<phibins;++phi)
00608                 {
00609                   iphi=phi+1;
00610                   if (!validDetId((HcalSubdetector)(subdet), ieta, iphi, depth+1)) continue;
00611                   HcalDetId detid((HcalSubdetector)(subdet), ieta, iphi, depth+1);
00612                   ADC_ped=0;
00613                   ADC_width=0;
00614                   fC_ped=0;
00615                   fC_width=0;
00616                   calibs_= cond.getHcalCalibrations(detid);  
00617                   const HcalPedestalWidth* pedw = cond.getPedestalWidth(detid);
00618                   const HcalQIECoder* channelCoder_ = cond.getHcalCoder(detid);
00619 
00620                   // Loop over capIDs
00621                   for (unsigned int capid=0;capid<4;++capid)
00622                     {
00623                       // Still need to determine how to convert widths to ADC or fC
00624                       // calibs_.pedestal value is always in fC, according to Radek
00625                       temp_fC = calibs_.pedestal(capid);
00626                       fC_ped+= temp_fC;
00627                       // convert to ADC from fC
00628                       temp_ADC=channelCoder_->adc(*shape_,
00629                                                   (float)calibs_.pedestal(capid),
00630                                                   capid);
00631                       ADC_ped+=temp_ADC;
00632                       // Pedestals assumed to be read out in fC
00633                       temp_fC=pedw->getSigma(capid,capid);
00634                       fC_width+=temp_fC;
00635                       temp_ADC=pedw->getSigma(capid,capid)*pow(1.*channelCoder_->adc(*shape_,(float)calibs_.pedestal(capid),capid)/calibs_.pedestal(capid),2);
00636                       ADC_width+=temp_ADC;
00637                     }//capid loop
00638 
00639                   // Pedestal values are average over four cap IDs
00640                   // widths are sqrt(SUM [sigma_ii^2])/4.
00641                   fC_ped/=4.;
00642                   ADC_ped/=4.;
00643 
00644                   // Divide width by 2, or by four?
00645                   // Dividing by 2 gives subtracted results closer to zero -- estimate of variance?
00646                   fC_width=pow(fC_width,0.5)/2.;
00647                   ADC_width=pow(ADC_width,0.5)/2.;
00648 
00649                   if (debug_>1)
00650                     {
00651                       std::cout <<"<HcalMonitorClient::PlotPedestalValues> HcalDet ID = "<<(HcalSubdetector)subdet<<": ("<<ieta<<", "<<iphi<<", "<<depth<<")"<<std::endl;
00652                       std::cout <<"\tADC pedestal = "<<ADC_ped<<" +/- "<<ADC_width<<std::endl;
00653                       std::cout <<"\tfC pedestal = "<<fC_ped<<" +/- "<<fC_width<<std::endl;
00654                     }
00655                   // Shift HF by -/+1 when filling eta-phi histograms
00656                   int zside=0;
00657                   if (subdet==4)
00658                     {
00659                       if (ieta<0) zside=-1;
00660                       else zside=1;
00661                     }
00662                   ADC_PedestalFromDBByDepth->depth[depth]->Fill(ieta+zside,iphi,ADC_ped);
00663                   ADC_WidthFromDBByDepth->depth[depth]->Fill(ieta+zside, iphi, ADC_width);
00664                   fC_PedestalFromDBByDepth->depth[depth]->Fill(ieta+zside,iphi,fC_ped);
00665                   fC_WidthFromDBByDepth->depth[depth]->Fill(ieta+zside, iphi, fC_width);
00666                 } // phi loop
00667             } // eta loop
00668         } //depth loop
00669 
00670     } // subdet loop
00671   FillUnphysicalHEHFBins(*ADC_PedestalFromDBByDepth);
00672   FillUnphysicalHEHFBins(*ADC_WidthFromDBByDepth);
00673   FillUnphysicalHEHFBins(*fC_PedestalFromDBByDepth);
00674   FillUnphysicalHEHFBins(*fC_WidthFromDBByDepth);
00675 
00676   // Center ADC pedestal values near 3 +/- 1
00677   for (unsigned int i=0;i<ADC_PedestalFromDBByDepth->depth.size();++i)
00678   {
00679     ADC_PedestalFromDBByDepth->depth[i]->getTH2F()->SetMinimum(0);
00680     if (ADC_PedestalFromDBByDepth->depth[i]->getTH2F()->GetMaximum()<6)
00681       ADC_PedestalFromDBByDepth->depth[i]->getTH2F()->SetMaximum(6);
00682   }
00683 
00684   for (unsigned int i=0;i<ADC_WidthFromDBByDepth->depth.size();++i)
00685   {
00686     ADC_WidthFromDBByDepth->depth[i]->getTH2F()->SetMinimum(0);
00687     if (ADC_WidthFromDBByDepth->depth[i]->getTH2F()->GetMaximum()<2)
00688       ADC_WidthFromDBByDepth->depth[i]->getTH2F()->SetMaximum(2);
00689   }
00690 
00691 }
00692 
00693 DEFINE_FWK_MODULE(HcalMonitorClient);