CMS 3D CMS Logo

TriggerMatchProducer.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_TagAndProbe_TriggerMatchProducer_h
2 #define PhysicsTools_TagAndProbe_TriggerMatchProducer_h
3 
4 // system include files
5 #include <memory>
6 
7 // user include files
22 
23 #include <string>
24 #include <cmath>
25 #include <TString.h>
26 #include <TRegexp.h>
27 
28 // forward declarations
29 template <class object>
31 public:
32  explicit TriggerMatchProducer(const edm::ParameterSet&);
33  ~TriggerMatchProducer() override;
34 
35 private:
36  void beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) override;
37  void produce(edm::Event&, const edm::EventSetup&) override;
38 
39  // ----------member data --------------------------
40 
51  // bool isFilter_;
52  // bool printIndex_;
53  bool changed_;
55 };
56 
57 template <class object>
59  : hltPrescaleProvider_(iConfig, consumesCollector(), *this) {
60  _inputProducer = iConfig.template getParameter<edm::InputTag>("InputProducer");
61  _inputProducerToken = consumes<edm::View<object> >(_inputProducer);
62 
63  // **************** Trigger ******************* //
64  const edm::InputTag dTriggerEventTag("hltTriggerSummaryAOD", "", "HLT");
65  triggerEventTag_ = iConfig.getUntrackedParameter<edm::InputTag>("triggerEventTag", dTriggerEventTag);
66  triggerEventToken_ = consumes<trigger::TriggerEvent>(triggerEventTag_);
67 
68  const edm::InputTag dTriggerResults("TriggerResults", "", "HLT");
69  // By default, trigger results are labeled "TriggerResults" with process name "HLT" in the event.
70  triggerResultsTag_ = iConfig.getUntrackedParameter<edm::InputTag>("triggerResultsTag", dTriggerResults);
71  triggerResultsToken_ = consumes<edm::TriggerResults>(triggerResultsTag_);
72 
73  // const edm::InputTag dHLTTag("HLT_Ele15_LW_L1R", "","HLT8E29");
74  hltTag_ = iConfig.getUntrackedParameter<std::string>("hltTag", "HLT_Ele*");
75 
76  delRMatchingCut_ = iConfig.getUntrackedParameter<double>("triggerDelRMatch", 0.30);
77  // ******************************************** //
78  //Trigger path VS l1 trigger filter. Trigger Path is default.
79  // isFilter_ = iConfig.getUntrackedParameter<bool>("isTriggerFilter",false);
80  // printIndex_ = iConfig.getUntrackedParameter<bool>("verbose",false);
81 
82  produces<edm::PtrVector<object> >();
83  produces<edm::RefToBaseVector<object> >("R");
84 
85  filterName_ = "";
86 }
87 
88 template <class object>
90 
91 //
92 // member functions
93 //
94 
95 // ------------ method called to produce the data ------------
96 template <class object>
98  using namespace std;
99  using namespace edm;
100  using namespace reco;
101  using namespace trigger;
102 
103  HLTConfigProvider const& hltConfig = hltPrescaleProvider_.hltConfigProvider();
104 
105  // Create the output collection
106  std::unique_ptr<edm::RefToBaseVector<object> > outColRef(new edm::RefToBaseVector<object>);
107  std::unique_ptr<edm::PtrVector<object> > outColPtr(new edm::PtrVector<object>);
108 
109  // Get the input collection
110  edm::Handle<edm::View<object> > candHandle;
111  try {
112  event.getByToken(_inputProducerToken, candHandle);
113  } catch (cms::Exception& ex) {
114  edm::LogError("TriggerMatchProducer") << "Error! Can't get collection: " << _inputProducer;
115  throw ex;
116  }
117 
118  // Trigger Info
120  event.getByToken(triggerEventToken_, trgEvent);
122  event.getByToken(triggerResultsToken_, pTrgResults);
123 
124  //gracefully choose the single appropriate HLT path from the list of desired paths
125  std::vector<std::string> activeHLTPathsInThisEvent = hltConfig.triggerNames();
126  std::map<std::string, bool> triggerInMenu;
127  std::map<std::string, bool> triggerUnprescaled;
128  // for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin();
129  // iMyHLT != hltTags_.end(); ++iMyHLT) {
130  // if ( triggerInMenu.find((*iMyHLT).label()) == triggerInMenu.end() )
131  // triggerInMenu[(*iMyHLT).label()] = false;
132  // if ( triggerUnprescaled.find((*iMyHLT).label()) == triggerUnprescaled.end() )
133  // triggerUnprescaled[(*iMyHLT).label()] = false;
134  // }
135  for (std::vector<std::string>::const_iterator iHLT = activeHLTPathsInThisEvent.begin();
136  iHLT != activeHLTPathsInThisEvent.end();
137  ++iHLT) {
138  // cout << "######## " << *iHLT << endl;
139 
140  if (TString(*iHLT).Contains(TRegexp(hltTag_))) {
141  triggerInMenu[*iHLT] = true;
142  if (hltPrescaleProvider_.prescaleValue(event, eventSetup, *iHLT) == 1)
143  triggerUnprescaled[*iHLT] = true;
144  }
145  }
146 
147  // Some sanity checks
148  if (not trgEvent.isValid()) {
149  edm::LogInfo("info") << "******** Following Trigger Summary Object Not Found: " << triggerEventTag_;
150 
151  event.put(std::move(outColRef), "R");
152  event.put(std::move(outColPtr));
153  return;
154  }
155 
156  //---------------------------------------------------------------------------
157 
158  edm::InputTag filterTag;
159  // loop over these objects to see whether they match
160  const trigger::TriggerObjectCollection& TOC(trgEvent->getObjects());
161 
162  std::vector<int> index;
163  std::vector<std::string> filters;
164  // if(isFilter_){
165  //-----------------------------------------------------------------------
166  for (std::map<std::string, bool>::const_iterator iMyHLT = triggerInMenu.begin(); iMyHLT != triggerInMenu.end();
167  ++iMyHLT) {
168  if (!(iMyHLT->second && triggerUnprescaled[iMyHLT->first]))
169  continue;
170 
171  int triggerIndex = -1;
172  edm::InputTag filterTag;
173  try {
174  filters = hltConfig.moduleLabels(iMyHLT->first);
175  triggerIndex = hltConfig.triggerIndex(iMyHLT->first);
176  } catch (std::exception const&) {
177  cout << "bad trigger\n";
178  }
179  // Results from TriggerResults product
180  if (triggerIndex == -1 || !(pTrgResults->wasrun(triggerIndex)) || !(pTrgResults->accept(triggerIndex)) ||
181  (pTrgResults->error(triggerIndex))) {
182  continue;
183  }
184 
185  for (std::vector<std::string>::iterator filter = filters.begin(); filter != filters.end(); ++filter) {
186  edm::InputTag testTag(*filter, "", triggerEventTag_.process());
187  int testindex = trgEvent->filterIndex(testTag);
188  if (!(testindex >= trgEvent->sizeFilters())) {
189  filterName_ = *filter;
190  filterTag = testTag;
191  }
192  }
193 
194  //using last filter tag
195  index.push_back(trgEvent->filterIndex(filterTag));
196  // std::cout << "TrgPath" << iMyHLT->first << "hltTag_.label() " <<
197  // filterTag.label() << " filter name " <<
198  // filterName_ << " sizeFilters " <<
199  // trgEvent->sizeFilters() << std::endl;
200  }
201 
202  // Loop over the candidate collection
203  edm::PtrVector<object> ptrVect;
205  for (size_t i = 0; i < candHandle->size(); ++i) {
206  ptrVect.push_back(candHandle->ptrAt(i));
207  refs.push_back(candHandle->refAt(i));
208  }
209  // find how many objects there are
210  unsigned int counter = 0;
211  for (typename edm::View<object>::const_iterator j = candHandle->begin(); j != candHandle->end(); ++j, ++counter) {
212  bool hltTrigger = false;
213  for (unsigned int idx = 0; idx < index.size(); ++idx) {
214  if (hltTrigger)
215  continue;
216  const trigger::Keys& KEYS(trgEvent->filterKeys(index[idx]));
217  const size_type nK(KEYS.size());
218  // Get cut decision for each candidate
219  // Did this candidate cause a HLT trigger?
220 
221  for (int ipart = 0; ipart != nK; ++ipart) {
222  const trigger::TriggerObject& TO = TOC[KEYS[ipart]];
223  double dRval = deltaR(j->eta(), j->phi(), TO.eta(), TO.phi());
224  hltTrigger = dRval < delRMatchingCut_;
225  if (hltTrigger)
226  break;
227  }
228  }
229 
230  if (hltTrigger) {
231  outColRef->push_back(refs[counter]);
232  outColPtr->push_back(ptrVect[counter]);
233  }
234  }
235  event.put(std::move(outColRef), "R");
236  event.put(std::move(outColPtr));
237 }
238 
239 // ---- method called once each job just before starting event loop ---
240 template <class object>
242  // std::cout << "calling init(" << "iRun" << ", " << "iSetup" << ", " << triggerEventTag_.process() << ", " << "changed_" << ") in beginRun()" << std::endl;
243  if (!hltPrescaleProvider_.init(iRun, iSetup, triggerEventTag_.process(), changed_)) {
244  edm::LogError("TriggerMatchProducer") << "Error! Can't initialize HLTConfigProvider";
245  throw cms::Exception("HLTConfigProvider::init() returned non 0");
246  }
247  // HLTConfigProvider const& hltConfig = hltPrescaleProvider_.hltConfigProvider();
248  // if(printIndex_ && changed_)
249  // std::cout << "HLT configuration changed !" << std::endl;
250  // std::vector<std::string> filters = hltConfig.moduleLabels( hltTag_.label() );
251 }
252 #endif
trigger::TriggerObject::phi
float phi() const
Definition: TriggerObject.h:54
counter
Definition: counter.py:1
TriggerMatchProducer::triggerEventTag_
edm::InputTag triggerEventTag_
Definition: TriggerMatchProducer.h:43
TriggerMatchProducer::TriggerMatchProducer
TriggerMatchProducer(const edm::ParameterSet &)
Definition: TriggerMatchProducer.h:58
trigger::TriggerEvent::sizeFilters
trigger::size_type sizeFilters() const
Definition: TriggerEvent.h:146
mps_fire.i
i
Definition: mps_fire.py:428
filters
std::vector< TPRegexp > filters
Definition: eve_filter.cc:22
TriggerMatchProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: TriggerMatchProducer.h:97
trigger::TriggerEvent::filterKeys
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:118
MessageLogger.h
TriggerResults.h
edm::Run
Definition: Run.h:45
TriggerEvent.h
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
TriggerMatchProducer::beginRun
void beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup) override
Definition: TriggerMatchProducer.h:241
gather_cfg.cout
cout
Definition: gather_cfg.py:144
edm::HLTGlobalStatus::wasrun
bool wasrun() const
Was at least one path run?
Definition: HLTGlobalStatus.h:47
EDProducer.h
edm::HLTGlobalStatus::error
bool error() const
Has any path encountered an error (exception)
Definition: HLTGlobalStatus.h:51
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::Handle
Definition: AssociativeIterator.h:50
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
MakerMacros.h
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
edm::RefToBaseVector::push_back
void push_back(const RefToBase< T > &)
Definition: RefToBaseVector.h:217
edm::PtrVector
Definition: PtrVector.h:36
Service.h
TriggerMatchProducer::filterName_
std::string filterName_
Definition: TriggerMatchProducer.h:49
TriggerMatchProducer::storeRefCollection_
bool storeRefCollection_
Definition: TriggerMatchProducer.h:50
TriggerMatchProducer::hltPrescaleProvider_
HLTPrescaleProvider hltPrescaleProvider_
Definition: TriggerMatchProducer.h:54
TriggerMatchProducer::changed_
bool changed_
Definition: TriggerMatchProducer.h:53
TriggerMatchProducer::triggerResultsToken_
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
Definition: TriggerMatchProducer.h:46
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
trigger::TriggerObject
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
edm::PtrVector::push_back
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:149
TriggerMatchProducer::triggerEventToken_
edm::EDGetTokenT< trigger::TriggerEvent > triggerEventToken_
Definition: TriggerMatchProducer.h:44
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
TriggerMatchProducer::_inputProducer
edm::InputTag _inputProducer
Definition: TriggerMatchProducer.h:41
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
TriggerMatchProducer::hltTag_
std::string hltTag_
Definition: TriggerMatchProducer.h:47
trigger::TriggerObjectCollection
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
TriggerMatchProducer::triggerResultsTag_
edm::InputTag triggerResultsTag_
Definition: TriggerMatchProducer.h:45
trigger::TriggerEvent::filterIndex
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:132
trigger::Keys
std::vector< size_type > Keys
Definition: TriggerTypeDefs.h:19
TriggerMatchProducer::delRMatchingCut_
double delRMatchingCut_
Definition: TriggerMatchProducer.h:48
trigger::TriggerObject::eta
float eta() const
Definition: TriggerObject.h:53
edm::stream::EDProducer
Definition: EDProducer.h:36
HLTPrescaleProvider
Definition: HLTPrescaleProvider.h:38
TriggerMatchProducer
Definition: TriggerMatchProducer.h:30
counter
static std::atomic< unsigned int > counter
Definition: SharedResourceNames.cc:18
edm::EventSetup
Definition: EventSetup.h:58
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
HLTConfigProvider.h
edm::RefToBaseVector
Definition: EDProductfwd.h:29
trigger::TriggerEvent::getObjects
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:101
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TriggerObject.h
ShallowCloneCandidate.h
HLTConfigProvider
Definition: HLTConfigProvider.h:29
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
Frameworkfwd.h
Exception
Definition: hltDiff.cc:245
TriggerMatchProducer::~TriggerMatchProducer
~TriggerMatchProducer() override
Definition: TriggerMatchProducer.h:89
TriggerMatchProducer::_inputProducerToken
edm::EDGetTokenT< edm::View< object > > _inputProducerToken
Definition: TriggerMatchProducer.h:42
HLTPrescaleProvider.h
HltComparatorCreateWorkflow.hltConfig
hltConfig
Definition: HltComparatorCreateWorkflow.py:161
edm::HLTGlobalStatus::accept
bool accept() const
Has at least one path accepted the event?
Definition: HLTGlobalStatus.h:49
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
trigger
Definition: HLTPrescaleTableCond.h:8
cms::Exception
Definition: Exception.h:70
ParameterSet.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
edm::InputTag
Definition: InputTag.h:15