CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQM/EcalPreshowerMonitorModule/src/ESTrendTask.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004 #include <math.h>
00005 
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/Framework/interface/ESHandle.h"
00009 #include "FWCore/Framework/interface/MakerMacros.h"
00010 #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 #include "DataFormats/EcalDetId/interface/ESDetId.h"
00012 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00013 #include "DQMServices/Core/interface/MonitorElement.h"
00014 #include "DQMServices/Core/interface/DQMStore.h"
00015 #include "DQM/EcalPreshowerMonitorModule/interface/ESTrendTask.h"
00016 #include "DataFormats/EcalRawData/interface/ESDCCHeaderBlock.h"
00017 #include "DataFormats/EcalRawData/interface/ESKCHIPBlock.h"
00018 #include "DataFormats/EcalRawData/interface/EcalRawDataCollections.h"
00019 
00020 using namespace cms;
00021 using namespace edm;
00022 using namespace std;
00023 
00024 ESTrendTask::ESTrendTask(const ParameterSet& ps) {
00025 
00026   init_ = false;
00027 
00028   dqmStore_ = Service<DQMStore>().operator->();
00029 
00030   prefixME_       = ps.getUntrackedParameter<string>("prefixME", "EcalPreshower"); 
00031   enableCleanup_  = ps.getUntrackedParameter<bool>("enableCleanup", false);
00032   mergeRuns_      = ps.getUntrackedParameter<bool>("mergeRuns", false);
00033   rechitlabel_    = ps.getParameter<InputTag>("RecHitLabel");
00034   dccCollections_ = ps.getParameter<InputTag>("ESDCCCollections");
00035 
00036   for (int i=0; i<2; ++i)
00037     for (int j=0; j<2; ++j) {
00038       hESRecHitTrend_[i][j] = 0;
00039       hESRecHitTrendHr_[i][j] = 0;
00040     }
00041 
00042   hESSLinkErrTrend_ = 0;
00043   hESFiberErrTrend_ = 0;
00044   hESSLinkErrTrendHr_ = 0;
00045   hESFiberErrTrendHr_ = 0;
00046 
00047   start_time_ = 0;
00048   current_time_ = 0;
00049   last_time_ = 0;
00050 
00051 }
00052 
00053 ESTrendTask::~ESTrendTask() {
00054 }
00055 
00056 void ESTrendTask::beginJob(void) {
00057 
00058   ievt_ = 0;
00059 
00060   if ( dqmStore_ ) {
00061     dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
00062     dqmStore_->rmdir(prefixME_ + "/ESTrendTask");
00063   }
00064 
00065 }
00066 
00067 void ESTrendTask::beginRun(const Run& r, const EventSetup& c) {
00068 
00069   if ( ! mergeRuns_ ) this->reset();
00070 
00071   start_time_ = time(NULL);
00072 
00073   //std::cout << "start time : " << start_time_ << std::endl;
00074 
00075 }
00076 
00077 void ESTrendTask::endRun(const Run& r, const EventSetup& c) {
00078 
00079 }
00080 
00081 void ESTrendTask::reset(void) {
00082 
00083   for (int i=0 ; i<2; ++i)  
00084     for (int j=0 ; j<2; ++j) {
00085       if (hESRecHitTrend_[i][j]) hESRecHitTrend_[i][j]->Reset();
00086       if (hESRecHitTrendHr_[i][j]) hESRecHitTrendHr_[i][j]->Reset();
00087     }
00088 
00089   if (hESSLinkErrTrend_) hESSLinkErrTrend_->Reset();
00090   if (hESFiberErrTrend_) hESFiberErrTrend_->Reset();
00091   if (hESSLinkErrTrendHr_) hESSLinkErrTrendHr_->Reset();
00092   if (hESFiberErrTrendHr_) hESFiberErrTrendHr_->Reset();
00093 
00094 }
00095 
00096 void ESTrendTask::setup(void) {
00097 
00098   init_ = true;
00099 
00100   char histo[200];
00101 
00102   if ( dqmStore_ ) {
00103     dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
00104 
00105     for (int i=0 ; i<2; ++i)  
00106       for (int j=0 ; j<2; ++j) {
00107         int iz = (i==0)? 1:-1;
00108         sprintf(histo, "ES Trending RH Occ per 5 mins Z %d P %d", iz, j+1);
00109         hESRecHitTrend_[i][j] = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
00110         hESRecHitTrend_[i][j]->setAxisTitle("Elapse time (Minutes)", 1);
00111         hESRecHitTrend_[i][j]->setAxisTitle("ES RecHit Occupancy / 5 minutes", 2);
00112 
00113         sprintf(histo, "ES Trending RH Occ per hour Z %d P %d", iz, j+1);
00114         hESRecHitTrendHr_[i][j] = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
00115         hESRecHitTrendHr_[i][j]->setAxisTitle("Elapse time (Hours)", 1);
00116         hESRecHitTrendHr_[i][j]->setAxisTitle("ES RecHit Occupancy / hour", 2);
00117       }
00118 
00119     sprintf(histo, "ES Trending SLink CRC Error per 5 mins");
00120     hESSLinkErrTrend_ = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
00121     hESSLinkErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
00122     hESSLinkErrTrend_->setAxisTitle("ES SLink CRC Err / 5 minutes", 2);
00123 
00124     sprintf(histo, "ES Trending Fiber Error per 5 mins");
00125     hESFiberErrTrend_ = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
00126     hESFiberErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
00127     hESFiberErrTrend_->setAxisTitle("ES Fiber Err / 5 minutes", 2);
00128 
00129     sprintf(histo, "ES Trending SLink CRC Error per hour");
00130     hESSLinkErrTrendHr_ = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
00131     hESSLinkErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
00132     hESSLinkErrTrendHr_->setAxisTitle("ES SLink CRC Err / hour", 2);
00133 
00134     sprintf(histo, "ES Trending Fiber Error per hour");
00135     hESFiberErrTrendHr_ = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
00136     hESFiberErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
00137     hESFiberErrTrendHr_->setAxisTitle("ES Fiber Err / hour", 2);
00138   }
00139 
00140 }
00141 
00142 void ESTrendTask::cleanup(void) {
00143 
00144   if ( ! init_ ) return;
00145 
00146   if ( dqmStore_ ) {
00147     dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
00148 
00149     for (int i=0 ; i<2; ++i)  
00150       for (int j=0 ; j<2; ++j) {
00151         if (hESRecHitTrend_[i][j]) dqmStore_->removeElement(hESRecHitTrend_[i][j]->getName());
00152         hESRecHitTrend_[i][j] = 0;
00153         if (hESRecHitTrendHr_[i][j]) dqmStore_->removeElement(hESRecHitTrendHr_[i][j]->getName());
00154         hESRecHitTrendHr_[i][j] = 0;
00155       }
00156 
00157     if (hESSLinkErrTrend_) dqmStore_->removeElement(hESSLinkErrTrend_->getName());
00158     hESSLinkErrTrend_ = 0;
00159     if (hESFiberErrTrend_) dqmStore_->removeElement(hESFiberErrTrend_->getName());
00160     hESFiberErrTrend_ = 0;
00161     if (hESSLinkErrTrendHr_) dqmStore_->removeElement(hESSLinkErrTrendHr_->getName());
00162     hESSLinkErrTrendHr_ = 0;
00163     if (hESFiberErrTrendHr_) dqmStore_->removeElement(hESFiberErrTrendHr_->getName());
00164     hESFiberErrTrendHr_ = 0;
00165   }
00166 
00167   init_ = false;
00168 
00169 }
00170 
00171 void ESTrendTask::endJob(void) {
00172 
00173   LogInfo("ESTrendTask") << "analyzed " << ievt_ << " events";
00174 
00175   if ( enableCleanup_ ) this->cleanup();
00176 
00177 }
00178 
00179 void ESTrendTask::analyze(const Event& e, const EventSetup& c) {
00180 
00181   if ( ! init_ ) this->setup();
00182 
00183   ievt_++;
00184 
00185   // Collect time information
00186   updateTime();
00187 
00188   long int diff_current_start = current_time_ - start_time_;
00189   long int diff_last_start    = last_time_ - start_time_;
00190   //LogInfo("ESTrendTask") << "time difference is negative in " << ievt_ << " events\n"
00191   //<< "\tcurrent - start time = " << diff_current_start
00192   //<< ", \tlast - start time = " << diff_last_start << endl;
00193 
00194   //  std::cout << "current_time : " << current_time_ << ", diff : " << diff_current_start << std::endl;
00195 
00196   // Calculate time interval and bin width
00197   //  int minuteBinWidth = int(nBasicClusterMinutely_->getTProfile()->GetXaxis()->GetBinWidth(1));
00198   int minuteBinWidth = 5;
00199   long int minuteBinDiff = diff_current_start/60/minuteBinWidth - diff_last_start/60/minuteBinWidth;
00200   long int minuteDiff = (current_time_ - last_time_)/60;
00201 
00202   //  int hourBinWidth = int(nBasicClusterHourly_->getTProfile()->GetXaxis()->GetBinWidth(1));
00203   int hourBinWidth = 1;
00204   long int hourBinDiff = diff_current_start/3600/hourBinWidth - diff_last_start/3600/hourBinWidth;
00205   long int hourDiff = (current_time_ - last_time_)/3600;
00206 
00207   if (minuteDiff >= minuteBinWidth) {
00208     while (minuteDiff >= minuteBinWidth) minuteDiff -= minuteBinWidth;
00209   }
00210   if (hourDiff >= hourBinWidth) {
00211     while (hourDiff >= hourBinWidth) hourDiff -= hourBinWidth;
00212   }
00213 
00214   // ES DCC
00215   Int_t slinkCRCErr = 0;
00216   Int_t fiberErr = 0;
00217   vector<int> fiberStatus;
00218   Handle<ESRawDataCollection> dccs;
00219   if ( e.getByLabel(dccCollections_, dccs) ) {
00220     for (ESRawDataCollection::const_iterator dccItr = dccs->begin(); dccItr != dccs->end(); ++dccItr) {
00221       ESDCCHeaderBlock dcc = (*dccItr);
00222 
00223       if (dcc.getDCCErrors() == 101) slinkCRCErr++;
00224 
00225       fiberStatus = dcc.getFEChannelStatus();
00226       for (unsigned int i=0; i<fiberStatus.size(); ++i) {
00227         if (fiberStatus[i]==4 || fiberStatus[i]==8 || fiberStatus[i]==10 || fiberStatus[i]==11 || fiberStatus[i]==12)
00228           fiberErr++;
00229       }
00230       
00231     }
00232   }
00233 
00234   shift2Right(hESSLinkErrTrend_->getTProfile(), minuteBinDiff);
00235   hESSLinkErrTrend_->Fill(minuteDiff, slinkCRCErr);
00236 
00237   shift2Right(hESFiberErrTrend_->getTProfile(), minuteBinDiff);
00238   hESFiberErrTrend_->Fill(minuteDiff, fiberErr);
00239 
00240   shift2Right(hESSLinkErrTrendHr_->getTProfile(), hourBinDiff);
00241   hESSLinkErrTrendHr_->Fill(hourDiff, slinkCRCErr);
00242 
00243   shift2Right(hESFiberErrTrendHr_->getTProfile(), hourBinDiff);
00244   hESFiberErrTrendHr_->Fill(hourDiff, fiberErr);
00245 
00246   // ES RecHits
00247   int zside, plane, ix, iy, strip;
00248   int nrh[2][2];
00249   for (int i = 0; i < 2; i++ ) 
00250     for( int j = 0; j < 2; j++) {
00251       nrh[i][j] = 0;
00252     }
00253 
00254   Handle<EcalRecHitCollection> ESRecHit;
00255   if ( e.getByLabel(rechitlabel_, ESRecHit) ) {
00256     
00257     for (ESRecHitCollection::const_iterator hitItr = ESRecHit->begin(); hitItr != ESRecHit->end(); ++hitItr) {
00258       
00259       ESDetId id = ESDetId(hitItr->id());
00260       
00261       zside = id.zside();
00262       plane = id.plane();
00263       ix    = id.six();
00264       iy    = id.siy();
00265       strip = id.strip();
00266       
00267       int i = (zside==1)? 0:1;
00268       int j = plane-1;
00269       
00270       nrh[i][j]++;
00271     }
00272   } else {
00273     LogWarning("ESTrendTask") << rechitlabel_ << " not available";
00274   }
00275   
00276   for (int i=0; i<2; ++i) 
00277     for (int j=0; j<2; ++j) {
00278       shift2Right(hESRecHitTrend_[i][j]->getTProfile(), minuteBinDiff);
00279       hESRecHitTrend_[i][j]->Fill(minuteDiff, nrh[i][j]/(1072*32.));
00280 
00281       shift2Right(hESRecHitTrendHr_[i][j]->getTProfile(), hourBinDiff);
00282       hESRecHitTrendHr_[i][j]->Fill(hourDiff, nrh[i][j]/(1072*32.));
00283     }
00284 
00285 }
00286 
00287 void ESTrendTask::updateTime() {
00288 
00289   last_time_ = current_time_;
00290   current_time_ = time(NULL);
00291 
00292 }
00293 
00294 void ESTrendTask::shift2Right(TProfile* p, int bins) {
00295 
00296   if(bins <= 0) return;
00297 
00298   if(!p->GetSumw2()) p->Sumw2();
00299   int nBins = p->GetXaxis()->GetNbins();
00300 
00301   // by shifting n bin to the right, the number of entries are
00302   // reduced by the number in n bins including the overflow bin.
00303   double nentries = p->GetEntries();
00304   for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(nBins+1-bins);
00305   p->SetEntries(nentries);
00306   
00307   // the last bin goes to overflow
00308   // each bin moves to the right
00309 
00310   TArrayD* sumw2 = p->GetSumw2();
00311 
00312   for(int i=nBins+1; i>bins; i--) {
00313     // GetBinContent return binContent/binEntries
00314     p->SetBinContent(i, p->GetBinContent(i-bins)*p->GetBinEntries(i-bins));
00315     p->SetBinEntries(i,p->GetBinEntries(i-bins));
00316     sumw2->SetAt(sumw2->GetAt(i-bins),i);
00317   }
00318 
00319 }
00320 
00321 void ESTrendTask::shift2Left(TProfile* p, int bins) {
00322 
00323   if(bins <= 0) return;
00324 
00325   if(!p->GetSumw2()) p->Sumw2();
00326   int nBins = p->GetXaxis()->GetNbins();
00327 
00328   // by shifting n bin to the left, the number of entries are
00329   // reduced by the number in n bins including the underflow bin.
00330   double nentries = p->GetEntries();
00331   for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(i);
00332   p->SetEntries(nentries);
00333   
00334   // the first bin goes to underflow
00335   // each bin moves to the right
00336 
00337   TArrayD* sumw2 = p->GetSumw2();
00338 
00339   for(int i=0; i<=nBins+1-bins; i++) {
00340     // GetBinContent return binContent/binEntries
00341     p->SetBinContent(i, p->GetBinContent(i+bins)*p->GetBinEntries(i+bins));
00342     p->SetBinEntries(i,p->GetBinEntries(i+bins));
00343     sumw2->SetAt(sumw2->GetAt(i+bins),i);
00344   }
00345 
00346 }
00347 
00348 DEFINE_FWK_MODULE(ESTrendTask);