CMS 3D CMS Logo

QGLikelihoodCalculator.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import ROOT
3 import math
4 
5 
6 
7 def getBinNumber( bins, value ) :
8 
9  if value<bins[0] or value>bins[len(bins)-1]: return False
10  for i in range(0,len(bins)-1):
11  if value>bins[i] and value<bins[i+1]:
12  return i
13 
14  return -1
15 
16 
17 
18 
20 
21  def __init__(self, filename) :
22  self.pdfs = {}
23  self.etaBins = []
24  self.ptBins = []
25  self.rhoBins = []
26  self.varNames = { 0:'mult', 1:'ptD', 2:'axis2' }
27  self.qgNames = { 0:'quark', 1:'gluon' }
28  self.init(filename)
29 
30  def init(self, filename) :
31 
32  print("[QGLikelihoodCalculator]: Initializing from file: " + filename)
33 
34  f = ROOT.TFile.Open(filename)
35  if f.IsZombie() : return False
36  try :
37  self.etaBins = f.Get("etaBins")
38  self.ptBins = f.Get("ptBins")
39  self.rhoBins = f.Get("rhoBins")
40  except :
41  return False
42 
43 
44 
45  self.pdfs = {}
46 
47  for it in range(0,len(self.qgNames)):
48  self.pdfs[self.qgNames[it]] = {}
49  for iv in range(0,len(self.varNames)):
50  self.pdfs[self.qgNames[it]][self.varNames[iv]] = {}
51  for ie in range(0,len(self.etaBins)):
52  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie] = {}
53  for ip in range(0,len(self.ptBins)):
54  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie][ip] = {}
55  for ir in range(0,len(self.rhoBins)):
56  self.pdfs[self.qgNames[it]][self.varNames[iv]][ie][ip][ir] = 0
57 
58 
59 
60  print("[QGLikelihoodCalculator]: Initialized binning of pdfs...")
61 
62  keys = f.GetListOfKeys()
63  for key in keys :
64  if key.IsFolder() == False: continue
65  hists = key.ReadObj().GetListOfKeys()
66  for hist in hists :
67  pieces = hist.GetName().split("_")
68  varName = pieces[0]
69  qgType = pieces[1]
70  etaStr = pieces[2]
71  ptStr = pieces[3]
72  rhoStr = pieces[4]
73  etaBin = int(etaStr.split("eta")[1])
74  ptBin = int( ptStr.split("pt") [1])
75  rhoBin = int(rhoStr.split("rho")[1])
76  histogram = hist.ReadObj()
77  histogram.SetDirectory(0)
78  self.pdfs[qgType][varName][etaBin][ptBin][rhoBin] = histogram
79 
80  print("[QGLikelihoodCalculator]: pdfs initialized...")
81 
82 
83  return True
84 
85 
86 
87 
88  def isValidRange( self, pt, rho, eta ) :
89 
90  if pt < self.ptBins[0]: return False
91  if pt > self.ptBins[len(self.ptBins)-1]: return False
92  if rho < self.rhoBins[0]: return False
93  if rho > self.rhoBins[len(self.rhoBins)-1]: return False
94  if math.fabs(eta) < self.etaBins[0]: return False
95  if math.fabs(eta) > self.etaBins[len(self.etaBins)-1]: return False
96  return True
97 
98 
99 
100 
101  def findEntry( self, eta, pt, rho, qgType, varName ) :
102 
103  etaBin = getBinNumber( self.etaBins, math.fabs(eta))
104  if etaBin==-1: return None
105  ptBin = getBinNumber( self.ptBins, pt )
106  if ptBin==-1 : return None
107 
108  rhoBin = getBinNumber( self.rhoBins, rho )
109  if rhoBin==-1 : return None
110 
111  return self.pdfs[qgType][varName][etaBin][ptBin][rhoBin]
112 
113 
114 
115 
116 
117  def computeQGLikelihood( self, jet, rho ):
118 
119  if self.isValidRange(jet.pt(), rho, jet.eta())==False: return -1
120 
121  # careful!!! this needs to be in the same order of self.varNames
122  vars = {0:jet.mult, 1:jet.ptd, 2:jet.axis2}
123 
124  Q=1.
125  G=1.
126 
127  #print "----------------------"
128  #if jet.partonFlavour()==21 :
129  # print "this jet is a GLUON"
130  #elif math.fabs(jet.partonFlavour())<4 and jet.partonFlavour()!=0:
131  # print "this jet is a QUARK"
132  #print "pt: " + str(jet.pt()) + " eta: " + str(jet.eta()) + " rho: " + str(rho)
133  #print "multi: " + str(jet.mult) + " ptd: " + str(jet.ptd) + " axis2: " + str(jet.axis2)
134 
135  for i in vars :
136 
137  #print self.varNames[i] + ": " + str(vars[i])
138  # quarks
139  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, "quark", self.varNames[i])
140 
141  if qgEntry == None: return -1
142  Qi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
143  mQ = qgEntry.GetMean()
144  #print "Qi: " + str(Qi)
145 
146  # gluons
147  qgEntry = self.findEntry(jet.eta(), jet.pt(), rho, "gluon", self.varNames[i])
148 
149  if qgEntry == None: return -1
150  Gi = qgEntry.GetBinContent(qgEntry.FindBin(vars[i]))
151  mG = qgEntry.GetMean()
152  #print "Gi: " + str(Gi)
153 
154  epsilon=0.
155  delta=0.000001
156  if Qi <= epsilon and Gi <= epsilon :
157  if mQ>mG :
158  if vars[i] > mQ :
159  Qi = 1.-delta
160  Gi = delta
161  elif vars[i] < mG :
162  Qi = delta
163  Gi = 1.-delta
164  else :
165  if vars[i]<mQ :
166  Qi = 1.-delta
167  Gi = delta
168  elif vars[i]>mG :
169  Qi = delta
170  Gi = 1.-delta
171 
172  Q*=Qi
173  G*=Gi
174 
175 
176  #print "Q: " + str(Q)
177  #print "G: " + str(G)
178  if Q==0. : return 0.
179  else : return Q/(Q+G)
180 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def findEntry(self, eta, pt, rho, qgType, varName)
double split
Definition: MVATrainer.cc:139