CMS 3D CMS Logo

LogErrorEventFilter.cc
Go to the documentation of this file.
1 //
2 // $Id: LogErrorEventFilter.cc,v 1.4 2013/04/09 10:06:10 davidlt Exp $
3 //
4 
19 
21 
22 #include <map>
23 #include <set>
24 #include <string>
25 #include <iomanip>
26 #include <iostream>
27 #include <iterator>
28 
29 namespace leef {
31  struct ErrorSort {
32  bool operator()(const Error &e1, const Error &e2) const {
33  if (e1.severity.getLevel() != e2.severity.getLevel()) return e1.severity.getLevel() > e2.severity.getLevel();
34  if (e1.module != e2.module) return e1.module < e2.module;
35  if (e1.category != e2.category) return e1.category < e2.category;
36  return false;
37  }
38  };
39  using ErrorSet = std::set<edm::ErrorSummaryEntry,ErrorSort>;
40 
41  struct RunErrors {
43  npass_{0}, nfail_{0}, collectionGuard_{false} {}
44  //Errors should be sufficiently infrequent so that the use of a
45  // spin lock on a thread-unsafe container should not pose a
46  // performance problem
47  CMS_THREAD_GUARD(collectionGuard_) mutable ErrorSet errorCollection_;
48  mutable std::atomic<size_t> npass_;
49  mutable std::atomic<size_t> nfail_;
50 
51  mutable std::atomic<bool> collectionGuard_;
52  };
53 
54  struct LumiErrors {
56  npass_{0}, nfail_{0}, collectionGuard_{false} {}
57 
58  CMS_THREAD_GUARD(collectionGuard_) mutable ErrorSet errorCollection_;
59  mutable std::atomic<size_t> npass_;
60  mutable std::atomic<size_t> nfail_;
61 
62  mutable std::atomic<bool> collectionGuard_;
63  };
64 }
65 
66 namespace {
67  //Use std::unique_ptr to guarantee that the use of an atomic
68  // as a spin lock will get reset to false
69  struct release {
70  void operator()(std::atomic<bool>* b) const noexcept { b->store(false); }
71  };
72  std::unique_ptr<std::atomic<bool>, release> make_guard(std::atomic<bool>& b) noexcept {
73  bool expected = false;
74  while( not b.compare_exchange_strong(expected,true) );
75 
76  return std::unique_ptr<std::atomic<bool>, release>(&b,release());
77  }
78 
79 }
80 
81 using namespace leef;
82 
83 class LogErrorEventFilter : public edm::global::EDFilter<edm::RunCache<leef::RunErrors>,
84  edm::LuminosityBlockCache<LumiErrors>,
85  edm::EndLuminosityBlockProducer> {
86  public:
87  explicit LogErrorEventFilter(const edm::ParameterSet & iConfig);
88  ~LogErrorEventFilter() override { }
89 
90  bool filter(edm::StreamID, edm::Event & iEvent, const edm::EventSetup& iSetup) const override;
91  std::shared_ptr<LumiErrors> globalBeginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override;
92  void globalEndLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override;
93  void globalEndLuminosityBlockProduce(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override;
94  std::shared_ptr<RunErrors> globalBeginRun(const edm::Run &run, const edm::EventSetup &iSetup) const override;
95  void globalEndRun(const edm::Run &run, const edm::EventSetup &iSetup) const override;
96  void endJob() override;
97 
98  private:
100  typedef std::vector<edm::ErrorSummaryEntry> ErrorList;
101 
105  std::set<std::string> modulesToWatch_;
106  std::set<std::string> modulesToIgnore_;
107  std::set<std::string> categoriesToWatch_;
108  std::set<std::string> categoriesToIgnore_;
109  CMS_THREAD_GUARD(statsGuard_) mutable std::map<std::pair<uint32_t,uint32_t>, std::pair<size_t,size_t> > statsPerLumi_;
110  CMS_THREAD_GUARD(statsGuard_) mutable std::map<uint32_t, std::pair<size_t,size_t> > statsPerRun_;
111  CMS_THREAD_GUARD(statsGuard_) mutable ErrorSet errorCollectionAll_;
112  double thresholdPerLumi_, thresholdPerRun_;
113  size_t maxSavedEventsPerLumi_;
114  bool verbose_, veryVerbose_;
115  bool taggedMode_, forcedValue_;
116  mutable std::atomic<bool> statsGuard_;
117 
118  template<typename Collection> static void increment(ErrorSet &scoreboard, Collection &list);
119  template<typename Collection> static void print(const Collection &errors) ;
120 
121  static std::unique_ptr<ErrorList > serialize(const ErrorSet &set) {
122  return std::make_unique<ErrorList>(set.begin(), set.end());
123  }
124 };
125 
127  src_(iConfig.getParameter<edm::InputTag>("src")),
128  srcT_(consumes<ErrorList>(iConfig.getParameter<edm::InputTag>("src"))),
129  readSummaryMode_(iConfig.existsAs<bool>("readSummaryMode") ? iConfig.getParameter<bool>("readSummaryMode") : false),
130  thresholdPerLumi_(iConfig.getParameter<double>("maxErrorFractionInLumi")),
131  thresholdPerRun_(iConfig.getParameter<double>("maxErrorFractionInRun")),
132  maxSavedEventsPerLumi_(iConfig.getParameter<uint32_t>("maxSavedEventsPerLumiAndError")),
133  verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
134  veryVerbose_(iConfig.getUntrackedParameter<bool>("veryVerbose", false)),
135  taggedMode_(iConfig.getUntrackedParameter<bool>("taggedMode", false)),
136  forcedValue_(iConfig.getUntrackedParameter<bool>("forcedValue", true))
137 {
138  produces<ErrorList, edm::Transition::EndLuminosityBlock>();
139  produces<int, edm::Transition::EndLuminosityBlock>("pass");
140  produces<int, edm::Transition::EndLuminosityBlock>("fail");
141  //produces<ErrorList, edm::InRun>();
142  produces<bool>();
143 
144  if (iConfig.existsAs<std::vector<std::string> >("modulesToWatch")) {
145  std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToWatch");
146  if (!(modules.size() == 1 && modules[0] == "*")) {
147  modulesToWatch_.insert(modules.begin(), modules.end());
148  }
149  }
150  if (iConfig.existsAs<std::vector<std::string> >("modulesToIgnore")) {
151  std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToIgnore");
152  if (!(modules.size() == 1 && modules[0] == "*")) {
153  modulesToIgnore_.insert(modules.begin(), modules.end());
154  }
155  }
156  if (iConfig.existsAs<std::vector<std::string> >("categoriesToWatch")) {
157  std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToWatch");
158  if (!(categories.size() == 1 && categories[0] == "*")) {
159  categoriesToWatch_.insert(categories.begin(), categories.end());
160  }
161  }
162  if (iConfig.existsAs<std::vector<std::string> >("categoriesToIgnore")) {
163  std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToIgnore");
164  if (!(categories.size() == 1 && categories[0] == "*")) {
165  categoriesToIgnore_.insert(categories.begin(), categories.end());
166  }
167  }
168  //std::ostream_iterator<std::string> dump(std::cout, ", ");
169  //std::cout << "\nWatch modules: " ; std::copy(modulesToWatch_.begin(), modulesToWatch_.end(), dump);
170  //std::cout << "\nIgnore modules: " ; std::copy(modulesToIgnore_.begin(), modulesToIgnore_.end(), dump);
171  //std::cout << "\nIgnore categories: " ; std::copy(categoriesToIgnore_.begin(), categoriesToIgnore_.end(), dump);
172  //std::cout << "\nWatch categories: " ; std::copy(categoriesToWatch_.begin(), categoriesToWatch_.end(), dump);
173  //std::cout << std::endl;
174 
175 }
176 
177 std::shared_ptr<LumiErrors>
179  auto ret = std::make_shared<LumiErrors>();
180  if (readSummaryMode_) {
182  edm::Handle<int> hpass, hfail;
183  lumi.getByLabel(src_, handle);
184  lumi.getByLabel(edm::InputTag(src_.label(), "pass", src_.process()), hpass);
185  lumi.getByLabel(edm::InputTag(src_.label(), "fail", src_.process()), hfail);
186  increment(ret->errorCollection_, *handle);
187  ret->npass_ = *hpass;
188  ret->nfail_ = *hfail;
189 
190  auto runC = runCache(lumi.getRun().index());
191  runC->npass_ +=*hpass;
192  runC->nfail_ += *hfail;
193  }
194  return ret;
195 }
196 void
198  auto lumiC = luminosityBlockCache(lumi.index());
199  auto nfail = lumiC->nfail_.load();
200  auto npass = lumiC->npass_.load();
201  {
202  auto guard = make_guard(statsGuard_);
203  statsPerLumi_[std::pair<uint32_t,uint32_t>(lumi.run(), lumi.luminosityBlock())] = std::pair<size_t,size_t>(npass, nfail);
204  }
205  {
206  //synchronize lumiC->errorCollection_
207  auto guard = make_guard(lumiC->collectionGuard_);
208 
209  {
210  if (nfail < thresholdPerLumi_*(npass+nfail)) {
211  //synchronize runC->errorCollection_
212  auto runC = runCache(lumi.getRun().index());
213  auto guard = make_guard(runC->collectionGuard_);
214  increment(runC->errorCollection_, lumiC->errorCollection_);
215  }
216  }
217  if (verbose_) {
218  if (!lumiC->errorCollection_.empty()) {
219  std::cout << "\n === REPORT FOR RUN " << lumi.run() << " LUMI " << lumi.luminosityBlock() << " === " << std::endl;
220  print(lumiC->errorCollection_);
221  }
222  }
223  }
224 }
225 
226 void
228  auto lumiC = luminosityBlockCache(lumi.index());
229  {
230  //synchronize errorCollection_
231  auto guard = make_guard(lumiC->collectionGuard_);
232  lumi.put(serialize(lumiC->errorCollection_));
233  }
234  lumi.put(std::make_unique<int>(lumiC->npass_.load()), "pass");
235  lumi.put(std::make_unique<int>(lumiC->nfail_.load()), "fail");
236 }
237 
238 
239 std::shared_ptr<RunErrors>
241  return std::make_shared<RunErrors>();
242 }
243 
244 void
246  auto runC = runCache(run.index());
247  auto npass = runC->npass_.load();
248  auto nfail = runC->nfail_.load();
249  {
250  auto guard = make_guard(statsGuard_);
251  statsPerRun_[run.run()] = std::pair<size_t,size_t>(npass, nfail);
252  }
253  {
254  //synchronize errorCollection_
255  auto guard = make_guard(runC->collectionGuard_);
256  if (nfail < thresholdPerRun_*(npass+nfail)) {
257  auto guard = make_guard(statsGuard_);
258  increment(errorCollectionAll_, runC->errorCollection_);
259  }
260  if (verbose_) {
261  if (!runC->errorCollection_.empty()) {
262  std::cout << "\n === REPORT FOR RUN " << run.run() << " === " << std::endl;
263  print(runC->errorCollection_);
264  }
265  }
266  }
267 }
268 
269 void
271  if (verbose_) {
272  std::cout << "\n === REPORT FOR JOB === " << std::endl;
273  //synchronizes statsPerRun_ and errorCollectionAll_
274  auto guard = make_guard(statsGuard_);
276 
277  typedef std::pair<size_t,size_t> counter;
278 
279  std::cout << "\n === SCOREBOARD PER RUN === " << std::endl;
280  typedef std::pair<uint32_t, counter> hitRun;
281  for(auto const& hit : statsPerRun_) {
282  double fract = hit.second.second/double(hit.second.first + hit.second.second);
283  printf("run %6d: fail %7zu, pass %7zu, fraction %7.3f%%%s\n", hit.first, hit.second.second, hit.second.first, fract*100., (fract >= thresholdPerRun_ ? " (run excluded from summary list)" : ""));
284  }
285 
286  std::cout << "\n === SCOREBOARD PER LUMI === " << std::endl;
287  typedef std::pair<std::pair<uint32_t,uint32_t>, counter> hitLumi;
288  for(auto const& hit : statsPerLumi_) {
289  double fract = hit.second.second/double(hit.second.first + hit.second.second);
290  printf("run %6d, lumi %4d: fail %zu, pass %zu, fraction %7.3f%%%s\n", hit.first.first, hit.first.second, hit.second.second, hit.second.first, fract*100., (fract >= thresholdPerLumi_ ? " (lumi excluded from run list)" : ""));
291  }
292  }
293 }
294 
295 bool
297  if (readSummaryMode_) return true;
298 
299  bool fail = false, save = false;
300 
302  iEvent.getByToken(srcT_, errors);
303 
304  auto runC = runCache(iEvent.getRun().index());
305  auto lumiC = luminosityBlockCache(iEvent.getLuminosityBlock().index());
306 
307  if (errors->empty()) {
308  ++(runC->npass_);
309  ++(lumiC->npass_);
310  iEvent.put(std::make_unique<bool>(false));
311 
312  if(taggedMode_) return forcedValue_;
313  return false;
314  }
315 
316  for(auto const& err : *errors) {
317  if (!modulesToWatch_.empty() && (modulesToWatch_.count(err.module) == 0)) continue;
318  if (!categoriesToWatch_.empty() && (categoriesToWatch_.count(err.category) == 0)) continue;
319  if (!modulesToIgnore_.empty() && (modulesToIgnore_.count(err.module) != 0)) continue;
320  if (!categoriesToIgnore_.empty() && (categoriesToIgnore_.count(err.category) != 0)) continue;
321 
322  fail = true;
323 
324  //synchronize errorCollection_
325  auto guard = make_guard(lumiC->collectionGuard_);
326 
327  std::pair<ErrorSet::iterator, bool> result = lumiC->errorCollection_.insert(err);
328  if (!result.second) { // already there
329  // need the const_cast as set elements are const
330  const_cast<unsigned int &>(result.first->count) += err.count;
331  if (result.first->count < maxSavedEventsPerLumi_) save = true;
332  } else {
333  save = true;
334  }
335 
336  }
337  if (save && veryVerbose_) {
338  std::cout << "\n === REPORT FOR EVENT " << iEvent.id().event() << " RUN " << iEvent.run() << " LUMI " << iEvent.luminosityBlock() << " === " << std::endl;
339  print(*errors);
340  }
341 
342 
343  if (fail) { ++(lumiC->nfail_); ++(runC->nfail_); } else { ++(runC->npass_); ++(lumiC->npass_); }
344  iEvent.put(std::make_unique<bool>(fail)); // fail is the unbiased boolean
345 
346  if(taggedMode_) return forcedValue_;
347  return save;
348 }
349 
350 template<typename Collection>
351 void
352 LogErrorEventFilter::increment(ErrorSet &scoreboard, Collection &list) {
353  for(auto const& err : list) {
354  std::pair<ErrorSet::iterator, bool> result = scoreboard.insert(err);
355  // need the const_cast as set elements are const
356  if (!result.second) const_cast<unsigned int &>(result.first->count) += err.count;
357  }
358 }
359 
360 template<typename Collection>
361 void
362 LogErrorEventFilter::print(const Collection &errors) {
363  using namespace std;
364  cout << setw(40) << left << "Category" << " " <<
365  setw(60) << left << "Module" << " " <<
366  setw(10) << left << "Level" << " " <<
367  setw(9) << right << "Count" << "\n";
368  cout << setw(40) << left << "----------------------------------------" << " " <<
369  setw(60) << left << "------------------------------------------------------------" << " " <<
370  setw(10) << left << "----------" << " " <<
371  setw(9) << right << "---------" << "\n";
372  for(auto const& err : errors) {
373  cout << setw(40) << left << err.category << " " <<
374  setw(60) << left << err.module << " " <<
375  setw(10) << left << err.severity.getName() << " " <<
376  setw(9) << right << err.count << "\n";
377  }
378  cout << flush;
379 }
380 
std::shared_ptr< LumiErrors > globalBeginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override
std::set< edm::ErrorSummaryEntry, ErrorSort > ErrorSet
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
static void print(const Collection &errors)
std::set< std::string > categoriesToIgnore_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
std::set< std::string > modulesToWatch_
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:161
std::pair< Binary, Binary > serialize(const T &payload)
Definition: Serialization.h:62
RunNumber_t run() const
Definition: RunBase.h:40
LuminosityBlockIndex index() const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::ErrorSummaryEntry Error
std::shared_ptr< RunErrors > globalBeginRun(const edm::Run &run, const edm::EventSetup &iSetup) const override
ELseverityLevel severity
void globalEndLuminosityBlockProduce(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
Run const & getRun() const
Definition: Event.cc:99
static std::unique_ptr< ErrorList > serialize(const ErrorSet &set)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
bool getByLabel(std::string const &label, Handle< PROD > &result) const
LuminosityBlockNumber_t luminosityBlock() const
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void globalEndRun(const edm::Run &run, const edm::EventSetup &iSetup) const override
static void increment(ErrorSet &scoreboard, Collection &list)
void put(std::unique_ptr< PROD > product)
Put a new product.
bool filter(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
RunNumber_t run() const
Definition: Event.h:101
std::atomic< bool > statsGuard_
#define CMS_THREAD_GUARD(_var_)
RunIndex index() const
Definition: Run.cc:21
RunNumber_t run() const
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
bool operator()(const Error &e1, const Error &e2) const
LuminosityBlock const & getLuminosityBlock() const
Definition: Event.h:97
const Fraction< n, m >::type & fract()
Definition: Fraction.h:37
std::vector< edm::ErrorSummaryEntry > ErrorList
std::map< uint32_t, std::pair< size_t, size_t > > statsPerRun_
void globalEndLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) const override
std::set< std::string > categoriesToWatch_
#define noexcept
std::set< std::string > modulesToIgnore_
std::map< std::pair< uint32_t, uint32_t >, std::pair< size_t, size_t > > statsPerLumi_
double b
Definition: hdecay.h:120
std::string const & label() const
Definition: InputTag.h:36
std::string const & process() const
Definition: InputTag.h:40
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
static std::atomic< unsigned int > counter
Definition: errors.py:1
def fail(errstr="")
edm::EDGetTokenT< ErrorList > srcT_
save
Definition: cuy.py:1165
Definition: Run.h:45
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run
LogErrorEventFilter(const edm::ParameterSet &iConfig)
Run const & getRun() const