CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes
ShapeTools.ShapeBuilder Class Reference
Inheritance diagram for ShapeTools.ShapeBuilder:

Public Member Functions

def __init__
 
def argSetToString
 
def doCombination
 
def doCombinedDataset
 
def doIndividualModels
 
def doObservables
 

-----— ModelBuilder interface -------—

More...
 
def getData
 
def getExtraNorm
 
def getPdf
 
def getShape
 

-----— Low level helpers -------—

More...
 
def isShapeSystematic
 
def prepareAllShapes
 

-----— High level helpers -------—

More...
 
def shape2Data
 
def shape2Pdf
 

Public Attributes

 pdfModes
 
 wsp
 
 wspnames
 

Detailed Description

Definition at line 7 of file ShapeTools.py.

Constructor & Destructor Documentation

def ShapeTools.ShapeBuilder.__init__ (   self,
  datacard,
  options 
)

Definition at line 8 of file ShapeTools.py.

8 
9  def __init__(self,datacard,options):
10  ModelBuilder.__init__(self,datacard,options)
11  if not datacard.hasShapes:
12  raise RuntimeError, "You're using a ShapeBuilder for a model that has no shapes"
13  if options.libs:
14  for lib in options.libs:
15  ROOT.gSystem.Load(lib)
16  self.wspnames = {}
self.wsp = None

Member Function Documentation

def ShapeTools.ShapeBuilder.argSetToString (   self,
  argset 
)

Definition at line 467 of file ShapeTools.py.

References join().

468  def argSetToString(self,argset):
469  names = []
470  it = argset.createIterator()
471  while True:
472  arg = it.Next()
473  if not arg: break
474  names.append(arg.GetName())
475  return ",".join(names)
476 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def ShapeTools.ShapeBuilder.doCombination (   self)

Definition at line 77 of file ShapeTools.py.

References clone(), and cmsPerfRegress.getParameters().

77 
78  def doCombination(self):
79  ## Contrary to Number-counting models, here each channel PDF already contains the nuisances
80  ## So we just have to build the combined pdf
81  if len(self.DC.bins) > 1 or self.options.forceSimPdf:
82  for (postfixIn,postfixOut) in [ ("","_s"), ("_bonly","_b") ]:
83  simPdf = ROOT.RooSimultaneous("model"+postfixOut, "model"+postfixOut, self.out.binCat)
84  for b in self.DC.bins:
85  pdfi = self.out.pdf("pdf_bin%s%s" % (b,postfixIn))
86  simPdf.addPdf(pdfi, b)
87  self.out._import(simPdf)
88  else:
89  self.out._import(self.out.pdf("pdf_bin%s" % self.DC.bins[0]).clone("model_s"), ROOT.RooFit.Silence())
90  self.out._import(self.out.pdf("pdf_bin%s_bonly" % self.DC.bins[0]).clone("model_b"), ROOT.RooFit.Silence())
91  if self.options.fixpars:
92  pars = self.out.pdf("model_s").getParameters(self.out.obs)
93  iter = pars.createIterator()
94  while True:
95  arg = iter.Next()
96  if arg == None: break;
97  if arg.InheritsFrom("RooRealVar") and arg.GetName() != "r":
arg.setConstant(True);
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
def ShapeTools.ShapeBuilder.doCombinedDataset (   self)

Definition at line 196 of file ShapeTools.py.

References CSCTMBBlockedCFEB.getData(), CSCTMBMiniScope.getData(), TkLasBeam.getData(), DTConfigPluginHandler.getData(), DTConfigAbstractHandler.getData(), edm::EventSetup.getData(), EDMtoMEConverter.getData(), DTPosNeg.getData(), SiStripThreshold.getData(), SiPixelActionExecutor.getData(), and ShapeTools.ShapeBuilder.getData().

Referenced by ShapeTools.ShapeBuilder.doObservables().

197  def doCombinedDataset(self):
198  if len(self.DC.bins) == 1 and not self.options.forceSimPdf:
199  data = self.getData(self.DC.bins[0],self.options.dataname).Clone(self.options.dataname)
200  self.out._import(data)
201  return
202  if self.out.mode == "binned":
203  combiner = ROOT.CombDataSetFactory(self.out.obs, self.out.binCat)
204  for b in self.DC.bins: combiner.addSetBin(b, self.getData(b,self.options.dataname))
205  self.out.data_obs = combiner.done(self.options.dataname,self.options.dataname)
206  self.out._import(self.out.data_obs)
207  elif self.out.mode == "unbinned":
208  combiner = ROOT.CombDataSetFactory(self.out.obs, self.out.binCat)
209  for b in self.DC.bins: combiner.addSetAny(b, self.getData(b,self.options.dataname))
210  self.out.data_obs = combiner.doneUnbinned(self.options.dataname,self.options.dataname)
211  self.out._import(self.out.data_obs)
212  else: raise RuntimeException, "Only combined datasets are supported"
213  #print "Created combined dataset with ",self.out.data_obs.numEntries()," entries, out of:"
#for b in self.DC.bins: print " bin", b, ": entries = ", self.getData(b,self.options.dataname).numEntries()
def ShapeTools.ShapeBuilder.doIndividualModels (   self)

