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