コード例 #1
0
ファイル: DataMoments.py プロジェクト: GerhardRaven/P2VV
def normalizeMoments( momentsFileOrig, momentsFileNorm, normMoment = 'Re_ang_A0_A0', printMoms = True ):
    # moment containers
    funcNames = [ ]
    moments = { }
    correlations = { }
    
    # read moments
    print 'input moments:'
    from P2VV.Utilities.DataMoments import readMoments, printMoments
    readMoments( momentsFileOrig, BasisFuncNames = funcNames, Moments = moments, Correlations = correlations, ProcessAll = True )
    if printMoms: printMoments( BasisFuncNames = funcNames, Moments = moments, Correlations = correlations )
    
    # calculate moments with new normalization
    from math import sqrt
    valNorm = moments[normMoment][0]
    errNorm = moments[normMoment][1]
    momsNew  = { }
    corrsNew = { }
    # moments value and error
    for name in funcNames :
        valNew   = moments[name][0] / valNorm
        err      = moments[name][1]
        corrNorm = correlations[name][normMoment]
        varNew   = ( err**2 + valNew**2 * errNorm**2 - 2. * valNew * err * errNorm * corrNorm ) / valNorm**2
        momsNew[name] = ( valNew, sqrt(varNew), ( sqrt( valNew**2 / varNew ) ) if varNew != 0. else 0. if valNew == 0. else float('inf') )
        corrsNew[name] = { }
    
    # moments correlations
    for name0 in funcNames :
        for name1 in funcNames :
            valsNew   = ( momsNew[name0][0], momsNew[name1][0] )
            errsNew   = ( momsNew[name0][1], momsNew[name1][1] )
            errs      = ( moments[name0][1], moments[name1][1] )
            corr01    = correlations[name0][name1]
            corr0Norm = correlations[name0][normMoment]
            corr1Norm = correlations[name1][normMoment]
            corr01New = ( errs[0] * errs[1] * corr01 + valsNew[0] * valsNew[1] * errNorm**2\
                         - valsNew[1] * errs[0] * errNorm * corr0Norm - valsNew[0] * errs[1] * errNorm * corr1Norm ) / valNorm**2
            if abs( errsNew[0] ) > 1.e-10 and abs( errsNew[1] ) > 1.e-10 :
                corr01New /= errsNew[0] * errsNew[1]
            elif abs( corr01New ) < 1.e-10 and name0 == normMoment and name1 == normMoment :
                corr01New = 1.
            elif abs( corr01New ) < 1.e-10 and ( name0 == normMoment or name1 == normMoment ) :
                corr01New = 0.
            else :
                corr01New = -999.
            corrsNew[name0][name1] = corr01New
            corrsNew[name1][name0] = corr01New
    
    print 'output moments:'
    from P2VV.Utilities.DataMoments import writeMoments
    writeMoments( momentsFileNorm, BasisFuncNames = funcNames, Moments = momsNew, Correlations = corrsNew )
    if printMoms: printMoments( BasisFuncNames = funcNames, Moments = momsNew, Correlations = corrsNew )
コード例 #2
0
ファイル: DataMoments.py プロジェクト: GerhardRaven/P2VV
 def read( self, filePath = 'moments', **kwargs ) :
     readMoments( filePath, BasisFuncNames = self._basisFuncNames, Moments = self._coefficients, Correlations = self._correlations,
                 **kwargs )
     self._covMatrixSize = len(self._correlations)
