CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Typedefs | Functions
MuScleFitUtils.h File Reference
#include <CLHEP/Vector/LorentzVector.h>
#include "DataFormats/MuonReco/interface/Muon.h"
#include "DataFormats/MuonReco/interface/MuonFwd.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
#include "SimDataFormats/Track/interface/SimTrackContainer.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "TGraphErrors.h"
#include "TH2F.h"
#include "TMinuit.h"
#include "MuonAnalysis/MomentumScaleCalibration/interface/CrossSectionHandler.h"
#include "MuonAnalysis/MomentumScaleCalibration/interface/BackgroundHandler.h"
#include "MuonAnalysis/MomentumScaleCalibration/interface/ResolutionFunction.h"
#include <vector>

Go to the source code of this file.

Classes

class  biasFunctionBase< T >
 
struct  MuScleFitUtils::byPt
 
struct  MuScleFitUtils::massResolComponentsStruct
 
class  MuScleFitUtils
 
class  resolutionFunctionBase< T >
 
class  scaleFunctionBase< T >
 

Typedefs

typedef
reco::Particle::LorentzVector 
lorentzVector
 

Functions

void likelihood (int &npar, double *grad, double &fval, double *xval, int flag)
 

Typedef Documentation

Definition at line 41 of file MuScleFitUtils.h.

Function Documentation

void likelihood ( int &  npar,
double *  grad,
double &  fval,
double *  xval,
int  flag 
)

Definition at line 1696 of file MuScleFitUtils.cc.

References MuScleFitUtils::applyScale(), MuScleFitUtils::computeWeight(), gather_cfg::cout, MuScleFitUtils::debug, MuScleFitUtils::doScaleFit, reco::tau::disc::Eta(), MuScleFitUtils::iev_, MuScleFitUtils::invDimuonMass(), MuScleFitUtils::likelihoodInLoop_, create_public_lumi_plots::log, MuScleFitUtils::loopCounter, MuScleFitUtils::massProb(), MuScleFitUtils::massResolution(), MuScleFitUtils::minuitLoop_, MuScleFitUtils::normalizationChanged_, MuScleFitUtils::normalizeLikelihoodByEventNumber_, MuScleFitUtils::oldNormalization_, mix_2012_Summer_inTimeOnly_cff::prob, MuScleFitUtils::ReducedSavedPair, MuScleFitUtils::rminPtr_, MuScleFitUtils::SavedPair, and histoStyle::weight.

Referenced by pat::ElectronSelector::filter(), MuScleFitUtils::minimizeLikelihood(), and PFNuclearProducer::produce().

