Пример #1
0
    def RooParametricHist(self,name=''):
        '''Produce a RooParametricHist2D filled with this object's binVars.

        Returns:
            RooParametricHist2D: Output RooFit object to pass to Combine.
        '''
        out_rph = {}
        out_add = {}
        for cat in _subspace:
            cat_name = self.name+'_'+cat
            cat_hist = self.binning.CreateHist(cat_name+'_temp',cat)
            obj_name = '%s_%s'%(name if name != '' else self.name, cat)

            self.binArgLists[cat] = RooArgList()
            for ybin in range(1,len(self.binning.ybinList)):
                for xbin in range(1,len(self.binning.xbinByCat[cat])):
                    bin_name   = '%s_bin_%s-%s'%(cat_name,xbin,ybin)
                    self.binArgLists[cat].add(self.binVars[bin_name])

            out_rph[cat] = RooParametricHist2D(
                        obj_name, obj_name,
                        self.binning.xVars[cat],
                        self.binning.yVar,
                        self.binArgLists[cat], cat_hist
            )
            out_add[cat] = RooAddition(obj_name+'_norm',obj_name+'_norm',self.binArgLists[cat])
        return out_rph, out_add
Пример #2
0
def getBinByBinErrorBand(mc_comps, observables, binWidths, rfr, weights=None):
    if rfr is None:
        return None
    bins = []
    binning = observables[0].getBinning()
    stepsize = binning.averageBinWidth()
    low = binning.lowBound()
    high = binning.highBound()
    real_weights = RooArgList()
    if weights is not None:
        for bw, w in zip(binWidths, weights):
            real_weights.add(RF.RooConst(bw.getVal() * w))
    else:
        for bw in binWidths:
            real_weights.add(RF.RooConst(bw.getVal()))
    m = low
    while m < high - 1e-6:
        rname = "bin" + str(m)
        intes = RooArgList()
        for obs, mc in zip(observables, mc_comps):
            intes.add(mc.createIntegral(RooArgSet(obs), RF.Range(rname)))
        totbin = RooAddition("sumbin", "sumbin", intes, real_weights)
        val = totbin.getVal()
        err = ROOT.RU.getPropagatedError(totbin, rfr)
        bins.append((val, err))
        logging.debug("Found error of {}".format(err))
        m += stepsize
    yvals = []
    xvals = []
    for i, b in enumerate(bins):
        yvals.extend([b[0] + b[1], b[0] + b[1]])
        xvals.extend([binning.binLow(i), binning.binLow(i)])
    xvals.append(high)
    xvals = xvals[1:]
    xvals.extend(reversed(xvals))
    for b in reversed(bins):
        yvals.extend([b[0] - b[1], b[0] - b[1]])
    # # FIXME JWH
    # for x,y in zip(xvals,yvals):
    #     print "(x,y) = ({},{})".format(x,y)
    yvals_a = array('d', yvals)
    xvals_a = array('d', xvals)
    curve = TGraph(len(xvals), xvals_a, yvals_a)
    return curve
Пример #3
0
def getSumAndError(list_comps, rfr, window=None):
    """ list_comps: list of tuples (obs, bwidth, comp)
    """
    complist = RooArgList()
    widthlist = RooArgList()
    logging.debug("{}".format(list_comps))
    for l in list_comps:
        if window:
            l[0].setRange("myrange", window[0], window[1])
            inte = l[2].createIntegral(RooArgSet(l[0]), RF.Range("myrange"))
        else:
            inte = l[2].createIntegral(RooArgSet(l[0]))
        complist.add(inte)
        widthlist.add(RF.RooConst(l[1]))
    roosum = RooAddition("sum", "sum", complist, widthlist)
    val = roosum.getVal()
    if rfr is not None:
        error = ROOT.RU.getPropagatedError(roosum, rfr)
    else:
        error = -1
    return [val, error]
Пример #4
0
 def __init__(self, workspace, category):
     self.ws = workspace
     self.cat = category
     self.mc = self.ws.obj('ModelConfig')
     self.index_cat = self.mc.GetPdf().index_category
     self.obsData = self.ws.data('obsData')
     self.pdf = self.mc.GetPdf().pdf(self.cat)
     self._obs = self.pdf.getObservables(self.mc.GetObservables()).first()
     self._frame = self._obs.frame()
     self._frame.SetName('{0}'.format(self.cat.name))
     keepalive(self._frame, self._frame.GetName())
     self._components = [
         Component(comp) for comp in self.iter_pdf_components()
     ]
     self._signal = Component(
         RooAddition('sum_sig_{0}'.format(self.cat.name),
                     'sum_sig_{0}'.format(self.cat.name),
                     self.signal_pdf_components()))
     self._background = Component(
         RooAddition('sum_bkg_{0}'.format(self.cat.name),
                     'sum_bkg_{0}'.format(self.cat.name),
                     self.background_pdf_components()))
Пример #5
0
def getPdfInRegionsWithRangeName(w,sample,region,rangeName):
    """
    Should be moved to $HF/src/Utils.h -- FIXME
    """
    if isinstance(sample,list):
        sampleArgList = RooArgList()
        sample_str="group"
        for s in sample:
            componentTmp = Util.GetComponent(w,s,region,True,rangeName)
            sample_str=sample_str+"_"+s
            sampleArgList.add(componentTmp)
            pass
        pdfInRegion = RooAddition(sample_str,sample_str,sampleArgList)
    else:
        pdfInRegion  = Util.GetComponent(w,sample,region,False,rangeName)
        pass
    return pdfInRegion
Пример #6
0
def getPdfInRegions(w,sample,region):
    """
    Return the PDF in a region for a sample
    Should be moved to $HF/src/Utils.h -- FIXME

    @param sample The sample to find
    @param region The region to use
    """
    if isinstance(sample,list):
        sampleArgList = RooArgList()
        sample_str="group"
        for s in sample:
            componentTmp = Util.GetComponent(w,s,region,True)
            sample_str=sample_str+"_"+s
            sampleArgList.add(componentTmp)
            pass
        pdfInRegion = RooAddition(sample_str,sample_str,sampleArgList)
    else:
        pdfInRegion  = Util.GetComponent(w,sample,region)
        pass
    return pdfInRegion
Пример #7
0
def latexfitresults(filename,regionList,sampleList,exactRegionNames=False,dataname='obsData',showSum=False, doAsym=True, blinded=False):
  workspacename = 'w'
  w = Util.GetWorkspaceFromFile(filename,'w')

  if w==None:
    print "ERROR : Cannot open workspace : ", workspacename
    sys.exit(1) 

  resultAfterFit = w.obj('RooExpandedFitResult_afterFit')
  if resultAfterFit==None:
    print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit"
    sys.exit(1)

  resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit')
  if resultBeforeFit==None:
    print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit"
    sys.exit(1)

  data_set = w.data(dataname)
  if data_set==None:
    print "ERROR : Cannot open dataset : ", "data_set"+suffix
    sys.exit(1)
      
  regionCat = w.obj("channelCat")
  data_set.table(regionCat).Print("v");

  regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in regionList]
  print regionFullNameList

  ######

  snapshot =  'snapshot_paramsVals_RooExpandedFitResult_afterFit'
  w.loadSnapshot(snapshot)

  if not w.loadSnapshot(snapshot):
    print "ERROR : Cannot load snapshot : ", snapshot
    sys.exit(1)

  tablenumbers = {}

  # SUM ALL REGIONS
  sumName = ""
  for index, reg in enumerate(regionList):
    if index == 0:
      sumName = reg
    else:
      sumName = sumName + " + " + reg
  
  regionListWithSum = list(regionList)
  if showSum:
    regionListWithSum.append(sumName)

  tablenumbers['names'] = regionListWithSum

  regionCatList = [ 'channelCat==channelCat::' +region.Data() for region in regionFullNameList]
  
  regionDatasetList = [data_set.reduce(regioncat) for regioncat in regionCatList]
  for index, data in enumerate(regionDatasetList):
    data.SetName("data_" + regionList[index])
    data.SetTitle("data_" + regionList[index])
    
  nobs_regionList = [ data.sumEntries() for data in regionDatasetList]
  #SUM
  sumNobs = 0.
  for nobs in nobs_regionList:
    sumNobs += nobs
    ## print " \n XXX nobs = ", nobs, "    sumNobs = ", sumNobs
  if showSum:
    nobs_regionList.append(sumNobs)
  tablenumbers['nobs'] = nobs_regionList
 
  ######
  ######
  ######  FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT
  ######
  ######

  # total pdf, not splitting in components
  pdfinRegionList = [ Util.GetRegionPdf(w, region)  for region in regionList]
  varinRegionList =  [ Util.GetRegionVar(w, region) for region in regionList]
  rrspdfinRegionList = []
  for index,pdf in enumerate(pdfinRegionList):
#    pdf.Print("t")
    prodList = pdf.pdfList()
    foundRRS = 0
    for idx in range(prodList.getSize()):
      #      if "BG" in prodList[idx].GetName():
      #        prodList[idx].Print("t")
      if prodList[idx].InheritsFrom("RooRealSumPdf"):
        rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]));
        rrspdfinRegionList.append(rrspdfInt)
        foundRRS += 1
    if foundRRS >1 or foundRRS==0:
      print " \n\n WARNING: ", pdf.GetName(), " has ", foundRRS, " instances of RooRealSumPdf"
      print pdf.GetName(), " component list:", prodList.Print("v")
    
  nFittedInRegionList =  [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)]
  pdfFittedErrInRegionList = [ Util.GetPropagatedError(pdf, resultAfterFit, doAsym) for pdf in rrspdfinRegionList]

  if showSum:
    pdfInAllRegions = RooArgSet()
    for index, pdf in enumerate(rrspdfinRegionList):
      pdfInAllRegions.add(pdf)
    pdfSumInAllRegions = RooAddition( "pdf_AllRegions_AFTER", "pdf_AllRegions_AFTER", RooArgList(pdfInAllRegions))
    pdfSumInAllRegions.Print()
    nPdfSumVal = pdfSumInAllRegions.getVal()
    nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultAfterFit, doAsym)
    nFittedInRegionList.append(nPdfSumVal)
    pdfFittedErrInRegionList.append(nPdfSumError)
  
  tablenumbers['TOTAL_FITTED_bkg_events']    =  nFittedInRegionList
  tablenumbers['TOTAL_FITTED_bkg_events_err']    =  pdfFittedErrInRegionList
 
  if blinded: 
    nobs_regionList = [ data.sumEntries() for data in regionDatasetList]
    nobs_regionList[-1] = -1
    tablenumbers['nobs'] = nobs_regionList

  # components
  for isam, sample in enumerate(sampleList):
    sampleName=getName(sample)
    nSampleInRegionVal = []
    nSampleInRegionError = []
    sampleInAllRegions = RooArgSet()
    for ireg, region in enumerate(regionList):
      sampleInRegion=getPdfInRegions(w,sample,region)
      #sampleInRegion = Util.GetComponent(w,sample,region,exactRegionNames)
      sampleInRegionVal = 0.
      sampleInRegionError = 0.
      if not sampleInRegion==None:
        sampleInRegion.Print()
        sampleInRegionVal = sampleInRegion.getVal()
        sampleInRegionError = Util.GetPropagatedError(sampleInRegion, resultAfterFit, doAsym) 
        sampleInAllRegions.add(sampleInRegion)
      else:
        print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region =",region, "\n"
      nSampleInRegionVal.append(sampleInRegionVal)
      nSampleInRegionError.append(sampleInRegionError)
    if showSum:
      sampleSumInAllRegions = RooAddition( (sampleName+"_AllRegions_FITTED"), (sampleName+"_AllRegions_FITTED"), RooArgList(sampleInAllRegions))
      sampleSumInAllRegions.Print()
      nSampleSumVal = sampleSumInAllRegions.getVal()
      nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions, resultAfterFit, doAsym)
      nSampleInRegionVal.append(nSampleSumVal)
      nSampleInRegionError.append(nSampleSumError)
    tablenumbers['Fitted_events_'+sampleName]   = nSampleInRegionVal
    tablenumbers['Fitted_err_'+sampleName]   = nSampleInRegionError

  print tablenumbers

  ######
  ######
  ######  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORE__ THE FIT
  ######
  ######

  #  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS BEFORE THE FIT
  w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit')

  # check if any of the initial scaling factors is != 1
  _result = w.obj('RooExpandedFitResult_beforeFit')
  _muFacs = _result.floatParsFinal()

  for i in range(len(_muFacs)):
    if "mu_" in _muFacs[i].GetName() and _muFacs[i].getVal() != 1.0:
      print  " \n WARNING: scaling factor %s != 1.0 (%g) expected MC yield WILL BE WRONG!" % (_muFacs[i].GetName(), _muFacs[i].getVal())
  
  pdfinRegionList = [ Util.GetRegionPdf(w, region)  for region in regionList]
  varinRegionList =  [ Util.GetRegionVar(w, region) for region in regionList]
  rrspdfinRegionList = []
  for index,pdf in enumerate(pdfinRegionList):
    prodList = pdf.pdfList()
    foundRRS = 0
    for idx in range(prodList.getSize()):
      if prodList[idx].InheritsFrom("RooRealSumPdf"):
        prodList[idx].Print()
        rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]))
        rrspdfinRegionList.append(rrspdfInt)
        foundRRS += 1
    if foundRRS >1 or foundRRS==0:
      print " \n\n WARNING: ", pdf.GetName(), " has ", foundRRS, " instances of RooRealSumPdf"
      print pdf.GetName(), " component list:", prodList.Print("v")

  nExpInRegionList =  [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)]
  pdfExpErrInRegionList = [ Util.GetPropagatedError(pdf, resultBeforeFit, doAsym)  for pdf in rrspdfinRegionList]
  
  if showSum:
    pdfInAllRegions = RooArgSet()
    for index, pdf in enumerate(rrspdfinRegionList):
      pdfInAllRegions.add(pdf)
    pdfSumInAllRegions = RooAddition( "pdf_AllRegions_BEFORE", "pdf_AllRegions_BEFORE", RooArgList(pdfInAllRegions))
    nPdfSumVal = pdfSumInAllRegions.getVal()
    nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultBeforeFit, doAsym)
    nExpInRegionList.append(nPdfSumVal)
    pdfExpErrInRegionList.append(nPdfSumError)
  
  tablenumbers['TOTAL_MC_EXP_BKG_events']    =  nExpInRegionList
  tablenumbers['TOTAL_MC_EXP_BKG_err']    =  pdfExpErrInRegionList
  
  for isam, sample in enumerate(sampleList):
    sampleName=getName(sample)
    nMCSampleInRegionVal = []
    nMCSampleInRegionError = []
    MCSampleInAllRegions = RooArgSet()
    for ireg, region in enumerate(regionList):
      MCSampleInRegion = getPdfInRegions(w,sample,region)
      #MCSampleInRegion = Util.GetComponent(w,sample,region,exactRegionNames)
      MCSampleInRegionVal = 0.
      MCSampleInRegionError = 0.
      if not MCSampleInRegion==None:
        MCSampleInRegionVal = MCSampleInRegion.getVal()
        MCSampleInRegionError = Util.GetPropagatedError(MCSampleInRegion, resultBeforeFit, doAsym) 
        MCSampleInAllRegions.add(MCSampleInRegion)
      else:
        print " \n WARNING: sample=", sampleName, " non-existent (empty) in region=",region
      nMCSampleInRegionVal.append(MCSampleInRegionVal)
      nMCSampleInRegionError.append(MCSampleInRegionError)
    if showSum:
      MCSampleSumInAllRegions = RooAddition( (sampleName+"_AllRegions_MC"), (sampleName+"_AllRegions_MC"), RooArgList(MCSampleInAllRegions))
      nMCSampleSumVal = MCSampleSumInAllRegions.getVal()
      nMCSampleSumError = Util.GetPropagatedError(MCSampleSumInAllRegions, resultBeforeFit, doAsym)
      nMCSampleInRegionVal.append(nMCSampleSumVal)
      nMCSampleInRegionError.append(nMCSampleSumError)
    tablenumbers['MC_exp_events_'+sampleName] = nMCSampleInRegionVal
    tablenumbers['MC_exp_err_'+sampleName] = nMCSampleInRegionError

    #  sorted(tablenumbers, key=lambda sample: sample[1])   # sort by age
  map_listofkeys = tablenumbers.keys()
  map_listofkeys.sort()
  
  for name in map_listofkeys:
    if tablenumbers.has_key(name) :
      print name, ": ", tablenumbers[name]
      
  ###
  return tablenumbers
