Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <memory>
0002 
0003 #include "DQM/EcalMonitorDbModule/interface/EcalCondDBWriter.h"
0004 
0005 #include "DQMServices/Core/interface/DQMStore.h"
0006 
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 
0013 #include "TObjArray.h"
0014 #include "TPRegexp.h"
0015 #include "TString.h"
0016 
0017 void setBit(int &_bitArray, unsigned _iBit) { _bitArray |= (0x1 << _iBit); }
0018 
0019 bool getBit(int &_bitArray, unsigned _iBit) { return (_bitArray & (0x1 << _iBit)) != 0; }
0020 
0021 EcalCondDBWriter::EcalCondDBWriter(edm::ParameterSet const &_ps)
0022     : runNumber_(0),
0023       db_(nullptr),
0024       location_(_ps.getUntrackedParameter<std::string>("location")),
0025       runType_(_ps.getUntrackedParameter<std::string>("runType")),
0026       runGeneralTag_(_ps.getUntrackedParameter<std::string>("runGeneralTag")),
0027       monRunGeneralTag_(_ps.getUntrackedParameter<std::string>("monRunGeneralTag")),
0028       summaryWriter_(_ps.getUntrackedParameterSet("workerParams")),
0029       verbosity_(_ps.getUntrackedParameter<int>("verbosity")),
0030       executed_(false) {
0031   std::vector<std::string> inputRootFiles(_ps.getUntrackedParameter<std::vector<std::string>>("inputRootFiles"));
0032 
0033   if (inputRootFiles.empty())
0034     throw cms::Exception("Configuration") << "No input ROOT file given";
0035 
0036   if (verbosity_ > 0)
0037     edm::LogInfo("EcalDQM") << "Initializing DQMStore from input ROOT files";
0038 
0039   DQMStore &dqmStore(*edm::Service<DQMStore>());
0040 
0041   for (unsigned iF(0); iF < inputRootFiles.size(); ++iF) {
0042     std::string &fileName(inputRootFiles[iF]);
0043 
0044     if (verbosity_ > 1)
0045       edm::LogInfo("EcalDQM") << " " << fileName;
0046 
0047     TPRegexp pat("DQM_V[0-9]+(?:|_[0-9a-zA-Z]+)_R([0-9]+)");
0048     std::unique_ptr<TObjArray> matches(pat.MatchS(fileName.c_str()));
0049     if (matches->GetEntries() == 0)
0050       throw cms::Exception("Configuration") << "Input file " << fileName << " is not an DQM output";
0051 
0052     if (iF == 0)
0053       runNumber_ = TString(matches->At(1)->GetName()).Atoi();
0054     else if (TString(matches->At(1)->GetName()).Atoi() != runNumber_)
0055       throw cms::Exception("Configuration") << "Input files disagree in run number";
0056 
0057     dqmStore.open(fileName, false, "", "", DQMStore::StripRunDirs);
0058   }
0059 
0060   std::string DBName(_ps.getUntrackedParameter<std::string>("DBName"));
0061   std::string hostName(_ps.getUntrackedParameter<std::string>("hostName"));
0062   int hostPort(_ps.getUntrackedParameter<int>("hostPort"));
0063   std::string userName(_ps.getUntrackedParameter<std::string>("userName"));
0064   std::string password(_ps.getUntrackedParameter<std::string>("password"));
0065 
0066   std::unique_ptr<EcalCondDBInterface> db(nullptr);
0067 
0068   if (verbosity_ > 0)
0069     edm::LogInfo("EcalDQM") << "Establishing DB connection";
0070 
0071   try {
0072     db = std::make_unique<EcalCondDBInterface>(DBName, userName, password);
0073   } catch (std::runtime_error &re) {
0074     if (!hostName.empty()) {
0075       try {
0076         db = std::make_unique<EcalCondDBInterface>(hostName, DBName, userName, password, hostPort);
0077       } catch (std::runtime_error &re2) {
0078         throw cms::Exception("DBError") << re2.what();
0079       }
0080     } else
0081       throw cms::Exception("DBError") << re.what();
0082   }
0083 
0084   db_ = db.release();
0085 
0086   if (verbosity_ > 0)
0087     edm::LogInfo("EcalDQM") << " Done.";
0088 
0089   edm::ParameterSet const &workerParams(_ps.getUntrackedParameterSet("workerParams"));
0090 
0091   workers_[Integrity] = new ecaldqm::IntegrityWriter(workerParams);
0092   workers_[Cosmic] = nullptr;
0093   workers_[Laser] = new ecaldqm::LaserWriter(workerParams);
0094   workers_[Pedestal] = new ecaldqm::PedestalWriter(workerParams);
0095   workers_[Presample] = new ecaldqm::PresampleWriter(workerParams);
0096   workers_[TestPulse] = new ecaldqm::TestPulseWriter(workerParams);
0097   workers_[BeamCalo] = nullptr;
0098   workers_[BeamHodo] = nullptr;
0099   workers_[TriggerPrimitives] = nullptr;
0100   workers_[Cluster] = nullptr;
0101   workers_[Timing] = new ecaldqm::TimingWriter(workerParams);
0102   workers_[Led] = new ecaldqm::LedWriter(workerParams);
0103   workers_[RawData] = nullptr;
0104   workers_[Occupancy] = new ecaldqm::OccupancyWriter(workerParams);
0105 
0106   for (unsigned iC(0); iC < nTasks; ++iC)
0107     if (workers_[iC])
0108       workers_[iC]->setVerbosity(verbosity_);
0109 }
0110 
0111 EcalCondDBWriter::~EcalCondDBWriter() {
0112   try {
0113     delete db_;
0114   } catch (std::runtime_error &e) {
0115     throw cms::Exception("DBError") << e.what();
0116   }
0117 
0118   for (unsigned iC(0); iC < nTasks; ++iC)
0119     delete workers_[iC];
0120 }
0121 
0122 void EcalCondDBWriter::beginRun(edm::Run const &_run, edm::EventSetup const &_es) {
0123   for (unsigned iC(0); iC < nTasks; ++iC)
0124     if (workers_[iC])
0125       workers_[iC]->setSetupObjects(_es);
0126 }
0127 
0128 void EcalCondDBWriter::dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &_igetter) {
0129   if (executed_)
0130     return;
0131 
0132   /////////////////////// INPUT INITIALIZATION /////////////////////////
0133 
0134   if (verbosity_ > 1)
0135     edm::LogInfo("EcalDQM") << " Searching event info";
0136 
0137   uint64_t timeStampInFile(0);
0138   unsigned processedEvents(0);
0139 
0140   _igetter.cd();
0141   std::vector<std::string> dirs(_igetter.getSubdirs());
0142   for (unsigned iD(0); iD < dirs.size(); ++iD) {
0143     if (!_igetter.dirExists(dirs[iD] + "/EventInfo"))
0144       continue;
0145 
0146     MonitorElement *timeStampME(_igetter.get(dirs[iD] + "/EventInfo/runStartTimeStamp"));
0147     if (timeStampME) {
0148       double timeStampValue(timeStampME->getFloatValue());
0149       uint64_t seconds(timeStampValue);
0150       uint64_t microseconds((timeStampValue - seconds) * 1.e6);
0151       timeStampInFile = (seconds << 32) | microseconds;
0152     }
0153 
0154     MonitorElement *eventsME(_igetter.get(dirs[iD] + "/EventInfo/processedEvents"));
0155     if (eventsME)
0156       processedEvents = eventsME->getIntValue();
0157 
0158     if (timeStampInFile != 0 && processedEvents != 0) {
0159       if (verbosity_ > 1)
0160         edm::LogInfo("EcalDQM") << " Event info found; timestamp=" << timeStampInFile
0161                                 << " processedEvents=" << processedEvents;
0162       break;
0163     }
0164   }
0165 
0166   if (verbosity_ > 0)
0167     edm::LogInfo("EcalDQM") << " Done.";
0168 
0169   //////////////////////// SOURCE INITIALIZATION //////////////////////////
0170 
0171   if (verbosity_ > 0)
0172     edm::LogInfo("EcalDQM") << "Setting up source MonitorElements for given run type " << runType_;
0173 
0174   int taskList(0);
0175   for (unsigned iC(0); iC < nTasks; ++iC) {
0176     if (!workers_[iC] || !workers_[iC]->runsOn(runType_))
0177       continue;
0178 
0179     workers_[iC]->retrieveSource(_igetter);
0180 
0181     setBit(taskList, iC);
0182   }
0183 
0184   if (verbosity_ > 0)
0185     edm::LogInfo("EcalDQM") << " Done.";
0186 
0187   //////////////////////// DB INITIALIZATION //////////////////////////
0188 
0189   if (verbosity_ > 0)
0190     edm::LogInfo("EcalDQM") << "Initializing DB entry";
0191 
0192   RunIOV runIOV;
0193   RunTag runTag;
0194   try {
0195     runIOV = db_->fetchRunIOV(location_, runNumber_);
0196     runTag = runIOV.getRunTag();
0197   } catch (std::runtime_error &e) {
0198     std::cerr << e.what();
0199 
0200     if (timeStampInFile == 0)
0201       throw cms::Exception("Initialization") << "Time stamp for the run could not be found";
0202 
0203     LocationDef locationDef;
0204     locationDef.setLocation(location_);
0205     RunTypeDef runTypeDef;
0206     runTypeDef.setRunType(runType_);
0207     runTag.setLocationDef(locationDef);
0208     runTag.setRunTypeDef(runTypeDef);
0209     runTag.setGeneralTag(runGeneralTag_);
0210 
0211     runIOV.setRunStart(Tm(timeStampInFile));
0212     runIOV.setRunNumber(runNumber_);
0213     runIOV.setRunTag(runTag);
0214 
0215     try {
0216       db_->insertRunIOV(&runIOV);
0217       runIOV = db_->fetchRunIOV(&runTag, runNumber_);
0218     } catch (std::runtime_error &e) {
0219       throw cms::Exception("DBError") << e.what();
0220     }
0221   }
0222 
0223   // No filtering - DAQ definitions change time to time..
0224   //   if(runType_ != runIOV.getRunTag().getRunTypeDef().getRunType())
0225   //     throw cms::Exception("Configuration") << "Given run type " << runType_
0226   //     << " does not match the run type in DB " <<
0227   //     runIOV.getRunTag().getRunTypeDef().getRunType();
0228 
0229   MonVersionDef versionDef;
0230   versionDef.setMonitoringVersion("test01");  // the only mon_ver in mon_version_def table as of September
0231                                               // 2012
0232   MonRunTag monTag;
0233   monTag.setMonVersionDef(versionDef);
0234   monTag.setGeneralTag(monRunGeneralTag_);
0235 
0236   MonRunIOV monIOV;
0237 
0238   try {
0239     monIOV = db_->fetchMonRunIOV(&runTag, &monTag, runNumber_, 1);
0240   } catch (std::runtime_error &e) {
0241     std::cerr << e.what();
0242 
0243     monIOV.setRunIOV(runIOV);
0244     monIOV.setSubRunNumber(1);
0245     monIOV.setSubRunStart(runIOV.getRunStart());
0246     monIOV.setSubRunEnd(runIOV.getRunEnd());
0247     monIOV.setMonRunTag(monTag);
0248 
0249     try {
0250       db_->insertMonRunIOV(&monIOV);
0251       monIOV = db_->fetchMonRunIOV(&runTag, &monTag, runNumber_, 1);
0252     } catch (std::runtime_error &e) {
0253       throw cms::Exception("DBError") << e.what();
0254     }
0255   }
0256 
0257   if (verbosity_ > 0)
0258     edm::LogInfo("EcalDQM") << " Done.";
0259 
0260   //////////////////////// DB WRITING //////////////////////////
0261 
0262   if (verbosity_ > 0)
0263     edm::LogInfo("EcalDQM") << "Writing to DB";
0264 
0265   int outcome(0);
0266   for (unsigned iC(0); iC < nTasks; ++iC) {
0267     if (!getBit(taskList, iC))
0268       continue;
0269 
0270     if (verbosity_ > 1)
0271       edm::LogInfo("EcalDQM") << " " << workers_[iC]->getName();
0272 
0273     if (workers_[iC]->isActive() && workers_[iC]->run(db_, monIOV))
0274       setBit(outcome, iC);
0275   }
0276 
0277   if (verbosity_ > 0)
0278     edm::LogInfo("EcalDQM") << " Done.";
0279 
0280   if (verbosity_ > 0)
0281     edm::LogInfo("EcalDQM") << "Registering the outcome of DB writing";
0282 
0283   summaryWriter_.setTaskList(taskList);
0284   summaryWriter_.setOutcome(outcome);
0285   summaryWriter_.setProcessedEvents(processedEvents);
0286   summaryWriter_.run(db_, monIOV);
0287 
0288   if (verbosity_ > 0)
0289     edm::LogInfo("EcalDQM") << " Done.";
0290 
0291   executed_ = true;
0292 }
0293 
0294 DEFINE_FWK_MODULE(EcalCondDBWriter);