ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/plugins/LogErrorAnalysis.cc
Revision: 1.2
Committed: Tue Nov 30 13:32:46 2010 UTC (14 years, 5 months ago) by venturia
Content type: text/plain
Branch: MAIN
Changes since 1.1: +4 -3 lines
Log Message:
modified InputTag.h path

File Contents

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