CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/DQM/TrigXMonitor/src/HLTScalers.cc

Go to the documentation of this file.
00001 // $Id: HLTScalers.cc,v 1.31 2011/04/01 09:47:00 rekovic Exp $
00002 // 
00003 // $Log: HLTScalers.cc,v $
00004 // Revision 1.31  2011/04/01 09:47:00  rekovic
00005 // Check if stream A exists before quering for its PD content
00006 //
00007 // Revision 1.30  2011/03/30 21:44:03  fwyzard
00008 // make sure HLTConfigProvider is used only if succesfully initialized
00009 //
00010 // Revision 1.29  2011/03/30 21:35:40  fwyzard
00011 // make sure all members are initialized
00012 //
00013 // Revision 1.28  2011/03/29 09:46:03  rekovic
00014 // clean vector pairPDPaths in beginRun and tidy up
00015 //
00016 // Revision 1.27  2011/03/24 18:35:38  rekovic
00017 // Change name for pd histo
00018 //
00019 // Revision 1.26  2011/03/24 18:25:45  rekovic
00020 // Add single 1D plot of streamA content
00021 //
00022 // Revision 1.25  2010/07/20 02:58:27  wmtan
00023 // Add missing #include files
00024 //
00025 // Revision 1.24  2010/03/17 20:54:51  wittich
00026 // add scalers that I manually reset on beginLumi
00027 //
00028 // Revision 1.23  2010/02/25 17:34:01  wdd
00029 // Central migration of TriggerNames class interface
00030 //
00031 // Revision 1.22  2010/02/24 17:43:47  wittich
00032 // - keep trying to get path names if it doesn't work first time
00033 // - move the Bx histograms out of raw to the toplevel directory.
00034 //
00035 // Revision 1.21  2010/02/11 23:54:28  wittich
00036 // modify how the monitoring histo is filled
00037 //
00038 // Revision 1.20  2010/02/11 00:11:08  wmtan
00039 // Adapt to moved framework header
00040 //
00041 // Revision 1.19  2010/02/02 13:53:05  wittich
00042 // fix duplicate histogram name
00043 //
00044 // Revision 1.18  2010/02/02 11:42:53  wittich
00045 // new diagnostic histograms
00046 //
00047 // Revision 1.17  2009/11/20 00:39:12  lorenzo
00048 // fixes
00049 //
00050 // Revision 1.16  2008/09/03 13:59:06  wittich
00051 // make HLT DQM path configurable via python parameter,
00052 // which defaults to HLT/HLTScalers_EvF
00053 //
00054 // Revision 1.15  2008/09/03 02:13:47  wittich
00055 // - bug fix in L1Scalers
00056 // - configurable dqm directory in L1SCalers
00057 // - other minor tweaks in HLTScalers
00058 //
00059 
00060 #include <iostream>
00061 
00062 
00063 // FW
00064 #include "FWCore/Framework/interface/Event.h"
00065 #include "FWCore/Framework/interface/Run.h"
00066 
00067 #include "FWCore/ServiceRegistry/interface/Service.h"
00068 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00069 
00070 #include "FWCore/Framework/interface/LuminosityBlock.h"
00071 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00072 
00073 // HLT
00074 #include "DataFormats/Common/interface/TriggerResults.h"
00075 #include "DataFormats/Common/interface/HLTenums.h"
00076 #include "FWCore/Common/interface/TriggerNames.h"
00077 
00078 
00079 #include "DQM/TrigXMonitor/interface/HLTScalers.h"
00080 #include "DataFormats/Common/interface/Handle.h"
00081 #include "DQMServices/Core/interface/DQMStore.h"
00082 #include "DQMServices/Core/interface/MonitorElement.h"
00083 
00084 using namespace edm;
00085 
00086 
00087 
00088 HLTScalers::HLTScalers(const edm::ParameterSet &ps):
00089   folderName_(ps.getUntrackedParameter<std::string>("dqmFolder", "HLT/HLTScalers_EvF")),
00090   processname_(ps.getParameter<std::string>("processname")),
00091   pairPDPaths_(),
00092   trigResultsSource_(ps.getParameter<edm::InputTag>("triggerResults")),
00093   dbe_(0),
00094   scalersN_(0),
00095   scalersException_(0),
00096   hltCorrelations_(0),
00097   detailedScalers_(0), 
00098   nProc_(0),
00099   nLumiBlock_(0),
00100   hltBx_(0),
00101   hltBxVsPath_(0),
00102   hltOverallScaler_(0),
00103   hltOverallScalerN_(0),
00104   diagnostic_(0),
00105   resetMe_(true),
00106   sentPaths_(false),
00107   monitorDaemon_(ps.getUntrackedParameter<bool>("MonitorDaemon", false)),
00108   nev_(0), 
00109   nLumi_(0),
00110   currentRun_(-1)
00111 {
00112   LogDebug("HLTScalers") << "HLTScalers: constructor...." ;
00113 
00114   dbe_ = Service<DQMStore>().operator->();
00115   if (dbe_ ) {
00116     dbe_->setVerbose(0);
00117     dbe_->setCurrentFolder(folderName_);
00118   }
00119 }
00120 
00121 
00122 void HLTScalers::beginJob(void)
00123 {
00124   LogDebug("HLTScalers") << "HLTScalers::beginJob()..." << std::endl;
00125 
00126   if (dbe_) {
00127     std::string rawdir(folderName_ + "/raw");
00128     dbe_->setCurrentFolder(rawdir);
00129 
00130     nProc_      = dbe_->bookInt("nProcessed");
00131     nLumiBlock_ = dbe_->bookInt("nLumiBlock");
00132     diagnostic_ = dbe_->book1D("hltMerge", "HLT merging diagnostic", 
00133                                1, 0.5, 1.5);
00134 
00135     // fill for ever accepted event 
00136     hltOverallScaler_  = dbe_->book1D("hltOverallScaler", "HLT Overall Scaler", 
00137                                       1, 0.5, 1.5);
00138     hltOverallScalerN_ = dbe_->book1D("hltOverallScalerN", 
00139                                       "Reset HLT Overall Scaler", 1, 0.5, 1.5);
00140     
00141     // other ME's are now found on the first event of the new run, 
00142     // when we know more about the HLT configuration.
00143   }
00144 }
00145 
00146 void HLTScalers::analyze(const edm::Event &e, const edm::EventSetup &c)
00147 {
00148   nProc_->Fill(++nev_);
00149   diagnostic_->setBinContent(1,1); // this ME is never touched - 
00150                                    // it just tells you how the merging is doing.
00151 
00152   edm::Handle<TriggerResults> hltResults;
00153   bool b = e.getByLabel(trigResultsSource_, hltResults);
00154   if ( !b ) {
00155     edm::LogInfo("HLTScalers") << "getByLabel for TriggerResults failed"
00156                                << " with label " << trigResultsSource_;
00157     return;
00158   }
00159   
00160   
00161   int npath = hltResults->size();
00162   unsigned int nPD = pairPDPaths_.size();
00163 
00164   // on the first event of a new run we book new ME's
00165   if (resetMe_ ) {
00166     LogInfo("HLTScalers") << "analyze(): new run. dump path for this evt " 
00167                           << e.id() << ", \n"
00168                           << *hltResults ;
00169 
00170     if (not dbe_)
00171       return;
00172 
00173     // need to get maxModules dynamically
00174     int maxModules = 200;
00175 
00176     std::string rawdir(folderName_ + "/raw");
00177     dbe_->setCurrentFolder(rawdir);
00178 
00179     scalersPD_          = dbe_->book1D("pdScalers", "PD scalers (stream A)",
00180                                        nPD, -0.5, nPD-0.5);
00181     detailedScalers_    = dbe_->book2D("detailedHltScalers", "HLT Scalers", 
00182                                        npath, -0.5, npath-0.5,
00183                                        maxModules, 0, maxModules-1);
00184     scalers_            = dbe_->book1D("hltScalers", "HLT scalers",
00185                                        npath, -0.5, npath-0.5);
00186     scalersN_           = dbe_->book1D("hltScalersN", "Reset HLT scalers",
00187                                        npath, -0.5, npath-0.5);
00188     scalersException_   = dbe_->book1D("hltExceptions", "HLT Exception scalers",
00189                                        npath, -0.5, npath-0.5);
00190     hltCorrelations_    = dbe_->book2D("hltCorrelations", "HLT Scalers", 
00191                                        npath, -0.5, npath-0.5,
00192                                        npath, -0.5, npath-0.5);
00193 
00194     // these two belong in top-level
00195     dbe_->setCurrentFolder(folderName_);
00196     hltBxVsPath_        = dbe_->book2D("hltBxVsPath", "HLT Accept vs Bunch Number", 
00197                                        3600, -0.5, 3599.5,
00198                                        npath, -0.5, npath-0.5);
00199     hltBx_              = dbe_->book1D("hltBx", "Bx of HLT Accepted Events ", 
00200                                        3600, -0.5, 3599.5);
00201 
00202     resetMe_ = false;
00203   } // end resetMe_ - pseudo-end-run record
00204 
00205   const edm::TriggerNames & trigNames = e.triggerNames(*hltResults);
00206   // for some reason this doesn't appear to work on the first event sometimes
00207   if ( ! sentPaths_ ) {
00208     const edm::TriggerNames & names = e.triggerNames(*hltResults);
00209 
00210     // save path names in DQM-accessible format
00211     int q = 0;
00212     for ( TriggerNames::Strings::const_iterator 
00213           j = names.triggerNames().begin();
00214           j !=names.triggerNames().end(); ++j ) {
00215       
00216       LogDebug("HLTScalers") << q << ": " << *j ;
00217       ++q;
00218       scalers_->getTH1()->GetXaxis()->SetBinLabel(q, j->c_str());
00219     }
00220 
00221     for (unsigned int i = 0; i < nPD; i++) {
00222       LogDebug("HLTScalers") << i << ": " << pairPDPaths_[i].first << std::endl ;
00223       scalersPD_->getTH1()->GetXaxis()->SetBinLabel(i+1, pairPDPaths_[i].first.c_str());
00224     }
00225 
00226     sentPaths_ = true;
00227   }
00228       
00229   bool accept = false;
00230   int bx = e.bunchCrossing();
00231   for ( int i = 0; i < npath; ++i ) {
00232     // state returns 0 on ready, 1 on accept, 2 on fail, 3 on exception.
00233     // these are defined in HLTEnums.h
00234     for ( unsigned int j = 0; j < hltResults->index(i); ++j ) {
00235       detailedScalers_->Fill(i,j);
00236     }
00237     if ( hltResults->state(i) == hlt::Pass) {
00238       scalers_->Fill(i);
00239       scalersN_->Fill(i);
00240       hltBxVsPath_->Fill(bx, i);
00241       accept = true;
00242       for ( int j = i + 1; j < npath; ++j ) {
00243         if ( hltResults->state(j) == hlt::Pass) {
00244           hltCorrelations_->Fill(i,j); // fill 
00245           hltCorrelations_->Fill(j,i);
00246         }
00247       }
00248     }
00249     else if ( hltResults->state(i) == hlt::Exception) {
00250       scalersException_->Fill(i);
00251     }
00252   }
00253   if ( accept ) {
00254     hltOverallScaler_->Fill(1.0);
00255     hltOverallScalerN_->Fill(1.0);
00256     hltBx_->Fill(int(bx));
00257   }
00258 
00259   bool anyGroupPassed = false;
00260   for (unsigned int mi = 0; mi < pairPDPaths_.size(); mi++) {
00261 
00262     bool groupPassed = false;
00263 
00264     for (unsigned int i = 0; i < pairPDPaths_[mi].second.size(); i++)
00265     { 
00266 
00267       //string hltPathName =  hist_2d->GetXaxis()->GetBinLabel(i);
00268       std::string hltPathName =  pairPDPaths_[mi].second[i];
00269 
00270       // check if this is hlt path name
00271       //unsigned int pathByIndex = triggerNames.triggerIndex(hltPathName);
00272       unsigned int pathByIndex = trigNames.triggerIndex(pairPDPaths_[mi].second[i]);
00273       if(pathByIndex >= hltResults->size() ) continue;
00274 
00275       // check if its L1 passed
00276       // comment out below but set groupL1Passed to true always
00277       //if(hasL1Passed(hltPathName,triggerNames)) groupL1Passed = true;
00278       //groupL1Passed = true;
00279 
00280       // Fill HLTPassed Matrix and HLTPassFail Matrix
00281       // --------------------------------------------------------
00282 
00283       if(hltResults->accept(pathByIndex)) {
00284         groupPassed = true; 
00285         break;
00286      }
00287 
00288     }
00289 
00290     if(groupPassed) {
00291       scalersPD_->Fill(mi);
00292       anyGroupPassed = true;
00293     }
00294 
00295   }
00296 
00297   if(anyGroupPassed) scalersPD_->Fill(pairPDPaths_.size()-1);
00298 }
00299 
00300 void HLTScalers::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00301                                       const edm::EventSetup& c)
00302 {
00303   LogDebug("HLTScalers") << "Start of luminosity block." ;
00304   // reset the N guys
00305   if ( scalersN_ ) 
00306     scalersN_->Reset();
00307   if ( hltOverallScalerN_ )
00308     hltOverallScalerN_->Reset();
00309 }
00310 
00311 
00312 void HLTScalers::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00313                                     const edm::EventSetup& c)
00314 {
00315   // put this in as a first-pass for figuring out the rate
00316   // each lumi block is 23 seconds in length
00317   nLumiBlock_->Fill(lumiSeg.id().luminosityBlock());
00318  
00319   LogDebug("HLTScalers") << "End of luminosity block." ;
00320 
00321 }
00322 
00323 
00325 void HLTScalers::beginRun(const edm::Run& run, const edm::EventSetup& c)
00326 {
00327   LogDebug("HLTScalers") << "HLTScalers::beginRun, run "
00328                          << run.id();
00329   if ( currentRun_ != int(run.id().run()) ) {
00330     resetMe_ = true;
00331     currentRun_ = run.id().run();
00332   }
00333 
00334   // HLT config does not change within runs!
00335   bool changed=false;
00336 
00337   // clear vector pairPDPaths_
00338   pairPDPaths_.clear();
00339 
00340   if (not hltConfig_.init(run, c, processname_, changed)) {
00341     edm::LogError("TrigXMonitor") << "HLTConfigProvider failed to initialize.";
00342   } else {
00343 
00344     // check if trigger name in (new) config
00345     //  cout << "Available TriggerNames are: " << endl;
00346     //  hltConfig_.dump("Triggers");
00347 
00348     if (hltConfig_.streamIndex("A")<hltConfig_.streamNames().size()) {
00349 
00350       // get hold of PD names and constituent path names
00351       const std::vector<std::string> & PD = hltConfig_.streamContent("A") ;
00352 
00353       for (unsigned int i = 0; i < PD.size(); i++) {
00354 
00355         const std::vector<std::string> & datasetPaths = hltConfig_.datasetContent(PD[i]);
00356         pairPDPaths_.push_back(make_pair(PD[i], datasetPaths));
00357 
00358       }
00359 
00360       // push stream A and its PDs
00361       pairPDPaths_.push_back(make_pair("A", PD));
00362 
00363     } else {
00364 
00365       LogDebug("HLTScalers") << "HLTScalers::beginRun, steamm A not in the HLT menu ";
00366 
00367     }
00368 
00369   }
00370 }
00371 
00373 void HLTScalers::endRun(const edm::Run& run, const edm::EventSetup& c)
00374 {
00375   LogDebug("HLTScalers") << "HLTScalers::endRun , run "
00376                      << run.id();
00377   if ( currentRun_ != int(run.id().run()) ) {
00378     resetMe_ = true;
00379     currentRun_ = run.id().run();
00380   }
00381 }