CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuScleFitUtils.h
Go to the documentation of this file.
1 #ifndef MuScleFitUtils_H
2 #define MuScleFitUtils_H
3 
12 #include <CLHEP/Vector/LorentzVector.h>
15 // #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
20 #include "TGraphErrors.h"
21 #include "TH2F.h"
22 #include "TMinuit.h"
23 
29 
30 #include <vector>
31 
32 // #include "Functions.h"
33 // class biasFunctionBase<std::vector<double> >;
34 // class scaleFunctionBase<double*>;
35 template <class T> class biasFunctionBase;
36 template <class T> class scaleFunctionBase;
37 class smearFunctionBase;
38 template <class T> class resolutionFunctionBase;
40 class BackgroundHandler;
41 
42 class SimTrack;
43 class TString;
44 class TTree;
45 
47 
49 public:
50 
51  // Constructor
52  // ----------
54 
55  // Destructor
56  // ----------
57  virtual ~MuScleFitUtils() {};
58 
59  // Operations
60  // ----------
61  static std::pair<SimTrack, SimTrack> findBestSimuRes( const std::vector<SimTrack>& simMuons );
62  static std::pair<MuScleFitMuon, MuScleFitMuon> findBestRecoRes( const std::vector<MuScleFitMuon>& muons );
63  static std::pair<lorentzVector, lorentzVector> findGenMuFromRes( const reco::GenParticleCollection* genParticles);
64  static std::pair<lorentzVector, lorentzVector> findGenMuFromRes( const edm::HepMCProduct* evtMC );
65  static std::pair<lorentzVector, lorentzVector> findSimMuFromRes( const edm::Handle<edm::HepMCProduct> & evtMC,
66  const edm::Handle<edm::SimTrackContainer> & simTracks);
67 
68  static std::vector<TGraphErrors*> fitMass (TH2F* histo);
69  static std::vector<TGraphErrors*> fitReso (TH2F* histo);
70 
71  static lorentzVector applyScale( const lorentzVector & muon, const std::vector<double> & parval, const int charge );
72  static lorentzVector applyScale( const lorentzVector & muon, double* parval, const int charge );
73  static lorentzVector applyBias( const lorentzVector & muon, const int charge );
75  static lorentzVector fromPtEtaPhiToPxPyPz( const double* ptEtaPhiE );
76 
77  static void minimizeLikelihood();
78 
79  static double invDimuonMass( const lorentzVector & mu1, const lorentzVector & mu2 );
80  static double massResolution( const lorentzVector & mu1, const lorentzVector & mu2 );
81  static double massResolution( const lorentzVector & mu1, const lorentzVector & mu2, const std::vector<double> & parval );
82  static double massResolution( const lorentzVector & mu1, const lorentzVector & mu2, std::auto_ptr<double> parval );
83  static double massResolution( const lorentzVector & mu1, const lorentzVector & mu2, double* parval );
84  static double massResolution( const lorentzVector& mu1, const lorentzVector& mu2, const ResolutionFunction & resolFunc );
85 
86  static double massProb( const double & mass, const double & rapidity, const int ires, const double & massResol );
87  /* static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, const std::vector<double> & parval, const bool doUseBkgrWindow = false ); */
88  /* static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, double * parval, const bool doUseBkgrWindow = false ); */
89  static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, const std::vector<double> & parval, const bool doUseBkgrWindow, const double & eta1, const double & eta2 );
90  static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, double * parval, const bool doUseBkgrWindow, const double & eta1, const double & eta2 );
91  static double computeWeight( const double & mass, const int iev, const bool doUseBkgrWindow = false );
92 
93  static double deltaPhi( const double & phi1, const double & phi2 )
94  {
95  double deltaPhi = phi1 - phi2;
96  while(deltaPhi >= TMath::Pi()) deltaPhi -= 2*TMath::Pi();
97  while(deltaPhi < -TMath::Pi()) deltaPhi += 2*TMath::Pi();
98  return fabs(deltaPhi);
99  }
101  static double deltaPhiNoFabs( const double & phi1, const double & phi2 )
102  {
103  double deltaPhi = phi1 - phi2;
104  while(deltaPhi >= TMath::Pi()) deltaPhi -= 2*TMath::Pi();
105  while(deltaPhi < -TMath::Pi()) deltaPhi += 2*TMath::Pi();
106  return deltaPhi;
107  }
108  static double deltaR(const double & eta1, const double & eta2, const double & phi1, const double & phi2)
109  {
110  return sqrt( std::pow( eta1-eta2, 2 ) + std::pow( deltaPhi(phi1, phi2), 2 ) );
111  }
112 
113  static int debug; // debug option set by MuScleFit
114  static bool ResFound; // bool flag true if best resonance found (cuts on pt and eta)
115 
116  static const int totalResNum; // Total number of resonance: 6
117  static double massWindowHalfWidth[3][6]; // parameter set by MuScleFitUtils
118  static double ResGamma[6]; // parameter set by MuScleFitUtils
119  static double ResMass[6]; // parameter set by MuScleFitUtils
120  static double ResMinMass[6]; // parameter set by MuScleFitBase
121  static double crossSection[6];
122  static const double mMu2;
123  static const double muMass;
124 
125  // Array of the pdgId of resonances
126  static const unsigned int motherPdgIdArray[6];
127 
128  static unsigned int loopCounter; // parameter set by MuScleFit
129 
130  static int SmearType;
132  static int BiasType;
133  // No error, we take functions from the same group for scale and bias.
135  static int ResolFitType;
138  static int ScaleFitType;
141  static int BgrFitType;
142  // Three background regions:
143  // - one for the Z
144  // - one for the Upsilons
145  // - one for J/Psi and Psi2S
146  static const int backgroundFunctionsRegions;
147  // static backgroundFunctionBase * backgroundFunctionForRegion[];
148  // A background function for each resonance
149  // static backgroundFunctionBase * backgroundFunction[];
150 
151  // The Cross section handler takes care of computing the relative cross
152  // sections to be used depending on the resonances that are being fitted.
153  // This corresponds to a normalization of the signal pdf.
155 
156  // The background handler takes care of using the correct function in each
157  // window, use regions or resonance windows and rescale the fractions when needed
159 
160  // Parameters used to select whether to do a fit
161  static std::vector<int> doResolFit;
162  static std::vector<int> doScaleFit;
163  static std::vector<int> doCrossSectionFit;
164  static std::vector<int> doBackgroundFit;
165 
166  static int minuitLoop_;
167  static TH1D* likelihoodInLoop_;
168  static TH1D* signalProb_;
169  static TH1D* backgroundProb_;
170 
171  static bool duringMinos_;
172 
173  static std::vector<double> parSmear;
174  static std::vector<double> parBias;
175  static std::vector<double> parResol;
176  static std::vector<double> parResolStep;
177  static std::vector<double> parResolMin;
178  static std::vector<double> parResolMax;
179  static std::vector<double> parScale;
180  static std::vector<double> parScaleStep;
181  static std::vector<double> parScaleMin;
182  static std::vector<double> parScaleMax;
183  static std::vector<double> parCrossSection;
184  static std::vector<double> parBgr;
185  static std::vector<int> parResolFix;
186  static std::vector<int> parScaleFix;
187  static std::vector<int> parCrossSectionFix;
188  static std::vector<int> parBgrFix;
189  static std::vector<int> parResolOrder;
190  static std::vector<int> parScaleOrder;
191  static std::vector<int> parCrossSectionOrder;
192  static std::vector<int> parBgrOrder;
193  static std::vector<int> resfind;
194  static int FitStrategy;
195  static bool speedup; // parameter set by MuScleFit - whether to speedup processing
196  static double x[7][10000]; // smearing values set by MuScleFit constructor
197  static int goodmuon; // number of events with a usable resonance
198  static int counter_resprob;// number of times there are resolution problems
199  static double GLZValue[40][1001][1001]; // matrix with integral values of Lorentz * Gaussian
200  static double GLZNorm[40][1001]; // normalization values per each sigma
201  static double GLValue[6][1001][1001]; // matrix with integral values of Lorentz * Gaussian
202  static double GLNorm[6][1001]; // normalization values per each sigma
203  static double ResMaxSigma[6]; // max sigma of matrix
204  static double ResHalfWidth[6]; // halfwidth in matrix
205  static int nbins; // number of bins in matrix
206  static int MuonType; // 0, 1, 2 - 0 is GM, 1 is SM, 2 is track
207  static int MuonTypeForCheckMassWindow; // Reduced to be 0, 1 or 2. It is = MuonType when MuonType < 3, = 2 otherwise.
208 
209  static std::vector<std::vector<double> > parvalue;
210  // static std::map<unsigned int,std::vector<double> > parvalue;
211  static std::vector<int> parfix;
212  static std::vector<int> parorder;
213 
214  static std::vector<std::pair<lorentzVector,lorentzVector> > SavedPair;
215  static std::vector<std::pair<MuScleFitMuon,MuScleFitMuon> > SavedPairMuScleFitMuons;
216  static std::vector<std::pair<lorentzVector,lorentzVector> > ReducedSavedPair;
217  static std::vector<std::pair<lorentzVector,lorentzVector> > genPair;
218  static std::vector<std::pair<lorentzVector,lorentzVector> > simPair;
219 
220  static bool scaleFitNotDone_;
221 
223  // Pointer to the minuit object
224  static TMinuit * rminPtr_;
225  // Value stored to check whether to apply a new normalization to the likelihood
226  static double oldNormalization_;
227  static unsigned int normalizationChanged_;
228 
229  // This must be set to true if using events generated with Sherpa
230  static bool sherpa_;
231 
232  // Decide whether to use the rapidity bins for the Z
233  static bool rapidityBinsForZ_;
234 
235  static int iev_;
236 
237  static bool useProbsFile_;
238 
239  // Cuts on the muons to use in the fit
240  static bool separateRanges_;
241  static double minMuonPt_;
242  static double maxMuonPt_;
243  static double minMuonEtaFirstRange_;
244  static double maxMuonEtaFirstRange_;
245  static double minMuonEtaSecondRange_;
246  static double maxMuonEtaSecondRange_;
247  static double deltaPhiMinCut_;
248  static double deltaPhiMaxCut_;
249 
250  static bool debugMassResol_;
252  {
253  double dmdpt1;
254  double dmdpt2;
255  double dmdphi1;
256  double dmdphi2;
257  double dmdcotgth1;
258  double dmdcotgth2;
260 
261  // Fit accuracy and debug parameters
262  static bool startWithSimplex_;
263  static bool computeMinosErrors_;
264  static bool minimumShapePlots_;
265 
267  // static bool checkMassWindow( const double & mass, const int ires, const double & resMass, const double & leftFactor = 1., const double & rightFactor = 1. );
268  static bool checkMassWindow( const double & mass, const double & leftBorder, const double & rightBorder );
269 
271  static double probability( const double & mass, const double & massResol,
272  const double GLvalue[][1001][1001], const double GLnorm[][1001],
273  const int iRes, const int iY );
274 
275 protected:
276 
277 private:
278 
279  struct byPt {
280  bool operator() (const reco::Muon &a, const reco::Muon &b) const {
281  return a.pt() > b.pt();
282  }
283  };
284 
285 };
286 
287 extern "C" void likelihood (int& npar, double* grad, double& fval, double* xval, int flag);
288 
289 #endif
static double deltaPhiNoFabs(const double &phi1, const double &phi2)
Without fabs at the end, used to have a symmetric distribution for the resolution fits and variance c...
static std::vector< std::pair< lorentzVector, lorentzVector > > simPair
const double Pi
static std::vector< int > doScaleFit
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
static std::vector< int > doResolFit
static std::pair< lorentzVector, lorentzVector > findGenMuFromRes(const reco::GenParticleCollection *genParticles)
static double GLValue[6][1001][1001]
static std::vector< double > parBias
static std::vector< int > parCrossSectionOrder
static smearFunctionBase * smearFunction
static std::vector< int > parScaleOrder
static std::vector< int > doCrossSectionFit
static void minimizeLikelihood()
static double maxMuonEtaSecondRange_
static std::vector< int > parBgrOrder
static std::vector< int > parfix
static double deltaPhiMaxCut_
static unsigned int loopCounter
static std::vector< double > parResolMax
static std::pair< lorentzVector, lorentzVector > findSimMuFromRes(const edm::Handle< edm::HepMCProduct > &evtMC, const edm::Handle< edm::SimTrackContainer > &simTracks)
static std::vector< std::pair< MuScleFitMuon, MuScleFitMuon > > SavedPairMuScleFitMuons
static int nbins
void likelihood(int &npar, double *grad, double &fval, double *xval, int flag)
static bool startWithSimplex_
static std::vector< int > doBackgroundFit
static std::vector< double > parResol
static bool debugMassResol_
int ires[2]
static double ResMinMass[6]
static TH1D * backgroundProb_
static BackgroundHandler * backgroundHandler
static double x[7][10000]
static int debug
static std::vector< TGraphErrors * > fitMass(TH2F *histo)
static double ResMass[6]
static double massWindowHalfWidth[3][6]
static bool speedup
static bool scaleFitNotDone_
static unsigned int normalizationChanged_
static bool ResFound
static scaleFunctionBase< std::vector< double > > * biasFunction
static bool checkMassWindow(const double &mass, const double &leftBorder, const double &rightBorder)
Method to check if the mass value is within the mass window of the i-th resonance.
static std::vector< int > parBgrFix
static std::vector< double > parResolMin
static bool minimumShapePlots_
virtual ~MuScleFitUtils()
static int MuonTypeForCheckMassWindow
static double minMuonEtaFirstRange_
static double massProb(const double &mass, const double &rapidity, const int ires, const double &massResol)
static struct MuScleFitUtils::massResolComponentsStruct massResolComponents
static int BiasType
static std::vector< int > parScaleFix
static bool computeMinosErrors_
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
static scaleFunctionBase< std::vector< double > > * scaleFunctionForVec
static std::pair< SimTrack, SimTrack > findBestSimuRes(const std::vector< SimTrack > &simMuons)
static std::vector< std::vector< double > > parvalue
static double maxMuonPt_
static std::pair< MuScleFitMuon, MuScleFitMuon > findBestRecoRes(const std::vector< MuScleFitMuon > &muons)
static int ScaleFitType
static std::vector< std::pair< lorentzVector, lorentzVector > > genPair
static std::vector< std::pair< lorentzVector, lorentzVector > > ReducedSavedPair
static const int totalResNum
static const double muMass
static double massResolution(const lorentzVector &mu1, const lorentzVector &mu2)
static const unsigned int motherPdgIdArray[6]
static int minuitLoop_
static std::vector< double > parScaleMin
static lorentzVector applyBias(const lorentzVector &muon, const int charge)
static double GLZNorm[40][1001]
T sqrt(T t)
Definition: SSEVec.h:48
static std::vector< double > parBgr
static int SmearType
static lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
static double ResMaxSigma[6]
static double computeWeight(const double &mass, const int iev, const bool doUseBkgrWindow=false)
static std::vector< double > parScaleStep
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 TH1D * signalProb_
static std::vector< int > parResolFix
static double GLZValue[40][1001][1001]
static TMinuit * rminPtr_
static std::vector< double > parSmear
static std::vector< int > parResolOrder
static bool sherpa_
static int BgrFitType
static TH1D * likelihoodInLoop_
static const int backgroundFunctionsRegions
static std::vector< double > parScaleMax
static std::vector< double > parScale
static double oldNormalization_
static bool duringMinos_
static double ResGamma[6]
static double deltaPhiMinCut_
double b
Definition: hdecay.h:120
static bool rapidityBinsForZ_
static int ResolFitType
static int goodmuon
static bool separateRanges_
static double minMuonEtaSecondRange_
static int iev_
static std::vector< double > parCrossSection
static const double mMu2
static double GLNorm[6][1001]
static std::vector< TGraphErrors * > fitReso(TH2F *histo)
tuple muons
Definition: patZpeak.py:38
static lorentzVector applySmearing(const lorentzVector &muon)
static bool normalizeLikelihoodByEventNumber_
double a
Definition: hdecay.h:121
static int MuonType
static double deltaPhi(const double &phi1, const double &phi2)
static double probability(const double &mass, const double &massResol, const double GLvalue[][1001][1001], const double GLnorm[][1001], const int iRes, const int iY)
Computes the probability given the mass, mass resolution and the arrays with the probabilities and th...
static double minMuonPt_
static scaleFunctionBase< double * > * scaleFunction
static bool useProbsFile_
static std::vector< int > resfind
static double ResHalfWidth[6]
virtual float pt() const GCC11_FINAL
transverse momentum
static int counter_resprob
static resolutionFunctionBase< std::vector< double > > * resolutionFunctionForVec
static std::vector< int > parorder
static double crossSection[6]
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:27
static int FitStrategy
static double maxMuonEtaFirstRange_
bool operator()(const reco::Muon &a, const reco::Muon &b) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
static double deltaR(const double &eta1, const double &eta2, const double &phi1, const double &phi2)
static CrossSectionHandler * crossSectionHandler
static std::vector< double > parResolStep
static std::vector< int > parCrossSectionFix
static resolutionFunctionBase< double * > * resolutionFunction