CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATTriggerEventProducer.cc
Go to the documentation of this file.
1 //
2 // $Id: PATTriggerEventProducer.cc,v 1.19 2013/02/27 23:26:56 wmtan Exp $
3 //
4 
5 
7 
8 #include <cassert>
9 
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" );
51  if ( iConfig.exists( "triggerEvent" ) ) tagTriggerEvent_ = iConfig.getParameter< InputTag >( "triggerEvent" );
52  if ( iConfig.exists( "patTriggerProducer" ) ) tagTriggerProducer_ = iConfig.getParameter< InputTag >( "patTriggerProducer" );
53  if ( iConfig.exists( "condGtTag" ) ) tagCondGt_ = iConfig.getParameter< InputTag >( "condGtTag" );
54  if ( iConfig.exists( "l1GtTag" ) ) tagL1Gt_ = iConfig.getParameter< InputTag >( "l1GtTag" );
55  if ( iConfig.exists( "patTriggerMatches" ) ) tagsTriggerMatcher_ = iConfig.getParameter< std::vector< InputTag > >( "patTriggerMatches" );
56 
57  for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
58  produces< TriggerObjectMatch >( tagsTriggerMatcher_.at( iMatch ).label() );
59  }
60  produces< TriggerEvent >();
61 
62 }
63 
64 
65 void PATTriggerEventProducer::beginRun(const Run & iRun, const EventSetup & iSetup )
66 {
67 
68  // Initialize process name
69  if ( autoProcessName_ ) {
70  // reset
71  nameProcess_ = "*";
72  // determine process name from last run TriggerSummaryProducerAOD module in process history of input
73  const ProcessHistory & processHistory( iRun.processHistory() );
74  ProcessConfiguration processConfiguration;
75  ParameterSet processPSet;
76  // unbroken loop, which relies on time ordering (accepts the last found entry)
77  for ( ProcessHistory::const_iterator iHist = processHistory.begin(); iHist != processHistory.end(); ++iHist ) {
78  if ( processHistory.getConfigurationForProcess( iHist->processName(), processConfiguration ) &&
79  pset::Registry::instance()->getMapped( processConfiguration.parameterSetID(), processPSet ) &&
80  processPSet.exists( tagTriggerEvent_.label() )
81  ) {
82  nameProcess_ = iHist->processName();
83  LogDebug( "autoProcessName" ) << "HLT process name '" << nameProcess_ << "' discovered";
84  }
85  }
86  // terminate, if nothing is found
87  if ( nameProcess_ == "*" ) {
88  LogError( "autoProcessName" ) << "trigger::TriggerEvent product with label '" << tagTriggerEvent_.label() << "' not produced according to process history of input data\n"
89  << "No trigger information produced.";
90  return;
91  }
92  LogInfo( "autoProcessName" ) << "HLT process name " << nameProcess_ << " used for PAT trigger information";
93  }
94  // adapt configuration of used input tags
95  if ( tagTriggerResults_.process().empty() || tagTriggerResults_.process() == "*" ) {
97  } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
98  LogWarning( "triggerResultsTag" ) << "TriggerResults process name '" << tagTriggerResults_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
99  }
100  if ( tagTriggerEvent_.process().empty() || tagTriggerEvent_.process() == "*" ) {
102  } else if ( tagTriggerEvent_.process() != nameProcess_ ) {
103  LogWarning( "triggerEventTag" ) << "TriggerEvent process name '" << tagTriggerEvent_.process() << "' differs from HLT process name '" << nameProcess_ << "'";
104  }
105 
106  gtCondRunInit_ = false;
107  if ( ! tagCondGt_.label().empty() ) {
108  Handle< ConditionsInRunBlock > condRunBlock;
109  iRun.getByLabel( tagCondGt_, condRunBlock );
110  if ( condRunBlock.isValid() ) {
111  condRun_ = *condRunBlock;
112  gtCondRunInit_ = true;
113  } else {
114  LogError( "conditionsInEdm" ) << "ConditionsInRunBlock product with InputTag '" << tagCondGt_.encode() << "' not in run";
115  }
116  }
117 
118  // Initialize HLTConfigProvider
119  hltConfigInit_ = false;
120  bool changed( true );
121  if ( ! hltConfig_.init( iRun, iSetup, nameProcess_, changed ) ) {
122  LogError( "hltConfigExtraction" ) << "HLT config extraction error with process name '" << nameProcess_ << "'";
123  } else if ( hltConfig_.size() <= 0 ) {
124  LogError( "hltConfigSize" ) << "HLT config size error";
125  } else hltConfigInit_ = true;
126 
127 }
128 
129 void PATTriggerEventProducer::beginLuminosityBlock(const LuminosityBlock & iLuminosityBlock, const EventSetup & iSetup )
130 {
131 
132  // Terminate, if auto process name determination failed
133  if ( nameProcess_ == "*" ) return;
134 
135  gtCondLumiInit_ = false;
136  if ( ! tagCondGt_.label().empty() ) {
137  Handle< ConditionsInLumiBlock > condLumiBlock;
138  iLuminosityBlock.getByLabel( tagCondGt_, condLumiBlock );
139  if ( condLumiBlock.isValid() ) {
140  condLumi_ = *condLumiBlock;
141  gtCondLumiInit_ = true;
142  } else {
143  LogError( "conditionsInEdm" ) << "ConditionsInLumiBlock product with InputTag '" << tagCondGt_.encode() << "' not in lumi";
144  }
145  }
146 
147 }
148 
149 
151 {
152 
153  // Terminate, if auto process name determination failed
154  if ( nameProcess_ == "*" ) return;
155 
156  if ( ! hltConfigInit_ ) return;
157 
158  ESHandle< L1GtTriggerMenu > handleL1GtTriggerMenu;
159  iSetup.get< L1GtTriggerMenuRcd >().get( handleL1GtTriggerMenu );
160  Handle< TriggerResults > handleTriggerResults;
161  iEvent.getByLabel( tagTriggerResults_, handleTriggerResults );
162  if ( ! handleTriggerResults.isValid() ) {
163  LogError( "triggerResultsValid" ) << "TriggerResults product with InputTag '" << tagTriggerResults_.encode() << "' not in event\n"
164  << "No trigger information produced";
165  return;
166  }
167  Handle< TriggerAlgorithmCollection > handleTriggerAlgorithms;
168  iEvent.getByLabel( tagTriggerProducer_, handleTriggerAlgorithms );
169  Handle< TriggerConditionCollection > handleTriggerConditions;
170  iEvent.getByLabel( tagTriggerProducer_, handleTriggerConditions );
171  Handle< TriggerPathCollection > handleTriggerPaths;
172  iEvent.getByLabel( tagTriggerProducer_, handleTriggerPaths );
173  Handle< TriggerFilterCollection > handleTriggerFilters;
174  iEvent.getByLabel( tagTriggerProducer_, handleTriggerFilters );
175  Handle< TriggerObjectCollection > handleTriggerObjects;
176  iEvent.getByLabel( tagTriggerProducer_, handleTriggerObjects );
177 
178  bool physDecl( false );
179  if ( iEvent.isRealData() && ! tagL1Gt_.label().empty() ) {
180  Handle< L1GlobalTriggerReadoutRecord > handleL1GlobalTriggerReadoutRecord;
181  iEvent.getByLabel( tagL1Gt_, handleL1GlobalTriggerReadoutRecord );
182  if ( handleL1GlobalTriggerReadoutRecord.isValid() ) {
183  L1GtFdlWord fdlWord = handleL1GlobalTriggerReadoutRecord->gtFdlWord();
184  if ( fdlWord.physicsDeclared() == 1 ) {
185  physDecl = true;
186  }
187  } else {
188  LogError( "l1GlobalTriggerReadoutRecordValid" ) << "L1GlobalTriggerReadoutRecord product with InputTag '" << tagL1Gt_.encode() << "' not in event";
189  }
190  } else {
191  physDecl = true;
192  }
193 
194 
195  // produce trigger event
196 
197  std::auto_ptr< TriggerEvent > triggerEvent( new TriggerEvent( handleL1GtTriggerMenu->gtTriggerMenuName(), std::string( hltConfig_.tableName() ), handleTriggerResults->wasrun(), handleTriggerResults->accept(), handleTriggerResults->error(), physDecl ) );
198  // set product references to trigger collections
199  if ( handleTriggerAlgorithms.isValid() ) {
200  triggerEvent->setAlgorithms( handleTriggerAlgorithms );
201  } else {
202  LogError( "triggerAlgorithmsValid" ) << "pat::TriggerAlgorithmCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
203  }
204  if ( handleTriggerConditions.isValid() ) {
205  triggerEvent->setConditions( handleTriggerConditions );
206  } else {
207  LogError( "triggerConditionsValid" ) << "pat::TriggerConditionCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
208  }
209  if ( handleTriggerPaths.isValid() ) {
210  triggerEvent->setPaths( handleTriggerPaths );
211  } else {
212  LogError( "triggerPathsValid" ) << "pat::TriggerPathCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
213  }
214  if ( handleTriggerFilters.isValid() ) {
215  triggerEvent->setFilters( handleTriggerFilters );
216  } else {
217  LogError( "triggerFiltersValid" ) << "pat::TriggerFilterCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
218  }
219  if ( handleTriggerObjects.isValid() ) {
220  triggerEvent->setObjects( handleTriggerObjects );
221  } else {
222  LogError( "triggerObjectsValid" ) << "pat::TriggerObjectCollection product with InputTag '" << tagTriggerProducer_.encode() << "' not in event";
223  }
224  if ( gtCondRunInit_ ) {
225  triggerEvent->setLhcFill( condRun_.lhcFillNumber );
226  triggerEvent->setBeamMode( condRun_.beamMode );
227  triggerEvent->setBeamMomentum( condRun_.beamMomentum );
228  triggerEvent->setBCurrentStart( condRun_.BStartCurrent );
229  triggerEvent->setBCurrentStop( condRun_.BStopCurrent );
230  triggerEvent->setBCurrentAvg( condRun_.BAvgCurrent );
231  }
232  if ( gtCondLumiInit_ ) {
233  triggerEvent->setIntensityBeam1( condLumi_.totalIntensityBeam1 );
234  triggerEvent->setIntensityBeam2( condLumi_.totalIntensityBeam2 );
235  }
236  if ( ! tagCondGt_.label().empty() ) {
237  Handle< ConditionsInEventBlock > condEventBlock;
238  iEvent.getByLabel( tagCondGt_, condEventBlock );
239  if ( condEventBlock.isValid() ) {
240  triggerEvent->setBstMasterStatus( condEventBlock->bstMasterStatus );
241  triggerEvent->setTurnCount( condEventBlock->turnCountNumber );
242  } else {
243  LogError( "conditionsInEdm" ) << "ConditionsInEventBlock product with InputTag '" << tagCondGt_.encode() << "' not in event";
244  }
245  }
246 
247  // produce trigger match association and set references
248  if ( handleTriggerObjects.isValid() ) {
249  for ( size_t iMatch = 0; iMatch < tagsTriggerMatcher_.size(); ++iMatch ) {
250  const std::string labelTriggerObjectMatcher( tagsTriggerMatcher_.at( iMatch ).label() );
251  // copy trigger match association using TriggerObjectStandAlone to those using TriggerObject
252  // relying on the fact, that only one candidate collection is present in the association
253  Handle< TriggerObjectStandAloneMatch > handleTriggerObjectStandAloneMatch;
254  iEvent.getByLabel( labelTriggerObjectMatcher, handleTriggerObjectStandAloneMatch );
255  if ( ! handleTriggerObjectStandAloneMatch.isValid() ) {
256  LogError( "triggerMatchValid" ) << "pat::TriggerObjectStandAloneMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
257  continue;
258  }
259  AssociativeIterator< reco::CandidateBaseRef, TriggerObjectStandAloneMatch > it( *handleTriggerObjectStandAloneMatch, EdmEventItemGetter< reco::CandidateBaseRef >( iEvent ) ), itEnd( it.end() );
260  Handle< reco::CandidateView > handleCands;
261  if ( it != itEnd ) iEvent.get( it->first.id(), handleCands );
262  std::vector< int > indices;
263  while ( it != itEnd ) {
264  indices.push_back( it->second.key() );
265  ++it;
266  }
267  std::auto_ptr< TriggerObjectMatch > triggerObjectMatch( new TriggerObjectMatch( handleTriggerObjects ) );
268  TriggerObjectMatch::Filler matchFiller( *triggerObjectMatch );
269  if ( handleCands.isValid() ) {
270  matchFiller.insert( handleCands, indices.begin(), indices.end() );
271  }
272  matchFiller.fill();
273  OrphanHandle< TriggerObjectMatch > handleTriggerObjectMatch( iEvent.put( triggerObjectMatch, labelTriggerObjectMatcher ) );
274  // set product reference to trigger match association
275  if ( ! handleTriggerObjectMatch.isValid() ) {
276  LogError( "triggerMatchValid" ) << "pat::TriggerObjectMatch product with InputTag '" << labelTriggerObjectMatcher << "' not in event";
277  continue;
278  }
279  if ( ! ( triggerEvent->addObjectMatchResult( handleTriggerObjectMatch, labelTriggerObjectMatcher ) ) ) {
280  LogWarning( "triggerObjectMatchReplication" ) << "pat::TriggerEvent contains already a pat::TriggerObjectMatch from matcher module '" << labelTriggerObjectMatcher << "'";
281  }
282  }
283  }
284 
285  iEvent.put( triggerEvent );
286 
287 }
288 
289 
#define LogDebug(id)
unsigned int size() const
number of trigger paths in trigger table
collection_type::const_iterator const_iterator
T getParameter(std::string const &) const
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:176
static PFTauRenderPlugin instance
edm::Association< TriggerObjectCollection > TriggerObjectMatch
Association of TriggerObjects to store matches to Candidates.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
boost::uint32_t lhcFillNumber
bool exists(std::string const &parameterName) const
checks if a parameter exists
boost::uint32_t totalIntensityBeam1
const std::string & tableName() const
HLT ConfDB table name.
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Produces the central entry point to full PAT trigger information.
bool isRealData() const
Definition: EventBase.h:60
std::string encode() const
Definition: InputTag.cc:164
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
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)
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:282
ProcessHistory const & processHistory() const
Definition: Run.cc:101
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
std::vector< edm::InputTag > tagsTriggerMatcher_
Analysis-level trigger event class.
Definition: TriggerEvent.h:44
virtual void beginLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) override
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 &quot;physics declared&quot; bit
Definition: L1GtFdlWord.h:229
edm::ConditionsInRunBlock condRun_
std::string const & label() const
Definition: InputTag.h:42
std::string const & process() const
Definition: InputTag.h:46
edm::ConditionsInLumiBlock condLumi_
boost::uint16_t beamMomentum
std::string const & instance() const
Definition: InputTag.h:43
virtual void beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
Definition: Run.h:36