CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
T getUntrackedParameter(std::string const &, T const &) const
HLTPrescaleProvider hltPrescaleProvider_
float phi() const
Definition: TriggerObject.h:54
void produce(edm::Event &, const edm::EventSetup &) override
float eta() const
Definition: TriggerObject.h:53
Log< level::Error, false > LogError
uint16_t size_type
std::vector< TPRegexp > filters
Definition: eve_filter.cc:22
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
edm::InputTag triggerResultsTag_
void beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup) override
def move
Definition: eostools.py:511
edm::EDGetTokenT< edm::View< object > > _inputProducerToken
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
Log< level::Info, false > LogInfo
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
edm::EDGetTokenT< trigger::TriggerEvent > triggerEventToken_
std::vector< size_type > Keys
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
static std::atomic< unsigned int > counter
TriggerMatchProducer(const edm::ParameterSet &)
void push_back(const RefToBase< T > &)
edm::InputTag triggerEventTag_
tuple cout
Definition: gather_cfg.py:144
Definition: Run.h:45