00001
00002
00003
00004
00005
00006 #include "PhysicsTools/PatAlgos/plugins/PATTriggerProducer.h"
00007
00008 #include <cassert>
00009
00010
00011 using namespace pat;
00012
00013 PATTriggerProducer::PATTriggerProducer( const edm::ParameterSet & iConfig ) :
00014 nameProcess_( iConfig.getParameter< std::string >( "processName" ) ),
00015 tagTriggerResults_( iConfig.getParameter< edm::InputTag >( "triggerResults" ) ),
00016 tagTriggerEvent_( iConfig.getParameter< edm::InputTag >( "triggerEvent" ) ),
00017 onlyStandAlone_( iConfig.getParameter< bool >( "onlyStandAlone" ) ),
00018 addPathModuleLabels_( iConfig.getParameter< bool >( "addPathModuleLabels" ) )
00019 {
00020 if ( tagTriggerResults_.process().empty() ) {
00021 tagTriggerResults_ = edm::InputTag( tagTriggerResults_.label(), tagTriggerResults_.instance(), nameProcess_ );
00022 }
00023 if ( tagTriggerEvent_.process().empty() ) {
00024 tagTriggerEvent_ = edm::InputTag( tagTriggerEvent_.label(), tagTriggerEvent_.instance(), nameProcess_ );
00025 }
00026
00027 if ( ! onlyStandAlone_ ) {
00028 produces< TriggerPathCollection >();
00029 produces< TriggerFilterCollection >();
00030 produces< TriggerObjectCollection >();
00031 }
00032 produces< TriggerObjectStandAloneCollection >();
00033 }
00034
00035 PATTriggerProducer::~PATTriggerProducer()
00036 {
00037 }
00038
00039 void PATTriggerProducer::beginRun( edm::Run & iRun, const edm::EventSetup & iSetup )
00040 {
00041 if ( ! hltConfig_.init( nameProcess_ ) ) {
00042 edm::LogError( "errorHltConfigExtraction" ) << "HLT config extraction error with process name " << nameProcess_;
00043 return;
00044 }
00045 }
00046
00047 void PATTriggerProducer::produce( edm::Event& iEvent, const edm::EventSetup& iSetup )
00048 {
00049 if ( hltConfig_.size() <= 0 ) {
00050 edm::LogError( "errorHltConfigSize" ) << "HLT config size error" << "\n"
00051 << "Check for occurence of an \"errorHltConfigExtraction\" from beginRun()";
00052 return;
00053 }
00054 edm::Handle< edm::TriggerResults > handleTriggerResults;
00055 iEvent.getByLabel( tagTriggerResults_, handleTriggerResults );
00056 if ( ! handleTriggerResults.isValid() ) {
00057 edm::LogError( "errorTriggerResultsValid" ) << "edm::TriggerResults product with InputTag " << tagTriggerResults_.encode() << " not in event";
00058 return;
00059 }
00060 edm::Handle< trigger::TriggerEvent > handleTriggerEvent;
00061 iEvent.getByLabel( tagTriggerEvent_, handleTriggerEvent );
00062 if ( ! handleTriggerEvent.isValid() ) {
00063 edm::LogError( "errorTriggerEventValid" ) << "trigger::TriggerEvent product with InputTag " << tagTriggerEvent_.encode() << " not in event";
00064 return;
00065 }
00066
00067
00068
00069 const unsigned sizePaths( hltConfig_.size() );
00070 const unsigned sizeFilters( handleTriggerEvent->sizeFilters() );
00071 const unsigned sizeObjects( handleTriggerEvent->sizeObjects() );
00072
00073 std::auto_ptr< TriggerPathCollection > triggerPaths( new TriggerPathCollection() );
00074 triggerPaths->reserve( onlyStandAlone_ ? 0 : sizePaths );
00075 std::map< std::string, int > moduleStates;
00076 std::multimap< std::string, std::string > filterPaths;
00077
00078 for ( unsigned iP = 0; iP < sizePaths; ++iP ) {
00079 const std::string namePath( hltConfig_.triggerName( iP ) );
00080 const unsigned indexPath( hltConfig_.triggerIndex( namePath ) );
00081 const unsigned sizeModules( hltConfig_.size( namePath ) );
00082 for ( unsigned iM = 0; iM < sizeModules; ++iM ) {
00083 const std::string nameModule( hltConfig_.moduleLabel( indexPath, iM ) );
00084 const unsigned indexFilter( handleTriggerEvent->filterIndex( edm::InputTag( nameModule, "", nameProcess_ ) ) );
00085 if ( indexFilter < sizeFilters ) {
00086 filterPaths.insert( std::pair< std::string, std::string >( nameModule, namePath ) );
00087 }
00088 }
00089 if ( ! onlyStandAlone_ ) {
00090 const unsigned indexLastFilter( handleTriggerResults->index( indexPath ) );
00091 TriggerPath triggerPath( namePath, indexPath, 0, handleTriggerResults->wasrun( indexPath ), handleTriggerResults->accept( indexPath ), handleTriggerResults->error( indexPath ), indexLastFilter );
00092
00093 assert( indexLastFilter < sizeModules );
00094 std::map< unsigned, std::string > indicesModules;
00095 for ( unsigned iM = 0; iM < sizeModules; ++iM ) {
00096 const std::string nameModule( hltConfig_.moduleLabel( indexPath, iM ) );
00097 if ( addPathModuleLabels_ ) {
00098 triggerPath.addModule( nameModule );
00099 }
00100 const unsigned indexFilter( handleTriggerEvent->filterIndex( edm::InputTag( nameModule, "", nameProcess_ ) ) );
00101 if ( indexFilter < sizeFilters ) {
00102 triggerPath.addFilterIndex( indexFilter );
00103 }
00104 const unsigned slotModule( hltConfig_.moduleIndex( indexPath, nameModule ) );
00105 indicesModules.insert( std::pair< unsigned, std::string >( slotModule, nameModule ) );
00106 }
00107
00108 triggerPaths->push_back( triggerPath );
00109
00110 for ( std::map< unsigned, std::string >::const_iterator iM = indicesModules.begin(); iM != indicesModules.end(); ++iM ) {
00111 if ( iM->first < indexLastFilter ) {
00112 moduleStates[ iM->second ] = 1;
00113 } else if ( iM->first == indexLastFilter ) {
00114 moduleStates[ iM->second ] = handleTriggerResults->accept( indexPath );
00115 } else if ( moduleStates.find( iM->second ) == moduleStates.end() ) {
00116 moduleStates[ iM->second ] = -1;
00117 }
00118 }
00119 }
00120 }
00121
00122 if ( ! onlyStandAlone_ ) {
00123 iEvent.put( triggerPaths );
00124 }
00125
00126
00127
00128
00129 std::auto_ptr< TriggerFilterCollection > triggerFilters( new TriggerFilterCollection() );
00130 triggerFilters->reserve( onlyStandAlone_ ? 0 : sizeFilters );
00131 std::multimap< trigger::size_type, int > filterIds;
00132 std::multimap< trigger::size_type, std::string > filterLabels;
00133
00134 for ( unsigned iF = 0; iF < sizeFilters; ++iF ) {
00135 const std::string nameFilter( handleTriggerEvent->filterTag( iF ).label() );
00136 const trigger::Keys & keys = handleTriggerEvent->filterKeys( iF );
00137 const trigger::Vids & ids = handleTriggerEvent->filterIds( iF );
00138 assert( ids.size() == keys.size() );
00139 for ( unsigned iK = 0; iK < keys.size(); ++iK ) {
00140 filterLabels.insert( std::pair< trigger::size_type, std::string >( keys[ iK ], nameFilter ) );
00141 filterIds.insert( std::pair< trigger::size_type, int >( keys[ iK ], ids[ iK ] ) );
00142 }
00143 if ( ! onlyStandAlone_ ) {
00144 TriggerFilter triggerFilter( nameFilter );
00145
00146 const std::string typeFilter( hltConfig_.moduleType( nameFilter ) );
00147 triggerFilter.setType( typeFilter );
00148
00149 for ( unsigned iK = 0; iK < keys.size(); ++iK ) {
00150 triggerFilter.addObjectKey( keys[ iK ] );
00151 }
00152 for ( unsigned iI = 0; iI < ids.size(); ++iI ) {
00153 triggerFilter.addObjectId( ids[ iI ] );
00154 }
00155
00156 std::map< std::string, int >::iterator iS( moduleStates.find( nameFilter ) );
00157 if ( iS != moduleStates.end() ) {
00158 if ( ! triggerFilter.setStatus( iS->second ) ) {
00159 triggerFilter.setStatus( -1 );
00160 }
00161 } else {
00162 triggerFilter.setStatus( -1 );
00163 }
00164
00165 triggerFilters->push_back( triggerFilter );
00166 }
00167
00168 }
00169
00170 if ( ! onlyStandAlone_ ) {
00171 iEvent.put( triggerFilters );
00172 }
00173
00174
00175
00176 std::auto_ptr< TriggerObjectCollection > triggerObjects( new TriggerObjectCollection() );
00177 triggerObjects->reserve( onlyStandAlone_ ? 0 : sizeObjects );
00178 std::auto_ptr< TriggerObjectStandAloneCollection > triggerObjectsStandAlone( new TriggerObjectStandAloneCollection() );
00179 triggerObjectsStandAlone->reserve( sizeObjects );
00180
00181 const trigger::Keys & collectionKeys( handleTriggerEvent->collectionKeys() );
00182 for ( unsigned iO = 0, iC = 0; iO < sizeObjects && iC < handleTriggerEvent->sizeCollections(); ++iO ) {
00183 TriggerObject triggerObject( handleTriggerEvent->getObjects().at( iO ) );
00184
00185 while ( iO >= collectionKeys[ iC ] ) {
00186 ++iC;
00187 }
00188 triggerObject.setCollection( handleTriggerEvent->collectionTag( iC ).encode() );
00189
00190 for ( std::multimap< trigger::size_type, int >::iterator iM = filterIds.begin(); iM != filterIds.end(); ++iM ) {
00191 if ( iM->first == iO && ! triggerObject.hasFilterId( iM->second ) ) {
00192 triggerObject.addFilterId( iM->second );
00193 }
00194 }
00195 if ( ! onlyStandAlone_ ) {
00196 triggerObjects->push_back( triggerObject );
00197 }
00198
00199 TriggerObjectStandAlone triggerObjectStandAlone( triggerObject );
00200 for ( std::multimap< trigger::size_type, std::string >::iterator iM = filterLabels.begin(); iM != filterLabels.end(); ++iM ) {
00201 if ( iM->first == iO ) {
00202 for ( std::multimap< std::string, std::string >::iterator iP = filterPaths.begin(); iP != filterPaths.end(); ++iP ) {
00203 if ( iP->first == iM->second ) {
00204 triggerObjectStandAlone.addPathName( iP->second );
00205 break;
00206 }
00207 }
00208 triggerObjectStandAlone.addFilterLabel( iM->second );
00209 break;
00210 }
00211 }
00212
00213 triggerObjectsStandAlone->push_back( triggerObjectStandAlone );
00214 }
00215
00216 if ( ! onlyStandAlone_ ) {
00217 iEvent.put( triggerObjects );
00218 }
00219 iEvent.put( triggerObjectsStandAlone );
00220 }
00221
00222
00223 #include "FWCore/Framework/interface/MakerMacros.h"
00224 DEFINE_FWK_MODULE( PATTriggerProducer );