CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PATTriggerEventProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PatAlgos
4 // Class: pat::PATTriggerEventProducer
5 //
6 //
46 
47 #include <cassert>
48 #include <string>
49 #include <vector>
50 
51 namespace pat {
52 
54  public:
57 
58  private:
59  void beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) override;
60  void beginLuminosityBlock(const edm::LuminosityBlock& iLumi, const edm::EventSetup& iSetup) override;
61  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
62 
63  std::string nameProcess_; // configuration
65  edm::InputTag tagTriggerProducer_; // configuration (optional with default)
71  std::vector<edm::InputTag> tagsTriggerMatcher_; // configuration (optional)
72  std::vector<edm::EDGetTokenT<TriggerObjectStandAloneMatch> > triggerMatcherTokens_;
73  // L1
74  edm::InputTag tagL1Gt_; // configuration (optional with default)
76  // HLT
79  edm::InputTag tagTriggerResults_; // configuration (optional with default)
81  edm::InputTag tagTriggerEvent_; // configuration (optional with default)
82  // Conditions
83  edm::InputTag tagCondGt_; // configuration (optional with default)
89  bool gtCondRunInit_ = false;
90  bool gtCondLumiInit_ = false;
92  };
93 
94 } // namespace pat
95 
96 using namespace pat;
97 using namespace edm;
98 
100  : nameProcess_(iConfig.getParameter<std::string>("processName")),
101  autoProcessName_(nameProcess_ == "*"),
102  tagTriggerProducer_("patTrigger"),
103  tagsTriggerMatcher_(),
104  // L1 configuration parameters
105  tagL1Gt_(),
106  // HLTConfigProvider
107  hltConfigInit_(false),
108  // HLT configuration parameters
109  tagTriggerResults_("TriggerResults"),
110  tagTriggerEvent_("hltTriggerSummaryAOD"),
111  // Conditions configuration parameters
112  tagCondGt_(),
113  // Conditions
114  condRun_(),
115  condLumi_(),
116  handleL1GtTriggerMenuToken_{esConsumes()} {
117  if (iConfig.exists("triggerResults"))
118  tagTriggerResults_ = iConfig.getParameter<InputTag>("triggerResults");
119  triggerResultsGetter_ =
120  GetterOfProducts<TriggerResults>(InputTagMatch(InputTag(tagTriggerResults_.label(),
121  tagTriggerResults_.instance(),
122  autoProcessName_ ? std::string("") : nameProcess_)),
123  this);
124  if (iConfig.exists("triggerEvent"))
125  tagTriggerEvent_ = iConfig.getParameter<InputTag>("triggerEvent");
126  if (iConfig.exists("patTriggerProducer"))
127  tagTriggerProducer_ = iConfig.getParameter<InputTag>("patTriggerProducer");
128  triggerAlgorithmCollectionToken_ = mayConsume<TriggerAlgorithmCollection>(tagTriggerProducer_);
129  triggerConditionCollectionToken_ = mayConsume<TriggerConditionCollection>(tagTriggerProducer_);
130  triggerPathCollectionToken_ = mayConsume<TriggerPathCollection>(tagTriggerProducer_);
131  triggerFilterCollectionToken_ = mayConsume<TriggerFilterCollection>(tagTriggerProducer_);
132  triggerObjectCollectionToken_ = mayConsume<TriggerObjectCollection>(tagTriggerProducer_);
133  if (iConfig.exists("condGtTag")) {
134  tagCondGt_ = iConfig.getParameter<InputTag>("condGtTag");
135  tagCondGtRunToken_ = mayConsume<ConditionsInRunBlock, InRun>(tagCondGt_);
136  tagCondGtLumiToken_ = mayConsume<ConditionsInLumiBlock, InLumi>(tagCondGt_);
137  tagCondGtEventToken_ = mayConsume<ConditionsInEventBlock>(tagCondGt_);
138  }
139  if (iConfig.exists("l1GtTag"))
140  tagL1Gt_ = iConfig.getParameter<InputTag>("l1GtTag");
141  l1GtToken_ = mayConsume<L1GlobalTriggerReadoutRecord>(tagL1Gt_);
142  if (iConfig.exists("patTriggerMatches"))
143  tagsTriggerMatcher_ = iConfig.getParameter<std::vector<InputTag> >("patTriggerMatches");
144  triggerMatcherTokens_ = vector_transform(
145  tagsTriggerMatcher_, [this](InputTag const& tag) { return mayConsume<TriggerObjectStandAloneMatch>(tag); });
146 
147  callWhenNewProductsRegistered([this](BranchDescription const& bd) {
148  if (not(this->autoProcessName_ and bd.processName() == this->moduleDescription().processName())) {
149  triggerResultsGetter_(bd);
150  }
151  });
152 
153  for (size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch) {
154  produces<TriggerObjectMatch>(tagsTriggerMatcher_.at(iMatch).label());
155  }
156  produces<TriggerEvent>();
157 }
158 
159 void PATTriggerEventProducer::beginRun(const Run& iRun, const EventSetup& iSetup) {
160  // Initialize process name
161  if (autoProcessName_) {
162  // reset
163  nameProcess_ = "*";
164  // determine process name from last run TriggerSummaryProducerAOD module in process history of input
165  const ProcessHistory& processHistory(iRun.processHistory());
167  ParameterSet processPSet;
168  // unbroken loop, which relies on time ordering (accepts the last found entry)
169  for (ProcessHistory::const_iterator iHist = processHistory.begin(); iHist != processHistory.end(); ++iHist) {
170  if (processHistory.getConfigurationForProcess(iHist->processName(), processConfiguration) &&
171  pset::Registry::instance()->getMapped(processConfiguration.parameterSetID(), processPSet) &&
172  processPSet.exists(tagTriggerEvent_.label())) {
173  nameProcess_ = iHist->processName();
174  LogDebug("autoProcessName") << "HLT process name '" << nameProcess_ << "' discovered";
175  }
176  }
177  // terminate, if nothing is found
178  if (nameProcess_ == "*") {
179  LogError("autoProcessName") << "trigger::TriggerEvent product with label '" << tagTriggerEvent_.label()
180  << "' not produced according to process history of input data\n"
181  << "No trigger information produced.";
182  return;
183  }
184  LogInfo("autoProcessName") << "HLT process name " << nameProcess_ << " used for PAT trigger information";
185  }
186  // adapt configuration of used input tags
187  if (tagTriggerResults_.process().empty() || tagTriggerResults_.process() == "*") {
189  } else if (tagTriggerResults_.process() != nameProcess_) {
190  LogWarning("triggerResultsTag") << "TriggerResults process name '" << tagTriggerResults_.process()
191  << "' differs from HLT process name '" << nameProcess_ << "'";
192  }
193  if (tagTriggerEvent_.process().empty() || tagTriggerEvent_.process() == "*") {
195  } else if (tagTriggerEvent_.process() != nameProcess_) {
196  LogWarning("triggerEventTag") << "TriggerEvent process name '" << tagTriggerEvent_.process()
197  << "' differs from HLT process name '" << nameProcess_ << "'";
198  }
199 
200  gtCondRunInit_ = false;
201  if (!tagCondGt_.label().empty()) {
202  Handle<ConditionsInRunBlock> condRunBlock;
203  iRun.getByToken(tagCondGtRunToken_, condRunBlock);
204  if (condRunBlock.isValid()) {
205  condRun_ = *condRunBlock;
206  gtCondRunInit_ = true;
207  } else {
208  LogError("conditionsInEdm") << "ConditionsInRunBlock product with InputTag '" << tagCondGt_.encode()
209  << "' not in run";
210  }
211  }
212 
213  // Initialize HLTConfigProvider
214  hltConfigInit_ = false;
215  bool changed(true);
216  if (!hltConfig_.init(iRun, iSetup, nameProcess_, changed)) {
217  LogError("hltConfigExtraction") << "HLT config extraction error with process name '" << nameProcess_ << "'";
218  } else if (hltConfig_.size() <= 0) {
219  LogError("hltConfigSize") << "HLT config size error";
220  } else
221  hltConfigInit_ = true;
222 }
223 
224 void PATTriggerEventProducer::beginLuminosityBlock(const LuminosityBlock& iLuminosityBlock, const EventSetup& iSetup) {
225  // Terminate, if auto process name determination failed
226  if (nameProcess_ == "*")
227  return;
228 
229  gtCondLumiInit_ = false;
230  if (!tagCondGt_.label().empty()) {
231  Handle<ConditionsInLumiBlock> condLumiBlock;
232  iLuminosityBlock.getByToken(tagCondGtLumiToken_, condLumiBlock);
233  if (condLumiBlock.isValid()) {
234  condLumi_ = *condLumiBlock;
235  gtCondLumiInit_ = true;
236  } else {
237  LogError("conditionsInEdm") << "ConditionsInLumiBlock product with InputTag '" << tagCondGt_.encode()
238  << "' not in lumi";
239  }
240  }
241 }
242 
244  // Terminate, if auto process name determination failed
245  if (nameProcess_ == "*")
246  return;
247 
248  if (!hltConfigInit_)
249  return;
250 
251  auto handleL1GtTriggerMenu = iSetup.getHandle(handleL1GtTriggerMenuToken_);
252  Handle<TriggerResults> handleTriggerResults;
253  iEvent.getByLabel(tagTriggerResults_, handleTriggerResults);
254  // iEvent.getByToken( triggerResultsToken_, handleTriggerResults );
255  if (!handleTriggerResults.isValid()) {
256  LogError("triggerResultsValid") << "TriggerResults product with InputTag '" << tagTriggerResults_.encode()
257  << "' not in event\n"
258  << "No trigger information produced";
259  return;
260  }
261  Handle<TriggerAlgorithmCollection> handleTriggerAlgorithms;
262  iEvent.getByToken(triggerAlgorithmCollectionToken_, handleTriggerAlgorithms);
263  Handle<TriggerConditionCollection> handleTriggerConditions;
264  iEvent.getByToken(triggerConditionCollectionToken_, handleTriggerConditions);
265  Handle<TriggerPathCollection> handleTriggerPaths;
266  iEvent.getByToken(triggerPathCollectionToken_, handleTriggerPaths);
267  Handle<TriggerFilterCollection> handleTriggerFilters;
268  iEvent.getByToken(triggerFilterCollectionToken_, handleTriggerFilters);
269  Handle<TriggerObjectCollection> handleTriggerObjects;
270  iEvent.getByToken(triggerObjectCollectionToken_, handleTriggerObjects);
271 
272  bool physDecl(false);
273  if (iEvent.isRealData() && !tagL1Gt_.label().empty()) {
274  Handle<L1GlobalTriggerReadoutRecord> handleL1GlobalTriggerReadoutRecord;
275  iEvent.getByToken(l1GtToken_, handleL1GlobalTriggerReadoutRecord);
276  if (handleL1GlobalTriggerReadoutRecord.isValid()) {
277  L1GtFdlWord fdlWord = handleL1GlobalTriggerReadoutRecord->gtFdlWord();
278  if (fdlWord.physicsDeclared() == 1) {
279  physDecl = true;
280  }
281  } else {
282  LogError("l1GlobalTriggerReadoutRecordValid")
283  << "L1GlobalTriggerReadoutRecord product with InputTag '" << tagL1Gt_.encode() << "' not in event";
284  }
285  } else {
286  physDecl = true;
287  }
288 
289  // produce trigger event
290 
291  auto triggerEvent = std::make_unique<TriggerEvent>(handleL1GtTriggerMenu->gtTriggerMenuName(),
293  handleTriggerResults->wasrun(),
294  handleTriggerResults->accept(),
295  handleTriggerResults->error(),
296  physDecl);
297  // set product references to trigger collections
298  if (handleTriggerAlgorithms.isValid()) {
299  triggerEvent->setAlgorithms(handleTriggerAlgorithms);
300  } else {
301  LogError("triggerAlgorithmsValid") << "pat::TriggerAlgorithmCollection product with InputTag '"
302  << tagTriggerProducer_.encode() << "' not in event";
303  }
304  if (handleTriggerConditions.isValid()) {
305  triggerEvent->setConditions(handleTriggerConditions);
306  } else {
307  LogError("triggerConditionsValid") << "pat::TriggerConditionCollection product with InputTag '"
308  << tagTriggerProducer_.encode() << "' not in event";
309  }
310  if (handleTriggerPaths.isValid()) {
311  triggerEvent->setPaths(handleTriggerPaths);
312  } else {
313  LogError("triggerPathsValid") << "pat::TriggerPathCollection product with InputTag '"
314  << tagTriggerProducer_.encode() << "' not in event";
315  }
316  if (handleTriggerFilters.isValid()) {
317  triggerEvent->setFilters(handleTriggerFilters);
318  } else {
319  LogError("triggerFiltersValid") << "pat::TriggerFilterCollection product with InputTag '"
320  << tagTriggerProducer_.encode() << "' not in event";
321  }
322  if (handleTriggerObjects.isValid()) {
323  triggerEvent->setObjects(handleTriggerObjects);
324  } else {
325  LogError("triggerObjectsValid") << "pat::TriggerObjectCollection product with InputTag '"
326  << tagTriggerProducer_.encode() << "' not in event";
327  }
328  if (gtCondRunInit_) {
329  triggerEvent->setLhcFill(condRun_.lhcFillNumber);
330  triggerEvent->setBeamMode(condRun_.beamMode);
331  triggerEvent->setBeamMomentum(condRun_.beamMomentum);
332  triggerEvent->setBCurrentStart(condRun_.BStartCurrent);
333  triggerEvent->setBCurrentStop(condRun_.BStopCurrent);
334  triggerEvent->setBCurrentAvg(condRun_.BAvgCurrent);
335  }
336  if (gtCondLumiInit_) {
337  triggerEvent->setIntensityBeam1(condLumi_.totalIntensityBeam1);
338  triggerEvent->setIntensityBeam2(condLumi_.totalIntensityBeam2);
339  }
340  if (!tagCondGt_.label().empty()) {
341  Handle<ConditionsInEventBlock> condEventBlock;
342  iEvent.getByToken(tagCondGtEventToken_, condEventBlock);
343  if (condEventBlock.isValid()) {
344  triggerEvent->setBstMasterStatus(condEventBlock->bstMasterStatus);
345  triggerEvent->setTurnCount(condEventBlock->turnCountNumber);
346  } else {
347  LogError("conditionsInEdm") << "ConditionsInEventBlock product with InputTag '" << tagCondGt_.encode()
348  << "' not in event";
349  }
350  }
351 
352  // produce trigger match association and set references
353  if (handleTriggerObjects.isValid()) {
354  for (size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch) {
355  const std::string labelTriggerObjectMatcher(tagsTriggerMatcher_.at(iMatch).label());
356  // copy trigger match association using TriggerObjectStandAlone to those using TriggerObject
357  // relying on the fact, that only one candidate collection is present in the association
358  Handle<TriggerObjectStandAloneMatch> handleTriggerObjectStandAloneMatch;
359  iEvent.getByToken(triggerMatcherTokens_.at(iMatch), handleTriggerObjectStandAloneMatch);
360  if (!handleTriggerObjectStandAloneMatch.isValid()) {
361  LogError("triggerMatchValid") << "pat::TriggerObjectStandAloneMatch product with InputTag '"
362  << labelTriggerObjectMatcher << "' not in event";
363  continue;
364  }
365  auto it = makeAssociativeIterator<reco::CandidateBaseRef>(*handleTriggerObjectStandAloneMatch, iEvent);
366  auto itEnd = it.end();
367  Handle<reco::CandidateView> handleCands;
368  if (it != itEnd)
369  iEvent.get(it->first.id(), handleCands);
370  std::vector<int> indices;
371  while (it != itEnd) {
372  indices.push_back(it->second.key());
373  ++it;
374  }
375  auto triggerObjectMatch = std::make_unique<TriggerObjectMatch>(handleTriggerObjects);
376  TriggerObjectMatch::Filler matchFiller(*triggerObjectMatch);
377  if (handleCands.isValid()) {
378  matchFiller.insert(handleCands, indices.begin(), indices.end());
379  }
380  matchFiller.fill();
381  OrphanHandle<TriggerObjectMatch> handleTriggerObjectMatch(
382  iEvent.put(std::move(triggerObjectMatch), labelTriggerObjectMatcher));
383  // set product reference to trigger match association
384  if (!handleTriggerObjectMatch.isValid()) {
385  LogError("triggerMatchValid") << "pat::TriggerObjectMatch product with InputTag '" << labelTriggerObjectMatcher
386  << "' not in event";
387  continue;
388  }
389  if (!(triggerEvent->addObjectMatchResult(handleTriggerObjectMatch, labelTriggerObjectMatcher))) {
390  LogWarning("triggerObjectMatchReplication")
391  << "pat::TriggerEvent contains already a pat::TriggerObjectMatch from matcher module '"
392  << labelTriggerObjectMatcher << "'";
393  }
394  }
395  }
396 
397  iEvent.put(std::move(triggerEvent));
398 }
399 
unsigned int size() const
number of trigger paths in trigger table
collection_type::const_iterator const_iterator
edm::EDGetTokenT< TriggerObjectCollection > triggerObjectCollectionToken_
std::vector< edm::EDGetTokenT< TriggerObjectStandAloneMatch > > triggerMatcherTokens_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::EDGetTokenT< edm::ConditionsInLumiBlock > tagCondGtLumiToken_
edm::EDGetTokenT< TriggerConditionCollection > triggerConditionCollectionToken_
const edm::ESGetToken< L1GtTriggerMenu, L1GtTriggerMenuRcd > handleL1GtTriggerMenuToken_
static PFTauRenderPlugin instance
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
bool getByToken(EDGetToken token, Handle< PROD > &result) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::GetterOfProducts< edm::TriggerResults > triggerResultsGetter_
bool exists(std::string const &parameterName) const
checks if a parameter exists
processConfiguration
Definition: Schedule.cc:687
edm::EDGetTokenT< TriggerPathCollection > triggerPathCollectionToken_
std::string const & processName() const
const std::string & tableName() const
HLT ConfDB table name.
Log< level::Error, false > LogError
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
Produces the central entry point to full PAT trigger information.
bool isRealData() const
Definition: EventBase.h:62
std::string encode() const
Definition: InputTag.cc:159
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< edm::ConditionsInEventBlock > tagCondGtEventToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:318
PATTriggerEventProducer(const edm::ParameterSet &iConfig)
def move
Definition: eostools.py:511
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
ProcessHistory const & processHistory() const
Definition: Run.cc:113
edm::EDGetTokenT< edm::ConditionsInRunBlock > tagCondGtRunToken_
bool isValid() const
Definition: HandleBase.h:70
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:500
Log< level::Info, false > LogInfo
void beginLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
edm::EDGetTokenT< L1GlobalTriggerReadoutRecord > l1GtToken_
edm::EDGetTokenT< TriggerAlgorithmCollection > triggerAlgorithmCollectionToken_
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
const cms_uint16_t physicsDeclared() const
get/set &quot;physics declared&quot; bit
Definition: L1GtFdlWord.h:169
edm::ConditionsInRunBlock condRun_
std::string const & label() const
Definition: InputTag.h:36
std::string const & process() const
Definition: InputTag.h:40
std::vector< edm::InputTag > tagsTriggerMatcher_
edm::EDGetTokenT< TriggerFilterCollection > triggerFilterCollectionToken_
edm::ConditionsInLumiBlock condLumi_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
Log< level::Warning, false > LogWarning
list indices
Definition: dqmdumpme.py:50
std::string const & instance() const
Definition: InputTag.h:37
void beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
Definition: Run.h:45
#define LogDebug(id)