def doNeyman(responseMatrix,comphep1000Matrix,comphep1100Matrix,comphep0100Matrix,N): result=[] comphep1000Gen=comphep1000Matrix.ProjectionX("comphep1000Gen") comphep1100Gen=comphep1100Matrix.ProjectionX("comphep1100Gen") comphep0100Gen=comphep0100Matrix.ProjectionX("comphep0100Gen") comphep1000Reco=comphep1000Matrix.ProjectionY("comphep1000Reco") comphep1100Reco=comphep1100Matrix.ProjectionY("comphep1100Reco") comphep0100Reco=comphep0100Matrix.ProjectionY("comphep0100Reco") ''' cv=ROOT.TCanvas("cv","",800,600) cv.Divide(1,2) cv.cd(1) comphep1000Gen.Draw() comphep1100Gen.Draw("Same") comphep0100Gen.Draw("Same") cv.cd(2) comphep1000Reco.Draw() comphep1100Reco.Draw("Same") comphep0100Reco.Draw("Same") cv.Update() cv.WaitPrimitive() ''' tmHistinverted=u2b.readResponseFromHistogramAndInvert(responseMatrix) for i in range(N): vl=1.0 vr=1.0*i/(N-1) genDist=generateWeightedHist(comphep1000Gen,comphep1100Gen,comphep0100Gen,vl,vr) recoDist=generateWeightedHist(comphep1000Reco,comphep1100Reco,comphep0100Reco,vl,vr) measured = u2b.DataDistribution.createFromHistogram(recoDist,True,False,0.0) unfolded= u2b.CompoundDistribution(u2b.Unfolding(tmHistinverted),measured) asymmetry=u2b.CompoundDistribution(u2b.Asymmetry(),unfolded) result.append({ "vl":vl, "vr":vr, "generated":calcAsymmetry(genDist), "unfolded":asymmetry.getMean(0), "uncertainty":asymmetry.getUncertainty(0), "bias":math.fabs(calcAsymmetry(genDist)-asymmetry.getMean(0)) }) return result
def doUnfolding( histFiles, signalHistName, backgroundHistNames, dataHistNames, responseFiles, responseHistName, fitResult, systematic="nominal", useStatUnc=True, useMCStatUnc=True, useFitUnc=True, ): measured = None if options.mc_only: # fake data by adding up all nominal histograms fakeData = None for histName in fitResult.keys(): if histName == signalHistName or histName in backgroundHistNames: if fakeData == None: fakeData = readHist1d( histFiles, HISTPREFIX + histName, "nominal", fitResult[histName]["scale"], REBIN_RECO ) else: fakeData.Add( readHist1d( histFiles, HISTPREFIX + histName, "nominal", fitResult[histName]["scale"], REBIN_RECO ) ) measured = u2b.DataDistribution.createFromHistogram(fakeData, useStatUnc, False, 0.0) else: # read data histograms data = None for histName in dataHistNames: if data == None: data = readHist1d(histFiles, HISTPREFIX + histName, "nominal", 1, REBIN_RECO) measured = u2b.DataDistribution.createFromHistogram(data, useStatUnc, False, 0.0) # TODO: decorrelate before subtracting - or define correlation between bgs for histName in fitResult.keys(): if histName in backgroundHistNames: backgroundHist = readHist1d( histFiles, HISTPREFIX + histName, systematic, fitResult[histName]["scale"], REBIN_RECO ) backgroundDist = u2b.DataDistribution.createFromHistogram( backgroundHist, False, useMCStatUnc, fitResult[histName]["uncertainty"] if useFitUnc else 0.0 ) # subtract background from measured data measured = u2b.CompoundDistribution(u2b.Subtraction(), measured, backgroundDist) tmHist = readHist2d( responseFiles, responseHistName, systematic, fitResult[signalHistName]["scale"], REBIN_GEN, REBIN_RECO ) tmHistinverted = u2b.readResponseFromHistogramAndInvert(tmHist) if verbose: print "measured after bg subtraction:" print " ... m1=", measured.getMean(0), " +- ", measured.getUncertainty(0) print " ... m1=", measured.getMeanSampled(0), " +- ", measured.getUncertaintySampled(0), " (toys)" print " ... m2=", measured.getMean(1), " +- ", measured.getUncertainty(1) print " ... m1=", measured.getMeanSampled(1), " +- ", measured.getUncertaintySampled(1), " (toys)" print " ... rho=", measured.getCorrelation(0, 1) print " ... rho=", measured.getCorrelationSampled(0, 1), " (toys)" print unfolded = u2b.CompoundDistribution(u2b.Unfolding(tmHistinverted), measured) if verbose: print "unfolded distribution:" print " ... u1=", unfolded.getMean(0), " +- ", unfolded.getUncertainty(0) print " ... u1=", unfolded.getMeanSampled(0), " +- ", unfolded.getUncertaintySampled(0), " (toys)" print " ... u2=", unfolded.getMean(1), " +- ", unfolded.getUncertainty(1) print " ... u2=", unfolded.getMeanSampled(1), " +- ", unfolded.getUncertaintySampled(1), " (toys)" print " ... rho=", unfolded.getCorrelation(0, 1) print " ... rho=", unfolded.getCorrelationSampled(0, 1), " (toys)" print # calculate asymmetry asymmetry = u2b.CompoundDistribution(u2b.Asymmetry(), unfolded) if verbose: print "final result:" print " ... A=", asymmetry.getMean(0), " +- ", asymmetry.getUncertainty(0) return {"mean": asymmetry.getMean(0), "uncertainty": asymmetry.getUncertainty(0)}