1696  {
1697 
1698  if (MuScleFitUtils::debug>19) std::cout << "[MuScleFitUtils-likelihood]: In likelihood function" << std::endl;
1699 
1700  const lorentzVector * recMu1;
1701  const lorentzVector * recMu2;
1702  lorentzVector corrMu1;
1703  lorentzVector corrMu2;
1704 
1705  // if (MuScleFitUtils::debug>19) {
1706  // int parnumber = (int)(MuScleFitUtils::parResol.size()+MuScleFitUtils::parScale.size()+
1707  // MuScleFitUtils::parCrossSection.size()+MuScleFitUtils::parBgr.size());
1708  // std::cout << "[MuScleFitUtils-likelihood]: Looping on tree with ";
1709  // for (int ipar=0; ipar<parnumber; ipar++) {
1710  // std::cout << "Parameter #" << ipar << " with value " << xval[ipar] << " ";
1711  // }
1712  // std::cout << std::endl;
1713  // }
1714 
1715  // Loop on the tree
1716  // ----------------
1717  double flike = 0;
1718  int evtsinlik = 0;
1719  int evtsoutlik = 0;
1720  // std::cout << "SavedPair.size() = " << MuScleFitUtils::SavedPair.size() << std::endl;
1721  if( MuScleFitUtils::debug>0 ) {
1722  std::cout << "SavedPair.size() = " << MuScleFitUtils::SavedPair.size() << std::endl;
1723  std::cout << "ReducedSavedPair.size() = " << MuScleFitUtils::ReducedSavedPair.size() << std::endl;
1724  }
1725  // for( unsigned int nev=0; nev<MuScleFitUtils::SavedPair.size(); ++nev ) {
1726  for( unsigned int nev=0; nev<MuScleFitUtils::ReducedSavedPair.size(); ++nev ) {
1727 
1728  // recMu1 = &(MuScleFitUtils::SavedPair[nev].first);
1729  // recMu2 = &(MuScleFitUtils::SavedPair[nev].second);
1730  recMu1 = &(MuScleFitUtils::ReducedSavedPair[nev].first);
1731  recMu2 = &(MuScleFitUtils::ReducedSavedPair[nev].second);
1732 
1733  // Compute original mass
1734  // ---------------------
1735  double mass = MuScleFitUtils::invDimuonMass( *recMu1, *recMu2 );
1736 
1737  // Compute weight and reference mass (from original mass)
1738  // ------------------------------------------------------
1740  if( weight!=0. ) {
1741  // Compute corrected mass (from previous biases) only if we are currently fitting the scale
1742  // ----------------------------------------------------------------------------------------
1744 // std::cout << "Original pt1 = " << corrMu1.Pt() << std::endl;
1745 // std::cout << "Original pt2 = " << corrMu2.Pt() << std::endl;
1746  corrMu1 = MuScleFitUtils::applyScale(*recMu1, xval, -1);
1747  corrMu2 = MuScleFitUtils::applyScale(*recMu2, xval, 1);
1748 
1749 // if( (corrMu1.Pt() != corrMu1.Pt()) || (corrMu2.Pt() != corrMu2.Pt()) ) {
1750 // std::cout << "Rescaled pt1 = " << corrMu1.Pt() << std::endl;
1751 // std::cout << "Rescaled pt2 = " << corrMu2.Pt() << std::endl;
1752 // }
1753 // std::cout << "Rescaled pt1 = " << corrMu1.Pt() << std::endl;
1754 // std::cout << "Rescaled pt2 = " << corrMu2.Pt() << std::endl;
1755  }
1756  else {
1757  corrMu1 = *recMu1;
1758  corrMu2 = *recMu2;
1759 
1760 // if( (corrMu1.Pt() != corrMu1.Pt()) || (corrMu2.Pt() != corrMu2.Pt()) ) {
1761 // std::cout << "Not rescaled pt1 = " << corrMu1.Pt() << std::endl;
1762 // std::cout << "Not rescaled pt2 = " << corrMu2.Pt() << std::endl;
1763 // }
1764  }
1765  double corrMass = MuScleFitUtils::invDimuonMass(corrMu1, corrMu2);
1766  double Y = (corrMu1+corrMu2).Rapidity();
1767  double resEta = (corrMu1+corrMu2).Eta();
1768  if( MuScleFitUtils::debug>19 ) {
1769  std::cout << "[MuScleFitUtils-likelihood]: Original/Corrected resonance mass = " << mass
1770  << " / " << corrMass << std::endl;
1771  }
1772 
1773  // Compute mass resolution
1774  // -----------------------
1775  double massResol = MuScleFitUtils::massResolution(corrMu1, corrMu2, xval);
1776  if (MuScleFitUtils::debug>19)
1777  std::cout << "[MuScleFitUtils-likelihood]: Resolution is " << massResol << std::endl;
1778 
1779  // Compute probability of this mass value including background modeling
1780  // --------------------------------------------------------------------
1781  if (MuScleFitUtils::debug>1) std::cout << "calling massProb inside likelihood function" << std::endl;
1782 
1783  // double prob = MuScleFitUtils::massProb( corrMass, resEta, Y, massResol, xval );
1784  double prob = MuScleFitUtils::massProb( corrMass, resEta, Y, massResol, xval, false, corrMu1.eta(), corrMu2.eta() );
1785  if (MuScleFitUtils::debug>1) std::cout << "likelihood:massProb = " << prob << std::endl;
1786 
1787  // Compute likelihood
1788  // ------------------
1789  if( prob>0 ) {
1790  // flike += log(prob*10000)*weight; // NNBB! x10000 to see if we can recover the problem of boundary
1791  flike += log(prob)*weight;
1792  evtsinlik += 1; // NNBB test: see if likelihood per event is smarter (boundary problem)
1793  } else {
1794  if( MuScleFitUtils::debug > 0 ) {
1795  std::cout << "WARNING: corrMass = " << corrMass << " outside window, this will cause a discontinuity in the likelihood. Consider increasing the safety bands which are now set to 90% of the normalization window to avoid this problem" << std::endl;
1796  std::cout << "Original mass was = " << mass << std::endl;
1797  std::cout << "WARNING: massResol = " << massResol << " outside window" << std::endl;
1798  }
1799  evtsoutlik += 1;
1800  }
1801  if (MuScleFitUtils::debug>19)
1802  std::cout << "[MuScleFitUtils-likelihood]: Mass probability = " << prob << std::endl;
1803  } // weight!=0
1804 
1805  } // End of loop on tree events
1806 
1807 // // Protection for low statistic. If the likelihood manages to throw out all the signal
1808 // // events and stays with ~ 10 events in the resonance window it could have a better likelihood
1809 // // because of ~ uniformly distributed events (a random combination could be good and spoil the fit).
1810 // // We require that the number of events included in the fit does not change more than 5% in each minuit loop.
1811 // bool lowStatPenalty = false;
1812 // if( MuScleFitUtils::minuitLoop_ > 0 ) {
1813 // double newEventsOutInRatio = double(evtsinlik);
1814 // // double newEventsOutInRatio = double(evtsoutlik)/double(evtsinlik);
1815 // double ratio = newEventsOutInRatio/MuScleFitUtils::oldEventsOutInRatio_;
1816 // MuScleFitUtils::oldEventsOutInRatio_ = newEventsOutInRatio;
1817 // if( ratio < 0.8 || ratio > 1.2 ) {
1818 // std::cout << "Warning: too much change from oldEventsInLikelihood to newEventsInLikelihood, ratio is = " << ratio << std::endl;
1819 // std::cout << "oldEventsInLikelihood = " << MuScleFitUtils::oldEventsOutInRatio_ << ", newEventsInLikelihood = " << newEventsOutInRatio << std::endl;
1820 // lowStatPenalty = true;
1821 // }
1822 // }
1823 
1824  // It is a product of probabilities, we compare the sqrt_N of them. Thus N becomes a denominator of the logarithm.
1825  if( evtsinlik != 0 ) {
1826 
1828  // && !(MuScleFitUtils::duringMinos_) ) {
1829  if( MuScleFitUtils::rminPtr_ == 0 ) {
1830  std::cout << "ERROR: rminPtr_ = " << MuScleFitUtils::rminPtr_ << ", code will crash" << std::endl;
1831  }
1832  double normalizationArg[] = {1/double(evtsinlik)};
1833  // Reset the normalizationArg only if it changed
1834  if( MuScleFitUtils::oldNormalization_ != normalizationArg[0] ) {
1835  int ierror = 0;
1836 // if( MuScleFitUtils::likelihoodInLoop_ != 0 ) {
1837 // // This condition is set only when minimizing. Later calls of hesse and minos will not change the value
1838 // // This is done to avoid minos being confused by changing the UP parameter during its computation.
1839 // MuScleFitUtils::rminPtr_->mnexcm("SET ERR", normalizationArg, 1, ierror);
1840 // }
1841  MuScleFitUtils::rminPtr_->mnexcm("SET ERR", normalizationArg, 1, ierror);
1842  std::cout << "oldNormalization = " << MuScleFitUtils::oldNormalization_ << " new = " << normalizationArg[0] << std::endl;
1843  MuScleFitUtils::oldNormalization_ = normalizationArg[0];
1845  }
1846  fval = -2.*flike/double(evtsinlik);
1847  // fval = -2.*flike;
1848  // if( lowStatPenalty ) {
1849  // fval *= 100;
1850  // }
1851  }
1852  else {
1853  fval = -2.*flike;
1854  }
1855  }
1856  else {
1857  std::cout << "Problem: Events in likelihood = " << evtsinlik << std::endl;
1858  fval = 999999999.;
1859  }
1860  // fval = -2.*flike;
1861  if (MuScleFitUtils::debug>19)
1862  std::cout << "[MuScleFitUtils-likelihood]: End tree loop with likelihood value = " << fval << std::endl;
1863 
1864 // #ifdef DEBUG
1865 
1866 // if( MuScleFitUtils::minuitLoop_ < 10000 ) {
1870  }
1871  // }
1872  // else std::cout << "minuitLoop over 10000. Not filling histogram" << std::endl;
1873 
1874  std::cout<<"MINUIT loop number "<<MuScleFitUtils::minuitLoop_<<", likelihood = "<<fval<<std::endl;
1875 
1876  if( MuScleFitUtils::debug > 0 ) {
1877  // if( MuScleFitUtils::duringMinos_ ) {
1878  // int parnumber = (int)(MuScleFitUtils::parResol.size()+MuScleFitUtils::parScale.size()+
1879  // MuScleFitUtils::parCrossSection.size()+MuScleFitUtils::parBgr.size());
1880  // std::cout << "[MuScleFitUtils-likelihood]: Looping on tree with ";
1881  // for (int ipar=0; ipar<parnumber; ipar++) {
1882  // std::cout << "Parameter #" << ipar << " with value " << xval[ipar] << " ";
1883  // }
1884  // std::cout << std::endl;
1885  // std::cout << "[MuScleFitUtils-likelihood]: likelihood value = " << fval << std::endl;
1886  // }
1887  std::cout << "Events in likelihood = " << evtsinlik << std::endl;
1888  std::cout << "Events out likelihood = " << evtsoutlik << std::endl;
1889  }
1890 
1891 // #endif
1892 }
static std::vector< int > doScaleFit
static unsigned int loopCounter
static int debug
static unsigned int normalizationChanged_
static double massProb(const double &mass, const double &rapidity, const int ires, const double &massResol)
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:8
static std::vector< std::pair< lorentzVector, lorentzVector > > ReducedSavedPair
static double massResolution(const lorentzVector &mu1, const lorentzVector &mu2)
static int minuitLoop_
static double computeWeight(const double &mass, const int iev, const bool doUseBkgrWindow=false)
static std::vector< std::pair< lorentzVector, lorentzVector > > SavedPair
static double invDimuonMass(const lorentzVector &mu1, const lorentzVector &mu2)
static lorentzVector applyScale(const lorentzVector &muon, const std::vector< double > &parval, const int charge)
static TMinuit * rminPtr_
static TH1D * likelihoodInLoop_
static double oldNormalization_
static int iev_
static bool normalizeLikelihoodByEventNumber_
tuple cout
Definition: gather_cfg.py:121
int weight
Definition: histoStyle.py:50