def __init__(self,energy): print("Reading files...") path = "../data/rawData/"+str(energy)+"/data/" eventNr, nrParticles = np.loadtxt(path+"B_MULT",dtype=int,usecols=(0,2),unpack=True) NPOM = np.loadtxt(path+"NPOM.dat",dtype=int) finalpr = open(path+"finalpr.data",'r') self.path = path self.eventNr, self.nrParticles, self.finalpr, self.NPOM =\ eventNr, nrParticles, finalpr, NPOM self.bcorr = [] self.elasticEvents = 0 self.inElasticEvents = 0 self.countedEvents = 0 self.bcorr_linecount = 0 self.nbnf_all = np.zeros(2) self.nbnf_nsd = np.zeros(2) self.nbnf_etalim = np.zeros(2) self.nbnf_ptcut = np.zeros(2) self.Nbins = 50 start = -0.5 stop = self.Nbins + start self.nfnb_file = ROOT.TFile.Open('7000_4M_nfnb_all.root','recreate') H_all_input =[["all","ALL"],["nsd","Non-single diffraction"],\ ["etalim","|\eta|<1"],["ptcut","0.3<p_{T}<1.5"]] H_all_treename = ["H","all"] H_div_input =[["all_div","DIV"],["nsd_div","Non-single diffraction"],\ ["etalim_div","|\eta|<1"],["ptcut_div","0.3<p_{T}<1.5"]] H_div_treename = ["H","div"] H_nf_input =[["nf","NF"],["nsd_nf","Non-single diffraction"],\ ["etalim_nf","|\eta|<1"],["ptcut_nf","0.3<p_{T}<1.5"]] H_nf_treename = ["H","nf"] self.trees = [hist(H_all_treename,H_all_input,self.Nbins,start,stop),\ hist(H_div_treename,H_div_input,self.Nbins,start,stop),\ hist(H_nf_treename,H_nf_input,self.Nbins,start,stop)]
def observe(self, img): # get patches corresponding to each particle mh, mw = self.model.shape[:2] minx = (self.particles[:, 0] - mw / 2).astype(np.int) miny = (self.particles[:, 1] - mh / 2).astype(np.int) candidates = [ img[miny[i]:miny[i] + mh, minx[i]:minx[i] + mw] for i in range(self.num_particles) ] #Compute importance weight - similarity of each patch to the model self.weights[:] = [ hist(cand, self.model, std=10, num_bins=8) for cand in candidates ] self.weights /= self.weights.sum()
def option(): print '1 : New input File \n 2 : clear data \n 3: Exit program ' op = int(raw_input('Enter your option : ')) alpha = 'abcdefghijklmnopqrstuvwxyz' # these are char which % occurence we have to calculate if op == 1: print 'Put your text file in current folder then ' filename = str(raw_input('Enter file name with extention : ')) hs = hist(filename) #create object of class hist hs.histogram( ) #itterate operation on file for calculation of accurence print 'result of this itteration is :' ## print result of this itteration in regular order a ...z for key in alpha: if key in hs.data: print key, ' => ', hs.data[key] else: print key, ' => ', 0 #overal result db = database() #create object of class database database.add_store(db, hs.data) # update database db.result() #update result.txt file db.close_db() #close database connect print '\n\n' option() elif op == 2: db = database() #create object of class database db.refresh_database() # Reset database to 0 print 'database refresed \n\n' # print db.Data ##To check data is refresed or not option() elif op == 3: print " thank uu.... " exit()
from hist import * n = "qwertyasdaaafghzxcv" lis = hist(n) print lis
def makeWorkspace(self, iOutput, iDatas, iFuncs, iVars, iCat="cat0", iShift=True): lW = r.RooWorkspace("w_" + str(iCat)) # get the pT bin ipt = iCat[-1:] sigMassesForInterpolation = [] shapeForInterpolation_central = [] shapeForInterpolation_scaleUp = [] shapeForInterpolation_scaleDn = [] shapeForInterpolation_smearUp = [] shapeForInterpolation_smearDn = [] self._outfile_validation.cd() for pFunc in iFuncs: ptbin = ipt process = pFunc.GetName().split("_")[0] cat = pFunc.GetName().split("_")[1] mass = 0. #### bbb hout = [] histDict = {} if 'tqq' in process or 'wqq' in process or 'zqq' in process: tmph = self._inputfile.Get(process + '_' + cat).Clone(process + '_' + cat) tmph_up = self._inputfile.Get(process + '_' + cat).Clone(process + '_' + cat + '_' + 'mcstatUp') tmph_down = self._inputfile.Get(process + '_' + cat).Clone(process + '_' + cat + '_' + 'mcstatDown') # tmph.Scale(getSF(process,cat,self._inputfile)) # tmph_up.Scale(getSF(process,cat,self._inputfile)) # tmph_down.Scale(getSF(process,cat,self._inputfile)) tmph_mass = proj('cat', str(ipt), tmph, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_up = proj('cat', str(ipt), tmph_up, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_down = proj('cat', str(ipt), tmph_down, self._mass_nbins, self._mass_lo, self._mass_hi) for i in range(1, tmph_mass_up.GetNbinsX() + 1): mcstatup = tmph_mass_up.GetBinContent( i) + tmph_mass_up.GetBinError(i) mcstatdown = max( 0., tmph_mass_down.GetBinContent(i) - tmph_mass_down.GetBinError(i)) tmph_mass_up.SetBinContent(i, mcstatup) tmph_mass_down.SetBinContent(i, mcstatdown) tmph_mass.SetName(pFunc.GetName()) tmph_mass_up.SetName(pFunc.GetName() + '_' + pFunc.GetName().replace('_', '') + 'mcstatUp') tmph_mass_down.SetName(pFunc.GetName() + '_' + pFunc.GetName().replace('_', '') + 'mcstatDown') histDict[pFunc.GetName()] = tmph_mass histDict[pFunc.GetName() + '_' + pFunc.GetName().replace('_', '') + 'mcstatUp'] = tmph_mass_up histDict[pFunc.GetName() + '_' + pFunc.GetName().replace('_', '') + 'mcstatDown'] = tmph_mass_down uncorrelate(histDict, 'mcstat') for key, myhist in histDict.iteritems(): if 'mcstat' in key: print key hout.append(myhist) for h in hout: tmprdh = r.RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) # validation self._outfile_validation.cd() h.Write() if iShift and ("wqq" in process or "zqq" in process): if process == "wqq": mass = 75. elif process == "zqq": mass = 100. else: mass = float(process[3:]) print process, mass #### # get the matched and unmatched hist print "getting the matched and unmatched hists" tmph_matched = self._inputfile.Get(process + "_" + cat + "_matched") tmph_unmatched = self._inputfile.Get(process + "_" + cat + "_unmatched") tmph_mass_matched = proj("cat", str(ipt), tmph_matched, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_unmatched = proj("cat", str(ipt), tmph_unmatched, self._mass_nbins, self._mass_lo, self._mass_hi) print "start loop over bins" self.RhoSafetyCheck(tmph_mass_matched, ipt) self.RhoSafetyCheck(tmph_mass_unmatched, ipt) ##### # smear/shift the matched hist_container = hist([mass], [tmph_mass_matched]) mass_shift = 1.0 mass_shift_unc = 0.15 # This is 5 sigma shift! Change the card accordingly res_shift = 1.10 res_shift_unc = 0.1 # get new central value shift_val = mass - mass * mass_shift tmp_shifted_h = hist_container.shift(tmph_mass_matched, shift_val) # get new central value and new smeared value smear_val = res_shift - 1. tmp_smeared_h = hist_container.smear(tmp_shifted_h[0], smear_val) hmatched_new_central = tmp_smeared_h[0] if smear_val <= 0.: hmatched_new_central = tmp_smeared_h[1] # get shift up/down shift_unc = mass * mass_shift * mass_shift_unc hmatchedsys_shift = hist_container.shift( hmatched_new_central, shift_unc) # get res up/down hmatchedsys_smear = hist_container.smear( hmatched_new_central, res_shift_unc) ##### # add back the unmatched print "adding back the unmatched events" hmatched_new_central.Add(tmph_mass_unmatched) hmatchedsys_shift[0].Add(tmph_mass_unmatched) hmatchedsys_shift[1].Add(tmph_mass_unmatched) hmatchedsys_smear[0].Add(tmph_mass_unmatched) hmatchedsys_smear[1].Add(tmph_mass_unmatched) hmatched_new_central.SetName(pFunc.GetName()) hmatchedsys_shift[0].SetName(pFunc.GetName() + "_scaleUp") hmatchedsys_shift[1].SetName(pFunc.GetName() + "_scaleDown") hmatchedsys_smear[0].SetName(pFunc.GetName() + "_smearUp") hmatchedsys_smear[1].SetName(pFunc.GetName() + "_smearDown") hout = [ hmatched_new_central, hmatchedsys_shift[0], hmatchedsys_shift[1], hmatchedsys_smear[0], hmatchedsys_smear[1] ] print "match the jets to NOT the W/Z" if mass > 0 and mass != 75. and mass != 100.: sigMassesForInterpolation.append(mass) shapeForInterpolation_central.append(hmatched_new_central) shapeForInterpolation_scaleUp.append(hmatchedsys_shift[0]) shapeForInterpolation_scaleDn.append(hmatchedsys_shift[1]) shapeForInterpolation_smearUp.append(hmatchedsys_smear[0]) shapeForInterpolation_smearDn.append(hmatchedsys_smear[1]) for h in hout: print h.GetName() self.RhoSafetyCheck(h, ipt) h.Write() tmprdh = RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) if h.GetName().find("scale") > -1: pName = h.GetName().replace("scale", "scalept") tmprdh = RooDataHist(pName, pName, r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) else: getattr(lW, 'import')(pFunc, r.RooFit.RecycleConflictNodes()) # do the signal interpolation print "---------------------------------------------------------------" print len(sigMassesForInterpolation), sigMassesForInterpolation print iCat morphedHistContainer_central = hist(sigMassesForInterpolation, shapeForInterpolation_central) morphedHistContainer_scaleUp = hist(sigMassesForInterpolation, shapeForInterpolation_scaleUp) morphedHistContainer_scaleDn = hist(sigMassesForInterpolation, shapeForInterpolation_scaleDn) morphedHistContainer_smearUp = hist(sigMassesForInterpolation, shapeForInterpolation_smearUp) morphedHistContainer_smearDn = hist(sigMassesForInterpolation, shapeForInterpolation_smearDn) interpolatedMasses = [ 15., 20., 30., 35., 40., 45., 55., 60.0, 65., 70., 80., 85., 90.0, 95., 105., 110.0, 115., 120., 130., 135.0, 140., 145., 155., 160., 165.0, 170., 175., 180.0, 185., 190., 195. ] #interpolatedMasses = [195.,205.,210.,215.,220.,225.,230.,235.,240,245.,255.,260.,265.,270.,275.,280.,285.,290.,295.] for m in interpolatedMasses: htmp_central = morphedHistContainer_central.morph(m) htmp_scaleUp = morphedHistContainer_scaleUp.morph(m) htmp_scaleDn = morphedHistContainer_scaleDn.morph(m) htmp_smearUp = morphedHistContainer_smearUp.morph(m) htmp_smearDn = morphedHistContainer_smearDn.morph(m) htmp_central.SetName("zqq%i_%s" % (int(m), iCat)) htmp_scaleUp.SetName("zqq%i_%s_scaleUp" % (int(m), iCat)) htmp_scaleDn.SetName("zqq%i_%s_scaleDown" % (int(m), iCat)) htmp_smearUp.SetName("zqq%i_%s_smearUp" % (int(m), iCat)) htmp_smearDn.SetName("zqq%i_%s_smearDown" % (int(m), iCat)) hout = [ htmp_central, htmp_scaleUp, htmp_scaleDn, htmp_smearUp, htmp_smearDn ] for h in hout: print h.GetName() self.RhoSafetyCheck(h, ipt) h.Write() tmprdh = RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) if h.GetName().find("scale") > -1: pName = h.GetName().replace("scale", "scalept") tmprdh = RooDataHist(pName, pName, r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) for pData in iDatas: getattr(lW, 'import')(pData, r.RooFit.RecycleConflictNodes()) if iCat.find("pass_cat1") == -1: lW.writeToFile(iOutput, False) else: lW.writeToFile(iOutput)
def makeWorkspace(self,iOutput,iDatas,iFuncs,iVars,iCat="cat0",iShift=True): lW = r.RooWorkspace("w_"+str(iCat)) # get the pT bin ipt = iCat[-1:]; sigMassesForInterpolation = []; shapeForInterpolation_central = []; shapeForInterpolation_scaleUp = []; shapeForInterpolation_scaleDn = []; shapeForInterpolation_smearUp = []; shapeForInterpolation_smearDn = []; self._outfile_validation.cd(); for pFunc in iFuncs: ptbin = ipt; process = pFunc.GetName().split("_")[0]; cat = pFunc.GetName().split("_")[1]; mass = 0.; #### bbb hout = []; histDict = {} if 'tqq' in process or 'wqq' in process or 'zqq' in process: tmph = self._inputfile.Get(process+'_'+cat).Clone(process+'_'+cat) tmph_up = self._inputfile.Get(process+'_'+cat).Clone(process+'_'+cat+'_'+'mcstatUp') tmph_down = self._inputfile.Get(process+'_'+cat).Clone(process+'_'+cat+'_'+'mcstatDown') # tmph.Scale(getSF(process,cat,self._inputfile)) # tmph_up.Scale(getSF(process,cat,self._inputfile)) # tmph_down.Scale(getSF(process,cat,self._inputfile)) tmph_mass = proj('cat',str(ipt),tmph,self._mass_nbins,self._mass_lo,self._mass_hi) tmph_mass_up = proj('cat',str(ipt),tmph_up,self._mass_nbins,self._mass_lo,self._mass_hi) tmph_mass_down = proj('cat',str(ipt),tmph_down,self._mass_nbins,self._mass_lo,self._mass_hi) for i in range(1,tmph_mass_up.GetNbinsX()+1): mcstatup = tmph_mass_up.GetBinContent(i) + tmph_mass_up.GetBinError(i) mcstatdown = max(0.,tmph_mass_down.GetBinContent(i) - tmph_mass_down.GetBinError(i)) tmph_mass_up.SetBinContent(i,mcstatup) tmph_mass_down.SetBinContent(i,mcstatdown) tmph_mass.SetName(pFunc.GetName()) tmph_mass_up.SetName(pFunc.GetName()+'_'+pFunc.GetName().replace('_','')+'mcstatUp') tmph_mass_down.SetName(pFunc.GetName()+'_'+pFunc.GetName().replace('_','')+'mcstatDown') histDict[pFunc.GetName()] = tmph_mass histDict[pFunc.GetName()+'_'+pFunc.GetName().replace('_','')+'mcstatUp'] = tmph_mass_up histDict[pFunc.GetName()+'_'+pFunc.GetName().replace('_','')+'mcstatDown'] = tmph_mass_down uncorrelate(histDict,'mcstat') for key, myhist in histDict.iteritems(): if 'mcstat' in key: print key hout.append(myhist) for h in hout: tmprdh = r.RooDataHist(h.GetName(),h.GetName(),r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) # validation self._outfile_validation.cd() h.Write() if iShift and ("wqq" in process or "zqq" in process): if process == "wqq": mass = 80.; elif process == "zqq": mass = 91.; else: mass = float(process[3:]) print process, mass; #### # get the matched and unmatched hist tmph_matched = self._inputfile.Get(process+"_"+cat+"_matched"); tmph_unmatched = self._inputfile.Get(process+"_"+cat+"_unmatched"); tmph_mass_matched = proj("cat",str(ipt),tmph_matched,self._mass_nbins,self._mass_lo,self._mass_hi); tmph_mass_unmatched = proj("cat",str(ipt),tmph_unmatched,self._mass_nbins,self._mass_lo,self._mass_hi); for i0 in range(1,self._mass_nbins+1): print pFunc.GetName() if ((i0 > 31 or i0 < 0) and int(ipt) == 1) or ((i0 > 38 or i0 < 4) and int(ipt) == 2) or ((i0 > 46 or i0 < 5) and int(ipt) == 3) or ((i0 > 56 or i0 < 7) and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): tmph_mass_matched.SetBinContent(i0,0); tmph_mass_unmatched.SetBinContent(i0,0); ##### # smear/shift the matched hist_container = hist( [mass],[tmph_mass_matched] ); mass_shift = 1.0; mass_shift_unc = 0.15; # This is 5 sigma shift! Change the card accordingly res_shift = 1.10; res_shift_unc = 0.1; # get new central value shift_val = mass - mass*mass_shift; tmp_shifted_h = hist_container.shift( tmph_mass_matched, shift_val); # get new central value and new smeared value smear_val = res_shift - 1.; tmp_smeared_h = hist_container.smear( tmp_shifted_h[0] , smear_val) hmatched_new_central = tmp_smeared_h[0]; if smear_val <= 0.: hmatched_new_central = tmp_smeared_h[1]; # get shift up/down shift_unc = mass*mass_shift*mass_shift_unc; hmatchedsys_shift = hist_container.shift( hmatched_new_central, shift_unc); # get res up/down hmatchedsys_smear = hist_container.smear( hmatched_new_central, res_shift_unc); ##### # add back the unmatched hmatched_new_central.Add(tmph_mass_unmatched); hmatchedsys_shift[0].Add(tmph_mass_unmatched); hmatchedsys_shift[1].Add(tmph_mass_unmatched); hmatchedsys_smear[0].Add(tmph_mass_unmatched); hmatchedsys_smear[1].Add(tmph_mass_unmatched); hmatched_new_central.SetName(pFunc.GetName()); hmatchedsys_shift[0].SetName(pFunc.GetName()+"_scaleUp"); hmatchedsys_shift[1].SetName(pFunc.GetName()+"_scaleDown"); hmatchedsys_smear[0].SetName(pFunc.GetName()+"_smearUp"); hmatchedsys_smear[1].SetName(pFunc.GetName()+"_smearDown"); hout = [hmatched_new_central,hmatchedsys_shift[0],hmatchedsys_shift[1],hmatchedsys_smear[0],hmatchedsys_smear[1]]; if mass > 0 and mass != 80. and mass != 91.:# and mass != 250. and mass != 300.: sigMassesForInterpolation.append(mass); shapeForInterpolation_central.append(hmatched_new_central) shapeForInterpolation_scaleUp.append(hmatchedsys_shift[0]) shapeForInterpolation_scaleDn.append(hmatchedsys_shift[1]) shapeForInterpolation_smearUp.append(hmatchedsys_smear[0]) shapeForInterpolation_smearDn.append(hmatchedsys_smear[1]) for h in hout: print h.GetName() for i0 in range(1,self._mass_nbins+1): if ((i0 > 31 or i0 < 0) and int(ipt) == 1) or ((i0 > 38 or i0 < 4) and int(ipt) == 2) or ((i0 > 46 or i0 < 5) and int(ipt) == 3) or ((i0 > 56 or i0 < 7) and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): h.SetBinContent(i0,0); h.Write(); tmprdh = RooDataHist(h.GetName(),h.GetName(),r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) if h.GetName().find("scale") > -1: pName=h.GetName().replace("scale","scalept") tmprdh = RooDataHist(pName,pName,r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) else: getattr(lW,'import')(pFunc,r.RooFit.RecycleConflictNodes()) # do the signal interpolation print "---------------------------------------------------------------" print len(sigMassesForInterpolation), sigMassesForInterpolation print iCat morphedHistContainer_central = hist(sigMassesForInterpolation,shapeForInterpolation_central); morphedHistContainer_scaleUp = hist(sigMassesForInterpolation,shapeForInterpolation_scaleUp); morphedHistContainer_scaleDn = hist(sigMassesForInterpolation,shapeForInterpolation_scaleDn); morphedHistContainer_smearUp = hist(sigMassesForInterpolation,shapeForInterpolation_smearUp); morphedHistContainer_smearDn = hist(sigMassesForInterpolation,shapeForInterpolation_smearDn); interpolatedMasses = [55.,60.0,65.,70.,80.,85.,90.0,95.,105.,110.0,115.,120.,130.,135.0,140.,145.,155.,160.,165.0,170.,175.,180.0,185.,190.,195.,205.,210.,215.,220.,225.,230.,235.,240,245.,255.,260.,265.,270.,275.,280.,285.,290.,295.] #interpolatedMasses = [195.,205.,210.,215.,220.,225.,230.,235.,240,245.,255.,260.,265.,270.,275.,280.,285.,290.,295.] for m in interpolatedMasses: htmp_central = morphedHistContainer_central.morph(m); htmp_scaleUp = morphedHistContainer_scaleUp.morph(m); htmp_scaleDn = morphedHistContainer_scaleDn.morph(m); htmp_smearUp = morphedHistContainer_smearUp.morph(m); htmp_smearDn = morphedHistContainer_smearDn.morph(m); htmp_central.SetName("zqq%i_%s" % (int(m),iCat)); htmp_scaleUp.SetName("zqq%i_%s_scaleUp" % (int(m),iCat)); htmp_scaleDn.SetName("zqq%i_%s_scaleDown" % (int(m),iCat)); htmp_smearUp.SetName("zqq%i_%s_smearUp" % (int(m),iCat)); htmp_smearDn.SetName("zqq%i_%s_smearDown" % (int(m),iCat)); hout = [htmp_central,htmp_scaleUp,htmp_scaleDn,htmp_smearUp,htmp_smearDn]; for h in hout: print h.GetName() for i0 in range(1,self._mass_nbins+1): if (i0 > 31 and int(ipt) == 1) or ((i0 > 38 or i0 < 4) and int(ipt) == 2) or ((i0 > 46 or i0 < 5) and int(ipt) == 3) or ( (i0 < 7 or i0 > 56) and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): h.SetBinContent(i0,0); h.Write(); tmprdh = RooDataHist(h.GetName(),h.GetName(),r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) if h.GetName().find("scale") > -1: pName=h.GetName().replace("scale","scalept") tmprdh = RooDataHist(pName,pName,r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) for pData in iDatas: getattr(lW,'import')(pData,r.RooFit.RecycleConflictNodes()) if iCat.find("pass_cat1") == -1: lW.writeToFile(iOutput,False) else: lW.writeToFile(iOutput)
def makeWorkspace(self, iOutput, iDatas, iFuncs, iVars, iCat="cat0", iShift=True, iSyst=True, ptVal=500): lW = r.RooWorkspace("w_" + str(iCat)) # get the pT bin ipt = iCat[-1:] for pFunc in iFuncs: pFunc.Print() ptbin = ipt process = pFunc.GetName().split('_')[0] cat = pFunc.GetName().split('_')[1] mass = 0 systematics = ['JES', 'JER', 'trigger'] if iSyst and ('tqq' in process or 'wqq' in process or 'zqq' in process or 'hqq' in process): # get systematic histograms hout = [] for syst in systematics: tmph_up = self._inputfile.Get(process + '_' + cat + '_' + syst + 'Up') tmph_down = self._inputfile.Get(process + '_' + cat + '_' + syst + 'Down') tmph_mass_up = proj('cat', str(ipt), tmph_up, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_down = proj('cat', str(ipt), tmph_down, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_up.SetName(pFunc.GetName() + '_' + syst + 'Up') tmph_mass_down.SetName(pFunc.GetName() + '_' + syst + 'Down') hout.append(tmph_mass_up) hout.append(tmph_mass_down) # blind if necessary and output to workspace for h in hout: for i in range(1, h.GetNbinsX() + 1): massVal = h.GetXaxis().GetBinCenter(i) rhoVal = r.TMath.Log(massVal * massVal / ptVal / ptVal) if self._blind and massVal > BLIND_LO and massVal < BLIND_HI: print "blinding signal region for %s, mass bin [%i,%i] " % ( h.GetName(), h.GetXaxis().GetBinLowEdge(i), h.GetXaxis().GetBinUpEdge(i)) h.SetBinContent(i, 0.) if rhoVal < RHO_LO or rhoVal > RHO_HI: print "removing rho = %.2f for %s, ptVal = %.2f, mass bin [%i,%i]" % ( rhoVal, h.GetName(), ptVal, h.GetXaxis().GetBinLowEdge(i), h.GetXaxis().GetBinUpEdge(i)) h.SetBinContent(i, 0.) tmprdh = r.RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) # validation self._outfile_validation.cd() h.Write() if iShift and ('wqq' in process or 'zqq' in process or 'hqq' in process): if process == 'wqq': mass = 80. elif process == 'zqq': mass = 91. else: mass = float(process[-3:]) # hqq125 -> 125 # get the matched and unmatched hist tmph_matched = self._inputfile.Get(process + '_' + cat + '_matched') tmph_unmatched = self._inputfile.Get(process + '_' + cat + '_unmatched') tmph_mass_matched = proj('cat', str(ipt), tmph_matched, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_unmatched = proj('cat', str(ipt), tmph_unmatched, self._mass_nbins, self._mass_lo, self._mass_hi) # smear/shift the matched hist_container = hist([mass], [tmph_mass_matched]) mass_shift = 0.99 mass_shift_unc = 0.03 * 2. #(2 sigma shift) res_shift = 1.094 res_shift_unc = 0.123 * 2. #(2 sigma shift) # get new central value shift_val = mass - mass * mass_shift tmp_shifted_h = hist_container.shift(tmph_mass_matched, shift_val) # get new central value and new smeared value smear_val = res_shift - 1 tmp_smeared_h = hist_container.smear(tmp_shifted_h[0], smear_val) hmatched_new_central = tmp_smeared_h[0] if smear_val <= 0: hmatched_new_central = tmp_smeared_h[1] # get shift up/down shift_unc = mass * mass_shift * mass_shift_unc hmatchedsys_shift = hist_container.shift( hmatched_new_central, mass * mass_shift_unc) # get res up/down hmatchedsys_smear = hist_container.smear( hmatched_new_central, res_shift_unc) # add back the unmatched hmatched_new_central.Add(tmph_mass_unmatched) hmatchedsys_shift[0].Add(tmph_mass_unmatched) hmatchedsys_shift[1].Add(tmph_mass_unmatched) hmatchedsys_smear[0].Add(tmph_mass_unmatched) hmatchedsys_smear[1].Add(tmph_mass_unmatched) hmatched_new_central.SetName(pFunc.GetName()) hmatchedsys_shift[0].SetName(pFunc.GetName() + "_scaleUp") hmatchedsys_shift[1].SetName(pFunc.GetName() + "_scaleDown") hmatchedsys_smear[0].SetName(pFunc.GetName() + "_smearUp") hmatchedsys_smear[1].SetName(pFunc.GetName() + "_smearDown") hout = [ hmatched_new_central, hmatchedsys_shift[0], hmatchedsys_shift[1], hmatchedsys_smear[0], hmatchedsys_smear[1] ] # blind if necessary and output to workspace for h in hout: for i in range(1, h.GetNbinsX() + 1): massVal = h.GetXaxis().GetBinCenter(i) rhoVal = r.TMath.Log(massVal * massVal / ptVal / ptVal) if self._blind and massVal > BLIND_LO and massVal < BLIND_HI: print "blinding signal region for %s, mass bin [%i,%i] " % ( h.GetName(), h.GetXaxis().GetBinLowEdge(i), h.GetXaxis().GetBinUpEdge(i)) h.SetBinContent(i, 0.) if rhoVal < RHO_LO or rhoVal > RHO_HI: print "removing rho = %.2f for %s, ptVal = %.2f, mass bin [%i,%i]" % ( rhoVal, h.GetName(), ptVal, h.GetXaxis().GetBinLowEdge(i), h.GetXaxis().GetBinUpEdge(i)) h.SetBinContent(i, 0.) tmprdh = r.RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) # validation self._outfile_validation.cd() h.Write() else: getattr(lW, 'import')(pFunc, r.RooFit.RecycleConflictNodes()) for pData in iDatas: pData.Print() getattr(lW, 'import')(pData, r.RooFit.RecycleConflictNodes() ) # , r.RooFit.Rename("data_obs") self._outfile_validation.Write() #self._outfile_validation.Close() if iCat.find("pass_cat1") == -1: lW.writeToFile(iOutput, False) else: lW.writeToFile(iOutput)
def makeWorkspace(self,iOutput,iDatas,iFuncs,iVars,iCat="cat0",iShift=True): lW = r.RooWorkspace("w_"+str(iCat)) # get the pT bin ipt = iCat[-1:]; sigMassesForInterpolation = []; shapeForInterpolation_central = []; shapeForInterpolation_scaleUp = []; shapeForInterpolation_scaleDn = []; shapeForInterpolation_smearUp = []; shapeForInterpolation_smearDn = []; self._outfile_validation.cd(); for pFunc in iFuncs: ptbin = ipt; process = pFunc.GetName().split("_")[0]; cat = pFunc.GetName().split("_")[1]; mass = 0.; if iShift and ("wqq" in process or "zqq" in process): if process == "wqq": mass = 80.; elif process == "zqq": mass = 91.; else: mass = float(process[3:]) print process, mass; #### # get the matched and unmatched hist tmph_matched = self._inputfile.Get(process+"_"+cat+"_matched"); tmph_unmatched = self._inputfile.Get(process+"_"+cat+"_unmatched"); tmph_mass_matched = proj("cat",str(ipt),tmph_matched,self._mass_nbins,self._mass_lo,self._mass_hi); tmph_mass_unmatched = proj("cat",str(ipt),tmph_unmatched,self._mass_nbins,self._mass_lo,self._mass_hi); for i0 in range(1,self._mass_nbins+1): if (i0 > 31 and int(ipt) == 1) or (i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or (i0 < 7 and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): tmph_mass_matched.SetBinContent(i0,0); tmph_mass_unmatched.SetBinContent(i0,0); ##### # smear/shift the matched hist_container = hist( [mass],[tmph_mass_matched] ); mass_shift = 0.99; mass_shift_unc = 0.15; # This is 5 sigma shift! Change the card accordingly res_shift = 1.094; res_shift_unc = 0.123; # get new central value shift_val = mass - mass*mass_shift; tmp_shifted_h = hist_container.shift( tmph_mass_matched, shift_val); # get new central value and new smeared value smear_val = res_shift - 1.; tmp_smeared_h = hist_container.smear( tmp_shifted_h[0], smear_val) hmatched_new_central = tmp_smeared_h[0]; if smear_val <= 0.: hmatched_new_central = tmp_smeared_h[1]; # get shift up/down shift_unc = mass*mass_shift*mass_shift_unc; hmatchedsys_shift = hist_container.shift( hmatched_new_central, shift_unc); # get res up/down hmatchedsys_smear = hist_container.smear( hmatched_new_central, res_shift_unc); ##### # add back the unmatched hmatched_new_central.Add(tmph_mass_unmatched); hmatchedsys_shift[0].Add(tmph_mass_unmatched); hmatchedsys_shift[1].Add(tmph_mass_unmatched); hmatchedsys_smear[0].Add(tmph_mass_unmatched); hmatchedsys_smear[1].Add(tmph_mass_unmatched); hmatched_new_central.SetName(pFunc.GetName()); hmatchedsys_shift[0].SetName(pFunc.GetName()+"_scaleUp"); hmatchedsys_shift[1].SetName(pFunc.GetName()+"_scaleDown"); hmatchedsys_smear[0].SetName(pFunc.GetName()+"_smearUp"); hmatchedsys_smear[1].SetName(pFunc.GetName()+"_smearDown"); hout = [hmatched_new_central,hmatchedsys_shift[0],hmatchedsys_shift[1],hmatchedsys_smear[0],hmatchedsys_smear[1]]; if mass > 0 and mass != 80. and mass != 91. and mass != 250. and mass != 300.: sigMassesForInterpolation.append(mass); shapeForInterpolation_central.append(hmatched_new_central) shapeForInterpolation_scaleUp.append(hmatchedsys_shift[0]) shapeForInterpolation_scaleDn.append(hmatchedsys_shift[1]) shapeForInterpolation_smearUp.append(hmatchedsys_smear[0]) shapeForInterpolation_smearDn.append(hmatchedsys_smear[1]) for h in hout: print h.GetName() for i0 in range(1,self._mass_nbins+1): if (i0 > 31 and int(ipt) == 1) or (i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or (i0 < 7 and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): h.SetBinContent(i0,0); h.Write(); tmprdh = RooDataHist(h.GetName(),h.GetName(),r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) else: getattr(lW,'import')(pFunc,r.RooFit.RecycleConflictNodes()) # do the signal interpolation print "---------------------------------------------------------------" print len(sigMassesForInterpolation), sigMassesForInterpolation print iCat morphedHistContainer_central = hist(sigMassesForInterpolation,shapeForInterpolation_central); morphedHistContainer_scaleUp = hist(sigMassesForInterpolation,shapeForInterpolation_scaleUp); morphedHistContainer_scaleDn = hist(sigMassesForInterpolation,shapeForInterpolation_scaleDn); morphedHistContainer_smearUp = hist(sigMassesForInterpolation,shapeForInterpolation_smearUp); morphedHistContainer_smearDn = hist(sigMassesForInterpolation,shapeForInterpolation_smearDn); interpolatedMasses = [60.0,90.0,110.0,135.0,165.0,180.0] for m in interpolatedMasses: htmp_central = morphedHistContainer_central.morph(m); htmp_scaleUp = morphedHistContainer_scaleUp.morph(m); htmp_scaleDn = morphedHistContainer_scaleDn.morph(m); htmp_smearUp = morphedHistContainer_smearUp.morph(m); htmp_smearDn = morphedHistContainer_smearDn.morph(m); htmp_central.SetName("zqq%i_%s" % (int(m),iCat)); htmp_scaleUp.SetName("zqq%i_%s_scaleUp" % (int(m),iCat)); htmp_scaleDn.SetName("zqq%i_%s_scaleDown" % (int(m),iCat)); htmp_smearUp.SetName("zqq%i_%s_smearUp" % (int(m),iCat)); htmp_smearDn.SetName("zqq%i_%s_smearDown" % (int(m),iCat)); hout = [htmp_central,htmp_scaleUp,htmp_scaleDn,htmp_smearUp,htmp_smearDn]; for h in hout: print h.GetName() for i0 in range(1,self._mass_nbins+1): if (i0 > 31 and int(ipt) == 1) or (i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or ( (i0 < 7 or i0 > 56) and int(ipt) == 4) or ( i0 < 7 and int(ipt) == 5): h.SetBinContent(i0,0); h.Write(); tmprdh = RooDataHist(h.GetName(),h.GetName(),r.RooArgList(self._lMSD),h) getattr(lW,'import')(tmprdh, r.RooFit.RecycleConflictNodes()) for pData in iDatas: getattr(lW,'import')(pData,r.RooFit.RecycleConflictNodes()) if iCat.find("pass_cat1") == -1: lW.writeToFile(iOutput,False) else: lW.writeToFile(iOutput)
def makeWorkspace(self, iOutput, iDatas, iFuncs, iVars, iCat="cat0", iShift=True): lW = r.RooWorkspace("w_" + str(iCat)) # get the pT bin ipt = iCat[-1:] sigMassesForInterpolation = [] shapeForInterpolation_central = [] shapeForInterpolation_scaleUp = [] shapeForInterpolation_scaleDn = [] shapeForInterpolation_smearUp = [] shapeForInterpolation_smearDn = [] self._outfile_validation.cd() for pFunc in iFuncs: ptbin = ipt process = pFunc.GetName().split("_")[0] cat = pFunc.GetName().split("_")[1] mass = 0. if iShift and ("wqq" in process or "zqq" in process): if process == "wqq": mass = 80. elif process == "zqq": mass = 91. else: mass = float(process[3:]) print process, mass #### # get the matched and unmatched hist tmph_matched = self._inputfile.Get(process + "_" + cat + "_matched") tmph_unmatched = self._inputfile.Get(process + "_" + cat + "_unmatched") tmph_mass_matched = proj("cat", str(ipt), tmph_matched, self._mass_nbins, self._mass_lo, self._mass_hi) tmph_mass_unmatched = proj("cat", str(ipt), tmph_unmatched, self._mass_nbins, self._mass_lo, self._mass_hi) for i0 in range(1, self._mass_nbins + 1): print '!!!!!!!!! YYY' print pFunc.GetName() if 'pass' in pFunc.GetName() and ( process == "zqq" or process == "wqq"): # or process == "zqq"): if i0 > 18: tmph_mass_matched.SetBinContent(i0, 0) tmph_mass_unmatched.SetBinContent(i0, 0) if (i0 > 31 and int(ipt) == 1) or (i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or ( i0 < 7 and int(ipt) == 4) or (i0 < 7 and int(ipt) == 5): tmph_mass_matched.SetBinContent(i0, 0) tmph_mass_unmatched.SetBinContent(i0, 0) ##### # smear/shift the matched hist_container = hist([mass], [tmph_mass_matched]) mass_shift = 0.99 mass_shift_unc = 0.15 # This is 5 sigma shift! Change the card accordingly res_shift = 1.094 res_shift_unc = 0.123 # get new central value shift_val = mass - mass * mass_shift tmp_shifted_h = hist_container.shift(tmph_mass_matched, shift_val) # get new central value and new smeared value smear_val = res_shift - 1. tmp_smeared_h = hist_container.smear(tmp_shifted_h[0], smear_val) hmatched_new_central = tmp_smeared_h[0] if smear_val <= 0.: hmatched_new_central = tmp_smeared_h[1] # get shift up/down shift_unc = mass * mass_shift * mass_shift_unc hmatchedsys_shift = hist_container.shift( hmatched_new_central, shift_unc) # get res up/down hmatchedsys_smear = hist_container.smear( hmatched_new_central, res_shift_unc) ##### # add back the unmatched hmatched_new_central.Add(tmph_mass_unmatched) hmatchedsys_shift[0].Add(tmph_mass_unmatched) hmatchedsys_shift[1].Add(tmph_mass_unmatched) hmatchedsys_smear[0].Add(tmph_mass_unmatched) hmatchedsys_smear[1].Add(tmph_mass_unmatched) hmatched_new_central.SetName(pFunc.GetName()) hmatchedsys_shift[0].SetName(pFunc.GetName() + "_scaleUp") hmatchedsys_shift[1].SetName(pFunc.GetName() + "_scaleDown") hmatchedsys_smear[0].SetName(pFunc.GetName() + "_smearUp") hmatchedsys_smear[1].SetName(pFunc.GetName() + "_smearDown") hout = [ hmatched_new_central, hmatchedsys_shift[0], hmatchedsys_shift[1], hmatchedsys_smear[0], hmatchedsys_smear[1] ] if mass > 0 and mass != 80. and mass != 91. and mass != 250. and mass != 300.: sigMassesForInterpolation.append(mass) shapeForInterpolation_central.append(hmatched_new_central) shapeForInterpolation_scaleUp.append(hmatchedsys_shift[0]) shapeForInterpolation_scaleDn.append(hmatchedsys_shift[1]) shapeForInterpolation_smearUp.append(hmatchedsys_smear[0]) shapeForInterpolation_smearDn.append(hmatchedsys_smear[1]) for h in hout: print h.GetName() for i0 in range(1, self._mass_nbins + 1): if 'pass' in h.GetName() and ( process == "zqq" or process == "wqq"): # or process == "zqq"): if i0 > 18: h.SetBinContent(i0, 0) if (i0 > 31 and int(ipt) == 1) or ( i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or ( i0 < 7 and int(ipt) == 4) or (i0 < 7 and int(ipt) == 5): h.SetBinContent(i0, 0) h.Write() tmprdh = RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) else: getattr(lW, 'import')(pFunc, r.RooFit.RecycleConflictNodes()) # do the signal interpolation print "---------------------------------------------------------------" print len(sigMassesForInterpolation), sigMassesForInterpolation print iCat morphedHistContainer_central = hist(sigMassesForInterpolation, shapeForInterpolation_central) morphedHistContainer_scaleUp = hist(sigMassesForInterpolation, shapeForInterpolation_scaleUp) morphedHistContainer_scaleDn = hist(sigMassesForInterpolation, shapeForInterpolation_scaleDn) morphedHistContainer_smearUp = hist(sigMassesForInterpolation, shapeForInterpolation_smearUp) morphedHistContainer_smearDn = hist(sigMassesForInterpolation, shapeForInterpolation_smearDn) interpolatedMasses = [60.0, 90.0, 110.0, 135.0, 165.0, 180.0] for m in interpolatedMasses: htmp_central = morphedHistContainer_central.morph(m) htmp_scaleUp = morphedHistContainer_scaleUp.morph(m) htmp_scaleDn = morphedHistContainer_scaleDn.morph(m) htmp_smearUp = morphedHistContainer_smearUp.morph(m) htmp_smearDn = morphedHistContainer_smearDn.morph(m) htmp_central.SetName("zqq%i_%s" % (int(m), iCat)) htmp_scaleUp.SetName("zqq%i_%s_scaleUp" % (int(m), iCat)) htmp_scaleDn.SetName("zqq%i_%s_scaleDown" % (int(m), iCat)) htmp_smearUp.SetName("zqq%i_%s_smearUp" % (int(m), iCat)) htmp_smearDn.SetName("zqq%i_%s_smearDown" % (int(m), iCat)) hout = [ htmp_central, htmp_scaleUp, htmp_scaleDn, htmp_smearUp, htmp_smearDn ] for h in hout: print h.GetName() for i0 in range(1, self._mass_nbins + 1): if (i0 > 31 and int(ipt) == 1) or (i0 > 38 and int(ipt) == 2) or (i0 > 46 and int(ipt) == 3) or ( (i0 < 7 or i0 > 56) and int(ipt) == 4) or (i0 < 7 and int(ipt) == 5): h.SetBinContent(i0, 0) h.Write() tmprdh = RooDataHist(h.GetName(), h.GetName(), r.RooArgList(self._lMSD), h) getattr(lW, 'import')(tmprdh, r.RooFit.RecycleConflictNodes()) for pData in iDatas: getattr(lW, 'import')(pData, r.RooFit.RecycleConflictNodes()) if iCat.find("pass_cat1") == -1: lW.writeToFile(iOutput, False) else: lW.writeToFile(iOutput)