CMS 3D CMS Logo

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 
43 
44  self.pdfs = {}
45 
46  for it in range(0,len(self.qgNames)):
47  self.pdfs[self.qgNames[it]] = {}
48  for iv in range(0,len(self.varNames)):
49  self.pdfs[self.qgNames[it]][self.varNames[iv]] = {}
50  for ie in range(0,len(self.etaBins)):
51  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie] = {}
52  for ip in range(0,len(self.ptBins)):
53  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie][ip] = {}
54  for ir in range(0,len(self.rhoBins)):
55  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie][ip][ir] = 0
56 
57 
58 
59  print "[QGLikelihoodCalculator]: Initialized binning of pdfs..."
60 
61  keys = f.GetListOfKeys()
62  for key in keys :
63  if key.IsFolder() == False: continue
64  hists = key.ReadObj().GetListOfKeys()
65  for hist in hists :
66  pieces = hist.GetName().split("_")
67  varName = pieces[0]
68  qgType = pieces[1]
69  etaStr = pieces[2]
70  ptStr = pieces[3]
71  rhoStr = pieces[4]
72  etaBin = int(etaStr.split("eta")[1])
73  ptBin = int( ptStr.split("pt") [1])
74  rhoBin = int(rhoStr.split("rho")[1])
75  histogram = hist.ReadObj()
76  histogram.SetDirectory(0)
77  self.pdfs[qgType][varName][etaBin][ptBin][rhoBin] = histogram
78 
79  print "[QGLikelihoodCalculator]: pdfs initialized..."
80 
81 
82  return True
83 
84 
85 
86 
87  def isValidRange( self, pt, rho, eta ) :
88 
89  if pt < self.ptBins[0]: return False
90  if pt > self.ptBins[len(self.ptBins)-1]: return False
91  if rho < self.rhoBins[0]: return False
92  if rho > self.rhoBins[len(self.rhoBins)-1]: return False
93  if math.fabs(eta) < self.etaBins[0]: return False
94  if math.fabs(eta) > self.etaBins[len(self.etaBins)-1]: return False
95  return True
96 
97 
98 
99 
100  def findEntry( self, eta, pt, rho, qgType, varName ) :
101 
102  etaBin = getBinNumber( self.etaBins, math.fabs(eta))
103  if etaBin==-1: return None
104  ptBin = getBinNumber( self.ptBins, pt )
105  if ptBin==-1 : return None
106 
107  rhoBin = getBinNumber( self.rhoBins, rho )
108  if rhoBin==-1 : return None
109 
110  return self.pdfs[qgType][varName][etaBin][ptBin][rhoBin]
111 
112 
113 
114 
115 
116  def computeQGLikelihood( self, jet, rho ):
117 
118  if self.isValidRange(jet.pt(), rho, jet.eta())==False: return -1
119 
120  # careful!!! this needs to be in the same order of self.varNames
121  vars = {0:jet.mult, 1:jet.ptd, 2:jet.axis2}
122 
123  Q=1.
124  G=1.
125 
126  #print "----------------------"
127  #if jet.partonFlavour()==21 :
128  # print "this jet is a GLUON"
129  #elif math.fabs(jet.partonFlavour())<4 and jet.partonFlavour()!=0:
130  # print "this jet is a QUARK"
131  #print "pt: " + str(jet.pt()) + " eta: " + str(jet.eta()) + " rho: " + str(rho)
132  #print "multi: " + str(jet.mult) + " ptd: " + str(jet.ptd) + " axis2: " + str(jet.axis2)
133 
134  for i in vars :
135 
136  #print self.varNames[i] + ": " + str(vars[i])
137  # quarks
138  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, "quark", self.varNames[i])
139 
140  if qgEntry == None: return -1
141  Qi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
142  mQ = qgEntry.GetMean()
143  #print "Qi: " + str(Qi)
144 
145  # gluons
146  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, "gluon", self.varNames[i])
147 
148  if qgEntry == None: return -1
149  Gi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
150  mG = qgEntry.GetMean()
151  #print "Gi: " + str(Gi)
152 
153  epsilon=0.
154  delta=0.000001
155  if Qi <= epsilon and Gi <= epsilon :
156  if mQ>mG :
157  if vars[i] > mQ :
158  Qi = 1.-delta
159  Gi = delta
160  elif vars[i] < mG :
161  Qi = delta
162  Gi = 1.-delta
163  else :
164  if vars[i]<mQ :
165  Qi = 1.-delta
166  Gi = delta
167  elif vars[i]>mG :
168  Qi = delta
169  Gi = 1.-delta
170 
171  Q*=Qi
172  G*=Gi
173 
174 
175  #print "Q: " + str(Q)
176  #print "G: " + str(G)
177  if Q==0. : return 0.
178  else : return Q/(Q+G)
179 
def findEntry(self, eta, pt, rho, qgType, varName)
double split
Definition: MVATrainer.cc:139