def __init__(self,tree,name,weight_file,var_file,cutval): """ A class to handle the decision of the BDT""" TMVA.Tools.Instance() self._reader = TMVA.Reader() self._tree = tree self._variables = {} self._cutvalue = -1 self._bdtscore = -9999 self._name = name self._weight_file = weight_file self._var_file = var_file self.SetReader(self._name,self._weight_file,self._var_file) self.SetCutValue(cutval)
def __init__(self, name, weight_file, variables, cutval, training='training'): """ A class to handle the decision of the BDT""" TMVA.Tools.Instance() self._reader = TMVA.Reader() self._vars = variables self._vals = [array('f', [0.]) for i in range(0, len(variables))] self._cutval = cutval self._score = -9999 self._name = name self._training = training
def __init__(self, **kwargs): """ This initialization function sets up the TMVA macro path, and sets up variables for the TMVA reader. These variables are defined as single entry Python arrays. One has to use these arrays as TMVA/ROOT works by reference. This means that adding variables to the TMVA reader amounts to adding pointers to the TMVA reader. We have to then iterate (not very numpy) through our variable arrays, setting the reader variable arrays (and evaluating as we go). kwargs: - var_names: the names of the variables used in the BDT application (default 10 variables) - tmva_dir: the installation location of TMVA macros (/home/dean/software/tmva/TMVA-v4.2.0/test) """ var_names = kwargs.get('var_names', [ 'peaks', 'mean_peaks', 'integral', 'integral_over_peaks', 'max', 'mean', 'max_over_mean', 'std_dev_peaks', 'entropy', 'ps_integral' ]) tmvadir = kwargs.get("tmva_dir", "/home/dean/software/tmva/TMVA-v4.2.0/test") #setting up TMVA macro path try: macro = os.path.join(tmvadir, "TMVAlogon.C") loadmacro = os.path.join(tmvadir, "TMVAGui.C") gROOT.SetMacroPath(tmvadir) gROOT.Macro(macro) gROOT.LoadMacro(loadmacro) except: print("Couldn't successfully establish TMVA macro path") sys.exit() self.reader = TMVA.Reader("!Color") variables = [] for i in xrange(len(var_names)): variables.append(array('f', [0])) for name, var in zip(var_names, variables): self.reader.AddVariable(name, var) self.variables = variables
class OutputTree(TreeModel): classID = FloatCol(default=-1111.) var1 = FloatCol(default=-1111.) var2 = FloatCol(default=-1111.) var3 = FloatCol(default=-1111.) weight = FloatCol(default=-1111.) cls0 = FloatCol(default=-1111.) cls1 = FloatCol(default=-1111.) cls2 = FloatCol(default=-1111.) outfile = root_open('tmva_example_multiple_backgrounds__applied.root', 'recreate') output_tree = Tree('multiBkg', model=OutputTree) r1 = TMVA.Reader("!Color:!Silent") r2 = TMVA.Reader("!Color:!Silent") # r3 = TMVA.Reader( "!Color:!Silent" ) r1.AddVariable('var1', output_tree.var1) r1.AddVariable('var2', output_tree.var2) r1.AddVariable('var3', output_tree.var3) r2.AddVariable('var1', output_tree.var1) r2.AddVariable('var2', output_tree.var2) r2.AddVariable('var3', output_tree.var3) # r3.AddVariable('var1', output_tree.var1) # r3.AddVariable('var2', output_tree.var2) # r3.AddVariable('var3', output_tree.var3)
Muon3_innerTrack_numberOfLostTrackerInnerHits = array('f', [0.]) Muon3_numberOfMatchedStations = array('f', [0.]) Muon3_calEnergy_had = array('f', [0.]) Muon3_innerTrack_numberofValidHits = array('f', [0.]) Muon3_calEnergy_em = array('f', [0.]) Muon3_innerTrack_numberOfLostTrackerOuterHits = array('f', [0.]) Muon3_innerTrack_normalizedChi2 = array('f', [0.]) Muon3_innerTrack_validFraction = array('f', [0.]) Muon3_innerTrack_pixelLayersWithMeasurement = array('f', [0.]) Muon3_ptErrOverPt = array('f', [0.]) fake = array('f', [0.]) MuonId = array('f', [-99.0]) readerMuonId = TMVA.Reader("!Color:!Silent") readerMuonId.AddVariable("Muon_caloCompatibility", Muon3_caloCompatibility) readerMuonId.AddVariable("Muon_segmentCompatibility", Muon3_segmentCompatibility) readerMuonId.AddVariable("Muon_innerTrack_numberOfLostTrackerHits", Muon3_innerTrack_numberOfLostTrackerHits) readerMuonId.AddVariable("Muon_innerTrack_numberOfLostTrackerInnerHits", Muon3_innerTrack_numberOfLostTrackerInnerHits) readerMuonId.AddVariable("Muon_numberOfMatchedStations", Muon3_numberOfMatchedStations) readerMuonId.AddVariable("Muon_calEnergy_had", Muon3_calEnergy_had) readerMuonId.AddVariable("Muon_innerTrack_numberofValidHits", Muon3_innerTrack_numberofValidHits) readerMuonId.AddVariable("Muon_calEnergy_em", Muon3_calEnergy_em) readerMuonId.AddVariable("Muon_innerTrack_numberOfLostTrackerOuterHits", Muon3_innerTrack_numberOfLostTrackerOuterHits)
## This tutorial shows how to apply a trained model to new data (regression). ## ## \macro_code ## ## \date 2017 ## \author TMVA Team from ROOT import TMVA, TFile, TString from array import array from subprocess import call from os.path import isfile # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() reader = TMVA.Reader("Color:!Silent") # Load data if not isfile('tmva_reg_example.root'): call([ 'curl', '-L', '-O', 'http://root.cern.ch/files/tmva_reg_example.root' ]) data = TFile.Open('tmva_reg_example.root') tree = data.Get('TreeR') branches = {} for branch in tree.GetListOfBranches(): branchName = branch.GetName() branches[branchName] = array('f', [-999]) tree.SetBranchAddress(branchName, branches[branchName])
from root_numpy.tmva import add_classification_events, evaluate_reader from root_numpy import ROOT_VERSION from ROOT import TMVA, TFile, TCut reader = TMVA.Reader() def arr(): return array('f', [0.]) reader2j.AddVariable("hmmpt", arr()) reader2j.AddVariable("hmmrap", arr()) reader2j.AddVariable("hmmthetacs", arr()) reader2j.AddVariable("hmmphics", arr()) reader2j.AddVariable("j1pt", arr()) reader2j.AddVariable("j1eta", arr()) reader2j.AddVariable("j2pt", arr()) reader2j.AddVariable("detajj", arr()) reader2j.AddVariable("dphijj", arr()) reader2j.AddVariable("mjj", arr()) reader2j.AddVariable("met", arr()) reader2j.AddVariable("zepen", arr()) reader2j.AddVariable("njets", arr()) reader2j.AddVariable("drmj", arr()) reader2j.AddVariable("m1ptOverMass", arr()) reader2j.AddVariable("m2ptOverMass", arr()) reader2j.AddVariable("m1eta", arr()) reader2j.AddVariable("m2eta", arr()) reader2j.AddSpectator("hmerr", arr()) reader2j.AddSpectator("weight", arr())
def main(): try: # Retrive command line options shortopts = "m:i:t:o:vh?" longopts = [ "methods=", "inputfile=", "inputtrees=", "outputfile=", "verbose", "help", "usage" ] opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts) except getopt.GetoptError: # Print help information and exit: print "ERROR: unknown options in argument %s" % sys.argv[1:] usage() sys.exit(1) infname = DEFAULT_INFNAME treeNameSig = DEFAULT_TREESIG treeNameBkg = DEFAULT_TREEBKG outfname = DEFAULT_OUTFNAME methods = DEFAULT_METHODS verbose = False for o, a in opts: if o in ("-?", "-h", "--help", "--usage"): usage() sys.exit(0) elif o in ("-m", "--methods"): methods = a elif o in ("-i", "--inputfile"): infname = a elif o in ("-o", "--outputfile"): outfname = a elif o in ("-t", "--inputtrees"): a.strip() trees = a.rsplit(' ') trees.sort() trees.reverse() if len(trees) - trees.count('') != 2: print "ERROR: need to give two trees (each one for signal and background)" print trees sys.exit(1) treeNameSig = trees[0] treeNameBkg = trees[1] elif o in ("-v", "--verbose"): verbose = True # Print methods mlist = methods.replace(' ', ',').split(',') print "=== TMVApplication: use method(s)..." for m in mlist: if m.strip() != '': print "=== - <%s>" % m.strip() # Import ROOT classes from ROOT import gSystem, gROOT, gApplication, TFile, TTree, TCut, TH1F, TStopwatch # check ROOT version, give alarm if 5.18 if gROOT.GetVersionCode() >= 332288 and gROOT.GetVersionCode() < 332544: print "*** You are running ROOT version 5.18, which has problems in PyROOT such that TMVA" print "*** does not run properly (function calls with enums in the argument are ignored)." print "*** Solution: either use CINT or a C++ compiled version (see TMVA/macros or TMVA/examples)," print "*** or use another ROOT version (e.g., ROOT 5.19)." sys.exit(1) # Logon not automatically loaded through PyROOT (logon loads TMVA library) load also GUI gROOT.SetMacroPath("../macros/") gROOT.Macro("../macros/TMVAlogon.C") # Import TMVA classes from ROOT from ROOT import TMVA # Create the Reader object reader = TMVA.Reader("!Color") # Create a set of variables and declare them to the reader # - the variable names must corresponds in name and type to # those given in the weight file(s) that you use # what to do ??? var1 = array('f', [0]) var2 = array('f', [0]) var3 = array('f', [0]) var4 = array('f', [0]) reader.AddVariable("var1+var2", var1) reader.AddVariable("var1-var2", var2) reader.AddVariable("var3", var3) reader.AddVariable("var4", var4) # book the MVA methods dir = "weights/" prefix = "TMVAnalysis_" for m in mlist: reader.BookMVA(m + " method", dir + prefix + m + ".weights.txt") ####################################################################### # For an example how to apply your own plugin method, please see # TMVA/macros/TMVApplication.C ####################################################################### # Book output histograms nbin = 80 histList = [] for m in mlist: histList.append(TH1F(m, m, nbin, -3, 3)) # Book example histogram for probability (the other methods would be done similarly) if "Fisher" in mlist: probHistFi = TH1F("PROBA_MVA_Fisher", "PROBA_MVA_Fisher", nbin, 0, 1) rarityHistFi = TH1F("RARITY_MVA_Fisher", "RARITY_MVA_Fisher", nbin, 0, 1) # Prepare input tree (this must be replaced by your data source) # in this example, there is a toy tree with signal and one with background events # we'll later on use only the "signal" events for the test in this example. # fname = "./tmva_example.root" print "--- Accessing data file: %s" % fname input = TFile.Open(fname) if not input: print "ERROR: could not open data file: %s" % fname sys.exit(1) # # Prepare the analysis tree # - here the variable names have to corresponds to your tree # - you can use the same variables as above which is slightly faster, # but of course you can use different ones and copy the values inside the event loop # print "--- Select signal sample" theTree = input.Get("TreeS") userVar1 = array('f', [0]) userVar2 = array('f', [0]) theTree.SetBranchAddress("var1", userVar1) theTree.SetBranchAddress("var2", userVar2) theTree.SetBranchAddress("var3", var3) theTree.SetBranchAddress("var4", var4) # Efficiency calculator for cut method nSelCuts = 0 effS = 0.7 # Process the events print "--- Processing: %i events" % theTree.GetEntries() sw = TStopwatch() sw.Start() for ievt in range(theTree.GetEntries()): if ievt % 1000 == 0: print "--- ... Processing event: %i" % ievt # Fill event in memory theTree.GetEntry(ievt) # Compute MVA input variables var1[0] = userVar1[0] + userVar2[0] var2[0] = userVar1[0] - userVar2[0] # Return the MVAs and fill to histograms if "CutsGA" in mlist: passed = reader.EvaluateMVA("CutsGA method", effS) if passed: nSelCuts = nSelCuts + 1 # Fill histograms with MVA outputs for h in histList: h.Fill(reader.EvaluateMVA(h.GetName() + " method")) # Retrieve probability instead of MVA output if "Fisher" in mlist: probHistFi.Fill(reader.GetProba("Fisher method")) rarityHistFi.Fill(reader.GetRarity("Fisher method")) # Get elapsed time sw.Stop() print "--- End of event loop: %s" % sw.Print() # Return computed efficeincies if "CutsGA" in mlist: eff = float(nSelCuts) / theTree.GetEntries() deff = math.sqrt(eff * (1.0 - eff) / theTree.GetEntries()) print "--- Signal efficiency for Cuts method : %.5g +- %.5g (required was: %.5g)" % ( eff, deff, effS) # Test: retrieve cuts for particular signal efficiency mcuts = reader.FindMVA("CutsGA method") cutsMin = array('d', [0, 0, 0, 0]) cutsMax = array('d', [0, 0, 0, 0]) mcuts.GetCuts(0.7, cutsMin, cutsMax) print "--- -------------------------------------------------------------" print "--- Retrieve cut values for signal efficiency of 0.7 from Reader" for ivar in range(4): print "... Cut: %.5g < %s <= %.5g" % ( cutsMin[ivar], reader.GetVarName(ivar), cutsMax[ivar]) print "--- -------------------------------------------------------------" # # write histograms # target = TFile("TMVApp.root", "RECREATE") for h in histList: h.Write() # Write also probability hists if "Fisher" in mlist: probHistFi.Write() rarityHistFi.Write() target.Close() print "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" print "==> TMVApplication is done!"
# In[11]: #path = "/eos/uscms/store/user/corderom/Corrections2016Rereco/" #phVals = ["EE","EB"] phVals = ["EE"] path = "" file = {} reader = {} for ph in phVals: file[ph] = "TMVAnalysis_BDT_"+ph+".weights.xml" reader[ph] = TMVA.Reader() # In[12]: varName= { "recoPhi" :["photonOnePhi"], "r9" :["photonOneR9"], "sieieFull5x5" :["photonOneSieie"], "sieipFull5x5" :["photonOneSieip"], "e2x2Full5x5/e5x5Full5x5":["photonOneE2x2","photonOneE5x5"], "recoSCEta" :["photonOneEta"], "rawE" :["photonOneScRawE"], "scEtaWidth" :["photonOneScEtaWidth"], "scPhiWidth" :["photonOneScPhiWidth"],
'var_Iso08MuMin', 'var_MaxMuonImpactAngle', 'var_MaxtrkKink', 'var_MuonglbkinkSum', 'var_MinMatchedStations', 'var_MaxdeltaMuZ' ] varlist_help = ['var_mass12', 'var_mass13'] print "Initializing TMVA..." # Setup TMVA TMVA.Tools.Instance() mvaReaderList = ['readerA', 'readerB', 'readerC', 'readerBC'] mvaReaders = {} mvaWeights = {} print "Setting up reader..." for reader in mvaReaderList: mvaReaders[reader] = TMVA.Reader('Color:!Silent') # tmpstr = 'MyTMVAClassification_Test_vars_'+fileStr(reader) tmpstr = args.weights_prefix + '_vars_' + fileStr(reader) #--weights-prefix mvaWeights[reader] = TString( BASEDIR + '/' + tmpstr + '_BDT.weights.xml' ) # weights weights.xml file after training, place it to CommonFiles #Vars2016_vars_ # Open input file and set branch addresses rootfile = TFile(filename, 'READ') rootfile.ls() tree_bkg = rootfile.Get('TreeB') tree_ds = rootfile.Get('TreeS_Ds')
def main(): usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option( '-s', '--suffix', dest='input_suffix', help='suffix used to identify inputs from network training', default=None, type='string') (opt, args) = parser.parse_args() '''if opt.json == None: print 'input variable .json not defined!' sys.exit(1)''' if opt.input_suffix == None: print 'Input files suffix not defined!' sys.exit(1) classifier_suffix = opt.input_suffix classifier_parent_dir = 'BinaryClassifier_BDTG_%s' % (classifier_suffix) # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() reader = TMVA.Reader("Color:!Silent") #ttH_filename = 'samples/2017MC/training_region/TTHnobb.root' ttH_filename = 'samples/DiLepTR_ttH_bInclude.root' data_ttH = TFile.Open(ttH_filename) #data_ttH_tree = data_ttH.Get('syncTree') data_ttH_tree = data_ttH.Get('BOOM') print 'ttH file to evaluate: ', ttH_filename if classifier_suffix == "ttHvsttJets": #bckg_filename = 'samples/2017MC/testing_region/TTJets.root' #bckg_filename = 'samples/2017MC/training_region/TTJets.root' bckg_filename = 'samples/DiLepTR_ttJets_bInclude.root' data_bckg = TFile.Open(bckg_filename) #data_bckg_tree = data_bckg.Get('syncTree') data_bckg_tree = data_bckg.Get('BOOM') elif classifier_suffix == "ttHvsttV": #bckg_filename = 'samples/2017MC/testing_region/ttV.root' #bckg_filename = 'samples/2017MC/training_region/ttV.root' bckg_filename = 'samples/DiLepTR_ttV_bInclude.root' data_bckg = TFile.Open(bckg_filename) #data_bckg_tree = data_bckg.Get('syncTree') data_bckg_tree = data_bckg.Get('BOOM') print 'ttJets file to evaluate: ', bckg_filename keys_list = [] if classifier_suffix == "ttHvsttJets": keys_list = [( "max(abs(LepGood_eta[iLepFO_Recl[0]]),abs(LepGood_eta[iLepFO_Recl[1]]))", "maxeta"), ("nJet25_Recl", "Jet_numLoose"), ("mindr_lep1_jet", "mindrlep1jet"), ("mindr_lep2_jet", "mindrlep2jet"), ("MT_met_lep1", "SR_InvarMassT"), ("max(-1.1,BDTv8_eventReco_mvaValue)", "hadTop_BDT")] elif classifier_suffix == "ttHvsttV": keys_list = [( "max(abs(LepGood_eta[iLepFO_Recl[0]]),abs(LepGood_eta[iLepFO_Recl[1]]))", "maxeta"), ("nJet25_Recl", "Jet_numLoose"), ("mindr_lep1_jet", "mindrlep1jet"), ("mindr_lep2_jet", "mindrlep2jet"), ("MT_met_lep1", "SR_InvarMassT"), ("LepGood_conePt[iLepFO_Recl[1]]", "corrptlep1"), ("LepGood_conePt[iLepFO_Recl[0]]", "corrptlep2"), ("max(-1.1,BDTv8_eventReco_Hj_score)", "Hj1_BDT")] branches_ttree = {} for reader_key, ttree_key in keys_list: print 'Setting Branch address: ', ttree_key if ttree_key == 'Jet_numLoose': #branches_ttree[ttree_key] = array('I', [999]) branches_ttree[ttree_key] = array('d', [999]) else: #branches_ttree[ttree_key] = array('f', [-999]) branches_ttree[ttree_key] = array('d', [-999]) if 'hadTop_BDT' in ttree_key: keyname = 'hadTop_BDT' data_ttH_tree.SetBranchAddress(str(keyname), branches_ttree[ttree_key]) data_bckg_tree.SetBranchAddress(str(keyname), branches_ttree[ttree_key]) elif 'Hj1_BDT' in ttree_key: keyname = 'Hj1_BDT' data_ttH_tree.SetBranchAddress(str(keyname), branches_ttree[ttree_key]) data_bckg_tree.SetBranchAddress(str(keyname), branches_ttree[ttree_key]) else: data_ttH_tree.SetBranchAddress(str(ttree_key), branches_ttree[ttree_key]) data_bckg_tree.SetBranchAddress(str(ttree_key), branches_ttree[ttree_key]) # Keep track of event numbers for cross-checks. event_number_branch = array('L', [999]) data_ttH_tree.SetBranchAddress('nEvent', event_number_branch) branches_reader = {} # Register names of inputs with reader. Together with the name give the address of the local variable that carries the updated input variables during event loop. for reader_key, ttree_key in keys_list: print 'Add variable name %s: ' % reader_key branches_reader[reader_key] = array('f', [-999]) reader.AddVariable(str(reader_key), branches_reader[reader_key]) iLepFO_Recl0 = array('f', [-999]) reader.AddSpectator('iLepFO_Recl[0]', iLepFO_Recl0) iLepFO_Recl1 = array('f', [-999]) reader.AddSpectator('iLepFO_Recl[1]', iLepFO_Recl1) iLepFO_Recl2 = array('f', [-999]) reader.AddSpectator('iLepFO_Recl[2]', iLepFO_Recl2) # Book methods # First argument is user defined name. Doesn not have to be same as training name. # True type of method and full configuration are read from the weights file specified in the second argument. if classifier_suffix == "ttHvsttJets": #mva_weights_dir = 'BinaryClassifier_BDTG_%s/weights/2lss_ttbar_withBDTv8_BDTG.weights.xml' % classifier_suffix mva_weights_dir = '../../../FromMarco/2lss_ttbar_withBDTv8_BDTG.weights.xml' elif classifier_suffix == "ttHvsttV": #mva_weights_dir = 'BinaryClassifier_BDTG_%s/weights/2lss_ttV_withHj_BDTG.weights.xml' % classifier_suffix mva_weights_dir = '../../../FromMarco/2lss_ttV_withHj_BDTG.weights.xml' print 'using weights file: ', mva_weights_dir reader.BookMVA('BDTG', TString(mva_weights_dir)) classifier_samples_dir = classifier_parent_dir + "/outputs" classifier_plots_dir = classifier_parent_dir + "/plots" if not os.path.exists(classifier_plots_dir): os.makedirs(classifier_plots_dir) if not os.path.exists(classifier_samples_dir): os.makedirs(classifier_samples_dir) # Define outputs: files to store histograms/ttree with results from application of classifiers and any histos/trees themselves. output_file_name = '%s/Applied_%s.root' % (classifier_samples_dir, classifier_parent_dir) output_file = TFile.Open(output_file_name, 'RECREATE') network_evaluation(data_ttH_tree, keys_list, 'ttH', branches_ttree, branches_reader, reader) if classifier_suffix == 'ttHvsttJets': network_evaluation(data_bckg_tree, keys_list, 'ttjets', branches_ttree, branches_reader, reader) if classifier_suffix == 'ttHvsttV': network_evaluation(data_bckg_tree, keys_list, 'ttV', branches_ttree, branches_reader, reader) output_file.Close()
foutname += str(emax) + '.root' fout = TFile(foutname, 'recreate') if sample == 'ttbar': is_ttbar = 1 else: is_ttbar = 0 # print 'emin and max', emin, emax if emax == 0: print 'emax == 0' print "Using top mass constraint" # if sample != 'data': b1 = Particle('b1',tkin,1) # else: b1 = Particle('b1',tkin) # if sample != 'data': b2 = Particle('b2',tkin,1) # else: b2 = Particle('b2',tkin) reader = TMVA.Reader("!Color:!Silent") readerMisc = TMVA.Reader("!Color:!Silent") readerQGL = TMVA.Reader("!Color:!Silent") mytree = ManageTTree(tvars, tkin) #usevar = ['top1_m','top2_m','w1_m','w2_m','deltaRl1l2','deltaRq1q2','deltaRb1b2','deltaRb1w1','deltaRb2w2','deltaPhiw1w2','deltaPhit1t2','q1b1_mass','p1b2_mass','deltaRb1w2','deltaRb2w1','mindeltaRb1q','simple_chi2','mindeltaRb2p', 'deltaEtal1l2', 'deltaEtaq1q2','jet_CSV[0]','jet_CSV[1]','jet_CSV[2]','jet_CSV[3]','jet_CSV[4]','jet_CSV[5]'] #usevar = ['top1_m','top2_m','w1_m','w2_m','deltaRl1l2','deltaRq1q2','deltaRb1b2','deltaRb1w1','deltaRb2w2','deltaPhit1t2','q1b1_mass','p1b2_mass','deltaRb1w2','deltaRb2w1','simple_chi2','mindeltaRb2p', 'deltaEtal1l2', 'deltaEtaq1q2','jet_CSV[0]','jet_CSV[1]','jet_CSV[2]','jet_CSV[3]','jet_CSV[4]','jet_CSV[5]'] usevar = [ 'top1_m', 'top2_m', 'w1_m', 'w2_m', 'deltaRl1l2', 'deltaRq1q2', 'deltaRb1b2', 'deltaRb1w1', 'deltaRb2w2', 'deltaPhiw1w2', 'deltaPhit1t2', 'q1b1_mass', 'p1b2_mass', 'deltaRb1w2', 'deltaRb2w1', 'mindeltaRb1q', 'mindeltaRb2p', 'deltaEtal1l2', 'deltaEtaq1q2', 'jet_CSV[0]', 'jet_CSV[1]', 'jet_CSV[2]', 'jet_CSV[3]', 'jet_CSV[4]',
file2 = open('../mvavars2.txt') file3 = open('../mvavars3.txt') mvavars1 = [] mvavars2 = [] mvavars3 = [] for line in file1: mvavars1.append(line.strip()) for line in file2: mvavars2.append(line.strip()) for line in file3: print line mvavars3.append(line.strip()) reader3 = TMVA.Reader() var3_ = [] for i, var3 in enumerate(mvavars1): var3_.append(array.array('f',[0])) reader3.AddVariable(var3,var3_[i]) reader3.BookMVA("MLP","weights/TMVAClassification_HZZ_2e2mu_MLP.weights.xml") #weight weight ele1_pt ele1_eta ele1_phi ele1_charge ele1_trackIso ele1_EcalIso ele1_HcalIso ele1_X ele1_SIP ele2_pt ele2_eta ele2_phi ele2_charge ele2_trackIso ele2_EcalIso ele2_HcalIso ele2_X ele2_SIP ele3_pt ele3_eta ele3_phi ele3_charge ele3_trackIso ele3_EcalIso ele3_HcalIso ele3_X ele3_SIP ele4_pt ele4_eta ele4_phi ele4_charge ele4_trackIso ele4_EcalIso ele4_HcalIso ele4_X ele4_SIP #worst_iso_X second_worst_iso_X worst_vertex second_worst_vertex mZ mZstar mbestH index channel sample end def loop(hweights, sig, hs, c1): #------- sig events ---------
for period, channel in itertools.product(periods, channels): dijetDEta = array.array('f', [-999]) dijetDPhi = array.array('f', [-999]) llgJJDPhi = array.array('f', [-999]) jPhotonDRMin = array.array('f', [-999]) ptt = array.array('f', [-999]) jetOnePt = array.array('f', [-999]) jetTwoPt = array.array('f', [-999]) kin_bdt = array.array('f', [-999]) vbfPtBalance = array.array('f', [-999]) photonZepp = array.array('f', [-999]) kin_bdt_james = array.array('f', [-999]) vbfWeightsFile = 'trained_bdts/vbf_bdt_combined_ming-yan_current.xml' vbf_reader = t.Reader("!Color:Silent") vbf_reader.AddVariable('dijetDEta', dijetDEta) vbf_reader.AddVariable('dijetDPhi', dijetDPhi) vbf_reader.AddVariable('llgJJDPhi', llgJJDPhi) vbf_reader.AddVariable('jPhotonDRMin', jPhotonDRMin) vbf_reader.AddVariable('ptt', ptt) vbf_reader.AddVariable('jetOnePt', jetOnePt) vbf_reader.AddVariable('jetTwoPt', jetTwoPt) vbf_reader.AddVariable('kin_bdt', kin_bdt) vbf_reader.AddVariable('vbfPtBalance', vbfPtBalance) vbf_reader.AddVariable('photonZepp', photonZepp) vbf_reader.BookMVA('BDT method', vbfWeightsFile) #vbfWeightsFileJames = 'trained_bdts/vbf_bdt_combined_james_current_half_signal_BDT.weights.xml'
## \macro_code ## ## \author Lailin XU from ROOT import TMVA, TFile, TTree, TCut, TH1F, TCanvas, gROOT, TLegend from subprocess import call from os.path import isfile from array import array gROOT.SetStyle("ATLAS") # Setup TMVA TMVA.Tools.Instance() # Reader. One reader for each application. reader = TMVA.Reader("Color:!Silent") reader_S = TMVA.Reader("Color:!Silent") reader_B = TMVA.Reader("Color:!Silent") # Inputs # ============= # Load data # An unknown sample trfile = "Zp2TeV_ttbar.root" data = TFile.Open(trfile) tree = data.Get('tree') # Known signal trfile_S = "Zp1TeV_ttbar.root" data_S = TFile.Open(trfile_S) tree_S = data_S.Get('tree')
def beginLoop(self, setup): super(TreeProducer, self).beginLoop(setup) self.rootfile = TFile('/'.join([self.dirName, 'tree.root']), 'recreate') self.tree = Tree('events', '') self.tree.var('weight', float) self.tree.var('nbjets', float) bookParticle(self.tree, 'l') bookMet(self.tree, 'met') # fatjet stuff for flavour in ['higgs', 'top']: bookParticle(self.tree, '{}jet'.format(flavour)) bookParticle(self.tree, 'softDropped_{}jet'.format(flavour)) self.tree.var('{}jet_tau1'.format(flavour), float) self.tree.var('{}jet_tau2'.format(flavour), float) self.tree.var('{}jet_tau3'.format(flavour), float) self.tree.var('{}jet_tau32'.format(flavour), float) self.tree.var('{}jet_tau31'.format(flavour), float) self.tree.var('{}jet_tau21'.format(flavour), float) self.tree.var('{}jet_flow15'.format(flavour), float) self.tree.var('{}jet_flow25'.format(flavour), float) self.tree.var('{}jet_flow35'.format(flavour), float) self.tree.var('{}jet_flow45'.format(flavour), float) self.tree.var('{}jet_flow55'.format(flavour), float) self.tree.var('{}jet_nbs'.format(flavour), float) self.tree.var('{}jet_mbs'.format(flavour), float) self.tree.var('{}jet_bdt_th'.format(flavour), float) # for MVA self.reader = TMVA.Reader() self.bdt_tau1 = array.array('f', [0]) self.bdt_tau2 = array.array('f', [0]) self.bdt_tau3 = array.array('f', [0]) self.bdt_tau21 = array.array('f', [0]) self.bdt_tau31 = array.array('f', [0]) self.bdt_tau32 = array.array('f', [0]) self.bdt_flow15 = array.array('f', [0]) self.bdt_flow25 = array.array('f', [0]) self.bdt_flow35 = array.array('f', [0]) self.bdt_flow45 = array.array('f', [0]) self.bdt_flow55 = array.array('f', [0]) self.bdt_jet_m = array.array('f', [0]) self.bdt_softDropped_jet_m = array.array('f', [0]) self.bdt_jet_nbs = array.array('f', [0]) self.reader.AddVariable('jet_m', self.bdt_jet_m) self.reader.AddVariable('softDropped_jet_m', self.bdt_softDropped_jet_m) self.reader.AddVariable('jet_tau1', self.bdt_tau1) self.reader.AddVariable('jet_tau2', self.bdt_tau2) self.reader.AddVariable('jet_tau3', self.bdt_tau3) self.reader.AddVariable('jet_tau32', self.bdt_tau32) self.reader.AddVariable('jet_tau31', self.bdt_tau31) self.reader.AddVariable('jet_tau21', self.bdt_tau21) self.reader.AddVariable('jet_flow15', self.bdt_flow15) self.reader.AddVariable('jet_flow25', self.bdt_flow25) self.reader.AddVariable('jet_flow35', self.bdt_flow35) self.reader.AddVariable('jet_flow45', self.bdt_flow45) self.reader.AddVariable('jet_flow55', self.bdt_flow55) self.reader.AddVariable('jet_nbs', self.bdt_jet_nbs) #path = "/afs/cern.ch/work/s/selvaggi/private/FCCSW/heppy/FCChhAnalyses/tth_boosted/" path = "/eos/experiment/fcc/hh/analyses/Higgs/ttH/BDT/" self.reader.BookMVA("BDT", str(path) + "BDT_BDT_Higgs_vs_Top.weights.xml")
from array import array from tqdm import tqdm import math from bdt_common import total_pot, variables, spectators, choose_shower from bdt_common import is_active, is_fiducial, distance from bdt_common import ELECTRON_MASS, PROTON_MASS, MAX_N_SHOWERS, MAX_N_TRACKS, N_UNI from bdt_common import PROTON_THRESHOLD, ELECTRON_THRESHOLD from proton_energy import length2energy import time import statistics import sys STORE_SYS = True reader_reclass = TMVA.Reader(":".join(["!V", "!Silent", "Color"])) angle = array("f", [0]) pca = array("f", [0]) res = array("f", [0]) open_angle = array("f", [0]) n_hits = array("f", [0]) ratio = array("f", [0]) reader_reclass.AddVariable("angle", angle) reader_reclass.AddVariable("pca", pca) reader_reclass.AddVariable("res", res) reader_reclass.AddVariable("open_angle", open_angle) reader_reclass.AddVariable("ratio", ratio) reader_reclass.AddVariable("n_hits", n_hits) reader_reclass.BookMVA(
zgLittleTheta = array.array('f', [-999]) zgBigTheta = array.array('f', [-999]) llgPtOverM = array.array('f', [-999]) leptonOneEta = array.array('f', [-999]) leptonTwoEta = array.array('f', [-999]) photonEta = array.array('f', [-999]) zgPhi = array.array('f', [-999]) photonMVA = array.array('f', [-999]) corrPhotonMVA = array.array('f', [-999]) photonERes = array.array('f', [-999]) lPhotonDRMin = array.array('f', [-999]) lPhotonDRMax = array.array('f', [-999]) kinWeightsFile = 'trained_bdts/kin_bdt_combined_ming-yan_current.xml' kin_reader = t.Reader("!Color:Silent") kin_reader.AddVariable('zgLittleTheta', zgLittleTheta) kin_reader.AddVariable('zgBigTheta', zgBigTheta) kin_reader.AddVariable('llgPtOverM', llgPtOverM) kin_reader.AddVariable('leptonOneEta', leptonOneEta) kin_reader.AddVariable('leptonTwoEta', leptonTwoEta) kin_reader.AddVariable('photonEta', photonEta) kin_reader.AddVariable('zgPhi', zgPhi) kin_reader.AddVariable('photonMVA', corrPhotonMVA) kin_reader.AddVariable('photonERes', photonERes) kin_reader.AddVariable('lPhotonDRMin', lPhotonDRMin) kin_reader.AddVariable('lPhotonDRMax', lPhotonDRMax) kin_reader.BookMVA('BDT method', kinWeightsFile)
with open(filename) as f: lines = f.read().splitlines() emin = map(int, re.findall(r'\d+', lines[0]))[0] emax = map(int, re.findall(r'\d+', lines[0]))[1] foutname = 'Skim_' foutname += sample + '_' foutname += str(emax) + '.root' fout = TFile(foutname, 'recreate') if sample == 'ttbar': is_ttbar = 1 else: is_ttbar = 0 if emax == 0: print 'emax == 0' reader = TMVA.Reader("!Color:!Silent") readerQCD = TMVA.Reader("!Color:!Silent") mytree = ManageTTree(tvars, tkin) usevar = [ 'lp1_eta', 'lp2_eta', 'lq1_eta', 'lq2_eta', 'b1_eta', 'b2_eta', 'top1_m', 'top2_m', 'w1_m', 'w2_m', 'deltaRp1p2', 'deltaRq1q2', 'jet_MOverPt[0]', 'jet_MOverPt[1]', 'jet_MOverPt[2]', 'jet_MOverPt[3]', 'jet_MOverPt[4]', 'jet_MOverPt[5]', 'deltaRb1b2', 'deltaRb1w1', 'deltaRb2w2', 'deltaPhiw1w2', 'deltaPhit1t2', 'p1b1_mass', 'q1b2_mass', 'deltaRb1w2', 'deltaRb2w1', 'mindeltaRb1p', 'simple_chi2', 'mindeltaRb2q', 'deltaEtap1p2', 'deltaEtaq1q2', 'jet_CSV[0]', 'jet_CSV[1]', 'jet_CSV[2]', 'jet_CSV[3]', 'jet_CSV[4]', 'jet_CSV[5]' ] useQCD = [
def tmva_model(): model_path = "dataset_dnn/weights/TMVAClassification_BDT.weights.xml" print "loading TMVA........." print "....\n...\n..\n." reader = tmva.Reader() #reloading all the features into the loader #tmva requires that le = len(y_test) for col in columns: reader.AddVariable(col, np.zeros(le, dtype='float32')) # accepts 32 bit only. reader.BookMVA("BDT", model_path) decision_value = [] prediction = [] for row in x_test: a = root.std.vector(root.Double)() for r in row: a.push_back(r) value = reader.EvaluateMVA(a, "BDT") decision_value.append(value) if value > 0: prediction.append(1) else: prediction.append(0) # print classification_report(y_test, prediction, target_names=["background", "signal"]) # print "Area under ROC curve: %.4f"%(roc_auc_score(y_test, prediction)) dval_arr = np.array(decision_value) bg_scores = dval_arr[y_test == 0] # scores corresponding to background sg_scores = dval_arr[y_test == 1] # scores corresponding to signal plt.figure("fig-1") plt.subplot(1, 2, 1) #background histogram h2 = plt.hist(bg_scores.ravel(), color='r', alpha=0.5, range=(-0.7, 0.4), bins=30) #SIGNAL(tau-tau decay) histogram h1 = plt.hist(sg_scores.ravel(), color='b', ec='b', alpha=0.5, range=(-.7, 0.4), bins=30) plt.xlabel("TMVA BDT output") plt.ylabel("Events") handles = [ Rectangle((0, 1), 1, 1, color='r', ec="k"), Rectangle((0, 1), 1, 1, color='b', ec="k") ] plt.legend(handles, ["background", "signal"]) AUC = roc_auc_score(y_test, prediction) print "Area under ROC curve: %.4f" % (AUC) plt.subplot(1, 2, 2) #calculating false and true postive rate fpr, tpr, thresholds = roc_curve(y_test, dval_arr) draw_roc_curve(plt, fpr, tpr, AUC) plt.show()
import ROOT, array import ROOT, array from ROOT import TFile, TH1F, TGraph, TCanvas, TLegend, TTree from ROOT import TMVA, TMath import sys print sys.argv process = sys.argv[1] reader = TMVA.Reader("!V") nJet = array.array('f', [0]) mindr_lep1_jet = array.array('f', [0]) mindr_lep2_jet = array.array('f', [0]) mindr_lep3_jet = array.array('f', [0]) avg_dr_jet = array.array('f', [0]) lep1_abs_eta = array.array('f', [0]) lep2_abs_eta = array.array('f', [0]) lep3_abs_eta = array.array('f', [0]) max_lep_eta = array.array('f', [0]) lep1_conePt = array.array('f', [0]) lep2_conePt = array.array('f', [0]) lep3_conePt = array.array('f', [0]) mindr_tau_jet = array.array('f', [0]) ptmiss = array.array('f', [0]) mT_lep1 = array.array('f', [0]) mT_lep2 = array.array('f', [0]) mT_lep3 = array.array('f', [0]) htmiss = array.array('f', [0]) dr_leps = array.array('f', [0]) tau_pt = array.array('f', [0]) tau_abs_eta = array.array('f', [0])
def main(weights, picklename, filename, treename='bTag_AntiKt2PV0TrackJets'): ''' evaluate the tmva method after transforming input data into right format Args: ----- weights: .xml file out of mv2 training containing bdt parameters picklename: name of the output pickle to store new mv2 values filename: .root file with ntuples used to evaluate the tmva method treename: (optional) name of the TTree to consider Returns: -------- status Raises: ------- nothing yet, but to be improved ''' print 'Parsing XML file...' # -- Load XML file tree = ET.parse(weights) root = tree.getroot() # -- Get list of variable names from XML file var_list = [ var.attrib['Label'] for var in root.findall('Variables')[0].findall('Variable') ] # -- Count the input variables that go into MV2: n_vars = len(var_list) print 'Loading .root file for evaluation...' # -- Get ntuples: df = pup.root2panda( filename, treename, branches=[ 'jet_pt', 'jet_eta', 'jet_phi', 'jet_m', 'jet_ip2d_pu', 'jet_ip2d_pc', 'jet_ip2d_pb', 'jet_ip3d_pu', 'jet_ip3d_pc', 'jet_ip3d_pb', 'jet_sv1_vtx_x', 'jet_sv1_vtx_y', 'jet_sv1_vtx_z', 'jet_sv1_ntrkv', 'jet_sv1_m', 'jet_sv1_efc', 'jet_sv1_n2t', 'jet_sv1_sig3d', 'jet_jf_n2t', 'jet_jf_ntrkAtVx', 'jet_jf_nvtx', 'jet_jf_nvtx1t', 'jet_jf_m', 'jet_jf_efc', 'jet_jf_sig3d', 'jet_jf_deta', 'jet_jf_dphi', 'PVx', 'PVy', 'PVz' ]) # -- Insert default values, calculate MV2 variables from the branches in df df = transformVars(df) # -- Map ntuple names to var_list names_mapping = { 'pt': 'jet_pt', 'abs(eta)': 'abs(jet_eta)', 'ip2': 'jet_ip2', 'ip2_c': 'jet_ip2_c', 'ip2_cu': 'jet_ip2_cu', 'ip3': 'jet_ip3', 'ip3_c': 'jet_ip3_c', 'ip3_cu': 'jet_ip3_cu', 'sv1_ntkv': 'jet_sv1_ntrkv', 'sv1_mass': 'jet_sv1_m', 'sv1_efrc': 'jet_sv1_efc', 'sv1_n2t': 'jet_sv1_n2t', 'sv1_Lxy': 'jet_sv1_Lxy', 'sv1_L3d': 'jet_sv1_L3d', 'sv1_sig3': 'jet_sv1_sig3d', 'sv1_dR': 'jet_sv1_dR', 'jf_n2tv': 'jet_jf_n2t', 'jf_ntrkv': 'jet_jf_ntrkAtVx', 'jf_nvtx': 'jet_jf_nvtx', 'jf_nvtx1t': 'jet_jf_nvtx1t', 'jf_mass': 'jet_jf_m', 'jf_efrc': 'jet_jf_efc', 'jf_dR': 'jet_jf_dR', 'jf_sig3': 'jet_jf_sig3d' } print 'Initializing TMVA...' # -- TMVA: Initialize reader, add empty variables and weights from training reader = TMVA.Reader() for n in range(n_vars): reader.AddVariable(var_list[n], array('f', [0])) reader.BookMVA('BDTG akt2', weights) print 'Creating feature matrix...' # -- Get features for each event and store them in X_test X_buf = [] for event in df[[names_mapping[var] for var in var_list]].values: X_buf.extend( np.array([normalize_type(jet) for jet in event]).T.tolist()) X_test = np.array(X_buf) print 'Evaluating!' # -- TMVA: Evaluate! twoclass_output = evaluate_reader(reader, 'BDTG akt2', X_test) # -- Reshape the MV2 output into event-jet format reorganized = match_shape(twoclass_output, df['jet_pt']) import cPickle print 'Saving new MV2 weights in {}'.format(picklename) cPickle.dump(reorganized, open(picklename, 'wb')) # -- Write the new branch to the tree (currently de-activated) #add_branch(reorganized, filename, treename, 'jet_mv2c20_new') print 'Done. Success!' return 0
def main(): usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option( '-s', '--suffix', dest='input_suffix', help='suffix used to identify inputs from network training', default=None, type='string') parser.add_option('-j', '--json', dest='json', help='json file with list of variables', default=None, type='string') parser.add_option('-i', '--input', dest='input_file', help='input file', default=None, type='string') (opt, args) = parser.parse_args() jsonFile = open(opt.json, 'r') if opt.json == None: print 'input variable .json not defined!' sys.exit(1) if opt.input_suffix == None: print 'Input files suffix not defined!' sys.exit(1) if opt.input_file == None: print 'Input file not defined!' sys.exit(1) new_variable_list = json.load(jsonFile, encoding="utf-8").items() n_input_vars = 0 for key, value in new_variable_list: n_input_vars = n_input_vars + 1 input_file = opt.input_file classifier_suffix = opt.input_suffix classifier_parent_dir = '/afs/cern.ch/work/j/jthomasw/private/IHEP/ttHML/github/ttH_multilepton/DNN/Evaluation/V7-DNN_%s' % ( classifier_suffix) # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() reader = TMVA.Reader("Color:!Silent") # Check files exist if not isfile(input_file): print 'No such input file: %s' % input_file # Open files and load ttrees data_file = TFile.Open(input_file) data_tree = data_file.Get("syncTree") branches_tree = {} integer_branches_tree = {} for key, value in new_variable_list: if 'hadTop_BDT' in key: branches_tree[key] = array('f', [-999]) keyname = 'hadTop_BDT' data_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif 'Hj1_BDT' in key: branches_tree[key] = array('f', [-999]) keyname = 'Hj1_BDT' data_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif ('n_fakeablesel_mu' in key) or ('n_fakeablesel_ele' in key) or ('Jet_numLoose' in key): integer_branches_tree[key] = array('I', [9999]) data_tree.SetBranchAddress(str(key), integer_branches_tree[key]) else: branches_tree[key] = array('f', [-999]) data_tree.SetBranchAddress(str(key), branches_tree[key]) branches_reader = {} # Register names of inputs with reader. Together with the name give the address of the local variable that carries the updated input variables during event loop. for key, value in new_variable_list: print 'Add variable name %s: ' % key branches_reader[key] = array('f', [-999]) reader.AddVariable(str(key), branches_reader[key]) event_number = array('f', [-999]) reader.AddSpectator('nEvent', event_number) # Book methods # First argument is user defined name. Doesn not have to be same as training name. # True type of method and full configuration are read from the weights file specified in the second argument. mva_weights_dir = '%s/weights/Factory_V7-DNN_%s_DNN.weights.xml' % ( classifier_parent_dir, classifier_suffix) print 'using weights file: ', mva_weights_dir reader.BookMVA('DNN', TString(mva_weights_dir)) if '/2L/' in input_file: analysis_region = '2L' elif '/ttWctrl/' in input_file: analysis_region = 'ttWctrl' elif '/JESDownttWctrl/' in input_file: analysis_region = 'JESDownttWctrl' elif '/JESUpttWctrl/' in input_file: analysis_region = 'JESUpttWctrl' elif '/ClosTTWctrl/' in input_file: analysis_region = 'ClosTTWctrl' elif '/ttZctrl/' in input_file: analysis_region = 'ttZctrl' elif '/Clos2LSS/' in input_file: analysis_region = 'Closure' elif '/JESDown2L/' in input_file: analysis_region = 'JESDown2L' elif '/JESUp2L/' in input_file: analysis_region = 'JESUp2L' time_suffix = str(datetime.now(pytz.utc)).split(' ') print time_suffix[0] #classifier_samples_dir = classifier_parent_dir+"/outputs" classifier_samples_dir = classifier_parent_dir + "/outputs-newbinning" #classifier_plots_dir = classifier_parent_dir+"/plots" classifier_plots_dir = classifier_parent_dir + "/plots-newbinning" if not os.path.exists(classifier_plots_dir): os.makedirs(classifier_plots_dir) if not os.path.exists(classifier_samples_dir): os.makedirs(classifier_samples_dir) analysis_region_samples_dir = '%s/%s' % (classifier_samples_dir, analysis_region) analysis_region_plots_dir = '%s/%s' % (classifier_plots_dir, analysis_region) if not os.path.exists(analysis_region_plots_dir): os.makedirs(analysis_region_plots_dir) if not os.path.exists(analysis_region_samples_dir): os.makedirs(analysis_region_samples_dir) output_suffix = input_file[input_file.rindex('/') + 1:] print 'output_suffix: ', output_suffix # Define outputs: files to store histograms/ttree with results from application of classifiers and any histos/trees themselves. output_file_name = '%s/Evaluated_%s_%s' % ( analysis_region_samples_dir, classifier_suffix, output_suffix) output_file = TFile.Open(output_file_name, 'RECREATE') output_tree = data_tree.CopyTree("") output_tree.SetName("output_tree") nEvents_check = output_tree.BuildIndex("nEvent", "run") print 'Copied %s events from original tree' % (nEvents_check) sample_nickname = '' if 'THQ_htt_2L' in input_file: sample_nickname = 'THQ_htt_2L' if 'THQ_hzz_2L' in input_file: sample_nickname = 'THQ_hzz_2L' if 'THW_hww_2L' in input_file: sample_nickname = 'THW_hww_2L' if 'TTH_hmm_2L' in input_file: sample_nickname = 'TTH_hmm_2L' if 'TTH_htt_2L' in input_file: sample_nickname = 'TTH_htt_2L' if 'TTH_hzz_2L' in input_file: sample_nickname = 'TTH_hzz_2L' if 'THQ_hww_2L' in input_file: sample_nickname = 'THQ_hww_2L' if 'THW_htt_2L' in input_file: sample_nickname = 'THW_htt_2L' if 'THW_hzz_2L' in input_file: sample_nickname = 'THW_hzz_2L' if 'TTH_hot_2L' in input_file: sample_nickname = 'TTH_hot_2L' if 'TTH_hww_2L' in input_file: sample_nickname = 'TTH_hww_2L' if 'TTWW_2L' in input_file: sample_nickname = 'TTWW_2L' if 'TTW_2L' in input_file: sample_nickname = 'TTW_2L' if 'TTZ_2L' in input_file: sample_nickname = 'TTZ_2L' if 'Conv_2L' in input_file: sample_nickname = 'Conv_2L' if 'EWK_2L' in input_file: sample_nickname = 'EWK_2L' if 'Fakes_2L' in input_file: sample_nickname = 'Fakes_2L' if 'Flips_2L' in input_file: sample_nickname = 'Flips_2L' if 'Rares_2L' in input_file: sample_nickname = 'Rares_2L' if 'TT_Clos' in input_file: sample_nickname = 'TT_Clos' if 'Data' in input_file: sample_nickname = 'Data' # Evaluate network and use max node response to categorise event. Only maximum node response will be plotted per event meaning each event will only contribute in the maximum nodes response histogram. network_evaluation(data_tree, new_variable_list, sample_nickname, branches_tree, integer_branches_tree, branches_reader, reader, True, output_tree) output_file.Write() gDirectory.Delete("syncTree;*") output_file.Close() print 'Job complete. Exiting.' sys.exit(0)
def main(): usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option( '-s', '--suffix', dest='input_suffix', help='suffix used to identify inputs from network training', default=None, type='string') parser.add_option('-j', '--json', dest='json', help='json file with list of variables', default=None, type='string') (opt, args) = parser.parse_args() jsonFile = open(opt.json, 'r') if opt.json == None: print 'input variable .json not defined!' sys.exit(1) if opt.input_suffix == None: print 'Input files suffix not defined!' sys.exit(1) new_variable_list = json.load(jsonFile, encoding='utf-8').items() # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() reader = TMVA.Reader("Color:!Silent") input_file_ttH = 'samples/2017_updated_MC/2LSS/TTH_2LSS.root' input_file_ttV = 'samples/2017_updated_MC/2LSS/ttV.root' #input_file_ttJets = 'samples/2017_updated_MC/2LSS/TTJets.root' input_file_ttJets = 'samples/2017_updated_MC/2LSS/Fakes_2LSS.root' # Check files exist if not isfile(input_file_ttH): print 'No such input file: %s' % input_file_ttH if not isfile(input_file_ttJets): print 'No such input file: %s' % input_file_ttJets if not isfile(input_file_ttV): print 'No such input file: %s' % input_file_ttV # Open files and load ttrees data_ttH = TFile.Open(input_file_ttH) data_ttV = TFile.Open(input_file_ttV) data_ttJets = TFile.Open(input_file_ttJets) data_ttH_tree = data_ttH.Get('syncTree') data_ttV_tree = data_ttV.Get('syncTree') data_ttJets_tree = data_ttJets.Get('syncTree') branches_tree = {} integer_branches_tree = {} for key, value in new_variable_list: #branches_tree[key] = array('d', [-999]) branches_tree[key] = array('f', [-999]) if 'hadTop_BDT' in key: keyname = 'hadTop_BDT' data_ttH_tree.SetBranchAddress(str(keyname), branches_tree[key]) data_ttV_tree.SetBranchAddress(str(keyname), branches_tree[key]) data_ttJets_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif 'Hj1_BDT' in key: keyname = 'Hj1_BDT' data_ttH_tree.SetBranchAddress(str(keyname), branches_tree[key]) data_ttV_tree.SetBranchAddress(str(keyname), branches_tree[key]) data_ttJets_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif ('n_fakeablesel_mu' in key) or ('n_fakeablesel_ele' in key) or ('Jet_numLoose' in key): integer_branches_tree[key] = array('I', [9999]) data_ttH_tree.SetBranchAddress(str(key), integer_branches_tree[key]) data_ttH_tree.SetBranchAddress(str(key), integer_branches_tree[key]) data_ttJets_tree.SetBranchAddress(str(key), integer_branches_tree[key]) else: data_ttH_tree.SetBranchAddress(str(key), branches_tree[key]) data_ttV_tree.SetBranchAddress(str(key), branches_tree[key]) data_ttJets_tree.SetBranchAddress(str(key), branches_tree[key]) branches_reader = {} # Register names of inputs with reader. Together with the name give the address of the local variable that carries the updated input variables during event loop. for key, value in new_variable_list: print 'Add variable name %s: ' % key branches_reader[key] = array('f', [-999]) reader.AddVariable(str(key), branches_reader[key]) event_number = array('f', [-999]) reader.AddSpectator('nEvent', event_number) num_inputs = 0 for key, value in new_variable_list: num_inputs = num_inputs + 1 classifier_suffix = opt.input_suffix # Book methods # First argument is user defined name. Doesn not have to be same as training name. # True type of method and full configuration are read from the weights file specified in the second argument. mva_weights_dir = 'MultiClass_DNN_%sVars_%s/weights/Factory_MultiClass_DNN_%sVars_%s_DNN.weights.xml' % ( str(num_inputs), classifier_suffix, str(num_inputs), classifier_suffix) print 'using weights file: ', mva_weights_dir reader.BookMVA('DNN', TString(mva_weights_dir)) classifier_samples_dir = 'MultiClass_DNN_%sVars_%s/outputs' % ( str(num_inputs), classifier_suffix) classifier_plots_dir = 'MultiClass_DNN_%sVars_%s/plots' % ( str(num_inputs), classifier_suffix) if not os.path.exists(classifier_plots_dir): os.makedirs(classifier_plots_dir) if not os.path.exists(classifier_samples_dir): os.makedirs(classifier_samples_dir) # Define outputs: files to store histograms/ttree with results from application of classifiers and any histos/trees themselves. output_file_name = '%s/Applied_MultiClass_DNN_%sVars_%s.root' % ( classifier_samples_dir, str(num_inputs), classifier_suffix) output_file = TFile.Open(output_file_name, 'RECREATE') # Evaluate network and make plots of the response on each of the nodes to each of the simulated samples. #network_evaluation(data_ttH_tree, new_variable_list, 'ttH', branches_tree, branches_reader, reader, False) #network_evaluation(data_ttV_tree, new_variable_list, 'ttV', branches_tree, branches_reader, reader, False) #network_evaluation(data_ttJets_tree, new_variable_list, 'ttJets', branches_tree, branches_reader, reader, False) # Evaluate network and use max node response to categorise event. Only maximum node response will be plotted per event meaning each event will only contribute in the maximum nodes response histogram. network_evaluation(data_ttH_tree, new_variable_list, 'ttH', branches_tree, integer_branches_tree, branches_reader, reader, True) network_evaluation(data_ttV_tree, new_variable_list, 'ttV', branches_tree, integer_branches_tree, branches_reader, reader, True) network_evaluation(data_ttJets_tree, new_variable_list, 'ttJets', branches_tree, integer_branches_tree, branches_reader, reader, True) output_file.Close()
def main(): Muon1_cLP = array('f', [-1.0]) Muon1_cLM = array('f', [-1.0]) Muon1_staRelChi2 = array('f', [-1.0]) Muon1_trkRelChi2 = array('f', [-1.0]) Muon1_glbdEP = array('f', [-1.0]) Muon1_trkKink = array('f', [-1.0]) Muon1_glbKink = array('f', [-1.0]) Muon1_glbTrkP = array('f', [-1.0]) Muon1_nTVH = array('f', [-1.0]) Muon1_nVPH = array('f', [-1.0]) Muon1_vMHC = array('f', [-1.0]) Muon1_nMS = array('f', [-1.0]) Muon1_segComp = array('f', [-1.0]) Muon1_tIpOnOut = array('f', [-1.0]) Muon1_glbNChi2 = array('f', [-1.0]) Muon1_inner_nChi2 = array('f', [-1.0]) Muon1_outer_nChi2 = array('f', [-1.0]) Muon1_innner_VF = array('f', [-1.0]) mu1_eta = array('f', [-1.0]) mu1_pt = array('f', [-1.0]) mu1_phi = array('f', [-1.0]) mu1_SoftMVA = array('f', [-1.0]) Muon2_cLP = array('f', [-1.0]) Muon2_cLM = array('f', [-1.0]) Muon2_staRelChi2 = array('f', [-1.0]) Muon2_trkRelChi2 = array('f', [-1.0]) Muon2_glbdEP = array('f', [-1.0]) Muon2_trkKink = array('f', [-1.0]) Muon2_glbKink = array('f', [-1.0]) Muon2_glbTrkP = array('f', [-1.0]) Muon2_nTVH = array('f', [-1.0]) Muon2_nVPH = array('f', [-1.0]) Muon2_vMHC = array('f', [-1.0]) Muon2_nMS = array('f', [-1.0]) Muon2_segComp = array('f', [-1.0]) Muon2_tIpOnOut = array('f', [-1.0]) Muon2_glbNChi2 = array('f', [-1.0]) Muon2_inner_nChi2 = array('f', [-1.0]) Muon2_outer_nChi2 = array('f', [-1.0]) Muon2_innner_VF = array('f', [-1.0]) mu2_eta = array('f', [-1.0]) mu2_pt = array('f', [-1.0]) mu2_phi = array('f', [-1.0]) mu2_SoftMVA = array('f', [-1.0]) Muon3_cLP = array('f', [-1.0]) Muon3_cLM = array('f', [-1.0]) Muon3_staRelChi2 = array('f', [-1.0]) Muon3_trkRelChi2 = array('f', [-1.0]) Muon3_glbdEP = array('f', [-1.0]) Muon3_trkKink = array('f', [-1.0]) Muon3_glbKink = array('f', [-1.0]) Muon3_glbTrkP = array('f', [-1.0]) Muon3_nTVH = array('f', [-1.0]) Muon3_nVPH = array('f', [-1.0]) Muon3_vMHC = array('f', [-1.0]) Muon3_nMS = array('f', [-1.0]) Muon3_segComp = array('f', [-1.0]) Muon3_tIpOnOut = array('f', [-1.0]) Muon3_glbNChi2 = array('f', [-1.0]) Muon3_inner_nChi2 = array('f', [-1.0]) Muon3_outer_nChi2 = array('f', [-1.0]) Muon3_innner_VF = array('f', [-1.0]) mu3_eta = array('f', [-1.0]) mu3_pt = array('f', [-1.0]) mu3_phi = array('f', [-1.0]) mu3_SoftMVA = array('f', [-1.0]) var_dnnSegCompMuMin = array('f', [-1.0]) # Muon Id 1 reader_Muon1Id_barrel = TMVA.Reader("!Color:!Silent") reader_Muon1Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon1_cLM) reader_Muon1Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon1_cLP) reader_Muon1Id_barrel.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon1_staRelChi2) reader_Muon1Id_barrel.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon1_trkRelChi2) reader_Muon1Id_barrel.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon1_glbdEP) reader_Muon1Id_barrel.AddVariable("log(mu_combinedQuality_trkKink)", Muon1_trkKink) reader_Muon1Id_barrel.AddVariable("log(mu_combinedQuality_glbKink)", Muon1_glbKink) reader_Muon1Id_barrel.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon1_glbTrkP) reader_Muon1Id_barrel.AddVariable("mu_Numberofvalidpixelhits", Muon1_nVPH) reader_Muon1Id_barrel.AddVariable("mu_trackerLayersWithMeasurement", Muon1_nTVH) reader_Muon1Id_barrel.AddVariable("mu_validMuonHitComb", Muon1_vMHC) reader_Muon1Id_barrel.AddVariable("mu_numberOfMatchedStations", Muon1_nMS) reader_Muon1Id_barrel.AddVariable("mu_segmentCompatibility", Muon1_segComp) reader_Muon1Id_barrel.AddVariable("mu_timeAtIpInOutErr", Muon1_tIpOnOut) reader_Muon1Id_barrel.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon1_glbNChi2) reader_Muon1Id_barrel.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon1_inner_nChi2) reader_Muon1Id_barrel.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon1_outer_nChi2) reader_Muon1Id_barrel.AddVariable("mu_innerTrack_validFraction", Muon1_innner_VF) reader_Muon1Id_barrel.AddSpectator("mu_eta", mu1_eta) reader_Muon1Id_barrel.AddSpectator("mu_pt", mu1_pt) reader_Muon1Id_barrel.AddSpectator("mu_phi", mu1_phi) reader_Muon1Id_barrel.AddSpectator("mu_SoftMVA", mu1_SoftMVA) reader_Muon1Id_barrel.BookMVA( "BDT", basedir + "weights/weights_barrel/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles reader_Muon1Id_endcap = TMVA.Reader("!Color:!Silent") reader_Muon1Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon1_cLM) reader_Muon1Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon1_cLP) reader_Muon1Id_endcap.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon1_staRelChi2) reader_Muon1Id_endcap.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon1_trkRelChi2) reader_Muon1Id_endcap.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon1_glbdEP) reader_Muon1Id_endcap.AddVariable("log(mu_combinedQuality_trkKink)", Muon1_trkKink) reader_Muon1Id_endcap.AddVariable("log(mu_combinedQuality_glbKink)", Muon1_glbKink) reader_Muon1Id_endcap.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon1_glbTrkP) reader_Muon1Id_endcap.AddVariable("mu_Numberofvalidpixelhits", Muon1_nVPH) reader_Muon1Id_endcap.AddVariable("mu_trackerLayersWithMeasurement", Muon1_nTVH) reader_Muon1Id_endcap.AddVariable("mu_validMuonHitComb", Muon1_vMHC) reader_Muon1Id_endcap.AddVariable("mu_numberOfMatchedStations", Muon1_nMS) reader_Muon1Id_endcap.AddVariable("mu_segmentCompatibility", Muon1_segComp) reader_Muon1Id_endcap.AddVariable("mu_timeAtIpInOutErr", Muon1_tIpOnOut) reader_Muon1Id_endcap.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon1_glbNChi2) reader_Muon1Id_endcap.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon1_inner_nChi2) reader_Muon1Id_endcap.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon1_outer_nChi2) reader_Muon1Id_endcap.AddVariable("mu_innerTrack_validFraction", Muon1_innner_VF) reader_Muon1Id_endcap.AddSpectator("mu_eta", mu1_eta) reader_Muon1Id_endcap.AddSpectator("mu_pt", mu1_pt) reader_Muon1Id_endcap.AddSpectator("mu_phi", mu1_phi) reader_Muon1Id_endcap.AddSpectator("mu_SoftMVA", mu1_SoftMVA) reader_Muon1Id_endcap.BookMVA( "BDT", basedir + "weights/weights_endcap/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles # (MuonId 2) reader_Muon2Id_barrel = TMVA.Reader("!Color:!Silent") reader_Muon2Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon2_cLM) reader_Muon2Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon2_cLP) reader_Muon2Id_barrel.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon2_staRelChi2) reader_Muon2Id_barrel.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon2_trkRelChi2) reader_Muon2Id_barrel.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon2_glbdEP) reader_Muon2Id_barrel.AddVariable("log(mu_combinedQuality_trkKink)", Muon2_trkKink) reader_Muon2Id_barrel.AddVariable("log(mu_combinedQuality_glbKink)", Muon2_glbKink) reader_Muon2Id_barrel.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon2_glbTrkP) reader_Muon2Id_barrel.AddVariable("mu_Numberofvalidpixelhits", Muon2_nVPH) reader_Muon2Id_barrel.AddVariable("mu_trackerLayersWithMeasurement", Muon2_nTVH) reader_Muon2Id_barrel.AddVariable("mu_validMuonHitComb", Muon2_vMHC) reader_Muon2Id_barrel.AddVariable("mu_numberOfMatchedStations", Muon2_nMS) reader_Muon2Id_barrel.AddVariable("mu_segmentCompatibility", Muon2_segComp) reader_Muon2Id_barrel.AddVariable("mu_timeAtIpInOutErr", Muon2_tIpOnOut) reader_Muon2Id_barrel.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon2_glbNChi2) reader_Muon2Id_barrel.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon2_inner_nChi2) reader_Muon2Id_barrel.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon2_outer_nChi2) reader_Muon2Id_barrel.AddVariable("mu_innerTrack_validFraction", Muon2_innner_VF) reader_Muon2Id_barrel.AddSpectator("mu_eta", mu2_eta) reader_Muon2Id_barrel.AddSpectator("mu_pt", mu2_pt) reader_Muon2Id_barrel.AddSpectator("mu_phi", mu2_phi) reader_Muon2Id_barrel.AddSpectator("mu_SoftMVA", mu2_SoftMVA) reader_Muon2Id_barrel.BookMVA( "BDT", basedir + "/weights/weights_barrel/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles reader_Muon2Id_endcap = TMVA.Reader("!Color:!Silent") reader_Muon2Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon2_cLM) reader_Muon2Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon2_cLP) reader_Muon2Id_endcap.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon2_staRelChi2) reader_Muon2Id_endcap.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon2_trkRelChi2) reader_Muon2Id_endcap.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon2_glbdEP) reader_Muon2Id_endcap.AddVariable("log(mu_combinedQuality_trkKink)", Muon2_trkKink) reader_Muon2Id_endcap.AddVariable("log(mu_combinedQuality_glbKink)", Muon2_glbKink) reader_Muon2Id_endcap.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon2_glbTrkP) reader_Muon2Id_endcap.AddVariable("mu_Numberofvalidpixelhits", Muon2_nVPH) reader_Muon2Id_endcap.AddVariable("mu_trackerLayersWithMeasurement", Muon2_nTVH) reader_Muon2Id_endcap.AddVariable("mu_validMuonHitComb", Muon2_vMHC) reader_Muon2Id_endcap.AddVariable("mu_numberOfMatchedStations", Muon2_nMS) reader_Muon2Id_endcap.AddVariable("mu_segmentCompatibility", Muon2_segComp) reader_Muon2Id_endcap.AddVariable("mu_timeAtIpInOutErr", Muon2_tIpOnOut) reader_Muon2Id_endcap.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon2_glbNChi2) reader_Muon2Id_endcap.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon2_inner_nChi2) reader_Muon2Id_endcap.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon2_outer_nChi2) reader_Muon2Id_endcap.AddVariable("mu_innerTrack_validFraction", Muon2_innner_VF) reader_Muon2Id_endcap.AddSpectator("mu_eta", mu2_eta) reader_Muon2Id_endcap.AddSpectator("mu_pt", mu2_pt) reader_Muon2Id_endcap.AddSpectator("mu_phi", mu2_phi) reader_Muon2Id_endcap.AddSpectator("mu_SoftMVA", mu2_SoftMVA) reader_Muon2Id_endcap.BookMVA( "BDT", basedir + "/weights/weights_endcap/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles # (MuonId 3) reader_Muon3Id_barrel = TMVA.Reader("!Color:!Silent") reader_Muon3Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon3_cLM) reader_Muon3Id_barrel.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon3_cLP) reader_Muon3Id_barrel.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon3_staRelChi2) reader_Muon3Id_barrel.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon3_trkRelChi2) reader_Muon3Id_barrel.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon3_glbdEP) reader_Muon3Id_barrel.AddVariable("log(mu_combinedQuality_trkKink)", Muon3_trkKink) reader_Muon3Id_barrel.AddVariable("log(mu_combinedQuality_glbKink)", Muon3_glbKink) reader_Muon3Id_barrel.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon3_glbTrkP) reader_Muon3Id_barrel.AddVariable("mu_Numberofvalidpixelhits", Muon3_nVPH) reader_Muon3Id_barrel.AddVariable("mu_trackerLayersWithMeasurement", Muon3_nTVH) reader_Muon3Id_barrel.AddVariable("mu_validMuonHitComb", Muon3_vMHC) reader_Muon3Id_barrel.AddVariable("mu_numberOfMatchedStations", Muon3_nMS) reader_Muon3Id_barrel.AddVariable("mu_segmentCompatibility", Muon3_segComp) reader_Muon3Id_barrel.AddVariable("mu_timeAtIpInOutErr", Muon3_tIpOnOut) reader_Muon3Id_barrel.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon3_glbNChi2) reader_Muon3Id_barrel.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon3_inner_nChi2) reader_Muon3Id_barrel.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon3_outer_nChi2) reader_Muon3Id_barrel.AddVariable("mu_innerTrack_validFraction", Muon3_innner_VF) reader_Muon3Id_barrel.AddSpectator("mu_eta", mu2_eta) reader_Muon3Id_barrel.AddSpectator("mu_pt", mu2_pt) reader_Muon3Id_barrel.AddSpectator("mu_phi", mu2_phi) reader_Muon3Id_barrel.AddSpectator("mu_SoftMVA", mu2_SoftMVA) reader_Muon3Id_barrel.BookMVA( "BDT", basedir + "/weights/weights_barrel/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles reader_Muon3Id_endcap = TMVA.Reader("!Color:!Silent") reader_Muon3Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalMomentum>250?250:mu_combinedQuality_chi2LocalMomentum", Muon3_cLM) reader_Muon3Id_endcap.AddVariable( "mu_combinedQuality_chi2LocalPosition>50?50:mu_combinedQuality_chi2LocalPosition", Muon3_cLP) reader_Muon3Id_endcap.AddVariable( "mu_combinedQuality_staRelChi2>50?50:mu_combinedQuality_staRelChi2", Muon3_staRelChi2) reader_Muon3Id_endcap.AddVariable( "mu_combinedQuality_trkRelChi2>50?50:mu_combinedQuality_trkRelChi2", Muon3_trkRelChi2) reader_Muon3Id_endcap.AddVariable("mu_combinedQuality_globalDeltaEtaPhi", Muon3_glbdEP) reader_Muon3Id_endcap.AddVariable("log(mu_combinedQuality_trkKink)", Muon3_trkKink) reader_Muon3Id_endcap.AddVariable("log(mu_combinedQuality_glbKink)", Muon3_glbKink) reader_Muon3Id_endcap.AddVariable( "mu_combinedQuality_glbTrackProbability>150?150:mu_combinedQuality_glbTrackProbability", Muon3_glbTrkP) reader_Muon3Id_endcap.AddVariable("mu_Numberofvalidpixelhits", Muon3_nVPH) reader_Muon3Id_endcap.AddVariable("mu_trackerLayersWithMeasurement", Muon3_nTVH) reader_Muon3Id_endcap.AddVariable("mu_validMuonHitComb", Muon3_vMHC) reader_Muon3Id_endcap.AddVariable("mu_numberOfMatchedStations", Muon3_nMS) reader_Muon3Id_endcap.AddVariable("mu_segmentCompatibility", Muon3_segComp) reader_Muon3Id_endcap.AddVariable("mu_timeAtIpInOutErr", Muon3_tIpOnOut) reader_Muon3Id_endcap.AddVariable("mu_GLnormChi2>50?50:mu_GLnormChi2", Muon3_glbNChi2) reader_Muon3Id_endcap.AddVariable( "mu_innerTrack_normalizedChi2>50?50:mu_innerTrack_normalizedChi2", Muon3_inner_nChi2) reader_Muon3Id_endcap.AddVariable( "mu_outerTrack_normalizedChi2>80?80:mu_outerTrack_normalizedChi2", Muon3_outer_nChi2) reader_Muon3Id_endcap.AddVariable("mu_innerTrack_validFraction", Muon3_innner_VF) reader_Muon3Id_endcap.AddSpectator("mu_eta", mu3_eta) reader_Muon3Id_endcap.AddSpectator("mu_pt", mu3_pt) reader_Muon3Id_endcap.AddSpectator("mu_phi", mu3_phi) reader_Muon3Id_endcap.AddSpectator("mu_SoftMVA", mu3_SoftMVA) reader_Muon3Id_endcap.BookMVA( "BDT", basedir + "/weights/weights_endcap/TMVA_new_BDT.weights.xml") # weights weights.xml file after training, place it to CommonFiles file_ = ROOT.TFile( "/eos/user/b/bjoshi/RunIITau23Mu/AnalysisTrees/T3MSelectionTreeInput_combined_with_keras_scores.root", "READ") old_tree_bkg = file_.Get('TreeB') old_tree_ds = file_.Get('TreeS_Ds') old_tree_bu = file_.Get('TreeS_Bu') old_tree_bd = file_.Get('TreeS_Bd') new_file = ROOT.TFile( "/eos/user/b/bjoshi/RunIITau23Mu/AnalysisTrees/T3MSelectionTreeInput_combined_globalMuonId.root", "RECREATE") new_tree_bkg = old_tree_bkg.CloneTree(0) new_tree_ds = old_tree_ds.CloneTree(0) new_tree_bu = old_tree_bu.CloneTree(0) new_tree_bd = old_tree_bd.CloneTree(0) old_tree_list = [old_tree_bkg, old_tree_ds, old_tree_bu, old_tree_bd] new_tree_list = [new_tree_bkg, new_tree_ds, new_tree_bu, new_tree_bd] globalMuon1Id = [ array('f', [-99.]), array('f', [-99.]), array('f', [-99.]), array('f', [-99.]) ] globalMuon2Id = [ array('f', [-99.]), array('f', [-99.]), array('f', [-99.]), array('f', [-99.]) ] globalMuon3Id = [ array('f', [-99.]), array('f', [-99.]), array('f', [-99.]), array('f', [-99.]) ] var_dnnSegCompMuMin = [ array('f', [-99.]), array('f', [-99.]), array('f', [-99.]), array('f', [-99.]) ] for i in xrange(4): new_tree_list[i].Branch("globalMuon1Id", globalMuon1Id[i], "globalMuon1Id/F") new_tree_list[i].Branch("globalMuon2Id", globalMuon2Id[i], "globalMuon2Id/F") new_tree_list[i].Branch("globalMuon3Id", globalMuon3Id[i], "globalMuon3Id/F") new_tree_list[i].Branch("var_dnnSegCompMuMin", var_dnnSegCompMuMin[i], "var_dnnSegCompMuMin/F") for i, tree in enumerate(old_tree_list): nentries = tree.GetEntriesFast() print tree.GetName() for j in xrange(nentries): if (j % 1000 == 0): print "Processing ", j, "/", nentries, " ..." tree.GetEntry(j) muon1_score = -99.0 muon2_score = -99.0 muon3_score = -99.0 Muon1_cLP[0] = tree.Muon1_combinedQuality_chi2LocalPosition Muon1_cLM[0] = tree.Muon1_combinedQuality_chi2LocalMomentum Muon1_staRelChi2[0] = tree.Muon1_combinedQuality_staRelChi2 Muon1_trkRelChi2[0] = tree.Muon1_combinedQuality_trkRelChi2 Muon1_glbdEP[0] = tree.Muon1_combinedQuality_globalDeltaEtaPhi Muon1_trkKink[0] = np.log(0.01 + tree.Muon1_combinedQuality_trkKink) Muon1_glbKink[0] = np.log(0.01 + tree.Muon1_combinedQuality_glbKink) Muon1_glbTrkP[0] = tree.Muon1_combinedQuality_glbTrackProbability Muon1_nTVH[0] = tree.Muon1_trackerLayersWithMeasurement Muon1_nVPH[0] = tree.Muon1_numberofValidPixelHits Muon1_vMHC[0] = tree.Muon1_vmuonhitcomb_reco Muon1_nMS[0] = tree.Muon1_numberOfMatchedStations Muon1_segComp[0] = tree.Muon1_segmentCompatibility Muon1_tIpOnOut[0] = tree.var_Muon1_timeAtIpInOutErr Muon1_glbNChi2[0] = tree.Muon1_normChi2 Muon1_inner_nChi2[0] = tree.Muon1_innerTrack_normalizedChi2 Muon1_outer_nChi2[0] = tree.Muon1_outerTrack_normalizedChi2 Muon1_innner_VF[0] = tree.Muon1_innerTrack_validFraction Muon2_cLM[0] = tree.Muon2_combinedQuality_chi2LocalMomentum Muon2_cLP[0] = tree.Muon2_combinedQuality_chi2LocalPosition Muon2_staRelChi2[0] = tree.Muon2_combinedQuality_staRelChi2 Muon2_trkRelChi2[0] = tree.Muon2_combinedQuality_trkRelChi2 Muon2_glbdEP[0] = tree.Muon2_combinedQuality_globalDeltaEtaPhi Muon2_trkKink[0] = np.log(0.01 + tree.Muon2_combinedQuality_trkKink) Muon2_glbKink[0] = np.log(0.01 + tree.Muon2_combinedQuality_glbKink) Muon2_glbTrkP[0] = tree.Muon2_combinedQuality_glbTrackProbability Muon2_nTVH[0] = tree.Muon2_trackerLayersWithMeasurement Muon2_nVPH[0] = tree.Muon2_numberofValidPixelHits Muon2_vMHC[0] = tree.Muon2_vmuonhitcomb_reco Muon2_nMS[0] = tree.Muon2_numberOfMatchedStations Muon2_segComp[0] = tree.Muon2_segmentCompatibility Muon2_tIpOnOut[0] = tree.var_Muon2_timeAtIpInOutErr Muon2_glbNChi2[0] = tree.Muon2_normChi2 Muon2_inner_nChi2[0] = tree.Muon2_innerTrack_normalizedChi2 Muon2_outer_nChi2[0] = tree.Muon2_outerTrack_normalizedChi2 Muon2_innner_VF[0] = tree.Muon2_innerTrack_validFraction Muon3_cLM[0] = tree.Muon3_combinedQuality_chi2LocalMomentum Muon3_cLP[0] = tree.Muon3_combinedQuality_chi2LocalPosition Muon3_staRelChi2[0] = tree.Muon3_combinedQuality_staRelChi2 Muon3_trkRelChi2[0] = tree.Muon3_combinedQuality_trkRelChi2 Muon3_glbdEP[0] = tree.Muon3_combinedQuality_globalDeltaEtaPhi Muon3_trkKink[0] = np.log(0.01 + tree.Muon3_combinedQuality_trkKink) Muon3_glbKink[0] = np.log(0.01 + tree.Muon3_combinedQuality_glbKink) Muon3_glbTrkP[0] = tree.Muon3_combinedQuality_glbTrackProbability Muon3_nTVH[0] = tree.Muon3_trackerLayersWithMeasurement Muon3_nVPH[0] = tree.Muon3_numberofValidPixelHits Muon3_vMHC[0] = tree.Muon3_vmuonhitcomb_reco Muon3_nMS[0] = tree.Muon3_numberOfMatchedStations Muon3_segComp[0] = tree.Muon3_segmentCompatibility Muon3_tIpOnOut[0] = tree.var_Muon3_timeAtIpInOutErr Muon3_glbNChi2[0] = tree.Muon3_normChi2 Muon3_inner_nChi2[0] = tree.Muon3_innerTrack_normalizedChi2 Muon3_outer_nChi2[0] = tree.Muon3_outerTrack_normalizedChi2 Muon3_innner_VF[0] = tree.Muon3_innerTrack_validFraction if (abs(tree.var_Muon1_Eta) < 1.2): muon1_score = reader_Muon1Id_barrel.EvaluateMVA("BDT") else: muon1_score = reader_Muon1Id_endcap.EvaluateMVA("BDT") if (abs(tree.var_Muon2_Eta) < 1.2): muon2_score = reader_Muon2Id_barrel.EvaluateMVA("BDT") else: muon2_score = reader_Muon2Id_endcap.EvaluateMVA("BDT") if (abs(tree.var_Muon3_Eta) < 1.2): muon3_score = reader_Muon3Id_barrel.EvaluateMVA("BDT") else: muon3_score = reader_Muon3Id_endcap.EvaluateMVA("BDT") globalMuon1Id[i][0] = muon1_score globalMuon2Id[i][0] = muon2_score if (tree.threeGlobal == 1): globalMuon3Id[i][0] = muon3_score else: globalMuon3Id[i][0] = -99.0 var_dnnSegCompMuMin[i][0] = min(tree.muon1_seg_comp_dnn, tree.muon2_seg_comp_dnn, tree.muon3_seg_comp_dnn) new_tree_list[i].Fill() new_file.cd() new_tree_bkg.Write() new_tree_ds.Write() new_tree_bu.Write() new_tree_bd.Write() new_file.Close() file_.Close()
def FillBDTscore(self): phVals = ["EE", "EB"] path = "/home/jcordero/CMS/JYCMCMS/SMP_ZG/python/Corrections/" file = {} for ph in phVals: file[ph] = "TMVAnalysis_BDT_" + ph + ".weights.xml" reader[ph] = TMVA.Reader() ########################################### ## ## Adding the variables to the MVA object ## & ## Fill in the variables from the Data ## phVar = {} varName = { "recoPhi": ["photonOnePhi"], "r9": ["photonOneR9"], "sieieFull5x5": ["photonOneSieie"], "sieipFull5x5": ["photonOneSieip"], "e2x2Full5x5/e5x5Full5x5": ["photonOneE2x2", "photonOneE5x5"], "recoSCEta": ["photonOneEta"], "rawE": ["photonOneScRawE"], "scEtaWidth": ["photonOneScEtaWidth"], "scPhiWidth": ["photonOneScPhiWidth"], "esEn/rawE": ["photonOnePreShowerE", "photonOneScRawE"], "esRR": ["photonOneSrr"], "rho": ["Rho"], } var = {} for ph in ["EE", "EB"]: var[ph] = {} for vn in varName.keys(): var[ph][vn] = array.array('f', [0]) reader[ph].AddVariable(vn, var[ph][vn]) if ph == "EE": # this is just to fill in once if len(varName[vn]) > 1: phVar[vn] = ZG.GetWithCuts( varName[vn][0]) / ZG.GetWithCuts(varName[vn][1]) else: phVar[vn] = ZG.GetWithCuts(varName[vn][0]) reader[ph].BookMVA("BDT", path + file[ph]) ####################### ## ## Fill the BDT ## ShowerShapeBDT = [] for i in range(len(self.GetWithCuts('cuts'))): if np.abs(phVar["recoSCEta"][i]) > 1.48: ph = "EE" else: ph = "EB" for vn in varName: var[ph][vn] = phVar[vn] ShowerShapeBDT.append(reader[ph].EvaluateMVA("BDT")) ####################################### ## ## Add the BDT score to the DataFrame ## sefl.df["ShowerShapeBDT"] = ShowerShapeBDT
def main(): try: # Retrive command line options shortopts = "m:i:o:d:vh?" longopts = [ "methods=", "inputfile=", "outputfile=", "datatype=", "verbose", "help", "usage" ] opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts) except getopt.GetoptError: # Print help information and exit: print "ERROR: unknown options in argument %s" % sys.argv[1:] usage() sys.exit(1) treeNameSig = DEFAULT_TREESIG treeNameBkg = DEFAULT_TREEBKG methods = DEFAULT_METHODS directory = DEFAULT_DATA verbose = False for o, a in opts: if o in ("-?", "-h", "--help", "--usage"): usage() sys.exit(0) elif o in ("-m", "--methods"): methods = a elif o in ("-d", "--datatype"): directory = a elif o in ("-v", "--verbose"): verbose = True # Print methods #take leading and trailing white space out methods = methods.strip(" ") mlist = methods.replace(' ', ',').split(',') print "=== TMVApplication: use method(s)..." for m in mlist: if m.strip() != '': print "=== - <%s>" % m.strip() # Import ROOT classes from ROOT import gSystem, gROOT, gApplication, TFile, TTree, TCut, TH1F, TStopwatch print("ROOT classes successfully imported!\n") # DCS 17/06/2016 # check ROOT version, give alarm if 5.18 if gROOT.GetVersionCode() >= 332288 and gROOT.GetVersionCode() < 332544: print "*** You are running ROOT version 5.18, which has problems in PyROOT such that TMVA" print "*** does not run properly (function calls with enums in the argument are ignored)." print "*** Solution: either use CINT or a C++ compiled version (see TMVA/macros or TMVA/examples)," print "*** or use another ROOT version (e.g., ROOT 5.19)." sys.exit(1) # Logon not automatically loaded through PyROOT (logon loads TMVA library) load also GUI tmvadir = "/home/dean/software/tmva/TMVA-v4.2.0/test" macro = os.path.join(tmvadir, "TMVAlogon.C") loadmacro = os.path.join(tmvadir, "TMVAGui.C") gROOT.SetMacroPath(tmvadir) gROOT.Macro(macro) gROOT.LoadMacro(loadmacro) print("ROOT macro path loaded correctly!\n") # Import TMVA classes from ROOT from ROOT import TMVA # Create the Reader object reader = TMVA.Reader("!Color") var1 = array('f', [0]) var2 = array('f', [0]) var3 = array('f', [0]) var4 = array('f', [0]) var5 = array('f', [0]) var6 = array('f', [0]) var7 = array('f', [0]) var8 = array('f', [0]) var9 = array('f', [0]) var10 = array('f', [0]) variables = [var1, var2, var3, var4, var5, var6, var7, var8, var9, var10] var_names = [ 'peaks', 'mean_peaks', 'integral', 'integral_over_peaks', 'max', 'mean', 'max_over_mean', 'std_dev_peaks', 'entropy', 'ps_integral' ] #variables = [var1, var2, var3, var4] #var_names = ['var1', 'var2', 'var3', 'var4'] for name, var in zip(var_names, variables): reader.AddVariable(name, var) print("Variables successfully loaded!\n") #reader.AddVariable("Nclusters.value", var1) #reader.AddVariable("(TMath::Log10(eventinfo_ALLOfflinePulseSeriesReco.tot_charge))*1000/eventinfo_ALLOfflinePulseSeriesReco.length" ,var2) #reader.AddVariable("MDCOGLaunches.value*1000./eventinfo_ALLOfflinePulseSeriesReco.length",var3) #reader.AddVariable("Nclusters.value*1000./eventinfo_ALLOfflinePulseSeriesReco.length" ,var4) #reader.AddVariable("NSMT8TRIGGER.value/eventinfo_ALLOfflinePulseSeriesReco.nstrings",var5) #reader.AddVariable("MedianCluster.value",var6) # book the MVA methods #dir = "weights/" #prefix = "TMVAClassification_" # #for m in mlist: # print( m + " method", dir + prefix + m + ".weights.xml") # reader.BookMVA( m + " method", dir + prefix + m + ".weights.xml" ) weight_dir = "/home/dean/capstone/TMVA-classifier/weights/" weights = [f for f in os.listdir(weight_dir) if ".xml" in f] for i, f in enumerate(weights): reader.BookMVA("BDT_{}".format(i), os.path.join(weight_dir, f)) #only care about BDT # reader.BookMVA("BDT","weights/TMVAClassification_BDT.weights.xml") ####################################################################### # For an example how to apply your own plugin method, please see # TMVA/macros/TMVApplication.C ####################################################################### # Book output histograms nbin = 100 histList = [] for m in mlist: histList.append(TH1F(m, m, nbin, -3, 3)) for h in histList: h.Fill(reader.EvaluateMVA(h.GetName() + " method")) # Book example histogram for probability (the other methods would be done similarly) if "Fisher" in mlist: probHistFi = TH1F("PROBA_MVA_Fisher", "PROBA_MVA_Fisher", nbin, 0, 1) rarityHistFi = TH1F("RARITY_MVA_Fisher", "RARITY_MVA_Fisher", nbin, 0, 1) filelist = glob(directory + "/" + "Level4b*.hdf") print 30 * "#" print "the filelist, ", filelist print 30 * "--" for file in filelist: try: startfile = tables.openFile(file, "a") #DELETE BDTs if they exist for var in startfile.root._v_children.keys(): if re.match("BDT_", var): startfile.removeNode("/", var) startfile.removeNode("/__I3Index__", var) #NOW CLONE THE NODE for name in histList: startfile.copyNode("/__I3Index__/StdDCOGLaunches", "/__I3Index__", str(name.GetName())) startfile.copyNode("/StdDCOGLaunches", "/", str(name.GetName())) startfile.close() h5 = tables.openFile(file, 'r') mcog_over_t = numpy.divide(h5.root.MDCOGLaunches.cols.value[:],\ h5.root.eventinfo_ALLOfflinePulseSeriesReco.cols.length[:]/1000.) q_over_t = numpy.divide(numpy.log10(h5.root.eventinfo_ALLOfflinePulseSeriesReco.cols.tot_charge[:]),\ h5.root.eventinfo_ALLOfflinePulseSeriesReco.cols.length[:]/1000.) ncluster_over_t = numpy.divide(h5.root.Nclusters.cols.value[:],\ h5.root.eventinfo_ALLOfflinePulseSeriesReco.cols.length[:]/1000.) nsmt8_over_string = numpy.divide(h5.root.NSMT8TRIGGER.cols.value[:],\ h5.root.eventinfo_ALLOfflinePulseSeriesReco.cols.nstrings[:]) s1 = array('f', h5.root.Nclusters.cols.value[:]) s2 = array('f', q_over_t) s3 = array('f', mcog_over_t[:]) s4 = array('f', ncluster_over_t[:]) s5 = array('f', nsmt8_over_string[:]) s6 = array('f', h5.root.MedianCluster.cols.value[:]) h5.close() result = numpy.zeros((len(histList), len(s1)), numpy.dtype([('Classifier', numpy.double)])) for ievt in range(len(s1)): #if ievt%1000 == 0: # print "--- ... Processing event: %i" % ievt # Fill event in memory # Compute MVA input variables var1[0] = s1[ievt] var2[0] = s2[ievt] var3[0] = s3[ievt] var4[0] = s4[ievt] var5[0] = s5[ievt] var6[0] = s6[ievt] # Fill histograms with MVA outputs for j, h in enumerate(histList): h.Fill(reader.EvaluateMVA(h.GetName() + " method")) result[j][ievt]["Classifier"] = reader.EvaluateMVA( h.GetName() + " method") endfile = tables.openFile(file, 'a') for k, name in enumerate(histList): modifiedNode = endfile.getNode("/", str(name.GetName())) modifiedNode.cols.value[ievt] = result[k][ievt][ "Classifier"] endfile.close() print time.strftime('Elapsed time - %H:%M:%S', time.gmtime(time.clock())) #sanity check of the mva values writen in the hdf files #ifile=tables.openFile(file,'r') # if len(ifile.root.BDT_400_20.cols.BDT) != len(ifile.root.MPEFit.cols.Zenith): # ifile.close() # print "Something wrong with file: ", k, j+1 #exit() #os.system("rm "+"/data/icecube01/users/redlpete/IC59L2/TableIOL3/H5FilesIncludingScores/H5%0.2d%0.2d.hd5"%(k,j+1)) # ifile.close() except ImportError as exce: print "file does not exist", k, j + 1 print exce exit() ifile = tables.openFile("test.h5", mode='a') class Score(IsDescription): score = Float64Col() group = ifile.createGroup("/", 'Background', 'Scoreinfo') table = ifile.createTable(group, 'score', Score, "Example") particle = table.row for n in range(len(result)): particle['score'] = result[n] particle.append() print "--- End of event loop: %s" % sw.Print() target = TFile("TMVApp1.root", "RECREATE") for h in histList: h.Write() target.Close() print "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" print "==> TMVApplication is done!"
def ApplyAddCuts(InputFolder, OutputFolder, Campaign): ChannelList = ChannelList_MC16a if Campaign == "MC16d": ChannelList = ChannelList_MC16d if Campaign == "MC16e": ChannelList = ChannelList_MC16e TreeList = ["nominal"] if "SYSLJ" in InputFolder: TreeList = SystTreeList InputFiles = glob.glob(InputFolder + "/*root*") for InputFile in InputFiles: OutputFile = InputFile.replace(InputFolder, OutputFolder) if os.path.exists(OutputFile): continue fF = TFile(InputFile, "READ") fF_out = TFile(OutputFile, "RECREATE") # save sumWeights tree to new file fT_weight = fF.Get("sumWeights") fT_weight_out = fT_weight.CloneTree() fT_weight_out.Write() # save all bookkeeping histograms to new file for Channel in ChannelList: DIR = TDirectory() fF.GetObject(Channel, DIR) DIR.ReadAll() fF_out.mkdir(Channel) fF_out.cd(Channel) DIR_new = TDirectory() DIR.GetList().Write() DIR_new.Write() fF_out.cd() for TreeName in TreeList: print( "---------------------------------------------------------------------------> Running over tree = ", TreeName) if TreeName == "nominal_SYST": continue fT = fF.Get(TreeName) entries = fT.GetEntries() reader1 = TMVA.Reader() reader2 = TMVA.Reader() var_LL_1 = array('f', [0]) reader1.AddVariable("klfitter_logLikelihood[0]", var_LL_1) var_EvtProb_1 = array('f', [0]) reader1.AddVariable("klfitter_eventProbability[0]", var_EvtProb_1) var_WhadPt_1 = array('f', [0]) reader1.AddVariable("klf_orig_Whad_pt", var_WhadPt_1) var_WlepPt_1 = array('f', [0]) reader1.AddVariable("klf_orig_Wlep_pt", var_WlepPt_1) var_ThadPt_1 = array('f', [0]) reader1.AddVariable("klf_orig_tophad_pt", var_ThadPt_1) var_TlepPt_1 = array('f', [0]) reader1.AddVariable("klf_orig_toplep_pt", var_TlepPt_1) var_j_n_1 = array('f', [0]) reader1.AddVariable("tma_njets", var_j_n_1) var_met_1 = array('f', [0]) reader1.AddVariable("tma_met", var_met_1) var_TTbarPt_1 = array('f', [0]) reader1.AddVariable("klf_orig_ttbar_pt", var_TTbarPt_1) var_mwt_1 = array('f', [0]) reader1.AddVariable("tma_mtw", var_mwt_1) var_DRwjets_1 = array('f', [0]) reader1.AddVariable("klf_orig_dR_qq_W", var_DRwjets_1) var_DRbjets_1 = array('f', [0]) reader1.AddVariable("klf_orig_dR_bb", var_DRbjets_1) var_LL_2 = array('f', [0]) reader2.AddVariable("klfitter_logLikelihood[0]", var_LL_2) var_EvtProb_2 = array('f', [0]) reader2.AddVariable("klfitter_eventProbability[0]", var_EvtProb_2) var_WhadPt_2 = array('f', [0]) reader2.AddVariable("klf_orig_Whad_pt", var_WhadPt_2) var_WlepPt_2 = array('f', [0]) reader2.AddVariable("klf_orig_Wlep_pt", var_WlepPt_2) var_ThadPt_2 = array('f', [0]) reader2.AddVariable("klf_orig_tophad_pt", var_ThadPt_2) var_TlepPt_2 = array('f', [0]) reader2.AddVariable("klf_orig_toplep_pt", var_TlepPt_2) var_j_n_2 = array('f', [0]) reader2.AddVariable("tma_njets", var_j_n_2) var_met_2 = array('f', [0]) reader2.AddVariable("tma_met", var_met_2) var_TTbarPt_2 = array('f', [0]) reader2.AddVariable("klf_orig_ttbar_pt", var_TTbarPt_2) var_mwt_2 = array('f', [0]) reader2.AddVariable("tma_mtw", var_mwt_2) var_DRwjets_2 = array('f', [0]) reader2.AddVariable("klf_orig_dR_qq_W", var_DRwjets_2) var_DRbjets_2 = array('f', [0]) reader2.AddVariable("klf_orig_dR_bb", var_DRbjets_2) reader1.BookMVA( "BDT", "TrainingSteffenChristmas/weights_noWindowCuts/MassTraining_BDT.weights.xml" ) reader2.BookMVA( "BDT", "TrainingSteffenChristmas/weights_withWindowCuts/MassTraining_BDT.weights.xml" ) fT_out = fT.CloneTree(0) d_noWC = array('f', [0.]) d_withWC = array('f', [0.]) fT_out.Branch("bdtOutput_8TeVlike_noWC", d_noWC, 'bdtOutput_8TeVlike_noWC/F') fT_out.Branch("bdtOutput_8TeVlike_withWC", d_withWC, 'bdtOutput_8TeVlike_withWC/F') for i in range(0, entries): fT.GetEntry(i) var_LL_1[0] = fT.klfitter_logLikelihood[0] var_EvtProb_1[0] = fT.klfitter_eventProbability[0] var_WhadPt_1[0] = fT.klf_orig_Whad_pt var_WlepPt_1[0] = fT.klf_orig_Wlep_pt var_ThadPt_1[0] = fT.klf_orig_tophad_pt var_TlepPt_1[0] = fT.klf_orig_toplep_pt var_j_n_1[0] = fT.tma_njets var_met_1[0] = fT.tma_met var_TTbarPt_1[0] = fT.klf_orig_ttbar_pt var_mwt_1[0] = fT.tma_mtw var_DRwjets_1[0] = fT.klf_orig_dR_qq_W var_DRbjets_1[0] = fT.klf_orig_dR_bb var_LL_2[0] = fT.klfitter_logLikelihood[0] var_EvtProb_2[0] = fT.klfitter_eventProbability[0] var_WhadPt_2[0] = fT.klf_orig_Whad_pt var_WlepPt_2[0] = fT.klf_orig_Wlep_pt var_ThadPt_2[0] = fT.klf_orig_tophad_pt var_TlepPt_2[0] = fT.klf_orig_toplep_pt var_j_n_2[0] = fT.tma_njets var_met_2[0] = fT.tma_met var_TTbarPt_2[0] = fT.klf_orig_ttbar_pt var_mwt_2[0] = fT.tma_mtw var_DRwjets_2[0] = fT.klf_orig_dR_qq_W var_DRbjets_2[0] = fT.klf_orig_dR_bb d_noWC[0] = reader1.EvaluateMVA("BDT") d_withWC[0] = reader2.EvaluateMVA("BDT") #print d_noWC[0]," ",d_withWC[0] fT_out.Fill() fT_out.Write() fF_out.Close()