Definition at line 39 of file ShapeTools.py.

References ModelTools.ModelBuilderBase.doObj(), ShapeTools.ShapeBuilder.getExtraNorm(), ShapeTools.ShapeBuilder.getPdf(), relativeConstraints.keys, ShapeTools.ShapeBuilder.pdfModes, and indexGen.title.

39 
40  def doIndividualModels(self):
41  for b in self.DC.bins:
42  pdfs = ROOT.RooArgList(); bgpdfs = ROOT.RooArgList()
43  coeffs = ROOT.RooArgList(); bgcoeffs = ROOT.RooArgList()
44  for p in self.DC.exp[b].keys(): # so that we get only self.DC.processes contributing to this bin
45  if self.DC.exp[b][p] == 0: continue
46  (pdf,coeff) = (self.getPdf(b,p), self.out.function("n_exp_bin%s_proc_%s" % (b,p)))
47  extranorm = self.getExtraNorm(b,p)
48  if extranorm:
49  self.doObj("n_exp_final_bin%s_proc_%s" % (b,p), "prod", "n_exp_bin%s_proc_%s, %s" % (b,p, extranorm))
50  coeff = self.out.function("n_exp_final_bin%s_proc_%s" % (b,p))
51  pdfs.add(pdf); coeffs.add(coeff)
52  if not self.DC.isSignal[p]:
53  bgpdfs.add(pdf); bgcoeffs.add(coeff)
54  if self.options.verbose: print "Creating RooAddPdf %s with %s elements" % ("pdf_bin"+b, coeffs.getSize())
55  sum_s = ROOT.RooAddPdf("pdf_bin%s" % b, "", pdfs, coeffs)
56  sum_b = ROOT.RooAddPdf("pdf_bin%s_bonly" % b, "", bgpdfs, bgcoeffs)
57  if b in self.pdfModes:
58  sum_s.setAttribute('forceGen'+self.pdfModes[b].title())
59  sum_b.setAttribute('forceGen'+self.pdfModes[b].title())
60  if len(self.DC.systs):
61  ## rename the pdfs
62  sum_s.SetName("pdf_bin%s_nuis" % b); sum_b.SetName("pdf_bin%s_bonly_nuis" % b)
63  # now we multiply by all the nuisances, but avoiding nested products
64  # so we first make a list of all nuisances plus the RooAddPdf
65  sumPlusNuis_s = ROOT.RooArgList(self.out.nuisPdfs); sumPlusNuis_s.add(sum_s)
66  sumPlusNuis_b = ROOT.RooArgList(self.out.nuisPdfs); sumPlusNuis_b.add(sum_b)
67  # then make RooProdPdf and import it
68  pdf_s = ROOT.RooProdPdf("pdf_bin%s" % b, "", sumPlusNuis_s)
69  pdf_b = ROOT.RooProdPdf("pdf_bin%s_bonly" % b, "", sumPlusNuis_b)
70  if b in self.pdfModes:
71  pdf_s.setAttribute('forceGen'+self.pdfModes[b].title())
72  pdf_b.setAttribute('forceGen'+self.pdfModes[b].title())
73  self.out._import(pdf_s, ROOT.RooFit.RenameConflictNodes(b))
74  self.out._import(pdf_b, ROOT.RooFit.RecycleConflictNodes(), ROOT.RooFit.Silence())
75  else:
76  self.out._import(sum_s, ROOT.RooFit.RenameConflictNodes(b))
self.out._import(sum_b, ROOT.RooFit.RecycleConflictNodes(), ROOT.RooFit.Silence())
def ShapeTools.ShapeBuilder.doObservables (   self)


-----— ModelBuilder interface -------—

Definition at line 20 of file ShapeTools.py.

References ShapeTools.ShapeBuilder.doCombinedDataset(), ModelTools.ModelBuilderBase.doSet(), ModelTools.ModelBuilderBase.doVar(), join(), and ShapeTools.ShapeBuilder.prepareAllShapes().

20 
21  def doObservables(self):
22  if (self.options.verbose > 1): stderr.write("Using shapes: qui si parra' la tua nobilitate\n")
23  self.prepareAllShapes();
24  if len(self.DC.bins) > 1 or self.options.forceSimPdf:
25  ## start with just a few channels
26  strexpr="CMS_channel[" + ",".join(["%s=%d" % (l,i) for i,l in enumerate(self.DC.bins[:5])]) + "]";
27  self.doVar(strexpr);
28  self.out.binCat = self.out.cat("CMS_channel");
29  ## then add all the others, to avoid a too long factory string
30  for i,l in enumerate(self.DC.bins[5:]): self.out.binCat.defineType(l,i+5)
31  if self.options.verbose: stderr.write("Will use category 'CMS_channel' to identify the %d channels\n" % self.out.binCat.numTypes())
32  self.out.obs = ROOT.RooArgSet()
33  self.out.obs.add(self.out.binVars)
34  self.out.obs.add(self.out.binCat)
35  else:
36  self.out.obs = self.out.binVars
37  self.doSet("observables",self.out.obs)
38  if len(self.DC.obs) != 0:
self.doCombinedDataset()
def prepareAllShapes
-----— High level helpers -------—
Definition: ShapeTools.py:101
def doObservables
-----— ModelBuilder interface -------—
Definition: ShapeTools.py:20
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def ShapeTools.ShapeBuilder.getData (   self,
  channel,
  process,
  syst = "",
  _cache = {} 
)

