CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQM/EcalEndcapMonitorClient/src/EETimingClient.cc

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