CMS 3D CMS Logo

BeamFitter.cc
Go to the documentation of this file.
1 
15 
20 
23 
27 
28 // Update the string representations of the time
30  char ts[] = "yyyy.mn.dd hh:mm:ss zzz ";
31  char* fbeginTime = ts;
32  strftime(fbeginTime,sizeof(ts),"%Y.%m.%d %H:%M:%S GMT",gmtime(&freftime[0]));
33  sprintf(fbeginTimeOfFit,"%s",fbeginTime);
34  char* fendTime = ts;
35  strftime(fendTime,sizeof(ts),"%Y.%m.%d %H:%M:%S GMT",gmtime(&freftime[1]));
36  sprintf(fendTimeOfFit,"%s",fendTime);
37 }
38 
39 
42 {
43 
44  debug_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("Debug");
45  tracksToken_ = iColl.consumes<reco::TrackCollection>(
46  iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<edm::InputTag>("TrackCollection"));
47  vertexToken_ = iColl.consumes<edm::View<reco::Vertex> >(
48  iConfig.getUntrackedParameter<edm::InputTag>("primaryVertex", edm::InputTag("offlinePrimaryVertices")));
49  beamSpotToken_ = iColl.consumes<reco::BeamSpot>(
50  iConfig.getUntrackedParameter<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot")));
51  writeTxt_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("WriteAscii");
52  outputTxt_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::string>("AsciiFileName");
53  appendRunTxt_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("AppendRunToFileName");
54  writeDIPTxt_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("WriteDIPAscii");
55  // Specify whether we want to write the DIP file even if the fit is failed.
56  writeDIPBadFit_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("WriteDIPOnBadFit", true);
57  outputDIPTxt_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::string>("DIPFileName");
58  saveNtuple_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("SaveNtuple");
59  saveBeamFit_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("SaveFitResults");
60  savePVVertices_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("SavePVVertices");
61  isMuon_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<bool>("IsMuonCollection");
62 
63  trk_MinpT_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("MinimumPt");
64  trk_MaxEta_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("MaximumEta");
65  trk_MaxIP_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("MaximumImpactParameter");
66  trk_MaxZ_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("MaximumZ");
67  trk_MinNTotLayers_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<int>("MinimumTotalLayers");
68  trk_MinNPixLayers_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<int>("MinimumPixelLayers");
69  trk_MaxNormChi2_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("MaximumNormChi2");
70  trk_Algorithm_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::vector<std::string> >("TrackAlgorithm");
71  trk_Quality_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::vector<std::string> >("TrackQuality");
72  min_Ntrks_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<int>("MinimumInputTracks");
73  convergence_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("FractionOfFittedTrks");
74  inputBeamWidth_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<double>("InputBeamWidth",-1.);
75 
76  for (unsigned int j=0;j<trk_Algorithm_.size();j++)
78  for (unsigned int j=0;j<trk_Quality_.size();j++)
80 
82  outputfilename_ = iConfig.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::string>("OutputFileName");
83  file_ = TFile::Open(outputfilename_.c_str(),"RECREATE");
84  }
85  if (saveNtuple_) {
86  ftree_ = new TTree("mytree","mytree");
87  ftree_->AutoSave();
88 
89  ftree_->Branch("pt",&fpt,"fpt/D");
90  ftree_->Branch("d0",&fd0,"fd0/D");
91  ftree_->Branch("d0bs",&fd0bs,"fd0bs/D");
92  ftree_->Branch("sigmad0",&fsigmad0,"fsigmad0/D");
93  ftree_->Branch("phi0",&fphi0,"fphi0/D");
94  ftree_->Branch("z0",&fz0,"fz0/D");
95  ftree_->Branch("sigmaz0",&fsigmaz0,"fsigmaz0/D");
96  ftree_->Branch("theta",&ftheta,"ftheta/D");
97  ftree_->Branch("eta",&feta,"feta/D");
98  ftree_->Branch("charge",&fcharge,"fcharge/I");
99  ftree_->Branch("normchi2",&fnormchi2,"fnormchi2/D");
100  ftree_->Branch("nTotLayerMeas",&fnTotLayerMeas,"fnTotLayerMeas/i");
101  ftree_->Branch("nStripLayerMeas",&fnStripLayerMeas,"fnStripLayerMeas/i");
102  ftree_->Branch("nPixelLayerMeas",&fnPixelLayerMeas,"fnPixelLayerMeas/i");
103  ftree_->Branch("nTIBLayerMeas",&fnTIBLayerMeas,"fnTIBLayerMeas/i");
104  ftree_->Branch("nTOBLayerMeas",&fnTOBLayerMeas,"fnTOBLayerMeas/i");
105  ftree_->Branch("nTIDLayerMeas",&fnTIDLayerMeas,"fnTIDLayerMeas/i");
106  ftree_->Branch("nTECLayerMeas",&fnTECLayerMeas,"fnTECLayerMeas/i");
107  ftree_->Branch("nPXBLayerMeas",&fnPXBLayerMeas,"fnPXBLayerMeas/i");
108  ftree_->Branch("nPXFLayerMeas",&fnPXFLayerMeas,"fnPXFLayerMeas/i");
109  ftree_->Branch("cov",&fcov,"fcov[7][7]/D");
110  ftree_->Branch("vx",&fvx,"fvx/D");
111  ftree_->Branch("vy",&fvy,"fvy/D");
112  ftree_->Branch("quality",&fquality,"fquality/O");
113  ftree_->Branch("algo",&falgo,"falgo/O");
114  ftree_->Branch("run",&frun,"frun/i");
115  ftree_->Branch("lumi",&flumi,"flumi/i");
116  ftree_->Branch("pvValid",&fpvValid,"fpvValid/O");
117  ftree_->Branch("pvx", &fpvx, "fpvx/D");
118  ftree_->Branch("pvy", &fpvy, "fpvy/D");
119  ftree_->Branch("pvz", &fpvz, "fpvz/D");
120  }
121  if (saveBeamFit_){
122  ftreeFit_ = new TTree("fitResults","fitResults");
123  ftreeFit_->AutoSave();
124  ftreeFit_->Branch("run",&frunFit,"frunFit/i");
125  ftreeFit_->Branch("beginLumi",&fbeginLumiOfFit,"fbeginLumiOfFit/i");
126  ftreeFit_->Branch("endLumi",&fendLumiOfFit,"fendLumiOfFit/i");
127  ftreeFit_->Branch("beginTime",fbeginTimeOfFit,"fbeginTimeOfFit/C");
128  ftreeFit_->Branch("endTime",fendTimeOfFit,"fendTimeOfFit/C");
129  ftreeFit_->Branch("x",&fx,"fx/D");
130  ftreeFit_->Branch("y",&fy,"fy/D");
131  ftreeFit_->Branch("z",&fz,"fz/D");
132  ftreeFit_->Branch("sigmaZ",&fsigmaZ,"fsigmaZ/D");
133  ftreeFit_->Branch("dxdz",&fdxdz,"fdxdz/D");
134  ftreeFit_->Branch("dydz",&fdydz,"fdydz/D");
135  ftreeFit_->Branch("xErr",&fxErr,"fxErr/D");
136  ftreeFit_->Branch("yErr",&fyErr,"fyErr/D");
137  ftreeFit_->Branch("zErr",&fzErr,"fzErr/D");
138  ftreeFit_->Branch("sigmaZErr",&fsigmaZErr,"fsigmaZErr/D");
139  ftreeFit_->Branch("dxdzErr",&fdxdzErr,"fdxdzErr/D");
140  ftreeFit_->Branch("dydzErr",&fdydzErr,"fdydzErr/D");
141  ftreeFit_->Branch("widthX",&fwidthX,"fwidthX/D");
142  ftreeFit_->Branch("widthY",&fwidthY,"fwidthY/D");
143  ftreeFit_->Branch("widthXErr",&fwidthXErr,"fwidthXErr/D");
144  ftreeFit_->Branch("widthYErr",&fwidthYErr,"fwidthYErr/D");
145  }
146 
147  fBSvector.clear();
148  ftotal_tracks = 0;
151  frun = flumi = -1;
153  fquality = falgo = true;
154  fpvValid = true;
155  fpvx = fpvy = fpvz = 0;
156  fitted_ = false;
157  resetRefTime();
158 
159  //debug histograms
160  h1ntrks = new TH1F("h1ntrks","number of tracks per event",50,0,50);
161  h1vz_event = new TH1F("h1vz_event","track Vz", 50, -30, 30 );
162  h1cutFlow = new TH1F("h1cutFlow","Cut flow table of track selection", 9, 0, 9);
163  h1cutFlow->GetXaxis()->SetBinLabel(1,"No cut");
164  h1cutFlow->GetXaxis()->SetBinLabel(2,"Traker hits");
165  h1cutFlow->GetXaxis()->SetBinLabel(3,"Pixel hits");
166  h1cutFlow->GetXaxis()->SetBinLabel(4,"norm. #chi^{2}");
167  h1cutFlow->GetXaxis()->SetBinLabel(5,"algo");
168  h1cutFlow->GetXaxis()->SetBinLabel(6,"quality");
169  h1cutFlow->GetXaxis()->SetBinLabel(7,"d_{0}");
170  h1cutFlow->GetXaxis()->SetBinLabel(8,"z_{0}");
171  h1cutFlow->GetXaxis()->SetBinLabel(9,"p_{T}");
172  resetCutFlow();
173 
174  // Primary vertex fitter
175  MyPVFitter = new PVFitter(iConfig, iColl);
176  MyPVFitter->resetAll();
177  if (savePVVertices_){
178  fPVTree_ = new TTree("PrimaryVertices","PrimaryVertices");
180  }
181 
182  // check filename
183  ffilename_changed = false;
184  if (writeDIPTxt_)
185  fasciiDIP.open(outputDIPTxt_.c_str());
186 }
187 
189 
190  if (saveNtuple_) {
191  file_->cd();
192  if (fitted_ && h1z) h1z->Write();
193  h1ntrks->Write();
194  h1vz_event->Write();
195  if (h1cutFlow) h1cutFlow->Write();
196  ftree_->Write();
197  }
198  if (saveBeamFit_){
199  file_->cd();
200  ftreeFit_->Write();
201  }
202  if (savePVVertices_){
203  file_->cd();
204  fPVTree_->Write();
205  }
206 
207 
209  file_->Close();
210  delete file_;
211  }
212  delete MyPVFitter;
213 }
214 
215 
217 {
218 
219 
220  frun = iEvent.id().run();
221  const edm::TimeValue_t ftimestamp = iEvent.time().value();
222  const std::time_t ftmptime = ftimestamp >> 32;
223 
224  if (fbeginLumiOfFit == -1) freftime[0] = freftime[1] = ftmptime;
225  if (freftime[0] == 0 || ftmptime < freftime[0]) freftime[0] = ftmptime;
226  if (freftime[1] == 0 || ftmptime > freftime[1]) freftime[1] = ftmptime;
227  // Update the human-readable string versions of the time
228  updateBTime();
229 
230  flumi = iEvent.luminosityBlock();
231  frunFit = frun;
234 
236  iEvent.getByToken(tracksToken_, TrackCollection);
237 
238  //------ Primary Vertices
240  bool hasPVs = false;
242  if ( iEvent.getByToken(vertexToken_, PVCollection ) ) {
243  pv = *PVCollection;
244  hasPVs = true;
245  }
246  //------
247 
248  //------ Beam Spot in current event
249  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
250  const reco::BeamSpot *refBS = nullptr;
251  if ( iEvent.getByToken(beamSpotToken_, recoBeamSpotHandle) )
252  refBS = recoBeamSpotHandle.product();
253  //-------
254 
255  const reco::TrackCollection *tracks = TrackCollection.product();
256 
257  double eventZ = 0;
258  double averageZ = 0;
259 
260  for (reco::TrackCollection::const_iterator track = tracks->begin();
261  track != tracks->end(); ++track){
262 
263  if (!isMuon_) {
264  const reco::HitPattern &trkHP = track->hitPattern();
265 
275  } else {
276  fnTotLayerMeas = track->numberOfValidHits();
277  }
278 
279  fpt = track->pt();
280  feta = track->eta();
281  fphi0 = track->phi();
282  fcharge = track->charge();
283  fnormchi2 = track->normalizedChi2();
284  fd0 = track->d0();
285  if (refBS) fd0bs = -1*track->dxy(refBS->position());
286  else fd0bs = 0.;
287 
288  fsigmad0 = track->d0Error();
289  fz0 = track->dz();
290  fsigmaz0 = track->dzError();
291  ftheta = track->theta();
292  fvx = track->vx();
293  fvy = track->vy();
294 
295  for (int i=0; i<5; ++i) {
296  for (int j=0; j<5; ++j) {
297  fcov[i][j] = track->covariance(i,j);
298  }
299  }
300 
301  fquality = true;
302  falgo = true;
303 
304  if (! isMuon_ ) {
305  if (!quality_.empty()) {
306  fquality = false;
307  for (unsigned int i = 0; i<quality_.size();++i) {
308  if(debug_) edm::LogInfo("BeamFitter") << "quality_[" << i << "] = " << track->qualityName(quality_[i]) << std::endl;
309  if (track->quality(quality_[i])) {
310  fquality = true;
311  break;
312  }
313  }
314  }
315 
316 
317  // Track algorithm
318 
319  if (!algorithm_.empty()) {
320  if (std::find(algorithm_.begin(),algorithm_.end(),track->algo())==algorithm_.end())
321  falgo = false;
322  }
323 
324  }
325 
326  // check if we have a valid PV
327  fpvValid = false;
328 
329  if ( hasPVs ) {
330 
331  for ( size_t ipv=0; ipv != pv.size(); ++ipv ) {
332 
333  if (! pv[ipv].isFake() ) fpvValid = true;
334 
335  if ( ipv==0 && !pv[0].isFake() ) { fpvx = pv[0].x(); fpvy = pv[0].y(); fpvz = pv[0].z(); } // fix this later
336 
337 
338  }
339 
340  }
341 
342 
343  if (saveNtuple_) ftree_->Fill();
344  ftotal_tracks++;
345 
347  // Track selection
348  if (fnTotLayerMeas >= trk_MinNTotLayers_) { countPass[1] += 1;
350  if (fnormchi2 < trk_MaxNormChi2_) { countPass[3] += 1;
351  if (falgo) {countPass[4] += 1;
352  if (fquality) { countPass[5] += 1;
353  if (std::abs( fd0 ) < trk_MaxIP_) { countPass[6] += 1;
354  if (std::abs( fz0 ) < trk_MaxZ_){ countPass[7] += 1;
355  if (fpt > trk_MinpT_) {
356  countPass[8] += 1;
357  if (std::abs( feta ) < trk_MaxEta_
358  //&& fpvValid
359  ) {
360  if (debug_) {
361  edm::LogInfo("BeamFitter") << "Selected track quality = " << track->qualityMask()
362  << "; track algorithm = " << track->algoName() << "= TrackAlgorithm: " << track->algo() << std::endl;
363  }
365  BSTrk.setVx(fvx);
366  BSTrk.setVy(fvy);
367  fBSvector.push_back(BSTrk);
368  averageZ += fz0;
369  }
370  }
371  }
372  }
373  }
374  }
375  }
376  }
377  }// track selection
378 
379  }// tracks
380 
381  averageZ = averageZ/(float)(fBSvector.size());
382 
383  for( std::vector<BSTrkParameters>::const_iterator iparam = fBSvector.begin(); iparam != fBSvector.end(); ++iparam) {
384 
385  eventZ += fabs( iparam->z0() - averageZ );
386 
387  }
388 
389  h1ntrks->Fill( fBSvector.size() );
390  h1vz_event->Fill( eventZ/(float)(fBSvector.size() ) ) ;
391  for (unsigned int i=0; i < sizeof(countPass)/sizeof(countPass[0]); i++)
392  h1cutFlow->SetBinContent(i+1,countPass[i]);
393 
394  MyPVFitter->readEvent(iEvent);
395 
396 }
397 
399 // run both PV and track fitters
400  bool fit_ok = false;
401  bool pv_fit_ok = false;
402  reco::BeamSpot bspotPV;
403  reco::BeamSpot bspotTrk;
404 
405  // First run PV fitter
407  edm::LogInfo("BeamFitter") << " [BeamFitterBxDebugTime] freftime[0] = " << freftime[0]
408  << "; address = " << &freftime[0]
409  << " = " << fbeginTimeOfFit << std::endl;
410  edm::LogInfo("BeamFitter") << " [BeamFitterBxDebugTime] freftime[1] = " << freftime[1]
411  << "; address = " << &freftime[1]
412  << " = " << fendTimeOfFit << std::endl;
413 
414  if ( MyPVFitter->runBXFitter() ) {
416  pv_fit_ok = true;
417  }
418  if(writeTxt_ ) dumpTxtFile(outputTxt_,true); // all reaults
419  if(writeDIPTxt_ && (pv_fit_ok || writeDIPBadFit_)) {
420  dumpTxtFile(outputDIPTxt_,false); // for DQM/DIP
421  }
422  return pv_fit_ok;
423  }
424 
425  if ( MyPVFitter->runFitter() ) {
426 
427  bspotPV = MyPVFitter->getBeamSpot();
428 
429  // take beam width from PV fit and pass it to track fitter
430  // assume circular transverse beam width
431  inputBeamWidth_ = sqrt( pow(bspotPV.BeamWidthX(),2) + pow(bspotPV.BeamWidthY(),2) )/sqrt(2);
432  pv_fit_ok = true;
433 
434  } else {
435  // problems with PV fit
437  bspotTrk.setType(reco::BeamSpot::Unknown); //propagate error to trk beam spot
438  }
439 
440  if ( runFitterNoTxt() ) {
441 
442  bspotTrk = fbeamspot;
443  fit_ok = true;
444  } else {
445  // beamfit failed, flagged as empty beam spot
446  bspotTrk.setType(reco::BeamSpot::Fake);
447  fit_ok = false;
448  }
449 
450  // combined results into one single beam spot
451 
452  // to pass errors I have to create another beam spot object
454  for (int j = 0 ; j < 7 ; ++j) {
455  for(int k = j ; k < 7 ; ++k) {
456  matrix(j,k) = bspotTrk.covariance(j,k);
457  }
458  }
459  // change beam width error to one from PV
460  if (pv_fit_ok && fit_ok ) {
462 
463  // get Z and sigmaZ from PV fit
464  matrix(2,2) = bspotPV.covariance(2,2);
465  matrix(3,3) = bspotPV.covariance(3,3);
466  reco::BeamSpot tmpbs(reco::BeamSpot::Point(bspotTrk.x0(), bspotTrk.y0(),
467  bspotPV.z0() ),
468  bspotPV.sigmaZ() ,
469  bspotTrk.dxdz(),
470  bspotTrk.dydz(),
471  bspotPV.BeamWidthX(),
472  matrix,
473  bspotPV.type() );
474  tmpbs.setBeamWidthY( bspotPV.BeamWidthY() );
475  // overwrite beam spot result
476  fbeamspot = tmpbs;
477  }
478  if (pv_fit_ok && fit_ok) {
479  fbeamspot.setType(bspotPV.type());
480  }
481  else if(!pv_fit_ok && fit_ok){
483  }
484  else if(pv_fit_ok && !fit_ok){
486  }
487  else if(!pv_fit_ok && !fit_ok){
489  }
490 
491  if(writeTxt_ ) dumpTxtFile(outputTxt_,true); // all reaults
492  if(writeDIPTxt_ && ((fit_ok && pv_fit_ok) || writeDIPBadFit_)) {
493  dumpTxtFile(outputDIPTxt_,false); // for DQM/DIP
494  for(size_t i= 0; i < 7; i++)ForDIPPV_.push_back(0.0);
495  }
496 
497  return fit_ok && pv_fit_ok;
498 }
499 
501  edm::LogInfo("BeamFitter") << " [BeamFitterDebugTime] freftime[0] = " << freftime[0]
502  << "; address = " << &freftime[0]
503  << " = " << fbeginTimeOfFit << std::endl;
504  edm::LogInfo("BeamFitter") << " [BeamFitterDebugTime] freftime[1] = " << freftime[1]
505  << "; address = " << &freftime[1]
506  << " = " << fendTimeOfFit << std::endl;
507 
508  if (fbeginLumiOfFit == -1 || fendLumiOfFit == -1) {
509  edm::LogWarning("BeamFitter") << "No event read! No Fitting!" << std::endl;
510  return false;
511  }
512 
513  bool fit_ok = false;
514  // default fit to extract beam spot info
515  if(fBSvector.size() > 1 ){
516 
517  edm::LogInfo("BeamFitter") << "Calculating beam spot..." << std::endl
518  << "We will use " << fBSvector.size() << " good tracks out of " << ftotal_tracks << std::endl;
519 
520  BSFitter *myalgo = new BSFitter( fBSvector );
521  myalgo->SetMaximumZ( trk_MaxZ_ );
522  myalgo->SetConvergence( convergence_ );
523  myalgo->SetMinimumNTrks( min_Ntrks_ );
525 
526  fbeamspot = myalgo->Fit();
527 
528 
529  // retrieve histogram for Vz
530  h1z = (TH1F*) myalgo->GetVzHisto();
531 
532  delete myalgo;
533  if ( fbeamspot.type() != 0 ) {// save all results except for Fake (all 0.)
534  fit_ok = true;
535  if (saveBeamFit_){
536  fx = fbeamspot.x0();
537  fy = fbeamspot.y0();
538  fz = fbeamspot.z0();
540  fdxdz = fbeamspot.dxdz();
541  fdydz = fbeamspot.dydz();
544  fxErr = fbeamspot.x0Error();
545  fyErr = fbeamspot.y0Error();
546  fzErr = fbeamspot.z0Error();
552  ftreeFit_->Fill();
553  }
554  }
555  }
556  else{ // tracks <= 1
557  reco::BeamSpot tmpbs;
558  fbeamspot = tmpbs;
560  edm::LogInfo("BeamFitter") << "Not enough good tracks selected! No beam fit!" << std::endl;
561 
562  }
563  fitted_ = true;
564  return fit_ok;
565 
566 }
567 
569 
570  bool fit_ok = runFitterNoTxt();
571 
572  if(writeTxt_ ) dumpTxtFile(outputTxt_,true); // all reaults
573  if(writeDIPTxt_ && (fit_ok || writeDIPBadFit_)) {
574  dumpTxtFile(outputDIPTxt_,false); // for DQM/DIP
575  }
576  return fit_ok;
577 }
578 
580  bool widthfit_ok = false;
581  // default fit to extract beam spot info
582  if(fBSvector.size() > 1 ){
583 
584  edm::LogInfo("BeamFitter") << "Calculating beam spot positions('d0-phi0' method) and width using llh Fit"<< std::endl
585  << "We will use " << fBSvector.size() << " good tracks out of " << ftotal_tracks << std::endl;
586 
587  BSFitter *myalgo = new BSFitter( fBSvector );
588  myalgo->SetMaximumZ( trk_MaxZ_ );
589  myalgo->SetConvergence( convergence_ );
590  myalgo->SetMinimumNTrks(min_Ntrks_);
592 
593 
594  myalgo->SetFitVariable(std::string("d*z"));
595  myalgo->SetFitType(std::string("likelihood"));
596  fbeamWidthFit = myalgo->Fit();
597 
598  //Add to .txt file
600 
601  delete myalgo;
602 
603  // not fake
604  if ( fbeamspot.type() != 0 )
605  widthfit_ok = true;
606  }
607  else{
609  edm::LogWarning("BeamFitter") << "Not enough good tracks selected! No beam fit!" << std::endl;
610  }
611  return widthfit_ok;
612 }
613 
615  std::ofstream outFile;
616  outFile.open(fileName.c_str(),std::ios::app);
617  outFile<<"-------------------------------------------------------------------------------------------------------------------------------------------------------------"<<std::endl;
618  outFile<<"Beam width(in cm) from Log-likelihood fit (Here we assume a symmetric beam(SigmaX=SigmaY)!)"<<std::endl;
619  outFile<<" "<<std::endl;
620  outFile << "BeamWidth = " <<fbeamWidthFit.BeamWidthX() <<" +/- "<<fbeamWidthFit.BeamWidthXError() << std::endl;
621  outFile.close();
622 }
623 
625  std::ofstream outFile;
626 
627  std::string tmpname = outputTxt_;
628  char index[15];
630  sprintf(index,"%s%i","_Run", frun );
631  tmpname.insert(outputTxt_.length()-4,index);
632  fileName = tmpname;
633  ffilename_changed = true;
634  }
635 
636  if (!append)
637  outFile.open(fileName.c_str());
638  else
639  outFile.open(fileName.c_str(),std::ios::app);
640 
642 
643  for (std::map<int,reco::BeamSpot>::const_iterator abspot = fbspotPVMap.begin(); abspot!= fbspotPVMap.end(); ++abspot) {
644  reco::BeamSpot beamspottmp = abspot->second;
645  int bx = abspot->first;
646 
647  outFile << "Runnumber " << frun << " bx " << bx << std::endl;
648  outFile << "BeginTimeOfFit " << fbeginTimeOfFit << " " << freftime[0] << std::endl;
649  outFile << "EndTimeOfFit " << fendTimeOfFit << " " << freftime[1] << std::endl;
650  outFile << "LumiRange " << fbeginLumiOfFit << " - " << fendLumiOfFit << std::endl;
651  outFile << "Type " << beamspottmp.type() << std::endl;
652  outFile << "X0 " << beamspottmp.x0() << std::endl;
653  outFile << "Y0 " << beamspottmp.y0() << std::endl;
654  outFile << "Z0 " << beamspottmp.z0() << std::endl;
655  outFile << "sigmaZ0 " << beamspottmp.sigmaZ() << std::endl;
656  outFile << "dxdz " << beamspottmp.dxdz() << std::endl;
657  outFile << "dydz " << beamspottmp.dydz() << std::endl;
658  outFile << "BeamWidthX " << beamspottmp.BeamWidthX() << std::endl;
659  outFile << "BeamWidthY " << beamspottmp.BeamWidthY() << std::endl;
660  for (int i = 0; i<6; ++i) {
661  outFile << "Cov("<<i<<",j) ";
662  for (int j=0; j<7; ++j) {
663  outFile << beamspottmp.covariance(i,j) << " ";
664  }
665  outFile << std::endl;
666  }
667  outFile << "Cov(6,j) 0 0 0 0 0 0 " << beamspottmp.covariance(6,6) << std::endl;
668  //}
669  outFile << "EmittanceX " << beamspottmp.emittanceX() << std::endl;
670  outFile << "EmittanceY " << beamspottmp.emittanceY() << std::endl;
671  outFile << "BetaStar " << beamspottmp.betaStar() << std::endl;
672 
673  }
674  }//if bx results needed
675  else {
676 
677  beamspot::BeamSpotContainer currentBS;
678 
679  currentBS.beamspot = fbeamspot ;
680  currentBS.run = frun ;
683  currentBS.beginLumiOfFit = fbeginLumiOfFit;
684  currentBS.endLumiOfFit = fendLumiOfFit ;
685  std::copy(freftime, freftime+2, currentBS.reftime);
686 
687  beamspot::dumpBeamSpotTxt(outFile, currentBS);
688 
689  //write here Pv info for DIP only: This added only if append is false, which happen for DIP only :)
690  if(!append){
691  outFile << "events "<< (int)ForDIPPV_[0] << std::endl;
692  outFile << "meanPV "<< ForDIPPV_[1] << std::endl;
693  outFile << "meanErrPV "<< ForDIPPV_[2] << std::endl;
694  outFile << "rmsPV "<< ForDIPPV_[3] << std::endl;
695  outFile << "rmsErrPV "<< ForDIPPV_[4] << std::endl;
696  outFile << "maxPV "<< (int)ForDIPPV_[5] << std::endl;
697  outFile << "nPV "<< (int)ForDIPPV_[6] << std::endl;
698  }//writeDIPPVInfo_
699  }//else end here
700 
701  outFile.close();
702 }
703 
705  BeamSpotObjects *pBSObjects = new BeamSpotObjects();
706 
707  pBSObjects->SetPosition(fbeamspot.position().X(),fbeamspot.position().Y(),fbeamspot.position().Z());
708  //std::cout << " wrote: x= " << fbeamspot.position().X() << " y= "<< fbeamspot.position().Y() << " z= " << fbeamspot.position().Z() << std::endl;
709  pBSObjects->SetSigmaZ(fbeamspot.sigmaZ());
710  pBSObjects->Setdxdz(fbeamspot.dxdz());
711  pBSObjects->Setdydz(fbeamspot.dydz());
712  //if (inputBeamWidth_ > 0 ) {
713  // std::cout << " beam width value forced to be " << inputBeamWidth_ << std::endl;
714  // pBSObjects->SetBeamWidthX(inputBeamWidth_);
715  // pBSObjects->SetBeamWidthY(inputBeamWidth_);
716  //} else {
717  // need to fix this
718  //std::cout << " using default value, 15e-4, for beam width!!!"<<std::endl;
719  pBSObjects->SetBeamWidthX(fbeamspot.BeamWidthX() );
720  pBSObjects->SetBeamWidthY(fbeamspot.BeamWidthY() );
721  //}
722 
723  for (int i = 0; i<7; ++i) {
724  for (int j=0; j<7; ++j) {
725  pBSObjects->SetCovariance(i,j,fbeamspot.covariance(i,j));
726  }
727  }
729  if( poolDbService.isAvailable() ) {
730  std::cout << "poolDBService available"<<std::endl;
731  if ( poolDbService->isNewTagRequest( "BeamSpotObjectsRcd" ) ) {
732  std::cout << "new tag requested" << std::endl;
733  poolDbService->createNewIOV<BeamSpotObjects>( pBSObjects, poolDbService->beginOfTime(),poolDbService->endOfTime(),
734  "BeamSpotObjectsRcd" );
735  }
736  else {
737  std::cout << "no new tag requested" << std::endl;
738  poolDbService->appendSinceTime<BeamSpotObjects>( pBSObjects, poolDbService->currentTime(),
739  "BeamSpotObjectsRcd" );
740  }
741  }
742 }
743 
745  if(!fBSvector.empty()){
746  BSFitter *myalgo = new BSFitter( fBSvector );
747  fbeamspot = myalgo->Fit_d0phi();
748 
749  // iterative
750  if(debug_)
751  std::cout << " d0-phi Iterative:" << std::endl;
752  BSFitter *myitealgo = new BSFitter( fBSvector );
753  myitealgo->Setd0Cut_d0phi(4.0);
754  reco::BeamSpot beam_ite = myitealgo->Fit_ited0phi();
755  if (debug_){
756  std::cout << beam_ite << std::endl;
757  std::cout << "\n Now run tests of the different fits\n";
758  }
759  // from here are just tests
760  std::string fit_type = "chi2";
761  myalgo->SetFitVariable(std::string("z"));
762  myalgo->SetFitType(std::string("chi2"));
763  reco::BeamSpot beam_fit_z_chi2 = myalgo->Fit();
764  if (debug_){
765  std::cout << " z Chi2 Fit ONLY:" << std::endl;
766  std::cout << beam_fit_z_chi2 << std::endl;
767  }
768 
769  fit_type = "combined";
770  myalgo->SetFitVariable("z");
771  myalgo->SetFitType("combined");
772  reco::BeamSpot beam_fit_z_lh = myalgo->Fit();
773  if (debug_){
774  std::cout << " z Log-Likelihood Fit ONLY:" << std::endl;
775  std::cout << beam_fit_z_lh << std::endl;
776  }
777 
778  myalgo->SetFitVariable("d");
779  myalgo->SetFitType("d0phi");
780  reco::BeamSpot beam_fit_dphi = myalgo->Fit();
781  if (debug_){
782  std::cout << " d0-phi0 Fit ONLY:" << std::endl;
783  std::cout << beam_fit_dphi << std::endl;
784  }
785  /*
786  myalgo->SetFitVariable(std::string("d*z"));
787  myalgo->SetFitType(std::string("likelihood"));
788  reco::BeamSpot beam_fit_dz_lh = myalgo->Fit();
789  if (debug_){
790  std::cout << " Log-Likelihood Fit:" << std::endl;
791  std::cout << beam_fit_dz_lh << std::endl;
792  }
793 
794  myalgo->SetFitVariable(std::string("d*z"));
795  myalgo->SetFitType(std::string("resolution"));
796  reco::BeamSpot beam_fit_dresz_lh = myalgo->Fit();
797  if(debug_){
798  std::cout << " IP Resolution Fit" << std::endl;
799  std::cout << beam_fit_dresz_lh << std::endl;
800 
801  std::cout << "c0 = " << myalgo->GetResPar0() << " +- " << myalgo->GetResPar0Err() << std::endl;
802  std::cout << "c1 = " << myalgo->GetResPar1() << " +- " << myalgo->GetResPar1Err() << std::endl;
803  }
804  */
805  }
806  else
807  if (debug_) std::cout << "No good track selected! No beam fit!" << std::endl;
808 }
809 
RunNumber_t run() const
Definition: EventID.h:39
int stripTOBLayersWithMeasurement() const
Definition: HitPattern.cc:606
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:31
T getParameter(std::string const &) const
double trk_MaxZ_
Definition: BeamFitter.h:158
double trk_MaxEta_
Definition: BeamFitter.h:159
reco::BeamSpot fbeamspot
Definition: BeamFitter.h:140
double z0() const
z coordinate
Definition: BeamSpot.h:68
T getUntrackedParameter(std::string const &, T const &) const
double fwidthXErr
Definition: BeamFitter.h:242
double fwidthX
Definition: BeamFitter.h:240
std::vector< std::string > trk_Algorithm_
Definition: BeamFitter.h:164
double sigmaZ0Error() const
error on sigma z
Definition: BeamSpot.h:96
void setTree(TTree *tree)
Definition: PVFitter.cc:187
int fendLumiOfFit
Definition: BeamFitter.h:225
double fwidthY
Definition: BeamFitter.h:241
int countPass[9]
Definition: BeamFitter.h:248
bool isMuon_
Definition: BeamFitter.h:172
bool runPVandTrkFitter()
Definition: BeamFitter.cc:398
void write2DB()
Definition: BeamFitter.cc:704
def copy(args, dbName)
int stripTIBLayersWithMeasurement() const
Definition: HitPattern.cc:584
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
reco::BeamSpot Fit()
Definition: BSFitter.cc:108
char fendTimeOfFit[32]
Definition: BeamFitter.h:227
double dydzError() const
error on dydz
Definition: BeamSpot.h:100
double fdxdz
Definition: BeamFitter.h:232
double fx
Definition: BeamFitter.h:228
double fnormchi2
Definition: BeamFitter.h:192
bool ffilename_changed
Definition: BeamFitter.h:174
bool debug_
Definition: BeamFitter.h:147
std::map< int, reco::BeamSpot > getBeamSpotMap()
Definition: PVFitter.h:92
bool runBeamWidthFitter()
Definition: BeamFitter.cc:579
void SetSigmaZ(double val)
set sigma Z, RMS bunch length
TH1F * h1z
Definition: BeamFitter.h:181
void SetMaximumZ(double z)
Definition: BSFitter.h:61
int fnTOBLayerMeas
Definition: BeamFitter.h:204
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
double feta
Definition: BeamFitter.h:190
std::vector< std::string > trk_Quality_
Definition: BeamFitter.h:165
double convergence_
Definition: BeamFitter.h:169
double trk_MaxNormChi2_
Definition: BeamFitter.h:163
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:63
void dumpBWTxtFile(std::string &)
Definition: BeamFitter.cc:614
size_type size() const
double fsigmaZErr
Definition: BeamFitter.h:237
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:29
TH1F * h1vz_event
Definition: BeamFitter.h:246
void SetCovariance(int i, int j, double val)
set i,j element of the full covariance matrix 7x7
double fvy
Definition: BeamFitter.h:212
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:501
void SetInputBeamWidth(double val)
Definition: BSFitter.h:66
#define nullptr
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void Setdydz(double val)
set dydz slope, crossing angle in XZ
int fnPixelLayerMeas
Definition: BeamFitter.h:200
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:520
int pixelEndcapLayersWithMeasurement() const
Definition: HitPattern.cc:572
void readEvent(const edm::Event &iEvent)
Definition: BeamFitter.cc:216
double fphi0
Definition: BeamFitter.h:193
std::ofstream fasciiDIP
Definition: BeamFitter.h:145
double emittanceX() const
additional information
Definition: BeamSpot.h:136
void setType(BeamType type)
set beam type
Definition: BeamSpot.h:131
reco::BeamSpot fbeamWidthFit
Definition: BeamFitter.h:141
std::time_t freftime[2]
Definition: BeamFitter.h:219
PVFitter * MyPVFitter
Definition: BeamFitter.h:250
double betaStar() const
Definition: BeamSpot.h:138
std::vector< BSTrkParameters > fBSvector
Definition: BeamFitter.h:139
int fnTotLayerMeas
Definition: BeamFitter.h:199
double dydz() const
dydz slope
Definition: BeamSpot.h:84
std::string outputTxt_
Definition: BeamFitter.h:155
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
double fpvx
Definition: BeamFitter.h:218
int iEvent
Definition: GenABIO.cc:230
double fdxdzErr
Definition: BeamFitter.h:238
double trk_MaxIP_
Definition: BeamFitter.h:160
void readEvent(const edm::Event &iEvent)
Definition: PVFitter.cc:99
bool runFitter()
Definition: BeamFitter.cc:568
void setBeamWidthY(double v)
Definition: BeamSpot.h:109
double emittanceY() const
Definition: BeamSpot.h:137
bool runFitterNoTxt()
Definition: BeamFitter.cc:500
TFile * file_
Definition: BeamFitter.h:186
double dxdzError() const
error on dxdz
Definition: BeamSpot.h:98
TTree * ftree_
Definition: BeamFitter.h:187
double fyErr
Definition: BeamFitter.h:235
bool falgo
Definition: BeamFitter.h:216
bool isNewTagRequest(const std::string &recordName)
int ftotal_tracks
Definition: BeamFitter.h:170
int min_Ntrks_
Definition: BeamFitter.h:171
int stripTIDLayersWithMeasurement() const
Definition: HitPattern.cc:595
T sqrt(T t)
Definition: SSEVec.h:18
TH1F * h1cutFlow
Definition: BeamFitter.h:247
double fdydz
Definition: BeamFitter.h:233
TTree * fPVTree_
Definition: BeamFitter.h:251
std::vector< reco::TrackBase::TrackAlgorithm > algorithm_
Definition: BeamFitter.h:167
bool isAvailable() const
Definition: Service.h:46
TH1F * h1ntrks
Definition: BeamFitter.h:245
char fbeginTimeOfFit[32]
Definition: BeamFitter.h:226
double fpvz
Definition: BeamFitter.h:218
def pv(vc)
Definition: MetAnalyzer.py:6
double fz0
Definition: BeamFitter.h:197
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double ftheta
Definition: BeamFitter.h:188
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:86
double fd0
Definition: BeamFitter.h:194
bool fquality
Definition: BeamFitter.h:215
double fy
Definition: BeamFitter.h:229
void Setdxdz(double val)
set dxdz slope, crossing angle
double BeamWidthYError() const
error on beam width Y, assume error in X = Y
Definition: BeamSpot.h:105
int fnStripLayerMeas
Definition: BeamFitter.h:201
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
double fwidthYErr
Definition: BeamFitter.h:243
unsigned long long TimeValue_t
Definition: Timestamp.h:28
double fxErr
Definition: BeamFitter.h:234
bool appendRunTxt_
Definition: BeamFitter.h:148
void setVx(double vx)
int fnTIDLayerMeas
Definition: BeamFitter.h:203
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
double dxdz() const
dxdz slope
Definition: BeamSpot.h:82
bool saveNtuple_
Definition: BeamFitter.h:182
double x0Error() const
error on x
Definition: BeamSpot.h:90
double y0Error() const
error on y
Definition: BeamSpot.h:92
int k[5][pyjets_maxn]
double fsigmaZ
Definition: BeamFitter.h:231
int fbeginLumiOfFit
Definition: BeamFitter.h:224
std::string outputfilename_
Definition: BeamFitter.h:185
bool writeTxt_
Definition: BeamFitter.h:152
double fsigmad0
Definition: BeamFitter.h:196
std::string outputDIPTxt_
Definition: BeamFitter.h:156
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:125
bool fpvValid
Definition: BeamFitter.h:217
double fdydzErr
Definition: BeamFitter.h:239
virtual ~BeamFitter()
Definition: BeamFitter.cc:188
void dumpTxtFile(std::string &, bool)
Definition: BeamFitter.cc:624
std::vector< float > ForDIPPV_
Definition: BeamFitter.h:177
T const * product() const
Definition: Handle.h:81
double inputBeamWidth_
Definition: BeamFitter.h:168
reco::BeamSpot Fit_ited0phi()
Definition: BSFitter.cc:401
bool savePVVertices_
Definition: BeamFitter.h:184
bool runFitter()
Definition: PVFitter.cc:316
void SetFitVariable(std::string name)
Definition: BSFitter.h:45
void updateBTime()
Definition: BeamFitter.cc:29
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
int stripLayersWithMeasurement() const
Definition: HitPattern.h:1036
int pixelBarrelLayersWithMeasurement() const
Definition: HitPattern.cc:560
double BeamWidthY() const
beam width Y
Definition: BeamSpot.h:88
void runAllFitter()
Definition: BeamFitter.cc:744
void SetMinimumNTrks(int n)
Definition: BSFitter.h:63
void resetAll()
Definition: PVFitter.h:80
bool writeDIPTxt_
Definition: BeamFitter.h:153
double fvx
Definition: BeamFitter.h:211
void setVy(double vy)
void SetBeamWidthX(double val)
set average transverse beam width X
double fpt
Definition: BeamFitter.h:189
void SetBeamWidthY(double val)
set average transverse beam width Y
bool runBXFitter()
Definition: PVFitter.cc:192
void Setd0Cut_d0phi(double d0cut)
Definition: BSFitter.cc:641
std::map< int, reco::BeamSpot > fbspotPVMap
Definition: BeamFitter.h:142
double covariance(int i, int j) const
(i,j)-th element of error matrix
Definition: BeamSpot.h:112
edm::EventID id() const
Definition: EventBase.h:60
double fcov[7][7]
Definition: BeamFitter.h:210
std::vector< reco::TrackBase::TrackQuality > quality_
Definition: BeamFitter.h:166
double fsigmaz0
Definition: BeamFitter.h:198
int trk_MinNPixLayers_
Definition: BeamFitter.h:162
int fnTIBLayerMeas
Definition: BeamFitter.h:202
TH1F * GetVzHisto()
Definition: BSFitter.h:105
TTree * ftreeFit_
Definition: BeamFitter.h:222
int stripTECLayersWithMeasurement() const
Definition: HitPattern.cc:617
static TrackAlgorithm algoByName(const std::string &name)
Definition: TrackBase.cc:137
double y0() const
y coordinate
Definition: BeamSpot.h:66
void resetCutFlow()
Definition: BeamFitter.h:105
double fd0bs
Definition: BeamFitter.h:195
const Point & position() const
position
Definition: BeamSpot.h:62
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
Definition: BeamFitter.h:149
void dumpBeamSpotTxt(std::ofstream &outFile, BeamSpotContainer const &bsContainer)
double trk_MinpT_
Definition: BeamFitter.h:157
reco::BeamSpot getBeamSpot()
Definition: PVFitter.h:91
bool writeDIPBadFit_
Definition: BeamFitter.h:154
void SetConvergence(double val)
Definition: BSFitter.h:62
reco::BeamSpot Fit_d0phi()
Definition: BSFitter.cc:457
double fzErr
Definition: BeamFitter.h:236
int fnPXBLayerMeas
Definition: BeamFitter.h:206
TimeValue_t value() const
Definition: Timestamp.h:56
bool IsFitPerBunchCrossing()
Definition: PVFitter.h:93
edm::Timestamp time() const
Definition: EventBase.h:61
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
double fpvy
Definition: BeamFitter.h:218
bool saveBeamFit_
Definition: BeamFitter.h:183
edm::EDGetTokenT< edm::View< reco::Vertex > > vertexToken_
Definition: BeamFitter.h:150
double fz
Definition: BeamFitter.h:230
double getWidthXerr()
Definition: PVFitter.h:57
bool fitted_
Definition: BeamFitter.h:173
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
Definition: BeamFitter.h:151
int fnPXFLayerMeas
Definition: BeamFitter.h:207
BeamType type() const
return beam type
Definition: BeamSpot.h:129
int fnTECLayerMeas
Definition: BeamFitter.h:205
int trk_MinNTotLayers_
Definition: BeamFitter.h:161
void SetPosition(double x, double y, double z)
set XYZ position
double x0() const
x coordinate
Definition: BeamSpot.h:64
void resetRefTime()
Definition: BeamFitter.h:56
void SetFitType(std::string type)
Definition: BSFitter.h:41