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