Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:18

0001 /*!
0002   \file MonitorElementsDb.cc
0003   \brief Generate a Monitor Element from DB data
0004   \author B. Gobbo
0005 */
0006 
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 
0009 #include <cmath>
0010 #include <fstream>
0011 #include <iostream>
0012 
0013 #include "DQMServices/Core/interface/DQMStore.h"
0014 
0015 #include "RelationalAccess/ICursor.h"
0016 #include "RelationalAccess/IQuery.h"
0017 #include "RelationalAccess/ISchema.h"
0018 #include "RelationalAccess/ITransaction.h"
0019 
0020 #include "CoralBase/Attribute.h"
0021 #include "CoralBase/AttributeList.h"
0022 
0023 #include "TCanvas.h"
0024 #include "TStyle.h"
0025 
0026 #include "TH1.h"
0027 #include "TH2.h"
0028 #include "TProfile.h"
0029 
0030 #include "DQM/EcalMonitorDbModule/interface/MonitorElementsDb.h"
0031 
0032 MonitorElementsDb::MonitorElementsDb(const edm::ParameterSet &ps, std::string &xmlFile) {
0033   xmlFile_ = xmlFile;
0034 
0035   dqmStore_ = edm::Service<DQMStore>().operator->();
0036 
0037   prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
0038 
0039   if (dqmStore_) {
0040     dqmStore_->setCurrentFolder(prefixME_);
0041 
0042     parser_ = new MonitorXMLParser(xmlFile_);
0043     try {
0044       parser_->load();
0045     } catch (std::runtime_error const &e) {
0046       delete parser_;
0047       parser_ = nullptr;
0048       std::cerr << "Error loading parser: " << e.what() << std::endl;
0049     }
0050 
0051     if (parser_)
0052       MEinfo_ = parser_->getDB_ME();
0053 
0054     for (unsigned int i = 0; i < MEinfo_.size(); i++) {
0055       MonitorElement *tmp;
0056       tmp = nullptr;
0057       if (strcmp(MEinfo_[i].type.c_str(), "th1d") == 0) {
0058         tmp = dqmStore_->book1D(MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto);
0059       } else if (strcmp(MEinfo_[i].type.c_str(), "th2d") == 0) {
0060         tmp = dqmStore_->book2D(MEinfo_[i].title,
0061                                 MEinfo_[i].title,
0062                                 MEinfo_[i].xbins,
0063                                 MEinfo_[i].xfrom,
0064                                 MEinfo_[i].xto,
0065                                 MEinfo_[i].ybins,
0066                                 MEinfo_[i].yfrom,
0067                                 MEinfo_[i].yto);
0068       } else if (strcmp(MEinfo_[i].type.c_str(), "tprofile") == 0) {
0069         tmp = dqmStore_->bookProfile(MEinfo_[i].title,
0070                                      MEinfo_[i].title,
0071                                      MEinfo_[i].xbins,
0072                                      MEinfo_[i].xfrom,
0073                                      MEinfo_[i].xto,
0074                                      MEinfo_[i].ybins,
0075                                      MEinfo_[i].yfrom,
0076                                      MEinfo_[i].yto);
0077       } else if (strcmp(MEinfo_[i].type.c_str(), "tprofile2d") == 0) {
0078         tmp = dqmStore_->bookProfile2D(MEinfo_[i].title,
0079                                        MEinfo_[i].title,
0080                                        MEinfo_[i].xbins,
0081                                        MEinfo_[i].xfrom,
0082                                        MEinfo_[i].xto,
0083                                        MEinfo_[i].ybins,
0084                                        MEinfo_[i].yfrom,
0085                                        MEinfo_[i].yto,
0086                                        MEinfo_[i].zbins,
0087                                        MEinfo_[i].zfrom,
0088                                        MEinfo_[i].zto);
0089       }
0090 
0091       MEs_.push_back(tmp);
0092     }
0093   }
0094 
0095   ievt_ = 0;
0096 }
0097 
0098 MonitorElementsDb::~MonitorElementsDb() { delete parser_; }
0099 
0100 void MonitorElementsDb::beginJob(void) { ievt_ = 0; }
0101 
0102 void MonitorElementsDb::endJob(void) { std::cout << "MonitorElementsDb: analyzed " << ievt_ << " events" << std::endl; }
0103 
0104 void MonitorElementsDb::analyze(const edm::Event &e, const edm::EventSetup &c, coral::ISessionProxy *session) {
0105   ievt_++;
0106 
0107   bool atLeastAQuery;
0108   atLeastAQuery = false;
0109 
0110   std::vector<std::string> vars;
0111 
0112   if (session) {
0113     for (unsigned int i = 0; i < MEinfo_.size(); i++) {
0114       // i-th ME...
0115 
0116       if (MEs_[i] != nullptr && (ievt_ % MEinfo_[i].ncycle) == 0) {
0117         MEs_[i]->Reset();
0118 
0119         vars.clear();
0120 
0121         try {
0122           atLeastAQuery = true;
0123 
0124           session->transaction().start(true);
0125 
0126           coral::ISchema &schema = session->nominalSchema();
0127 
0128           coral::IQuery *query = schema.newQuery();
0129 
0130           for (unsigned int j = 0; j < MEinfo_[i].queries.size(); j++) {
0131             if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToTableList") == 0) {
0132               query->addToTableList(MEinfo_[i].queries[j].arg);
0133             } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOutputList") == 0) {
0134               query->addToOutputList(MEinfo_[i].queries[j].arg, MEinfo_[i].queries[j].alias);
0135               vars.push_back(MEinfo_[i].queries[j].alias);
0136             } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "setCondition") == 0) {
0137               query->setCondition(MEinfo_[i].queries[j].arg, coral::AttributeList());
0138             } else if (strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOrderList") == 0) {
0139               query->addToOrderList(MEinfo_[i].queries[j].arg);
0140             }
0141           }
0142 
0143           coral::ICursor &cursor = query->execute();
0144 
0145           unsigned int k = 0;
0146 
0147           while (cursor.next() && k < MEinfo_[i].loop) {
0148             // while ( cursor.next() ) {
0149 
0150             const coral::AttributeList &row = cursor.currentRow();
0151 
0152             std::vector<float> vvars;
0153             vvars.clear();
0154             for (unsigned int l = 0; l < vars.size(); l++) {
0155               if (!vars[l].empty()) {
0156                 vvars.push_back(row[vars[l]].data<float>());
0157               }
0158             }
0159             if (vvars.size() == 2) {
0160               // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] <<
0161               // std::endl;
0162               MEs_[i]->Fill(vvars[0], vvars[1]);
0163             } else if (vvars.size() == 3) {
0164               // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << "
0165               // -- " << vvars[2] << std::endl;
0166               MEs_[i]->Fill(vvars[0], vvars[1], vvars[2]);
0167             } else if (vvars.size() == 4) {
0168               // std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << "
0169               // -- " << vvars[2] << " -- " << vvars[3] << std::endl;
0170               MEs_[i]->Fill(vvars[0], vvars[1], vvars[2], vvars[3]);
0171             } else {
0172               std::cerr << "Too many variables to plot..." << std::endl;
0173               exit(1);
0174             }
0175 
0176             k++;
0177           }
0178 
0179           delete query;
0180 
0181         } catch (coral::Exception &e) {
0182           std::cerr << "CORAL Exception : " << e.what() << std::endl;
0183         } catch (std::exception &e) {
0184           std::cerr << "Standard C++ exception : " << e.what() << std::endl;
0185         }
0186       }
0187     }
0188 
0189     if (atLeastAQuery)
0190       session->transaction().commit();
0191   }
0192 }
0193 
0194 void MonitorElementsDb::htmlOutput(std::string &htmlDir) {
0195   gStyle->SetOptStat(0);
0196   gStyle->SetOptFit();
0197   gStyle->SetPalette(1, nullptr);
0198 
0199   for (unsigned int i = 0; i < MEinfo_.size(); i++) {
0200     if (MEs_[i] != nullptr && (ievt_ % MEinfo_[i].ncycle) == 0) {
0201       TCanvas *c1;
0202       int n = MEinfo_[i].xbins > MEinfo_[i].ybins ? int(round(float(MEinfo_[i].xbins) / float(MEinfo_[i].ybins)))
0203                                                   : int(round(float(MEinfo_[i].ybins) / float(MEinfo_[i].xbins)));
0204       if (MEinfo_[i].xbins > MEinfo_[i].ybins) {
0205         c1 = new TCanvas("c1", "dummy", 400 * n, 400);
0206       } else {
0207         c1 = new TCanvas("c1", "dummy", 400, 400 * n);
0208       }
0209       c1->SetGrid();
0210       c1->cd();
0211 
0212       const double histMax = 1.e15;
0213 
0214       TObject *ob = const_cast<MonitorElement *>(MEs_[i])->getRootObject();
0215       if (ob) {
0216         if (dynamic_cast<TH1F *>(ob)) {
0217           TH1F *h = dynamic_cast<TH1F *>(ob);
0218           h->Draw();
0219         } else if (dynamic_cast<TH2F *>(ob)) {
0220           TH2F *h = dynamic_cast<TH2F *>(ob);
0221           if (h->GetMaximum(histMax) > 1.e4) {
0222             gPad->SetLogz(1);
0223           } else {
0224             gPad->SetLogz(0);
0225           }
0226           h->Draw("colz");
0227         } else if (dynamic_cast<TProfile *>(ob)) {
0228           TProfile *h = dynamic_cast<TProfile *>(ob);
0229           if (h->GetMaximum(histMax) > 1.e4) {
0230             gPad->SetLogz(1);
0231           } else {
0232             gPad->SetLogz(0);
0233           }
0234           h->Draw("colz");
0235         }
0236       }
0237 
0238       c1->Update();
0239       std::string name = htmlDir + "/" + MEinfo_[i].title + ".png";
0240       c1->SaveAs(name.c_str());
0241 
0242       delete c1;
0243     }
0244   }
0245 }