CMS 3D CMS Logo

PuppiAlgo.cc
Go to the documentation of this file.
3 #include "Math/QuantFuncMathCore.h"
4 #include "Math/SpecFuncMathCore.h"
5 #include "Math/ProbFunc.h"
6 #include "TMath.h"
7 
9  fEtaMin = iConfig.getParameter<std::vector<double>>("etaMin");
10  fEtaMax = iConfig.getParameter<std::vector<double>>("etaMax");
11  fPtMin = iConfig.getParameter<std::vector<double>>("ptMin");
12  fNeutralPtMin = iConfig.getParameter<std::vector<double>>("MinNeutralPt"); // Weighted Neutral Pt Cut
13  fNeutralPtSlope = iConfig.getParameter<std::vector<double>>("MinNeutralPtSlope"); // Slope vs #pv
14  fRMSEtaSF = iConfig.getParameter<std::vector<double>>("RMSEtaSF");
15  fMedEtaSF = iConfig.getParameter<std::vector<double>>("MedEtaSF");
16  fEtaMaxExtrap = iConfig.getParameter<double>("EtaMaxExtrap");
17 
18  std::vector<edm::ParameterSet> lAlgos = iConfig.getParameter<std::vector<edm::ParameterSet>>("puppiAlgos");
19  fNAlgos = lAlgos.size();
20  //Uber Configurable Puppi
21  std::vector<double> tmprms;
22  std::vector<double> tmpmed;
23 
24  for (unsigned int i0 = 0; i0 < lAlgos.size(); i0++) {
25  int pAlgoId = lAlgos[i0].getParameter<int>("algoId");
26  bool pCharged = lAlgos[i0].getParameter<bool>("useCharged");
27  bool pWeight0 = lAlgos[i0].getParameter<bool>("applyLowPUCorr");
28  int pComb = lAlgos[i0].getParameter<int>("combOpt"); // 0=> add in chi2/1=>Multiply p-values
29  double pConeSize = lAlgos[i0].getParameter<double>("cone"); // Min Pt when computing pt and rms
30  double pRMSPtMin = lAlgos[i0].getParameter<double>("rmsPtMin"); // Min Pt when computing pt and rms
31  double pRMSSF = lAlgos[i0].getParameter<double>("rmsScaleFactor"); // Additional Tuning parameter for Jokers
32  fAlgoId.push_back(pAlgoId);
33  fCharged.push_back(pCharged);
34  fAdjust.push_back(pWeight0);
35  fCombId.push_back(pComb);
36  fConeSize.push_back(pConeSize);
37  fRMSPtMin.push_back(pRMSPtMin);
38  fRMSScaleFactor.push_back(pRMSSF);
39  double pRMS = 0;
40  double pMed = 0;
41  double pMean = 0;
42  int pNCount = 0;
43  fRMS.push_back(pRMS);
44  fMedian.push_back(pMed);
45  fMean.push_back(pMean);
46  fNCount.push_back(pNCount);
47 
48  tmprms.clear();
49  tmpmed.clear();
50  for (unsigned int j0 = 0; j0 < fEtaMin.size(); j0++) {
51  tmprms.push_back(pRMS);
52  tmpmed.push_back(pMed);
53  }
54  fRMS_perEta.push_back(tmprms);
55  fMedian_perEta.push_back(tmpmed);
56  }
57 
58  cur_PtMin = -99.;
59  cur_NeutralPtMin = -99.;
60  cur_NeutralPtSlope = -99.;
61  cur_RMS = -99.;
62  cur_Med = -99.;
63 }
65  fPups.clear();
66  fPupsPV.clear();
67 }
69  fPups.clear();
70  fPupsPV.clear();
71  for (unsigned int i0 = 0; i0 < fNAlgos; i0++) {
72  fMedian[i0] = 0;
73  fRMS[i0] = 0;
74  fMean[i0] = 0;
75  fNCount[i0] = 0;
76  }
77 }
78 
79 void PuppiAlgo::fixAlgoEtaBin(int i_eta) {
80  cur_PtMin = fPtMin[i_eta];
83  cur_RMS = fRMS_perEta[0][i_eta]; // 0 is number of algos within this eta bin
84  cur_Med = fMedian_perEta[0][i_eta]; // 0 is number of algos within this eta bin
85 }
86 
87 void PuppiAlgo::add(const PuppiCandidate &iParticle, const double &iVal, const unsigned int iAlgo) {
88  if (iParticle.pt() < fRMSPtMin[iAlgo])
89  return;
90  // Change from SRR : Previously used fastjet::PseudoJet::user_index to decide the particle type.
91  // In CMSSW we use the user_index to specify the index in the input collection, so I invented
92  // a new mechanism using the fastjet UserInfo functionality. Of course, it's still just an integer
93  // but that interface could be changed (or augmented) if desired / needed.
94  int puppi_id = iParticle.puppi_register();
95  if (puppi_id == std::numeric_limits<int>::lowest()) {
96  throw cms::Exception("PuppiRegisterNotSet") << "The puppi register is not set. This must be set before use.\n";
97  }
98 
99  // added by Nhan -- for all eta regions, compute mean/RMS from the central charged PU
100  if ((std::abs(iParticle.eta()) < fEtaMaxExtrap) && (puppi_id == 2)) {
101  fPups.push_back(iVal);
102  fNCount[iAlgo]++;
103  }
104  // for the low PU case, correction. for checking that the PU-only median will be below the PV particles
105  if (std::abs(iParticle.eta()) < fEtaMaxExtrap && (puppi_id == 1))
106  fPupsPV.push_back(iVal);
107 }
108 
111 //NHAN'S VERSION
112 void PuppiAlgo::computeMedRMS(const unsigned int &iAlgo) {
113  //std::cout << "fNCount[iAlgo] = " << fNCount[iAlgo] << std::endl;
114  if (iAlgo >= fNAlgos)
115  return;
116  if (fNCount[iAlgo] == 0)
117  return;
118 
119  // sort alphas
120  int lNBefore = 0;
121  for (unsigned int i0 = 0; i0 < iAlgo; i0++)
122  lNBefore += fNCount[i0];
123  std::sort(fPups.begin() + lNBefore, fPups.begin() + lNBefore + fNCount[iAlgo]);
124 
125  // in case you have alphas == 0
126  int lNum0 = 0;
127  for (int i0 = lNBefore; i0 < lNBefore + fNCount[iAlgo]; i0++) {
128  if (fPups[i0] == 0)
129  lNum0 = i0 - lNBefore;
130  }
131 
132  // comput median, removed lCorr for now
133  int lNHalfway = lNBefore + lNum0 + int(double(fNCount[iAlgo] - lNum0) * 0.50);
134  fMedian[iAlgo] = fPups[lNHalfway];
135  double lMed = fMedian[iAlgo]; //Just to make the readability easier
136 
137  int lNRMS = 0;
138  for (int i0 = lNBefore; i0 < lNBefore + fNCount[iAlgo]; i0++) {
139  fMean[iAlgo] += fPups[i0];
140  if (fPups[i0] == 0)
141  continue;
142  // if(!fCharged[iAlgo] && fAdjust[iAlgo] && fPups[i0] > lMed) continue;
143  if (fAdjust[iAlgo] && fPups[i0] > lMed)
144  continue;
145  lNRMS++;
146  fRMS[iAlgo] += (fPups[i0] - lMed) * (fPups[i0] - lMed);
147  }
148  fMean[iAlgo] /= fNCount[iAlgo];
149  if (lNRMS > 0)
150  fRMS[iAlgo] /= lNRMS;
151  if (fRMS[iAlgo] == 0)
152  fRMS[iAlgo] = 1e-5;
153  // here is the raw RMS
154  fRMS[iAlgo] = sqrt(fRMS[iAlgo]);
155 
156  // some ways to do corrections to fRMS and fMedian
157  fRMS[iAlgo] *= fRMSScaleFactor[iAlgo];
158 
159  if (fAdjust[iAlgo]) {
160  //Adjust the p-value to correspond to the median
161  std::sort(fPupsPV.begin(), fPupsPV.end());
162  int lNPV = 0;
163  for (unsigned int i0 = 0; i0 < fPupsPV.size(); i0++)
164  if (fPupsPV[i0] <= lMed)
165  lNPV++;
166  double lAdjust = double(lNPV) / double(lNPV + 0.5 * fNCount[iAlgo]);
167  if (lAdjust > 0) {
168  fMedian[iAlgo] -= sqrt(ROOT::Math::chisquared_quantile(lAdjust, 1.) * fRMS[iAlgo]);
169  fRMS[iAlgo] -= sqrt(ROOT::Math::chisquared_quantile(lAdjust, 1.) * fRMS[iAlgo]);
170  }
171  }
172 
173  // fRMS_perEta[iAlgo] *= cur_RMSEtaSF;
174  // fMedian_perEta[iAlgo] *= cur_MedEtaSF;
175 
176  for (unsigned int j0 = 0; j0 < fEtaMin.size(); j0++) {
177  fRMS_perEta[iAlgo][j0] = fRMS[iAlgo] * fRMSEtaSF[j0];
178  fMedian_perEta[iAlgo][j0] = fMedian[iAlgo] * fMedEtaSF[j0];
179  }
180 }
182 
183 //This code is probably a bit confusing
184 double PuppiAlgo::compute(std::vector<double> const &iVals, double iChi2) const {
185  if (fAlgoId[0] == -1)
186  return 1;
187  double lVal = 0.;
188  double lPVal = 1.;
189  int lNDOF = 0;
190  for (unsigned int i0 = 0; i0 < fNAlgos; i0++) {
191  if (fNCount[i0] == 0)
192  return 1.; //in the NoPU case return 1.
193  if (fCombId[i0] == 1 && i0 > 0) { //Compute the previous p-value so that p-values can be multiplieed
194  double pPVal = ROOT::Math::chisquared_cdf(lVal, lNDOF);
195  lPVal *= pPVal;
196  lNDOF = 0;
197  lVal = 0;
198  }
199  double pVal = iVals[i0];
200  //Special Check for any algo with log(0)
201  if (fAlgoId[i0] == 0 && iVals[i0] == 0)
202  pVal = cur_Med;
203  if (fAlgoId[i0] == 3 && iVals[i0] == 0)
204  pVal = cur_Med;
205  if (fAlgoId[i0] == 5 && iVals[i0] == 0)
206  pVal = cur_Med;
207  lVal += (pVal - cur_Med) * (fabs(pVal - cur_Med)) / cur_RMS / cur_RMS;
208  lNDOF++;
209  if (i0 == 0 && iChi2 != 0)
210  lNDOF++; //Add external Chi2 to first element
211  if (i0 == 0 && iChi2 != 0)
212  lVal += iChi2; //Add external Chi2 to first element
213  }
214  //Top it off with the last calc
215  lPVal *= ROOT::Math::chisquared_cdf(lVal, lNDOF);
216  return lPVal;
217 }
218 // ------------------------------------------------------------------------------------------
220  edm::ParameterSetDescription puppialgos;
221  puppialgos.add<int>("algoId", 5);
222  puppialgos.add<bool>("useCharged", false);
223  puppialgos.add<bool>("applyLowPUCorr", false);
224  puppialgos.add<int>("combOpt", 5);
225  puppialgos.add<double>("cone", .4);
226  puppialgos.add<double>("rmsPtMin", .1);
227  puppialgos.add<double>("rmsScaleFactor", 1.0);
228  std::vector<edm::ParameterSet> VPSetPuppiAlgos;
229  edm::ParameterSet puppiset;
230  puppiset.addParameter<int>("algoId", 5);
231  puppiset.addParameter<bool>("useCharged", false);
232  puppiset.addParameter<bool>("applyLowPUCorr", false);
233  puppiset.addParameter<int>("combOpt", 5);
234  puppiset.addParameter<double>("cone", .4);
235  puppiset.addParameter<double>("rmsPtMin", .1);
236  puppiset.addParameter<double>("rmsScaleFactor", 1.0);
237  VPSetPuppiAlgos.push_back(puppiset);
238 
240  algos.addVPSet("puppiAlgos", puppialgos, VPSetPuppiAlgos);
241  std::vector<edm::ParameterSet> VPSetAlgos;
242  edm::ParameterSet algosset;
243  algos.add<std::vector<double>>("etaMin", {0.});
244  algos.add<std::vector<double>>("etaMax", {2.5});
245  algos.add<std::vector<double>>("ptMin", {0.});
246  algos.add<std::vector<double>>("MinNeutralPt", {0.2});
247  algos.add<std::vector<double>>("MinNeutralPtSlope", {0.015});
248  algos.add<std::vector<double>>("RMSEtaSF", {1.0});
249  algos.add<std::vector<double>>("MedEtaSF", {1.0});
250  algos.add<double>("EtaMaxExtrap", 2.0);
251  algosset.addParameter<std::vector<double>>("etaMin", {0.});
252  algosset.addParameter<std::vector<double>>("etaMax", {2.5});
253  algosset.addParameter<std::vector<double>>("ptMin", {0.});
254  algosset.addParameter<std::vector<double>>("MinNeutralPt", {0.2});
255  algosset.addParameter<std::vector<double>>("MinNeutralPtSlope", {0.015});
256  algosset.addParameter<std::vector<double>>("RMSEtaSF", {1.0});
257  algosset.addParameter<std::vector<double>>("MedEtaSF", {1.0});
258  algosset.addParameter<double>("EtaMaxExtrap", 2.0);
259  algosset.addParameter<std::vector<edm::ParameterSet>>("puppiAlgos", VPSetPuppiAlgos);
260  VPSetAlgos.push_back(algosset);
261  desc.addVPSet("algos", algos, VPSetAlgos);
262 }
void add(const PuppiCandidate &iParticle, const double &iVal, const unsigned int iAlgo)
Definition: PuppiAlgo.cc:87
T getParameter(std::string const &) const
void computeMedRMS(const unsigned int &iAlgo)
Definition: PuppiAlgo.cc:112
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
double cur_PtMin
Definition: PuppiAlgo.h:50
std::vector< double > fEtaMin
Definition: PuppiAlgo.h:41
static void fillDescriptionsPuppiAlgo(edm::ParameterSetDescription &desc)
Definition: PuppiAlgo.cc:219
double eta() const
std::vector< bool > fAdjust
Definition: PuppiAlgo.h:65
PuppiAlgo(edm::ParameterSet &iConfig)
Definition: PuppiAlgo.cc:8
double cur_NeutralPtSlope
Definition: PuppiAlgo.h:52
std::vector< double > fConeSize
Definition: PuppiAlgo.h:67
void reset()
Definition: PuppiAlgo.cc:68
double cur_Med
Definition: PuppiAlgo.h:54
std::vector< std::vector< double > > fMedian_perEta
Definition: PuppiAlgo.h:59
T sqrt(T t)
Definition: SSEVec.h:19
~PuppiAlgo()
Definition: PuppiAlgo.cc:64
std::vector< double > fRMS
Definition: PuppiAlgo.h:56
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:124
std::vector< double > fNeutralPtMin
Definition: PuppiAlgo.h:43
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int puppi_register() const
std::vector< float > fPupsPV
Definition: PuppiAlgo.h:62
ParameterDescriptionBase * add(U const &iLabel, T const &value)
double cur_NeutralPtMin
Definition: PuppiAlgo.h:51
double compute(std::vector< double > const &iVals, double iChi2) const
Definition: PuppiAlgo.cc:184
std::vector< float > fPups
Definition: PuppiAlgo.h:61
std::vector< double > fMean
Definition: PuppiAlgo.h:70
std::vector< int > fAlgoId
Definition: PuppiAlgo.h:63
std::vector< double > fRMSPtMin
Definition: PuppiAlgo.h:68
std::vector< int > fCombId
Definition: PuppiAlgo.h:66
std::vector< double > fRMSScaleFactor
Definition: PuppiAlgo.h:69
std::vector< double > fPtMin
Definition: PuppiAlgo.h:42
std::vector< double > fRMSEtaSF
Definition: PuppiAlgo.h:46
std::vector< double > fNeutralPtSlope
Definition: PuppiAlgo.h:44
std::vector< double > fMedEtaSF
Definition: PuppiAlgo.h:47
std::vector< double > fEtaMax
Definition: PuppiAlgo.h:40
double fEtaMaxExtrap
Definition: PuppiAlgo.h:48
double cur_RMS
Definition: PuppiAlgo.h:53
void fixAlgoEtaBin(int i_eta)
Definition: PuppiAlgo.cc:79
std::vector< bool > fCharged
Definition: PuppiAlgo.h:64
std::vector< std::vector< double > > fRMS_perEta
Definition: PuppiAlgo.h:58
unsigned int fNAlgos
Definition: PuppiAlgo.h:39
std::vector< int > fNCount
Definition: PuppiAlgo.h:71
std::vector< double > fMedian
Definition: PuppiAlgo.h:57