CMS 3D CMS Logo

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