CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripDetVOffTrendPlotter.cc
Go to the documentation of this file.
5 
6 #include <iostream>
7 #include <sstream>
8 
10 
15 
16 #include <TROOT.h>
17 #include <TSystem.h>
18 #include <TCanvas.h>
19 #include <TFile.h>
20 #include <TLegend.h>
21 #include <TGraph.h>
22 #include <TH1.h>
23 
24 
26 public:
27  const std::vector<Color_t> PLOT_COLORS {kBlack, kRed, kBlue, kOrange, kMagenta};
28 
29  explicit SiStripDetVOffTrendPlotter(const edm::ParameterSet& iConfig );
31  virtual void analyze( const edm::Event& evt, const edm::EventSetup& evtSetup);
32  virtual void endJob();
33 
34 private:
35  std::string formatIOV(cond::Time_t iov, std::string format="%Y-%m-%d__%H_%M_%S");
36  void prepGraph(TGraph *gr, TString name, TString title, Color_t color);
37 
40  std::vector<std::string> m_plotTags;
41 
42  // Time interval (in hours) to go back for the plotting.
44  // 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.
47  // Set the plot format. Default: png.
49  // Specify output root file name. Leave empty if do not want to save plots in a root file.
51  TFile *fout;
53 };
54 
56  m_connectionPool(),
57  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
58  m_plotTags( iConfig.getParameter< std::vector<std::string> >("plotTags") ),
59  m_interval( iConfig.getParameter< int >("timeInterval") ),
60  m_startTime( iConfig.getUntrackedParameter< std::string >("startTime", "") ),
61  m_endTime( iConfig.getUntrackedParameter< std::string >("endTime", "") ),
62  m_plotFormat( iConfig.getUntrackedParameter< std::string >("plotFormat", "png") ),
63  m_outputFile( iConfig.getUntrackedParameter< std::string >("outputFile", "") ),
64  fout(nullptr){
67  if (m_outputFile!="") fout = new TFile(m_outputFile.data(), "RECREATE");
68 }
69 
71  if (fout) fout->Close();
72 }
73 
75 
76  // get total number of modules
77  auto num_modules = detidReader->getAllDetIds().size();
78 
79  // get start and end time for DB query
80  boost::posix_time::ptime p_start, p_end;
81  if (m_interval > 0){
82  // from m_interval hours ago to now
83  p_end = boost::posix_time::second_clock::universal_time();
84  p_start = p_end - boost::posix_time::hours(m_interval);
85  }else {
86  // use start and end time from config file
87  p_start = boost::posix_time::time_from_string(m_startTime);
88  p_end = boost::posix_time::time_from_string(m_endTime);
89  }
90  cond::Time_t startIov = cond::time::from_boost(p_start);
91  cond::Time_t endIov = cond::time::from_boost(p_end);
92  if (startIov > endIov)
93  throw cms::Exception("endTime must be greater than startTime!");
94  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
95  << "Set start IOV " << startIov << " (" << boost::posix_time::to_simple_string(p_start) << ")"
96  << "\n ... Set end IOV " << endIov << " (" << boost::posix_time::to_simple_string(p_end) << ")" ;
97 
98  // open db session
99  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
100  << "Query the condition database " << m_condDb;
101  cond::persistency::Session condDbSession = m_connectionPool.createSession( m_condDb );
102  condDbSession.transaction().start( true );
103 
104  // loop over all tags to plot
105  std::vector<TGraph*> hvgraphs, lvgraphs;
106  TLegend *leg_hv = new TLegend(0.75, 0.87, 0.99, 0.99);
107  TLegend *leg_lv = new TLegend(0.75, 0.87, 0.99, 0.99);
108  for (unsigned itag=0; itag<m_plotTags.size(); ++itag){
109  auto tag = m_plotTags.at(itag);
110  auto color = itag<PLOT_COLORS.size() ? PLOT_COLORS.at(itag) : kGreen+itag;
111 
112  std::vector<double> vTime;
113  std::vector<double> vHVOffPercent, vLVOffPercent;
114 
115  // query the database
116  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
117  << "Reading IOVs from tag " << tag;
118  cond::persistency::IOVProxy iovProxy = condDbSession.readIov(tag, true); // load all?
119  auto iiov = iovProxy.find(startIov);
120  auto eiov = iovProxy.find(endIov);
121  int niov = 0;
122  while (iiov != iovProxy.end() && iiov != eiov){
123  // convert cond::Time_t to seconds since epoch
124  vTime.push_back(cond::time::unpack((*iiov).since).first);
125  auto payload = condDbSession.fetchPayload<SiStripDetVOff>( (*iiov).payloadId );
126  vHVOffPercent.push_back( 1.0*payload->getHVoffCounts()/num_modules );
127  vLVOffPercent.push_back( 1.0*payload->getLVoffCounts()/num_modules );
128  // debug
129  std::cout << boost::posix_time::to_simple_string(cond::time::to_boost((*iiov).since))
130  << " (" << (*iiov).since << ")"
131  << ", # HV Off=" << std::setw(6) << payload->getHVoffCounts()
132  << ", # LV Off=" << std::setw(6) << payload->getLVoffCounts() << std::endl;
133  ++iiov;
134  ++niov;
135  }
136  edm::LogInfo("SiStripDetVOffTrendPlotter") << "[SiStripDetVOffTrendPlotter::" << __func__ << "] "
137  << "Read " << niov << " IOVs from tag " << tag << " in the specified interval.";
138 
139  TGraph *hv = new TGraph(vTime.size(), vTime.data(), vHVOffPercent.data());
140  prepGraph(hv, TString("HVOff_")+tag, ";UTC;Fraction of HV off", color);
141  leg_hv->AddEntry(hv, tag.data(), "LP");
142 
143  TGraph *lv = new TGraph(vTime.size(), vTime.data(), vLVOffPercent.data());
144  prepGraph(lv, TString("LVOff_")+tag, ";UTC;Fraction of LV off", color);
145  leg_lv->AddEntry(lv, tag.data(), "LP");
146 
147  hvgraphs.push_back(hv);
148  lvgraphs.push_back(lv);
149  }
150 
151  condDbSession.transaction().commit();
152 
153  // Make plots
154  TCanvas c("c", "c", 900, 600);
155  c.SetTopMargin(0.12);
156  c.SetBottomMargin(0.08);
157  c.SetGridx();
158  c.SetGridy();
159  for (const auto hv : hvgraphs){
160  if (hv==hvgraphs.front())
161  hv->Draw("ALP");
162  else
163  hv->Draw("ALPsame");
164  if (fout){
165  fout->cd();
166  hv->Write();
167  }
168  }
169  leg_hv->Draw();
170  c.Print( ("HVOff_from_" + formatIOV(startIov) + "_to_" + formatIOV(endIov) + ".pdf").data() );
171 
172  c.Clear();
173  for (const auto lv : lvgraphs){
174  if (lv==hvgraphs.front())
175  lv->Draw("ALP");
176  else
177  lv->Draw("ALPsame");
178  if (fout){
179  fout->cd();
180  lv->Write();
181  }
182  }
183  leg_lv->Draw();
184  c.Print( ("LVOff_from_" + formatIOV(startIov) + "_to_" + formatIOV(endIov) + ".pdf").data() );
185 
186 }
187 
189 }
190 
192  auto facet = new boost::posix_time::time_facet(format.c_str());
193  std::ostringstream stream;
194  stream.imbue(std::locale(stream.getloc(), facet));
195  stream << cond::time::to_boost(iov);
196  return stream.str();
197 }
198 
199 void SiStripDetVOffTrendPlotter::prepGraph(TGraph* gr, TString name, TString title, Color_t color) {
200  gr->SetName(name);
201  gr->SetTitle(title);
202  gr->SetLineColor(color);
203  gr->SetMarkerStyle(20);
204  gr->SetMarkerColor(color);
205  gr->GetXaxis()->SetTimeDisplay(1);
206  gr->GetXaxis()->SetTimeOffset(0, "gmt");
207  gr->GetYaxis()->SetRangeUser(0, 1.01);
208 }
209 
210 
212 
T getParameter(std::string const &) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
boost::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:190
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
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
tuple iov
Definition: o2o.py:307
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:288
Session createSession(const std::string &connectionString, bool writeCapable=false)
Time_t from_boost(boost::posix_time::ptime bt)
cond::persistency::ConnectionPool m_connectionPool
const std::vector< Color_t > PLOT_COLORS
SiStripDetVOffTrendPlotter(const edm::ParameterSet &iConfig)
tuple cout
Definition: gather_cfg.py:145
boost::posix_time::ptime to_boost(Time_t iValue)
Iterator end() const
Definition: IOVProxy.cc:265
cond::UnpackedTime unpack(cond::Time_t iValue)