34 #include "Minuit2/FCNBase.h" 35 #include "Minuit2/FunctionMinimum.h" 36 #include "Minuit2/MnMigrad.h" 37 #include "Minuit2/MnPrint.h" 39 #include "TDirectory.h" 40 #include "TMinuitMinimizer.h" 65 TMinuitMinimizer::UseStaticMinuit(
false);
69 .getUntrackedParameter<edm::InputTag>(
"VertexCollection",
edm::InputTag(
"offlinePrimaryVertices")));
113 for (reco::VertexCollection::const_iterator
pv = PVCollection->begin();
pv != PVCollection->end(); ++
pv ) {
115 if (
pv != PVCollection->begin())
break;
119 if (
pv->isFake() ||
pv->tracksSize()==0 )
continue;
125 const auto& testTrack =
pv->trackRefAt(0);
126 if(testTrack.isNull() || !testTrack.isAvailable())
128 edm::LogInfo(
"") <<
"Track collection not found. Skipping cut on sumPt.";
133 for(
auto iTrack =
pv->tracks_begin(); iTrack !=
pv->tracks_end(); ++iTrack)
135 const auto pt = (*iTrack)->pt();
166 pvData.
posCorr[0] =
pv->covariance(0,1)/
pv->xError()/
pv->yError();
167 pvData.
posCorr[1] =
pv->covariance(0,2)/
pv->xError()/
pv->zError();
168 pvData.
posCorr[2] =
pv->covariance(1,2)/
pv->yError()/
pv->zError();
195 edm::LogInfo(
"PVFitter") <<
" Number of bunch crossings: " <<
bxMap_.size() << std::endl;
199 for (
std::map<
int,std::vector<BeamSpotFitPVData> >::const_iterator pvStore =
bxMap_.begin();
200 pvStore!=
bxMap_.end(); ++pvStore) {
206 edm::LogInfo(
"PVFitter") <<
" Number of PVs collected for PVFitter: " << (pvStore->second).
size() <<
" in bx: " << pvStore->first << std::endl;
209 edm::LogWarning(
"PVFitter") <<
" not enough PVs, continue" << std::endl;
214 edm::LogInfo(
"PVFitter") <<
"Calculating beam spot with PVs ..." << std::endl;
223 MnUserParameters upar;
224 upar.Add(
"x" , 0. , 0.02 , -10., 10.);
225 upar.Add(
"y" , 0. , 0.02 , -10., 10.);
226 upar.Add(
"z" , 0. , 0.20 , -30., 30.);
227 upar.Add(
"ex" , 0.015, 0.01 , 0. , 10.);
228 upar.Add(
"corrxy", 0. , 0.02 , -1. , 1. );
229 upar.Add(
"ey" , 0.015, 0.01 , 0. , 10.);
230 upar.Add(
"dxdz" , 0. , 0.0002, -0.1, 0.1);
231 upar.Add(
"dydz" , 0. , 0.0002, -0.1, 0.1);
232 upar.Add(
"ez" , 1. , 0.1 , 0. , 30.);
235 MnMigrad migrad(*fcn, upar);
244 FunctionMinimum ierr = migrad(0,1.);
245 if ( !ierr.IsValid() ) {
246 edm::LogInfo(
"PVFitter") <<
"3D beam spot fit failed in 1st iteration" << std::endl;
260 if ( !ierr.IsValid() ) {
261 edm::LogInfo(
"PVFitter") <<
"3D beam spot fit failed in 2nd iteration" << std::endl;
272 if ( !ierr.IsValid() ) {
273 edm::LogInfo(
"PVFitter") <<
"3D beam spot fit failed in 3rd iteration" << std::endl;
299 upar.Value(6), upar.Value(7),
307 edm::LogInfo(
"PVFitter") <<
"3D PV fit done for this bunch crossing."<<std::endl;
309 fit_ok = fit_ok &
true;
319 edm::LogInfo(
"PVFitter") <<
" Number of PVs collected for PVFitter: " <<
pvStore_.size() << std::endl;
325 TDirectory::TContext guard{
nullptr};
326 std::ostringstream
str;
328 std::unique_ptr<TH1D> h1PVx{
hPVx->ProjectionX( (
std::string(
"h1PVx")+str.str()).c_str(), 0, -1,
"e") };
329 std::unique_ptr<TH1D> h1PVy{
hPVy->ProjectionX( (
std::string(
"h1PVy")+str.str()).c_str(), 0, -1,
"e") };
330 std::unique_ptr<TH1D> h1PVz{
hPVx->ProjectionY( (
std::string(
"h1PVz")+str.str()).c_str(), 0, -1,
"e") };
334 TF1 gausx(
"localGausX",
"gaus",0.,1.,TF1::EAddToList::kNo);
335 TF1 gausy(
"localGausY",
"gaus",0.,1.,TF1::EAddToList::kNo);
336 TF1 gausz(
"localGausZ",
"gaus",0.,1.,TF1::EAddToList::kNo);
338 h1PVx->Fit(&gausx,
"QLMN0 SERIAL");
339 h1PVy->Fit(&gausy,
"QLMN0 SERIAL");
340 h1PVz->Fit(&gausz,
"QLMN0 SERIAL");
343 fwidthX = gausx.GetParameter(2);
344 fwidthY = gausy.GetParameter(2);
345 fwidthZ = gausz.GetParameter(2);
350 double estX = gausx.GetParameter(1);
351 double estY = gausy.GetParameter(1);
352 double estZ = gausz.GetParameter(1);
360 matrix(2,2) = gausz.GetParError(1) * gausz.GetParError(1);
365 gausy.GetParameter(1),
366 gausz.GetParameter(1) ),
384 MnUserParameters upar;
385 upar.Add(
"x" , estX , errX , -10. , 10. );
386 upar.Add(
"y" , estY , errY , -10. , 10. );
387 upar.Add(
"z" , estZ , errZ , -30. , 30. );
388 upar.Add(
"ex" , 0.015 , 0.01 , 0. , 10. );
389 upar.Add(
"corrxy", 0. , 0.02 , -1. , 1. );
390 upar.Add(
"ey" , 0.015 , 0.01 , 0. , 10. );
391 upar.Add(
"dxdz" , 0. , 0.0002 , -0.1 , 0.1 );
392 upar.Add(
"dydz" , 0. , 0.0002 , -0.1 , 0.1 );
393 upar.Add(
"ez" , 1. , 0.1 , 0. , 30. );
395 MnMigrad migrad(*fcn, upar);
403 FunctionMinimum ierr = migrad(0,1.);
404 if ( !ierr.IsValid() ) {
405 edm::LogWarning(
"PVFitter") <<
"3D beam spot fit failed in 1st iteration" << std::endl;
414 results = ierr.UserParameters().Params() ; \
415 errors = ierr.UserParameters().Errors() ; \
418 fcn->setLimits(results[0]-
sigmaCut_*results[3],
425 if ( !ierr.IsValid() ) {
426 edm::LogWarning(
"PVFitter") <<
"3D beam spot fit failed in 2nd iteration" << std::endl;
436 if ( !ierr.IsValid() ) {
437 edm::LogWarning(
"PVFitter") <<
"3D beam spot fit failed in 3rd iteration" << std::endl;
441 results = ierr.UserParameters().Params() ; \
442 errors = ierr.UserParameters().Errors() ; \
444 fwidthX = results[3];
453 edm::LogWarning(
"PVFitter") <<
"3D beam spot fit returns nan in 3rd iteration" << std::endl;
469 results[6], results[7],
504 unsigned int iwrite(0);
505 for (
unsigned int i=0; i<
pvStore_.size(); ++
i ) {
511 edm::LogInfo(
"PVFitter") <<
"Reduced primary vertex store size to " 512 <<
pvStore_.size() <<
" ; new dynamic quality cut = " 513 << dynamicQualityCut_ << std::endl;
std::vector< double > pvQualities_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
math::Error< dimension >::type CovarianceMatrix
T getParameter(std::string const &) const
void setTree(TTree *tree)
std::map< int, std::vector< BeamSpotFitPVData > > bxMap_
void initialize(const edm::ParameterSet &iConfig, edm::ConsumesCollector &iColl)
void run(unsigned int run)
void compressStore()
reduce size of primary vertex cache by increasing quality limit
bool getByToken(EDGetToken token, Handle< PROD > &result) const
int bunchCrossing() const
edm::LuminosityBlockNumber_t luminosityBlock() const
std::vector< Vertex > VertexCollection
collection of Vertex objects
math::XYZPoint Point
point in the space
double covariance(int i, int j) const
(i, j)-th element of error matrix, i, j = 0, ... 2
double dynamicQualityCut_
void setType(BeamType type)
set beam type
void bunchCrossing(unsigned int bunchCrossing)
void lumi(unsigned int lumi)
std::vector< BeamSpotFitPVData > pvStore_
void readEvent(const edm::Event &iEvent)
void setBeamWidthY(double v)
BeamSpotTreeData theBeamSpotTreeData_
bool fFitPerBunchCrossing
void pvData(const BeamSpotFitPVData &pvData)
std::map< int, reco::BeamSpot > fbspotMap
edm::EDGetTokenT< reco::VertexCollection > vertexToken_
double pvQuality(const reco::Vertex &pv) const
vertex quality measure
unsigned int maxNrVertices_
unsigned int minVtxTracks_
void fcn(int &, double *, double &, double *, int)
unsigned int minNrVertices_
void setLimits(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
void setBeamWidthX(double v)
Power< A, B >::type pow(const A &a, const B &b)