CMS 3D CMS Logo

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