diff --git a/HelperFunction/interface/HelperFunction.h b/HelperFunction/interface/HelperFunction.h index 485ab79..ed20115 100644 --- a/HelperFunction/interface/HelperFunction.h +++ b/HelperFunction/interface/HelperFunction.h @@ -94,18 +94,20 @@ class HelperFunction void setdebug(int d){debug_= d;}; //ForZ - double pterr(reco::Candidate *c, bool isData); + double pterr(const reco::Candidate *c, bool isData) const; //double pterr(pat::Electron electron, bool isData); //double pterr(pat::Muon muon, bool isData); - double pterr(TLorentzVector fsrPhoton); + double pterr(const TLorentzVector& fsrPhoton) const; - double pterr(reco::GsfElectron* electron, bool isData); - double pterr(reco::Muon* muon, bool isData); + double pterr(const reco::GsfElectron* electron, bool isData) const; + double pterr(const reco::Muon* muon, bool isData) const; - double masserror(std::vector p4s, std::vector pTErrs); + double masserror(const std::vector& p4s, + const std::vector& pTErrs) const; - double masserrorFullCov(std::vector p4s, TMatrixDSym covMatrix); + double masserrorFullCov(const std::vector& p4s, + TMatrixDSym& covMatrix) const; //double masserror(std::vector p4s, ) diff --git a/HelperFunction/src/HelperFunction.cc b/HelperFunction/src/HelperFunction.cc index cc1b0bd..2568d06 100644 --- a/HelperFunction/src/HelperFunction.cc +++ b/HelperFunction/src/HelperFunction.cc @@ -78,7 +78,8 @@ HelperFunction::~HelperFunction() // member functions // -double HelperFunction:: masserrorFullCov(std::vector p4s, TMatrixDSym covMatrix){ +double HelperFunction:: masserrorFullCov(const std::vector& p4s, + TMatrixDSym& covMatrix) const { int ndim = 3*p4s.size(); if(debug_) cout<<""< p4s, TMatri } -double HelperFunction::masserror( std::vector Lep, std::vector pterr){ +double HelperFunction::masserror( const std::vector& Lep, + const std::vector& pterr) const { // if(Lep.size()!= pterr.size()!=4) {std::cout<<" Lepsize="< Lep, std::vector (&(*c)) ) != 0) + if ((gsf = dynamic_cast (&(*c)) ) != 0) { pterrLep=pterr(gsf, isData); } - else if ((mu = dynamic_cast (&(*c)) ) != 0) + else if ((mu = dynamic_cast (&(*c)) ) != 0) { pterrLep=pterr(mu, isData); if(debug_)cout<<"reco pt err is "< (&(*c)) )!=0){ + if( (patmu = dynamic_cast (&(*c)) )!=0){ if ( patmu->hasUserFloat("correctedPtError") == true ) { if(debug_) cout<<"use userFloat for muon pt err"< (&(*c)) ) != 0) + else if ((pf = dynamic_cast (&(*c)) ) != 0) { pterrLep=pterr(c, isData); } + else + throw cms::Exception("InvalidCast") << "KinZFitter only takes particles " + << "whose pointers can be cast to " + << "electrons, muons, or PFCandidates" + << std::endl; return pterrLep; } -double HelperFunction::pterr( reco::Muon* mu, bool isData){ +double HelperFunction::pterr( const reco::Muon* mu, bool isData) const { double pterr = mu->muonBestTrack()->ptError(); return pterr; } -double HelperFunction::pterr( reco::GsfElectron * elec, bool isData ){ +double HelperFunction::pterr( const reco::GsfElectron * elec, bool isData ) const { if(debug_) cout<<"reco:gsfelectron pt err"< selectedLeptons, std::map selectedFsrPhotons); + void Setup(const std::vector< const reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhotons); + /// For backwards compatibility + void Setup(const std::vector< reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhotons); /// void KinRefitZ(); @@ -70,17 +75,17 @@ class KinZfitter { double l3, double l4, double lph3, double lph4); // result wrappers - double GetRefitM4l(); - double GetM4l(); - double GetRefitMZ1(); - double GetRefitMZ2(); - double GetMZ1(); - double GetMZ2(); - - double GetMZ1Err(); - double GetRefitM4lErr(); - double GetM4lErr(); - double GetRefitM4lErrFullCov(); + double GetRefitM4l() const; + double GetM4l() const; + double GetRefitMZ1() const; + double GetRefitMZ2() const; + double GetMZ1() const; + double GetMZ2() const; + + double GetMZ1Err() const; + double GetRefitM4lErr() const; + double GetM4lErr() const; + double GetRefitM4lErrFullCov() const; // cov matrix change for spherical coordinate to Cartisean coordinates @@ -99,8 +104,8 @@ class KinZfitter { TMatrixDSym GetRefitZZOFBigCov(); */ - std::vector GetRefitP4s(); - std::vector GetP4s(); + std::vector GetRefitP4s() const; + std::vector GetP4s() const; //////////////// @@ -147,11 +152,12 @@ class KinZfitter { /// HelperFunction class to calcluate per lepton(+photon) pT error HelperFunction * helperFunc_; - void initZs(std::vector< reco::Candidate* > selectedLeptons, std::map selectedFsrPhoton); + void initZs(const std::vector< const reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhoton); void SetFitInput(FitInput &input, - vector ZLep, vector ZLepErr, - vector ZGamma, vector ZGammaErr); + const vector& ZLep, const vector& ZLepErr, + const vector& ZGamma, const vector& ZGammaErr); void SetFitOutput(FitInput &input, FitOutput &output, double &l1, double &l2, double &lph1, double &lph2, @@ -170,7 +176,7 @@ class KinZfitter { vector &Z2Gamma, vector &Z2GammaErr, vector &Z1id, vector &Z2id); - bool IsFourEFourMu(vector &Z1id, vector &Z2id); + bool IsFourEFourMu(const vector &Z1id, const vector &Z2id) const; /// lepton ids for Z1 Z2 std::vector idsZ1_, idsZ2_; /// lepton ids that fsr photon associated to diff --git a/KinZfitter/src/KinZfitter.cpp b/KinZfitter/src/KinZfitter.cpp index 5cccd4e..102f8a2 100644 --- a/KinZfitter/src/KinZfitter.cpp +++ b/KinZfitter/src/KinZfitter.cpp @@ -34,7 +34,13 @@ KinZfitter::KinZfitter(bool isData) } -void KinZfitter::Setup(std::vector< reco::Candidate* > selectedLeptons, std::map selectedFsrPhotons){ +KinZfitter::~KinZfitter() +{ + delete helperFunc_; +} + +void KinZfitter::Setup(const std::vector< const reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhotons){ // reset everything for each event idsZ1_.clear(); idsZ2_.clear(); @@ -93,11 +99,21 @@ void KinZfitter::Setup(std::vector< reco::Candidate* > selectedLeptons, std::map } +void KinZfitter::Setup(const std::vector< reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhotons){ + + // copy whole vector to make each element const separately + std::vector temp(selectedLeptons.begin(), + selectedLeptons.end()); + Setup(temp, selectedFsrPhotons); +} + ///---------------------------------------------------------------------------------------------- ///---------------------------------------------------------------------------------------------- -void KinZfitter::initZs(std::vector< reco::Candidate* > selectedLeptons, std::map selectedFsrPhotons){ +void KinZfitter::initZs(const std::vector< const reco::Candidate* >& selectedLeptons, + const std::map& selectedFsrPhotons){ if(debug_) cout<<"init leptons"< selectedLeptons, std::ma { double pTerr = 0; TLorentzVector p4; - reco::Candidate * c = selectedLeptons[il]; + const reco::Candidate * c = selectedLeptons[il]; pTerr = helperFunc_->pterr(c , isData_); p4.SetPxPyPzE(c->px(),c->py(),c->pz(),c->energy()); int pdgId = c->pdgId(); @@ -132,8 +148,8 @@ void KinZfitter::initZs(std::vector< reco::Candidate* > selectedLeptons, std::ma for(unsigned int ifsr = 0; ifsr<4; ifsr++) { - TLorentzVector p4 = selectedFsrPhotons[ifsr]; - if(selectedFsrPhotons[ifsr].Pt()==0) continue; + if(selectedFsrPhotons.find(ifsr) == selectedFsrPhotons.end()) continue; + TLorentzVector p4 = selectedFsrPhotons.at(ifsr); if(debug_) cout<<"ifsr "< p4s = GetP4s(); @@ -250,7 +266,7 @@ double KinZfitter::GetM4l() } -double KinZfitter::GetRefitM4l() +double KinZfitter::GetRefitM4l() const { vector p4s = GetRefitP4s(); @@ -264,7 +280,7 @@ double KinZfitter::GetRefitM4l() } -double KinZfitter::GetRefitMZ1() +double KinZfitter::GetRefitMZ1() const { vector p4s = GetRefitP4s(); @@ -276,7 +292,7 @@ double KinZfitter::GetRefitMZ1() return pZ1.M(); } -double KinZfitter::GetRefitMZ2() +double KinZfitter::GetRefitMZ2() const { vector p4s = GetRefitP4s(); @@ -289,7 +305,7 @@ double KinZfitter::GetRefitMZ2() } -double KinZfitter::GetMZ1() +double KinZfitter::GetMZ1() const { vector p4s = GetP4s(); @@ -301,7 +317,7 @@ double KinZfitter::GetMZ1() return pZ1.M(); } -double KinZfitter::GetMZ2() +double KinZfitter::GetMZ2() const { vector p4s = GetP4s(); @@ -315,7 +331,7 @@ double KinZfitter::GetMZ2() } -double KinZfitter::GetRefitM4lErr() +double KinZfitter::GetRefitM4lErr() const { vector p4s; @@ -349,7 +365,7 @@ double KinZfitter::GetRefitM4lErr() } -double KinZfitter::GetRefitM4lErrFullCov() +double KinZfitter::GetRefitM4lErrFullCov() const { @@ -460,7 +476,7 @@ double KinZfitter::GetRefitM4lErrFullCov() } -double KinZfitter::GetM4lErr() +double KinZfitter::GetM4lErr() const { vector p4s; @@ -490,7 +506,7 @@ double KinZfitter::GetM4lErr() } -double KinZfitter::GetMZ1Err() +double KinZfitter::GetMZ1Err() const { vector p4s; @@ -511,7 +527,7 @@ double KinZfitter::GetMZ1Err() } -vector KinZfitter::GetRefitP4s() +vector KinZfitter::GetRefitP4s() const { TLorentzVector Z1_1 = p4sZ1REFIT_[0]; TLorentzVector Z1_2 = p4sZ1REFIT_[1]; @@ -547,7 +563,7 @@ vector KinZfitter::GetRefitP4s() } -vector KinZfitter::GetP4s() +vector KinZfitter::GetP4s() const { TLorentzVector Z1_1 = p4sZ1_[0]; TLorentzVector Z1_2 = p4sZ1_[1]; @@ -612,13 +628,12 @@ void KinZfitter::KinRefitZ() if (fourEfourMu) {//4e,4mu, do reshuffle RepairZ1Z2(p4sZ1_, pTerrsZ1_, p4sZ1ph_, pTerrsZ1ph_, p4sZ2_, pTerrsZ2_, p4sZ2ph_, pTerrsZ2ph_, idsZ1_, idsZ2_); - + } SetFitInput(fitInput1, p4sZ1_, pTerrsZ1_, p4sZ1ph_, pTerrsZ1ph_); Driver(fitInput1, fitOutput1); SetFitOutput(fitInput1, fitOutput1, l1, l2, lph1, lph2, pTerrsZ1REFIT_, pTerrsZ1phREFIT_, covMatrixZ1_); - SetFitInput(fitInput2, p4sZ2_, pTerrsZ2_, p4sZ2ph_, pTerrsZ2ph_); Driver(fitInput2, fitOutput2); SetFitOutput(fitInput2, fitOutput2, l3, l4, lph3, lph4, pTerrsZ2REFIT_, pTerrsZ2phREFIT_, covMatrixZ2_); @@ -641,8 +656,8 @@ void KinZfitter::Driver(KinZfitter::FitInput &input, KinZfitter::FitOutput &out void KinZfitter::SetFitInput(KinZfitter::FitInput &input, - vector ZLep, vector ZLepErr, - vector ZGamma, vector ZGammaErr) { + const vector& ZLep, const vector& ZLepErr, + const vector& ZGamma, const vector& ZGammaErr) { TLorentzVector lep1 = ZLep[0]; TLorentzVector lep2 = ZLep[1]; @@ -805,6 +820,8 @@ void KinZfitter::MakeModel(/*RooWorkspace &w,*/ KinZfitter::FitInput &input, Kin PDFRelBW = new RooProdPdf("PDFRelBW", "PDFRelBW", RooArgList(gauss1_lep, gauss2_lep, *RelBW)); if (input.nFsr == 1) { + delete mZ; + delete RelBW; mZ = new RooFormulaVar("mZ", "TMath::Sqrt(2*@0+2*@1+2*@2+@3*@3+@4*@4)", RooArgList(p1D2, p1Dph1, p2Dph1, m1, m2)); RelBW = new RooGenericPdf("RelBW","1/( pow(mZ*mZ-bwMean*bwMean,2)+pow(mZ,4)*pow(bwGamma/bwMean,2) )", RooArgSet(*mZ,bwMean,bwGamma) ); @@ -813,6 +830,8 @@ void KinZfitter::MakeModel(/*RooWorkspace &w,*/ KinZfitter::FitInput &input, Kin } if (input.nFsr == 2) { + delete mZ; + delete RelBW; mZ = new RooFormulaVar("mZ", "TMath::Sqrt(2*@0+2*@1+2*@2+2*@3+2*@4+2*@5+@6*@6+@7*@7)", RooArgList(p1D2,p1Dph1,p2Dph1,p1Dph2,p2Dph2,ph1Dph2, m1, m2)); RelBW = new RooGenericPdf("RelBW","1/( pow(mZ*mZ-bwMean*bwMean,2)+pow(mZ,4)*pow(bwGamma/bwMean,2) )", RooArgSet(*mZ,bwMean,bwGamma) ); @@ -903,6 +922,8 @@ void KinZfitter::MakeModel(/*RooWorkspace &w,*/ KinZfitter::FitInput &input, Kin } */ + if(r) + delete r; delete rastmp; delete pTs; delete PDFRelBW; @@ -914,7 +935,7 @@ void KinZfitter::MakeModel(/*RooWorkspace &w,*/ KinZfitter::FitInput &input, Kin delete PDFRelBWxCBxgauss; } -bool KinZfitter::IsFourEFourMu(vector &Z1id, vector &Z2id) { +bool KinZfitter::IsFourEFourMu(const vector &Z1id, const vector &Z2id) const{ bool flag = false; @@ -1162,13 +1183,14 @@ int KinZfitter::PerZ1Likelihood(double & l1, double & l2, double & lph1, double // mZ1 RooFormulaVar* mZ1; - mZ1 = new RooFormulaVar("mZ1","TMath::Sqrt(2*@0+@1*@1+@2*@2)",RooArgList(p1D2,*m1,*m2)); if(p4sZ1ph_.size()==1) mZ1 = new RooFormulaVar("mZ1","TMath::Sqrt(2*@0+2*@1+2*@2+@3*@3+@4*@4)", RooArgList(p1D2, p1Dph1, p2Dph1, *m1,*m2)); - if(p4sZ1ph_.size()==2) + else if(p4sZ1ph_.size()==2) mZ1 = new RooFormulaVar("mZ1","TMath::Sqrt(2*@0+2*@1+2*@2+2*@3+2*@4+2*@5+@6*@6+@7*@7)", RooArgList(p1D2,p1Dph1,p2Dph1,p1Dph2,p2Dph2,ph1Dph2, *m1,*m2)); + else + mZ1 = new RooFormulaVar("mZ1","TMath::Sqrt(2*@0+@1*@1+@2*@2)",RooArgList(p1D2,*m1,*m2)); if(debug_) cout<<"mZ1 is "<getVal()<=2) + else if(p4sZ1ph_.size()>=2) rastmp = new RooArgSet(*pT1RECO,*pT2RECO,*pTph1RECO,*pTph2RECO); + else + rastmp = new RooArgSet(*pT1RECO,*pT2RECO); RooDataSet* pTs = new RooDataSet("pTs","pTs", *rastmp); pTs->add(*rastmp); @@ -1275,7 +1299,8 @@ int KinZfitter::PerZ1Likelihood(double & l1, double & l2, double & lph1, double } //delete nll; - delete r; + if(r) + delete r; delete mZ1; delete pT1; delete pT2; delete pTph1; delete pTph2; delete pT1RECO; delete pT2RECO; delete pTph1RECO; delete pTph2RECO; @@ -1283,6 +1308,10 @@ int KinZfitter::PerZ1Likelihood(double & l1, double & l2, double & lph1, double delete PDFRelBWxCBxgauss; delete pTs; delete rastmp; + delete m1; delete m2; + delete theta1; delete theta2; delete phi1; delete phi2; + delete thetaph1; delete thetaph2; delete phiph1; delete phiph2; + delete p1v3D2; if(debug_) cout<<"end Z1 refit"<