ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
functions.cpp
Go to the documentation of this file.
1 #include "core/functions.hpp"
2 
3 #include "TVector2.h"
4 
5 #include "core/utilities.hpp"
6 
7 namespace Functions{
8  const NamedFunc n_isr_match("n_isr_match", NISRMatch);
9 
10  const NamedFunc njets_weights_ttisr("njets_weights_ttisr", [](const Baby &b){
11  return NJetsWeights_ttISR(b, false);
12  });
13 
14  const NamedFunc njets_weights_visr("njets_weights_visr", NJetsWeights_vISR);
15 
16  const NamedFunc min_dphi_lep_met("min_dphi_lep_met", [](const Baby &b) -> NamedFunc::ScalarType{
17  double phi1, eta1, phi2, eta2;
18  DileptonAngles(b, eta1, phi1, eta2, phi2);
19  double dphi1 = fabs(TVector2::Phi_mpi_pi(phi1-b.met_phi()));
20  double dphi2 = fabs(TVector2::Phi_mpi_pi(phi2-b.met_phi()));
21  if(phi1 != -999 && phi2 != -999){
22  return std::min(dphi1,dphi2);
23  }else if(phi1 != -999 && phi2 == -999){
24  return dphi1;
25  }else if(phi1 == -999 && phi2 != -999){
26  return dphi2;
27  }
28  return -1;
29  });
30 
31  const NamedFunc max_dphi_lep_met("max_dphi_lep_met", [](const Baby &b) -> NamedFunc::ScalarType{
32  double phi1, eta1, phi2, eta2;
33  DileptonAngles(b, eta1, phi1, eta2, phi2);
34  double dphi1 = fabs(TVector2::Phi_mpi_pi(phi1-b.met_phi()));
35  double dphi2 = fabs(TVector2::Phi_mpi_pi(phi2-b.met_phi()));
36  if(phi1 != -999 && phi2 != -999){
37  return std::max(dphi1,dphi2);
38  }else if(phi1 != -999 && phi2 == -999){
39  return dphi1;
40  }else if(phi1 == -999 && phi2 != -999){
41  return dphi2;
42  }
43  return -1;
44  });
45 
46  const NamedFunc min_dphi_lep_jet("min_dphi_lep_jet", [](const Baby &b) ->NamedFunc::ScalarType{
47  double phi1, eta1, phi2, eta2;
48  DileptonAngles(b, eta1, phi1, eta2, phi2);
49  double minphi = -1.;
50  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
51  if(!IsGoodJet(b,ijet)) continue;
52  double dphi1 = fabs(TVector2::Phi_mpi_pi(phi1-b.jets_phi()->at(ijet)));
53  double dphi2 = fabs(TVector2::Phi_mpi_pi(phi2-b.jets_phi()->at(ijet)));
54  double thisdphi = -1;
55  if(phi1 != -999 && phi2 != -999){
56  thisdphi = std::min(dphi1, dphi2);
57  }else if(phi1 != -999 && phi2 == -999){
58  thisdphi = dphi1;
59  }else if(phi1 == -999 && phi2 != -999){
60  thisdphi = dphi2;
61  }
62  if(minphi < 0. || thisdphi < minphi){
63  minphi = thisdphi;
64  }
65  }
66  return minphi;
67  });
68 
69  const NamedFunc max_dphi_lep_jet("max_dphi_lep_jet", [](const Baby &b) ->NamedFunc::ScalarType{
70  double phi1, eta1, phi2, eta2;
71  DileptonAngles(b, eta1, phi1, eta2, phi2);
72  double maxphi = -1.;
73  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
74  if(!IsGoodJet(b,ijet)) continue;
75  double dphi1 = fabs(TVector2::Phi_mpi_pi(phi1-b.jets_phi()->at(ijet)));
76  double dphi2 = fabs(TVector2::Phi_mpi_pi(phi2-b.jets_phi()->at(ijet)));
77  double thisdphi = -1;
78  if(phi1 != -999 && phi2 != -999){
79  thisdphi = std::max(dphi1, dphi2);
80  }else if(phi1 != -999 && phi2 == -999){
81  thisdphi = dphi1;
82  }else if(phi1 == -999 && phi2 != -999){
83  thisdphi = dphi2;
84  }
85  if(maxphi < 0. || thisdphi > maxphi){
86  maxphi = thisdphi;
87  }
88  }
89  return maxphi;
90  });
91 
92  const NamedFunc min_dphi_met_jet("min_dphi_met_jet", [](const Baby &b) ->NamedFunc::ScalarType{
93  double minphi = -1.;
94  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
95  if(!IsGoodJet(b,ijet)) continue;
96  double thisdphi = fabs(TVector2::Phi_mpi_pi(b.met_phi()-b.jets_phi()->at(ijet)));
97  if(minphi < 0. || thisdphi < minphi){
98  minphi = thisdphi;
99  }
100  }
101  return minphi;
102  });
103 
104  const NamedFunc max_dphi_met_jet("max_dphi_met_jet", [](const Baby &b) ->NamedFunc::ScalarType{
105  double maxphi = -1.;
106  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
107  if(!IsGoodJet(b,ijet)) continue;
108  double thisdphi = fabs(TVector2::Phi_mpi_pi(b.met_phi()-b.jets_phi()->at(ijet)));
109  if(maxphi < 0. || thisdphi > maxphi){
110  maxphi = thisdphi;
111  }
112  }
113  return maxphi;
114  });
115 
116  const NamedFunc min_dr_lep_jet("min_dr_lep_jet", [](const Baby &b) ->NamedFunc::ScalarType{
117  double phi1, eta1, phi2, eta2;
118  DileptonAngles(b, eta1, phi1, eta2, phi2);
119  double minr = -1.;
120  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
121  if(!IsGoodJet(b,ijet)) continue;
122  double dr1 = hypot(TVector2::Phi_mpi_pi(phi1-b.jets_phi()->at(ijet)), eta2-eta1);
123  double dr2 = hypot(TVector2::Phi_mpi_pi(phi2-b.jets_phi()->at(ijet)), eta2-eta1);
124  double thisdr = -1;
125  if(phi1 != -999 && phi2 != -999){
126  thisdr = std::min(dr1, dr2);
127  }else if(phi1 != -999 && phi2 == -999){
128  thisdr = dr1;
129  }else if(phi1 == -999 && phi2 != -999){
130  thisdr = dr2;
131  }
132  if(minr < 0. || thisdr < minr){
133  minr = thisdr;
134  }
135  }
136  return minr;
137  });
138 
139  const NamedFunc max_dr_lep_jet("max_dr_lep_jet", [](const Baby &b) ->NamedFunc::ScalarType{
140  double phi1, eta1, phi2, eta2;
141  DileptonAngles(b, eta1, phi1, eta2, phi2);
142  double maxr = -1.;
143  for(size_t ijet = 0; ijet < b.jets_pt()->size(); ++ijet){
144  if(!IsGoodJet(b,ijet)) continue;
145  double dr1 = hypot(TVector2::Phi_mpi_pi(phi1-b.jets_phi()->at(ijet)), eta2-eta1);
146  double dr2 = hypot(TVector2::Phi_mpi_pi(phi2-b.jets_phi()->at(ijet)), eta2-eta1);
147  double thisdr = -1;
148  if(phi1 != -999 && phi2 != -999){
149  thisdr = std::max(dr1, dr2);
150  }else if(phi1 != -999 && phi2 == -999){
151  thisdr = dr1;
152  }else if(phi1 == -999 && phi2 != -999){
153  thisdr = dr2;
154  }
155  if(maxr < 0. || thisdr > maxr){
156  maxr = thisdr;
157  }
158  }
159  return maxr;
160  });
161 
162  bool IsGoodJet(const Baby &b, size_t ijet){
163  return ijet<b.jets_pt()->size()
164  && b.jets_pt()->at(ijet) > 30.
165  && fabs(b.jets_eta()->at(ijet))<2.4
166  && !b.jets_islep()->at(ijet);
167  }
168 
169  bool IsGoodElectron(const Baby &b, size_t iel){
170  return iel<b.els_pt()->size()
171  && b.els_pt()->at(iel)>20.
172  && fabs(b.els_sceta()->at(iel))<2.5
173  && b.els_sigid()->at(iel)
174  && b.els_miniso()->at(iel) >= 0.
175  && b.els_miniso()->at(iel) < 0.1;
176  }
177 
178  bool IsGoodMuon(const Baby &b, size_t imu){
179  return imu<b.mus_pt()->size()
180  && b.mus_pt()->at(imu)>20.
181  && fabs(b.mus_eta()->at(imu))<2.4
182  && b.mus_sigid()->at(imu)
183  && b.mus_miniso()->at(imu) >= 0.
184  && b.mus_miniso()->at(imu) < 0.2;
185  }
186 
187  bool IsGoodTrack(const Baby &b, size_t itk){
188  if(itk >= b.tks_pt()->size()) return false;
189 
190  if(abs(b.tks_pdg()->at(itk))==211 && b.tks_pt()->at(itk)>15. && b.tks_miniso()->at(itk)<0.1 && b.tks_mt2()->at(itk)<60 && b.tks_dz()->at(itk)<0.07 && b.tks_d0()->at(itk)<0.05 ){
191  return true;
192  }else if(abs(b.tks_pdg()->at(itk))==13 && b.tks_pt()->at(itk)>10. && b.tks_miniso()->at(itk)<0.2 && b.tks_mt2()->at(itk)<80 && b.tks_dz()->at(itk)<0.07 && b.tks_d0()->at(itk)<0.05){
193  return true;
194  }else if(abs(b.tks_pdg()->at(itk))==11 && b.tks_pt()->at(itk)>10. && b.tks_miniso()->at(itk)<0.2 && b.tks_mt2()->at(itk)<80 && b.tks_dz()->at(itk)<0.07 && b.tks_d0()->at(itk)<0.05){
195  return true;
196  }else{
197  return false;
198  }
199  }
200 
201  NamedFunc::ScalarType NJetsWeights_ttISR(const Baby &b, bool use_baby_nisr){
202  if (b.ntrupv()<0) return 1.; // Do not reweight Data
203 
204  float wgt = b.weight()/b.eff_trig()/b.w_toppt();
205 
206  int nisrjets = use_baby_nisr ? b.nisr() : NISRMatch(b);
207 
208  // weights derived in TTJets and applied using the nisr calculation algorithm
209  if (nisrjets==0) return 1.099*wgt; // +- 0.012
210  else if (nisrjets==1) return 0.969*wgt; // +- 0.014
211  else if (nisrjets==2) return 0.870*wgt; // +- 0.020
212  else if (nisrjets==3) return 0.772*wgt; // +- 0.031
213  else if (nisrjets==4) return 0.712*wgt; // +- 0.051
214  else if (nisrjets==5) return 0.661*wgt; // +- 0.088
215  else if (nisrjets>=6) return 0.566*wgt; // +- 0.133
216  else return wgt;
217  }
218 
220  if (b.ntrupv()<0) return 1.; // Do not reweight Data
221 
222  float wgt = b.weight()/b.eff_trig()/b.w_toppt();
223  if(b.SampleType()<30 && b.SampleType()>=60) return wgt;
224 
225  int nisrjets(b.njets());
226  // weights derived in DY+jets
227  if (nisrjets==0) return 0.981*wgt; // +- 0.001
228  else if (nisrjets==1) return 1.071*wgt; // +- 0.001
229  else if (nisrjets==2) return 1.169*wgt; // +- 0.003
230  else if (nisrjets==3) return 1.157*wgt; // +- 0.007
231  else if (nisrjets==4) return 1.014*wgt; // +- 0.013
232  else if (nisrjets==5) return 0.920*wgt; // +- 0.025
233  else if (nisrjets==6) return 0.867*wgt; // +- 0.048
234  else if (nisrjets>=7) return 0.935*wgt; // +- 0.088
235  else return wgt;
236  }
237 
238  int NISRMatch(const Baby &b){
239  int Nisr=0;
240  for (size_t ijet(0); ijet<b.jets_pt()->size(); ++ijet){
241  if(!IsGoodJet(b, ijet)) continue;
242  bool matched=false;
243  for (size_t imc(0); imc<b.mc_pt()->size(); ++imc){
244  if(b.mc_status()->at(imc)!=23 || abs(b.mc_id()->at(imc))>5) continue;
245  if(!(abs(b.mc_mom()->at(imc))==6 || abs(b.mc_mom()->at(imc))==23 ||
246  abs(b.mc_mom()->at(imc))==24 || abs(b.mc_mom()->at(imc))==15)) continue; // In our ntuples where all taus come from W
247  float dR = deltaR(b.jets_eta()->at(ijet), b.jets_phi()->at(ijet), b.mc_eta()->at(imc), b.mc_phi()->at(imc));
248  if(dR<0.4){
249  matched = true;
250  break;
251  }
252  } // Loop over MC particles
253  if(!matched) ++Nisr;
254  } // Loop over jets
255 
256  return Nisr;
257  }
258 
259  void DileptonAngles(const Baby &b,
262  phi1 = -999.; eta1 = -999.;
263  phi2 = -999.; eta2 = -999.;
264  bool h1=false, h2=false;
265  if(b.nels()==2 && b.nmus()==0){
266  for(size_t iel = 0; iel < b.els_pt()->size() && !(h1&&h2); ++iel){
267  if(IsGoodElectron(b, iel)){
268  if(!h1){
269  phi1 = b.els_phi()->at(iel);
270  eta1 = b.els_sceta()->at(iel);
271  h1 = true;
272  }else if(!h2){
273  phi2 = b.els_phi()->at(iel);
274  eta2 = b.els_sceta()->at(iel);
275  h2 = true;
276  }
277  }
278  }
279  }else if(b.nels()==1 && b.nmus()==1){
280  for(size_t iel = 0; iel < b.els_pt()->size() && !h1; ++iel){
281  if(IsGoodElectron(b, iel)){
282  if(!h1){
283  phi1 = b.els_phi()->at(iel);
284  eta1 = b.els_sceta()->at(iel);
285  h1 = true;
286  }else if(!h2){
287  phi2 = b.els_phi()->at(iel);
288  eta2 = b.els_sceta()->at(iel);
289  h2 = true;
290  }
291  }
292  }
293  for(size_t imu = 0; imu < b.mus_pt()->size() && h1 && !h2; ++imu){
294  if(IsGoodMuon(b, imu)){
295  if(!h1){
296  phi1 = b.mus_phi()->at(imu);
297  eta1 = b.mus_eta()->at(imu);
298  h1 = true;
299  }else if(!h2){
300  phi2 = b.mus_phi()->at(imu);
301  eta2 = b.mus_eta()->at(imu);
302  h2 = true;
303  }
304  }
305  }
306  }else if(b.nels()==0 && b.nmus()==2){
307  for(size_t imu = 0; imu < b.mus_pt()->size() && !(h1&&h2); ++imu){
308  if(IsGoodMuon(b, imu)){
309  if(!h1){
310  phi1 = b.mus_phi()->at(imu);
311  eta1 = b.mus_eta()->at(imu);
312  h1 = true;
313  }else if(!h2){
314  phi2 = b.mus_phi()->at(imu);
315  eta2 = b.mus_eta()->at(imu);
316  h2 = true;
317  }
318  }
319  }
320  }else if(b.nels()==1 && b.nmus()==0 && b.nveto()==1){
321  for(size_t iel = 0; iel < b.els_pt()->size() && !h1; ++iel){
322  if(IsGoodElectron(b, iel)){
323  if(!h1){
324  phi1 = b.els_phi()->at(iel);
325  eta1 = b.els_sceta()->at(iel);
326  h1 = true;
327  }else if(!h2){
328  phi2 = b.els_phi()->at(iel);
329  eta2 = b.els_sceta()->at(iel);
330  h2 = true;
331  }
332  }
333  }
334  for(size_t itk = 0; itk < b.tks_pt()->size() && h1 && !h2; ++itk){
335  if(IsGoodTrack(b, itk)){
336  if(!h1){
337  phi1 = b.tks_phi()->at(itk);
338  eta1 = b.tks_eta()->at(itk);
339  h1 = true;
340  }else if(!h2){
341  phi2 = b.tks_phi()->at(itk);
342  eta2 = b.tks_eta()->at(itk);
343  h2 = true;
344  }
345  }
346  }
347  }else if(b.nels()==0 && b.nmus()==1 && b.nveto()==1){
348  for(size_t imu = 0; imu < b.mus_pt()->size() && !h1; ++imu){
349  if(IsGoodMuon(b, imu)){
350  if(!h1){
351  phi1 = b.mus_phi()->at(imu);
352  eta1 = b.mus_eta()->at(imu);
353  h1 = true;
354  }else if(!h2){
355  phi2 = b.mus_phi()->at(imu);
356  eta2 = b.mus_eta()->at(imu);
357  h2 = true;
358  }
359  }
360  }
361  for(size_t itk = 0; itk < b.tks_pt()->size() && h1 && !h2; ++itk){
362  if(IsGoodTrack(b, itk)){
363  if(!h1){
364  phi1 = b.tks_phi()->at(itk);
365  eta1 = b.tks_eta()->at(itk);
366  h1 = true;
367  }else if(!h2){
368  phi2 = b.tks_phi()->at(itk);
369  eta2 = b.tks_eta()->at(itk);
370  h2 = true;
371  }
372  }
373  }
374  }
375  }
376 }
std::vector< int > *const & mc_status() const
Get mc_status for current event and cache it.
Definition: baby.cpp:4625
std::vector< float > *const & mus_eta() const
Get mus_eta for current event and cache it.
Definition: baby.cpp:5525
const NamedFunc min_dphi_lep_met
const NamedFunc njets_weights_ttisr
const NamedFunc min_dr_lep_jet
float const & eff_trig() const
Get eff_trig for current event and cache it.
Definition: baby.cpp:2981
int const & njets() const
Get njets for current event and cache it.
Definition: baby.cpp:5981
std::vector< float > *const & els_pt() const
Get els_pt for current event and cache it.
Definition: baby.cpp:3461
int NISRMatch(const Baby &b)
Definition: functions.cpp:238
std::vector< float > *const & mus_phi() const
Get mus_phi for current event and cache it.
Definition: baby.cpp:5621
std::vector< float > *const & jets_phi() const
Get jets_phi for current event and cache it.
Definition: baby.cpp:4277
const NamedFunc max_dphi_lep_jet
Abstract base class for access to ntuple variables.
Definition: baby.hpp:16
std::vector< float > *const & tks_mt2() const
Get tks_mt2 for current event and cache it.
Definition: baby.cpp:7253
const NamedFunc njets_weights_visr
std::vector< float > *const & mus_pt() const
Get mus_pt for current event and cache it.
Definition: baby.cpp:5633
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
float const & met_phi() const
Get met_phi for current event and cache it.
Definition: baby.cpp:4733
std::vector< float > *const & tks_eta() const
Get tks_eta for current event and cache it.
Definition: baby.cpp:7217
float const & w_toppt() const
Get w_toppt for current event and cache it.
Definition: baby.cpp:7505
std::vector< float > *const & els_miniso() const
Get els_miniso for current event and cache it.
Definition: baby.cpp:3437
bool IsGoodMuon(const Baby &b, std::size_t imu)
double ScalarType
Definition: named_func.hpp:15
int const & nmus() const
Get nmus for current event and cache it.
Definition: baby.cpp:6077
std::vector< float > *const & tks_d0() const
Get tks_d0 for current event and cache it.
Definition: baby.cpp:7193
const NamedFunc max_dr_lep_jet
NamedFunc::ScalarType NJetsWeights_ttISR(const Baby &b, bool use_baby_nisr)
Definition: functions.cpp:201
std::vector< int > *const & mc_mom() const
Get mc_mom for current event and cache it.
Definition: baby.cpp:4577
std::vector< float > *const & mc_pt() const
Get mc_pt for current event and cache it.
Definition: baby.cpp:4613
std::vector< float > *const & els_phi() const
Get els_phi for current event and cache it.
Definition: baby.cpp:3449
std::vector< float > *const & jets_eta() const
Get jets_eta for current event and cache it.
Definition: baby.cpp:4121
std::vector< bool > *const & mus_sigid() const
Get mus_sigid for current event and cache it.
Definition: baby.cpp:5681
std::vector< bool > *const & els_sigid() const
Get els_sigid for current event and cache it.
Definition: baby.cpp:3509
const NamedFunc min_dphi_lep_jet
float deltaR(float eta1, float phi1, float eta2, float phi2)
Definition: utilities.cpp:420
const NamedFunc n_isr_match
std::vector< float > *const & tks_miniso() const
Get tks_miniso for current event and cache it.
Definition: baby.cpp:7229
std::vector< float > *const & tks_pt() const
Get tks_pt for current event and cache it.
Definition: baby.cpp:7289
const NamedFunc min_dphi_met_jet
float const & weight() const
Get weight for current event and cache it.
Definition: baby.cpp:7517
std::vector< float > *const & els_sceta() const
Get els_sceta for current event and cache it.
Definition: baby.cpp:3485
std::vector< float > *const & tks_phi() const
Get tks_phi for current event and cache it.
Definition: baby.cpp:7277
std::vector< int > *const & mc_id() const
Get mc_id for current event and cache it.
Definition: baby.cpp:4553
float const & nisr() const
Get nisr for current event and cache it.
Definition: baby.cpp:5957
int const & nels() const
Get nels for current event and cache it.
Definition: baby.cpp:5897
int const & nveto() const
Get nveto for current event and cache it.
Definition: baby.cpp:6257
std::vector< int > *const & tks_pdg() const
Get tks_pdg for current event and cache it.
Definition: baby.cpp:7265
const NamedFunc max_dphi_met_jet
std::vector< float > *const & jets_pt() const
Get jets_pt for current event and cache it.
Definition: baby.cpp:4289
bool IsGoodJet(const Baby &b, std::size_t ijet)
bool IsGoodElectron(const Baby &b, std::size_t iel)
std::vector< float > *const & mus_miniso() const
Get mus_miniso for current event and cache it.
Definition: baby.cpp:5585
std::vector< float > *const & tks_dz() const
Get tks_dz for current event and cache it.
Definition: baby.cpp:7205
int const & ntrupv() const
Get ntrupv for current event and cache it.
Definition: baby.cpp:6197
bool IsGoodTrack(const Baby &b, std::size_t itk)
std::vector< float > *const & mc_eta() const
Get mc_eta for current event and cache it.
Definition: baby.cpp:4541
void DileptonAngles(const Baby &b, NamedFunc::ScalarType &eta1, NamedFunc::ScalarType &phi1, NamedFunc::ScalarType &eta2, NamedFunc::ScalarType &phi2)
Definition: functions.cpp:259
const NamedFunc max_dphi_lep_met
std::vector< bool > *const & jets_islep() const
Get jets_islep for current event and cache it.
Definition: baby.cpp:4253
int SampleType() const
Definition: baby.cpp:1681
NamedFunc::ScalarType NJetsWeights_vISR(const Baby &b)
Definition: functions.cpp:219
std::vector< float > *const & mc_phi() const
Get mc_phi for current event and cache it.
Definition: baby.cpp:4601