Definition at line 310 of file ShapeTools.py.

References HcalPulseShapes.getShape(), HcalQIEData.getShape(), CastorQIEData.getShape(), FWGeometry.getShape(), ShapeTools.ShapeBuilder.getShape(), and ShapeTools.ShapeBuilder.shape2Data().

Referenced by ShapeTools.ShapeBuilder.doCombinedDataset().

311  def getData(self,channel,process,syst="",_cache={}):
return self.shape2Data(self.getShape(channel,process,syst),channel,process)
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
def ShapeTools.ShapeBuilder.getExtraNorm (   self,
  channel,
  process 
)

Definition at line 371 of file ShapeTools.py.

References abs, HcalPulseShapes.getShape(), HcalQIEData.getShape(), CastorQIEData.getShape(), FWGeometry.getShape(), ShapeTools.ShapeBuilder.getShape(), ModelTools.ModelBuilder.isShapeSystematic(), ShapeTools.ShapeBuilder.isShapeSystematic(), join(), and funct.pow().

Referenced by ShapeTools.ShapeBuilder.doIndividualModels().

372  def getExtraNorm(self,channel,process):
373  postFix="Sig" if (process in self.DC.isSignal and self.DC.isSignal[process]) else "Bkg"
374  terms = []
375  shapeNominal = self.getShape(channel,process)
376  if shapeNominal == None:
377  # FIXME no extra norm for dummy pdfs (could be changed)
378  return None
379  if shapeNominal.InheritsFrom("RooAbsPdf"):
380  # return nominal multiplicative normalization constant
381  normname = "shape%s_%s_%s%s_norm" % (postFix,process,channel, "_")
382  if self.out.arg(normname): return normname
383  else: return None
384  normNominal = 0
385  if shapeNominal.InheritsFrom("TH1"): normNominal = shapeNominal.Integral()
386  elif shapeNominal.InheritsFrom("RooDataHist"): normNominal = shapeNominal.sumEntries()
387  else: return None
388  if normNominal == 0: raise RuntimeError, "Null norm for channel %s, process %s" % (channel,process)
389  for (syst,nofloat,pdf,args,errline) in self.DC.systs:
390  if "shape" not in pdf: continue
391  if errline[channel][process] != 0:
392  if pdf[-1] == "?" and not self.isShapeSystematic(channel,process,syst): continue
393  shapeUp = self.getShape(channel,process,syst+"Up")
394  shapeDown = self.getShape(channel,process,syst+"Down")
395  if shapeUp.ClassName() != shapeNominal.ClassName(): raise RuntimeError, "Mismatched shape types for channel %s, process %s, syst" % (channel,process,syst)
396  if shapeDown.ClassName() != shapeNominal.ClassName(): raise RuntimeError, "Mismatched shape types for channel %s, process %s, syst" % (channel,process,syst)
397  kappaUp,kappaDown = 1,1
398  if shapeNominal.InheritsFrom("TH1"):
399  kappaUp,kappaDown = shapeUp.Integral(),shapeDown.Integral()
400  elif shapeNominal.InheritsFrom("RooDataHist"):
401  kappaUp,kappaDown = shapeUp.sumEntries(),shapeDown.sumEntries()
402  if not kappaUp > 0: raise RuntimeError, "Bogus norm %r for channel %s, process %s, systematic %s Up" % (kappaUp, channel,process,syst)
403  if not kappaDown > 0: raise RuntimeError, "Bogus norm %r for channel %s, process %s, systematic %s Down" % (kappaDown, channel,process,syst)
404  kappaUp /=normNominal; kappaDown /= normNominal
405  if abs(kappaUp-1) < 1e-3 and abs(kappaDown-1) < 1e-3: continue
406  # if errline[channel][process] == <x> it means the gaussian should be scaled by <x> before doing pow
407  # for convenience, we scale the kappas
408  kappasScaled = [ pow(x, errline[channel][process]) for x in kappaDown,kappaUp ]
409  terms.append("AsymPow(%f,%f,%s)" % (kappasScaled[0], kappasScaled[1], syst))
return ",".join(terms) if terms else None;
#define abs(x)
Definition: mlp_lapack.h:159
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def ShapeTools.ShapeBuilder.getPdf (   self,
  channel,
  process,
  _cache = {} 
)

Definition at line 312 of file ShapeTools.py.

