ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/DGele/PhysicsTools/PatAlgos/plugins/PATTriggerProducer.cc
Revision: 1.2
Committed: Tue Oct 20 17:42:59 2009 UTC (15 years, 6 months ago) by dgele
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
State: FILE REMOVED
Log Message:
remove

File Contents

# Content
1 //
2 // $Id: PATTriggerProducer.cc,v 1.1 2009/10/20 17:15:14 dgele Exp $
3 //
4
5
6 #include "PhysicsTools/PatAlgos/plugins/PATTriggerProducer.h"
7
8 #include <cassert>
9
10
11 using namespace pat;
12
13 PATTriggerProducer::PATTriggerProducer( const edm::ParameterSet & iConfig ) :
14 nameProcess_( iConfig.getParameter< std::string >( "processName" ) ),
15 tagTriggerResults_( iConfig.getParameter< edm::InputTag >( "triggerResults" ) ),
16 tagTriggerEvent_( iConfig.getParameter< edm::InputTag >( "triggerEvent" ) ),
17 onlyStandAlone_( iConfig.getParameter< bool >( "onlyStandAlone" ) ),
18 addPathModuleLabels_( iConfig.getParameter< bool >( "addPathModuleLabels" ) )
19 {
20 if ( tagTriggerResults_.process().empty() ) {
21 tagTriggerResults_ = edm::InputTag( tagTriggerResults_.label(), tagTriggerResults_.instance(), nameProcess_ );
22 }
23 if ( tagTriggerEvent_.process().empty() ) {
24 tagTriggerEvent_ = edm::InputTag( tagTriggerEvent_.label(), tagTriggerEvent_.instance(), nameProcess_ );
25 }
26
27 if ( ! onlyStandAlone_ ) {
28 produces< TriggerPathCollection >();
29 produces< TriggerFilterCollection >();
30 produces< TriggerObjectCollection >();
31 }
32 produces< TriggerObjectStandAloneCollection >();
33 }
34
35 PATTriggerProducer::~PATTriggerProducer()
36 {
37 }
38
39 void PATTriggerProducer::beginRun( edm::Run & iRun, const edm::EventSetup & iSetup )
40 {
41 if ( ! hltConfig_.init( nameProcess_ ) ) {
42 edm::LogError( "errorHltConfigExtraction" ) << "HLT config extraction error with process name " << nameProcess_;
43 return;
44 }
45 }
46
47 void PATTriggerProducer::produce( edm::Event& iEvent, const edm::EventSetup& iSetup )
48 {
49 if ( hltConfig_.size() <= 0 ) {
50 edm::LogError( "errorHltConfigSize" ) << "HLT config size error" << "\n"
51 << "Check for occurence of an \"errorHltConfigExtraction\" from beginRun()";
52 return;
53 }
54 edm::Handle< edm::TriggerResults > handleTriggerResults;
55 iEvent.getByLabel( tagTriggerResults_, handleTriggerResults );
56 if ( ! handleTriggerResults.isValid() ) {
57 edm::LogError( "errorTriggerResultsValid" ) << "edm::TriggerResults product with InputTag " << tagTriggerResults_.encode() << " not in event";
58 return;
59 }
60 edm::Handle< trigger::TriggerEvent > handleTriggerEvent;
61 iEvent.getByLabel( tagTriggerEvent_, handleTriggerEvent );
62 if ( ! handleTriggerEvent.isValid() ) {
63 edm::LogError( "errorTriggerEventValid" ) << "trigger::TriggerEvent product with InputTag " << tagTriggerEvent_.encode() << " not in event";
64 return;
65 }
66
67 // produce trigger paths and determine status of modules
68
69 const unsigned sizePaths( hltConfig_.size() );
70 const unsigned sizeFilters( handleTriggerEvent->sizeFilters() );
71 const unsigned sizeObjects( handleTriggerEvent->sizeObjects() );
72
73 std::auto_ptr< TriggerPathCollection > triggerPaths( new TriggerPathCollection() );
74 triggerPaths->reserve( onlyStandAlone_ ? 0 : sizePaths );
75 std::map< std::string, int > moduleStates;
76 std::multimap< std::string, std::string > filterPaths;
77
78 for ( unsigned iP = 0; iP < sizePaths; ++iP ) {
79 const std::string namePath( hltConfig_.triggerName( iP ) );
80 const unsigned indexPath( hltConfig_.triggerIndex( namePath ) );
81 const unsigned sizeModules( hltConfig_.size( namePath ) );
82 for ( unsigned iM = 0; iM < sizeModules; ++iM ) {
83 const std::string nameModule( hltConfig_.moduleLabel( indexPath, iM ) );
84 const unsigned indexFilter( handleTriggerEvent->filterIndex( edm::InputTag( nameModule, "", nameProcess_ ) ) );
85 if ( indexFilter < sizeFilters ) {
86 filterPaths.insert( std::pair< std::string, std::string >( nameModule, namePath ) );
87 }
88 }
89 if ( ! onlyStandAlone_ ) {
90 const unsigned indexLastFilter( handleTriggerResults->index( indexPath ) );
91 TriggerPath triggerPath( namePath, indexPath, 0, handleTriggerResults->wasrun( indexPath ), handleTriggerResults->accept( indexPath ), handleTriggerResults->error( indexPath ), indexLastFilter );
92 // add module names to path and states' map
93 assert( indexLastFilter < sizeModules );
94 std::map< unsigned, std::string > indicesModules;
95 for ( unsigned iM = 0; iM < sizeModules; ++iM ) {
96 const std::string nameModule( hltConfig_.moduleLabel( indexPath, iM ) );
97 if ( addPathModuleLabels_ ) {
98 triggerPath.addModule( nameModule );
99 }
100 const unsigned indexFilter( handleTriggerEvent->filterIndex( edm::InputTag( nameModule, "", nameProcess_ ) ) );
101 if ( indexFilter < sizeFilters ) {
102 triggerPath.addFilterIndex( indexFilter );
103 }
104 const unsigned slotModule( hltConfig_.moduleIndex( indexPath, nameModule ) );
105 indicesModules.insert( std::pair< unsigned, std::string >( slotModule, nameModule ) );
106 }
107 // store path
108 triggerPaths->push_back( triggerPath );
109 // store module states to be used for the filters
110 for ( std::map< unsigned, std::string >::const_iterator iM = indicesModules.begin(); iM != indicesModules.end(); ++iM ) {
111 if ( iM->first < indexLastFilter ) {
112 moduleStates[ iM->second ] = 1;
113 } else if ( iM->first == indexLastFilter ) {
114 moduleStates[ iM->second ] = handleTriggerResults->accept( indexPath );
115 } else if ( moduleStates.find( iM->second ) == moduleStates.end() ) {
116 moduleStates[ iM->second ] = -1;
117 }
118 }
119 }
120 }
121
122 if ( ! onlyStandAlone_ ) {
123 iEvent.put( triggerPaths );
124 }
125
126 // produce trigger filters and store used trigger object types
127 // (only last active filter(s) available from trigger::TriggerEvent)
128
129 std::auto_ptr< TriggerFilterCollection > triggerFilters( new TriggerFilterCollection() );
130 triggerFilters->reserve( onlyStandAlone_ ? 0 : sizeFilters );
131 std::multimap< trigger::size_type, int > filterIds;
132 std::multimap< trigger::size_type, std::string > filterLabels;
133
134 for ( unsigned iF = 0; iF < sizeFilters; ++iF ) {
135 const std::string nameFilter( handleTriggerEvent->filterTag( iF ).label() );
136 const trigger::Keys & keys = handleTriggerEvent->filterKeys( iF );
137 const trigger::Vids & ids = handleTriggerEvent->filterIds( iF );
138 assert( ids.size() == keys.size() );
139 for ( unsigned iK = 0; iK < keys.size(); ++iK ) {
140 filterLabels.insert( std::pair< trigger::size_type, std::string >( keys[ iK ], nameFilter ) ); // only for objects used in last active filter
141 filterIds.insert( std::pair< trigger::size_type, int >( keys[ iK ], ids[ iK ] ) ); // only for objects used in last active filter
142 }
143 if ( ! onlyStandAlone_ ) {
144 TriggerFilter triggerFilter( nameFilter );
145 // set filter type
146 const std::string typeFilter( hltConfig_.moduleType( nameFilter ) );
147 triggerFilter.setType( typeFilter );
148 // set filter IDs of used objects
149 for ( unsigned iK = 0; iK < keys.size(); ++iK ) {
150 triggerFilter.addObjectKey( keys[ iK ] );
151 }
152 for ( unsigned iI = 0; iI < ids.size(); ++iI ) {
153 triggerFilter.addObjectId( ids[ iI ] );
154 }
155 // set status from path info
156 std::map< std::string, int >::iterator iS( moduleStates.find( nameFilter ) );
157 if ( iS != moduleStates.end() ) {
158 if ( ! triggerFilter.setStatus( iS->second ) ) {
159 triggerFilter.setStatus( -1 ); // different code for "unvalid status determined" needed?
160 }
161 } else {
162 triggerFilter.setStatus( -1 ); // different code for "unknown" needed?
163 }
164 // store filter
165 triggerFilters->push_back( triggerFilter );
166 }
167 // store used trigger object types to be used with the objects
168 }
169
170 if ( ! onlyStandAlone_ ) {
171 iEvent.put( triggerFilters );
172 }
173
174 // produce trigger objects
175
176 std::auto_ptr< TriggerObjectCollection > triggerObjects( new TriggerObjectCollection() );
177 triggerObjects->reserve( onlyStandAlone_ ? 0 : sizeObjects );
178 std::auto_ptr< TriggerObjectStandAloneCollection > triggerObjectsStandAlone( new TriggerObjectStandAloneCollection() );
179 triggerObjectsStandAlone->reserve( sizeObjects );
180
181 const trigger::Keys & collectionKeys( handleTriggerEvent->collectionKeys() );
182 for ( unsigned iO = 0, iC = 0; iO < sizeObjects && iC < handleTriggerEvent->sizeCollections(); ++iO ) {
183 TriggerObject triggerObject( handleTriggerEvent->getObjects().at( iO ) );
184 // set collection
185 while ( iO >= collectionKeys[ iC ] ) {
186 ++iC;
187 } // relies on well ordering of trigger objects with respect to the collections
188 triggerObject.setCollection( handleTriggerEvent->collectionTag( iC ).encode() );
189 // set filter ID
190 for ( std::multimap< trigger::size_type, int >::iterator iM = filterIds.begin(); iM != filterIds.end(); ++iM ) {
191 if ( iM->first == iO && ! triggerObject.hasFilterId( iM->second ) ) {
192 triggerObject.addFilterId( iM->second );
193 }
194 }
195 if ( ! onlyStandAlone_ ) {
196 triggerObjects->push_back( triggerObject );
197 }
198 // stand-alone trigger object
199 TriggerObjectStandAlone triggerObjectStandAlone( triggerObject );
200 for ( std::multimap< trigger::size_type, std::string >::iterator iM = filterLabels.begin(); iM != filterLabels.end(); ++iM ) {
201 if ( iM->first == iO ) {
202 for ( std::multimap< std::string, std::string >::iterator iP = filterPaths.begin(); iP != filterPaths.end(); ++iP ) {
203 if ( iP->first == iM->second ) {
204 triggerObjectStandAlone.addPathName( iP->second );
205 break;
206 }
207 }
208 triggerObjectStandAlone.addFilterLabel( iM->second );
209 break;
210 }
211 }
212
213 triggerObjectsStandAlone->push_back( triggerObjectStandAlone );
214 }
215
216 if ( ! onlyStandAlone_ ) {
217 iEvent.put( triggerObjects );
218 }
219 iEvent.put( triggerObjectsStandAlone );
220 }
221
222
223 #include "FWCore/Framework/interface/MakerMacros.h"
224 DEFINE_FWK_MODULE( PATTriggerProducer );