00001
00002
00003
00004
00005 #include "DQM/HLTEvF/interface/HLTMonBitSummary.h"
00006
00007 #include "FWCore/Framework/interface/Frameworkfwd.h"
00008 #include "FWCore/Framework/interface/EDAnalyzer.h"
00009
00010 #include "FWCore/Framework/interface/Event.h"
00011 #include "FWCore/Framework/interface/MakerMacros.h"
00012 #include "FWCore/Framework/interface/ESHandle.h"
00013
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015
00016 #include "FWCore/Common/interface/TriggerNames.h"
00017 #include "DataFormats/Common/interface/TriggerResults.h"
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 #include "FWCore/Framework/interface/Run.h"
00020
00021
00022
00023 #include "FWCore/ServiceRegistry/interface/Service.h"
00024 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00025 #include "TH1F.h"
00026 #include "TProfile.h"
00027 #include "TH2F.h"
00028
00029 #include "TPRegexp.h"
00030 #include "TString.h"
00031
00032 #include "DQMServices/Core/interface/DQMStore.h"
00033 #include "DQMServices/Core/interface/MonitorElement.h"
00034
00035
00036 #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
00037 #include "CondFormats/DataRecord/interface/AlCaRecoTriggerBitsRcd.h"
00038
00039
00040 HLTMonBitSummary::HLTMonBitSummary(const edm::ParameterSet& iConfig) :
00041 inputTag_ (iConfig.getParameter<edm::InputTag> ("TriggerResultsTag")),
00042 HLTPathNamesConfigPreVal_(iConfig.getParameter<std::vector<std::string > >("HLTPaths")),
00043 total_(0),
00044 nValidTriggers_(0),
00045 nValidConfigTriggers_(0),
00046 ndenomAccept_(0)
00047 {
00048 denominatorWild_ = iConfig.getUntrackedParameter<std::string>("denominatorWild","");
00049 denominator_ = iConfig.getUntrackedParameter<std::string>("denominator");
00050 directory_ = iConfig.getUntrackedParameter<std::string>("directory","HLT/Generic/Summary");
00051 histLabel_ = iConfig.getUntrackedParameter<std::string>("histLabel","Generic");
00052
00053 dummyFilters_.clear();
00054 filterTypes_ = iConfig.getUntrackedParameter<std::vector<std::string > >("filterTypes",dummyFilters_);
00055 esPathsKey_ = iConfig.getUntrackedParameter<std::string>("eventSetupPathsKey","");
00056
00057 configFlag_ = false;
00058 filterFlag_ = false;
00059 hltchange_ = false;
00060
00061 dbe_ = NULL;
00062 dbe_ = edm::Service < DQMStore > ().operator->();
00063 dbe_->setVerbose(0);
00064
00065 }
00066
00067
00068 HLTMonBitSummary::~HLTMonBitSummary(){}
00069
00070
00071
00072
00073
00074 void HLTMonBitSummary::beginRun(const edm::Run & r, const edm::EventSetup &iSetup){
00075
00076
00077 std::string processName = inputTag_.process();
00078 if (processName != ""){
00079
00080
00081 if (esPathsKey_.size()) {
00082
00083 edm::ESHandle<AlCaRecoTriggerBits> triggerBits;
00084 iSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
00085 typedef std::map<std::string, std::string> TriggerMap;
00086 const TriggerMap &triggerMap = triggerBits->m_alcarecoToTrig;
00087
00088 TriggerMap::const_iterator listIter = triggerMap.find(esPathsKey_);
00089
00090 if (listIter == triggerMap.end()) {
00091 throw cms::Exception("Configuration")
00092
00093
00094 <<" No triggerList with key " << esPathsKey_ << " in AlCaRecoTriggerBitsRcd";
00095 }
00096
00097
00098
00099 HLTPathNamesKey_ = triggerBits->decompose(listIter->second);
00100 }
00101
00102 bool initOK = false;
00103 initOK = hltConfig_.init(r, iSetup, processName, hltchange_);
00104
00105 HLTPathNamesConfig_.clear();
00106 if(HLTPathNamesConfigPreVal_.size()){
00107
00108 if (initOK) {
00109 configSelector(HLTPathNamesConfigPreVal_, HLTPathNamesConfig_);
00110 }
00111 else{
00112 edm::LogError("HLTMonBitSummary") << "HLTConfigProvider initialization with process name "
00113 << processName << " failed." << std::endl;
00114 }
00115 }
00116
00117
00118 std::vector<int> removePaths;
00119 for(size_t i=0; i<HLTPathNamesKey_.size(); ++i){
00120 for(size_t j=0; j<HLTPathNamesConfig_.size(); ++j){
00121 if(HLTPathNamesConfig_[j] == HLTPathNamesKey_[i]) removePaths.push_back(i);
00122 }
00123 }
00124 reverse(removePaths.begin(),removePaths.end());
00125 if(removePaths.size()){
00126 for(unsigned int k=0; k<removePaths.size(); ++k)
00127 HLTPathNamesKey_.erase(HLTPathNamesKey_.begin()+removePaths[k]);
00128 }
00129
00130
00131
00132 HLTPathsByName_.clear();
00133 HLTPathsByName_.reserve(HLTPathNamesConfig_.size() + HLTPathNamesKey_.size());
00134 HLTPathsByName_.insert(HLTPathsByName_.end(),HLTPathNamesConfig_.begin(),HLTPathNamesConfig_.end());
00135 HLTPathsByName_.insert(HLTPathsByName_.end(),HLTPathNamesKey_.begin(),HLTPathNamesKey_.end());
00136
00137 count_.resize(HLTPathsByName_.size());
00138 HLTPathsByIndex_.resize(HLTPathsByName_.size());
00139
00140
00141 if( (nValidTriggers_ != HLTPathsByName_.size() || hltchange_) && total_!=0 ){
00142 edm::LogWarning("HLTMonBitSummary") << "A change in the HLT configuration has been found."
00143 << std::endl
00144 << "BitSummary histograms do not support changing configurations."
00145 << std::endl
00146 << "Processing of events halted.";
00147 configFlag_ = true;
00148 }
00149
00150 if(!configFlag_){
00151
00152 nValidTriggers_ = HLTPathsByName_.size();
00153 nValidConfigTriggers_ = HLTPathNamesConfig_.size();
00154
00155
00156
00157 if(initOK){
00158 if(!filterTypes_.empty()){
00159 triggerFilters_.clear();
00160 triggerFilterIndices_.clear();
00161 for( size_t i = 0; i < nValidConfigTriggers_; i++) {
00162
00163 triggerFilters_.push_back(std::vector <std::string>());
00164
00165 triggerFilterIndices_.push_back(std::vector <uint>());
00166
00167 std::vector<std::string> moduleNames = hltConfig_.moduleLabels( HLTPathNamesConfig_[i] );
00168
00169 triggerFilters_[i].push_back(HLTPathNamesConfig_[i]);
00170 triggerFilterIndices_[i].push_back(0);
00171
00172 int numModule = 0, numFilters = 0;
00173 std::string moduleName, moduleType;
00174 unsigned int moduleIndex;
00175
00176
00177 std::vector<std::string>::const_iterator iDumpModName;
00178 for (iDumpModName = moduleNames.begin();iDumpModName != moduleNames.end();iDumpModName++) {
00179 moduleName = *iDumpModName;
00180 moduleType = hltConfig_.moduleType(moduleName);
00181 moduleIndex = hltConfig_.moduleIndex(HLTPathNamesConfig_[i], moduleName);
00182 LogDebug ("HLTMonBitSummary") << "Module" << numModule
00183 << " is called " << moduleName
00184 << " , type = " << moduleType
00185 << " , index = " << moduleIndex
00186 << std::endl;
00187 numModule++;
00188 for(size_t k = 0; k < filterTypes_.size(); k++) {
00189 if(moduleType == filterTypes_[k]) {
00190 numFilters++;
00191 triggerFilters_[i].push_back(moduleName);
00192 triggerFilterIndices_[i].push_back(moduleIndex);
00193 }
00194 }
00195 }
00196 }
00197 }
00198 }
00199 else{
00200 edm::LogError("HLTMonBitSummary") << "HLTConfigProvider initialization has failed."
00201 << " Could not get filter names." << std::endl;
00202 filterFlag_ = true;
00203 }
00204
00205
00206 HLTPathDenomName_.clear();
00207 HLTPathDenomNamePreVal_.clear();
00208 if( denominatorWild_.size() != 0 ) HLTPathDenomNamePreVal_.push_back(denominatorWild_);
00209 if(initOK){
00210 configSelector(HLTPathDenomNamePreVal_, HLTPathDenomName_);
00211 }
00212
00213
00214
00215
00216 if(HLTPathDenomName_.size()==1) denominator_ = HLTPathDenomName_[0];
00217 }
00218 }
00219
00220
00221 if(dbe_ && !configFlag_){
00222
00223 int nbin = nValidConfigTriggers_;
00224
00225 dbe_->setCurrentFolder(directory_);
00226
00227
00228 int nbin_sub = 8;
00229
00230
00231 if(!filterTypes_.empty() && !filterFlag_){
00232 dbe_->setCurrentFolder(directory_ + "Trigger_Filters/");
00233
00234
00235 hSubFilterCount.clear();
00236 hSubFilterEff.clear();
00237
00238 for( int trig = 0; trig < nbin; trig++ ) {
00239
00240
00241 hSubFilterCount.push_back(dbe_->book1D("Filters_" + triggerFilters_[trig][0],
00242 "Filters_" + triggerFilters_[trig][0],
00243 nbin_sub+1, -0.5, 0.5+(double)nbin_sub));
00244
00245 for(int filt = 0; filt < (int)triggerFilters_[trig].size()-1; filt++){
00246 hSubFilterCount[trig]->setBinLabel(filt+1, triggerFilters_[trig][filt+1]);
00247 }
00248 }
00249
00250 dbe_->setCurrentFolder(directory_ + "Trigger_Efficiencies/");
00251 for( int trig = 0; trig < nbin; trig++ ) {
00252
00253 hSubFilterEff.push_back(dbe_->book1D("Efficiency_" + triggerFilters_[trig][0],
00254 "Efficiency_" + triggerFilters_[trig][0],
00255 nbin_sub+1, -0.5, 0.5+(double)nbin_sub));
00256
00257 for(int filt = 0; filt < (int)triggerFilters_[trig].size()-1; filt++){
00258 hSubFilterEff[trig]->setBinLabel(filt+1,triggerFilters_[trig][filt+1]);
00259 }
00260 }
00261 }
00262
00263
00264
00265
00266 std::stringstream rNs;
00267 rNs<<r.run();
00268 std::string rN = rNs.str();
00269 LogDebug("HLTMonBitSummary")<<"this is the beginning of a NEW run: "<< r.run();
00270
00271
00272
00273
00274
00275
00276 for (uint i=0; i < nValidTriggers_ && i < 400 ; ++i){
00277 h1_->getTH1F()->GetXaxis()->SetBinLabel(i+1,HLTPathsByName_[i].c_str());
00278 h2_->getTH2F()->GetXaxis()->SetBinLabel(i+1,HLTPathsByName_[i].c_str());
00279 h2_->getTH2F()->GetYaxis()->SetBinLabel(i+1,HLTPathsByName_[i].c_str());
00280 pf_->getTH1F()->GetXaxis()->SetBinLabel(i+1,HLTPathsByName_[i].c_str());
00281 if (ratio_)
00282 ratio_->getTH1F()->GetXaxis()->SetBinLabel(i+1,(HLTPathsByName_[i]+" & "+denominator_).c_str());
00283 }
00284
00285
00286 }
00287
00288 }
00289
00290
00291
00292 void
00293 HLTMonBitSummary::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00294 {
00295 if(configFlag_) return;
00296
00297 total_++;
00298 const std::string invalid("@@invalid@@");
00299
00300
00301 edm::Handle<edm::TriggerResults> trh;
00302 iEvent.getByLabel(inputTag_,trh);
00303
00304 if (trh.failedToGet()) {
00305 edm::LogError("HLTMonBitSummary")<<" could not get: "<<inputTag_;
00306 return;
00307 }
00308
00309
00310 const edm::TriggerNames & triggerNames = iEvent.triggerNames(*trh);
00311
00312 unsigned int lastModule = 0;
00313
00314
00315
00316
00317
00318
00319 for (unsigned int trig=0; trig < nValidTriggers_; trig++) {
00320
00321 HLTPathsByIndex_[trig]=triggerNames.triggerIndex(HLTPathsByName_[trig]);
00322 }
00323
00324
00325 for (unsigned int trig=0; trig < nValidConfigTriggers_; trig++) {
00326
00327
00328
00329
00330 if(!filterTypes_.empty() && !filterFlag_ && HLTPathsByIndex_[trig] < trh->size()) {
00331 lastModule = trh->index(HLTPathsByIndex_[trig]);
00332
00333
00334 for(unsigned int filt = 0; filt < triggerFilters_[trig].size()-1; filt++){
00335
00336
00337
00338
00339 int binNumber = hSubFilterCount[trig]->getTH1F()->GetXaxis()->FindBin(triggerFilters_[trig][filt+1].c_str());
00340
00341
00342 if(trh->accept(HLTPathsByIndex_[trig])){
00343 hSubFilterCount[trig]->Fill(binNumber-1);
00344 }
00345
00346
00347 else if(triggerFilterIndices_[trig][filt+1] < lastModule){
00348 hSubFilterCount[trig]->Fill(binNumber-1);
00349 }
00350
00351
00352
00353 float eff = (float)hSubFilterCount[trig]->getBinContent(binNumber) / (float)total_ ;
00354 float efferr = sqrt(eff*(1-eff)/ (float)total_);
00355 hSubFilterEff[trig]->setBinContent(binNumber,eff);
00356 hSubFilterEff[trig]->setBinError(binNumber,efferr);
00357
00358 }
00359 }
00360 }
00361
00362
00363 std::vector<bool> validity(nValidTriggers_);
00364 for (unsigned int i=0; i!=nValidTriggers_; i++) {
00365 validity[i]=( (HLTPathsByIndex_[i]<trh->size()) && (HLTPathsByName_[i]!=invalid) );
00366 }
00367
00368
00369
00370
00371
00372
00373 uint denominatorIndex = 0;
00374 bool denominatorValidity= false;
00375 if (denominator_!="") {
00376 denominatorIndex=triggerNames.triggerIndex(denominator_);
00377 denominatorValidity= (denominatorIndex <trh->size());
00378 }
00379
00380 std::stringstream report;
00381 std::string sep=" ";
00382 bool atLeastOne=false;
00383
00384
00385 bool denomAccept=false;
00386 if (ratio_ && denominatorValidity) {
00387 denomAccept=trh->accept(denominatorIndex);
00388 if(denomAccept) ndenomAccept_++;
00389 }
00390
00391 for (unsigned int i=0; i!=nValidTriggers_; i++) {
00392 if (!validity[i]) continue;
00393 bool iAccept=trh->accept(HLTPathsByIndex_[i]);
00394 if (iAccept) {
00395 report<<sep<<HLTPathsByName_[i];
00396 count_[i]++;
00397 sep=", ";
00398 atLeastOne=true;
00399
00400 h1_->Fill(i);
00401
00402 for (unsigned int j=i; j!=nValidTriggers_; j++) {
00403 if (!validity[j]) continue;
00404 if (trh->accept(HLTPathsByIndex_[j]))
00405 h2_->Fill(i,j);
00406 }
00407 }
00408
00409 float pf_eff = (float)h1_->getBinContent(i+1) / (float)total_ ;
00410 float pf_efferr = sqrt(pf_eff*(1-pf_eff) / (float)total_);
00411 pf_->setBinContent(i+1,pf_eff);
00412 pf_->setBinError(i+1,pf_efferr);
00413
00414
00415 if (ratio_ && denomAccept){
00416 float ratio_eff = (float)h1_->getBinContent(i+1) / (float)ndenomAccept_ ;
00417 float ratio_efferr = sqrt(ratio_eff*(1-ratio_eff) / (float)ndenomAccept_);
00418 ratio_->setBinContent(i+1,ratio_eff);
00419 ratio_->setBinError(i+1,ratio_efferr);
00420 }
00421
00422
00423
00424 for (unsigned int j=0; j!=i; j++) {
00425 if (!validity[j]) continue;
00426 bool jAccept=trh->accept(HLTPathsByIndex_[j]);
00427 if (iAccept || jAccept)
00428 h2_->Fill(i,j);
00429 }
00430
00431 }
00432
00433 if (atLeastOne){
00434 LogDebug("BitPlotting|BitReport")<<report.str();
00435 }
00436
00437
00438
00439
00440
00441 }
00442 void HLTMonBitSummary::beginJob() {
00443
00444 if(dbe_){
00445 if (directory_ != "" && directory_.substr(directory_.length()-1,1) != "/" ) directory_ = directory_+"/" ;
00446
00447 float min = -0.5;
00448 float max = 400-0.5;
00449
00450 uint nBin = 400;
00451
00452 dbe_->setCurrentFolder(directory_);
00453
00454 h1_ = dbe_->book1D("PassingBits_Summary_"+histLabel_,"PassingBits_Summary_"+histLabel_, nBin, min, max);
00455 h2_ = dbe_->book2D("PassingBits_Correlation_"+histLabel_,"PassingBits_Correlation_"+histLabel_,nBin,min,max, nBin,min,max);
00456 pf_ = dbe_->book1D("Efficiency_Summary_"+histLabel_,"Efficiency_Summary_"+histLabel_, nBin, min, max);
00457 if (denominator_!="")
00458
00459 ratio_ = dbe_->book1D("HLTRate_"+histLabel_,"HLTRate_"+histLabel_,nBin,min,max);
00460 else
00461 ratio_=0;
00462
00463 }
00464
00465 }
00466
00467
00468 void HLTMonBitSummary::endJob() {
00469
00470 std::stringstream report;
00471 report <<" out of: "<<total_<<" events.\n";
00472 if(!count_.empty()){
00473 for (uint i=0; i!=HLTPathsByName_.size();i++){
00474 report<<HLTPathsByName_[i]<<" passed: "<<count_[i]<<" times.\n";
00475 count_[i]=0;
00476 }
00477 }
00478
00479 edm::LogInfo("HLTMonBitSummary|BitSummary")<<report.str();
00480 LogDebug("HLTMonBitSummary|BitSummary")<<report.str();
00481 total_=0;
00482
00483
00484 }
00485
00486
00487 void HLTMonBitSummary::configSelector(std::vector<std::string> selectTriggers, std::vector<std::string > & theSelectTriggers){
00488
00489
00490 std::vector<std::string> validTriggerNames = hltConfig_.triggerNames();
00491
00492 bool goodToGo = false;
00493
00494 while(!goodToGo && selectTriggers.size()!=0){
00495 goodToGo=true;
00496 for (std::vector<std::string>::iterator j=selectTriggers.begin();j!=selectTriggers.end();++j){
00497
00498
00499
00500 TPRegexp wildCard(*j);
00501
00502 for (unsigned int i = 0; i != validTriggerNames.size(); ++i){
00503 if (TString(validTriggerNames[i]).Contains(wildCard)){
00504
00505 if (find(theSelectTriggers.begin(),
00506 theSelectTriggers.end(),
00507 validTriggerNames[i])==theSelectTriggers.end()){
00508
00509 theSelectTriggers.push_back( validTriggerNames[i] );
00510 }
00511 }
00512 }
00513 }
00514 }
00515 }