References ModelTools.ModelBuilderBase.doObj(), first, reco.get(), HcalPulseShapes.getShape(), HcalQIEData.getShape(), CastorQIEData.getShape(), FWGeometry.getShape(), ShapeTools.ShapeBuilder.getShape(), ModelTools.ModelBuilder.isShapeSystematic(), ShapeTools.ShapeBuilder.isShapeSystematic(), and ShapeTools.ShapeBuilder.shape2Pdf().

Referenced by ShapeTools.ShapeBuilder.doIndividualModels().

313  def getPdf(self,channel,process,_cache={}):
314  postFix="Sig" if (process in self.DC.isSignal and self.DC.isSignal[process]) else "Bkg"
315  if _cache.has_key((channel,process)): return _cache[(channel,process)]
316  shapeNominal = self.getShape(channel,process)
317  nominalPdf = self.shape2Pdf(shapeNominal,channel,process)
318  if shapeNominal == None: return nominalPdf # no point morphing a fake shape
319  morphs = []; shapeAlgo = None
320  for (syst,nofloat,pdf,args,errline) in self.DC.systs:
321  if not "shape" in pdf: continue
322  if errline[channel][process] == 0: continue
323  allowNoSyst = (pdf[-1] == "?")
324  pdf = pdf.replace("?","")
325  if shapeAlgo == None: shapeAlgo = pdf
326  elif pdf != shapeAlgo:
327  errmsg = "ERROR for channel %s, process %s. " % (channel,process)
328  errmsg += "Requesting morphing %s for systematic %s after having requested %s. " % (pdf, syst, shapeAlgo)
329  raise RuntimeError, errmsg+" One can use only one morphing algorithm for a given shape";
330  if errline[channel][process] != 0:
331  if allowNoSyst and not self.isShapeSystematic(channel,process,syst): continue
332  shapeUp = self.getShape(channel,process,syst+"Up")
333  shapeDown = self.getShape(channel,process,syst+"Down")
334  if shapeUp.ClassName() != shapeNominal.ClassName(): raise RuntimeError, "Mismatched shape types for channel %s, process %s, syst %s" % (channel,process,syst)
335  if shapeDown.ClassName() != shapeNominal.ClassName(): raise RuntimeError, "Mismatched shape types for channel %s, process %s, syst %s" % (channel,process,syst)
336  morphs.append((syst,errline[channel][process],self.shape2Pdf(shapeUp,channel,process),self.shape2Pdf(shapeDown,channel,process)))
337  if len(morphs) == 0: return nominalPdf
338  if shapeAlgo == "shapeN": stderr.write("Warning: the shapeN implementation in RooStats and L&S are different\n")
339  pdfs = ROOT.RooArgList(nominalPdf)
340  coeffs = ROOT.RooArgList()
341  minscale = 1
342  for (syst,scale,pdfUp,pdfDown) in morphs:
343  pdfs.add(pdfUp); pdfs.add(pdfDown);
344  if scale == 1:
345  coeffs.add(self.out.var(syst))
346  else: # must scale it :-/
347  coeffs.add(self.doObj("%s_scaled_%s_%s" % (syst,channel,process), "prod","%s, %s" % (scale,syst)))
348  if scale < minscale: minscale = scale
349  qrange = minscale; qalgo = 0;
350  if shapeAlgo[-1] == "*":
351  qalgo = 100
352  shapeAlgo = shapeAlgo[:-1]
353  if shapeAlgo == "shape": shapeAlgo = self.options.defMorph
354  if "shapeL" in shapeAlgo: qrange = 0;
355  elif "shapeN" in shapeAlgo: qalgo = -1;
356  if "2a" in shapeAlgo: # old shape2
357  if not nominalPdf.InheritsFrom("RooHistPdf"): raise RuntimeError, "Algorithms 'shape2', 'shapeL2', shapeN2' only work with histogram templates"
358  if nominalPdf.dataHist().get().getSize() != 1: raise RuntimeError, "Algorithms 'shape2', 'shapeL2', shapeN2' only work in one dimension"
359  xvar = nominalPdf.dataHist().get().first()
360  _cache[(channel,process)] = ROOT.VerticalInterpHistPdf("shape%s_%s_%s_morph" % (postFix,channel,process), "", xvar, pdfs, coeffs, qrange, qalgo)
361  elif "2" in shapeAlgo: # new faster shape2
362  if not nominalPdf.InheritsFrom("RooHistPdf"): raise RuntimeError, "Algorithms 'shape2', 'shapeL2', shapeN2' only work with histogram templates"
363  if nominalPdf.dataHist().get().getSize() != 1: raise RuntimeError, "Algorithms 'shape2', 'shapeL2', shapeN2' only work in one dimension"
364  xvar = nominalPdf.dataHist().get().first()
365  _cache[(channel,process)] = ROOT.FastVerticalInterpHistPdf("shape%s_%s_%s_morph" % (postFix,channel,process), "", xvar, pdfs, coeffs, qrange, qalgo)
366  else:
367  _cache[(channel,process)] = ROOT.VerticalInterpPdf("shape%s_%s_%s_morph" % (postFix,channel,process), "", pdfs, coeffs, qrange, qalgo)
return _cache[(channel,process)]
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
bool first
Definition: L1TdeRCT.cc:94
T get(const Candidate &c)
Definition: component.h:56
def ShapeTools.ShapeBuilder.getShape (   self,
  channel,
  process,
  syst = "",
  _fileCache = {},
  _cache = {},
  allowNoSyst = False 
)


