def serialize(self, obj): if not isinstance(obj, TAxis): root2json.serialize(self, obj) title = obj.GetTitle() name = obj.GetName() if title == '': title = "<msub><mi>%s</mi><mi>%s</mi></msub>" % (name[0].upper(), name[1:]) me = { 'color': self.color(obj.GetAxisColor()), 'style': attaxis().serialize(obj), 'min': obj.GetXmin(), 'max': obj.GetXmax(), 'title': title, 'name': name } if obj.IsVariableBinSize(): me['bins'] = obj.GetXbins() else: me['width'] = obj.GetBinWidth(1) if obj.GetTimeFormat(): me['time'] = obj.SetTimeFormat() me['offset'] = obj.SetTimeOffset() labels = TIter(obj.GetLabels()) alabels = [] label = labels.Next() while label != None: alabels.append(label.GetName()) label = labels.Next() if len(alabels) > 0: me['labels'] = alabels return me
def convHStackToHist(hs): hsIter = TIter(hs.GetHists()) hout = hsIter().Clone("{0}_toHist".format(hs.GetName())) h = hsIter.Next() while h: hout.Add(h) h = hsIter.Next() return hout
def serialize(self, obj): if not isinstance(obj, TDirectory): return root2json.serialize(self, obj, False) keys = TIter(obj.GetListOfKeys()) nxtkey = keys.Next() me = {'class': 'directory', 'data': []} data = me['data'] while nxtkey != None: if 'TH1' in nxtkey.GetClassName(): data.append({nxtkey.GetClassName(): nxtkey.GetName()}) nxtkey = keys.Next() return me
def BuildFromRootPrimitive(rootprimitive): """ Build dataset from a root primitive """ result = DataSet() trackContainers = rootprimitive.FindObject("trackContainers") clusterContainers = rootprimitive.FindObject("clusterContainers") trackIter = TIter(trackContainers) clusterIter = TIter(clusterContainers) currententry = trackIter.Next() while currententry: entryname = currententry.GetName() if "trackcontainer" in entryname: contname = entryname.replace("trackcontainer_", "") result.AddTrackContainer( contname, TrackContainer.BuildFromRootPrimitive(currententry)) currententry = trackIter.Next() currententry = clusterIter.Next() while currententry: entryname = currententry.GetName() if "clustercontainer" in entryname: contname = entryname.replace("clustercontainer_", "") result.AddClusterContainer( contname, ClusterContainer.BuildFromRootPrimitive(currententry)) currententry = clusterIter.Next() return result
def getRegions(rootfile): rootfile.cd() nextkey = TIter(gDirectory.GetListOfKeys()) key = nextkey.Next() regions = {} while key and key.GetTitle(): if key.IsFolder() == 1: label = getNewRegionName(key.GetName()) if label in regions: regions[label].append(key.GetName()) else: regions[label] = [key.GetName()] key = nextkey.Next() return regions
def __str__(self): list_of_files = self.ch.GetListOfFiles() next_file = TIter(list_of_files) self.logger.logDEBUG("Input file list is given below.") self.logger.logDEBUG("Absolute path: ", os.path.dirname(next_file.GetTitle())) ifile = next_file.Begin() for f in range(list_of_files.GetEntries()): self.logger.logDEBUG("\t{0}".format( os.path.basename(ifile.GetTitle())), Stamp=False) ifile = next_file.Next() self.logger.logDEBUG("End of the input file list.") return 1
def readDirectory( tdir ): """ Recursively read content of ROOT's TDirectory tdir and add full pathnames to outputlist """ outputlist=[] from ROOT import TIter nextkey = TIter( tdir.GetListOfKeys() ) key = nextkey() while key: obj = key.ReadObj() if obj.IsA().InheritsFrom("TH1") or obj.IsA().InheritsFrom("TTree"): curdirname = stripPathName(tdir) if len(curdirname)>0: objpath = "%s/%s"%(stripPathName(tdir),obj.GetName()) else: objpath = obj.GetName() #print obj.GetName()," :-> ",objpath outputlist.append( objpath ) elif obj.IsA().InheritsFrom("TDirectory"): from ROOT import gDirectory #print gDirectory.GetPath(),obj.GetName(),obj.GetPath() outputlist += readDirectory( obj ) key=nextkey() return outputlist
def AccessHistogramsRecursively(the_dir, path, sum_hists, sum_dir, hists_to_sum): # loop over all keys in the current directory key_iter = TIter(the_dir.GetListOfKeys()) key = key_iter() while( key ): obj = key.ReadObj() obj_pathname = path+"/"+key.GetName() #print key.GetName() + " " + str(type(obj)) if(isinstance(obj,TH1)): if VERBOSE: print "matching hname = %s, hnamepath = %s" %(obj.GetName(),obj_pathname) #print "histogram = " + str(obj.GetName()) #if not match_hnamepaths(hists_to_sum, path+"/"+obj.GetName()): # match either histogram names or full path names if (obj.GetName() not in hists_to_sum) and (obj_pathname not in hists_to_sum): key = key_iter() continue # we want to sum up histograms over multiple ROOT files hnamepath = obj_pathname if not hnamepath in sum_hists.keys(): sum_hists[hnamepath] = obj.Clone() sum_hists[hnamepath].SetDirectory(sum_dir) else: sum_hists[hnamepath].Add(obj) # if the object is a directory, access what's inside if(isinstance(obj, TDirectory)): new_sum_dir = sum_dir.GetDirectory(key.GetName()) if(not new_sum_dir): new_sum_dir = sum_dir.mkdir(key.GetName()) AccessHistogramsRecursively(obj, obj_pathname, sum_hists, new_sum_dir, hists_to_sum) # move to next item in the directory key = key_iter()
def BuildFromRootFile(filename, name): result = ResultData(name) inputfile = TFile.Open(filename) gROOT.cd() keyIter = TIter(inputfile.GetListOfKeys()) key = keyIter.Next() while key: if key.GetName() == "MCTruth": result.SetMCTruth(ParticleTHnSparse(key.ReadObj())) else: result.SetData(key.GetName(), DataSet.BuildFromRootPrimitive(key.ReadObj())) key = keyIter.Next() inputfile.Close() print "Results successfully reconstructed from file %s" %(filename) #result.Print() return result
def PlotHistsRecursive(self, the_dir, path, hists_to_plot): # loop over all keys in the current directory, ROOT-style key_iter = TIter(the_dir.GetListOfKeys()) key = key_iter() while( key ): obj = key.ReadObj() obj_pathname = path+"/"+key.GetName() # if the object is a histogram, then see if we should plot it if(isinstance(obj,TH1)): if self.VERBOSE>1: logging.info("matching hname = %s, hnamepath = %s" %(obj.GetName(),obj_pathname)) #if not match_hnamepaths(hists_to_sum, path+"/"+obj.GetName()): # match either histogram names or full path names if (obj.GetName() not in hists_to_plot) and (obj_pathname not in hists_to_plot): key = key_iter() continue # plot histogram if(isinstance(obj,TH2)): self.plot_2dhist(obj) else: self.plot_hist(obj) # save image to disk self.print_canvas_png("_"+obj_pathname) ## name hack for backward compatability #print_canvas_pdf() # if the object is a directory, access what's inside if(isinstance(obj, TDirectory)): self.PlotHistsRecursive(obj, obj_pathname, hists_to_plot) # END OF LOOP - move to next item in the directory key = key_iter() del key_iter
def __str__(self): list_of_files = self.ch.GetListOfFiles() next_file = TIter(list_of_files) print("Input file list is given below.") for f in range(list_of_files.GetEntries()): print("\t{0}".format(next_file().GetTitle())) print("End of the input file list.") return ""
def plot_ph2acf_rootfile(runnr, module_per_id, plotfoldername, tag=''): ROOT.gROOT.SetBatch(True) runnrstr = '%06i' % runnr infilepattern = 'Results/Run%s_*.root' % runnrstr matches = glob.glob(infilepattern) if len(matches) > 1: raise ValueError( 'Trying to plot histograms in rootfile for runnr. %i, but there is more than one file found with the pattern \'Run%s_*.root\': ' % (runnrstr) + matches) infilename = matches[0] infile = TFile(infilename, 'READ') foldername = 'Detector/Board_0/OpticalGroup_0/' infile.cd(foldername) dir = ROOT.gDirectory iter = TIter(dir.GetListOfKeys()) modules = [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()] for module in modules: histpath = foldername + module moduleid = int(module.replace('Hybrid_', '')) modulename = module_per_id[moduleid] infile.cd() infile.cd(histpath) chips = [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()] for chip in chips: chip_dummy = chip fullhistpath = os.path.join(histpath, chip) infile.cd() infile.cd(fullhistpath) canvases = [ key.GetName() for key in ROOT.gDirectory.GetListOfKeys() ] infile.cd() for canvasname in canvases: if canvasname == 'Channel': continue canvas = infile.Get(os.path.join(fullhistpath, canvasname)) hist = canvas.GetPrimitive(canvasname) canvastitle = canvasname.split('_')[4] + '_' + chip outcanvas = TCanvas('c', canvastitle, 500, 500) if 'SCurves' in canvastitle: ROOT.gPad.SetLogz() hist.Draw('colz') ROOT.gStyle.SetOptStat(0) outdir = os.path.join('plots', 'thresholds', plotfoldername, '') outfilename = canvastitle + tag + '.pdf' outfilename = outfilename.replace('Chip_', '%s_chip' % (modulename)) ensureDirectory(outdir) outcanvas.SaveAs(outdir + outfilename) outcanvas.SaveAs(outdir + outfilename.replace('pdf', 'png')) del infile
def print_pad(pad): next = TIter(pad.GetListOfPrimitives()) print "#####################" obj = next() while obj != None: print obj.GetName(), obj.ClassName() obj = next() print "#####################"
def slice_and_save(sample,histo,outfile): outfile.cd() histo_stack=THStack(histo,'x','Stack_'+histo.GetName(),'') histo_1d=histo_stack.GetHists() histo_stack.Write(histo_stack.GetName()+'_'+sample) nextinlist=TIter(histo_1d) obj=nextinlist() while obj: obj.Write(obj.GetName()+'_'+sample) obj=nextinlist() histo.Write(histo.GetName()+'_'+sample)
def ProcessJets(self, triggerclass, dataset, histlist): """ Fill jet hists to the histogram container 1. find all histograms for the given trigger class that contain the trigger class name and Jet 2. Group them according to jet pt and histogram type """ histiter = TIter(histlist) histfound = histiter.Next() histlist = [] while histfound: histname = str(histfound.GetName()) if triggerclass in histname and "TrackJetHist" in histname: histlist.append(histfound) histfound = histiter.Next() for jethist in histlist: histname = str(jethist.GetName()) jetpt = self.__GetJetPt(histname) dataset.AddJetSpectrum(jethist, jetpt, True if "hMC" in histname else False)
def get_thresholds_from_last(): # last run has highest run-number and must have 'ThrAdjustment' in name files = [ f for f in glob.glob(os.path.join('Results', '*.root')) if 'ThrAdjustment' in f ] # list is unordered runnrs = [int(n.split('_')[0].split('Run')[1]) for n in files] maxidx = runnrs.index(max(runnrs)) lastfilename = files[maxidx] print(lastfilename) infile = TFile(lastfilename, 'READ') foldername = 'Detector/Board_0/OpticalGroup_0/' infile.cd(foldername) dir = ROOT.gDirectory iter = TIter(dir.GetListOfKeys()) modules = [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()] thresholds_per_id_and_chip = {} for module in modules: thresholds_per_id_and_chip[int(module.split('_')[1])] = {} histpath = foldername + module infile.cd() infile.cd(histpath) chips = [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()] print(module, chips) for chip in chips: mod_dummy = module chip_dummy = chip fullhistpath = os.path.join(histpath, chip) infile.cd() infile.cd(fullhistpath) objs = [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()] infile.cd() for objname in objs: if not 'Threhsold' in objname: continue canvas = infile.Get(os.path.join(fullhistpath, objname)) hist = canvas.GetPrimitive(objname) vthresh = int( hist.GetBinCenter(hist.GetMaximumBin()) - hist.GetBinWidth(hist.GetMaximumBin()) / 2.) thresholds_per_id_and_chip[int(module.split('_')[1])][int( chip.split('_')[1])] = vthresh del infile return thresholds_per_id_and_chip
def CountNumberOfHistograms(the_dir): global NUMBER_OF_HISTOGRAMS # loop over all keys in the current directory key_iter = TIter(the_dir.GetListOfKeys()) key = key_iter() while (key): obj = key.ReadObj() if (isinstance(obj, TH1)): NUMBER_OF_HISTOGRAMS += 1 #print obj.GetName() + " " + str(NUMBER_OF_HISTOGRAMS) # if the object is a directory, access what's inside if (isinstance(obj, TDirectory)): CountNumberOfHistograms(obj) # go to next entry key = key_iter()
def LoopAndScale(dir,Weight): """goes through all histos and reweights them""" nextkey=TIter(dir.GetListOfKeys()) key=nextkey() #================================================= #Iterate over the histos in the file #ListOfNewH=[] while(str(key).find('nil') == -1): #GET THE KEY obj=dir.Get(key.GetName() ) #print 'the current directory is ',gDirectory.pwd() #rint 'currently at object',obj.GetName() #raw_input() # #IF a histo # # if obj.IsA().InheritsFrom("TH1"): newhist=obj.Clone(key.GetName()) SetOwnership(newhist,False) newhist.Scale(Weight) elif obj.IsA().InheritsFrom("TDirectory"): newdir=gDirectory.mkdir(obj.GetName()) newdir.cd() LoopAndScale(obj,Weight) # # #set it to return back to where it was gDirectory.GetMotherDir().cd() else: print "Unknown object" print "It is called ",key.GetName() # #dir.cd() key=nextkey()
def AccessDataRecursively(the_dir, path, online_file): global CURRENT_HISTOGRAM_NUM # loop over all keys in the current directory key_iter = TIter(the_dir.GetListOfKeys()) key = key_iter() while (key): obj = key.ReadObj() obj_pathname = path + "/" + key.GetName() #print key.GetName() + " " + str(type(obj)) if (isinstance(obj, TH1)): CURRENT_HISTOGRAM_NUM += 1 #print "histogram = " + str(obj.GetName()) # get corresponding online hist # be lazy and don't worry about iterating over the other file in parallel h_online = online_file.Get(obj_pathname) if not h_online: print "Could not find online histogram: " + obj_pathname continue if (isinstance(obj, TH2)): #print "Is 2D histo!" plot_2d_hists(obj, h_online) else: # make plots plot_offline_hist(obj) plot_online_hist(h_online) # output canvas print_canvas_pdf() if (WEB_OUTPUT): #print_canvas_png(path+"/"+key.GetName()) print_canvas_svg(path + "/" + key.GetName()) # if the object is a directory, access what's inside if (isinstance(obj, TDirectory)): AccessDataRecursively(obj, obj_pathname, online_file) # move to next item in the directory key = key_iter()
def invert_col(self, pad): bgcol = rt.kBlack fgcol = rt.kOrange - 3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #TText if obj.InheritsFrom(TText.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #print obj.GetName(), obj.ClassName() #move to the next item obj = next()
def copyAllKeys(fin, key, cwd, cwdOut, fout): className = key.GetClassName() name = key.GetName() obj = None if "TDirectory" in className: obj = fin.GetDirectory(cwd + "/" + name) nextIter = TIter(obj.GetListOfKeys()) key = nextIter() nameOut = re.sub(r"NoElectronMuonFiducialCuts", r"", name) cwdOut = nameOut if not cwdOut else (cwdOut + "/" + nameOut) cwd = name if not cwd else (cwd + "/" + name) fout.mkdir(cwdOut) while key: copyAllKeys(fin, key, cwd, cwdOut, fout) key = nextIter() else: obj = fin.Get(cwd + "/" + name) if name == "cutFlow": fixPtCut(obj) fout.cd(cwdOut) obj.Write(name)
def invert_col(pad, fgcol=rt.kOrange - 3, bgcol=rt.kBlack): #set foreground and background color #fgcol = rt.kAzure #fgcol = rt.kGreen #fgcol = rt.kOrange-3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #Legend if obj.InheritsFrom(TLegend.Class()) == True: obj.SetTextColor(fgcol) #obj.SetFillStyle(1000) #obj.SetFillColor(fgcol) #obj.SetTextColor(bgcol) #ln = TIter(obj.GetListOfPrimitives()) #lo = ln.Next() #while lo != None: #if lo.GetObject() == None: #lo = ln.Next() #continue #if lo.GetObject().InheritsFrom(TH1.Class()) == True: #hx = lo.GetObject() #hx.SetFillColor(bgcol) #if hx.GetMarkerColor() == rt.kBlack: #hx.SetMarkerColor(fgcol) #hx.SetLineColor(fgcol) #pass #lo = ln.Next() #RooHist if obj.InheritsFrom(RooHist.Class()) == True: if obj.GetMarkerColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #H2 if obj.InheritsFrom(TH2.Class()) == True: obj.SetAxisColor(fgcol, "Z") obj.SetLabelColor(fgcol, "Z") obj.GetZaxis().SetTitleColor(fgcol) #obj.SetLineColor(fgcol) #obj.SetMarkerColor(fgcol) #TLatex if obj.InheritsFrom(TLatex.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #F2 if obj.InheritsFrom(TF2.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis(), obj.GetZaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #F1 if obj.InheritsFrom(TF1.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #TGraph if obj.InheritsFrom(TGraph.Class()) == True: obj.SetFillColor(bgcol) ax = obj.GetXaxis() ay = obj.GetYaxis() ax.SetAxisColor(fgcol) ay.SetAxisColor(fgcol) ax.SetLabelColor(fgcol) ay.SetLabelColor(fgcol) ax.SetTitleColor(fgcol) ay.SetTitleColor(fgcol) if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #TGaxis if obj.InheritsFrom(TGaxis.Class()) == True: obj.SetLineColor(fgcol) obj.SetLabelColor(fgcol) obj.SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #move to next item obj = next()
################################################################################ argc = len(sys.argv) if argc < 3: print "Usage: " + sys.argv[0] + " INPUT_FILE OUTPUT_FILE" sys.exit(1) inputFileName = sys.argv[1] outputFileName = sys.argv[2] if not os.path.isfile(inputFileName): print "\"" + inputFileName + "\" does not exist!" sys.exit(1) if os.path.isfile(outputFileName): print "\"" + outputFileName + "\" already exists!" sys.exit(1) ################################################################################ ################################################################################ # Copy all keys from input file to output file ################################################################################ fin = TFile.Open(inputFileName) fout = TFile.Open(outputFileName, "recreate") nextIter = TIter(fin.GetListOfKeys()) key = nextIter() cwd = "" while key: copyAllKeys(fin, key, cwd, cwd, fout) key = nextIter() fin.Close() fout.Close() ################################################################################
from spartyjet.gui import SpartyGui from ROOT import gClient, gStyle, TFile, TTree, TIter import sys #====================================== if len(sys.argv) < 2: print 'Usage: ./guiExample.py filename [filename2 ...]' files = sys.argv[1:] # find trees trees = [] for filename in files: f = TFile(filename) treenames = [] next = TIter(f.GetListOfKeys()) k = next() while k: if k.GetClassName() == "TTree" and k.GetName() not in treenames: trees += [(k.GetName(), filename)] treenames += [k.GetName()] k = next() gui = SpartyGui(gClient.GetRoot()) [gui.addTree(tree, file) for tree,file in trees] #To manually set input branches use 'inputname' #[gui.addTree("SpartyJet_Tree", file, inputname='Clusters') for file in files] gui.init()
def load_all_hists(self, filename, prefix="", suffix="", zero_overflow=False, projection_x=None, projection_y=None, merge_ewk=False, merge_top=False, strip_suffix=False, rebin_ngroup=None, rebin_low=None, rebin_high=None, move_overflow=False, scale_bin_error=None, excluded_list=[]): # # merge histograms, works on 1d, 2d... # legend = '[TprimeData.load_all_hists]:' #print legend, excluded_list # special prefix for 1D projections prefix_1d = 'hist_' # these histograms will be merged together under name in ewkName #ewk = ['Wjets', 'WW', 'WZ', 'ZZ', 'Zjets', 'SingleToptW', 'SingleTopT', 'SM', 'QCD','SinTop'] ewk = [ 'SingleToptW', 'SingleTopT', 'SingleTopS', 'SingleTopbarT', 'SingleTopbarS', 'SinTop', 'Wjets', 'WW', 'WZ', 'ZZ', 'Zjets', 'SM', 'QCD', 'Ewk' ] ewkName = 'ewk' # these histograms will be merged together under name in topName top = ['TTjets', 'TOP', 'Top', 'top'] topName = 'top' # these histogram names will be changed to tprimeName tprime = ['TPrime', 'Tprime', 'tprime', 'TPRIME'] tprimeName = 'tprime' # these histogram names will be changed to dataName datanames = ['DATA', 'Data', 'data'] dataName = 'DATA' infile = TFile(filename, "read") _keys = TIter(infile.GetListOfKeys()) _key = _keys.Begin() _nhists = 0 while _key: _name = _key.GetName() # fix double underscores # _new_name = '' # _was_underscore = False # for _let in _name: # if _let=='_': # if _was_underscore: # print 'double underscore fixed' # else: # _was_underscore = True # _new_name += _let # else: # _new_name += _let # _was_underscore = False # # _name = _new_name if strip_suffix: #_name = _name.strip().split('_')[0] _newname = _name.strip().split('_')[0] if '__jes__plus' in _name: _newname += '__jes__plus' if '__jes__minus' in _name: _newname += '__jes__minus' _name = _newname _name = prefix + _name + suffix # merge certain histos into one if merge_ewk: for hname in ewk: #_name = _name.replace(hname+'_', ewkName+'_') _name = _name.replace(hname, ewkName) # merge top templates if merge_top: for hname in top: #_name = _name.replace(hname+'_', topName+'_') _name = _name.replace(hname, topName) # uniform signal names for hname in tprime: _name = _name.replace(hname, tprimeName) # uniform data names for hname in datanames: _name = _name.replace(hname, dataName) # skip histos from the excluded list if _name in excluded_list: _key = _keys.Next() continue _hist = copy.deepcopy(infile.Get(_key.GetName())) # scale error in each bin (in case of multiple use of the same source) _nbinx = _hist.GetNbinsX() _nbiny = None if 'TH2' in _hist.ClassName(): _nbiny = _hist.GetNbinsY() if scale_bin_error: print legend, 'histogram errors are multiplied by', float( scale_bin_error) for i in range(0, _nbinx + 2): for j in range(0, _nbiny + 2): _ibin = _hist.GetBin(i, j) _err = _hist.GetBinError(_ibin) _hist.SetBinError(_ibin, _err * float(scale_bin_error)) # move over- and underflow to adjacent visible bins # fixme: unfinished if move_overflow: _binx = _hist.GetNbinsX() _biny = None if 'TH2' in _hist.ClassName(): _biny = _hist.GetNbinsY() for i in range(0, _binx + 2): for j in range(0, _biny + 2): _ibin = GetBin(i, j) # project onto 1D if requested and the hist is 2D _class_name = _hist.ClassName() if 'TH2' in _class_name: if projection_x or projection_y: _tmpname = _name + '_tmp' _hist.SetName(_tmpname) if projection_x: _hist = copy.deepcopy( _hist.ProjectionX(_name + '_proj', 0, -1, 'e')) if projection_y: _hist = copy.deepcopy( _hist.ProjectionY(_name + '_proj', 0, -1, 'e')) _hist.SetName(_name) # rebin if requested if rebin_ngroup: self.tmphist = _hist _hist = self.Rebin1dHist(self.tmphist, rebin_ngroup, rebin_low, rebin_high, move_overflow=True) # add the hist to the list and dictionary if _name not in self.hist_names: self.hist_names.append(_name) self.hists[_name] = copy.deepcopy(_hist) else: self.hists[_name].Add(copy.deepcopy(_hist)) _nhists += 1 self.hists[_name].SetName(_name) # set True to remove overflow if zero_overflow: # zero overflow/underflow bins (assuming that they are already added to the adjacent visible bins) # print 'Will zero out overflow and underflow bins!!!' #print self.hists[_name].ClassName() if 'TH2' in self.hists[_name].ClassName(): print legend, 'TH2 detected...' print legend, 'Will zero out overflow and underflow bins (no copying)!!!' _binx = self.hists[_name].GetNbinsX() _biny = self.hists[_name].GetNbinsY() for _x in range(0, _binx + 2): for _y in range(0, _biny + 2): if _x == 0 or _x == _binx + 1 or _y == 0 or _y == _biny + 1: # sanity check #if self.hists[_name].GetBinContent(_x,_y) > 0.000001: # print 'Overflow:', self.hists[_name].GetBinContent(_x,_y), 'set to 0' self.hists[_name].SetBinContent(_x, _y, 0.0) self.hists[_name].SetBinError(_x, _y, 0.0) else: print legend, 'cannot comply with request to zero out over-/underflow for 1D hist' _key = _keys.Next() # second loop over all hists # for name in self.hist_names: # # rebin # if rebin_ngroup: # self.hists[name] = copy.deepcopy(self.Rebin1dHist(self.hists[name], # rebin_ngroup, rebin_low, rebin_high, # move_overflow = True) ) print 'Merged into', len(self.hists), 'histograms'
def LoadAllHists(self, filename, prefix="", suffix="", low=100, high=1000, binwidth=20, setOverflow=True): # # Same as load_all_hists() but reduces the hist range # Range is hardcoded at the moment 100-550 # Also, trying to merge electroweak category # # these histograms will be merged together under name in ewkName ewk = ['Wjets', 'WW', 'WZ', 'ZZ', 'Zjets', 'SingleToptW', 'SingleTopT'] ewkName = 'Ewk' infile = TFile(filename, "read") _keys = TIter(infile.GetListOfKeys()) _key = _keys.Begin() _nhists = 0 while _key: _name = _key.GetName() _name = str(prefix + _name + suffix) # merge certain electroweak histos into one for hname in ewk: _name = _name.replace(hname + '_', ewkName + '_') if _name not in self.hist_names: self.hist_names.append(_name) #_id = tuple(_name.split('_')) _nhists += 1 #self.hist_ids.append(_id) _xbins = array('d') _low = low _high = high _width = binwidth for ind in range(0, int((_high - _low) / _width + 0.5) + 1, 1): #for binLow in range(_low, int(_high + _width+0.1), _width): binLow = _low + ind * _width _xbins.append(binLow) _hist = copy.deepcopy(infile.Get(_key.GetName())) #firstOverflowBin = _hist.FindFirstBinAbove(_high-1.5*_width) # including the bin before overflow originalBinWidth = _hist.GetBinWidth( 1) # assume the input binned uniformly firstOverflowBin = int((_high - _low) / originalBinWidth) lastOverflowBin = _hist.GetNbinsX( ) + 1 # including the actual overflow overflowErr = ROOT.Double() _overflow = _hist.IntegralAndError(firstOverflowBin, lastOverflowBin, overflowErr) print originalBinWidth, firstOverflowBin, lastOverflowBin, _overflow, overflowErr if setOverflow: _hist.SetBinContent(firstOverflowBin, _overflow) _hist.SetBinError(firstOverflowBin, overflowErr) _hist = _hist.Rebin(len(_xbins) - 1, 'copyhist', _xbins) # if this hist already exists, append to it if _name in self.hists: self.hists[_name].Add(copy.deepcopy(_hist)) else: self.hists[_name] = copy.deepcopy(_hist) self.hists[_name].SetName(_name) _key = _keys.Next() print 'Loaded', len(self.hists), 'histograms'
def writeNewFile(infile, outfile, regions): infile.cd() allregions = [] nextregionkey = TIter(gDirectory.GetListOfKeys()) regionkey = nextregionkey.Next() histograms = {} while regionkey and regionkey.GetTitle(): if regionkey.IsFolder() == 1: regionname = regionkey.GetName() newregionname = getNewRegionName(regionname) histograms[newregionname] = {} allregions.append(regionname) inregiondir = infile.Get(regionname) inregiondir.cd() nextsamplekey = TIter(gDirectory.GetListOfKeys()) samplekey = nextsamplekey.Next() while samplekey: if samplekey.IsFolder() == 1: samplename = samplekey.GetName() insampledir = inregiondir.Get(samplename) insampledir.cd() hist = insampledir.Get("nominal") if samplename in histograms[newregionname]: histograms[newregionname][samplename].Add(hist) else: histograms[newregionname][samplename] = hist samplekey = nextsamplekey.Next() regionkey = nextregionkey.Next() #get the binning binning = {} for newregion in histograms.keys(): if newregion == 'llAll_cba_boost_loose_signal' or newregion == 'llAll_cba_boost_loose_df_signal' or newregion == 'llAll_cba_boost_loose_ee_signal' or newregion == 'llAll_cba_boost_loose_mumu_signal': #binning[newregion]=[60.0, 80.0, 90.0, 100.0, 110.0, 120.0, 130.0, 150.0, 190.0, 200.0] binning[newregion] = [ 60.0, 70.0, 90.0, 110.0, 130.0, 150.0, 185.0, 200.0 ] elif newregion == 'llAll_cba_boost_tight_signal' or newregion == 'llAll_cba_boost_tight_df_signal' or newregion == 'llAll_cba_boost_tight_ee_signal' or newregion == 'llAll_cba_boost_tight_mumu_signal': binning[newregion] = [ 60.0, 80.0, 90.0, 100.0, 110.0, 120.0, 130.0, 140.0, 200.0 ] #binning[newregion]=[60.0, 95.0, 100.0, 105.0, 110.0, 115.0, 120.0, 125.0, 130.0, 135.0, 140.0, 145.0, 150.0, 155.0, 160.0, 165.0, 170.0, 200.0] elif newregion == 'llAll_cba_vbf_loose_signal' or newregion == 'llAll_cba_vbf_loose_df_signal' or newregion == 'llAll_cba_vbf_loose_ee_signal' or newregion == 'llAll_cba_vbf_loose_mumu_signal': binning[newregion] = [55.0, 85.0, 100.0, 115.0, 145.0, 205.0] #binning[newregion] = [55.0, 85.0, 90.0, 95.0, 100.0, 105.0, 110.0, 115.0, 120.0, 125.0, 130.0, 135.0, 140.0, 145.0, 150.0, 155.0, 160.0, 165.0, 170.0, 175.0, 180.0, 185.0, 190.0, 205.0] elif newregion == 'llAll_cba_vbf_tight_signal' or newregion == 'llAll_cba_vbf_tight_df_signal' or newregion == 'llAll_cba_vbf_tight_ee_signal' or newregion == 'llAll_cba_vbf_tight_mumu_signal': #binning[newregion] = [55.0, 85.0, 100.0, 115.0, 145.0, 205.0] binning[newregion] = [ 55.0, 60.0, 80.0, 100.0, 120.0, 140.0, 160.0, 195.0, 205.0 ] elif newregion == 'llAll_cba_vbf_ztt' or newregion == 'llAll_cba_boost_ztt': binning[newregion] = [55.0, 85.0, 100.0, 115.0, 145.0, 205.0] binning[newregion] = [0, 100.0, 150.0, 200.0] else: print newregion binning[newregion] = getBinning(histograms[newregion]) print "Binning for region ", newregion, " -> ", binning[newregion] #now write output file infile.cd() nextregionkey = TIter(gDirectory.GetListOfKeys()) regionkey = nextregionkey.Next() while regionkey and regionkey.GetTitle(): if regionkey.IsFolder() == 1: regionname = regionkey.GetName() newregionname = getNewRegionName(regionname) outfile.cd() outfile.mkdir(regionname) outregiondir = outfile.Get(regionname) infile.cd() inregiondir = infile.Get(regionname) inregiondir.cd() nextsamplekey = TIter(gDirectory.GetListOfKeys()) samplekey = nextsamplekey.Next() while samplekey: if samplekey.IsFolder() == 1: samplename = samplekey.GetName() outregiondir.cd() outregiondir.mkdir(samplename) outsampledir = outregiondir.Get(samplename) inregiondir.cd() insampledir = inregiondir.Get(samplename) insampledir.cd() nextsystkey = TIter(gDirectory.GetListOfKeys()) systkey = nextsystkey.Next() while systkey: obj = systkey.ReadObj() if obj.IsA().InheritsFrom("TH1"): systname = systkey.GetName() outsampledir.cd() outhist = rebin(obj, binning[newregionname], outsampledir) outhist.Write() systkey = nextsystkey.Next() else: #take care of lumi histogram obj = samplekey.ReadObj() if obj.IsA().InheritsFrom("TH1"): newobj = obj.Clone() outregiondir.cd() newobj.SetDirectory(outregiondir) newobj.Write() samplekey = nextsamplekey.Next() regionkey = nextregionkey.Next()
histo.GetXaxis().GetBinUpEdge(i)) + "]" for j in range(1, histo.GetYaxis().GetNbins() + 1): yBinValue = yaxisName + ":[" + str( histo.GetYaxis().GetBinLowEdge(j)) + "," + str( histo.GetYaxis().GetBinUpEdge(j)) + "]" yBins[yBinValue] = getValueError(histo.GetBinContent(i, j), histo.GetBinError(i, j)) xBins[xBinValue] = yBins return xBins data = {} rootoutput = TFile.Open(inputTree) nextkey = TIter(rootoutput.GetListOfKeys()) key = nextkey.Next() while (key): #loop if key.IsFolder() != 1: continue if DEBUG == 1: print "debug key ", key.GetTitle() directory = rootoutput.GetDirectory(key.GetTitle()) keyInDir = TIter(directory.GetListOfKeys()) subkey = keyInDir.Next() efficienciesForThisID = {} while (subkey): if "efficiencies" in subkey.GetName() and GetEffJSON_DATA == 1: directory2 = rootoutput.GetDirectory(key.GetTitle() + "/" + subkey.GetName()) keyInDir2 = TIter(directory2.GetListOfKeys())
def listContentOfPad(c1): next = TIter(c1.GetListOfPrimitives()) while True: obj = next() if not obj: break print obj.ClassName(), obj.GetName(), obj.GetTitle()
def PlotHistsRecursive(the_dir, path, level, pdf_fname): level += 1 # loop over all keys in the current directory, ROOT-style key_iter = TIter(the_dir.GetListOfKeys()) key = key_iter() if (level == 2): pdf_fname = the_dir.GetName() first[pdf_fname] = True while (key): obj = key.ReadObj() obj_pathname = path + "/" + key.GetName() c1.Clear() # if the object is a histogram, then see if we should plot it if (isinstance(obj, TH1)): print "plotting " + obj_pathname obj.SetTitle(obj_pathname) h2 = f2.Get(obj_pathname) h2.SetTitle(obj_pathname) # plot histogram if (isinstance(obj, TH2)): obj.SetTitle(obj_pathname + " - HG3") h2.SetTitle(obj_pathname + " - HG4") c1.Divide(1, 2) c1.cd(1) plot_2dhist(obj) c1.cd(2) plot_2dhist(h2) # save image to disk if first[pdf_fname] == True: c1.Print(pdf_fname + ".pdf(") else: c1.Print(pdf_fname + ".pdf") key = key_iter() continue # self.plot_2dhist(obj) #else: # self.plot_hist(obj) #obj.Rebin() #h2.Rebin() plot_hist(obj) plot_overlay_hist(h2) leg = TLegend(0.72, 0.77, 0.9, 0.9) leg.AddEntry(obj, "hdgeant", "l") leg.AddEntry(h2, "hdgeant4", "l") leg.Draw() # save image to disk if first == True: c1.Print(pdf_fname + ".pdf(") else: c1.Print(pdf_fname + ".pdf") if ratios == True: h1f = TH1F(obj) h2f = TH1F(h2) h1f.Divide(h2f) plot_ratio_hist(h1f) c1.Print("out.pdf") # if the object is a directory, access what's inside if (isinstance(obj, TDirectory)): PlotHistsRecursive(obj, obj_pathname, level, pdf_fname) # END OF LOOP - move to next item in the directory key = key_iter() del key_iter