CMS 3D CMS Logo

NanoAODDQM.cc
Go to the documentation of this file.
9 
10 #include <regex>
11 #include <sstream>
12 #include <numeric>
13 
14 namespace {
15  std::string replaceStringsToColumGets(const std::string &expr, const nanoaod::FlatTable &table) {
16  std::regex token("\\w+");
17  std::sregex_iterator tbegin(expr.begin(), expr.end(), token), tend;
18  if (tbegin == tend)
19  return expr;
20  std::stringstream out;
21  std::sregex_iterator last;
22  for (std::sregex_iterator i = tbegin; i != tend; last = i, ++i) {
23  std::smatch match = *i;
24  out << match.prefix().str();
25  if (table.columnIndex(match.str()) != -1) {
26  out << "getAnyValue(\"" << match.str() << "\")";
27  } else {
28  out << match.str();
29  }
30  }
31  out << last->suffix().str();
32  return out.str();
33  };
34 } // namespace
35 
36 class NanoAODDQM : public DQMEDAnalyzer {
37 public:
39 
41  void analyze(const edm::Event &, const edm::EventSetup &) override;
42 
43 protected:
44  //Book histograms
45  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
46  void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override {}
47 
48 private:
49  class Plot {
50  public:
52  virtual ~Plot() {}
53  virtual void fill(const FlatTable &table, const std::vector<bool> &rowsel) = 0;
54  const std::string &name() const { return plot_->getName(); }
55 
56  protected:
58  };
59  class Count1D : public Plot {
60  public:
62  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
63  cfg.getParameter<std::string>("title"),
64  cfg.getParameter<uint32_t>("nbins"),
65  cfg.getParameter<double>("min"),
66  cfg.getParameter<double>("max"))) {}
67  ~Count1D() override {}
68  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
69  plot_->Fill(std::accumulate(rowsel.begin(), rowsel.end(), 0u));
70  }
71  };
72 
73  class Plot1D : public Plot {
74  public:
76  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
77  cfg.getParameter<std::string>("title"),
78  cfg.getParameter<uint32_t>("nbins"),
79  cfg.getParameter<double>("min"),
80  cfg.getParameter<double>("max"))),
81  col_(cfg.getParameter<std::string>("column")) {}
82  ~Plot1D() override {}
83  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
84  int icol = table.columnIndex(col_);
85  if (icol == -1)
86  return; // columns may be missing (e.g. mc-only)
87  switch (table.columnType(icol)) {
89  vfill<float>(table, icol, rowsel);
90  break;
92  vfill<int>(table, icol, rowsel);
93  break;
95  vfill<uint8_t>(table, icol, rowsel);
96  break;
98  vfill<uint8_t>(table, icol, rowsel);
99  break;
100  }
101  }
102 
103  protected:
105  template <typename T>
106  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
107  const auto &data = table.columnData<T>(icol);
108  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
109  if (rowsel[i])
110  plot_->Fill(data[i]);
111  }
112  }
113  };
114  class Profile1D : public Plot {
115  public:
117  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
118  cfg.getParameter<std::string>("title"),
119  cfg.getParameter<uint32_t>("nbins"),
120  cfg.getParameter<double>("min"),
121  cfg.getParameter<double>("max"),
122  0.,
123  0.,
124  "")),
125  ycol_(cfg.getParameter<std::string>("ycolumn")),
126  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
127  ~Profile1D() override {}
128  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
129  int icolx = table.columnIndex(xcol_);
130  int icoly = table.columnIndex(ycol_);
131  if (icolx == -1)
132  throw cms::Exception("LogicError", "Missing " + xcol_);
133  if (icoly == -1)
134  throw cms::Exception("LogicError", "Missing " + ycol_);
135  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
136  if (rowsel[irow])
137  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
138  }
139  }
140 
141  protected:
143  };
144 
145  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
146  const std::string &kind = cfg.getParameter<std::string>("kind");
147  if (kind == "none")
148  return nullptr;
149  if (kind == "count1d")
150  return std::make_unique<Count1D>(booker, cfg);
151  if (kind == "hist1d")
152  return std::make_unique<Plot1D>(booker, cfg);
153  if (kind == "prof1d")
154  return std::make_unique<Profile1D>(booker, cfg);
155  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
156  }
157 
158  struct SelGroupConfig {
162  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
163  std::vector<std::unique_ptr<Plot>> plots;
164  SelGroupConfig() : name(), cutstr(), cutptr(), plots() {}
165  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {}
166  bool nullCut() const { return cutstr.empty(); }
167  void fillSel(const FlatTable &table, std::vector<bool> &out) {
168  out.resize(table.size());
169  if (nullCut()) {
170  std::fill(out.begin(), out.end(), true);
171  } else {
172  if (!cutptr) {
173  cutptr.reset(new Selector(replaceStringsToColumGets(cutstr, table)));
174  }
175  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
176  out[i] = (*cutptr)(table.row(i));
177  }
178  }
179  }
180  };
181  struct GroupConfig {
182  std::vector<edm::ParameterSet> plotPSets;
183  std::vector<SelGroupConfig> selGroups;
184  };
185  std::map<std::string, GroupConfig> groups_;
186 };
187 
189  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
191  auto &group = groups_[name];
192  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
193  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
194  group.selGroups.emplace_back(); // no selection (all entries)
195  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
196  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
197  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
198  }
199  }
200  consumesMany<FlatTable>();
201 }
202 
204  booker.setCurrentFolder("Physics/NanoAODDQM");
205 
206  for (auto &pair : groups_) {
207  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
208  for (auto &sels : pair.second.selGroups) {
209  std::string dir("Physics/NanoAODDQM/" + pair.first);
210  if (!sels.nullCut())
211  dir += "/" + sels.name;
212  booker.setCurrentFolder(dir);
213  auto &plots = sels.plots;
214  plots.clear();
215  plots.reserve(pair.second.plotPSets.size());
216  for (const auto &cfg : pair.second.plotPSets) {
217  auto plot = makePlot(booker, cfg);
218  if (plot)
219  plots.push_back(std::move(plot));
220  }
221  }
222  }
223 }
224 
226  std::vector<edm::Handle<FlatTable>> alltables;
227  iEvent.getManyByType(alltables);
228  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
229 
230  for (const auto &htab : alltables) {
231  if (htab->extension())
232  continue;
233  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
234  }
235  for (const auto &htab : alltables) {
236  if (htab->extension()) {
237  if (maintables.find(htab->name()) == maintables.end())
238  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
239  maintables[htab->name()].second.push_back(htab.product());
240  }
241  }
242 
243  FlatTable merged;
244  for (auto &pair : groups_) {
245  const std::string &name = pair.first;
246  if (maintables.find(name) == maintables.end())
247  continue; // may happen for missing collections
248  auto &tables = maintables[name];
249  const FlatTable *table = tables.first;
250  if (!tables.second.empty()) {
251  merged = *tables.first;
252  for (auto *other : tables.second) {
253  merged.addExtension(*other);
254  }
255  table = &merged;
256  }
257  std::vector<bool> selbits;
258  for (auto &sel : pair.second.selGroups) {
259  sel.fillSel(*table, selbits);
260 
261  for (auto &plot : sel.plots) {
262  plot->fill(*table, selbits);
263  }
264  }
265  }
266 }
267 
boost::sub_range< const std::vector< T > > columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:66
void addExtension(const FlatTable &extension)
Definition: FlatTable.cc:11
T getParameter(std::string const &) const
std::string col_
Definition: NanoAODDQM.cc:104
RowView row(unsigned int row) const
Definition: FlatTable.h:103
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:182
int columnIndex(const std::string &name) const
Definition: FlatTable.cc:3
void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:46
virtual void fill(const FlatTable &table, const std::vector< bool > &rowsel)=0
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: NanoAODDQM.cc:203
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:418
std::vector< SelGroupConfig > selGroups
Definition: NanoAODDQM.cc:183
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:58
~Plot1D() override
Definition: NanoAODDQM.cc:82
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:162
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:225
const std::string & getName() const
get name of ME
nanoaod::FlatTable FlatTable
Definition: NanoAODDQM.cc:38
void getManyByType(std::vector< Handle< PROD >> &results) const
Definition: Event.h:516
static std::unique_ptr< Plot > makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:145
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:75
NanoAODDQM(const edm::ParameterSet &)
Definition: NanoAODDQM.cc:188
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:168
~Count1D() override
Definition: NanoAODDQM.cc:67
void Fill(long long x)
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:116
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:185
virtual ~Plot()
Definition: NanoAODDQM.cc:52
void vfill(const FlatTable &table, int icol, const std::vector< bool > &rowsel)
Definition: NanoAODDQM.cc:106
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:128
Count1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:61
Plot(MonitorElement *me)
Definition: NanoAODDQM.cc:51
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:68
std::vector< std::unique_ptr< Plot > > plots
Definition: NanoAODDQM.cc:163
SelGroupConfig(const std::string &nam, const std::string &cut)
Definition: NanoAODDQM.cc:165
unsigned int size() const
Definition: FlatTable.h:50
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:159
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:167
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:83
MonitorElement * plot_
Definition: NanoAODDQM.cc:57
double getAnyValue(unsigned int row, unsigned int column) const
Definition: FlatTable.cc:30
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
const std::string & name() const
Definition: NanoAODDQM.cc:54