CMS 3D CMS Logo

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

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