def getGTfromDQMFile(DQMfile, Run_number, globalTagVar): if not os.path.isfile(DQMfile): print("Error: file" + str(DQMfile) + "not found, exit") sys.exit(0) thefile = TFile(DQMfile) globalTagDir = 'DQMData/Run ' + Run_number + '/Info/Run summary/CMSSWInfo' if not gDirectory.GetDirectory(globalTagDir): print("Warning: globalTag not found in DQM file") sys.exit(0) keys = gDirectory.GetDirectory(globalTagDir).GetListOfKeys() key = keys[0] globalTag = '' while key: obj = key.ReadObj() if globalTagVar in obj.GetName(): globalTag = obj.GetName()[len("<" + globalTagVar + ">s="):-len("</" + globalTagVar + ">")] break key = keys.After(key) if len(globalTag) > 1: if globalTag.find('::') >= 0: print(globalTag[0:globalTag.find('::')]) else: print(globalTag) return globalTag
def putHistogram(self, dataset, systematicVariation, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param dataset Dataset object # @param systematicVariation SystematicVariation object # @param var Variable object # @param cut Cut object # @param histogram ROOT::TH1 object #if not self._open( dataset, systematicVariation, var, cut ): # return None if not self._open(dataset, systematicVariation, var, cut, 'update'): self.logger.warning( 'putHistogram(): unable to store histogram, file not open') return None path, histogramName = self._buildPath(dataset, systematicVariation, var, cut) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self._file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) histogram = histogram.Clone(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def putHistogram(self, channelName, datasetName, systematicName, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param datasetName the name of the dataset # @param systematicName the name of the systematic variation # @param var the variable # @param cut the cut # @param histogram the histogram object if not self._open(): return None path, histogramName = self._buildPath(channelName, datasetName, systematicName) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self.file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) if 'resopara' in histogramName or 'resoperp' in histogramName: if '_low' in histogramName: histogramName.replace('_low', '') elif '_high' in histogramName: return histogram.SetTitle(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def postcmd(self, stop, line): self.oldpwd = self.pwd.GetDirectory('') self.pwd = gDirectory.GetDirectory('') dirn = self.pwd.GetName() if len(dirn) > 20: dirn = '{}..{}'.format(dirn[0:9], dirn[-9:]) self.prompt = '{}> '.format(dirn) return cmd.Cmd.postcmd(self, stop, line)
def get_dir(self, path=None): """Return directory from path. Doesn't check for non-directory. It's the caller's responsibility. """ if not path: return gDirectory.GetDirectory('') else: path = pathspec(path) with savepwd(): if path.rfile: # need to change to correct file first if path.rfile not in [f.GetName() for f in self.files]: # opening a file changes dir to the new file self.files += [ROOT.TFile.Open(path.rfile, 'read')] else: gROOT.cd('{}:'.format(path.rfile)) return gDirectory.GetDirectory(path.rpath)
def applyInAllDirectories(functor, directory, depth=1): oldDirectory = gDirectory.GetDirectory("") directory.cd() prefix = "-" prefix *= depth * 2 print prefix, "Examining directory: %s" % directory.GetName() # call our funciton print "calling" functor(directory) # find sub directories depth += 1 # recurse into subdirectories for dir in objectsInheritingFrom("TDirectory", directory): applyInAllDirectories(functor, dir, depth) oldDirectory.cd()
from ROOT import gDirectory import sys import os DQMfile = sys.argv[1] RunNumber = sys.argv[2] globalTagVar = sys.argv[3] if not os.path.isfile(DQMfile): # print "Error: file", DQMfile, "not found, exit" sys.exit(0) thefile = TFile(DQMfile) globalTagDir = 'DQMData/Run ' + RunNumber + '/Info/Run summary/CMSSWInfo' if not gDirectory.GetDirectory(globalTagDir): # print "Warning: globalTag not found in DQM file" sys.exit(0) keys = gDirectory.GetDirectory(globalTagDir).GetListOfKeys() key = keys[0] globalTag = '' while key: obj = key.ReadObj() if globalTagVar in obj.GetName(): globalTag = obj.GetName()[len("<" + globalTagVar + ">s="):-len("</" + globalTagVar + ">")] break key = keys.After(key) if len(globalTag) > 1:
sys.path.append('python') ifile = sys.argv[1] cfgfile = sys.argv[2] ofile = sys.argv[3] iFILE = rt.TFile(ifile) oFILE = rt.TFile(ofile, "RECREATE") cfg = imp.load_source('final', cfgfile) for _cfg in cfg.cfg: hist = _cfg.getHist(iFILE) if not gDirectory.GetDirectory(_cfg.collection): gDirectory.mkdir(_cfg.collection + "/" + _cfg.ID) gDirectory.cd(_cfg.collection + "/" + _cfg.ID) else: gDirectory.cd(_cfg.collection) if not gDirectory.GetDirectory(_cfg.ID): gDirectory.mkdir(_cfg.ID) gDirectory.cd(_cfg.ID) for h in hist: h.Write() #CANVAS.Print(_cfg.collection + "_" + _cfg.ID + "_" + h.GetName() + ".pdf") gDirectory.cd("/") oFILE.Close()
def __init__(self): self.pwd = gDirectory.GetDirectory('')
def fhadd(self, prefix="", force=False, verbose=False, slow=True): """ taken from https://root.cern.ch/phpBB3/viewtopic.php?t=14881 This function will merge objects from a list of root files and write them to a target root file. The target file is newly created and must not exist, or if -f ("force") is given, must not be one of the source files. IMPORTANT: It is required that all files have the same content! Fast but memory hungry alternative to ROOT's hadd. Arguments: target -- name of the target root file sources -- list of source root files classname -- restrict merging to objects inheriting from classname force -- overwrite target file if exists """ target = prefix + self.Name + ".root" sources = [j.Output for j in self.Jobs] TH1.AddDirectory(False) # check if target file exists and exit if it does and not in force mode if not force and os.path.exists(target): raise RuntimeError("target file %s exists" % target) # open the target file print "fhadd Target file:", target outfile = TFile(target, "RECREATE") # open the seed file - contents is looked up from here seedfilename = sources[0] print "fhadd Source file 1", seedfilename seedfile = TFile(seedfilename) # get contents of seed file print "looping over seed file" contents = self.loop(seedfile) print "done %d objects are ready to be merged" % len(contents) if (verbose): for c in contents: print c # open remaining files otherfiles = [] for n, f in enumerate(sources[1:]): print "fhadd Source file %d: %s" % (n + 2, f) otherfiles.append(TFile(f)) # loop over contents and merge objects from other files to seed file objects for n, (path, hname) in enumerate(contents): print "fhadd Target object: %s" % os.path.join(path, hname) obj_path = os.path.join(path, hname) obj_ = seedfile.Get(obj_path[1:]) outfile.cd('/') # create target directory structure for d in path.split('/')[1:]: directory = gDirectory.GetDirectory(d) if not directory: gDirectory.mkdir(d).cd() else: gDirectory.cd(d) obj = None if obj_.InheritsFrom("TTree"): obj = obj_.CloneTree() else: obj = obj_.Clone() # merge objects l = TList() for o in [of.Get(obj_path[1:]) for of in otherfiles]: l.Add(o) obj.Merge(l) # delete objects if in slow mode if slow: print "Deleting %d object(s)", l.GetEntries() l.Delete() # write object to target obj.Write(obj.GetName(), TObject.kOverwrite) print "Writing and closing file" # let ROOT forget about open files - prevents deletion of TKeys for f in [outfile, seedfile] + otherfiles: gROOT.GetListOfFiles().Remove(f) outfile.Write() outfile.Close() for f in [seedfile] + otherfiles: f.Close()
def main(): args = parser() filename = args.filename os.system('mkdir -p rootfiles') os.system('mkdir -p plots') f = tables.open_file(filename) print(f"Successfully opened file: {filename}.") data = f.get_node('/data') print("Read /data") event_ids = getDataCol('event_id', data) waveforms = getDataCol('waveform', data) times = getDataCol('time', data) timestamps = getDataCol('timestamp', data) thresFlags = getDataCol('thresholdFlags', data) i_1V1 = getDataCol('i_1V1', data) i_1V35 = getDataCol('i_1V35', data) i_1V8 = getDataCol('i_1V8', data) i_2V5 = getDataCol('i_2V5', data) i_3V3 = getDataCol('i_3V3', data) v_1V1 = getDataCol('v_1V1', data) v_1V35 = getDataCol('v_1V35', data) v_1V8 = getDataCol('v_1V8', data) v_2V5 = getDataCol('v_2V5', data) v_3V3 = getDataCol('v_3V3', data) nsamples = len(waveforms[0]) fq = np.linspace(0, 240, nsamples) gmean = np.mean(waveforms[0]) globalmean = np.mean(waveforms) print(np.mean(waveforms[0])) c = TCanvas("c", "c", 800, 600) c.Draw() c.SetGrid() of = TFile('rootfiles/{0}.root'.format(filename.split('.h', 1)[0]), "RECREATE") h = TH1D('qdist', 'qdist;ADC count;Entry', 500, -100, 900) hpk = TH1D('peak', 'peak;ADC count;Entry', 500, -100, 900) havg = TH1D('avgwf', 'Averaged Waveform;Sampling Bin;ADC count', nsamples, 0, nsamples) havgfft = TH1D('avgfft', 'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]', int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1)) hifft = TH1D('Subtwf', 'Averaged Waveform;Sampling Bin;ADC count', nsamples, 0, nsamples) hsfft = TH1D('Subtfft', 'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]', int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1)) hsspe = TH1D('qdist_subt', 'qdist_subt;ADC count;Entry', 500, -100, 900) hmax = TH1D('hmax', 'hmax;ADC count;Entry', 500, -100, 900) bsfluc = TH1D('bsfluc', 'bsfluc;ADC count;Entry', 100, int(globalmean) - 50, int(globalmean) + 50) bsshift = TGraphErrors() bsshift.SetName('bsshift') bsshift.SetTitle('bsshift;Event Number;Baseline [LSB]') nrshift = TGraphErrors() nrshift.SetName('nrshift') nrshift.SetTitle('NoiseRateShift;Event Number;Noise Rate [Hz]') rmsshift = TGraphErrors() rmsshift.SetName('rmsshift') rmsshift.SetTitle('RMSNoiseShift;Event Number;RMS Noise [LSB]') rmstime = TGraphErrors() rmstime.SetName('rmstime') rmstime.SetTitle('rmstime;Unix time [s];RMS Noise [LSB]') powerc = TGraphErrors() powerc.SetName('powerc') powerc.SetTitle('powerc;Event Number;Power Consumption [W]') h_powerc = TH1D('h_powerc', 'h_powerc;Power Consumption [W];Entry', 100, 0, 5) winmin = args.minimum winmax = args.minimum + args.window bsstart = args.baselineEst print(f'Total #Events: {len(waveforms)}') print( f'Setting... Window [{winmin}:{winmax}] and Pedestal start from {bsstart}.\n' ) topdir = gDirectory.GetDirectory(gDirectory.GetPath()) subdir = topdir.mkdir("Waveforms") subdir2 = topdir.mkdir("FFT") subdir3 = topdir.mkdir("proj") thrdir = topdir.mkdir("thresFlags") fltwfs = [] starttimestamp = timestamps[0] procN = len(waveforms) if args.nevents > 0: procN = args.nevents for i in tqdm(range(procN)): if i < args.nskip: continue waveform = waveforms[i] timestamp = timestamps[i] bsfluc.Fill(np.mean(waveform)) n = bsshift.GetN() bsshift.Set(n + 1) bsshift.SetPoint(n, i, np.mean(waveform)) bsshift.SetPointError(n, 0, np.std(waveform)) n = rmsshift.GetN() rmsshift.Set(n + 1) rmsshift.SetPoint(n, i, np.std(waveform)) rmsshift.SetPointError(n, 0, 0) hmax.Fill(np.max(waveform) - np.mean(waveform)) timeinterval = ((timestamp - starttimestamp - i * nsamples) / 240.e6 - i * 0.501) nr = 0 nrerr = 0 if timeinterval > 0: nr = (i + 1) / timeinterval nrerr = np.sqrt(i + 1) / timeinterval n = nrshift.GetN() nrshift.Set(n + 1) nrshift.SetPoint(n, i, nr) nrshift.SetPointError(n, 0, nrerr) # FFT & IFFT ### F_abs_amp = doFFT(waveform) F = np.fft.fft(waveform) F2 = np.copy(F) fc = 60 df = 2 F2[(fq > fc - df) & (fq < fc + df)] = 0 F2[(fq > 2 * fc - df) & (fq < 2 * fc + df)] = 0 F2[(fq > 240 - fc - df) & (fq < 240 - fc + df)] = 0 F2_abs = np.abs(F2) F2_abs_amp = F2_abs / len(waveform) * 2 F2_abs_amp[0] = F_abs_amp[0] / 2 F2_ifft = np.fft.ifft(F2) F2_ifft_real = F2_ifft.real ###### baseline_mean = np.mean(waveform[bsstart:nsamples]) if args.fixbs: baseline_mean = globalmean center = int(baseline_mean) proj = TH1D(f'proj{i}', f'Projection waveform{i};ADC count;Entry', 300, center - 150, center + 150) selected = waveform[waveform < np.mean(waveform) + 10] for j in range(len(selected)): proj.Fill(selected[j]) #proj.Fit("gaus") #if len(selected) > 0: # f = proj.GetFunction('gaus') # norm = f.GetParameter(0) # mean = f.GetParameter(1) # sigma = f.GetParameter(2) reduced_waveform = waveform - baseline_mean scale = (nsamples - bsstart) / (winmax - winmin) #h.Fill(sum(waveform[winmin:winmax])-sum(waveform[bsstart:nsamples])/scale) h.Fill(sum(reduced_waveform[winmin:winmax])) hsspe.Fill( np.sum(F2_ifft_real[winmin:winmax]) - baseline_mean * (winmax - winmin)) hpk.Fill( max(waveform[winmin:winmax]) - np.mean(waveform[bsstart:nsamples])) hfft = TH1D(f'FFT{i}', 'FFT{i};Frequency [MHz];Amplitude [LSB]', int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1)) hfft2 = TH1D(f'FFT2{i}', 'FFT2{i};Frequency [MHz];Amplitude [LSB]', int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1)) for j in range(int(len(F_abs_amp) / 2. + 1)): hfft.Fill(fq[j], F_abs_amp[j]) hfft2.Fill(fq[j], F2_abs_amp[j]) h2 = TH1D(f'w{i}', 'Waveform{i};Sampling Bin;ADC count', nsamples, 0, nsamples) for j in range(len(waveform)): h2.Fill(j, waveform[j]) #h2.Draw("hist") #if np.max(F2_abs_amp[1:int(len(F2_abs_amp)/2.+1)]) > 0.5: # c.SetLogy(0) # h2.SetLineColor(4) # h2.Draw("hist") # c.Print(f"plots/w{i}.pdf(") # c.SetLogy(1) # hfft.SetLineColor(4) # hfft.Draw("hist") # c.Print(f"plots/w{i}.pdf)") htot = TH1D(f'thresflags{i}', 'ThresholdFlags{i};Sampling Bin;Threshold Flag', nsamples, 0, nsamples) for j in range(len(thresFlags[i])): htot.Fill(j, thresFlags[i][j]) if max(waveform) - baseline_mean < args.threshold: continue fltwfs.append(waveform) if args.silent: del hfft del hfft2 del h2 del htot del proj del waveform del timestamp del F del F2 gc.collect() continue subdir2.cd() hfft.Write() subdir.cd() h2.Write() subdir3.cd() proj.Write() thrdir.cd() htot.Write() print('') for i in range(len(i_1V1)): internal_power = v_1V1[i] * i_1V1[i] + v_1V35[i] * i_1V35[i] + v_1V8[ i] * i_1V8[i] + v_2V5[i] * i_2V5[i] + v_3V3[i] * i_3V3[i] n = powerc.GetN() powerc.Set(n + 1) powerc.SetPoint(n, i, internal_power * 1e-3) powerc.SetPointError(n, 0, 0) h_powerc.Fill(internal_power * 1e-3) avgfltwfs = np.mean(fltwfs, axis=0) for i in range(len(avgfltwfs)): havg.Fill(i, avgfltwfs[i]) Favg_abs_amp = doFFT(avgfltwfs) Favg = np.fft.fft(avgfltwfs) Favg2 = np.copy(Favg) Favg2[(fq > 59) & (fq < 61)] = 0 Favg2[(fq > 119) & (fq < 121)] = 0 Favg2[(fq > 179) & (fq < 181)] = 0 Favg2_abs = np.abs(Favg2) Favg2_abs_amp = Favg2_abs / len(avgfltwfs) * 2 Favg2_abs_amp[0] = Favg_abs_amp[0] / 2 Favg2_ifft = np.fft.ifft(Favg2) Favg2_ifft_real = Favg2_ifft.real for i in range(int(len(Favg_abs_amp) / 2. + 1)): havgfft.Fill(fq[i], Favg_abs_amp[i]) for i in range(int(len(Favg2_abs_amp) / 2. + 1)): hsfft.Fill(fq[i], Favg2_abs_amp[i]) for i in range(len(Favg2_ifft_real)): hifft.Fill(i, Favg2_ifft_real[i]) topdir.cd() h.Write() hpk.Write() havg.Write() havgfft.Write() hsfft.Write() hifft.Write() hsspe.Write() hmax.Write() bsfluc.Write() bsshift.Write() nrshift.Write() rmsshift.Write() powerc.Write() h_powerc.Write() of.Close() f.close()