CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/PhysicsTools/PatAlgos/plugins/PATTriggerEventProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATTriggerEventProducer.cc,v 1.18 2012/05/14 18:02:02 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 
00178   bool physDecl( false );
00179   if ( iEvent.isRealData() && ! tagL1Gt_.label().empty() ) {
00180     Handle< L1GlobalTriggerReadoutRecord > handleL1GlobalTriggerReadoutRecord;
00181     iEvent.getByLabel( tagL1Gt_, handleL1GlobalTriggerReadoutRecord );
00182     if ( handleL1GlobalTriggerReadoutRecord.isValid() ) {
00183       L1GtFdlWord fdlWord = handleL1GlobalTriggerReadoutRecord->gtFdlWord();
00184       if ( fdlWord.physicsDeclared() == 1 ) {
00185         physDecl = true;
00186       }
00187     } else {
00188       LogError( "l1GlobalTriggerReadoutRecordValid" ) << "L1GlobalTriggerReadoutRecord product with InputTag '" << tagL1Gt_.encode() << "' not in event";
00189     }
00190   } else {
00191     physDecl = true;
00192   }
00193 
00194 
00195   // produce trigger event
00196 
00197   std::auto_ptr< TriggerEvent > triggerEvent( new TriggerEvent( handleL1GtTriggerMenu->gtTriggerMenuName(), std::string( hltConfig_.tableName() ), handleTriggerResults->wasrun(), handleTriggerResults->accept(), handleTriggerResults->error(), physDecl ) );
00198   // set product references to trigger collections
00199   if ( handleTriggerAlgorithms.isValid() ) {
00200     triggerEvent->setAlgorithms( handleTriggerAlgorithms );
00201   } else {
00202     LogError( "triggerAlgorithmsValid" ) << "pat::TriggerAlgorithmCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00203   }
00204   if ( handleTriggerConditions.isValid() ) {
00205     triggerEvent->setConditions( handleTriggerConditions );
00206   } else {
00207     LogError( "triggerConditionsValid" ) << "pat::TriggerConditionCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00208   }
00209   if ( handleTriggerPaths.isValid() ) {
00210     triggerEvent->setPaths( handleTriggerPaths );
00211   } else {
00212     LogError( "triggerPathsValid" ) << "pat::TriggerPathCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00213   }
00214   if ( handleTriggerFilters.isValid() ) {
00215     triggerEvent->setFilters( handleTriggerFilters );
00216   } else {
00217     LogError( "triggerFiltersValid" ) << "pat::TriggerFilterCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00218   }
00219   if ( handleTriggerObjects.isValid() ) {
00220     triggerEvent->setObjects( handleTriggerObjects );
00221   } else {
00222     LogError( "triggerObjectsValid" ) << "pat::TriggerObjectCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
00223   }
00224   if ( gtCondRunInit_ ) {
00225     triggerEvent->setLhcFill( condRun_.lhcFillNumber );
00226     triggerEvent->setBeamMode( condRun_.beamMode );
00227     triggerEvent->setBeamMomentum( condRun_.beamMomentum );
00228     triggerEvent->setBCurrentStart( condRun_.BStartCurrent );
00229     triggerEvent->setBCurrentStop( condRun_.BStopCurrent );
00230     triggerEvent->setBCurrentAvg( condRun_.BAvgCurrent );
00231   }
00232   if ( gtCondLumiInit_ ) {
00233     triggerEvent->setIntensityBeam1( condLumi_.totalIntensityBeam1 );
00234     triggerEvent->setIntensityBeam2( condLumi_.totalIntensityBeam2 );
00235   }
00236   if ( ! tagCondGt_.label().empty() ) {
00237     Handle< ConditionsInEventBlock > condEventBlock;
00238     iEvent.getByLabel( tagCondGt_, condEventBlock );
00239     if ( condEventBlock.isValid() ) {
00240       triggerEvent->setBstMasterStatus( condEventBlock->bstMasterStatus );
00241       triggerEvent->setTurnCount( condEventBlock->turnCountNumber );
00242     } else {
00243       LogError( "conditionsInEdm" ) << "ConditionsInEventBlock product with InputTag '" << tagCondGt_.encode() << "' not in event";
00244     }
00245   }
00246 
00247   // produce trigger match association and set references
00248   if ( handleTriggerObjects.isValid() ) {
00249     for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
00250       const std::string labelTriggerObjectMatcher( tagsTriggerMatcher_.at( iMatch ).label() );
00251       // copy trigger match association using TriggerObjectStandAlone to those using TriggerObject
00252       // relying on the fact, that only one candidate collection is present in the association
00253       Handle< TriggerObjectStandAloneMatch > handleTriggerObjectStandAloneMatch;
00254       iEvent.getByLabel( labelTriggerObjectMatcher, handleTriggerObjectStandAloneMatch );
00255       if ( ! handleTriggerObjectStandAloneMatch.isValid() ) {
00256         LogError( "triggerMatchValid" ) << "pat::TriggerObjectStandAloneMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
00257         continue;
00258       }
00259       AssociativeIterator< reco::CandidateBaseRef, TriggerObjectStandAloneMatch > it( *handleTriggerObjectStandAloneMatch, EdmEventItemGetter< reco::CandidateBaseRef >( iEvent ) ), itEnd( it.end() );
00260       Handle< reco::CandidateView > handleCands;
00261       if ( it != itEnd ) iEvent.get( it->first.id(), handleCands );
00262       std::vector< int > indices;
00263       while ( it != itEnd ) {
00264         indices.push_back( it->second.key() );
00265         ++it;
00266       }
00267       std::auto_ptr< TriggerObjectMatch > triggerObjectMatch( new TriggerObjectMatch( handleTriggerObjects ) );
00268       TriggerObjectMatch::Filler matchFiller( *triggerObjectMatch );
00269       if ( handleCands.isValid() ) {
00270         matchFiller.insert( handleCands, indices.begin(), indices.end() );
00271       }
00272       matchFiller.fill();
00273       OrphanHandle< TriggerObjectMatch > handleTriggerObjectMatch( iEvent.put( triggerObjectMatch, labelTriggerObjectMatcher ) );
00274       // set product reference to trigger match association
00275       if ( ! handleTriggerObjectMatch.isValid() ) {
00276         LogError( "triggerMatchValid" ) << "pat::TriggerObjectMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
00277         continue;
00278       }
00279       if ( ! ( triggerEvent->addObjectMatchResult( handleTriggerObjectMatch, labelTriggerObjectMatcher ) ) ) {
00280         LogWarning( "triggerObjectMatchReplication" ) << "pat::TriggerEvent contains already a pat::TriggerObjectMatch from matcher module '" << labelTriggerObjectMatcher << "'";
00281       }
00282     }
00283   }
00284 
00285   iEvent.put( triggerEvent );
00286 
00287 }
00288 
00289 
00290 #include "FWCore/Framework/interface/MakerMacros.h"
00291 DEFINE_FWK_MODULE( PATTriggerEventProducer );