CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/PhysicsTools/PatAlgos/plugins/PATTriggerEventProducer.cc

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