CMS 3D CMS Logo

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