CMS 3D CMS Logo

HltComparator.cc
Go to the documentation of this file.
1 // Originally written by James Jackson
2 // modified by Peter Wittich
3 
4 // user include files
7 //#include "FWCore/Utilities/interface/Exception.h"
8 
9 //#include "FWCore/MessageLogger/interface/MessageLogger.h"
12 
13 #include <TH1.h>
14 #include <iostream>
15 #include <string>
16 #include <vector>
17 
18 typedef std::vector<std::string> StringCollection;
19 
20 // types of outcomes possible.
21 // only some are errors
22 enum {
33 };
34 
35 // Analyser constructor
37  : hltOnlineResults_(consumes<edm::TriggerResults>(iConfig.getParameter<edm::InputTag>("OnlineResults"))),
38  hltOfflineResults_(consumes<edm::TriggerResults>(iConfig.getParameter<edm::InputTag>("OfflineResults"))),
39  init_(false),
40  verbose_(iConfig.getUntrackedParameter<bool>("verbose")),
41  skipPathList_(iConfig.getUntrackedParameter<std::vector<std::string>>("skipPaths")),
42  usePathList_(iConfig.getUntrackedParameter<std::vector<std::string>>("usePaths")) {
43  // std::cout << " HERE I AM " << std::endl;
44  produces<StringCollection>("failedTriggerDescription");
45  // std::cout << " HERE I GO " << std::endl;
46  usesResource(TFileService::kSharedResource);
47 }
48 
50 
51 // Initialises online --> offline trigger bit mappings and histograms
53  const edm::TriggerResults &offlineResults,
54  edm::Event &e) {
55  init_ = true;
56 
57  // Get trigger names
58  const edm::TriggerNames &onlineTriggerNames = e.triggerNames(onlineResults);
59  const edm::TriggerNames &offlineTriggerNames = e.triggerNames(offlineResults);
60  onlineActualNames_ = onlineTriggerNames.triggerNames();
61  offlineActualNames_ = offlineTriggerNames.triggerNames();
63 
64  // do we need to throw? I guess the whole job is crap if this happens.
65  // sort of assumes we're the only game in town.
66  if (numTriggers_ != offlineActualNames_.size()) {
67  throw cms::Exception("IncorrectTriggers") << "Online had " << numTriggers_ << "triggers, "
68  << "Offline had " << offlineActualNames_.size() << "triggers";
69  }
70 
71  // Create bit mappings
72  std::map<std::string, unsigned int> offlineNameBitMap;
73  for (unsigned int i = 0; i < numTriggers_; ++i) {
74  offlineNameBitMap[offlineActualNames_[i]] = i;
75  }
76  for (unsigned int i = 0; i < numTriggers_; ++i) {
77  // Find offline position for fixed online bit
78  std::map<std::string, unsigned int>::iterator it = offlineNameBitMap.find(onlineActualNames_[i]);
79  if (it != offlineNameBitMap.end()) {
80  onlineToOfflineBitMappings_.push_back(it->second);
81  } else {
82  throw cms::Exception("IncorrectTriggers") << "Online trigger path " << onlineActualNames_[i]
83  << " not found in Offline "
84  "processing";
85  }
86  }
87 
88  // Create histograms
90  for (std::vector<std::string>::iterator it = onlineActualNames_.begin(); it != onlineActualNames_.end(); ++it) {
91  // Bin descriptions: OnOfPass, OnOffFail, OnPassOffFail, OnFailOffPass,
92  // OnOffError, OnRunOffError, OnErrorOffRun, OnRunOffNot OnNotOffRun
93  // OnNotOffNot
94  TH1F *h = fs->make<TH1F>(it->c_str(), it->c_str(), 10, 0, 10);
95  TAxis *a = h->GetXaxis();
96  a->SetBinLabel(1, "OnPass_OffPass");
97  a->SetBinLabel(2, "OnFail_OffFail");
98  a->SetBinLabel(3, "OnPass_OffFail");
99  a->SetBinLabel(4, "OnFail_OffPass");
100  a->SetBinLabel(5, "OnError_OffError");
101  a->SetBinLabel(6, "OnRun_OffError");
102  a->SetBinLabel(7, "OnError_OffRun");
103  a->SetBinLabel(8, "OnRun_OffNotRun");
104  a->SetBinLabel(9, "OnNotRun_OffRun");
105  a->SetBinLabel(10, "OnNotRun_OffNotRun");
106  comparisonHists_.push_back(h);
107  }
108 }
109 
110 // Format a comparison result
112  switch (i) {
113  case 0:
114  return std::string("OnPass_OffPass");
115  break;
116  case 1:
117  return std::string("OnFail_OffFail");
118  break;
119  case 2:
120  return std::string("OnPass_OffFail");
121  break;
122  case 3:
123  return std::string("OnFail_OffPass");
124  break;
125  case 4:
126  return std::string("OnError_OffError");
127  break;
128  case 5:
129  return std::string("OnRun_OffError");
130  break;
131  case 6:
132  return std::string("OnError_OffRun");
133  break;
134  case 7:
135  return std::string("OnRun_OffNotRun");
136  break;
137  case 8:
138  return std::string("OnNotRun_OffRun");
139  break;
140  case 9:
141  return std::string("OnNotRun_OffNotRun");
142  break;
143  }
144  return std::string("CODE NOT KNOWN");
145 }
146 
148  // std::cout << "top of the filter " << std::endl;
149  // Get trigger results
150  edm::Handle<edm::TriggerResults> onlineResults;
151  edm::Handle<edm::TriggerResults> offlineResults;
152  event.getByToken(hltOnlineResults_, onlineResults);
153  event.getByToken(hltOfflineResults_, offlineResults);
154 
155  std::unique_ptr<StringCollection> resultDescription(new StringCollection);
156 
157  // Initialise comparator if required
158  if (!init_) {
159  initialise(*onlineResults, *offlineResults, event);
160  }
161 
162  // Perform trigger checks
163  bool hasDisagreement = false;
164  for (unsigned int i = 0; i < numTriggers_; ++i) {
165  unsigned int offlineTriggerBit = onlineToOfflineBitMappings_[i];
166 
167  bool onRun = onlineResults->wasrun(i);
168  bool offRun = offlineResults->wasrun(offlineTriggerBit);
169  bool onAccept = onlineResults->accept(i);
170  bool offAccept = offlineResults->accept(offlineTriggerBit);
171  bool onError = onlineResults->error(i);
172  bool offError = offlineResults->error(offlineTriggerBit);
173 
174  int result = -1;
175  if (onError || offError) {
176  if (onError && offError) {
177  result = 4;
178  } else if (onError) {
179  result = 6;
180  } else {
181  result = 5;
182  }
183  } else if ((!onRun) || (!offRun)) {
184  if ((!onRun) && (!offRun)) {
185  result = 9;
186  } else if (!onRun) {
187  result = 8;
188  } else {
189  result = 7;
190  }
191  } else {
192  if (onAccept && offAccept) {
193  result = 0;
194  } else if ((!onAccept) && (!offAccept)) {
195  result = 1;
196  } else if (onAccept) {
197  result = 2;
198  } else {
199  result = 3;
200  }
201  }
202 
203  // Fill the results histogram
204  comparisonHists_[i]->Fill(result);
205 
206  // if the online-offline comparison results in a failure, we
207  // want to send the result to a special stream. Hence we _pass_ the filter.
208  // If it all worked as expected the filter fails and the event doesn't go
209  // to the output stream.
210  if ((result == kOnPassOffFail) || (result == kOnFailOffPass) || (result == kOnRunOffError) ||
212  // is this one we should ignore? check the skip list
213  if (verbose()) {
214  std::cout << "Found disagreemenet " << result << ", name is " << onlineActualNames_[i] << std::endl;
215  }
216  std::ostringstream desc;
218  resultDescription->push_back(desc.str());
219  if (std::find(skipPathList_.begin(), skipPathList_.end(), onlineActualNames_[i]) == skipPathList_.end()) {
220  if (!usePathList_.empty()) {
221  // only use specified paths to debug
222  if (std::find(usePathList_.begin(), usePathList_.end(), onlineActualNames_[i]) != usePathList_.end())
223  hasDisagreement = true;
224  } else
225  hasDisagreement = true;
226  }
227  }
228 
229  // Record the trigger error code
230  // I think this should be result > 2? (pw)
231  if (verbose() && (result > 1)) {
232  std::cout << "HLT-Compare: Event " << event.id().event() << " Path " << onlineActualNames_[i] << " "
233  << formatResult(result) << std::endl;
234 #ifdef NOTDEF
235  triggerComparisonErrors_[event.id().event()][onlineActualNames_[i]] = result;
236 #endif // NOTDEF
237  }
238  }
239 
240  // std::cout << " HERE I STAY " << std::endl;
241  event.put(std::move(resultDescription), "failedTriggerDescription");
242  // std::cout << " HERE I WENT " << std::endl;
243 
244  if (hasDisagreement)
245  return true;
246  else
247  return false;
248 }
249 
251 
252 // Print the trigger results
254 #ifdef NOTDEF
255  std::cout << "HLT-Compare ---------- Trigger Comparison Summary ----------" << std::endl;
256  std::cout << "HLT-Compare The following events had trigger mismatches:" << std::endl;
257  std::map<unsigned int, std::map<std::string, unsigned int>>::iterator it;
258  for (it = triggerComparisonErrors_.begin(); it != triggerComparisonErrors_.end(); ++it) {
259  std::cout << "HLT-Compare Event: " << it->first << std::endl;
260  std::map<std::string, unsigned int>::iterator jt;
261  for (jt = it->second.begin(); jt != it->second.end(); ++jt) {
262  std::cout << "HLT-Compare Path: " << jt->first << " : " << formatResult(jt->second) << std::endl;
263  }
264  }
265  std::cout << "HLT-Compare ------------ End Trigger Comparison ------------" << std::endl;
266 #endif // NOTDEF
267 }
static const std::string kSharedResource
Definition: TFileService.h:76
bool accept() const
Has at least one path accepted the event?
unsigned int numTriggers_
Definition: HltComparator.h:38
std::vector< unsigned int > onlineToOfflineBitMappings_
Definition: HltComparator.h:26
bool error() const
Has any path encountered an error (exception)
Strings const & triggerNames() const
Definition: TriggerNames.cc:48
edm::EDGetTokenT< edm::TriggerResults > hltOnlineResults_
Definition: HltComparator.h:21
bool filter(edm::Event &, const edm::EventSetup &) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
HltComparator(const edm::ParameterSet &)
bool wasrun() const
Was at least one path run?
std::vector< TH1F * > comparisonHists_
Definition: HltComparator.h:28
edm::EDGetTokenT< edm::TriggerResults > hltOfflineResults_
Definition: HltComparator.h:22
std::vector< std::string > onlineActualNames_
Definition: HltComparator.h:24
std::vector< std::string > usePathList_
Definition: HltComparator.h:36
void endJob() override
std::vector< std::string > offlineActualNames_
Definition: HltComparator.h:25
void initialise(const edm::TriggerResults &, const edm::TriggerResults &, edm::Event &e)
bool verbose() const
Definition: HltComparator.h:33
void beginJob() override
~HltComparator() override
std::string formatResult(const unsigned int)
std::vector< std::string > skipPathList_
Definition: HltComparator.h:35
HLT enums.
double a
Definition: hdecay.h:119
std::vector< std::string > StringCollection
std::map< unsigned int, std::map< std::string, unsigned int > > triggerComparisonErrors_
Definition: HltComparator.h:29
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1