CMS 3D CMS Logo

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>
37 template <class T>
38 class scaleFunctionBase;
39 class smearFunctionBase;
40 template <class T>
43 class BackgroundHandler;
44 
45 class SimTrack;
46 class TString;
47 class TTree;
48 
50 
52 public:
53  // Constructor
54  // ----------
56 
57  // Destructor
58  // ----------
59  virtual ~MuScleFitUtils() {}
60 
61  // Operations
62  // ----------
63  static std::pair<SimTrack, SimTrack> findBestSimuRes(const std::vector<SimTrack>& simMuons);
64  static std::pair<MuScleFitMuon, MuScleFitMuon> findBestRecoRes(const std::vector<MuScleFitMuon>& muons);
65  static std::pair<lorentzVector, lorentzVector> findGenMuFromRes(const reco::GenParticleCollection* genParticles);
66  static std::pair<lorentzVector, lorentzVector> findGenMuFromRes(const edm::HepMCProduct* evtMC);
67  static std::pair<lorentzVector, lorentzVector> findSimMuFromRes(const edm::Handle<edm::HepMCProduct>& evtMC,
69 
70  static std::vector<TGraphErrors*> fitMass(TH2F* histo);
71  static std::vector<TGraphErrors*> fitReso(TH2F* histo);
72 
73  static lorentzVector applyScale(const lorentzVector& muon, const std::vector<double>& parval, const int charge);
74  static lorentzVector applyScale(const lorentzVector& muon, double* parval, const int charge);
75  static lorentzVector applyBias(const lorentzVector& muon, const int charge);
77  static lorentzVector fromPtEtaPhiToPxPyPz(const double* ptEtaPhiE);
78 
79  static void minimizeLikelihood();
80 
81  static double invDimuonMass(const lorentzVector& mu1, const lorentzVector& mu2);
82  static double massResolution(const lorentzVector& mu1, const lorentzVector& mu2);
83  static double massResolution(const lorentzVector& mu1, const lorentzVector& mu2, const std::vector<double>& parval);
84  static double massResolution(const lorentzVector& mu1, const lorentzVector& mu2, std::unique_ptr<double> parval);
85  static double massResolution(const lorentzVector& mu1, const lorentzVector& mu2, double* parval);
86  static double massResolution(const lorentzVector& mu1, const lorentzVector& mu2, const ResolutionFunction& resolFunc);
87 
88  static double massProb(const double& mass, const double& rapidity, const int ires, const double& massResol);
89  /* static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, const std::vector<double> & parval, const bool doUseBkgrWindow = false ); */
90  /* static double massProb( const double & mass, const double & resEta, const double & rapidity, const double & massResol, double * parval, const bool doUseBkgrWindow = false ); */
91  static double massProb(const double& mass,
92  const double& resEta,
93  const double& rapidity,
94  const double& massResol,
95  const std::vector<double>& parval,
96  const bool doUseBkgrWindow,
97  const double& eta1,
98  const double& eta2);
99  static double massProb(const double& mass,
100  const double& resEta,
101  const double& rapidity,
102  const double& massResol,
103  double* parval,
104  const bool doUseBkgrWindow,
105  const double& eta1,
106  const double& eta2);
107  static double computeWeight(const double& mass, const int iev, const bool doUseBkgrWindow = false);
108 
109  static double deltaPhi(const double& phi1, const double& phi2) {
110  double deltaPhi = phi1 - phi2;
111  while (deltaPhi >= TMath::Pi())
112  deltaPhi -= 2 * TMath::Pi();
113  while (deltaPhi < -TMath::Pi())
114  deltaPhi += 2 * TMath::Pi();
115  return fabs(deltaPhi);
116  }
118  static double deltaPhiNoFabs(const double& phi1, const double& phi2) {
119  double deltaPhi = phi1 - phi2;
120  while (deltaPhi >= TMath::Pi())
121  deltaPhi -= 2 * TMath::Pi();
122  while (deltaPhi < -TMath::Pi())
123  deltaPhi += 2 * TMath::Pi();
124  return deltaPhi;
125  }
126  static double deltaR(const double& eta1, const double& eta2, const double& phi1, const double& phi2) {
127  return sqrt(std::pow(eta1 - eta2, 2) + std::pow(deltaPhi(phi1, phi2), 2));
128  }
129 
130  static int debug; // debug option set by MuScleFit
131  static bool ResFound; // bool flag true if best resonance found (cuts on pt and eta)
132 
133  static const int totalResNum; // Total number of resonance: 6
134  static double massWindowHalfWidth[3][6]; // parameter set by MuScleFitUtils
135  static double ResGamma[6]; // parameter set by MuScleFitUtils
136  static double ResMass[6]; // parameter set by MuScleFitUtils
137  static double ResMinMass[6]; // parameter set by MuScleFitBase
138  static double crossSection[6];
139  static const double mMu2;
140  static const double muMass;
141 
142  // Array of the pdgId of resonances
143  static const unsigned int motherPdgIdArray[6];
144 
145  static unsigned int loopCounter; // parameter set by MuScleFit
146 
147  static int SmearType;
149  static int BiasType;
150  // No error, we take functions from the same group for scale and bias.
152  static int ResolFitType;
155  static int ScaleFitType;
158  static int BgrFitType;
159  // Three background regions:
160  // - one for the Z
161  // - one for the Upsilons
162  // - one for J/Psi and Psi2S
163  static const int backgroundFunctionsRegions;
164  // static backgroundFunctionBase * backgroundFunctionForRegion[];
165  // A background function for each resonance
166  // static backgroundFunctionBase * backgroundFunction[];
167 
168  // The Cross section handler takes care of computing the relative cross
169  // sections to be used depending on the resonances that are being fitted.
170  // This corresponds to a normalization of the signal pdf.
172 
173  // The background handler takes care of using the correct function in each
174  // window, use regions or resonance windows and rescale the fractions when needed
176 
177  // Parameters used to select whether to do a fit
178  static std::vector<int> doResolFit;
179  static std::vector<int> doScaleFit;
180  static std::vector<int> doCrossSectionFit;
181  static std::vector<int> doBackgroundFit;
182 
183  static int minuitLoop_;
184  static TH1D* likelihoodInLoop_;
185  static TH1D* signalProb_;
186  static TH1D* backgroundProb_;
187 
188  static bool duringMinos_;
189 
190  static std::vector<double> parSmear;
191  static std::vector<double> parBias;
192  static std::vector<double> parResol;
193  static std::vector<double> parResolStep;
194  static std::vector<double> parResolMin;
195  static std::vector<double> parResolMax;
196  static std::vector<double> parScale;
197  static std::vector<double> parScaleStep;
198  static std::vector<double> parScaleMin;
199  static std::vector<double> parScaleMax;
200  static std::vector<double> parCrossSection;
201  static std::vector<double> parBgr;
202  static std::vector<int> parResolFix;
203  static std::vector<int> parScaleFix;
204  static std::vector<int> parCrossSectionFix;
205  static std::vector<int> parBgrFix;
206  static std::vector<int> parResolOrder;
207  static std::vector<int> parScaleOrder;
208  static std::vector<int> parCrossSectionOrder;
209  static std::vector<int> parBgrOrder;
210  static std::vector<int> resfind;
211  static int FitStrategy;
212  static bool speedup; // parameter set by MuScleFit - whether to speedup processing
213  static double x[7][10000]; // smearing values set by MuScleFit constructor
214  static int goodmuon; // number of events with a usable resonance
215  static int counter_resprob; // number of times there are resolution problems
216  static double GLZValue[40][1001][1001]; // matrix with integral values of Lorentz * Gaussian
217  static double GLZNorm[40][1001]; // normalization values per each sigma
218  static double GLValue[6][1001][1001]; // matrix with integral values of Lorentz * Gaussian
219  static double GLNorm[6][1001]; // normalization values per each sigma
220  static double ResMaxSigma[6]; // max sigma of matrix
221  static double ResHalfWidth[6]; // halfwidth in matrix
222  static int nbins; // number of bins in matrix
223  static int MuonType; // 0, 1, 2 - 0 is GM, 1 is SM, 2 is track
224  static int MuonTypeForCheckMassWindow; // Reduced to be 0, 1 or 2. It is = MuonType when MuonType < 3, = 2 otherwise.
225 
226  static std::vector<std::vector<double> > parvalue;
227  // static std::map<unsigned int,std::vector<double> > parvalue;
228  static std::vector<int> parfix;
229  static std::vector<int> parorder;
230 
231  static std::vector<std::pair<lorentzVector, lorentzVector> > SavedPair;
232  static std::vector<std::pair<lorentzVector, lorentzVector> > ReducedSavedPair;
233  static std::vector<std::pair<lorentzVector, lorentzVector> > genPair;
234  static std::vector<std::pair<MuScleFitMuon, MuScleFitMuon> > SavedPairMuScleFitMuons;
235  static std::vector<std::pair<MuScleFitMuon, MuScleFitMuon> > genMuscleFitPair;
236  static std::vector<std::pair<lorentzVector, lorentzVector> > simPair;
237 
238  static bool scaleFitNotDone_;
239 
241  // Pointer to the minuit object
242  static TMinuit* rminPtr_;
243  // Value stored to check whether to apply a new normalization to the likelihood
244  static double oldNormalization_;
245  static unsigned int normalizationChanged_;
246 
247  // This must be set to true if using events generated with Sherpa
248  static bool sherpa_;
249 
250  // Decide whether to use the rapidity bins for the Z
251  static bool rapidityBinsForZ_;
252 
253  static int iev_;
254 
255  static bool useProbsFile_;
256 
257  // Cuts on the muons to use in the fit
258  static bool separateRanges_;
259  static double minMuonPt_;
260  static double maxMuonPt_;
261  static double minMuonEtaFirstRange_;
262  static double maxMuonEtaFirstRange_;
263  static double minMuonEtaSecondRange_;
264  static double maxMuonEtaSecondRange_;
265  static double deltaPhiMinCut_;
266  static double deltaPhiMaxCut_;
267 
268  static bool debugMassResol_;
270  double dmdpt1;
271  double dmdpt2;
272  double dmdphi1;
273  double dmdphi2;
274  double dmdcotgth1;
275  double dmdcotgth2;
277 
278  // Fit accuracy and debug parameters
279  static bool startWithSimplex_;
280  static bool computeMinosErrors_;
281  static bool minimumShapePlots_;
282 
284  // static bool checkMassWindow( const double & mass, const int ires, const double & resMass, const double & leftFactor = 1., const double & rightFactor = 1. );
285  static bool checkMassWindow(const double& mass, const double& leftBorder, const double& rightBorder);
286 
288  static double probability(const double& mass,
289  const double& massResol,
290  const double GLvalue[][1001][1001],
291  const double GLnorm[][1001],
292  const int iRes,
293  const int iY);
294 
295 protected:
296 private:
297  struct byPt {
298  bool operator()(const reco::Muon& a, const reco::Muon& b) const { return a.pt() > b.pt(); }
299  };
300 };
301 
302 extern "C" void likelihood(int& npar, double* grad, double& fval, double* xval, int flag);
303 
304 #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
static std::vector< std::pair< MuScleFitMuon, MuScleFitMuon > > genMuscleFitPair
static bool startWithSimplex_
static std::vector< int > doBackgroundFit
static std::vector< double > parResol
static bool debugMassResol_
static double ResMinMass[6]
static TH1D * backgroundProb_
static BackgroundHandler * backgroundHandler
void likelihood(int &npar, double *grad, double &fval, double *xval, int flag)
static double x[7][10000]
static int debug
static std::vector< TGraphErrors * > fitMass(TH2F *histo)
static double ResMass[6]
static double massWindowHalfWidth[3][6]
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
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)
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:214
static struct MuScleFitUtils::massResolComponentsStruct massResolComponents
static int BiasType
static std::vector< int > parScaleFix
reco::Particle::LorentzVector lorentzVector
static bool computeMinosErrors_
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
bool operator()(const reco::Muon &a, const reco::Muon &b) const
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:23
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)
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]
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:21
static int FitStrategy
static double maxMuonEtaFirstRange_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
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