babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
utilities.cc
Go to the documentation of this file.
1 // Common utilities
2 
3 #include <cmath>
4 #include <iostream>
7 #include "TMath.h"
8 
9 using namespace std;
10 
11 namespace utilities{
12  bool contains(const string &s, const string &pat){
13  return s.find(pat) != string::npos;
14  }
15 
16  float sumMass(const LVector &a, const LVector &b){
17  return sqrt(a.mass2()+b.mass2()+2.*(a.E()*b.E()-a.Px()*b.Px()-a.Py()*b.Py()-a.Pz()*b.Pz()));
18  }
19 
20  float sumPt(const LVector &a, const LVector &b){
21  return sqrt(pow((a.Px()+b.Px()),2) +pow((a.Py()+b.Py()),2));
22  }
23 
24  float dPhi(float phi1, float phi2){
25  float delphi = TMath::Abs(TMath::Abs(TMath::Abs(phi1 - phi2) - TMath::Pi())-TMath::Pi());
26  return delphi;
27  }
28 
29  float dR(float phi1, float phi2, float eta1, float eta2){
30  float dr = sqrt(pow(eta1-eta2,2)+pow(dPhi(phi1,phi2),2));
31  return dr;
32 
33  }
34 
35 
36  bool greaterPt(const reco::Candidate *a, const reco::Candidate *b) {
37  return a->pt() > b->pt();
38  }
39 
40  bool greaterM(const fastjet::PseudoJet &a, const fastjet::PseudoJet &b){
41  return a.m() > b.m();
42  }
43 
44  float getMT(float pt1, float phi1, float pt2, float phi2){
45  //Faster calculation of mT in massless
46  return sqrt(2.*pt1*pt2*(1.-cos(phi2-phi1)));
47  }
48 
49  float getMT(float m1, float pt1, float phi1,
50  float m2, float pt2, float phi2){
51  return sqrt(m1*m1+m2*m2+2.*(sqrt((m1*m1+pt1*pt1)*(m2*m2+pt2*pt2))-pt1*pt2*cos(phi1-phi2)));
52  }
53 
54  float getMT2(float pt1, float phi1, float pt2, float phi2, float met, float met_phi){
55  return getMT2(0., pt1, phi1, 0., pt2, phi2, met, met_phi);
56  }
57 
58  float getMT2(float m1, float pt1, float phi1,
59  float m2, float pt2, float phi2,
60  float met, float met_phi){
62  double mVisA = m1; // mass of visible object on side A. Must be >=0.
63  double pxA = pt1*cos(phi1); // x momentum of visible object on side A.
64  double pyA = pt1*sin(phi1); // y momentum of visible object on side A.
65 
66  double mVisB = m2; // mass of visible object on side B. Must be >=0.
67  double pxB = pt2*cos(phi2); // x momentum of visible object on side B.
68  double pyB = pt2*sin(phi2); // y momentum of visible object on side B.
69 
70  double pxMiss = met*cos(met_phi); // x component of missing transverse momentum.
71  double pyMiss = met*sin(met_phi); // y component of missing transverse momentum.
72 
73  double chiA = 0; // hypothesised mass of invisible on side A. Must be >=0.
74  double chiB = 0; // hypothesised mass of invisible on side B. Must be >=0.
75  double desiredPrecisionOnMt2 = 0; // Must be >=0. If 0 alg aims for machine precision. if >0, MT2 computed to supplied absolute precision.
76 
78  mVisA, pxA, pyA,
79  mVisB, pxB, pyB,
80  pxMiss, pyMiss,
81  chiA, chiB,
82  desiredPrecisionOnMt2);
83 
84 
85 
86  return MT2;
87  }
88 
89 
90  string execute(const string &cmd){
91  FILE *pipe = popen(cmd.c_str(), "r");
92  if(!pipe) ERROR("Could not open pipe.");
93  const size_t buffer_size = 128;
94  char buffer[buffer_size];
95  string result = "";
96  while(!feof(pipe)){
97  if(fgets(buffer, buffer_size, pipe) != NULL) result += buffer;
98  }
99 
100  pclose(pipe);
101  return result;
102  }
103 
104  TString roundNumber(double num, int decimals, double denom){
105  if(denom==0) return " - ";
106  double neg = 1; if(num*denom<0) neg = -1;
107  num /= neg*denom; num += 0.5*pow(10.,-decimals);
108  long num_int = static_cast<long>(num);
109  long num_dec = static_cast<long>((1+num-num_int)*pow(10.,decimals));
110  TString s_dec = ""; s_dec += num_dec; s_dec.Remove(0,1);
111  TString result="";
112  if(neg<0) result+="-";
113  result+= num_int;
114  if(decimals>0) {
115  result+="."; result+=s_dec;
116  }
117 
118  TString afterdot = result;
119  afterdot.Remove(0,afterdot.First(".")+1);
120  for(int i=0; i<decimals-afterdot.Length(); i++)
121  result += "0";
122  return result;
123  }
124 
125  TString addCommas(double num){
126  TString result(""); result += num;
127  int posdot(result.First('.'));
128  if(posdot==-1) posdot = result.Length();
129  for(int ind(posdot-3); ind > 0; ind -= 3)
130  result.Insert(ind, ",");
131  return result;
132  }
133 
134 }
bool greaterM(const fastjet::PseudoJet &a, const fastjet::PseudoJet &b)
Definition: utilities.cc:40
TString addCommas(double num)
Definition: utilities.cc:499
static double get_mT2(const double mVis1, const double pxVis1, const double pyVis1, const double mVis2, const double pxVis2, const double pyVis2, const double pxMiss, const double pyMiss, const double mInvis1, const double mInvis2, const double desiredPrecisionOnMT2=0, const bool useDeciSectionsInitially=true)
string execute(const string &cmd)
Definition: utilities.cc:567
STL namespace.
TString roundNumber(double num, int decimals, double denom)
Definition: utilities.cc:478
float getMT(float pt1, float phi1, float pt2, float phi2)
Definition: utilities.cc:44
#define ERROR(x)
Definition: utilities.hh:18
string cmd
Moving file.
float dR(float eta1, float eta2, float phi1, float phi2)
Definition: utilities.cc:474
bool contains(const string &s, const string &pat)
Definition: utilities.cc:12
bool greaterPt(const reco::Candidate *a, const reco::Candidate *b)
Definition: utilities.cc:36
float sumMass(const LVector &a, const LVector &b)
Definition: utilities.cc:16
tuple ind
Definition: resubmit.py:140
reco::Candidate::LorentzVector LVector
Definition: utilities.hh:16
float dPhi(float phi1, float phi2)
Definition: utilities.cc:24
float getMT2(float pt1, float phi1, float pt2, float phi2, float met, float met_phi)
Definition: utilities.cc:54
float sumPt(const LVector &a, const LVector &b)
Definition: utilities.cc:20
static void disableCopyrightMessage(const bool printIfFirst=false)