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