def write_hist(self): makeDirIfNeeded(self.output_path) output_file = ROOT.TFile(self.output_path, 'recreate') output_file.cd() self.hist.Write() self.sideBand.Write() output_file.Close()
def savePlots(Canv, destination): destination_components = destination.split('/') cleaned_components = [ x for x in destination_components if not isTimeStampFormat(x) ] try: index_for_php = cleaned_components.index('Results') except: index_for_php = None if index_for_php: php_destination = '/user/lwezenbe/public_html/' php_destination += '/'.join(cleaned_components[index_for_php + 1:]) makeDirIfNeeded(php_destination.rsplit('/', 1)[0]) os.system('cp -rf /user/lwezenbe/private/PhD/index.php ' + php_destination.rsplit('/', 1)[0] + '/index.php') print destination Canv.SaveAs(destination + ".pdf") Canv.SaveAs(destination + ".png") Canv.SaveAs(destination + ".root") #Clean out the php directory you want to write to if it is already filled, otherwise things go wrong with updating the file on the website #os.system("rm "+php_destination.rsplit('/')[0]+"/*") if index_for_php: Canv.SaveAs(php_destination + ".pdf") Canv.SaveAs(php_destination + ".png") Canv.SaveAs(php_destination + ".root")
def writeFR(self): makeDirIfNeeded(self.OUTPUT_DIR) folder_name = 'tmp_'+self.name.split('_')[0].split('-')[0] makeDirIfNeeded(self.OUTPUT_DIR +'/'+folder_name) output_file = ROOT.TFile(self.OUTPUT_DIR + '/'+folder_name+'/' + self.name + '.root', 'recreate') output_file.cd() self.fakeRate_num.Write() self.fakeRate_denom.Write() output_file.Close()
def write(self, output, additionalInfo): makeDirIfNeeded(output) output_file = ROOT.TFile( output + '/' + self.name + '_' + additionalInfo + '.root', 'recreate') output_file.cd() for i in xrange(len(self.workingPoints)): self.efficiency_num[i].Write() self.efficiency_denom.Write() output_file.Close()
def write(self, output_dir, additionalInfo=None): makeDirIfNeeded(output_dir) output_name = self.name if additionalInfo: output_name += '_' + additionalInfo output_file = TFile(output_dir + '/' + output_name + '.root', 'recreate') output_file.cd() self.eff_numerator.Write() self.eff_denominator.Write() self.misid_numerator.Write() self.misid_denominator.Write() output_file.Close()
def merge(effOrmisid, sampleName, method, category): if category == 'LepDiscr': basefolder = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency/Histos/LepDiscr' #Define the algorithms and their working points tau_id_algos = [('MuonDiscrMVA', ['Loose', 'Tight']), ('MuonDiscrdeeptau', ['VVVLoose', 'VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight', 'VTight', 'VVTight']), ('ElectronDiscrMVA', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('ElectronDiscrdeeptau', ['VVVLoose', 'VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight', 'VTight', 'VVTight'])] #Change getTauLepDiscr() accordingly else: #Define the algorithms and their working points tau_id_algos = [('oldMVA2015', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('newMVA2015', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('oldMVA2017v2', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('newMVA2017v2', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('cut_based', ['VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight']), ('deeptau', ['VVVLoose', 'VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight', 'VTight', 'VVTight'])] #If you add more ID's, don't forget to change it in the getTauIDs() function in objectSelection as well basefolder = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency/Histos/'+category #basefolder = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency_/Histos/'+category if effOrmisid == 'efficiency': folder = 'Efficiency' elif effOrmisid == 'fakerate': folder = 'FakeRate' #Save efficiencies makeDirIfNeeded(basefolder+'/Merged') makeDirIfNeeded(basefolder+'/Merged/'+folder) makeDirIfNeeded(basefolder+'/Merged/'+folder+'/'+method) makeDirIfNeeded(basefolder+'/Merged/'+folder+'/'+method+'/'+sampleName) for tau_id in tau_id_algos: os.system('hadd -f ' + basefolder + "/Merged/"+folder+"/"+ method+'/'+sampleName+'/roc_'+effOrmisid+'_'+tau_id[0]+'.root '+ basefolder + '/'+sampleName + '/'+method+'/roc_'+effOrmisid+'_'+ tau_id[0]+'_*.root') eta_files = glob.glob(basefolder + '/' + sampleName + '/' + method + '/eta_efficiency_'+tau_id[0] +'_*.root') os.system('hadd -f ' + basefolder+'/Merged/'+folder+'/'+method+'/'+ sampleName+'/eta_'+effOrmisid+'_'+tau_id[0]+'.root '+ basefolder + '/' + sampleName + '/' + method + '/eta_'+effOrmisid+'_'+tau_id[0] +'_*.root') pt_files = glob.glob(basefolder + '/' + sampleName + '/' + method + '/eta_efficiency_'+tau_id[0] +'_*.root') os.system('hadd -f ' + basefolder+'/Merged/'+folder+'/'+method+'/'+ sampleName+'/pt_'+effOrmisid+'_'+tau_id[0]+'.root '+ basefolder + '/' + sampleName + '/' + method + '/pt_'+effOrmisid+'_'+tau_id[0] +'_*.root')
samples = [args.effName, args.frName] methods = [args.effMethod, args.frMethod] basefolderInput = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency/Histos/' + args.category + '/Merged' basefolderOutput = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency/Plots/' + args.category #basefolderInput = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency_/Histos/'+args.category+'/Merged' #basefolderOutput = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency_/Plots/'+args.category efficiencyOrfakerate = ['efficiency', 'fakerate'] efficiencyOrfakerateFile = ['Efficiency', 'FakeRate'] var = ['pt', 'eta'] xNames = [['p_{T}^{#tau} [GeV]', '|#eta|_{#tau}'], ['p_{T}^{jet} [GeV]', '|#eta|^{jet}']] #make output directory makeDirIfNeeded(basefolderOutput) makeDirIfNeeded(basefolderOutput + '/' + samples[0] + '-' + methods[0] + '_' + samples[1] + '-' + methods[1]) basefolderOutput = timeStampFolder( basefolderOutput + '/' + samples[0] + '-' + methods[0] + '_' + samples[1] + '-' + methods[1], args.usefulInfo) makeDirIfNeeded(basefolderOutput + '/ROC') #ROC extraText = [] extraText.append(extraTextFormat("Efficiency: " + samples[0] + " MC")) extraText.append(extraTextFormat("FR : " + samples[1] + " MC")) extraText.append(extraTextFormat('p_{T}^{#tau} > 20 GeV, |#eta_{#tau}| < 2.3')) ROC_graphs = [] for n, name in enumerate(tau_id_algos):
out_path = basefolderOutput + '/' + hn + '_' + tn + '_' + str( ele_cut_index) + '_' + str(mu_cut_index) calcAndDrawSignificance( s, background_hists[i][mu_cut_index][ele_cut_index], 'Iso working point', background_names, [hn], out_path, 2016, ylog=True, customLabels=IsoWorkingPoints, extraText=extraText) basefolderOutput = basefolderOutput + '/Combined' makeDirIfNeeded(basefolderOutput) for i, tn in enumerate(ntau): for mu_cut_index, muname in enumerate(MuWorkingPoints): for s, hn in zip(signal_hists_comb[i][mu_cut_index], signal_names): extraText = [] extraText.append(extraTextFormat(tn + ' channel', 0.5, 0.9, None)) extraText.append(extraTextFormat('Muon discr: ' + muname)) if specific_mass: extraText.append( extraTextFormat('m_{#chi_{1}} = ' + mchi1 + ' GeV')) extraText.append( extraTextFormat('m_{#chi_{2}} = ' + mchi2 + ' GeV')) line_division = (6, EleWorkingPoints)
'--method', action='store', default='AN' ) #Currently no use except for consistent file structure until next iteration argParser.add_argument('--inputFile', action='store', default='inputFilesv3') argParser.add_argument('--isTest', action='store', default=False) args = argParser.parse_args() sampleList = Sample.createSampleList( '/user/lwezenbe/private/PhD/Code/TauStudy/Efficiency/Data/' + args.inputFile + '.conf') sample = Sample.getSampleFromList(sampleList, args.sampleName) print sample.output Chain = sample.initTree(needhCount=False) basefolder = '/storage_mnt/storage/user/lwezenbe/private/PhD/Results/TauStudy/Efficiency/Histos/LepDiscr/' makeDirIfNeeded(basefolder) makeDirIfNeeded(basefolder + sample.output) makeDirIfNeeded(basefolder + sample.output + '/' + args.method) basefolder = basefolder + sample.output + '/' + args.method #Define the algorithms and their working points tau_id_algos = [ ('MuonDiscrMVA', ['Loose', 'Tight']), #('MuonDiscrdeeptau', ['VVVLoose', 'VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight', 'VTight', 'VVTight']), ('MuonDiscrdeeptau', ['VLoose', 'Loose', 'Medium', 'Tight']), ('ElectronDiscrMVA', ['VLoose', 'Loose', 'Medium', 'Tight', 'VTight']), ('ElectronDiscrdeeptau', [ 'VVVLoose', 'VVLoose', 'VLoose', 'Loose', 'Medium', 'Tight', 'VTight', 'VVTight' ]) ] #Change getTauLepDiscr() accordingly
baseFolderInput = '/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '_final' baseFolderOutput = '/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '_final' def isData(f): list_of_datafiles = [ 'SingleMuon', 'SingleElectron', 'DoubleMuon', 'DoubleEG', 'MuonEG', 'EGamma' ] for dataf in list_of_datafiles: if dataf in f: return True return False makeDirIfNeeded(baseFolderOutput) #data_included = True list_of_folders = glob.glob(baseFolderInput + '/tmp_*') for f in list_of_folders: makeDirIfNeeded(baseFolderOutput + '/Log') if not isData(f): log = baseFolderOutput + '/Log/' + f.rsplit('/', 1)[1] + '.log' command = 'python /storage_mnt/storage/user/lwezenbe/private/PhD/Code/TauStudy/Skimmer/mergeTuples.py --f=' + f + ' --Output=' + baseFolderOutput sub.launchCream02(command, log, False, 'Merge_' + f.rsplit('/', 1)[1]) # else: # data_included = True # #if data_included: # log = baseFolderOutput + '/Log/data.log'
import subSample import jobSubmitter as sub from helpers_old import makeDirIfNeeded import argparse argParser = argparse.ArgumentParser(description="Argument parser") argParser.add_argument('--year', action='store', default='2016') argParser.add_argument('--skim', action='store', default='FR') argParser.add_argument('--analysis', action='store', default='ewkino') args = argParser.parse_args() files_to_skim = subSample.createSampleList('Data/' + args.analysis + '_' + args.year + '.conf') makeDirIfNeeded('/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '/Log') for f in files_to_skim: number_of_subdir = int( subprocess.check_output("/bin/ls -lA " + f + "/* | egrep -c '^-|^d'", shell=True, stderr=subprocess.STDOUT)) for subdir in xrange(number_of_subdir): subsample = subSample.subSample(f, subdir) nJobs = len(subsample.arrangeFilesInSubjobs()) for job in xrange(nJobs): log = '/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '/Log/' + subsample.group + '_' + subsample.name + '_' + str( subdir) + '_' + str(job) + '.log' #temporary lazy log command = 'python /storage_mnt/storage/user/lwezenbe/private/PhD/Code/TauStudy/Skimmer/main.py --path=' + f + ' --subDir=' + str( subdir) + ' --subJob=' + str( job) + ' --skim=' + args.skim + ' --year=' + args.year
argParser = argparse.ArgumentParser(description = "Argument parser") argParser.add_argument('--path', action='store', default='/pnfs/iihe/cms/store/user/wverbeke/heavyNeutrino/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_MiniAOD2016v3_ext1-v2_singlelepton_MC_2016_v2') argParser.add_argument('--subDir', action='store', default=0) argParser.add_argument('--subJob', action='store', default=0) argParser.add_argument('--year', action='store', default='2016') argParser.add_argument('--skim', action='store', default='ewkino') argParser.add_argument('--isTest', action='store_true', default=False) args = argParser.parse_args() subsample = subSample.subSample(args.path, int(args.subDir)) Chain = subsample.initChain(int(args.subJob)) #Create output file output_dir = '/user/lwezenbe/public/ntuples/'+args.year+ '/'+args.skim+'_final/tmp_'+subsample.group makeDirIfNeeded(output_dir) output_file = TFile(output_dir+'/'+subsample.group+'_'+ subsample.name+'_' +str(args.subDir)+'_'+str(args.subJob)+'.root', 'recreate') output_file.mkdir('blackJackAndHookers') output_file.cd('blackJackAndHookers') #Get hcounters import eventSelection isData = eventSelection.isData(subsample.group) if not isData: hCounter = subsample.getHist(int(args.subJob), 'hCounter') if 'TChi' in subsample.group: hCounterSUSY = subsample.getHist(int(args.subJob), 'hCounterSUSY') #initialize new tree and delete unused branches #branches_to_delete = ['HLT', 'Trigger', 'Flag', 'nLhe', 'IP', 'lElectron', 'leptonMva', 'relIso', 'miniIso', 'closestJet', 'lMuon', '_ph', 'HN'] branches_to_delete = ['HLT_IsoMu22','HLT_MET','HLT_PFMET', 'HLT_PFHT', 'HLT_CaloJet', 'nLhe', 'lMuon', '_ph', 'HN', 'deepTau', 'Flag'] for branch in branches_to_delete:
if isSignal: sample.xsec = xsec(Chain._mChi2) sample.hCount = hCounterSUSY.GetBinContent( hCounterSUSY.FindBin(Chain._mChi2, Chain._mChi1)) #Fill histogram weightfactor = Chain._weight * ((sample.xsec * lumi) / sample.hCount) print weightfactor if evSel.nTau == 1: Single_Tau_Output.Fill(WP + .5, weightfactor) elif evSel.nTau == 2: Di_Tau_Output.Fill(WP + .5, weightfactor) for i in range(len(evSel.IsoWorkingPoints)): print Single_Tau_Output.GetBinContent(i + 1), Di_Tau_Output.GetBinContent(i + 1) if not args.isTest: #Save output makeDirIfNeeded(basefolder + '/' + sample.output) Single_Tau_Output.SaveAs(basefolder + '/' + sample.output + '/' + sample.name + '_singletau_' + args.ele_cut_index + '_' + args.mu_cut_index + '_subjob_' + str(args.subJob) + '.root') Di_Tau_Output.SaveAs(basefolder + '/' + sample.output + '/' + sample.name + '_ditau_' + args.ele_cut_index + '_' + args.mu_cut_index + '_subjob_' + str(args.subJob) + '.root')
from helpers_old import makeDirIfNeeded list_of_datafiles = [ 'SingleMuon', 'SingleElectron', 'DoubleMuon', 'DoubleEG', 'MuonEG' ] import argparse argParser = argparse.ArgumentParser(description="Argument parser") argParser.add_argument('--year', action='store', default='2016') argParser.add_argument('--skim', action='store', default='FR') args = argParser.parse_args() output_folder = '/storage_mnt/storage/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '/tmp_Data' makeDirIfNeeded(output_folder) event_information_set = set() for f_name in list_of_datafiles: file_list = glob.glob( '/storage_mnt/storage/user/lwezenbe/public/ntuples/' + args.year + '/' + args.skim + '/tmp_' + f_name + '/*.root') for i, sub_f_name in enumerate(file_list): print sub_f_name f = ROOT.TFile(sub_f_name) c = f.Get('blackJackAndHookers/blackJackAndHookersTree') try: c.GetEntry() except: continue
import fakeRateCalculator from helpers_old import makeDirIfNeeded, makePathTimeStamped #Parse arguments import argparse argParser = argparse.ArgumentParser(description = "Argument parser") argParser.add_argument('--inData', action='store_true', default=False) argParser.add_argument('--year', action='store', default='2016') args = argParser.parse_args() if args.inData: indata_str = 'DATA' else: indata_str = 'MC' output_dir = '/user/lwezenbe/private/PhD/Results/TauStudy/FakeRate/Plots/'+indata_str+args.year makeDirIfNeeded('/user/lwezenbe/private/PhD/Results/TauStudy/FakeRate/Plots') makeDirIfNeeded('/user/lwezenbe/private/PhD/Results/TauStudy/FakeRate/Plots/'+indata_str+args.year) output_dir = makePathTimeStamped(output_dir) FR = fakeRateCalculator.fakeRateCalculator(args.year, indata_str) tightToLoose = FR.getTightToLoose() output_str = output_dir+'/tightToLoose' plt.draw2DHist(tightToLoose, 'p_{T} [GeV]', '|#eta|', output_str, int(args.year))
import plottingTools as plt from helpers_old import getObjFromFile, makeDirIfNeeded, makePathTimeStamped, CleanNegativeBins import argparse, glob argParser = argparse.ArgumentParser(description = "Argument parser") argParser.add_argument('sample', action='store') argParser.add_argument('input_dir', action='store') argParser.add_argument('output_dir', action='store') args = argParser.parse_args() xtitle_map = {'ptTau': 'P_{T}(#tau) [GeV]', 'etaTau': '#eta(#tau)', 'Mll': 'M(ll) [GeV]', 'met':'E_{T}^{miss} [GeV]'} ytitle_map = {'ptTau': 'Taus', 'etaTau': 'Taus', 'Mll': 'Events', 'met':'Events'} log_map = {'ptTau': False, 'etaTau': False, 'Mll': False, 'met':False} categ_map = {'Categ_C': 'C: OSSF + #tau', 'Categ_D': 'D: OSOF + #tau', 'Categ_E': 'E: SS + #tau' , 'Categ_F':'F: e/#mu + #tau#tau', 'Check': 'FR measurement region'} makeDirIfNeeded(args.output_dir+'/'+args.sample) file_paths = glob.glob(args.input_dir + '/' + args.sample + '/*.root') year = 2016 if '2017' in args.input_dir: year = 2017 elif '2018' in args.input_dir: year = 2018 for f in file_paths: print f h_name = f.rsplit('/', 1)[1] h_name = h_name.split('.')[0] print h_name split_names = h_name.rsplit('_', 1) categ_name = split_names[0]
from helpers_old import makeDirIfNeeded #Parse arguments import argparse argParser = argparse.ArgumentParser(description="Argument parser") argParser.add_argument('--year', action='store', default='2016') argParser.add_argument('--inData', action='store_true') argParser.add_argument('--runLocal', action='store_true') args = argParser.parse_args() sampleList = createSampleList( '/user/lwezenbe/private/PhD/Code/TauStudy/FakeRate/Data/inputFiles_' + args.year + '.conf') makeDirIfNeeded("/user/lwezenbe/private/PhD/Code/TauStudy/FakeRate/log") for sample in sampleList: if not args.inData and not 'DYJets' in sample.output: continue for subJob in xrange(sample.splitJobs): log = "/user/lwezenbe/private/PhD/Code/TauStudy/FakeRate/log/" + sample.name + "_subjob_" + str( subJob) + ".log" command = 'python /storage_mnt/storage/user/lwezenbe/private/PhD/Code/TauStudy/FakeRate/main.py --sampleName=' + sample.name + ' --subJob=' + str( subJob) + ' --year=' + args.year if args.inData: command += ' --inData' if args.inData: inDataStr = "True" else: inDataStr = "False" if args.runLocal: