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