CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
18 //#include "FWCore/ParameterSet/interface/InputTag.h"
20 
22 
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <iomanip>
27 #include <iostream>
28 #include <iterator>
29 #include <boost/foreach.hpp>
30 #define foreach BOOST_FOREACH
31 
32 
33 class LogErrorEventFilter : public edm::one::EDFilter<edm::one::WatchRuns,
34  edm::one::WatchLuminosityBlocks,
35  edm::EndLuminosityBlockProducer> {
36  public:
37  explicit LogErrorEventFilter(const edm::ParameterSet & iConfig);
38  virtual ~LogErrorEventFilter() { }
39 
40  virtual bool filter(edm::Event & iEvent, const edm::EventSetup& iSetup) override;
41  virtual void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override;
42  virtual void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override;
43  virtual void endLuminosityBlockProduce(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override;
44  virtual void beginRun(const edm::Run &run, const edm::EventSetup &iSetup) override;
45  virtual void endRun(const edm::Run &run, const edm::EventSetup &iSetup) override;
46  virtual void endJob() override;
47 
48  private:
50  struct ErrorSort {
51  bool operator()(const Error &e1, const Error &e2) {
52  if (e1.severity.getLevel() != e2.severity.getLevel()) return e1.severity.getLevel() > e2.severity.getLevel();
53  if (e1.module != e2.module) return e1.module < e2.module;
54  if (e1.category != e2.category) return e1.category < e2.category;
55  return false;
56  }
57  };
58  typedef std::vector<edm::ErrorSummaryEntry> ErrorList;
59  typedef std::set<edm::ErrorSummaryEntry,ErrorSort> ErrorSet;
60 
66  std::set<std::string> modulesToWatch_;
67  std::set<std::string> modulesToIgnore_;
68  std::set<std::string> categoriesToWatch_;
69  std::set<std::string> categoriesToIgnore_;
70  std::map<std::pair<uint32_t,uint32_t>, std::pair<size_t,size_t> > statsPerLumi_;
71  std::map<uint32_t, std::pair<size_t,size_t> > statsPerRun_;
78 
79  template<typename Collection> static void increment(ErrorSet &scoreboard, Collection &list);
80  template<typename Collection> static void print(const Collection &errors) ;
81 
82  static std::auto_ptr<ErrorList > serialize(const ErrorSet &set) {
83  std::auto_ptr<ErrorList> ret(new ErrorList(set.begin(), set.end()));
84  return ret;
85  }
86 };
87 
89  src_(iConfig.getParameter<edm::InputTag>("src")),
90  srcT_(consumes<ErrorList>(iConfig.getParameter<edm::InputTag>("src"))),
91  readSummaryMode_(iConfig.existsAs<bool>("readSummaryMode") ? iConfig.getParameter<bool>("readSummaryMode") : false),
92  thresholdPerLumi_(iConfig.getParameter<double>("maxErrorFractionInLumi")),
93  thresholdPerRun_(iConfig.getParameter<double>("maxErrorFractionInRun")),
94  maxSavedEventsPerLumi_(iConfig.getParameter<uint32_t>("maxSavedEventsPerLumiAndError")),
95  verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
96  veryVerbose_(iConfig.getUntrackedParameter<bool>("veryVerbose", false)),
97  taggedMode_(iConfig.getUntrackedParameter<bool>("taggedMode", false)),
98  forcedValue_(iConfig.getUntrackedParameter<bool>("forcedValue", true))
99 {
100  produces<ErrorList, edm::InLumi>();
101  produces<int, edm::InLumi>("pass");
102  produces<int, edm::InLumi>("fail");
103  //produces<ErrorList, edm::InRun>();
104  produces<bool>();
105 
106  if (iConfig.existsAs<std::vector<std::string> >("modulesToWatch")) {
107  std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToWatch");
108  if (!(modules.size() == 1 && modules[0] == "*")) {
109  modulesToWatch_.insert(modules.begin(), modules.end());
110  }
111  }
112  if (iConfig.existsAs<std::vector<std::string> >("modulesToIgnore")) {
113  std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToIgnore");
114  if (!(modules.size() == 1 && modules[0] == "*")) {
115  modulesToIgnore_.insert(modules.begin(), modules.end());
116  }
117  }
118  if (iConfig.existsAs<std::vector<std::string> >("categoriesToWatch")) {
119  std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToWatch");
120  if (!(categories.size() == 1 && categories[0] == "*")) {
121  categoriesToWatch_.insert(categories.begin(), categories.end());
122  }
123  }
124  if (iConfig.existsAs<std::vector<std::string> >("categoriesToIgnore")) {
125  std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToIgnore");
126  if (!(categories.size() == 1 && categories[0] == "*")) {
127  categoriesToIgnore_.insert(categories.begin(), categories.end());
128  }
129  }
130  //std::ostream_iterator<std::string> dump(std::cout, ", ");
131  //std::cout << "\nWatch modules: " ; std::copy(modulesToWatch_.begin(), modulesToWatch_.end(), dump);
132  //std::cout << "\nIgnore modules: " ; std::copy(modulesToIgnore_.begin(), modulesToIgnore_.end(), dump);
133  //std::cout << "\nIgnore categories: " ; std::copy(categoriesToIgnore_.begin(), categoriesToIgnore_.end(), dump);
134  //std::cout << "\nWatch categories: " ; std::copy(categoriesToWatch_.begin(), categoriesToWatch_.end(), dump);
135  //std::cout << std::endl;
136 
137 }
138 
139 void
141  npassLumi_ = 0; nfailLumi_ = 0;
142  errorCollectionThisLumi_.clear();
143  if (readSummaryMode_) {
145  edm::Handle<int> hpass, hfail;
146  lumi.getByLabel(src_, handle);
147  lumi.getByLabel(edm::InputTag(src_.label(), "pass", src_.process()), hpass);
148  lumi.getByLabel(edm::InputTag(src_.label(), "fail", src_.process()), hfail);
150  npassLumi_ = *hpass;
151  nfailLumi_ = *hfail;
154  }
155 }
156 void
158  statsPerLumi_[std::pair<uint32_t,uint32_t>(lumi.run(), lumi.luminosityBlock())] = std::pair<size_t,size_t>(npassLumi_, nfailLumi_);
161  }
162  if (verbose_) {
163  if (!errorCollectionThisLumi_.empty()) {
164  std::cout << "\n === REPORT FOR RUN " << lumi.run() << " LUMI " << lumi.luminosityBlock() << " === " << std::endl;
166  }
167  }
168 }
169 
170 void
173  std::auto_ptr<int> outpass(new int(npassLumi_)); lumi.put(outpass, "pass");
174  std::auto_ptr<int> outfail(new int(nfailLumi_)); lumi.put(outfail, "fail");
175 }
176 
177 
178 void
180  npassRun_ = 0; nfailRun_ = 0;
181  errorCollectionThisRun_.clear();
182 }
183 
184 void
186  statsPerRun_[run.run()] = std::pair<size_t,size_t>(npassRun_, nfailRun_);
187  if (nfailRun_ < thresholdPerRun_*(npassRun_+nfailRun_)) {
189  }
190  if (verbose_) {
191  if (!errorCollectionThisRun_.empty()) {
192  std::cout << "\n === REPORT FOR RUN " << run.run() << " === " << std::endl;
194  }
195  }
196  //run.put(serialize(errorCollectionThisRun_));
197 }
198 
199 void
201  if (verbose_) {
202  std::cout << "\n === REPORT FOR JOB === " << std::endl;
204 
205  typedef std::pair<size_t,size_t> counter;
206 
207  std::cout << "\n === SCOREBOARD PER RUN === " << std::endl;
208  typedef std::pair<uint32_t, counter> hitRun;
209  foreach(const hitRun &hit, statsPerRun_) {
210  double fract = hit.second.second/double(hit.second.first + hit.second.second);
211  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)" : ""));
212  }
213 
214  std::cout << "\n === SCOREBOARD PER LUMI === " << std::endl;
215  typedef std::pair<std::pair<uint32_t,uint32_t>, counter> hitLumi;
216  foreach(const hitLumi &hit, statsPerLumi_) {
217  double fract = hit.second.second/double(hit.second.first + hit.second.second);
218  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)" : ""));
219  }
220  }
221 }
222 
223 bool
225  if (readSummaryMode_) return true;
226 
227  bool fail = false, save = false;
228 
230  iEvent.getByToken(srcT_, errors);
231 
232 
233  if (errors->empty()) {
234  npassRun_++; npassLumi_++;
235  iEvent.put( std::auto_ptr<bool>(new bool(false)) );
236 
237  if(taggedMode_) return forcedValue_;
238  return false;
239  }
240 
241  foreach (const Error &err, *errors) {
242  if (!modulesToWatch_.empty() && (modulesToWatch_.count(err.module) == 0)) continue;
243  if (!categoriesToWatch_.empty() && (categoriesToWatch_.count(err.category) == 0)) continue;
244  if (!modulesToIgnore_.empty() && (modulesToIgnore_.count(err.module) != 0)) continue;
245  if (!categoriesToIgnore_.empty() && (categoriesToIgnore_.count(err.category) != 0)) continue;
246 
247  fail = true;
248  std::pair<ErrorSet::iterator, bool> result = errorCollectionThisLumi_.insert(err);
249  if (!result.second) { // already there
250  // need the const_cast as set elements are const
251  const_cast<unsigned int &>(result.first->count) += err.count;
252  if (result.first->count < maxSavedEventsPerLumi_) save = true;
253  } else {
254  save = true;
255  }
256 
257  }
258  if (save && veryVerbose_) {
259  std::cout << "\n === REPORT FOR EVENT " << iEvent.id().event() << " RUN " << iEvent.run() << " LUMI " << iEvent.luminosityBlock() << " === " << std::endl;
260  print(*errors);
261  }
262 
263 
264  if (fail) { nfailLumi_++; nfailRun_++; } else { npassRun_++; npassLumi_++; }
265  iEvent.put( std::auto_ptr<bool>(new bool(fail)) ); // fail is the unbiased boolean
266 
267  if(taggedMode_) return forcedValue_;
268  return save;
269 }
270 
271 template<typename Collection>
272 void
273 LogErrorEventFilter::increment(ErrorSet &scoreboard, Collection &list) {
274  foreach (const Error &err, list) {
275  std::pair<ErrorSet::iterator, bool> result = scoreboard.insert(err);
276  // need the const_cast as set elements are const
277  if (!result.second) const_cast<unsigned int &>(result.first->count) += err.count;
278  }
279 }
280 
281 template<typename Collection>
282 void
283 LogErrorEventFilter::print(const Collection &errors) {
284  using namespace std;
285  cout << setw(40) << left << "Category" << " " <<
286  setw(60) << left << "Module" << " " <<
287  setw(10) << left << "Level" << " " <<
288  setw(9) << right << "Count" << "\n";
289  cout << setw(40) << left << "----------------------------------------" << " " <<
290  setw(60) << left << "------------------------------------------------------------" << " " <<
291  setw(10) << left << "----------" << " " <<
292  setw(9) << right << "---------" << "\n";
293  foreach (const Error &err, errors) {
294  cout << setw(40) << left << err.category << " " <<
295  setw(60) << left << err.module << " " <<
296  setw(10) << left << err.severity.getName() << " " <<
297  setw(9) << right << err.count << "\n";
298  }
299  cout << flush;
300 }
301 
bool operator()(const Error &e1, const Error &e2)
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_
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:186
virtual void endLuminosityBlockProduce(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override
RunNumber_t run() const
Definition: RunBase.h:42
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
edm::ErrorSummaryEntry Error
std::set< edm::ErrorSummaryEntry, ErrorSort > ErrorSet
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
tuple lumi
Definition: fjr2json.py:35
virtual void endRun(const edm::Run &run, const edm::EventSetup &iSetup) override
ELseverityLevel severity
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:63
static std::auto_ptr< ErrorList > serialize(const ErrorSet &set)
bool getByLabel(std::string const &label, Handle< PROD > &result) const
LuminosityBlockNumber_t luminosityBlock() const
virtual void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
int iEvent
Definition: GenABIO.cc:230
static void increment(ErrorSet &scoreboard, Collection &list)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
virtual void endJob() override
tuple result
Definition: query.py:137
const ELstring getName() const
RunNumber_t run() const
Definition: Event.h:92
tuple handle
Definition: patZpeak.py:22
if(c.getParameter< edm::InputTag >("puppiValueMap").label().size()!=0)
RunNumber_t run() const
const Fraction< n, m >::type & fract()
Definition: Fraction.h:38
std::vector< edm::ErrorSummaryEntry > ErrorList
std::map< uint32_t, std::pair< size_t, size_t > > statsPerRun_
std::set< std::string > categoriesToWatch_
std::set< std::string > modulesToIgnore_
std::map< std::pair< uint32_t, uint32_t >, std::pair< size_t, size_t > > statsPerLumi_
virtual void beginRun(const edm::Run &run, const edm::EventSetup &iSetup) override
std::string const & label() const
Definition: InputTag.h:43
std::string const & process() const
Definition: InputTag.h:47
edm::EventID id() const
Definition: EventBase.h:60
list save
Definition: cuy.py:1163
static std::atomic< unsigned int > counter
tuple cout
Definition: gather_cfg.py:121
volatile std::atomic< bool > shutdown_flag false
virtual void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) override
edm::EDGetTokenT< ErrorList > srcT_
Definition: Run.h:43
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
void put(std::auto_ptr< PROD > product)
Put a new product.
LogErrorEventFilter(const edm::ParameterSet &iConfig)