CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/DQM/EcalBarrelMonitorClient/src/EBTimingClient.cc

Go to the documentation of this file.
00001 /*
00002  * \file EBTimingClient.cc
00003  *
00004  * $Date: 2011/09/02 14:02:36 $
00005  * $Revision: 1.111 $
00006  * \author G. Della Ricca
00007  *
00008 */
00009 
00010 #include <memory>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <iomanip>
00014 #include <math.h>
00015 
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 
00018 #include "DQMServices/Core/interface/DQMStore.h"
00019 #include "DQMServices/Core/interface/MonitorElement.h"
00020 
00021 #ifdef WITH_ECAL_COND_DB
00022 #include "OnlineDB/EcalCondDB/interface/MonTimingCrystalDat.h"
00023 #include "OnlineDB/EcalCondDB/interface/RunCrystalErrorsDat.h"
00024 #include "OnlineDB/EcalCondDB/interface/RunTTErrorsDat.h"
00025 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
00026 #include "DQM/EcalCommon/interface/LogicID.h"
00027 #endif
00028 
00029 #include "DQM/EcalCommon/interface/Masks.h"
00030 
00031 #include "DQM/EcalCommon/interface/UtilsClient.h"
00032 #include "DQM/EcalCommon/interface/Numbers.h"
00033 
00034 #include "DQM/EcalBarrelMonitorClient/interface/EBTimingClient.h"
00035 
00036 EBTimingClient::EBTimingClient(const edm::ParameterSet& ps) {
00037 
00038   // cloneME switch
00039   cloneME_ = ps.getUntrackedParameter<bool>("cloneME", true);
00040 
00041   // verbose switch
00042   verbose_ = ps.getUntrackedParameter<bool>("verbose", true);
00043 
00044   // debug switch
00045   debug_ = ps.getUntrackedParameter<bool>("debug", false);
00046 
00047   // prefixME path
00048   prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
00049 
00050   // enableCleanup_ switch
00051   enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup", false);
00052 
00053   // vector of selected Super Modules (Defaults to all 36).
00054   superModules_.reserve(36);
00055   for ( unsigned int i = 1; i <= 36; i++ ) superModules_.push_back(i);
00056   superModules_ = ps.getUntrackedParameter<std::vector<int> >("superModules", superModules_);
00057 
00058   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00059 
00060     int ism = superModules_[i];
00061 
00062     h01_[ism-1] = 0;
00063     h02_[ism-1] = 0;
00064 
00065     meh01_[ism-1] = 0;
00066     meh02_[ism-1] = 0;
00067 
00068   }
00069 
00070   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00071 
00072     int ism = superModules_[i];
00073 
00074     meg01_[ism-1] = 0;
00075 
00076     mea01_[ism-1] = 0;
00077 
00078     mep01_[ism-1] = 0;
00079 
00080     mer01_[ism-1] = 0;
00081 
00082   }
00083 
00084   meTimeSummaryMapProjEta_ = 0;
00085   meTimeSummaryMapProjPhi_ = 0;
00086 
00087   expectedMean_ = 0.0;
00088   discrepancyMean_ = 2.0;
00089   RMSThreshold_ = 6.0;
00090 
00091 }
00092 
00093 EBTimingClient::~EBTimingClient() {
00094 
00095 }
00096 
00097 void EBTimingClient::beginJob(void) {
00098 
00099   dqmStore_ = edm::Service<DQMStore>().operator->();
00100 
00101   if ( debug_ ) std::cout << "EBTimingClient: beginJob" << std::endl;
00102 
00103   ievt_ = 0;
00104   jevt_ = 0;
00105 
00106 }
00107 
00108 void EBTimingClient::beginRun(void) {
00109 
00110   if ( debug_ ) std::cout << "EBTimingClient: beginRun" << std::endl;
00111 
00112   jevt_ = 0;
00113 
00114   this->setup();
00115 
00116 }
00117 
00118 void EBTimingClient::endJob(void) {
00119 
00120   if ( debug_ ) std::cout << "EBTimingClient: endJob, ievt = " << ievt_ << std::endl;
00121 
00122   this->cleanup();
00123 
00124 }
00125 
00126 void EBTimingClient::endRun(void) {
00127 
00128   if ( debug_ ) std::cout << "EBTimingClient: endRun, jevt = " << jevt_ << std::endl;
00129 
00130   this->cleanup();
00131 
00132 }
00133 
00134 void EBTimingClient::setup(void) {
00135 
00136   std::string name;
00137 
00138   dqmStore_->setCurrentFolder( prefixME_ + "/EBTimingClient" );
00139 
00140   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00141 
00142     int ism = superModules_[i];
00143 
00144     if ( meg01_[ism-1] ) dqmStore_->removeElement( meg01_[ism-1]->getName() );
00145     name = "EBTMT timing quality " + Numbers::sEB(ism);
00146     meg01_[ism-1] = dqmStore_->book2D(name, name, 85, 0., 85., 20, 0., 20.);
00147     meg01_[ism-1]->setAxisTitle("ieta", 1);
00148     meg01_[ism-1]->setAxisTitle("iphi", 2);
00149 
00150     if ( mea01_[ism-1] ) dqmStore_->removeElement( mea01_[ism-1]->getName() );
00151     name = "EBTMT timing " + Numbers::sEB(ism);
00152     mea01_[ism-1] = dqmStore_->book1D(name, name, 1700, 0., 1700.);
00153     mea01_[ism-1]->setAxisTitle("channel", 1);
00154     mea01_[ism-1]->setAxisTitle("time (ns)", 2);
00155 
00156     if ( mep01_[ism-1] ) dqmStore_->removeElement( mep01_[ism-1]->getName() );
00157     name = "EBTMT timing mean " + Numbers::sEB(ism);
00158     mep01_[ism-1] = dqmStore_->book1D(name, name, 100, -25.0, 25.0);
00159     mep01_[ism-1]->setAxisTitle("mean (ns)", 1);
00160 
00161     if ( mer01_[ism-1] ) dqmStore_->removeElement( mer01_[ism-1]->getName() );
00162     name = "EBTMT timing rms " + Numbers::sEB(ism);
00163     mer01_[ism-1] = dqmStore_->book1D(name, name, 100, 0.0, 10.0);
00164     mer01_[ism-1]->setAxisTitle("rms (ns)", 1);
00165 
00166   }
00167 
00168   name = "EBTMT timing projection eta";
00169   meTimeSummaryMapProjEta_ = dqmStore_->bookProfile(name, name, 34, -85., 85., -20., 20., "");
00170   meTimeSummaryMapProjEta_->setAxisTitle("jeta", 1);
00171   meTimeSummaryMapProjEta_->setAxisTitle("time (ns)", 2);
00172 
00173   name = "EBTMT timing projection phi";
00174   meTimeSummaryMapProjPhi_ = dqmStore_->bookProfile(name, name, 72, 0., 360., -20., 20., "");
00175   meTimeSummaryMapProjPhi_->setAxisTitle("jphi", 1);
00176   meTimeSummaryMapProjPhi_->setAxisTitle("time (ns)", 2);
00177 
00178   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00179 
00180     int ism = superModules_[i];
00181 
00182     if ( meg01_[ism-1] ) meg01_[ism-1]->Reset();
00183 
00184     for ( int ie = 1; ie <= 85; ie++ ) {
00185       for ( int ip = 1; ip <= 20; ip++ ) {
00186 
00187         if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent( ie, ip, 2. );
00188 
00189       }
00190     }
00191 
00192     if ( mea01_[ism-1] ) mea01_[ism-1]->Reset();
00193     if ( mep01_[ism-1] ) mep01_[ism-1]->Reset();
00194     if ( mer01_[ism-1] ) mer01_[ism-1]->Reset();
00195 
00196   }
00197 
00198 
00199 
00200 }
00201 
00202 void EBTimingClient::cleanup(void) {
00203 
00204   if ( ! enableCleanup_ ) return;
00205 
00206   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00207 
00208     int ism = superModules_[i];
00209 
00210     if ( cloneME_ ) {
00211       if ( h01_[ism-1] ) delete h01_[ism-1];
00212       if ( h02_[ism-1] ) delete h02_[ism-1];
00213     }
00214 
00215     h01_[ism-1] = 0;
00216     h02_[ism-1] = 0;
00217 
00218     meh01_[ism-1] = 0;
00219     meh02_[ism-1] = 0;
00220 
00221   }
00222 
00223   dqmStore_->setCurrentFolder( prefixME_ + "/EBTimingClient" );
00224 
00225   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00226 
00227     int ism = superModules_[i];
00228 
00229     if ( meg01_[ism-1] ) dqmStore_->removeElement( meg01_[ism-1]->getName() );
00230     meg01_[ism-1] = 0;
00231 
00232     if ( mea01_[ism-1] ) dqmStore_->removeElement( mea01_[ism-1]->getName() );
00233     mea01_[ism-1] = 0;
00234 
00235     if ( mep01_[ism-1] ) dqmStore_->removeElement( mep01_[ism-1]->getName() );
00236     mep01_[ism-1] = 0;
00237 
00238     if ( mer01_[ism-1] ) dqmStore_->removeElement( mer01_[ism-1]->getName() );
00239     mer01_[ism-1] = 0;
00240 
00241   }
00242 
00243   if ( meTimeSummaryMapProjEta_ ) dqmStore_->removeElement( meTimeSummaryMapProjEta_->getName() );
00244   if ( meTimeSummaryMapProjPhi_ ) dqmStore_->removeElement( meTimeSummaryMapProjPhi_->getName() );
00245 
00246 }
00247 
00248 #ifdef WITH_ECAL_COND_DB
00249 bool EBTimingClient::writeDb(EcalCondDBInterface* econn, RunIOV* runiov, MonRunIOV* moniov, bool& status) {
00250 
00251   status = true;
00252 
00253   EcalLogicID ecid;
00254 
00255   MonTimingCrystalDat t;
00256   std::map<EcalLogicID, MonTimingCrystalDat> dataset;
00257 
00258   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00259 
00260     int ism = superModules_[i];
00261 
00262     if ( verbose_ ) {
00263       std::cout << " " << Numbers::sEB(ism) << " (ism=" << ism << ")" << std::endl;
00264       std::cout << std::endl;
00265       UtilsClient::printBadChannels(meg01_[ism-1], h01_[ism-1]);
00266     }
00267 
00268     float num01;
00269     float mean01;
00270     float rms01;
00271 
00272     for ( int ie = 1; ie <= 85; ie++ ) {
00273       for ( int ip = 1; ip <= 20; ip++ ) {
00274 
00275         bool update01;
00276 
00277         update01 = UtilsClient::getBinStatistics(h01_[ism-1], ie, ip, num01, mean01, rms01);
00278         // Task timing map is shifted of +50 ns for graphical reasons. Shift back it.
00279         mean01 -= 50.;
00280 
00281         if ( update01 ) {
00282 
00283           if ( Numbers::icEB(ism, ie, ip) == 1 ) {
00284 
00285             if ( verbose_ ) {
00286               std::cout << "Preparing dataset for " << Numbers::sEB(ism) << " (ism=" << ism << ")" << std::endl;
00287               std::cout << "crystal (" << ie << "," << ip << ") " << num01  << " " << mean01 << " " << rms01  << std::endl;
00288               std::cout << std::endl;
00289             }
00290 
00291           }
00292 
00293           t.setTimingMean(mean01);
00294           t.setTimingRMS(rms01);
00295 
00296           if ( UtilsClient::getBinStatus(meg01_[ism-1], ie, ip) ) {
00297             t.setTaskStatus(true);
00298           } else {
00299             t.setTaskStatus(false);
00300           }
00301 
00302           status = status && UtilsClient::getBinQuality(meg01_[ism-1], ie, ip);
00303 
00304           int ic = Numbers::indexEB(ism, ie, ip);
00305 
00306           if ( econn ) {
00307             ecid = LogicID::getEcalLogicID("EB_crystal_number", Numbers::iSM(ism, EcalBarrel), ic);
00308             dataset[ecid] = t;
00309           }
00310 
00311         }
00312 
00313       }
00314     }
00315 
00316   }
00317 
00318   if ( econn ) {
00319     try {
00320       if ( verbose_ ) std::cout << "Inserting MonTimingCrystalDat ..." << std::endl;
00321       if ( dataset.size() != 0 ) econn->insertDataArraySet(&dataset, moniov);
00322       if ( verbose_ ) std::cout << "done." << std::endl;
00323     } catch (std::runtime_error &e) {
00324       std::cerr << e.what() << std::endl;
00325     }
00326   }
00327 
00328   return true;
00329 
00330 }
00331 #endif
00332 
00333 void EBTimingClient::analyze(void) {
00334 
00335   ievt_++;
00336   jevt_++;
00337   if ( ievt_ % 10 == 0 ) {
00338     if ( debug_ ) std::cout << "EBTimingClient: ievt/jevt = " << ievt_ << "/" << jevt_ << std::endl;
00339   }
00340 
00341   uint32_t bits01 = 0;
00342   bits01 |= 1 << EcalDQMStatusHelper::PHYSICS_BAD_CHANNEL_WARNING;
00343 
00344   MonitorElement* me;
00345 
00346   if( meTimeSummaryMapProjEta_ ) meTimeSummaryMapProjEta_->Reset();
00347   if( meTimeSummaryMapProjPhi_ ) meTimeSummaryMapProjPhi_->Reset();
00348 
00349   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00350 
00351     int ism = superModules_[i];
00352 
00353     me = dqmStore_->get( prefixME_ + "/EBTimingTask/EBTMT timing " + Numbers::sEB(ism) );
00354     h01_[ism-1] = UtilsClient::getHisto<TProfile2D*>( me, cloneME_, h01_[ism-1] );
00355     meh01_[ism-1] = me;
00356 
00357     me = dqmStore_->get( prefixME_ + "/EBTimingTask/EBTMT timing vs amplitude " + Numbers::sEB(ism) );
00358     h02_[ism-1] = UtilsClient::getHisto<TH2F*>( me, cloneME_, h02_[ism-1] );
00359     meh02_[ism-1] = me;
00360 
00361     if ( meg01_[ism-1] ) meg01_[ism-1]->Reset();
00362     if ( mea01_[ism-1] ) mea01_[ism-1]->Reset();
00363     if ( mep01_[ism-1] ) mep01_[ism-1]->Reset();
00364     if ( mer01_[ism-1] ) mer01_[ism-1]->Reset();
00365 
00366     for ( int ie = 1; ie <= 85; ie++ ) {
00367       for ( int ip = 1; ip <= 20; ip++ ) {
00368 
00369         if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent(ie, ip, 2.);
00370 
00371         bool update01;
00372 
00373         float num01;
00374         float mean01;
00375         float rms01;
00376 
00377         update01 = UtilsClient::getBinStatistics(h01_[ism-1], ie, ip, num01, mean01, rms01, 3.);
00378         // Task timing map is shifted of +50 ns for graphical reasons. Shift back it.
00379         mean01 -= 50.;
00380 
00381         if ( update01 ) {
00382 
00383           float val;
00384 
00385           val = 1.;
00386           if ( std::abs(mean01 - expectedMean_) > discrepancyMean_ )
00387             val = 0.;
00388           if ( rms01 > RMSThreshold_ )
00389             val = 0.;
00390           if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent(ie, ip, val);
00391 
00392           int ic = Numbers::icEB(ism, ie, ip);
00393 
00394           if ( mea01_[ism-1] ) {
00395             mea01_[ism-1]->setBinContent(ic, mean01);
00396             mea01_[ism-1]->setBinError(ic, rms01);
00397           }
00398           if ( mep01_[ism-1] ) mep01_[ism-1]->Fill(mean01);
00399           if ( mer01_[ism-1] ) mer01_[ism-1]->Fill(rms01);
00400 
00401           float eta, phi;
00402 
00403           if ( ism <= 18 ) {
00404             eta = -ie+0.5;
00405             phi = ip+20*(ism-1)-0.5;
00406           } else {
00407             eta = ie-0.5;
00408             phi = (20-ip)+20*(ism-19)+0.5;
00409           }
00410 
00411           if( meTimeSummaryMapProjEta_ ) meTimeSummaryMapProjEta_->Fill(eta, mean01);
00412           if( meTimeSummaryMapProjPhi_ ) meTimeSummaryMapProjPhi_->Fill(phi, mean01);
00413 
00414         }
00415 
00416         if ( Masks::maskChannel(ism, ie, ip, bits01, EcalBarrel) ) UtilsClient::maskBinContent( meg01_[ism-1], ie, ip );
00417 
00418 
00419 
00420       }
00421     }
00422 
00423   }
00424 
00425 }
00426