-----— Low level helpers -------—

Definition at line 217 of file ShapeTools.py.

References ModelTools.ModelBuilderBase.doVar(), linker.replace(), ShapeTools.ShapeBuilder.wsp, and ShapeTools.ShapeBuilder.wspnames.

Referenced by ShapeTools.ShapeBuilder.getData(), ShapeTools.ShapeBuilder.getExtraNorm(), ShapeTools.ShapeBuilder.getPdf(), and ShapeTools.ShapeBuilder.isShapeSystematic().

218  def getShape(self,channel,process,syst="",_fileCache={},_cache={},allowNoSyst=False):
219  if _cache.has_key((channel,process,syst)):
220  if self.options.verbose > 1: print "recyling (%s,%s,%s) -> %s\n" % (channel,process,syst,_cache[(channel,process,syst)].GetName())
221  return _cache[(channel,process,syst)];
222  postFix="Sig" if (process in self.DC.isSignal and self.DC.isSignal[process]) else "Bkg"
223  bentry = None
224  if self.DC.shapeMap.has_key(channel): bentry = self.DC.shapeMap[channel]
225  elif self.DC.shapeMap.has_key("*"): bentry = self.DC.shapeMap["*"]
226  else: raise KeyError, "Shape map has no entry for channel '%s'" % (channel)
227  names = []
228  if bentry.has_key(process): names = bentry[process]
229  elif bentry.has_key("*"): names = bentry["*"]
230  elif self.DC.shapeMap["*"].has_key(process): names = self.DC.shapeMap["*"][process]
231  elif self.DC.shapeMap["*"].has_key("*"): names = self.DC.shapeMap["*"]["*"]
232  else: raise KeyError, "Shape map has no entry for process '%s', channel '%s'" % (process,channel)
233  if len(names) == 1 and names[0] == "FAKE": return None
234  if syst != "":
235  if len(names) == 2:
236  if allowNoSyst: return None
237  raise RuntimeError, "Can't find systematic "+syst+" for process '%s', channel '%s'" % (process,channel)
238  names = [names[0], names[2]]
239  else:
240  names = [names[0], names[1]]
241  strmass = "%d" % self.options.mass if self.options.mass % 1 == 0 else str(self.options.mass)
242  finalNames = [ x.replace("$PROCESS",process).replace("$CHANNEL",channel).replace("$SYSTEMATIC",syst).replace("$MASS",strmass) for x in names ]
243  if not _fileCache.has_key(finalNames[0]):
244  trueFName = finalNames[0]
245  if not os.path.exists(trueFName) and not os.path.isabs(trueFName) and os.path.exists(self.options.baseDir+"/"+trueFName):
246  trueFName = self.options.baseDir+"/"+trueFName;
247  _fileCache[finalNames[0]] = ROOT.TFile.Open(trueFName)
248  file = _fileCache[finalNames[0]]; objname = finalNames[1]
249  if not file: raise RuntimeError, "Cannot open file %s (from pattern %s)" % (finalNames[0],names[0])
250  if ":" in objname: # workspace:obj or ttree:xvar or th1::xvar
251  (wname, oname) = objname.split(":")
252  if (file,wname) not in self.wspnames :
253  self.wspnames[(file,wname)] = file.Get(wname)
254  self.wsp = self.wspnames[(file,wname)]
255  if not self.wsp: raise RuntimeError, "Failed to find %s in file %s (from pattern %s, %s)" % (wname,finalNames[0],names[1],names[0])
256  if self.wsp.ClassName() == "RooWorkspace":
257  ret = self.wsp.data(oname)
258  if not ret: ret = self.wsp.pdf(oname)
259  if not ret: raise RuntimeError, "Object %s in workspace %s in file %s does not exist or it's neither a data nor a pdf" % (oname, wname, finalNames[0])
260  # Fix the fact that more than one entry can refer to the same object
261  ret = ret.Clone()
262  ret.SetName("shape%s_%s_%s%s" % (postFix,process,channel, "_"+syst if syst else ""))
263  _cache[(channel,process,syst)] = ret
264  if not syst:
265  normname = "%s_norm" % (oname)
266  norm = self.wsp.arg(normname)
267  if norm:
268  if normname in self.DC.flatParamNuisances:
269  self.DC.flatParamNuisances[normname] = False # don't warn if not found
270  norm.setAttribute("flatParam")
271  norm.SetName("shape%s_%s_%s%s_norm" % (postFix,process,channel, "_"))
272  self.out._import(norm, ROOT.RooFit.RecycleConflictNodes())
273  if self.options.verbose > 1: print "import (%s,%s) -> %s\n" % (finalNames[0],objname,ret.GetName())
274  return ret;
275  elif self.wsp.ClassName() == "TTree":
276  ##If it is a tree we will convert it in RooDataSet . Then we can decide if we want to build a
277  ##RooKeysPdf or if we want to use it as an unbinned dataset
278  if not self.wsp: raise RuntimeError, "Failed to find %s in file %s (from pattern %s, %s)" % (wname,finalNames[0],names[1],names[0])
279  self.doVar("%s[%f,%f]" % (oname,self.wsp.GetMinimum(oname),self.wsp.GetMaximum(oname)))
280  #Check if it is weighted
281  self.doVar("__WEIGHT__[0.,1000.]")
282  rds = ROOT.RooDataSet("shape%s_%s_%s%s" % (postFix,process,channel, "_"+syst if syst else ""), "shape%s_%s_%s%s" % (postFix,process,channel, "_"+syst if syst else ""),self.wsp,ROOT.RooArgSet(self.out.var(oname)),"","__WEIGHT__")
283  rds.var = oname
284  _cache[(channel,process,syst)] = rds
285  if self.options.verbose > 1: print "import (%s,%s) -> %s\n" % (finalNames[0],wname,rds.GetName())
286  return rds
287  elif self.wsp.InheritsFrom("TH1"):
288  ##If it is a Histogram we will convert it in RooDataSet preserving the bins
289  if not self.wsp: raise RuntimeError, "Failed to find %s in file %s (from pattern %s, %s)" % (wname,finalNames[0],names[1],names[0])
290  name = "shape%s_%s_%s%s" % (postFix,process,channel, "_"+syst if syst else "")
291  # don't make it twice
292  for X in _neverDelete:
293  if X.InheritsFrom("TNamed") and X.GetName() == name: return X
294  self.doVar("%s[%f,%f]" % (oname,self.wsp.GetXaxis().GetXmin(),self.wsp.GetXaxis().GetXmax()))
295  rds = ROOT.RooDataHist(name, name, ROOT.RooArgList(self.out.var(oname)), self.wsp)
296  rds.var = oname
297  if self.options.verbose > 1: stderr.write("import (%s,%s) -> %s\n" % (finalNames[0],wname,rds.GetName()))
298  _neverDelete.append(rds)
299  return rds
300  else:
301  raise RuntimeError, "Object %s in file %s has unrecognized type %s" (wname, finalNames[0], self.wsp.ClassName())
302  else: # histogram
303  ret = file.Get(objname);
304  if not ret:
305  if allowNoSyst: return None
306  raise RuntimeError, "Failed to find %s in file %s (from pattern %s, %s)" % (objname,finalNames[0],names[1],names[0])
307  ret.SetName("shape%s_%s_%s%s" % (postFix,process,channel, "_"+syst if syst else ""))
308  if self.options.verbose > 1: print "import (%s,%s) -> %s\n" % (finalNames[0],objname,ret.GetName())
309  _cache[(channel,process,syst)] = ret
return ret
def replace
Definition: linker.py:10
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
def ShapeTools.ShapeBuilder.isShapeSystematic (   self,
  channel,
  process,
  syst 
)

