def totalNumberOfGeneratedEvents(path, source="", modifier=""): """ path: path to directory containing all sample files returns dict samples names -> number of simulated events in source sample (note these include events without EMu EMu EMu signature, too ) """ from ROOT import TFile result = {} #~ print path if "baseTrees" in source: for sampleName, filePath in getFilePathsAndSampleNames( path, source).iteritems(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny( "analysis paths").GetBinContent(1) for sampleName, filePath in getFilePathsAndSampleNames( path, "baseTrees").iteritems(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny( "analysis paths").GetBinContent(1) else: for sampleName, filePath in getFilePathsAndSampleNames( path, source, modifier).iteritems(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny( "analysis paths").GetBinContent(1) return result
def CheckIfFileIsReweighted(self, file, lumi): """Checks if the file has already been reweighted to a given luminosity""" #print "checking the file ",file verbose = True #the file to investigate is: FileGuess = file.replace('.root', '_RWTo' + str(int(lumi)) + '.root') if verbose: print 'INVESTIGATING ', FileGuess histoname = 'RWto' + str(int(lumi)) if commands.getoutput('ls ' + str(FileGuess)).find('No such file') != -1: #the file is not there yet, so now rw before if verbose: print 'file not found, so no RW is assumed' return False, FileGuess else: dummy = TFile(FileGuess, "READ") # # outfind = dummy.FindObjectAny(histoname) if str(outfind).find("nil") != -1: #the file has been not reweighted before 'histo not found' if verbose: print 'the file is there but the histo isn\'t' isRW = False else: #check that it was reweighted AFTER this file #was produced filetime = GetDateSeconds(file) dateofrw = dummy.FindObjectAny("date_of_reweighting_to_" + str(int(lumi))) if str(dateofrw).find('nil') == -1: rwtime = int(dateofrw.GetTitle()) #print 'the filetime is ',filetime #print 'the rwtime is ',rwtime #print 'rw file newer? ',int(rwtime)>int(filetime) #raw_input('continue') # # isRW = int(rwtime) > int(filetime) if verbose: print ' rwtime is ', rwtime print ' filetime is ', filetime print ' ', (rwtime) > int(filetime) else: print 'the histo date_of_reweighting_to_' + str( int(lumi)) + ' was not found' isRW = False # #print 'returning ',isRW,FileGuess if isRW: yesorno = 'HAVE' else: yesorno = 'have NOT' print ' it has been determined that the files ' + yesorno + ' have been reweighted' return isRW, FileGuess
def negWeightFractions(path,muon=True): """ path: path to directory containing all sample files returns dict samples names -> fraction of events wiBackgrounds2016th negative weights in source sample """ from ROOT import TFile result = {} for sampleName, filePath in getFilePathsAndSampleNames(path,muon).items(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny("weights").GetBinContent(1)/(rootFile.FindObjectAny("weights").GetBinContent(1)+rootFile.FindObjectAny("weights").GetBinContent(2)) return result
def negWeightFractions(path,muon=True,genMass=False): """ path: path to directory containing all sample files returns dict samples names -> fraction of events wiBackgrounds2016th negative weights in source sample """ from ROOT import TFile result = {} for sampleName, filePath in getFilePathsAndSampleNames(path,muon).items(): if genMass: if "CI" in filePath or "ADD" in filePath or "2018" in filePath or "2016" in filePath: continue filePath = filePath.replace("dileptonAna_muons","dileptonAna_genMassOther") filePath = filePath.replace("dileptonAna_electrons","dileptonAna_genMassOther_electrons") rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny("weights").GetBinContent(1)/(rootFile.FindObjectAny("weights").GetBinContent(1)+rootFile.FindObjectAny("weights").GetBinContent(2)) return result
def OLDCheckIfFileIsReweighted(self, file): dummy = TFile(file, "READ") outfind = dummy.FindObjectAny("isReweighted") if str(outfind).find("nil") != -1: #the file has been not reweighted before isRW = False else: isRW = True # return isRW
def plot_histograms(input_file_name, charge, par, par_name, unit): input_file = TFile(input_file_name, "READ") h_gen = input_file.FindObjectAny("pars_" + charge + "_" + par) h_reco = input_file.FindObjectAny("EstimatedPar_" + charge + "_" + par) h_error = input_file.FindObjectAny("EstimatedParError_" + charge + "_" + par) h_relError = input_file.FindObjectAny("EstimatedParRelError_" + charge + "_" + par) c = TCanvas("c", "c", 800, 800) c.Divide(2, 2) c.cd(1) l_1 = draw_superimposed(h_gen, h_reco, par_name, par_name + " " + unit) c.cd(2) draw_and_fit(h_error, par_name + "_{gen} - " + par_name + "_{reco} " + unit) c.cd(3) draw_and_fit( h_relError, "(" + par_name + "_{gen} - " + par_name + "_{reco})/" + par_name + "_{gen}") return c, l_1, h_gen, h_reco, h_error, h_relError, input_file
def readHists(h, fname): f = TFile(fname) for akey in f.GetListOfKeys(): name = akey.GetName() try: hname = int(name) except: hname = name cln = f.FindObjectAny(name).Class().GetName() if cln.find('TH') < 0: continue if h.has_key(hname): h[hname].Add(f.FindObjectAny(name)) else: h[hname] = f.FindObjectAny(name).Clone() h[hname].SetDirectory(gROOT) h[hname].Sumw2() if cln == 'TH2D' or cln == 'TH2F': for p in ['_projx', '_projy']: if p.find('x') > -1: h[hname + p] = h[hname].ProjectionX() else: h[hname + p] = h[hname].ProjectionY() h[hname + p].SetName(name + p) h[hname + p].SetDirectory(gROOT) return
def totalNumberOfGeneratedEvents(path,muon=True): """ path: path to directory containing all sample files returns dict samples names -> number of simulated events in source sample """ from ROOT import TFile result = {} for sampleName, filePath in getFilePathsAndSampleNames(path,muon).items(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny("Events").GetBinContent(1) return result
def totalNumberOfGeneratedEvents(path): """ path: path to directory containing all sample files returns dict samples names -> number of simulated events in source sample (note these include events without EMu EMu EMu signature, too ) """ from ROOT import TFile result = {} for sampleName, filePath in getFilePathsAndSampleNames(path).iteritems(): rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny( "analysis paths").GetBinContent(1) result[sampleName].SetDirectory(0) rootFile.Close() return result
def totalNumberOfGeneratedEvents(path,muon=True,genMass=False): """ path: path to directory containing all sample files returns dict samples names -> number of simulated events in source sample """ from ROOT import TFile result = {} for sampleName, filePath in getFilePathsAndSampleNames(path,muon).items(): if genMass: if "CI" in filePath or "ADD" in filePath or "2018" in filePath or "2016" in filePath: continue filePath = filePath.replace("dileptonAna_muons","dileptonAna_genMassOther") filePath = filePath.replace("dileptonAna_electrons","dileptonAna_genMassOther_electrons") rootFile = TFile(filePath, "read") result[sampleName] = rootFile.FindObjectAny("Events").GetBinContent(1) return result
def filecheck(rootfile): f = TFile(rootfile) if (f.IsZombie()): #print "File corrupted" f.Close() return 0 else: hist = f.FindObjectAny("reportSummaryContents") #(skip filecheck for HcalTiming files!!) if (hist == None and rootfile.rfind('HcalTiming') == -1): #print "File is incomplete" f.Close() return 0 else: #print "File is OK" f.Close() return 1
#!/usr/bin/env python from ROOT import TFile import argparse parser = argparse.ArgumentParser(description='adjusting crazy bambu structure') parser.add_argument('-i', action='store', dest='input', help='input file') parser.add_argument('-o', action='store', dest='output', help='dataset') args = parser.parse_args() if not args.input: args.input = '/home/mzanetti/cms/hist/monojet-2013-June12/filefi/031/s12-h125inv-vbf-v7a/monojet-2013-June12_s12-h125inv-vbf-v7a_noskim_0000.root' input = TFile(args.input) hDAllEvents = input.FindObjectAny('hDAllEvents') hNPUTrue = input.FindObjectAny('hNPUTrue') if not args.output: args.output = 'tmp.root' output = TFile(args.output, 'recreate') hDAllEvents.Write() hNPUTrue.Write() tree_names = { 'MJetTree': 'tree', 'MJetTreeDiLepton': 'treeZll', 'MJetTreeWlnu': 'treeWln', } trees, newTrees = {}, {} for tree_name in tree_names: input.cd() trees[tree_name] = input.FindObjectAny(tree_name) trees[tree_name].SetName(tree_names[tree_name])
if line.strip()[0] == '#': continue # skip data if line.strip().split()[-1] != '~': continue xsecs[line.strip().split()[1]] = float(line.strip().split()[4]) # process the datasets for d in glob.glob(merged_dir + '*.root'): dataset = d.replace(merged_dir, '').replace(configuration + '_', '').replace('_noskim.root', '') if not dataset in xsecs.keys(): continue print 'processing', dataset ref_file = TFile(d) hDAllEvents = ref_file.FindObjectAny('hDAllEvents') hNPUTrue = ref_file.FindObjectAny('hNPUTrue') # normalization ref_initialEntries = hDAllEvents.GetBinContent(1) # xsec ref_xsec = xsecs[dataset] # pileup data puFile = TFile( '/home/mzanetti/cms/ntupleAnalysis/DM/MyDataPileupHistogram.root') pu_target = puFile.FindObjectAny('pileup') pu_target.Scale(1.0 / pu_target.GetSumOfWeights()) pu_source = ref_file.FindObjectAny("hNPUTrue") pu_source.Rebin(10) pu_source.Scale(1.0 / pu_source.GetSumOfWeights())
print("Input " + inFileName) print("Output " + outFileName) print("Ouput File Type " + outFileType) cscRootName = "MUON_CSC_PED" overviewName = "Overview" inFile = TFile(inFileName) cnv = TCanvas("c1", "c1") rootDir = TDirectory() overDir = TDirectory() rootDir = inFile.FindObjectAny(cscRootName) overDir = rootDir.FindObjectAny(overviewName) hist = TH1D() #Page 1 hist = overDir.Get("h_csc_calib_numSignificant") hist.Draw() cnv.Print(outFileName + "(", outFileType) #Page 2 cnv.Clear() cnv.Divide(2, 2) i = 1 cnv.cd(i)
def checkRooWorkspace(self): f = TFile(self.rootfile) ws = f.FindObjectAny(self.wsName) f.Close() return bool(ws)
def container_sizes(f): f = TFile(f) t = f.FindObjectAny('cbmsim') print "name ZipBytes TotBytes TotalSize" for b in t.GetListOfBranches(): print b.GetName(), b.GetZipBytes(), b.GetTotBytes(), b.GetTotalSize()
from os import path from bitstring import Bits, BitArray from ROOT import TCanvas, TFile, TH1, TH3, TH3D, TH3S from ROOT import gROOT offset = 0.5 filename = raw_input('Input the file name: ') filename = str(filename) if(path.isfile(filename)): example = TFile( filename ) example.ls() objname = raw_input('Input the histogram name: ') objname = str(objname) print(objname) histo = example.FindObjectAny("objname") if(histo == None): print('No such file.') elif(histo.InheritsFrom("TH3")): xco = raw_input('Input the x channel: ') xco = float(xco) yco = raw_input('Input the y channel: ') xco = float(yco) result = histo.ProjectionZ(xco - offset, xco + offset, yco - offset, yco + offset) result.Draw() quit = raw_input('Press enter to quit.') else: print('Object not a 3-D histogram.') else: print('File not existent. Quitting.')