def TH1_to_TH2_Convert(filename, outputpath=''): FILELIST = read_file_name(filename) fn = FILELIST[0]; print(fn) histo_name_list = GetHistoNameList(filename); print(histo_name_list) # print(BranchName(histo_name_list[0])) # print Exclued_BranchName(histo_name_list[0]) indicator = 0 histList = [] f = TFile(filename,"READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next(); #print(key.GetName()) while key: dirlistb = f.GetListOfKeys() ITERb = dirlistb.MakeIterator() keyb = ITERb.Next() ijk = 0 while keyb: if(Exclued_BranchName(key.GetName()) == Exclued_BranchName(keyb.GetName())): if(key.GetName() != keyb.GetName()): Name2DHist = Exclued_BranchName(key.GetName()) + "Y_" + BranchName(key.GetName()) + "_X_" + BranchName(keyb.GetName()) # hist = TH2D print(Name2DHist) indicator = indicator + 1 keyb = ITERb.Next() key = ITER.Next() print(indicator)
def Open(path): from ROOT import TFile f = TFile(path, 'read') if len(f.GetListOfKeys()) > 0: run_numbers = [key.GetName() for key in f.GetListOfKeys()] return f, run_numbers else: return f, None
def add_signal(fname, process, signal_file, signal, scale): print 'adding signal', signal, ' with cross section', scale, 'to', process print 'working file', fname, 'signal file', signal_file signal_file = TFile(signal_file, 'READ') work_file = TFile(fname, 'UPDATE') keys = work_file.GetListOfKeys() signal_keys = signal_file.GetListOfKeys() original = [] for key in keys: key = str(key.GetName()) if len(key.split('__')) > 3: continue category = key.split("__")[0] #print key, category if '*' in process: process_split = process.split('*') if not all(part in key for part in process_split): continue #print 'matched',process,'with',key elif process not in key: continue for signal_key in signal_keys: signal_key = str(signal_key.GetName()) #print 'comparing',key, signal_key sig_cat = signal_key.split("__")[0] #print signal, signal_key.split('__')[1] if signal.replace('-', '_') not in signal_key.split("__")[1].replace( '-', '_'): continue #print category, sig_cat if category == sig_cat: print 'adding', signal_key, 'to', key sig_hist = signal_file.Get(signal_key).Clone() #original.append(work_file.Get(key).Clone(key)) #binning = get_binning(original[-1]) hist = work_file.Get(key).Clone(key) work_file.Delete(key + ';*') binning = get_binning(hist) #print binning if binning: sig_hist = sig_hist.Rebin( len(binning) - 1, key, array.array('d', binning)) sig_hist.Scale(scale) #print 'adding',sig_hist.GetIntegral(), 'to', original[-1].GetIntegral() #original[-1].Add(sig_hist) hist.Add(sig_hist) original.append(hist) #.Write('',TObject.kOverwrite) break signal_file.Close() work_file.cd() for item in original: item.Write('') #,TObject.kOverwrite); work_file.Close()
def Fill_histograms(FILENAME, BRANCHLISTALL, DICHISTLIST, BranchListEachTree): gBenchmark.Start("Filling & Writing Histograms") # print("!@#!@#!#",DICHISTLIST.keys()) # print("!@#!@#!",BRANCHLISTALL) DicNumpyArray_branch = {} for numpyarray in BRANCHLISTALL: a = numpy.array([0], 'd') DicNumpyArray_branch[numpyarray] = a DicNumpyArray_branch = collections.OrderedDict( sorted(DicNumpyArray_branch.items()) ) # !!!the input times are ordered!!! # print("!@#!@#!#",DicNumpyArray_branch) f = TFile(FILENAME, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() while key: it = 0 tree = key.ReadObj() # print("!@#!@#!#",DICHISTLIST[tree.GetName()]) for i in range(len(DicNumpyArray_branch)): if (list(DicNumpyArray_branch.keys())[i] in BranchListEachTree[tree.GetName()]): it = it + 1 pass else: continue tree.SetBranchAddress( list(DicNumpyArray_branch.keys())[i], list(DicNumpyArray_branch.values())[i]) if (it == 0): continue ENTRY = tree.GetEntries() print("for tree", tree.GetName()) for i in range(ENTRY): tree.GetEntry(i) if (i % 1000 == 0): print("now looping", i, "th Events, total of ", ENTRY, "events") # for j in range(len(DICHISTLIST[tree.GetName()])): for j in range(len(DicNumpyArray_branch)): for k in range(len(DICHISTLIST[tree.GetName()])): if (list(DicNumpyArray_branch.keys())[j] in DICHISTLIST[tree.GetName()][k].GetName()): DICHISTLIST[tree.GetName()][k].Fill( list(DicNumpyArray_branch.values())[j][0]) else: continue #DICHISTLIST[tree.GetName()][k].Scale(DicNumpyArray_branch["JWeight"][j][0]) key = ITER.Next() # print(DICHISTLIST) # gBenchmark.Show("filling") f.Close() return DICHISTLIST
def signal_rebin(rebin_file, signal="Bprime", process='DATA'): #print 'starting the rebinning of',rebin_file work_file = TFile(rebin_file, "UPDATE") keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) if process in key: hist = work_file.Get(key) binning = get_binning(hist) for other in keys: other = str(other.GetName()) if not key.split('__')[0] in other: continue if not signal in other: continue #if len(other.split('__'))>2:continue if not process in other: #print other work_hist = work_file.Get(other) hist_list.append( work_hist.Rebin( len(binning) - 1, other, array.array('d', binning))) #work_hist.Write('',TObject.kOverwrite) for item in hist_list: item.Write('', TObject.kOverwrite) work_file.Close() del hist_list
def sys_reweight(filename, weight=''): work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() nominal_hists = [] uncer_list = [] for key in keys: key = str(key.GetName()) if 'DATA' in key or 'Data' in key or 'Background' in key or 'Signal' in key: continue if 'minus' in key or 'plus' in key: uncer = key.split('__')[2] if uncer not in uncer_list: uncer_list.append(uncer) continue if key not in nominal_hists: nominal_hists.append(key) for hist in nominal_hists: #print hist nominal_number = (work_file.Get(hist)).Integral() for uncer in uncer_list: if weight not in uncer: continue up = work_file.Get(hist + '__' + uncer + '__plus') down = work_file.Get(hist + '__' + uncer + '__minus') #print 'scale plus:',nominal_number/up.Integral(),'minus',nominal_number/down.Integral() up.Scale(nominal_number / up.Integral()) down.Scale(nominal_number / down.Integral()) up.Write('', TObject.kOverwrite) down.Write('', TObject.kOverwrite) work_file.Close() del nominal_hists
def add_substraction_unc(filename, signal_file, signal, values): work_file = TFile(filename, 'UPDATE') signal_file = TFile(signal_file, 'READ') signal_cross = values[0] uncer = values[1] hist_list = [] keys = work_file.GetListOfKeys() for key in keys: key = str(key.GetName()) if len(key.split('__')) > 2: continue if not 'Background' in key: continue category = key.split('__')[0] hist_up = work_file.Get(key).Clone(key + '__signalSub__plus') hist_down = work_file.Get(key).Clone(key + '__signalSub__minus') signal_hist = signal_file.Get(category + '__' + signal).Clone() binning = get_binning(hist_up) if binning: signal_hist = signal_hist.Rebin( len(binning) - 1, key, array.array('d', binning)) signal_hist.Scale(uncer) hist_up.Add(signal_hist) hist_down.Add(signal_hist, -1) hist_list.append(hist_up) hist_list.append(hist_down) work_file.cd() for item in hist_list: item.Write('', TObject.kOverwrite) work_file.Close() signal_file.Close() print 'added signal substraction uncertainty to', filename
def invert_bin_content(in_file, process="", out_file="", factor=-1.): if not out_file: out_file = in_file.replace(".root", "_inverted.root") work_file = TFile(in_file, 'READ') keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) if len(key.split('__')) == 2: hist = work_file.Get(key).Clone( key.split('__')[0] + '__' + process) else: sign = '' if 'minus' in key.split('__')[3]: sign = 'plus' else: sign = 'minus' hist = work_file.Get(key).Clone( key.split('__')[0] + '__' + process + '__' + key.split('__')[2] + '__' + sign) #hist = work_file.Get(key).Clone(key.split('__')[0]+'__'+process+'__'+key.split('__')[2]+'__'+key.split('__')[3]) for i in xrange(hist.GetNcells()): hist.SetBinContent(i, factor * hist.GetBinContent(i)) hist_list.append(hist) print out_file output = TFile(out_file, 'RECREATE') for item in hist_list: item.Write('', TObject.kOverwrite) output.Close() work_file.Close() return out_file
def GetNumberOfEvents(FilesSet): NumberOfEvents = {'SkimNumber' : {}, 'TotalNumber' : 0} for histFile in FilesSet: ScoutFile = TFile(histFile) if ScoutFile.IsZombie(): print histFile + " is a bad root file." FilesSet.remove(histFile) continue randomChannelDirectory = "" TotalNumberTmp = 0 for key in ScoutFile.GetListOfKeys(): if key.GetClassName() != "TDirectoryFile" or "CutFlow" not in key.GetName(): continue randomChannelDirectory = key.GetName() channelName = randomChannelDirectory[0:len(randomChannelDirectory)-14] if not NumberOfEvents['SkimNumber'].has_key(channelName): NumberOfEvents['SkimNumber'][channelName] = 0 OriginalCounterObj = ScoutFile.Get(randomChannelDirectory + "/eventCounter") SkimCounterObj = ScoutFile.Get(randomChannelDirectory + "/cutFlow") TotalNumberTmp = 0 if not OriginalCounterObj: print "Could not find eventCounter histogram in " + str(histFile) + " !" continue elif not SkimCounterObj: print "Could not find cutFlow histogram in " + str(histFile) + " !" else: OriginalCounter = OriginalCounterObj.Clone() OriginalCounter.SetDirectory(0) TotalNumberTmp = TotalNumberTmp + OriginalCounter.GetBinContent(1) SkimCounter = SkimCounterObj.Clone() SkimCounter.SetDirectory(0) NumberOfEvents['SkimNumber'][channelName] = NumberOfEvents['SkimNumber'][channelName] + SkimCounter.GetBinContent(SkimCounter.GetXaxis().GetNbins()) NumberOfEvents['TotalNumber'] = NumberOfEvents['TotalNumber'] + TotalNumberTmp return NumberOfEvents
def sculp_categories(filename, category=[]): print 'working on', filename work_file = TFile(filename, 'READ') keys = work_file.GetListOfKeys() out = [] if not category: for key in keys: key = str(key.GetName()) cat = key.split('__')[0] cat = cat.replace('Ele', '') cat = cat.replace('Mu', '') if cat not in category: category.append(cat) print category for cat in category: hists = [] for key in keys: key = str(key.GetName()) if cat not in key: continue hists.append(work_file.Get(key).Clone()) out_name = filename.replace('.root', '_' + cat + '.root') out.append(out_name) output = TFile(out_name, 'RECREATE') output.cd() for i in hists: i.Write() output.Close() work_file.Close() return out
def create_significance_files(filename, signal): print 'starting to create significance file from', filename work_file = TFile(filename, 'READ') keys = work_file.GetListOfKeys() categories_done = [] hist_sig = [] hist_other = [] masses = [] for key in keys: key = str(key.GetName()) if signal in key: hist_sig.append(work_file.Get(key).Clone()) else: hist_other.append(work_file.Get(key).Clone()) for h in hist_sig: name = h.GetName() name = name.replace('10p', '') name = name.replace('20p', '') name = name.replace('30p', '') name = ''.join(x for x in name if x.isdigit()) masses.append(name) output = TFile( filename.replace('.root', name + '_' + signal + '.root'), 'RECREATE') output.Cd() h.Write() for o in hist_other: o.Write() output.Close() work_file.Close() return masses
def load_trees_from_file(file_name): """ Load all TTree from a ROOT file @param file_name Name of ROOT file @return dict with name of tree as key and the tree as value, ROOT file """ if not os.path.isfile(file_name): raise IOError("File %s does not exist." % file_name) root_file = TFile(file_name) if root_file.IsZombie(): raise IOError("Can't open root file %s." % file_name) keys = root_file.GetListOfKeys() trees = {} for key in keys: tree = root_file.Get(key.GetName()) if isinstance(tree, TTree): trees[key.GetName()] = tree return trees, root_file
def Fill_histograms(FILENAME, BRANCHLISTALL, DICHISTLIST): DicNumpyArray_branch = {} for numpyarray in BRANCHLISTALL: a = numpy.array([0], 'd') DicNumpyArray_branch[numpyarray] = a f = TFile(FILENAME, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() while key: tree = key.ReadObj() for i in range(len(DicNumpyArray_branch)): tree.SetBranchAddress(DicNumpyArray_branch.keys()[i], DicNumpyArray_branch.values()[i]) ENTRY = tree.GetEntries() for i in range(ENTRY): tree.GetEntry(i) for j in range(len(DICHISTLIST[tree.GetName()])): for k in range(len(DICHISTLIST[tree.GetName()])): if (DicNumpyArray_branch.keys()[j] in DICHISTLIST[tree.GetName()][k].GetName()): DICHISTLIST[tree.GetName()][k].Fill( DicNumpyArray_branch.values()[j][0]) else: continue key = ITER.Next() # print(DICHISTLIST) return DICHISTLIST
def _guessTreeName(self, pattern): """ Find the set of keys of all TTrees in all files matching pattern. If the set contains only one key Returns: the TTree key else raises ValueError. """ names = [] for fnam in self.files: rfile = TFile(fnam) for key in rfile.GetListOfKeys(): obj = rfile.Get(key.GetName()) if type(obj) is TTree: names.append( key.GetName() ) thename = set(names) if len(thename)==1: return list(thename)[0] else: err = [ 'several TTree keys in {pattern}:'.format( pattern=pattern ), ','.join(thename) ] raise ValueError('\n'.join(err))
def get_branch_list_each_tree(self): PATH_included_root = read_file_name_root(self._infile)[2] DicTreeBranchNameList = {} f = TFile(PATH_included_root, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() while key: BranchNameList = [] tree = key.ReadObj() branchlist = tree.GetListOfBranches() if (branchlist.IsEmpty()): continue ITER_b = branchlist.MakeIterator() key_b = ITER_b.Next() while key_b: BranchNameList.append(key_b.GetName()) key_b = ITER_b.Next() DicTreeBranchNameList[tree.GetName()] = BranchNameList key = ITER.Next() f.Close() return DicTreeBranchNameList
def merge_channel(in_file, channel=[], out_file=None): if not out_file: out_file = in_file.replace('.root', '_mergedChannel.root') work_file = TFile(in_file, "READ") keys = work_file.GetListOfKeys() hist_list = [] categories = [] for key in keys: key = str(key.GetName()) splitname = key.split('__') category = splitname[0] for item in channel: category = category.replace(item, '') process = splitname[1] new_name = category + '__' + process if new_name not in categories: categories.append(new_name) hist_list.append(work_file.Get(key).Clone(new_name)) else: for i, cat in enumerate(categories): if new_name in cat: hist_list[i].Add(work_file.Get(key)) work_file.Close() output_file = TFile(out_file, 'RECREATE') for hist in hist_list: hist.Write('', TObject.kOverwrite) output_file.Close() return out_file
def compute(filename): warnings.simplefilter("ignore") file = TFile(filename) keys = file.GetListOfKeys() print 'input file:', filename tuple_store = [] for key in keys: key = key.GetName() if 'Background' in key or 'plus' in key or 'minus' in key or 'DATA' in key: continue signalname = key.split('__')[1] histogram = file.Get(key) #entries = float(histogram.GetSumOfWeights()) entries = float(histogram.GetEntries()) found_sig = False if tuple_store: for item in tuple_store: if item.name == signalname: #print 'add',key,entries, 'to', item.name, 'sum',item.entries+entries item.Add(entries, histogram) found_sig = True break if not found_sig: #print 'creating',signalname,entries,'with', key tuple_store.append(signal_store(entries, signalname, histogram)) #print tuple_store tuple_store.sort(key=lambda x: x.mass) return tuple_store
def get_branch_list_each_tree(PATH_included_root): from ROOT import TTree, TFile DicTreeBranchNameList = {} f = TFile(PATH_included_root, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() while key: BranchNameList = [] tree = key.ReadObj() branchlist = tree.GetListOfBranches() if (branchlist.IsEmpty()): continue ITER_b = branchlist.MakeIterator() key_b = ITER_b.Next() while key_b: BranchNameList.append(key_b.GetName()) key_b = ITER_b.Next() DicTreeBranchNameList[tree.GetName()] = BranchNameList key = ITER.Next() return DicTreeBranchNameList
def randomize_hists(filename, contains, seed=0): print 'randomize hists in', filename, work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() hist_list = [] random.seed(seed) for key in keys: key = str(key.GetName()) #print key if not contains in key: continue hist = work_file.Get(key).Clone() work_file.Delete(key + ';*') for i in xrange(hist.GetNcells()): error = hist.GetBinError(i) #if error ==0 and hist.GetBinContent(i)==0: error =1 new = (random.random() * 2 - 1) * error + hist.GetBinContent(i) m = 0 while (new < 0 and m < 1000): m += 1 new = (random.random() * 2 - 1) * error + hist.GetBinContent(i) if (new < 0): new = 0 print key, 'new content', new, 'old', hist.GetBinContent( i), 'error', error hist.SetBinContent(i, new) hist_list.append(hist) for item in hist_list: item.Write()
def getCutflow( ecm="161" ): sampleCounters= dict() filename= "sjm"+ecm+".root" if ecm == "91": filename= "sjm"+ecm+"_all.root" f= TFile( filename ) eventCountsHisto= f.Get( "Eventcounts" ) eventCounts= unpackAlphanumericHisto( eventCountsHisto ) keyList= f.GetListOfKeys() MCsamples= [ "Signal", "AltSignal", "BkgWWqqqq", "BkgWWllqq", "BkgWWeeqq" ] samples= [ "Data" ] + MCsamples countersKeys= [ key.GetTitle() for key in keyList if key.GetTitle() in samples ] for counterskey in countersKeys: hist= f.Get( counterskey ) if hist: counters= unpackAlphanumericHisto( hist ) counters["events"]= eventCounts[counterskey] sampleCounters[counterskey]= counters import SjmConfigParser cfgFilename= "sjmconfig_"+ecm+".cfg" config= SjmConfigParser.readConfig( [ cfgFilename ] ) lumi= getFromConfig( config, "Data", "lumi" ) sampleCounters["Data"]["lumi"]= lumi for section in MCsamples: xsec= getFromConfig( config, section, "xsec" ) if xsec: sampleCounters[section]["xsec"]= xsec return sampleCounters
def readHists(h, fname, wanted=[], withProjections=True): if fname[0:4] == "/eos": eospath = gSystem.Getenv("EOSSHIP") + fname f = TFile.Open(eospath) else: f = TFile(fname) for akey in f.GetListOfKeys(): name = akey.GetName() try: hname = int(name) except: hname = name if len(wanted) > 0: if not hname in wanted: continue obj = akey.ReadObj() cln = obj.Class().GetName() if not cln.find('TCanv') < 0: h[hname] = obj.Clone() if cln.find('TH') < 0: continue if h.has_key(hname): rc = h[hname].Add(obj) if not rc: print "Error when adding histogram ", hname else: h[hname] = obj.Clone() if h[hname].GetSumw2N() == 0: h[hname].Sumw2() h[hname].SetDirectory(gROOT) if (cln == 'TH2D' or cln == 'TH2F') and withProjections: for p in ['_projx', '_projy']: if type(hname) == type('s'): projname = hname + p else: projname = str(hname) + p if p.find('x') > -1: h[projname] = h[hname].ProjectionX() else: h[projname] = h[hname].ProjectionY() h[projname].SetName(name + p) h[projname].SetDirectory(gROOT) return
def showcase_set_branch_address(BRANCHLISTALL, PATH_FILE_NAME): from ROOT import TTree, TFile import numpy DicNumpyArray_branch = {} for numpyarray in BRANCHLISTALL: a = numpy.array([0], 'd') DicNumpyArray_branch[numpyarray] = a f = TFile(PATH_FILE_NAME, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() while key: tree = key.ReadObj() for i in range(len(DicNumpyArray_branch)): tree.SetBranchAddress(DicNumpyArray_branch.keys()[i], DicNumpyArray_branch.values()[i]) #print(DicNumpyArray_branch.keys()[i]) key = ITER.Next() print( "!!! This is just a showcase of SetBranchAddress, not really set !!!") return DicNumpyArray_branch
def fillLists(): # List files and fill categories for c in os.listdir('rootfiles_' + options.name + '/'): if c.endswith('root'): categories.append(c.replace('.root', '')) # Read file and histograms inFile = TFile("rootfiles_" + options.name + "/" + categories[0] + ".root", "READ") inFile.cd() for key in inFile.GetListOfKeys(): obj = key.ReadObj() if obj.IsA().InheritsFrom("TH1"): name = obj.GetName() if 'DM' in name: sign.append(name) elif not "data_obs" in name and not "BkgSum" in name: back.append(name) # Categories (directories) if obj.IsFolder(): subdir = obj.GetName() subdir = subdir.replace('Up', '').replace('Down', '') print subdir if not subdir in greenShape: continue if not subdir in shape: shape.append(subdir) inFile.Close()
def set_histograms(FILENAME, S_FILENAME, HISTO_XRANGE, NBins=100): f = TFile(FILENAME, "READ") dirlist = f.GetListOfKeys() ITER = dirlist.MakeIterator() key = ITER.Next() DichistList = {} # DichistList = dict() while key: histList = [] NamehistList = [] tree = key.ReadObj() branchlist = tree.GetListOfBranches() if (branchlist.IsEmpty()): continue ITER_b = branchlist.MakeIterator() key_b = ITER_b.Next() while key_b: Namehist = S_FILENAME + "_" + tree.GetName() + "_" + key_b.GetName( ) + "_hist" # print(Namehist) for j in range(len(HISTO_XRANGE[tree.GetName()])): if key_b.GetName() in HISTO_XRANGE[tree.GetName()].keys()[j]: hist = TH1D(Namehist, Namehist, NBins, HISTO_XRANGE[tree.GetName()].values()[j][0], HISTO_XRANGE[tree.GetName()].values()[j][1]) histList.append(hist) else: continue key_b = ITER_b.Next() DichistList[tree.GetName()] = histList key = ITER.Next() f.Close() print(DichistList) return DichistList
def download_objects(input_file="t.root", out_path="/tmp/ccdbtest2/", host="http://ccdb-test.cern.ch:8080", overwrite=False): msg("Downloading CCDB objects from input file", input_file) out_path = os.path.normpath(out_path) f = TFile(input_file, "READ") lk = f.GetListOfKeys() obj_done = [] for i in lk: name = i.GetName() cycle = i.GetCycle() if name in obj_done: continue obj_done.append(name) obj = f.Get(f"{name};{cycle}") name = name.replace("--", "/") limits = [int(obj.GetPointY(j)) for j in range(obj.GetN())] verbose_msg(name, len(limits), "First", limits[0], convert_timestamp(limits[0]), "Last", limits[-1], convert_timestamp(limits[-1])) for j in limits: get_ccdb_obj(name, j, out_path=out_path, host=host, show=False, verbose=True, tag=True, overwrite_preexisting=overwrite) f.Close() for i in obj_downloaded: msg("Downloaded", obj_downloaded[i], i)
def pyMakeClass(options, arguments): from ROOT import TFile, TTree inFile = TFile(options.inputSample) fileKeyNames = [] for key in inFile.GetListOfKeys(): fileKeyNames.append(key.GetName()) if options.debug: pprint(fileKeyNames) if "/" in options.treeName: if options.debug: print "looks like the tree name specified is within some TDirectory structure" treePath = options.treeName.split("/") treeName = treePath.pop() for tdirectory in treePath: for dirKeys in inFile.Get(tdirectory).GetListOfKeys(): fileKeyNames.append(dirKeys.GetName()) inFile.cd(tdirectory) if options.debug: print "found these keys in the TDirectory structure:" pprint(fileKeyNames) else: treeName = options.treeName if not treeName in fileKeyNames: print "Error: could not find a tree named %s in %s" % ( options.treeName, options.inputSample) inFile.ls() exit(1) else: inTree = inFile.Get(options.treeName) inTree.MakeClass(options.className)
def getRegionsVars(): """ Get processed regions from hists produced """ f = TFile("hists/%s.root" % inputsamples[0]) keys = [name.GetName() for name in f.GetListOfKeys()] #first get all the region and var names of hists dupregions = [] dupvars = [] for key in keys: pos = key.find('_') dupregions.append(key[0:pos]) dupvars.append(key[pos + 1:len(key)]) #strip out the repeated region names regions = [] for x in dupregions: if x not in regions: regions.append(x) #strip out the repeated var names varnames = [] for x in dupvars: if x not in varnames: varnames.append(x) print 'Regions are processed: ' for region in regions: print region print 'variables are filled: ' for varname in varnames: print varname return regions, varnames
def readHists(h, fname): if fname[0:4] == "/eos": eospath = "root://eoslhcb.cern.ch/" + fname f = ROOT.TFile.Open(eospath) else: f = TFile(fname) for akey in f.GetListOfKeys(): name = akey.GetName() try: hname = int(name) except: hname = name obj = akey.ReadObj() cln = obj.Class().GetName() if not cln.find('TCanv') < 0: h[hname] = obj.Clone() if cln.find('TH') < 0: continue if h.has_key(hname): rc = h[hname].Add(obj) if not rc: print "Error when adding histogram ", hname else: h[hname] = obj.Clone() if h[hname].GetSumw2N() == 0: h[hname].Sumw2() h[hname].SetDirectory(gROOT) if cln == 'TH2D' or cln == 'TH2F': for p in ['_projx', '_projy']: if type(hname) == type('s'): projname = hname + p else: projname = str(hname) + p if p.find('x') > -1: h[projname] = h[hname].ProjectionX() else: h[projname] = h[hname].ProjectionY() h[projname].SetName(name + p) h[projname].SetDirectory(gROOT) return
class Raw: def __init__(self, c: Converter, load_file=False): self.Parent = c self.RunNumber = c.RunNumber self.SoftDir = c.SoftDir.joinpath(c.Config.get('SOFTWARE', 'eudaq2')) self.DataDir = c.DataDir self.SaveDir = c.SaveDir self.FilePath = self.load_file_path() self.OutFilePath = c.SaveDir.joinpath(f'run{self.RunNumber:06d}.root') if load_file: self.F = TFile(str(self.OutFilePath)) if load_file else None self.P = [ self.F.Get(key.GetName()).Get('Hits') for key in self.F.GetListOfKeys() if key.GetName().startswith('Plane') ] self.Draw = Draw(c.Config.FilePath) def __repr__(self): return f'Raw file analysis run {self.RunNumber} ({self.FilePath.name})' def load_file_path(self): n = list( self.DataDir.joinpath('raw').glob(f'run{self.RunNumber:06d}*.raw')) return n[0] if len(n) else None def generate_fit_files(self): c = None for dut in range(self.Parent.NDUTPlanes): c = self.Parent.get_calibration(dut) if not c.FitFileName.exists(): c.save_fit_pars() return c.CalPath def convert(self): """ convert binary raw file to root file with eudaq""" cal_path = self.generate_fit_files() if self.FilePath is None: critical('raw file does not exist for run: {}'.format( self.RunNumber)) self.OutFilePath.parent.mkdir(exist_ok=True) cmd = f'{self.SoftDir.joinpath("bin", "euCliConverter")} -i {self.FilePath} -o {self.OutFilePath} -c {cal_path}' info( f'Convert {self.FilePath.name} to {self.OutFilePath.name} using EUDAQ-2\n' ) info(f'{cmd}\n') check_call(cmd, shell=True) for f in Path().glob('AutoDict_vector*'): remove_file(f) def make_cuts(self): [t.SetEstimate(t.GetEntries() * 10) for t in self.P] n = [get_tree_vec(t, 'NHits', dtype='i2') for t in self.P[:6]] c = all([i == 1 for i in n], axis=0) return [c.repeat(i) for i in n]
def getTasksFromRootfile(filePath): file = TFile(filePath, 'READ') keys = file.GetListOfKeys() keyNames = [key.GetName() for key in keys] #log.logDebug("%s" % keyNames) file.Close() return keyNames