Пример #8
0
def latexfitresults(filename,
                    regionList,
                    sampleList,
                    dataname='obsData',
                    showSum=False,
                    doAsym=True,
                    blinded=False,
                    splitBins=False):
    """
  Calculate before/after-fit yields in all channels given
  
  @param filename The filename containing afterFit workspace
  @param regionList A list of regions to be considered
  @param sampleList A list of samples to be considered
  @param dataname The name of dataset (default='obsData')
  @param showSum Calculates sum of all regions if set to true (default=False)
  @param doAsym Calculates asymmetric errors taken from MINOS (default=True)
  @param blinded Observed event count will not be shown if set to True (default=False)
  @param splitBins Calculates bin-by-bin yields for all regions if set to True (default=False)
  """
    """
  pick up workspace from file
  """
    workspacename = 'w'
    w = Util.GetWorkspaceFromFile(filename, 'w')
    if w == None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)
    """
  pick up after-fit RooExpandedFitResult from workspace
  """
    resultAfterFit = w.obj('RooExpandedFitResult_afterFit')
    if resultAfterFit == None:
        print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit"
        sys.exit(1)
    """
  pick up before-fit RooExpandedFitResult from workspace
  """
    resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit')
    if resultBeforeFit == None:
        print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit"
        sys.exit(1)
    """
  pick up dataset from workspace
  """
    data_set = w.data(dataname)
    if data_set == None:
        print "ERROR : Cannot open dataset : ", "data_set" + suffix
        sys.exit(1)
    """
  pick up channel category (RooCategory) from workspace
  """
    regionCat = w.obj("channelCat")
    if not blinded:
        data_set.table(regionCat).Print("v")
    """
  find full (long) name list of regions (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50)
  """
    regionFullNameList = [
        Util.GetFullRegionName(regionCat, region) for region in regionList
    ]
    """
  load afterFit workspace snapshot (=set all parameters to values after fit)
  """
    snapshot = 'snapshot_paramsVals_RooExpandedFitResult_afterFit'
    w.loadSnapshot(snapshot)

    if not w.loadSnapshot(snapshot):
        print "ERROR : Cannot load snapshot : ", snapshot
        sys.exit(1)
    """
  define set, for all names/yields to be saved in
  """
    tablenumbers = {}
    """
  if showSum=True define names for sum of all regions and add to regionList
  """
    sumName = ""
    for index, reg in enumerate(regionList):
        if index == 0:
            sumName = reg
        else:
            sumName = sumName + " + " + reg

    regionListWithSum = list(regionList)
    if showSum:
        regionListWithSum.append(sumName)

    tablenumbers['names'] = regionListWithSum
    """
  make a list of channelCat calls for every region
  """
    regionCatList = [
        'channelCat==channelCat::' + region.Data()
        for region in regionFullNameList
    ]
    """
  retrieve number of observed (=data) events per region
  """
    regionDatasetList = [
        data_set.reduce(regioncat) for regioncat in regionCatList
    ]
    for index, data in enumerate(regionDatasetList):
        data.SetName("data_" + regionList[index])
        data.SetTitle("data_" + regionList[index])

    nobs_regionList = [data.sumEntries() for data in regionDatasetList]
    """
  if showSum=True calculate the total number of observed events in all regions  
  """
    sumNobs = 0.
    for nobs in nobs_regionList:
        sumNobs += nobs
    if showSum:
        nobs_regionList.append(sumNobs)
    tablenumbers['nobs'] = nobs_regionList
    """
  FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT
  """
    """
  get a list of pdf's and variables per region
  """
    pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList]
    varinRegionList = [Util.GetRegionVar(w, region) for region in regionList]
    """
  if splitBins=True get the list of Nbins, binMax and binMin; make a list of new region names for each bin
  """
    varNbinsInRegionList = []
    varBinLowInRegionList = []
    varBinHighInRegionList = []
    rangeNameBinsInRegionList = []
    if splitBins:
        varNbinsInRegionList = [
            Util.GetRegionVar(w, region).getBinning().numBins()
            for region in regionList
        ]
        varBinLowInRegionList = [[
            Util.GetRegionVar(w, region).getBinning(
                (region + "binning")).binLow(ibin)
            for ibin in range(0, varNbinsInRegionList[idx])
        ] for idx, region in enumerate(regionList)]
        varBinHighInRegionList = [[
            Util.GetRegionVar(w, region).getBinning(
                (region + "binning")).binHigh(ibin)
            for ibin in range(0, varNbinsInRegionList[idx])
        ] for idx, region in enumerate(regionList)]
        rangeNameBinsInRegionList = [[
            regionList[idx] + "_bin" + str(ibin)
            for ibin in range(0, varNbinsInRegionList[idx])
        ] for idx, region in enumerate(regionList)]
        for index, region in enumerate(regionList):
            if varNbinsInRegionList[index] == 1:
                print " \n YieldsTable.py: WARNING: you have called -P (= per-bin yields) but this region ", region, " has only 1 bin \n"
    """
  if splitBins=True reshuffle the regionName list; each region name is followed by names of each bin (i.e. regionNameList=['SR3J','SR3J_bin1','SR3j_bin2','SR4J','SR4J_bin1'])
  """
    regionListWithBins = []
    if splitBins:
        for index, region in enumerate(regionList):
            regionListWithBins.append(region)
            for ibin in range(0, varNbinsInRegionList[index]):
                regionListWithBins.append(
                    rangeNameBinsInRegionList[index][ibin])
        tablenumbers['names'] = regionListWithBins
    """
  calculate number of observed(=data) events per bin
  """
    nobs_regionListWithBins = []
    if splitBins:
        binFuncInRegionList = [
            RooBinningCategory("bin_" + region, "bin_" + region,
                               varinRegionList[index])
            for index, region in enumerate(regionList)
        ]
        for index, data in enumerate(regionDatasetList):
            data.addColumn(binFuncInRegionList[index])
            if not blinded:
                data.table(binFuncInRegionList[index]).Print("v")
            nobs_regionListWithBins.append(data.sumEntries())
            for ibin in range(0, varNbinsInRegionList[index]):
                nobs_regionListWithBins.append(
                    (data.reduce(binFuncInRegionList[index].GetName() + "==" +
                                 binFuncInRegionList[index].GetName() + "::" +
                                 varinRegionList[index].GetName() + "_bin" +
                                 str(ibin))).sumEntries())

        tablenumbers['nobs'] = nobs_regionListWithBins
    """
  if blinded=True, set all numbers of observed events to -1
  """
    if blinded:
        for index, nobs in enumerate(nobs_regionListWithBins):
            nobs_regionListWithBins[index] = -1
        tablenumbers['nobs'] = nobs_regionListWithBins
    """
  get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples)
  """
    rrspdfinRegionList = []
    for index, pdf in enumerate(pdfinRegionList):
        prodList = pdf.pdfList()
        foundRRS = 0
        for idx in range(prodList.getSize()):
            if prodList[idx].InheritsFrom("RooRealSumPdf"):
                rrspdfInt = prodList[idx].createIntegral(
                    RooArgSet(varinRegionList[index]))
                rrspdfinRegionList.append(rrspdfInt)
                if splitBins:
                    origMin = varinRegionList[index].getMin()
                    origMax = varinRegionList[index].getMax()
                    for ibin in range(0, varNbinsInRegionList[index]):
                        rangeName = rangeNameBinsInRegionList[index][ibin]
                        varinRegionList[index].setRange(
                            rangeName, varBinLowInRegionList[index][ibin],
                            varBinHighInRegionList[index][ibin])
                        rrspdfInt = prodList[idx].createIntegral(
                            RooArgSet(varinRegionList[index]), rangeName)
                        rrspdfinRegionList.append(rrspdfInt)
                    varinRegionList[index].setRange(origMin, origMax)
                foundRRS += 1
        if foundRRS > 1 or foundRRS == 0:
            print " \n\n WARNING: ", pdf.GetName(
            ), " has ", foundRRS, " instances of RooRealSumPdf"
            print pdf.GetName(), " component list:", prodList.Print("v")
    """
  calculate total pdf number of fitted events and error
  """
    nFittedInRegionList = [
        pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)
    ]
    pdfFittedErrInRegionList = [
        Util.GetPropagatedError(pdf, resultAfterFit, doAsym)
        for pdf in rrspdfinRegionList
    ]
    """
  if showSum=True calculate the total number of fitted events in all regions  
  """
    if showSum:
        pdfInAllRegions = RooArgSet()
        for index, pdf in enumerate(rrspdfinRegionList):
            pdfInAllRegions.add(pdf)
        pdfSumInAllRegions = RooAddition("pdf_AllRegions_AFTER",
                                         "pdf_AllRegions_AFTER",
                                         RooArgList(pdfInAllRegions))
        nPdfSumVal = pdfSumInAllRegions.getVal()
        nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions,
                                               resultAfterFit, doAsym)
        nFittedInRegionList.append(nPdfSumVal)
        pdfFittedErrInRegionList.append(nPdfSumError)

    tablenumbers['TOTAL_FITTED_bkg_events'] = nFittedInRegionList
    tablenumbers['TOTAL_FITTED_bkg_events_err'] = pdfFittedErrInRegionList
    """
  calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf
  """
    for isam, sample in enumerate(sampleList):
        sampleName = getName(sample)
        nSampleInRegionVal = []
        nSampleInRegionError = []
        sampleInAllRegions = RooArgSet()
        for ireg, region in enumerate(regionList):
            sampleInRegion = getPdfInRegions(w, sample, region)
            sampleInRegionVal = 0.
            sampleInRegionError = 0.
            if not sampleInRegion == None:
                sampleInRegionVal = sampleInRegion.getVal()
                sampleInRegionError = Util.GetPropagatedError(
                    sampleInRegion, resultAfterFit, doAsym)
                sampleInAllRegions.add(sampleInRegion)
            else:
                print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region =", region, "\n"
            nSampleInRegionVal.append(sampleInRegionVal)
            nSampleInRegionError.append(sampleInRegionError)
            """
      if splitBins=True calculate numbers of fitted events plus error per bin      
      """
            if splitBins:
                origMin = varinRegionList[ireg].getMin()
                origMax = varinRegionList[ireg].getMax()
                for ibin in range(0, varNbinsInRegionList[ireg]):
                    rangeName = rangeNameBinsInRegionList[ireg][ibin]
                    sampleInRegion = getPdfInRegionsWithRangeName(
                        w, sample, region, rangeName)
                    sampleInRegionVal = 0.
                    sampleInRegionError = 0.
                    if not sampleInRegion == None:
                        varinRegionList[ireg].setRange(
                            rangeName, varBinLowInRegionList[ireg][ibin],
                            varBinHighInRegionList[ireg][ibin])
                        sampleInRegionVal = sampleInRegion.getVal()
                        sampleInRegionError = Util.GetPropagatedError(
                            sampleInRegion, resultAfterFit, doAsym)
                    else:
                        print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=", region, " bin=", ibin, " \n"
                    nSampleInRegionVal.append(sampleInRegionVal)
                    nSampleInRegionError.append(sampleInRegionError)

                varinRegionList[ireg].setRange(origMin, origMax)
        """
    if showSum=True calculate the total number of fitted events in all regions  
    """
        if showSum:
            sampleSumInAllRegions = RooAddition(
                (sampleName + "_AllRegions_FITTED"),
                (sampleName + "_AllRegions_FITTED"),
                RooArgList(sampleInAllRegions))
            nSampleSumVal = sampleSumInAllRegions.getVal()
            nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions,
                                                      resultAfterFit, doAsym)
            nSampleInRegionVal.append(nSampleSumVal)
            nSampleInRegionError.append(nSampleSumError)
        tablenumbers['Fitted_events_' + sampleName] = nSampleInRegionVal
        tablenumbers['Fitted_err_' + sampleName] = nSampleInRegionError

    print "\n starting BEFORE-FIT calculations \n"
    """
  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORRE__ THE FIT
  """
    """
  load beforeFit workspace snapshot (=set all parameters to values before fit)
  """
    w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit')
    """
  check if any of the initial scaling factors is != 1
  """
    _result = w.obj('RooExpandedFitResult_beforeFit')
    _muFacs = _result.floatParsFinal()

    for i in range(len(_muFacs)):
        if "mu_" in _muFacs[i].GetName() and _muFacs[i].getVal() != 1.0:
            print " \n WARNING: scaling factor %s != 1.0 (%g) expected MC yield WILL BE WRONG!" % (
                _muFacs[i].GetName(), _muFacs[i].getVal())
    """
  get a list of pdf's and variables per region
  """
    pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList]
    varinRegionList = [Util.GetRegionVar(w, region) for region in regionList]
    """
  get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples)
  """
    rrspdfinRegionList = []
    for index, pdf in enumerate(pdfinRegionList):
        prodList = pdf.pdfList()
        foundRRS = 0
        for idx in range(prodList.getSize()):
            if prodList[idx].InheritsFrom("RooRealSumPdf"):
                rrspdfInt = prodList[idx].createIntegral(
                    RooArgSet(varinRegionList[index]))
                rrspdfinRegionList.append(rrspdfInt)
                if splitBins:
                    origMin = varinRegionList[index].getMin()
                    origMax = varinRegionList[index].getMax()
                    for ibin in range(0, varNbinsInRegionList[index]):
                        rangeName = rangeNameBinsInRegionList[index][ibin]
                        varinRegionList[index].setRange(
                            rangeName, varBinLowInRegionList[index][ibin],
                            varBinHighInRegionList[index][ibin])
                        rrspdfInt = prodList[idx].createIntegral(
                            RooArgSet(varinRegionList[index]), rangeName)
                        rrspdfinRegionList.append(rrspdfInt)
                    varinRegionList[index].setRange(origMin, origMax)
                foundRRS += 1
        if foundRRS > 1 or foundRRS == 0:
            print " \n\n WARNING: ", pdf.GetName(
            ), " has ", foundRRS, " instances of RooRealSumPdf"
            print pdf.GetName(), " component list:", prodList.Print("v")
    """
  calculate total pdf number of expected events and error
  """
    nExpInRegionList = [
        pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)
    ]
    pdfExpErrInRegionList = [
        Util.GetPropagatedError(pdf, resultBeforeFit, doAsym)
        for pdf in rrspdfinRegionList
    ]
    """
  if showSum=True calculate the total number of expected events in all regions  
  """
    if showSum:
        pdfInAllRegions = RooArgSet()
        for index, pdf in enumerate(rrspdfinRegionList):
            pdfInAllRegions.add(pdf)
        pdfSumInAllRegions = RooAddition("pdf_AllRegions_BEFORE",
                                         "pdf_AllRegions_BEFORE",
                                         RooArgList(pdfInAllRegions))
        nPdfSumVal = pdfSumInAllRegions.getVal()
        nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions,
                                               resultBeforeFit, doAsym)
        nExpInRegionList.append(nPdfSumVal)
        pdfExpErrInRegionList.append(nPdfSumError)

    tablenumbers['TOTAL_MC_EXP_BKG_events'] = nExpInRegionList
    tablenumbers['TOTAL_MC_EXP_BKG_err'] = pdfExpErrInRegionList
    """
  calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf
  """
    for isam, sample in enumerate(sampleList):
        sampleName = getName(sample)
        nMCSampleInRegionVal = []
        nMCSampleInRegionError = []
        MCSampleInAllRegions = RooArgSet()
        for ireg, region in enumerate(regionList):
            MCSampleInRegion = getPdfInRegions(w, sample, region)
            MCSampleInRegionVal = 0.
            MCSampleInRegionError = 0.
            if not MCSampleInRegion == None:
                MCSampleInRegionVal = MCSampleInRegion.getVal()
                MCSampleInRegionError = Util.GetPropagatedError(
                    MCSampleInRegion, resultBeforeFit, doAsym)
                MCSampleInAllRegions.add(MCSampleInRegion)
            else:
                print " \n WARNING: sample=", sampleName, " non-existent (empty) in region=", region
            nMCSampleInRegionVal.append(MCSampleInRegionVal)
            nMCSampleInRegionError.append(MCSampleInRegionError)
            """
      if splitBins=True calculate numbers of fitted events plus error per bin      
      """
            if splitBins:
                origMin = varinRegionList[ireg].getMin()
                origMax = varinRegionList[ireg].getMax()
                for ibin in range(0, varNbinsInRegionList[ireg]):
                    rangeName = rangeNameBinsInRegionList[ireg][ibin]
                    MCSampleInRegion = getPdfInRegionsWithRangeName(
                        w, sample, region, rangeName)
                    MCSampleInRegionVal = 0.
                    MCSampleInRegionError = 0.
                    if not MCSampleInRegion == None:
                        varinRegionList[ireg].setRange(
                            rangeName, varBinLowInRegionList[ireg][ibin],
                            varBinHighInRegionList[ireg][ibin])
                        MCSampleInRegionVal = MCSampleInRegion.getVal()
                        MCSampleInRegionError = Util.GetPropagatedError(
                            MCSampleInRegion, resultBeforeFit, doAsym)
                    else:
                        print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=", region, " bin=", ibin, " \n"
                    nMCSampleInRegionVal.append(MCSampleInRegionVal)
                    nMCSampleInRegionError.append(MCSampleInRegionError)

                varinRegionList[ireg].setRange(origMin, origMax)
        """
    if showSum=True calculate the total number of fitted events in all regions  
    """
        if showSum:
            MCSampleSumInAllRegions = RooAddition(
                (sampleName + "_AllRegions_MC"),
                (sampleName + "_AllRegions_MC"),
                RooArgList(MCSampleInAllRegions))
            nMCSampleSumVal = MCSampleSumInAllRegions.getVal()
            nMCSampleSumError = Util.GetPropagatedError(
                MCSampleSumInAllRegions, resultBeforeFit, doAsym)
            nMCSampleInRegionVal.append(nMCSampleSumVal)
            nMCSampleInRegionError.append(nMCSampleSumError)
        tablenumbers['MC_exp_events_' + sampleName] = nMCSampleInRegionVal
        tablenumbers['MC_exp_err_' + sampleName] = nMCSampleInRegionError
    """
  sort the tablenumbers set
  """
    map_listofkeys = tablenumbers.keys()
    map_listofkeys.sort()
    """
  print the sorted tablenumbers set
  """
    for name in map_listofkeys:
        if tablenumbers.has_key(name):
            print name, ": ", tablenumbers[name]

    return tablenumbers