Definition at line 368 of file ShapeTools.py.

References HcalPulseShapes.getShape(), HcalQIEData.getShape(), CastorQIEData.getShape(), FWGeometry.getShape(), and ShapeTools.ShapeBuilder.getShape().

Referenced by ShapeTools.ShapeBuilder.getExtraNorm(), and ShapeTools.ShapeBuilder.getPdf().

369  def isShapeSystematic(self,channel,process,syst):
370  shapeUp = self.getShape(channel,process,syst+"Up",allowNoSyst=True)
return shapeUp != None
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
def ShapeTools.ShapeBuilder.prepareAllShapes (   self)


-----— High level helpers -------—

Definition at line 101 of file ShapeTools.py.

Referenced by ShapeTools.ShapeBuilder.doObservables().

102  def prepareAllShapes(self):
103  shapeTypes = []; shapeBins = []; shapeObs = {}
104  self.pdfModes = {}
105  for ib,b in enumerate(self.DC.bins):
106  databins = {}; bgbins = {}
107  for p in [self.options.dataname]+self.DC.exp[b].keys():
108  if len(self.DC.obs) == 0 and p == self.options.dataname: continue
109  if p != self.options.dataname and self.DC.exp[b][p] == 0: continue
110  shape = self.getShape(b,p); norm = 0;
111  if shape == None: # counting experiment
112  if not self.out.var("CMS_fakeObs"):
113  self.doVar("CMS_fakeObs[0,1]");
114  self.out.var("CMS_fakeObs").setBins(1);
115  self.doSet("CMS_fakeObsSet","CMS_fakeObs");
116  shapeObs["CMS_fakeObsSet"] = self.out.set("CMS_fakeObsSet")
117  if p == self.options.dataname:
118  self.pdfModes[b] = 'binned'
119  shapeTypes.append("RooDataHist")
120  else:
121  shapeTypes.append("RooAbsPdf");
122  elif shape.ClassName().startswith("TH1"):
123  shapeTypes.append("TH1"); shapeBins.append(shape.GetNbinsX())
124  norm = shape.Integral()
125  if p == self.options.dataname:
126  if self.options.poisson > 0 and norm > self.options.poisson:
127  self.pdfModes[b] = 'poisson'
128  else:
129  self.pdfModes[b] = 'binned'
130  for i in xrange(1, shape.GetNbinsX()+1):
131  if shape.GetBinContent(i) > 0: databins[i] = True
132  elif not self.DC.isSignal[p]:
133  for i in xrange(1, shape.GetNbinsX()+1):
134  if shape.GetBinContent(i) > 0: bgbins[i] = True
135  elif shape.InheritsFrom("RooDataHist"):
136  shapeTypes.append("RooDataHist");
137  shapeBins.append(shape.numEntries())
138  shapeObs[self.argSetToString(shape.get())] = shape.get()
139  norm = shape.sumEntries()
140  if p == self.options.dataname:
141  if self.options.poisson > 0 and norm > self.options.poisson:
142  self.pdfModes[b] = 'poisson'
143  else:
144  self.pdfModes[b] = 'binned'
145  elif shape.InheritsFrom("RooDataSet"):
146  shapeTypes.append("RooDataSet");
147  shapeObs[self.argSetToString(shape.get())] = shape.get()
148  norm = shape.sumEntries()
149  if p == self.options.dataname: self.pdfModes[b] = 'unbinned'
150  elif shape.InheritsFrom("TTree"):
151  shapeTypes.append("TTree");
152  if p == self.options.dataname: self.pdfModes[b] = 'unbinned'
153  elif shape.InheritsFrom("RooAbsPdf"):
154  shapeTypes.append("RooAbsPdf");
155  else: raise RuntimeError, "Currently supporting only TH1s, RooDataHist and RooAbsPdfs"
156  if norm != 0:
157  if p == self.options.dataname:
158  if len(self.DC.obs):
159  if self.DC.obs[b] == -1: self.DC.obs[b] = norm
160  elif abs(norm-self.DC.obs[b]) > 0.01:
161  if not self.options.noCheckNorm: raise RuntimeError, "Mismatch in normalizations for observed data in bin %s: text %f, shape %f" % (b,self.DC.obs[b],norm)
162  else:
163  if self.DC.exp[b][p] == -1: self.DC.exp[b][p] = norm
164  elif self.DC.exp[b][p] > 0 and abs(norm-self.DC.exp[b][p]) > 0.01*max(1,self.DC.exp[b][p]):
165  if not self.options.noCheckNorm: raise RuntimeError, "Mismatch in normalizations for bin %s, process %s: rate %f, shape %f" % (b,p,self.DC.exp[b][p],norm)
166  if len(databins) > 0:
167  for i in databins.iterkeys():
168  if i not in bgbins: stderr.write("Channel %s has bin %d fill in data but empty in all backgrounds\n" % (b,i))
169  if shapeTypes.count("TH1"):
170  self.out.maxbins = max(shapeBins)
171  if self.options.verbose: stderr.write("Will use binning variable CMS_th1x with %d bins\n" % self.out.maxbins)
172  self.doVar("CMS_th1x[0,%d]" % self.out.maxbins); self.out.var("CMS_th1x").setBins(self.out.maxbins)
173  self.out.binVar = self.out.var("CMS_th1x")
174  shapeObs['CMS_th1x'] = self.out.binVar
175  if shapeTypes.count("TH1") == len(shapeTypes):
176  self.out.mode = "binned"
177  self.out.binVars = ROOT.RooArgSet(self.out.binVar)
178  elif shapeTypes.count("RooDataSet") > 0 or shapeTypes.count("TTree") > 0 or len(shapeObs.keys()) > 1:
179  self.out.mode = "unbinned"
180  if self.options.verbose: stderr.write("Will try to work with unbinned datasets\n")
181  if self.options.verbose: stderr.write("Observables: %s\n" % str(shapeObs.keys()))
182  if len(shapeObs.keys()) != 1:
183  self.out.binVars = ROOT.RooArgSet()
184  for obs in shapeObs.values():
185  self.out.binVars.add(obs, False)
186  else:
187  self.out.binVars = shapeObs.values()[0]
188  self.out._import(self.out.binVars)
189  else:
190  self.out.mode = "binned"
191  if self.options.verbose: stderr.write("Will try to make a binned dataset\n")
192  if self.options.verbose: stderr.write("Observables: %s\n" % str(shapeObs.keys()))
193  if len(shapeObs.keys()) != 1:
194  raise RuntimeError, "There's more than once choice of observables: %s\n" % str(shapeObs.keys())
195  self.out.binVars = shapeObs.values()[0]
self.out._import(self.out.binVars)
def prepareAllShapes
-----— High level helpers -------—
Definition: ShapeTools.py:101
#define abs(x)
Definition: mlp_lapack.h:159
def getShape
-----— Low level helpers -------—
Definition: ShapeTools.py:217
const T & max(const T &a, const T &b)
def ShapeTools.ShapeBuilder.shape2Data (   self,
  shape,
  channel,
  process,
  _cache = {} 
)

