#Can handle more than one input ana file if needed, just add to list inputFiles = [argv[1] + "/" + x for x in listdir(argv[1])] vtxChain = TChain("VertexTree") LEEChain = TChain("LEE1e1pTree") angleChain = TChain("AngleAnalysis") shapeChain = TChain("ShapeAnalysis") gapChain = TChain("GapAnalysis") dqdsChain = TChain("dQdSAnalysis") matchChain = TChain("MatchAnalysis") eventChain = TChain("EventVertexTree") for infile in inputFiles: vtxChain.Add(infile) LEEChain.Add(infile) angleChain.Add(infile) shapeChain.Add(infile) gapChain.Add(infile) dqdsChain.Add(infile) matchChain.Add(infile) eventChain.Add(infile) vtxChain.AddFriend(LEEChain) vtxChain.AddFriend(angleChain) vtxChain.AddFriend(shapeChain) vtxChain.AddFriend(gapChain) vtxChain.AddFriend(dqdsChain) vtxChain.AddFriend(matchChain)
#!/usr/bin/env python from ROOT import TFile, TTree, TH1F, TH1D, TH1, TCanvas, TChain, TGraphAsymmErrors, TMath, TH2D, TLorentzVector, AddressOf, gROOT, TNamed import ROOT as ROOT import os import sys, optparse from array import array import math import numpy as numpy_ ROOT.gROOT.LoadMacro("Loader.h+") outfilename = "ntupleHistos.root" ntuple = TChain("outTree") ntuple.Add(sys.argv[1]) NEntries = ntuple.GetEntries() if len(sys.argv) > 2: #if sys.argv[2]=="test": NEntries = int(sys.argv[2]) print "WARNING: Running in TEST MODE" print 'NEntries = ' + str(NEntries) h_z_ratio = TH1F('h_z_ratio', 'h_z_ratio', 100, 0, 40) h_SubJet_csv = TH1F('h_SubJet_csv', 'h_SubJet_csv', 100, 0., 1.) # h_trackSipdSig_3 =TH1F('h_trackSipdSig_3', 'h_trackSipdSig_3', 20,-10.,10.) # h_trackSipdSig_2 =TH1F('h_trackSipdSig_2', 'h_trackSipdSig_2', 6,0.,6.) # h_trackSipdSig_1 =TH1F('h_trackSipdSig_1', 'h_trackSipdSig_1', 80,0.,800.) # h_trackSipdSig_0 =TH1F('h_trackSipdSig_0', 'h_trackSipdSig_0', 80,0.,400.) # h_trackSipdSig_1_0 =TH1F('h_trackSipdSig_1_0', 'h_trackSipdSig_1_0', 80,0.,400.)
} for dataType, dataFile, in dataTypes.iteritems(): for ch, hists in channel.iteritems(): filepath='' # Find appropriate file path if dataType == 'MC': filepath = basepath+dataFile[0] elif dataType == 'Data' and ch == 'E' : filepath = basepath+dataFile[0] elif dataType == 'Data' and ch == 'Mu' : filepath = basepath+dataFile[1] print dataType print ch print filepath treepath = 'TTbar_plus_X_analysis/'+ch+'PlusJets/Cutflow/Cutflow' # Find appropriate tree chain = TChain(treepath) chain.Add(filepath) nevent=0 for event in chain: nevent=nevent+1 if nevent==10000 : break tag = event.__getattr__("Cutflow") # Get list of cuts in bool form passLastCut=True for i, cut in enumerate(tag) : # Loop over list of cuts if dataType == 'Data' : hist = hists[0] # Choose between MC and Data Histograms elif dataType == 'MC' : hist = hists[1] if (ch == 'Mu' and i >= 6) : cutName = Cuts[i+1] # Skip Conversion Label
def GetReweightedYieldAndError(condor_dir, process, channel, srcCTau, dstCTau, limitType): global printLock if os.path.isfile('condor/' + condor_dir + '/' + process + '.root') and not '_1cm_' in process: return GetYieldAndError(condor_dir, process, channel) realProcessName = process.replace(dstCTau, srcCTau) if arguments.verbose: printLock.acquire() print 'Reweighting yield from ' + realProcessName + ' to ' + process + ' ...' printLock.release() chain = TChain(signal_channel_tree) chain.Add('condor/' + condor_dir + '/' + realProcessName + '/hist_*.root') lifetimeWeightName = 'eventvariable_lifetimeWeight_1000024_' + srcCTau + 'To' + dstCTau realInputFile = TFile('condor/' + condor_dir + '/' + realProcessName + '.root') nGenerated = realInputFile.Get( channel.replace('Plotter/Met Plots', 'CutFlowPlotter/eventCounter')).GetEntries() crossSectionWeight = 1.0 if process != data_dataset: crossSectionWeight *= lumi / nGenerated if arguments.limitType == "higgsino": crossSectionWeight *= float(signal_cross_sections_higgsino[ process.split('_')[1][:-3]]['value']) elif arguments.limitType == "wino": crossSectionWeight *= float( signal_cross_sections[process.split('_')[2][:-3]]['value']) totalWeight = 0.0 totalWeight2 = 0.0 for iEvent in range(chain.GetEntries()): chain.GetEntry(iEvent) if lifetimeWeightName == 'eventvariable_lifetimeWeight_1000024_10cmTo1cm': lifetimeWeight = GetMissingLifetimeWeight(chain) else: lifetimeWeight = getattr(chain, lifetimeWeightName) if chain.eventvariable_cTau_1000024_0 < 0: printLock.acquire() print 'WARNING: somehow event number ' + str( iEvent ) + ' in sample ' + process + ' has no charginos at all! Ignoring this event...' printLock.release() continue # or lifetimeWeight = 0 really thisWeight = crossSectionWeight * lifetimeWeight * chain.eventvariable_isrWeight * chain.eventvariable_grandOrWeight * chain.eventvariable_puScalingFactor if arguments.era.startswith('2017_'): thisWeight *= chain.eventvariable_L1ECALPrefiringWeight totalWeight += thisWeight totalWeight2 += thisWeight * thisWeight yieldAndError = { 'yield': totalWeight, 'rawYield': chain.GetEntries(), 'error': 1.0 + (math.sqrt(totalWeight2) / totalWeight) if totalWeight > 0.0 else 1.0, 'absError': math.sqrt(totalWeight2) if totalWeight2 > 0.0 else 1.1, 'weight': totalWeight / chain.GetEntries() if chain.GetEntries() > 0.0 else 0.0, 'acceptance': totalWeight / nGenerated / crossSectionWeight, 'acceptanceError': (math.sqrt(totalWeight2) if totalWeight2 > 0.0 else 1.1) / nGenerated / crossSectionWeight, } return yieldAndError
def applyThresholdCut(dsNum): """ ./plots2.py -thr [dsNum] Create "after cuts" histograms for each dataset (similar to genRawHists) Use a super fine binning and huge energy range for versatility. Don't distinguish between enriched and natural detectors in 2D plots. 1. sum histogram 'hSum' 2. cpd vs energy 'hCPDE' 3. cpd vs run 'hCPDrun' """ threshCutE = 0.9 # require the mu to be at least this high to keep the channel kpb = 0.01 eLo, eHi = 0., 15000. nBins = int((eHi-eLo)/kpb) chList = ds.GetGoodChanList(dsNum) chList = [ch for ch in chList if ch!=692 and ch!=1232] calDB = db.TinyDB('../calDB.json') pars = db.Query() hTot = TH1D("hTot","hTot",nBins,eLo,eHi) hEnr = TH1D("hEnr","hEnr",nBins,eLo,eHi) hNat = TH1D("hNat","hNat",nBins,eLo,eHi) fList, fMissing, fNoThresh, fCut = [], [], [], [] dsPath = "/global/homes/w/wisecg/project/cuts/fs_rn_wf" runLo, runHi = 0, 0 for bkgIdx in range(ds.dsMap[dsNum]+1): if bkgIdx % 10 == 0 and bkgIdx > 0: print 100. * bkgIdx / float(ds.dsMap[dsNum]+1), "% done." threshKey = "thresh_ds%d_bkgidx%d" % (dsNum, bkgIdx) recList = calDB.search(pars.key == threshKey) if len(recList)!=1: print "Error: found too many records for key:",threshKey for record in recList: print record return threshDict = recList[0]['vals'] for chan in chList: # save threshold information chKey = u'%d' % chan if chKey not in threshDict.keys(): fNoThresh.append([dsNum,bkgIdx,chan]) continue mu, sig = threshDict[u'%d' % chan][0], threshDict[u'%d' % chan][1] # save file paths fName = "%s/fs_rn_wf-DS%d-%d-ch%d.root" % (dsPath, dsNum, bkgIdx, chan) if os.path.isfile(fName) == True: if 0. < mu < threshCutE: fList.append(fName) # print "ch %d bkgIdx %d mu %.2f sig %.2f" % (chan,bkgIdx,mu,sig) else: fCut.append(fName) # print "ch %d bkgIdx %d mu %.2f sig %.2f" % (chan,bkgIdx,mu,sig) else: fMissing.append(fName) continue # calculate efficiency curve thisErf = TF1("thisErf","0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1]) ))") thisErf.SetParameter(0,mu) thisErf.SetParameter(1,abs(sig)) fTmp = TFile(fName) tTmp = fTmp.Get("skimTree") tTmp.GetEntry(0) if runLo==0: runLo = tTmp.run tTmp.GetEntry(tTmp.GetEntries()-1) if tTmp.run > runHi: runHi = tTmp.run cTmp = fTmp.Get("chanCut").GetTitle() hTmp = wl.H1D(tTmp,nBins,eLo,eHi,"trapENFCal",cTmp,"hTmp","hTmp") hTmp.Divide(thisErf) hTot.Add(hTmp) hTmpEnr = wl.H1D(tTmp,nBins,eLo,eHi,"trapENFCal",cTmp+" && isEnr && trapENFCal > %.1f" % mu,"hTmpEnr","hTmpEnr") hTmpEnr.Divide(thisErf) hEnr.Add(hTmpEnr) hTmpNat = wl.H1D(tTmp,nBins,eLo,eHi,"trapENFCal",cTmp+" && !isEnr && trapENFCal > %.1f" % mu,"hTmpNat","hTmpNat") hTmpNat.Divide(thisErf) hNat.Add(hTmpNat) print "DS-%d nFound: %d nMissing: %d nCut: %d nNoThresh: %d" % (dsNum,len(fList),len(fMissing),len(fCut),len(fNoThresh)) # save to a permanent place fOut = TFile("../data/latThresh_DS%d.root" % dsNum, "RECREATE") hTot.Write() hEnr.Write() hNat.Write() # load the full chain w/ files passing threshold cut (but no efficiency correction applied.) chainFSRNWF = TChain("skimTree") [chainFSRNWF.Add(f) for f in fList] hFSRNWF = wl.H1D(chainFSRNWF,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hFSRNWF") cpdLo, cpdHi = 111, 175 if dsNum==4: cpdLo, cpdHi = 211, 275 if dsNum==5: cpdLo, cpdHi = 111, 275 nCPD = cpdHi - cpdLo hCPDE = wl.H2D(chainFSRNWF,nBins,eLo,eHi,nCPD,cpdLo,cpdHi,"C*100+P*10+D:trapENFCal","","trapENFCal","CPD","CPD vs. E","hCPDE") hCPDE.Write() chainFSRNWF.GetEntry(0) runLo = chainFSRNWF.run chainFSRNWF.GetEntry(chainFSRNWF.GetEntries()-1) runHi = chainFSRNWF.run nRuns = runHi - runLo + 1 print "First run:",runLo,"Last run:",runHi nRuns = runHi - runLo + 1 hCPDrun = wl.H2D(chainFSRNWF,nRuns,runLo,runHi,nCPD,cpdLo,cpdHi,"C*100+P*10+D:run","","run","CPD","CPD vs. run","hCPDrun") hCPDrun.Write() kpb = 0.1 rebinFactor = int(kpb/0.01) hTot = hTot.Rebin(rebinFactor) # this creates a new histogram hTot.GetXaxis().SetRangeUser(0.,12.) c = TCanvas("c","Shan is pretty",800,600) c.SetLogy(1) hTot.SetMinimum(0.5) hTot.SetLineColor(ROOT.kBlue) hTot.SetTitle("") hTot.GetXaxis().SetTitle("trapENFCal") hTot.GetYaxis().SetTitle("Counts") hTot.Draw("hist") hFSRNWF = hFSRNWF.Rebin(rebinFactor) hFSRNWF.GetXaxis().SetRangeUser(0.,12.) hFSRNWF.SetLineColor(ROOT.kRed) hFSRNWF.Draw("hist same") leg = TLegend(0.7,0.75,0.89,0.89) leg.AddEntry(hFSRNWF,"no thresh corr.","l") leg.AddEntry(hTot,"w/ thresh","l") leg.Draw("same") c.Print("../plots/latThresh_DS%d.pdf" % dsNum) fOut.Close()
from ROOT import TH1F, TH2F, TFile, TLorentzVector, TChain # load FWLite C++ libraries ROOT.gSystem.Load("libFWCoreFWLite.so") ROOT.gSystem.Load("libDataFormatsFWLite.so") ROOT.FWLiteEnabler.enable() # load FWlite python libraries from DataFormats.FWLite import Handle, Events data_chain = TChain('events') data_list = open(args.inFile, 'r') data_files = data_list.readlines() for i in data_files: data_chain.Add(i.strip()) outfile = TFile(args.outFile, "recreate") # Book histos hlt_jet_pt = TH1F('hlt_jet_pt', 'hlt_jet_pt', 100, 0, 500) hlt_jet_m = TH1F('hlt_jet_m', 'hlt_jet_m', 40, 0, 80) hlt_HT = TH1F('hlt_HT', 'hlt_HT', 100, 0, 1800) fj_jet_pt = TH1F('fj_jet_pt', 'fj_jet_pt', 100, 0, 500) fj_jet_pt_low = TH1F('fj_jet_pt_low', 'fj_jet_pt_low', 50, 0, 20) fj_jet_pt_high = TH1F('fj_jet_pt_high', 'fj_jet_pt_high', 100, 0, 500) fj_jet_m = TH1F('fj_jet_m', 'fj_jet_m', 40, 0, 80) fj_HT = TH1F('fj_HT', 'fj_HT', 100, 0, 1800) fj_HT20 = TH1F('fj_HT20', 'fj_HT20', 100, 0, 1800)
def test_tree2array(self): chain = TChain('tree') chain.Add(self.ld('single1.root')) self.check_single(tree2array(chain))
from ROOT import TROOT, TCanvas, TH1F, TChain, TLine #constants m_D_meson = 1869.65 m_D_meson_down = m_D_meson-100.0 m_D_meson_up = m_D_meson+100.0 err=30 halferr=err/2.0 # load files chain= TChain('D2KSPiPiPi_DD/DecayTree') chain.Add('DV_Prompt_D2Kshhh_2016_DT_0.root') #you can add as many files as needed print 'Number of entries: ' , chain.GetEntries() #prints in the command line the number of entries L0_set=('D_L0HadronDecision_TOS==1' or 'D_L0MuonDecision_Tis==1' or 'D_L0DiMuonDecision_Tis==1' or \ 'D_L0ElectronDecision_Tis==1' or 'D_L0PhotonDecision_Tis==1') Hlt1_set=('D_Hlt1TrackMVADecision_TOS==1' or 'D_Hlt1TwoTrackMVADecision_TOS==1') trigger=L0_set and Hlt1_set SignalWindow = 'D_M > 1839.65 ' and 'D_M < 1899.65 ' SideBand = ('D_M > 1754.65 ' and 'D_M < 1784.65 ') \ or ('D_M > 1954.65' and 'D_M < 1984.65') #D_PT h1_D_PT = TH1F("h1_D_PT", "D_PT", 100, 0, 20000) #from 0 to 600000, 100 bins
if line.strip() and not '#' in line: # this skips empty lines line = line.replace('/', '_').replace('_$(channel)', '').replace( '_bkg', 'bkg').replace('_sig', 'sig') id, sample = line.split(" ") results.setdefault(sample.strip(), []).append(int(id.strip())) #for key in results: # print(key, results[key]) #print(results) #print(results["bkg_Top_1"][0]) path = [] path_file = "/eos/atlas/user/a/ademaria/Slim_July_Production/mc_nom/" for k, v in results.items(): Entries = [] for i in range(0, len(v)): path = os.listdir(path_file) fname = [f for f in path if str(results[k][i]) in f][0] #print(fname) root_path = path_file + "/" + fname # root_files = [f1 for f1 in os.listdir(root_path) if f1.endswith('.root')] root_files = os.listdir(root_path) mychain1 = TChain("NOMINAL") for rootname in root_files: mychain1.Add(root_path + "/" + rootname) Entries.append(mychain1.GetEntries()) #print(Entries) TotEntries = sum(Entries) #print ("%18s %d" %(k,int(TotEntries))) out.write("%18s %d\n" % (k, int(TotEntries))) #print(path) out.close()
print "Input:", options.inputfile, "; Output:", outfilename #outfilename= 'SkimmedTree.root' skimmedTree = TChain("tree/treeMaker") if isfarmout: infile = open(inputfilename) failcount = 0 for ifile in infile: try: f_tmp = TFile.Open(ifile.rstrip(), 'READ') if f_tmp.IsZombie(): # or fileIsCorr(ifile.rstrip()): failcount += 1 continue skimmedTree.Add(ifile.rstrip()) except: failcount += 1 if failcount > 0: print "Could not read %d files. Skipping them." % failcount if not isfarmout: skimmedTree.Add(inputfilename) # # skimmedTree.Add(sys.argv[1]) def arctan(x, y): corr = 0 if (x > 0 and y >= 0) or (x > 0 and y < 0):
plotbinning = [[70, 0.6, 1.6], [70, 0.6, 1.6], [70, 0.85, 1.45]] #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # pdfout = PDFPrinting(outputname) outputfile = ROOT.TFile(outputname + ".root", "RECREATE") outputfile.cd() c1 = ROOT.TCanvas() tree_sig = TChain("MVATree") tree_bkg = TChain("MVATree") samplesel = "" for f in glob(ttHlocation + "/*.root"): tree_sig.Add(f) samplesel = "Evt_Odd == 1 &&" for f in glob(ttbarlocation + "/*.root"): tree_bkg.Add(f) #--------------------------------------------------------- #--------------------- Signal Sample --------------------- #--------------------------------------------------------- print "Processing Singnal Sample" # Catplots for regression Bias for ireg, reg in enumerate(regressions): corrcatpT = CatPlots("Regression Output", [0, 50, 75, 100, 150, 200, 300, 600], "Jet_Pt", "Jet p_{T}", False, False, "ttHbb", plotbinning[ireg],
for iw, weight in enumerate(weightlist): targetvoutput = TwoDplot("Target", "Output", [50, 0.6, 1.6], [40, 0.7, 1.5]) targetvoutput.setAxisTitle("Target", "Output") print "Processing weight", iw, "of", len(weightlist) - 1 print "Weight name:", weight.split("/")[-1] tree = TChain("MVATree") if sample is "Signal": samplesel = "" for f in glob(ttHlocation + "/*.root"): tree.Add(f) else: samplesel = "Evt_Odd == 1 &&" for f in glob(ttbarlocations[iw] + "/*.root"): tree.Add(f) if dotargetcomp: partonvgenjet_ratio = TwoDplot("Parton", "GenJet", [50, 0.8, 1.2], [50, 0.8, 1.2]) partonvgenjet_ratio.setAxisTitle("Parton p_{T} ratio", "GenJet p_{T} ratio") partonvgenjet_ratio.projecttoTwoDplot( tree, "(" + prefix[iw] + "Jet_MatchedPartonPt/" + prefix[iw] + "Jet_Pt):(" + prefix[iw] + "Jet_MatchedGenJetwNuPt/" + prefix[iw] + "Jet_Pt)", "abs(" + prefix[iw] + "Jet_MatchedPartonFlav) == 5")
outfilename = 'SkimmedTree.root' PUPPI = True CA15 = False ## When running on farmout #inputfilename = os.environ['INPUT'] #outfilename = os.environ['OUTPUT'] skimmedTree = TChain("tree/treeMaker") ##======use this for providing list of file======## #infile = open(inputfilename) #for ifile in infile: # skimmedTree.Add(ifile.rstrip()) # samplename = WhichSample(inputfilename) ##======use this for providing list of file======## skimmedTree.Add(sys.argv[1]) #samplename = WhichSample(sys.argv[1]) def AnalyzeDataSet(): NEntries = skimmedTree.GetEntries() h_total = TH1F('h_total', 'h_total', 2, 0, 2) h_total_mcweight = TH1F('h_total_mcweight', 'h_total_mcweight', 2, 0, 2) outfile = TFile(outfilename, 'RECREATE') outTree = TTree('outTree', 'tree branches') st_runId = numpy_.zeros(1, dtype=int) st_lumiSection = array('L', [0])
mu_data_samplenames.append('SingleMu_Run2016Hv2') mu_data_samplenames.append('SingleMu_Run2016Hv3') el_data_samplenames = [] el_data_samplenames.append('SingleEl_Run2016Bv2') el_data_samplenames.append('SingleEl_Run2016C') el_data_samplenames.append('SingleEl_Run2016D') el_data_samplenames.append('SingleEl_Run2016E') el_data_samplenames.append('SingleEl_Run2016F') el_data_samplenames.append('SingleEl_Run2016G') el_data_samplenames.append('SingleEl_Run2016Hv2') el_data_samplenames.append('SingleEl_Run2016Hv3') #data chains mu_data_chain = TChain('tree') for n in mu_data_samplenames: mu_data_chain.Add(total_ttree_dir_path + n + '_skim_all.root') el_data_chain = TChain('tree') for n in el_data_samplenames: el_data_chain.Add(total_ttree_dir_path + n + '_skim_all.root') #set data chains for channame in all_channels.keys(): if channame.find('_mu') != -1: all_channels[channame].setDataChain(mu_data_chain) elif channame.find('_el') != -1: all_channels[channame].setDataChain(el_data_chain) else: print 'ERROR: failed to determine leptype for chain with name ' + channame #skim data chains in parallel
# ROOT imports from ROOT import TChain # custom ROOT classes from ROOT import alp, ComposableSelector, CounterOperator, JetFilterOperator, BTagFilterOperator, JetPairingOperator, DiJetPlotterOperator from ROOT import BaseOperator, FolderOperator, EventWriterOperator config = {"jets_branch_name": "Jets", "hlt_names":[]} selector = ComposableSelector(alp.Event)(0, json.dumps(config)) selector.addOperator(BaseOperator(alp.Event)()) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(JetFilterOperator(alp.Event)(2.5, 30., 4)) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(BTagFilterOperator(alp.Event)("pfCombinedInclusiveSecondaryVertexV2BJetTags", 0.800, 4)) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(FolderOperator(alp.Event)("empty_one")) selector.addOperator(JetPairingOperator(alp.Event)(4)) selector.addOperator(FolderOperator(alp.Event)("4CSVM")) selector.addOperator(DiJetPlotterOperator(alp.Event)()) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(EventWriterOperator(alp.Event)()) selector.addOperator(FolderOperator(alp.Event)("empty_two")) tchain = TChain("ntuple/tree") tchain.Add("/lustre/cmswork/hh/alpha_ntuples/v0_20161004/GluGluToHHTo4B_node_SM_13TeV-madgraph_v14-v1/0000/output.root") tchain.Process(selector, "", 10000)
# Opening root file #fname = glob("/pnfs/uboone/persistent/users/aschu/MC_BNB_Cosmic/prodgenie_bnb_nu_cosmic_uboone_v05_08_00_anatree.root") #fname = glob("/pnfs/uboone/persistent/users/aschu/MEC/MECmerge.root") #fname = glob("/pnfs/uboone/persistent/users/aschu/TEM/TEMmerge.root") #fname = glob("/pnfs/uboone/scratch/users/mdeltutt/v05_11_00/anatree_bnb_cosmic_eventWeight_MACCQE_ccincl_wflash_2/*/ana_hist.root") # Ma CCQE Selection I fname = glob( "/pnfs/uboone/scratch/users/mdeltutt/v05_11_00/anatree_prodgenie_bnb_nu_cosmic_uboone_mcc7_reco2_reweightMACCQE/*/ana_hist.root" ) # Ma CCQE Selection II #print "Input file: ", fname # Creating TChain chain = TChain("analysistree/anatree") chainPOT = TChain("analysistree/pottree") for f in fname: chain.Add(f) chainPOT.Add(f) # Printing the number of entries entries = chain.GetEntries() entriesPOT = chainPOT.GetEntries() print "Number of entries in the ANA tree: ", entries print "Number of entries in the POT tree: ", entriesPOT # Getting POT number #NominalPOT = 6.0e20 TotalPOT = 0 for jentry in xrange(entriesPOT): entry = chainPOT.GetEntry(jentry) #print '{:e}'.format(float(chainPOT.pot)) TotalPOT += chainPOT.pot
nBins = 50 maxX = 3 minX = -maxX maxY = 3 minY = -maxY outfile = remainder[0] datafile = open(remainder[1], "r") dataFiles = [] for line in (raw.strip().split() for raw in datafile): dataFiles.append(line[0]) dataevents = TChain("ntuple") for i in range(len(dataFiles)): dataevents.Add(dataFiles[i]) mcfile = open(remainder[2], "r") mcFiles = [] for line in (raw.strip().split() for raw in mcfile): mcFiles.append(line[0]) mcevents = TChain("ntuple") for i in range(len(mcFiles)): mcevents.Add(mcFiles[i]) apfile = open(remainder[3], "r") apfiles = [] events = [] mass = []
config["xsec_br"] = samples[sname]["xsec_br"] config["matcheff"] = samples[sname]["matcheff"] config["kfactor"] = samples[sname]["kfactor"] json_str = json.dumps(config) #define selectors list selector = ComposableSelector(alp.Event)(0, json_str) selector.addOperator(BaseOperator(alp.Event)()) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(MCTruthOperator(alp.Event)(0.5, True)) selector.addOperator(CounterOperator(alp.Event)()) selector.addOperator(FolderOperator(alp.Event)("pair")) selector.addOperator(JetPlotterOperator(alp.Event)(btagAlgo,weights_v)) selector.addOperator(DiJetPlotterOperator(alp.Event)(weights_v)) selector.addOperator(EventWriterOperator(alp.Event)(json_str,weights_v)) #create tChain and process each files tchain = TChain("pair/tree") for File in files: tchain.Add(File) nev = numEvents if (numEvents > 0 and numEvents < tchain.GetEntries()) else tchain.GetEntries() procOpt = "ofile=./"+sname+".root" if not oDir else "ofile="+oDir+"/"+sname+".root" print "max numEv {}".format(nev) tchain.Process(selector, procOpt, nev) ns+=1 print "### processed {} samples ###".format(ns)
from ROOT import TFile, TChain, TH1F, TH3F, gDirectory, TCanvas, kRed from geomUtil import isect_line_plane_v3 from glob import glob bin_ang = 12 fidvol = 20 bin_len = 8 f = glob("../root_files/mucs_cry_overlay/*/ana_hist.root") h_res_phi = TH1F("h_res_phi", "", 100, -10, 10) h_res_theta = TH1F("h_res_theta", "", 100, -10, 10) entry = TChain("analysistree/anatree") for filename in f: entry.Add(filename) y_end = 116.5 p_no = [0, 1, 0] p_co_tpc = [0, y_end, 0] in_tpc = 0 mucs_reco = 0 reco = 0 total_mucs_reco = 0 decay = 0 ok = 0 entries = entry.GetEntries() print(entries) for event in range(entries):
import math c1 = TCanvas("c1") p1 = TPad("p1", "p1", 0.05, 0.05, 0.95, 0.95) p1.Draw() p1.cd() red_lines = [] blue_lines = [] green_lines = [] red_lines_2D = [] blue_lines_2D = [] green_lines_2D = [] chain = TChain("events_pandoraCosmic") chain.Add("../root_files/CORSIKA.root") chain.Add("../root_files/MuCSRun7347_Group181_MergedTree.Root") #downstream chain.Add("../root_files/MuCSRun7348_Group181_MergedTree.Root") #downstream chain.Add("../root_files/MuCSRun7702_Group182_MergedTree.Root") #upstream chain.Add("../root_files/MuCSRun7703_Group183_MergedTree.Root") #upstream entries = chain.GetEntries() print(entries) for entry in range(entries): ientry = chain.LoadTree(entry) nb = chain.GetEntry(entry) cosmic_pe = chain.flash_pe l_mucs = chain.MuCS_TPC_len if entry % 1000 == 0: print(entry)
def test_tree2rec(self): chain = TChain('tree') chain.Add(self.ld('single1.root'))
import tensorflow as tf from tensorflow.python.keras import models as m from tensorflow.python.keras import layers as l import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.externals import joblib from ROOT import TFile, TTree, TChain print("All classes initialized successfully!!!") sigChan = TChain("varTree") sigChan.Add("signal.root") bkgChan = TChain("varTree") bkgChan.Add("background.root") print("Data read from the trees. Printing out the contents.") sigChan.Print() bkgChan.Print() sigSampleSize = sigChan.GetEntries() bkgSampleSize = bkgChan.GetEntries() sampleSize = sigSampleSize if sigSampleSize<bkgSampleSize else bkgSampleSize sigFull = sigChan.AsMatrix() print(sigFull[0]) print(sigFull.shape) bkgFull = bkgChan.AsMatrix() print(bkgFull.shape)
def optimizeCutOrder(dsNum): """ ./plots2.py -ord [dsNum] Optimize the order of reduction: fs, rn, wf, fs+rn, fs+wf, rn+wf, fs+rn+wf now I'm also thinking about applying the threshold cut to every file ... ... that will tell you how good the cuts are when we're above/below the detector threshold ... do I want that? anyway, try it first without. (simpler.) Results: [raw, fs, rn, wf, fs+rn, fs+wf, fs+rn, rn+wf, fs+rn+wf] DS1 outputs: Int2: [330357.60, 606.30, 314160.80, 1359.80, 52.80, 21.70, 1358.30, 21.50] Int5: [4362.90, 52.90, 1018.90, 54.90, 52.60, 30.50, 54.40, 30.20] Int10: [1991.90, 75.20, 98.00, 63.40, 74.80, 52.10, 63.40, 52.10] Int50: [94.20, 75.00, 90.70, 63.40, 74.90, 51.50, 63.40, 51.50] Int250: [51.60, 42.50, 49.80, 39.20, 42.40, 35.80, 39.10, 35.70] Reduction order (least to most, <2 kev): raw, rn, wf, rn+wf, fs, fs+rn, fs+wf, fs+rn+wf Order to plot cut reductions: raw, fs, fs+wf, fs+rn+wf. Clearly fs is our best noise discriminator. """ int2, int5, int10, int50, int250 = [], [], [], [], [] kpb = 0.1 eLo, eHi = 0., 250. nBins = int((eHi-eLo)/kpb) fIn = TFile("../data/latRaw_DS%d.root" % dsNum) hRaw = fIn.Get("hSum") rebinFactor = int(kpb/0.01) hRaw = hRaw.Rebin(rebinFactor) getIntegralCounts(hRaw, int2, int5, int10, int50, int250) fileFS = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/fs/fitSlo-DS%d-*.root" % dsNum)) chainFS = TChain("skimTree") [chainFS.Add(f) for f in fileFS] hFS = wl.H1D(chainFS,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hFS") getIntegralCounts(hFS, int2, int5, int10, int50, int250) fileRN = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/rn/riseNoise-DS%d-*.root" % dsNum)) chainRN = TChain("skimTree") [chainRN.Add(f) for f in fileRN] hRN = wl.H1D(chainRN,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hRN") getIntegralCounts(hRN, int2, int5, int10, int50, int250) fileWF = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/wf/wfstd-DS%d-*.root" % dsNum)) chainWF = TChain("skimTree") [chainWF.Add(f) for f in fileWF] hWF = wl.H1D(chainWF,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hWF") getIntegralCounts(hWF, int2, int5, int10, int50, int250) fileFSRN = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/fs_rn/fs_rn-DS%d-*.root" % dsNum)) chainFSRN = TChain("skimTree") [chainFSRN.Add(f) for f in fileFSRN] hFSRN = wl.H1D(chainFSRN,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hFSRN") getIntegralCounts(hFSRN, int2, int5, int10, int50, int250) fileFSWF = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/fs_wf/fs_wf-DS%d-*.root" % dsNum)) chainFSWF = TChain("skimTree") [chainFSWF.Add(f) for f in fileFSWF] hFSWF = wl.H1D(chainFSWF,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hFSWF") getIntegralCounts(hFSWF, int2, int5, int10, int50, int250) fileRNWF = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/rn_wf/rn_wf-DS%d-*.root" % dsNum)) chainRNWF = TChain("skimTree") [chainRNWF.Add(f) for f in fileRNWF] hRNWF = wl.H1D(chainRNWF,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hRNWF") getIntegralCounts(hRNWF, int2, int5, int10, int50, int250) fileFSRNWF = sorted(glob.glob("/global/homes/w/wisecg/project/cuts/fs_rn_wf/fs_rn_wf-DS%d-*.root" % dsNum)) chainFSRNWF = TChain("skimTree") [chainFSRNWF.Add(f) for f in fileFSRNWF] hFSRNWF = wl.H1D(chainFSRNWF,nBins,eLo,eHi,"trapENFCal","","trapENFCal","","hFSRNWF") getIntegralCounts(hFSRNWF, int2, int5, int10, int50, int250) # stackoverflow trick to make list printing prettier class prettyfloat(float): def __repr__(self): return "%.2f" % self print "Int2:", map(prettyfloat, int2) print "Int5:", map(prettyfloat, int5) print "Int10:", map(prettyfloat, int10) print "Int50:", map(prettyfloat, int50) print "Int250:", map(prettyfloat, int250)
def Count(chan, trigs): #deal with weights first sumWeights = TChain("sumWeights") sumWeights.Add("%stth*.root" % prepath) weights = [] fCurrent_wt = 0 sampleNEvt = 0 nWeightEntries = sumWeights.GetEntries() for a in range(nWeightEntries): sumWeights.GetEntry(a) totalEventsWeighted = getattr(sumWeights, 'totalEventsWeighted') if sumWeights.GetTreeNumber() != fCurrent_wt: fCurrent_wt = sumWeights.GetTreeNumber() weights.append(sampleNEvt) sampleNEvt = 0 sampleNEvt = sampleNEvt + totalEventsWeighted if a == nWeightEntries - 1: weights.append(sampleNEvt) #last file chain = TChain("nominal") chain.Add("%stth*.root" % prepath) nentries = chain.GetEntries() chain.SetBranchStatus("*", 0) chain.SetBranchStatus("Mll01", 1) chain.SetBranchStatus("total_charge", 1) chain.SetBranchStatus("lep_Pt_0", 1) chain.SetBranchStatus("lep_Pt_1", 1) chain.SetBranchStatus("lep_Eta_0", 1) chain.SetBranchStatus("lep_Eta_1", 1) chain.SetBranchStatus("lep_ID_0", 1) chain.SetBranchStatus("lep_ID_1", 1) chain.SetBranchStatus("lep_truthPdgId_0", 1) chain.SetBranchStatus("lep_truthPdgId_1", 1) chain.SetBranchStatus("lep_truthOrigin_0", 1) chain.SetBranchStatus("lep_truthOrigin_1", 1) chain.SetBranchStatus("lep_truthType_0", 1) chain.SetBranchStatus("lep_truthType_1", 1) chain.SetBranchStatus("lep_isQMisID_0", 1) chain.SetBranchStatus("lep_isQMisID_1", 1) chain.SetBranchStatus("nJets_OR_T_MV2c10_70", 1) chain.SetBranchStatus("nJets_OR_T", 1) chain.SetBranchStatus("lep_isTightLH_0", 1) chain.SetBranchStatus("lep_isTightLH_1", 1) chain.SetBranchStatus("lep_isLooseLH_0", 1) chain.SetBranchStatus("lep_isLooseLH_1", 1) chain.SetBranchStatus("lep_isolationFixedCutTight_0", 1) chain.SetBranchStatus("lep_isolationFixedCutLoose_0", 1) chain.SetBranchStatus("lep_isolationFixedCutTight_1", 1) chain.SetBranchStatus("lep_isolationFixedCutLoose_1", 1) chain.SetBranchStatus("lep_isolationFixedCutTightTrackOnly_0", 1) chain.SetBranchStatus("lep_isolationFixedCutTightTrackOnly_1", 1) chain.SetBranchStatus("HLT*", 1) chain.SetBranchStatus("*type", 1) chain.SetBranchStatus("RunYear", 1) chain.SetBranchStatus("passEventCleaning", 1) chain.SetBranchStatus("lep_isTrigMatch_0", 1) chain.SetBranchStatus("lep_isTrigMatch_1", 1) chain.SetBranchStatus("lep_isTrigMatchDLT_0", 1) chain.SetBranchStatus("lep_isTrigMatchDLT_1", 1) chain.SetBranchStatus("mcWeightOrg", 1) chain.SetBranchStatus("pileupEventWeight_090", 1) chain.SetBranchStatus("lepSFObjTight", 1) chain.SetBranchStatus("lepSFTrigTight", 1) chain.SetBranchStatus("JVT_EventWeight", 1) chain.SetBranchStatus("SherpaNJetWeight", 1) chain.SetBranchStatus("MV2c10_70_EventWeight", 1) chain.SetBranchStatus("lep_chargeIDBDT*", 1) chain.SetBranchStatus("nTaus_OR_Pt25", 1) chain.SetBranchStatus("tau_JetBDTSigTight_0", 1) chain.SetBranchStatus("tau_JetBDTSigTight_1", 1) chain.SetBranchStatus("tau_tagWeightBin_0", 1) chain.SetBranchStatus("tau_tagWeightBin_1", 1) chain.SetBranchStatus("tau_passMuonOLR_0", 1) chain.SetBranchStatus("tau_passMuonOLR_1", 1) chain.SetBranchStatus("tau_passEleBDT_0", 1) chain.SetBranchStatus("tau_passEleBDT_1", 1) chain.SetBranchStatus("tau_charge_0", 1) chain.SetBranchStatus("tau_charge_1", 1) chain.SetBranchStatus("lep_ID_2", 1) chain.SetBranchStatus("Mll02", 1) chain.SetBranchStatus("lep_promptLeptonVeto_TagWeight_0", 1) chain.SetBranchStatus("lep_promptLeptonVeto_TagWeight_1", 1) chain.SetBranchStatus("lep_ambiguityType_0", 1) chain.SetBranchStatus("lep_ambiguityType_1", 1) #cuts fCurrent = -1 chain.LoadTree(0) cuts_sr = TTreeFormula("cuts_sr", chan, chain) cuts_trig = TTreeFormula("cuts_trig", trigs, chain) raw_evts, numevts = 0, 0 for evt in range(nentries): #for event in chain: #if evt%10000==0 : print evt chain.GetEntry(evt) #get current file currentFileName = chain.GetCurrentFile().GetName() RunYear = getattr(chain, "RunYear") mcWeightOrg = getattr(chain, "mcWeightOrg") pileupEventWeight_090 = getattr(chain, "pileupEventWeight_090") lepSFObjTight = getattr(chain, "lepSFObjTight") lepSFTrigTight = getattr(chain, "lepSFTrigTight") JVT_EventWeight = getattr(chain, "JVT_EventWeight") SherpaNJetWeight = getattr(chain, "SherpaNJetWeight") MV2c10_70_EventWeight = getattr(chain, "MV2c10_70_EventWeight") lumi = 1.0 if RunYear < 2016.5: lumi = 36074.6 if RunYear > 2016.5: lumi = 43813.7 if chain.GetTreeNumber() != fCurrent: fCurrent = chain.GetTreeNumber() cuts_sr.Notify() cuts_trig.Notify() if cuts_sr.EvalInstance() and cuts_trig.EvalInstance(): #if cuts_trig.EvalInstance(): if "341177" in currentFileName: weight = 0.05343 if "341270" in currentFileName: weight = 0.22276 if "341271" in currentFileName: weight = 0.23082 kfac, filEff = 1, 1 #weight=weight*kfac*filEff*mcWeightOrg*pileupEventWeight_090*lepSFObjTight*lepSFTrigTight*JVT_EventWeight*SherpaNJetWeight*MV2c10_70_EventWeight*lumi/weights[fCurrent] weight = weight * kfac * filEff * mcWeightOrg * lepSFObjTight * lepSFTrigTight * JVT_EventWeight * SherpaNJetWeight * MV2c10_70_EventWeight * lumi / weights[ fCurrent] #print xsec, kfac, filEff, event.mcWeightOrg,event.pileupEventWeight_090,event.lepSFObjTight,event.lepSFTrigTight,event.JVT_EventWeight,event.SherpaNJetWeight,event.MV2c10_70_EventWeight, lumi raw_evts = raw_evts + 1 numevts = numevts + weight print "%s(%.2f)" % (raw_evts, numevts)
def genRawHists(dsNum): """ ./plots2.py -gen [dsNum] Create "raw" LAT histograms for each dataset. Use a super fine binning and huge energy range for versatility. Don't distinguish between enriched and natural detectors. 1. sum histogram 'hSum' 2. cpd vs energy 'hCPDE' 3. cpd vs run 'hCPDrun' Could also do channel-by-channel, bkgIdx by bkgIdx, but hopefully the 2D CPD plots will be enough for diagnostics (can examine their bin contents). Plus we can loop over the bins in the raw histo instead of re-drawing everything. """ print "Plots2 started:",time.strftime('%X %x %Z') startT = time.clock() kpb = 0.01 eLo, eHi = 0., 15000. nBins = int((eHi-eLo)/kpb) # build the file list sequentially fList = [] dsPath = "/global/homes/w/wisecg/project/bg-lat" for bkgIdx in range(ds.dsMap[dsNum]+1): tmp = glob.glob("%s/latSkimDS%d_%d_*.root" % (dsPath, dsNum, bkgIdx)) nFiles = len(tmp) for subIdx in range(nFiles): fName = "%s/latSkimDS%d_%d_%d.root" % (dsPath, dsNum, bkgIdx, subIdx) if os.path.isfile(fName) == True: fList.append(fName) else: print "File doesn't exist, exiting ...",fName return # fList = fList[:2] # limit fList (debug only) fCut = TFile(fList[0]) dcCut = fCut.Get("theCut").GetTitle() dcCut += "&& gain==0" ch = TChain("skimTree") [ch.Add(f) for f in fList] print "Generating DS-%d (%d entries)." % (dsNum,ch.GetEntries()) cpdLo, cpdHi = 111, 175 if dsNum==4: cpdLo, cpdHi = 211, 275 if dsNum==5: cpdLo, cpdHi = 111, 275 nCPD = cpdHi - cpdLo ch.GetEntry(0) runLo = ch.run ch.GetEntry(ch.GetEntries()-1) runHi = ch.run nRuns = runHi - runLo + 1 print "First run:",runLo,"Last run:",runHi fOut = TFile("../data/latRaw_DS%d.root" % dsNum,"RECREATE") print "Generating sum spectrum. %.2f mins elapsed. %s" % ((time.clock()-startT)/60., time.strftime('%X %x %Z')) h1 = wl.H1D(ch,nBins,eLo,eHi,"trapENFCal",dcCut,"trapENFCal","Counts","raw sum LAT spectrum","hSum") h1.Write() print "Generating CPD vs. E. %.2f mins elapsed. %s" % ((time.clock()-startT)/60., time.strftime('%X %x %Z')) h2 = wl.H2D(ch,nBins,eLo,eHi,nCPD,cpdLo,cpdHi,"C*100+P*10+D:trapENFCal",dcCut,"trapENFCal","CPD","CPD vs. E","hCPDE") h2.Write() print "Generating CPD vs. run. %.2f mins elapsed. %s" % ((time.clock()-startT)/60., time.strftime('%X %x %Z')) h3 = wl.H2D(ch,nRuns,runLo,runHi,nCPD,cpdLo,cpdHi,"C*100+P*10+D:run",dcCut,"run","CPD","CPD vs. run","hCPDrun") h3.Write() fOut.Close() print "Time Elapsed:",(time.clock()-startT)/60.,"minutes." print "Done at",time.strftime('%X %x %Z')
def makeNtuple_ttbar_ljets(inputFiles, outputname, treename="Delphes", arrayFormat=None, maxevents=None): # inputs infiles = getFileList(inputFiles) chain = TChain(treename) for finput in infiles: chain.Add(finput) nevents = chain.GetEntries() print('Total number of entries:', nevents) foutput = TFile(outputname + '.root', "recreate") # tree tree = TTree("ntuple", "ntuple") # variables variables = varTTbarLJets() variables.set_up_branches(tree) # loop over events t_tree_start = time.time() for ievt, event in enumerate(chain): if maxevents is not None and ievt >= maxevents: print("maxevents:", maxevents) break if not ievt % 10000: print("processing event {}/{}".format(ievt, nevents)) if not passEventSelection(event): continue variables.set_default() # event weight variables.w[0] = event.Event[0].Weight # partons # top t_afterFSR = getTopAfterFSR(event.Particle) t_p4 = TLorentzVector() t_p4.SetPxPyPzE(t_afterFSR.Px, t_afterFSR.Py, t_afterFSR.Pz, t_afterFSR.E) t_ishadronic = isHadronicTop(t_afterFSR, event.Particle) variables.t_ishadronic[0] = t_ishadronic W_fromT = getWfromTopDecay(t_afterFSR, event.Particle, afterFSR=True) W_fromT_p4 = TLorentzVector() W_fromT_p4.SetPxPyPzE(W_fromT.Px, W_fromT.Py, W_fromT.Pz, W_fromT.E) # antitop tbar_afterFSR = getAntiTopAfterFSR(event.Particle) tbar_p4 = TLorentzVector() tbar_p4.SetPxPyPzE(tbar_afterFSR.Px, tbar_afterFSR.Py, tbar_afterFSR.Pz, tbar_afterFSR.E) tbar_ishadronic = isHadronicTop(tbar_afterFSR, event.Particle) variables.tbar_ishadronic[0] = tbar_ishadronic W_fromTbar = getWfromTopDecay(tbar_afterFSR, event.Particle, afterFSR=True) W_fromTbar_p4 = TLorentzVector() W_fromTbar_p4.SetPxPyPzE(W_fromTbar.Px, W_fromTbar.Py, W_fromTbar.Pz, W_fromTbar.E) th_p4, tl_p4 = None, None wh_p4, wl_p4 = None, None if t_ishadronic and not tbar_ishadronic: th_p4 = t_p4 tl_p4 = tbar_p4 wh_p4 = W_fromT_p4 wl_p4 = W_fromTbar_p4 elif not t_ishadronic and tbar_ishadronic: th_p4 = tbar_p4 tl_p4 = t_p4 wh_p4 = W_fromTbar_p4 wl_p4 = W_fromT_p4 # veto events in boosted category #if isBoostedTop(event, th_p4): # continue if th_p4 is not None: variables.th_pt[0] = th_p4.Pt() variables.th_eta[0] = th_p4.Eta() variables.th_y[0] = th_p4.Rapidity() variables.th_phi[0] = th_p4.Phi() variables.th_m[0] = th_p4.M() variables.th_e[0] = th_p4.E() if tl_p4 is not None: variables.tl_pt[0] = tl_p4.Pt() variables.tl_eta[0] = tl_p4.Eta() variables.tl_y[0] = tl_p4.Rapidity() variables.tl_phi[0] = tl_p4.Phi() variables.tl_m[0] = tl_p4.M() variables.tl_e[0] = tl_p4.E() if wh_p4 is not None: variables.wh_pt[0] = wh_p4.Pt() variables.wh_eta[0] = wh_p4.Eta() variables.wh_phi[0] = wh_p4.Phi() variables.wh_m[0] = wh_p4.M() variables.wh_e[0] = wh_p4.E() if wl_p4 is not None: variables.wl_pt[0] = wl_p4.Pt() variables.wl_eta[0] = wl_p4.Eta() variables.wl_phi[0] = wl_p4.Phi() variables.wl_m[0] = wl_p4.M() variables.wl_e[0] = wl_p4.E() # lepton if passElectronCuts(event) and not passMuonCuts(event): variables.lep_pt[0] = event.Electron[0].PT variables.lep_eta[0] = event.Electron[0].Eta variables.lep_phi[0] = event.Electron[0].Phi variables.lep_pdgid[0] = -11 * event.Electron[0].Charge elif passMuonCuts(event) and not passElectronCuts(event): variables.lep_pt[0] = event.Muon[0].PT variables.lep_eta[0] = event.Muon[0].Eta variables.lep_phi[0] = event.Muon[0].Phi variables.lep_pdgid[0] = -13 * event.Muon[0].Charge # jets variables.njets[0] = 0 variables.njets25[0] = 0 for i, jet in enumerate(event.Jet): variables.njets[0] += 1 if jet.PT > 25.: variables.njets25[0] += 1 if i == 0: variables.j1_pt[0] = jet.PT variables.j1_eta[0] = jet.Eta variables.j1_phi[0] = jet.Phi variables.j1_m[0] = jet.Mass variables.j1_isbtag[0] = jet.BTag if i == 1: variables.j2_pt[0] = jet.PT variables.j2_eta[0] = jet.Eta variables.j2_phi[0] = jet.Phi variables.j2_m[0] = jet.Mass variables.j2_isbtag[0] = jet.BTag if i == 2: variables.j3_pt[0] = jet.PT variables.j3_eta[0] = jet.Eta variables.j3_phi[0] = jet.Phi variables.j3_m[0] = jet.Mass variables.j3_isbtag[0] = jet.BTag if i == 3: variables.j4_pt[0] = jet.PT variables.j4_eta[0] = jet.Eta variables.j4_phi[0] = jet.Phi variables.j4_m[0] = jet.Mass variables.j4_isbtag[0] = jet.BTag if i == 4: variables.j5_pt[0] = jet.PT variables.j5_eta[0] = jet.Eta variables.j5_phi[0] = jet.Phi variables.j5_m[0] = jet.Mass variables.j5_isbtag[0] = jet.BTag if i == 5: variables.j6_pt[0] = jet.PT variables.j6_eta[0] = jet.Eta variables.j6_phi[0] = jet.Phi variables.j6_m[0] = jet.Mass variables.j6_isbtag[0] = jet.BTag # MET variables.met_met[0] = event.MissingET[0].MET variables.met_phi[0] = event.MissingET[0].Phi tree.Fill() t_tree_done = time.time() print("Making flat nutple took {:.2f} seconds ({:.3f} ms/event)".format( t_tree_done - t_tree_start, (t_tree_done - t_tree_start) * 1000 / nevents)) print("Finish writing tree to file") foutput.Write() if arrayFormat is not None: print("Save tree as array") # convert tree to array import resource ROOT.ROOT.EnableImplicitMT() usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss print("Current RAM usage: {:.3f} GB".format(usage * 1e-6)) t_arr_start = time.time() ntuple_arr_dict = ROOT.RDataFrame(tree).AsNumpy() t_arr_done = time.time() print("Converting TTree took {:.2f} seconds".format(t_arr_done - t_arr_start)) usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss print("Current RAM usage: {:.3f} GB".format(usage * 1e-6)) if arrayFormat == "npz": np.savez(outputname + '.npz', **ntuple_arr) elif arrayFormat == "h5": import h5py with h5py.File(outputname + '.h5', 'w') as hf: for var_name, var_arr in ntuple_arr_dict.items(): hf.create_dataset(var_name, data=var_arr) else: raise RuntimeError("Current supported format: 'npz' or 'h5'")
def scrapeData(): from ROOT import TChain ds, mod = 1, 1 print("Loading cut data ...") bkg = dsi.BkgInfo() cal = dsi.CalInfo() nSub = bkg.dsMap()[ds] chList = dsi.GetGoodChanList(ds) calDB = db.TinyDB('../calDB.json') pars = db.Query() fsD, rnSD, rnCD = {}, {}, {} nCal = cal.GetNCalIdxs(ds, mod) for iC in range(nCal + 1): fsD[iC] = dsi.getDBRecord("fitSlo_ds%d_idx%d_m%d_Peak" % (ds, iC, mod), False, calDB, pars) rnSD[iC] = dsi.getDBRecord( "riseNoise_ds%d_idx%d_m%d_SoftPlus" % (ds, iC, mod), False, calDB, pars) rnCD[iC] = dsi.getDBRecord( "riseNoise_ds%d_idx%d_m%d_Continuum" % (ds, iC, mod), False, calDB, pars) thD = {} for iB in range(nSub + 1): thD[iB] = dsi.getDBRecord("thresh_ds%d_bkgidx%d" % (ds, iB), False, calDB, pars) print("Looping over sub-ranges ...") hitE, fSlo, chans, runs, fsCut, thMu, thSig = [], [], [], [], [], [], [] rise, riseCut = [], [] for sub in range(nSub + 1): tc = TChain("skimTree") fRaw = sorted( glob.glob("%s/latSkimDS%d_%d_*.root" % (dsi.latDir, ds, sub))) for f in fRaw: tc.Add(f) print("%d/%d %d" % (sub, nSub, tc.GetEntries())) # for some reason range 44 in ds1 is corrupted? n = tc.Draw("trapENFCal:fitSlo:channel:run", "", "goff") if n == 0: print("skipped", sub) continue # n = tc.Draw("run:channel:trapENFCal:fitSlo","","goff") # t1, t2, t3, t4 = tc.GetV1(), tc.GetV2(), tc.GetV3(), tc.GetV4() tn = ["run", "channel", "trapENFCal", "fitSlo", "riseNoise"] vals = wl.GetVX(tc, tn, "") t1, t2, t3, t4, t5 = vals["run"], vals["channel"], vals[ "trapENFCal"], vals["fitSlo"], vals["riseNoise"] n = len(t1) pRun = -1 for i in range(n): run = int(t1[i]) if run != pRun: cIdx = cal.GetCalIdx("ds%d_m%d" % (ds, mod), run) bIdx = bkg.GetBkgIdx(ds, run) tmpFS = fsD[cIdx] tmpTH = thD[bIdx] tmpRNC = rnCD[cIdx] tmpRNS = rnSD[cIdx] fsCutChan = list(tmpFS.keys()) thCutChan = list(tmpTH.keys()) rnCutChan = list(tmpRNC.keys()) pRun = run chan = int(t2[i]) if chan not in chList: continue if chan not in fsCutChan: continue if chan not in thCutChan: continue if chan not in rnCutChan: continue fsVal = tmpFS[chan][2] thVal = tmpTH[chan][0] + 3 * tmpTH[chan][1] a = max(tmpRNS[chan][0], tmpRNC[chan][4]) b = tmpRNS[chan][1] c = tmpRNS[chan][2] d = tmpRNS[chan][3] if d == 0: continue rnVal = a + b * np.log(1 + np.exp(t3[i] - c / d)) hitE.append(t3[i]) fSlo.append(t4[i]) runs.append(run) chans.append(chan) fsCut.append(tmpFS[chan][2]) thMu.append(tmpTH[chan][0]) thSig.append(tmpTH[chan][1]) rise.append(t5[i]) riseCut.append(rnVal) # # fCut = sorted(glob.glob("%s/results_v1/fs/fitSlo-DS%d-*.root" % (dsi.dataDir, dsNum))) print(len(hitE)) np.savez("../data/sea-plt-2.npz", hitE, fSlo, chans, runs, fsCut, thMu, thSig, rise, riseCut)
''' from ROOT import gROOT, TChain from rootpy.interactive import wait from rootpy.plotting import Hist import rootpy.plotting.root2matplotlib as rplt import matplotlib.pyplot as plt from dps.utils.plotting import make_plot, Histogram_properties if __name__ == '__main__': # file available on soolin: input_file = "/hdfs/TopQuarkGroup/mc/8TeV/v11/NoSkimUnfolding/BLT/TTJets_MassiveBinDECAY_TuneZ2star_8TeV/TTJets_nTuple_53X_mc_merged.root" # this file contains multiple trees. For now we will focus on the electron channel target_tree = "rootTupleTreeEPlusJets/ePlusJetsTree" gROOT.SetBatch(1) chain = TChain(target_tree) chain.Add(input_file) # now, we want to be fast and just read one variabe # first disable all variables chain.SetBranchStatus("*", 0) # now enable the one we are interested in: chain.SetBranchStatus("unfolding.genMET", 1) # We want to store this variable in a histogram # 80 bins, from 0 to 400 (GeV) h_gen_met = Hist(80, 0, 400) # since we are planning to run over many events, let's cache the fill function fill = h_gen_met.Fill # ready to read all events n_processed_events = 0 stop_at = 10**5 # this is enough for this example
def doSlimSMQCD(treeName,outputFile,inputFiles,year): from ROOT import TChain from ROOT import TFile from ROOT import ROOT #import rootlogon ROOT.gROOT.SetBatch(1) import sys print ("sys.argv = ", sys.argv) if not len(sys.argv)>=2: raise Exception ("Must specify inputFiles as argument!") #inputFiles = sys.argv[1].split(',') inputFiles = inputFiles print ("inputFiles = ", inputFiles) #get main tree ch = TChain(treeName) for file in inputFiles: ch.Add(file) nEntries = ch.GetEntries() #print ("nEntries = ", nEntries) #*****set branches***** #set branch status, at first, all off ch.SetBranchStatus("*", 0) from JetN2N.SlimSMQCDBranches import SlimSMQCDBranchesList from JetN2N.SlimSMQCDBranches2011 import SlimSMQCDBranchesList2011 #event information if year == '2011': for branch in SlimSMQCDBranchesList2011: print (branch) ch.SetBranchStatus(branch,1) else: for branch in SlimSMQCDBranchesList: print (branch) ch.SetBranchStatus(branch,1) #*****set branches end***** #get trigger tree chTri = TChain(treeName+"Meta/TrigConfTree") for file in inputFiles: chTri.Add(file) if chTri.LoadTree(0) < 0: chTri = 0 #get bunch tree chBunch = TChain(treeName+"Meta/BunchConfTree") for file in inputFiles: chBunch.Add(file) if chBunch.LoadTree(0) < 0: chBunch = 0 #get cut flow tree #chCutFlow = TChain(treeName+"Meta/CutFlowTree") #for file in inputFiles: # chCutFlow.Add(file) # Write to new file outFile = outputFile newFile = TFile(outFile, "RECREATE") #new tree ch_new = ch.CloneTree(0) # event counter #cutName=["preD3PD","postD3PD","trigCut"] #evnum=[0,0,0] #weight=[0,0,0] #event selection for i in range(nEntries): ch.GetEntry(i) #evnum[1]+=1 #if hasattr(ch,"mcevt_weight") \ # and ch.mcevt_weight.size() !=0 \ # and ch.mcevt_weight[0].size() !=0: # w=ch.mcevt_weight[0][0] #else: # w=1 #weight[1]+=w ch_new.Fill() #evnum[2]+=1 #weight[2]+=w newFile.cd() ch_new.Write() #nEntriesNew = ch_new.GetEntries() #print ("nEntriesForNewFile = ", nEntriesNew) #check cut flow at D3PD level #if chCutFlow.GetEntries() != 0: # for i in range (chCutFlow.GetEntries()): # chCutFlow.GetEntry(i) # cutFlowName=chCutFlow.name # for j in range(cutFlowName.size()): # if cutName.at(j) == "AllExecutedEvents": # evnum[0]+=chCutFlow.nAcceptedEvents.at(j) # weight[0]+=chCutFlow.nWeightedAcceptedEvents.at(j) #else: # evnum[0]=evnum[1] # weight[0]=weight[1] #copy trigger meta data newFile.cd() newdir = newFile.mkdir( treeName+"Meta", treeName+"Meta" ) newdir.cd() if chTri != 0: chTri_new = chTri.CloneTree() chTri_new.Write() if chBunch != 0: chBunch_new = chBunch.CloneTree() chBunch_new.Write()
def xLoop(fileName, var = '', cut = '', weight = '1', treeName = 'Events', nSlots = 1, nbins = 0, bin0 = -1, binN = -1, bins = [], e0 = 0, nEvents = -1, histo = None, iChunk = -1): # Read from dictionary if isinstance(fileName, dict): d = fileName fileName = d['fileName'] var = d['var'] cut = d['cut'] weight = d['weight'] treeName = d['treeName'] nSlots = d['nSlots'] nbins = d['nbins'] bin0 = d['bin0'] binN = d['binN'] bins = d['bins'] e0 = d['e0'] nEvents = d['nEvents'] histo = d['histo'] iChunk = d['iChunk'] # Load the TChain t = TChain(treeName) if ',' in fileName: files = fileName.replace(' ', '').split(',') for f in files: t.Add(f) else: t.Add(fileName) nEntries = t.GetEntries() if nEvents+e0 > nEntries: nEvents = nEntries-e0 elif nEvents == -1: nEvents = nEntries nEv = -1 # Multiprocess... iN = e0+nEvents eventsPerChunk = float(iN-e0)/nSlots if nSlots > 1: inputs = [] pool = Pool(nSlots) for i in range(nSlots): first = e0+int(eventsPerChunk*(i)) last = e0+int(eventsPerChunk*(i+1)) nEv = last - first print 'Chunk %i: [first, last] = [%i, %i]... nEvents = %i'%(i, first, last, nEv) impi = {} impi['fileName'] = fileName impi['var'] = var impi['cut'] = cut impi['weight'] = weight impi['treeName'] = treeName impi['nSlots'] = 1 impi['nbins'] = nbins impi['bin0'] = bin0 impi['binN'] = binN impi['bins'] = bins impi['e0'] = first impi['nEvents'] = nEv impi['histo'] = None if histo == None else histo.Clone("histo%i"%i) impi['iChunk'] = i inputs.append(impi) print 'Starting the loop with %i nodes...'%nSlots pool.map(xLoop, inputs) pool.close() pool.join() return # Prepare the histogram if histo == None: if bins == []: histo = TH1F('histo', '', nbins, bin0, binN) else : ba = array('f', bins) histo = TH1F('histo', '', bins, ba) # Print some information if iChunk == -1: print 'Starting the loop in event %i... runing on %i events of a total of %i'%(e0, nEvents, nEntries) theCut = cut; theVar = var; theWeight = weight if iChunk <= 0: print ' Variable: %s'%theVar print ' Cut : %s'%theCut print ' Weight : %s'%theWeight for iEv in range(e0, nEvents+e0): t.GetEntry(iEv) if not t.TNBtags >= 1: continue nEv += 1 if iChunk == -1: if nEv % 10000 == 0: print "[%i/%i] (%1.2f %s), event %i in tree"%(nEv, nEvents, float(nEv)/float(nEvents), '%', e0+nEv) elif iChunk == 0: ns = float(nEntries)/float(nEvents) if nEv % 10000 == 0: print "[%i/%i] (%1.2f %s)"%(nEv*ns, nEntries, float(nEv)/float(nEvents), '%') passCut = EvalExpForEvent(theCut, t) val = EvalExpForEvent(theVar, t) weight = EvalExpForEvent(theWeight, t) if passCut: histo.Fill(val, weight) if nEv >= nEvents: break return histo