CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PuppiAlgo.cc
Go to the documentation of this file.
4 #include "fastjet/internal/base.hh"
5 #include "Math/QuantFuncMathCore.h"
6 #include "Math/SpecFuncMathCore.h"
7 #include "Math/ProbFunc.h"
8 #include "TMath.h"
9 
10 
12  fEtaMin = iConfig.getParameter<double>("etaMin");
13  fEtaMax = iConfig.getParameter<double>("etaMax");
14  fPtMin = iConfig.getParameter<double>("ptMin");
15  fNeutralPtMin = iConfig.getParameter<double>("MinNeutralPt"); // Weighted Neutral Pt Cut
16  fNeutralPtSlope = iConfig.getParameter<double>("MinNeutralPtSlope"); // Slope vs #pv
17 
18  std::vector<edm::ParameterSet> lAlgos = iConfig.getParameter<std::vector<edm::ParameterSet> >("puppiAlgos");
19  fNAlgos = lAlgos.size();
20  //Uber Configurable Puppi
21  for(unsigned int i0 = 0; i0 < lAlgos.size(); i0++) {
22  int pAlgoId = lAlgos[i0].getParameter<int > ("algoId");
23  bool pCharged = lAlgos[i0].getParameter<bool> ("useCharged");
24  bool pWeight0 = lAlgos[i0].getParameter<bool> ("applyLowPUCorr");
25  int pComb = lAlgos[i0].getParameter<int> ("combOpt"); // 0=> add in chi2/1=>Multiply p-values
26  double pConeSize = lAlgos[i0].getParameter<double>("cone"); // Min Pt when computing pt and rms
27  double pRMSPtMin = lAlgos[i0].getParameter<double>("rmsPtMin"); // Min Pt when computing pt and rms
28  double pRMSSF = lAlgos[i0].getParameter<double>("rmsScaleFactor"); // Additional Tuning parameter for Jokers
29  fAlgoId .push_back(pAlgoId);
30  fCharged .push_back(pCharged);
31  fAdjust .push_back(pWeight0);
32  fCombId .push_back(pComb);
33  fConeSize .push_back(pConeSize);
34  fRMSPtMin .push_back(pRMSPtMin);
35  fRMSScaleFactor.push_back(pRMSSF);
36  double pRMS = 0;
37  double pMed = 0;
38  double pMean = 0;
39  int pNCount = 0;
40  fRMS .push_back(pRMS);
41  fMedian.push_back(pMed);
42  fMean .push_back(pMean);
43  fNCount.push_back(pNCount);
44  }
45 }
47  fPups .clear();
48  fPupsPV.clear();
49 }
51  fPups .clear();
52  fPupsPV.clear();
53  for(unsigned int i0 = 0; i0 < fNAlgos; i0++) {
54  fMedian[i0] = 0;
55  fRMS [i0] = 0;
56  fMean [i0] = 0;
57  fNCount[i0] = 0;
58  }
59 }
60 void PuppiAlgo::add(const fastjet::PseudoJet &iParticle,const double &iVal,const unsigned int iAlgo) {
61  if(iParticle.pt() < fRMSPtMin[iAlgo]) return;
62  // Change from SRR : Previously used fastjet::PseudoJet::user_index to decide the particle type.
63  // In CMSSW we use the user_index to specify the index in the input collection, so I invented
64  // a new mechanism using the fastjet UserInfo functionality. Of course, it's still just an integer
65  // but that interface could be changed (or augmented) if desired / needed.
66  int puppi_register = std::numeric_limits<int>::lowest();
67  if ( iParticle.has_user_info() ) {
68  PuppiContainer::PuppiUserInfo const * pInfo = dynamic_cast<PuppiContainer::PuppiUserInfo const *>( iParticle.user_info_ptr() );
69  if ( pInfo != 0 ) {
70  puppi_register = pInfo->puppi_register();
71  }
72  }
73  if ( puppi_register == std::numeric_limits<int>::lowest() ) {
74  throw cms::Exception("PuppiRegisterNotSet") << "The puppi register is not set. This must be set before use.\n";
75  }
76  if(fCharged[iAlgo] && std::abs(puppi_register) < 1) return;
77  if(fCharged[iAlgo] && (std::abs(puppi_register) >=1 && std::abs(puppi_register) <=2)) fPupsPV.push_back(iVal);
78  if(fCharged[iAlgo] && std::abs(puppi_register) < 3) return;
79  fPups.push_back(iVal);
80  fNCount[iAlgo]++;
81 }
82 void PuppiAlgo::computeMedRMS(const unsigned int &iAlgo,const double &iPVFrac) {
83  if(iAlgo >= fNAlgos ) return;
84  if(fNCount[iAlgo] == 0) return;
85  int lNBefore = 0;
86  for(unsigned int i0 = 0; i0 < iAlgo; i0++) lNBefore += fNCount[i0];
87  std::sort(fPups.begin()+lNBefore,fPups.begin()+lNBefore+fNCount[iAlgo]);
88  double lCorr = 1.;
89  //if(!fCharged[iAlgo] && fAdjust[iAlgo]) lCorr *= 1. - iPVFrac;
90  if(fAdjust[iAlgo]) lCorr *= 1. - iPVFrac;
91  int lNum0 = 0;
92  for(int i0 = lNBefore; i0 < lNBefore+fNCount[iAlgo]; i0++) {
93  if(fPups[i0] == 0) lNum0 = i0-lNBefore;
94  }
95  //lNum0 = 0;
96  int lNHalfway = lNBefore + lNum0 + int( double( fNCount[iAlgo]-lNum0 )*0.50*lCorr);
97  fMedian[iAlgo] = fPups[lNHalfway];
98  double lMed = fMedian[iAlgo]; //Just to make the readability easier
99 
100  int lNRMS = 0;
101  for(int i0 = lNBefore; i0 < lNBefore+fNCount[iAlgo]; i0++) {
102  fMean[iAlgo] += fPups[i0];
103  if(fPups[i0] == 0) continue;
104  if(!fCharged[iAlgo] && fAdjust[iAlgo] && fPups[i0] > lMed) continue;
105  //if(fAdjust[iAlgo] && fPups[i0] > lMed) continue;
106  lNRMS++;
107  fRMS [iAlgo] += (fPups[i0]-lMed)*(fPups[i0]-lMed);
108  }
109  fMean[iAlgo]/=fNCount[iAlgo];
110  if(lNRMS > 0) fRMS [iAlgo]/=lNRMS;
111  if(fRMS[iAlgo] == 0) fRMS[iAlgo] = 1e-5;
112 
113  fRMS [iAlgo] = sqrt(fRMS[iAlgo]);
114  fRMS [iAlgo] *= fRMSScaleFactor[iAlgo];
115  //if(!fCharged[iAlgo]) std::cout << " Process : " << iAlgo << " Median : " << fMedian[iAlgo] << " +/- " << fRMS[iAlgo] << " -- Begin : " << lNBefore << " -- Total : " << fNCount[iAlgo] << " -- 50% " << lNHalfway << " Fraction less than @ Median : " << std::endl;
116  if(!fAdjust[iAlgo]) return;
117  //Adjust the p-value to correspond to the median
118  std::sort(fPupsPV.begin(),fPupsPV.end());
119  int lNPV = 0; for(unsigned int i0 = 0; i0 < fPupsPV.size(); i0++) if(fPupsPV[i0] <= lMed ) lNPV++;
120  double lAdjust = 1.5*double(lNPV)/double(fPupsPV.size()+fNCount[iAlgo]);
121  if(lAdjust > 0) fMedian[iAlgo] -= sqrt(ROOT::Math::chisquared_quantile(lAdjust,1.)*fRMS[iAlgo]);
122 }
123 //This code is probably a bit confusing
124 double PuppiAlgo::compute(std::vector<double> const &iVals,double iChi2) const {
125  if(fAlgoId[0] == -1) return 1;
126  double lVal = 0.;
127  double lPVal = 1.;
128  int lNDOF = 0;
129  for(unsigned int i0 = 0; i0 < fNAlgos; i0++) {
130  if(fNCount[i0] == 0) return 1.; //in the NoPU case return 1.
131  if(fCombId[i0] == 1 && i0 > 0) { //Compute the previous p-value so that p-values can be multiplieed
132  double pPVal = ROOT::Math::chisquared_cdf(lVal,lNDOF);
133  lPVal *= pPVal;
134  lNDOF = 0;
135  lVal = 0;
136  }
137  double pVal = iVals[i0];
138  //Special Check for any algo with log(0)
139  if(fAlgoId[i0] == 0 && iVals[i0] == 0) pVal = fMedian[i0];
140  if(fAlgoId[i0] == 3 && iVals[i0] == 0) pVal = fMedian[i0];
141  if(fAlgoId[i0] == 5 && iVals[i0] == 0) pVal = fMedian[i0];
142  lVal += (pVal-fMedian[i0])*(fabs(pVal-fMedian[i0]))/fRMS[i0]/fRMS[i0];
143  lNDOF++;
144  if(i0 == 0 && iChi2 != 0) lNDOF++; //Add external Chi2 to first element
145  if(i0 == 0 && iChi2 != 0) lVal+=iChi2; //Add external Chi2 to first element
146  }
147  //Top it off with the last calc
148  lPVal *= ROOT::Math::chisquared_cdf(lVal,lNDOF);
149  return lPVal;
150 }
T getParameter(std::string const &) const
void add(const fastjet::PseudoJet &iParticle, const double &iVal, const unsigned int iAlgo)
Definition: PuppiAlgo.cc:60
double fNeutralPtMin
Definition: PuppiAlgo.h:34
float fPtMin
Definition: PuppiAlgo.h:33
std::vector< bool > fAdjust
Definition: PuppiAlgo.h:40
PuppiAlgo(edm::ParameterSet &iConfig)
Definition: PuppiAlgo.cc:11
float fEtaMin
Definition: PuppiAlgo.h:32
std::vector< double > fConeSize
Definition: PuppiAlgo.h:42
void reset()
Definition: PuppiAlgo.cc:50
T sqrt(T t)
Definition: SSEVec.h:48
~PuppiAlgo()
Definition: PuppiAlgo.cc:46
std::vector< double > fRMS
Definition: PuppiAlgo.h:45
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< float > fPupsPV
Definition: PuppiAlgo.h:37
double compute(std::vector< double > const &iVals, double iChi2) const
Definition: PuppiAlgo.cc:124
std::vector< float > fPups
Definition: PuppiAlgo.h:36
std::vector< double > fMean
Definition: PuppiAlgo.h:47
std::vector< int > fAlgoId
Definition: PuppiAlgo.h:38
std::vector< double > fRMSPtMin
Definition: PuppiAlgo.h:43
std::vector< int > fCombId
Definition: PuppiAlgo.h:41
std::vector< double > fRMSScaleFactor
Definition: PuppiAlgo.h:44
void computeMedRMS(const unsigned int &iAlgo, const double &iPVFrac)
Definition: PuppiAlgo.cc:82
double fNeutralPtSlope
Definition: PuppiAlgo.h:35
std::vector< bool > fCharged
Definition: PuppiAlgo.h:39
unsigned int fNAlgos
Definition: PuppiAlgo.h:30
float fEtaMax
Definition: PuppiAlgo.h:31
std::vector< int > fNCount
Definition: PuppiAlgo.h:48
std::vector< double > fMedian
Definition: PuppiAlgo.h:46