CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BeamSpotPayloadInspectorHelper.h
Go to the documentation of this file.
1 #ifndef CONDCORE_BEAMSPOTPLUGINS_BEAMSPOTPAYLOADINSPECTORHELPER_H
2 #define CONDCORE_BEAMSPOTPLUGINS_BEAMSPOTPAYLOADINSPECTORHELPER_H
3 
4 // User includes
5 
11 
12 // ROOT includes
13 
14 #include <memory>
15 #include <sstream>
16 #include "TCanvas.h"
17 #include "TStyle.h"
18 #include "TH2F.h"
19 #include "TLatex.h"
20 
21 namespace BeamSpotPI {
22 
23  inline std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
24  auto kLowMask = 0XFFFFFFFF;
25  auto run = (since >> 32);
26  auto lumi = (since & kLowMask);
27  return std::make_pair(run, lumi);
28  }
29 
30  enum parameters {
31  X = 0, // 0 regular BS methods
32  Y = 1, // 1
33  Z = 2, // 2
34  sigmaX = 3, // 3
35  sigmaY = 4, // 4
36  sigmaZ = 5, // 5
37  dxdz = 6, // 6
38  dydz = 7, // 7
39  lastLumi = 8, // 8 additional int's
40  lastRun = 9, // 9
41  lastFill = 10, // 10
42  nTracks = 11, // 11
43  nPVs = 12, // 12
44  nUsedEvents = 13, // 13
45  maxPVs = 14, // 14
46  meanPV = 15, // 15 additional float's
47  meanErrorPV = 16, // 16
48  rmsPV = 17, // 17
49  rmsErrorPV = 18, // 18
50  creationTime = 19, // 19 additional cond::Time_t
51  startTimeStamp = 20, // 20
52  endTimeStamp = 21, // 21
53  startTime = 22, // 22 additional std::string
54  endTime = 23, // 23
55  lumiRange = 24, // 24
57  };
58 
59  /************************************************/
60  inline std::string getStringFromParamEnum(const parameters& parameter) {
61  switch (parameter) {
62  case X:
63  return "X";
64  case Y:
65  return "Y";
66  case Z:
67  return "Z";
68  case sigmaX:
69  return "sigmaX";
70  case sigmaY:
71  return "sigmaY";
72  case sigmaZ:
73  return "sigmaZ";
74  case dxdz:
75  return "dx/dz";
76  case dydz:
77  return "dy/dz";
78  default:
79  return "should never be here";
80  }
81  }
82 
83  /************************************************
84  template classes (history)
85  *************************************************/
86 
87  template <parameters my_param, class PayloadType>
88  class BeamSpot_history : public cond::payloadInspector::HistoryPlot<PayloadType, std::pair<double, double> > {
89  public:
91  : cond::payloadInspector::HistoryPlot<PayloadType, std::pair<double, double> >(
92  getStringFromParamEnum(my_param) + " vs run number", getStringFromParamEnum(my_param)) {}
93 
94  std::pair<double, double> getFromPayload(PayloadType& payload) override {
95  auto ret = std::make_pair<double, double>(-9999., -9999.);
96 
97  switch (my_param) {
98  case X:
99  return std::make_pair<double, double>(payload.GetX(), payload.GetXError());
100  case Y:
101  return std::make_pair<double, double>(payload.GetY(), payload.GetYError());
102  case Z:
103  return std::make_pair<double, double>(payload.GetZ(), payload.GetZError());
104  case sigmaX:
105  return std::make_pair<double, double>(payload.GetBeamWidthX(), payload.GetBeamWidthXError());
106  case sigmaY:
107  return std::make_pair<double, double>(payload.GetBeamWidthY(), payload.GetBeamWidthYError());
108  case sigmaZ:
109  return std::make_pair<double, double>(payload.GetSigmaZ(), payload.GetSigmaZError());
110  case dxdz:
111  return std::make_pair<double, double>(payload.Getdxdz(), payload.GetdxdzError());
112  case dydz:
113  return std::make_pair<double, double>(payload.Getdydz(), payload.GetdydzError());
114  case END_OF_TYPES:
115  return ret;
116  default:
117  return ret;
118  }
119  }
120  };
121 
122  /************************************************
123  template classes (run history)
124  *************************************************/
125 
126  template <parameters my_param, class PayloadType>
127  class BeamSpot_runhistory : public cond::payloadInspector::RunHistoryPlot<PayloadType, std::pair<double, double> > {
128  public:
130  : cond::payloadInspector::RunHistoryPlot<PayloadType, std::pair<double, double> >(
131  getStringFromParamEnum(my_param) + " vs run number", getStringFromParamEnum(my_param)) {}
132 
133  std::pair<double, double> getFromPayload(PayloadType& payload) override {
134  auto ret = std::make_pair<double, double>(-9999., -9999.);
135 
136  switch (my_param) {
137  case X:
138  return std::make_pair<double, double>(payload.GetX(), payload.GetXError());
139  case Y:
140  return std::make_pair<double, double>(payload.GetY(), payload.GetYError());
141  case Z:
142  return std::make_pair<double, double>(payload.GetZ(), payload.GetZError());
143  case sigmaX:
144  return std::make_pair<double, double>(payload.GetBeamWidthX(), payload.GetBeamWidthXError());
145  case sigmaY:
146  return std::make_pair<double, double>(payload.GetBeamWidthY(), payload.GetBeamWidthYError());
147  case sigmaZ:
148  return std::make_pair<double, double>(payload.GetSigmaZ(), payload.GetSigmaZError());
149  case dxdz:
150  return std::make_pair<double, double>(payload.Getdxdz(), payload.GetdxdzError());
151  case dydz:
152  return std::make_pair<double, double>(payload.Getdydz(), payload.GetdydzError());
153  case END_OF_TYPES:
154  return ret;
155  default:
156  return ret;
157  }
158  }
159  };
160 
161  /************************************************
162  template classes (time history)
163  *************************************************/
164 
165  template <parameters my_param, class PayloadType>
166  class BeamSpot_timehistory : public cond::payloadInspector::TimeHistoryPlot<PayloadType, std::pair<double, double> > {
167  public:
169  : cond::payloadInspector::TimeHistoryPlot<PayloadType, std::pair<double, double> >(
170  getStringFromParamEnum(my_param) + " vs time", getStringFromParamEnum(my_param)) {}
171 
172  std::pair<double, double> getFromPayload(PayloadType& payload) override {
173  auto ret = std::make_pair<double, double>(-9999., -9999.);
174 
175  switch (my_param) {
176  case X:
177  return std::make_pair<double, double>(payload.GetX(), payload.GetXError());
178  case Y:
179  return std::make_pair<double, double>(payload.GetY(), payload.GetYError());
180  case Z:
181  return std::make_pair<double, double>(payload.GetZ(), payload.GetZError());
182  case sigmaX:
183  return std::make_pair<double, double>(payload.GetBeamWidthX(), payload.GetBeamWidthXError());
184  case sigmaY:
185  return std::make_pair<double, double>(payload.GetBeamWidthY(), payload.GetBeamWidthYError());
186  case sigmaZ:
187  return std::make_pair<double, double>(payload.GetSigmaZ(), payload.GetSigmaZError());
188  case dxdz:
189  return std::make_pair<double, double>(payload.Getdxdz(), payload.GetdxdzError());
190  case dydz:
191  return std::make_pair<double, double>(payload.Getdydz(), payload.GetdydzError());
192  case END_OF_TYPES:
193  return ret;
194  default:
195  return ret;
196  }
197  }
198  };
199 
200  /************************************************
201  X-Y correlation plot
202  *************************************************/
203  template <class PayloadType>
204  class xyCorrelation : public cond::payloadInspector::ScatterPlot<PayloadType, double, double> {
205  public:
206  xyCorrelation() : cond::payloadInspector::ScatterPlot<PayloadType, double, double>("BeamSpot x vs y", "x", "y") {}
207 
208  std::tuple<double, double> getFromPayload(PayloadType& payload) override {
209  return std::make_tuple(payload.GetX(), payload.GetY());
210  }
211  };
212 
213  /************************************************
214  Display of Beam Spot parameters
215  *************************************************/
216  template <class PayloadType>
217  class DisplayParameters : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
218  public:
220  : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
221  "Display of BeamSpot parameters") {
222  if constexpr (std::is_same_v<PayloadType, BeamSpotOnlineObjects>) {
223  isOnline_ = true;
224  } else {
225  isOnline_ = false;
226  }
227  }
228 
229  bool fill() override {
230  auto tag = cond::payloadInspector::PlotBase::getTag<0>();
231  auto tagname = tag.name;
232  auto iov = tag.iovs.front();
233 
234  gStyle->SetHistMinimumZero(kTRUE);
235 
236  m_payload = this->fetchPayload(std::get<1>(iov));
237 
238  TCanvas canvas("Beam Spot Parameters Summary", "BeamSpot Parameters summary", isOnline_ ? 1500 : 1000, 1000);
239  if (isOnline_) {
240  canvas.Divide(2, 1);
241  }
242  canvas.cd(1);
243 
244  canvas.cd(1)->SetTopMargin(0.05);
245  canvas.cd(1)->SetBottomMargin(0.06);
246  canvas.cd(1)->SetLeftMargin(0.15);
247  canvas.cd(1)->SetRightMargin(0.01);
248  canvas.cd(1)->Modified();
249  canvas.cd(1)->SetGrid();
250 
251  auto h2_BSParameters = std::make_unique<TH2F>("Parameters", "", 2, 0.0, 2.0, 8, 0, 8.);
252  h2_BSParameters->SetStats(false);
253 
254  std::function<double(parameters, bool)> cutFunctor = [this](parameters my_param, bool isError) {
255  double ret(-999.);
256  if (!isError) {
257  switch (my_param) {
258  case X:
259  return m_payload->GetX();
260  case Y:
261  return m_payload->GetY();
262  case Z:
263  return m_payload->GetZ();
264  case sigmaX:
265  return m_payload->GetBeamWidthX();
266  case sigmaY:
267  return m_payload->GetBeamWidthY();
268  case sigmaZ:
269  return m_payload->GetSigmaZ();
270  case dxdz:
271  return m_payload->Getdxdz();
272  case dydz:
273  return m_payload->Getdydz();
274  case END_OF_TYPES:
275  return ret;
276  default:
277  return ret;
278  }
279  } else {
280  switch (my_param) {
281  case X:
282  return m_payload->GetXError();
283  case Y:
284  return m_payload->GetYError();
285  case Z:
286  return m_payload->GetZError();
287  case sigmaX:
288  return m_payload->GetBeamWidthXError();
289  case sigmaY:
290  return m_payload->GetBeamWidthYError();
291  case sigmaZ:
292  return m_payload->GetSigmaZError();
293  case dxdz:
294  return m_payload->GetdxdzError();
295  case dydz:
296  return m_payload->GetdydzError();
297  case END_OF_TYPES:
298  return ret;
299  default:
300  return ret;
301  }
302  }
303  };
304 
305  h2_BSParameters->GetXaxis()->SetBinLabel(1, "Value");
306  h2_BSParameters->GetXaxis()->SetBinLabel(2, "Error");
307 
308  unsigned int yBin = 8;
309  for (int foo = parameters::X; foo <= parameters::dydz; foo++) {
310  parameters param = static_cast<parameters>(foo);
311  std::string theLabel = getStringFromTypeEnum(param);
312  h2_BSParameters->GetYaxis()->SetBinLabel(yBin, theLabel.c_str());
313  h2_BSParameters->SetBinContent(1, yBin, cutFunctor(param, false));
314  h2_BSParameters->SetBinContent(2, yBin, cutFunctor(param, true));
315  yBin--;
316  }
317 
318  h2_BSParameters->GetXaxis()->LabelsOption("h");
319  h2_BSParameters->GetYaxis()->SetLabelSize(0.05);
320  h2_BSParameters->GetXaxis()->SetLabelSize(0.05);
321  h2_BSParameters->SetMarkerSize(1.5);
322  h2_BSParameters->Draw("TEXT");
323 
324  auto ltx = TLatex();
325  ltx.SetTextFont(62);
326  if (isOnline_) {
327  ltx.SetTextSize(0.030);
328  } else {
329  ltx.SetTextSize(0.025);
330  }
331  ltx.SetTextAlign(11);
332 
333  auto runLS = BeamSpotPI::unpack(std::get<0>(iov));
334 
335  ltx.DrawLatexNDC(
336  gPad->GetLeftMargin(),
337  1 - gPad->GetTopMargin() + 0.01,
338  (tagname + " IOV: #color[4]{" + std::to_string(runLS.first) + "," + std::to_string(runLS.second) + "}")
339  .c_str());
340 
341  if (isOnline_) {
342  canvas.cd(2);
343  canvas.cd(2)->SetTopMargin(0.05);
344  canvas.cd(2)->SetBottomMargin(0.06);
345  canvas.cd(2)->SetLeftMargin(0.25);
346  canvas.cd(2)->SetRightMargin(0.01);
347  canvas.cd(2)->Modified();
348  canvas.cd(2)->SetGrid();
349 
350  auto extras = fillTheExtraHistogram();
351  if (extras) {
352  for (int bin = 1; bin <= extras->GetNbinsY(); bin++) {
353  edm::LogVerbatim("BeamSpotPayloadInspectorHelper")
354  << extras->GetYaxis()->GetBinLabel(bin) << ": " << extras->GetBinContent(1, bin) << "\n";
355  }
356  }
357  extras->Draw("TEXT");
358 
359  ltx.DrawLatexNDC(
360  gPad->GetLeftMargin(),
361  1 - gPad->GetTopMargin() + 0.01,
362  (tagname + " IOV: #color[4]{" + std::to_string(runLS.first) + "," + std::to_string(runLS.second) + "}")
363  .c_str());
364 
366  canvas.SaveAs(fileName.c_str());
367 
368  return true;
369  } else {
371  canvas.SaveAs(fileName.c_str());
372 
373  return true;
374  }
375  }
376 
377  public:
378  virtual std::shared_ptr<TH2F> fillTheExtraHistogram() const { return nullptr; }
379 
380  protected:
381  bool isOnline_;
382  std::shared_ptr<PayloadType> m_payload;
383 
384  /************************************************/
385  virtual std::string getStringFromTypeEnum(const parameters& parameter) const {
386  switch (parameter) {
387  case X:
388  return "X [cm]";
389  case Y:
390  return "Y [cm]";
391  case Z:
392  return "Z [cm]";
393  case sigmaX:
394  return "#sigma_{X} [cm]";
395  case sigmaY:
396  return "#sigma_{Y} [cm]";
397  case sigmaZ:
398  return "#sigma_{Z} [cm]";
399  case dxdz:
400  return "#frac{dX}{dZ} [rad]";
401  case dydz:
402  return "#frac{dY}{dZ} [rad]";
403  default:
404  return "should never be here";
405  }
406  }
407  };
408 } // namespace BeamSpotPI
409 
410 #endif
Log< level::Info, true > LogVerbatim
virtual std::string getStringFromTypeEnum(const parameters &parameter) const
tuple ret
prodAgent to be discontinued
std::pair< unsigned int, unsigned int > unpack(cond::Time_t since)
tuple yBin
Definition: cuy.py:892
#define X(str)
Definition: MuonsGrabber.cc:38
virtual std::shared_ptr< TH2F > fillTheExtraHistogram() const
const Time_t kLowMask(0xFFFFFFFF)
std::tuple< double, double > getFromPayload(PayloadType &payload) override
def canvas
Definition: svgfig.py:482
ScatterPlot(const std::string &title, const std::string &xLabel, const std::string &yLabel)
unsigned long long Time_t
Definition: Time.h:14
std::pair< double, double > getFromPayload(PayloadType &payload) override
list lumi
Definition: dqmdumpme.py:53
HistoryPlot(const std::string &title, const std::string &yLabel)
std::pair< double, double > getFromPayload(PayloadType &payload) override
std::pair< double, double > getFromPayload(PayloadType &payload) override
std::shared_ptr< PayloadType > m_payload
std::string getStringFromParamEnum(const parameters &parameter)
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)