def CATSplit(card): dcIn = cardTools(card) CATS=dcIn.content['block1']['bin'] print CATS print dcIn.content['block2'] for iBin in dcIn.content['block1']['bin']: TargetCard=card.replace('.txt','_'+iBin+'.txt') print TargetCard dcOut = cardTools(card) for jBin in CATS: if not iBin == jBin : pkBin = -1 for kBin in dcIn.content['block2']['bin']: pkBin += 1 if kBin == jBin : print 'remove' , dcIn.content['block2']['bin'][pkBin],dcIn.content['block2']['process'][pkBin] dcOut.remCol(bin=kBin,process=dcIn.content['block2']['process'][pkBin]) for iSysType in dcIn.content['systs']: for iSyst in dcIn.content['systs'][iSysType]: if jBin in iSyst : if iSyst in dcOut.content['systs'][iSysType]: dcOut.remSystLine(systtype=iSysType,tag=iSyst) dcOut.content['header1']['jmax'] = '*' dcOut.content['header1']['imax'] = '1' os.system('rm '+TargetCard) dcOut.write(TargetCard)
def ChangeModel(iChannel, iEnergy, iMass, card, iAltModel): if 'altmodel' in channels[ options.Version][iChannel][iEnergy] and iAltModel in channels[ options.Version][iChannel][iEnergy]['altmodel']: dcIn = cardTools(card) dcOut = cardTools(card) cardOut = card.replace('.txt', '_' + iAltModel + '.txt') print channels[ options.Version][iChannel][iEnergy]['altmodel'][iAltModel] for iProc in channels[ options.Version][iChannel][iEnergy]['altmodel'][iAltModel]: print iProc Val = channels[options.Version][iChannel][iEnergy]['altmodel'][ iAltModel][iProc] print Val dcOut.remShape(iProc, Val[0]) dcOut.addShape(iProc, Val[0].replace('$MASS', str(iMass)), Val[1].replace('$MASS', str(iMass)), Val[2].replace('$MASS', str(iMass))) os.system('rm ' + cardOut) dcOut.write(cardOut) return cardOut
def RenameSyst(card,renameSyst): dcIn = cardTools(card) dcOut = cardTools(card) print dcIn.content['block2']['bin'] TargetCard = os.path.split(card)[0] + '/' + (os.path.split(card)[1]) + '_rSyst' # Set Systematic names for iType in dcIn.content['systs'] : for iSyst in dcIn.content['systs'][iType]: for iRename in renameSyst: oSyst = iRename.split(':')[0] tSyst = iRename.split(':')[1] if iSyst == oSyst: dcOut.content['systs'][iType][tSyst] = dc(dcIn.content['systs'][iType][iSyst]) dcOut.linenumbers[(iType,tSyst)] = dcOut.linenumbers[(iType,iSyst)] del dcOut.content['systs'][iType][iSyst] del dcOut.linenumbers[(iType,iSyst)] # Rename histograms shFiles = [] for iEntry in dcIn.content['header2'] : if dcIn.content['header2'][iEntry][0] == 'shapes' and dcIn.content['header2'][iEntry][1] != 'data_obs' and not ':' in dcIn.content['header2'][iEntry][3]: shFiles.append(dcIn.content['header2'][iEntry][3]) shFiles = list(set(shFiles)) print shFiles for iFile in shFiles: fileInName = os.path.split(card)[0]+'/'+iFile fileOutName = fileInName + '_rSyst' print fileInName print fileOutName fIn = TFile.Open(fileInName,'READ') fOut = TFile.Open(fileOutName,'RECREATE') Hists = [X.GetName() for X in fIn.GetListOfKeys()] for iHist in Hists : H = fIn.Get(iHist) if len(iHist.split('_')) > 2 : iSyst = '_'.join(iHist.split('_')[2:-1])+'_'+iHist.split('_')[-1].replace('Up','').replace('Down','') for iRename in renameSyst: oSyst = iRename.split(':')[0] tSyst = iRename.split(':')[1] if iSyst == oSyst: #print H , H.GetTitle() H.SetName(iHist.replace(oSyst,tSyst)) H.SetTitle(H.GetTitle().replace(oSyst,tSyst)) #print H , H.GetTitle() fOut.cd() H.Write() fIn.Close() fOut.Close() keepFile = os.path.split(card)[0]+'/'+iFile + '_rKeep' os.system('mv '+fileInName+' '+keepFile) os.system('mv '+fileOutName+' '+fileInName) os.system('rm '+TargetCard) dcOut.write(TargetCard) keepCard = os.path.split(card)[0] + '/' + (os.path.split(card)[1]) + '_rKeep' os.system('mv '+card+' '+keepCard) os.system('mv '+TargetCard+' '+card)
def PDFSplit(iChannel,iEnergy,card,TargetCard,iMass): dcIn = cardTools(card) dcOut = cardTools(card) if channels[options.Version][iChannel][iEnergy]['tag'] == 'vbfbb': print dcIn.content['header1'] dcOut.remShape('background','*') dcOut.addShape('bkg_qcd' ,'*','CMS_vbfbb_data_workspace.root','w:bkg_$CHANNEL') dcOut.addShape('bkg_Z','*','CMS_vbfbb_data_workspace.root','w:CMS_vbfbb_Z_model_$CHANNEL') dcOut.addShape('bkg_top' ,'*','CMS_vbfbb_data_workspace.root','w:Top_model_$CHANNEL') #print '----' #print dcOut.content['header2'] print dcIn.content['block2'] print dcIn.content['systs'] dcOut.remCol(process='background') dcOut.addSystLine(systtype='lnN',tag='CMS_vbfbb_bkg_Z_norm') dcOut.addSystLine(systtype='lnN',tag='CMS_vbfbb_bkg_Top_norm') for iBin in xrange(0 , len( dcIn.content['block2']['bin'] ) ): jBin=dcIn.content['block2']['bin'][iBin] iProc=dcIn.content['block2']['process'][iBin] if iProc == 'background' : YieldIn= float(dcIn.getRate(bin=jBin,process=iProc)) print jBin,iProc, YieldIn sZjet = dcIn.content['systs']['param']['CMS_vbfbb_nzjet_'+jBin] sTop = dcIn.content['systs']['param']['CMS_vbfbb_ntop_'+jBin] dcOut.remSystLine(systtype='param',tag='CMS_vbfbb_nzjet_'+jBin) dcOut.remSystLine(systtype='param',tag='CMS_vbfbb_ntop_'+jBin) nQCD = YieldIn - float(sTop[0]) - float(sZjet[0]) dcOut.addCol(jBin,'bkg_qcd' ,'1',str(nQCD),'-1') dcOut.addCol(jBin,'bkg_Z','2',sZjet[0] ,'-1') dcOut.addCol(jBin,'bkg_top' ,'3',sTop[0] ,'-1') dcOut.addSystLine(systtype='lnU',tag='CMS_vbfbb_nqcd_'+jBin) #dcOut.addSystLine(systtype='lnN',tag='CMS_vbfbb_nzjet_'+jBin) #dcOut.addSystLine(systtype='lnN',tag='CMS_vbfbb_ntop_'+jBin) Zsyst=1.+round(float(sZjet[1])/float(sZjet[0]),3) Tsyst=1.+round(float(sTop[1])/float(sTop[0]),3) dcOut.setSyst(bin=jBin, processId='1', syst='lnU', tag='CMS_vbfbb_nqcd_'+jBin ,value='1.05') #dcOut.setSyst(bin=jBin, processId='2', syst='lnN', tag='CMS_vbfbb_nzjet_'+jBin ,value=str(Zsyst)) #dcOut.setSyst(bin=jBin, processId='3', syst='lnN', tag='CMS_vbfbb_ntop_'+jBin ,value=str(Tsyst)) dcOut.setSyst(bin=jBin, processId='2', syst='lnN', tag='CMS_vbfbb_bkg_Z_norm' ,value=str(Zsyst)) dcOut.setSyst(bin=jBin, processId='3', syst='lnN', tag='CMS_vbfbb_bkg_Top_norm' ,value=str(Tsyst)) dcOut.content['header1']['jmax'] = '*' dcOut.content['header1']['imax'] = '*' print dcOut.content['systs'] else: print 'PDFSplit UNDEFINED for channel : ' , channels[options.Version][iChannel][iEnergy]['tag'] os.system('rm '+TargetCard) dcOut.write(TargetCard)
def RenormProc(card,renormProc): dcIn = cardTools(card) dcOut = cardTools(card) print dcIn.content['block2']['bin'] TargetCard = os.path.split(card)[0] + '_rProc/' + (os.path.split(card)[1]) os.system('mkdir -p '+os.path.split(card)[0] + '_rProc') # Scale Yields for iBin in xrange(0 , len( dcIn.content['block2']['bin'] ) ): jBin=dcIn.content['block2']['bin'][iBin] iProc=dcIn.content['block2']['process'][iBin] for iRenorm in renormProc: pRenorm = iRenorm.split(':')[0] fRenorm = float(iRenorm.split(':')[1]) if pRenorm == iProc : YieldIn = float(dcIn.getRate(bin=jBin,process=iProc)) YieldOut = YieldIn*fRenorm print iProc, YieldIn , '--> ' , YieldOut dcOut.setRate(bin=jBin,process=iProc,value=YieldOut) # Histograms shFiles = [] for iEntry in dcIn.content['header2'] : if dcIn.content['header2'][iEntry][0] == 'shapes' and dcIn.content['header2'][iEntry][1] != 'data_obs' and not ':' in dcIn.content['header2'][iEntry][3]: shFiles.append(dcIn.content['header2'][iEntry][3]) shFiles = list(set(shFiles)) print shFiles for iFile in shFiles: fileInName = os.path.split(card)[0]+'/'+iFile fileOutName = os.path.split(card)[0] + '_rProc/' +iFile print fileInName print fileOutName fIn = TFile.Open(fileInName,'READ') fOut = TFile.Open(fileOutName,'RECREATE') Hists = [X.GetName() for X in fIn.GetListOfKeys()] for iHist in Hists : H = fIn.Get(iHist) for iRenorm in renormProc: pRenorm = iRenorm.split(':')[0] fRenorm = float(iRenorm.split(':')[1]) if pRenorm in iHist : H.Scale(fRenorm) #print H , H.GetTitle() fOut.cd() H.Write() fIn.Close() fOut.Close() os.system('rm '+TargetCard) dcOut.write(TargetCard)
def WJetFix(cardIn,cardForWJ): if not iChannel in preProc['WJetFix']['ChannelList'] : return cardIn cardWJ =cardForWJ.replace(cardDir,'couplings').replace(str(iMass),(preProc['WJetFix']['massWJ']).replace('$MASS',str(iMass))).replace('_hsm','') cardOut=cardIn.replace(os.path.splitext(cardIn)[1],'_WJetFix'+os.path.splitext(cardIn)[1] ) print 'WJetFix IN :', cardIn print 'WJetFix WJet:', cardWJ print 'WJetFix OUT :', cardOut dcIn = cardTools(cardIn) dcWJ = cardTools(cardWJ) for iBin in dcIn.content['block1']['bin']: ProcList=[] iPosProc=-1 for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc=dcIn.content['block2']['process'][iPosProc] ProcList.append(Proc) for iProc in preProc['WJetFix']['processList'] : if iProc in ProcList: #print iProc dcIn.setRate(bin=iBin,process=iProc,value=dcWJ.getCol(bin=iBin,process=iProc)['block2']['rate']) for iSystType in dcIn.content['systs']: for iSyst in dcIn.content['systs'][iSystType]: dcIn.setSyst(bin=iBin,process=iProc,syst=iSystType, tag=iSyst , value='-') for iSystType in dcWJ.content['systs']: if iSystType in dcIn.content['systs']: for iSyst in dcWJ.content['systs'][iSystType]: if iSyst in dcIn.content['systs'][iSystType]: dcIn.setSyst(bin=iBin,process=iProc,syst=iSystType, tag=iSyst , value= dcWJ.getSyst(bin=iBin,process=iProc,syst=iSystType, tag=iSyst)) #print len(dcIn.content['header2']) , '-->' , dcIn.content['header2'] if len(dcIn.content['header2']) > 0: for iShape in dcWJ.content['header2']: #print dcWJ.content['header2'][iShape][1] if dcWJ.content['header2'][iShape][1] == '*' or dcWJ.content['header2'][iShape][1] == 'iProc': #print 'Replace' Shape=dcWJ.content['header2'][iShape] #print cardDir FileWJ=cardWJ.replace(cardWJ.split('/')[-1],Shape[3]) if len(Shape) == 5 : dcIn.addShape(iProc,Shape[2],FileWJ,Shape[4]) if len(Shape) == 6 : dcIn.addShape(iProc,Shape[2],FileWJ,Shape[4],Shape[5]) #print len(dcIn.content['header2']) , '-->' , dcIn.content['header2'] os.system('rm '+cardOut) dcIn.write(cardOut) return cardOut
def KillqqHALT(card): print 'Kill qqH in ', card dcOut = cardTools(card) cardOut = card.replace('.txt', '_noqqHALT.txt') dcOut.remCol(process='qqbarH_ALT') os.system('rm ' + cardOut) dcOut.write(cardOut) return cardOut
def KillqqHALT(card): print 'Kill qqH in ',card dcOut = cardTools(card) cardOut=card.replace('.txt','_noqqHALT.txt') dcOut.remCol(process='qqbarH_ALT') os.system('rm '+cardOut) dcOut.write(cardOut) return cardOut
def ChangeModel(iChannel,iEnergy,iMass,card,iAltModel): if 'altmodel' in channels[options.Version][iChannel][iEnergy] and iAltModel in channels[options.Version][iChannel][iEnergy]['altmodel']: dcIn = cardTools(card) dcOut = cardTools(card) cardOut=card.replace('.txt','_'+iAltModel+'.txt') print channels[options.Version][iChannel][iEnergy]['altmodel'][iAltModel] for iProc in channels[options.Version][iChannel][iEnergy]['altmodel'][iAltModel]: print iProc Val=channels[options.Version][iChannel][iEnergy]['altmodel'][iAltModel][iProc] print Val dcOut.remShape(iProc,Val[0]) dcOut.addShape(iProc,Val[0].replace('$MASS',str(iMass)) ,Val[1].replace('$MASS',str(iMass)) ,Val[2].replace('$MASS',str(iMass)) ) os.system('rm '+cardOut) dcOut.write(cardOut) return cardOut
from manipDataCard import card as cardTools from copy import deepcopy TargetDir = '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1hzzlike/' Config = { 'hwwof_0j_7TeV.txt' : { '1p' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/' , '1m' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' } , 'hwwof_1j_7TeV.txt' : { '1p' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/' , '1m' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' } , 'hwwof_0j_8TeV.txt' : { '1p' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/' , '1m' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' } , 'hwwof_1j_8TeV.txt' : { '1p' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/' , '1m' : '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' } } for iComb in Config: dc1p = cardTools(Config[iComb]['1p']+iComb) dc1m = cardTools(Config[iComb]['1m']+iComb) dcOut = cardTools(Config[iComb]['1m']+iComb) # Copy 1p to ggH_ALT2 dcOut.addCol( dc1p.content['block2']['bin'][1] , 'ggH_ALT2' , -6 , dc1p.content['block2']['rate'][1] , dc1p.content['block1']['observation'][0] ) #print dcOut.content['block1'] #print dcOut.content['block2'] # Set systematics for iType in dc1p.content['systs'] : #print iType for iSyst in dc1p.content['systs'][iType]: #print iSyst, dc1p.getSyst( bin=dc1p.content['block2']['bin'][1] , process='ggH_ALT' , syst=iType , tag=iSyst ) if not 'StatBound' in iSyst: dcOut.setSyst( bin=dc1p.content['block2']['bin'][1] , process='ggH_ALT2' , syst=iType , tag=iSyst , value=dc1p.getSyst( bin=dc1p.content['block2']['bin'][1] , process='ggH_ALT' , syst=iType , tag=iSyst ) )
def SMToys(cardIn): dirName=os.path.dirname(cardIn) cardOut=cardIn.replace(os.path.splitext(cardIn)[1],'_SMToys'+os.path.splitext(cardIn)[1] ) dcIn = cardTools(cardIn) dcOut = cardTools(cardIn) for iBin in dcIn.content['block1']['bin']: iPosProc=-1 INJList=[] SMXList=[] ShapeProc={} # Copy rate and syst from *_SM to Signal for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc=dcIn.content['block2']['process'][iPosProc] PrId=dcIn.content['block2']['processId'][iPosProc] if iBin == jBin and '_SM' in Proc: INJList.append(Proc) if iBin == jBin and int(PrId) <= 0: SMXList.append(Proc) for iProc in SMXList: #print iProc if iProc+'_SM' in INJList: jProc=iProc+'_SM' dcOut.setRate(bin=iBin,process=iProc,value=dcIn.getCol(bin=iBin,process=jProc)['block2']['rate']) SkipList=[] for iSystType in dcOut.content['systs']: for iSyst in dcOut.content['systs'][iSystType]: dcOut.setSyst(bin=iBin,process=iProc,syst=iSystType, tag=iSyst , value='-') if '_SM' in iSyst : jSyst=iSyst.replace('_SM','') SkipList.append(jSyst) for iSystType in dcIn.content['systs']: for iSyst in dcIn.content['systs'][iSystType]: jSyst=iSyst if '_SM' in iSyst and iSystType != 'shape' and iSystType != 'shapeN2' : jSyst=iSyst.replace('_SM','') #print iSyst , '->' , jSyst , iSystType if jSyst in dcOut.content['systs'][iSystType] and iSyst not in SkipList : dcOut.setSyst(bin=iBin,process=iProc,syst=iSystType, tag=jSyst , value= dcIn.getSyst(bin=iBin,process=jProc,syst=iSystType, tag=iSyst)) if len(dcIn.content['header2']) > 0: dcOut.remShape(jProc) for iShape in dcIn.content['header2']: if ( dcIn.content['header2'][iShape][1] == '*' or dcIn.content['header2'][iShape][1] == jProc ) and ( dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin ) : if iProc not in ShapeProc: ShapeProc[iProc] = deepcopy(dcIn.content['header2'][iShape]) else: if ShapeProc[iProc][1] == '*' or ShapeProc[iProc][2] == '*' : ShapeProc[iProc] = deepcopy(dcIn.content['header2'][iShape]) else: sys.exit('ERROR: Missing process to inject : '+iProc) # Add Shape lines if needed for sProc in ShapeProc: ShapeProc[sProc][1] = sProc ShapeProc[sProc][2] = iBin ShapeProc[sProc][4] = ShapeProc[sProc][4].replace('$PROCESS',sProc+'_SM') ShapeProc[sProc][5] = ShapeProc[sProc][5].replace('$PROCESS',sProc+'_SM') dcOut.addShape(sProc,iBin,ShapeProc[sProc][3],ShapeProc[sProc][4],ShapeProc[sProc][5]) # Remove *_SM process for jProc in INJList: dcOut.remCol(bin=iBin,process=jProc) # Fix lnU systematic if 'lnU' in dcOut.content['systs']: print dcOut.content['systs']['lnU'] for iSyst in dcOut.content['systs']['lnU']: for i in xrange(0,len(dcOut.content['systs']['lnU'][iSyst])-1): dcOut.content['systs']['lnU'][iSyst][i] = '-' print dcOut.content['systs']['lnU'] os.system('rm '+cardOut) dcOut.write(cardOut) return cardOut
def ExtrapolateMass(iChannel,iEnergy,card,TargetCard,iMass,iMassExtra): #print 'ExtrapolateMass: ',iChannel,card,TargetCard,iMass,iMassExtra dcIn = cardTools(card) dcOut = cardTools(card) print channels[options.Version][iChannel][iEnergy] # Load YR Data #path = os.environ['CMSSW_BASE']+"/src/HiggsAnalysis/CombinedLimit/data/" path = '/afs/cern.ch/work/x/xjanssen/cms/vbfHbbCards/CMSSW_6_1_1'+'/src/HiggsAnalysis/CombinedLimit/data/' xs_ggH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ggH.txt') xs_qqH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-vbfH.txt') xs_WH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-WH.txt') xs_ZH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ZH.txt') xs_ttH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ttH.txt') br_VV = file2map(path+'lhc-hxswg/sm/br/BR.txt') br_ff = file2map(path+'lhc-hxswg/sm/br/BR1.txt') # Scale Yields for iBin in xrange(0 , len( dcIn.content['block2']['bin'] ) ): jBin=dcIn.content['block2']['bin'][iBin] iProc=dcIn.content['block2']['process'][iBin] # X-section xs_Scale = 1. if dcIn.content['block2']['process'][iBin] == 'ggH' : xs_Scale = GetYRVal(xs_ggH,iMassExtra,'XS_pb')/GetYRVal(xs_ggH,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'qqH' : xs_Scale = GetYRVal(xs_qqH,iMassExtra,'XS_pb')/GetYRVal(xs_qqH,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'WH' : xs_Scale = GetYRVal(xs_WH, iMassExtra,'XS_pb')/GetYRVal(xs_WH ,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ZH' : xs_Scale = GetYRVal(xs_ZH, iMassExtra,'XS_pb')/GetYRVal(xs_ZH ,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ttH' : xs_Scale = GetYRVal(xs_ttH,iMassExtra,'XS_pb')/GetYRVal(xs_ttH,iMass,'XS_pb') # BR br_Scale = 1. if dcIn.content['block2']['process'][iBin] in ['ggH','qqH','WH','ZH','ttH']: if channels[options.Version][iChannel][iEnergy]['tag'] == 'vbfbb' : br_Scale = GetYRVal(br_ffr,iMassExtra,'H_bb')/GetYRVal(br_ff,iMass,'H_bb') if channels[options.Version][iChannel][iEnergy]['branch'] == 'hww' : br_Scale = GetYRVal(br_VV ,iMassExtra,'H_WW')/GetYRVal(br_VV,iMass,'H_WW') else : print 'ERROR: Unknown BR for :', iChannel, channels[options.Version][iChannel][iEnergy]['tag'] exit() YieldIn= float(dcIn.getRate(bin=jBin,process=iProc)) YieldOut= float(dcIn.getRate(bin=jBin,process=iProc))*xs_Scale*br_Scale #print xs_Scale , br_Scale print jBin,iProc,dcIn.getRate(bin=jBin,process=iProc) , '-->' , str(YieldIn*xs_Scale*br_Scale) dcOut.setRate(bin=jBin,process=iProc,value=YieldOut) # Modify Workspace : vbfbb -> change mass of H template if channels[options.Version][iChannel][iEnergy]['tag'] == 'vbfbb': for iEntry in dcIn.content['header2']: if dcIn.content['header2'][iEntry][1] == 'qqH' and (dcIn.content['header2'][iEntry][2] == iBin or dcIn.content['header2'][iEntry][2] == '*') : fName = card.replace(channels[options.Version][iChannel][iEnergy]['card'],dcIn.content['header2'][iEntry][3]) wName = dcIn.content['header2'][iEntry][4].split(':')[0] wFile = TFile.Open(fName,'READ') wTmp = wFile.Get(wName) gROOT.ProcessLine('gROOT->cd()') w = wTmp.Clone() wTmp.Delete() wFile.Close() MassList=[115, 120, 125, 130, 135] MassFit = {} MassExt = {} for iBin in dcIn.content['block1']['bin'] : MassFit[iBin] = { } for jMass in massList: MassFit[iBin][jMass] = w.obj('CMS_vbfbb_mean_m'+str(jMass)+'_'+iBin).getVal() for iBin in dcIn.content['block1']['bin'] : h = TH1F('hMassFit','hMassFit',5,112.5,137.5) for jMass in MassList: h.Fill(jMass, MassFit[iBin][jMass]) pol1=TF1("pol1","[0]+[1]*x",110,140.) fit = h.Fit("pol1","0") p0 = pol1.GetParameter(0); p1 = pol1.GetParameter(1); h.Delete() pol1.Delete() fit.Delete() MassExt[iBin] = round(p0 + p1 * iMassExtra,3) print iMassExtra, MassExt[iBin] for iBin in dcIn.content['block1']['bin'] : w.obj('CMS_vbfbb_mean_m'+str(iMass)+'_'+iBin).setVal(MassExt[iBin]) dcOut.content['systs']['param']['CMS_vbfbb_mean_m'+str(iMass)+'_'+iBin][0] = str(MassExt[iBin]) print 'CMS_vbfbb_mean_m'+str(iMass)+'_'+iBin , ':' , dcIn.content['systs']['param']['CMS_vbfbb_mean_m'+str(iMass)+'_'+iBin] , ' --> ' , dcOut.content['systs']['param']['CMS_vbfbb_mean_m'+str(iMass)+'_'+iBin] fOutName = fName.replace(str(iMass),str(iMassExtra)).replace('.root','_m'+str(iMass)+'Extrapolated.root') fOut = TFile(fOutName,"RECREATE") w.Write() w.Delete() fOut.Close() print fOutName for iEntry in dcOut.content['header2']: if dcOut.content['header2'][iEntry][1] in ['qqH','ggH']: dcOut.content['header2'][iEntry][3] = dcOut.content['header2'][iEntry][3].replace('.root','_m'+str(iMass)+'Extrapolated.root') # Modify Histograms for HWW2l2v if channels[options.Version][iChannel][iEnergy]['branch'] == 'hww' and any(['shapes' in X for X in dcIn.content['header2'].itervalues()]) : shFiles = [] for iEntry in dcIn.content['header2'] : if dcIn.content['header2'][iEntry][0] == 'shapes' and dcIn.content['header2'][iEntry][1] != 'data_obs' and not ':' in dcIn.content['header2'][iEntry][3]: shFiles.append(dcIn.content['header2'][iEntry][3]) shFiles = list(set(shFiles)) print shFiles for iFile in shFiles: fileInName = os.path.split(card)[0]+'/'+iFile fileOutName = os.path.split(TargetCard)[0]+'/'+iFile print fileInName print fileOutName fIn = TFile.Open(fileInName,'READ') fOut = TFile.Open(fileOutName,'RECREATE') Hists = [X.GetName() for X in fIn.GetListOfKeys()] for iHist in Hists : H = fIn.Get(iHist) isHiggs = False if len(iHist.split('_')) == 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : isHiggs = True elif len(iHist.split('_')) > 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] and iHist.split('_')[2] != 'SM' : isHiggs = True if isHiggs : xs_Scale = 1. if iHist.split('_')[1] == 'ggH' : xs_Scale = GetYRVal(xs_ggH,iMassExtra,'XS_pb')/GetYRVal(xs_ggH,iMass,'XS_pb') if iHist.split('_')[1] == 'qqH' : xs_Scale = GetYRVal(xs_qqH,iMassExtra,'XS_pb')/GetYRVal(xs_qqH,iMass,'XS_pb') if iHist.split('_')[1] == 'WH' : xs_Scale = GetYRVal(xs_WH ,iMassExtra,'XS_pb')/GetYRVal(xs_WH ,iMass,'XS_pb') if iHist.split('_')[1] == 'ZH' : xs_Scale = GetYRVal(xs_ZH ,iMassExtra,'XS_pb')/GetYRVal(xs_ZH ,iMass,'XS_pb') if iHist.split('_')[1] == 'ttH' : xs_Scale = GetYRVal(xs_ttH,iMassExtra,'XS_pb')/GetYRVal(xs_ttH,iMass,'XS_pb') #if len(iHist.split('_')) == 2 : # print H.Integral() # print xs_Scale , br_Scale br_Scale = GetYRVal(br_VV,iMassExtra,'H_WW')/GetYRVal(br_VV,iMass,'H_WW') H.Scale(xs_Scale*br_Scale) #if len(iHist.split('_')) == 2 : print H.Integral() fOut.cd() H.Write() fIn.Close() fOut.Close() os.system('rm '+TargetCard) dcOut.write(TargetCard)
print "Input Data Card : ",options.InputFile print "Output EFT File : ",options.OutputFile if options.InputFile == 'None' : print 'Undefined Input File -> exit()' exit() if options.OutputFile == 'None' : print 'Undefined Output File -> exit()' exit() if options.InputFile == options.OutputFile : print 'Input and Output files are the same !!! -> exit()' exit() # Open Files dc=cardTools(options.InputFile) f = open(options.OutputFile,'w') # [Global] Section f.write('[Global] \n') # ... EFT parameters Params=options.params.split(';') f.write('dimension = '+str(len(Params))+'\n') nParam=0 for iParam in Params : nParam+=1 vParam = iParam.split(':') f.write('par'+str(nParam)+'Name = ' + vParam[0] + '\n')
def SMInject(cardIn,rmSMProc=True): dirName=os.path.dirname(cardIn) cardOut=cardIn.replace(os.path.splitext(cardIn)[1],'_SMInject'+os.path.splitext(cardIn)[1] ) dcIn = cardTools(cardIn) dcOut = cardTools(cardIn) #dcIn.write() #print dcIn.content['block1'] #print dcIn.content['block2'] iPosBin=-1 for iBin in dcIn.content['block1']['bin']: iPosBin+=1 iPosProc=-1 RateBG=0. RateSM=0. ProcList=[] INJList=[] for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc=dcIn.content['block2']['process'][iPosProc] PrId=dcIn.content['block2']['processId'][iPosProc] if iBin == jBin and '_SM' in Proc: INJList.append(Proc) ProcList.append(Proc) RateSM+=float(dcIn.getRate(bin=iBin,process=Proc)) if iBin == jBin and int(PrId) > 0 and '_SM' not in Proc: ProcList.append(Proc) RateBG+=float(dcIn.getRate(bin=iBin,process=Proc)) RateInj=RateSM+RateBG #print ProcList , RateInj , RateSM , RateBG dcOut.content['block1']['observation'][iPosBin] = str(int(round(RateInj,0))) if len(dcIn.content['header2']) > 0: dcOut.remShape('data_obs') ShapeData=[] ShapeProc={} for iShape in dcIn.content['header2']: #print 'SHAPE:' , dcIn.content['header2'][iShape] if dcIn.content['header2'][iShape][1] == 'data_obs' and ( dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin ) : if len(ShapeData) == 0 : ShapeData=deepcopy(dcIn.content['header2'][iShape]) else : if ShapeData[2] == '*' : ShapeData=deepcopy(dcIn.content['header2'][iShape]) for Proc in ProcList: if ( dcIn.content['header2'][iShape][1] == '*' or dcIn.content['header2'][iShape][1] == Proc ) and ( dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin ) : if Proc not in ShapeProc: ShapeProc[Proc] = deepcopy(dcIn.content['header2'][iShape]) else: if ShapeProc[Proc][1] == '*' or ShapeProc[Proc][2] == '*' : ShapeProc[Proc] = deepcopy(dcIn.content['header2'][iShape]) # Create Pseudo-data PseudoDataFirst = False for sProc in ShapeProc: fileName=dirName+'/'+ShapeProc[sProc][3] histName=ShapeProc[sProc][4].replace('$PROCESS',sProc) fHist = TFile.Open(fileName) Hist = fHist.Get(histName) gROOT.cd() if not PseudoDataFirst : PseudoDataFirst = True PseudoData = Hist.Clone("histo_PseudoData") PseudoData.SetTitle("histo_PseudoData") else : PseudoData.Add(Hist) fHist.Close() # round Pseudo-data (including under/over-flow) for i in xrange(0,PseudoData.GetNbinsX()+2): val = round(PseudoData.GetBinContent(i),0) if val>0 : err=sqrt(val) else : err=0 PseudoData.SetBinContent(i,val) PseudoData.SetBinError(i,err) dcOut.content['block1']['observation'][iPosBin] = str(int(round(PseudoData.Integral(),0))) fileShape=ShapeData[3] fileShape=fileShape.replace(os.path.splitext(fileShape)[1],'_pseudodata_'+iBin+os.path.splitext(fileShape)[1] ) fHist = TFile.Open(dirName+'/'+fileShape,'RECREATE') PseudoData.Write() fHist.Close() dcOut.addShape('data_obs',iBin,fileShape,'histo_PseudoData') # Remove *_SM process if rmSMProc: for jProc in INJList: dcOut.remCol(bin=iBin,process=jProc) #print PseudoData.Integral() , PseudoData.IsA().GetName() , fileShape #print ShapeProc print dcIn.content['block1'] , '--->' ,dcOut.content['block1'] os.system('rm '+cardOut) dcOut.write(cardOut) return cardOut
def SMToys(cardIn): dirName = os.path.dirname(cardIn) cardOut = cardIn.replace( os.path.splitext(cardIn)[1], '_SMToys' + os.path.splitext(cardIn)[1]) dcIn = cardTools(cardIn) dcOut = cardTools(cardIn) for iBin in dcIn.content['block1']['bin']: iPosProc = -1 INJList = [] SMXList = [] ShapeProc = {} # Copy rate and syst from *_SM to Signal for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc = dcIn.content['block2']['process'][iPosProc] PrId = dcIn.content['block2']['processId'][iPosProc] if iBin == jBin and '_SM' in Proc: INJList.append(Proc) if iBin == jBin and int(PrId) <= 0: SMXList.append(Proc) for iProc in SMXList: #print iProc if iProc + '_SM' in INJList: jProc = iProc + '_SM' dcOut.setRate(bin=iBin, process=iProc, value=dcIn.getCol( bin=iBin, process=jProc)['block2']['rate']) SkipList = [] for iSystType in dcOut.content['systs']: for iSyst in dcOut.content['systs'][iSystType]: dcOut.setSyst(bin=iBin, process=iProc, syst=iSystType, tag=iSyst, value='-') if '_SM' in iSyst: jSyst = iSyst.replace('_SM', '') SkipList.append(jSyst) for iSystType in dcIn.content['systs']: for iSyst in dcIn.content['systs'][iSystType]: jSyst = iSyst if '_SM' in iSyst and iSystType != 'shape' and iSystType != 'shapeN2': jSyst = iSyst.replace('_SM', '') #print iSyst , '->' , jSyst , iSystType if jSyst in dcOut.content['systs'][ iSystType] and iSyst not in SkipList: dcOut.setSyst(bin=iBin, process=iProc, syst=iSystType, tag=jSyst, value=dcIn.getSyst(bin=iBin, process=jProc, syst=iSystType, tag=iSyst)) if len(dcIn.content['header2']) > 0: dcOut.remShape(jProc) for iShape in dcIn.content['header2']: if (dcIn.content['header2'][iShape][1] == '*' or dcIn.content['header2'][iShape][1] == jProc ) and (dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin): if iProc not in ShapeProc: ShapeProc[iProc] = deepcopy( dcIn.content['header2'][iShape]) else: if ShapeProc[iProc][1] == '*' or ShapeProc[ iProc][2] == '*': ShapeProc[iProc] = deepcopy( dcIn.content['header2'][iShape]) else: sys.exit('ERROR: Missing process to inject : ' + iProc) # Add Shape lines if needed for sProc in ShapeProc: ShapeProc[sProc][1] = sProc ShapeProc[sProc][2] = iBin ShapeProc[sProc][4] = ShapeProc[sProc][4].replace( '$PROCESS', sProc + '_SM') ShapeProc[sProc][5] = ShapeProc[sProc][5].replace( '$PROCESS', sProc + '_SM') dcOut.addShape(sProc, iBin, ShapeProc[sProc][3], ShapeProc[sProc][4], ShapeProc[sProc][5]) # Remove *_SM process for jProc in INJList: dcOut.remCol(bin=iBin, process=jProc) # Fix lnU systematic if 'lnU' in dcOut.content['systs']: print dcOut.content['systs']['lnU'] for iSyst in dcOut.content['systs']['lnU']: for i in xrange(0, len(dcOut.content['systs']['lnU'][iSyst]) - 1): dcOut.content['systs']['lnU'][iSyst][i] = '-' print dcOut.content['systs']['lnU'] os.system('rm ' + cardOut) dcOut.write(cardOut) return cardOut
def ExtrapolateMassEff(iChannel,iEnergy,card,cardLow,cardHigh,TargetCard,vMass,iMassExtra,HistExtraPol=False,ExtraPolMeth='Mix'): print 'ExtrapolateMassEff: ',iChannel,vMass,iMassExtra dcIn = cardTools(card) dcLow = cardTools(cardLow) dcHigh = cardTools(cardHigh) dcOut = cardTools(card) #print channels[options.Version][iChannel][iEnergy] # Load YR Data #path = os.environ['CMSSW_BASE']+"/src/HiggsAnalysis/CombinedLimit/data/" path = '/afs/cern.ch/work/x/xjanssen/cms/vbfHbbCards/CMSSW_6_1_1'+'/src/HiggsAnalysis/CombinedLimit/data/' xs_ggH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ggH.txt') xs_qqH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-vbfH.txt') xs_WH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-WH.txt') xs_ZH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ZH.txt') xs_ttH = file2map(path+'lhc-hxswg/sm/xs/'+iEnergy+'/'+iEnergy+'-ttH.txt') br_VV = file2map(path+'lhc-hxswg/sm/br/BR.txt') br_ff = file2map(path+'lhc-hxswg/sm/br/BR1.txt') # Scale Yields ScaleFactors = {} for iBin in xrange(0 , len( dcIn.content['block2']['bin'] ) ): jBin=dcIn.content['block2']['bin'][iBin] iProc=dcIn.content['block2']['process'][iBin] YieldIn= float(dcIn.getRate(bin=jBin,process=iProc)) wLow = 1. wHigh = 1. # No Efficiency interpolation if vMass[0] == vMass[1] : # X-section xs_Scale = 1. if dcIn.content['block2']['process'][iBin] == 'ggH' : xs_Scale = GetYRVal(xs_ggH,iMassExtra,'XS_pb')/GetYRVal(xs_ggH,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'qqH' : xs_Scale = GetYRVal(xs_qqH,iMassExtra,'XS_pb')/GetYRVal(xs_qqH,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'WH' : xs_Scale = GetYRVal(xs_WH, iMassExtra,'XS_pb')/GetYRVal(xs_WH ,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ZH' : xs_Scale = GetYRVal(xs_ZH, iMassExtra,'XS_pb')/GetYRVal(xs_ZH ,iMass,'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ttH' : xs_Scale = GetYRVal(xs_ttH,iMassExtra,'XS_pb')/GetYRVal(xs_ttH,iMass,'XS_pb') # BR br_Scale = 1. if dcIn.content['block2']['process'][iBin] in ['ggH','qqH','WH','ZH','ttH']: if channels[options.Version][iChannel][iEnergy]['branch'] == 'hww' : br_Scale = GetYRVal(br_VV ,iMassExtra,'H_WW')/GetYRVal(br_VV,iMass,'H_WW') else : print 'ERROR: Unknown BR for :', iChannel, channels[options.Version][iChannel][iEnergy]['tag'] exit() YieldOut= float(dcIn.getRate(bin=jBin,process=iProc))*xs_Scale*br_Scale YieldLow =YieldOut YieldHigh=YieldOut elif vMass[0] < vMass[1] : wLow = float(iMassExtra-vMass[0])/float(vMass[1]-vMass[0]) wHigh = float(vMass[1]-iMassExtra)/float(vMass[1]-vMass[0]) # X-section xs_Scale_Low = 1. if dcIn.content['block2']['process'][iBin] == 'ggH' : xs_Scale_Low = GetYRVal(xs_ggH,iMassExtra,'XS_pb')/GetYRVal(xs_ggH,vMass[0],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'qqH' : xs_Scale_Low = GetYRVal(xs_qqH,iMassExtra,'XS_pb')/GetYRVal(xs_qqH,vMass[0],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'WH' : xs_Scale_Low = GetYRVal(xs_WH, iMassExtra,'XS_pb')/GetYRVal(xs_WH ,vMass[0],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ZH' : xs_Scale_Low = GetYRVal(xs_ZH, iMassExtra,'XS_pb')/GetYRVal(xs_ZH ,vMass[0],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ttH' : xs_Scale_Low = GetYRVal(xs_ttH,iMassExtra,'XS_pb')/GetYRVal(xs_ttH,vMass[0],'XS_pb') xs_Scale_High= 1. if dcIn.content['block2']['process'][iBin] == 'ggH' : xs_Scale_High = GetYRVal(xs_ggH,iMassExtra,'XS_pb')/GetYRVal(xs_ggH,vMass[1],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'qqH' : xs_Scale_High = GetYRVal(xs_qqH,iMassExtra,'XS_pb')/GetYRVal(xs_qqH,vMass[1],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'WH' : xs_Scale_High = GetYRVal(xs_WH, iMassExtra,'XS_pb')/GetYRVal(xs_WH ,vMass[1],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ZH' : xs_Scale_High = GetYRVal(xs_ZH, iMassExtra,'XS_pb')/GetYRVal(xs_ZH ,vMass[1],'XS_pb') if dcIn.content['block2']['process'][iBin] == 'ttH' : xs_Scale_High = GetYRVal(xs_ttH,iMassExtra,'XS_pb')/GetYRVal(xs_ttH,vMass[1],'XS_pb') # BR br_Scale_Low = 1. br_Scale_High = 1. if dcIn.content['block2']['process'][iBin] in ['ggH','qqH','WH','ZH','ttH']: if channels[options.Version][iChannel][iEnergy]['branch'] == 'hww' : br_Scale_Low = GetYRVal(br_VV ,iMassExtra,'H_WW')/GetYRVal(br_VV,vMass[0],'H_WW') br_Scale_High = GetYRVal(br_VV ,iMassExtra,'H_WW')/GetYRVal(br_VV,vMass[1],'H_WW') else : print 'ERROR: Unknown BR for :', iChannel, channels[options.Version][iChannel][iEnergy]['tag'] exit() if dcIn.content['block2']['process'][iBin] in ['ggH','qqH','WH','ZH','ttH']: ScaleLow = wLow *xs_Scale_Low *br_Scale_Low ScaleHigh = wHigh*xs_Scale_High*br_Scale_High if vMass[1] >= 350 and dcIn.content['block2']['process'][iBin] in ['WH','ZH','ttH']: YieldOut = xs_Scale_Low *br_Scale_Low*float(dcLow.getRate(bin=jBin,process=iProc)) YieldLow = YieldOut YieldHigh= YieldOut else: YieldOut = ScaleLow * float(dcLow.getRate(bin=jBin,process=iProc)) + ScaleHigh * float(dcHigh.getRate(bin=jBin,process=iProc)) YieldLow = xs_Scale_Low *br_Scale_Low*float(dcLow.getRate(bin=jBin,process=iProc)) YieldHigh = xs_Scale_High*br_Scale_High*float(dcHigh.getRate(bin=jBin,process=iProc)) else : YieldOut = float(dcIn.getRate(bin=jBin,process=iProc)) else: print 'ExtrapolateMassEff: ERROR in input masses',vMass,iMassExtra return #print xs_Scale , br_Scale if dcIn.content['block2']['process'][iBin] in ['ggH','qqH','WH','ZH','ttH']: print jBin,iProc,dcIn.getRate(bin=jBin,process=iProc) , '-->' , YieldOut , '(Low: ' , YieldLow , ' , High: ' , YieldHigh , ')' dcOut.setRate(bin=jBin,process=iProc,value=YieldOut) ScaleFactors[dcIn.content['block2']['process'][iBin]] = {} if YieldOut>0 and YieldIn>0: ScaleFactors[dcIn.content['block2']['process'][iBin]]['Avg'] = YieldOut/YieldIn ScaleFactors[dcIn.content['block2']['process'][iBin]]['Low'] = wLow *(YieldLow /YieldIn) ScaleFactors[dcIn.content['block2']['process'][iBin]]['High'] = wHigh*(YieldHigh/YieldIn) else: ScaleFactors[dcIn.content['block2']['process'][iBin]]['Avg'] = 1. ScaleFactors[dcIn.content['block2']['process'][iBin]]['Low'] = 1. ScaleFactors[dcIn.content['block2']['process'][iBin]]['High'] = 1. # Modify Histograms for HWW2l2v if channels[options.Version][iChannel][iEnergy]['branch'] == 'hww' and any(['shapes' in X for X in dcIn.content['header2'].itervalues()]) : shFiles = [] for iEntry in dcIn.content['header2'] : if dcIn.content['header2'][iEntry][0] == 'shapes' and dcIn.content['header2'][iEntry][1] != 'data_obs' and not ':' in dcIn.content['header2'][iEntry][3]: shFiles.append(dcIn.content['header2'][iEntry][3]) shFiles = list(set(shFiles)) print shFiles for iFile in shFiles: if not HistExtraPol or vMass[0] == vMass[1]: fileInName = os.path.split(card)[0]+'/'+iFile fileOutName = os.path.split(TargetCard)[0]+'/'+iFile print 'File IN : ',fileInName print 'File OUT: ',fileOutName fIn = TFile.Open(fileInName,'READ') fOut = TFile.Open(fileOutName,'RECREATE') Hists = [X.GetName() for X in fIn.GetListOfKeys()] for iHist in Hists : H = fIn.Get(iHist) isHiggs = False if len(iHist.split('_')) == 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : isHiggs = True elif len(iHist.split('_')) > 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] and iHist.split('_')[2] != 'SM' : isHiggs = True if isHiggs : Scale = 1. if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : Scale = ScaleFactors[iHist.split('_')[1]]['Avg'] H.Scale(Scale) fOut.cd() H.Write() fIn.Close() fOut.Close() elif ExtraPolMeth == 'Mix' : fileInName = os.path.split(card)[0]+'/'+iFile fileLowName = os.path.split(cardLow)[0]+'/'+iFile fileHighName = os.path.split(cardHigh)[0]+'/'+iFile fileOutName = os.path.split(TargetCard)[0]+'/'+iFile print 'File IN : ',fileInName print 'File Low : ',fileLowName print 'File High: ',fileHighName print 'File OUT : ',fileOutName fIn = TFile.Open(fileInName,'READ') fLow = TFile.Open(fileLowName,'READ') fHigh = TFile.Open(fileHighName,'READ') fOut = TFile.Open(fileOutName,'RECREATE') Hists = [X.GetName() for X in fIn.GetListOfKeys()] for iHist in Hists : HIn = fIn.Get(iHist) HLow = fLow.Get(iHist) HHigh = fHigh.Get(iHist) isHiggs = False if len(iHist.split('_')) == 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : isHiggs = True elif len(iHist.split('_')) > 2 : if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] and iHist.split('_')[2] != 'SM' : isHiggs = True if isHiggs : if vMass[1] >= 350 and iHist.split('_')[1] in ['WH','ZH','ttH']: Scale = 1. if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : Scale = ScaleFactors[iHist.split('_')[1]]['Avg'] H = HIn.Clone() H.Scale(Scale) else : print HLow,HHigh ScaleLow = 1. ScaleHigh = 1. if iHist.split('_')[1] in ['ggH','qqH','ZH','WH','ttH'] : ScaleLow = ScaleFactors[iHist.split('_')[1]]['Low'] ScaleHigh = ScaleFactors[iHist.split('_')[1]]['High'] HLow.Scale(ScaleLow) HHigh.Scale(ScaleHigh) H = HLow.Clone() H.Add(HHigh) else : H = HLow.Clone() fOut.cd() H.Write() fIn.Close() fLow.Close() fHigh.Close() fOut.Close() else: print 'Hist ExtraPolMeth UNDEFINED !!!! --> ',ExtraPolMeth exit() os.system('rm '+TargetCard) dcOut.write(TargetCard)
print "Input Data Card : ", options.InputFile print "Output EFT File : ", options.OutputFile if options.InputFile == 'None': print 'Undefined Input File -> exit()' exit() if options.OutputFile == 'None': print 'Undefined Output File -> exit()' exit() if options.InputFile == options.OutputFile: print 'Input and Output files are the same !!! -> exit()' exit() # Open Files dc = cardTools(options.InputFile) f = open(options.OutputFile, 'w') # [Global] Section f.write('[Global] \n') # ... EFT parameters Params = options.params.split(';') f.write('dimension = ' + str(len(Params)) + '\n') nParam = 0 for iParam in Params: nParam += 1 vParam = iParam.split(':') f.write('par' + str(nParam) + 'Name = ' + vParam[0] + '\n')
def SMInject(cardIn, rmSMProc=True): dirName = os.path.dirname(cardIn) cardOut = cardIn.replace( os.path.splitext(cardIn)[1], '_SMInject' + os.path.splitext(cardIn)[1]) dcIn = cardTools(cardIn) dcOut = cardTools(cardIn) #dcIn.write() #print dcIn.content['block1'] #print dcIn.content['block2'] iPosBin = -1 for iBin in dcIn.content['block1']['bin']: iPosBin += 1 iPosProc = -1 RateBG = 0. RateSM = 0. ProcList = [] INJList = [] for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc = dcIn.content['block2']['process'][iPosProc] PrId = dcIn.content['block2']['processId'][iPosProc] if iBin == jBin and '_SM' in Proc: INJList.append(Proc) ProcList.append(Proc) RateSM += float(dcIn.getRate(bin=iBin, process=Proc)) if iBin == jBin and int(PrId) > 0 and '_SM' not in Proc: ProcList.append(Proc) RateBG += float(dcIn.getRate(bin=iBin, process=Proc)) RateInj = RateSM + RateBG #print ProcList , RateInj , RateSM , RateBG dcOut.content['block1']['observation'][iPosBin] = str( int(round(RateInj, 0))) if len(dcIn.content['header2']) > 0: dcOut.remShape('data_obs') ShapeData = [] ShapeProc = {} for iShape in dcIn.content['header2']: #print 'SHAPE:' , dcIn.content['header2'][iShape] if dcIn.content['header2'][iShape][1] == 'data_obs' and ( dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin): if len(ShapeData) == 0: ShapeData = deepcopy(dcIn.content['header2'][iShape]) else: if ShapeData[2] == '*': ShapeData = deepcopy( dcIn.content['header2'][iShape]) for Proc in ProcList: if (dcIn.content['header2'][iShape][1] == '*' or dcIn.content['header2'][iShape][1] == Proc ) and (dcIn.content['header2'][iShape][2] == '*' or dcIn.content['header2'][iShape][2] == iBin): if Proc not in ShapeProc: ShapeProc[Proc] = deepcopy( dcIn.content['header2'][iShape]) else: if ShapeProc[Proc][1] == '*' or ShapeProc[Proc][ 2] == '*': ShapeProc[Proc] = deepcopy( dcIn.content['header2'][iShape]) # Create Pseudo-data PseudoDataFirst = False for sProc in ShapeProc: fileName = dirName + '/' + ShapeProc[sProc][3] histName = ShapeProc[sProc][4].replace('$PROCESS', sProc) fHist = TFile.Open(fileName) Hist = fHist.Get(histName) gROOT.cd() if not PseudoDataFirst: PseudoDataFirst = True PseudoData = Hist.Clone("histo_PseudoData") PseudoData.SetTitle("histo_PseudoData") else: PseudoData.Add(Hist) fHist.Close() # round Pseudo-data (including under/over-flow) for i in xrange(0, PseudoData.GetNbinsX() + 2): val = round(PseudoData.GetBinContent(i), 0) if val > 0: err = sqrt(val) else: err = 0 PseudoData.SetBinContent(i, val) PseudoData.SetBinError(i, err) dcOut.content['block1']['observation'][iPosBin] = str( int(round(PseudoData.Integral(), 0))) fileShape = ShapeData[3] fileShape = fileShape.replace( os.path.splitext(fileShape)[1], '_pseudodata_' + iBin + os.path.splitext(fileShape)[1]) fHist = TFile.Open(dirName + '/' + fileShape, 'RECREATE') PseudoData.Write() fHist.Close() dcOut.addShape('data_obs', iBin, fileShape, 'histo_PseudoData') # Remove *_SM process if rmSMProc: for jProc in INJList: dcOut.remCol(bin=iBin, process=jProc) #print PseudoData.Integral() , PseudoData.IsA().GetName() , fileShape #print ShapeProc print dcIn.content['block1'], '--->', dcOut.content['block1'] os.system('rm ' + cardOut) dcOut.write(cardOut) return cardOut
def WJetFix(cardIn, cardForWJ): if not iChannel in preProc['WJetFix']['ChannelList']: return cardIn cardWJ = cardForWJ.replace(cardDir, 'couplings').replace( str(iMass), (preProc['WJetFix']['massWJ']).replace( '$MASS', str(iMass))).replace('_hsm', '') cardOut = cardIn.replace( os.path.splitext(cardIn)[1], '_WJetFix' + os.path.splitext(cardIn)[1]) print 'WJetFix IN :', cardIn print 'WJetFix WJet:', cardWJ print 'WJetFix OUT :', cardOut dcIn = cardTools(cardIn) dcWJ = cardTools(cardWJ) for iBin in dcIn.content['block1']['bin']: ProcList = [] iPosProc = -1 for jBin in dcIn.content['block2']['bin']: iPosProc += 1 Proc = dcIn.content['block2']['process'][iPosProc] ProcList.append(Proc) for iProc in preProc['WJetFix']['processList']: if iProc in ProcList: #print iProc dcIn.setRate(bin=iBin, process=iProc, value=dcWJ.getCol( bin=iBin, process=iProc)['block2']['rate']) for iSystType in dcIn.content['systs']: for iSyst in dcIn.content['systs'][iSystType]: dcIn.setSyst(bin=iBin, process=iProc, syst=iSystType, tag=iSyst, value='-') for iSystType in dcWJ.content['systs']: if iSystType in dcIn.content['systs']: for iSyst in dcWJ.content['systs'][iSystType]: if iSyst in dcIn.content['systs'][iSystType]: dcIn.setSyst(bin=iBin, process=iProc, syst=iSystType, tag=iSyst, value=dcWJ.getSyst(bin=iBin, process=iProc, syst=iSystType, tag=iSyst)) #print len(dcIn.content['header2']) , '-->' , dcIn.content['header2'] if len(dcIn.content['header2']) > 0: for iShape in dcWJ.content['header2']: #print dcWJ.content['header2'][iShape][1] if dcWJ.content['header2'][iShape][ 1] == '*' or dcWJ.content['header2'][iShape][ 1] == 'iProc': #print 'Replace' Shape = dcWJ.content['header2'][iShape] #print cardDir FileWJ = cardWJ.replace( cardWJ.split('/')[-1], Shape[3]) if len(Shape) == 5: dcIn.addShape(iProc, Shape[2], FileWJ, Shape[4]) if len(Shape) == 6: dcIn.addShape(iProc, Shape[2], FileWJ, Shape[4], Shape[5]) #print len(dcIn.content['header2']) , '-->' , dcIn.content['header2'] os.system('rm ' + cardOut) dcIn.write(cardOut) return cardOut
iPar2=(iJob-1)/(JobParamSize[0]*targets[iTarget]['NJobs']) command=command.replace('$'+JobParamName[0],str(targets[iTarget]['JobsParam'][JobParamName[0]][iPar1])) command=command.replace('$'+JobParamName[1],str(targets[iTarget]['JobsParam'][JobParamName[1]][iPar2])) PF=PF+'_'+JobParamName[0]+str(targets[iTarget]['JobsParam'][JobParamName[0]][iPar1]).replace('.','d') PF=PF+'_'+JobParamName[1]+str(targets[iTarget]['JobsParam'][JobParamName[1]][iPar2]).replace('.','d') else : sys.exit() # for iParam in range(0,len(JobsParam)): # Nuissance Freezing if 'FreezeNuis' in targets[iTarget]: toFreeze=[] card=TargetDir+'/'+str(iMass)+'/'+iComb if options.energy != 0: card += '_' + str(options.energy) + 'TeV' if iAltModel != 'NONE' and 'AltModel' in targets[iTarget] and targets[iTarget]['AltModel'] == 'Gen' : card += '_' + iAltModel card += ".txt" dc = cardTools(card) #print dc.content['systs'] for iTF in targets[iTarget]['FreezeNuis']: for iSystType in dc.content['systs']: if targets[iTarget]['FreezeNuis'][iTF][0] == iSystType or targets[iTarget]['FreezeNuis'][iTF][0] == '*' : for iSyst in dc.content['systs'][iSystType]: print iSystType , iSyst if targets[iTarget]['FreezeNuis'][iTF][1] in iSyst or targets[iTarget]['FreezeNuis'][iTF][1] == '*' : print '-->Freeze' toFreeze.append(iSyst) if len(toFreeze) > 0 : command += ' --freezeNuisances '+toFreeze[0] if len(toFreeze) > 1 : for iF in xrange(1,len(toFreeze) ): command += ','+toFreeze[iF] # outfile outname=outname
'1p': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/', '1m': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' }, 'hwwof_0j_8TeV.txt': { '1p': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/', '1m': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' }, 'hwwof_1j_8TeV.txt': { '1p': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1p/', '1m': '/afs/cern.ch/work/x/xjanssen/cms/Spin2014/V4/jcp/1m/' } } for iComb in Config: dc1p = cardTools(Config[iComb]['1p'] + iComb) dc1m = cardTools(Config[iComb]['1m'] + iComb) dcOut = cardTools(Config[iComb]['1m'] + iComb) # Copy 1p to ggH_ALT2 dcOut.addCol(dc1p.content['block2']['bin'][1], 'ggH_ALT2', -6, dc1p.content['block2']['rate'][1], dc1p.content['block1']['observation'][0]) #print dcOut.content['block1'] #print dcOut.content['block2'] # Set systematics for iType in dc1p.content['systs']: #print iType for iSyst in dc1p.content['systs'][iType]: #print iSyst, dc1p.getSyst( bin=dc1p.content['block2']['bin'][1] , process='ggH_ALT' , syst=iType , tag=iSyst )