Пример #9
0
def getFrame(cat,
             obsData,
             simPdf,
             mc,
             fit_res,
             error_band_strategy=1,
             compute_yields=False):
    """
    Build a frame with the different fit components and their uncertainties

    Parameters
    ----------
    cat    : RooCategory
        Category of the simultaneous PDF we are interested in
    obsData: RooDataHist object (either real data or asimov or PE)
    simPdf : RooSimultaneous PDF
    mc : ModelConfig object
    fit_res: RooFitResult
        Result of the fit (covariance matrix, NLL value, ...)
    error_band_strategy : True/False
        True: Use the linear approximation to extract the error band
        False: Use a sampling method
        See http://root.cern.ch/root/html/RooAbsReal.html#RooAbsReal:plotOnWithErrorBand
    verbose: True/False

    TODO: implement a more generic way to retrieve the binning.
    Currently it has to be put in the workspace with the name binWidth_obs_x_{channel}_0
    """

    hlist = []
    if compute_yields:
        yields = {}
    # --> Get the total (signal+bkg) model pdf
    pdftmp = simPdf.getPdf(cat.GetName())
    if not pdftmp:
        raise RuntimeError('Could not retrieve the total pdf ')
    # --> Get the list of observables
    obstmp = pdftmp.getObservables(mc.GetObservables())
    if not obstmp:
        raise RuntimeError('Could not retrieve the list of observable')
    # --> Get the first (and only) observable (mmc mass for cut based)
    obs = obstmp.first()
    log.info('Observable: {0}'.format(obs.GetName()))
    # --> Get the RooDataHist of the given category
    datatmp = obsData.reduce("{0}=={1}::{2}".format(
        simPdf.indexCat().GetName(),
        simPdf.indexCat().GetName(), cat.GetName()))
    datatmp.__class__ = ROOT.RooAbsData  # --> Ugly fix !!!
    # --> Get the binning width of the category (stored as a workspace variable)
    binWidth = pdftmp.getVariables().find('binWidth_obs_x_{0}_0'.format(
        cat.GetName()))
    if not binWidth:
        raise RuntimeError('Could not retrieve the binWidth')
    # --> parameter of interest (mu=sigma/sigma_sm)
    poi = mc.GetParametersOfInterest().first()
    poi_fitted_val = poi.getVal()
    log.info('POI: {0} = {1} +/- {2}'.format(poi.GetName(), poi.getVal(),
                                             poi.getError()))

    # --> Create the data histogram
    hist_data = asrootpy(datatmp.createHistogram("hdata_" + cat.GetName(),
                                                 obs))
    hist_data.name = "hdata_{0}".format(cat.GetName())
    hist_data.title = ""
    hlist.append(hist_data)
    #--> compute the data yields
    if compute_yields:
        Yield_data = hist_data.Integral()
        yields['Data'] = (Yield_data, 0)

    # --> Create the frame structure from the observable
    frame = obs.frame()
    frame.SetName(cat.GetName())
    datatmp.plotOn(frame, ROOT.RooFit.DataError(ROOT.RooAbsData.Poisson),
                   ROOT.RooFit.Name("Data"), ROOT.RooFit.MarkerSize(1))

    # --> Create the signal histogram template
    hist_sig = hist_data.Clone('h_TotalSignal_{0}'.format(cat.GetName()))
    hist_sig.Reset()
    signal_comps = RooArgList()
    bkg_comps = RooArgList()
    # --> get the list of components (hadhad HSG4: QCD,Other,Ztautau, Signal_Z, Signal_W, Signal_gg, Signal_VBF)
    # --> and iterate over
    pdfmodel = pdftmp.getComponents().find(cat.GetName() + '_model')
    funcListIter = pdfmodel.funcList().iterator()
    while True:
        comp = funcListIter.Next()
        if not comp:
            break

        name = comp.GetName().replace('L_x_', '').split('_')[0]
        log.info('Scan component {0}'.format(comp.GetName()))
        hist_comp = asrootpy(
            comp.createHistogram(cat.GetName() + "_" + comp.GetName(), obs,
                                 ROOT.RooFit.Extended(False)))
        hist_comp.name = 'h_{0}_{1}'.format(name, cat.GetName())
        hist_comp.title = ''
        hlist.append(hist_comp)

        if 'Signal' in comp.GetName():
            signal_comps.add(comp)
            hist_sig.Add(hist_comp)
        else:
            bkg_comps.add(comp)
        Integral_comp = comp.createIntegral(RooArgSet(obs))
        Yield_comp = Integral_comp.getVal() * binWidth.getVal()
        log.info('Integral = {0}'.format(Yield_comp))
        #         if Yield_comp==0:
        #             raise RuntimeError('Yield integral is wrong !!')

        # --> Add the components to the frame but in an invisible way
        pdfmodel.plotOn(
            frame, ROOT.RooFit.Components(comp.GetName()),
            ROOT.RooFit.Normalization(Yield_comp, ROOT.RooAbsReal.NumEvent),
            ROOT.RooFit.Name("NoStacked_" + comp.GetName()),
            ROOT.RooFit.Invisible())
        if fit_res:
            # --> Add the components uncertainty band
            comp.plotOn(
                frame,
                ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
                ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
                ROOT.RooFit.Name("FitError_AfterFit_" + comp.GetName()),
                ROOT.RooFit.Invisible())
            if compute_yields:
                if Yield_comp == 0:
                    Yield_comp_err = 0.
                else:
                    Yield_comp_err = Integral_comp.getPropagatedError(
                        fit_res) * binWidth.getVal()
                yields[comp.GetName()] = (Yield_comp, Yield_comp_err)

    hlist.append(hist_sig)
    # --> total signal yields
    if compute_yields:
        signal_tot_comp = RooAddition('pdf_sum_sig', 'pdf_sum_sig',
                                      signal_comps)
        Integral_signal = signal_tot_comp.createIntegral(RooArgSet(obs))
        yields_sig_tot = Integral_signal.getVal() * binWidth.getVal()
        yields_sig_tot_err = Integral_signal.getPropagatedError(
            fit_res) * binWidth.getVal()
        yields['TotalSignal'] = (yields_sig_tot, yields_sig_tot_err)
        bkg_tot_comp = RooAddition('pdf_sum_bkg', 'pdf_sum_bkg', bkg_comps)
        Integral_bkg = bkg_tot_comp.createIntegral(RooArgSet(obs))
        yields_bkg_tot = Integral_bkg.getVal() * binWidth.getVal()
        yields_bkg_tot_err = Integral_bkg.getPropagatedError(
            fit_res) * binWidth.getVal()
        yields['bkg'] = (yields_bkg_tot, yields_bkg_tot_err)
        log.info('Bkg Total Yield: {0}'.format(yields_bkg_tot))

    # --> bkg+signal PDF central value and error
    Integral_total = pdfmodel.createIntegral(RooArgSet(obs))
    Yield_total = Integral_total.getVal() * binWidth.getVal()
    log.info('Postfit Total Yield: {0}'.format(Yield_total))
    hist_bkg_plus_sig = asrootpy(
        pdfmodel.createHistogram("hbkg_plus_sig_" + cat.GetName(), obs,
                                 ROOT.RooFit.Extended(False)))
    hist_bkg_plus_sig.name = 'hbkg_plus_sig_' + cat.GetName()
    hist_bkg_plus_sig.title = ''
    hist_bkg_plus_sig.Scale(Yield_total)
    hlist.append(hist_bkg_plus_sig)

    # --> Add the components to the frame but in an invisible way
    pdftmp.plotOn(
        frame, ROOT.RooFit.Normalization(Yield_total,
                                         ROOT.RooAbsReal.NumEvent),
        ROOT.RooFit.Name("Bkg_plus_sig"))

    if fit_res:
        pdftmp.plotOn(
            frame, ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
            ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
            ROOT.RooFit.Name("FitError_AfterFit"),
            ROOT.RooFit.FillColor(ROOT.kOrange), ROOT.RooFit.LineWidth(2),
            ROOT.RooFit.LineColor(ROOT.kBlue))

    # --> bkg only PDF central value and error
    poi.setVal(0.)
    Integral_bkg_total = pdfmodel.createIntegral(RooArgSet(obs))
    Yield_bkg_total = Integral_bkg_total.getVal() * binWidth.getVal()

    hist_bkg = pdfmodel.createHistogram("hbkg_" + cat.GetName(), obs,
                                        ROOT.RooFit.Extended(False))
    hist_bkg.Scale(Yield_bkg_total)
    hist_bkg.SetName("hbkg_" + cat.GetName())
    hist_bkg.SetTitle("")
    hlist.append(hist_bkg)
    pdftmp.plotOn(
        frame,
        ROOT.RooFit.Normalization(Yield_bkg_total, ROOT.RooAbsReal.NumEvent),
        ROOT.RooFit.Name("Bkg"), ROOT.RooFit.LineStyle(ROOT.kDashed))
    if fit_res:
        pdftmp.plotOn(
            frame, ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
            ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
            ROOT.RooFit.Name("FitError_AfterFit_Mu0"),
            ROOT.RooFit.FillColor(ROOT.kOrange), ROOT.RooFit.LineWidth(2),
            ROOT.RooFit.LineColor(ROOT.kBlue))
        #if compute_yields:
        # Yield_bkg_total_err = Integral_bkg_total.getPropagatedError(fit_res)*binWidth.getVal()
        # yields['bkg'] = (Yield_bkg_total, Yield_bkg_total_err)

    poi.setVal(poi_fitted_val)
    if compute_yields:
        return frame, hlist, yields
    else:
        return frame, hlist
Пример #10
0
def doFit(ws, options):
    rap_bins = range(1, len(jpsi.pTRange))
    pt_bins = None

    if options.testBin is not None:
        rap_bins = [int(options.testBin.split(',')[0])]
        pt_bins = [int(options.testBin.split(',')[1]) - 1]

    for rap_bin in rap_bins:
        if options.testBin is None:
            pt_bins = range(len(jpsi.pTRange[rap_bin]))
        for pt_bin in pt_bins:

            sigMaxMass = jpsi.polMassJpsi[
                rap_bin] + jpsi.nSigMass * jpsi.sigmaMassJpsi[rap_bin]
            sigMinMass = jpsi.polMassJpsi[
                rap_bin] - jpsi.nSigMass * jpsi.sigmaMassJpsi[rap_bin]

            sbHighMass = jpsi.polMassJpsi[
                rap_bin] + jpsi.nSigBkgHigh * jpsi.sigmaMassJpsi[rap_bin]
            sbLowMass = jpsi.polMassJpsi[
                rap_bin] - jpsi.nSigBkgLow * jpsi.sigmaMassJpsi[rap_bin]

            jPsiMass = ws.var('JpsiMass')
            jPsicTau = ws.var('Jpsict')

            jPsiMass.setRange('mlfit_prompt', 2.7, 3.5)
            jPsiMass.setRange('mlfit_nonPrompt', 2.7, 3.5)

            jPsiMass.setRange('NormalizationRangeFormlfit_prompt', 2.7, 3.5)
            jPsiMass.setRange('NormalizationRangeFormlfit_nonPrompt', 2.7, 3.5)

            jPsicTau.setRange('mlfit_signal', -1, 2.5)
            jPsicTau.setRange('mlfit_leftMassSideBand', -1, 2.5)
            jPsicTau.setRange('mlfit_rightMassSideBand', -1, 2.5)

            jPsicTau.setRange('NormalizationRangeFormlfit_signal', -1, 2.5)
            jPsicTau.setRange('NormalizationRangeFormlfit_leftMassSideBand',
                              -1, 2.5)
            jPsicTau.setRange('NormalizationRangeFormlfit_rightMassSideBand',
                              -1, 2.5)

            #jPsicTau.setRange('NormalizationRangeFormlfit_promptSignal',-1,.1)
            #jPsicTau.setRange('NormalizationRangeFormlfit_nonPromptSignal',.1,2.5)
            #jPsicTau.setRange('NormalizationRangeFormlfit_leftMassSideBand',-1,2.5)
            #jPsicTau.setRange('NormalizationRangeFormlfit_rightMassSideBand',-1,2.5)

            #jPsicTau.setRange('mlfit_promptSignal',-1,.1)
            #jPsicTau.setRange('mlfit_nonPromptSignal',.1,2.5)
            #jPsicTau.setRange('mlfit_leftMassSideBand',-1,2.5)
            #jPsicTau.setRange('mlfit_rightMassSideBand',-1,2.5)

            #reset parameters
            ws.var('CBn').setVal(.5)
            ws.var('CBalpha').setVal(.5)
            ws.var('CBmass').setVal(3.1)
            ws.var('CBsigma').setVal(.02)
            ws.var('bkgLambda').setVal(0)

            ws.var('bkgTauSSDL').setVal(.5)
            #ws.var('bkgTauFDL').setVal(.5)
            ws.var('bkgTauDSDL').setVal(.5)
            ws.var('fBkgSSDL').setVal(.5)
            ws.var('fBkgLR').setVal(.5)

            ws.var('bkgTauSSDR').setVal(.5)
            #ws.var('bkgTauFDR').setVal(.5)
            ws.var('bkgTauDSDR').setVal(.5)
            ws.var('fBkgSSDR').setVal(.5)
            #ws.var('fBkgFDR').setVal(.25)

            #ws.var('nPrompt').setVal(5000)
            #ws.var('nNonPrompt').setVal(500)
            #ws.var('nBackground').setVal(100)
            #ws.var('nBackgroundL').setVal(50)
            #ws.var('nBackgroundR').setVal(50)

            ws.var('nonPromptTau').setVal(.5)
            ws.var('promptMean').setVal(0)
            ws.var('ctResolution').setVal(1)

            LPdf = ws.pdf('LPdf')
            MPdf = ws.pdf('MPdf')

            data = ws.data('data_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1))

            NLLs = RooArgSet()

            MassNLL = MPdf.createNLL(
                data, ROOT.RooFit.Range('mlfit'), ROOT.RooFit.SplitRange(True),
                ROOT.RooFit.ConditionalObservables(
                    RooArgSet(ws.var('JpsictErr'))), ROOT.RooFit.NumCPU(2))

            CTauNLL = LPdf.createNLL(
                data, ROOT.RooFit.Range('mlfit'), ROOT.RooFit.SplitRange(True),
                ROOT.RooFit.ConditionalObservables(
                    RooArgSet(ws.var('JpsictErr'))), ROOT.RooFit.NumCPU(2))

            NLLs.add(MassNLL)
            NLLs.add(CTauNLL)

            simNLL = RooAddition('add', 'add', NLLs)

            minuit = RooMinuit(simNLL)
            minuit.setStrategy(2)
            minuit.setPrintEvalErrors(-1)

            minuit.simplex()
            minuit.migrad()
            minuit.migrad()
            minuit.hesse()

            fitresult = minuit.save('polfitresult_rap' + str(rap_bin) + '_pt' +
                                    str(pt_bin + 1))
            getattr(ws, 'import')(fitresult)

            ws.saveSnapshot(
                'snapshot_rap' + str(rap_bin) + '_pt' + str(pt_bin + 1),
                ws.allVars())
def latexfitresults(filename,regionList,sampleList,dataname='obsData',showSum=False, doAsym=True, blinded=False, splitBins=False):
  """
  Calculate before/after-fit yields in all channels given

  @param filename The filename containing afterFit workspace
  @param regionList A list of regions to be considered
  @param sampleList A list of samples to be considered
  @param dataname The name of dataset (default='obsData')
  @param showSum Calculates sum of all regions if set to true (default=False)
  @param doAsym Calculates asymmetric errors taken from MINOS (default=True)
  @param blinded Observed event count will not be shown if set to True (default=False)
  @param splitBins Calculates bin-by-bin yields for all regions if set to True (default=False)
  """

  """
  pick up workspace from file
  """
  workspacename = 'w'
  w = Util.GetWorkspaceFromFile(filename,'w')
  if w==None:
    print "ERROR : Cannot open workspace : ", workspacename
    sys.exit(1)

  """
  pick up after-fit RooExpandedFitResult from workspace
  """
  resultAfterFit = w.obj('RooExpandedFitResult_afterFit')
  if resultAfterFit==None:
    print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit"
    sys.exit(1)

  """
  pick up before-fit RooExpandedFitResult from workspace
  """
  resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit')
  if resultBeforeFit==None:
    print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit"
    sys.exit(1)

  """
  pick up dataset from workspace
  """
  data_set = w.data(dataname)
  if data_set==None:
    print "ERROR : Cannot open dataset : ", "data_set"+suffix
    sys.exit(1)

  """
  pick up channel category (RooCategory) from workspace
  """
  regionCat = w.obj("channelCat")
  if not blinded:
    data_set.table(regionCat).Print("v")

  """
  find full (long) name list of regions (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50)
  """
  regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in regionList]


  """
  load afterFit workspace snapshot (=set all parameters to values after fit)
  """
  snapshot =  'snapshot_paramsVals_RooExpandedFitResult_afterFit'
  w.loadSnapshot(snapshot)

  if not w.loadSnapshot(snapshot):
    print "ERROR : Cannot load snapshot : ", snapshot
    sys.exit(1)

  """
  define set, for all names/yields to be saved in
  """
  tablenumbers = {}

  """
  if showSum=True define names for sum of all regions and add to regionList
  """
  sumName = ""
  for index, reg in enumerate(regionList):
    if index == 0:
      sumName = reg
    else:
      sumName = sumName + " + " + reg

  regionListWithSum = list(regionList)
  if showSum:
    regionListWithSum.append(sumName)

  tablenumbers['names'] = regionListWithSum

  """
  make a list of channelCat calls for every region
  """
  regionCatList = [ 'channelCat==channelCat::' +region.Data() for region in regionFullNameList]

  """
  retrieve number of observed (=data) events per region
  """
  print regionCatList
  srindex = regionFullNameList.index("SR_cuts")
  regionDatasetList = [data_set.reduce(regioncat) for regioncat in regionCatList]

  for index, data in enumerate(regionDatasetList):
    print "data," , data, data.GetName()
    data.SetName("data_" + regionList[index])
    data.SetTitle("data_" + regionList[index])

  nobs_regionList = [ data.sumEntries() for data in regionDatasetList]
  if blinded :  nobs_regionList[srindex] = -1
  """
  if showSum=True calculate the total number of observed events in all regions
  """
  sumNobs = 0.
  for nobs in nobs_regionList:
    sumNobs += nobs
  if showSum:
    nobs_regionList.append(sumNobs)
  tablenumbers['nobs'] = nobs_regionList


  """
  FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT
  """

  """
  get a list of pdf's and variables per region
  """
  pdfinRegionList = [ Util.GetRegionPdf(w, region)  for region in regionList]
  varinRegionList =  [ Util.GetRegionVar(w, region) for region in regionList]

  """
  if splitBins=True get the list of Nbins, binMax and binMin; make a list of new region names for each bin
  """
  varNbinsInRegionList =  []
  varBinLowInRegionList = []
  varBinHighInRegionList =  []
  rangeNameBinsInRegionList = []
  if splitBins:
    varNbinsInRegionList = [Util.GetRegionVar(w, region).getBinning().numBins() for region in regionList]
    varBinLowInRegionList = [[Util.GetRegionVar(w, region).getBinning((region+"binning")).binLow(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx,region in enumerate(regionList)]
    varBinHighInRegionList = [[Util.GetRegionVar(w, region).getBinning((region+"binning")).binHigh(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx,region in enumerate(regionList)]
    rangeNameBinsInRegionList = [[regionList[idx]+"_bin"+str(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx,region in enumerate(regionList)]
    for index,region in enumerate(regionList):
      if varNbinsInRegionList[index]==1:
        print " \n YieldsTable.py: WARNING: you have called -P (= per-bin yields) but this region ", region, " has only 1 bin \n"



  """
  if splitBins=True reshuffle the regionName list; each region name is followed by names of each bin (i.e. regionNameList=['SR3J','SR3J_bin1','SR3j_bin2','SR4J','SR4J_bin1'])
  """
  regionListWithBins = []
  if splitBins:
    for index,region in enumerate(regionList):
      regionListWithBins.append(region)
      for ibin in range(0,varNbinsInRegionList[index]):
        regionListWithBins.append(rangeNameBinsInRegionList[index][ibin])
    tablenumbers['names'] = regionListWithBins


  """
  calculate number of observed(=data) events per bin
  """
  nobs_regionListWithBins = []
  if splitBins:
    binFuncInRegionList = [RooBinningCategory("bin_"+region,"bin_"+region,varinRegionList[index]) for index,region in enumerate(regionList)]
    for index, data in enumerate(regionDatasetList):
      data.addColumn(binFuncInRegionList[index])
      if not blinded:
        data.table(binFuncInRegionList[index]).Print("v")
      nobs_regionListWithBins.append(data.sumEntries())
      for ibin in range(0,varNbinsInRegionList[index]):
        nobs_regionListWithBins.append((data.reduce(binFuncInRegionList[index].GetName()+"=="+binFuncInRegionList[index].GetName()+"::"+varinRegionList[index].GetName()+"_bin"+str(ibin))).sumEntries())

    tablenumbers['nobs'] = nobs_regionListWithBins

  """
  if blinded=True, set all numbers of observed events to -1
  """
  if blinded and splitBins:
    for index, nobs in enumerate(nobs_regionListWithBins):
      nobs_regionListWithBins[index] = -1
    tablenumbers['nobs'] = nobs_regionListWithBins


  """
  get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples)
  """
  rrspdfinRegionList = []
  for index,pdf in enumerate(pdfinRegionList):
    prodList = pdf.pdfList()
    foundRRS = 0
    for idx in range(prodList.getSize()):
      if prodList[idx].InheritsFrom("RooRealSumPdf"):
        rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]))
        rrspdfinRegionList.append(rrspdfInt)
        if splitBins:
          origMin = varinRegionList[index].getMin()
          origMax = varinRegionList[index].getMax()
          for ibin in range(0,varNbinsInRegionList[index]):
            rangeName = rangeNameBinsInRegionList[index][ibin]
            varinRegionList[index].setRange(rangeName,varBinLowInRegionList[index][ibin],varBinHighInRegionList[index][ibin])
            rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]),rangeName)
            rrspdfinRegionList.append(rrspdfInt)
          varinRegionList[index].setRange(origMin,origMax)
        foundRRS += 1
    if foundRRS >1 or foundRRS==0:
      print " \n\n WARNING: ", pdf.GetName(), " has ", foundRRS, " instances of RooRealSumPdf"
      print pdf.GetName(), " component list:", prodList.Print("v")

  """
  calculate total pdf number of fitted events and error
  """
  nFittedInRegionList =  [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)]
  pdfFittedErrInRegionList = [ Util.GetPropagatedError(pdf, resultAfterFit, doAsym) for pdf in rrspdfinRegionList]


  """
  if showSum=True calculate the total number of fitted events in all regions
  """
  if showSum:
    pdfInAllRegions = RooArgSet()
    for index, pdf in enumerate(rrspdfinRegionList):
      pdfInAllRegions.add(pdf)
    pdfSumInAllRegions = RooAddition( "pdf_AllRegions_AFTER", "pdf_AllRegions_AFTER", RooArgList(pdfInAllRegions))
    nPdfSumVal = pdfSumInAllRegions.getVal()
    nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultAfterFit, doAsym)
    nFittedInRegionList.append(nPdfSumVal)
    pdfFittedErrInRegionList.append(nPdfSumError)

  tablenumbers['TOTAL_FITTED_bkg_events']    =  nFittedInRegionList
  tablenumbers['TOTAL_FITTED_bkg_events_err']    =  pdfFittedErrInRegionList

  """
  calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf
  """
  for isam, sample in enumerate(sampleList):
    sampleName=getName(sample)
    nSampleInRegionVal = []
    nSampleInRegionError = []
    sampleInAllRegions = RooArgSet()
    for ireg, region in enumerate(regionList):
      sampleInRegion=getPdfInRegions(w,sample,region)
      sampleInRegionVal = 0.
      sampleInRegionError = 0.
      if not sampleInRegion==None:
        sampleInRegionVal = sampleInRegion.getVal()
        sampleInRegionError = Util.GetPropagatedError(sampleInRegion, resultAfterFit, doAsym)
        sampleInAllRegions.add(sampleInRegion)
      else:
        print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region =",region, "\n"
      nSampleInRegionVal.append(sampleInRegionVal)
      nSampleInRegionError.append(sampleInRegionError)

      """
      if splitBins=True calculate numbers of fitted events plus error per bin
      """
      if splitBins:
        origMin = varinRegionList[ireg].getMin()
        origMax = varinRegionList[ireg].getMax()
        for ibin in range(0,varNbinsInRegionList[ireg]):
          rangeName = rangeNameBinsInRegionList[ireg][ibin]
          sampleInRegion=getPdfInRegionsWithRangeName(w,sample,region,rangeName)
          sampleInRegionVal = 0.
          sampleInRegionError = 0.
          if not sampleInRegion==None:
            varinRegionList[ireg].setRange(rangeName,varBinLowInRegionList[ireg][ibin],varBinHighInRegionList[ireg][ibin])
            sampleInRegionVal = sampleInRegion.getVal()
            sampleInRegionError = Util.GetPropagatedError(sampleInRegion, resultAfterFit, doAsym)
          else:
            print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=",region, " bin=",ibin, " \n"
          nSampleInRegionVal.append(sampleInRegionVal)
          nSampleInRegionError.append(sampleInRegionError)

        varinRegionList[ireg].setRange(origMin,origMax)

    """
    if showSum=True calculate the total number of fitted events in all regions
    """
    if showSum:
      sampleSumInAllRegions = RooAddition( (sampleName+"_AllRegions_FITTED"), (sampleName+"_AllRegions_FITTED"), RooArgList(sampleInAllRegions))
      nSampleSumVal = sampleSumInAllRegions.getVal()
      nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions, resultAfterFit, doAsym)
      nSampleInRegionVal.append(nSampleSumVal)
      nSampleInRegionError.append(nSampleSumError)
    tablenumbers['Fitted_events_'+sampleName]   = nSampleInRegionVal
    tablenumbers['Fitted_err_'+sampleName]   = nSampleInRegionError



  print "\n starting BEFORE-FIT calculations \n"
  """
  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORRE__ THE FIT
  """

  """
  load beforeFit workspace snapshot (=set all parameters to values before fit)
  """
  w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit')

  """
  check if any of the initial scaling factors is != 1
  """
  _result = w.obj('RooExpandedFitResult_beforeFit')
  _muFacs = _result.floatParsFinal()

  for i in range(len(_muFacs)):
    if "mu_" in _muFacs[i].GetName() and _muFacs[i].getVal() != 1.0:
      print  " \n WARNING: scaling factor %s != 1.0 (%g) expected MC yield WILL BE WRONG!" % (_muFacs[i].GetName(), _muFacs[i].getVal())

  """
  get a list of pdf's and variables per region
  """
  pdfinRegionList = [ Util.GetRegionPdf(w, region)  for region in regionList]
  varinRegionList =  [ Util.GetRegionVar(w, region) for region in regionList]

  """
  get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples)
  """
  rrspdfinRegionList = []
  for index,pdf in enumerate(pdfinRegionList):
    prodList = pdf.pdfList()
    foundRRS = 0
    for idx in range(prodList.getSize()):
      if prodList[idx].InheritsFrom("RooRealSumPdf"):
        rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]))
        rrspdfinRegionList.append(rrspdfInt)
        if splitBins:
          origMin = varinRegionList[index].getMin()
          origMax = varinRegionList[index].getMax()
          for ibin in range(0,varNbinsInRegionList[index]):
            rangeName = rangeNameBinsInRegionList[index][ibin]
            varinRegionList[index].setRange(rangeName,varBinLowInRegionList[index][ibin],varBinHighInRegionList[index][ibin])
            rrspdfInt =  prodList[idx].createIntegral(RooArgSet(varinRegionList[index]),rangeName)
            rrspdfinRegionList.append(rrspdfInt)
          varinRegionList[index].setRange(origMin,origMax)
        foundRRS += 1
    if foundRRS >1 or foundRRS==0:
      print " \n\n WARNING: ", pdf.GetName(), " has ", foundRRS, " instances of RooRealSumPdf"
      print pdf.GetName(), " component list:", prodList.Print("v")

  """
  calculate total pdf number of expected events and error
  """
  nExpInRegionList =  [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)]
  pdfExpErrInRegionList = [ Util.GetPropagatedError(pdf, resultBeforeFit, doAsym)  for pdf in rrspdfinRegionList]

  """
  if showSum=True calculate the total number of expected events in all regions
  """
  if showSum:
    pdfInAllRegions = RooArgSet()
    for index, pdf in enumerate(rrspdfinRegionList):
      pdfInAllRegions.add(pdf)
    pdfSumInAllRegions = RooAddition( "pdf_AllRegions_BEFORE", "pdf_AllRegions_BEFORE", RooArgList(pdfInAllRegions))
    nPdfSumVal = pdfSumInAllRegions.getVal()
    nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultBeforeFit, doAsym)
    nExpInRegionList.append(nPdfSumVal)
    pdfExpErrInRegionList.append(nPdfSumError)

  tablenumbers['TOTAL_MC_EXP_BKG_events']    =  nExpInRegionList
  tablenumbers['TOTAL_MC_EXP_BKG_err']    =  pdfExpErrInRegionList

  """
  calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf
  """
  for isam, sample in enumerate(sampleList):
    sampleName=getName(sample)
    nMCSampleInRegionVal = []
    nMCSampleInRegionError = []
    MCSampleInAllRegions = RooArgSet()
    for ireg, region in enumerate(regionList):
      MCSampleInRegion = getPdfInRegions(w,sample,region)
      MCSampleInRegionVal = 0.
      MCSampleInRegionError = 0.
      if not MCSampleInRegion==None:
        MCSampleInRegionVal = MCSampleInRegion.getVal()
        MCSampleInRegionError = Util.GetPropagatedError(MCSampleInRegion, resultBeforeFit, doAsym)
        MCSampleInAllRegions.add(MCSampleInRegion)
      else:
        print " \n WARNING: sample=", sampleName, " non-existent (empty) in region=",region
      nMCSampleInRegionVal.append(MCSampleInRegionVal)
      nMCSampleInRegionError.append(MCSampleInRegionError)

      """
      if splitBins=True calculate numbers of fitted events plus error per bin
      """
      if splitBins:
        origMin = varinRegionList[ireg].getMin()
        origMax = varinRegionList[ireg].getMax()
        for ibin in range(0,varNbinsInRegionList[ireg]):
          rangeName = rangeNameBinsInRegionList[ireg][ibin]
          MCSampleInRegion=getPdfInRegionsWithRangeName(w,sample,region,rangeName)
          MCSampleInRegionVal = 0.
          MCSampleInRegionError = 0.
          if not MCSampleInRegion==None:
            varinRegionList[ireg].setRange(rangeName,varBinLowInRegionList[ireg][ibin],varBinHighInRegionList[ireg][ibin])
            MCSampleInRegionVal = MCSampleInRegion.getVal()
            MCSampleInRegionError = Util.GetPropagatedError(MCSampleInRegion, resultBeforeFit, doAsym)
          else:
            print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=",region, " bin=",ibin, " \n"
          nMCSampleInRegionVal.append(MCSampleInRegionVal)
          nMCSampleInRegionError.append(MCSampleInRegionError)

        varinRegionList[ireg].setRange(origMin,origMax)

    """
    if showSum=True calculate the total number of fitted events in all regions
    """
    if showSum:
      MCSampleSumInAllRegions = RooAddition( (sampleName+"_AllRegions_MC"), (sampleName+"_AllRegions_MC"), RooArgList(MCSampleInAllRegions))
      nMCSampleSumVal = MCSampleSumInAllRegions.getVal()
      nMCSampleSumError = Util.GetPropagatedError(MCSampleSumInAllRegions, resultBeforeFit, doAsym)
      nMCSampleInRegionVal.append(nMCSampleSumVal)
      nMCSampleInRegionError.append(nMCSampleSumError)
    tablenumbers['MC_exp_events_'+sampleName] = nMCSampleInRegionVal
    tablenumbers['MC_exp_err_'+sampleName] = nMCSampleInRegionError

  """
  sort the tablenumbers set
  """
  map_listofkeys = tablenumbers.keys()
  map_listofkeys.sort()

  """
  print the sorted tablenumbers set
  """
  for name in map_listofkeys:
    if tablenumbers.has_key(name) :
      print name, ": ", tablenumbers[name]

  return tablenumbers
