CMS 3D CMS Logo

PATTriggerEventProducer.cc
Go to the documentation of this file.
1 //
2 //
3 
4 
6 
7 #include <cassert>
8 
10 
12 
19 
23 
24 
25 using namespace pat;
26 using namespace edm;
27 
28 
30  nameProcess_( iConfig.getParameter< std::string >( "processName" ) ),
31  autoProcessName_( nameProcess_ == "*" ),
32  tagTriggerProducer_( "patTrigger" ),
33  tagsTriggerMatcher_(),
34  // L1 configuration parameters
35  tagL1Gt_(),
36  // HLTConfigProvider
37  hltConfigInit_( false ),
38  // HLT configuration parameters
39  tagTriggerResults_( "TriggerResults" ),
40  tagTriggerEvent_( "hltTriggerSummaryAOD" ),
41  // Conditions configuration parameters
42  tagCondGt_(),
43  // Conditions
44  condRun_(),
45  condLumi_(),
46  gtCondRunInit_( false ),
47  gtCondLumiInit_( false )
48 {
49 
50  if ( iConfig.exists( "triggerResults" ) ) tagTriggerResults_ = iConfig.getParameter< InputTag >( "triggerResults" );
52  if ( iConfig.exists( "triggerEvent" ) ) tagTriggerEvent_ = iConfig.getParameter< InputTag >( "triggerEvent" );
53  if ( iConfig.exists( "patTriggerProducer" ) ) tagTriggerProducer_ = iConfig.getParameter< InputTag >( "patTriggerProducer" );
54  triggerAlgorithmCollectionToken_ = mayConsume< TriggerAlgorithmCollection >( tagTriggerProducer_ );
55  triggerConditionCollectionToken_ = mayConsume< TriggerConditionCollection >( tagTriggerProducer_ );
56  triggerPathCollectionToken_ = mayConsume< TriggerPathCollection >( tagTriggerProducer_ );
57  triggerFilterCollectionToken_ = mayConsume< TriggerFilterCollection >( tagTriggerProducer_ );
58  triggerObjectCollectionToken_ = mayConsume< TriggerObjectCollection >( tagTriggerProducer_ );
59  if ( iConfig.exists( "condGtTag" ) ) {
60  tagCondGt_ = iConfig.getParameter< InputTag >( "condGtTag" );
61  tagCondGtRunToken_ = mayConsume< ConditionsInRunBlock, InRun >( tagCondGt_ );
62  tagCondGtLumiToken_ = mayConsume< ConditionsInLumiBlock, InLumi >( tagCondGt_ );
63  tagCondGtEventToken_ = mayConsume< ConditionsInEventBlock >( tagCondGt_ );
64  }
65  if ( iConfig.exists( "l1GtTag" ) ) tagL1Gt_ = iConfig.getParameter< InputTag >( "l1GtTag" );
66  l1GtToken_ = mayConsume< L1GlobalTriggerReadoutRecord >( tagL1Gt_ );
67  if ( iConfig.exists( "patTriggerMatches" ) ) tagsTriggerMatcher_ = iConfig.getParameter< std::vector< InputTag > >( "patTriggerMatches" );
68  triggerMatcherTokens_ = vector_transform( tagsTriggerMatcher_, [this](InputTag const & tag) { return mayConsume< TriggerObjectStandAloneMatch >( tag ); } );
69 
70  callWhenNewProductsRegistered( [ this, &iConfig ]( BranchDescription const& bd ) {
71  if(not ( this->autoProcessName_ and bd.processName()==this->moduleDescription().processName()) ) {
73  }
74  } );
75 
76  for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
77  produces< TriggerObjectMatch >( tagsTriggerMatcher_.at( iMatch ).label() );
78  }
79  produces< TriggerEvent >();
80 
81 }
82 
83 
84 void PATTriggerEventProducer::beginRun(const Run & iRun, const EventSetup & iSetup )
85 {
86 
87  // Initialize process name
88  if ( autoProcessName_ ) {
89  // reset
90  nameProcess_ = "*";
91  // determine process name from last run TriggerSummaryProducerAOD module in process history of input
92  const ProcessHistory & processHistory( iRun.processHistory() );
93  ProcessConfiguration processConfiguration;
94  ParameterSet processPSet;
95  // unbroken loop, which relies on time ordering (accepts the last found entry)
96  for ( ProcessHistory::const_iterator iHist = processHistory.begin(); iHist != processHistory.end(); ++iHist ) {
97  if ( processHistory.getConfigurationForProcess( iHist->processName(), processConfiguration ) &&
98  pset::Registry::instance()->getMapped( processConfiguration.parameterSetID(), processPSet ) &&
99  processPSet.exists( tagTriggerEvent_.label() )
100  ) {
101  nameProcess_ = iHist->processName();
102  LogDebug( "autoProcessName" ) << "HLT process name '" << nameProcess_ << "' discovered";
103  }
104  }
105  // terminate, if nothing is found
106  if ( nameProcess_ == "*" ) {
107  LogError( "autoProcessName" ) << "trigger::TriggerEvent product with label '" << tagTriggerEvent_.label() << "' not produced according to process history of input data\n"
108  << "No trigger information produced.";
109  return;
110  }
111  LogInfo( "autoProcessName" ) << "HLT process name " << nameProcess_ << " used for PAT trigger information";
112  }
113  // adapt configuration of used input tags
114  if ( tagTriggerResults_.process().empty() || tagTriggerResults_.process() == "*" ) {
116  } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
117  LogWarning( "triggerResultsTag" ) << "TriggerResults process name '" << tagTriggerResults_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
118  }
119  if ( tagTriggerEvent_.process().empty() || tagTriggerEvent_.process() == "*" ) {
121  } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
122  LogWarning( "triggerEventTag" ) << "TriggerEvent process name '" << tagTriggerEvent_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
123  }
124 
125  gtCondRunInit_ = false;
126  if ( ! tagCondGt_.label().empty() ) {
127  Handle< ConditionsInRunBlock > condRunBlock;
128  iRun.getByToken( tagCondGtRunToken_, condRunBlock );
129  if ( condRunBlock.isValid() ) {
130  condRun_ = *condRunBlock;
131  gtCondRunInit_ = true;
132  } else {
133  LogError( "conditionsInEdm" ) << "ConditionsInRunBlock product with InputTag '" << tagCondGt_.encode() << "' not in run";
134  }
135  }
136 
137  // Initialize HLTConfigProvider
138  hltConfigInit_ = false;
139  bool changed( true );
140  if ( ! hltConfig_.init( iRun, iSetup, nameProcess_, changed ) ) {
141  LogError( "hltConfigExtraction" ) << "HLT config extraction error with process name '" << nameProcess_ << "'";
142  } else if ( hltConfig_.size() <= 0 ) {
143  LogError( "hltConfigSize" ) << "HLT config size error";
144  } else hltConfigInit_ = true;
145 
146 }
147 
148 void PATTriggerEventProducer::beginLuminosityBlock(const LuminosityBlock & iLuminosityBlock, const EventSetup & iSetup )
149 {
150 
151  // Terminate, if auto process name determination failed
152  if ( nameProcess_ == "*" ) return;
153 
154  gtCondLumiInit_ = false;
155  if ( ! tagCondGt_.label().empty() ) {
156  Handle< ConditionsInLumiBlock > condLumiBlock;
157  iLuminosityBlock.getByToken( tagCondGtLumiToken_, condLumiBlock );
158  if ( condLumiBlock.isValid() ) {
159  condLumi_ = *condLumiBlock;
160  gtCondLumiInit_ = true;
161  } else {
162  LogError( "conditionsInEdm" ) << "ConditionsInLumiBlock product with InputTag '" << tagCondGt_.encode() << "' not in lumi";
163  }
164  }
165 
166 }
167 
168 
170 {
171 
172  // Terminate, if auto process name determination failed
173  if ( nameProcess_ == "*" ) return;
174 
175  if ( ! hltConfigInit_ ) return;
176 
177  ESHandle< L1GtTriggerMenu > handleL1GtTriggerMenu;
178  iSetup.get< L1GtTriggerMenuRcd >().get( handleL1GtTriggerMenu );
179  Handle< TriggerResults > handleTriggerResults;
180  iEvent.getByLabel( tagTriggerResults_, handleTriggerResults );
181 // iEvent.getByToken( triggerResultsToken_, handleTriggerResults );
182  if ( ! handleTriggerResults.isValid() ) {
183  LogError( "triggerResultsValid" ) << "TriggerResults product with InputTag '" << tagTriggerResults_.encode() << "' not in event\n"
184  << "No trigger information produced";
185  return;
186  }
187  Handle< TriggerAlgorithmCollection > handleTriggerAlgorithms;
188  iEvent.getByToken( triggerAlgorithmCollectionToken_, handleTriggerAlgorithms );
189  Handle< TriggerConditionCollection > handleTriggerConditions;
190  iEvent.getByToken( triggerConditionCollectionToken_, handleTriggerConditions );
191  Handle< TriggerPathCollection > handleTriggerPaths;
192  iEvent.getByToken( triggerPathCollectionToken_, handleTriggerPaths );
193  Handle< TriggerFilterCollection > handleTriggerFilters;
194  iEvent.getByToken( triggerFilterCollectionToken_, handleTriggerFilters );
195  Handle< TriggerObjectCollection > handleTriggerObjects;
196  iEvent.getByToken( triggerObjectCollectionToken_, handleTriggerObjects );
197 
198  bool physDecl( false );
199  if ( iEvent.isRealData() && ! tagL1Gt_.label().empty() ) {
200  Handle< L1GlobalTriggerReadoutRecord > handleL1GlobalTriggerReadoutRecord;
201  iEvent.getByToken( l1GtToken_, handleL1GlobalTriggerReadoutRecord );
202  if ( handleL1GlobalTriggerReadoutRecord.isValid() ) {
203  L1GtFdlWord fdlWord = handleL1GlobalTriggerReadoutRecord->gtFdlWord();
204  if ( fdlWord.physicsDeclared() == 1 ) {
205  physDecl = true;
206  }
207  } else {
208  LogError( "l1GlobalTriggerReadoutRecordValid" ) << "L1GlobalTriggerReadoutRecord product with InputTag '" << tagL1Gt_.encode() << "' not in event";
209  }
210  } else {
211  physDecl = true;
212  }
213 
214 
215  // produce trigger event
216 
217  auto triggerEvent = std::make_unique<TriggerEvent>( handleL1GtTriggerMenu->gtTriggerMenuName(), std::string( hltConfig_.tableName() ), handleTriggerResults->wasrun(), handleTriggerResults->accept(), handleTriggerResults->error(), physDecl );
218  // set product references to trigger collections
219  if ( handleTriggerAlgorithms.isValid() ) {
220  triggerEvent->setAlgorithms( handleTriggerAlgorithms );
221  } else {
222  LogError( "triggerAlgorithmsValid" ) << "pat::TriggerAlgorithmCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
223  }
224  if ( handleTriggerConditions.isValid() ) {
225  triggerEvent->setConditions( handleTriggerConditions );
226  } else {
227  LogError( "triggerConditionsValid" ) << "pat::TriggerConditionCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
228  }
229  if ( handleTriggerPaths.isValid() ) {
230  triggerEvent->setPaths( handleTriggerPaths );
231  } else {
232  LogError( "triggerPathsValid" ) << "pat::TriggerPathCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
233  }
234  if ( handleTriggerFilters.isValid() ) {
235  triggerEvent->setFilters( handleTriggerFilters );
236  } else {
237  LogError( "triggerFiltersValid" ) << "pat::TriggerFilterCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
238  }
239  if ( handleTriggerObjects.isValid() ) {
240  triggerEvent->setObjects( handleTriggerObjects );
241  } else {
242  LogError( "triggerObjectsValid" ) << "pat::TriggerObjectCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
243  }
244  if ( gtCondRunInit_ ) {
245  triggerEvent->setLhcFill( condRun_.lhcFillNumber );
246  triggerEvent->setBeamMode( condRun_.beamMode );
247  triggerEvent->setBeamMomentum( condRun_.beamMomentum );
248  triggerEvent->setBCurrentStart( condRun_.BStartCurrent );
249  triggerEvent->setBCurrentStop( condRun_.BStopCurrent );
250  triggerEvent->setBCurrentAvg( condRun_.BAvgCurrent );
251  }
252  if ( gtCondLumiInit_ ) {
253  triggerEvent->setIntensityBeam1( condLumi_.totalIntensityBeam1 );
254  triggerEvent->setIntensityBeam2( condLumi_.totalIntensityBeam2 );
255  }
256  if ( ! tagCondGt_.label().empty() ) {
257  Handle< ConditionsInEventBlock > condEventBlock;
258  iEvent.getByToken( tagCondGtEventToken_, condEventBlock );
259  if ( condEventBlock.isValid() ) {
260  triggerEvent->setBstMasterStatus( condEventBlock->bstMasterStatus );
261  triggerEvent->setTurnCount( condEventBlock->turnCountNumber );
262  } else {
263  LogError( "conditionsInEdm" ) << "ConditionsInEventBlock product with InputTag '" << tagCondGt_.encode() << "' not in event";
264  }
265  }
266 
267  // produce trigger match association and set references
268  if ( handleTriggerObjects.isValid() ) {
269  for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
270  const std::string labelTriggerObjectMatcher( tagsTriggerMatcher_.at( iMatch ).label() );
271  // copy trigger match association using TriggerObjectStandAlone to those using TriggerObject
272  // relying on the fact, that only one candidate collection is present in the association
273  Handle< TriggerObjectStandAloneMatch > handleTriggerObjectStandAloneMatch;
274  iEvent.getByToken( triggerMatcherTokens_.at( iMatch ), handleTriggerObjectStandAloneMatch );
275  if ( ! handleTriggerObjectStandAloneMatch.isValid() ) {
276  LogError( "triggerMatchValid" ) << "pat::TriggerObjectStandAloneMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
277  continue;
278  }
279  AssociativeIterator< reco::CandidateBaseRef, TriggerObjectStandAloneMatch > it( *handleTriggerObjectStandAloneMatch, EdmEventItemGetter< reco::CandidateBaseRef >( iEvent ) ), itEnd( it.end() );
280  Handle< reco::CandidateView > handleCands;
281  if ( it != itEnd ) iEvent.get( it->first.id(), handleCands );
282  std::vector< int > indices;
283  while ( it != itEnd ) {
284  indices.push_back( it->second.key() );
285  ++it;
286  }
287  auto triggerObjectMatch = std::make_unique<TriggerObjectMatch>(handleTriggerObjects);
288  TriggerObjectMatch::Filler matchFiller( *triggerObjectMatch );
289  if ( handleCands.isValid() ) {
290  matchFiller.insert( handleCands, indices.begin(), indices.end() );
291  }
292  matchFiller.fill();
293  OrphanHandle< TriggerObjectMatch > handleTriggerObjectMatch( iEvent.put(std::move(triggerObjectMatch), labelTriggerObjectMatcher ) );
294  // set product reference to trigger match association
295  if ( ! handleTriggerObjectMatch.isValid() ) {
296  LogError( "triggerMatchValid" ) << "pat::TriggerObjectMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
297  continue;
298  }
299  if ( ! ( triggerEvent->addObjectMatchResult( handleTriggerObjectMatch, labelTriggerObjectMatcher ) ) ) {
300  LogWarning( "triggerObjectMatchReplication" ) << "pat::TriggerEvent contains already a pat::TriggerObjectMatch from matcher module '" << labelTriggerObjectMatcher << "'";
301  }
302  }
303  }
304 
305  iEvent.put(std::move(triggerEvent) );
306 
307 }
308 
309 
#define LogDebug(id)
unsigned int size() const
number of trigger paths in trigger table
collection_type::const_iterator const_iterator
const std::string & gtTriggerMenuName() const
T getParameter(std::string const &) const
void callWhenNewProductsRegistered(std::function< void(BranchDescription const &)> const &func)
Definition: ProducerBase.h:76
bool wasrun() const
Was at least one path run?
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
edm::EDGetTokenT< TriggerConditionCollection > triggerConditionCollectionToken_
std::string const & processName() const
static PFTauRenderPlugin instance
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
bool getByToken(EDGetToken token, Handle< PROD > &result) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool accept() const
Has at least one path accepted the event?
boost::uint32_t lhcFillNumber
bool exists(std::string const &parameterName) const
checks if a parameter exists
boost::uint32_t totalIntensityBeam1
std::string const & processName() const
const std::string & tableName() const
HLT ConfDB table name.
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
edm::EDGetTokenT< edm::ConditionsInLumiBlock > tagCondGtLumiToken_
Produces the central entry point to full PAT trigger information.
bool isRealData() const
Definition: EventBase.h:64
std::string encode() const
Definition: InputTag.cc:166
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:230
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:287
boost::uint32_t totalIntensityBeam2
Helper class that fetches some type of Ref given ProductID and index, using the edm::Event.
PATTriggerEventProducer(const edm::ParameterSet &iConfig)
edm::GetterOfProducts< edm::TriggerResults > triggerResultsGetter_
bool error() const
Has any path encountered an error (exception)
edm::EDGetTokenT< TriggerObjectCollection > triggerObjectCollectionToken_
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:330
ProcessHistory const & processHistory() const
Definition: Run.cc:120
std::vector< edm::EDGetTokenT< TriggerObjectStandAloneMatch > > triggerMatcherTokens_
boost::uint16_t bstMasterStatus
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:464
std::vector< edm::InputTag > tagsTriggerMatcher_
ModuleDescription const & moduleDescription() const
virtual void beginLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
edm::EDGetTokenT< L1GlobalTriggerReadoutRecord > l1GtToken_
const L1GtFdlWord gtFdlWord(int bxInEventValue) const
get / set FDL word (record) in the GT readout record
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
const T & get() const
Definition: EventSetup.h:55
const cms_uint16_t physicsDeclared() const
get/set "physics declared" bit
Definition: L1GtFdlWord.h:227
edm::ConditionsInRunBlock condRun_
edm::EDGetTokenT< edm::ConditionsInRunBlock > tagCondGtRunToken_
std::string const & label() const
Definition: InputTag.h:36
std::string const & process() const
Definition: InputTag.h:40
HLT enums.
boost::uint32_t turnCountNumber
edm::EDGetTokenT< edm::ConditionsInEventBlock > tagCondGtEventToken_
edm::ConditionsInLumiBlock condLumi_
boost::uint16_t beamMomentum
std::string const & instance() const
Definition: InputTag.h:37
virtual void beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
def move(src, dest)
Definition: eostools.py:510
edm::EDGetTokenT< TriggerFilterCollection > triggerFilterCollectionToken_
Definition: Run.h:43
edm::EDGetTokenT< TriggerPathCollection > triggerPathCollectionToken_
edm::EDGetTokenT< TriggerAlgorithmCollection > triggerAlgorithmCollectionToken_