def getMostContributingModulesinRange(file, run, pathduplet): ############################################################### moduleList = [] firstpathswitch = True maxmodtime = 0. tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s paths" % ( run, THEPROCESS) gDirectory.cd(dirname) #Loop over all the paths that were found to contribute #in the speciied range for pduplet in pathduplet: thepathname = pduplet.path hName = dirname + "/" + thepathname + "/module_time_real_running" thepathweight = pduplet.weight hist = tfile.Get(hName) #If first path get the denominator for the weight if firstpathswitch: maxmodtime = hist.GetMaximum() firstpathswitch = False #Loop over the modules and store the triplet nbins = hist.GetNbinsX() for thebin in range(1, nbins): themodname = hist.GetXaxis().GetBinLabel(thebin) themodtime = hist.GetBinContent(thebin) themodweight = themodtime / maxmodtime moduleList.append( Quartet(thepathname, themodname, thepathweight, themodweight)) #print thepathname+", "+themodname+", "+str(themodweight*thepathweight) moduleList = sorted(moduleList, key=lambda quartet: quartet.weight, reverse=True) return moduleList
def get_modules_timing(infile, run): ############################################################### theDict = {} #this is dangerously hardcoded: process = "TIMING" tfile = TFile(infile) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s modules" % ( run, process) gDirectory.cd(dirname) #type of plot modplot = "time_real" #print dirname for key in gDirectory.GetListOfKeys(): histname = key.GetName() histnamesplit = histname.split() #check if the module info is the one we need modulename = histnamesplit[0] if not modulename[0].isupper(): if histnamesplit[1] == modplot: hName = dirname + "/" + histname hist = tfile.Get(hName) theMean = hist.GetMean() theDict[modulename] = theMean return theDict
def get_nuis_corr(tfile, is_conditional, is_asimov, poi=1): f = ROOT.TFile.Open(tfile) mu = "1" if is_asimov: stub = "Asimov" f.cd("PlotsAfterFitToAsimov") mu = str(poi) else: stub = "Global" f.cd("PlotsAfterGlobalFit") mu = str(poi) if is_conditional: gDirectory.cd("conditionnal_MuIsEqualTo_" + mu) #if is_asimov: # gDirectory.cd("conditionnal_MuIsEqualTo_"+mu) #else: # gDirectory.cd("conditionnal_MuIsEqualTo_0") #p_nuis = gDirectory.Get("can_NuisPara_"+stub+"Fit_conditionnal_mu0") #p_corr = gDirectory.Get("can_CorrMatrix_"+stub+"Fit_conditionnal_mu0") p_nuis = gDirectory.Get("can_NuisPara_" + stub + "Fit_conditionnal_mu" + mu) p_corr = gDirectory.Get("can_CorrMatrix_" + stub + "Fit_conditionnal_mu" + mu) else: gDirectory.cd("unconditionnal") p_nuis = gDirectory.Get("can_NuisPara_" + stub + "Fit_unconditionnal_mu" + mu) p_corr = gDirectory.Get("can_CorrMatrix_" + stub + "Fit_unconditionnal_mu" + mu) return p_nuis, p_corr
def main(): fwhm = np.array([], dtype="float") da.save_Tree(filepath, win_percent=0.1) f = TFile(filepath + "/" + "focus.root", "recreate") f.mkdir("distribution_of_amp") f.distribution.cd() da.hist2(filepath, 1, 0, 1) h2 = f.distribution.hist2 gDirectory.cd("..") f.mkfir("projection") f.projection.cd() z = h2.GetNbinsY() for i in range(0, z): da.projection(h2, 'x', name="_pz", i, i + 1) fwhm_value = calculate_FWHM(f.projection.hist2_pz) np.append(fwhm, fwhm_value) da.projection(h2, 'x', "_z" + str(i), i, i + 1) f2 = TFile(filepath + "/" + "position_and_amp.root") z_min = f2.t1.GetMinimum("z") z_max = f2.t1.GetMaximum("z") z = np.arange(z_min, z_max + 1) z = z.astype("float") tgr = TGraph(z.size, z, fwhm) t1 = TCanva("t1", "focus") tgr.GetXaxis().SetTitle("z[um]") tgr.GetYaxis().SetTitle("FWHM[um]") tgr.Draw("AC*") tgr.Fit("pol2") gStyle.SetOptFit(1) c1.Update() c1.SaveAs("./result/focus.pdf")
def putHistogram(self, dataset, systematicVariation, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param dataset Dataset object # @param systematicVariation SystematicVariation object # @param var Variable object # @param cut Cut object # @param histogram ROOT::TH1 object #if not self._open( dataset, systematicVariation, var, cut ): # return None if not self._open(dataset, systematicVariation, var, cut, 'update'): self.logger.warning( 'putHistogram(): unable to store histogram, file not open') return None path, histogramName = self._buildPath(dataset, systematicVariation, var, cut) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self._file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) histogram = histogram.Clone(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def main(): # parse command line argument (rootfilename, outprefix, plotprefix) = parse_args() # create a tprint object and export functions to current scope # remember functions can be treated as variables in python tp = tprint((rootfilename, outprefix, plotprefix)) fp = tp.fullpath #fp(filename) returns string with prefixed dir/filename (see outprefix) fs = tp.fullstring #fs(title) returns string with prefixed title (see plotprefix) # read the rootlogon.C file for plot style gROOT.ProcessLine(".x rootlogon.C") tf = ROOT.TFile(rootfilename) runNumberDirectory = tf.GetListOfKeys()[0].GetName() gDirectory.cd(tf.GetListOfKeys()[0].GetName() + "/IDAlignMon/ExtendedTracks_NoTriggerSelection/Residuals") p = {} #dictionary to store the pulls makeIBLPlots(fp, fs, p) makePixelBarrelPlots(fp, fs, p) makePixelECAPlots(fp, fs, p) makePixelECCPlots(fp, fs, p) makeSCTBarrelPlots(fp, fs, p) makeSCTECAPlots(fp, fs, p) makeSCTECCPlots(fp, fs, p) makeTRTBarrelPlots(fp, fs, p) makeTRTECAPlots(fp, fs, p) makeTRTECCPlots(fp, fs, p) #since we don't treat endcaps seperately (yet), average both endcaps calculateEndcapAverages(p) printPulls(p)
def putHistogram(self, channelName, datasetName, systematicName, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param datasetName the name of the dataset # @param systematicName the name of the systematic variation # @param var the variable # @param cut the cut # @param histogram the histogram object if not self._open(): return None path, histogramName = self._buildPath(channelName, datasetName, systematicName) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self.file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) if 'resopara' in histogramName or 'resoperp' in histogramName: if '_low' in histogramName: histogramName.replace('_low', '') elif '_high' in histogramName: return histogram.SetTitle(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def retrieveHistogram(inputFile, h_Path, h_Name): h_file = TFile(inputFile) gDirectory.cd(h_Path) h = gDirectory.Get(h_Name) h.SetDirectory(0) return h
def getObjectsTitleId(self, titles_list): myDict = {} for key in gDirectory.GetListOfKeys(): mypath = gDirectory.GetPathStatic() self.filterKey(key,mypath,myDict, titles_list) gDirectory.cd(mypath) return myDict
def find_trigger(filename): tf = TFile(filename) keys = [key.GetName() for key in gDirectory.GetListOfKeys()] for key in keys: print('key = {}'.format(key)) gDirectory.cd(key) sub_keys = [key.GetName() for key in gDirectory.GetListOfKeys()] if len(sub_keys) > 1: print('sub_keys = {}'.format(sub_keys)) gDirectory.cd('..')
def saveForLimit(TightIsoOS, prefixLabel, mass, massType, category, susy): TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('ZZ')) TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('WZ')) noscomp = { 'DYJets' :'ZTT', 'DYJets_Electron' :'ZL', 'DYJets_Fakes' :'ZJ', 'WJets' :'W', 'TTJets' :'TT', 'WW' :'VV', #'ZZ' :'', #'WZ' :'', 'QCDdata' :'QCD', 'Data' :'data_obs' } if susy : sigcomp = { 'HiggsSUSYBB' +str(mass):'bbH'+str(mass), 'HiggsSUSYGluGlu'+str(mass):'ggH'+str(mass), } else : sigcomp = { 'HiggsGGH'+str(mass):'ggH'+str(mass), 'HiggsVBF'+str(mass):'qqH'+str(mass), 'HiggsVH' +str(mass):'VH' +str(mass), } allcomp = {} allcomp.update(noscomp) allcomp.update(sigcomp) fileName = '/'.join([os.getcwd(),prefixLabel,prefixLabel+'_tauTau_'+category+'_'+TightIsoOS.varName+'.root']) if TightIsoOS.varName == massType+'' : if not os.path.isfile(fileName) : rootfile = TFile(fileName,'recreate') channel = rootfile.mkdir('tauTau_'+category) for comp in allcomp.keys() : TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) channel.Add(TightIsoOS.Hist(comp).weighted) channel.Write() else : rootfile = TFile(fileName,'update') gDirectory.cd('tauTau_'+category) for comp in sigcomp.keys() : TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) TightIsoOS.Hist(comp).weighted.Write() gDirectory.cd('..') rootfile.Close()
def signal_efficiency(cut_ID, run, nevents): """ Assuming .root file opened, access directory to calculate signal efficiency of a single event. cut_ID: string run: int nevents: int """ gDirectory.cd("/{}/signal/run_{}".format(cut_ID, run)) mwp_signal = gDirectory.Get("mwp_signal") nselected = mwp_signal.GetEntries() return float(nselected) / nevents
def getPathOrderByIntegral(file,run,lowerLimit,upperLimit): pathList = [] process = "TIMING" tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s paths" % (run, process) gDirectory.cd(dirname) for everyPath in gDirectory.GetListOfKeys(): if everyPath.GetName().startswith("path "): hist=tfile.Get(dirname+"/"+everyPath.GetName()+"/path time_real") pathList.append(Duplet(everyPath.GetName(),hist.Integral(lowerLimit/5,upperLimit/5))) pathList=sorted(pathList,key=lambda duplet: duplet.weight, reverse=True) return pathList
def saveToFile(h, fileName, directory, name): f = TFile(fileName, 'UPDATE') path = '/' ## Create the directory tree for dir in directory.split('/'): path += dir + '/' if not gDirectory.Get(dir): gDirectory.mkdir(dir) gDirectory.cd(dir) ## Now the tree is written, move to the dir f.cd(path) hc = h.Clone() hc.Write(name, TObject.kOverwrite) f.Close()
def create_master_workspaces(meta_data): pwd = gDirectory.GetPath() ws_list = {} for (sample,chanlist) in meta_data.getAssociation().iteritems(): ws_list[sample] = {} for (channel,proclist) in chanlist.iteritems(): #make the workspace we're going to use this_ws = RooWorkspace('%s-%s'%(channel,sample)) initialize_workspace(this_ws) ws_list[sample][channel] = {} for (process,subproclist) in proclist.iteritems(): print sample, channel, process if 'HToZG'in process: ws_list[sample][channel][process] = [] for (subproc,info) in subproclist.iteritems(): print '\tprocessing: %s'%subproc input_file = info['input_file'] info['num_mc_events'] = -1 if input_file == '': print '\t no input file found! Skipping!' continue if 'data' not in process: print '\t mc input = %s'%input_file.split('/')[-1] if 'HToZG' in subproc: info['num_mc_events'] = \ extract_higgs_data_in_categories(subproc, input_file, this_ws) ws_list[sample][channel][process].append(subproc) else: info['num_mc_events'] = \ extract_bkg_data_in_categories(subproc, input_file, this_ws) else: print '\t data input = %s'%input_file.split('/')[-1] extract_data_in_categories(channel,input_file,this_ws) #end loop over processes and data fout_name = '%s_%s_master_workspace.root'%(channel,sample) fout = TFile.Open(fout_name,'recreate') fout.cd() this_ws.Write() fout.Close() gDirectory.cd(pwd) ws_list[sample][channel]['filename'] = fout_name return ws_list
def extract_data_in_categories(channel,input_file,ws): pwd = gDirectory.GetPath() fin = TFile.Open(input_file,'read') gDirectory.cd(pwd) tree = fin.Get('selected_zg') data = RooDataSet('%s_data'%channel, 'real data %s channel'%channel, tree, ws.set('vars') ) data_in_ws = ws.data('%s_data'%channel) if not not data_in_ws: data_in_ws.append(data) else: getattr(ws,'import')(data)
def getMax(file,run): pathList = [] process = "TIMING" tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s paths" % (run, process) gDirectory.cd(dirname) for everyPath in gDirectory.GetListOfKeys(): if everyPath.GetName().startswith("path "): hist=tfile.Get(dirname+"/"+everyPath.GetName()+"/module_time_real_total") nbins=hist.GetNbinsX() maxBin=0 maxTime=0 for everyIndexProcces in range(0,nbins+1): if hist.GetBinContent(everyIndexProcces) > maxTime: maxBin=everyIndexProcces maxTime=hist.GetBinContent(maxBin) pathList.append(Triplet(everyPath.GetName(),hist.GetXaxis().GetBinLabel(maxBin),hist.GetBinContent(maxBin))) return pathList
def func(file,run,mode,argument): wholeDict = {} process = "TIMING" tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/" % (run) gDirectory.cd(dirname) dirnameModule = "process %s modules" % (process) gDirectory.cd(dirnameModule) totalSum=0 for everyKey in gDirectory.GetListOfKeys(): keyName=everyKey.GetName() if (keyName[0].islower() and keyName.endswith("time_real")): hist = tfile.Get(dirname+dirnameModule+"/"+keyName) totalSum=totalSum+hist.GetMean() keyName=keyName.split()[0] wholeDict.update({keyName.split()[0]:hist.GetMean()}) dirnamePath = "process %s paths" % (process) gDirectory.cd("../") gDirectory.cd(dirnamePath) cuontTotalPaths=0 countIncludedPaths=0 excludedDict=wholeDict.copy() for everyPath in gDirectory.GetListOfKeys(): everyPathName= everyPath.GetName() if everyPathName.startswith("path ") or everyPathName.startswith("endpath ") : cuontTotalPaths+=1 if not strainer(everyPath,mode,argument): countIncludedPaths+=1 hist=tfile.Get(dirname+dirnamePath+"/"+everyPathName+"/module_time_real_total") nbins=hist.GetNbinsX() for moduleIndex in range(1,nbins+1): labelModule=hist.GetXaxis().GetBinLabel(moduleIndex) if excludedDict.has_key(labelModule): del excludedDict[labelModule] #print sumatory of mean times excludeSum=0 for everyKey in excludedDict: excludeSum+=excludedDict[everyKey] f = io.open(unicode("ExtractPaths"+file.split('_')[1].strip(".csv")+".csv"),'w',encoding='utf8') f.write(unicode("Actual Mean Event Real Time:, %f\n" % (tfile.Get(dirname+"event time_real").GetMean()))) f.write(unicode("Total Paths:, %i\n"% (cuontTotalPaths))) f.write(unicode("Total Modules:, %i\n"% (len(wholeDict)))) f.write(unicode("Total Mean Modules Sum:, %f\n"% (totalSum))) f.write(unicode("Excluded Paths:, %i\n"% (cuontTotalPaths-countIncludedPaths))) f.write(unicode("Excluded Modules:, %i\n"% (len(excludedDict)))) f.write(unicode("Excluded Mean Modules Sum:, %f\n"% (excludeSum))) f.write(unicode("Net Paths:, %i\n"% (countIncludedPaths))) f.write(unicode("Net Modules:, %i\n"% (len(wholeDict)-len(excludedDict)))) f.write(unicode("Net Mean Modules Sum:, %f\n"% (totalSum-excludeSum)))
def getPathOrderByIntegral(file, run, lowerLimit, upperLimit): pathList = [] process = "TIMING" tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s paths" % ( run, process) gDirectory.cd(dirname) for everyPath in gDirectory.GetListOfKeys(): if everyPath.GetName().startswith("path "): hist = tfile.Get(dirname + "/" + everyPath.GetName() + "/path time_real") pathList.append( Duplet(everyPath.GetName(), hist.Integral(lowerLimit / 5, upperLimit / 5))) pathList = sorted(pathList, key=lambda duplet: duplet.weight, reverse=True) return pathList
def getFitResult(cfg): """ Go and fetch RooFitResult in a FitCrossChecks.root file """ f = TFile.Open("{0}/FitCrossChecks.root".format(cfg._fcc_directory)) if not f.IsOpen(): raise RuntimeError("Couldn't find file {0}".format( "{0}/FitCrossChecks.root".format(cfg._fcc_directory))) if cfg._is_asimov: f.cd("PlotsAfterFitToAsimov") else: f.cd("PlotsAfterGlobalFit") if cfg._is_conditional: gDirectory.cd("conditionnal_MuIsEqualTo_{0}".format(cfg._mu)) else: gDirectory.cd("unconditionnal") rfr = gDirectory.Get("fitResult").Clone() # bonus: get a suffix corresponding to the setup suffix = getPostfitSuffix(cfg) f.Close() return rfr, suffix
def extract_higgs_data_in_categories(subproc,input_file,ws): pwd = gDirectory.GetPath() fin = TFile.Open(input_file,'read') gDirectory.cd(pwd) tree = fin.Get('selected_zg') mc_tot_events = float(fin.Get('eventCount').GetBinContent(1)) data_total_weight,data_pu_weight = make_weighted_dataset(subproc, ws,tree, mc_tot_events) ws.factory('%s_acceff[%f]'%(subproc, data_pu_weight.sumEntries()/mc_tot_events)) fin.Close() #save with and without process cross section normalization getattr(ws,'import')(data_total_weight) getattr(ws,'import')(data_pu_weight) return mc_tot_events
def getMostContributingPathsInRange(file, run, lowerlimit, upperlimit): ############################################################### pathList = [] tfile = TFile(file) dirname = "DQMData/Run %s/HLT/Run summary/TimerService/process %s paths" % ( run, THEPROCESS) gDirectory.cd(dirname) #Loop over all the paths in the file to check which ones most contribute #to the specified timing range for thepath in gDirectory.GetListOfKeys(): pathName = thepath.GetName() if (pathName.startswith("path ") and pathName.find("HLTriggerFinalPath") == -1): hName = dirname + "/" + thepath.GetName() + "/path time_real" hist = tfile.Get(hName) #manipulate the lowerlimit and upperlimit #to get the appropiate range in histogram #Get the bin numbers for the lowerlimit and upperlimit nbins = hist.GetNbinsX() lowerbin = 1 upperbin = nbins for thebin in range(1, nbins): lowbinedge = hist.GetBinLowEdge(thebin) highbinedge = hist.GetBinLowEdge(thebin + 1) if (lowerlimit >= lowbinedge and lowerlimit < highbinedge): lowerbin = thebin if (upperlimit > lowbinedge and upperlimit <= highbinedge): upperbin = thebin pathweight = hist.Integral(lowerbin, upperbin) / hist.Integral() #only add paths that actually contribute to the range if (pathweight > 0): pathList.append(Duplet(pathName, pathweight)) #order the paths by weight pathList = sorted(pathList, key=lambda duplet: duplet.weight, reverse=True) return pathList
def extract_bkg_data_in_categories(subproc,input_file,ws): pwd = gDirectory.GetPath() fin = TFile.Open(input_file,'read') gDirectory.cd(pwd) tree = fin.Get('selected_zg') mc_tot_events = float(fin.Get('eventCount').GetBinContent(1)) data_proper_weight,data_pu_weight = make_weighted_dataset(subproc, ws,tree, mc_tot_events) fin.Close() data_in_ws = ws.data('mc_background_shape_data') if not not data_in_ws: getattr(ws,'import')(data_proper_weight) data_in_ws.append(data_proper_weight) else: getattr(ws,'import')(data_proper_weight, RooFit.Rename('mc_background_shape_data')) getattr(ws,'import')(data_proper_weight) return mc_tot_events
def save(self, where): """Save the canvas Args: where: str or TDirectory to store the canvas """ if isinstance(where, TDirectory): # check if TDirectory and store there prev_dir = gDirectory.cd() if gDirectory else None where.cd() self._canvas.Write() if prev_dir: prev_dir.cd() return # Otherwise assume where is a string and just call SaveAs self._canvas.SaveAs(where)
def saveForLimit(TightIsoOS, prefixLabel, mass, massType, fine_binning, category, susy): TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('ZZ')) TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('WZ')) noscomp = { 'DYJets' :'ZTT', 'DYJets_Electron' :'ZL', 'DYJets_Fakes' :'ZJ', 'WJets' :'W', 'TTJets' :'TT', 'WW' :'VV', 'QCDdata' :'QCD', 'Data' :'data_obs' } if susy : TightIsoOS.Hist('HiggsGGH125').Add(TightIsoOS.Hist('HiggsVBF125')) ## adding SM Higgs as a bkg TightIsoOS.Hist('HiggsGGH125').Add(TightIsoOS.Hist('HiggsVH125')) ## adding SM Higgs as a bkg noscomp.update({'HiggsGGH125':'ggH_SM125+qqH_SM125+VH_SM125'}) sigcomp = { 'HiggsSUSYBB' +str(mass):'bbH'+str(mass), 'HiggsSUSYGluGlu'+str(mass):'ggH'+str(mass), } else : sigcomp = { 'HiggsGGH'+str(mass):'ggH'+str(mass), 'HiggsVBF'+str(mass):'qqH'+str(mass), 'HiggsVH' +str(mass):'VH' +str(mass), } allcomp = {} if fine_binning : fbnoscomp = {} fbsigcomp = {} for k in noscomp.keys() : fbnoscomp.update({k:noscomp[k]+'_fine_binning'}) for k in sigcomp.keys() : fbsigcomp.update({k:sigcomp[k]+'_fine_binning'}) allcomp.update(fbnoscomp) allcomp.update(fbsigcomp) else : allcomp.update(noscomp) allcomp.update(sigcomp) fileName = '/'.join([os.getcwd(),prefixLabel,prefixLabel+'_tauTau_'+category+'_'+TightIsoOS.varName+'.root']) if TightIsoOS.varName == massType : if not os.path.isfile(fileName) : rootfile = TFile(fileName,'recreate') channel = rootfile.mkdir('tauTau_'+category) for comp in allcomp.keys() : TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) channel.Add(TightIsoOS.Hist(comp).weighted) channel.Write() else : rootfile = TFile(fileName,'update') rootfile.cd('tauTau_'+category) alreadyIn = [] dirList = gDirectory.GetListOfKeys() for k2 in dirList: h2 = k2.ReadObj() alreadyIn.append(h2.GetName()) for comp in allcomp.keys() : if comp in alreadyIn : pass TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) TightIsoOS.Hist(comp).weighted.Write() gDirectory.cd('..') rootfile.Close()
#!/usr/bin/env python import ROOT from ROOT import TFile, TTree, gDirectory, TH1F import sys if len(sys.argv) != 2: sys.exit("this program accepts one argument (the file name!)") print "Opening %s" % (sys.argv[1]) pwd = gDirectory.GetPath() file = TFile.Open(sys.argv[1]) gDirectory.cd(pwd) eventCount = file.Get("ee").Get("eventCount") eeNtuple = file.Get("ee").Get("final").Get("Ntuple") eegNtuple = file.Get("eeg").Get("final").Get("Ntuple") print "Initial: %i" % (eventCount.GetEntries()) def ecal_fiducial(eta): absEta = abs(eta) return (absEta < 1.4442 or (absEta > 1.566 and absEta < 2.5)) def trigger_req(event, i): return (event.doubleETightPass[i] == 1 and event.doubleETightPrescale[i] == 1)
#!/usr/bin/env python import ROOT from ROOT import TFile, TTree, gDirectory, TH1F import sys,cPickle if len(sys.argv) != 2: sys.exit("this program accepts one argument (the file name!)") print "Opening %s"%(sys.argv[1]) pwd = gDirectory.GetPath() file = TFile.Open(sys.argv[1]) gDirectory.cd(pwd) eventCount = file.Get("mm").Get("eventCount") mmNtuple = file.Get("mm").Get("final").Get("Ntuple") mmgNtuple = file.Get("mmg").Get("final").Get("Ntuple") print "Initial: %i"%(eventCount.GetEntries()) def trigger_req(event,i): return (event.mu17mu8Pass[i] == 1 and event.mu17mu8Prescale[i] == 1) def vtx_req(event,i): return (event.pvIsValid[i] == 1 and event.pvIsFake[i] == 0) def mu_id(event,i): return (event.m1Pt[i] > 10. and event.m2Pt[i] > 10. and event.m1AbsEta[i] < 2.4 and event.m2AbsEta[i] < 2.4 and
def saveForLimit(TightIsoOS, prefixLabel, mass, massType, fine_binning, category, susy, radion, scale_1pb, selCompsDataMass): if scale_1pb : for histo in TightIsoOS.histos : if 'Higgs' in histo.name : if susy and '125' in h.name : continue #comp_name = deepcopy(h.name) comp_name = histo.name comp_name = comp_name.replace('_pthUp','') comp_name = comp_name.replace('_pthNom','') comp_name = comp_name.replace('_pthDown','') if 'HiggsWH' in comp_name : cross_section = selCompsDataMass[mass]['HiggsVH'+str(mass)].xSectionWH elif 'HiggsZH' in comp_name : cross_section = selCompsDataMass[mass]['HiggsVH'+str(mass)].xSectionZH elif 'HiggsttH' in comp_name : cross_section = selCompsDataMass[mass]['HiggsVH'+str(mass)].xSectionttH else : cross_section = selCompsDataMass[mass][comp_name].xSection print comp_name print 'scaling Higgs to 1pb xSection', histo.name, 'integral before', histo.Integral(), 'xSection',cross_section histo.Scale(1./cross_section) print 'scaling Higgs to 1pb xSection', histo.name, 'integral after', histo.Integral() #TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('ZZ')) #TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('WZ')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('WZJetsTo2L2Q')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('WZJetsTo3LNu')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo4L')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo2L2Nu')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo2L2Q')) ## we add single top to dibosons TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('T_tW')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('Tbar_tW')) #TightIsoOS.Hist('T_tW').Add(TightIsoOS.Hist('Tbar_tW')) TightIsoOS.Hist('TTJetsFullLept').Add(TightIsoOS.Hist('TTJetsSemiLept')) TightIsoOS.Hist('TTJetsFullLept').Add(TightIsoOS.Hist('TTJetsHadronic')) if susy : TightIsoOS.Hist('DYJets').Add(TightIsoOS.Hist('TTJets_emb')) noscomp = { 'DYJets' :'ZTT', 'DYJets_ZL' :'ZL', 'DYJets_ZJ' :'ZJ', 'WJets' :'W', #'TTJets' :'TT', 'TTJetsFullLept' :'TT', #'T_tW' :'T', #'WW' :'VV', 'WWJetsTo2L2Nu' :'VV', 'QCDdata' :'QCD', 'Data' :'data_obs' #'HiggsGGHtoWW125' :'ggH_hww_SM125', #'HiggsVBFtoWW125' :'qqH_hww_SM125', #'HiggsVHtoWW125' :'VH_hww_SM125' , } if susy : sigcomp = { 'HiggsSUSYBB' +str(mass):'bbH'+str(mass), 'HiggsSUSYGluGlu'+str(mass):'ggH'+str(mass), } noscomp.update({'HiggsGGH125':'ggH_SM125'}) noscomp.update({'HiggsVBF125':'qqH_SM125'}) #noscomp.update({'HiggsVH125' :'VH_SM125' }) noscomp.update({'HiggsWH125' :'WH_SM125' }) noscomp.update({'HiggsZH125' :'ZH_SM125' }) noscomp.update({'HiggsttH125':'ttH_SM125'}) elif radion : sigcomp = { 'Radion' +str(mass):'Radion'+str(mass), } else : sigcomp = { 'HiggsGGH'+str(mass):'ggH'+str(mass), 'HiggsVBF'+str(mass):'qqH'+str(mass), #'HiggsVH' +str(mass):'VH' +str(mass), 'HiggsWH' +str(mass):'WH' +str(mass), 'HiggsZH' +str(mass):'ZH' +str(mass), 'HiggsttH'+str(mass):'ttH'+str(mass), 'HiggsGGH'+str(mass)+'_pthUp' :'ggH'+str(mass)+'_QCDscale_ggH1inUp' , #'HiggsGGH'+str(mass)+'_pthNom' :'ggH'+str(mass)+'_QCDscale_ggH1inNom' , 'HiggsGGH'+str(mass)+'_pthDown':'ggH'+str(mass)+'_QCDscale_ggH1inDown', } allcomp = {} if fine_binning : fbnoscomp = {} fbsigcomp = {} for k in noscomp.keys() : fbnoscomp.update({k:noscomp[k]+'_fine_binning'}) for k in sigcomp.keys() : fbsigcomp.update({k:sigcomp[k]+'_fine_binning'}) allcomp.update(fbnoscomp) allcomp.update(fbsigcomp) else : allcomp.update(noscomp) allcomp.update(sigcomp) fileName = '/'.join([os.getcwd(),prefixLabel,prefixLabel+'_tauTau_'+category+'_'+TightIsoOS.varName+'.root']) if TightIsoOS.varName == massType : if not os.path.isfile(fileName) : rootfile = TFile(fileName,'recreate') channel = rootfile.mkdir('tauTau_'+category) print TightIsoOS.histos for comp in allcomp.keys() : TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) channel.Add(TightIsoOS.Hist(comp).weighted) channel.Write() else : rootfile = TFile(fileName,'update') rootfile.cd('tauTau_'+category) alreadyIn = [] dirList = gDirectory.GetListOfKeys() for k2 in dirList: h2 = k2.ReadObj() alreadyIn.append(h2.GetName()) for comp in allcomp.keys() : if allcomp[comp] in alreadyIn : pass TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) TightIsoOS.Hist(comp).weighted.Write() gDirectory.cd('..') rootfile.Close()
def fhadd(self, force=False, verbose=False, slow=True): """ taken from https://root.cern.ch/phpBB3/viewtopic.php?t=14881 This function will merge objects from a list of root files and write them to a target root file. The target file is newly created and must not exist, or if -f ("force") is given, must not be one of the source files. IMPORTANT: It is required that all files have the same content! Fast but memory hungry alternative to ROOT's hadd. Arguments: target -- name of the target root file sources -- list of source root files classname -- restrict merging to objects inheriting from classname force -- overwrite target file if exists """ target = self.Name + ".root" sources = self.Jobs TH1.AddDirectory(False) # check if target file exists and exit if it does and not in force mode if not force and os.path.exists(target): raise RuntimeError("target file %s exists" % target) # open the target file print "fhadd Target file:", target outfile = TFile(target, "RECREATE") # open the seed file - contents is looked up from here seedfilename = sources[0] print "fhadd Source file 1", seedfilename seedfile = TFile(seedfilename) # get contents of seed file print "looping over seed file" contents = self.loop(seedfile) print "done %d objects are ready to be merged" % len(contents) if( verbose ): for c in contents: print c # open remaining files otherfiles = [] for n, f in enumerate(sources[1:]): #print "fhadd Source file %d: %s" % (n+2, f) otherfiles.append(TFile(f)) cut = "" #(CMS_hgg_mass > 100 && CMS_hgg_mass < 180) && (diphoMVA > -0.4) && (n_loose_ele == 1 || n_LooseMu25 == 1) && (MET_pt > 30)" # loop over contents and merge objects from other files to seed file objects for n, (path, hname) in enumerate(contents): #print "fhadd Target object: %s" % os.path.join(path, hname) obj_path = os.path.join(path, hname) obj_ = seedfile.Get(obj_path[1:]) outfile.cd('/') # create target directory structure for d in path.split('/')[1:]: directory = gDirectory.GetDirectory(d) if not directory: gDirectory.mkdir(d).cd() else: gDirectory.cd(d) obj = None IsTree = False #if "sigma" in obj_.GetName(): # continue if obj_.InheritsFrom("TTree"): #obj = obj_.CloneTree() obj = obj_.CopyTree( cut ) IsTree = True else: continue obj = obj_.Clone() # merge objects l = TList() for o in [of.Get(obj_path[1:]) for of in otherfiles]: if IsTree : l.Add( o.CopyTree( cut ) ) else : l.Add(o) obj.Merge(l) # delete objects if in slow mode if slow: #print "Deleting %d object(s)", l.GetEntries() l.Delete() # write object to target obj.Write(obj.GetName(), TObject.kOverwrite) print "Writing and closing file" # let ROOT forget about open files - prevents deletion of TKeys for f in [outfile, seedfile]+otherfiles: gROOT.GetListOfFiles().Remove(f); outfile.Write() outfile.Close() for f in [seedfile]+otherfiles: f.Close() print "fhadd completed"
def analyse_scenario(file_baseline, file_selection, ROOT_file, scenario_names, memory_cache, int_lumi): """Analyse a specific scenario. We receive in input two json files, @file_baseline and @file_selection, and perform a standard set of plot of information extracted from @file_selection versus information extracted from @file_baseline. """ gDirectory.cd('/') folder = '%s_vs_%s' % (scenario_names[int(re.match('json_(\d+).*', os.path.splitext(file_baseline)[0]).group(1))], scenario_names[int(re.match('json_(\d+).*', os.path.splitext(file_selection)[0]).group(1))]) gDirectory.mkdir(folder) gDirectory.cd(folder) full_file = open(file_baseline, 'r') full_file_content = [''.join(l) for l in full_file.readlines()] full_object = cjson.decode(full_file_content[0]) selection_file = open(file_selection, 'r') selection_file_content = [''.join(l) for l in selection_file.readlines()] selection_object = cjson.decode(selection_file_content[0]) sum_inv_tot_w = 0 w_eff_rec_tot_over_del_tot = 0 w_eff_rec_tot_over_del_tot_numerator = 0 sum_inv_selection_w = 0 w_eff_rec_selection_over_rec_tot = 0 w_eff_rec_selection_over_rec_tot_numerator = 0 all_del_selection_lumi = 0 all_rec_selection_lumi = 0 run_list = [] list_del_tot_lumi = [] list_del_lumi = [] list_eff_rec_tot_over_del_tot = [] list_w_eff_rec_tot_over_del_tot = [] list_eff_rec_selection_over_rec_tot = [] list_w_eff_rec_selection_over_rec_tot = [] run_count = 0 print "\n%s vs %s" % (file_baseline, file_selection) for run in sorted(full_object.keys()): run_count += 1 if run_count%25 == 0: sys.stdout.write('.') sys.stdout.flush() run_list.append(int(run)) (del_tot_lumi, rec_tot_lumi) = get_luminosity_cached_from_memory(run, full_object[run], memory_cache, certified=True) (eff_rec_tot_over_del_tot, w_eff_rec_tot_over_del_tot, w_mean_numerator, sum_w) = compute_efficiencies(del_tot_lumi, rec_tot_lumi, rec_tot_lumi, w_eff_rec_tot_over_del_tot_numerator, sum_inv_tot_w) w_eff_rec_tot_over_del_tot_numerator = w_mean_numerator sum_inv_tot_w = sum_w list_eff_rec_tot_over_del_tot.append(eff_rec_tot_over_del_tot) list_w_eff_rec_tot_over_del_tot.append(w_eff_rec_tot_over_del_tot) list_del_tot_lumi.append(int_lumi[run][0]) list_del_lumi.append(del_tot_lumi) if run in selection_object.keys(): (del_selection_lumi, rec_selection_lumi) = get_luminosity_cached_from_memory(run, selection_object[run], memory_cache, certified=True) else: (del_selection_lumi, rec_selection_lumi) = (0., 0.) all_del_selection_lumi += del_selection_lumi all_rec_selection_lumi += rec_selection_lumi (eff_rec_selection_over_rec_tot, w_eff_rec_selection_over_rec_tot, w_mean_numerator, sum_w) = compute_efficiencies(rec_tot_lumi, rec_selection_lumi, rec_tot_lumi, w_eff_rec_selection_over_rec_tot_numerator, sum_inv_selection_w) w_eff_rec_selection_over_rec_tot_numerator = w_mean_numerator sum_inv_selection_w = sum_w list_eff_rec_selection_over_rec_tot.append(eff_rec_selection_over_rec_tot) list_w_eff_rec_selection_over_rec_tot.append(w_eff_rec_selection_over_rec_tot) x_axis_list = [(run_list, 'Vs_Run'), (list_del_tot_lumi, 'Vs_IntLumi'), (list_del_lumi, 'Vs_Lumi')] for x_type in x_axis_list: gDirectory.cd('/%s' % folder) x_folder = '%s' % x_type[1] gDirectory.mkdir('%s' % x_folder) gDirectory.cd('%s' % x_folder) ROOTsave(x_type[0], [{'values': list_eff_rec_tot_over_del_tot, 'y_label': 'rec_tot_vs_del_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_eff_rec_selection_over_rec_tot, 'y_label': 'rec_selection_vs_rec_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_w_eff_rec_tot_over_del_tot, 'y_label': 'running_wm_rec_tot_vs_del_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_w_eff_rec_selection_over_rec_tot, 'y_label': 'running_wm_rec_selection_vs_rec_tot', 'y_range': [0., 1.05], 'y_projection': True}, ])
print 'file: ', str(m) if str(m).find("_SM1_") > 0: folder = "tauTau_2012_SM1" folderNew = "tauTau_boost" if str(m).find("_SM2_") > 0: folder = "tauTau_2012_SM2" folderNew = "tauTau_vbf" if str(m).find("_SM0_") > 0: folder = "tauTau_2012_SM1" folderNew = "tauTau_incl" if str(m).find("_SM3_") > 0: folder = "tauTau_2012_SM1" folderNew = "tauTau_vh" myFile = TFile.Open(m,'read') gDirectory.cd(folder) dirList = gDirectory.GetListOfKeys() print 'mass', mass if mass == 125 : for k1 in dirList : #histo = k1.ReadObj() histo = copy.deepcopy(k1.ReadObj()) print 'name ', str(histo.GetName()) if str(histo.GetName()) == 'QCD' : for bin in range(histo.GetNbinsX()) : if histo.GetBinContent(bin) < 0.001 : histo.SetBinContent(bin,0.001) alpha = 1 - 0.6827 ### 68% quantile of Poisson distributoin #binError = 0.20*Math.gamma_quantile_c(alpha/2,1,1)
def build_category_workspaces(ws_list,metadata): pwd = gDirectory.GetPath() assoc = metadata.getAssociation() category_type = os.environ['hzgcategorytype'] signal_type = os.environ['hzgsignalmodel'] background_type = os.environ['hzgbkgmodel'] categories = categories_map[category_type]['categories'] catname = categories_map[category_type]['leafname'] for (sample,chanlist) in ws_list.iteritems(): for (channel,chaninfo) in chanlist.iteritems(): master_file = TFile.Open(chaninfo['filename'],'read') gDirectory.cd(pwd) master_ws = master_file.Get('%s-%s'%(channel,sample)) processes = chaninfo.keys() processes.remove('filename') #make workspaces for each category cat_workspaces = {} if not len(processes): continue for category in categories: cat_workspaces[category] =\ RooWorkspace('%s-%s-cat%i'%(channel, sample, category), '%s-%s-cat%i'%(channel, sample, category)) cat_ws = cat_workspaces[category] cat_data = master_ws.data('%s_data'%channel)\ .reduce('%s == %i'%(catname,category)) getattr(cat_ws,'import')( cat_data, RooFit.Rename('%s_data_cat%i'%(channel,category)) ) cat_bkg = master_ws.data('mc_background_shape_data')\ .reduce('%s == %i'%(catname,category)) getattr(cat_ws,'import')(cat_bkg) #make background model bkg_mdl = make_background_for_cat(category_type, category, background_type) for line in bkg_mdl: cat_workspaces[category].factory(line) #build background model + data workspaces for category in categories: #write out category information cat_file = TFile.Open( '%s_%s_data_category_%i_workspace.root'\ %(channel,sample,category), 'recreate') cat_workspaces[category].Write() cat_file.Close() #generate signal workspaces for each mass point #and category acceptances = {} proc_workspaces = {} for process in processes: acceptance = TGraph(0) acceptance.SetName('g%s_acceff'%process) proc_workspaces[process] = RooWorkspace('%s-%s-%s'%(channel, sample, process)) proc_name_short = process.split('To')[0] subproc_list = chaninfo[process] print sample, channel, process masses = {} for subproc in subproc_list: masses[assoc[sample][channel][process]\ [subproc]['mass']] = subproc for k,mass in enumerate(sorted(masses.keys())): friendly_mass_name = str(mass).replace('.','p') sig_fit_range = 'fit_%s'%friendly_mass_name subproc = masses[mass] #get acc*eff (calculated in master ws) acceptance.SetPoint( k,mass, master_ws.var('%s_acceff'%subproc).getVal() ) for category in categories: #get and import signal dataset for this cat cat_sig_nopu = master_ws.data( '%s_shape_data_puonly'%subproc ).reduce('%s == %i'%(catname,category)) cat_sig = master_ws.data( '%s_shape_data'%subproc ).reduce('%s == %i'%(catname,category)) getattr(proc_workspaces[process],'import')( cat_sig_nopu, RooFit.Rename( '%s_shape_data_puonly_cat%i'%(subproc,category) ) ) getattr(proc_workspaces[process],'import')( cat_sig, RooFit.Rename('%s_shape_data_cat%i'%(subproc, category)) ) #build signal model sig_model = make_signal_for_cat(category_type, category, signal_type, mass, proc_name_short) for line in sig_model: proc_workspaces[process].factory(line) #end dataset import and model defintions #setup fitting range proc_workspaces[process].var("Mzg").setRange( sig_fit_range, mass-30,mass+30 ) #for each mass and category in this process #fit the signal datasets for k,mass in enumerate(sorted(masses.keys())): subproc = masses[mass] mname = str(mass).replace('.','p') for category in categories: #fit the signal model to pu-only weighted data sig_model = 'signal_model_m%s_%s_cat%i'%( mname, proc_name_short, category ) sig_data = proc_workspaces[process].data( '%s_shape_data_puonly_cat%i'%(subproc,category) ) sig_pdf = proc_workspaces[process].pdf(sig_model) # fit in range around peak to remove outliers that # cause pdf to be zero # get errors proportional to number of actual events #sig_pdf.fitTo(sig_data, # RooFit.Range(sig_fit_range), # RooFit.SumW2Error(True) # ) #set all fitted parameters constant params = sig_pdf.getParameters(sig_data) params_it = params.iterator() while ( not not params_it.Next() ): params_it.setConstant(True) del params #embed acceptance data in workspace getattr(proc_workspaces[process],'import')(acceptance) for process in processes: #write out category information proc_file = TFile.Open( '%s_%s_process_%s_workspace.root'\ %(channel,sample,process), 'recreate') proc_workspaces[process].Write() proc_file.Close() #close channel master_file.Close()
def fhadd(self, prefix="", force=False, verbose=False, slow=True): """ taken from https://root.cern.ch/phpBB3/viewtopic.php?t=14881 This function will merge objects from a list of root files and write them to a target root file. The target file is newly created and must not exist, or if -f ("force") is given, must not be one of the source files. IMPORTANT: It is required that all files have the same content! Fast but memory hungry alternative to ROOT's hadd. Arguments: target -- name of the target root file sources -- list of source root files classname -- restrict merging to objects inheriting from classname force -- overwrite target file if exists """ target = prefix + self.Name + ".root" sources = [j.Output for j in self.Jobs] TH1.AddDirectory(False) # check if target file exists and exit if it does and not in force mode if not force and os.path.exists(target): raise RuntimeError("target file %s exists" % target) # open the target file print "fhadd Target file:", target outfile = TFile(target, "RECREATE") # open the seed file - contents is looked up from here seedfilename = sources[0] print "fhadd Source file 1", seedfilename seedfile = TFile(seedfilename) # get contents of seed file print "looping over seed file" contents = self.loop(seedfile) print "done %d objects are ready to be merged" % len(contents) if (verbose): for c in contents: print c # open remaining files otherfiles = [] for n, f in enumerate(sources[1:]): print "fhadd Source file %d: %s" % (n + 2, f) otherfiles.append(TFile(f)) # loop over contents and merge objects from other files to seed file objects for n, (path, hname) in enumerate(contents): print "fhadd Target object: %s" % os.path.join(path, hname) obj_path = os.path.join(path, hname) obj_ = seedfile.Get(obj_path[1:]) outfile.cd('/') # create target directory structure for d in path.split('/')[1:]: directory = gDirectory.GetDirectory(d) if not directory: gDirectory.mkdir(d).cd() else: gDirectory.cd(d) obj = None if obj_.InheritsFrom("TTree"): obj = obj_.CloneTree() else: obj = obj_.Clone() # merge objects l = TList() for o in [of.Get(obj_path[1:]) for of in otherfiles]: l.Add(o) obj.Merge(l) # delete objects if in slow mode if slow: print "Deleting %d object(s)", l.GetEntries() l.Delete() # write object to target obj.Write(obj.GetName(), TObject.kOverwrite) print "Writing and closing file" # let ROOT forget about open files - prevents deletion of TKeys for f in [outfile, seedfile] + otherfiles: gROOT.GetListOfFiles().Remove(f) outfile.Write() outfile.Close() for f in [seedfile] + otherfiles: f.Close()
def run(self): if not os.path.isfile(self.input_filename): print("Cannot find %s. Exiting.".format(self.input_filename)) sys.exit() tfile_input = ROOT.TFile(self.input_filename) tfile_input.cd() # gDirectory.cd('VirtualDetector') gDirectory.cd('Detector') keys = [key.GetName() for key in gDirectory.GetListOfKeys()] detector_events = {} for key in gDirectory.GetListOfKeys(): detector_events[key.GetName()] = key.ReadObj() for detector, events in detector_events.items(): events.BuildIndex('EventID', 'TrackID') # ROOT prevents building a single tree that is too large. for category, detectors in self.category_detectors.items(): line_to_process = 'struct ' + category + ' {Int_t EventID; Int_t TrackID; ' for variable in self.variables: data_type = 'Float_t' if variable == 'EventID' or variable == 'TrackID': data_type = 'Int_t' for detector in detectors: line_to_process += data_type + ' ' + variable + detector + '; ' line_to_process += '};' ROOT.gROOT.ProcessLine(line_to_process) category_structs = {} for category in self.category_detectors.keys(): category_structs[category] = eval('ROOT.' + category + '()') for category, detectors in self.category_detectors.items(): for detector in detectors: for variable in self.variables: events = detector_events[detector] events.SetBranchAddress( variable, ROOT.AddressOf(category_structs[category], variable + detector)) tfile_output = ROOT.TFile(self.output_filename, 'RECREATE') spill_trees = {} pointers = {} track_count = 0 spill_count = 0 for event in detector_events[self.starter_tree]: track_count += 1 (event_id, track_id) = (int(event.EventID), int(event.TrackID)) spill = 1 + (event_id // self.spill_size) if self.gamma_cutoff > 0. and event.PDGid == 22: energy = (event.Px**2 + event.Py**2 + event.Pz**2)**0.5 if energy < self.gamma_cutoff: continue if spill not in spill_trees: spill_count += 1 if spill_count > self.max_spill > 0: break self.add_tree(spill_trees, spill, pointers) pointers[spill, 'SpillID'][0] = spill pointers[spill, 'EventID'][0] = event_id pointers[spill, 'TrackID'][0] = track_id for detector, events in detector_events.items(): entry_number = events.GetEntryWithIndex(event_id, track_id) track_present = not (entry_number == -1) pointers[spill, 'TrackPresent' + detector][0] = track_present for variable in self.variables: if variable == 'EventID' or variable == 'TrackID': continue variable_detector = variable + detector value = MergeTree.DEFAULT_VALUE if track_present: value = getattr( category_structs[ self.detector_categories[detector]], variable_detector) if variable == 't': random.seed(event_id) value = value * 1.e-9 + self.spill_interval * float( spill) + self.random_offset_seconds() pointers[spill, variable_detector][0] = value spill_trees[spill].Fill() print('{} total tracks in {}'.format(track_count, self.starter_tree)) print('{} total spills'.format(spill_count - 1)) tfile_output.cd() for tree in spill_trees.values(): tree.Write() tfile_output.Close() tfile_input.Close()
def prepare_truth_models(ws,cat,mass,channel,turnon,truth): if channel in study_inputs: bkg_data = RooDataSet("bkgdata_%s"%(channel), "M_{ll#gamma} with Errors", ws.set("observables_weight"), "weight") for k,file in enumerate(study_inputs[channel]): f_in = TFile.Open(file,"READ") gDirectory.cd(pwd) n_events = f_in.Get("eventCount").GetBinContent(1) tree = f_in.Get("selected_zg").CloneTree() tree.SetName("tree_%s_%i"%(channel,k)) f_in.Close() d_in = RooDataSet("temp", "M_{ll#gamma} with Errors", tree, ws.set("observables")) norm = RooConstVar("norm","norm",n_events) weight = RooFormulaVar("weight", "weight", "1.3*@0*@1/@2", #1.3 gives 19.6/fb RooArgList(ws.var("puWeight"), ws.var("procWeight"), norm) ) d_in.addColumn(weight) d_in_weight = RooDataSet("temp_weight", "M_{ll#gamma} with Errors", d_in, ws.set("observables_weight"), '','weight') bkg_data.append(d_in_weight) # split off data for each category, create the # toy dataset truth models data = bkg_data.reduce("Mz + Mzg > 185 && r94cat == %i"%cat) getattr(ws,'import')(data, RooFit.Rename('bkgdata_%s_%i'%(channel, cat))) nevts = data.sumEntries('Mzg > %f && Mzg < %f'%(mass-1.5,mass+1.5)) #for sigm turn on we want erf truth if turnon == 'sigm' and truth == 'exp': #make RooDecay 'truth' model with erf turn on ws.factory( 'RooGaussModel::MzgResoShape_exp_erf_%s_cat%i(Mzg,'\ 'bias_exp_erf_%s_cat%i[120,90,150],sigma_exp_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooDecay::MzgTruthModelBase_exp_erf_%s_cat%i(Mzg,'\ 'tau_erf_%s_cat%i[25,0,50],MzgResoShape_exp_erf_%s_cat%i,' 'RooDecay::SingleSided)'%(channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_erf_%s_cat%i('\ 'MzgTruthModelBase_exp_erf_%s_cat%i,'\ 'norm_truth_exp_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) if turnon == 'sigm' and truth == 'pow': #make power-law truth model with erf turn on ws.factory('EXPR::MzgTruthModelShape_pow_erf_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_erf_%s_cat%i[105,100,130],'\ 'pow_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooGaussModel::MzgResoShape_pow_erf_%s_cat%i(Mzg,'\ 'bias_pow_erf_%s_cat%i[0],sigma_pow_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory('FCONV::MzgTruthModelBase_pow_erf_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_erf_%s_cat%i,'\ 'MzgResoShape_pow_erf_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_erf_%s_cat%i('\ 'MzgTruthModelBase_pow_erf_%s_cat%i,'\ 'norm_truth_pow_erf_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) #for erf fitting turn on we want sigmoid truth if turnon == 'erf' and truth == 'exp': #build exponential convoluted with sigmoid turn-on ws.factory('RooStepExponential::MzgTruthModelShape_exp_sigm_%s_cat%i'\ '(Mzg,tau_sigm_%s_cat%i[-0.05,-10,0],'\ 'step_exp_sigm_%s_cat%i[110,100,130])'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooLogistics::MzgResoShape_exp_sigm_%s_cat%i(%s)'%( channel,cat, ','.join(['Mzg', 'bias_exp_sigm_%s_cat%i[0]'%(channel,cat), 'sigma_exp_sigm_%s_cat%i[5,0.01,20]'%(channel,cat)]) ) ) ws.factory('FCONV::MzgTruthModelBase_exp_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_exp_sigm_%s_cat%i,'\ 'MzgResoShape_exp_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_sigm_%s_cat%i('\ 'MzgTruthModelBase_exp_sigm_%s_cat%i,'\ 'norm_truth_exp_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) if turnon == 'erf' and truth == 'pow': #build power-law convoluted with sigmoid turn-on ws.factory('EXPR::MzgTruthModelShape_pow_sigm_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_sigm_%s_cat%i[105,100,130],'\ 'pow_sigm_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooLogistics::MzgResoShape_pow_sigm_%s_cat%i(%s)'%( channel,cat, ','.join(['Mzg', 'bias_pow_sigm_%s_cat%i[0]'%(channel,cat), 'sigma_pow_sigm_%s_cat%i[5,0.01,20]'%(channel,cat)]) ) ) ws.factory('FCONV::MzgTruthModelBase_pow_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_sigm_%s_cat%i,'\ 'MzgResoShape_pow_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_sigm_%s_cat%i('\ 'MzgTruthModelBase_pow_sigm_%s_cat%i,'\ 'norm_truth_pow_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) )
def recurse_thru_file(in_tfile, options, full_path='/'): '''Recursive function to find all contents in a given ROOT file''' keys = in_tfile.GetDirectory(full_path).GetListOfKeys() for key in keys: name = key.GetName() classname = key.GetClassName() if 'TDirectory' in classname: gDirectory.cd(name) recurse_thru_file(in_tfile, options, '/'.join([full_path, name])) gDirectory.cd("..") else: if options.name and name != options.name: continue full_name = '/'.join([full_path, name]) obj = in_tfile.Get(full_name) if not obj: continue simple_name = full_name[2:] print "%s" % simple_name, for arg in [x[2:] for x in sys.argv if x.startswith("--")]: if "classname" == arg: print "%s" % classname, if obj.InheritsFrom('TH1'): if "entries" == arg: print " %i" % obj.GetEntries(), if "contents" == arg: if obj.InheritsFrom('TH2'): # Print contents as they would look on the 2D graph # Left to right, top to bottom. Start in upper left corner. for j in reversed(range(obj.GetNbinsY())): print print " %s" % ' '.join([ str(obj.GetBinContent(i + 1, j + 1)) for i in range(obj.GetNbinsX()) ]), else: print " %s" % ' '.join([ str(obj.GetBinContent(i + 1)) for i in range(obj.GetNbinsX()) ]), if "errors" == arg: if obj.InheritsFrom('TH2'): for j in reversed(range(obj.GetNbinsY())): print print " %s" % ' '.join([ str(obj.GetBinError(i + 1, j + 1)) for i in range(obj.GetNbinsX()) ]), else: print " %s" % ' '.join([ str(obj.GetBinError(i + 1)) for i in range(obj.GetNbinsX()) ]), if "bincenter" == arg: print " %s" % ' '.join([ str(obj.GetBinCenter(i + 1)) for i in range(obj.GetNbinsX()) ]), if "max" == arg: print " %i" % obj.GetMaximum(), if "min" == arg: print " %i" % obj.GetMinimum(), if "overflow" == arg: print " %i" % obj.GetBinContent(obj.GetNbinsX()), if "underflow" == arg: print " %i" % obj.GetBinContent(0), if obj.InheritsFrom('TGraph'): if "contents" == arg: x, y = Double(0), Double(0) xvals = [] yvals = [] for i in range(obj.GetN()): obj.GetPoint(i, x, y) xvals.append(copy.copy(x)) yvals.append(copy.copy(y)) for point in zip(xvals, yvals): print " (%d, %d)" % point, print ""
def recurse_thru_file(in_tfile, options, full_path='/'): '''Recursive function to find all contents in a given ROOT file''' keys = in_tfile.GetDirectory(full_path).GetListOfKeys() for key in keys: name = key.GetName() classname = key.GetClassName() if 'TDirectory' in classname: gDirectory.cd(name) recurse_thru_file(in_tfile, options, '/'.join([full_path,name])) gDirectory.cd("..") else: if options.name and name != options.name: continue full_name = '/'.join([full_path,name]) obj = in_tfile.Get(full_name) if not obj: continue simple_name = full_name[2:] print("%s" % simple_name, end=' ') for arg in [x[2:] for x in sys.argv if x.startswith("--")]: if "classname" == arg: print("%s" % classname, end=' ') if obj.InheritsFrom('TH1'): if "entries" == arg: print(" %i" % obj.GetEntries(), end=' ') if "contents" == arg: if obj.InheritsFrom('TH2'): # Print contents as they would look on the 2D graph # Left to right, top to bottom. Start in upper left corner. for j in reversed(list(range(obj.GetNbinsY()))): print() print(" %s" % ' '.join( [str(obj.GetBinContent(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ') else: print(" %s" % ' '.join( [str(obj.GetBinContent(i+1)) for i in range(obj.GetNbinsX())]), end=' ') if "errors" == arg: if obj.InheritsFrom('TH2'): for j in reversed(list(range(obj.GetNbinsY()))): print() print(" %s" % ' '.join( [str(obj.GetBinError(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ') else: print(" %s" % ' '.join( [str(obj.GetBinError(i+1)) for i in range(obj.GetNbinsX())]), end=' ') if "bincenter" == arg: print(" %s" % ' '.join( [str(obj.GetBinCenter(i+1)) for i in range(obj.GetNbinsX())]), end=' ') if "max" == arg: print(" %i" % obj.GetMaximum(), end=' ') if "min" == arg: print(" %i" % obj.GetMinimum(), end=' ') if "overflow" == arg: print(" %i" % obj.GetBinContent(obj.GetNbinsX()), end=' ') if "underflow" == arg: print(" %i" % obj.GetBinContent(0), end=' ') if obj.InheritsFrom('TGraph'): if "contents" == arg: x, y = Double(0), Double(0) xvals = [] yvals = [] for i in range(obj.GetN()): obj.GetPoint(i, x, y) xvals.append(copy.copy(x)) yvals.append(copy.copy(y)) for point in zip(xvals,yvals): print(" (%d, %d)" % point, end=' ') print("")
def saveForLimit(TightIsoOS, prefixLabel, mass, massType, fine_binning, category, susy): #TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('ZZ')) #TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('WZ')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('WZJetsTo2L2Q')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('WZJetsTo3LNu')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo4L')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo2L2Nu')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('ZZJetsTo2L2Q')) ## we add single top to dibosons TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('T_tW')) TightIsoOS.Hist('WWJetsTo2L2Nu').Add(TightIsoOS.Hist('Tbar_tW')) #TightIsoOS.Hist('T_tW').Add(TightIsoOS.Hist('Tbar_tW')) TightIsoOS.Hist('TTJetsFullLept').Add(TightIsoOS.Hist('TTJetsSemiLept')) TightIsoOS.Hist('TTJetsFullLept').Add(TightIsoOS.Hist('TTJetsHadronic')) if susy : TightIsoOS.Hist('DYJets').Add(TightIsoOS.Hist('TTJets_emb')) noscomp = { 'DYJets' :'ZTT', 'DYJets_ZL' :'ZL', 'DYJets_ZJ' :'ZJ', 'WJets' :'W', #'TTJets' :'TT', 'TTJetsFullLept' :'TT', #'T_tW' :'T', #'WW' :'VV', 'WWJetsTo2L2Nu' :'VV', 'QCDdata' :'QCD', 'Data' :'data_obs' #'HiggsGGHtoWW125' :'ggH_hww_SM125', #'HiggsVBFtoWW125' :'qqH_hww_SM125', #'HiggsVHtoWW125' :'VH_hww_SM125' , } if susy : sigcomp = { 'HiggsSUSYBB' +str(mass):'bbH'+str(mass), 'HiggsSUSYGluGlu'+str(mass):'ggH'+str(mass), } noscomp.update({'HiggsGGH125':'ggH_SM125'}) noscomp.update({'HiggsVBF125':'qqH_SM125'}) noscomp.update({'HiggsVH125' :'VH_SM125' }) else : sigcomp = { 'HiggsGGH'+str(mass):'ggH'+str(mass), 'HiggsVBF'+str(mass):'qqH'+str(mass), 'HiggsVH' +str(mass):'VH' +str(mass), 'HiggsGGH'+str(mass)+'_pthUp' :'ggH'+str(mass)+'_QCDscale_ggH1inUp' , #'HiggsGGH'+str(mass)+'_pthNom' :'ggH'+str(mass)+'_QCDscale_ggH1inNom' , 'HiggsGGH'+str(mass)+'_pthDown':'ggH'+str(mass)+'_QCDscale_ggH1inDown', } allcomp = {} if fine_binning : fbnoscomp = {} fbsigcomp = {} for k in noscomp.keys() : fbnoscomp.update({k:noscomp[k]+'_fine_binning'}) for k in sigcomp.keys() : fbsigcomp.update({k:sigcomp[k]+'_fine_binning'}) allcomp.update(fbnoscomp) allcomp.update(fbsigcomp) else : allcomp.update(noscomp) allcomp.update(sigcomp) fileName = '/'.join([os.getcwd(),prefixLabel,prefixLabel+'_tauTau_'+category+'_'+TightIsoOS.varName+'.root']) if TightIsoOS.varName == massType : if not os.path.isfile(fileName) : rootfile = TFile(fileName,'recreate') channel = rootfile.mkdir('tauTau_'+category) print TightIsoOS.histos for comp in allcomp.keys() : TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) channel.Add(TightIsoOS.Hist(comp).weighted) channel.Write() else : rootfile = TFile(fileName,'update') rootfile.cd('tauTau_'+category) alreadyIn = [] dirList = gDirectory.GetListOfKeys() for k2 in dirList: h2 = k2.ReadObj() alreadyIn.append(h2.GetName()) for comp in allcomp.keys() : if allcomp[comp] in alreadyIn : pass TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) TightIsoOS.Hist(comp).weighted.Write() gDirectory.cd('..') rootfile.Close()
import inspect def lineno(): '''Returns the current line number in our program.''' print inspect.currentframe().f_back.f_lineno gROOT.SetBatch(True) grandeFile = TFile.Open('htt_tt.inputs-sm-8TeV.root','recreate') grandeFile.mkdir("tauTau_boost") grandeFile.mkdir("tauTau_vbf") myFile = TFile.Open('htt_tt.inputs-sm-8TeV.root','read') #myFile = TFile.Open('tauTau.root','read') gDirectory.cd() listOfDir = gDirectory.GetListOfKeys() for fol1 in listOfDir : folObj = fol1.ReadObj() folder = folObj.GetName() #for folder in ['tauTau_boost','tauTau_vbf'] : #for folder in ['tauTau_boost'] : #for folder in ['tauTau_vbf'] : #lineno() myFile.cd(folder) dirList = gDirectory.GetListOfKeys()
data_obs = dataHist.Clone("data_obs") rebinnedData = data_obs.Rebin(len(binning) - 1, "", binning) ## input to combine , open root file if template: myfile = TFile("%s%s.root" % (plotDirectory, myfilename), "update") # i have to get the nominal histogram from root file first and get the integration value if systematics == '': myDatahist = rebinnedData.Clone("nominal") mydataDir = "%s/data_obs/" % channel if myfile.GetDirectory(mydataDir): gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() else: gDirectory.mkdir(mydataDir) gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() # create directory only if it does not exist ### ele channel for iprocess in template_category.keys(): myfile.cd() mydir = "%s/%s/" % (channel, iprocess) #print "%s/%s/"%(channel,iprocess)
def saveForLimit(TightIsoOS, prefixLabel, mass, massType, fine_binning, category, susy): TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('ZZ')) TightIsoOS.Hist('WW').Add(TightIsoOS.Hist('WZ')) noscomp = { 'DYJets': 'ZTT', 'DYJets_Electron': 'ZL', 'DYJets_Fakes': 'ZJ', 'WJets': 'W', 'TTJets': 'TT', 'WW': 'VV', 'QCDdata': 'QCD', 'Data': 'data_obs' } if susy: TightIsoOS.Hist('HiggsGGH125').Add( TightIsoOS.Hist('HiggsVBF125')) ## adding SM Higgs as a bkg TightIsoOS.Hist('HiggsGGH125').Add( TightIsoOS.Hist('HiggsVH125')) ## adding SM Higgs as a bkg noscomp.update({'HiggsGGH125': 'ggH_SM125+qqH_SM125+VH_SM125'}) sigcomp = { 'HiggsSUSYBB' + str(mass): 'bbH' + str(mass), 'HiggsSUSYGluGlu' + str(mass): 'ggH' + str(mass), } else: sigcomp = { 'HiggsGGH' + str(mass): 'ggH' + str(mass), 'HiggsVBF' + str(mass): 'qqH' + str(mass), 'HiggsVH' + str(mass): 'VH' + str(mass), } allcomp = {} if fine_binning: fbnoscomp = {} fbsigcomp = {} for k in noscomp.keys(): fbnoscomp.update({k: noscomp[k] + '_fine_binning'}) for k in sigcomp.keys(): fbsigcomp.update({k: sigcomp[k] + '_fine_binning'}) allcomp.update(fbnoscomp) allcomp.update(fbsigcomp) else: allcomp.update(noscomp) allcomp.update(sigcomp) fileName = '/'.join([ os.getcwd(), prefixLabel, prefixLabel + '_tauTau_' + category + '_' + TightIsoOS.varName + '.root' ]) if TightIsoOS.varName == massType: if not os.path.isfile(fileName): rootfile = TFile(fileName, 'recreate') channel = rootfile.mkdir('tauTau_' + category) for comp in allcomp.keys(): TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) channel.Add(TightIsoOS.Hist(comp).weighted) channel.Write() else: rootfile = TFile(fileName, 'update') rootfile.cd('tauTau_' + category) alreadyIn = [] dirList = gDirectory.GetListOfKeys() for k2 in dirList: h2 = k2.ReadObj() alreadyIn.append(h2.GetName()) for comp in allcomp.keys(): if comp in alreadyIn: pass TightIsoOS.Hist(comp).weighted.SetName(allcomp[comp]) TightIsoOS.Hist(comp).weighted.Write() gDirectory.cd('..') rootfile.Close()
for bin in (0,20,40,60,80,100,120,140,160,180,200,250,300,350): binningV.append(bin) binningVH = array.array('d') for bin in (0,15,30,45,60,75,90,105,120,135,150,165,180,195,210,250,300,350): binningVH.append(bin) grandeFile = TFile.Open('htt_tt.inputs-sm-8TeV.root','read') histosB = [] histosV = [] for directory, binning, hists in [ ['tauTau_boost',binningB,histosB],['tauTau_vbf',binningV,histosV] ] : print directory print binning gDirectory.cd(directory) dirList = gDirectory.GetListOfKeys() for k1 in dirList : histo = copy.deepcopy(k1.ReadObj()) if histo.GetNbinsX() > 0: histo = histo.Rebin(len(binning)-1,histo.GetName(),binning) histo2 = copy.deepcopy(histo) for bin in range(histo2.GetNbinsX()+1) : if 'QCD' in histo2.GetName() or 'ZTT' in histo2.GetName() and not 'tau' in histo2.GetName(): if histo2.GetBinContent(bin)<0.001: histo2.SetBinContent(bin,0.001) histo2.SetBinError(bin,1.) hists.append(histo2) gDirectory.cd('..') grandeFile.Close()
sys.path.append('python') ifile = sys.argv[1] cfgfile = sys.argv[2] ofile = sys.argv[3] iFILE = rt.TFile(ifile) oFILE = rt.TFile(ofile, "RECREATE") cfg = imp.load_source('final', cfgfile) for _cfg in cfg.cfg: hist = _cfg.getHist(iFILE) if not gDirectory.GetDirectory(_cfg.collection): gDirectory.mkdir(_cfg.collection + "/" + _cfg.ID) gDirectory.cd(_cfg.collection + "/" + _cfg.ID) else: gDirectory.cd(_cfg.collection) if not gDirectory.GetDirectory(_cfg.ID): gDirectory.mkdir(_cfg.ID) gDirectory.cd(_cfg.ID) for h in hist: h.Write() #CANVAS.Print(_cfg.collection + "_" + _cfg.ID + "_" + h.GetName() + ".pdf") gDirectory.cd("/") oFILE.Close()
def prepare_truth_models(ws, cat, mass, channel, turnon, truth): if channel in study_inputs: bkg_data = RooDataSet("bkgdata_%s" % (channel), "M_{ll#gamma} with Errors", ws.set("observables_weight"), "weight") for k, file in enumerate(study_inputs[channel]): f_in = TFile.Open(file, "READ") gDirectory.cd(pwd) n_events = f_in.Get("eventCount").GetBinContent(1) tree = f_in.Get("selected_zg").CloneTree() tree.SetName("tree_%s_%i" % (channel, k)) f_in.Close() d_in = RooDataSet("temp", "M_{ll#gamma} with Errors", tree, ws.set("observables")) norm = RooConstVar("norm", "norm", n_events) weight = RooFormulaVar( "weight", "weight", "1.3*@0*@1/@2", #1.3 gives 19.6/fb RooArgList(ws.var("puWeight"), ws.var("procWeight"), norm)) d_in.addColumn(weight) d_in_weight = RooDataSet("temp_weight", "M_{ll#gamma} with Errors", d_in, ws.set("observables_weight"), '', 'weight') bkg_data.append(d_in_weight) # split off data for each category, create the # toy dataset truth models data = bkg_data.reduce("Mz + Mzg > 185 && r94cat == %i" % cat) getattr(ws, 'import')(data, RooFit.Rename('bkgdata_%s_%i' % (channel, cat))) nevts = data.sumEntries('Mzg > %f && Mzg < %f' % (mass - 1.5, mass + 1.5)) #for sigm turn on we want erf truth if turnon == 'sigm' and truth == 'exp': #make RooDecay 'truth' model with erf turn on ws.factory( 'RooGaussModel::MzgResoShape_exp_erf_%s_cat%i(Mzg,'\ 'bias_exp_erf_%s_cat%i[120,90,150],sigma_exp_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooDecay::MzgTruthModelBase_exp_erf_%s_cat%i(Mzg,'\ 'tau_erf_%s_cat%i[25,0,50],MzgResoShape_exp_erf_%s_cat%i,' 'RooDecay::SingleSided)'%(channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_erf_%s_cat%i('\ 'MzgTruthModelBase_exp_erf_%s_cat%i,'\ 'norm_truth_exp_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) if turnon == 'sigm' and truth == 'pow': #make power-law truth model with erf turn on ws.factory('EXPR::MzgTruthModelShape_pow_erf_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_erf_%s_cat%i[105,100,130],'\ 'pow_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooGaussModel::MzgResoShape_pow_erf_%s_cat%i(Mzg,'\ 'bias_pow_erf_%s_cat%i[0],sigma_pow_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory('FCONV::MzgTruthModelBase_pow_erf_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_erf_%s_cat%i,'\ 'MzgResoShape_pow_erf_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_erf_%s_cat%i('\ 'MzgTruthModelBase_pow_erf_%s_cat%i,'\ 'norm_truth_pow_erf_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) #for erf fitting turn on we want sigmoid truth if turnon == 'erf' and truth == 'exp': #build exponential convoluted with sigmoid turn-on ws.factory('RooStepExponential::MzgTruthModelShape_exp_sigm_%s_cat%i'\ '(Mzg,tau_sigm_%s_cat%i[-0.05,-10,0],'\ 'step_exp_sigm_%s_cat%i[110,100,130])'%(channel,cat, channel,cat, channel,cat)) ws.factory('RooLogistics::MzgResoShape_exp_sigm_%s_cat%i(%s)' % (channel, cat, ','.join([ 'Mzg', 'bias_exp_sigm_%s_cat%i[0]' % (channel, cat), 'sigma_exp_sigm_%s_cat%i[5,0.01,20]' % (channel, cat) ]))) ws.factory('FCONV::MzgTruthModelBase_exp_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_exp_sigm_%s_cat%i,'\ 'MzgResoShape_exp_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_sigm_%s_cat%i('\ 'MzgTruthModelBase_exp_sigm_%s_cat%i,'\ 'norm_truth_exp_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) if turnon == 'erf' and truth == 'pow': #build power-law convoluted with sigmoid turn-on ws.factory('EXPR::MzgTruthModelShape_pow_sigm_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_sigm_%s_cat%i[105,100,130],'\ 'pow_sigm_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory('RooLogistics::MzgResoShape_pow_sigm_%s_cat%i(%s)' % (channel, cat, ','.join([ 'Mzg', 'bias_pow_sigm_%s_cat%i[0]' % (channel, cat), 'sigma_pow_sigm_%s_cat%i[5,0.01,20]' % (channel, cat) ]))) ws.factory('FCONV::MzgTruthModelBase_pow_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_sigm_%s_cat%i,'\ 'MzgResoShape_pow_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_sigm_%s_cat%i('\ 'MzgTruthModelBase_pow_sigm_%s_cat%i,'\ 'norm_truth_pow_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True))
mynewfile.cd('HLT') gDirectory.pwd() gDirectory.mkdir('EgammaMon') mynewfile.cd('HLT/EgammaMon') gDirectory.pwd() gDirectory.mkdir('L1') mynewfile.cd('HLT/EgammaMon/L1') h.Write() h1.Write() h2.Write() h3.Write() h4.Write() h5.Write() gDirectory.cd('../') gDirectory.pwd() gDirectory.mkdir('L2Calo') mynewfile.cd('HLT/EgammaMon/L2Calo') g.Write() g1.Write() g2.Write() g3.Write() g4.Write() g5.Write() gDirectory.cd('../../') gDirectory.pwd() gDirectory.mkdir('MuonMon') gDirectory.cd('MuonMon')
nameMC = str(fname) #directories = ['nVert','l1Pt','l2Pt','dRtt'] #directories = ['dRtt','nVert','l1jetWidth','l2jetWidth','l1jetPt','l2jetPt'] #directories = ['dRtt','pThiggs','nVert'] #directories = ['l1jetPt'] directories = ['nVert','l1jetPt'] for dir in directories : print '################################' print '## ',dir,' ' print '################################' qcdFile = TFile.Open(name,'read') gDirectory.cd(dir) histoList = gDirectory.GetListOfKeys() for k1 in histoList : h1 = k1.ReadObj() if h1.GetName() == 'QCDtightSS' : tight = h1.Clone() #tight.Scale(1/h1.Integral()) if h1.GetName() == 'QCDlooseSS' : loose = h1.Clone() #loose.Scale(1/h1.Integral()) MCFile = TFile.Open(nameMC,'read') gDirectory.cd(dir)