Пример #12
0
def getComponents(cfg, w):
    """ Fetch all components (data, MC pdfs...) for a given workspace
    Organize all of this in a map
    """
    iscomb = cfg.dataname == 'combData'  # SKC
    mc = w.obj("ModelConfig")
    data = w.data(cfg.dataname)
    simPdf = w.pdf('combPdf' if iscomb else "simPdf")
    channelCat = simPdf.indexCat()
    chanName = channelCat.GetName()

    comps = {}

    for tt in categories(channelCat):
        ttname = tt.GetName()
        pdftmp = simPdf.getPdf(ttname)
        datatmp = data.reduce("{0}=={1}::{2}".format(chanName, chanName,
                                                     ttname))
        obs = pdftmp.getObservables(mc.GetObservables()).first()
        obs_set = RooArgSet(obs)
        sure, yup = ttname_mod(cfg, ttname)
        binWidth = pdftmp.getVariables().find("binWidth_obs_x_{0}_0{1}".format(
            ttname, sure + yup))
        if not binWidth:
            it = pdftmp.getVariables().createIterator()
            print('---------ttname is-------{}'.format(ttname))
            var = it.Next()
            while var:
                if 'binWidth' in var.GetName(): print(var.GetName())
                var = it.Next()
        logging.debug("    Bin Width : {}".format(binWidth.getVal()))
        comps[ttname] = [obs, binWidth, datatmp, {}, pdftmp]

        binning = obs.getBinning()
        stepsize = binning.averageBinWidth()
        low = binning.lowBound()
        high = binning.highBound()
        m = low
        while m < (high - 1e-6):
            obs.setRange("bin" + str(m), m, m + stepsize)
            m += stepsize

        bkgList = RooArgList()
        sigList = RooArgList()
        totList = RooArgList()

        for c in components(pdftmp, ttname, iscomb):
            compname = c.GetName()
            has_mass, res = cfg.is_signal(compname)
            if has_mass:
                sigList.add(c)
            else:
                bkgList.add(c)
            totList.add(c)

            comps[ttname][3][compname] = c

        sigSum = RooAddition("Signal", "sig_Sum", sigList)
        comps[ttname][3]["Signal"] = sigSum
        bkgSum = RooAddition("Bkg", "bkg_Sum", bkgList)
        comps[ttname][3]["Bkg"] = bkgSum
        totSum = RooAddition("MC", "tot_Sum", totList)
        comps[ttname][3]["MC"] = totSum

        try:
            cfg.combineComps(comps)
        except AttributeError:
            continue

    return comps
