CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQMOffline/Trigger/src/DQMOfflineMuonTrigAnalyzer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     OfflineDQMMuonTrigAnalyzer
00004 // Class:       OfflineDQMMuonTrigAnalyzer
00005 // 
00013 //
00014 // Jason Slaunwhite, based on code from Jeff Klukas
00015 // $Id: DQMOfflineMuonTrigAnalyzer.cc,v 1.16 2010/07/21 04:23:22 wmtan Exp $
00016 //
00017 //
00018 
00019 // system include files
00020 #include <memory>
00021 
00022 // user include files
00023 #include "FWCore/Framework/interface/Frameworkfwd.h"
00024 #include "FWCore/Framework/interface/EDAnalyzer.h"
00025 #include "FWCore/Framework/interface/Event.h"
00026 #include "FWCore/Framework/interface/MakerMacros.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00029 
00030 
00031 
00032 #include "FWCore/Framework/interface/Run.h"
00033 #include "FWCore/Framework/interface/EventSetup.h"
00034 
00035 
00036 #include "DQMOffline/Trigger/interface/HLTMuonMatchAndPlot.h"
00037 #include "DQMOffline/Trigger/interface/HLTMuonOverlap.h"
00038 
00039 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00040 #include "FWCore/ServiceRegistry/interface/Service.h"
00041 //#include "CommonTools/Utilities/interface/StringCutObjectSelector.h"
00042 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00043 
00044 #include "TFile.h"
00045 #include "TDirectory.h"
00046 #include "TPRegexp.h"
00047 
00048 
00049 using namespace std;
00050 using namespace edm;
00051 using reco::Muon;
00052 
00053 
00054 class OfflineDQMMuonTrigAnalyzer : public edm::EDAnalyzer {
00055 
00056 public:
00057   explicit OfflineDQMMuonTrigAnalyzer(const edm::ParameterSet&);
00058   ~OfflineDQMMuonTrigAnalyzer();
00059 
00060 private:
00061   virtual void beginJob() ;
00062 
00063   virtual void beginRun(edm::Run const& currentRun, edm::EventSetup const& currentEventSetup);
00064   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00065   virtual void endRun (const edm::Run& r, const edm::EventSetup& c);
00066   virtual void endJob() ;
00067 
00068   int theNumberOfTriggers;
00069   std::vector<HLTMuonMatchAndPlot*> theTriggerAnalyzers;
00070   //HLTMuonOverlap *theOverlapAnalyzer;
00071 
00072   bool weHaveProcessedTheFirstRun;
00073   bool useDQMStore;
00074 
00075   vector<string> triggerNames;
00076   string theHltProcessName;
00077   vector<edm::ParameterSet> customCollection;
00078 
00079   const ParameterSet& initialPset;
00080   
00081   vector<TPRegexp> trigRegExps;
00082 
00083 
00084 };
00085 
00086 
00087 
00088 OfflineDQMMuonTrigAnalyzer::OfflineDQMMuonTrigAnalyzer(const ParameterSet& pset):
00089   initialPset(pset)
00090 {
00091 
00092   LogTrace ("HLTMuonVal") << "\n\n Inside MuonTriggerRate Constructor\n\n";
00093   
00094   triggerNames = initialPset.getParameter< vector<string> >
00095                                 ("TriggerRegExpStrings");
00096 
00097   
00098   
00099   for (vector<string>::const_iterator iTrigName = triggerNames.begin();
00100        iTrigName != triggerNames.end();
00101        iTrigName ++) {
00102 
00103     trigRegExps.push_back( TPRegexp((*iTrigName).c_str()) );
00104     
00105     LogTrace ("HLTMuonVal") << "Trigger Reg Exp = " << trigRegExps.back().GetPattern() << endl;
00106   }
00107 
00108   useDQMStore = initialPset.getUntrackedParameter<bool>("DQMStore", false);
00109        
00110   
00111   theHltProcessName = initialPset.getParameter<string>("HltProcessName");
00112 
00113   //string defRecoLabel = initialPset.getUntrackedParameter<string>("RecoLabel","");
00114   //string highPtTracksLabel =  initialPset.getParameter <string> ("highPtTrackCollection");
00115 
00116   //vector<string> recoCollectionNames = initialPset.getParameter < vector<string> > ("allCollectionNames");
00117 
00118   
00119   // make analyzers for each collection. Push the collections into a vector
00120   //vector <string> recoCollectionNames;
00121   //if (defRecoLabel != "") recoCollectionNames.push_back(defRecoLabel);
00122   //if (highPtTracksLabel != "") recoCollectionNames.push_back(highPtTracksLabel);
00123 
00124   customCollection = initialPset.getParameter<vector<edm::ParameterSet> > ("customCollection");
00125 
00126   weHaveProcessedTheFirstRun = false;
00127 
00128   
00129 }
00130 
00131 void OfflineDQMMuonTrigAnalyzer::beginRun(edm::Run const& currentRun, edm::EventSetup const& currentEventSetup) {
00132 
00133 
00134   if (!weHaveProcessedTheFirstRun) {
00135     weHaveProcessedTheFirstRun= true;
00136     vector<edm::ParameterSet>::iterator iPSet;
00137 
00138     LogTrace ("HLTMuonVal") << "customCollection is a vector of size = " << customCollection.size() << std::endl
00139                             << "looping over entries" << std::endl;
00140 
00141     
00142     DQMStore * dbe_ = 0;
00143     if ( useDQMStore ) {
00144       dbe_ = Service<DQMStore>().operator->();
00145       dbe_->setVerbose(0);    
00146     }
00147 
00148 
00149     vector < MuonSelectionStruct > customSelectors;
00150     vector < string > customNames;
00151     // Print out information about each initialPset
00152     for ( iPSet = customCollection.begin();
00153           iPSet != customCollection.end();
00154           iPSet++) {
00155       string customCuts  = iPSet->getUntrackedParameter<string> ("recoCuts");
00156       string customName = iPSet->getUntrackedParameter<string> ("collectionName");
00157       string hltCuts   = iPSet->getUntrackedParameter<string> ("hltCuts");
00158       string targetTrackCollection = iPSet->getUntrackedParameter<string> ("trackCollection");
00159       double  customD0Cut = iPSet->getUntrackedParameter<double> ("d0cut");
00160       double customZ0Cut = iPSet->getUntrackedParameter<double> ("z0cut");
00161 
00162       vector<string> requiredTriggers = iPSet->getUntrackedParameter< vector<string> > ("requiredTriggers");
00163     
00164       LogTrace("HLTMuonVal") << "customTargetCollection = " << customName  << std::endl
00165                              << "customCuts = " << customCuts << std::endl
00166                              << "targetTrackCollection = " << targetTrackCollection << std::endl
00167                              << "d0 cut = " << customD0Cut << std::endl
00168                              << "z0 cut = " << customZ0Cut << std:: endl ;
00169 
00170       if (dbe_) {
00171 
00172         string description = customName + ", reco cuts = " + customCuts
00173           + ", hlt cuts = " + hltCuts + ", trackCollection = " + targetTrackCollection
00174           + ", required triggers, ";
00175 
00176         // add the required triggers
00177         for (vector <string>::const_iterator trigit = requiredTriggers.begin();
00178              trigit != requiredTriggers.end();
00179              trigit++){
00180           description  += (*trigit) + ", ";
00181         }
00182 
00183         // Add the other cuts
00184         ostringstream ossd0, ossz0, osschi2, osshits;
00185 
00186       
00187         ossd0 << customD0Cut;
00188         ossz0 << customZ0Cut;
00189         //osschi2 << customChi2Cut;
00190         //osshits << customNHitsCut;
00191 
00192         description += "|d0| < "  + ossd0.str() + ", |z0| < " + ossz0.str();
00193         
00194 
00195         LogTrace ("HLTMuonVal") << "Storing description = " << description << endl;
00196 
00197         dbe_->setCurrentFolder("HLT/Muon/Distributions/");
00198 
00199         dbe_->bookString (customName, description);            
00200 
00201       }
00202 
00203 
00204       StringCutObjectSelector<Muon> tempRecoSelector(customCuts);
00205       StringCutObjectSelector<trigger::TriggerObject> tempHltSelector(hltCuts);
00206     
00207       // create a custom selector
00208       MuonSelectionStruct tempStruct(tempRecoSelector, tempHltSelector,
00209                                      customName, customD0Cut, customZ0Cut,
00210                                      targetTrackCollection, requiredTriggers);
00211 
00212     
00213       customNames.push_back ( customName);
00214       customSelectors.push_back(tempStruct);
00215     }
00216 
00217   
00218   
00219     LogTrace ("HLTMuonVal") << "Initializing HLTConfigProvider with HLT process name: " << theHltProcessName << endl;
00220 
00221 
00222     // Be careful. If hltConfig provider doesn't initialize sucessfully,
00223     // then you can get in trouble.
00224 
00225     HLTConfigProvider hltConfig;
00226     
00227     bool hltConfigChanged;
00228     bool hltConfigInitSuccess = hltConfig.init(currentRun, currentEventSetup, theHltProcessName, hltConfigChanged);
00229 
00230     
00231     vector<string> validTriggerNames;
00232 
00233     if (hltConfigInitSuccess)
00234       validTriggerNames = hltConfig.triggerNames();
00235 
00236     if (validTriggerNames.size() < 1) {
00237       LogInfo ("HLTMuonVal") << endl << endl << endl
00238                              << "---> WARNING: The HLT Config Provider gave you an empty list of valid trigger names" << endl
00239                              << "Could be a problem with the HLT Process Name (you provided  " << theHltProcessName <<")" << endl
00240                              << "W/o valid triggers we can't produce plots, exiting..."
00241                              << endl << endl << endl;
00242     
00243       // don't return... you'll automatically skip the rest
00244       //return;
00245     }
00246 
00247     vector<string>::const_iterator iDumpName;
00248     unsigned int numTriggers = 0;
00249   
00250     vector<string> parsedMuonTrigNames;
00251 
00252     // declare a bunch of trigger name patterns that you'd like to match
00253     // and do the matching
00254     // the end of line anchor removes matches to
00255     // multi-object triggers
00256     TPRegexp l1l2MuTrigExp   ("HLT_L[12]Mu[^_]*$");
00257     TPRegexp isoMuTrigExp    ("HLT_[iI]soMu[^_]*$");
00258     TPRegexp normalMuExp     ("HLT_Mu[^_]*$");
00259     TPRegexp l1l2DoubleExp   ("HLT_L[12]DoubleMu[^_]*$");
00260     TPRegexp normalDoubleExp ("HLT_DoubleMu[^_]*$");
00261 
00262 
00263   
00264   
00265     for (iDumpName = validTriggerNames.begin();
00266          iDumpName != validTriggerNames.end();
00267          iDumpName++) {
00268 
00269       LogTrace ("HLTMuonVal") << "Trigger " << numTriggers
00270                               << " is called " << (*iDumpName)
00271                               << endl;
00272       numTriggers++;
00273 
00274       TString tempTStringModName(*iDumpName);
00275 
00276       bool matchesAnyRegexp = false;
00277       for (vector<TPRegexp>::const_iterator iTrigRegExp = trigRegExps.begin();
00278            iTrigRegExp != trigRegExps.end();
00279            iTrigRegExp ++) {
00280         TPRegexp tempRegExp((*iTrigRegExp));
00281         if (tempTStringModName.Contains(tempRegExp))
00282           matchesAnyRegexp = true;
00283       }
00284 
00285       if (matchesAnyRegexp) {
00286 
00287         LogTrace ("HLTMuonVal") << "------> This is a muon trigger"
00288                                 << endl;
00289 
00290         parsedMuonTrigNames.push_back(*iDumpName);
00291 
00292       }
00293     
00294     }
00295 
00296     // loop over each combination of selection + trigger
00297     // make a match and plot analyzer for each combination
00298 
00299     vector<MuonSelectionStruct>::iterator iMuonSelector;
00300     vector<string>::iterator iName = customNames.begin();
00301 
00302     LogTrace ("HLTMuonVal") << "Looping over custom selectors" << endl;
00303     unsigned numSelectors = 0;
00304     for ( iMuonSelector = customSelectors.begin();
00305           iMuonSelector != customSelectors.end();
00306           iMuonSelector++) {
00307 
00308       LogTrace ("HLTMuonVal") << "Num selectors  =  " << numSelectors << endl;
00309     
00310       for( size_t i = 0; i < parsedMuonTrigNames.size(); i++) {
00311       
00312         LogTrace ("HLTMuonVal") << "PARSED: Making match and plot for trigger " << parsedMuonTrigNames[i] << endl;
00313       
00314         vector<string> moduleNames = hltConfig.moduleLabels( parsedMuonTrigNames[i] );
00315         HLTMuonMatchAndPlot *analyzer;
00316         analyzer = new HLTMuonMatchAndPlot( initialPset, parsedMuonTrigNames[i], moduleNames, (*iMuonSelector), (*iName), validTriggerNames, currentRun, currentEventSetup );
00317         theTriggerAnalyzers.push_back( analyzer );
00318       
00319       }
00320       numSelectors++;
00321       iName++;
00322     }
00323     //theOverlapAnalyzer = new HLTMuonOverlap( initialPset );    
00324     theNumberOfTriggers = theTriggerAnalyzers.size();
00325 
00326     LogTrace ("HLTMuonVal") << "You have created " << theNumberOfTriggers
00327                             << " trigger analyzers" << endl;
00328 
00329 
00331 
00332     
00333   vector<HLTMuonMatchAndPlot *>::iterator thisAnalyzer;
00334   int nTotalAnalyzers = 0;
00335   for ( thisAnalyzer  = theTriggerAnalyzers.begin(); 
00336         thisAnalyzer != theTriggerAnalyzers.end(); 
00337         ++thisAnalyzer )
00338     {
00339 
00340       LogTrace ("HLTMuonVal")
00341         << "Calling begin for analyzer " << nTotalAnalyzers;
00342        
00343       (*thisAnalyzer)->begin();
00344 
00345       
00346       nTotalAnalyzers++;
00347     }
00348 
00349   LogTrace ("HLTMuonVal")
00350     << "OfflineDQMMuonTrigAnalyzer: Calling being for overlap analyzer" << endl;
00351 
00352     
00353   }// end if this is the first run
00354 }
00355 
00356 OfflineDQMMuonTrigAnalyzer::~OfflineDQMMuonTrigAnalyzer()
00357 {
00358 
00359   LogTrace ("HLTMuonVal")
00360     << "Inside OfflineDQMMuonTrigAnalyzer destructor" << endl;
00361   
00362   vector<HLTMuonMatchAndPlot *>::iterator thisAnalyzer;
00363   for ( thisAnalyzer  = theTriggerAnalyzers.begin(); 
00364         thisAnalyzer != theTriggerAnalyzers.end(); 
00365         ++thisAnalyzer )
00366   {
00367     delete *thisAnalyzer;
00368   } 
00369   theTriggerAnalyzers.clear();
00370   //delete theOverlapAnalyzer;
00371 }
00372 
00373 
00374 //
00375 // member functions
00376 //
00377 
00378 void
00379 OfflineDQMMuonTrigAnalyzer::analyze(const Event& iEvent, const EventSetup& iSetup)
00380 {
00381   LogTrace ("HLTMuonVal")
00382     << "Inside OfflineDQMMuonTrigAnalyzer analyze" << endl;
00383   
00384   vector<HLTMuonMatchAndPlot *>::iterator thisAnalyzer;
00385   for ( thisAnalyzer  = theTriggerAnalyzers.begin(); 
00386         thisAnalyzer != theTriggerAnalyzers.end(); ++thisAnalyzer )
00387     {
00388       (*thisAnalyzer)->analyze(iEvent);
00389     } 
00390   //theOverlapAnalyzer ->analyze(iEvent);
00391 }
00392 
00393 
00394 
00395 void 
00396 OfflineDQMMuonTrigAnalyzer::beginJob()
00397 {
00398 
00399   LogTrace ("HLTMuonVal")
00400     << "Inside OfflineDQMMuonTrigAnalyzer beginJob" << endl;
00401 
00402   LogTrace ("HLTMuonVal")
00403     << "Looking at a vector of analyzers, with size " << theTriggerAnalyzers.size() << endl;
00404 
00405 
00406   
00407   //theOverlapAnalyzer ->begin();
00408 }
00409 
00410 void 
00411 OfflineDQMMuonTrigAnalyzer::endRun ( const edm::Run& theRun, const edm::EventSetup& theEventSetup) {
00412 
00413   LogTrace ("HLTMuonVal")
00414     << "Inside OfflineDQMMuonTrigAnalyzer endRun()" << endl;
00415   
00416   
00417   vector<HLTMuonMatchAndPlot *>::iterator thisAnalyzer;
00418   for ( thisAnalyzer  = theTriggerAnalyzers.begin(); 
00419         thisAnalyzer != theTriggerAnalyzers.end(); 
00420         ++thisAnalyzer )
00421     {
00422       (*thisAnalyzer)->endRun(theRun, theEventSetup);
00423     }
00424 
00425 }
00426 
00427 
00428 
00429 void 
00430 OfflineDQMMuonTrigAnalyzer::endJob() {
00431 
00432   LogTrace ("HLTMuonVal")
00433     << "Inside OfflineDQMMuonTrigAnalyzer endJob()" << endl;
00434   
00435   
00436   vector<HLTMuonMatchAndPlot *>::iterator thisAnalyzer;
00437   for ( thisAnalyzer  = theTriggerAnalyzers.begin(); 
00438         thisAnalyzer != theTriggerAnalyzers.end(); 
00439         ++thisAnalyzer )
00440     {
00441       (*thisAnalyzer)->finish();
00442     }
00443   //theOverlapAnalyzer ->finish();
00444 }
00445 
00446 //define this as a plug-in
00447 DEFINE_FWK_MODULE(OfflineDQMMuonTrigAnalyzer);