CMS 3D CMS Logo

CandidateSummaryTable.cc
Go to the documentation of this file.
1 //
2 //
3 
21 #include <iomanip>
22 #include <atomic>
23 
26 
27 namespace pathelpers {
28  struct Record {
30  mutable std::atomic<size_t> present, empty, min, max, total;
31  Record(const edm::InputTag& tag) : src(tag), present(0), empty(0), min(0), max(0), total(0) {}
32  Record(const Record& other) : src(other.src),
33  present(other.present.load()),
34  empty(other.empty.load()),
35  min(other.min.load()),
36  max(other.max.load()),
37  total(other.total.load()) {}
39  present++;
40  const size_t size = items.size();
41  if (size == 0) {
42  empty++;
43  } else {
44  auto previousMin = min.load();
45  while( previousMin > size and not min.compare_exchange_weak(previousMin, size) ) {}
46  auto previousMax = max.load();
47  while( previousMax < size and not max.compare_exchange_weak(previousMax, size) ) {}
48  }
49  total += size;
50  }
51  };
52 
53  struct RecordCache {
54  RecordCache(const edm::ParameterSet& iConfig) :
55  perEvent_(iConfig.getUntrackedParameter<bool>("perEvent", false)),
56  perJob_(iConfig.getUntrackedParameter<bool>("perJob", true)),
57  self_(iConfig.getParameter<std::string>("@module_label")),
58  logName_(iConfig.getUntrackedParameter<std::string>("logName")),
59  dumpItems_(iConfig.getUntrackedParameter<bool>("dumpItems", false)),
60  totalEvents_(0) {
61  const std::vector<edm::InputTag>& tags = iConfig.getParameter<std::vector<edm::InputTag> >("candidates");
62  for( const auto& tag : tags ) {
63  collections_.emplace_back( tag );
64  }
65  }
66  const bool perEvent_, perJob_;
67  const std::string self_, logName_;
68  const bool dumpItems_;
69  mutable std::atomic<size_t> totalEvents_;
70  std::vector<Record> collections_; // size of vector is never altered later! (atomics are in the class below)
71  };
72 
73 }
74 
75 namespace pat {
76  class CandidateSummaryTable : public edm::stream::EDAnalyzer<edm::GlobalCache<pathelpers::RecordCache> > {
77  public:
78  explicit CandidateSummaryTable(const edm::ParameterSet & iConfig, const pathelpers::RecordCache*);
79  ~CandidateSummaryTable() override;
80 
81  static std::unique_ptr<pathelpers::RecordCache> initializeGlobalCache(edm::ParameterSet const& conf) {
82  return std::make_unique<pathelpers::RecordCache>(conf);
83  }
84 
85  void analyze(const edm::Event & iEvent, const edm::EventSetup& iSetup) override;
86 
87  static void globalEndJob(const pathelpers::RecordCache*);
88 
89  private:
90  std::vector<std::pair<edm::InputTag,edm::EDGetTokenT<edm::View<reco::Candidate> > > > srcTokens;
91  };
92 } // namespace
93 
95  const std::vector<edm::InputTag>& inputs = iConfig.getParameter<std::vector<edm::InputTag> >("candidates");
96  for (std::vector<edm::InputTag>::const_iterator it = inputs.begin(); it != inputs.end(); ++it) {
97  srcTokens.emplace_back(*it, consumes<edm::View<reco::Candidate> >(*it));
98  }
99 }
100 
102 }
103 
104 void
106  using namespace edm;
107  using std::setw; using std::left; using std::right; using std::setprecision;
108 
110  if (globalCache()->perEvent_) {
111  LogInfo(globalCache()->logName_) << "Per Event Table " << globalCache()->logName_
112  << " (" << globalCache()->self_ << ", run:event "
113  << iEvent.id().run() << ":" << iEvent.id().event() << ")";
114  }
115  ++(globalCache()->totalEvents_);
116  auto& collections = globalCache()->collections_;
117  auto tags = srcTokens.cbegin();
118  for (auto it = collections.begin(), ed = collections.end(); it != ed; ++it, ++tags) {
119  iEvent.getByToken(tags->second, candidates);
120  if (!candidates.failedToGet()) it->update(*candidates);
121  if (globalCache()->perEvent_) {
122  LogVerbatim(globalCache()->logName_) << " " << setw(30) << left << it->src.encode() << right;
123  if (globalCache()->dumpItems_) {
124  size_t i = 0;
125  std::ostringstream oss;
126  for (View<reco::Candidate>::const_iterator cand = candidates->begin(), endc = candidates->end(); cand != endc; ++cand, ++i) {
127  oss << " [" << setw(3) << i << "]" <<
128  " pt " << setw(7) << setprecision(5) << cand->pt() <<
129  " eta " << setw(7) << setprecision(5) << cand->eta() <<
130  " phi " << setw(7) << setprecision(5) << cand->phi() <<
131  " et " << setw(7) << setprecision(5) << cand->et() <<
132  " phi " << setw(7) << setprecision(5) << cand->phi() <<
133  " charge " << setw(2) << cand->charge() <<
134  " id " << setw(7) << cand->pdgId() <<
135  " st " << setw(7) << cand->status() << "\n";
136  }
137  LogVerbatim(globalCache()->logName_) << oss.str();
138  }
139  }
140  }
141  if (globalCache()->perEvent_) LogInfo(globalCache()->logName_) << "" ; // add an empty line
142 }
143 
144 
145 void
147  using std::setw; using std::left; using std::right; using std::setprecision;
148  if (rcd->perJob_) {
149  std::ostringstream oss;
150  oss << "Summary Table " << rcd->logName_ << " (" << rcd->self_ << ", events total " << rcd->totalEvents_ << ")\n";
151  for (auto it = rcd->collections_.cbegin(), ed = rcd->collections_.cend(); it != ed; ++it) {
152  oss << " " << setw(30) << left << it->src.encode() << right <<
153  " present " << setw(7) << it->present << " (" << setw(4) << setprecision(3) << (it->present*100.0/rcd->totalEvents_) << "%)" <<
154  " empty " << setw(7) << it->empty << " (" << setw(4) << setprecision(3) << (it->empty*100.0/rcd->totalEvents_) << "%)" <<
155  " min " << setw(7) << it->min <<
156  " max " << setw(7) << it->max <<
157  " total " << setw(7) << it->total <<
158  " avg " << setw(5) << setprecision(3) << (it->total/double(rcd->totalEvents_)) << "\n";
159  }
160  oss << "\n";
161  edm::LogVerbatim(rcd->logName_) << oss.str();
162  }
163 }
164 
RunNumber_t run() const
Definition: EventID.h:39
size
Write out results.
std::vector< Record > collections_
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
static std::unique_ptr< pathelpers::RecordCache > initializeGlobalCache(edm::ParameterSet const &conf)
CandidateSummaryTable(const edm::ParameterSet &iConfig, const pathelpers::RecordCache *)
std::atomic< size_t > total
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
virtual example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
Record(const Record &other)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
Record(const edm::InputTag &tag)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::atomic< size_t > totalEvents_
std::vector< std::pair< edm::InputTag, edm::EDGetTokenT< edm::View< reco::Candidate > > > > srcTokens
size_type size() const
const edm::InputTag src
RecordCache(const edm::ParameterSet &iConfig)
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:230
Produce a summary table of some candidate collections.
static void globalEndJob(const pathelpers::RecordCache *)
std::atomic< size_t > empty
bool failedToGet() const
Definition: HandleBase.h:78
std::atomic< size_t > present
def load(fileName)
Definition: svgfig.py:546
std::atomic< size_t > max
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
void update(const edm::View< reco::Candidate > &items) const
std::atomic< size_t > min