ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/plugins/LogErrorAnalysis.cc
Revision: 1.1
Committed: Tue Jan 26 09:47:36 2010 UTC (15 years, 3 months ago) by gpetrucc
Content type: text/plain
Branch: MAIN
CVS Tags: V03-00-00, V02-00-00
Log Message:
Skim events with errors

File Contents

# Content
1 //
2 // $Id: LogErrorAnalysis.cc,v 1.1 2009/05/28 17:33:26 gpetrucc Exp $
3 //
4
5 /**
6 \class gLogErrorAnalysis PATMuonKinematics.h "PhysicsTools/PatAlgos/interface/PATMuonKinematics.h"
7 \brief Use StandAlone track to define the 4-momentum of a PAT Muon (normally the global one is used)
8
9 \author Giovanni Petrucciani
10 \version $Id: LogErrorAnalysis.cc,v 1.1 2009/05/28 17:33:26 gpetrucc Exp $
11 */
12
13
14 #include "FWCore/Framework/interface/EDFilter.h"
15 #include "FWCore/Framework/interface/Event.h"
16 #include "FWCore/Framework/interface/Run.h"
17 #include "FWCore/Framework/interface/LuminosityBlock.h"
18 #include "FWCore/ParameterSet/interface/ParameterSet.h"
19 #include "FWCore/ParameterSet/interface/InputTag.h"
20
21 #include "FWCore/MessageLogger/interface/ErrorSummaryEntry.h"
22
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <iomanip>
27 #include <iterator>
28 #include <boost/foreach.hpp>
29 #define foreach BOOST_FOREACH
30
31
32 class LogErrorAnalysis : public edm::EDFilter {
33 public:
34 explicit LogErrorAnalysis(const edm::ParameterSet & iConfig);
35 virtual ~LogErrorAnalysis() { }
36
37 virtual bool filter(edm::Event & iEvent, const edm::EventSetup & iSetup);
38 virtual bool beginLuminosityBlock(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup);
39 virtual bool endLuminosityBlock(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup);
40 virtual bool beginRun(edm::Run &run, const edm::EventSetup &iSetup);
41 virtual bool endRun(edm::Run &run, const edm::EventSetup &iSetup);
42 virtual void endJob();
43
44 private:
45 typedef edm::ErrorSummaryEntry Error;
46 struct ErrorSort {
47 bool operator()(const Error &e1, const Error &e2) {
48 if (e1.severity.getLevel() != e2.severity.getLevel()) return e1.severity.getLevel() > e2.severity.getLevel();
49 if (e1.module != e2.module) return e1.module < e2.module;
50 if (e1.category != e2.category) return e1.category < e2.category;
51 return false;
52 }
53 };
54 typedef std::vector<edm::ErrorSummaryEntry> ErrorList;
55 typedef std::set<edm::ErrorSummaryEntry,ErrorSort> ErrorSet;
56
57 edm::InputTag src_;
58 bool readSummaryMode_;
59 size_t npassLumi_, nfailLumi_;
60 size_t npassRun_, nfailRun_;
61 std::set<std::string> modulesToWatch_;
62 std::set<std::string> modulesToIgnore_;
63 std::set<std::string> categoriesToWatch_;
64 std::set<std::string> categoriesToIgnore_;
65 std::map<std::pair<uint32_t,uint32_t>, std::pair<size_t,size_t> > statsPerLumi_;
66 std::map<uint32_t, std::pair<size_t,size_t> > statsPerRun_;
67 ErrorSet errorCollectionAll_;
68 ErrorSet errorCollectionThisLumi_, errorCollectionThisRun_;
69 double thresholdPerLumi_, thresholdPerRun_;
70 size_t maxSavedEventsPerLumi_;
71 bool verbose_, veryVerbose_;
72
73 template<typename Collection> static void increment(ErrorSet &scoreboard, Collection &list);
74 template<typename Collection> static void print(const Collection &errors) ;
75
76 static std::auto_ptr<ErrorList > serialize(const ErrorSet &set) {
77 std::auto_ptr<ErrorList> ret(new ErrorList(set.begin(), set.end()));
78 return ret;
79 }
80 };
81
82 LogErrorAnalysis::LogErrorAnalysis(const edm::ParameterSet & iConfig) :
83 src_(iConfig.getParameter<edm::InputTag>("src")),
84 readSummaryMode_(iConfig.existsAs<bool>("readSummaryMode") ? iConfig.getParameter<bool>("readSummaryMode") : false),
85 thresholdPerLumi_(iConfig.getParameter<double>("maxErrorFractionInLumi")),
86 thresholdPerRun_(iConfig.getParameter<double>("maxErrorFractionInRun")),
87 maxSavedEventsPerLumi_(iConfig.getParameter<uint32_t>("maxSavedEventsPerLumiAndError")),
88 verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
89 veryVerbose_(iConfig.getUntrackedParameter<bool>("veryVerbose", false))
90 {
91 produces<ErrorList, edm::InLumi>();
92 produces<int, edm::InLumi>("pass");
93 produces<int, edm::InLumi>("fail");
94 //produces<ErrorList, edm::InRun>();
95
96 if (iConfig.existsAs<std::vector<std::string> >("modulesToWatch")) {
97 std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToWatch");
98 if (!(modules.size() == 1 && modules[0] == "*")) {
99 modulesToWatch_.insert(modules.begin(), modules.end());
100 }
101 }
102 if (iConfig.existsAs<std::vector<std::string> >("modulesToIgnore")) {
103 std::vector<std::string> modules = iConfig.getParameter<std::vector<std::string> >("modulesToIgnore");
104 if (!(modules.size() == 1 && modules[0] == "*")) {
105 modulesToIgnore_.insert(modules.begin(), modules.end());
106 }
107 }
108 if (iConfig.existsAs<std::vector<std::string> >("categoriesToWatch")) {
109 std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToWatch");
110 if (!(categories.size() == 1 && categories[0] == "*")) {
111 categoriesToWatch_.insert(categories.begin(), categories.end());
112 }
113 }
114 if (iConfig.existsAs<std::vector<std::string> >("categoriesToIgnore")) {
115 std::vector<std::string> categories = iConfig.getParameter<std::vector<std::string> >("categoriesToIgnore");
116 if (!(categories.size() == 1 && categories[0] == "*")) {
117 categoriesToIgnore_.insert(categories.begin(), categories.end());
118 }
119 }
120 std::ostream_iterator<std::string> dump(std::cout, ", ");
121 std::cout << "\nWatch modules: " ; std::copy(modulesToWatch_.begin(), modulesToWatch_.end(), dump);
122 std::cout << "\nIgnore modules: " ; std::copy(modulesToIgnore_.begin(), modulesToIgnore_.end(), dump);
123 std::cout << "\nIgnore categories: " ; std::copy(categoriesToIgnore_.begin(), categoriesToIgnore_.end(), dump);
124 std::cout << "\nWatch categories: " ; std::copy(categoriesToWatch_.begin(), categoriesToWatch_.end(), dump);
125 std::cout << std::endl;
126
127 }
128
129 bool
130 LogErrorAnalysis::beginLuminosityBlock(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) {
131 npassLumi_ = 0; nfailLumi_ = 0;
132 errorCollectionThisLumi_.clear();
133 if (readSummaryMode_) {
134 edm::Handle<ErrorList> handle;
135 edm::Handle<int> hpass, hfail;
136 lumi.getByLabel(src_, handle);
137 lumi.getByLabel(edm::InputTag(src_.label(), "pass", src_.process()), hpass);
138 lumi.getByLabel(edm::InputTag(src_.label(), "fail", src_.process()), hfail);
139 increment(errorCollectionThisLumi_, *handle);
140 npassLumi_ = *hpass;
141 nfailLumi_ = *hfail;
142 npassRun_ += npassLumi_;
143 nfailRun_ += nfailLumi_;
144 }
145 return true;
146 }
147
148 bool
149 LogErrorAnalysis::endLuminosityBlock(edm::LuminosityBlock &lumi, const edm::EventSetup &iSetup) {
150 statsPerLumi_[std::pair<uint32_t,uint32_t>(lumi.run(), lumi.luminosityBlock())] = std::pair<size_t,size_t>(npassLumi_, nfailLumi_);
151 if (nfailLumi_ < thresholdPerLumi_*(npassLumi_+nfailLumi_)) {
152 increment(errorCollectionThisRun_, errorCollectionThisLumi_);
153 }
154 if (verbose_) {
155 if (!errorCollectionThisLumi_.empty()) {
156 std::cout << "\n === REPORT FOR RUN " << lumi.run() << " LUMI " << lumi.luminosityBlock() << " === " << std::endl;
157 print(errorCollectionThisLumi_);
158 }
159 }
160 lumi.put(serialize(errorCollectionThisLumi_));
161 std::auto_ptr<int> outpass(new int(npassLumi_)); lumi.put(outpass, "pass");
162 std::auto_ptr<int> outfail(new int(nfailLumi_)); lumi.put(outfail, "fail");
163 return true;
164 }
165
166
167 bool
168 LogErrorAnalysis::beginRun(edm::Run &run, const edm::EventSetup &iSetup) {
169 npassRun_ = 0; nfailRun_ = 0;
170 errorCollectionThisRun_.clear();
171 return true;
172 }
173
174 bool
175 LogErrorAnalysis::endRun(edm::Run &run, const edm::EventSetup &iSetup) {
176 statsPerRun_[run.run()] = std::pair<size_t,size_t>(npassRun_, nfailRun_);
177 if (nfailRun_ < thresholdPerRun_*(npassRun_+nfailRun_)) {
178 increment(errorCollectionAll_, errorCollectionThisRun_);
179 }
180 if (verbose_) {
181 if (!errorCollectionThisRun_.empty()) {
182 std::cout << "\n === REPORT FOR RUN " << run.run() << " === " << std::endl;
183 print(errorCollectionThisRun_);
184 }
185 }
186 //run.put(serialize(errorCollectionThisRun_));
187 return true;
188 }
189
190 void
191 LogErrorAnalysis::endJob() {
192 if (verbose_) {
193 std::cout << "\n === REPORT FOR JOB === " << std::endl;
194 print(errorCollectionAll_);
195
196 typedef std::pair<size_t,size_t> counter;
197
198 std::cout << "\n === SCOREBOARD PER RUN === " << std::endl;
199 typedef std::pair<uint32_t, counter> hitRun;
200 foreach(const hitRun &hit, statsPerRun_) {
201 double fract = hit.second.second/double(hit.second.first + hit.second.second);
202 printf("run %6d: fail %7d, pass %7d, fraction %7.3f%%%s\n", hit.first, hit.second.second, hit.second.first, fract*100., (fract >= thresholdPerRun_ ? " (run excluded from summary list)" : ""));
203 }
204
205 std::cout << "\n === SCOREBOARD PER LUMI === " << std::endl;
206 typedef std::pair<std::pair<uint32_t,uint32_t>, counter> hitLumi;
207 foreach(const hitLumi &hit, statsPerLumi_) {
208 double fract = hit.second.second/double(hit.second.first + hit.second.second);
209 printf("run %6d, lumi %4d: fail %7d, pass %7d, 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)" : ""));
210 }
211 }
212 }
213
214 bool
215 LogErrorAnalysis::filter(edm::Event & iEvent, const edm::EventSetup & iSetup) {
216 if (readSummaryMode_) return true;
217
218 bool fail = false, save = false;
219
220 edm::Handle<ErrorList> errors;
221 iEvent.getByLabel(src_, errors);
222
223
224 if (errors->empty()) {
225 npassRun_++; npassLumi_++;
226 return false;
227 }
228
229 foreach (const Error &err, *errors) {
230 if (!modulesToWatch_.empty() && (modulesToWatch_.count(err.module) == 0)) continue;
231 if (!categoriesToWatch_.empty() && (categoriesToWatch_.count(err.category) == 0)) continue;
232 if (!modulesToIgnore_.empty() && (modulesToIgnore_.count(err.module) != 0)) continue;
233 if (!categoriesToIgnore_.empty() && (categoriesToIgnore_.count(err.category) != 0)) continue;
234
235 fail = true;
236 std::pair<ErrorSet::iterator, bool> result = errorCollectionThisLumi_.insert(err);
237 if (!result.second) { // already there
238 // need the const_cast as set elements are const
239 const_cast<unsigned int &>(result.first->count) += err.count;
240 if (result.first->count < maxSavedEventsPerLumi_) save = true;
241 } else {
242 save = true;
243 }
244
245 }
246 if (save && veryVerbose_) {
247 std::cout << "\n === REPORT FOR EVENT " << iEvent.id().event() << " RUN " << iEvent.run() << " LUMI " << iEvent.luminosityBlock() << " === " << std::endl;
248 print(*errors);
249 }
250
251
252 if (fail) { nfailLumi_++; nfailRun_++; } else { npassRun_++; npassLumi_++; }
253 return save;
254 }
255
256 template<typename Collection>
257 void
258 LogErrorAnalysis::increment(ErrorSet &scoreboard, Collection &list) {
259 foreach (const Error &err, list) {
260 std::pair<ErrorSet::iterator, bool> result = scoreboard.insert(err);
261 // need the const_cast as set elements are const
262 if (!result.second) const_cast<unsigned int &>(result.first->count) += err.count;
263 }
264 }
265
266 template<typename Collection>
267 void
268 LogErrorAnalysis::print(const Collection &errors) {
269 using namespace std;
270 cout << setw(40) << left << "Category" << " " <<
271 setw(60) << left << "Module" << " " <<
272 setw(10) << left << "Level" << " " <<
273 setw(9) << right << "Count" << "\n";
274 cout << setw(40) << left << "----------------------------------------" << " " <<
275 setw(60) << left << "------------------------------------------------------------" << " " <<
276 setw(10) << left << "----------" << " " <<
277 setw(9) << right << "---------" << "\n";
278 foreach (const Error &err, errors) {
279 cout << setw(40) << left << err.category << " " <<
280 setw(60) << left << err.module << " " <<
281 setw(10) << left << err.severity.getName() << " " <<
282 setw(9) << right << err.count << "\n";
283 }
284 cout << flush;
285 }
286
287 #include "FWCore/Framework/interface/MakerMacros.h"
288 DEFINE_FWK_MODULE(LogErrorAnalysis);