CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/CommonTools/TriggerUtils/plugins/CandidateTriggerObjectProducer.cc

Go to the documentation of this file.
00001 #include "FWCore/Common/interface/TriggerNames.h"
00002 #include "FWCore/Common/interface/TriggerResultsByName.h"
00003 #include "CommonTools/TriggerUtils/interface/CandidateTriggerObjectProducer.h"
00004 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00005 #include "DataFormats/Candidate/interface/Candidate.h"
00006 #include "DataFormats/Candidate/interface/LeafCandidate.h"
00007 #include "TString.h"
00008 #include "TRegexp.h"
00009 #include <cassert>
00010 
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 //
00013 // constructors and destructor
00014 //
00015 CandidateTriggerObjectProducer::CandidateTriggerObjectProducer(const edm::ParameterSet& ps) : 
00016   triggerResultsTag_(ps.getParameter<edm::InputTag>("triggerResults")),
00017   triggerEventTag_(ps.getParameter<edm::InputTag>("triggerEvent")),
00018   triggerName_(ps.getParameter<std::string>("triggerName"))
00019 {
00020   using namespace edm;
00021 
00022 //   cout << "Trigger Object Producer:" << endl
00023 //        << "   TriggerResultsTag = " << triggerResultsTag_.encode() << endl
00024 //        << "   TriggerEventTag = " << triggerEventTag_.encode() << endl;
00025 
00026   produces<reco::CandidateCollection>();
00027   
00028 }
00029 
00030 CandidateTriggerObjectProducer::~CandidateTriggerObjectProducer()
00031 {
00032 }
00033 
00034 //
00035 // member functions
00036 //
00037 void
00038 CandidateTriggerObjectProducer::beginRun(edm::Run& iRun, edm::EventSetup const& iSetup)
00039 {
00040   using namespace edm;
00041 
00042   bool changed(false);
00043   if(!hltConfig_.init(iRun,iSetup,triggerEventTag_.process(),changed) ){
00044     edm::LogError( "CandidateTriggerObjectProducer" ) << 
00045       "Error! Can't initialize HLTConfigProvider";
00046     throw cms::Exception("HLTConfigProvider::init() returned non 0");
00047   }
00048 
00049   return;
00050 
00051 }
00052 
00053 // ------------ method called to produce the data  ------------
00054 void
00055 CandidateTriggerObjectProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
00056 {
00057   using namespace edm;
00058   using namespace reco;
00059   using namespace trigger;
00060 
00061   std::auto_ptr<reco::CandidateCollection> coll( new reco::CandidateCollection );
00062 
00063   // get event products
00064   iEvent.getByLabel(triggerResultsTag_,triggerResultsHandle_);
00065   if (!triggerResultsHandle_.isValid()) {
00066      edm::LogError( "CandidateTriggerObjectProducer" ) << "CandidateTriggerObjectProducer::analyze: Error in getting TriggerResults product from Event!" ;
00067     return;
00068   }
00069   iEvent.getByLabel(triggerEventTag_,triggerEventHandle_);
00070   if (!triggerEventHandle_.isValid()) {
00071     edm::LogError( "CandidateTriggerObjectProducer" ) << "CandidateTriggerObjectProducer::analyze: Error in getting TriggerEvent product from Event!" ;
00072     return;
00073   }
00074   // sanity check
00075   //   std::cout << hltConfig_.size() << std::endl;
00076   //   std::cout << triggerResultsHandle_->size() << std::endl;
00077   assert(triggerResultsHandle_->size()==hltConfig_.size());
00078   
00079   const unsigned int n(hltConfig_.size());
00080   std::vector<std::string> activeHLTPathsInThisEvent = hltConfig_.triggerNames();
00081   std::map<std::string, bool> triggerInMenu;
00082   std::map<std::string, bool> triggerUnprescaled;
00083 
00084   for (std::vector<std::string>::const_iterator iHLT = activeHLTPathsInThisEvent.begin(); 
00085        iHLT != activeHLTPathsInThisEvent.end(); ++iHLT) 
00086     {
00087       //matching with regexp filter name. More than 1 matching filter is allowed 
00088       if (TString(*iHLT).Contains(TRegexp(TString(triggerName_))))
00089          {
00090            triggerInMenu[*iHLT] = true;
00091            const std::pair<int,int> prescales(hltConfig_.prescaleValues(iEvent,iSetup,*iHLT));
00092            if (prescales.first * prescales.second == 1) 
00093              triggerUnprescaled[*iHLT] = true;         
00094          }
00095      }
00096 
00097   for (std::map<std::string, bool>::const_iterator iMyHLT = triggerInMenu.begin(); 
00098        iMyHLT != triggerInMenu.end(); ++iMyHLT) 
00099     {
00100       //using only unprescaled triggers
00101       if (!(iMyHLT->second && triggerUnprescaled[iMyHLT->first])) 
00102         continue;
00103       const unsigned int triggerIndex(hltConfig_.triggerIndex(iMyHLT->first));
00104       
00105       assert(triggerIndex==iEvent.triggerNames(*triggerResultsHandle_).triggerIndex(iMyHLT->first));
00106 
00107       // abort on invalid trigger name
00108       if (triggerIndex>=n) {
00109         edm::LogError( "CandidateTriggerObjectProducer" ) << "CandidateTriggerObjectProducer::analyzeTrigger: path "
00110              << triggerName_ << " - not found!" ;
00111         return;
00112       }
00113   
00114       // modules on this trigger path
00115       //      const unsigned int m(hltConfig_.size(triggerIndex));
00116       const std::vector<std::string>& moduleLabels(hltConfig_.saveTagsModules(triggerIndex));
00117 
00118       // Results from TriggerResults product
00119       if (!(triggerResultsHandle_->wasrun(triggerIndex)) ||
00120           !(triggerResultsHandle_->accept(triggerIndex)) ||
00121           (triggerResultsHandle_->error(triggerIndex)))
00122         {
00123           continue;
00124         }
00125 
00126 //       const unsigned int moduleIndex(triggerResultsHandle_->index(triggerIndex));
00127       
00128 //       assert (moduleIndex<m);
00129       
00130       // Results from TriggerEvent product - Looking only on last filter since trigger is accepted
00131       for (unsigned int imodule=0;imodule<moduleLabels.size();++imodule)
00132         {
00133           const std::string& moduleLabel(moduleLabels[imodule]);
00134           const std::string  moduleType(hltConfig_.moduleType(moduleLabel));
00135           //Avoiding L1 seeds
00136           if (moduleType.find("Level1GTSeed") != std::string::npos)
00137             continue;
00138           // check whether the module is packed up in TriggerEvent product
00139           const unsigned int filterIndex(triggerEventHandle_->filterIndex(InputTag(moduleLabel,"",triggerEventTag_.process())));
00140           if (filterIndex<triggerEventHandle_->sizeFilters()) {
00141             //      std::cout << " 'L3' filter in slot " << imodule << " - label/type " << moduleLabel << "/" << moduleType << std::endl;
00142             const Vids& VIDS (triggerEventHandle_->filterIds(filterIndex));
00143             const Keys& KEYS(triggerEventHandle_->filterKeys(filterIndex));
00144             const size_type nI(VIDS.size());
00145             const size_type nK(KEYS.size());
00146             assert(nI==nK);
00147             const size_type n(std::max(nI,nK));
00148             //      std::cout << "   " << n  << " accepted 'L3' objects found: " << std::endl;
00149             const TriggerObjectCollection& TOC(triggerEventHandle_->getObjects());
00150             for (size_type i=0; i!=n; ++i) {
00151               const TriggerObject& TO(TOC[KEYS[i]]);
00152               coll->push_back(reco::LeafCandidate( 0, TO.particle().p4(), reco::Particle::Point( 0., 0., 0. ), TO.id() ));
00153 //            std::cout << "   " << i << " " << VIDS[i] << "/" << KEYS[i] << ": "
00154 //                      << TO.id() << " " << TO.pt() << " " << TO.eta() << " " << TO.phi() << " " << TO.mass()
00155 //                      << std::endl;
00156             }
00157           }
00158         }
00159     }
00160   
00161   
00162   iEvent.put(coll);
00163   return;
00164 }
00165 
00166 #include "FWCore/Framework/interface/MakerMacros.h"
00167 
00168 DEFINE_FWK_MODULE(CandidateTriggerObjectProducer);
00169 
00170