CMS 3D CMS Logo

SiStripDetVOffTrendPlotter.cc
Go to the documentation of this file.
6 
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 
12 
16 
20 
21 #include <TROOT.h>
22 #include <TSystem.h>
23 #include <TCanvas.h>
24 #include <TFile.h>
25 #include <TLegend.h>
26 #include <TGraph.h>
27 #include <TH1.h>
28 
30 public:
31  const std::vector<Color_t> PLOT_COLORS{kRed, kBlue, kBlack, kOrange, kMagenta};
32 
33  explicit SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig);
34  ~SiStripDetVOffTrendPlotter() override;
35  void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override;
36  void endJob() override;
37 
38 private:
39  std::string formatIOV(cond::Time_t iov, std::string format = "%Y-%m-%d__%H_%M_%S");
40  void prepGraph(TGraph *gr, TString name, TString title, Color_t color);
41  void dumpCSV(bool isHV, std::size_t nModules);
42 
45  std::vector<std::string> m_plotTags;
46 
47  // Time interval (in hours) to go back for the plotting.
49  // Or manually specify the start/end time. Format: "2002-01-20 23:59:59.000". Set m_interval to non-positive number to use this.
52  // Specify output plot file name. Will use the timestamps if left empty.
54  // Specify output root file name. Leave empty if do not want to save plots in a root file.
56  // Specify output CSV file name. Leave empty if do not want to dump HV/LV counts in a CSV file.
58  TFile *fout;
59 
60  // IOV TAG #HV, #LV
61  std::map<cond::Time_t, std::map<std::string, std::pair<int, int>>> iovMap;
62 
64 };
65 
67  : m_connectionPool(),
68  m_condDb(iConfig.getParameter<std::string>("conditionDatabase")),
69  m_plotTags(iConfig.getParameter<std::vector<std::string>>("plotTags")),
70  m_interval(iConfig.getParameter<int>("timeInterval")),
71  m_startTime(iConfig.getUntrackedParameter<std::string>("startTime", "")),
72  m_endTime(iConfig.getUntrackedParameter<std::string>("endTime", "")),
73  m_outputPlot(iConfig.getUntrackedParameter<std::string>("outputPlot", "")),
74  m_outputRootFile(iConfig.getUntrackedParameter<std::string>("outputRootFile", "")),
75  m_outputCSV(iConfig.getUntrackedParameter<std::string>("outputCSV", "")),
76  fout(nullptr),
77  geomDetToken_(esConsumes()) {
80  if (!m_outputRootFile.empty())
81  fout = new TFile(m_outputRootFile.data(), "RECREATE");
82 }
83 
85  if (fout)
86  fout->Close();
87 }
88 
90  // get total number of modules
91  const auto num_modules = TrackerGeometryUtils::getSiStripDetIds(evtSetup.getData(geomDetToken_)).size();
92 
93  // get start and end time for DB query
94  boost::posix_time::ptime p_start, p_end;
95  if (m_interval > 0) {
96  // from m_interval hours ago to now
97  p_end = boost::posix_time::second_clock::universal_time();
98  p_start = p_end - boost::posix_time::hours(m_interval);
99  } else {
100  // use start and end time from config file
101  p_start = boost::posix_time::time_from_string(m_startTime);
102  p_end = boost::posix_time::time_from_string(m_endTime);
103  }
104  cond::Time_t startIov = cond::time::from_boost(p_start);
105  cond::Time_t endIov = cond::time::from_boost(p_end);
106  if (startIov > endIov)
107  throw cms::Exception("endTime must be greater than startTime!");
108  edm::LogInfo("SiStripDetVOffTrendPlotter")
109  << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
110  << "Set start IOV " << startIov << " (" << boost::posix_time::to_simple_string(p_start) << ")"
111  << "\n ... Set end IOV " << endIov << " (" << boost::posix_time::to_simple_string(p_end) << ")";
112 
113  // open db session
114  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
115  << "Query the condition database " << m_condDb;
117  condDbSession.transaction().start(true);
118 
119  // loop over all tags to plot
120  std::vector<TGraph *> hvgraphs, lvgraphs;
121  TLegend *leg_hv = new TLegend(0.6, 0.87, 0.99, 0.99);
122  TLegend *leg_lv = new TLegend(0.6, 0.87, 0.99, 0.99);
123  for (unsigned itag = 0; itag < m_plotTags.size(); ++itag) {
124  auto tag = m_plotTags.at(itag);
125  auto color = itag < PLOT_COLORS.size() ? PLOT_COLORS.at(itag) : kGreen + itag;
126 
127  std::vector<double> vTime;
128  std::vector<double> vHVOffPercent, vLVOffPercent;
129 
130  // query the database
131  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
132  << "Reading IOVs from tag " << tag;
133  cond::persistency::IOVProxy iovProxy = condDbSession.readIov(tag);
134  auto iovs = iovProxy.selectAll();
135  auto iiov = iovs.find(startIov);
136  auto eiov = iovs.find(endIov);
137  int niov = 0;
138  while (iiov != iovs.end() && (*iiov).since <= (*eiov).since) {
139  // convert cond::Time_t to seconds since epoch
140  if ((*iiov).since < startIov)
141  vTime.push_back(cond::time::unpack(startIov).first);
142  else
143  vTime.push_back(cond::time::unpack((*iiov).since).first);
144  auto payload = condDbSession.fetchPayload<SiStripDetVOff>((*iiov).payloadId);
145  vHVOffPercent.push_back(1.0 * payload->getHVoffCounts() / num_modules);
146  vLVOffPercent.push_back(1.0 * payload->getLVoffCounts() / num_modules);
147  iovMap[(*iiov).since][tag] = {payload->getHVoffCounts(), payload->getLVoffCounts()};
148  // debug
149  std::cout << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since)) << " (" << (*iiov).since
150  << ")"
151  << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts() << ", # LV Off=" << std::setw(6)
152  << payload->getLVoffCounts() << std::endl;
153  ++iiov;
154  ++niov;
155  }
156  edm::LogInfo("SiStripDetVOffTrendPlotter")
157  << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
158  << "Read " << niov << " IOVs from tag " << tag << " in the specified interval.";
159 
160  TGraph *hv = new TGraph(vTime.size(), vTime.data(), vHVOffPercent.data());
161  prepGraph(hv, TString("HVOff_") + tag, ";UTC;Fraction of HV off", color);
162  leg_hv->AddEntry(hv, tag.data(), "LP");
163 
164  TGraph *lv = new TGraph(vTime.size(), vTime.data(), vLVOffPercent.data());
165  prepGraph(lv, TString("LVOff_") + tag, ";UTC;Fraction of LV off", color);
166  leg_lv->AddEntry(lv, tag.data(), "LP");
167 
168  hvgraphs.push_back(hv);
169  lvgraphs.push_back(lv);
170  }
171 
172  condDbSession.transaction().commit();
173 
174  // Make plots
175  TCanvas c("c", "c", 1800, 1200);
176  c.SetTopMargin(0.12);
177  c.SetBottomMargin(0.08);
178  c.SetGridx();
179  c.SetGridy();
180  for (const auto hv : hvgraphs) {
181  if (hv == hvgraphs.front())
182  hv->Draw("ALP");
183  else
184  hv->Draw("LPsame");
185  if (fout) {
186  fout->cd();
187  hv->Write();
188  }
189  }
190  leg_hv->Draw();
191  std::string plot_postfix =
192  !m_outputPlot.empty() ? m_outputPlot : "from_" + formatIOV(startIov) + "_to_" + formatIOV(endIov) + ".png";
193  c.Print(("HVOff_" + plot_postfix).data());
194 
195  c.Clear();
196  for (const auto lv : lvgraphs) {
197  if (lv == lvgraphs.front())
198  lv->Draw("ALP");
199  else
200  lv->Draw("LPsame");
201  if (fout) {
202  fout->cd();
203  lv->Write();
204  }
205  }
206  leg_lv->Draw();
207  c.Print(("LVOff_" + plot_postfix).data());
208 
209  if (!m_outputCSV.empty()) {
210  dumpCSV(true, num_modules);
211  dumpCSV(false, num_modules);
212  }
213 }
214 
216 
218  auto facet = new boost::posix_time::time_facet(format.c_str());
219  std::ostringstream stream;
220  stream.imbue(std::locale(stream.getloc(), facet));
222  return stream.str();
223 }
224 
225 void SiStripDetVOffTrendPlotter::prepGraph(TGraph *gr, TString name, TString title, Color_t color) {
226  gr->SetName(name);
227  gr->SetTitle(title);
228  gr->SetLineColor(color);
229  gr->SetLineWidth(2);
230  gr->SetMarkerStyle(20);
231  gr->SetMarkerSize(1.5);
232  gr->SetMarkerColor(color);
233  gr->GetXaxis()->SetTimeDisplay(1);
234  gr->GetXaxis()->SetLabelOffset(0.02);
235  gr->GetXaxis()->SetTimeFormat("#splitline{%b %d}{%H:%M}");
236  gr->GetXaxis()->SetTimeOffset(0, "gmt");
237  gr->GetXaxis()->SetLabelSize(0.025);
238  gr->GetXaxis()->SetTitleSize(0.025);
239  gr->GetXaxis()->SetTitleOffset(1.6);
240  gr->GetYaxis()->SetRangeUser(0, 1.05);
241 }
242 
243 void SiStripDetVOffTrendPlotter::dumpCSV(bool isHV, std::size_t nModules) {
244  std::string outCSV = isHV ? "HVOff_table_" + m_outputCSV : "LVOff_table_" + m_outputCSV;
245  std::ofstream csv;
246  csv.open(outCSV);
247  csv << "IOV,Timestamp(UTC),";
248  for (const auto &tag : m_plotTags)
249  csv << tag << ",";
250  csv << std::endl;
251 
252  csv << std::fixed << std::setprecision(1);
253 
254  for (const auto &v : iovMap) {
255  auto iov = v.first;
256  auto timestamp = boost::posix_time::to_simple_string(cond::time::to_boost(iov));
257  csv << iov << "," << timestamp << ",";
258  for (const auto &tag : m_plotTags) {
259  if (v.second.find(tag) != v.second.end()) {
260  int count = isHV ? v.second.at(tag).first : v.second.at(tag).second;
261  csv << count << " (" << 100. * count / nModules << "%)";
262  }
263  csv << ",";
264  }
265  csv << std::endl;
266  }
267 
268  csv.close();
269 }
270 
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
runGCPTkAlMap.title
string title
Definition: runGCPTkAlMap.py:94
ConnectionPool.h
cond::time::to_boost
boost::posix_time::ptime to_boost(Time_t iValue)
Definition: TimeConversions.h:39
SiStripDetVOffTrendPlotter::m_outputRootFile
std::string m_outputRootFile
Definition: SiStripDetVOffTrendPlotter.cc:55
Time.h
cms::cuda::stream
cudaStream_t stream
Definition: HistoContainer.h:57
gather_cfg.cout
cout
Definition: gather_cfg.py:144
SiStripDetVOffTrendPlotter::m_startTime
std::string m_startTime
Definition: SiStripDetVOffTrendPlotter.cc:50
SiStripDetVOffTrendPlotter::iovMap
std::map< cond::Time_t, std::map< std::string, std::pair< int, int > > > iovMap
Definition: SiStripDetVOffTrendPlotter.cc:61
SiStripDetVOffTrendPlotter::SiStripDetVOffTrendPlotter
SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig)
Definition: SiStripDetVOffTrendPlotter.cc:66
SiStripDetVOffTrendPlotter::~SiStripDetVOffTrendPlotter
~SiStripDetVOffTrendPlotter() override
Definition: SiStripDetVOffTrendPlotter.cc:84
EDAnalyzer.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
findQualityFiles.v
v
Definition: findQualityFiles.py:179
SiStripDetVOffTrendPlotter::geomDetToken_
edm::ESGetToken< GeometricDet, IdealGeometryRecord > geomDetToken_
Definition: SiStripDetVOffTrendPlotter.cc:63
cond::persistency::ConnectionPool::createSession
Session createSession(const std::string &connectionString, bool writeCapable=false)
Definition: ConnectionPool.cc:172
cond::persistency::IOVProxy::selectAll
IOVArray selectAll()
Definition: IOVProxy.cc:175
TrackerGeometryUtils::getSiStripDetIds
std::vector< uint32_t > getSiStripDetIds(const GeometricDet &geomDet)
Definition: utils.cc:5
edm::EDAnalyzer
Definition: EDAnalyzer.h:28
cond::persistency::Session::fetchPayload
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
SiStripDetVOffTrendPlotter::m_interval
int m_interval
Definition: SiStripDetVOffTrendPlotter.cc:48
MakerMacros.h
cond::persistency::IOVArray::find
Iterator find(cond::Time_t time) const
Definition: IOVProxy.cc:95
cond::timestamp
Definition: Time.h:19
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
SiStripDetVOffTrendPlotter::m_outputCSV
std::string m_outputCSV
Definition: SiStripDetVOffTrendPlotter.cc:57
Service.h
SiStripDetVOffTrendPlotter::formatIOV
std::string formatIOV(cond::Time_t iov, std::string format="%Y-%m-%d__%H_%M_%S")
Definition: SiStripDetVOffTrendPlotter.cc:217
cond::persistency::ConnectionPool
Definition: ConnectionPool.h:35
SiStripDetVOffTrendPlotter
Definition: SiStripDetVOffTrendPlotter.cc:29
submitPVResolutionJobs.count
count
Definition: submitPVResolutionJobs.py:352
jets_cff.payload
payload
Definition: jets_cff.py:32
SiStripDetVOffTrendPlotter::m_connectionPool
cond::persistency::ConnectionPool m_connectionPool
Definition: SiStripDetVOffTrendPlotter.cc:43
first
auto first
Definition: CAHitNtupletGeneratorKernelsImpl.h:112
cond::persistency::Session::readIov
IOVProxy readIov(const std::string &tag)
Definition: Session.cc:63
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::IOVProxy
Definition: IOVProxy.h:92
SiStripDetVOffTrendPlotter::m_endTime
std::string m_endTime
Definition: SiStripDetVOffTrendPlotter.cc:51
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
cond::time::from_boost
Time_t from_boost(boost::posix_time::ptime bt)
Definition: TimeConversions.h:43
cond::persistency::Session
Definition: Session.h:63
SiStripDetVOffTrendPlotter::dumpCSV
void dumpCSV(bool isHV, std::size_t nModules)
Definition: SiStripDetVOffTrendPlotter.cc:243
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
groupFilesInBlocks.fout
fout
Definition: groupFilesInBlocks.py:162
SiStripDetVOffTrendPlotter::m_plotTags
std::vector< std::string > m_plotTags
Definition: SiStripDetVOffTrendPlotter.cc:45
createfilelist.int
int
Definition: createfilelist.py:10
cond::persistency::Transaction::commit
void commit()
Definition: Session.cc:23
SiStripDetVOff
Definition: SiStripDetVOff.h:31
SiStripDetVOffTrendPlotter::m_condDb
std::string m_condDb
Definition: SiStripDetVOffTrendPlotter.cc:44
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
SiStripDetVOffTrendPlotter::fout
TFile * fout
Definition: SiStripDetVOffTrendPlotter.cc:58
cond::persistency::ConnectionPool::setParameters
void setParameters(const edm::ParameterSet &connectionPset)
Definition: ConnectionPool.cc:43
IdealGeometryRecord.h
utils.h
edm::EventSetup
Definition: EventSetup.h:58
cond::persistency::ConnectionPool::configure
void configure()
Definition: ConnectionPool.cc:127
SiStripDetVOffTrendPlotter::endJob
void endJob() override
Definition: SiStripDetVOffTrendPlotter.cc:215
SiStripDetVOffTrendPlotter::analyze
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
Definition: SiStripDetVOffTrendPlotter.cc:89
edm::ESGetToken< GeometricDet, IdealGeometryRecord >
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
SiStripDetVOffTrendPlotter::PLOT_COLORS
const std::vector< Color_t > PLOT_COLORS
Definition: SiStripDetVOffTrendPlotter.cc:31
cond::persistency::Session::transaction
Transaction & transaction()
Definition: Session.cc:52
cond::persistency::Transaction::start
void start(bool readOnly=true)
Definition: Session.cc:18
std
Definition: JetResolutionObject.h:76
Exception
Definition: hltDiff.cc:245
format
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
SiStripDetVOffTrendPlotter::m_outputPlot
std::string m_outputPlot
Definition: SiStripDetVOffTrendPlotter.cc:53
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
cond::time::unpack
cond::UnpackedTime unpack(cond::Time_t iValue)
Definition: TimeConversions.h:22
getRunInfo.hours
hours
Definition: getRunInfo.py:25
TimeConversions.h
ParameterSet.h
SiStripDetVOff.h
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
SiStripDetVOffTrendPlotter::prepGraph
void prepGraph(TGraph *gr, TString name, TString title, Color_t color)
Definition: SiStripDetVOffTrendPlotter.cc:225
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443