def main(): gStyle.SetOptStat(0) zplist = [600,600,1200,1200,2000,2000] a0list = [300,400,300,500,300,600] c1 = TCanvas('c1','c1',3) gPad.SetTickx() gPad.SetTicky() c1.SetLeftMargin(0.12) h_frame = TH1F('frame','',60,0,1200) h_frame.SetXTitle('P^{miss}_{T} (GeV)') h_frame.SetYTitle('Arbitrary units') h_frame.SetMaximum(0.3) h_higgsPt = TH1F('h_higgsPt','h_higgsPt',60,0,1200) h_higgsPtList = [] rootfilelist = [] for i in range(6): fName = 'higgsPt_MZp'+str(zplist[i])+'_MA0'+str(a0list[i])+'.root' rootfile = TFile(fName) rootfilelist.append(rootfile) colorList = [61,95,65,91,69,87] for i in range(6): hist = rootfilelist[i].Get('h_higgsPt') hist.SetName('h_higgsPt_MZp'+str(zplist[i])+'_MA0'+str(a0list[i])) h_higgsPtList.append(hist) h_higgsPtList[i].SetLineWidth(2) h_higgsPtList[i].SetLineColor(colorList[i]) info_bar = TLatex(0,0.305,'CMS') info_bar_3 = TLatex(700,0.15,'#font[42]{tan#beta=1.0}') info_bar_2 = TLatex(1200,0.305,'35.9 fb^{-1} (13 TeV)') info_bar.SetTextSize(0.03) info_bar_2.SetTextSize(0.03) info_bar_3.SetTextSize(0.04) info_bar_2.SetTextAlign(31) leg = TLegend(0.32,0.57,0.87,0.87) leg.SetBorderSize(0) for j in range(3): i = 2*j text = 'M_{Z\'} = '+str(zplist[i])+' GeV, M_{A} = '+str(a0list[i])+' GeV' leg.AddEntry(h_higgsPtList[i],text) for j in range(3): i = 2*j+1 text = 'M_{Z\'} = '+str(zplist[i])+' GeV, M_{A} = '+str(a0list[i])+' GeV' leg.AddEntry(h_higgsPtList[i],text) h_frame.Draw('hist') for i in range(6): h_higgsPtList[i].DrawNormalized('histsame') leg.Draw() info_bar.Draw() info_bar_2.Draw() info_bar_3.Draw() c1.Print('Zp2HDM_higgsPt.pdf') ## Baryonic histName_bar = ['missPt_MZp1000_MDM500','missPt_MZp1000_MDM100','missPt_MZp1000_MDM1','missPt_MZp500_MDM1','missPt_MZp100_MDM1','missPt_MZp10_MDM1'] legtext_bar = ["M_{Z'}=1000 GeV,M_{#chi}=500 GeV","M_{Z'}=1000 GeV,M_{#chi}=100 GeV","M_{Z'}=1000 GeV,M_{#chi}=1 GeV","M_{Z'}=500 GeV,M_{#chi}=1 GeV",\ "M_{Z'}=100 GeV,M_{#chi}=1 GeV","M_{Z'}=10 GeV,M_{#chi}=1 GeV"] h_frame.SetMaximum(0.25) h_frame.SetAxisRange(0., 750.,"X") leg.Clear() info_bar = TLatex(0,0.255,'CMS') info_bar_2 = TLatex(750,0.255,'35.9 fb^{-1} (13 TeV)') info_bar.SetTextSize(0.03) info_bar_2.SetTextSize(0.03) info_bar_2.SetTextAlign(31) kOrange = 800 colorList = [kOrange-4,kOrange-1,kOrange+3,70,65,55] h_higgsPt_BarList = [] f = TFile('BaryonicZp_missPt.root') for i in range(6): hist = TH1F() hist = f.Get(histName_bar[i]) h_higgsPt_BarList.append(hist) for i in range(6): #h_higgsPt_BarList[i].SetLineColor(93-6*i) h_higgsPt_BarList[i].SetLineColor(colorList[i]) leg.AddEntry(h_higgsPt_BarList[i],legtext_bar[i]) h_frame.Draw('hist') for i in range(5,-1,-1):h_higgsPt_BarList[i].DrawNormalized('histsame') leg.Draw() info_bar.Draw() info_bar_2.Draw() c1.Print('Baryonic_higgsPt.pdf') f.Close() '''
def writeHistogram(dataFileName, columnName, histogramName, rootFileName): rootfile = TFile(rootFileName, 'UPDATE') histogramDescription = histogramName datafile = open(dataFileName, 'r') text = datafile.read() descrRegex = re.compile('# left_bin_size(?P<Entries>.*)') descriptionLine = descrRegex.search(text) #print descriptionLine.group() entries = re.finditer(r'(\w|-)+', descriptionLine.group('Entries')) entriesList = list() for match in entries: name = match.group() if (name != 'er'): entriesList.append(name) #print entriesList index = 0 if columnName in entriesList: index = entriesList.index(columnName) else: print "No entry called ", columnName print 'entries are: ', entriesList return False #print index doubleRegex = r'-?[0-9]*\.?[0-9]+(e(\+|-)\d+)?' dataLineString = r'^\s*(?P<bin>' + doubleRegex + ')' for i in range(0, index): # the index is 0-based dataLineString += r'\s*' dataLineString += doubleRegex # this is the normal entry dataLineString += r'\s*' dataLineString += doubleRegex # this is the error dataLineString += r'\s*' dataLineString += '(?P<value>' + doubleRegex + ')' # this is the normal entry dataLineString += r'\s*' dataLineString += '(?P<error>' + doubleRegex + ')' # this is the error dataLineString += r'.*$' dataLineRegex = re.compile(dataLineString, re.MULTILINE) data = re.finditer(dataLineRegex, text) bins = list() values = list() errors = list() for match in data: bin = match.group('bin') value = match.group('value') error = match.group('error') bins.append(float(bin)) values.append(float(value)) errors.append(float(error)) # print 'bin: ',bin,' value ',value,' err ',error #print bins #print values #print errors dx = bins[1] - bins[0] variableWidth = False for i in range(0, len(bins) - 1): if (abs((bins[i + 1] - bins[i]) / dx - 1) > 0.0001): #print "bin spacing is not constant: ",bins[i+1]-bins[i],'!=', dx #print "relative difference: ",abs((bins[i+1]-bins[i])/dx ) -1 variableWidth = True if variableWidth: print "Variable bin width!" b = array('d', bins) hist = TH1D(histogramName, histogramDescription, len(bins) - 1, b) else: hist = TH1D(histogramName, histogramDescription, len(bins) - 1, bins[0], bins[-1]) #print "bin 0: ",bins[0] ,"last bin: ", bins[-1], "nbr: ",len(bins)-1 for i in range(0, len(bins)): hist.SetBinContent(i + 1, values[i]) hist.SetBinError(i + 1, errors[i]) hist.Write() rootfile.Close()
class TreeWriter: def __init__(self, data): self.Data = data self.File = None self.Tree = None self.VectorBranches = self.init_vector_branches() self.RunFileName = 'runNumber.txt' self.RunNumber = self.load_run_number() self.ProgressBar = None def start_pbar(self, n): self.ProgressBar = ProgressBar(widgets=['Progress: ', Percentage(), ' ', Bar(marker='>'), ' ', ETA(), ' ', FileTransferSpeed()], maxval=n) self.ProgressBar.start() def load_run_number(self): if isfile(self.RunFileName): f = open(self.RunFileName) run_number = int(f.readline()) f.close() return run_number else: f = open(self.RunFileName, 'w') f.write('1') f.close() return 1 def save_run_number(self): f = open(self.RunFileName, 'w') f.write('{n}'.format(n=self.RunNumber + 1)) f.close() @staticmethod def init_vector_branches(): dic = OrderedDict([('col', vector('unsigned short')()), ('row', vector('unsigned short')()), ('adc', vector('short')())]) return dic def clear_vectors(self): for key in self.VectorBranches.keys(): self.VectorBranches[key].clear() def set_branches(self): for key, vec in self.VectorBranches.items(): self.Tree.Branch(key, vec) def write_tree(self): self.File = TFile('run{n}.root'.format(n=str(self.RunNumber).zfill(3)), 'RECREATE') self.Tree = TTree('tree', 'The source tree') self.set_branches() self.start_pbar(len(self.Data)) for i, ev in enumerate(self.Data): self.ProgressBar.update(i + 1) self.clear_vectors() for pix in ev.pixels: self.VectorBranches['col'].push_back(int(pix.column)) self.VectorBranches['row'].push_back(int(pix.row)) self.VectorBranches['adc'].push_back(int(pix.value)) self.Tree.Fill() self.ProgressBar.finish() self.File.cd() self.File.Write() self.File.Close() self.save_run_number()
if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % ( hist['channel'], outputFile.GetName()) currentDir.Delete(deleteString) if 'nbinsY' in hist: # only make a 2D histogram if the key "nbinsY" is defined h = TH2D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'], hist['nbinsY'], hist['yMin'], hist['yMax']) else: h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax']) h.Sumw2() # Needed to get weights correct. cut = TCut(hist['cutString']) ch.Draw(hist['varToPlot'] + ">>" + hist['histName'], cut) h.Write() outputFile.Close() print "Histogram " + hist[ 'histName'] + " has been added to " + condor_dir + "/" + dataset + ".root" #merge output if composite dataset for composite_dataset in composite_datasets: component_datasets_list = "" component_dataset_file_path = "" for component_dataset in composite_dataset_definitions[composite_dataset]: component_dataset_dir = "%s/%s" % (condor_dir, component_dataset) component_dataset_file_path = component_dataset_dir + ".root" if os.path.isfile(component_dataset_file_path): component_datasets_list += " " + component_dataset_file_path composite_dataset_dir = "%s/%s" % (condor_dir, composite_dataset) command = "mergeHists -p %s %s" % (composite_dataset_dir, component_datasets_list)
def parseGAT(goodList, theCut, dsNum, bkgIdx=None, saveMe=False): """ Accesses MGTWaveform objects directly and returns some arbitrary object. NOTE: Have to access individual TFile's because of this old stupid ROOT/MJSW bug: When the GetEntry command changes from file1->file2 in the loop: AttributeError: 'TChain' object has no attribute 'MGTWaveforms' (LAT data) AttributeError: 'TChain' object has no attribute 'event' (GAT data) """ from ROOT import gDirectory, TFile, TTree, TChain, MGTWaveform, MJTMSWaveform, GATDataSet # this is what we return baseDict = {ch:[] for ch in goodList} # create run number list runList = [] bkgList = ds.bkgRunsDS[dsNum][bkgIdx] for idx in range(0,len(bkgList),2): [runList.append(run) for run in range(bkgList[idx], bkgList[idx+1]+1)] # get paths to data gds = GATDataSet() gatPath = gds.GetPathToRun(runList[0], GATDataSet.kGatified) gIdx = gatPath.find("mjd_run") gatPath = gatPath[:gIdx] bltPath = gds.GetPathToRun(runList[0], GATDataSet.kBuilt) bIdx = bltPath.find("OR_run") bltPath = bltPath[:bIdx] # loop over files for run in runList: start = time.time() # load trees gatFile = TFile(gatPath+"mjd_run%d.root" % run) gatTree = gatFile.Get("mjdTree") bltFile = TFile(bltPath+"OR_run%d.root" % run) bltTree = bltFile.Get("MGTree") gatTree.AddFriend(bltTree) # create TEntryList gatTree.Draw(">>elist", theCut, "entrylist") eList = gDirectory.Get("elist") gatTree.SetEntryList(eList) if eList.GetN()==0: continue # loop over entries passing cuts for iList in range(eList.GetN()): entry = gatTree.GetEntryNumber(iList); gatTree.LoadTree(entry) gatTree.GetEntry(entry) nChans = gatTree.channel.size() numPass = gatTree.Draw("channel",theCut,"GOFF",1,iList) chans = gatTree.GetV1() chanList = list(set(int(chans[n]) for n in xrange(numPass))) # loop over hits passing cuts (channels in good list only) hitList = (iH for iH in range(nChans) if gatTree.channel.at(iH) in goodList and gatTree.channel.at(iH) in chanList) for iH in hitList: if dsNum==2 or dsNum==6: wf_downsampled = bltTree.event.GetWaveform(iH) wf_regular = bltTree.event.GetAuxWaveform(iH) wf = MJTMSWaveform(wf_downsampled,wf_regular) else: wf = bltTree.event.GetWaveform(iH) # now we can pretty much save or calculate anything we want chan = int(gatTree.channel.at(iH)) signal = wl.processWaveform(wf) baseline,_ = signal.GetBaseNoise() baseline = float("%.2f" % baseline) # kill precision to save on memory globalTime = int(latTree.globalTime.fSec) run = int(latTree.run) baseDict[chan].append(baseline,globalTime,run) print("Run %d, %d entries, %d passing cuts. %.2f sec." % (run, gatTree.GetEntries(), eList.GetN(), time.time()-start)) gatFile.Close() bltFile.Close() # optionally save the output if saveMe: with open("../data/parseGAT_ds%d.json" % dsNum, 'w') as fOut: json.dump(baseDict, fOut) return baseDict
def rewriteShapeFile(self, new_hist_dict): f = TFile(self.__shape_path, 'RECREATE') for key, hist in new_hist_dict.items(): hist.Write(key) f.Close()
class ZHTreeProducer(Analyzer): def beginLoop(self, setup): super(ZHTreeProducer, self).beginLoop(setup) self.rootfile = TFile('/'.join([self.dirName, 'tree.root']), 'recreate') self.tree = Tree('events', '') if hasattr(self.cfg_ana, 'recoil'): bookParticle(self.tree, 'recoil') if hasattr(self.cfg_ana, 'zeds'): bookZed(self.tree, 'zed') self.taggers = ['b', 'bmatch', 'bfrac'] for label in self.cfg_ana.particles: bookParticle(self.tree, label) var(self.tree, 'n_' + label) for label in self.cfg_ana.jet_collections: bookJet(self.tree, '{}_1'.format(label), self.taggers) bookJet(self.tree, '{}_2'.format(label), self.taggers) for label in self.cfg_ana.resonances: iso = False if 'zed' in label and not 'qq' in label: iso = True bookResonanceWithLegs(self.tree, label, iso) bookResonanceWithLegs(self.tree, 'genboson1') bookResonanceWithLegs(self.tree, 'genboson2') for label in self.cfg_ana.misenergy: bookParticle(self.tree, label) ## bookParticle(self.tree, 'otherptc') ## var(self.tree, 'n_otherptcs') var(self.tree, 'n_nu') var(self.tree, 'beta4_chi2') def process(self, event): self.tree.reset() if hasattr(self.cfg_ana, 'recoil'): recoil = getattr(event, self.cfg_ana.recoil) fillParticle(self.tree, 'recoil', recoil) if hasattr(self.cfg_ana, 'zeds'): zeds = getattr(event, self.cfg_ana.zeds) if len(zeds) > 0: zed = zeds[0] fillZed(self.tree, 'zed', zed) for label in self.cfg_ana.misenergy: misenergy = getattr(event, label) fillParticle(self.tree, label, misenergy) for label in self.cfg_ana.particles: ptcs = getattr(event, label) fill(self.tree, 'n_' + label, len(ptcs)) if len(ptcs): fillParticle(self.tree, label, ptcs[0]) for label in self.cfg_ana.jet_collections: jets = getattr(event, label) for ijet, jet in enumerate(jets): if ijet == 2: break fillJet(self.tree, '{label}_{ijet}'.format(label=label, ijet=ijet + 1), jet, self.taggers) for label in self.cfg_ana.resonances: resonances = getattr(event, label) if len(resonances) > 0: resonance = resonances[0] iso = False if 'zed' in label and not 'qq' in label: iso = True fillResonanceWithLegs(self.tree, label, resonance, iso) neutrinos = getattr(event, 'neutrinos', None) if neutrinos: fill(self.tree, 'n_nu', len(neutrinos)) for i, boson in enumerate(event.gen_bosons[:2]): fillResonanceWithLegs(self.tree, 'genboson{i}'.format(i=i + 1), boson) ## otherptcs = event.particles_not_zed ## fill(self.tree, 'n_otherptcs', len(otherptcs)) ## if len(otherptcs): ## fillParticle(self.tree, 'otherptc', otherptcs[0]) if hasattr(event, 'beta4_chi2'): fill(self.tree, 'beta4_chi2', event.beta4_chi2) self.tree.tree.Fill() def write(self, setup): self.rootfile.Write() self.rootfile.Close()
for iy in range(1, Ny + 1): r = math.sqrt(P[ix][iy][0] * P[ix][iy][0] + P[ix][iy][1] * P[ix][iy][1]) if r < R: C_tmp[ix][iy] = evolution(dt, dx, dy, D, C[ix][iy], C[ix + 1][iy], C[ix - 1][iy], C[ix][iy + 1], C[ix][iy - 1]) Cmap = TH2D("Cmap_t" + str(t), "Cmap_t" + str(t), Nx + 2, (-Nx / 2 - 1) * dx + dx / 2, (Nx / 2 + 1) * dx + dx / 2, Ny + 2, (-Ny / 2 - 1) * dy + dy / 2, (Ny / 2 + 1) * dy + dy / 2) for ix in range(0, Nx + 2): for iy in range(0, Ny + 2): C[ix][iy] = C_tmp[ix][iy] Px = P[ix][iy][0] Py = P[ix][iy][1] iBinX = Cmap_t0.GetXaxis().FindBin(Px) iBinY = Cmap_t0.GetYaxis().FindBin(Py) Cmap.SetBinContent(iBinX, iBinY, C[ix][iy]) Cmap.Write('Ct_' + str(t * dt)) print "Completed!" FileOutRoot.Close()
def MakeTheHistogram(channel,NormMC,NormQCD,ShapeQCD,chl,Binning,NormMCTT): signal = ['Codex_'] signalName = ['Codex_'] mass = [ '800', '900', '1000', '1100', '1200', '1300', '1400', '1500' ] # TOTMASS = ['800','900','1000','1100','1200','1300','1400','1500'] category = [""] JetScaleOut = ["_CMS_scale_jes"+"Down", "", "_CMS_scale_jes"+"Up"] # JetResolOut = ["_CMS_scale_jer"+"Down", "", "_CMS_scale_jer"+"Up"] JetResolOut = [""] METScaleOut = ["_CMS_scale_met_UES"+"Down", "", "_CMS_scale_met_UES"+"Up","_CMS_scale_met_JES"+"Down", "_CMS_scale_met_JES"+"Up"] Signal_Unc_TopPTRW = ["_CMS_top_pt_Reweighting"+"Up","_CMS_top_pt_Reweighting"+"Down"] myOut = TFile(FinalName[chl]+NormMC+".root" , 'RECREATE') # Name Of the output file for NameCat in category: print "starting NameCat=%s and channel=%s and HistoName=%s "%(NameCat, channel, NormMC) tDirectory= myOut.mkdir(channel + str(NameCat)) tDirectory.cd() for jscale in range(len(JetScale)): for jres in range(len(JetResol)): for mscale in range(len(METScale)): if jscale != 1 and mscale!=1 : continue if jscale != 1 and jres!=0 : continue if jres != 0 and mscale!=1 : continue ################################################ # Filling Signal ################################################ for sig in range(len(signal)): for m in range(len(mass)): print "===>", str(mass[m]) print "---------------------------------------------------> Processing Signal ", signal[sig]+str(mass[m]) tDirectory.cd() Name= str(signal[sig])+str(mass[m]) NameOut= str(signalName[sig]) +str(mass[m])+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale]) NormHisto=NormFile.Get("XXX") NormHisto.Scale(0.001) RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling SingleTop ################################################ print "---------------------------------------------------> Processing SingleTop" tDirectory.cd() Name= "SingleTop" NameOut= "SingleTop"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling VV ################################################ print "---------------------------------------------------> Processing VV" tDirectory.cd() Name= "VV" NameOut= "VV"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling TOP ################################################ print "---------------------------------------------------> Processing TOP" tDirectory.cd() Name= "TTJets" NameOut= "TT"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) print NormMCTT NormFile= _FileReturn(Name, channel,NameCat, NormMCTT, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") NormFileShape= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHistoShape=NormFileShape.Get("XXX") NormHistoShape.Scale(NormHisto.Integral()*1.0/NormHistoShape.Integral()) NormHistoShape.Scale(SF_TT()) RebinedHist= NormHistoShape.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ############### Systematics on Shape and Norm for To PT Reweighting #### if jscale==1 and mscale==1 and jres==0: for systTopRW in range(len(SystematicTopPtReWeight)): tDirectory.cd() HistogramNorm = NormMCTT HistogramShape = NormMC.replace("_LQMass","_LQMass"+SystematicTopPtReWeight[systTopRW]) NameOut= "TT"+str(Signal_Unc_TopPTRW[systTopRW]) NormFile= _FileReturn(Name, channel,NameCat, HistogramNorm, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") NormFileShape= _FileReturn(Name, channel,NameCat, HistogramShape, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHistoShape=NormFileShape.Get("XXX") NormHistoShape.Scale(NormHisto.Integral()*1.0/NormHistoShape.Integral()) NormHistoShape.Scale(SF_TT()) RebinedHist= NormHistoShape.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling ZTT ################################################ print "---------------------------------------------------> Processing ZTT" tDirectory.cd() Name= "DYJetsToLL" NameOut= "ZTT"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling W ################################################ print "---------------------------------------------------> Processing W" tDirectory.cd() Name="WJetsToLNu" NameOut= "W"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) NormFileWNoCor= _FileReturn(Name, channel,NameCat, NormMC.replace("","") +"", JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHistoWNoCor=NormFileWNoCor.Get("XXX") WNoCorNormaliztaion=NormHistoWNoCor.Integral() NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") NormHisto.Scale(WNoCorNormaliztaion/NormHisto.Integral()) NormHisto.Scale(SF_W()) RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling QCD ################################################ if jscale==1 and mscale==1 and jres==0: print "---------------------------------------------------> Processing QCD" tDirectory.cd() Name= "SingleTop" SingleTSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) SingleTSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) Name= "VV" VVSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) VVSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) Name= "TTJets" TTSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) TTSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) Name= "DYJetsToLL" ZTTSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) ZTTSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) Name= "WJetsToLNu" WSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) WSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) Name="Data" DataSampleQCDNorm= _FileReturn(Name, channel,NameCat, NormQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) DataSampleQCDShape= _FileReturn(Name, channel,NameCat, ShapeQCD, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) SingleTSampleQCDShapeHist=SingleTSampleQCDShape.Get("XXX") VVSampleQCDShapeHist=VVSampleQCDShape.Get("XXX") TTSampleQCDShapeHist=TTSampleQCDShape.Get("XXX") ZTTSampleQCDShapeHist=ZTTSampleQCDShape.Get("XXX") WSampleQCDShapeHist=WSampleQCDShape.Get("XXX") DataSampleQCDShapeHist=DataSampleQCDShape.Get("XXX") print "\n----> ##########\nlooseQCDShape before=", DataSampleQCDShapeHist.Integral() if (SingleTSampleQCDShapeHist) : DataSampleQCDShapeHist.Add(SingleTSampleQCDShapeHist, -1) if (VVSampleQCDShapeHist): DataSampleQCDShapeHist.Add(VVSampleQCDShapeHist, -1) DataSampleQCDShapeHist.Add(TTSampleQCDShapeHist, -1) DataSampleQCDShapeHist.Add(ZTTSampleQCDShapeHist, -1) DataSampleQCDShapeHist.Add(WSampleQCDShapeHist, -1) print "\n----> ##########\nlooseQCDShaoe after=", DataSampleQCDShapeHist.Integral() SingleTSampleQCDNormHist=SingleTSampleQCDNorm.Get("XXX") VVSampleQCDNormHist=VVSampleQCDNorm.Get("XXX") TTSampleQCDNormHist=TTSampleQCDNorm.Get("XXX") ZTTSampleQCDNormHist=ZTTSampleQCDNorm.Get("XXX") WSampleQCDNormHist=WSampleQCDNorm.Get("XXX") DataSampleQCDNormHist=DataSampleQCDNorm.Get("XXX") if (SingleTSampleQCDNormHist) : DataSampleQCDNormHist.Add(SingleTSampleQCDNormHist, -1) if (VVSampleQCDNormHist): DataSampleQCDNormHist.Add(VVSampleQCDNormHist, -1) DataSampleQCDNormHist.Add(TTSampleQCDNormHist, -1) DataSampleQCDNormHist.Add(ZTTSampleQCDNormHist, -1) DataSampleQCDNormHist.Add(WSampleQCDNormHist, -1) print "\n##########\nlooseQCDNORM after=", DataSampleQCDNormHist.Integral() FR_FitMaram=Make_Mu_FakeRate(channel) QCDEstimation=0 for bin in xrange(50,1000): value=DataSampleQCDNormHist.GetBinContent(bin) if value < 0 : value=0 FR= _FIT_Jet_Function(bin+1.5,FR_FitMaram) if FR> 0.9: FR=0.9 QCDEstimation += value * FR/(1-FR) print "\n##########\n QCDEstimation", QCDEstimation NameOut= "QCD"+str(JetScaleOut[jscale])+str(JetResolOut[jres])+str(METScaleOut[mscale]) DataSampleQCDShapeHist.Scale(QCDEstimation/DataSampleQCDShapeHist.Integral()) # The shape is from btag-Loose Need get back norm RebinedHist= DataSampleQCDShapeHist.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) ################################################ # Filling Data ################################################ if jscale==1 and mscale==1 and jres==0: print "---------------------------------------------------> Processing Data" tDirectory.cd() Name='Data' NameOut='data_obs' NormFile= _FileReturn(Name, channel,NameCat, NormMC, JetScale[jscale] , JetResol[jres] , METScale[mscale] ) NormHisto=NormFile.Get("XXX") RebinedHist= NormHisto.Rebin(len(Binning)-1,"",Binning) tDirectory.WriteObject(RebinedHist,NameOut) myOut.Close()
def apply_filters(filename_noise_psds, filename_templates, inputfilepath, series, filename_root): print('apply_filters') V = get_noise_psds(filename_noise_psds) templates, E_min, E_max, map_bins_part = get_templates_nxm( filename_templates) man = OFManagerNxM(DT, T_PRE, templates, V, calc_r, calc_theta, E_min, E_max, map_bins_part) dr = DataReader() dr.OpenFile(inputfilepath, series, 0) tm_nxm = tree_manager('NxM') tm_nxm.Branch('t0') tm_nxm.Branch('chisq') tm_nxm.Branch('E') event_count = 0 while dr.LoadEvent(trigger='Trigger'): S = dr.GetTraces() dataS = np.asarray(S) dataS = np.sum(dataS, axis=0) if (np.mean(dataS[0:3000]) > np.mean(dataS[3000:10000]) and np.mean(dataS[0:3000]) > np.mean(dataS[10000:15000]) and np.mean(dataS[0:3000]) > np.mean(dataS[30000:32000])): continue if (np.mean(dataS[0:3000]) > 1.05 * np.mean(dataS[30000:32000])): continue if (1.05 * np.mean(dataS[0:3000]) < np.mean(dataS[30000:32000])): continue dataS = dataS * 1E7 peaks, properties = find_peaks(dataS.transpose(), prominence=1, width=20) width_half = peak_widths(dataS.transpose(), peaks, rel_height=0.5) if (len(peaks) == 0 or len(peaks) > 1 or any(width_half) > 2000): continue tmp_S = np.array(S) avg = np.mean(tmp_S[:, 0:5000], axis=1) for i in range(len(S)): S[i] = S[i] - avg[i] result = man.ProcessEvent(S) event_count += 1 if event_count % STEP_MONITOR == 0: print('Event', event_count) man.Draw(PATH + '/png', event_count) if type(result) == dict: tm_nxm['t0'] = result['t0'] tm_nxm['chisq'] = result['chisq'] tm_nxm['E'] = result['E'] else: tm_nxm['t0'] = -999999.0 tm_nxm['chisq'] = -999999.0 tm_nxm['E'] = -999999.0 tm_nxm.Fill() dr.CloseFile() filepointer = TFile(filename_root, 'recreate') tm_nxm.Write() filepointer.Close()
def main(): # configure and run pathROOT_1 = sys.argv[1] pathROOT_2 = sys.argv[2] pathROOT_3 = sys.argv[3] pathROOT_4 = sys.argv[4] f1 = TFile(pathROOT_1, 'read') f2 = TFile(pathROOT_2, 'read') f3 = TFile(pathROOT_3, 'read') f4 = TFile(pathROOT_4, 'read') if not os.path.exists('sumupsensor.root'): f5 = TFile('sumupsensor.root', 'new') else: f5 = TFile('sumupsensor.root', 'recreate') KeysToNamesOfHistos_1 = f1.GetListOfKeys() KeysToNamesOfHistos_2 = f2.GetListOfKeys() KeysToNamesOfHistos_3 = f3.GetListOfKeys() KeysToNamesOfHistos_4 = f4.GetListOfKeys() Keys = [] histos = [] histo_01 = [] histo_07 = [] histo_14 = [] histo_19 = [] appendHistos(KeysToNamesOfHistos_1, f1, histos) appendHistos(KeysToNamesOfHistos_2, f2, histos) appendHistos(KeysToNamesOfHistos_3, f3, histos) appendHistos(KeysToNamesOfHistos_4, f4, histos) integral_pr = 0.0 integral_ne = 0.0 integral_ka = 0.0 integral_pi = 0.0 for histo in histos: if "pr" in histo.GetName(): histo_01.append(histo) integral_pr += histo.Integral("width") elif "ne" in histo.GetName(): histo_07.append(histo) integral_ne += histo.Integral("width") elif "ka" in histo.GetName(): histo_14.append(histo) integral_ka += histo.Integral("width") elif "pi" in histo.GetName(): histo_19.append(histo) del histos[:] binX = histo_01[1].GetNbinsX() startX = histo_01[1].GetXaxis().GetXmin() endX = histo_01[1].GetXaxis().GetXmax() data_points_01 = [0] * binX data_points_07 = [0] * binX data_points_14 = [0] * binX data_points_19 = [0] * binX sumHistos(histo_01, binX, data_points_01) sumHistos(histo_07, binX, data_points_07) sumHistos(histo_14, binX, data_points_14) sumHistos(histo_19, binX, data_points_19) histos.append(TH1F("NEQ_01", "NEQ_01", binX, startX, endX)) histos.append(TH1F("NEQ_07", "NEQ_07", binX, startX, endX)) histos.append(TH1F("NEQ_14", "NEQ_14", binX, startX, endX)) histos.append(TH1F("NEQ_19", "NEQ_19", binX, startX, endX)) for index in xrange(binX): histos[0].SetBinContent(index + 1, data_points_01[index]) for index in xrange(binX): histos[1].SetBinContent(index + 1, data_points_07[index]) for index in xrange(binX): histos[2].SetBinContent(index + 1, data_points_14[index]) for index in xrange(binX): histos[3].SetBinContent(index + 1, data_points_19[index]) for histo in histos: histo.Write() f5.Close() f4.Close() f3.Close() f2.Close() f1.Close()
def makePlots(): inputFile = TFile('CutsPlots.root','READ') histos_data = dict() histos_MC = dict() keys = ['Y1S_nsigma', 'probFit1S', 'photon_pt', 'photon_eta', 'dimuon_pt', 'dimuon_rapidity', 'dz', 'invm1S'] for key in keys: histos_data[key] = inputFile.Get(key+'_data') histos_MC[key] = inputFile.Get(key+'_MC') titles = dict( Y1S_nsigma = 'Y1S_nsigma;Number of theoric standad deviation from #Upsilon(1S);', probFit1S = 'probFit1S;fit probability based on #chi^{2} over number dof;', photon_pt = 'photon p_{T};p_{T} [GeV];', photon_eta = 'photon_eta;#eta;', dimuon_pt = '#Upsilon p_{T};p_{T} [GeV];', dimuon_rapidity = '#Upsilon rapidity;y;', dz = '|dz|;|dz| [cm];', invm1S = 'Q-Value;m_{#mu#mu#gamma} - m_{#mu#mu} + m_{#Upsilon}^{PDG};' ) logY = dict( Y1S_nsigma = 0, probFit1S = 1, photon_pt = 0, photon_eta = 0, dimuon_pt = 0, dimuon_rapidity = 0, dz = 0, invm1S = 0 ) for histo in histos_data.values() + histos_MC.values(): if(histo.Integral() != 0): histo.Scale(1./histo.Integral()) for histo in histos_data.values(): histo.SetLineColor(ROOT.kGreen+2) histo.SetLineWidth(2) # histo.SetMarkerColor(ROOT.kGreen+2) # histo.SetMarkerStyle(21) for histo in histos_MC.values(): histo.SetLineColor(ROOT.kBlue) histo.SetLineWidth(2) # histo.SetMarkerColor(ROOT.kBlue) # histo.SetMarkerStyle(20) canvas=TCanvas("canvas","canvas") canvas.Print(outputFile_name+"[") #apre file .ps for key in keys: canvas.SetLogy(logY[key]) hs = THStack('hs', titles[key]) hs.Add(histos_data[key]) hs.Add(histos_MC[key]) hs.Draw('nostack') leg=canvas.BuildLegend(.8,.95,.92,.8); leg.SetFillColor(0); leg.Draw("SAME"); canvas.Update() canvas.Print(outputFile_name) canvas.Print('CutsPlots/'+key+'.png') canvas.Print(outputFile_name+"]") #chiude file .ps inputFile.Close()
def mix(path_in, path_out, mode, ecms, patch, sample): omega = array('d', 3 * [999.]) if ecms > 4316: D1_2420_path = './txts/xs_D1_2420_' + patch + '.txt' with open(D1_2420_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[0] = fargs[4] except: ''' ''' else: omega[0] = 0. DDPIPI_path = './txts/xs_DDPIPI_' + patch + '.txt' with open(DDPIPI_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[1] = fargs[4] except: ''' ''' psipp_path = './txts/xs_psipp_' + patch + '.txt' with open(psipp_path, 'r') as f: for line in f.readlines(): if '#' in line: line = line.strip('#') try: fargs = map(float, line.strip().strip('\n').split()) if fargs[0] == ecms: omega[2] = fargs[4] except: ''' ''' tot = 0 for i in xrange(len(omega)): tot += omega[i] for i in xrange(len(omega)): omega[i] = omega[i] / tot n = 0 if ecms <= 4316: n += 1 f_out = TFile(path_out[0], 'RECREATE') t_out = TTree('save', 'save') if sample == 'raw': m_rawm_D = array('d', [999.]) if sample == 'raw_after': m_rm_Dpipi = array('d', [999.]) if sample == 'raw': t_out.Branch('rawm_D', m_rawm_D, 'm_rawm_D/D') if sample == 'raw_after': t_out.Branch('rm_Dpipi', m_rm_Dpipi, 'm_rm_Dpipi/D') for i in xrange(len(path_in)): try: f_in = TFile(path_in[i]) t_in = f_in.Get('save') entries = t_in.GetEntries() logging.info(mode[i] + ' entries :' + str(entries)) except: logging.error(path_in[i] + ' is invalid!') sys.exit() print '--> Begin to process file: ' + path_in[i] nentries = t_in.GetEntries() for ientry in xrange(int(nentries * omega[n])): t_in.GetEntry(ientry) if t_in.m_rawm_D > 0.: if sample == 'raw': m_rawm_D[0] = t_in.m_rawm_D if sample == 'raw_after': m_rm_Dpipi[0] = t_in.m_rm_Dpipi t_out.Fill() n += 1 f_out.cd() t_out.Write() f_out.Close() print '--> End of processing file: ' + path_out[0]
if systematics == '': myhist = rebinnedHist[iprocess].Clone("nominal") else: myhist = rebinnedHist[iprocess].Clone("%s%s" % (systematics, mylevel)) # if systematics in ["PU"]: # myNominalHist = myfile.Get(mydir+"nominal") # valNominal = myNominalHist.Integral() # val = myhist.Integral() # print "nominal", valNominal, " ==> ", "syst",val # myhist.Scale(valNominal/val) # print "normalized", myhist.Integral() if myfile.GetDirectory(mydir): gDirectory.cd(mydir) if systematics == '': gDirectory.Delete("nominal;*") else: gDirectory.Delete("%s%s;*" % (systematics, mylevel)) myhist.Write() else: gDirectory.mkdir(mydir) gDirectory.cd(mydir) if systematics == '': gDirectory.Delete("nominal;*") else: gDirectory.Delete("%s%s;*" % (systematics, mylevel)) myhist.Write() print "%s%s.root" % (plotDirectory, "ttgamma_Prefit") myfile.Close()
histMaxX = bsTauHist.GetBinCenter(bsTauHist.GetMaximumBin()) print "HISTMAXX =", histMaxX, "HISTMAXY =", histMaxY theCanvas.Close() theCanvas = TCanvas() expFunc = TF1("expFunc", "%f*exp(-(x-%f)*[0])" % (histMaxY, histMaxX), histMaxX, maxExpFunc) tree1.Fit("expFunc", SEED, "%s>%f" % (SEED, histMaxX), "", "ILL") #tree1.Draw("SEED,"%s>=0" % SEED,"","ILL"); #tree1.Draw("SEED","%ss_ID>0&&%s>0" % (SEED[0],SEED)); #bsTauHist.Fit("expFunc","IL","",histMaxX+0.00001,maxExpFunc); #<-??? (ILL) #bsTauHist.Draw(); currentTime = time.time() os.chdir(os.environ['B2DXFITTERSROOT'] + '/tutorial') if (not os.path.exists('BsDsTauPlots')): os.mkdir("BsDsTauPlots") os.chdir('BsDsTauPlots') theCanvas.SaveAs("%s_Hist_%f.pdf" % (SEED, currentTime)) raw_input("Press Enter to continue") in_file.Close() #true Bs lifetime -> 1.47 * 10^-12 s -> 1/lifetime = 6.80 * 10^11 #true Ds lifetime -> 5.0 * 10^-13 s -> 1/lifetime = 2.0 * 10^12
def run( options, args ): from HltMonitoring import Utils run_nr = int( args[ 1 ] ) filename = "monitor_%d.root" % run_nr n_processes = options.Processes run_info = Utils.run_info( run_nr ) import pprint pprint.pprint(run_info) input_lists = configureInput( run_info, n_processes, options ) if not input_lists: return -1 evtMax = -1 if options.EvtMax != -1: evtMax = options.EvtMax / n_processes config = dict() config[ 'EvtMax' ] = evtMax config[ 'DDDBtag' ] = run_info['dddbTag'] config[ 'CondDBtag' ] = run_info['conddbTag'] config[ 'DataType' ] = data_type(run_info) config[ 'Run' ] = run_nr from HltMonitoring import Monitors monitors = dict( [ ( n, None ) for n in args[ 0 ].split( ':' ) ] ) for mon in monitors.keys(): try: monitors[ mon ] = getattr( Monitors, mon ) except AttributeError: print "Unknown monitor type: ", mon return -1 monitor = None combined = False if len( monitors ) == 1: monitor = monitors.items()[ 0 ][ 1 ]( config ) else: monitor = getattr( Monitors, "Combined" ) config[ 'monitors' ] = copy( monitors ) monitor = monitor( config ) config.update( monitor.config() ) wrappers = [] running = {} for i in xrange( n_processes ): cfg = copy( config ) cfg[ 'Input' ] = input_lists[ i ] # Put the options into a dictionary wrappers += [ ProcessWrapper( i, monitor.child_type(), 'monitor_%d' % i, cfg, options.Debug ) ] if not options.Debug: cwd = os.path.realpath( os.path.curdir ) dirname = os.path.join( cwd, '%d' % run_nr ) if not os.path.exists( dirname ): os.mkdir( dirname ) for wrapper in wrappers: wrapper.condition().acquire() wrapper.start() for wrapper in wrappers: wrapper.condition().wait() wrapper.condition().release() print "Initialised process ", wrapper.key() running[ wrapper.key() ] = wrapper from ROOT import TFile root_file = TFile( filename, 'recreate' ) hists = {} n_evt = 0 counter = 0 while True: ready = None try: ( ready, [], [] ) = select.select( running.values(), [], [] ) for wrapper in ready: info = wrapper.getData( False ) if type( info ) == type( '' ): # Message if info == 'DONE': key = wrapper.key() print "Process %d is done" % key running.pop( key ) continue n_evt += 1 counter += 1 # "Decode" the received info monitor.fill( info ) if counter >= 10000: print "Received event %d" % n_evt counter = 0 if len( running ) == 0: print "Done; processed %d events" % n_evt break except select.error: continue # join all the processes for wrapper in wrappers: wrapper.join() histograms = {} if len( monitors ) == 1: histograms[ monitors.keys()[ 0 ] ] = monitor.histograms() else: histograms = monitor.histograms() for name in monitors.keys(): root_dir = root_file.mkdir( name ) for histo in sorted(histograms[ name ].values(), key = lambda h: h.GetName()): root_dir.WriteObject( histo, histo.GetName() ) root_file.Close() return 0
def main(): f = open(filelistStr, "r") rtfileoutput = TFile(rtfileoutputStr, "recreate") waveDir = rtfileoutput.mkdir("Waveforms") resultsDir = rtfileoutput.mkdir("Results") # afterpulse counting num_afterpulse_events = [0, 0, 0, 0] afterpulse_probability = [0, 0, 0, 0] # prepare for histograms hAmplitude_list = [] hAmplitudeBin_list = [] hWave_list = [] hPedMean_list = [] hPedWidth_list = [] hFinalCharge_list = [] hNbOfPulses_list = [] hPulseStartTime_list = [] # start time of the pulse # time distribution of the pulses that are not due to fiber trigger (ie., # dark pulses or else) hPulseTimeDist_list = [] # time distribution of the pulses that are not due to fiber trigger (ie., # dark pulses or else) hPulseWidth_list = [] hPulseAmplitudeVsTime_list = [ ] # 2D histogram: pulse amplitude vs. pulse time bin # 2D histogram: pulse amplitude vs. pulse time bin hPulseAmplitudeVsWidth_list = [] hWaveAvg_list = [] # average of raw waveforms # special test on time difference between the first channel hTimeDiff = TH1F("hTimeDiff", "Time diference", 100, -10, 10) # unit in ns hTimeDiff.SetXTitle("Time difference (ns)") hTimeDiff.SetYTitle("N") for i in range(0, NCH, 1): # will keep a few waveforms name = "Wave_" + str(i) hist = TH1F(name, "", NSamples, 0, NSamples) hist.SetXTitle("Sample number") hist.SetYTitle("Amplitude (mV)") hist.SetLineColor(i + 1) hWave_list.append(hist) # will keep average waveforms name = "WaveAvg_" + str(i) hist = TH1F(name, "", NSamples, 0, NSamples) hist.SetXTitle("Sample number") hist.SetYTitle("Amplitude (mV)") hist.SetLineColor(i + 1) hWaveAvg_list.append(hist) # histogram of pulse baseline name = "PedMean_" + str(i) hist = TH1F(name, "", 1000, -5, 5) hist.SetXTitle("Amplitude (mv)") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hPedMean_list.append(hist) # histogram of pulse baseline width name = "PedWidth_" + str(i) hist = TH1F(name, "", 1000, -5, 5) hist.SetXTitle("Amplitude (mV)") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hPedWidth_list.append(hist) # pulse charge due to fiber triggers, unit converted to fC name = "FinalCharge_" + str(i) # would be best to have different settings for LED on vs LED off if ledStatus == False: hist = TH1F(name, "", 1000, -5, 20) else: hist = TH1F(name, "", 2000, -5, 200) hist.SetXTitle("Charge (pC)") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hFinalCharge_list.append(hist) # pulse amplitude, or nimimum (because of negative pulse) name = "PulseAmplitude_" + str(i) hist = TH1F(name, "", 500, 0, 500) hist.SetXTitle("Amplitude (mV)") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hAmplitude_list.append(hist) # pulse width, or nimimum (because of negative pulse) name = "PulseWidth_" + str(i) hist = TH1F(name, "", 100, 0, 100) hist.SetXTitle("Pulse width (ns)") hist.SetYTitle("Counts") # hist.SetLineColor(i+1) hPulseWidth_list.append(hist) # time bin where pulse amplitude is found, used to determine charge # integration region name = "PulseAmplitudeBin_" + str(i) hist = TH1F(name, "", 200, 100, 300) hist.SetXTitle("Amplitude bin number") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hAmplitudeBin_list.append(hist) # number of pulses found in the waveform not in the fiber trigger # region name = "NbOfPulses_" + str(i) hist = TH1F(name, "", 20, 0, 20) hist.SetXTitle("Number of pulses in a 20 #mus window") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hNbOfPulses_list.append(hist) # pulse start time, only choose those pulses above threshold name = "PulseStartTime_" + str(i) hist = TH1F(name, "", 200, 100, 300) hist.SetXTitle("Pulse start time bin (1.6 ns/bin)") hist.SetYTitle("Counts") # hist.SetLineColor(i+1) hPulseStartTime_list.append(hist) # pulse time distributions for those pulses are 100 ns later than the # fiber triggered pulse name = "PulseTimeDist_" + str(i) hist = TH1F(name, "", NSamples / 10, 0, NSamples) hist.SetXTitle("Pulse time bin (16 ns/bin)") hist.SetYTitle("Counts") hist.SetLineColor(i + 1) hPulseTimeDist_list.append(hist) # pulse time distributions for those pulses are 100 ns later than the # fiber triggered pulse name = "PulseAmpVsTimeBin_" + str(i) hist = TH2F(name, "", NSamples / 10, 0, NSamples, 500, 0, 500) hist.SetXTitle("Pulse time bin") hist.SetYTitle("Pulse Amplitude (mV)") hist.SetLineColor(i + 1) hPulseAmplitudeVsTime_list.append(hist) # pulse time distributions for those pulses are 100 ns later than the # fiber triggered pulse name = "PulseAmpVsWidth_" + str(i) hist = TH2F(name, "", 100, 0, 100, 500, 0, 500) hist.SetXTitle("Pulse time bin") hist.SetYTitle("Pulse Amplitude (mV)") hPulseAmplitudeVsWidth_list.append(hist) # process the waveforms baseline_mean = 0.0 baseline_width = 0.0 threshold = 0.0 sumwave = [[0 for x in range(NSamples)] for y in range(NCH)] # special test for time diff flag1 = [False for x in range(Nwaves)] time1 = [0 for x in range(Nwaves)] flag2 = [False for x in range(Nwaves)] time2 = [0 for x in range(Nwaves)] for ch in range(NCH): for waveNb in range(Nwaves): # flag to determine whether there was afterpulse afterpulse_flag = False afilename = f.readline().rstrip() awave = np.asarray(decode_wfm(afilename)) baseline_mean = np.average(awave[NSamples - 1000:NSamples]) baseline_width = np.std(awave[NSamples - 1000:NSamples]) threshold = baseline_mean - Nsigma * baseline_width hPedMean_list[ch].Fill(baseline_mean) hPedWidth_list[ch].Fill(baseline_width) TimeBinOfAmplitude = np.argmin(awave[70:800]) + 70 if ch == 0 and awave[TimeBinOfAmplitude] < threshold: flag1[waveNb] = True time1[waveNb] = TimeBinOfAmplitude if ch == 1 and awave[TimeBinOfAmplitude] < threshold: flag2[waveNb] = True time2[waveNb] = TimeBinOfAmplitude # get charge integration about the amplitude: 20.8 ns before to 36.8 ns after the amplitude # sumcharge = 0.0 # if TimeBinOfAmplitude>=168 and TimeBinOfAmplitude<=180: sumcharge = np.sum(awave[TimeBinOfAmplitude - 13:TimeBinOfAmplitude + 23]) pulsestartbin = TimeBinOfAmplitude while awave[ pulsestartbin] < threshold and pulsestartbin >= TimeBinOfAmplitude - 20: pulsestartbin -= 1 hPulseStartTime_list[ch].Fill(pulsestartbin - 1) # else: # sumcharge = np.sum(awave[172-13:172+23]) # convert charge involving the 50 Ohm impedance fC = (sumcharge - baseline_mean * (23 + 13 + 1)) * QFactor # if awave[TimeBinOfAmplitude] < threshold: hAmplitude_list[ch].Fill( -1000.0 * (awave[TimeBinOfAmplitude] - baseline_mean)) hAmplitudeBin_list[ch].Fill(TimeBinOfAmplitude) # print TimeBinOfAmplitude, awave[TimeBinOfAmplitude] hFinalCharge_list[ch].Fill(-1.0 * fC) # search for number of pulses peakindex = peakutils.peak.indexes(-1.0 * awave[0:NSamples], thres=-1.0 * threshold, min_dist=31, thres_abs=True) for pulse_i in range(len(peakindex)): # skip pulse if it is below threshold in magnitude if awave[peakindex[pulse_i]] > threshold: continue hPulseTimeDist_list[ch].Fill( peakindex[pulse_i]) # +TimeBinOfAmplitude+200 hPulseAmplitudeVsTime_list[ch].Fill( peakindex[pulse_i], -1000.0 * (awave[peakindex[pulse_i]] - baseline_mean)) # determine the pulse width thisindex = peakindex[pulse_i] thiswidth = 0 while awave[thisindex] <= threshold and thisindex >= 2: # print pulse_i, ch, thiswaveform[ch][thisindex], # threshold[ch] thiswidth += 1 thisindex -= 1 # if thisindex<=1: # print eventNb, ch, pulse_i, thiswaveform[ch][thisindex] thisindex = peakindex[pulse_i] while awave[thisindex + 1] <= threshold and thisindex <= NSamples - 3: thiswidth += 1 thisindex += 1 # if thisindex>=NSamples-2: # print eventNb, ch, pulse_i, thiswaveform[ch][thisindex] hPulseWidth_list[ch].Fill((thiswidth + 2) * 2) hPulseAmplitudeVsWidth_list[ch].Fill( (thiswidth + 2) * 2, -1000.0 * (awave[peakindex[pulse_i]] - baseline_mean)) # determine whether this occurred after an LED pulse if peakindex[pulse_i] > afterpulse_cut_index: # if so, trigger the afterpulse flag afterpulse_flag = True if waveNb % 100 == 0: # print ch, waveNb, afilename for bin in range(0, NSamples, 1): hWave_list[ch].SetBinContent( bin + 1, -1000.0 * (awave[bin] - baseline_mean)) # hWave_list[ch].SetBinContent(bin+1,awave[bin]) waveDir.cd() lenname = int(len(afilename)) newName = "Wave_Ch_" + \ str(ch) + "_" + str(waveNb) + \ afilename[lenname - 4 - 17:lenname - 4] titleName = "Evt " + str(waveNb) + \ "_Ch" + str(ch) + "_PulseBins_" for pulse_i in range(len(peakindex)): titleName += str(peakindex[pulse_i]) titleName += "_" hWave_list[ch].SetName(newName) hWave_list[ch].SetTitle(titleName) hWave_list[ch].SetEntries(NSamples) hWave_list[ch].Write() hNbOfPulses_list[ch].Fill(len(peakindex)) # average of raw waveforms for each PMT for bin in range(0, NSamples, 1): sumwave[ch][bin] += 1000.0 * (awave[bin] - baseline_mean) # if afterpulse flag has been triggered, mark that an afterpulse occurred if afterpulse_flag == True: num_afterpulse_events[ch] += 1.0 # calculate afterpulse probability for the channel afterpulse_probability[ch] = num_afterpulse_events[ch] / Nwaves for ch in range(NCH): for bin in range(NSamples): hWaveAvg_list[ch].SetBinContent(bin + 1, sumwave[ch][bin] / Nwaves) waveDir.cd() hWaveAvg_list[ch].Write() for waveNb in range(Nwaves): if flag1[waveNb] is True and flag2[waveNb] is True: hTimeDiff.Fill(time1[waveNb] - time2[waveNb]) f.close() # write results resultsDir.cd() for i in range(0, NCH, 1): # fit the charge distributions with Poisson distribution plus # exponential background hFinalCharge_list[i].Write() for i in range(0, NCH, 1): hAmplitude_list[i].Write() for i in range(0, NCH, 1): hAmplitudeBin_list[i].Write() for i in range(0, NCH, 1): hPedMean_list[i].Write() for i in range(0, NCH, 1): hPulseStartTime_list[i].Write() for i in range(0, NCH, 1): hPedWidth_list[i].Write() for i in range(0, NCH, 1): hPulseTimeDist_list[i].Write() print "number of pulses: ", hPulseTimeDist_list[i].GetEntries() for i in range(0, NCH, 1): hPulseWidth_list[i].Write() for i in range(0, NCH, 1): hPulseAmplitudeVsTime_list[i].Write() for i in range(0, NCH, 1): hPulseAmplitudeVsWidth_list[i].Write() for i in range(0, NCH, 1): hNbOfPulses_list[i].Write() # write afterpulse probabilities for i in range(0, NCH, 1): print "afterpulse probability: ", afterpulse_probability[i] hTimeDiff.Write() rtfileoutput.Close()
totalRateHist.GetXaxis().SetTitle("p_{t}^{" + objectName + "}") totalRateHist.GetXaxis().SetRangeUser(5, 500) totalRateHist.GetYaxis().SetTitle("Rate [Hz]") barrelRateHist.GetXaxis().SetTitle("p_{t}^{" + objectName + "}") barrelRateHist.GetXaxis().SetRangeUser(5, 500) barrelRateHist.GetYaxis().SetTitle("Rate [Hz]") endcapRateHist.GetXaxis().SetTitle("p_{t}^{" + objectName + "}") endcapRateHist.GetXaxis().SetRangeUser(5, 500) endcapRateHist.GetYaxis().SetTitle("Rate [Hz]") normalisedRatePlotFile.cd() totalRateHist.Write() barrelRateHist.Write() endcapRateHist.Write() nonNormalisedRatePlotFile.Close() print "NORMALISING THE RATE PLOT TO OBTAIN THE TRIGGER PASS PROBABILITY FOR MINBIAS AND PU140 EVENTS" nonNormalisedRatePlotFile = TFile("" + saveFolder + "/" + sampleRateEstimation + "_RatePlots_NotNormalised.root") totalRateHist = nonNormalisedRatePlotFile.Get(objectName + "TriggerRate") ppPassProbabilityHistogram = totalRateHist.Clone(objectName + "PPPassProbabilityHistogram") ppPassProbabilityHistogram.Scale(1./numberOfDelphesEvents) passProbabilityFile.cd() eventPassProbabilityHistogram = ppPassProbabilityHistogram.Clone(objectName + "EventPassProbabilityHistogram") for x in xrange(1, eventPassProbabilityHistogram.GetNbinsX()+1): ppPassProbability = ppPassProbabilityHistogram.GetBinContent(x) eventPassProbability = 1 - (1 - ppPassProbability)**averagePileUp eventPassProbabilityHistogram.SetBinContent(x, eventPassProbability)
''' f.ls() f.Get(channel).ls() f.Get(channel + '/' + process).ls() f.Get(channel + '/' + process + '/' + target_sys).ls() f.Get(channel + '/' + process + '/' + replacement_sys).ls() # ROOT could not get the following target/replacement histograms # this .ls() sequence was for debuggiing # but now it works # ROOT craft histo_target = f.Get('%s/%s/%s/%s' % (channel, process, target_sys, '_'.join( [channel, process, target_sys, distr]))) histo_replacement = f.Get('%s/%s/%s/%s' % (channel, process, replacement_sys, '_'.join( [channel, process, replacement_sys, distr]))) histo_target.Print() histo_replacement.Print() # loop through each bin and substitute value for bini in range(histo_target.GetSize()): repl_content = histo_replacement.GetBinContent(bini) repl_error = histo_replacement.GetBinError(bini) histo_target.SetBinContent(bini, repl_content) histo_target.SetBinError(bini, repl_error) f.Write("", TFile.kOverwrite) f.Close()
def runplik(file_name, sample_name): histograms = dict() for i in variables.getname(): #Nbin, minbin, maxbin = variables.getrange(i); bins = variables.getrange(i) buff = {} for ii in cuts.cuts: #buff[ii.getname()] = TH1F(i+ii.getname(), i, Nbin, minbin, maxbin); buff[ii.getname()] = TH1F(i + ii.getname(), i, len(bins) - 1, bins) histograms[i] = buff # open the file f1 = TFile(file_name) p1 = f1.Get('m2n/eventTree') entries = p1.GetEntriesFast() mu = WmuCollection() p1.SetBranchAddress('wmu', mu) tau = WtauCollection() p1.SetBranchAddress('wtau', tau) pair = WpairCollection() p1.SetBranchAddress('wpair', pair) jets = WjetCollection() p1.SetBranchAddress('wjet', jets) evt = Wevent() p1.SetBranchAddress('wevent', evt) genweightsum = 0 ztautauSF = 1 if 'DY' not in sample_name else 0.79 wjetsSF = 1 if 'WJets' not in sample_name else 0.76 tauidSF = 0.9 for jentry in xrange(entries): p1.GetEntry(jentry) NPU = evt.npu() if (NPU >= 0 and NPU < 80 and not 'Run' in sample_name): PUWeight = pu.weight[NPU] elif NPU >= 80 and not 'Run' in sample_name: PUWeight = 0 else: PUWeight = 1 #genweight = math.copysign(1, evt.genevtweight()); genweight = evt.genevtweight() #PUWeight=1. #<----------wywalic genweightsum += genweight if mu.size() != 0: goodjets = [] for j in jets: dphim = abs(j.phi() - mu[0].phi()) dphit = abs(j.phi() - tau[0].phi()) if dphim > pi: dphim -= (2 * pi) if dphit > pi: dphit -= (2 * pi) dRm = sqrt((j.eta() - mu[0].eta())**2 + dphim**2) dRt = sqrt((j.eta() - tau[0].eta())**2 + dphit**2) if j.pt() >= 30 and dRm > 0.5 and dRt > 0.5 and abs( j.eta()) < 4.7: goodjets.append(j) for ii in cuts.cuts: if ii.calculate(mu[0], tau[0], pair[0], evt, goodjets): for k, v in variables.getvariable(mu[0], tau[0], pair[0], evt, goodjets).items(): histograms[k][ii.getname()].Fill( v, genweight * PUWeight * ztautauSF * wjetsSF * weights.mutotaufakerateSF(tau[0].eta()) * tauidSF * weights.etotaufakerateSF(tau[0].eta())) outfilename = file_name.replace('.root', sample_name + '.root') outfile = TFile(outfilename, 'RECREATE') for k in histograms.keys(): for kk in histograms[k].keys(): histograms[k][kk].Write() geninfo = f1.Get('ininfo/evtweight').Clone() geninfo.Write() outfile.Write() outfile.Close() file_ = open('./root/' + sample_name + '.txt', 'a+') file_.write(sample_name + file_name + '; entris: ' + str(entries) + '; genweightsum: ' + str(genweightsum) + '\n') file_.close() return outfilename
def limit(): method = '' channel = "bb" particleP = "Z'" particle = channel multF = ZPTOBB THEORY = ['A1', 'B3'] suffix = "_"+BTAGGING if ISMC: suffix += "_MC" if SY: suffix += "_comb" #if method=="cls": suffix="_CLs" if SY: filename = "./combine/limits/MANtag_study/" + BTAGGING + "/combined_run2/"+ YEAR + "_M%d.txt" else: filename = "./combine/limits/MANtag_study/" + BTAGGING + "/"+ YEAR + "_M%d.txt" if CATEGORY!="": filename = filename.replace(BTAGGING + "/", BTAGGING + "/single_category/"+CATEGORY+"_") suffix += "_"+CATEGORY if ISMC: filename = filename.replace(".txt", "_MC.txt") mass, val = fillValues(filename) #print "mass =",mass #print "val =", val Obs0s = TGraph() Exp0s = TGraph() Exp1s = TGraphAsymmErrors() Exp2s = TGraphAsymmErrors() Sign = TGraph() pVal = TGraph() Best = TGraphAsymmErrors() Theory = {} for i, m in enumerate(mass): if not m in val: print "Key Error:", m, "not in value map" continue n = Exp0s.GetN() Obs0s.SetPoint(n, m, val[m][0]*multF) Exp0s.SetPoint(n, m, val[m][3]*multF) Exp1s.SetPoint(n, m, val[m][3]*multF) Exp1s.SetPointError(n, 0., 0., val[m][3]*multF-val[m][2]*multF, val[m][4]*multF-val[m][3]*multF) Exp2s.SetPoint(n, m, val[m][3]*multF) Exp2s.SetPointError(n, 0., 0., val[m][3]*multF-val[m][1]*multF, val[m][5]*multF-val[m][3]*multF) if len(val[m]) > 6: Sign.SetPoint(n, m, val[m][6]) if len(val[m]) > 7: pVal.SetPoint(n, m, val[m][7]) if len(val[m]) > 8: Best.SetPoint(n, m, val[m][8]) if len(val[m]) > 10: Best.SetPointError(n, 0., 0., abs(val[m][9]), val[m][10]) for t in THEORY: Theory[t] = TGraphAsymmErrors() addXZH = True for m in sorted(HVT[t]['W']['XS'].keys()): if m < mass[0] or m > mass[-1]: continue if m>4500: continue ## for now because I don't have the higher mass xs FIXME XsZ, XsZ_Up, XsZ_Down = 0., 0., 0. if addXZH: XsZ = 1000.*HVT[t]['Z']['XS'][m]*0.12 #temporary BR value set to 0.12 FIXME XsZ_Up = XsZ*(1.+math.hypot(HVT[t]['Z']['QCD'][m][0]-1., HVT[t]['Z']['PDF'][m][0]-1.)) XsZ_Down = XsZ*(1.-math.hypot(1.-HVT[t]['Z']['QCD'][m][0], 1.-HVT[t]['Z']['PDF'][m][0])) n = Theory[t].GetN() Theory[t].SetPoint(n, m, XsZ) Theory[t].SetPointError(n, 0., 0., (XsZ-XsZ_Down), (XsZ_Up-XsZ)) Theory[t].SetLineColor(theoryLineColor[t]) Theory[t].SetFillColor(theoryFillColor[t]) Theory[t].SetFillStyle(theoryFillStyle[t]) Theory[t].SetLineWidth(2) #Theory[t].SetLineStyle(7) Exp2s.SetLineWidth(2) Exp2s.SetLineStyle(1) Obs0s.SetLineWidth(3) Obs0s.SetMarkerStyle(0) Obs0s.SetLineColor(1) Exp0s.SetLineStyle(2) Exp0s.SetLineWidth(3) Exp1s.SetFillColor(417) #kGreen+1 Exp1s.SetLineColor(417) #kGreen+1 Exp2s.SetFillColor(800) #kOrange Exp2s.SetLineColor(800) #kOrange Exp2s.GetXaxis().SetTitle("m_{"+particleP+"} (GeV)") Exp2s.GetXaxis().SetTitleSize(Exp2s.GetXaxis().GetTitleSize()*1.25) Exp2s.GetXaxis().SetNoExponent(True) Exp2s.GetXaxis().SetMoreLogLabels(True) Exp2s.GetYaxis().SetTitle("#sigma("+particleP+") #bf{#it{#Beta}}("+particleP+" #rightarrow "+particle+") (fb)") Exp2s.GetYaxis().SetTitleOffset(1.5) Exp2s.GetYaxis().SetNoExponent(True) Exp2s.GetYaxis().SetMoreLogLabels() Sign.SetLineWidth(2) Sign.SetLineColor(629) Sign.GetXaxis().SetTitle("m_{"+particleP+"} (GeV)") Sign.GetXaxis().SetTitleSize(Sign.GetXaxis().GetTitleSize()*1.1) Sign.GetYaxis().SetTitle("Significance") pVal.SetLineWidth(2) pVal.SetLineColor(629) pVal.GetXaxis().SetTitle("m_{"+particleP+"} (GeV)") pVal.GetXaxis().SetTitleSize(pVal.GetXaxis().GetTitleSize()*1.1) pVal.GetYaxis().SetTitle("local p-Value") Best.SetLineWidth(2) Best.SetLineColor(629) Best.SetFillColor(629) Best.SetFillStyle(3003) Best.GetXaxis().SetTitle("m_{"+particleP+"} (GeV)") Best.GetXaxis().SetTitleSize(Best.GetXaxis().GetTitleSize()*1.1) Best.GetYaxis().SetTitle("Best Fit (pb)") c1 = TCanvas("c1", "Exclusion Limits", 800, 600) c1.cd() #SetPad(c1.GetPad(0)) c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetLeftMargin(0.12) c1.GetPad(0).SetTicks(1, 1) #c1.GetPad(0).SetGridx() #c1.GetPad(0).SetGridy() c1.GetPad(0).SetLogy() Exp2s.Draw("A3") Exp1s.Draw("SAME, 3") for t in THEORY: Theory[t].Draw("SAME, L3") Theory[t].Draw("SAME, L3X0Y0") Exp0s.Draw("SAME, L") if not options.blind: Obs0s.Draw("SAME, L") #setHistStyle(Exp2s) Exp2s.GetXaxis().SetTitleSize(0.050) Exp2s.GetYaxis().SetTitleSize(0.050) Exp2s.GetXaxis().SetLabelSize(0.045) Exp2s.GetYaxis().SetLabelSize(0.045) Exp2s.GetXaxis().SetTitleOffset(0.90) Exp2s.GetYaxis().SetTitleOffset(1.25) Exp2s.GetYaxis().SetMoreLogLabels(True) Exp2s.GetYaxis().SetNoExponent(True) Exp2s.GetYaxis().SetRangeUser(0.1, 5.e3) #else: Exp2s.GetYaxis().SetRangeUser(0.1, 1.e2) #Exp2s.GetXaxis().SetRangeUser(mass[0], min(mass[-1], MAXIMUM[channel] if channel in MAXIMUM else 1.e6)) Exp2s.GetXaxis().SetRangeUser(SIGNALS[0], SIGNALS[-1]) #drawAnalysis(channel) drawAnalysis("") #drawRegion(channel, True) drawRegion("", True) #drawCMS(LUMI, "Simulation Preliminary") #Preliminary drawCMS(LUMI, "Work in Progress", suppressCMS=True) # legend top = 0.9 nitems = 4 + len(THEORY) leg = TLegend(0.55, top-nitems*0.3/5., 0.98, top) #leg = TLegend(0.45, top-nitems*0.3/5., 0.98, top) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.SetHeader("95% CL upper limits") leg.AddEntry(Obs0s, "Observed", "l") leg.AddEntry(Exp0s, "Expected", "l") leg.AddEntry(Exp1s, "#pm 1 std. deviation", "f") leg.AddEntry(Exp2s, "#pm 2 std. deviation", "f") for t in THEORY: leg.AddEntry(Theory[t], theoryLabel[t], "fl") leg.Draw() latex = TLatex() latex.SetNDC() latex.SetTextSize(0.045) latex.SetTextFont(42) #latex.DrawLatex(0.66, leg.GetY1()-0.045, particleP+" #rightarrow "+particle+"h") leg2 = TLegend(0.12, 0.225-2*0.25/5., 0.65, 0.225) leg2.SetBorderSize(0) leg2.SetFillStyle(0) #1001 leg2.SetFillColor(0) c1.GetPad(0).RedrawAxis() leg2.Draw() if not options.blind: Obs0s.Draw("SAME, L") c1.GetPad(0).Update() if not gROOT.IsBatch(): raw_input("Press Enter to continue...") c1.Print("combine/plotsLimit/ExclusionLimits/MANtag_study/"+YEAR+suffix+".png") c1.Print("combine/plotsLimit/ExclusionLimits/MANtag_study/"+YEAR+suffix+".pdf") if 'ah' in channel or 'sl' in channel: c1.Print("combine/plotsLimit/ExclusionLimits/MANtag_study/"+YEAR+suffix+".C") c1.Print("combine/plotsLimit/ExclusionLimits/MANtag_study/"+YEAR+suffix+".root") for t in THEORY: print "Model", t, ":", for m in range(mass[0], mass[-1], 1): if not (Theory[t].Eval(m) > Obs0s.Eval(m)) == (Theory[t].Eval(m+1) > Obs0s.Eval(m+1)): print m, print "" return ##FIXME the paths in the following unused lines are not adjusted to have a separate MANtag_study directory # ---------- Significance ---------- c2 = TCanvas("c2", "Significance", 800, 600) c2.cd() c2.GetPad(0).SetTopMargin(0.06) c2.GetPad(0).SetRightMargin(0.05) c2.GetPad(0).SetTicks(1, 1) c2.GetPad(0).SetGridx() c2.GetPad(0).SetGridy() Sign.GetYaxis().SetRangeUser(0., 5.) Sign.Draw("AL3") #drawCMS(LUMI, "Preliminary") drawCMS(LUMI, "Work in Progress", suppressCMS=True) drawAnalysis(channel[1:3]) c2.Print("combine/plotsLimit/Significance/"+YEAR+suffix+".png") c2.Print("combine/plotsLimit/Significance/"+YEAR+suffix+".pdf") # c2.Print("plotsLimit/Significance/"+YEAR+suffix+".root") # c2.Print("plotsLimit/Significance/"+YEAR+suffix+".C") # ---------- p-Value ---------- c3 = TCanvas("c3", "p-Value", 800, 600) c3.cd() c3.GetPad(0).SetTopMargin(0.06) c3.GetPad(0).SetRightMargin(0.05) c3.GetPad(0).SetTicks(1, 1) c3.GetPad(0).SetGridx() c3.GetPad(0).SetGridy() c3.GetPad(0).SetLogy() pVal.Draw("AL3") pVal.GetYaxis().SetRangeUser(2.e-7, 0.5) ci = [1., 0.317310508, 0.045500264, 0.002699796, 0.00006334, 0.000000573303, 0.000000001973] line = TLine() line.SetLineColor(922) line.SetLineStyle(7) text = TLatex() text.SetTextColor(922) text.SetTextSize(0.025) text.SetTextAlign(12) for i in range(1, len(ci)-1): line.DrawLine(pVal.GetXaxis().GetXmin(), ci[i]/2, pVal.GetXaxis().GetXmax(), ci[i]/2); text.DrawLatex(pVal.GetXaxis().GetXmax()*1.01, ci[i]/2, "%d #sigma" % i); #drawCMS(LUMI, "Preliminary") drawCMS(LUMI, "Work in Progress", suppressCMS=True) drawAnalysis(channel[1:3]) c3.Print("combine/plotsLimit/pValue/"+YEAR+suffix+".png") c3.Print("combine/plotsLimit/pValue/"+YEAR+suffix+".pdf") # c3.Print("plotsLimit/pValue/"+YEAR+suffix+".root") # c3.Print("plotsLimit/pValue/"+YEAR+suffix+".C") # --------- Best Fit ---------- c4 = TCanvas("c4", "Best Fit", 800, 600) c4.cd() c4.GetPad(0).SetTopMargin(0.06) c4.GetPad(0).SetRightMargin(0.05) c4.GetPad(0).SetTicks(1, 1) c4.GetPad(0).SetGridx() c4.GetPad(0).SetGridy() Best.Draw("AL3") #drawCMS(LUMI, "Preliminary") drawCMS(LUMI, "Work in Progress", suppressCMS=True) drawAnalysis(channel[1:3]) c4.Print("combine/plotsLimit/BestFit/"+YEAR+suffix+".png") c4.Print("combine/plotsLimit/BestFit/"+YEAR+suffix+".pdf") # c4.Print("plotsLimit/BestFit/"+YEAR+suffix+".root") # c4.Print("plotsLimit/BestFit/"+YEAR+suffix+".C") if not gROOT.IsBatch(): raw_input("Press Enter to continue...") if 'ah' in channel: outFile = TFile("bands.root", "RECREATE") outFile.cd() pVal.Write("graph") Best.Write("best") outFile.Close()
class BJetEfficiencyCreator(Analyzer): def __init__(self, cfg_ana, cfg_comp, looperName): super(BJetEfficiencyCreator, self).__init__(cfg_ana, cfg_comp, looperName) self.h2_b = TH2F("h2_b","h2_b",19,20,1000,4,0,2.4) self.h2_c = TH2F("h2_c","h2_c",19,20,1000,4,0,2.4) self.h2_oth = TH2F("h2_oth","h2_oth",19,20,1000,4,0,2.4) #self.btag_b = TH2F("btag_b","btag_b",19,20,1000,4,0,2.4) #self.btag_c = TH2F("btag_c","btag_c",19,20,1000,4,0,2.4) #self.btag_oth = TH2F("btag_oth","btag_oth",19,20,1000,4,0,2.4) self.btag_eff_b = TH2F("btag_eff_b","btag_eff_b",19,20,1000,4,0,2.4) self.btag_eff_c = TH2F("btag_eff_c","btag_eff_c",19,20,1000,4,0,2.4) self.btag_eff_oth = TH2F("btag_eff_oth","btag_eff_oth",19,20,1000,4,0,2.4) def beginLoop(self, setup): super(BJetEfficiencyCreator, self).beginLoop(setup) self.counters.addCounter('BJetEfficiencyCreator') count = self.counters.counter('BJetEfficiencyCreator') count.register('All Events') #count.register('at least 2 good jets') count.register('total input jets') count.register('total b-jets') count.register('total b-tagged jets') def process(self, event): '''Adds the is_btagged attribute to the jets of the given jets collection. ''' self.counters.counter('BJetEfficiencyCreator').inc('All Events') jets = getattr(event, self.cfg_ana.jets) for jet in jets: self.counters.counter('BJetEfficiencyCreator').inc('total input jets') jet.is_btagged = isBTagged(csv=jet.btag("pfCombinedInclusiveSecondaryVertexV2BJetTags"), csv_cut=0.5803) #(loose wp csv2) # csv_cut=0.1522) #(loose wp deepcsv) # https://twiki.cern.ch/twiki/bin/viewauth/CMS/BtagRecommendation94X #print jet.hadronFlavour() if abs(jet.hadronFlavour()) == 5: self.counters.counter('BJetEfficiencyCreator').inc('total b-jets') self.h2_b.Fill(jet.pt(), abs(jet.eta())) if jet.is_btagged: self.counters.counter('BJetEfficiencyCreator').inc('total b-tagged jets') #self.btag_b.Fill(jet.pt(), abs(jet.eta())) self.btag_eff_b.Fill(jet.pt(), abs(jet.eta())) elif abs(jet.hadronFlavour()) == 4: self.h2_c.Fill(jet.pt(), abs(jet.eta())) if jet.is_btagged: #self.btag_c.Fill(jet.pt(), abs(jet.eta())) self.btag_eff_c.Fill(jet.pt(), abs(jet.eta())) elif jet.hadronFlavour() == 0: self.h2_oth.Fill(jet.pt(), abs(jet.eta())) if jet.is_btagged: #self.btag_oth.Fill(jet.pt(), abs(jet.eta())) self.btag_eff_oth.Fill(jet.pt(), abs(jet.eta())) def write(self, setup): self.btag_eff_b.Divide(self.h2_b) self.btag_eff_c.Divide(self.h2_c) self.btag_eff_oth.Divide(self.h2_oth) super(BJetEfficiencyCreator, self).write(setup) self.rootfile = TFile('/'.join([self.dirName, 'btag.root']), 'recreate') #import pdb; pdb.set_trace() #self.h2_b.Write() #self.h2_c.Write() #self.h2_oth.Write() #self.btag_b.Write() #self.btag_c.Write() #self.btag_oth.Write() self.btag_eff_b.Write() self.btag_eff_c.Write() self.btag_eff_oth.Write() self.rootfile.Write() self.rootfile.Close()
class PlotGraphs: def __init__(self, data, xlow, xhigh, ylow, yhigh, xlabel="", ylabel="", xLegend=.45, yLegend=.60, legendWidth=0.20, legendHeight=0.45, fillStyle=3395, drawOption='APL3', make_tfile=False): self.graph_index = {} self.ngraphs = 0 self.data = data self.drawOption = drawOption self.xlow = xlow self.xhigh = xhigh self.ylow = ylow self.yhigh = yhigh self.xlabel = xlabel self.ylabel = ylabel self.multigraph = TMultiGraph("MultiGraph", "") self.legend_type = {} if make_tfile: self.tfile = TFile('tgraphs.root', 'recreate') self.legend = TLegend(xLegend, yLegend, xLegend + legendWidth, yLegend + legendHeight) self.g_ = {} self.g2_ = {} self.g3_ = {} for cat in data.cat: if data.type[cat] == 'observed': #self.g_[cat] = TGraph(len(data.x[cat]), data.x[cat], data.y[cat]) self.g_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl[cat], data.exh[cat], data.eyl[cat], data.eyh[cat]) self.g_[cat].SetName(cat) if make_tfile: _gcopy = self.g_[cat].Clone() self.tfile.Append(_gcopy) if data.dofit[cat]: #self.g_[cat].Fit("pol3", "M", "", data.fit_min[cat], data.fit_max[cat]) f1 = TF1( "f1", "[0]+[1]*(x-1000.0)/1000.0+[2]*(x-1000.0)*(x-1000.0)/1000000.0+[3]*(x-1000.0)*(x-1000.0)*(x-1000.0)/1000000000.0", data.fit_min[cat], data.fit_max[cat]) f2 = TF1( "f2", "[0]+[1]*(x-1000.0)/1000.0+[2]*(x-1000.0)*(x-1000.0)/1000000.0", data.fit_min[cat], data.fit_max[cat]) f3 = TF1("f3", "[0]+[1]*(x-1000.0)/1000.0", data.fit_min[cat], data.fit_max[cat]) _fr = self.g_[cat].Fit("f2", "MEWS", "", data.fit_min[cat], data.fit_max[cat]) _fr.Print() elif data.type[cat] == 'expected': self.g_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl[cat], data.exh[cat], data.eyl[cat], data.eyh[cat]) self.g2_[cat] = TGraphAsymmErrors( len(data.x[cat]), data.x[cat], data.y[cat], data.exl2[cat], data.exh2[cat], data.eyl2[cat], data.eyh2[cat]) self.g3_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl2[cat], data.exh2[cat], data.y[cat], data.exl2[cat]) self.g3_[cat].SetFillStyle(1002) #self.g3_[cat].SetFillStyle(3008) # 95% quantile self.g2_[cat].SetMarkerColor(data.fill2_color[cat]) self.g2_[cat].SetMarkerStyle(data.marker_style[cat]) self.g2_[cat].SetMarkerSize(data.marker_size[cat]) self.g2_[cat].SetLineColor(data.fill2_color[cat]) self.g2_[cat].SetLineStyle(data.line_style[cat]) self.g2_[cat].SetLineWidth(data.line_width[cat]) #self.g2_[cat].SetFillColor(data.marker_color[cat]+2) self.g2_[cat].SetFillColor(data.fill2_color[cat]) #self.g2_[cat].SetFillStyle(3008) #self.g2_[cat].SetFillStyle(3003) self.g2_[cat].SetFillStyle(data.fill2_style[cat]) self.g_[cat].SetMarkerColor(data.marker_color[cat]) self.g_[cat].SetMarkerStyle(data.marker_style[cat]) self.g_[cat].SetMarkerSize(data.marker_size[cat]) self.g_[cat].SetLineColor(data.line_color[cat]) self.g_[cat].SetLineStyle(data.line_style[cat]) self.g_[cat].SetLineWidth(data.line_width[cat]) self.g_[cat].SetFillColor(data.fill_color[cat]) if data.fill_style[cat] == None: self.g_[cat].SetFillStyle(fillStyle) else: self.g_[cat].SetFillStyle(data.fill_style[cat]) if data.type[cat] == 'observed': # only the main observed limit is a line, # everything else is a filled area if cat[0:3] == 'obs': _legend_type = 'lp' if 'PC' in drawOption: _draw_option = 'PC' else: _draw_option = 'PL' self.multigraph.Add(self.g_[cat], _draw_option) elif cat[0:3] == 'SSM': _legend_type = 'lp' _draw_option = '3' self.multigraph.Add(self.g_[cat], _draw_option) # print theory curve #self.g_[cat].Print() elif cat[0:3] == 'Psi': _legend_type = 'lp' _draw_option = '3' self.multigraph.Add(self.g_[cat], _draw_option) elif cat[0:3] == 'Stu': _legend_type = 'lp' _draw_option = '3' self.multigraph.Add(self.g_[cat], _draw_option) elif cat[0:2] == 'RS': _legend_type = 'f' _draw_option = '3' self.multigraph.Add(self.g_[cat], _draw_option) else: _legend_type = 'f' self.multigraph.Add(self.g_[cat]) #self.multigraph.Add(self.g_[cat]) self.graph_index[cat] = self.ngraphs self.ngraphs += 1 #self.legend . AddEntry( self.g_[cat], data.tlegend[cat], _legend_type); self.legend_type[cat] = _legend_type elif data.type[cat] == 'expected': self.g3_[cat].SetFillColor(0) self.g3_[cat].SetMinimum(self.ylow) self.g3_[cat].SetMaximum(self.yhigh) self.g3_[cat].GetXaxis().SetLimits(self.xlow, self.xhigh) self.multigraph.Add(self.g3_[cat], 'C3') self.ngraphs += 1 # 95 expected band self.multigraph.Add(self.g2_[cat], 'C4L') self.ngraphs += 1 self.multigraph.Add(self.g_[cat], 'C4L') self.graph_index[cat] = self.ngraphs self.ngraphs += 1 # median line gline = self.g_[cat].Clone() gline.SetLineWidth(3) gline.SetLineColor(ROOT.kBlue) gline.SetLineStyle(2) # median expected marker gline.SetMarkerStyle(8) gline.SetMarkerSize(1) # print graph contents #gline.Print() #self.multigraph.Add(gline, 'LXC') self.multigraph.Add(gline, 'C3X') self.ngraphs += 1 # median expected legend #self.legend . AddEntry( gline, 'median expected', "l"); self.legend_type[cat] = "l" _g = self.g_[cat] self.g_[cat] = gline # 68% expected band legend #self.legend . AddEntry( self.g_[cat], '68% expected', "f"); self.legend_type[cat + '1sig'] = "f" self.g_[cat + '1sig'] = _g # 95% expected band legend #self.legend . AddEntry( self.g2_[cat], '95% expected', "f"); self.legend_type[cat + '2sig'] = "f" self.g_[cat + '2sig'] = self.g2_[cat] keylist = data.legend_index.keys() # keys are indices keylist.sort() for key in keylist: print key, data.legend_index[key] self.legend.AddEntry(self.g_[data.legend_index[key]], data.tlegend[data.legend_index[key]], self.legend_type[data.legend_index[key]]) self.legend.SetShadowColor(0) self.legend.SetFillColor(0) self.legend.SetLineColor(0) if make_tfile: self.tfile.Write() self.tfile.Close() def draw(self, yLabelSize=0.055): self.multigraph.SetMinimum(self.ylow) self.multigraph.SetMaximum(self.yhigh) self.multigraph.Draw(self.drawOption) self.multigraph.GetXaxis().SetNdivisions(405) self.multigraph.GetYaxis().SetNdivisions(405) self.multigraph.GetXaxis().SetLimits(self.xlow, self.xhigh) #self.multigraph.GetYaxis().SetTitle("") self.multigraph.GetYaxis().SetLabelSize(yLabelSize) self.multigraph.GetXaxis().SetLabelSize(yLabelSize) latex = TLatex() latex.SetNDC() #latex.SetTextSize(0.04) latex.SetTextSize(yLabelSize) latex.SetTextAlign(31) # align right latex.DrawLatex(0.95, 0.01, self.xlabel) latex.SetTextAngle(90) latex.DrawLatex(0.03, 0.9, self.ylabel) self.legend.SetFillStyle(0) self.legend.SetBorderSize(0) #self.legend.SetTextSize(0.04) #self.legend.SetTextSize(yLabelSize) self.legend.SetTextSize(yLabelSize * 0.7) self.legend.SetTextFont(42) self.legend.SetTextAlign(11) self.legend.Draw() return self.multigraph def draw_line(self, xline, ymin=-0.02, ymax=0.20): if xline == None: return #print 'XXXX', xline _x = array('d') _y = array('d') _x.append(xline) _y.append(ymin) _x.append(float(xline)) _y.append(ymax) g_ = TGraph(len(_x), _x, _y) self.multigraph.Add(g_, 'L') def print_values(self, cat1, cat2): legend = 'PlotGraphs::print_values():' if cat1 in self.data.cat: graph1 = self.multigraph.GetListOfGraphs().At( self.graph_index[cat1]) else: return None if cat2 in self.data.cat: graph2 = self.multigraph.GetListOfGraphs().At( self.graph_index[cat2]) else: return None for x in range(750, 1150, 50): dmax = 0.0 drelmax = 0.0 v1 = graph1.Eval(x) v2 = graph2.Eval(x) d = math.fabs(v2 - v1) drel = math.fabs((v2 - v1) / v1) if d > dmax: dmax = d if drel > drelmax: drelmax = drel print legend, 'x =', x print legend, cat1, 'value =', v1 print legend, cat2, 'value =', v2 print legend, 'abs diff =', d print legend, 'abs relative diff =', drel print legend, 'max abs diff =', dmax print legend, 'max abs relative diff =', drelmax def find_intersection(self, cat1, cat2, xmin=350, xmax=3500, precision=0.000000001): legend = 'PlotGraphs::find_intersection(%s,%s):' % (cat1, cat2) #always put "exp" or "obs" in cat1 if cat1 is "exp" or cat1 is "obs": if cat1 in self.data.cat: graph1 = self.multigraph.GetListOfGraphs().At( self.graph_index[cat1]) else: print "Failed to get ", cat1 return ["None"] else: print cat1, " is not obs/exp limit." return ["None"] if cat2 in self.data.cat: graph2 = self.multigraph.GetListOfGraphs().At( self.graph_index[cat2]) #graph2.Print() else: print "Failed to get ", cat2 return ["None"] _x = xmin print legend, graph1.Eval(_x) - graph2.Eval(_x) if graph1.Eval(_x) > graph2.Eval(_x): result = ["lowlimit"] else: result = [] _interval = 1 _sign1 = graph1.Eval(_x) > graph2.Eval(_x) while _x < xmax: _x += _interval _sign2 = graph1.Eval(_x) > graph2.Eval(_x) if _sign1 != _sign2: _d = 100000 _step = -0.5 * _interval _crosspt = _x while abs(_d) > precision: _crosspt += _step _d = graph1.Eval(_crosspt) - graph2.Eval(_crosspt) _sign2 = _d > 0 if _sign1 != _sign2: _step = -0.5 * abs(_step) else: _step = 0.5 * abs(_step) _sign1 = not (_sign1) result.append(_crosspt) if result: print "Allowed region:", _x = 0 while _x < len(result): if _x + 1 < len(result): print "[", result[_x], ",", result[_x + 1], "];", else: if _x + 1 == len(result): print "[", result[_x], ", inf ];" if _x + 2 == len(result): print _x += 2 else: print "[", xmin, "~", xmax, "] is excluded." del graph1 del graph2 return result
gStyle.SetOptFit(1) ### setup canvas c1 = TCanvas('c1', 'c1', 900, 600) #c1.Divide(1,5) c1.SetGrid() hcpuPerEvent1T.Draw('histbar') hcpuPerEvent1AT.Draw('histbar same') hcpuPerEvent4T.Draw('histbar same') hcpuPerEvent1ET.Draw('histbar same') hcpuPerEvent1CT.Draw('histbar same') hcpuPerEvent1DT.Draw('histbar same') legend = TLegend(0.1, 0.7, 0.28, 0.9) legend.AddEntry(hcpuPerEvent1T, "102x102 Lenna", "f") legend.AddEntry(hcpuPerEvent4T, "128x128 Lenna", "f") legend.AddEntry(hcpuPerEvent1AT, "256x256 Lenna", "f") legend.AddEntry(hcpuPerEvent1ET, "102x102 Baboon", "f") legend.AddEntry(hcpuPerEvent1CT, "128z128 Baboon", "f") legend.AddEntry(hcpuPerEvent1DT, "156x156 Baboon", "f") #legend.AddEntry(hcpuPerEvent4AT,"4 thread and single track in 1","f") legend.Draw() #c1.cd(5) c1.SaveAs("exp2_IteracionesxPSNR_erosion.png") savefile.Write() savefile.Close()
def parseLAT1(goodList, theCut, dsNum, bkgIdx=None, saveMe=False): """ Accesses MGTWaveform objects directly and returns some arbitrary object. Suffers from the same ROOT/MGSW TChain bug as parseGAT (see above). """ from ROOT import gDirectory, TFile, TTree, TChain, MGTWaveform, MJTMSWaveform, GATDataSet # this is what we return baseDict = {ch:[] for ch in goodList} p1Start = time.time() # get a sequential list of file names latDir, latList = ds.getLATList(dsNum, bkgIdx) # loop over each file in the list for fName in latList: start = time.time() # load trees latFile = TFile(latDir + "/" + fName) latTree = latFile.Get("skimTree") # create TEntryList latTree.Draw(">>elist", theCut, "entrylist") eList = gDirectory.Get("elist") latTree.SetEntryList(eList) # loop over entries passing cuts for iList in range(eList.GetN()): entry = latTree.GetEntryNumber(iList); latTree.LoadTree(entry) latTree.GetEntry(entry) nChans = latTree.channel.size() numPass = latTree.Draw("channel",theCut,"GOFF",1,iList) chans = latTree.GetV1() chanList = list(set(int(chans[n]) for n in xrange(numPass))) # loop over hits passing cuts (channels in good list only) hitList = (iH for iH in range(nChans) if latTree.channel.at(iH) in goodList and latTree.channel.at(iH) in chanList) for iH in hitList: # now we can pretty much save or calculate anything we want wf = latTree.MGTWaveforms.at(iH) chan = int(latTree.channel.at(iH)) signal = wl.processWaveform(wf) baseline,_ = signal.GetBaseNoise() baseline = float("%.2f" % baseline) # kill precision to save on memory globalTime = int(latTree.globalTime.fSec) run = int(latTree.run) baseDict[chan].append(baseline,globalTime,run) print("%s, %d entries, %d passing cuts. %.2f sec." % (fName, latTree.GetEntries(), eList.GetN(), time.time()-start)) latFile.Close() # optionally save the output if saveMe: with open("../data/parseLAT1_ds%d.json" % dsNum, 'w') as fOut: json.dump(baseDict, fOut) print( "Total Elapsed:",time.time() - p1Start) return baseDict
acc_var_el, acc_var_mu, acc_var_both = histo_cut_ev_eltau.GetBinContent( var_index), histo_cut_ev_mutau.GetBinContent( var_index), histo_both_cut.GetBinContent(var_index) if acc_var_el < 0.01: break var_el = (acc_var_el - acc_nom_el)**2 var_mu = (acc_var_mu - acc_nom_mu)**2 var_both = (acc_var_both - acc_nom_both)**2 #print var_el, var_mu, var_both variations[0] += var_el variations[1] += var_mu variations[2] += var_both tfile.Close() # dataset variations for sys_filename in sys_files: sys_file = TFile(sys_filename) histo_all_ev_eltau = sys_file.Get("all_ev_eltau") histo_cut_ev_eltau = sys_file.Get("cut_ev_eltau") histo_all_ev_mutau = sys_file.Get("all_ev_mutau") histo_cut_ev_mutau = sys_file.Get("cut_ev_mutau") histo_both_all = histo_all_ev_eltau + histo_all_ev_mutau histo_both_cut = histo_cut_ev_eltau + histo_cut_ev_mutau histo_both_cut.Divide(histo_both_all) histo_cut_ev_mutau.Divide(histo_all_ev_mutau)
# print var, MVA_Only[var][0] # print 'BDT_CWoLa2', mytree.variables['BDT_QCDCWoLa2'][0] # print 'BDT_CWoLa',mytree.variables['BDT_QCDCWoLa'][0] # print '###################################################' # for i in range(3): # if mytree.variables['n_jets'][0] == 7: # mycat = reader_Class1ad.EvaluateMulticlass("BDT_Class1ad")[i] # if mycat > highest: # highest = mycat # bestcat = i # else: # mycat = reader_Class2ad.EvaluateMulticlass("BDT_Class2ad")[i] # if mycat > highest: # highest = mycat # bestcat = i # mytree.variables['BDT_Class'][bestcat] = 1 #print 'ttcls: ',mytree.variables['ttCls'][0], ' bdt_class: ',BDT_Class, ' bdtmajor: ',BDT_ClassMajo, ' bdtmax: ',mytree.variables['BDT_ttbarMax'][0] mytree.variables['BDTQGL'][0] = readerQGL.EvaluateMVA("BDT_QGL") mytree.variables['BDTMisc'][0] = readerMisc.EvaluateMVA("BDT_Misc") tkin.Fill() mytree.ZeroArray() if e > emax: break watch.Print() tkin.Write() fout.Write() fout.Close()
hist3 = TH1F("hist3", "hist3", NBins, IBin, FBin) ENUM = t1.GetEntries() for i in range(ENUM): t1.GetEntry(i) if ((px[0] > -1) & (py[0] > 0.5) & (pz[0] < 3.)): hist1.Fill(px[0]) else: continue ENUM = t2.GetEntries() for i in range(ENUM): t2.GetEntry(i) if ((px[0] < -0.1) & (py[0] < 0.5) & (pz[0] < 2)): hist2.Fill(py[0]) else: continue ENUM = t2.GetEntries() for i in range(ENUM): t2.GetEntry(i) if ((px[0] < -0.1) & (py[0] < 0.5) & (pz[0] < 2)): hist3.Fill(pz[0]) else: continue hist1.Write() hist2.Write() hist3.Write() outfile.Close()
for i in range(0, 20): lambdaZ = -0.4 + 0.04 * i if lambdaZ > 0.41: break print '================ lambdaZ: ', lambdaZ for j in range(0, 7): morphAllLambda(basisFile, lambdaZ, -0.6 + 0.2 * j, morphedHistsInLambda) foutput.cd() for hist in morphedHistsInLambda: hist.Write() for i in range(0, 6): kappa = -0.6 + 0.2 * i if kappa > 0.61: break print '================ deltaKappaGamma: ', kappa for j in range(0, 81): morphAllKappa(foutput, -0.4 + 0.01 * j, kappa, morphedHistsInKappa) foutput.cd() for hist in morphedHistsInKappa: hist.Write() for hist in originalHists: hist.Write() foutput.Close()
class TMVA_Estimator(object): def __init__(self, name, n_vars, n_targets=1, method='BDT', task='Classification'): self.name = name self.n_vars = n_vars self.n_targets = n_targets self.method = method self.task = task self.tmpdir = tempfile.mkdtemp() self.output = TFile(os.path.join(self.tmpdir, 'tmva_output.root'), 'recreate') self.factory = TMVA.Factory(name, self.output, 'AnalysisType={0}:Silent'.format(task)) for n in range(n_vars): self.factory.AddVariable('X_{0}'.format(n), 'F') if task == 'Regression': for n in range(n_targets): self.factory.AddTarget('y_{0}'.format(n), 'F') def __del__(self): self.output.Close() shutil.rmtree(self.tmpdir) def fit(self, X, y, X_test=None, y_test=None, weights=None, weights_test=None, signal_label=None, **kwargs): # (re)configure settings since deleting a previous Factory resets all # this. This is poor design, TMVA. config = TMVA.gConfig() config.GetIONames().fWeightFileDir = self.tmpdir config.SetSilent(True) config.SetDrawProgressBar(False) self.factory.DeleteAllMethods() extra_kwargs = dict() if self.task == 'Regression': func = rnp.tmva.add_regression_events else: func = rnp.tmva.add_classification_events extra_kwargs['signal_label'] = signal_label # test exceptions assert_raises(TypeError, func, object(), X, y) assert_raises(ValueError, func, self.factory, X, y[:y.shape[0] / 2]) if weights is not None: assert_raises(ValueError, func, self.factory, X, y, weights=weights[:weights.shape[0] / 2]) assert_raises(ValueError, func, self.factory, X, y, weights=weights[:, np.newaxis]) assert_raises(ValueError, func, self.factory, [[[1, 2]]], [1]) assert_raises(ValueError, func, self.factory, [[1, 2]], [[[1]]]) func(self.factory, X, y, weights=weights, **extra_kwargs) if X_test is None: X_test = X y_test = y weights_test = weights func(self.factory, X_test, y_test, weights=weights_test, test=True, **extra_kwargs) self.factory.PrepareTrainingAndTestTree(TCut('1'), 'NormMode=EqualNumEvents') options = [] for param, value in kwargs.items(): if value is True: options.append(param) elif value is False: options.append('!{0}'.format(param)) else: options.append('{0}={1}'.format(param, value)) options = ':'.join(options) self.factory.BookMethod(self.method, self.method, options) self.factory.TrainAllMethods() def predict(self, X, aux=0.): reader = TMVA.Reader() for n in range(self.n_vars): reader.AddVariable('X_{0}'.format(n), array('f', [0.])) reader.BookMVA( self.method, os.path.join(self.tmpdir, '{0}_{1}.weights.xml'.format(self.name, self.method))) assert_raises(TypeError, rnp.tmva.evaluate_reader, object(), self.method, X) assert_raises(ValueError, rnp.tmva.evaluate_reader, reader, 'DoesNotExist', X) assert_raises(ValueError, rnp.tmva.evaluate_reader, reader, self.method, [[[1]]]) if self.task != 'Regression': assert_raises(ValueError, rnp.tmva.evaluate_reader, reader, self.method, [1, 2, 3]) output = rnp.tmva.evaluate_reader(reader, self.method, X, aux) if ROOT.gROOT.GetVersionInt() >= 60300: method = reader.FindMVA(self.method) assert_raises(TypeError, rnp.tmva.evaluate_method, object(), X) assert_raises(ValueError, rnp.tmva.evaluate_method, method, [[[1]]]) output_method = rnp.tmva.evaluate_method(method, X, aux) assert_array_equal(output, output_method) return output