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 
47 private:
48  class Plot {
49  public:
51  virtual ~Plot() {}
52  virtual void fill(const FlatTable &table, const std::vector<bool> &rowsel) = 0;
53  const std::string &name() const { return plot_->getName(); }
54 
55  protected:
57  };
58  class Count1D : public Plot {
59  public:
61  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
62  cfg.getParameter<std::string>("title"),
63  cfg.getParameter<uint32_t>("nbins"),
64  cfg.getParameter<double>("min"),
65  cfg.getParameter<double>("max"))) {}
66  ~Count1D() override {}
67  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
68  plot_->Fill(std::accumulate(rowsel.begin(), rowsel.end(), 0u));
69  }
70  };
71 
72  class Plot1D : public Plot {
73  public:
75  : Plot(booker.book1D(cfg.getParameter<std::string>("name"),
76  cfg.getParameter<std::string>("title"),
77  cfg.getParameter<uint32_t>("nbins"),
78  cfg.getParameter<double>("min"),
79  cfg.getParameter<double>("max"))),
80  col_(cfg.getParameter<std::string>("column")) {}
81  ~Plot1D() override {}
82  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
83  int icol = table.columnIndex(col_);
84  if (icol == -1)
85  return; // columns may be missing (e.g. mc-only)
86  switch (table.columnType(icol)) {
88  vfill<float>(table, icol, rowsel);
89  break;
91  vfill<int>(table, icol, rowsel);
92  break;
94  vfill<uint8_t>(table, icol, rowsel);
95  break;
97  vfill<uint8_t>(table, icol, rowsel);
98  break;
99  }
100  }
101 
102  protected:
104  template <typename T>
105  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
106  const auto &data = table.columnData<T>(icol);
107  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
108  if (rowsel[i])
109  plot_->Fill(data[i]);
110  }
111  }
112  };
113  class Profile1D : public Plot {
114  public:
116  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
117  cfg.getParameter<std::string>("title"),
118  cfg.getParameter<uint32_t>("nbins"),
119  cfg.getParameter<double>("min"),
120  cfg.getParameter<double>("max"),
121  0.,
122  0.,
123  "")),
124  ycol_(cfg.getParameter<std::string>("ycolumn")),
125  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
126  ~Profile1D() override {}
127  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
128  int icolx = table.columnIndex(xcol_);
129  int icoly = table.columnIndex(ycol_);
130  if (icolx == -1)
131  throw cms::Exception("LogicError", "Missing " + xcol_);
132  if (icoly == -1)
133  throw cms::Exception("LogicError", "Missing " + ycol_);
134  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
135  if (rowsel[irow])
136  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
137  }
138  }
139 
140  protected:
142  };
143 
144  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
145  const std::string &kind = cfg.getParameter<std::string>("kind");
146  if (kind == "none")
147  return nullptr;
148  if (kind == "count1d")
149  return std::make_unique<Count1D>(booker, cfg);
150  if (kind == "hist1d")
151  return std::make_unique<Plot1D>(booker, cfg);
152  if (kind == "prof1d")
153  return std::make_unique<Profile1D>(booker, cfg);
154  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
155  }
156 
157  struct SelGroupConfig {
161  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
162  std::vector<std::unique_ptr<Plot>> plots;
164  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {}
165  bool nullCut() const { return cutstr.empty(); }
166  void fillSel(const FlatTable &table, std::vector<bool> &out) {
167  out.resize(table.size());
168  if (nullCut()) {
169  std::fill(out.begin(), out.end(), true);
170  } else {
171  if (!cutptr) {
172  cutptr.reset(new Selector(replaceStringsToColumGets(cutstr, table)));
173  }
174  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
175  out[i] = (*cutptr)(table.row(i));
176  }
177  }
178  }
179  };
180  struct GroupConfig {
181  std::vector<edm::ParameterSet> plotPSets;
182  std::vector<SelGroupConfig> selGroups;
183  };
184  std::map<std::string, GroupConfig> groups_;
185 };
186 
188  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
189  for (const std::string &name : vplots.getParameterNamesForType<edm::ParameterSet>()) {
190  auto &group = groups_[name];
191  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
192  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
193  group.selGroups.emplace_back(); // no selection (all entries)
194  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
195  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
196  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
197  }
198  }
199  consumesMany<FlatTable>();
200 }
201 
203  booker.setCurrentFolder("Physics/NanoAODDQM");
204 
205  for (auto &pair : groups_) {
206  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
207  for (auto &sels : pair.second.selGroups) {
208  std::string dir("Physics/NanoAODDQM/" + pair.first);
209  if (!sels.nullCut())
210  dir += "/" + sels.name;
211  booker.setCurrentFolder(dir);
212  auto &plots = sels.plots;
213  plots.clear();
214  plots.reserve(pair.second.plotPSets.size());
215  for (const auto &cfg : pair.second.plotPSets) {
216  auto plot = makePlot(booker, cfg);
217  if (plot)
218  plots.push_back(std::move(plot));
219  }
220  }
221  }
222 }
223 
225  std::vector<edm::Handle<FlatTable>> alltables;
226  iEvent.getManyByType(alltables);
227  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
228 
229  for (const auto &htab : alltables) {
230  if (htab->extension())
231  continue;
232  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
233  }
234  for (const auto &htab : alltables) {
235  if (htab->extension()) {
236  if (maintables.find(htab->name()) == maintables.end())
237  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
238  maintables[htab->name()].second.push_back(htab.product());
239  }
240  }
241 
242  FlatTable merged;
243  for (auto &pair : groups_) {
244  const std::string &name = pair.first;
245  if (maintables.find(name) == maintables.end())
246  continue; // may happen for missing collections
247  auto &tables = maintables[name];
248  const FlatTable *table = tables.first;
249  if (!tables.second.empty()) {
250  merged = *tables.first;
251  for (auto *other : tables.second) {
252  merged.addExtension(*other);
253  }
254  table = &merged;
255  }
256  std::vector<bool> selbits;
257  for (auto &sel : pair.second.selGroups) {
258  sel.fillSel(*table, selbits);
259 
260  for (auto &plot : sel.plots) {
261  plot->fill(*table, selbits);
262  }
263  }
264  }
265 }
266 
nanoaod::FlatTable::FloatColumn
Definition: FlatTable.h:39
NanoAODDQM::Plot1D::Plot1D
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:74
dqm::impl::MonitorElement
Definition: MonitorElement.h:98
nanoDQM_cff.vplots
vplots
Definition: nanoDQM_cff.py:22
mps_fire.i
i
Definition: mps_fire.py:355
NanoAODDQM::Count1D
Definition: NanoAODDQM.cc:58
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
TkAlMuonSelectors_cfi.cut
cut
Definition: TkAlMuonSelectors_cfi.py:5
NanoAODDQM::SelGroupConfig
Definition: NanoAODDQM.cc:157
NanoAODDQM::Plot1D
Definition: NanoAODDQM.cc:72
NanoAODDQM::Plot::Plot
Plot(MonitorElement *me)
Definition: NanoAODDQM.cc:50
edm::Run
Definition: Run.h:45
NanoAODDQM::Plot::name
const std::string & name() const
Definition: NanoAODDQM.cc:53
NanoAODDQM::Plot1D::col_
std::string col_
Definition: NanoAODDQM.cc:103
NanoAODDQM::Plot1D::fill
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:82
dqm::implementation::NavigatorBase::setCurrentFolder
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
DQMStore.h
NanoAODDQM::GroupConfig
Definition: NanoAODDQM.cc:180
NanoAODDQM::SelGroupConfig::cutptr
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:161
postprocess-scan-build.tables
tables
Definition: postprocess-scan-build.py:10
plotFactory.plot
plot
Definition: plotFactory.py:109
NanoAODDQM::Profile1D::ycol_
std::string ycol_
Definition: NanoAODDQM.cc:141
NanoAODDQM::Profile1D::Profile1D
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:115
MakerMacros.h
dqmdumpme.last
last
Definition: dqmdumpme.py:56
NanoAODDQM::SelGroupConfig::cutstr
std::string cutstr
Definition: NanoAODDQM.cc:160
NanoAODDQM::Plot::plot_
MonitorElement * plot_
Definition: NanoAODDQM.cc:56
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
NanoAODDQM::GroupConfig::selGroups
std::vector< SelGroupConfig > selGroups
Definition: NanoAODDQM.cc:182
NanoAODDQM::Count1D::fill
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:67
dqm::impl::MonitorElement::Fill
void Fill(long long x)
Definition: MonitorElement.h:290
NanoAODDQM::Plot
Definition: NanoAODDQM.cc:48
trackingPlots.other
other
Definition: trackingPlots.py:1465
NanoAODDQM::bookHistograms
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: NanoAODDQM.cc:202
DQMEDAnalyzer.h
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DQMEDAnalyzer
Definition: DQMEDAnalyzer.py:1
ntuplemaker.fill
fill
Definition: ntuplemaker.py:304
nanoaod::FlatTable::addExtension
void addExtension(const FlatTable &extension)
Definition: FlatTable.cc:11
NanoAODDQM::Plot1D::~Plot1D
~Plot1D() override
Definition: NanoAODDQM.cc:81
edm::ParameterSet
Definition: ParameterSet.h:36
HLTObjectsMonitor_cfi.plots
plots
Definition: HLTObjectsMonitor_cfi.py:17
Event.h
NanoAODDQM::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: NanoAODDQM.cc:224
NanoAODDQM::Plot1D::vfill
void vfill(const FlatTable &table, int icol, const std::vector< bool > &rowsel)
Definition: NanoAODDQM.cc:105
match
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
NanoAODDQM::Profile1D::fill
void fill(const FlatTable &table, const std::vector< bool > &rowsel) override
Definition: NanoAODDQM.cc:127
iEvent
int iEvent
Definition: GenABIO.cc:224
NanoAODDQM::GroupConfig::plotPSets
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:181
NanoAODDQM::Count1D::Count1D
Count1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:60
edm::EventSetup
Definition: EventSetup.h:57
NanoAODDQM::SelGroupConfig::SelGroupConfig
SelGroupConfig(const std::string &nam, const std::string &cut)
Definition: NanoAODDQM.cc:164
NanoAODDQM::SelGroupConfig::plots
std::vector< std::unique_ptr< Plot > > plots
Definition: NanoAODDQM.cc:162
nanoaod::FlatTable::UInt8Column
Definition: FlatTable.h:41
CalibrationSummaryClient_cfi.kind
kind
Definition: CalibrationSummaryClient_cfi.py:37
FlatTable.h
InputTag.h
NanoAODDQM::FlatTable
nanoaod::FlatTable FlatTable
Definition: NanoAODDQM.cc:38
looper.cfg
cfg
Definition: looper.py:297
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
nanoaod::FlatTable
Definition: FlatTable.h:36
nanoaod::FlatTable::IntColumn
Definition: FlatTable.h:40
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
StringCutObjectSelector.h
NanoAODDQM::SelGroupConfig::nullCut
bool nullCut() const
Definition: NanoAODDQM.cc:165
jets_cff.expr
expr
Definition: jets_cff.py:449
Frameworkfwd.h
T
long double T
Definition: Basic3DVectorLD.h:48
nanoaod::FlatTable::BoolColumn
Definition: FlatTable.h:42
StringCutObjectSelector
Definition: StringCutObjectSelector.h:16
Exception
Definition: hltDiff.cc:246
NanoAODDQM::makePlot
static std::unique_ptr< Plot > makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:144
NanoAODDQM::SelGroupConfig::name
std::string name
Definition: NanoAODDQM.cc:159
NanoAODDQM::SelGroupConfig::Selector
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:158
NanoAODDQM::groups_
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:184
dqm::impl::MonitorElement::getName
const std::string & getName() const
get name of ME
Definition: MonitorElement.h:250
NanoAODDQM::NanoAODDQM
NanoAODDQM(const edm::ParameterSet &)
Definition: NanoAODDQM.cc:187
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
dqm::implementation::IBooker
Definition: DQMStore.h:43
NanoAODDQM::Count1D::~Count1D
~Count1D() override
Definition: NanoAODDQM.cc:66
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
cms::Exception
Definition: Exception.h:70
L1TMuonDQMOffline_cfi.cuts
cuts
Definition: L1TMuonDQMOffline_cfi.py:41
NanoAODDQM::Profile1D::xcol_
std::string xcol_
Definition: NanoAODDQM.cc:141
EgammaValidation_Wenu_cff.sel
sel
Definition: EgammaValidation_Wenu_cff.py:33
hlt_dqm_clientPB-live_cfg.me
me
Definition: hlt_dqm_clientPB-live_cfg.py:56
TableParser.table
table
Definition: TableParser.py:111
nanoDQM_cfi.sels
sels
Definition: nanoDQM_cfi.py:9
edm::Event
Definition: Event.h:73
NanoAODDQM
Definition: NanoAODDQM.cc:36
NanoAODDQM::SelGroupConfig::SelGroupConfig
SelGroupConfig()
Definition: NanoAODDQM.cc:163
NanoAODDQM::SelGroupConfig::fillSel
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:166
NanoAODDQM::Plot::fill
virtual void fill(const FlatTable &table, const std::vector< bool > &rowsel)=0
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
NanoAODDQM::Profile1D::~Profile1D
~Profile1D() override
Definition: NanoAODDQM.cc:126
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
NanoAODDQM::Plot::~Plot
virtual ~Plot()
Definition: NanoAODDQM.cc:51
watchdog.group
group
Definition: watchdog.py:82
NanoAODDQM::Profile1D
Definition: NanoAODDQM.cc:113
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:316