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  std::string replaceStringsToColumGets(const std::string &expr, const nanoaod::FlatTable &table) {
26  std::regex token("\\w+");
27  std::sregex_iterator tbegin(expr.begin(), expr.end(), token), tend;
28  if (tbegin == tend)
29  return expr;
30  std::stringstream out;
31  std::sregex_iterator last;
32  for (std::sregex_iterator i = tbegin; i != tend; last = i, ++i) {
33  const std::smatch &match = *i;
34  out << match.prefix().str();
35  if (table.columnIndex(match.str()) != -1) {
36  out << "getAnyValue(\"" << match.str() << "\")";
37  } else {
38  out << match.str();
39  }
40  }
41  out << last->suffix().str();
42  return out.str();
43  };
44 } // namespace
45 
46 class NanoAODDQM : public DQMEDAnalyzer {
47 public:
49 
51  void analyze(const edm::Event &, const edm::EventSetup &) override;
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
54 
55 protected:
56  //Book histograms
57  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
58 
59 private:
60  class Plot {
61  public:
63  virtual ~Plot() {}
64  virtual void fill(const FlatTable &table, const std::vector<bool> &rowsel) = 0;
65  const std::string &name() const { return plot_->getName(); }
66 
67  protected:
69  };
70  class Count1D : public Plot {
71  public:
73  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
74  cfg.getParameter<std::string>("title"),
75  cfg.getParameter<uint32_t>("nbins"),
76  cfg.getParameter<double>("min"),
77  cfg.getParameter<double>("max"))) {}
78  ~Count1D() override {}
79  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
80  plot_->Fill(std::accumulate(rowsel.begin(), rowsel.end(), 0u));
81  }
82  };
83 
84  class Plot1D : public Plot {
85  public:
87  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
88  cfg.getParameter<std::string>("title"),
89  cfg.getParameter<uint32_t>("nbins"),
90  cfg.getParameter<double>("min"),
91  cfg.getParameter<double>("max"))),
92  col_(cfg.getParameter<std::string>("column")),
93  bitset_(cfg.getParameter<bool>("bitset")) {}
94  ~Plot1D() override {}
95  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
96  int icol = table.columnIndex(col_);
97  if (icol == -1)
98  return; // columns may be missing (e.g. mc-only)
99  switch (table.columnType(icol)) {
101  vfill<int8_t>(table, icol, rowsel);
102  break;
104  vfill<uint8_t>(table, icol, rowsel);
105  break;
107  vfill<int16_t>(table, icol, rowsel);
108  break;
110  vfill<uint16_t>(table, icol, rowsel);
111  break;
113  vfill<int32_t>(table, icol, rowsel);
114  break;
116  vfill<uint32_t>(table, icol, rowsel);
117  break;
119  vfill<bool>(table, icol, rowsel);
120  break;
122  vfill<float>(table, icol, rowsel);
123  break;
125  vfill<double>(table, icol, rowsel);
126  break;
127  default:
128  throw cms::Exception("LogicError", "Unsupported type");
129  }
130  }
131 
132  protected:
134  bool bitset_;
135  template <typename T>
136  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
137  const auto &data = table.columnData<T>(icol);
138  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
139  if (rowsel[i]) {
140  const T val = data[i];
141  if constexpr (std::is_integral<T>::value) {
142  if (bitset_) {
143  for (unsigned int b = 0; b < std::numeric_limits<T>::digits; b++) {
144  if ((val >> b) & 0b1)
145  plot_->Fill(b);
146  }
147  } else {
148  plot_->Fill(val);
149  }
150  } else {
151  plot_->Fill(val);
152  }
153  }
154  }
155  }
156  };
157 
158  class Profile1D : public Plot {
159  public:
161  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
162  cfg.getParameter<std::string>("title"),
163  cfg.getParameter<uint32_t>("nbins"),
164  cfg.getParameter<double>("min"),
165  cfg.getParameter<double>("max"),
166  0.,
167  0.,
168  "")),
169  ycol_(cfg.getParameter<std::string>("ycolumn")),
170  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
171  ~Profile1D() override {}
172  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
173  int icolx = table.columnIndex(xcol_);
174  int icoly = table.columnIndex(ycol_);
175  if (icolx == -1)
176  throw cms::Exception("LogicError", "Missing " + xcol_);
177  if (icoly == -1)
178  throw cms::Exception("LogicError", "Missing " + ycol_);
179  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
180  if (rowsel[irow])
181  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
182  }
183  }
184 
185  protected:
187  };
188 
189  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
190  const std::string &kind = cfg.getParameter<std::string>("kind");
191  if (kind == "none")
192  return nullptr;
193  if (kind == "count1d")
194  return std::make_unique<Count1D>(booker, cfg);
195  if (kind == "hist1d")
196  return std::make_unique<Plot1D>(booker, cfg);
197  if (kind == "prof1d")
198  return std::make_unique<Profile1D>(booker, cfg);
199  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
200  }
201 
202  struct SelGroupConfig {
206  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
207  std::vector<std::unique_ptr<Plot>> plots;
209  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {}
210  bool nullCut() const { return cutstr.empty(); }
211  void fillSel(const FlatTable &table, std::vector<bool> &out) {
212  out.resize(table.size());
213  if (nullCut()) {
214  std::fill(out.begin(), out.end(), true);
215  } else {
216  if (!cutptr) {
217  cutptr = std::make_unique<Selector>(replaceStringsToColumGets(cutstr, table));
218  }
219  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
220  out[i] = (*cutptr)(table.row(i));
221  }
222  }
223  }
224  };
225  struct GroupConfig {
226  std::vector<edm::ParameterSet> plotPSets;
227  std::vector<SelGroupConfig> selGroups;
228  };
229  std::map<std::string, GroupConfig> groups_;
231 };
232 
233 NanoAODDQM::NanoAODDQM(const edm::ParameterSet &iConfig) : getterOfProducts_(edm::ProcessMatch("*"), this) {
234  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
235  for (const std::string &name : vplots.getParameterNamesForType<edm::ParameterSet>()) {
236  auto &group = groups_[name];
237  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
238  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
239  group.selGroups.emplace_back(); // no selection (all entries)
240  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
241  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
242  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
243  }
244  }
245  callWhenNewProductsRegistered(getterOfProducts_);
246 }
247 
250 
252  sels.setComment("a paramerter set to define the selections to be made from the table row");
254 
256  edm::ParameterDescription<std::string> title("title", true, edm::Comment("title of the plot"));
257  edm::ParameterDescription<uint32_t> nbins("nbins", true, edm::Comment("number of bins of the plot"));
258  edm::ParameterDescription<double> min("min", true, edm::Comment("starting value of the x axis"));
259  edm::ParameterDescription<double> max("max", true, edm::Comment("ending value of the x axis"));
260  edm::ParameterDescription<bool> bitset("bitset", false, true, edm::Comment("plot individual bits of values"));
262  "column", true, edm::Comment("name of the raw to fill the content of the plot"));
264  "xcolumn", true, edm::Comment("name of the raw to fill the x content of the plot"));
266  "ycolumn", true, edm::Comment("name of the raw to fill the y content of the plot"));
267 
269  plot.setComment("a parameter set that defines a DQM histogram");
270  plot.ifValue(
271  edm::ParameterDescription<std::string>("kind", "none", true, edm::Comment("the type of histogram")),
272  "none" >> (name) or //it should really be edm::EmptyGroupDescription(), but name is used in python by modifiers
273  "count1d" >> (name and title and nbins and min and max) or
274  "hist1d" >> (name and title and nbins and min and max and column and bitset) or
275  "prof1d" >> (name and title and nbins and min and max and xcolumn and ycolumn));
276 
278  vplot.setComment(
279  "a parameter set to define all the plots to be made from a table row selected from the name of the PSet");
280  vplot.add<edm::ParameterSetDescription>("sels", sels);
281  vplot.addVPSet("plots", plot);
282 
284  vplots.setComment("a parameter set to define all the set of plots to be made from the tables");
286  desc.add<edm::ParameterSetDescription>("vplots", vplots);
287 
288  descriptions.addWithDefaultLabel(desc);
289 }
290 
292  booker.setCurrentFolder("Physics/NanoAODDQM");
293 
294  for (auto &pair : groups_) {
295  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
296  for (auto &sels : pair.second.selGroups) {
297  std::string dir("Physics/NanoAODDQM/" + pair.first);
298  if (!sels.nullCut())
299  dir += "/" + sels.name;
300  booker.setCurrentFolder(dir);
301  auto &plots = sels.plots;
302  plots.clear();
303  plots.reserve(pair.second.plotPSets.size());
304  for (const auto &cfg : pair.second.plotPSets) {
305  auto plot = makePlot(booker, cfg);
306  if (plot)
307  plots.push_back(std::move(plot));
308  }
309  }
310  }
311 }
312 
314  std::vector<edm::Handle<FlatTable>> alltables;
315  getterOfProducts_.fillHandles(iEvent, alltables);
316  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
317 
318  for (const auto &htab : alltables) {
319  if (htab->extension())
320  continue;
321  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
322  }
323  for (const auto &htab : alltables) {
324  if (htab->extension()) {
325  if (maintables.find(htab->name()) == maintables.end())
326  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
327  maintables[htab->name()].second.push_back(htab.product());
328  }
329  }
330 
331  FlatTable merged;
332  for (auto &pair : groups_) {
333  const std::string &name = pair.first;
334  if (maintables.find(name) == maintables.end())
335  continue; // may happen for missing collections
336  auto &tables = maintables[name];
337  const FlatTable *table = tables.first;
338  if (!tables.second.empty()) {
339  merged = *tables.first;
340  for (auto *other : tables.second) {
341  merged.addExtension(*other);
342  }
343  table = &merged;
344  }
345  std::vector<bool> selbits;
346  for (auto &sel : pair.second.selGroups) {
347  sel.fillSel(*table, selbits);
348 
349  for (auto &plot : sel.plots) {
350  plot->fill(*table, selbits);
351  }
352  }
353  }
354 }
355 
void addExtension(const FlatTable &extension)
Definition: FlatTable.cc:11
std::string col_
Definition: NanoAODDQM.cc:133
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
weight_default_t b1[25]
Definition: b1.h:9
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:226
const std::string & name() const
Definition: NanoAODDQM.cc:65
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:291
std::vector< SelGroupConfig > selGroups
Definition: NanoAODDQM.cc:227
~Plot1D() override
Definition: NanoAODDQM.cc:94
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:206
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:313
nanoaod::FlatTable FlatTable
Definition: NanoAODDQM.cc:48
static std::unique_ptr< Plot > makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:189
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:86
NanoAODDQM(const edm::ParameterSet &)
Definition: NanoAODDQM.cc:233
~Count1D() override
Definition: NanoAODDQM.cc:78
void Fill(long long x)
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:160
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:229
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:63
void vfill(const FlatTable &table, int icol, const std::vector< bool > &rowsel)
Definition: NanoAODDQM.cc:136
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:172
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Count1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:72
Plot(MonitorElement *me)
Definition: NanoAODDQM.cc:62
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:79
std::vector< std::unique_ptr< Plot > > plots
Definition: NanoAODDQM.cc:207
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: NanoAODDQM.cc:248
double b
Definition: hdecay.h:118
edm::GetterOfProducts< FlatTable > getterOfProducts_
Definition: NanoAODDQM.cc:230
const std::string & getName() const
get name of ME
SelGroupConfig(const std::string &nam, const std::string &cut)
Definition: NanoAODDQM.cc:209
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:203
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:211
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:95
MonitorElement * plot_
Definition: NanoAODDQM.cc:68
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45