Пример #13
0
def getFrame(cat, obsData, simPdf, mc, fit_res, error_band_strategy=1, compute_yields=False):
    """
    Build a frame with the different fit components and their uncertainties

    Parameters
    ----------
    cat    : RooCategory
        Category of the simultaneous PDF we are interested in
    obsData: RooDataHist object (either real data or asimov or PE)
    simPdf : RooSimultaneous PDF
    mc : ModelConfig object
    fit_res: RooFitResult
        Result of the fit (covariance matrix, NLL value, ...)
    error_band_strategy : True/False
        True: Use the linear approximation to extract the error band
        False: Use a sampling method
        See http://root.cern.ch/root/html/RooAbsReal.html#RooAbsReal:plotOnWithErrorBand
    verbose: True/False

    TODO: implement a more generic way to retrieve the binning.
    Currently it has to be put in the workspace with the name binWidth_obs_x_{channel}_0
    """

    hlist = []
    if compute_yields:
        yields = {}
    # --> Get the total (signal+bkg) model pdf
    pdftmp = simPdf.getPdf(cat.GetName())
    if not pdftmp:
        raise RuntimeError('Could not retrieve the total pdf ')
    # --> Get the list of observables
    obstmp  = pdftmp.getObservables(mc.GetObservables())
    if not obstmp:
        raise RuntimeError('Could not retrieve the list of observable')
    # --> Get the first (and only) observable (mmc mass for cut based)
    obs  = obstmp.first()
    log.info('Observable: {0}'.format(obs.GetName()))
    # --> Get the RooDataHist of the given category
    datatmp = obsData.reduce("{0}=={1}::{2}".format(simPdf.indexCat().GetName(), simPdf.indexCat().GetName(), cat.GetName()))
    datatmp.__class__=ROOT.RooAbsData # --> Ugly fix !!!
    # --> Get the binning width of the category (stored as a workspace variable)
    binWidth = pdftmp.getVariables().find('binWidth_obs_x_{0}_0'.format(cat.GetName()))
    if not binWidth:
        raise RuntimeError('Could not retrieve the binWidth')
    # --> parameter of interest (mu=sigma/sigma_sm)
    poi =  mc.GetParametersOfInterest().first()
    poi_fitted_val = poi.getVal()
    log.info('POI: {0} = {1} +/- {2}'.format(poi.GetName(), poi.getVal(), poi.getError()))
            
    # --> Create the data histogram
    hist_data = asrootpy(datatmp.createHistogram("hdata_"+cat.GetName(), obs))
    hist_data.name = "hdata_{0}".format(cat.GetName())
    hist_data.title = ""
    hlist.append(hist_data)
    #--> compute the data yields
    if compute_yields:
        Yield_data = hist_data.Integral()
        yields['Data'] = (Yield_data, 0)

    # --> Create the frame structure from the observable
    frame = obs.frame()
    frame.SetName(cat.GetName())
    datatmp.plotOn(frame,
                   ROOT.RooFit.DataError(ROOT.RooAbsData.Poisson),
                   ROOT.RooFit.Name("Data"),
                   ROOT.RooFit.MarkerSize(1))

    # --> Create the signal histogram template
    hist_sig = hist_data.Clone('h_TotalSignal_{0}'.format(cat.GetName()))
    hist_sig.Reset()
    signal_comps = RooArgList()
    bkg_comps = RooArgList()
    # --> get the list of components (hadhad HSG4: QCD,Other,Ztautau, Signal_Z, Signal_W, Signal_gg, Signal_VBF)
    # --> and iterate over 
    pdfmodel = pdftmp.getComponents().find(cat.GetName()+'_model')
    funcListIter = pdfmodel.funcList().iterator()
    while True:
        comp = funcListIter.Next()
        if not comp:
            break

        name = comp.GetName().replace('L_x_', '').split('_')[0]
        log.info('Scan component {0}'.format(comp.GetName()))
        hist_comp = asrootpy(comp.createHistogram(cat.GetName()+"_"+comp.GetName(), obs, ROOT.RooFit.Extended(False)))
        hist_comp.name = 'h_{0}_{1}'.format(name, cat.GetName())
        hist_comp.title = ''
        hlist.append(hist_comp)

        if 'Signal' in comp.GetName():
            signal_comps.add(comp)
            hist_sig.Add(hist_comp)
        else:
            bkg_comps.add(comp)
        Integral_comp = comp.createIntegral(RooArgSet(obs))
        Yield_comp = Integral_comp.getVal() * binWidth.getVal()
        log.info('Integral = {0}'.format(Yield_comp))
        #         if Yield_comp==0:
        #             raise RuntimeError('Yield integral is wrong !!')


        # --> Add the components to the frame but in an invisible way
        pdfmodel.plotOn(frame,
                        ROOT.RooFit.Components(comp.GetName()),
                        ROOT.RooFit.Normalization(Yield_comp, ROOT.RooAbsReal.NumEvent),
                        ROOT.RooFit.Name("NoStacked_"+comp.GetName()),
                        ROOT.RooFit.Invisible())
        if fit_res:
            # --> Add the components uncertainty band 
            comp.plotOn(frame,
                        ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
                        ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
                        ROOT.RooFit.Name("FitError_AfterFit_"+comp.GetName()),
                        ROOT.RooFit.Invisible())
            if compute_yields:
                if Yield_comp==0:
                    Yield_comp_err=0.
                else:
                    Yield_comp_err = Integral_comp.getPropagatedError(fit_res)* binWidth.getVal()
                yields[comp.GetName()] = (Yield_comp, Yield_comp_err)

    hlist.append(hist_sig)
    # --> total signal yields
    if compute_yields:
        signal_tot_comp = RooAddition('pdf_sum_sig', 'pdf_sum_sig', signal_comps)
        Integral_signal = signal_tot_comp.createIntegral(RooArgSet(obs))
        yields_sig_tot = Integral_signal.getVal()*binWidth.getVal()
        yields_sig_tot_err = Integral_signal.getPropagatedError(fit_res)* binWidth.getVal()
        yields['TotalSignal'] = (yields_sig_tot, yields_sig_tot_err)
        bkg_tot_comp = RooAddition('pdf_sum_bkg', 'pdf_sum_bkg', bkg_comps)
        Integral_bkg = bkg_tot_comp.createIntegral(RooArgSet(obs))
        yields_bkg_tot = Integral_bkg.getVal()*binWidth.getVal()
        yields_bkg_tot_err = Integral_bkg.getPropagatedError(fit_res)* binWidth.getVal()
        yields['bkg'] = (yields_bkg_tot, yields_bkg_tot_err)
        log.info('Bkg Total Yield: {0}'.format(yields_bkg_tot))

    # --> bkg+signal PDF central value and error
    Integral_total = pdfmodel.createIntegral(RooArgSet(obs))
    Yield_total = Integral_total.getVal() * binWidth.getVal()
    log.info('Postfit Total Yield: {0}'.format(Yield_total))
    hist_bkg_plus_sig = asrootpy(pdfmodel.createHistogram("hbkg_plus_sig_"+cat.GetName(), obs, ROOT.RooFit.Extended(False)))
    hist_bkg_plus_sig.name = 'hbkg_plus_sig_'+cat.GetName()
    hist_bkg_plus_sig.title = ''
    hist_bkg_plus_sig.Scale(Yield_total)
    hlist.append(hist_bkg_plus_sig)

    # --> Add the components to the frame but in an invisible way
    pdftmp.plotOn(frame,
                   ROOT.RooFit.Normalization(Yield_total, ROOT.RooAbsReal.NumEvent),
                   ROOT.RooFit.Name("Bkg_plus_sig"))

    if fit_res:
        pdftmp.plotOn(frame,
                      ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
                      ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
                      ROOT.RooFit.Name("FitError_AfterFit"),
                      ROOT.RooFit.FillColor(ROOT.kOrange),
                      ROOT.RooFit.LineWidth(2),
                      ROOT.RooFit.LineColor(ROOT.kBlue))

    # --> bkg only PDF central value and error
    poi.setVal(0.)
    Integral_bkg_total = pdfmodel.createIntegral(RooArgSet(obs))
    Yield_bkg_total = Integral_bkg_total.getVal() * binWidth.getVal()

    hist_bkg = pdfmodel.createHistogram("hbkg_"+cat.GetName(), obs, ROOT.RooFit.Extended(False))
    hist_bkg.Scale(Yield_bkg_total)
    hist_bkg.SetName("hbkg_"+cat.GetName())
    hist_bkg.SetTitle("")
    hlist.append(hist_bkg)
    pdftmp.plotOn(frame,
                  ROOT.RooFit.Normalization(Yield_bkg_total, ROOT.RooAbsReal.NumEvent),
                  ROOT.RooFit.Name("Bkg"),
                  ROOT.RooFit.LineStyle(ROOT.kDashed))
    if fit_res:
        pdftmp.plotOn(frame,
                      ROOT.RooFit.VisualizeError(fit_res, 1, error_band_strategy),
                      ROOT.RooFit.Normalization(1, ROOT.RooAbsReal.RelativeExpected),
                      ROOT.RooFit.Name("FitError_AfterFit_Mu0"),
                      ROOT.RooFit.FillColor(ROOT.kOrange),
                      ROOT.RooFit.LineWidth(2),
                      ROOT.RooFit.LineColor(ROOT.kBlue))
        #if compute_yields:
            # Yield_bkg_total_err = Integral_bkg_total.getPropagatedError(fit_res)*binWidth.getVal()
            # yields['bkg'] = (Yield_bkg_total, Yield_bkg_total_err)

    poi.setVal(poi_fitted_val)
    if compute_yields:
        return frame, hlist, yields
    else:
        return frame, hlist