コード例 #3
0
ファイル: DataMoments.py プロジェクト: GerhardRaven/P2VV
def combineMoments( momentsFilesIn, momentsFileOut, prefix = '', printMoms = True):
    funcNames = [ 'Re_ang_A0_A0', 'Re_ang_Apar_Apar', 'Re_ang_Aperp_Aperp'
                 , 'Im_ang_Apar_Aperp', 'Re_ang_A0_Apar', 'Im_ang_A0_Aperp'
                 , 'Re_ang_AS_AS', 'Re_ang_Apar_AS', 'Im_ang_Aperp_AS', 'Re_ang_A0_AS' ]
    
    if prefix:
        funcNamesWPF = []
        for name in funcNames: funcNamesWPF += [prefix + '_' + name]
        funcNames =  funcNamesWPF

    # moment containers
    moments = [ ]
    correlations = [ ]
    
    # read moments
    from P2VV.Utilities.DataMoments import readMoments, printMoments
    for momsFile in momentsFilesIn :
        moments.append( { } )
        correlations.append( { } )
        readMoments( momsFile, BasisFuncNames = funcNames, Moments = moments[-1], Correlations = correlations[-1] )
        if printMoms: printMoments( BasisFuncNames = funcNames, Moments = moments[-1], Correlations = correlations[-1] )
    
    # combine moments
    from ROOT import TMatrixD
    combNames = [ name for name in funcNames if moments[0][name][1] > 0. ]
    momVec    = TMatrixD( len(combNames), 1              )
    covMat    = TMatrixD( len(combNames), len(combNames) )
    for moms, corrs in zip( moments, correlations ) :
        mom = TMatrixD( len(combNames), 1              )
        cov = TMatrixD( len(combNames), len(combNames) )
        for it1, name1 in enumerate(combNames) :
            mom[it1][0] = moms[name1][0]
            for it2, name2 in enumerate(combNames) :
                cov[it1][it2] = moms[name1][1] * moms[name2][1] * corrs[name1][name2]
        cov.Invert()
        momVec += cov * mom
        covMat += cov
    covMat.Invert()
    momVec = covMat * momVec
    
    momsNew  = { }
    corrsNew = { }
    for name in funcNames :
        if moments[0][name][1] > 0. : continue
        momsNew[name]  = moments[0][name]
        corrsNew[name] = correlations[0][name]
    
    from math import sqrt
    for it1, name1 in enumerate(combNames) :
        val = momVec[it1][0]
        err = sqrt( covMat[it1][it1] )
        momsNew[name1]  = ( val, err, abs( val / err ) )
        corrsNew[name1] = { }
        for name in funcNames :
            if moments[0][name][1] > 0. : continue
            corrsNew[name1][name] = correlations[0][name1][name]
        for it2, name2 in enumerate(combNames) :
            corrsNew[name1][name2] = covMat[it1][it2] / err / sqrt( covMat[it2][it2] )
    
    # write combined moments to file
    from P2VV.Utilities.DataMoments import writeMoments
    writeMoments( momentsFileOut, BasisFuncNames = funcNames, Moments = momsNew, Correlations = corrsNew )
    if printMoms: printMoments( BasisFuncNames = funcNames, Moments = momsNew, Correlations = corrsNew )
コード例 #4
0
ファイル: compareMoments.py プロジェクト: GerhardRaven/P2VV
funcNames = [ 'Re_ang_A0_A0', 'Re_ang_Apar_Apar', 'Re_ang_Aperp_Aperp'
             , 'Im_ang_Apar_Aperp', 'Re_ang_A0_Apar', 'Im_ang_A0_Aperp'
             , 'Re_ang_AS_AS', 'Re_ang_Apar_AS', 'Im_ang_Aperp_AS', 'Re_ang_A0_AS' ]

latex = True

# moment containers
moments1 = { }
moments2 = { }
correlations1 = { }
correlations2 = { }

# read moments
print 'input moments 1:'
from P2VV.Utilities.DataMoments import readMoments, printMoments
readMoments( momentsFile1, BasisFuncNames = funcNames, Moments = moments1, Correlations = correlations1 )
printMoments( BasisFuncNames = funcNames, Moments = moments1, Correlations = correlations1 )

print 'input moments 2:'
from P2VV.Utilities.DataMoments import readMoments, printMoments
readMoments( momentsFile2, BasisFuncNames = funcNames, Moments = moments2, Correlations = correlations2 )
printMoments( BasisFuncNames = funcNames, Moments = moments2, Correlations = correlations2 )

from ROOT import TMatrixD
diffInd   = 0
diffNames = [ name for name in funcNames if moments1[name][1] != 0. ]
diffs     = TMatrixD( len(diffNames), 1 )
diffCovs  = TMatrixD( len(diffNames), len(diffNames) )

from math import sqrt
print 'differences:'
コード例 #5
0
ファイル: normalizeMoments.py プロジェクト: GerhardRaven/P2VV
# script parameters
momentsFileOrig = 'Sim08_hel_UB_UT_trueTime_BkgCat050_KK30_Phys'
momentsFileNorm = 'Sim08_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_norm'
normMoment = 'Re_ang_A0_A0'

# moment containers
funcNames = [ ]
moments = { }
correlations = { }

# read moments
print 'input moments:'
from P2VV.Utilities.DataMoments import readMoments, printMoments
readMoments( momentsFileOrig, BasisFuncNames = funcNames, Moments = moments, Correlations = correlations, ProcessAll = True )
printMoments( BasisFuncNames = funcNames, Moments = moments, Correlations = correlations )

# calculate moments with new normalization
from math import sqrt
valNorm = moments[normMoment][0]
errNorm = moments[normMoment][1]
momsNew  = { }
corrsNew = { }
# moments value and error
for name in funcNames :
    valNew   = moments[name][0] / valNorm
    err      = moments[name][1]
    corrNorm = correlations[name][normMoment]
    varNew   = ( err**2 + valNew**2 * errNorm**2 - 2. * valNew * err * errNorm * corrNorm ) / valNorm**2
    momsNew[name] = ( valNew, sqrt(varNew), ( sqrt( valNew**2 / varNew ) ) if varNew != 0. else 0. if valNew == 0. else float('inf') )
    corrsNew[name] = { }
