CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/PatAlgos/plugins/PATTriggerEventProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATTriggerEventProducer.cc,v 1.15 2011/02/22 18:29:50 vadler Exp $
00003 //
00004 
00005 
00006 #include "PhysicsTools/PatAlgos/plugins/PATTriggerEventProducer.h"
00007 
00008 #include <cassert>
00009 
00010 #include "DataFormats/Common/interface/TriggerResults.h"
00011 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
00012 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
00013 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
00014 #include "CondFormats/DataRecord/interface/L1GtTriggerMenuRcd.h"
00015 #include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
00016 #include "DataFormats/PatCandidates/interface/TriggerEvent.h"
00017 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00018 #include "FWCore/ParameterSet/interface/Registry.h"
00019 
00020 #include "DataFormats/Common/interface/AssociativeIterator.h"
00021 #include "FWCore/Framework/interface/ESHandle.h"
00022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00023 
00024 
00025 using namespace pat;
00026 using namespace edm;
00027 
00028 
00029 PATTriggerEventProducer::PATTriggerEventProducer( const ParameterSet & iConfig ) :
00030   nameProcess_( iConfig.getParameter< std::string >( "processName" ) ),
00031   autoProcessName_( nameProcess_ == "*" ),
00032   tagTriggerProducer_( "patTrigger" ),
00033   tagsTriggerMatcher_(),
00034   // L1 configuration parameters
00035   tagL1Gt_(),
00036   // HLT configuration parameters
00037   tagTriggerResults_( "TriggerResults" ),
00038   tagTriggerEvent_( "hltTriggerSummaryAOD" ),
00039   // Conditions configuration parameters
00040   tagCondGt_()
00041 {
00042 
00043   if ( iConfig.exists( "triggerResults" ) )     tagTriggerResults_  = iConfig.getParameter< InputTag >( "triggerResults" );
00044   if ( iConfig.exists( "triggerEvent" ) )       tagTriggerEvent_    = iConfig.getParameter< InputTag >( "triggerEvent" );
00045   if ( iConfig.exists( "patTriggerProducer" ) ) tagTriggerProducer_ = iConfig.getParameter< InputTag >( "patTriggerProducer" );
00046   if ( iConfig.exists( "condGtTag" ) )          tagCondGt_          = iConfig.getParameter< InputTag >( "condGtTag" );
00047   if ( iConfig.exists( "l1GtTag" ) )            tagL1Gt_            = iConfig.getParameter< InputTag >( "l1GtTag" );
00048   if ( iConfig.exists( "patTriggerMatches" ) )  tagsTriggerMatcher_ = iConfig.getParameter< std::vector< InputTag > >( "patTriggerMatches" );
00049 
00050   for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
00051     produces< TriggerObjectMatch >( tagsTriggerMatcher_.at( iMatch ).label() );
00052   }
00053   produces< TriggerEvent >();
00054 
00055 }
00056 
00057 
00058 void PATTriggerEventProducer::beginRun( Run & iRun, const EventSetup & iSetup )
00059 {
00060 
00061   // Initialize process name
00062   if ( autoProcessName_ ) {
00063     // reset
00064     nameProcess_ = "*";
00065     // determine process name from last run TriggerSummaryProducerAOD module in process history of input
00066     const ProcessHistory & processHistory( iRun.processHistory() );
00067     ProcessConfiguration processConfiguration;
00068     ParameterSet processPSet;
00069     // unbroken loop, which relies on time ordering (accepts the last found entry)
00070     for ( ProcessHistory::const_iterator iHist = processHistory.begin(); iHist != processHistory.end(); ++iHist ) {
00071       if ( processHistory.getConfigurationForProcess( iHist->processName(), processConfiguration )     &&
00072            pset::Registry::instance()->getMapped( processConfiguration.parameterSetID(), processPSet ) &&
00073            processPSet.exists( tagTriggerEvent_.label() )
00074          ) {
00075         nameProcess_ = iHist->processName();
00076         LogDebug( "autoProcessName" ) << "HLT process name '" << nameProcess_ << "' discovered";
00077       }
00078     }
00079     // terminate, if nothing is found
00080     if ( nameProcess_ == "*" ) {
00081       LogError( "autoProcessName" ) << "trigger::TriggerEvent product with label '" << tagTriggerEvent_.label() << "' not produced according to process history of input data\n"
00082                                     << "No trigger information produced.";
00083       return;
00084     }
00085     LogInfo( "autoProcessName" ) << "HLT process name " << nameProcess_ << " used for PAT trigger information";
00086   }
00087   // adapt configuration of used input tags
00088   if ( tagTriggerResults_.process().empty() || tagTriggerResults_.process() == "*" ) {
00089     tagTriggerResults_ = InputTag( tagTriggerResults_.label(), tagTriggerResults_.instance(), nameProcess_ );
00090   } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
00091     LogWarning( "triggerResultsTag" ) << "TriggerResults process name '" << tagTriggerResults_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
00092   }
00093   if ( tagTriggerEvent_.process().empty() || tagTriggerEvent_.process()   == "*" ) {
00094     tagTriggerEvent_ = InputTag( tagTriggerEvent_.label(), tagTriggerEvent_.instance(), nameProcess_ );
00095   } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
00096     LogWarning( "triggerEventTag" ) << "TriggerEvent process name '" << tagTriggerEvent_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
00097   }
00098 
00099   gtCondRunInit_ = false;
00100   if ( ! tagCondGt_.label().empty() ) {
00101     Handle< ConditionsInRunBlock > condRunBlock;
00102     iRun.getByLabel( tagCondGt_, condRunBlock );
00103     if ( condRunBlock.isValid() ) {
00104       condRun_       = *condRunBlock;
00105       gtCondRunInit_ = true;
00106     } else {
00107       LogError( "conditionsInEdm" ) << "ConditionsInRunBlock product with InputTag '" << tagCondGt_.encode() << "' not in run";
00108     }
00109   }
00110 
00111   // Initialize HLTConfigProvider
00112   hltConfigInit_ = false;
00113   bool changed( true );
00114   if ( ! hltConfig_.init( iRun, iSetup, nameProcess_, changed ) ) {
00115     LogError( "hltConfigExtraction" ) << "HLT config extraction error with process name '" << nameProcess_ << "'";
00116   } else if ( hltConfig_.size() <= 0 ) {
00117     LogError( "hltConfigSize" ) << "HLT config size error";
00118   } else hltConfigInit_ = true;
00119 
00120 }
00121 
00122 void PATTriggerEventProducer::beginLuminosityBlock( LuminosityBlock & iLuminosityBlock, const EventSetup & iSetup )
00123 {
00124 
00125   // Terminate, if auto process name determination failed
00126   if ( nameProcess_ == "*" ) return;
00127 
00128   gtCondLumiInit_ = false;
00129   if ( ! tagCondGt_.label().empty() ) {
00130     Handle< ConditionsInLumiBlock > condLumiBlock;
00131     iLuminosityBlock.getByLabel( tagCondGt_, condLumiBlock );
00132     if ( condLumiBlock.isValid() ) {
00133       condLumi_       = *condLumiBlock;
00134       gtCondLumiInit_ = true;
00135     } else {
00136       LogError( "conditionsInEdm" ) << "ConditionsInLumiBlock product with InputTag '" << tagCondGt_.encode() << "' not in lumi";
00137     }
00138   }
00139 
00140 }
00141 
00142 
00143 void PATTriggerEventProducer::produce( Event& iEvent, const EventSetup& iSetup )
00144 {
00145 
00146   // Terminate, if auto process name determination failed
00147   if ( nameProcess_ == "*" ) return;
00148 
00149   if ( ! hltConfigInit_ ) return;
00150 
00151   ESHandle< L1GtTriggerMenu > handleL1GtTriggerMenu;
00152   iSetup.get< L1GtTriggerMenuRcd >().get( handleL1GtTriggerMenu );
00153   Handle< TriggerResults > handleTriggerResults;
00154   iEvent.getByLabel( tagTriggerResults_, handleTriggerResults );
00155   if ( ! handleTriggerResults.isValid() ) {
00156     LogError( "triggerResultsValid" ) << "TriggerResults product with InputTag '" << tagTriggerResults_.encode() << "' not in event\n"
00157                                       << "No trigger information produced";
00158     return;
00159   }
00160   Handle< TriggerAlgorithmCollection > handleTriggerAlgorithms;
00161   iEvent.getByLabel( tagTriggerProducer_, handleTriggerAlgorithms );
00162   Handle< TriggerConditionCollection > handleTriggerConditions;
00163   iEvent.getByLabel( tagTriggerProducer_, handleTriggerConditions );
00164   Handle< TriggerPathCollection > handleTriggerPaths;
00165   iEvent.getByLabel( tagTriggerProducer_, handleTriggerPaths );
00166   Handle< TriggerFilterCollection > handleTriggerFilters;
00167   iEvent.getByLabel( tagTriggerProducer_, handleTriggerFilters );
00168   Handle< TriggerObjectCollection > handleTriggerObjects;
00169   iEvent.getByLabel( tagTriggerProducer_, handleTriggerObjects );
00170   Handle< TriggerObjectStandAloneCollection > handleTriggerObjectsStandAlone;
00171   iEvent.getByLabel( tagTriggerProducer_, handleTriggerObjectsStandAlone );
00172   assert( handleTriggerObjects->size() == handleTriggerObjectsStandAlone->size() );
00173 
00174   bool physDecl( false );
00175   if ( iEvent.isRealData() && ! tagL1Gt_.label().empty() ) {
00176     Handle< L1GlobalTriggerReadoutRecord > handleL1GlobalTriggerReadoutRecord;
00177     iEvent.getByLabel( tagL1Gt_, handleL1GlobalTriggerReadoutRecord );
00178     if ( handleL1GlobalTriggerReadoutRecord.isValid() ) {
00179       L1GtFdlWord fdlWord = handleL1GlobalTriggerReadoutRecord->gtFdlWord();
00180       if ( fdlWord.physicsDeclared() == 1 ) {
00181         physDecl = true;
00182       }
00183     } else {
00184       LogError( "l1GlobalTriggerReadoutRecordValid" ) << "L1GlobalTriggerReadoutRecord product with InputTag '" << tagL1Gt_.encode() << "' not in event";
00185     }
00186   } else {
00187     physDecl = true;
00188   }
00189 
00190 
00191   // produce trigger event
00192 
00193   std::auto_ptr< TriggerEvent > triggerEvent( new TriggerEvent( handleL1GtTriggerMenu->gtTriggerMenuName(), std::string( hltConfig_.tableName() ), handleTriggerResults->wasrun(), handleTriggerResults->accept(), handleTriggerResults->error(), physDecl ) );
00194   // set product references to trigger collections
00195   if ( handleTriggerAlgorithms.isValid() ) {
00196     triggerEvent->setAlgorithms( handleTriggerAlgorithms );
00197   } else {
00198     LogError( "triggerAlgorithmsValid" ) << "pat::TriggerAlgorithmCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00199   }
00200   if ( handleTriggerConditions.isValid() ) {
00201     triggerEvent->setConditions( handleTriggerConditions );
00202   } else {
00203     LogError( "triggerConditionsValid" ) << "pat::TriggerConditionCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00204   }
00205   if ( handleTriggerPaths.isValid() ) {
00206     triggerEvent->setPaths( handleTriggerPaths );
00207   } else {
00208     LogError( "triggerPathsValid" ) << "pat::TriggerPathCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00209   }
00210   if ( handleTriggerFilters.isValid() ) {
00211     triggerEvent->setFilters( handleTriggerFilters );
00212   } else {
00213     LogError( "triggerFiltersValid" ) << "pat::TriggerFilterCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00214   }
00215   if ( handleTriggerObjects.isValid() ) {
00216     triggerEvent->setObjects( handleTriggerObjects );
00217   } else {
00218     LogError( "triggerObjectsValid" ) << "pat::TriggerObjectCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00219   }
00220   if ( gtCondRunInit_ ) {
00221     triggerEvent->setLhcFill( condRun_.lhcFillNumber );
00222     triggerEvent->setBeamMode( condRun_.beamMode );
00223     triggerEvent->setBeamMomentum( condRun_.beamMomentum );
00224     triggerEvent->setBCurrentStart( condRun_.BStartCurrent );
00225     triggerEvent->setBCurrentStop( condRun_.BStopCurrent );
00226     triggerEvent->setBCurrentAvg( condRun_.BAvgCurrent );
00227   }
00228   if ( gtCondLumiInit_ ) {
00229     triggerEvent->setIntensityBeam1( condLumi_.totalIntensityBeam1 );
00230     triggerEvent->setIntensityBeam2( condLumi_.totalIntensityBeam2 );
00231   }
00232   if ( ! tagCondGt_.label().empty() ) {
00233     Handle< ConditionsInEventBlock > condEventBlock;
00234     iEvent.getByLabel( tagCondGt_, condEventBlock );
00235     if ( condEventBlock.isValid() ) {
00236       triggerEvent->setBstMasterStatus( condEventBlock->bstMasterStatus );
00237       triggerEvent->setTurnCount( condEventBlock->turnCountNumber );
00238     } else {
00239       LogError( "conditionsInEdm" ) << "ConditionsInEventBlock product with InputTag '" << tagCondGt_.encode() << "' not in event";
00240     }
00241   }
00242 
00243   // produce trigger match association and set references
00244   if ( handleTriggerObjects.isValid() ) {
00245     for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
00246       const std::string labelTriggerObjectMatcher( tagsTriggerMatcher_.at( iMatch ).label() );
00247       // copy trigger match association using TriggerObjectStandAlone to those using TriggerObject
00248       // relying on the fact, that only one candidate collection is present in the association
00249       Handle< TriggerObjectStandAloneMatch > handleTriggerObjectStandAloneMatch;
00250       iEvent.getByLabel( labelTriggerObjectMatcher, handleTriggerObjectStandAloneMatch );
00251       if ( ! handleTriggerObjectStandAloneMatch.isValid() ) {
00252         LogError( "triggerMatchValid" ) << "pat::TriggerObjectStandAloneMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
00253         continue;
00254       }
00255       AssociativeIterator< reco::CandidateBaseRef, TriggerObjectStandAloneMatch > it( *handleTriggerObjectStandAloneMatch, EdmEventItemGetter< reco::CandidateBaseRef >( iEvent ) ), itEnd( it.end() );
00256       Handle< reco::CandidateView > handleCands;
00257       if ( it != itEnd ) iEvent.get( it->first.id(), handleCands );
00258       std::vector< int > indices;
00259       while ( it != itEnd ) {
00260         indices.push_back( it->second.key() );
00261         ++it;
00262       }
00263       std::auto_ptr< TriggerObjectMatch > triggerObjectMatch( new TriggerObjectMatch( handleTriggerObjects ) );
00264       TriggerObjectMatch::Filler matchFiller( *triggerObjectMatch );
00265       if ( handleCands.isValid() ) {
00266         matchFiller.insert( handleCands, indices.begin(), indices.end() );
00267       }
00268       matchFiller.fill();
00269       OrphanHandle< TriggerObjectMatch > handleTriggerObjectMatch( iEvent.put( triggerObjectMatch, labelTriggerObjectMatcher ) );
00270       // set product reference to trigger match association
00271       if ( ! handleTriggerObjectMatch.isValid() ) {
00272         LogError( "triggerMatchValid" ) << "pat::TriggerObjectMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
00273         continue;
00274       }
00275       if ( ! ( triggerEvent->addObjectMatchResult( handleTriggerObjectMatch, labelTriggerObjectMatcher ) ) ) {
00276         LogWarning( "triggerObjectMatchReplication" ) << "pat::TriggerEvent contains already a pat::TriggerObjectMatch from matcher module '" << labelTriggerObjectMatcher << "'";
00277       }
00278     }
00279   }
00280 
00281   iEvent.put( triggerEvent );
00282 
00283 }
00284 
00285 
00286 #include "FWCore/Framework/interface/MakerMacros.h"
00287 DEFINE_FWK_MODULE( PATTriggerEventProducer );