CMS 3D CMS Logo

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