CMS 3D CMS Logo

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