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