コード例 #6
0
ファイル: iterativeAngAcc.py プロジェクト: GerhardRaven/P2VV
                  'Sim08_{0}_{1}_Phys_norm_{2}'.format(MCProd,outputEffMomentsBaselineName,iterNumb),
                  normMoment = PhysicsReweight.getParNamePrefix() + '_Re_ang_A0_A0',
                  printMoms  = delIntermediateMoms
                  )
if delIntermediateMoms: os.remove('Sim08_{0}_{1}_Phys_{2}'.format(MCProd,outputEffMomentsBaselineName,iterNumb) )

# combine 2011, 2012 acceptances
if combineEffMoments:
    angAcc2011  = 'Sim08_2011_%s_Phys_norm_%s'%(outputEffMomentsBaselineName,iterNumb) 
    angAcc2012  = 'Sim08_2012_%s_Phys_norm_%s'%(outputEffMomentsBaselineName,iterNumb) 
    combAccName = 'Sim08_20112012_{0}_Phys_norm_{1}'.format(outputEffMomentsBaselineName,iterNumb)
    combineMoments( angAcc2011, angAcc2012, combAccName, Prefix = 'mc', delete=False )
    
# convert effyciency weights to efficiency moments
    moments, correlations = {}, {}
    readMoments( combAccName, BasisFuncNames = [], Moments = moments, Correlations = correlations, ProcessAll = True )    
    convertEffWeightsToMoments( moments, OutputFilePath = combAccName.replace('Phys','weights').replace('_norm',''),
                                WeightNamesPrefix = PhysicsReweight.getParNamePrefix(),
                                # PrintMoments      = False
                                )
  
# plot data after each reweighting step
if makePlots: 
    from P2VV.Utilities.MCReweighting import plotingScenarios, weightNamesDataKeysMap
    plot = True
    plotScenarioKey = options.rewSteps.replace('_','') if not mkkBmom2DRew else 'TwoD' + options.rewSteps.replace('_','') 
    try: case = plotingScenarios[plotScenarioKey]
    except KeyError: plot = False         
    if plot:
        for dataSetKeys in case:
            dataSets = {}
コード例 #7
0
ファイル: combineMoments.py プロジェクト: GerhardRaven/P2VV
momentsFilesIn = [ 'Sim08_2011_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_norm', 'Sim08_2012_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_norm' ]
momentsFileOut = 'Sim08_20112012_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_norm'
funcNames = [ 'Re_ang_A0_A0', 'Re_ang_Apar_Apar', 'Re_ang_Aperp_Aperp'
             , 'Im_ang_Apar_Aperp', 'Re_ang_A0_Apar', 'Im_ang_A0_Aperp'
             , 'Re_ang_AS_AS', 'Re_ang_Apar_AS', 'Im_ang_Aperp_AS', 'Re_ang_A0_AS' ]

# moment containers
moments = [ ]
correlations = [ ]

# read moments
from P2VV.Utilities.DataMoments import readMoments, printMoments
for momsFile in momentsFilesIn :
    moments.append( { } )
    correlations.append( { } )
    readMoments( momsFile, BasisFuncNames = funcNames, Moments = moments[-1], Correlations = correlations[-1] )
    printMoments( BasisFuncNames = funcNames, Moments = moments[-1], Correlations = correlations[-1] )

# combine moments
from ROOT import TMatrixD
combNames = [ name for name in funcNames if moments[0][name][1] > 0. ]
momVec    = TMatrixD( len(combNames), 1              )
covMat    = TMatrixD( len(combNames), len(combNames) )
for moms, corrs in zip( moments, correlations ) :
    mom = TMatrixD( len(combNames), 1              )
    cov = TMatrixD( len(combNames), len(combNames) )
    for it1, name1 in enumerate(combNames) :
        mom[it1][0] = moms[name1][0]
        for it2, name2 in enumerate(combNames) :
            cov[it1][it2] = moms[name1][1] * moms[name2][1] * corrs[name1][name2]
    cov.Invert()
コード例 #8
0
# script parameters
weightsFile = 'Sim08_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_norm'
momentsFile = 'Sim08_hel_UB_UT_trueTime_BkgCat050_KK30_Phys_moms_norm'
funcNames = [ 'Re_ang_A0_A0', 'Re_ang_Apar_Apar', 'Re_ang_Aperp_Aperp'
             , 'Im_ang_Apar_Aperp', 'Re_ang_A0_Apar', 'Im_ang_A0_Aperp'
             , 'Re_ang_AS_AS', 'Re_ang_Apar_AS', 'Im_ang_Aperp_AS', 'Re_ang_A0_AS' ]

# read weights
from P2VV.Utilities.DataMoments import readMoments, printMoments
weights = { }
weightCorrs = { }
readMoments( weightsFile, BasisFuncNames = funcNames, Moments = weights, Correlations = weightCorrs )
printMoments( BasisFuncNames = funcNames, Moments = weights, Correlations = weightCorrs )

# convert weights to moments
from P2VV.Utilities.DataMoments import convertEffWeightsToMoments
convertEffWeightsToMoments( Weights = weights, OutputFilePath = momentsFile )