CMS 3D CMS Logo

RamdiskMonitor.cc
Go to the documentation of this file.
4 
8 
11 
13 
14 #include <vector>
15 #include <map>
16 #include <sys/stat.h>
17 
18 #include <boost/regex.hpp>
19 #include <boost/format.hpp>
20 #include <boost/range.hpp>
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string/predicate.hpp>
23 
24 namespace dqm {
25  namespace rdm {
26  struct Empty {};
27  } // namespace rdm
28  class RamdiskMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<rdm::Empty>> {
29  public:
31  ~RamdiskMonitor() override;
32  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
33 
34  protected:
35  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
36  std::shared_ptr<rdm::Empty> globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi,
37  edm::EventSetup const &eSetup) const override;
39  void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override{};
40 
41  void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const;
42  double getRunTimestamp() const;
43 
44  const unsigned int runNumber_;
46  const std::vector<std::string> streamLabels_;
48 
49  struct StreamME {
52 
55  };
56 
57  std::map<std::string, StreamME> streams_;
58  mutable std::set<std::string> filesSeen_;
59  mutable double global_start_ = 0.;
60 
61  static constexpr double LUMI = 23.310893056;
62  };
63 
65  : runNumber_{ps.getUntrackedParameter<unsigned int>("runNumber")},
66  runInputDir_{ps.getUntrackedParameter<std::string>("runInputDir")},
67  streamLabels_{ps.getUntrackedParameter<std::vector<std::string>>("streamLabels")},
68  runPath_{str(boost::format("%s/run%06d") % runInputDir_ % runNumber_)}
69 
70  {}
71 
73 
74  void RamdiskMonitor::bookHistograms(DQMStore::IBooker &ib, edm::Run const &, edm::EventSetup const &) {
75  for (auto stream : streamLabels_) {
76  edm::LogInfo("RamdiskMonitor") << "Booking: " << stream;
77 
78  ib.cd();
79  ib.setCurrentFolder(std::string("Info/RamdiskMonitor/") + stream + "/");
80 
81  StreamME m;
82 
83  m.eventsAccepted = ib.book1D("EventAccepted", "# of accepted events per lumi", 4, 0., 4.);
84  m.eventsProcessed = ib.book1D("EventProcessed", "# of processed events per lumi", 4, 0., 4.);
85  m.deliveryDelayMTime =
86  ib.book1D("DeliveryDelayMTime", "Observed delivery delay for the data file (mtime).", 4, 0., 4.);
87  m.deliveryDelayCTime =
88  ib.book1D("DeliveryDelayCTime", "Observed delivery delay for the data file (ctime).", 4, 0., 4.);
89 
90  m.eventsAccepted->getTH1F()->SetCanExtend(TH1::kXaxis);
91  m.eventsProcessed->getTH1F()->SetCanExtend(TH1::kXaxis);
92  m.deliveryDelayMTime->getTH1F()->SetCanExtend(TH1::kXaxis);
93  m.deliveryDelayCTime->getTH1F()->SetCanExtend(TH1::kXaxis);
94 
95  m.eventsAccepted->setAxisTitle("Luminosity Section", 1);
96  m.eventsProcessed->setAxisTitle("Luminosity Section", 1);
97  m.deliveryDelayMTime->setAxisTitle("Luminosity Section", 1);
98  m.deliveryDelayCTime->setAxisTitle("Luminosity Section", 1);
99 
100  m.eventsAccepted->setAxisTitle("Number of events", 2);
101  m.eventsProcessed->setAxisTitle("Number of events", 2);
102  m.deliveryDelayMTime->setAxisTitle("Delay (s.)", 2);
103  m.deliveryDelayCTime->setAxisTitle("Delay (s.)", 2);
104 
105  streams_[stream] = m;
106  }
107  };
108 
110  if (global_start_ != 0)
111  return global_start_;
112 
113  std::string run_global = str(boost::format("%s/.run%06d.global") % runInputDir_ % runNumber_);
114  struct stat st;
115  if (::stat(run_global.c_str(), &st) != 0) {
116  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << run_global;
117  return 0.;
118  }
119 
120  global_start_ = st.st_mtime;
121  edm::LogPrint("RamdiskMonitor") << "Run start timestamp: " << global_start_;
122  return global_start_;
123  };
124 
125  void RamdiskMonitor::analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const {
126  using LumiEntry = dqmservices::DQMFileIterator::LumiEntry;
127 
128  // we are disabled, at least for this stream
129  if (streams_.empty())
130  return;
131 
132  auto itStream = streams_.find(label);
133  if (itStream == streams_.end()) {
134  edm::LogPrint("RamdiskMonitor") << "Stream not monitored [" << label << "]: " << fn;
135  return;
136  }
137 
138  StreamME m = itStream->second;
139 
140  // decode json and fill in some histograms
141  LumiEntry lumi_jsn = LumiEntry::load_json(runPath_, fn, lumi, -1);
142  m.eventsAccepted->setBinContent(lumi, lumi_jsn.n_events_accepted);
143  m.eventsProcessed->setBinContent(lumi, lumi_jsn.n_events_processed);
144 
145  // collect stat struct and calculate mtimes
146  struct stat st;
147  if (::stat(fn.c_str(), &st) != 0) {
148  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << fn;
149  return;
150  }
151 
152  // get start offset (from .global)
153  // abort the calculation if it does not exist
154  double start_offset = getRunTimestamp();
155  if (start_offset <= 0)
156  return;
157 
158  // check fff_dqmtools (separate repository)
159  // for calculation details
160  double mtime = st.st_mtime;
161  double ctime = st.st_ctime;
162 
163  // timeout from the begging of the run
164  double start_offset_mtime = mtime - start_offset - LUMI;
165  double start_offset_ctime = ctime - start_offset - LUMI;
166  double lumi_offset = (lumi - 1) * LUMI;
167 
168  // timeout from the time we think this lumi happenned
169  double delay_mtime = start_offset_mtime - lumi_offset;
170  double delay_ctime = start_offset_ctime - lumi_offset;
171 
172  m.deliveryDelayMTime->setBinContent(lumi, delay_mtime);
173  m.deliveryDelayCTime->setBinContent(lumi, delay_ctime);
174  };
175 
176  std::shared_ptr<dqm::rdm::Empty> RamdiskMonitor::globalBeginLuminosityBlock(edm::LuminosityBlock const &,
177  edm::EventSetup const &eSetup) const {
178  // search filesystem to find available lumi section files
179  using boost::filesystem::directory_entry;
180  using boost::filesystem::directory_iterator;
181 
182  directory_iterator dend;
183  for (directory_iterator di(runPath_); di != dend; ++di) {
184  const boost::regex fn_re("run(\\d+)_ls(\\d+)_([a-zA-Z0-9]+)(_.*)?\\.jsn");
185 
186  const std::string filename = di->path().filename().string();
187  const std::string fn = di->path().string();
188 
189  if (filesSeen_.find(filename) != filesSeen_.end()) {
190  continue;
191  }
192 
193  boost::smatch result;
194  if (boost::regex_match(filename, result, fn_re)) {
195  unsigned int run = std::stoi(result[1]);
196  unsigned int lumi = std::stoi(result[2]);
197  std::string label = result[3];
198 
199  filesSeen_.insert(filename);
200 
201  if (run != runNumber_)
202  continue;
203 
204  // check if this is EoR
205  if ((lumi == 0) && (label == "EoR")) {
206  // do not handle
207  continue;
208  }
209 
210  try {
211  this->analyzeFile(fn, run, lumi, label);
212  } catch (const std::exception &e) {
213  // it's likely we have read it too soon
214  filesSeen_.erase(filename);
215 
216  std::string msg("Found, tried to load the json, but failed (");
217  msg += e.what();
218  msg += "): ";
219  edm::LogWarning("RamdiskMonitor") << msg;
220  }
221  }
222  }
223 
224  // @TODO lookup info for the current lumi
225  return std::shared_ptr<dqm::rdm::Empty>();
226  }
227 
230 
231  desc.setComment(
232  "Analyses file timestams in the /fff/ramdisk and creates monitor "
233  "elements.");
234 
235  desc.addUntracked<std::vector<std::string>>("streamLabels")->setComment("List of streams to monitor.");
236 
237  desc.addUntracked<unsigned int>("runNumber")->setComment("Run number passed via configuration file.");
238 
239  desc.addUntracked<std::string>("runInputDir")->setComment("Directory where the DQM files will appear.");
240 
241  d.add("RamdiskMonitor", desc);
242  }
243 
244 } // namespace dqm
245 
247 
ConfigurationDescriptions.h
dqm::impl::MonitorElement
Definition: MonitorElement.h:98
dqm::RamdiskMonitor::runNumber_
const unsigned int runNumber_
Definition: RamdiskMonitor.cc:44
dqm::RamdiskMonitor::~RamdiskMonitor
~RamdiskMonitor() override
Definition: RamdiskMonitor.cc:72
dqm::RamdiskMonitor::getRunTimestamp
double getRunTimestamp() const
Definition: RamdiskMonitor.cc:109
dqm::RamdiskMonitor::StreamME
Definition: RamdiskMonitor.cc:49
MessageLogger.h
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::Run
Definition: Run.h:45
cms::cuda::stream
cudaStream_t stream
Definition: HistoContainer.h:57
dqm::RamdiskMonitor::runPath_
const std::string runPath_
Definition: RamdiskMonitor.cc:47
edm::LogInfo
Definition: MessageLogger.h:254
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
dqm::RamdiskMonitor::StreamME::deliveryDelayMTime
MonitorElement * deliveryDelayMTime
Definition: RamdiskMonitor.cc:53
dqm::RamdiskMonitor::runInputDir_
const std::string runInputDir_
Definition: RamdiskMonitor.cc:45
DQMOneEDAnalyzer
Definition: DQMOneEDAnalyzer.h:20
DQMStore.h
DQMFileIterator.h
dqm::RamdiskMonitor::filesSeen_
std::set< std::string > filesSeen_
Definition: RamdiskMonitor.cc:58
dqm::RamdiskMonitor::bookHistograms
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: RamdiskMonitor.cc:74
mps_check.msg
tuple msg
Definition: mps_check.py:285
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
dqmservices::DQMFileIterator::LumiEntry
Definition: DQMFileIterator.h:20
dqm::RamdiskMonitor::globalBeginLuminosityBlock
std::shared_ptr< rdm::Empty > globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) const override
Definition: RamdiskMonitor.cc:176
dqm::RamdiskMonitor::analyze
void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override
Definition: RamdiskMonitor.cc:39
DQMOneEDAnalyzer.h
MakerMacros.h
dqm::legacy::DQMStore::IBooker
dqm::implementation::IBooker IBooker
Definition: DQMStore.h:729
dqm::RamdiskMonitor
Definition: RamdiskMonitor.cc:28
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
hgcalPlots.stat
stat
Definition: hgcalPlots.py:1111
dqm-mbProfile.format
format
Definition: dqm-mbProfile.py:16
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
str
#define str(s)
Definition: TestProcessor.cc:48
dqm::RamdiskMonitor::StreamME::eventsAccepted
MonitorElement * eventsAccepted
Definition: RamdiskMonitor.cc:50
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
ParameterSetDescription.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
dqm::RamdiskMonitor::LUMI
static constexpr double LUMI
Definition: RamdiskMonitor.cc:61
edm::LogWarning
Definition: MessageLogger.h:141
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
edm::ParameterSetDescription::addUntracked
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:100
dqm::RamdiskMonitor::RamdiskMonitor
RamdiskMonitor(const edm::ParameterSet &ps)
Definition: RamdiskMonitor.cc:64
edm::ParameterSet
Definition: ParameterSet.h:36
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
Event.h
dqm::RamdiskMonitor::streamLabels_
const std::vector< std::string > streamLabels_
Definition: RamdiskMonitor.cc:46
edm::LogPrint
Definition: MessageLogger.h:342
cuy.ib
ib
Definition: cuy.py:662
edm::EventSetup
Definition: EventSetup.h:57
dqm::RamdiskMonitor::analyzeFile
void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const
Definition: RamdiskMonitor.cc:125
writedatasetfile.run
run
Definition: writedatasetfile.py:27
dqm::RamdiskMonitor::globalEndLuminosityBlock
void globalEndLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) final
Definition: RamdiskMonitor.cc:38
dqm::RamdiskMonitor::StreamME::deliveryDelayCTime
MonitorElement * deliveryDelayCTime
Definition: RamdiskMonitor.cc:54
dqm::RamdiskMonitor::global_start_
double global_start_
Definition: RamdiskMonitor.cc:59
EventSetup.h
personalPlayback.fn
fn
Definition: personalPlayback.py:515
dqm::RamdiskMonitor::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: RamdiskMonitor.cc:228
RamdiskMonitor
dqm::RamdiskMonitor RamdiskMonitor
Definition: RamdiskMonitor.cc:248
ztail.d
d
Definition: ztail.py:151
mps_fire.result
result
Definition: mps_fire.py:303
dqm
Definition: DQMStore.h:18
ParameterSet.h
edm::Event
Definition: Event.h:73
lumi
Definition: LumiSectionData.h:20
dqm::RamdiskMonitor::StreamME::eventsProcessed
MonitorElement * eventsProcessed
Definition: RamdiskMonitor.cc:51
dqm::rdm::Empty
Definition: RamdiskMonitor.cc:26
sistrip::runNumber_
static const char runNumber_[]
Definition: ConstantsForDqm.h:33
label
const char * label
Definition: PFTauDecayModeTools.cc:11
dqm::RamdiskMonitor::streams_
std::map< std::string, StreamME > streams_
Definition: RamdiskMonitor.cc:57
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37