CMS 3D CMS Logo

MonitorElementsDb.cc
Go to the documentation of this file.
1 
8 
9 #include <cmath>
10 #include <fstream>
11 #include <iostream>
12 
14 
16 
17 #include "RelationalAccess/ICursor.h"
18 #include "RelationalAccess/IQuery.h"
19 #include "RelationalAccess/ISchema.h"
20 #include "RelationalAccess/ITransaction.h"
21 
22 #include "CoralBase/Attribute.h"
23 #include "CoralBase/AttributeList.h"
24 
25 #include "TCanvas.h"
26 #include "TStyle.h"
27 
28 #include "TH1.h"
29 #include "TH2.h"
30 #include "TProfile.h"
31 
33 
35  xmlFile_ = xmlFile;
36 
38 
39  prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
40 
41  if (dqmStore_) {
43 
45  try {
46  parser_->load();
47  } catch (std::runtime_error const &e) {
48  delete parser_;
49  parser_ = nullptr;
50  std::cerr << "Error loading parser: " << e.what() << std::endl;
51  }
52 
53  if (parser_)
55 
56  for (unsigned int i = 0; i < MEinfo_.size(); i++) {
58  tmp = nullptr;
59  if (strcmp(MEinfo_[i].type.c_str(), "th1d") == 0) {
60  tmp = dqmStore_->book1D(MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto);
61  } else if (strcmp(MEinfo_[i].type.c_str(), "th2d") == 0) {
62  tmp = dqmStore_->book2D(MEinfo_[i].title,
63  MEinfo_[i].title,
64  MEinfo_[i].xbins,
65  MEinfo_[i].xfrom,
66  MEinfo_[i].xto,
67  MEinfo_[i].ybins,
68  MEinfo_[i].yfrom,
69  MEinfo_[i].yto);
70  } else if (strcmp(MEinfo_[i].type.c_str(), "tprofile") == 0) {
72  MEinfo_[i].title,
73  MEinfo_[i].xbins,
74  MEinfo_[i].xfrom,
75  MEinfo_[i].xto,
76  MEinfo_[i].ybins,
77  MEinfo_[i].yfrom,
78  MEinfo_[i].yto);
79  } else if (strcmp(MEinfo_[i].type.c_str(), "tprofile2d") == 0) {
81  MEinfo_[i].title,
82  MEinfo_[i].xbins,
83  MEinfo_[i].xfrom,
84  MEinfo_[i].xto,
85  MEinfo_[i].ybins,
86  MEinfo_[i].yfrom,
87  MEinfo_[i].yto,
88  MEinfo_[i].zbins,
89  MEinfo_[i].zfrom,
90  MEinfo_[i].zto);
91  }
92 
93  MEs_.push_back(tmp);
94  }
95  }
96 
97  ievt_ = 0;
98 }
99 
101 
103 
105  std::cout << "MonitorElementsDb: analyzed " << ievt_ << " events" << std::endl;
106  for (unsigned int i = 0; i < MEs_.size(); i++) {
107  if (MEs_[i] != nullptr)
109  }
110 }
111 
112 void MonitorElementsDb::analyze(const edm::Event &e, const edm::EventSetup &c, coral::ISessionProxy *session) {
113  ievt_++;
114 
115  bool atLeastAQuery;
116  atLeastAQuery = false;
117 
118  std::vector<std::string> vars;
119 
120  if (session) {
121  for (unsigned int i = 0; i < MEinfo_.size(); i++) {
122  // i-th ME...
123 
124  if (MEs_[i] != nullptr && (ievt_ % MEinfo_[i].ncycle) == 0) {
125  MEs_[i]->Reset();
126 
127  vars.clear();
128 
129  try {
130  atLeastAQuery = true;
131 
132  session->transaction().start(true);
133 
134  coral::ISchema &schema = session->nominalSchema();
135 
136  coral::IQuery *query = schema.newQuery();
137 
138  for (unsigned int j = 0; j < MEinfo_[i].queries.size(); j++) {
139  if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToTableList") == 0) {
140  query->addToTableList(MEinfo_[i].queries[j].arg);
141  } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOutputList") == 0) {
142  query->addToOutputList(MEinfo_[i].queries[j].arg, MEinfo_[i].queries[j].alias);
143  vars.push_back(MEinfo_[i].queries[j].alias);
144  } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "setCondition") == 0) {
145  query->setCondition(MEinfo_[i].queries[j].arg, coral::AttributeList());
146  } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOrderList") == 0) {
147  query->addToOrderList(MEinfo_[i].queries[j].arg);
148  }
149  }
150 
151  coral::ICursor &cursor = query->execute();
152 
153  unsigned int k = 0;
154 
155  while (cursor.next() && k < MEinfo_[i].loop) {
156  // while ( cursor.next() ) {
157 
158  const coral::AttributeList &row = cursor.currentRow();
159 
160  std::vector<float> vvars;
161  vvars.clear();
162  for (unsigned int l = 0; l < vars.size(); l++) {
163  if (!vars[l].empty()) {
164  vvars.push_back(row[vars[l]].data<float>());
165  }
166  }
167  if (vvars.size() == 2) {
168  // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] <<
169  // std::endl;
170  MEs_[i]->Fill(vvars[0], vvars[1]);
171  } else if (vvars.size() == 3) {
172  // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << "
173  // -- " << vvars[2] << std::endl;
174  MEs_[i]->Fill(vvars[0], vvars[1], vvars[2]);
175  } else if (vvars.size() == 4) {
176  // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << "
177  // -- " << vvars[2] << " -- " << vvars[3] << std::endl;
178  MEs_[i]->Fill(vvars[0], vvars[1], vvars[2], vvars[3]);
179  } else {
180  std::cerr << "Too many variables to plot..." << std::endl;
181  exit(1);
182  }
183 
184  k++;
185  }
186 
187  delete query;
188 
189  } catch (coral::Exception &e) {
190  std::cerr << "CORAL Exception : " << e.what() << std::endl;
191  } catch (std::exception &e) {
192  std::cerr << "Standard C++ exception : " << e.what() << std::endl;
193  }
194  }
195  }
196 
197  if (atLeastAQuery)
198  session->transaction().commit();
199  }
200 }
201 
203  gStyle->SetOptStat(0);
204  gStyle->SetOptFit();
205  gStyle->SetPalette(1, nullptr);
206 
207  for (unsigned int i = 0; i < MEinfo_.size(); i++) {
208  if (MEs_[i] != nullptr && (ievt_ % MEinfo_[i].ncycle) == 0) {
209  TCanvas *c1;
210  int n = MEinfo_[i].xbins > MEinfo_[i].ybins ? int(round(float(MEinfo_[i].xbins) / float(MEinfo_[i].ybins)))
211  : int(round(float(MEinfo_[i].ybins) / float(MEinfo_[i].xbins)));
212  if (MEinfo_[i].xbins > MEinfo_[i].ybins) {
213  c1 = new TCanvas("c1", "dummy", 400 * n, 400);
214  } else {
215  c1 = new TCanvas("c1", "dummy", 400, 400 * n);
216  }
217  c1->SetGrid();
218  c1->cd();
219 
220  const double histMax = 1.e15;
221 
222  TObject *ob = const_cast<MonitorElement *>(MEs_[i])->getRootObject();
223  if (ob) {
224  if (dynamic_cast<TH1F *>(ob)) {
225  TH1F *h = dynamic_cast<TH1F *>(ob);
226  h->Draw();
227  } else if (dynamic_cast<TH2F *>(ob)) {
228  TH2F *h = dynamic_cast<TH2F *>(ob);
229  if (h->GetMaximum(histMax) > 1.e4) {
230  gPad->SetLogz(1);
231  } else {
232  gPad->SetLogz(0);
233  }
234  h->Draw("colz");
235  } else if (dynamic_cast<TProfile *>(ob)) {
236  TProfile *h = dynamic_cast<TProfile *>(ob);
237  if (h->GetMaximum(histMax) > 1.e4) {
238  gPad->SetLogz(1);
239  } else {
240  gPad->SetLogz(0);
241  }
242  h->Draw("colz");
243  }
244  }
245 
246  c1->Update();
247  std::string name = htmlDir + "/" + MEinfo_[i].title + ".png";
248  c1->SaveAs(name.c_str());
249 
250  delete c1;
251  }
252  }
253 }
MonitorElement * book2D(char_string const &name, char_string const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Book 2D histogram.
Definition: DQMStore.cc:1173
MonitorElement * bookProfile2D(char_string const &name, char_string const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, double lowZ, double highZ, char const *option="s")
Definition: DQMStore.cc:1379
type
Definition: HCALResponse.h:21
T getUntrackedParameter(std::string const &, T const &) const
MonitorElementsDb(const edm::ParameterSet &ps, std::string &xmlFile)
Constructors.
const double xbins[]
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void removeElement(std::string const &name)
Definition: DQMStore.cc:3126
const std::vector< DB_ME > & getDB_ME(void) const
MonitorElement * book1D(char_string const &name, char_string const &title, int const nchX, double const lowX, double const highX)
Book 1D histogram.
Definition: DQMStore.cc:1098
Generate a Monitor Element from DB data.
A arg
Definition: Factorize.h:38
def query(query_str, verbose=False)
Definition: das.py:6
void analyze(const edm::Event &e, const edm::EventSetup &c, coral::ISessionProxy *s)
Analyze.
Definition: query.py:1
void load() noexcept(false)
std::vector< MonitorElement * > MEs_
std::vector< DB_ME > MEinfo_
int k[5][pyjets_maxn]
TString getName(TString structure, int layer, TString geometry)
Definition: DMRtrends.cc:167
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:571
MonitorElement * bookProfile(char_string const &name, char_string const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, char const *option="s")
Definition: DQMStore.cc:1285
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
vars
Definition: DeepTauId.cc:77
virtual ~MonitorElementsDb()
Destructor.
void htmlOutput(std::string &htmlDir)
MonitorXMLParser * parser_