|
| 1 | +#!/usr/bin/env python |
| 2 | +# to EXE: python scripts/BaselineSelector.py -s data_moriond -t -o def_cmva |
| 3 | + |
| 4 | +# good old python modules |
| 5 | +import json |
| 6 | +import os |
| 7 | +import importlib |
| 8 | +from glob import glob |
| 9 | + |
| 10 | +# ROOT imports |
| 11 | +import ROOT |
| 12 | +from ROOT import TChain, TH1F, TFile, vector, gROOT |
| 13 | +# custom ROOT classes |
| 14 | +from ROOT import alp, ComposableSelector, CounterOperator, TriggerOperator, JetFilterOperator, BTagFilterOperator, JetPairingOperator, DiJetPlotterOperator |
| 15 | +from ROOT import BaseOperator, EventWriterOperator, IsoMuFilterOperator, MetFilterOperator, JetPlotterOperator, FolderOperator, MiscellPlotterOperator |
| 16 | +from ROOT import ThrustFinderOperator, HemisphereProducerOperator, HemisphereWriterOperator, JEShifterOperator, JERShifterOperator, ReWeightingOperator |
| 17 | + |
| 18 | +# imports from ../python |
| 19 | +from Analysis.alp_analysis.alpSamples import samples |
| 20 | +from Analysis.alp_analysis.samplelists import samlists |
| 21 | +from Analysis.alp_analysis.triggerlists import triggerlists |
| 22 | +from Analysis.alp_analysis.workingpoints import wps |
| 23 | + |
| 24 | +TH1F.AddDirectory(0) |
| 25 | + |
| 26 | +# parsing parameters |
| 27 | +import argparse |
| 28 | +parser = argparse.ArgumentParser() |
| 29 | +parser.add_argument("-e", "--numEvts", help="number of events", type=int, default='-1') |
| 30 | +parser.add_argument("-s", "--samList", help="sample list", default="") |
| 31 | +parser.add_argument("-i", "--iDir", help="input directory", default="def_cmva") |
| 32 | +parser.add_argument("-o", "--oDir", help="output directory", default="re_weighted") |
| 33 | +parser.add_argument("-f", "--no_savePlots", help="to save histos already in output file", action='store_false', dest='savePlots', ) #to get faster execution |
| 34 | +# NOTICE: do not use trigger, jesUp, jesDown with '-m' |
| 35 | +parser.set_defaults(doTrigger=False, doMixed=False, savePlots=True) |
| 36 | +args = parser.parse_args() |
| 37 | + |
| 38 | +# exe parameters |
| 39 | +numEvents = args.numEvts |
| 40 | +if not args.samList: samList = ['SM'] # list of samples to be processed - append multiple lists |
| 41 | +else: samList = [args.samList] |
| 42 | +trgList = 'def_2016' |
| 43 | +intLumi_fb = 36.26 |
| 44 | + |
| 45 | +iDir = "/lustre/cmswork/hh/alp_moriond_base/" + args.iDir |
| 46 | +oDir = '/lustre/cmswork/hh/alp_moriond_base/' + args.oDir |
| 47 | + |
| 48 | +data_path = "{}/src/Analysis/alp_analysis/data/".format(os.environ["CMSSW_BASE"]) |
| 49 | + |
| 50 | +#weights to be applied |
| 51 | +weights = {'PUWeight', 'PdfWeight', 'BTagWeight'} |
| 52 | +# --------------- |
| 53 | + |
| 54 | +if not os.path.exists(oDir): os.mkdir(oDir) |
| 55 | +print oDir |
| 56 | + |
| 57 | +# to convert weights |
| 58 | +weights_v = vector("string")() |
| 59 | +for w in weights: weights_v.push_back(w) |
| 60 | + |
| 61 | +# to parse variables to the anlyzer |
| 62 | +config = { "eventInfo_branch_name" : "EventInfo", |
| 63 | + "jets_branch_name": "Jets", |
| 64 | + "dijets_branch_name": "DiJets", |
| 65 | + "dihiggs_branch_name": "DiHiggs", |
| 66 | + "genbfromhs_branch_name" : "GenBFromHs", |
| 67 | + "genhs_branch_name" : "GenHs", |
| 68 | + "tl_genhs_branch_name" : "TL_GenHs", |
| 69 | + } |
| 70 | +#"muons_branch_name" : "", |
| 71 | +#"electrons_branch_name" : "", |
| 72 | +#"met_branch_name" : "", |
| 73 | +config.update( |
| 74 | + { "n_gen_events":0, |
| 75 | + "xsec_br" : 0, |
| 76 | + "matcheff": 0, |
| 77 | + "kfactor" : 0, |
| 78 | + "isData" : False, |
| 79 | + "lumiFb" : intLumi_fb, |
| 80 | + "isMixed" : False, |
| 81 | + } ) |
| 82 | + |
| 83 | +snames = [] |
| 84 | +for s in samList: |
| 85 | + snames.extend(samlists[s]) |
| 86 | + |
| 87 | +# process samples |
| 88 | +ns = 0 |
| 89 | +for sname in snames: |
| 90 | + |
| 91 | + #loop on all samples needed.... or just the one to apply rew? |
| 92 | + #get file names in all sub-folders: |
| 93 | + reg_exp = iDir+sname+".root" |
| 94 | + print "reg_exp: {}".format(reg_exp) |
| 95 | + files = glob(reg_exp) |
| 96 | + print "\n ### processing {}".format(sname) |
| 97 | + |
| 98 | + #preliminary checks |
| 99 | + if not files: |
| 100 | + print "WARNING: files do not exist" |
| 101 | + continue |
| 102 | + else: |
| 103 | + if "Run" in files[0]: config["isData"] = True |
| 104 | + |
| 105 | + #read weights from alpSamples |
| 106 | + config["xsec_br"] = samples[sname]["xsec_br"] |
| 107 | + config["matcheff"] = samples[sname]["matcheff"] |
| 108 | + config["kfactor"] = samples[sname]["kfactor"] |
| 109 | + |
| 110 | + json_str = json.dumps(config) |
| 111 | + |
| 112 | + #define selectors list |
| 113 | + selector = ComposableSelector(alp.Event)(0, json_str) |
| 114 | + selector.addOperator(BaseOperator(alp.Event)()) |
| 115 | + |
| 116 | + selector.addOperator(FolderOperator(alp.Event)("base")) |
| 117 | + selector.addOperator(CounterOperator(alp.Event)(weights_v)) |
| 118 | + |
| 119 | + selector.addOperator(ReWeightingOperator(alp.Event)()) |
| 120 | + |
| 121 | + selector.addOperator(FolderOperator(alp.Event)("reweight")) |
| 122 | + selector.addOperator(CounterOperator(alp.Event)(weights_v)) |
| 123 | + if args.savePlots: selector.addOperator(JetPlotterOperator(alp.Event)(btagAlgo, weights_v)) |
| 124 | + if args.savePlots: selector.addOperator(DiJetPlotterOperator(alp.Event)(weights_v)) |
| 125 | + selector.addOperator(EventWriterOperator(alp.Event)(json_str, weights_v)) |
| 126 | + |
| 127 | + #create tChain and process each files |
| 128 | + treename = "pair/tree" |
| 129 | + tchain = TChain(treename) |
| 130 | + for File in files: |
| 131 | + tchain.Add(File) |
| 132 | + nev = numEvents if (numEvents > 0 and numEvents < tchain.GetEntries()) else tchain.GetEntries() |
| 133 | + procOpt = "ofile=./"+sname+".root" if not oDir else "ofile="+oDir+"/"+sname+".root" |
| 134 | + print "max numEv {}".format(nev) |
| 135 | + tchain.Process(selector, procOpt, nev) |
| 136 | + ns+=1 |
| 137 | + |
| 138 | + #some cleaning |
| 139 | + |
| 140 | + |
| 141 | +print "### processed {} samples ###".format(ns) |
0 commit comments