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<uint8_t>(table, icol, rowsel);
102  break;
104  vfill<int16_t>(table, icol, rowsel);
105  break;
107  vfill<uint16_t>(table, icol, rowsel);
108  break;
110  vfill<int32_t>(table, icol, rowsel);
111  break;
113  vfill<uint32_t>(table, icol, rowsel);
114  break;
116  vfill<bool>(table, icol, rowsel);
117  break;
119  vfill<float>(table, icol, rowsel);
120  break;
122  vfill<double>(table, icol, rowsel);
123  break;
124  default:
125  throw cms::Exception("LogicError", "Unsupported type");
126  }
127  }
128 
129  protected:
131  bool bitset_;
132  template <typename T>
133  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
134  const auto &data = table.columnData<T>(icol);
135  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
136  if (rowsel[i]) {
137  const T val = data[i];
139  if (bitset_) {
140  for (unsigned int b = 0; b < std::numeric_limits<T>::digits; b++) {
141  if ((val >> b) & 0b1)
142  plot_->Fill(b);
143  }
144  } else {
145  plot_->Fill(val);
146  }
147  } else {
148  plot_->Fill(val);
149  }
150  }
151  }
152  }
153  };
154 
155  class Profile1D : public Plot {
156  public:
158  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
159  cfg.getParameter<std::string>("title"),
160  cfg.getParameter<uint32_t>("nbins"),
161  cfg.getParameter<double>("min"),
162  cfg.getParameter<double>("max"),
163  0.,
164  0.,
165  "")),
166  ycol_(cfg.getParameter<std::string>("ycolumn")),
167  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
168  ~Profile1D() override {}
169  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
170  int icolx = table.columnIndex(xcol_);
171  int icoly = table.columnIndex(ycol_);
172  if (icolx == -1)
173  throw cms::Exception("LogicError", "Missing " + xcol_);
174  if (icoly == -1)
175  throw cms::Exception("LogicError", "Missing " + ycol_);
176  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
177  if (rowsel[irow])
178  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
179  }
180  }
181 
182  protected:
184  };
185 
186  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
187  const std::string &kind = cfg.getParameter<std::string>("kind");
188  if (kind == "none")
189  return nullptr;
190  if (kind == "count1d")
191  return std::make_unique<Count1D>(booker, cfg);
192  if (kind == "hist1d")
193  return std::make_unique<Plot1D>(booker, cfg);
194  if (kind == "prof1d")
195  return std::make_unique<Profile1D>(booker, cfg);
196  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
197  }
198 
199  struct SelGroupConfig {
203  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
204  std::vector<std::unique_ptr<Plot>> plots;
206  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {}
207  bool nullCut() const { return cutstr.empty(); }
208  void fillSel(const FlatTable &table, std::vector<bool> &out) {
209  out.resize(table.size());
210  if (nullCut()) {
211  std::fill(out.begin(), out.end(), true);
212  } else {
213  if (!cutptr) {
214  cutptr = std::make_unique<Selector>(replaceStringsToColumGets(cutstr, table));
215  }
216  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
217  out[i] = (*cutptr)(table.row(i));
218  }
219  }
220  }
221  };
222  struct GroupConfig {
223  std::vector<edm::ParameterSet> plotPSets;
224  std::vector<SelGroupConfig> selGroups;
225  };
226  std::map<std::string, GroupConfig> groups_;
228 };
229 
230 NanoAODDQM::NanoAODDQM(const edm::ParameterSet &iConfig) : getterOfProducts_(edm::ProcessMatch("*"), this) {
231  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
232  for (const std::string &name : vplots.getParameterNamesForType<edm::ParameterSet>()) {
233  auto &group = groups_[name];
234  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
235  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
236  group.selGroups.emplace_back(); // no selection (all entries)
237  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
238  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
239  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
240  }
241  }
242  callWhenNewProductsRegistered(getterOfProducts_);
243 }
244 
247 
249  sels.setComment("a paramerter set to define the selections to be made from the table row");
251 
253  edm::ParameterDescription<std::string> title("title", true, edm::Comment("title of the plot"));
254  edm::ParameterDescription<uint32_t> nbins("nbins", true, edm::Comment("number of bins of the plot"));
255  edm::ParameterDescription<double> min("min", true, edm::Comment("starting value of the x axis"));
256  edm::ParameterDescription<double> max("max", true, edm::Comment("ending value of the x axis"));
257  edm::ParameterDescription<bool> bitset("bitset", false, true, edm::Comment("plot individual bits of values"));
259  "column", true, edm::Comment("name of the raw to fill the content of the plot"));
261  "xcolumn", true, edm::Comment("name of the raw to fill the x content of the plot"));
263  "ycolumn", true, edm::Comment("name of the raw to fill the y content of the plot"));
264 
266  plot.setComment("a parameter set that defines a DQM histogram");
267  plot.ifValue(
268  edm::ParameterDescription<std::string>("kind", "none", true, edm::Comment("the type of histogram")),
269  "none" >> (name) or //it should really be edm::EmptyGroupDescription(), but name is used in python by modifiers
270  "count1d" >> (name and title and nbins and min and max) or
271  "hist1d" >> (name and title and nbins and min and max and column and bitset) or
272  "prof1d" >> (name and title and nbins and min and max and xcolumn and ycolumn));
273 
275  vplot.setComment(
276  "a parameter set to define all the plots to be made from a table row selected from the name of the PSet");
277  vplot.add<edm::ParameterSetDescription>("sels", sels);
278  vplot.addVPSet("plots", plot);
279 
281  vplots.setComment("a parameter set to define all the set of plots to be made from the tables");
283  desc.add<edm::ParameterSetDescription>("vplots", vplots);
284 
285  descriptions.addWithDefaultLabel(desc);
286 }
287 
289  booker.setCurrentFolder("Physics/NanoAODDQM");
290 
291  for (auto &pair : groups_) {
292  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
293  for (auto &sels : pair.second.selGroups) {
294  std::string dir("Physics/NanoAODDQM/" + pair.first);
295  if (!sels.nullCut())
296  dir += "/" + sels.name;
297  booker.setCurrentFolder(dir);
298  auto &plots = sels.plots;
299  plots.clear();
300  plots.reserve(pair.second.plotPSets.size());
301  for (const auto &cfg : pair.second.plotPSets) {
302  auto plot = makePlot(booker, cfg);
303  if (plot)
304  plots.push_back(std::move(plot));
305  }
306  }
307  }
308 }
309 
311  std::vector<edm::Handle<FlatTable>> alltables;
312  getterOfProducts_.fillHandles(iEvent, alltables);
313  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
314 
315  for (const auto &htab : alltables) {
316  if (htab->extension())
317  continue;
318  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
319  }
320  for (const auto &htab : alltables) {
321  if (htab->extension()) {
322  if (maintables.find(htab->name()) == maintables.end())
323  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
324  maintables[htab->name()].second.push_back(htab.product());
325  }
326  }
327 
328  FlatTable merged;
329  for (auto &pair : groups_) {
330  const std::string &name = pair.first;
331  if (maintables.find(name) == maintables.end())
332  continue; // may happen for missing collections
333  auto &tables = maintables[name];
334  const FlatTable *table = tables.first;
335  if (!tables.second.empty()) {
336  merged = *tables.first;
337  for (auto *other : tables.second) {
338  merged.addExtension(*other);
339  }
340  table = &merged;
341  }
342  std::vector<bool> selbits;
343  for (auto &sel : pair.second.selGroups) {
344  sel.fillSel(*table, selbits);
345 
346  for (auto &plot : sel.plots) {
347  plot->fill(*table, selbits);
348  }
349  }
350  }
351 }
352 
void addExtension(const FlatTable &extension)
Definition: FlatTable.cc:11
std::string col_
Definition: NanoAODDQM.cc:130
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:223
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:288
std::vector< SelGroupConfig > selGroups
Definition: NanoAODDQM.cc:224
~Plot1D() override
Definition: NanoAODDQM.cc:94
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:203
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:310
nanoaod::FlatTable FlatTable
Definition: NanoAODDQM.cc:48
static std::unique_ptr< Plot > makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:186
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:86
NanoAODDQM(const edm::ParameterSet &)
Definition: NanoAODDQM.cc:230
~Count1D() override
Definition: NanoAODDQM.cc:78
void Fill(long long x)
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:157
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:226
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:133
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:169
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:204
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: NanoAODDQM.cc:245
double b
Definition: hdecay.h:120
edm::GetterOfProducts< FlatTable > getterOfProducts_
Definition: NanoAODDQM.cc:227
const std::string & getName() const
get name of ME
SelGroupConfig(const std::string &nam, const std::string &cut)
Definition: NanoAODDQM.cc:206
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:200
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:208
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
static constexpr float b1
Definition: Run.h:45