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<bool>(table, icol, rowsel);
98  break;
99  default:
100  throw cms::Exception("LogicError", "Unsupported type");
101  }
102  }
103 
104  protected:
106  template <typename T>
107  void vfill(const FlatTable &table, int icol, const std::vector<bool> &rowsel) {
108  const auto &data = table.columnData<T>(icol);
109  for (unsigned int i = 0, n = data.size(); i < n; ++i) {
110  if (rowsel[i])
111  plot_->Fill(data[i]);
112  }
113  }
114  };
115  class Profile1D : public Plot {
116  public:
118  : Plot(booker.bookProfile(cfg.getParameter<std::string>("name"),
119  cfg.getParameter<std::string>("title"),
120  cfg.getParameter<uint32_t>("nbins"),
121  cfg.getParameter<double>("min"),
122  cfg.getParameter<double>("max"),
123  0.,
124  0.,
125  "")),
126  ycol_(cfg.getParameter<std::string>("ycolumn")),
127  xcol_(cfg.getParameter<std::string>("xcolumn")) {}
128  ~Profile1D() override {}
129  void fill(const FlatTable &table, const std::vector<bool> &rowsel) override {
130  int icolx = table.columnIndex(xcol_);
131  int icoly = table.columnIndex(ycol_);
132  if (icolx == -1)
133  throw cms::Exception("LogicError", "Missing " + xcol_);
134  if (icoly == -1)
135  throw cms::Exception("LogicError", "Missing " + ycol_);
136  for (unsigned int irow = 0, n = table.size(); irow < n; ++irow) {
137  if (rowsel[irow])
138  plot_->Fill(table.getAnyValue(irow, icolx), table.getAnyValue(irow, icoly));
139  }
140  }
141 
142  protected:
144  };
145 
146  static std::unique_ptr<Plot> makePlot(DQMStore::IBooker &booker, const edm::ParameterSet &cfg) {
147  const std::string &kind = cfg.getParameter<std::string>("kind");
148  if (kind == "none")
149  return nullptr;
150  if (kind == "count1d")
151  return std::make_unique<Count1D>(booker, cfg);
152  if (kind == "hist1d")
153  return std::make_unique<Plot1D>(booker, cfg);
154  if (kind == "prof1d")
155  return std::make_unique<Profile1D>(booker, cfg);
156  throw cms::Exception("Configuration", "Unsupported plot kind '" + kind + "'");
157  }
158 
159  struct SelGroupConfig {
163  std::unique_ptr<StringCutObjectSelector<FlatTable::RowView>> cutptr;
164  std::vector<std::unique_ptr<Plot>> plots;
166  SelGroupConfig(const std::string &nam, const std::string &cut) : name(nam), cutstr(cut), cutptr(), plots() {}
167  bool nullCut() const { return cutstr.empty(); }
168  void fillSel(const FlatTable &table, std::vector<bool> &out) {
169  out.resize(table.size());
170  if (nullCut()) {
171  std::fill(out.begin(), out.end(), true);
172  } else {
173  if (!cutptr) {
174  cutptr.reset(new Selector(replaceStringsToColumGets(cutstr, table)));
175  }
176  for (unsigned int i = 0, n = table.size(); i < n; ++i) {
177  out[i] = (*cutptr)(table.row(i));
178  }
179  }
180  }
181  };
182  struct GroupConfig {
183  std::vector<edm::ParameterSet> plotPSets;
184  std::vector<SelGroupConfig> selGroups;
185  };
186  std::map<std::string, GroupConfig> groups_;
187 };
188 
190  const edm::ParameterSet &vplots = iConfig.getParameter<edm::ParameterSet>("vplots");
191  for (const std::string &name : vplots.getParameterNamesForType<edm::ParameterSet>()) {
192  auto &group = groups_[name];
193  const auto &pset = vplots.getParameter<edm::ParameterSet>(name);
194  group.plotPSets = pset.getParameter<std::vector<edm::ParameterSet>>("plots");
195  group.selGroups.emplace_back(); // no selection (all entries)
196  const auto &cuts = pset.getParameter<edm::ParameterSet>("sels");
197  for (const std::string &cname : cuts.getParameterNamesForType<std::string>()) {
198  group.selGroups.emplace_back(cname, cuts.getParameter<std::string>(cname));
199  }
200  }
201  consumesMany<FlatTable>();
202 }
203 
205  booker.setCurrentFolder("Physics/NanoAODDQM");
206 
207  for (auto &pair : groups_) {
208  booker.setCurrentFolder("Physics/NanoAODDQM/" + pair.first);
209  for (auto &sels : pair.second.selGroups) {
210  std::string dir("Physics/NanoAODDQM/" + pair.first);
211  if (!sels.nullCut())
212  dir += "/" + sels.name;
213  booker.setCurrentFolder(dir);
214  auto &plots = sels.plots;
215  plots.clear();
216  plots.reserve(pair.second.plotPSets.size());
217  for (const auto &cfg : pair.second.plotPSets) {
218  auto plot = makePlot(booker, cfg);
219  if (plot)
220  plots.push_back(std::move(plot));
221  }
222  }
223  }
224 }
225 
227  std::vector<edm::Handle<FlatTable>> alltables;
228  iEvent.getManyByType(alltables);
229  std::map<std::string, std::pair<const FlatTable *, std::vector<const FlatTable *>>> maintables;
230 
231  for (const auto &htab : alltables) {
232  if (htab->extension())
233  continue;
234  maintables[htab->name()] = std::make_pair(htab.product(), std::vector<const FlatTable *>());
235  }
236  for (const auto &htab : alltables) {
237  if (htab->extension()) {
238  if (maintables.find(htab->name()) == maintables.end())
239  throw cms::Exception("LogicError", "Missing main table for " + htab->name());
240  maintables[htab->name()].second.push_back(htab.product());
241  }
242  }
243 
244  FlatTable merged;
245  for (auto &pair : groups_) {
246  const std::string &name = pair.first;
247  if (maintables.find(name) == maintables.end())
248  continue; // may happen for missing collections
249  auto &tables = maintables[name];
250  const FlatTable *table = tables.first;
251  if (!tables.second.empty()) {
252  merged = *tables.first;
253  for (auto *other : tables.second) {
254  merged.addExtension(*other);
255  }
256  table = &merged;
257  }
258  std::vector<bool> selbits;
259  for (auto &sel : pair.second.selGroups) {
260  sel.fillSel(*table, selbits);
261 
262  for (auto &plot : sel.plots) {
263  plot->fill(*table, selbits);
264  }
265  }
266  }
267 }
268 
NanoAODDQM::Plot1D::Plot1D
Plot1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:74
dqm::impl::MonitorElement
Definition: MonitorElement.h:98
nanoaod::FlatTable::ColumnType::Float
nanoDQM_cff.vplots
vplots
Definition: nanoDQM_cff.py:22
mps_fire.i
i
Definition: mps_fire.py:428
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:159
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:105
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:182
NanoAODDQM::SelGroupConfig::cutptr
std::unique_ptr< StringCutObjectSelector< FlatTable::RowView > > cutptr
Definition: NanoAODDQM.cc:163
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:143
NanoAODDQM::Profile1D::Profile1D
Profile1D(DQMStore::IBooker &booker, const edm::ParameterSet &cfg)
Definition: NanoAODDQM.cc:117
MakerMacros.h
dqmdumpme.last
last
Definition: dqmdumpme.py:56
NanoAODDQM::SelGroupConfig::cutstr
std::string cutstr
Definition: NanoAODDQM.cc:162
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:184
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:1467
nanoaod::FlatTable::ColumnType::UInt8
NanoAODDQM::bookHistograms
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: NanoAODDQM.cc:204
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:47
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:226
NanoAODDQM::Plot1D::vfill
void vfill(const FlatTable &table, int icol, const std::vector< bool > &rowsel)
Definition: NanoAODDQM.cc:107
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:129
iEvent
int iEvent
Definition: GenABIO.cc:224
NanoAODDQM::GroupConfig::plotPSets
std::vector< edm::ParameterSet > plotPSets
Definition: NanoAODDQM.cc:183
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:166
NanoAODDQM::SelGroupConfig::plots
std::vector< std::unique_ptr< Plot > > plots
Definition: NanoAODDQM.cc:164
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
nanoaod::FlatTable::ColumnType::Int
nanoaod::FlatTable
Definition: FlatTable.h:38
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:167
jets_cff.expr
expr
Definition: jets_cff.py:485
Frameworkfwd.h
T
long double T
Definition: Basic3DVectorLD.h:48
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:146
NanoAODDQM::SelGroupConfig::name
std::string name
Definition: NanoAODDQM.cc:161
NanoAODDQM::SelGroupConfig::Selector
StringCutObjectSelector< FlatTable::RowView > Selector
Definition: NanoAODDQM.cc:160
NanoAODDQM::groups_
std::map< std::string, GroupConfig > groups_
Definition: NanoAODDQM.cc:186
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:189
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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:143
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:61
TableParser.table
table
Definition: TableParser.py:111
nanoDQM_cfi.sels
sels
Definition: nanoDQM_cfi.py:9
edm::Event
Definition: Event.h:73
nanoaod::FlatTable::ColumnType::Bool
NanoAODDQM
Definition: NanoAODDQM.cc:36
NanoAODDQM::SelGroupConfig::SelGroupConfig
SelGroupConfig()
Definition: NanoAODDQM.cc:165
NanoAODDQM::SelGroupConfig::fillSel
void fillSel(const FlatTable &table, std::vector< bool > &out)
Definition: NanoAODDQM.cc:168
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:128
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:115
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:318