CMS 3D CMS Logo

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