Пример #14
0
def latexfitresults(filename,
                    regionList,
                    sampleList,
                    dataname='obsData',
                    showSum=False):

    w = Util.GetWorkspaceFromFile(filename, 'w')

    if w == None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)

    resultAfterFit = w.obj('RooExpandedFitResult_afterFit')
    if resultAfterFit == None:
        print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit"
        sys.exit(1)

    resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit')
    if resultBeforeFit == None:
        print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit"
        sys.exit(1)

    data_set = w.data(dataname)
    if data_set == None:
        print "ERROR : Cannot open dataset : ", "data_set" + suffix
        sys.exit(1)

    regionCat = w.obj("channelCat")
    data_set.table(regionCat).Print("v")

    regionFullNameList = [
        Util.GetFullRegionName(regionCat, region) for region in regionList
    ]
    print regionFullNameList

    ###

    snapshot = 'snapshot_paramsVals_RooExpandedFitResult_afterFit'
    w.loadSnapshot(snapshot)

    if not w.loadSnapshot(snapshot):
        print "ERROR : Cannot load snapshot : ", snapshot
        sys.exit(1)

    tablenumbers = {}

    # SUM ALL REGIONS
    sumName = ""
    for index, reg in enumerate(regionList):
        if index == 0:
            sumName = reg
        else:
            sumName = sumName + " + " + reg

    regionListWithSum = list(regionList)
    if showSum:
        regionListWithSum.append(sumName)

    tablenumbers['names'] = regionListWithSum

    regionCatList = [
        'channelCat==channelCat::' + region.Data()
        for region in regionFullNameList
    ]

    regionDatasetList = [
        data_set.reduce(regioncat) for regioncat in regionCatList
    ]
    for index, data in enumerate(regionDatasetList):
        data.SetName("data_" + regionList[index])
        data.SetTitle("data_" + regionList[index])

    nobs_regionList = [data.sumEntries() for data in regionDatasetList]
    #SUM
    sumNobs = 0.
    for nobs in nobs_regionList:
        sumNobs += nobs
        print " \n XXX nobs = ", nobs, "    sumNobs = ", sumNobs
    if showSum:
        nobs_regionList.append(sumNobs)
    tablenumbers['nobs'] = nobs_regionList

    ######
    ######
    ######  FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT
    ######
    ######

    # total pdf, not splitting in components
    pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList]
    varinRegionList = [Util.GetRegionVar(w, region) for region in regionList]
    rrspdfinRegionList = []
    for index, pdf in enumerate(pdfinRegionList):
        #    pdf.Print("t")
        prodList = pdf.pdfList()
        foundRRS = 0
        for idx in range(prodList.getSize()):
            #      if "BG" in prodList[idx].GetName():
            #        prodList[idx].Print("t")
            if prodList[idx].InheritsFrom("RooRealSumPdf"):
                rrspdfInt = prodList[idx].createIntegral(
                    RooArgSet(varinRegionList[index]))
                rrspdfinRegionList.append(rrspdfInt)
                foundRRS += 1
        if foundRRS > 1 or foundRRS == 0:
            print " \n\n WARNING: ", pdf.GetName(
            ), " has ", foundRRS, " instances of RooRealSumPdf"
            print pdf.GetName(), " component list:", prodList.Print("v")

    nFittedInRegionList = [
        pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)
    ]
    pdfFittedErrInRegionList = [
        Util.GetPropagatedError(pdf, resultAfterFit)
        for pdf in rrspdfinRegionList
    ]

    if showSum:
        pdfInAllRegions = RooArgSet()
        for index, pdf in enumerate(rrspdfinRegionList):
            pdfInAllRegions.add(pdf)
        pdfSumInAllRegions = RooAddition("pdf_AllRegions_AFTER",
                                         "pdf_AllRegions_AFTER",
                                         pdfInAllRegions)
        pdfSumInAllRegions.Print()
        nPdfSumVal = pdfSumInAllRegions.getVal()
        nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions,
                                               resultAfterFit)
        nFittedInRegionList.append(nPdfSumVal)
        pdfFittedErrInRegionList.append(nPdfSumError)

    tablenumbers['TOTAL_FITTED_bkg_events'] = nFittedInRegionList
    tablenumbers['TOTAL_FITTED_bkg_events_err'] = pdfFittedErrInRegionList

    # components
    for isam, sample in enumerate(sampleList):
        nSampleInRegionVal = []
        nSampleInRegionError = []
        sampleInAllRegions = RooArgSet()
        for ireg, region in enumerate(regionList):
            sampleInRegion = Util.GetComponent(w, sample, region)
            sampleInRegionVal = 0.
            sampleInRegionError = 0.
            if not sampleInRegion == None:
                sampleInRegion.Print()
                sampleInRegionVal = sampleInRegion.getVal()
                sampleInRegionError = Util.GetPropagatedError(
                    sampleInRegion, resultAfterFit)
                sampleInAllRegions.add(sampleInRegion)
            else:
                print " \n YieldsTable.py: WARNING: sample =", sample, " non-existent (empty) in region =", region, "\n"
            nSampleInRegionVal.append(sampleInRegionVal)
            nSampleInRegionError.append(sampleInRegionError)
        # print " \n\n  XXX-AFTER sample = ", sample
        if showSum:
            sampleSumInAllRegions = RooAddition(
                (sample + "_AllRegions_FITTED"),
                (sample + "_AllRegions_FITTED"), sampleInAllRegions)
            sampleSumInAllRegions.Print()
            nSampleSumVal = sampleSumInAllRegions.getVal()
            nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions,
                                                      resultAfterFit)
            nSampleInRegionVal.append(nSampleSumVal)
            nSampleInRegionError.append(nSampleSumError)
        tablenumbers['Fitted_events_' + sample] = nSampleInRegionVal
        tablenumbers['Fitted_err_' + sample] = nSampleInRegionError

    ######
    ######
    ######  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORE__ THE FIT
    ######
    ######

    #  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS BEFORE THE FIT
    w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit')

    pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList]
    varinRegionList = [Util.GetRegionVar(w, region) for region in regionList]
    rrspdfinRegionList = []
    for index, pdf in enumerate(pdfinRegionList):
        prodList = pdf.pdfList()
        foundRRS = 0
        for idx in range(prodList.getSize()):
            if prodList[idx].InheritsFrom("RooRealSumPdf"):
                #      print " \n\n  XXX-BEFORE  prodList[idx] = ", prodList[idx].GetName()
                prodList[idx].Print()
                rrspdfInt = prodList[idx].createIntegral(
                    RooArgSet(varinRegionList[index]))
                rrspdfinRegionList.append(rrspdfInt)
                foundRRS += 1
        if foundRRS > 1 or foundRRS == 0:
            print " \n\n WARNING: ", pdf.GetName(
            ), " has ", foundRRS, " instances of RooRealSumPdf"
            print pdf.GetName(), " component list:", prodList.Print("v")

    nExpInRegionList = [
        pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList)
    ]
    pdfExpErrInRegionList = [
        Util.GetPropagatedError(pdf, resultBeforeFit)
        for pdf in rrspdfinRegionList
    ]

    if showSum:
        pdfInAllRegions = RooArgSet()
        for index, pdf in enumerate(rrspdfinRegionList):
            pdfInAllRegions.add(pdf)
        pdfSumInAllRegions = RooAddition("pdf_AllRegions_BEFORE",
                                         "pdf_AllRegions_BEFORE",
                                         pdfInAllRegions)
        nPdfSumVal = pdfSumInAllRegions.getVal()
        nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions,
                                               resultAfterFit)
        nExpInRegionList.append(nPdfSumVal)
        pdfExpErrInRegionList.append(nPdfSumError)

    tablenumbers['TOTAL_MC_EXP_BKG_events'] = nExpInRegionList
    tablenumbers['TOTAL_MC_EXP_BKG_err'] = pdfExpErrInRegionList

    for isam, sample in enumerate(sampleList):
        nMCSampleInRegionVal = []
        nMCSampleInRegionError = []
        sampleInAllRegions = RooArgSet()
        for ireg, region in enumerate(regionList):
            MCSampleInRegion = Util.GetComponent(w, sample, region)
            MCSampleInRegionVal = 0.
            MCSampleInRegionError = 0.
            if not MCSampleInRegion == None:
                MCSampleInRegionVal = MCSampleInRegion.getVal()
                MCSampleInRegionError = Util.GetPropagatedError(
                    MCSampleInRegion, resultBeforeFit)
                sampleInAllRegions.add(sampleInRegion)
            else:
                print " \n WARNING: sample=", sample, " non-existent (empty) in region=", region
            nMCSampleInRegionVal.append(MCSampleInRegionVal)
            nMCSampleInRegionError.append(MCSampleInRegionError)
        #print " \n\n  XXX-BEFORE  sample = ", sample
        if showSum:
            sampleSumInAllRegions = RooAddition((sample + "_AllRegions_MC"),
                                                (sample + "_AllRegions_MC"),
                                                sampleInAllRegions)
            nSampleSumVal = sampleSumInAllRegions.getVal()
            nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions,
                                                      resultBeforeFit)
            nMCSampleInRegionVal.append(nSampleSumVal)
            nMCSampleInRegionError.append(nSampleSumError)
        tablenumbers['MC_exp_events_' + sample] = nMCSampleInRegionVal
        tablenumbers['MC_exp_err_' + sample] = nMCSampleInRegionError

        #  sorted(tablenumbers, key=lambda sample: sample[1])   # sort by age
    map_listofkeys = tablenumbers.keys()
    map_listofkeys.sort()

    for name in map_listofkeys:
        if tablenumbers.has_key(name):
            print name, ": ", tablenumbers[name]

    ###
    return tablenumbers
