diff --git a/crab/cc_looper.py b/crab/cc_looper.py index 4642ba52ce5d802d7c2d4c88a71c86051abe9969..a2437acdb37114b58198191eb7b80e3ccae942d3 100644 --- a/crab/cc_looper.py +++ b/crab/cc_looper.py @@ -1,5 +1,7 @@ -import ROOT, json, sys +#!/usr/bin/env python +import argparse, ROOT, json, sys import numpy as np +ROOT.PyConfig.IgnoreCommandLineOptions = True ROOT.gSystem.Load("libTTHCommonClassifier") CvectorvectorLorentz = getattr(ROOT, "std::vector<vector<TLorentzVector>>") @@ -106,7 +108,7 @@ class Jets: } -def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="2018"): +def main(infile_name, firstEvent, lastEvent, outfile_name, conf, era="2018"): """ Processes an input file with the CommonClassifier, saving the output in a file. infile_name (string): path to input file, can be root://, file://, ... @@ -116,7 +118,6 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 conf (dict): configuration dictionary """ - print "is data? ",is_data firstEvent = long(firstEvent) lastEvent = long(lastEvent) @@ -128,10 +129,10 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 #one file if isinstance(infile_name, basestring): infile = ROOT.TFile.Open(infile_name) - tree = infile.Get("MVATree") + tree = infile.Get("tree") #many files else: - tree = ROOT.TChain("MVATree") + tree = ROOT.TChain("tree") for fi in infile_name: tree.AddFile(fi) @@ -156,9 +157,6 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 for ud in ["Up","Down"]: bufs["mem_{}{}_p".format(unc,ud)] = np.zeros(1, dtype=np.float64) - - - outtree.Branch("event", bufs["event"], "event/L") outtree.Branch("run", bufs["run"], "run/L") outtree.Branch("lumi", bufs["lumi"], "lumi/L") @@ -200,9 +198,8 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 bufs["hypo"][0] = hypo ############################################## njets = tree.njets - - jets_unc = {} + jets_unc = {} #process jets jets_p4 = CvectorvectorLorentz() @@ -215,14 +212,10 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 for unc in jet_corrections: for ud in ["Up","Down"]: - if not is_data: - jets_unc["{}{}".format(unc,ud)] = list(getattr(tree,"jet_{a}{b}".format(a = unc, b = ud))) - else: - jets_unc["{}{}".format(unc,ud)] = 1. + jets_unc["{}{}".format(unc,ud)] = list(getattr(tree,"jet_{a}{b}".format(a = unc, b = ud))) nBCSVM = 0 - jets_p4_nominal = CvectorLorentz() for iJet in range(njets): @@ -379,11 +372,10 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 changes_jet_category = vec_from_list(Cvectorbool, changes) - if nBCSVM == 2: hypo = -2 bufs["hypo"][0] = hypo - + #process leptons nleps = tree.nleps leps_p4 = CvectorLorentz() @@ -396,16 +388,11 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 v = ROOT.TLorentzVector() v.SetPtEtaPhiM(leps_pt[ilep], leps_eta[ilep], leps_phi[ilep], leps_mass[ilep]) leps_p4.push_back(v) - + #process MET met = ROOT.TLorentzVector() - met.SetPtEtaPhiM( - tree.met_pt, - 0, - tree.met_phi, - 0 - ) - + met.SetPtEtaPhiM(tree.met_pt, 0, tree.met_phi, 0) + bufs["mem_p"][0] = 0 bufs["mem_p_sig"][0] = 0 bufs["mem_p_bkg"][0] = 0 @@ -414,8 +401,8 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 for unc in jet_corrections: for ud in ["Up","Down"]: bufs["mem_{}{}_p".format(unc,ud)][0] = 0 + #calculate the MEM - if hypo >= -1 and njets > 0: ret = cls_mem.GetOutput( leps_p4, @@ -443,10 +430,8 @@ def main(infile_name, firstEvent, lastEvent, outfile_name, conf,is_data, era="20 bufs["mem_{}{}_p".format(unc,ud)][0] = ret[0].p_variated[index] index += 1 - - outtree.Fill() - + outfile.Write() outfile.Close() @@ -472,14 +457,20 @@ if __name__ == "__main__": }, } - import argparse parser = argparse.ArgumentParser(description='Calculates the CommonClassifier on a common input ntuple') - parser.add_argument( - '--infile', - action="store", - nargs='+', - help="Input file name (PFN)", - required=True + parser.add_argument('-i', + '--infile', + action="store", + nargs='+', + help="Input file name (PFN)", + required=True, + ) + parser.add_argument('-o', + '--outfile', + action="store", + help="output file name, must be writeable", + default="out.root", + required=True, ) parser.add_argument( '--firstEvent', @@ -503,23 +494,22 @@ if __name__ == "__main__": required=False, default=None ) - parser.add_argument( - '--outfile', - action="store", - help="output file name, must be writeable", - default="out.root" - ) parser.add_argument( '--conf', type=str, choices=sorted(confs.keys()), default="DeepJet" ) + + parser.add_argument('-e', '--era', dest='era', choices=['2016', '2017', '2018'], required=True, action='store', default=None, + help='name of Run-2 era') + args = parser.parse_args() conf = confs[args.conf] print conf - #use maxEvents if it was specified + # use maxEvents if it was specified if not args.maxEvents is None: - args.lastEvent = args.firstEvent + args.maxEvents - 1 - main(args.infile, args.firstEvent, args.lastEvent, args.outfile, conf, False, era="2018") + args.lastEvent = args.firstEvent + args.maxEvents - 1 + + main(args.infile, args.firstEvent, args.lastEvent, args.outfile, conf, era=args.era)