CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QGLikelihoodCalculator.py
Go to the documentation of this file.
1 import ROOT
2 import math
3 
4 
5 
6 def getBinNumber( bins, value ) :
7 
8  if value<bins[0] or value>bins[len(bins)-1]: return False
9  for i in range(0,len(bins)-1):
10  if value>bins[i] and value<bins[i+1]:
11  return i
12 
13  return -1
14 
15 
16 
17 
19 
20  def __init__(self, filename) :
21  self.pdfs = {}
22  self.etaBins = []
23  self.ptBins = []
24  self.rhoBins = []
25  self.varNames = { 0:'mult', 1:'ptD', 2:'axis2' }
26  self.qgNames = { 0:'quark', 1:'gluon' }
27  self.init(filename)
28 
29  def init(self, filename) :
30 
31  print "[QGLikelihoodCalculator]: Initializing from file: " + filename
32 
33  f = ROOT.TFile.Open(filename)
34  if f.IsZombie() : return False
35  try :
36  self.etaBins = f.Get("etaBins")
37  self.ptBins = f.Get("ptBins")
38  self.rhoBins = f.Get("rhoBins")
39  except :
40  return False
41 
42  print "[QGLikelihoodCalculator]: Initialized binning of pdfs..."
43 
44  keys = f.GetListOfKeys()
45  for key in keys :
46  if key.IsFolder() == False: continue
47  hists = key.ReadObj().GetListOfKeys()
48  for hist in hists :
49  histogram = hist.ReadObj()
50  histogram.SetDirectory(0)
51  self.pdfs[hist.GetName()] = histogram
52 
53  print "[QGLikelihoodCalculator]: pdfs initialized..."
54 
55  return True
56 
57 
58 
59 
60  def isValidRange( self, pt, rho, eta ) :
61 
62  if pt < self.ptBins[0]: return False
63  if pt > self.ptBins[len(self.ptBins)-1]: return False
64  if rho < self.rhoBins[0]: return False
65  if rho > self.rhoBins[len(self.rhoBins)-1]: return False
66  if math.fabs(eta) < self.etaBins[0]: return False
67  if math.fabs(eta) > self.etaBins[len(self.etaBins)-1]: return False
68  return True
69 
70 
71 
72 
73  def findEntry( self, eta, pt, rho, qgIndex, varIndex ) :
74 
75  etaBin = getBinNumber( self.etaBins, math.fabs(eta))
76  if etaBin==-1: return None
77  ptBin = getBinNumber( self.ptBins, pt )
78  if ptBin==-1 : return None
79 
80  rhoBin = getBinNumber( self.rhoBins, rho )
81  if rhoBin==-1 : return None
82 
83  histName = self.varNames[varIndex] + "_" + self.qgNames[qgIndex] + "_eta" + str(etaBin) + "_pt" + str(ptBin) + "_rho" + str(rhoBin)
84 
85  return self.pdfs[histName]
86 
87 
88 
89 
90 
91  def computeQGLikelihood( self, jet, rho ):
92 
93  if self.isValidRange(jet.pt(), rho, jet.eta())==False: return -1
94 
95  vars = {0:jet.mult, 1:jet.ptd, 2:jet.axis2}
96 
97  Q=1.
98  G=1.
99 
100  #print "----------------------"
101  #if jet.partonFlavour()==21 :
102  # print "this jet is a GLUON"
103  #elif math.fabs(jet.partonFlavour())<4 and jet.partonFlavour()!=0:
104  # print "this jet is a QUARK"
105  #print "pt: " + str(jet.pt()) + " eta: " + str(jet.eta()) + " rho: " + str(rho)
106  #print "multi: " + str(jet.mult) + " ptd: " + str(jet.ptd) + " axis2: " + str(jet.axis2)
107 
108  for i in vars :
109 
110  #print self.varNames[i] + ": " + str(vars[i])
111  # quarks = 0
112  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, 0, i)
113 
114  if qgEntry == None: return -1
115  Qi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
116  mQ = qgEntry.GetMean()
117  #print "Qi: " + str(Qi)
118 
119  # gluons = 1
120  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, 1, i)
121 
122  if qgEntry == None: return -1
123  Gi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
124  mG = qgEntry.GetMean()
125  #print "Gi: " + str(Gi)
126 
127  epsilon=0.
128  delta=0.000001
129  if Qi <= epsilon and Gi <= epsilon :
130  if mQ>mG :
131  if vars[i] > mQ :
132  Qi = 1.-delta
133  Gi = delta
134  elif vars[i] < mG :
135  Qi = delta
136  Gi = 1.-delta
137  else :
138  if vars[i]<mQ :
139  Qi = 1.-delta
140  Gi = delta
141  elif vars[i]>mG :
142  Qi = delta
143  Gi = 1.-delta
144 
145  Q*=Qi
146  G*=Gi
147 
148 
149  #print "Q: " + str(Q)
150  #print "G: " + str(G)
151  if Q==0. : return 0.
152  else : return Q/(Q+G)
153