Definition at line 410 of file ShapeTools.py.

References min.

Referenced by ShapeTools.ShapeBuilder.getData(), and ShapeTools.ShapeBuilder.shape2Pdf().

411  def shape2Data(self,shape,channel,process,_cache={}):
412  postFix="Sig" if (process in self.DC.isSignal and self.DC.isSignal[process]) else "Bkg"
413  if shape == None:
414  name = "shape%s_%s_%s" % (postFix,channel,process)
415  if not _cache.has_key(name):
416  obs = ROOT.RooArgSet(self.out.var("CMS_fakeObs"))
417  obs.setRealValue("CMS_fakeObs",0.5);
418  if self.out.mode == "binned":
419  self.out.var("CMS_fakeObs").setBins(1)
420  rdh = ROOT.RooDataHist(name, name, obs)
421  rdh.set(obs, self.DC.obs[channel])
422  _cache[name] = rdh
423  else:
424  rds = ROOT.RooDataSet(name, name, obs)
425  if self.DC.obs[channel] == float(int(self.DC.obs[channel])):
426  for i in range(int(self.DC.obs[channel])): rds.add(obs)
427  else:
428  rds.add(obs, self.DC.obs[channel])
429  _cache[name] = rds
430  return _cache[name]
431  if not _cache.has_key(shape.GetName()):
432  if shape.ClassName().startswith("TH1"):
433  rebinh1 = ROOT.TH1F(shape.GetName()+"_rebin", "", self.out.maxbins, 0.0, float(self.out.maxbins))
434  for i in range(1,min(shape.GetNbinsX(),self.out.maxbins)+1):
435  rebinh1.SetBinContent(i, shape.GetBinContent(i))
436  rdh = ROOT.RooDataHist(shape.GetName(), shape.GetName(), ROOT.RooArgList(self.out.binVar), rebinh1)
437  self.out._import(rdh)
438  _cache[shape.GetName()] = rdh
439  elif shape.ClassName() in ["RooDataHist", "RooDataSet"]:
440  return shape
441  else: raise RuntimeError, "shape2Data not implemented for %s" % shape.ClassName()
return _cache[shape.GetName()]
#define min(a, b)
Definition: mlp_lapack.h:161
def ShapeTools.ShapeBuilder.shape2Pdf (   self,
  shape,
  channel,
  process,
  _cache = {} 
)

