CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/DQM/EcalEndcapMonitorClient/src/EETimingClient.cc

Go to the documentation of this file.
00001 /*
00002  * \file EETimingClient.cc
00003  *
00004  * $Date: 2011/09/02 14:02:36 $
00005  * $Revision: 1.110 $
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 "DataFormats/EcalDetId/interface/EEDetId.h"
00035 
00036 #include "DQM/EcalEndcapMonitorClient/interface/EETimingClient.h"
00037 
00038 EETimingClient::EETimingClient(const edm::ParameterSet& ps) {
00039 
00040   // cloneME switch
00041   cloneME_ = ps.getUntrackedParameter<bool>("cloneME", true);
00042 
00043   // verbose switch
00044   verbose_ = ps.getUntrackedParameter<bool>("verbose", true);
00045 
00046   // debug switch
00047   debug_ = ps.getUntrackedParameter<bool>("debug", false);
00048 
00049   // prefixME path
00050   prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
00051 
00052   // enableCleanup_ switch
00053   enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup", false);
00054 
00055   // vector of selected Super Modules (Defaults to all 18).
00056   superModules_.reserve(18);
00057   for ( unsigned int i = 1; i <= 18; i++ ) superModules_.push_back(i);
00058   superModules_ = ps.getUntrackedParameter<std::vector<int> >("superModules", superModules_);
00059 
00060   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00061 
00062     int ism = superModules_[i];
00063 
00064     h01_[ism-1] = 0;
00065     h02_[ism-1] = 0;
00066 
00067     meh01_[ism-1] = 0;
00068     meh02_[ism-1] = 0;
00069 
00070   }
00071 
00072   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00073 
00074     int ism = superModules_[i];
00075 
00076     meg01_[ism-1] = 0;
00077 
00078     mea01_[ism-1] = 0;
00079 
00080     mep01_[ism-1] = 0;
00081 
00082     mer01_[ism-1] = 0;
00083 
00084   }
00085 
00086   meTimeSummaryMapProjEta_[0] = 0;
00087   meTimeSummaryMapProjEta_[1] = 0;
00088   meTimeSummaryMapProjPhi_[0] = 0;
00089   meTimeSummaryMapProjPhi_[1] = 0;
00090 
00091   expectedMean_ = 0.0;
00092   discrepancyMean_ = 3.;
00093   RMSThreshold_ = 4.;
00094 
00095 }
00096 
00097 EETimingClient::~EETimingClient() {
00098 
00099 }
00100 
00101 void EETimingClient::beginJob(void) {
00102 
00103   dqmStore_ = edm::Service<DQMStore>().operator->();
00104 
00105   if ( debug_ ) std::cout << "EETimingClient: beginJob" <<  std::endl;
00106 
00107   ievt_ = 0;
00108   jevt_ = 0;
00109 
00110 }
00111 
00112 void EETimingClient::beginRun(void) {
00113 
00114   if ( debug_ ) std::cout << "EETimingClient: beginRun" <<  std::endl;
00115 
00116   jevt_ = 0;
00117 
00118   this->setup();
00119 
00120 }
00121 
00122 void EETimingClient::endJob(void) {
00123 
00124   if ( debug_ ) std::cout << "EETimingClient: endJob, ievt = " << ievt_ <<  std::endl;
00125 
00126   this->cleanup();
00127 
00128 }
00129 
00130 void EETimingClient::endRun(void) {
00131 
00132   if ( debug_ ) std::cout << "EETimingClient: endRun, jevt = " << jevt_ <<  std::endl;
00133 
00134   this->cleanup();
00135 
00136 }
00137 
00138 void EETimingClient::setup(void) {
00139 
00140   std::string name;
00141 
00142   dqmStore_->setCurrentFolder( prefixME_ + "/EETimingClient" );
00143 
00144   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00145 
00146     int ism = superModules_[i];
00147 
00148     if ( meg01_[ism-1] ) dqmStore_->removeElement( meg01_[ism-1]->getName() );
00149     name = "EETMT timing quality " + Numbers::sEE(ism);
00150     meg01_[ism-1] = dqmStore_->book2D(name, name, 50, Numbers::ix0EE(ism)+0., Numbers::ix0EE(ism)+50., 50, Numbers::iy0EE(ism)+0., Numbers::iy0EE(ism)+50.);
00151     meg01_[ism-1]->setAxisTitle("ix", 1);
00152     if ( ism >= 1 && ism <= 9 ) meg01_[ism-1]->setAxisTitle("101-ix", 1);
00153     meg01_[ism-1]->setAxisTitle("iy", 2);
00154 
00155     if ( mea01_[ism-1] ) dqmStore_->removeElement( mea01_[ism-1]->getName() );
00156     name = "EETMT timing " + Numbers::sEE(ism);
00157     mea01_[ism-1] = dqmStore_->book1D(name, name, 850, 0., 850.);
00158     mea01_[ism-1]->setAxisTitle("channel", 1);
00159     mea01_[ism-1]->setAxisTitle("time (ns)", 2);
00160 
00161     if ( mep01_[ism-1] ) dqmStore_->removeElement( mep01_[ism-1]->getName() );
00162     name = "EETMT timing mean " + Numbers::sEE(ism);
00163     mep01_[ism-1] = dqmStore_->book1D(name, name, 100, -25., 25.);
00164     mep01_[ism-1]->setAxisTitle("mean (ns)", 1);
00165 
00166     if ( mer01_[ism-1] ) dqmStore_->removeElement( mer01_[ism-1]->getName() );
00167     name = "EETMT timing rms " + Numbers::sEE(ism);
00168     mer01_[ism-1] = dqmStore_->book1D(name, name, 100, 0.0, 10.);
00169     mer01_[ism-1]->setAxisTitle("rms (ns)", 1);
00170 
00171   }
00172 
00173   name = "EETMT timing projection eta EE -";
00174   meTimeSummaryMapProjEta_[0] = dqmStore_->bookProfile(name, name, 20, -3.0, -1.479, -20., 20.,"");
00175   meTimeSummaryMapProjEta_[0]->setAxisTitle("eta", 1);
00176   meTimeSummaryMapProjEta_[0]->setAxisTitle("time (ns)", 2);
00177 
00178   name = "EETMT timing projection eta EE +";
00179   meTimeSummaryMapProjEta_[1] = dqmStore_->bookProfile(name, name, 20, 1.479, 3.0, -20., 20.,"");
00180   meTimeSummaryMapProjEta_[1]->setAxisTitle("eta", 1);
00181   meTimeSummaryMapProjEta_[1]->setAxisTitle("time (ns)", 2);
00182 
00183   name = "EETMT timing projection phi EE -";
00184   meTimeSummaryMapProjPhi_[0] = dqmStore_->bookProfile(name, name, 50, -M_PI, M_PI, -20., 20.,"");
00185   meTimeSummaryMapProjPhi_[0]->setAxisTitle("phi", 1);
00186   meTimeSummaryMapProjPhi_[0]->setAxisTitle("time (ns)", 2);
00187 
00188   name = "EETMT timing projection phi EE +";
00189   meTimeSummaryMapProjPhi_[1] = dqmStore_->bookProfile(name, name, 50, -M_PI, M_PI, -20., 20.,"");
00190   meTimeSummaryMapProjPhi_[1]->setAxisTitle("phi", 1);
00191   meTimeSummaryMapProjPhi_[1]->setAxisTitle("time (ns)", 2);
00192 
00193   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00194 
00195     int ism = superModules_[i];
00196 
00197     if ( meg01_[ism-1] ) meg01_[ism-1]->Reset();
00198 
00199     for ( int ix = 1; ix <= 50; ix++ ) {
00200       for ( int iy = 1; iy <= 50; iy++ ) {
00201 
00202         if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent( ix, iy, 6. );
00203 
00204         int jx = ix + Numbers::ix0EE(ism);
00205         int jy = iy + Numbers::iy0EE(ism);
00206 
00207         if ( ism >= 1 && ism <= 9 ) jx = 101 - jx;
00208 
00209         if ( Numbers::validEE(ism, jx, jy) ) {
00210           if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent( ix, iy, 2. );
00211         }
00212 
00213       }
00214     }
00215 
00216     if ( mea01_[ism-1] ) mea01_[ism-1]->Reset();
00217     if ( mep01_[ism-1] ) mep01_[ism-1]->Reset();
00218     if ( mer01_[ism-1] ) mer01_[ism-1]->Reset();
00219 
00220   }
00221 
00222 }
00223 
00224 void EETimingClient::cleanup(void) {
00225 
00226   if ( ! enableCleanup_ ) return;
00227 
00228   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00229 
00230     int ism = superModules_[i];
00231 
00232     if ( cloneME_ ) {
00233       if ( h01_[ism-1] ) delete h01_[ism-1];
00234       if ( h02_[ism-1] ) delete h02_[ism-1];
00235     }
00236 
00237     h01_[ism-1] = 0;
00238     h02_[ism-1] = 0;
00239 
00240     meh01_[ism-1] = 0;
00241     meh02_[ism-1] = 0;
00242 
00243   }
00244 
00245   dqmStore_->setCurrentFolder( prefixME_ + "/EETimingClient" );
00246 
00247   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00248 
00249     int ism = superModules_[i];
00250 
00251     if ( meg01_[ism-1] ) dqmStore_->removeElement( meg01_[ism-1]->getName() );
00252     meg01_[ism-1] = 0;
00253 
00254     if ( mea01_[ism-1] ) dqmStore_->removeElement( mea01_[ism-1]->getName() );
00255     mea01_[ism-1] = 0;
00256 
00257     if ( mep01_[ism-1] ) dqmStore_->removeElement( mep01_[ism-1]->getName() );
00258     mep01_[ism-1] = 0;
00259 
00260     if ( mer01_[ism-1] ) dqmStore_->removeElement( mer01_[ism-1]->getName() );
00261     mer01_[ism-1] = 0;
00262 
00263   }
00264 
00265   for(int i=0; i<2; i++){
00266     if ( meTimeSummaryMapProjEta_[i] ) dqmStore_->removeElement( meTimeSummaryMapProjEta_[i]->getName() );
00267     meTimeSummaryMapProjEta_[i] = 0;
00268 
00269     if ( meTimeSummaryMapProjPhi_[i] ) dqmStore_->removeElement( meTimeSummaryMapProjPhi_[i]->getName() );
00270     meTimeSummaryMapProjPhi_[i] = 0;
00271   }
00272 
00273 }
00274 
00275 #ifdef WITH_ECAL_COND_DB
00276 bool EETimingClient::writeDb(EcalCondDBInterface* econn, RunIOV* runiov, MonRunIOV* moniov, bool& status) {
00277 
00278   status = true;
00279 
00280   EcalLogicID ecid;
00281 
00282   MonTimingCrystalDat t;
00283   std::map<EcalLogicID, MonTimingCrystalDat> dataset;
00284 
00285   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00286 
00287     int ism = superModules_[i];
00288 
00289     if ( verbose_ ) {
00290       std::cout << " " << Numbers::sEE(ism) << " (ism=" << ism << ")" <<  std::endl;
00291       std::cout <<  std::endl;
00292       UtilsClient::printBadChannels(meg01_[ism-1], h01_[ism-1]);
00293     }
00294 
00295     float num01;
00296     float mean01;
00297     float rms01;
00298 
00299     for ( int ix = 1; ix <= 50; ix++ ) {
00300       for ( int iy = 1; iy <= 50; iy++ ) {
00301 
00302         int jx = ix + Numbers::ix0EE(ism);
00303         int jy = iy + Numbers::iy0EE(ism);
00304 
00305         if ( ism >= 1 && ism <= 9 ) jx = 101 - jx;
00306 
00307         if ( ! Numbers::validEE(ism, jx, jy) ) continue;
00308 
00309         bool update01;
00310 
00311         update01 = UtilsClient::getBinStatistics(h01_[ism-1], ix, iy, num01, mean01, rms01);
00312         // Task timing map is shifted of +50 ns for graphical reasons. Shift back it.
00313         mean01 -= 50.;
00314 
00315         if ( update01 ) {
00316 
00317           if ( Numbers::icEE(ism, jx, jy) == 1 ) {
00318 
00319             if ( verbose_ ) {
00320               std::cout << "Preparing dataset for " << Numbers::sEE(ism) << " (ism=" << ism << ")" <<  std::endl;
00321               std::cout << "crystal (" << Numbers::ix0EE(i+1)+ix << "," << Numbers::iy0EE(i+1)+iy << ") " << num01  << " " << mean01 << " " << rms01  <<  std::endl;
00322               std::cout <<  std::endl;
00323             }
00324 
00325           }
00326 
00327           t.setTimingMean(mean01);
00328           t.setTimingRMS(rms01);
00329 
00330           if ( UtilsClient::getBinStatus(meg01_[ism-1], ix, iy) ) {
00331             t.setTaskStatus(true);
00332           } else {
00333             t.setTaskStatus(false);
00334           }
00335 
00336           status = status && UtilsClient::getBinQuality(meg01_[ism-1], ix, iy);
00337 
00338           int ic = Numbers::indexEE(ism, jx, jy);
00339 
00340           if ( ic == -1 ) continue;
00341 
00342           if ( econn ) {
00343             ecid = LogicID::getEcalLogicID("EE_crystal_number", Numbers::iSM(ism, EcalEndcap), ic);
00344             dataset[ecid] = t;
00345           }
00346 
00347         }
00348 
00349       }
00350     }
00351 
00352   }
00353 
00354   if ( econn ) {
00355     try {
00356       if ( verbose_ ) std::cout << "Inserting MonTimingCrystalDat ..." <<  std::endl;
00357       if ( dataset.size() != 0 ) econn->insertDataArraySet(&dataset, moniov);
00358       if ( verbose_ ) std::cout << "done." <<  std::endl;
00359     } catch (std::runtime_error &e) {
00360       std::cerr << e.what() <<  std::endl;
00361     }
00362   }
00363 
00364   return true;
00365 
00366 }
00367 #endif
00368 
00369 void EETimingClient::analyze(void) {
00370 
00371   ievt_++;
00372   jevt_++;
00373   if ( ievt_ % 10 == 0 ) {
00374     if ( debug_ ) std::cout << "EETimingClient: ievt/jevt = " << ievt_ << "/" << jevt_ <<  std::endl;
00375   }
00376 
00377   uint32_t bits01 = 0;
00378   bits01 |= 1 << EcalDQMStatusHelper::PHYSICS_BAD_CHANNEL_WARNING;
00379 
00380   MonitorElement* me;
00381 
00382   if( meTimeSummaryMapProjEta_[0] && meTimeSummaryMapProjEta_[1] && meTimeSummaryMapProjPhi_[0] && meTimeSummaryMapProjPhi_[1] ){
00383 
00384     for(int iz=0; iz<2; iz++){
00385       int zside = -1 + iz * 2;
00386       me = dqmStore_->get(prefixME_ + "/EETimingTask/EETMT timing map EE " + (zside==0 ? "-" : "+"));
00387       TProfile2D *hmap = 0;
00388       if( me ) hmap = (TProfile2D *)me->getRootObject();
00389       if( hmap ){
00390 
00391         int nx = hmap->GetNbinsX();
00392         int ny = hmap->GetNbinsY();
00393 
00394         for(int jx=1; jx<=nx; jx++){
00395           for(int jy=1; jy<=ny; jy++){
00396 
00397             int ix = (jx-1)*5 + 1;
00398             int iy = (jy-1)*5 + 1;
00399             if( !EEDetId::validDetId(ix, iy, zside) ) continue;
00400 
00401             EEDetId id(ix, iy, zside);
00402 
00403             float yval = hmap->GetBinContent(jx,jy) - 50.;
00404 
00405             meTimeSummaryMapProjEta_[iz]->Fill(Numbers::eta(id), yval);
00406             meTimeSummaryMapProjPhi_[iz]->Fill(Numbers::phi(id), yval);
00407           }
00408         }
00409       }
00410     }
00411 
00412   }
00413 
00414   for(int i=0; i<2; i++){
00415     if( meTimeSummaryMapProjEta_[i] ) meTimeSummaryMapProjEta_[i]->Reset();
00416     if( meTimeSummaryMapProjPhi_[i] ) meTimeSummaryMapProjPhi_[i]->Reset();
00417   }
00418 
00419   for ( unsigned int i=0; i<superModules_.size(); i++ ) {
00420 
00421     int ism = superModules_[i];
00422 
00423     me = dqmStore_->get( prefixME_ + "/EETimingTask/EETMT timing " + Numbers::sEE(ism) );
00424     h01_[ism-1] = UtilsClient::getHisto( me, cloneME_, h01_[ism-1] );
00425     meh01_[ism-1] = me;
00426 
00427     me = dqmStore_->get( prefixME_ + "/EETimingTask/EETMT timing vs amplitude " + Numbers::sEE(ism) );
00428     h02_[ism-1] = UtilsClient::getHisto( me, cloneME_, h02_[ism-1] );
00429     meh02_[ism-1] = me;
00430 
00431     if ( meg01_[ism-1] ) meg01_[ism-1]->Reset();
00432     if ( mea01_[ism-1] ) mea01_[ism-1]->Reset();
00433     if ( mep01_[ism-1] ) mep01_[ism-1]->Reset();
00434     if ( mer01_[ism-1] ) mer01_[ism-1]->Reset();
00435 
00436     int iz = ism / 10;
00437 
00438     for ( int ix = 1; ix <= 50; ix++ ) {
00439       for ( int iy = 1; iy <= 50; iy++ ) {
00440 
00441         if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent(ix, iy, 6.);
00442 
00443         int jx = ix + Numbers::ix0EE(ism);
00444         int jy = iy + Numbers::iy0EE(ism);
00445 
00446         if ( ism >= 1 && ism <= 9 ) jx = 101 - jx;
00447 
00448         if ( Numbers::validEE(ism, jx, jy) ) {
00449           if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent( ix, iy, 2. );
00450         }
00451 
00452         bool update01;
00453 
00454         float num01;
00455         float mean01;
00456         float rms01;
00457 
00458         update01 = UtilsClient::getBinStatistics(h01_[ism-1], ix, iy, num01, mean01, rms01, 3.);
00459         // Task timing map is shifted of +50 ns for graphical reasons. Shift back it.
00460         mean01 -= 50.;
00461 
00462         if ( update01 ) {
00463 
00464           float val;
00465 
00466           val = 1.;
00467           if ( std::abs(mean01 - expectedMean_) > discrepancyMean_ )
00468             val = 0.;
00469           if ( rms01 > RMSThreshold_ )
00470             val = 0.;
00471           if ( meg01_[ism-1] ) meg01_[ism-1]->setBinContent(ix, iy, val);
00472 
00473           int ic = Numbers::icEE(ism, jx, jy);
00474 
00475           if ( ic != -1 ) {
00476             if ( mea01_[ism-1] ) {
00477               mea01_[ism-1]->setBinContent(ic, mean01);
00478               mea01_[ism-1]->setBinError(ic, rms01);
00479             }
00480 
00481             if ( mep01_[ism-1] ) mep01_[ism-1]->Fill(mean01);
00482             if ( mer01_[ism-1] ) mer01_[ism-1]->Fill(rms01);
00483           }
00484 
00485           EEDetId id(jx, jy, -1 + iz * 2);
00486 
00487           if( meTimeSummaryMapProjEta_[iz] ) meTimeSummaryMapProjEta_[iz]->Fill(Numbers::eta(id), mean01);
00488           if( meTimeSummaryMapProjPhi_[iz] ) meTimeSummaryMapProjPhi_[iz]->Fill(Numbers::phi(id), mean01);
00489 
00490         }
00491 
00492         if ( Masks::maskChannel(ism, ix, iy, bits01, EcalEndcap) ) UtilsClient::maskBinContent( meg01_[ism-1], ix, iy );
00493 
00494       }
00495     }
00496 
00497   }
00498 
00499 }
00500