00001 #include "DQMServices/Components/src/DQMFileSaver.h"
00002 #include "DQMServices/Core/interface/DQMStore.h"
00003 #include "DQMServices/Core/interface/MonitorElement.h"
00004 #include "FWCore/Framework/interface/Event.h"
00005 #include "FWCore/Framework/interface/Run.h"
00006 #include "FWCore/Framework/interface/LuminosityBlock.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/Version/interface/GetReleaseVersion.h"
00009 #include "FWCore/ServiceRegistry/interface/Service.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/MessageLogger/interface/JobReport.h"
00012 #include <sys/stat.h>
00013 #include <unistd.h>
00014 #include <iostream>
00015 #include <vector>
00016 #include <string>
00017 #include <TString.h>
00018 #include <TSystem.h>
00019
00020
00021 static void
00022 getAnInt(const edm::ParameterSet &ps, int &value, const std::string &name)
00023 {
00024 value = ps.getUntrackedParameter<int>(name, value);
00025 if (value < 1 && value != -1)
00026 throw cms::Exception("DQMFileSaver")
00027 << "Invalid '" << name << "' parameter '" << value
00028 << "'. Must be -1 or >= 1.";
00029 }
00030
00031 void
00032 DQMFileSaver::saveForOffline(const std::string &workflow, int run, int lumi)
00033 {
00034
00035 char suffix[64];
00036 sprintf(suffix, "R%09d", run);
00037
00038 char rewrite[128];
00039 if (lumi == 0)
00040 sprintf(rewrite, "\\1Run %d/\\2/Run summary", run);
00041 else
00042 sprintf(rewrite, "\\1Run %d/\\2/By Lumi Section %d-%d", irun_, ilumi_, ilumi_);
00043
00044 size_t pos = 0;
00045 std::string wflow;
00046 wflow.reserve(workflow.size() + 3);
00047 wflow = workflow;
00048 while ((pos = wflow.find('/', pos)) != std::string::npos)
00049 wflow.replace(pos++, 1, "__");
00050
00051 std::string filename = fileBaseName_ + suffix + wflow + ".root";
00052
00053 if (lumi == 0)
00054 {
00055
00056 dbe_->cd();
00057 dbe_->setCurrentFolder("Info/ProvInfo");
00058
00059
00060 MonitorElement* me = dbe_->get("Info/ProvInfo/CMSSW");
00061 if (!me) me = dbe_->bookString("CMSSW",edm::getReleaseVersion().c_str() );
00062
00063 me = dbe_->get("Info/ProvInfo/runIsComplete");
00064 if (!me) me = dbe_->bookFloat("runIsComplete");
00065
00066 if (me)
00067 {
00068 if (runIsComplete_)
00069 me->Fill(1.);
00070 else
00071 me->Fill(0.);
00072 }
00073
00074 dbe_->save(filename,
00075 "",
00076 "^(Reference/)?([^/]+)",
00077 rewrite,
00078 (DQMStore::SaveReferenceTag) saveReference_,
00079 saveReferenceQMin_,
00080 fileUpdate_);
00081 }
00082 else
00083 {
00084 std::vector<std::string> systems = (dbe_->cd(), dbe_->getSubdirs());
00085
00086 std::cout << " DQMFileSaver: storing EventInfo folders for Run: "
00087 << irun_ << ", Lumi Section: " << ilumi_ << ", Subsystems: " ;
00088
00089 for (size_t i = 0, e = systems.size(); i != e; ++i) {
00090 if (systems[i] != "Reference") {
00091 dbe_->cd();
00092 std::cout << systems[i] << " " ;
00093 dbe_->save(filename,
00094 systems[i]+"/EventInfo", "^(Reference/)?([^/]+)", rewrite,
00095 DQMStore::SaveWithoutReference,
00096 dqm::qstatus::STATUS_OK,
00097 fileUpdate_);
00098
00099 if (fileUpdate_=="RECREATE") fileUpdate_="UPDATE";
00100 }
00101 }
00102 std::cout << "\n";
00103 }
00104
00105 if (pastSavedFiles_.size() == 0)
00106 {
00107
00108 saveJobReport(filename);
00109 pastSavedFiles_.push_back(filename);
00110 }
00111
00112 }
00113
00114 static void
00115 doSaveForOnline(std::list<std::string> &pastSavedFiles,
00116 size_t numKeepSavedFiles,
00117 DQMStore *store,
00118 const std::string &filename,
00119 const std::string &directory,
00120 const std::string &rxpat,
00121 const std::string &rewrite,
00122 DQMStore::SaveReferenceTag saveref,
00123 int saveRefQMin)
00124 {
00125 store->save(filename, directory , rxpat,
00126 rewrite, saveref, saveRefQMin);
00127 pastSavedFiles.push_back(filename);
00128 if (pastSavedFiles.size() > numKeepSavedFiles)
00129 {
00130 remove(pastSavedFiles.front().c_str());
00131 pastSavedFiles.pop_front();
00132 }
00133 }
00134
00135 void
00136 DQMFileSaver::saveForOnline(const std::string &suffix, const std::string &rewrite)
00137 {
00138 std::vector<std::string> systems = (dbe_->cd(), dbe_->getSubdirs());
00139
00140 for (size_t i = 0, e = systems.size(); i != e; ++i)
00141 {
00142 if (systems[i] != "Reference")
00143 {
00144 dbe_->cd();
00145 if (MonitorElement* me = dbe_->get(systems[i] + "/EventInfo/processName"))
00146 {
00147 doSaveForOnline(pastSavedFiles_, numKeepSavedFiles_, dbe_,
00148 fileBaseName_ + me->getStringValue() + suffix + ".root",
00149 "", "^(Reference/)?([^/]+)", rewrite,
00150 (DQMStore::SaveReferenceTag) saveReference_,
00151 saveReferenceQMin_);
00152 return;
00153 }
00154 }
00155 }
00156
00157
00158 for (size_t i = 0, e = systems.size(); i != e; ++i)
00159 if (systems[i] != "Reference")
00160 {
00161 dbe_->cd();
00162 std::vector<MonitorElement*> pNamesVector = dbe_->getMatchingContents("^" + systems[i] + "/.*/EventInfo/processName",lat::Regexp::Perl);
00163 if (pNamesVector.size() > 0){
00164 doSaveForOnline(pastSavedFiles_, numKeepSavedFiles_, dbe_,
00165 fileBaseName_ + systems[i] + suffix + ".root",
00166 "", "^(Reference/)?([^/]+)", rewrite,
00167 (DQMStore::SaveReferenceTag) saveReference_,
00168 saveReferenceQMin_);
00169 pNamesVector.clear();
00170 return;
00171 }
00172 }
00173
00174
00175 for (size_t i = 0, e = systems.size(); i != e; ++i)
00176 if (systems[i] != "Reference")
00177 doSaveForOnline(pastSavedFiles_, numKeepSavedFiles_, dbe_,
00178 fileBaseName_ + systems[i] + suffix + ".root",
00179 systems[i], "^(Reference/)?([^/]+)", rewrite,
00180 (DQMStore::SaveReferenceTag) saveReference_,
00181 saveReferenceQMin_);
00182 }
00183
00184 void
00185 DQMFileSaver::saveJobReport(const std::string &filename)
00186 {
00187
00188
00189 edm::Service<edm::JobReport> jr;
00190 if (jr.isAvailable())
00191 {
00192 std::map<std::string, std::string> info;
00193 info["Source"] = "DQMStore";
00194 info["FileClass"] = "DQM";
00195 jr->reportAnalysisFile(filename, info);
00196 }
00197
00198 }
00199
00200
00201 DQMFileSaver::DQMFileSaver(const edm::ParameterSet &ps)
00202 : convention_ (Offline),
00203 workflow_ (""),
00204 producer_ ("DQM"),
00205 dirName_ ("."),
00206 version_ (1),
00207 runIsComplete_ (false),
00208 saveByLumiSection_ (-1),
00209 saveByEvent_ (-1),
00210 saveByMinute_ (-1),
00211 saveByTime_ (-1),
00212 saveByRun_ (1),
00213 saveAtJobEnd_ (false),
00214 saveReference_ (DQMStore::SaveWithReference),
00215 saveReferenceQMin_ (dqm::qstatus::STATUS_OK),
00216 forceRunNumber_ (-1),
00217 fileBaseName_ (""),
00218 fileUpdate_ ("RECREATE"),
00219 dbe_ (&*edm::Service<DQMStore>()),
00220 irun_ (-1),
00221 ilumi_ (-1),
00222 ilumiprev_ (-1),
00223 ievent_ (-1),
00224 nrun_ (0),
00225 nlumi_ (0),
00226 nevent_ (0),
00227 numKeepSavedFiles_ (5)
00228 {
00229
00230 std::string convention = ps.getUntrackedParameter<std::string>("convention", "Offline");
00231 if (convention == "Offline")
00232 convention_ = Offline;
00233 else if (convention == "Online")
00234 convention_ = Online;
00235 else
00236 throw cms::Exception("DQMFileSaver")
00237 << "Invalid 'convention' parameter '" << convention << "'."
00238 << " Expected one of 'Online' or 'Offline'.";
00239
00240
00241 if (convention_ != Online)
00242 {
00243 workflow_ = ps.getUntrackedParameter<std::string>("workflow", workflow_);
00244 if (workflow_.empty()
00245 || workflow_[0] != '/'
00246 || *workflow_.rbegin() == '/'
00247 || std::count(workflow_.begin(), workflow_.end(), '/') != 3
00248 || workflow_.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00249 "abcdefghijklmnopqrstuvwxyz"
00250 "0123456789"
00251 "-_/") != std::string::npos)
00252 throw cms::Exception("DQMFileSaver")
00253 << "Invalid 'workflow' parameter '" << workflow_
00254 << "'. Expected '/A/B/C'.";
00255 }
00256 else if (! ps.getUntrackedParameter<std::string>("workflow", "").empty())
00257 throw cms::Exception("DQMFileSaver")
00258 << "The 'workflow' parameter must be empty in 'Online' convention.";
00259 else
00260 {
00261 workflow_="/Global/Online/P5";
00262 }
00263
00264
00265 producer_ = ps.getUntrackedParameter<std::string>("producer", producer_);
00266 if (convention_ == Online
00267 && producer_ != "DQM"
00268 && producer_ != "HLTDQM"
00269 && producer_ != "Playback")
00270 {
00271 throw cms::Exception("DQMFileSaver")
00272 << "Invalid 'producer' parameter '" << producer_
00273 << "'. Expected 'DQM', 'HLTDQM' or 'Playback'.";
00274 }
00275 else if (convention_ != Online && producer_ != "DQM")
00276 {
00277 throw cms::Exception("DQMFileSaver")
00278 << "Invalid 'producer' parameter '" << producer_
00279 << "'. Expected 'DQM'.";
00280 }
00281
00282
00283 version_ = ps.getUntrackedParameter<int>("version", version_);
00284
00285 runIsComplete_ = ps.getUntrackedParameter<bool>("runIsComplete", runIsComplete_);
00286
00287
00288 std::string refsave = ps.getUntrackedParameter<std::string>("referenceHandling", "default");
00289 if (refsave == "default")
00290 ;
00291 else if (refsave == "skip")
00292 {
00293 saveReference_ = DQMStore::SaveWithoutReference;
00294
00295 }
00296 else if (refsave == "all")
00297 {
00298 saveReference_ = DQMStore::SaveWithReference;
00299
00300 }
00301 else if (refsave == "qtests")
00302 {
00303 saveReference_ = DQMStore::SaveWithReferenceForQTest;
00304
00305 }
00306 else
00307 throw cms::Exception("DQMFileSaver")
00308 << "Invalid 'referenceHandling' parameter '" << refsave
00309 << "'. Expected 'default', 'skip', 'all' or 'qtests'.";
00310
00311
00312 saveReferenceQMin_ = ps.getUntrackedParameter<int>("referenceRequireStatus", saveReferenceQMin_);
00313
00314
00315 struct stat s;
00316 dirName_ = ps.getUntrackedParameter<std::string>("dirName", dirName_);
00317 if (dirName_.empty() || stat(dirName_.c_str(), &s) == -1)
00318 throw cms::Exception("DQMFileSaver")
00319 << "Invalid 'dirName' parameter '" << dirName_ << "'.";
00320
00321
00322
00323
00324
00325 if (convention_ == Online)
00326 {
00327 getAnInt(ps, saveByEvent_, "saveByEvent");
00328 getAnInt(ps, saveByMinute_, "saveByMinute");
00329 getAnInt(ps, saveByTime_, "saveByTime");
00330 getAnInt(ps, numKeepSavedFiles_, "maxSavedFilesCount");
00331 }
00332
00333 if (convention_ == Online || convention_ == Offline)
00334 {
00335 getAnInt(ps, saveByRun_, "saveByRun");
00336 getAnInt(ps, saveByLumiSection_, "saveByLumiSection");
00337 }
00338
00339 if (convention_ != Online)
00340 {
00341 getAnInt(ps, forceRunNumber_, "forceRunNumber");
00342 saveAtJobEnd_ = ps.getUntrackedParameter<bool>("saveAtJobEnd", saveAtJobEnd_);
00343 }
00344
00345 if (saveAtJobEnd_ && forceRunNumber_ < 1)
00346 throw cms::Exception("DQMFileSaver")
00347 << "If saving at the end of the job, the run number must be"
00348 << " overridden to a specific value using 'forceRunNumber'.";
00349
00350
00351
00352 char version[8];
00353 sprintf(version, "_V%04d_", int(version_));
00354 version[7]='\0';
00355 fileBaseName_ = dirName_ + "/" + producer_ + version;
00356 gettimeofday(&start_, 0);
00357 saved_ = start_;
00358
00359
00360 edm::LogInfo("DQMFileSaver")
00361 << "DQM file saving settings:\n"
00362 << " using base file name '" << fileBaseName_ << "'\n"
00363 << " forcing run number " << forceRunNumber_ << "\n"
00364 << " saving every " << saveByLumiSection_ << " lumi section(s)\n"
00365 << " saving every " << saveByEvent_ << " event(s)\n"
00366 << " saving every " << saveByMinute_ << " minute(s)\n"
00367 << " saving every 2^n*" << saveByTime_ << " minutes \n"
00368 << " saving every " << saveByRun_ << " run(s)\n"
00369 << " saving at job end: " << (saveAtJobEnd_ ? "yes" : "no") << "\n"
00370 << " keeping at most " << numKeepSavedFiles_ << " files\n";
00371 }
00372
00373
00374 void
00375 DQMFileSaver::beginJob()
00376 {
00377 irun_ = ilumi_ = ilumiprev_ = ievent_ = -1;
00378 nrun_ = nlumi_ = nevent_ = 0;
00379 }
00380
00381 void
00382 DQMFileSaver::beginRun(const edm::Run &r, const edm::EventSetup &)
00383 {
00384 irun_ = (forceRunNumber_ == -1 ? r.id().run() : forceRunNumber_);
00385 ++nrun_;
00386 }
00387
00388 void
00389 DQMFileSaver::beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &)
00390 {
00391 ilumi_ = l.id().luminosityBlock();
00392 if (ilumiprev_ == -1) ilumiprev_ = ilumi_;
00393 ++nlumi_;
00394 }
00395
00396 void DQMFileSaver::analyze(const edm::Event &e, const edm::EventSetup &)
00397 {
00398 ++nevent_;
00399
00400 ievent_ = e.id().event();
00401
00402
00403 char suffix[64];
00404 if (ievent_ > 0 && saveByEvent_ > 0 && nevent_ == saveByEvent_)
00405 {
00406 if (convention_ != Online)
00407 throw cms::Exception("DQMFileSaver")
00408 << "Internal error, can save files by event"
00409 << " only in Online mode.";
00410
00411 sprintf(suffix, "_R%09d_E%08d", irun_, ievent_);
00412 saveForOnline(suffix, "\\1\\2");
00413 nevent_ = 0;
00414 }
00415
00416
00417 if ( ievent_ > 0 && ( saveByMinute_ > 0 || saveByTime_ > 0 ) )
00418 {
00419 if (convention_ != Online)
00420 throw cms::Exception("DQMFileSaver")
00421 << "Internal error, can save files by time"
00422 << " only in Online mode.";
00423
00424
00425 struct timeval tv;
00426 gettimeofday(&tv, 0);
00427
00428 double totalelapsed = ((tv.tv_sec + tv.tv_usec*1e-6)
00429 - (start_.tv_sec + start_.tv_usec*1e-6)) / 60;
00430 double elapsed = ((tv.tv_sec + tv.tv_usec*1e-6)
00431 - (saved_.tv_sec + saved_.tv_usec*1e-6)) / 60;
00432
00433
00434 if ( (saveByMinute_ > 0 && elapsed > saveByMinute_ ) ||
00435 (saveByTime_ > 0 && totalelapsed > saveByTime_ ) )
00436 {
00437 if ( saveByTime_ > 0 ) saveByTime_ *= 2;
00438 saved_ = tv;
00439 sprintf(suffix, "_R%09d_T%08d", irun_, int(totalelapsed));
00440 char rewrite[64]; sprintf(rewrite, "\\1Run %d/\\2/Run summary", irun_);
00441 saveForOnline(suffix, rewrite);
00442 }
00443 }
00444 }
00445
00446 void
00447 DQMFileSaver::endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
00448 {
00449
00450 if (ilumi_ > 0 && saveByLumiSection_ > 0 )
00451 {
00452 if (convention_ != Online && convention_ != Offline )
00453 throw cms::Exception("DQMFileSaver")
00454 << "Internal error, can save files at end of lumi block"
00455 << " only in Online or Offline mode.";
00456
00457 if (convention_ == Online && nlumi_ == saveByLumiSection_)
00458 {
00459 char suffix[64];
00460 char rewrite[128];
00461 sprintf(suffix, "_R%09d_L%06d", irun_, ilumi_);
00462 sprintf(rewrite, "\\1Run %d/\\2/By Lumi Section %d-%d", irun_, ilumiprev_, ilumi_);
00463 saveForOnline(suffix, rewrite);
00464 ilumiprev_ = -1;
00465 nlumi_ = 0;
00466 }
00467 if (convention_ == Offline)
00468 saveForOffline(workflow_, irun_, ilumi_);
00469 }
00470 }
00471
00472 void
00473 DQMFileSaver::endRun(const edm::Run &, const edm::EventSetup &)
00474 {
00475 if (irun_ > 0 && saveByRun_ > 0 && nrun_ == saveByRun_)
00476 {
00477 if (convention_ == Online)
00478 {
00479 char suffix[64]; sprintf(suffix, "_R%09d", irun_);
00480 char rewrite[64]; sprintf(rewrite, "\\1Run %d/\\2/Run summary", irun_);
00481 saveForOnline(suffix, rewrite);
00482 }
00483 else if (convention_ == Offline)
00484 saveForOffline(workflow_, irun_, 0);
00485 else
00486 throw cms::Exception("DQMFileSaver")
00487 << "Internal error. Can only save files in endRun()"
00488 << " in Online and Offline modes.";
00489
00490 nrun_ = 0;
00491 }
00492 }
00493
00494 void
00495 DQMFileSaver::endJob(void)
00496 {
00497 if (saveAtJobEnd_)
00498 {
00499 if (convention_ == Offline && forceRunNumber_ > 0)
00500 saveForOffline(workflow_, forceRunNumber_, 0);
00501 else
00502 throw cms::Exception("DQMFileSaver")
00503 << "Internal error. Can only save files at the end of the"
00504 << " job in Offline mode with run number overridden.";
00505 }
00506
00507 }