CMS 3D CMS Logo

BeamMonitor.cc
Go to the documentation of this file.
1 /*
2  * \file BeamMonitor.cc
3  * \author Geng-yuan Jeng/UC Riverside
4  * Francisco Yumiceva/FNAL
5  */
6 
7 /*
8 The code has been modified for running average
9 mode, and it gives results for the last NLS which is
10 configurable.
11 Sushil S. Chauhan /UCDavis
12 Evan Friis /UCDavis
13 The last tag for working versions without this change is
14 V00-03-25
15 */
16 
32 #include <numeric>
33 #include <cmath>
34 #include <memory>
35 #include <TMath.h>
36 #include <iostream>
37 #include <TStyle.h>
38 #include <ctime>
39 
40 using namespace std;
41 using namespace edm;
42 
43 void BeamMonitor::formatFitTime(char* ts, const time_t& t) {
44  //constexpr int CET(+1);
45  constexpr int CEST(+2);
46 
47  //tm * ptm;
48  //ptm = gmtime ( &t );
49  //int year = ptm->tm_year;
50 
51  //get correct year from ctime
52  time_t currentTime;
53  struct tm* localTime;
54  time(&currentTime); // Get the current time
55  localTime = localtime(&currentTime); // Convert the current time to the local time
56  int year = localTime->tm_year + 1900;
57 
58  tm* ptm;
59  ptm = gmtime(&t);
60 
61  //check if year is ok
62  if (year <= 37)
63  year += 2000;
64  if (year >= 70 && year <= 137)
65  year += 1900;
66 
67  if (year < 1995) {
68  edm::LogError("BadTimeStamp") << "year reported is " << year << " !!" << std::endl;
69  //year = 2015; //overwritten later by BeamFitter.cc for fits but needed here for TH1
70  //edm::LogError("BadTimeStamp") << "Resetting to " <<year<<std::endl;
71  }
72  sprintf(ts,
73  "%4d-%02d-%02d %02d:%02d:%02d",
74  year,
75  ptm->tm_mon + 1,
76  ptm->tm_mday,
77  (ptm->tm_hour + CEST) % 24,
78  ptm->tm_min,
79  ptm->tm_sec);
80 
81 #ifdef STRIP_TRAILING_BLANKS_IN_TIMEZONE
82  unsigned int b = strlen(ts);
83  while (ts[--b] == ' ') {
84  ts[b] = 0;
85  }
86 #endif
87 }
88 
89 static constexpr int buffTime = 23;
90 
91 //
92 // constructors and destructor
93 //
95  : dxBin_(ps.getParameter<int>("dxBin")),
96  dxMin_(ps.getParameter<double>("dxMin")),
97  dxMax_(ps.getParameter<double>("dxMax")),
98 
99  vxBin_(ps.getParameter<int>("vxBin")),
100  vxMin_(ps.getParameter<double>("vxMin")),
101  vxMax_(ps.getParameter<double>("vxMax")),
102 
103  phiBin_(ps.getParameter<int>("phiBin")),
104  phiMin_(ps.getParameter<double>("phiMin")),
105  phiMax_(ps.getParameter<double>("phiMax")),
106 
107  dzBin_(ps.getParameter<int>("dzBin")),
108  dzMin_(ps.getParameter<double>("dzMin")),
109  dzMax_(ps.getParameter<double>("dzMax")),
110 
111  countEvt_(0),
112  countLumi_(0),
113  nthBSTrk_(0),
114  nFitElements_(3),
115  resetHistos_(false),
116  StartAverage_(false),
117  firstAverageFit_(0),
118  countGapLumi_(0) {
119  monitorName_ = ps.getUntrackedParameter<string>("monitorName", "YourSubsystemName");
120  bsSrc_ = consumes<reco::BeamSpot>(ps.getUntrackedParameter<InputTag>("beamSpot"));
121  tracksLabel_ = consumes<reco::TrackCollection>(
122  ps.getParameter<ParameterSet>("BeamFitter").getUntrackedParameter<InputTag>("TrackCollection"));
123  pvSrc_ = consumes<reco::VertexCollection>(ps.getUntrackedParameter<InputTag>("primaryVertex"));
124  hltSrc_ = consumes<TriggerResults>(ps.getParameter<InputTag>("hltResults"));
125  intervalInSec_ = ps.getUntrackedParameter<int>("timeInterval", 920); //40 LS X 23"
126  fitNLumi_ = ps.getUntrackedParameter<int>("fitEveryNLumi", -1);
127  resetFitNLumi_ = ps.getUntrackedParameter<int>("resetEveryNLumi", -1);
128  fitPVNLumi_ = ps.getUntrackedParameter<int>("fitPVEveryNLumi", -1);
129  resetPVNLumi_ = ps.getUntrackedParameter<int>("resetPVEveryNLumi", -1);
130  deltaSigCut_ = ps.getUntrackedParameter<double>("deltaSignificanceCut", 15);
131  debug_ = ps.getUntrackedParameter<bool>("Debug");
132  onlineMode_ = ps.getUntrackedParameter<bool>("OnlineMode");
133  jetTrigger_ = ps.getUntrackedParameter<std::vector<std::string> >("jetTrigger");
134  min_Ntrks_ = ps.getParameter<ParameterSet>("BeamFitter").getUntrackedParameter<int>("MinimumInputTracks");
135  maxZ_ = ps.getParameter<ParameterSet>("BeamFitter").getUntrackedParameter<double>("MaximumZ");
136  minNrVertices_ = ps.getParameter<ParameterSet>("PVFitter").getUntrackedParameter<unsigned int>("minNrVerticesForFit");
137  minVtxNdf_ = ps.getParameter<ParameterSet>("PVFitter").getUntrackedParameter<double>("minVertexNdf");
138  minVtxWgt_ = ps.getParameter<ParameterSet>("PVFitter").getUntrackedParameter<double>("minVertexMeanWeight");
139 
140  if (!monitorName_.empty())
141  monitorName_ = monitorName_ + "/";
142 
143  theBeamFitter = std::make_unique<BeamFitter>(ps, consumesCollector());
144  theBeamFitter->resetTrkVector();
145  theBeamFitter->resetLSRange();
146  theBeamFitter->resetRefTime();
147  theBeamFitter->resetPVFitter();
148 
149  if (fitNLumi_ <= 0)
150  fitNLumi_ = 1;
152  refBStime[0] = refBStime[1] = refPVtime[0] = refPVtime[1] = 0;
153  maxZ_ = std::fabs(maxZ_);
154  lastlumi_ = 0;
155  nextlumi_ = 0;
156  processed_ = false;
157 }
158 
159 //--------------------------------------------------------
160 namespace {
161  /*The order of the enums is identical to the order in which
162  MonitorElements are added to hs
163  */
164  enum Hists {
165  k_x0_lumi,
166  k_x0_lumi_all,
167  k_y0_lumi,
168  k_y0_lumi_all,
169  k_z0_lumi,
170  k_z0_lumi_all,
171  k_sigmaX0_lumi,
172  k_sigmaX0_lumi_all,
173  k_sigmaY0_lumi,
174  k_sigmaY0_lumi_all,
175  k_sigmaZ0_lumi,
176  k_sigmaZ0_lumi_all,
177  k_x0_time,
178  k_x0_time_all,
179  k_y0_time,
180  k_y0_time_all,
181  k_z0_time,
182  k_z0_time_all,
183  k_sigmaX0_time,
184  k_sigmaX0_time_all,
185  k_sigmaY0_time,
186  k_sigmaY0_time_all,
187  k_sigmaZ0_time,
188  k_sigmaZ0_time_all,
189  k_PVx_lumi,
190  k_PVx_lumi_all,
191  k_PVy_lumi,
192  k_PVy_lumi_all,
193  k_PVz_lumi,
194  k_PVz_lumi_all,
195  k_PVx_time,
196  k_PVx_time_all,
197  k_PVy_time,
198  k_PVy_time_all,
199  k_PVz_time,
200  k_PVz_time_all,
201  kNumHists
202  };
203 } // namespace
204 
205 void BeamMonitor::bookHistograms(DQMStore::IBooker& iBooker, edm::Run const& iRun, edm::EventSetup const& iSetup) {
206  frun = iRun.run();
207  ftimestamp = iRun.beginTime().value();
208  tmpTime = ftimestamp >> 32;
210  char eventTime[64];
211  formatFitTime(eventTime, tmpTime);
212  edm::LogInfo("BeamMonitor") << "TimeOffset = " << eventTime << std::endl;
213  TDatime da(eventTime);
214  if (debug_) {
215  edm::LogInfo("BeamMonitor") << "TimeOffset = ";
216  da.Print();
217  }
218  auto daTime = da.Convert(kTRUE);
219 
220  // book some histograms here
221 
222  // create and cd into new folder
223  iBooker.setCurrentFolder(monitorName_ + "Fit");
224 
225  h_nTrk_lumi = iBooker.book1D("nTrk_lumi", "Num. of selected tracks vs lumi (Fit)", 20, 0.5, 20.5);
226  h_nTrk_lumi->setAxisTitle("Lumisection", 1);
227  h_nTrk_lumi->setAxisTitle("Num of Tracks for Fit", 2);
228 
229  //store vtx vs lumi for monitoring why fits fail
230  h_nVtx_lumi = iBooker.book1D("nVtx_lumi", "Num. of selected Vtx vs lumi (Fit)", 20, 0.5, 20.5);
231  h_nVtx_lumi->setAxisTitle("Lumisection", 1);
232  h_nVtx_lumi->setAxisTitle("Num of Vtx for Fit", 2);
233 
234  h_nVtx_lumi_all = iBooker.book1D("nVtx_lumi_all", "Num. of selected Vtx vs lumi (Fit) all", 20, 0.5, 20.5);
235  h_nVtx_lumi_all->getTH1()->SetCanExtend(TH1::kAllAxes);
236  h_nVtx_lumi_all->setAxisTitle("Lumisection", 1);
237  h_nVtx_lumi_all->setAxisTitle("Num of Vtx for Fit", 2);
238 
239  h_d0_phi0 = iBooker.bookProfile(
240  "d0_phi0", "d_{0} vs. #phi_{0} (Selected Tracks)", phiBin_, phiMin_, phiMax_, dxBin_, dxMin_, dxMax_, "");
241  h_d0_phi0->setAxisTitle("#phi_{0} (rad)", 1);
242  h_d0_phi0->setAxisTitle("d_{0} (cm)", 2);
243 
244  h_vx_vy = iBooker.book2D(
245  "trk_vx_vy", "Vertex (PCA) position of selected tracks", vxBin_, vxMin_, vxMax_, vxBin_, vxMin_, vxMax_);
246  h_vx_vy->setOption("COLZ");
247  // h_vx_vy->getTH1()->SetCanExtend(TH1::kAllAxes);
248  h_vx_vy->setAxisTitle("x coordinate of input track at PCA (cm)", 1);
249  h_vx_vy->setAxisTitle("y coordinate of input track at PCA (cm)", 2);
250 
251  {
252  TDatime* da = new TDatime();
253  gStyle->SetTimeOffset(da->Convert(kTRUE));
254  }
255 
256  const int nvar_ = 6;
257  string coord[nvar_] = {"x", "y", "z", "sigmaX", "sigmaY", "sigmaZ"};
258  string label[nvar_] = {
259  "x_{0} (cm)", "y_{0} (cm)", "z_{0} (cm)", "#sigma_{X_{0}} (cm)", "#sigma_{Y_{0}} (cm)", "#sigma_{Z_{0}} (cm)"};
260 
261  hs.reserve(kNumHists);
262  for (int i = 0; i < 4; i++) {
263  iBooker.setCurrentFolder(monitorName_ + "Fit");
264  for (int ic = 0; ic < nvar_; ++ic) {
265  TString histName(coord[ic]);
266  TString histTitle(coord[ic]);
267  string ytitle(label[ic]);
268  string xtitle("");
269  string options("E1");
270  bool createHisto = true;
271  switch (i) {
272  case 1: // BS vs time
273  histName += "0_time";
274  xtitle = "Time [UTC]";
275  if (ic < 3)
276  histTitle += " coordinate of beam spot vs time (Fit)";
277  else
278  histTitle = histTitle.Insert(5, " ") + " of beam spot vs time (Fit)";
279  break;
280  case 2: // PV vs lumi
281  if (ic < 3) {
282  iBooker.setCurrentFolder(monitorName_ + "PrimaryVertex");
283  histName.Insert(0, "PV");
284  histName += "_lumi";
285  histTitle.Insert(0, "Avg. ");
286  histTitle += " position of primary vtx vs lumi";
287  xtitle = "Lumisection";
288  ytitle.insert(0, "PV");
289  ytitle += " #pm #sigma_{PV";
290  ytitle += coord[ic];
291  ytitle += "} (cm)";
292  } else
293  createHisto = false;
294  break;
295  case 3: // PV vs time
296  if (ic < 3) {
297  iBooker.setCurrentFolder(monitorName_ + "PrimaryVertex");
298  histName.Insert(0, "PV");
299  histName += "_time";
300  histTitle.Insert(0, "Avg. ");
301  histTitle += " position of primary vtx vs time";
302  xtitle = "Time [UTC]";
303  ytitle.insert(0, "PV");
304  ytitle += " #pm #sigma_{PV";
305  ytitle += coord[ic];
306  ytitle += "} (cm)";
307  } else
308  createHisto = false;
309  break;
310  default: // BS vs lumi
311  histName += "0_lumi";
312  xtitle = "Lumisection";
313  if (ic < 3)
314  histTitle += " coordinate of beam spot vs lumi (Fit)";
315  else
316  histTitle = histTitle.Insert(5, " ") + " of beam spot vs lumi (Fit)";
317  break;
318  }
319  if (createHisto) {
320  edm::LogInfo("BeamMonitor") << "hitsName = " << histName << "; histTitle = " << histTitle << std::endl;
321  auto tmpHs = iBooker.book1D(histName, histTitle, 40, 0.5, 40.5);
322  hs.push_back(tmpHs);
323  tmpHs->setAxisTitle(xtitle, 1);
324  tmpHs->setAxisTitle(ytitle, 2);
325  tmpHs->getTH1()->SetOption("E1");
326  if (histName.Contains("time")) {
327  //int nbins = (intervalInSec_/23 > 0 ? intervalInSec_/23 : 40);
328  tmpHs->getTH1()->SetBins(intervalInSec_, 0.5, intervalInSec_ + 0.5);
329  tmpHs->setAxisTimeDisplay(1);
330  tmpHs->setAxisTimeFormat("%H:%M:%S", 1);
331  tmpHs->getTH1()->GetXaxis()->SetTimeOffset(daTime);
332  }
333  histName += "_all";
334  histTitle += " all";
335  tmpHs = iBooker.book1D(histName, histTitle, 40, 0.5, 40.5);
336  hs.push_back(tmpHs);
337  tmpHs->getTH1()->SetCanExtend(TH1::kAllAxes);
338  tmpHs->setAxisTitle(xtitle, 1);
339  tmpHs->setAxisTitle(ytitle, 2);
340  tmpHs->getTH1()->SetOption("E1");
341  if (histName.Contains("time")) {
342  //int nbins = (intervalInSec_/23 > 0 ? intervalInSec_/23 : 40);
343  tmpHs->getTH1()->SetBins(intervalInSec_, 0.5, intervalInSec_ + 0.5);
344  tmpHs->setAxisTimeDisplay(1);
345  tmpHs->setAxisTimeFormat("%H:%M:%S", 1);
346  tmpHs->getTH1()->GetXaxis()->SetTimeOffset(daTime);
347  }
348  }
349  }
350  }
351  assert(hs.size() == kNumHists);
352  assert(0 == strcmp(hs[k_sigmaY0_time]->getTH1()->GetName(), "sigmaY0_time"));
353  assert(0 == strcmp(hs[k_PVz_lumi_all]->getTH1()->GetName(), "PVz_lumi_all"));
354 
355  iBooker.setCurrentFolder(monitorName_ + "Fit");
356 
357  h_trk_z0 = iBooker.book1D("trk_z0", "z_{0} of selected tracks", dzBin_, dzMin_, dzMax_);
358  h_trk_z0->setAxisTitle("z_{0} of selected tracks (cm)", 1);
359 
360  h_vx_dz = iBooker.bookProfile(
361  "vx_dz", "v_{x} vs. dz of selected tracks", dzBin_, dzMin_, dzMax_, dxBin_, dxMin_, dxMax_, "");
362  h_vx_dz->setAxisTitle("dz (cm)", 1);
363  h_vx_dz->setAxisTitle("x coordinate of input track at PCA (cm)", 2);
364 
365  h_vy_dz = iBooker.bookProfile(
366  "vy_dz", "v_{y} vs. dz of selected tracks", dzBin_, dzMin_, dzMax_, dxBin_, dxMin_, dxMax_, "");
367  h_vy_dz->setAxisTitle("dz (cm)", 1);
368  h_vy_dz->setAxisTitle("y coordinate of input track at PCA (cm)", 2);
369 
370  h_x0 = iBooker.book1D("BeamMonitorFeedBack_x0", "x coordinate of beam spot (Fit)", 100, -0.01, 0.01);
371  h_x0->setAxisTitle("x_{0} (cm)", 1);
372  h_x0->getTH1()->SetCanExtend(TH1::kAllAxes);
373 
374  h_y0 = iBooker.book1D("BeamMonitorFeedBack_y0", "y coordinate of beam spot (Fit)", 100, -0.01, 0.01);
375  h_y0->setAxisTitle("y_{0} (cm)", 1);
376  h_y0->getTH1()->SetCanExtend(TH1::kAllAxes);
377 
378  h_z0 = iBooker.book1D("BeamMonitorFeedBack_z0", "z coordinate of beam spot (Fit)", dzBin_, dzMin_, dzMax_);
379  h_z0->setAxisTitle("z_{0} (cm)", 1);
380  h_z0->getTH1()->SetCanExtend(TH1::kAllAxes);
381 
382  h_sigmaX0 = iBooker.book1D("BeamMonitorFeedBack_sigmaX0", "sigma x0 of beam spot (Fit)", 100, 0, 0.05);
383  h_sigmaX0->setAxisTitle("#sigma_{X_{0}} (cm)", 1);
384  h_sigmaX0->getTH1()->SetCanExtend(TH1::kAllAxes);
385 
386  h_sigmaY0 = iBooker.book1D("BeamMonitorFeedBack_sigmaY0", "sigma y0 of beam spot (Fit)", 100, 0, 0.05);
387  h_sigmaY0->setAxisTitle("#sigma_{Y_{0}} (cm)", 1);
388  h_sigmaY0->getTH1()->SetCanExtend(TH1::kAllAxes);
389 
390  h_sigmaZ0 = iBooker.book1D("BeamMonitorFeedBack_sigmaZ0", "sigma z0 of beam spot (Fit)", 100, 0, 10);
391  h_sigmaZ0->setAxisTitle("#sigma_{Z_{0}} (cm)", 1);
392  h_sigmaZ0->getTH1()->SetCanExtend(TH1::kAllAxes);
393 
394  // Histograms of all reco tracks (without cuts):
395  h_trkPt = iBooker.book1D("trkPt", "p_{T} of all reco'd tracks (no selection)", 200, 0., 50.);
396  h_trkPt->setAxisTitle("p_{T} (GeV/c)", 1);
397 
398  h_trkVz = iBooker.book1D("trkVz", "Z coordinate of PCA of all reco'd tracks (no selection)", dzBin_, dzMin_, dzMax_);
399  h_trkVz->setAxisTitle("V_{Z} (cm)", 1);
400 
401  cutFlowTable = iBooker.book1D("cutFlowTable", "Cut flow table of track selection", 9, 0, 9);
402 
403  // Results of previous good fit:
404  fitResults = iBooker.book2D("fitResults", "Results of previous good beam fit", 2, 0, 2, 8, 0, 8);
405  fitResults->setAxisTitle("Fitted Beam Spot (cm)", 1);
406  fitResults->setBinLabel(8, "x_{0}", 2);
407  fitResults->setBinLabel(7, "y_{0}", 2);
408  fitResults->setBinLabel(6, "z_{0}", 2);
409  fitResults->setBinLabel(5, "#sigma_{Z}", 2);
410  fitResults->setBinLabel(4, "#frac{dx}{dz} (rad)", 2);
411  fitResults->setBinLabel(3, "#frac{dy}{dz} (rad)", 2);
412  fitResults->setBinLabel(2, "#sigma_{X}", 2);
413  fitResults->setBinLabel(1, "#sigma_{Y}", 2);
414  fitResults->setBinLabel(1, "Mean", 1);
415  fitResults->setBinLabel(2, "Stat. Error", 1);
416  fitResults->getTH1()->SetOption("text");
417 
418  // Histos of PrimaryVertices:
419  iBooker.setCurrentFolder(monitorName_ + "PrimaryVertex");
420 
421  h_nVtx = iBooker.book1D("vtxNbr", "Reconstructed Vertices(non-fake) in all Event", 60, -0.5, 59.5);
422  h_nVtx->setAxisTitle("Num. of reco. vertices", 1);
423 
424  //For one Trigger only
425  h_nVtx_st = iBooker.book1D("vtxNbr_SelectedTriggers", "Reconstructed Vertices(non-fake) in Events", 60, -0.5, 59.5);
426  //h_nVtx_st->setAxisTitle("Num. of reco. vertices for Un-Prescaled Jet Trigger",1);
427 
428  // Monitor only the PV with highest sum pt of assoc. trks:
429  h_PVx[0] = iBooker.book1D("PVX", "x coordinate of Primary Vtx", 50, -0.01, 0.01);
430  h_PVx[0]->setAxisTitle("PVx (cm)", 1);
431  h_PVx[0]->getTH1()->SetCanExtend(TH1::kAllAxes);
432 
433  h_PVy[0] = iBooker.book1D("PVY", "y coordinate of Primary Vtx", 50, -0.01, 0.01);
434  h_PVy[0]->setAxisTitle("PVy (cm)", 1);
435  h_PVy[0]->getTH1()->SetCanExtend(TH1::kAllAxes);
436 
437  h_PVz[0] = iBooker.book1D("PVZ", "z coordinate of Primary Vtx", dzBin_, dzMin_, dzMax_);
438  h_PVz[0]->setAxisTitle("PVz (cm)", 1);
439 
440  h_PVx[1] = iBooker.book1D("PVXFit", "x coordinate of Primary Vtx (Last Fit)", 50, -0.01, 0.01);
441  h_PVx[1]->setAxisTitle("PVx (cm)", 1);
442  h_PVx[1]->getTH1()->SetCanExtend(TH1::kAllAxes);
443 
444  h_PVy[1] = iBooker.book1D("PVYFit", "y coordinate of Primary Vtx (Last Fit)", 50, -0.01, 0.01);
445  h_PVy[1]->setAxisTitle("PVy (cm)", 1);
446  h_PVy[1]->getTH1()->SetCanExtend(TH1::kAllAxes);
447 
448  h_PVz[1] = iBooker.book1D("PVZFit", "z coordinate of Primary Vtx (Last Fit)", dzBin_, dzMin_, dzMax_);
449  h_PVz[1]->setAxisTitle("PVz (cm)", 1);
450 
451  h_PVxz = iBooker.bookProfile("PVxz", "PVx vs. PVz", dzBin_ / 2, dzMin_, dzMax_, dxBin_ / 2, dxMin_, dxMax_, "");
452  h_PVxz->setAxisTitle("PVz (cm)", 1);
453  h_PVxz->setAxisTitle("PVx (cm)", 2);
454 
455  h_PVyz = iBooker.bookProfile("PVyz", "PVy vs. PVz", dzBin_ / 2, dzMin_, dzMax_, dxBin_ / 2, dxMin_, dxMax_, "");
456  h_PVyz->setAxisTitle("PVz (cm)", 1);
457  h_PVyz->setAxisTitle("PVy (cm)", 2);
458 
459  // Results of previous good fit:
460  pvResults = iBooker.book2D("pvResults", "Results of fitting Primary Vertices", 2, 0, 2, 6, 0, 6);
461  pvResults->setAxisTitle("Fitted Primary Vertex (cm)", 1);
462  pvResults->setBinLabel(6, "PVx", 2);
463  pvResults->setBinLabel(5, "PVy", 2);
464  pvResults->setBinLabel(4, "PVz", 2);
465  pvResults->setBinLabel(3, "#sigma_{X}", 2);
466  pvResults->setBinLabel(2, "#sigma_{Y}", 2);
467  pvResults->setBinLabel(1, "#sigma_{Z}", 2);
468  pvResults->setBinLabel(1, "Mean", 1);
469  pvResults->setBinLabel(2, "Stat. Error", 1);
470  pvResults->getTH1()->SetOption("text");
471 
472  // Summary plots:
473  iBooker.setCurrentFolder(monitorName_ + "EventInfo");
474 
475  reportSummary = iBooker.bookFloat("reportSummary");
476  if (reportSummary)
477  reportSummary->Fill(std::numeric_limits<double>::quiet_NaN());
478 
479  char histo[20];
480  iBooker.setCurrentFolder(monitorName_ + "EventInfo/reportSummaryContents");
481  for (int n = 0; n < nFitElements_; n++) {
482  switch (n) {
483  case 0:
484  sprintf(histo, "x0_status");
485  break;
486  case 1:
487  sprintf(histo, "y0_status");
488  break;
489  case 2:
490  sprintf(histo, "z0_status");
491  break;
492  }
493  reportSummaryContents[n] = iBooker.bookFloat(histo);
494  }
495 
496  for (int i = 0; i < nFitElements_; i++) {
497  summaryContent_[i] = 0.;
498  reportSummaryContents[i]->Fill(std::numeric_limits<double>::quiet_NaN());
499  }
500 
501  iBooker.setCurrentFolder(monitorName_ + "EventInfo");
502 
503  reportSummaryMap = iBooker.book2D("reportSummaryMap", "Beam Spot Summary Map", 1, 0, 1, 3, 0, 3);
505  reportSummaryMap->setAxisTitle("Fitted Beam Spot", 2);
506  reportSummaryMap->setBinLabel(1, " ", 1);
507  reportSummaryMap->setBinLabel(1, "x_{0}", 2);
508  reportSummaryMap->setBinLabel(2, "y_{0}", 2);
509  reportSummaryMap->setBinLabel(3, "z_{0}", 2);
510  for (int i = 0; i < nFitElements_; i++) {
511  reportSummaryMap->setBinContent(1, i + 1, -1.);
512  }
513 }
514 
515 //--------------------------------------------------------
516 void BeamMonitor::beginLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
517  int nthlumi = lumiSeg.luminosityBlock();
518  const edm::TimeValue_t fbegintimestamp = lumiSeg.beginTime().value();
519  const std::time_t ftmptime = fbegintimestamp >> 32;
520 
521  if (countLumi_ == 0 && (!processed_)) {
523  refBStime[0] = refPVtime[0] = ftmptime;
524  mapBeginBSLS[countLumi_] = nthlumi;
525  mapBeginPVLS[countLumi_] = nthlumi;
526  mapBeginBSTime[countLumi_] = ftmptime;
527  mapBeginPVTime[countLumi_] = ftmptime;
528  } //for the first record
529 
530  if (nthlumi > nextlumi_) {
531  if (processed_) {
532  countLumi_++;
533  //store here them will need when we remove the first one of Last N LS
534  mapBeginBSLS[countLumi_] = nthlumi;
535  mapBeginPVLS[countLumi_] = nthlumi;
536  mapBeginBSTime[countLumi_] = ftmptime;
537  mapBeginPVTime[countLumi_] = ftmptime;
538  } //processed passed but not the first lumi
539  if ((!processed_) && countLumi_ != 0) {
540  mapBeginBSLS[countLumi_] = nthlumi;
541  mapBeginPVLS[countLumi_] = nthlumi;
542  mapBeginBSTime[countLumi_] = ftmptime;
543  mapBeginPVTime[countLumi_] = ftmptime;
544  } //processed fails for last lumi
545  } //nthLumi > nextlumi
546 
547  if (StartAverage_) {
548  //Just Make sure it get rest
549  refBStime[0] = 0;
550  refPVtime[0] = 0;
551  beginLumiOfPVFit_ = 0;
552  beginLumiOfBSFit_ = 0;
553 
554  if (debug_)
555  edm::LogInfo("BeamMonitor") << " beginLuminosityBlock: Size of mapBeginBSLS before = " << mapBeginBSLS.size()
556  << endl;
557  if (nthlumi >
558  nextlumi_) { //this make sure that it does not take into account this lumi for fitting and only look forward for new lumi
559  //as countLumi also remains the same so map value get overwritten once return to normal running.
560  //even if few LS are misssing and DQM module do not sees them then it catchs up again
561  map<int, int>::iterator itbs = mapBeginBSLS.begin();
562  map<int, int>::iterator itpv = mapBeginPVLS.begin();
563  map<int, std::time_t>::iterator itbstime = mapBeginBSTime.begin();
564  map<int, std::time_t>::iterator itpvtime = mapBeginPVTime.begin();
565 
566  if (processed_) { // otherwise if false then LS range of fit get messed up because we don't remove trk/pvs but we remove LS begin value . This prevent it as it happened if LS is there but no event are processed for some reason
567  mapBeginBSLS.erase(itbs);
568  mapBeginPVLS.erase(itpv);
569  mapBeginBSTime.erase(itbstime);
570  mapBeginPVTime.erase(itpvtime);
571  }
572  /*//not sure if want this or not ??
573  map<int, int>::iterator itgapb=mapBeginBSLS.begin();
574  map<int, int>::iterator itgape=mapBeginBSLS.end(); itgape--;
575  countGapLumi_ = ( (itgape->second) - (itgapb->second) );
576  //if we see Gap more than then 2*resetNFitLumi !!!!!!!
577  //for example if 10-15 is fitted and if 16-25 are missing then we next fit will be for range 11-26 but BS can change in between
578  // so better start as fresh and reset everything like starting in the begining!
579  if(countGapLumi_ >= 2*resetFitNLumi_){RestartFitting(); mapBeginBSLS[countLumi_] = nthlumi;}
580  */
581  }
582 
583  if (debug_)
584  edm::LogInfo("BeamMonitor") << " beginLuminosityBlock:: Size of mapBeginBSLS After = " << mapBeginBSLS.size()
585  << endl;
586 
587  map<int, int>::iterator bbs = mapBeginBSLS.begin();
588  map<int, int>::iterator bpv = mapBeginPVLS.begin();
589  map<int, std::time_t>::iterator bbst = mapBeginBSTime.begin();
590  map<int, std::time_t>::iterator bpvt = mapBeginPVTime.begin();
591 
592  if (beginLumiOfPVFit_ == 0)
593  beginLumiOfPVFit_ = bpv->second; //new begin time after removing the LS
594  if (beginLumiOfBSFit_ == 0)
595  beginLumiOfBSFit_ = bbs->second;
596  if (refBStime[0] == 0)
597  refBStime[0] = bbst->second;
598  if (refPVtime[0] == 0)
599  refPVtime[0] = bpvt->second;
600 
601  } //same logic for average fit as above commented line
602 
603  map<int, std::time_t>::iterator nbbst = mapBeginBSTime.begin();
604  map<int, std::time_t>::iterator nbpvt = mapBeginPVTime.begin();
605 
606  if (onlineMode_ && (nthlumi < nextlumi_))
607  return;
608 
609  if (onlineMode_) {
610  if (nthlumi > nextlumi_) {
611  if (countLumi_ != 0 && processed_)
612  FitAndFill(lumiSeg, lastlumi_, nextlumi_, nthlumi);
613  nextlumi_ = nthlumi;
614  edm::LogInfo("BeamMonitor") << "beginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
615  if ((StartAverage_) && refBStime[0] == 0)
616  refBStime[0] = nbbst->second;
617  if ((StartAverage_) && refPVtime[0] == 0)
618  refPVtime[0] = nbpvt->second;
619  }
620  } else {
621  if (processed_)
622  FitAndFill(lumiSeg, lastlumi_, nextlumi_, nthlumi);
623  nextlumi_ = nthlumi;
624  edm::LogInfo("BeamMonitor") << " beginLuminosityBlock:: Next Lumi to Fit: " << nextlumi_ << endl;
625  if ((StartAverage_) && refBStime[0] == 0)
626  refBStime[0] = nbbst->second;
627  if ((StartAverage_) && refPVtime[0] == 0)
628  refPVtime[0] = nbpvt->second;
629  }
630 
631  //countLumi_++;
632  if (processed_)
633  processed_ = false;
634  edm::LogInfo("BeamMonitor") << " beginLuminosityBlock:: Begin of Lumi: " << nthlumi << endl;
635 }
636 
637 // ----------------------------------------------------------
638 void BeamMonitor::analyze(const Event& iEvent, const EventSetup& iSetup) {
639  const int nthlumi = iEvent.luminosityBlock();
640  if (onlineMode_ && (nthlumi < nextlumi_)) {
641  edm::LogInfo("BeamMonitor") << "analyze:: Spilt event from previous lumi section!" << std::endl;
642  return;
643  }
644  if (onlineMode_ && (nthlumi > nextlumi_)) {
645  edm::LogInfo("BeamMonitor") << "analyze:: Spilt event from next lumi section!!!" << std::endl;
646  return;
647  }
648 
649  countEvt_++;
650  theBeamFitter->readEvent(
651  iEvent); //Remember when track fitter read the event in the same place the PVFitter read the events !!!!!!!!!
652 
653  Handle<reco::BeamSpot> recoBeamSpotHandle;
654  iEvent.getByToken(bsSrc_, recoBeamSpotHandle);
655  refBS = *recoBeamSpotHandle;
656 
657  //------Cut Flow Table filled every event!--------------------------------------
658  {
659  // Make a copy of the cut flow table from the beam fitter.
660  auto tmphisto = static_cast<TH1F*>(theBeamFitter->getCutFlow());
661  cutFlowTable->getTH1()->SetBins(
662  tmphisto->GetNbinsX(), tmphisto->GetXaxis()->GetXmin(), tmphisto->GetXaxis()->GetXmax());
663  // Update the bin labels
664  if (countEvt_ == 1) // SetLabel just once
665  for (int n = 0; n < tmphisto->GetNbinsX(); n++)
666  cutFlowTable->setBinLabel(n + 1, tmphisto->GetXaxis()->GetBinLabel(n + 1), 1);
667  cutFlowTable->Reset();
668  cutFlowTable->getTH1()->Add(tmphisto);
669  }
670 
671  //----Reco tracks -------------------------------------
673  iEvent.getByToken(tracksLabel_, TrackCollection);
674  const reco::TrackCollection* tracks = TrackCollection.product();
675  for (reco::TrackCollection::const_iterator track = tracks->begin(); track != tracks->end(); ++track) {
676  h_trkPt->Fill(track->pt()); //no need to change here for average bs
677  h_trkVz->Fill(track->vz());
678  }
679 
680  //-------HLT Trigger --------------------------------
682  bool JetTrigPass = false;
683  if (iEvent.getByToken(hltSrc_, triggerResults)) {
684  const edm::TriggerNames& trigNames = iEvent.triggerNames(*triggerResults);
685  for (unsigned int i = 0; i < triggerResults->size(); i++) {
686  const std::string& trigName = trigNames.triggerName(i);
687 
688  if (JetTrigPass)
689  continue;
690 
691  for (size_t t = 0; t < jetTrigger_.size(); ++t) {
692  if (JetTrigPass)
693  continue;
694 
695  string string_search(jetTrigger_[t]);
696  size_t found = trigName.find(string_search);
697 
698  if (found != string::npos) {
699  int thisTrigger_ = trigNames.triggerIndex(trigName);
700  if (triggerResults->accept(thisTrigger_))
701  JetTrigPass = true;
702  } //if trigger found
703  } //for(t=0;..)
704  } //for(i=0; ..)
705  } //if trigger colleciton exist)
706 
707  //------ Primary Vertices-------
709 
710  if (iEvent.getByToken(pvSrc_, PVCollection)) {
711  int nPVcount = 0;
712  int nPVcount_ST = 0; //For Single Trigger(hence ST)
713 
714  for (reco::VertexCollection::const_iterator pv = PVCollection->begin(); pv != PVCollection->end(); ++pv) {
715  //--- vertex selection
716  if (pv->isFake() || pv->tracksSize() == 0)
717  continue;
718  nPVcount++; // count non fake pv:
719 
720  if (JetTrigPass)
721  nPVcount_ST++; //non-fake pv with a specific trigger
722 
723  if (pv->ndof() < minVtxNdf_ || (pv->ndof() + 3.) / pv->tracksSize() < 2 * minVtxWgt_)
724  continue;
725 
726  //Fill this map to store xyx for pv so that later we can remove the first one for run aver
727  mapPVx[countLumi_].push_back(pv->x());
728  mapPVy[countLumi_].push_back(pv->y());
729  mapPVz[countLumi_].push_back(pv->z());
730 
731  if (!StartAverage_) { //for first N LS
732  h_PVx[0]->Fill(pv->x());
733  h_PVy[0]->Fill(pv->y());
734  h_PVz[0]->Fill(pv->z());
735  h_PVxz->Fill(pv->z(), pv->x());
736  h_PVyz->Fill(pv->z(), pv->y());
737  } //for first N LiS
738  else {
739  h_PVxz->Fill(pv->z(), pv->x());
740  h_PVyz->Fill(pv->z(), pv->y());
741  }
742 
743  } //loop over pvs
744 
745  h_nVtx->Fill(nPVcount * 1.); //no need to change it for average BS
746 
747  mapNPV[countLumi_].push_back((nPVcount_ST));
748 
749  if (!StartAverage_) {
750  h_nVtx_st->Fill(nPVcount_ST * 1.);
751  }
752 
753  } //if pv collection is availaable
754 
755  if (StartAverage_) {
756  map<int, std::vector<float> >::iterator itpvx = mapPVx.begin();
757  map<int, std::vector<float> >::iterator itpvy = mapPVy.begin();
758  map<int, std::vector<float> >::iterator itpvz = mapPVz.begin();
759 
760  map<int, std::vector<int> >::iterator itbspvinfo = mapNPV.begin();
761 
762  if ((int)mapPVx.size() > resetFitNLumi_) { //sometimes the events is not there but LS is there!
763  mapPVx.erase(itpvx);
764  mapPVy.erase(itpvy);
765  mapPVz.erase(itpvz);
766  mapNPV.erase(itbspvinfo);
767  } //loop over Last N lumi collected
768 
769  } //StartAverage==true
770 
771  processed_ = true;
772 }
773 
774 //--------------------------------------------------------
775 void BeamMonitor::endLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& iSetup) {
776  int nthlumi = lumiSeg.id().luminosityBlock();
777  edm::LogInfo("BeamMonitor") << "endLuminosityBlock:: Lumi of the last event before endLuminosityBlock: " << nthlumi
778  << endl;
779 
780  if (onlineMode_ && nthlumi < nextlumi_)
781  return;
782  const edm::TimeValue_t fendtimestamp = lumiSeg.endTime().value();
783  const std::time_t fendtime = fendtimestamp >> 32;
784  tmpTime = refBStime[1] = refPVtime[1] = fendtime;
785 }
786 
787 //--------------------------------------------------------
788 void BeamMonitor::FitAndFill(const LuminosityBlock& lumiSeg, int& lastlumi, int& nextlumi, int& nthlumi) {
789  if (onlineMode_ && (nthlumi <= nextlumi))
790  return;
791 
792  //set the correct run number when no event in the LS for fake output
793  if ((processed_) && theBeamFitter->getRunNumber() != frun)
794  theBeamFitter->setRun(frun);
795 
796  int currentlumi = nextlumi;
797  edm::LogInfo("BeamMonitor") << "FitAndFill:: Lumi of the current fit: " << currentlumi << endl;
798  lastlumi = currentlumi;
799  endLumiOfBSFit_ = currentlumi;
800  endLumiOfPVFit_ = currentlumi;
801 
802  //---------Fix for Runninv average-------------
803  mapLSPVStoreSize[countLumi_] = theBeamFitter->getPVvectorSize();
804 
805  if (StartAverage_) {
806  std::map<int, std::size_t>::iterator rmLSPVi = mapLSPVStoreSize.begin();
807  size_t SizeToRemovePV = rmLSPVi->second;
808  for (std::map<int, std::size_t>::iterator rmLSPVe = mapLSPVStoreSize.end(); ++rmLSPVi != rmLSPVe;)
809  rmLSPVi->second -= SizeToRemovePV;
810 
811  theBeamFitter->resizePVvector(SizeToRemovePV);
812 
813  map<int, std::size_t>::iterator tmpItpv = mapLSPVStoreSize.begin();
814  mapLSPVStoreSize.erase(tmpItpv);
815  }
816  if (debug_)
817  edm::LogInfo("BeamMonitor") << "FitAndFill:: Size of thePVvector After removing the PVs = "
818  << theBeamFitter->getPVvectorSize() << endl;
819 
820  //lets filt the PV for GUI here: It was in analyzer in preivous versiton but moved here due to absence of event in some lumis, works OK
821  bool resetHistoFlag_ = false;
822  if ((int)mapPVx.size() >= resetFitNLumi_ && (StartAverage_)) {
823  h_PVx[0]->Reset();
824  h_PVy[0]->Reset();
825  h_PVz[0]->Reset();
826  h_nVtx_st->Reset();
827  resetHistoFlag_ = true;
828  }
829 
830  int MaxPVs = 0;
831  int countEvtLastNLS_ = 0;
832  int countTotPV_ = 0;
833 
834  std::map<int, std::vector<int> >::iterator mnpv = mapNPV.begin();
835  std::map<int, std::vector<float> >::iterator mpv2 = mapPVy.begin();
836  std::map<int, std::vector<float> >::iterator mpv3 = mapPVz.begin();
837 
838  for (std::map<int, std::vector<float> >::iterator mpv1 = mapPVx.begin(); mpv1 != mapPVx.end();
839  ++mpv1, ++mpv2, ++mpv3, ++mnpv) {
840  std::vector<float>::iterator mpvs2 = (mpv2->second).begin();
841  std::vector<float>::iterator mpvs3 = (mpv3->second).begin();
842  for (std::vector<float>::iterator mpvs1 = (mpv1->second).begin(); mpvs1 != (mpv1->second).end();
843  ++mpvs1, ++mpvs2, ++mpvs3) {
844  if (resetHistoFlag_) {
845  h_PVx[0]->Fill(*mpvs1); //these histogram are reset after StartAverage_ flag is ON
846  h_PVy[0]->Fill(*mpvs2);
847  h_PVz[0]->Fill(*mpvs3);
848  }
849  } //loop over second
850 
851  //Do the same here for nPV distr.
852  for (std::vector<int>::iterator mnpvs = (mnpv->second).begin(); mnpvs != (mnpv->second).end(); ++mnpvs) {
853  if ((*mnpvs > 0) && (resetHistoFlag_))
854  h_nVtx_st->Fill((*mnpvs) * (1.0));
855  countEvtLastNLS_++;
856  countTotPV_ += (*mnpvs);
857  if ((*mnpvs) > MaxPVs)
858  MaxPVs = (*mnpvs);
859  } //loop over second of mapNPV
860 
861  } //loop over last N lumis
862 
863  char tmpTitlePV[100];
864  sprintf(tmpTitlePV, "%s %i %s %i", "Num. of reco. vertices for LS: ", beginLumiOfPVFit_, " to ", endLumiOfPVFit_);
865  h_nVtx_st->setAxisTitle(tmpTitlePV, 1);
866 
867  std::vector<float> DipPVInfo_;
868  DipPVInfo_.clear();
869 
870  if (countTotPV_ != 0) {
871  DipPVInfo_.push_back((float)countEvtLastNLS_);
872  DipPVInfo_.push_back(h_nVtx_st->getMean());
873  DipPVInfo_.push_back(h_nVtx_st->getMeanError());
874  DipPVInfo_.push_back(h_nVtx_st->getRMS());
875  DipPVInfo_.push_back(h_nVtx_st->getRMSError());
876  DipPVInfo_.push_back((float)MaxPVs);
877  DipPVInfo_.push_back((float)countTotPV_);
878  MaxPVs = 0;
879  } else {
880  for (size_t i = 0; i < 7; i++) {
881  if (i > 0) {
882  DipPVInfo_.push_back(0.);
883  } else {
884  DipPVInfo_.push_back((float)countEvtLastNLS_);
885  }
886  }
887  }
888  theBeamFitter->SetPVInfo(DipPVInfo_);
889  countEvtLastNLS_ = 0;
890 
891  if (onlineMode_) { // filling LS gap
892  // FIXME: need to add protection for the case if the gap is at the resetting LS!
893  const int countLS_bs = hs[k_x0_lumi]->getTH1()->GetEntries();
894  const int countLS_pv = hs[k_PVx_lumi]->getTH1()->GetEntries();
895  edm::LogInfo("BeamMonitor") << "FitAndFill:: countLS_bs = " << countLS_bs << " ; countLS_pv = " << countLS_pv
896  << std::endl;
897  int LSgap_bs = currentlumi / fitNLumi_ - countLS_bs;
898  int LSgap_pv = currentlumi / fitPVNLumi_ - countLS_pv;
899  if (currentlumi % fitNLumi_ == 0)
900  LSgap_bs--;
901  if (currentlumi % fitPVNLumi_ == 0)
902  LSgap_pv--;
903  edm::LogInfo("BeamMonitor") << "FitAndFill:: LSgap_bs = " << LSgap_bs << " ; LSgap_pv = " << LSgap_pv << std::endl;
904  // filling previous fits if LS gap ever exists
905  for (int ig = 0; ig < LSgap_bs; ig++) {
906  hs[k_x0_lumi]->ShiftFillLast(0., 0., fitNLumi_); //x0 , x0err, fitNLumi_; see DQMCore....
907  hs[k_y0_lumi]->ShiftFillLast(0., 0., fitNLumi_);
908  hs[k_z0_lumi]->ShiftFillLast(0., 0., fitNLumi_);
909  hs[k_sigmaX0_lumi]->ShiftFillLast(0., 0., fitNLumi_);
910  hs[k_sigmaY0_lumi]->ShiftFillLast(0., 0., fitNLumi_);
911  hs[k_sigmaZ0_lumi]->ShiftFillLast(0., 0., fitNLumi_);
913  }
914  for (int ig = 0; ig < LSgap_pv; ig++) {
915  hs[k_PVx_lumi]->ShiftFillLast(0., 0., fitPVNLumi_);
916  hs[k_PVy_lumi]->ShiftFillLast(0., 0., fitPVNLumi_);
917  hs[k_PVz_lumi]->ShiftFillLast(0., 0., fitPVNLumi_);
918  }
919  const int previousLS = h_nTrk_lumi->getTH1()->GetEntries();
920  for (int i = 1; i < (currentlumi - previousLS);
921  i++) //if (current-previoius)= 1 then never go inside the for loop!!!!!!!!!!!
923  }
924 
925  edm::LogInfo("BeamMonitor") << "FitAndFill:: Time lapsed since last scroll = " << tmpTime - refTime << std::endl;
926 
927  if (testScroll(tmpTime, refTime)) {
928  scrollTH1(hs[k_x0_time]->getTH1(), refTime);
929  scrollTH1(hs[k_y0_time]->getTH1(), refTime);
930  scrollTH1(hs[k_z0_time]->getTH1(), refTime);
931  scrollTH1(hs[k_sigmaX0_time]->getTH1(), refTime);
932  scrollTH1(hs[k_sigmaY0_time]->getTH1(), refTime);
933  scrollTH1(hs[k_sigmaZ0_time]->getTH1(), refTime);
934  scrollTH1(hs[k_PVx_time]->getTH1(), refTime);
935  scrollTH1(hs[k_PVy_time]->getTH1(), refTime);
936  scrollTH1(hs[k_PVz_time]->getTH1(), refTime);
937  }
938 
939  bool doPVFit = false;
940 
941  if (fitPVNLumi_ > 0) {
942  if (onlineMode_) {
943  if (currentlumi % fitPVNLumi_ == 0)
944  doPVFit = true;
945  } else if (countLumi_ % fitPVNLumi_ == 0)
946  doPVFit = true;
947  } else
948  doPVFit = true;
949 
950  if (doPVFit) {
951  edm::LogInfo("BeamMonitor") << "FitAndFill:: Do PV Fitting for LS = " << beginLumiOfPVFit_ << " to "
952  << endLumiOfPVFit_ << std::endl;
953  // Primary Vertex Fit:
954  if (h_PVx[0]->getTH1()->GetEntries() > minNrVertices_) {
955  pvResults->Reset();
956  char tmpTitle[50];
957  sprintf(
958  tmpTitle, "%s %i %s %i", "Fitted Primary Vertex (cm) of LS: ", beginLumiOfPVFit_, " to ", endLumiOfPVFit_);
959  pvResults->setAxisTitle(tmpTitle, 1);
960 
961  std::unique_ptr<TF1> fgaus{new TF1("fgaus", "gaus")};
962  double mean, width, meanErr, widthErr;
963  fgaus->SetLineColor(4);
964  h_PVx[0]->getTH1()->Fit(fgaus.get(), "QLM0");
965  mean = fgaus->GetParameter(1);
966  width = fgaus->GetParameter(2);
967  meanErr = fgaus->GetParError(1);
968  widthErr = fgaus->GetParError(2);
969 
970  hs[k_PVx_lumi]->ShiftFillLast(mean, width, fitPVNLumi_);
971  hs[k_PVx_lumi_all]->setBinContent(currentlumi, mean);
972  hs[k_PVx_lumi_all]->setBinError(currentlumi, width);
973  int nthBin = tmpTime - refTime;
974  if (nthBin < 0)
975  edm::LogInfo("BeamMonitor") << "FitAndFill:: Event time outside current range of time histograms!"
976  << std::endl;
977  if (nthBin > 0) {
978  hs[k_PVx_time]->setBinContent(nthBin, mean);
979  hs[k_PVx_time]->setBinError(nthBin, width);
980  }
981  int jthBin = tmpTime - startTime;
982  if (jthBin > 0) {
983  hs[k_PVx_time_all]->setBinContent(jthBin, mean);
984  hs[k_PVx_time_all]->setBinError(jthBin, width);
985  }
986  pvResults->setBinContent(1, 6, mean);
987  pvResults->setBinContent(1, 3, width);
988  pvResults->setBinContent(2, 6, meanErr);
989  pvResults->setBinContent(2, 3, widthErr);
990 
991  {
992  // snap shot of the fit
993  auto tmphisto = h_PVx[0]->getTH1F();
994  h_PVx[1]->getTH1()->SetBins(
995  tmphisto->GetNbinsX(), tmphisto->GetXaxis()->GetXmin(), tmphisto->GetXaxis()->GetXmax());
996  h_PVx[1]->Reset();
997  h_PVx[1]->getTH1()->Add(tmphisto);
998  h_PVx[1]->getTH1()->Fit(fgaus.get(), "QLM");
999  }
1000 
1001  h_PVy[0]->getTH1()->Fit(fgaus.get(), "QLM0");
1002  mean = fgaus->GetParameter(1);
1003  width = fgaus->GetParameter(2);
1004  meanErr = fgaus->GetParError(1);
1005  widthErr = fgaus->GetParError(2);
1006  hs[k_PVy_lumi]->ShiftFillLast(mean, width, fitPVNLumi_);
1007  hs[k_PVy_lumi_all]->setBinContent(currentlumi, mean);
1008  hs[k_PVy_lumi_all]->setBinError(currentlumi, width);
1009  if (nthBin > 0) {
1010  hs[k_PVy_time]->setBinContent(nthBin, mean);
1011  hs[k_PVy_time]->setBinError(nthBin, width);
1012  }
1013  if (jthBin > 0) {
1014  hs[k_PVy_time_all]->setBinContent(jthBin, mean);
1015  hs[k_PVy_time_all]->setBinError(jthBin, width);
1016  }
1017  pvResults->setBinContent(1, 5, mean);
1018  pvResults->setBinContent(1, 2, width);
1019  pvResults->setBinContent(2, 5, meanErr);
1020  pvResults->setBinContent(2, 2, widthErr);
1021  // snap shot of the fit
1022  {
1023  auto tmphisto = h_PVy[0]->getTH1F();
1024  h_PVy[1]->getTH1()->SetBins(
1025  tmphisto->GetNbinsX(), tmphisto->GetXaxis()->GetXmin(), tmphisto->GetXaxis()->GetXmax());
1026  h_PVy[1]->update();
1027  h_PVy[1]->Reset();
1028  h_PVy[1]->getTH1()->Add(tmphisto);
1029  h_PVy[1]->getTH1()->Fit(fgaus.get(), "QLM");
1030  }
1031 
1032  h_PVz[0]->getTH1()->Fit(fgaus.get(), "QLM0");
1033  mean = fgaus->GetParameter(1);
1034  width = fgaus->GetParameter(2);
1035  meanErr = fgaus->GetParError(1);
1036  widthErr = fgaus->GetParError(2);
1037  hs[k_PVz_lumi]->ShiftFillLast(mean, width, fitPVNLumi_);
1038  hs[k_PVz_lumi_all]->setBinContent(currentlumi, mean);
1039  hs[k_PVz_lumi_all]->setBinError(currentlumi, width);
1040  if (nthBin > 0) {
1041  hs[k_PVz_time]->setBinContent(nthBin, mean);
1042  hs[k_PVz_time]->setBinError(nthBin, width);
1043  }
1044  if (jthBin > 0) {
1045  hs[k_PVz_time_all]->setBinContent(jthBin, mean);
1046  hs[k_PVz_time_all]->setBinError(jthBin, width);
1047  }
1048  pvResults->setBinContent(1, 4, mean);
1049  pvResults->setBinContent(1, 1, width);
1050  pvResults->setBinContent(2, 4, meanErr);
1051  pvResults->setBinContent(2, 1, widthErr);
1052  {
1053  // snap shot of the fit
1054  auto tmphisto = h_PVz[0]->getTH1F();
1055  h_PVz[1]->getTH1()->SetBins(
1056  tmphisto->GetNbinsX(), tmphisto->GetXaxis()->GetXmin(), tmphisto->GetXaxis()->GetXmax());
1057  h_PVz[1]->update();
1058  h_PVz[1]->Reset();
1059  h_PVz[1]->getTH1()->Add(tmphisto);
1060  h_PVz[1]->getTH1()->Fit(fgaus.get(), "QLM");
1061  }
1062  } //check if found min Vertices
1063  } //do PVfit
1064 
1065  if ((resetPVNLumi_ > 0 && countLumi_ == resetPVNLumi_) || StartAverage_) {
1066  beginLumiOfPVFit_ = 0;
1067  refPVtime[0] = 0;
1068  }
1069 
1070  //---------Readjustment of theBSvector, RefTime, beginLSofFit---------
1071  vector<BSTrkParameters> theBSvector1 = theBeamFitter->getBSvector();
1072  mapLSBSTrkSize[countLumi_] = (theBSvector1.size());
1073  size_t PreviousRecords = 0; //needed to fill nth record of tracks in GUI
1074 
1075  if (StartAverage_) {
1076  size_t SizeToRemove = 0;
1077  std::map<int, std::size_t>::iterator rmls = mapLSBSTrkSize.begin();
1078  SizeToRemove = rmls->second;
1079  if (debug_)
1080  edm::LogInfo("BeamMonitor") << " The size to remove is = " << SizeToRemove << endl;
1081  int changedAfterThis = 0;
1082  for (std::map<int, std::size_t>::iterator rmLS = mapLSBSTrkSize.begin(); rmLS != mapLSBSTrkSize.end();
1083  ++rmLS, ++changedAfterThis) {
1084  if (changedAfterThis > 0) {
1085  (rmLS->second) = (rmLS->second) - SizeToRemove;
1086  if ((mapLSBSTrkSize.size() - (size_t)changedAfterThis) == 2)
1087  PreviousRecords = (rmLS->second);
1088  }
1089  }
1090 
1091  theBeamFitter->resizeBSvector(SizeToRemove);
1092 
1093  map<int, std::size_t>::iterator tmpIt = mapLSBSTrkSize.begin();
1094  mapLSBSTrkSize.erase(tmpIt);
1095 
1096  std::pair<int, int> checkfitLS = theBeamFitter->getFitLSRange();
1097  std::pair<time_t, time_t> checkfitTime = theBeamFitter->getRefTime();
1098  theBeamFitter->setFitLSRange(beginLumiOfBSFit_, checkfitLS.second);
1099  theBeamFitter->setRefTime(refBStime[0], checkfitTime.second);
1100  }
1101 
1102  //Fill the track for this fit
1103  vector<BSTrkParameters> theBSvector = theBeamFitter->getBSvector();
1104  h_nTrk_lumi->ShiftFillLast(theBSvector.size());
1105 
1106  if (debug_)
1107  edm::LogInfo("BeamMonitor") << "FitAndFill:: Size of theBSViector.size() After =" << theBSvector.size() << endl;
1108 
1109  bool countFitting = false;
1110  if (theBSvector.size() >= PreviousRecords && theBSvector.size() >= min_Ntrks_) {
1111  countFitting = true;
1112  }
1113 
1114  //---Fix for Cut Flow Table for Running average in a same way//the previous code has problem for resetting!!!
1115  mapLSCF[countLumi_] = *theBeamFitter->getCutFlow();
1116  if (StartAverage_ && !mapLSCF.empty()) {
1117  const TH1F& cutFlowToSubtract = mapLSCF.begin()->second;
1118  // Subtract the last cut flow from all of the others.
1119  std::map<int, TH1F>::iterator cf = mapLSCF.begin();
1120  // Start on second entry
1121  for (; cf != mapLSCF.end(); ++cf) {
1122  cf->second.Add(&cutFlowToSubtract, -1);
1123  }
1124  theBeamFitter->subtractFromCutFlow(&cutFlowToSubtract);
1125  // Remove the obsolete lumi section
1126  mapLSCF.erase(mapLSCF.begin());
1127  }
1128 
1129  if (resetHistos_) {
1130  h_d0_phi0->Reset();
1131  h_vx_vy->Reset();
1132  h_vx_dz->Reset();
1133  h_vy_dz->Reset();
1134  h_trk_z0->Reset();
1135  resetHistos_ = false;
1136  }
1137 
1138  if (StartAverage_)
1139  nthBSTrk_ = PreviousRecords; //after average proccess is ON//for 2-6 LS fit PreviousRecords is size from 2-5 LS
1140 
1141  edm::LogInfo("BeamMonitor") << " The Previous Recored for this fit is =" << nthBSTrk_ << endl;
1142 
1143  unsigned int itrk = 0;
1144  for (vector<BSTrkParameters>::const_iterator BSTrk = theBSvector.begin(); BSTrk != theBSvector.end();
1145  ++BSTrk, ++itrk) {
1146  if (itrk >= nthBSTrk_) { //fill for this record only !!
1147  h_d0_phi0->Fill(BSTrk->phi0(), BSTrk->d0());
1148  double vx = BSTrk->vx();
1149  double vy = BSTrk->vy();
1150  double z0 = BSTrk->z0();
1151  h_vx_vy->Fill(vx, vy);
1152  h_vx_dz->Fill(z0, vx);
1153  h_vy_dz->Fill(z0, vy);
1154  h_trk_z0->Fill(z0);
1155  }
1156  }
1157 
1158  nthBSTrk_ = theBSvector.size(); // keep track of num of tracks filled so far
1159 
1160  edm::LogInfo("BeamMonitor") << " The Current Recored for this fit is =" << nthBSTrk_ << endl;
1161 
1162  if (countFitting)
1163  edm::LogInfo("BeamMonitor") << "FitAndFill:: Num of tracks collected = " << nthBSTrk_ << endl;
1164 
1165  if (fitNLumi_ > 0) {
1166  if (onlineMode_) {
1167  if (currentlumi % fitNLumi_ != 0) {
1168  // for (std::map<TString,MonitorElement*>::iterator itAll = hs.begin();
1169  // itAll != hs.end(); ++itAll) {
1170  // if ((*itAll).first.Contains("all")) {
1171  // (*itAll).second->setBinContent(currentlumi,0.);
1172  // (*itAll).second->setBinError(currentlumi,0.);
1173  // }
1174  // }
1175  return;
1176  }
1177  } else if (countLumi_ % fitNLumi_ != 0)
1178  return;
1179  }
1180 
1181  edm::LogInfo("BeamMonitor") << "FitAndFill:: [DebugTime] refBStime[0] = " << refBStime[0]
1182  << "; address = " << &refBStime[0] << std::endl;
1183  edm::LogInfo("BeamMonitor") << "FitAndFill:: [DebugTime] refBStime[1] = " << refBStime[1]
1184  << "; address = " << &refBStime[1] << std::endl;
1185 
1186  //Fill for all LS even if fit fails
1187  h_nVtx_lumi->ShiftFillLast((theBeamFitter->getPVvectorSize()), 0., fitNLumi_);
1188  h_nVtx_lumi_all->setBinContent(currentlumi, (theBeamFitter->getPVvectorSize()));
1189 
1190  if (countFitting) {
1191  nFits_++;
1192  std::pair<int, int> fitLS = theBeamFitter->getFitLSRange();
1193  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamFitter] Do BeamSpot Fit for LS = " << fitLS.first << " to "
1194  << fitLS.second << std::endl;
1195  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamMonitor] Do BeamSpot Fit for LS = " << beginLumiOfBSFit_
1196  << " to " << endLumiOfBSFit_ << std::endl;
1197 
1198  //Now Run the PV and Track Fitter over the collected tracks and pvs
1199  if (theBeamFitter->runPVandTrkFitter()) {
1200  reco::BeamSpot bs = theBeamFitter->getBeamSpot();
1201  if (bs.type() > 0) // with good beamwidth fit
1202  preBS = bs; // cache good fit results
1203 
1204  edm::LogInfo("BeamMonitor") << "\n RESULTS OF DEFAULT FIT:" << endl;
1205  edm::LogInfo("BeamMonitor") << bs << endl;
1206  edm::LogInfo("BeamMonitor") << "[BeamFitter] fitting done \n" << endl;
1207 
1208  hs[k_x0_lumi]->ShiftFillLast(bs.x0(), bs.x0Error(), fitNLumi_);
1209  hs[k_y0_lumi]->ShiftFillLast(bs.y0(), bs.y0Error(), fitNLumi_);
1210  hs[k_z0_lumi]->ShiftFillLast(bs.z0(), bs.z0Error(), fitNLumi_);
1211  hs[k_sigmaX0_lumi]->ShiftFillLast(bs.BeamWidthX(), bs.BeamWidthXError(), fitNLumi_);
1212  hs[k_sigmaY0_lumi]->ShiftFillLast(bs.BeamWidthY(), bs.BeamWidthYError(), fitNLumi_);
1213  hs[k_sigmaZ0_lumi]->ShiftFillLast(bs.sigmaZ(), bs.sigmaZ0Error(), fitNLumi_);
1214  hs[k_x0_lumi_all]->setBinContent(currentlumi, bs.x0());
1215  hs[k_x0_lumi_all]->setBinError(currentlumi, bs.x0Error());
1216  hs[k_y0_lumi_all]->setBinContent(currentlumi, bs.y0());
1217  hs[k_y0_lumi_all]->setBinError(currentlumi, bs.y0Error());
1218  hs[k_z0_lumi_all]->setBinContent(currentlumi, bs.z0());
1219  hs[k_z0_lumi_all]->setBinError(currentlumi, bs.z0Error());
1220  hs[k_sigmaX0_lumi_all]->setBinContent(currentlumi, bs.BeamWidthX());
1221  hs[k_sigmaX0_lumi_all]->setBinError(currentlumi, bs.BeamWidthXError());
1222  hs[k_sigmaY0_lumi_all]->setBinContent(currentlumi, bs.BeamWidthY());
1223  hs[k_sigmaY0_lumi_all]->setBinError(currentlumi, bs.BeamWidthYError());
1224  hs[k_sigmaZ0_lumi_all]->setBinContent(currentlumi, bs.sigmaZ());
1225  hs[k_sigmaZ0_lumi_all]->setBinError(currentlumi, bs.sigmaZ0Error());
1226 
1227  int nthBin = tmpTime - refTime;
1228  if (nthBin > 0) {
1229  hs[k_x0_time]->setBinContent(nthBin, bs.x0());
1230  hs[k_y0_time]->setBinContent(nthBin, bs.y0());
1231  hs[k_z0_time]->setBinContent(nthBin, bs.z0());
1232  hs[k_sigmaX0_time]->setBinContent(nthBin, bs.BeamWidthX());
1233  hs[k_sigmaY0_time]->setBinContent(nthBin, bs.BeamWidthY());
1234  hs[k_sigmaZ0_time]->setBinContent(nthBin, bs.sigmaZ());
1235  hs[k_x0_time]->setBinError(nthBin, bs.x0Error());
1236  hs[k_y0_time]->setBinError(nthBin, bs.y0Error());
1237  hs[k_z0_time]->setBinError(nthBin, bs.z0Error());
1238  hs[k_sigmaX0_time]->setBinError(nthBin, bs.BeamWidthXError());
1239  hs[k_sigmaY0_time]->setBinError(nthBin, bs.BeamWidthYError());
1240  hs[k_sigmaZ0_time]->setBinError(nthBin, bs.sigmaZ0Error());
1241  }
1242 
1243  int jthBin = tmpTime - startTime;
1244  if (jthBin > 0) {
1245  hs[k_x0_time_all]->setBinContent(jthBin, bs.x0());
1246  hs[k_y0_time_all]->setBinContent(jthBin, bs.y0());
1247  hs[k_z0_time_all]->setBinContent(jthBin, bs.z0());
1248  hs[k_sigmaX0_time_all]->setBinContent(jthBin, bs.BeamWidthX());
1249  hs[k_sigmaY0_time_all]->setBinContent(jthBin, bs.BeamWidthY());
1250  hs[k_sigmaZ0_time_all]->setBinContent(jthBin, bs.sigmaZ());
1251  hs[k_x0_time_all]->setBinError(jthBin, bs.x0Error());
1252  hs[k_y0_time_all]->setBinError(jthBin, bs.y0Error());
1253  hs[k_z0_time_all]->setBinError(jthBin, bs.z0Error());
1254  hs[k_sigmaX0_time_all]->setBinError(jthBin, bs.BeamWidthXError());
1255  hs[k_sigmaY0_time_all]->setBinError(jthBin, bs.BeamWidthYError());
1256  hs[k_sigmaZ0_time_all]->setBinError(jthBin, bs.sigmaZ0Error());
1257  }
1258 
1259  h_x0->Fill(bs.x0());
1260  h_y0->Fill(bs.y0());
1261  h_z0->Fill(bs.z0());
1262  if (bs.type() > 0) { // with good beamwidth fit
1263  h_sigmaX0->Fill(bs.BeamWidthX());
1264  h_sigmaY0->Fill(bs.BeamWidthY());
1265  }
1266  h_sigmaZ0->Fill(bs.sigmaZ());
1267 
1268  if (nthBSTrk_ >= 2 * min_Ntrks_) {
1269  double amp = std::sqrt(bs.x0() * bs.x0() + bs.y0() * bs.y0());
1270  double alpha = std::atan2(bs.y0(), bs.x0());
1271  std::unique_ptr<TF1> f1{new TF1("f1", "[0]*sin(x-[1])", -3.14, 3.14)};
1272  f1->SetParameters(amp, alpha);
1273  f1->SetParLimits(0, amp - 0.1, amp + 0.1);
1274  f1->SetParLimits(1, alpha - 0.577, alpha + 0.577);
1275  f1->SetLineColor(4);
1276  h_d0_phi0->getTProfile()->Fit(f1.get(), "QR");
1277 
1278  double mean = bs.z0();
1279  double width = bs.sigmaZ();
1280  std::unique_ptr<TF1> fgaus{new TF1("fgaus", "gaus")};
1281  fgaus->SetParameters(mean, width);
1282  fgaus->SetLineColor(4);
1283  h_trk_z0->getTH1()->Fit(fgaus.get(), "QLRM", "", mean - 3 * width, mean + 3 * width);
1284  }
1285 
1286  fitResults->Reset();
1287  std::pair<int, int> LSRange = theBeamFitter->getFitLSRange();
1288  char tmpTitle[50];
1289  sprintf(tmpTitle, "%s %i %s %i", "Fitted Beam Spot (cm) of LS: ", LSRange.first, " to ", LSRange.second);
1290  fitResults->setAxisTitle(tmpTitle, 1);
1291  fitResults->setBinContent(1, 8, bs.x0());
1292  fitResults->setBinContent(1, 7, bs.y0());
1293  fitResults->setBinContent(1, 6, bs.z0());
1294  fitResults->setBinContent(1, 5, bs.sigmaZ());
1295  fitResults->setBinContent(1, 4, bs.dxdz());
1296  fitResults->setBinContent(1, 3, bs.dydz());
1297  if (bs.type() > 0) { // with good beamwidth fit
1298  fitResults->setBinContent(1, 2, bs.BeamWidthX());
1299  fitResults->setBinContent(1, 1, bs.BeamWidthY());
1300  } else { // fill cached widths
1303  }
1304 
1305  fitResults->setBinContent(2, 8, bs.x0Error());
1306  fitResults->setBinContent(2, 7, bs.y0Error());
1307  fitResults->setBinContent(2, 6, bs.z0Error());
1308  fitResults->setBinContent(2, 5, bs.sigmaZ0Error());
1309  fitResults->setBinContent(2, 4, bs.dxdzError());
1310  fitResults->setBinContent(2, 3, bs.dydzError());
1311  if (bs.type() > 0) { // with good beamwidth fit
1314  } else { // fill cached width errors
1317  }
1318 
1319  // count good fit
1320  // if (std::fabs(refBS.x0()-bs.x0())/bs.x0Error() < deltaSigCut_) { // disabled temporarily
1321  summaryContent_[0] += 1.;
1322  // }
1323  // if (std::fabs(refBS.y0()-bs.y0())/bs.y0Error() < deltaSigCut_) { // disabled temporarily
1324  summaryContent_[1] += 1.;
1325  // }
1326  // if (std::fabs(refBS.z0()-bs.z0())/bs.z0Error() < deltaSigCut_) { // disabled temporarily
1327  summaryContent_[2] += 1.;
1328  // }
1329 
1330  } //if (theBeamFitter->runPVandTrkFitter())
1331  else { // beam fit fails
1332  reco::BeamSpot bs = theBeamFitter->getBeamSpot();
1333  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamMonitor] Beam fit fails!!! \n" << endl;
1334  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamMonitor] Output beam spot for DIP \n" << endl;
1335  edm::LogInfo("BeamMonitor") << bs << endl;
1336 
1337  hs[k_sigmaX0_lumi]->ShiftFillLast(bs.BeamWidthX(), bs.BeamWidthXError(), fitNLumi_);
1338  hs[k_sigmaY0_lumi]->ShiftFillLast(bs.BeamWidthY(), bs.BeamWidthYError(), fitNLumi_);
1339  hs[k_sigmaZ0_lumi]->ShiftFillLast(bs.sigmaZ(), bs.sigmaZ0Error(), fitNLumi_);
1340  hs[k_x0_lumi]->ShiftFillLast(bs.x0(), bs.x0Error(), fitNLumi_);
1341  hs[k_y0_lumi]->ShiftFillLast(bs.y0(), bs.y0Error(), fitNLumi_);
1342  hs[k_z0_lumi]->ShiftFillLast(bs.z0(), bs.z0Error(), fitNLumi_);
1343  } // end of beam fit fails
1344 
1345  } //-------- end of countFitting------------------------------------------
1346  else { // no fit
1347  // Overwrite Fit LS and fit time when no event processed or no track selected
1349  theBeamFitter->setRefTime(refBStime[0], refBStime[1]);
1350  if (theBeamFitter->runPVandTrkFitter()) {
1351  } // Dump fake beam spot for DIP
1352  reco::BeamSpot bs = theBeamFitter->getBeamSpot();
1353  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamMonitor] No fitting \n" << endl;
1354  edm::LogInfo("BeamMonitor") << "FitAndFill:: [BeamMonitor] Output fake beam spot for DIP \n" << endl;
1355  edm::LogInfo("BeamMonitor") << bs << endl;
1356 
1357  hs[k_sigmaX0_lumi]->ShiftFillLast(bs.BeamWidthX(), bs.BeamWidthXError(), fitNLumi_);
1358  hs[k_sigmaY0_lumi]->ShiftFillLast(bs.BeamWidthY(), bs.BeamWidthYError(), fitNLumi_);
1359  hs[k_sigmaZ0_lumi]->ShiftFillLast(bs.sigmaZ(), bs.sigmaZ0Error(), fitNLumi_);
1360  hs[k_x0_lumi]->ShiftFillLast(bs.x0(), bs.x0Error(), fitNLumi_);
1361  hs[k_y0_lumi]->ShiftFillLast(bs.y0(), bs.y0Error(), fitNLumi_);
1362  hs[k_z0_lumi]->ShiftFillLast(bs.z0(), bs.z0Error(), fitNLumi_);
1363  }
1364 
1365  // Fill summary report
1366  if (countFitting) {
1367  for (int n = 0; n < nFitElements_; n++) {
1369  }
1370 
1371  summarySum_ = 0;
1372  for (int ii = 0; ii < nFitElements_; ii++) {
1374  }
1375  reportSummary_ = summarySum_ / (nFitElements_ * nFits_);
1376  if (reportSummary)
1378 
1379  for (int bi = 0; bi < nFitElements_; bi++) {
1380  reportSummaryMap->setBinContent(1, bi + 1, summaryContent_[bi] / (float)nFits_);
1381  }
1382  }
1383 
1384  if ((resetFitNLumi_ > 0 &&
1385  ((onlineMode_ &&
1386  countLumi_ == resetFitNLumi_) || //OR it should be currentLumi_ (if in sequence then does not mattar)
1387  (!onlineMode_ && countLumi_ == resetFitNLumi_))) ||
1388  (StartAverage_)) {
1389  edm::LogInfo("BeamMonitor") << "FitAndFill:: The flag is ON for running average Beam Spot fit" << endl;
1390  StartAverage_ = true;
1391  firstAverageFit_++;
1392  resetHistos_ = true;
1393  nthBSTrk_ = 0;
1394  beginLumiOfBSFit_ = 0;
1395  refBStime[0] = 0;
1396  }
1397 }
1398 
1399 //--------------------------------------------------------
1401  if (debug_)
1402  edm::LogInfo("BeamMonitor")
1403  << " RestartingFitting:: Restart Beami everything to a fresh start !!! because Gap is > 10 LS" << endl;
1404  //track based fit reset here
1405  resetHistos_ = true;
1406  nthBSTrk_ = 0;
1407  theBeamFitter->resetTrkVector();
1408  theBeamFitter->resetLSRange();
1409  theBeamFitter->resetRefTime();
1410  theBeamFitter->resetPVFitter();
1411  theBeamFitter->resetCutFlow();
1412  beginLumiOfBSFit_ = 0;
1413  refBStime[0] = 0;
1414  //pv based fit iis reset here
1415  h_PVx[0]->Reset();
1416  h_PVy[0]->Reset();
1417  h_PVz[0]->Reset();
1418  beginLumiOfPVFit_ = 0;
1419  refPVtime[0] = 0;
1420  //Clear all the Maps here
1421  mapPVx.clear();
1422  mapPVy.clear();
1423  mapPVz.clear();
1424  mapNPV.clear();
1425  mapBeginBSLS.clear();
1426  mapBeginPVLS.clear();
1427  mapBeginBSTime.clear();
1428  mapBeginPVTime.clear();
1429  mapLSBSTrkSize.clear();
1430  mapLSPVStoreSize.clear();
1431  mapLSCF.clear();
1432  countGapLumi_ = 0;
1433  countLumi_ = 0;
1434  StartAverage_ = false;
1435 }
1436 
1437 //-------------------------------------------------------
1438 void BeamMonitor::dqmEndRun(const Run& r, const EventSetup& context) {
1439  if (debug_)
1440  edm::LogInfo("BeamMonitor") << "dqmEndRun:: Clearing all the Maps " << endl;
1441  //Clear all the Maps here
1442  mapPVx.clear();
1443  mapPVy.clear();
1444  mapPVz.clear();
1445  mapNPV.clear();
1446  mapBeginBSLS.clear();
1447  mapBeginPVLS.clear();
1448  mapBeginBSTime.clear();
1449  mapBeginPVTime.clear();
1450  mapLSBSTrkSize.clear();
1451  mapLSPVStoreSize.clear();
1452  mapLSCF.clear();
1453 }
1454 
1455 //--------------------------------------------------------
1456 void BeamMonitor::scrollTH1(TH1* h, time_t ref) {
1457  char offsetTime[64];
1458  formatFitTime(offsetTime, ref);
1459  TDatime da(offsetTime);
1460  if (lastNZbin > 0) {
1461  double val = h->GetBinContent(lastNZbin);
1462  double valErr = h->GetBinError(lastNZbin);
1463  h->Reset();
1464  h->GetXaxis()->SetTimeOffset(da.Convert(kTRUE));
1465  int bin = (lastNZbin > buffTime ? buffTime : 1);
1466  h->SetBinContent(bin, val);
1467  h->SetBinError(bin, valErr);
1468  } else {
1469  h->Reset();
1470  h->GetXaxis()->SetTimeOffset(da.Convert(kTRUE));
1471  }
1472 }
1473 
1474 //--------------------------------------------------------
1475 // Method to check whether to chane histogram time offset (forward only)
1476 bool BeamMonitor::testScroll(time_t& tmpTime_, time_t& refTime_) {
1477  bool scroll_ = false;
1478  if (tmpTime_ - refTime_ >= intervalInSec_) {
1479  scroll_ = true;
1480  edm::LogInfo("BeamMonitor") << "testScroll:: Reset Time Offset" << std::endl;
1482  for (int bin = intervalInSec_; bin >= 1; bin--) {
1483  if (hs[k_x0_time]->getBinContent(bin) > 0) {
1484  lastNZbin = bin;
1485  break;
1486  }
1487  }
1488  edm::LogInfo("BeamMonitor") << "testScroll:: Last non zero bin = " << lastNZbin << std::endl;
1489  if (tmpTime_ - refTime_ >= intervalInSec_ + lastNZbin) {
1490  edm::LogInfo("BeamMonitor") << "testScroll:: Time difference too large since last readout" << std::endl;
1491  lastNZbin = 0;
1492  refTime_ = tmpTime_ - buffTime;
1493  } else {
1494  edm::LogInfo("BeamMonitor") << "testScroll:: Offset to last record" << std::endl;
1495  int offset = ((lastNZbin > buffTime) ? (lastNZbin - buffTime) : (lastNZbin - 1));
1496  refTime_ += offset;
1497  }
1498  }
1499  return scroll_;
1500 }
1501 
1503 
1504 // Local Variables:
1505 // show-trailing-whitespace: t
1506 // truncate-lines: t
1507 // End:
LuminosityBlockID id() const
#define CEST
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX)
Definition: DQMStore.cc:239
T getParameter(std::string const &) const
BeamMonitor(const edm::ParameterSet &)
Definition: BeamMonitor.cc:94
edm::EDGetTokenT< edm::TriggerResults > hltSrc_
Definition: BeamMonitor.h:74
const double phiMax_
Definition: BeamMonitor.h:65
double z0() const
z coordinate
Definition: BeamSpot.h:65
std::unique_ptr< BeamFitter > theBeamFitter
Definition: BeamMonitor.h:85
T getUntrackedParameter(std::string const &, T const &) const
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
unsigned int minNrVertices_
Definition: BeamMonitor.h:103
double sigmaZ0Error() const
error on sigma z
Definition: BeamSpot.h:92
int nFitElements_
Definition: BeamMonitor.h:98
std::time_t refBStime[2]
Definition: BeamMonitor.h:95
MonitorElement * reportSummary
Definition: BeamMonitor.h:164
void bookHistograms(DQMStore::IBooker &i, const edm::Run &r, const edm::EventSetup &c) override
Definition: BeamMonitor.cc:205
RunNumber_t run() const
Definition: RunBase.h:40
int endLumiOfPVFit_
Definition: BeamMonitor.h:92
int resetFitNLumi_
Definition: BeamMonitor.h:78
MonitorElement * h_nVtx
Definition: BeamMonitor.h:138
const double dzMin_
Definition: BeamMonitor.h:68
MonitorElement * bookFloat(TString const &name)
Definition: DQMStore.cc:233
double minVtxWgt_
Definition: BeamMonitor.h:105
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
MonitorElement * cutFlowTable
Definition: BeamMonitor.h:167
double dydzError() const
error on dydz
Definition: BeamSpot.h:96
MonitorElement * h_PVz[2]
Definition: BeamMonitor.h:142
bool testScroll(std::time_t &, std::time_t &)
virtual void setOption(const char *option)
std::map< int, std::vector< float > > mapPVx
Definition: BeamMonitor.h:149
void analyze(const edm::Event &e, const edm::EventSetup &c) override
Definition: BeamMonitor.cc:638
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:418
MonitorElement * h_PVxz
Definition: BeamMonitor.h:143
double minVtxNdf_
Definition: BeamMonitor.h:104
bool processed_
Definition: BeamMonitor.h:112
bool accept() const
Has at least one path accepted the event?
edm::EDGetTokenT< reco::VertexCollection > pvSrc_
Definition: BeamMonitor.h:73
Hists
Definition: BeamMonitor.cc:164
const double phiMin_
Definition: BeamMonitor.h:64
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
edm::TimeValue_t ftimestamp
Definition: BeamMonitor.h:174
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
const int dxBin_
Definition: BeamMonitor.h:55
std::vector< MonitorElement * > hs
Definition: BeamMonitor.h:146
void endLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &c) override
Definition: BeamMonitor.cc:775
Timestamp const & beginTime() const
MonitorElement * h_sigmaZ0
Definition: BeamMonitor.h:137
MonitorElement * h_nTrk_lumi
Definition: BeamMonitor.h:121
std::map< int, std::size_t > mapLSBSTrkSize
Definition: BeamMonitor.h:155
const int vxBin_
Definition: BeamMonitor.h:59
MonitorElement * fitResults
Definition: BeamMonitor.h:131
std::vector< std::string > jetTrigger_
Definition: BeamMonitor.h:83
int fitPVNLumi_
Definition: BeamMonitor.h:77
MonitorElement * h_sigmaY0
Definition: BeamMonitor.h:136
std::map< int, std::vector< float > > mapPVz
Definition: BeamMonitor.h:149
std::map< int, std::time_t > mapBeginPVTime
Definition: BeamMonitor.h:153
std::map< int, std::vector< int > > mapNPV
Definition: BeamMonitor.h:150
LuminosityBlockNumber_t luminosityBlock() const
void Fill(long long x)
virtual void Reset()
reset ME (ie. contents, errors, etc)
DQM_DEPRECATED void ShiftFillLast(double y, double ye=0., int32_t xscale=1)
std::map< int, std::time_t > mapBeginBSTime
Definition: BeamMonitor.h:153
const double dzMax_
Definition: BeamMonitor.h:69
char const * label
double dydz() const
dydz slope
Definition: BeamSpot.h:80
MonitorElement * h_y0
Definition: BeamMonitor.h:133
int intervalInSec_
Definition: BeamMonitor.h:80
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const double dxMin_
Definition: BeamMonitor.h:56
double dxdzError() const
error on dxdz
Definition: BeamSpot.h:94
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:24
int countGapLumi_
Definition: BeamMonitor.h:110
edm::EDGetTokenT< reco::TrackCollection > tracksLabel_
Definition: BeamMonitor.h:72
MonitorElement * h_vy_dz
Definition: BeamMonitor.h:128
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
T sqrt(T t)
Definition: SSEVec.h:19
unsigned int size() const
Get number of paths stored.
Float_t reportSummary_
Definition: BeamMonitor.h:161
Timestamp const & endTime() const
MonitorElement * h_PVyz
Definition: BeamMonitor.h:144
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, char const *option="s")
Definition: DQMStore.cc:333
std::time_t refTime
Definition: BeamMonitor.h:173
void formatFitTime(char *, const std::time_t &)
Definition: BeamMonitor.cc:43
MonitorElement * pvResults
Definition: BeamMonitor.h:145
int endLumiOfBSFit_
Definition: BeamMonitor.h:90
std::time_t refPVtime[2]
Definition: BeamMonitor.h:96
def pv(vc)
Definition: MetAnalyzer.py:7
MonitorElement * h_nVtx_lumi
Definition: BeamMonitor.h:122
void RestartFitting()
reco::BeamSpot refBS
Definition: BeamMonitor.h:117
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:82
#define end
Definition: vmac.h:39
double BeamWidthYError() const
error on beam width Y, assume error in X = Y
Definition: BeamSpot.h:101
static std::string const triggerResults
Definition: EdmProvDump.cc:45
virtual void setBinLabel(int bin, const std::string &label, int axis=1)
set bin label for x, y or z axis (axis=1, 2, 3 respectively)
double BeamWidthXError() const
error on beam width X, assume error in X = Y
Definition: BeamSpot.h:99
double z0Error() const
error on z
Definition: BeamSpot.h:90
unsigned long long TimeValue_t
Definition: Timestamp.h:28
virtual TProfile * getTProfile()
double dxdz() const
dxdz slope
Definition: BeamSpot.h:78
std::map< int, TH1F > mapLSCF
Definition: BeamMonitor.h:158
ii
Definition: cuy.py:590
double x0Error() const
error on x
Definition: BeamSpot.h:86
double y0Error() const
error on y
Definition: BeamSpot.h:88
MonitorElement * h_nVtx_lumi_all
Definition: BeamMonitor.h:123
double maxZ_
Definition: BeamMonitor.h:102
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:57
std::time_t startTime
Definition: BeamMonitor.h:172
static int buffTime
Definition: BeamMonitor.cc:89
void update()
Mark the object updated.
const int phiBin_
Definition: BeamMonitor.h:63
void FitAndFill(const edm::LuminosityBlock &lumiSeg, int &, int &, int &)
Definition: BeamMonitor.cc:788
Float_t summaryContent_[3]
Definition: BeamMonitor.h:163
MonitorElement * h_sigmaX0
Definition: BeamMonitor.h:135
bool StartAverage_
Definition: BeamMonitor.h:108
virtual double getMean(int axis=1) const
get mean value of histogram along x, y or z axis (axis=1, 2, 3 respectively)
edm::EDGetTokenT< reco::BeamSpot > bsSrc_
Definition: BeamMonitor.h:71
reco::BeamSpot preBS
Definition: BeamMonitor.h:118
const double dxMax_
Definition: BeamMonitor.h:57
MonitorElement * h_nVtx_st
Definition: BeamMonitor.h:139
T const * product() const
Definition: Handle.h:69
const double vxMin_
Definition: BeamMonitor.h:60
MonitorElement * h_PVy[2]
Definition: BeamMonitor.h:141
virtual void setBinContent(int binx, double content)
set content of bin (1-D)
int beginLumiOfPVFit_
Definition: BeamMonitor.h:91
std::string const & triggerName(unsigned int index) const
Definition: TriggerNames.cc:22
virtual double getMeanError(int axis=1) const
MonitorElement * reportSummaryContents[3]
Definition: BeamMonitor.h:165
double sigmaZ() const
sigma z
Definition: BeamSpot.h:76
virtual double getRMS(int axis=1) const
get RMS of histogram along x, y or z axis (axis=1, 2, 3 respectively)
double BeamWidthY() const
beam width Y
Definition: BeamSpot.h:84
double b
Definition: hdecay.h:118
LuminosityBlockNumber_t luminosityBlock() const
MonitorElement * h_x0
Definition: BeamMonitor.h:132
MonitorElement * h_trk_z0
Definition: BeamMonitor.h:125
const double vxMax_
Definition: BeamMonitor.h:61
int resetPVNLumi_
Definition: BeamMonitor.h:79
std::map< int, int > mapBeginBSLS
Definition: BeamMonitor.h:152
void beginLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context) override
Definition: BeamMonitor.cc:516
Timestamp const & beginTime() const
Definition: RunBase.h:41
Float_t summarySum_
Definition: BeamMonitor.h:162
MonitorElement * h_trkPt
Definition: BeamMonitor.h:129
#define begin
Definition: vmac.h:32
HLT enums.
MonitorElement * h_vx_dz
Definition: BeamMonitor.h:127
MonitorElement * h_z0
Definition: BeamMonitor.h:134
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Definition: DQMStore.cc:266
const int dzBin_
Definition: BeamMonitor.h:67
virtual double getRMSError(int axis=1) const
get RMS uncertainty of histogram along x, y or z axis(axis=1,2,3 respectively)
double y0() const
y coordinate
Definition: BeamSpot.h:63
unsigned int nthBSTrk_
Definition: BeamMonitor.h:97
unsigned int min_Ntrks_
Definition: BeamMonitor.h:101
alpha
zGenParticlesMatch = cms.InputTag(""),
bool resetHistos_
Definition: BeamMonitor.h:107
double deltaSigCut_
Definition: BeamMonitor.h:100
std::time_t tmpTime
Definition: BeamMonitor.h:171
int firstAverageFit_
Definition: BeamMonitor.h:109
void dqmEndRun(const edm::Run &r, const edm::EventSetup &c) override
MonitorElement * h_PVx[2]
Definition: BeamMonitor.h:140
std::map< int, int > mapBeginPVLS
Definition: BeamMonitor.h:152
int beginLumiOfBSFit_
Definition: BeamMonitor.h:89
MonitorElement * h_trkVz
Definition: BeamMonitor.h:130
std::string monitorName_
Definition: BeamMonitor.h:70
TimeValue_t value() const
Definition: Timestamp.h:45
std::map< int, std::vector< float > > mapPVy
Definition: BeamMonitor.h:149
MonitorElement * h_d0_phi0
Definition: BeamMonitor.h:124
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:265
#define constexpr
Definition: Run.h:45
MonitorElement * h_vx_vy
Definition: BeamMonitor.h:126
int countLumi_
Definition: BeamMonitor.h:88
void scrollTH1(TH1 *, std::time_t)
BeamType type() const
return beam type
Definition: BeamSpot.h:122
MonitorElement * reportSummaryMap
Definition: BeamMonitor.h:166
bool onlineMode_
Definition: BeamMonitor.h:82
double x0() const
x coordinate
Definition: BeamSpot.h:61
std::map< int, size_t > mapLSPVStoreSize
Definition: BeamMonitor.h:156
virtual void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)