CMS 3D CMS Logo

NanoAODDQM.cc
Go to the documentation of this file.
6 //#include "FWCore/ParameterSet/interface/EmptyGroupDescription.h"
13 
16 
17 #include <memory>
18 #include <limits>
19 #include <numeric>
20 #include <regex>
21 #include <sstream>
22 #include <type_traits>
23 
24 namespace {
25  bool notANumber(std::string const &iValue) {
26  auto find = iValue.find_first_not_of("0123456789");
27  return find != std::string::npos;
28  }
29  bool notRowMethod(std::string const &iValue) { return iValue != "row" and iValue != "table"; }
30 
31  bool notAFunction(std::string const &iSuffix) { return (iSuffix.empty() or iSuffix[0] != '('); }
32  std::string replaceStringsToColumGets(const std::string &expr) {
33  std::regex token("\\w+");
34  std::sregex_iterator tbegin(expr.begin(), expr.end(), token), tend;
35  if (tbegin == tend)
36  return expr;
37  std::stringstream out;
38  std::sregex_iterator last;
39  for (std::sregex_iterator i = tbegin; i != tend; last = i, ++i) {
40  const std::smatch &match = *i;
41  out << match.prefix().str();
42  if (notAFunction(i->suffix().str()) and notANumber(match.str()) and notRowMethod(match.str())) {
43  out << "getAnyValue(\"" << match.str() << "\")";
44  } else {
45  out << match.str();
46  }
47  }
48  out << last->suffix().str();
49  return out.str();
50  };
51 } // namespace
52 
53 class NanoAODDQM : public DQMEDAnalyzer {
54 public:
56 
58  void analyze(const edm::Event &, const edm::EventSetup &) override;
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
61 
62 protected:
63  //Book histograms
64  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
65 
66 private:
67  class Plot {
68  public:
70  virtual ~Plot() {}
71  virtual void fill(const FlatTable &table, const std::vector<bool> &rowsel) = 0;
72  const std::string &name() const { return plot_->getName(); }
73 
74  protected:
76  };
77  class Count1D : public Plot {
78  public:
80  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
81  cfg.getParameter<std::string>("title"),
82  cfg.getParameter<uint32_t>("nbins"),
83  cfg.getParameter<double>("min"),
84  cfg.getParameter<double>("max"))) {}
85  ~Count1D() override {}
86  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
87  plot_->Fill(std::accumulate(rowsel.begin(), rowsel.end(), 0u));
88  }
89  };
90 
91  class Plot1D : public Plot {
92  public:
94  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
95  cfg.getParameter<std::string>("title"),
96  cfg.getParameter<uint32_t>("nbins"),
97  cfg.getParameter<double>("min"),
98  cfg.getParameter<double>("max"))),
99  col_(cfg.getParameter<std::string>("column")),
100  bitset_(cfg.getParameter<bool>("bitset")) {}
101  ~Plot1D() override {}
102  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
103  int icol = table.columnIndex(col_);
104  if (icol == -1)
105  return; // columns may be missing (e.g. mc-only)
106  switch (table.columnType(icol)) {
108  vfill<uint8_t>(table, icol, rowsel);
109  break;
111  vfill<int16_t>(table, icol, rowsel);
112  break;
114  vfill<uint16_t>(table, icol, rowsel);
115  break;
117  vfill<int32_t>(table, icol, rowsel);
118  break;
120  vfill<uint32_t>(table, icol, rowsel);
121  break;
123  vfill<bool>(table, icol, rowsel);
124  break;
126  vfill<float>(table, icol, rowsel);
127  break;
129  vfill<double>(table, icol, rowsel);
130  break;
131  default:
132  throw cms::Exception("LogicError", "Unsupported type");
133  }
134  }
135 
136  protected:
138  bool bitset_;
139  template <typename T>
140  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
141  const auto &data = table.columnData<T>(icol);
142  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
143  if (rowsel[i]) {
144  const T val = data[i];
146  if (bitset_) {
147  for (unsigned int b = 0; b < std::numeric_limits<T>::digits; b++) {
148  if ((val >> b) & 0b1)
149  plot_->Fill(b);
150  }
151  } else {
152  plot_->Fill(val);
153  }
154  } else {
155  plot_->Fill(val);
156  }
157  }
158  }
159  }
160  };
161 
162  class Profile1D : public Plot {
163  public:
165  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
166  cfg.getParameter<std::string>("title"),
167  cfg.getParameter<uint32_t>("nbins"),
168  cfg.getParameter<double>("min"),
169  cfg.getParameter<double>("max"),
170  0.,
171  0.,
172  "")),
173  ycol_(cfg.getParameter<std::string>("ycolumn")),
174  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
175  ~Profile1D() override {}
176  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
177  int icolx = table.columnIndex(xcol_);
178  int icoly = table.columnIndex(ycol_);
179  if (icolx == -1)
180  throw cms::Exception("LogicError", "Missing " + xcol_);
181  if (icoly == -1)
182  throw cms::Exception("LogicError", "Missing " + ycol_);
183  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
184  if (rowsel[irow])
185  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
186  }
187  }
188 
189  protected:
191  };
192 
193  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
194  const std::string &kind = cfg.getParameter<std::string>("kind");
195  if (kind == "none")
196  return nullptr;
197  if (kind == "count1d")
198  return std::make_unique<Count1D>(booker, cfg);
199  if (kind == "hist1d")
200  return std::make_unique<Plot1D>(booker, cfg);
201  if (kind == "prof1d")
202  return std::make_unique<Profile1D>(booker, cfg);
203  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
204  }
205 
206  struct SelGroupConfig {
210  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
211  std::vector<std::unique_ptr<Plot>> plots;
213  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {
214  if (not nullCut()) {
215  cutptr = std::make_unique<Selector>(replaceStringsToColumGets(cutstr));
216  }
217  }
218  bool nullCut() const { return cutstr.empty(); }
219  void fillSel(const FlatTable &table, std::vector<bool> &out) {
220  out.resize(table.size());
221  if (nullCut()) {
222  std::fill(out.begin(), out.end(), true);
223  } else {
224  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
225  out[i] = (*cutptr)(table.row(i));
226  }
227  }
228  }
229  };
230  struct GroupConfig {
231  std::vector<edm::ParameterSet> plotPSets;
232  std::vector<SelGroupConfig> selGroups;
233  };
234  std::map<std::string, GroupConfig> groups_;
236 };
237 
238 NanoAODDQM::NanoAODDQM(const edm::ParameterSet &iConfig) : getterOfProducts_(edm::ProcessMatch("*"), this) {
239  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
240  for (const std::string &name : vplots.getParameterNamesForType<edm::ParameterSet>()) {
241  auto &group = groups_[name];
242  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
243  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
244  group.selGroups.emplace_back(); // no selection (all entries)
245  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
246  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
247  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
248  }
249  }
250  callWhenNewProductsRegistered(getterOfProducts_);
251 }
252 
255 
257  sels.setComment("a paramerter set to define the selections to be made from the table row");
259 
261  edm::ParameterDescription<std::string> title("title", true, edm::Comment("title of the plot"));
262  edm::ParameterDescription<uint32_t> nbins("nbins", true, edm::Comment("number of bins of the plot"));
263  edm::ParameterDescription<double> min("min", true, edm::Comment("starting value of the x axis"));
264  edm::ParameterDescription<double> max("max", true, edm::Comment("ending value of the x axis"));
265  edm::ParameterDescription<bool> bitset("bitset", false, true, edm::Comment("plot individual bits of values"));
267  "column", true, edm::Comment("name of the raw to fill the content of the plot"));
269  "xcolumn", true, edm::Comment("name of the raw to fill the x content of the plot"));
271  "ycolumn", true, edm::Comment("name of the raw to fill the y content of the plot"));
272 
274  plot.setComment("a parameter set that defines a DQM histogram");
275  plot.ifValue(
276  edm::ParameterDescription<std::string>("kind", "none", true, edm::Comment("the type of histogram")),
277  "none" >> (name) or //it should really be edm::EmptyGroupDescription(), but name is used in python by modifiers
278  "count1d" >> (name and title and nbins and min and max) or
279  "hist1d" >> (name and title and nbins and min and max and column and bitset) or
280  "prof1d" >> (name and title and nbins and min and max and xcolumn and ycolumn));
281 
283  vplot.setComment(
284  "a parameter set to define all the plots to be made from a table row selected from the name of the PSet");
285  vplot.add<edm::ParameterSetDescription>("sels", sels);
286  vplot.addVPSet("plots", plot);
287 
289  vplots.setComment("a parameter set to define all the set of plots to be made from the tables");
291  desc.add<edm::ParameterSetDescription>("vplots", vplots);
292 
293  descriptions.addWithDefaultLabel(desc);
294 }
295 
297  booker.setCurrentFolder("Physics/NanoAODDQM");
298 
299  for (auto &pair : groups_) {
300  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
301  for (auto &sels : pair.second.selGroups) {
302  std::string dir("Physics/NanoAODDQM/" + pair.first);
303  if (!sels.nullCut())
304  dir += "/" + sels.name;
305  booker.setCurrentFolder(dir);
306  auto &plots = sels.plots;
307  plots.clear();
308  plots.reserve(pair.second.plotPSets.size());
309  for (const auto &cfg : pair.second.plotPSets) {
310  auto plot = makePlot(booker, cfg);
311  if (plot)
312  plots.push_back(std::move(plot));
313  }
314  }
315  }
316 }
317 
319  std::vector<edm::Handle<FlatTable>> alltables;
320  getterOfProducts_.fillHandles(iEvent, alltables);
321  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
322 
323  for (const auto &htab : alltables) {
324  if (htab->extension())
325  continue;
326  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
327  }
328  for (const auto &htab : alltables) {
329  if (htab->extension()) {
330  if (maintables.find(htab->name()) == maintables.end())
331  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
332  maintables[htab->name()].second.push_back(htab.product());
333  }
334  }
335 
337  for (auto &pair : groups_) {
338  const std::string &name = pair.first;
339  if (maintables.find(name) == maintables.end())
340  continue; // may happen for missing collections
341  auto &tables = maintables[name];
342  const FlatTable *table = tables.first;
343  if (!tables.second.empty()) {
344  merged = *tables.first;
345  for (auto *other : tables.second) {
346  merged.addExtension(*other);
347  }
348  table = &merged;
349  }
350  std::vector<bool> selbits;
351  for (auto &sel : pair.second.selGroups) {
352  sel.fillSel(*table, selbits);
353 
354  for (auto &plot : sel.plots) {
355  plot->fill(*table, selbits);
356  }
357  }
358  }
359 }
360 
std::string col_
Definition: NanoAODDQM.cc:137
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:231
const std::string & name() const
Definition: NanoAODDQM.cc:72
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
virtual void fill(const FlatTable &table, const std::vector< bool > &rowsel)=0
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: NanoAODDQM.cc:296
std::vector< SelGroupConfig > selGroups
Definition: NanoAODDQM.cc:232
~Plot1D() override
Definition: NanoAODDQM.cc:101
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:210
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:318
nanoaod::FlatTable FlatTable
Definition: NanoAODDQM.cc:55
static std::unique_ptr< Plot > makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:193
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:93
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
NanoAODDQM(const edm::ParameterSet &)
Definition: NanoAODDQM.cc:238
~Count1D() override
Definition: NanoAODDQM.cc:85
void Fill(long long x)
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:164
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:234
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
virtual ~Plot()
Definition: NanoAODDQM.cc:70
void vfill(const FlatTable &table, int icol, const std::vector< bool > &rowsel)
Definition: NanoAODDQM.cc:140
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:176
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Count1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:79
Plot(MonitorElement *me)
Definition: NanoAODDQM.cc:69
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:86
std::vector< std::unique_ptr< Plot > > plots
Definition: NanoAODDQM.cc:211
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: NanoAODDQM.cc:253
double b
Definition: hdecay.h:120
edm::GetterOfProducts< FlatTable > getterOfProducts_
Definition: NanoAODDQM.cc:235
const std::string & getName() const
get name of ME
SelGroupConfig(const std::string &nam, const std::string &cut)
Definition: NanoAODDQM.cc:213
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:207
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:219
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
long double T
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:102
MonitorElement * plot_
Definition: NanoAODDQM.cc:75
def move(src, dest)
Definition: eostools.py:511
static constexpr float b1
Definition: Run.h:45