CMS 3D CMS Logo

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