Definition at line 442 of file ShapeTools.py.

References ModelTools.ModelBuilderBase.doObj(), and ShapeTools.ShapeBuilder.shape2Data().

Referenced by ShapeTools.ShapeBuilder.getPdf().

443  def shape2Pdf(self,shape,channel,process,_cache={}):
444  postFix="Sig" if (process in self.DC.isSignal and self.DC.isSignal[process]) else "Bkg"
445  if shape == None:
446  name = "shape%s_%s_%s" % (postFix,channel,process)
447  if not _cache.has_key(name):
448  _cache[name] = ROOT.RooUniform(name, name, ROOT.RooArgSet(self.out.var("CMS_fakeObs")))
449  return _cache[name]
450  if not _cache.has_key(shape.GetName()+"Pdf"):
451  if shape.ClassName().startswith("TH1"):
452  rdh = self.shape2Data(shape,channel,process)
453  rhp = self.doObj("%sPdf" % shape.GetName(), "HistPdf", "{%s}, %s" % (self.out.binVar.GetName(), shape.GetName()))
454  _cache[shape.GetName()+"Pdf"] = rhp
455  elif shape.InheritsFrom("RooAbsPdf"):
456  _cache[shape.GetName()+"Pdf"] = shape
457  elif shape.InheritsFrom("RooDataHist"):
458  rhp = ROOT.RooHistPdf("%sPdf" % shape.GetName(), "", shape.get(), shape)
459  self.out._import(rhp)
460  _cache[shape.GetName()+"Pdf"] = rhp
461  elif shape.InheritsFrom("RooDataSet"):
462  rkp = ROOT.RooKeysPdf("%sPdf" % shape.GetName(), "", self.out.var(shape.var), shape,3,1.5);
463  self.out._import(rkp)
464  _cache[shape.GetName()+"Pdf"] = rkp
465  else:
466  raise RuntimeError, "shape2Pdf not implemented for %s" % shape.ClassName()
return _cache[shape.GetName()+"Pdf"]

Member Data Documentation

ShapeTools.ShapeBuilder.pdfModes

Definition at line 103 of file ShapeTools.py.

Referenced by ShapeTools.ShapeBuilder.doIndividualModels().

ShapeTools.ShapeBuilder.wsp

Definition at line 16 of file ShapeTools.py.

Referenced by ShapeTools.ShapeBuilder.getShape().

ShapeTools.ShapeBuilder.wspnames

Definition at line 15 of file ShapeTools.py.

Referenced by ShapeTools.ShapeBuilder.getShape().