Пример #15
0
def toy_run(n_params,
            n_gauss,
            n_toys,
            toys_nevents,
            run_zfit,
            intermediate_result_factory=None):
    # pdf = chebys[0]

    # zfit.settings.set_verbosity(10)

    performance = {}
    performance["column"] = "number of events"
    for nevents in toys_nevents:
        # n_toys = 30 if nevents < 50000 else 10

        if run_zfit:
            zfit.run.create_session(reset_graph=True)
            # zfit.sess.close()
            # zfit.sess = tf.Session
        # initial_param_val, obs, pdf = build_pdf(n_gauss, n_params, run_zfit)

        lower = -1
        upper = 1
        # create observables
        if run_zfit:
            obs = zfit.Space("obs1", limits=(lower, upper))
        else:
            obs = RooRealVar("obs", "obs1", lower, upper)
            ROOT.SetOwnership(obs, False)
        # create parameters
        params = []
        params_initial = []
        mu_lower, mu_upper = 1, 3
        sigma_lower, sigma_upper = 0.5, 2
        # step_size = 0.003
        for i in range(n_params):
            if run_zfit:
                mu = zfit.Parameter(
                    f"mu_{i}_{nevents}",
                    np.random.uniform(low=mu_lower, high=mu_upper),
                    mu_lower,
                    mu_upper,
                    # step_size=step_size
                )
                sigma = zfit.Parameter(
                    f"sigma_{i}_{nevents}",
                    np.random.uniform(low=sigma_lower, high=sigma_upper),
                    sigma_lower,
                    sigma_upper,
                    # step_size=step_size
                )
            else:
                mu_initial = np.random.uniform(mu_lower, mu_upper)
                mu = RooRealVar(f"mu_{i}_{nevents}", "Mean of Gaussian",
                                mu_initial, mu_lower, mu_upper)
                ROOT.SetOwnership(mu, False)
                sigma_initial = np.random.uniform(mu_lower, mu_upper)
                sigma = RooRealVar(f"sigma_{i}_{nevents}", "Width of Gaussian",
                                   sigma_initial, sigma_lower, sigma_upper)
                ROOT.SetOwnership(sigma, False)
                params_initial.append((mu_initial, sigma_initial))
            params.append((mu, sigma))
        # create pdfs
        pdfs = []
        for i in range(n_gauss):
            mu, sigma = params[i % n_params]
            if run_zfit:
                shifted_mu = mu + 0.3 * i
                shifted_sigma = sigma + 0.1 * i
                pdf = zfit.pdf.Gauss(obs=obs,
                                     mu=shifted_mu,
                                     sigma=shifted_sigma)
                # from zfit.models.basic import CustomGaussOLD
                # pdf = CustomGaussOLD(obs=obs, mu=shifted_mu, sigma=shifted_sigma)
                # pdf.update_integration_options(mc_sampler=tf.random_uniform)
            else:
                shift1 = RooFit.RooConst(float(0.3 * i))
                shifted_mu = RooAddition(f"mu_shifted_{i}_{nevents}",
                                         f"Shifted mu {i}",
                                         RooArgList(mu, shift1))
                shift2 = RooFit.RooConst(float(0.1 * i))
                shifted_sigma = RooAddition(f"sigma_shifted_{i}_{nevents}",
                                            f"Shifted sigma {i}",
                                            RooArgList(sigma, shift2))
                pdf = RooGaussian(f"pdf_{i}_{nevents}", "Gaussian pdf", obs,
                                  shifted_mu, shifted_sigma)
                ROOT.SetOwnership(pdf, False)
                ROOT.SetOwnership(shift1, False)
                ROOT.SetOwnership(shifted_mu, False)
                ROOT.SetOwnership(shift2, False)
                ROOT.SetOwnership(shifted_sigma, False)
            pdfs.append(pdf)
        initial_param_val = 1 / n_gauss
        fracs = []
        for i in range(n_gauss - 1):
            frac_value = 1 / n_gauss
            lower_value = 0.0001
            upper_value = 1.5 / n_gauss
            if run_zfit:
                frac = zfit.Parameter(f"frac_{i}",
                                      value=1 / n_gauss,
                                      lower_limit=lower_value,
                                      upper_limit=upper_value)
                frac.floating = False
            else:
                frac = RooRealVar(f"frac_{i}_{nevents}", "Fraction of a gauss",
                                  frac_value)
                ROOT.SetOwnership(frac, False)
            fracs.append(frac)
        if run_zfit:
            sum_pdf = zfit.pdf.SumPDF(pdfs=pdfs, fracs=fracs)
            # sum_pdf.update_integration_options(mc_sampler=tf.random_uniform)

        else:
            sum_pdf = RooAddPdf(f"sum_pdf_{nevents}", "sum of pdfs",
                                RooArgList(*pdfs), RooArgList(*fracs))
            ROOT.SetOwnership(sum_pdf, False)
        pdf = sum_pdf

        # Create dictionary to save fit results
        failed_fits = 0
        successful_fits = 0
        performance[nevents] = {"success": [], "fail": []}

        if run_zfit:
            sampler = pdf.create_sampler(n=nevents, fixed_params=True)
            sampler.set_data_range(obs)
            nll = zfit.loss.UnbinnedNLL(pdf, sampler)

            minimizer = zfit.minimize.MinuitMinimizer(
                zfit.minimizers.baseminimizer.ToyStrategyFail(),
                verbosity=5,
                minimize_strategy=1)
            # minimizer.minimizer_options['tol'] = 100

            # minimizer._use_tfgrad = False

        timer = zfit_benchmark.timer.Timer(f"Toys {nevents}")
        if run_zfit:
            sampler.resample()
            # with tf.device("/device:GPU:0"):
            jit_scope = tf.contrib.compiler.jit.experimental_jit_scope
            # with jit_scope():
            to_run = [nll.value(), nll.gradients()]
            zfit.run(to_run)
            dependents = pdf.get_dependents()
        else:
            mgr = ROOT.RooMCStudy(pdf, RooArgSet(obs), RooFit.Silence())
            ROOT.SetOwnership(mgr, False)
        run_toystudy = False
        with progressbar.ProgressBar(max_value=n_toys) as bar:
            ident = 0
            with timer:
                if not run_toystudy:
                    while successful_fits < n_toys:
                        # print(f"starting run number {len(fitResults)}")
                        if run_zfit:
                            sampler.resample()

                            for param in dependents:
                                param.randomize()
                        else:
                            for (mu, sigma), (mu_val, sigma_val) in zip(
                                    params, params_initial):
                                mu.setVal(mu_val)
                                sigma.setVal(sigma_val)

                            data = pdf.generate(RooArgSet(obs), nevents)

                            for mu, sigma in params:
                                mu.setVal(np.random.uniform(
                                    mu_lower, mu_upper))
                                sigma.setVal(
                                    np.random.uniform(sigma_lower,
                                                      sigma_upper))

                        with timer.child(
                                f"toy number {successful_fits} {ident}"
                        ) as child:
                            if run_zfit:
                                # sampler.resample()

                                # with tf.device("/device:GPU:0"):
                                minimum = minimizer.minimize(nll)
                                # print(minimum.hesse())
                            else:

                                # for mu, sigma in params:
                                #     mu.setVal(np.random.uniform(mu_lower, mu_upper))
                                #     sigma.setVal(np.random.uniform(sigma_lower, sigma_upper))
                                # for frac in fracs:
                                #     frac.setVal(np.random.uniform(lower_value, upper_value))
                                result = pdf.fitTo(data, RooFit.NumCPU(12),
                                                   RooFit.Save(True),
                                                   RooFit.Hesse(False),
                                                   RooFit.Minos(False))
                        if ident == 0:
                            ident += 1
                            continue  # warm up run
                        if run_zfit:
                            if minimum.converged:
                                bar.update(successful_fits)
                                successful_fits += 1
                                fail_or_success = "success"
                            else:
                                child.elapsed = Decimal()
                                failed_fits += 1
                                fail_or_success = "fail"
                        else:
                            if result.status() == 0:
                                bar.update(successful_fits)
                                successful_fits += 1
                                fail_or_success = "success"
                            else:
                                child.elapsed = Decimal()
                                failed_fits += 1
                                fail_or_success = "fail"

                        ident += 1
                        performance[nevents][fail_or_success].append(
                            float(child.elapsed))
                else:

                    mgr.generateAndFit(n_toys, nevents)
                    performance[nevents]["success"].append(
                        [float(timer.elapsed) / n_toys for _ in range(n_toys)])

        with open(f"{run_name}tmp.yaml", "w") as f:
            if intermediate_result_factory:
                dump_result = intermediate_result_factory(performance)
            else:
                dump_result = performance.copy()
            dump_result["ATTENTION"] = "NOT FINISHED"
            yaml.dump(dump_result, f)
    return performance
def latexfitresults(filename, regionList, dataname='obsData'):

    print "hallo"
    ####

    w = Util.GetWorkspaceFromFile(filename, 'combined')

    if w == None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)

    w.Print()

    resultname = 'RooExpandedFitResult_afterFit'
    result = w.obj(resultname)

    if result == None:
        print "ERROR : Cannot open fit result : ", resultname
        sys.exit(1)

    snapshot = 'snapshot_paramsVals_' + resultname
    w.loadSnapshot(snapshot)

    if not w.loadSnapshot(snapshot):
        print "ERROR : Cannot load snapshot : ", snapshot
        sys.exit(1)

    data_set = w.data(dataname)
    if data_set == None:
        print "ERROR : Cannot open dataset : ", "data_set" + suffix
        sys.exit(1)

    regionCat = w.obj("channelCat")
    data_set.table(regionCat).Print("v")

    regionFullNameList = [
        Util.GetFullRegionName(regionCat, region) for region in regionList
    ]
    print " \n Requested regions = ", regionFullNameList, "\n\n"

    ###

    tablenumbers = {}
    tablenumbers['names'] = regionList

    regionCatList = [
        'channelCat==channelCat::' + region.Data()
        for region in regionFullNameList
    ]

    regionDatasetList = [
        data_set.reduce(regioncat) for regioncat in regionCatList
    ]
    for index, data in enumerate(regionDatasetList):
        data.SetName("data_" + regionList[index])
        data.SetTitle("data_" + regionList[index])

    nobs_regionList = [data.sumEntries() for data in regionDatasetList]
    tablenumbers['nobs'] = nobs_regionList

    ####

    bkginRegionList = [
        Util.GetComponent(w, "Top,WZ,QCD,BG", region) for region in regionList
    ]
    nbkginRegionList = [
        bkginRegion.getVal() for bkginRegion in bkginRegionList
    ]
    [region.Print() for region in bkginRegionList]
    print "\n N bkgs in regions = ", nbkginRegionList

    nbkgerrinRegionList = [
        Util.GetPropagatedError(bkginRegion, result)
        for bkginRegion in bkginRegionList
    ]
    print "\n error N bkgs in regions = ", nbkgerrinRegionList

    WZinRegionList = [
        Util.GetComponent(w, "WZ", region) for region in regionList
    ]
    TopinRegionList = [
        Util.GetComponent(w, "Top", region) for region in regionList
    ]

    nWZinRegionList = [WZinRegion.getVal() for WZinRegion in WZinRegionList]
    nTopinRegionList = [
        TopinRegion.getVal() for TopinRegion in TopinRegionList
    ]

    print "\n N WZ in regions = ", nWZinRegionList
    print "\n N Top in regions = ", nTopinRegionList

    nWZerrinRegionList = [
        Util.GetPropagatedError(WZinRegion, result)
        for WZinRegion in WZinRegionList
    ]
    nToperrinRegionList = [
        Util.GetPropagatedError(TopinRegion, result)
        for TopinRegion in TopinRegionList
    ]

    print "\n error N WZ in regions = ", nWZerrinRegionList
    print "\n error N Top in regions = ", nToperrinRegionList

    ######
    # Example how to add multiple backgrounds in multiple(!) regions
    TopWZinRegionList = [
        RooAddition(("TopWZin" + regionList[index]),
                    ("TopWZin" + regionList[index]),
                    RooArgSet(TopinRegionList[index], WZinRegionList[index]))
        for index, region in enumerate(regionList)
    ]
    nTopWZinRegionList = [
        TopWZinRegion.getVal() for TopWZinRegion in TopWZinRegionList
    ]
    nTopWZerrinRegionList = [
        Util.GetPropagatedError(TopWZinRegion, result)
        for TopWZinRegion in TopWZinRegionList
    ]

    ######
    ######
    ######  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORE__ THE FIT
    ######
    ######

    #  FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS BEFORE THE FIT
    w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit')

    pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList]

    varinRegionList = [Util.GetRegionVar(w, region) for region in regionList]

    nexpinRegionList = [
        pdf.expectedEvents(RooArgSet(varinRegionList[index]))
        for index, pdf in enumerate(pdfinRegionList)
    ]
    print "\n N expected in regions = ", nexpinRegionList

    fracWZinRegionList = [
        Util.GetComponentFracInRegion(w, "WZ", region) for region in regionList
    ]
    fracTopinRegionList = [
        Util.GetComponentFracInRegion(w, "Top", region)
        for region in regionList
    ]

    mcWZinRegionList = [
        fracWZinRegionList[index] * nexpinRegionList[index]
        for index, region in enumerate(regionList)
    ]
    mcTopinRegionList = [
        fracTopinRegionList[index] * nexpinRegionList[index]
        for index, region in enumerate(regionList)
    ]

    #  mcSMinRegionList = [ mcWZinRegionList[index] + mcTopinRegionList[index] +  mcQCDinRegionList[index] + mcBGinRegionList[index]  for index, region in enumerate(regionList)]
    mcSMinRegionList = [
        mcWZinRegionList[index] + mcTopinRegionList[index]
        for index, region in enumerate(regionList)
    ]
    print "\n N expected WZ in regions = ", mcWZinRegionList
    print "\n N expected Top in regions = ", mcTopinRegionList

    tablenumbers['MC_exp_top_WZ_events'] = [
        mcWZinRegionList[index] + mcTopinRegionList[index]
        for index, region in enumerate(regionList)
    ]
    tablenumbers['MC_exp_top_events'] = [
        mcTopinRegion for mcTopinRegion in mcTopinRegionList
    ]
    tablenumbers['MC_exp_WZ_events'] = [
        mcWZinRegion for mcWZinRegion in mcWZinRegionList
    ]
    tablenumbers['MC_exp_SM_events'] = [
        mcSMinRegion for mcSMinRegion in mcSMinRegionList
    ]

    tablenumbers['Fitted_bkg_events'] = nbkginRegionList
    tablenumbers['Fitted_bkg_events_err'] = nbkgerrinRegionList

    tablenumbers['Fitted_top_events'] = nTopinRegionList
    tablenumbers['Fitted_top_events_err'] = nToperrinRegionList

    tablenumbers['Fitted_WZ_events'] = nWZinRegionList
    tablenumbers['Fitted_WZ_events_err'] = nWZerrinRegionList

    tablenumbers['Fitted_top_WZ_events'] = nTopWZinRegionList
    tablenumbers['Fitted_top_WZ_events_err'] = nTopWZerrinRegionList

    ###
    return tablenumbers