def update(self,wsname,fname,storelist=[]): """Stores the current model (and observable) into a ROOT.Workspace, persistified in a root file Parameters ---------- wsname: str Name to be given to the ROOT.Workspace fname: str Name to be given to the ROOT file storelist: list(ROOT.RooDataSet,ROOT.RooRealVar,...), optional List of RooFit objects to be stored in the same workspace """ import os from ROOT import RooWorkspace, TFile # Create the ws with the model and data available w = RooWorkspace(wsname,'Workspace') wsImport = getattr(w,'import') # Put the models for rawtuple in self.__models.values(): model = rawtuple[0] anythingelse = rawtuple[1:] wsImport(model) # Put whatever the user want to store for _item in storelist: wsImport(_item) # Check if the fname already exist file_exist = os.path.isfile(fname) if file_exist: # Create a new auxiliar file, to provisionaly # store the ws auxfname = '_auxroot.root' w.writeToFile(auxfname) # Previously delete the old Workspace, if any _rootfile = TFile(fname) _rootfile.Delete(wsname+';*') _rootfile.Delete("ProcessID*;*") _rootfile.Close() # Open the auxiliary file _aux = TFile(auxfname) # and copy the ws to the rootfile w = _aux.Get(wsname) # and copy the ws to the rootfile w.writeToFile(fname,False) if file_exist: # And closing and removing auxfile _aux.Close() os.remove(auxfname)
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 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 rename_hists(filename, from_s, to_s): print 'rename hists in', filename, 'from', from_s, 'to', to_s work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) if from_s in key: hist = work_file.Get(key).Clone(key.replace(from_s, to_s)) hist_list.append(hist) work_file.Delete(key + ';*') for item in hist_list: item.Write()
def hist_title(filename, histtitel): print 'rename hist titel in', filename, 'to', histtitel work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) hist = work_file.Get(key).Clone() work_file.Delete(key + ';*') hist.SetTitle(histtitel) hist_list.append(hist) for item in hist_list: item.Write() work_file.Close()
def set_sqrt_errors(filename): print 'sqrt error hists in', filename, work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) #print key hist = work_file.Get(key).Clone() work_file.Delete(key + ';*') for i in xrange(hist.GetNcells()): hist.SetBinError(i, math.sqrt(hist.GetBinContent(i))) hist_list.append(hist) for item in hist_list: item.Write()
def fixed_rebin(filename, rebin_values, hist_contains=''): work_file = TFile(filename, "UPDATE") keys = work_file.GetListOfKeys() hists = [] for key in keys: key = str(key.GetName()) if not hist_contains in key: continue hists.append(work_file.Get(key).Clone()) if isinstance(rebin_values, (int, long)): hists[-1] = hists[-1].Rebin(rebin_values) else: print rebin_values hists[-1] = hists[-1].Rebin(len(rebin_values), hists[-1].GetName(), array.array('d', rebin_values)) work_file.Delete(key + ';*') for item in hists: item.Write('', TObject.kOverwrite) work_file.Close()
def blind_final(filename, obs, exp): print 'starting to calculate chi2 for file', filename work_file = TFile(filename, 'UPDATE') keys = work_file.GetListOfKeys() categories_done = [] hist_list = [] for key in keys: key = str(key.GetName()) category = key.split("__")[0] if category in categories_done: continue exp_hist = work_file.Get(category + '__' + exp).Clone(category + '__' + obs) hist_list.append(exp_hist) work_file.Delete(key + ';*') categories_done.append(category) for item in hist_list: item.Write('', TObject.kOverwrite) work_file.Close()
def copyOneFile(dataset): # If the input and output files are defined outside the loop, histograms after the first instance are not found. # I did not track down what the cause of this behavior was. --Wells fin = TFile(condor_dir + "/pu.root", "READ") fout = TFile( os.environ['CMSSW_BASE'] + "/src/OSUT3Analysis/Configuration/data/pu.root", "UPDATE") print "Copying histogram " + dataset + " from " + fin.GetName( ) + " to " + fout.GetName() fin.cd() h = fin.Get(dataset) if not h: print " Could not find hist named " + dataset + " in " + fin.GetName() return fout.cd() if dataset != h.GetName(): # print " Resetting name from " + h.GetName() + " to " + dataset h.SetName(dataset) # Check if the histogram already exists in the destination file h2 = fout.Get(dataset) if h2: print " The histogram " + h2.GetName( ) + " already exists in the destination file." overwrite = raw_input(" Do you want to overwrite it? (y/n): ") if not (overwrite is "y"): print " Will not overwrite existing histogram." return # Only overwrite if the user enters "y" # delete all previously existing instances of the histogram print " Will overwrite existing histogram." fout.Delete(dataset + ";*") h.Write() fin.Close() fout.Close()
def scale_hists(fname, process, factors): print 'scaling', fname, process work_file = TFile(fname, 'UPDATE') keys = work_file.GetListOfKeys() hist_list = [] for key in keys: key = str(key.GetName()) #if len(key.split('__'))>2: continue category = key.split("__")[0] if process not in key.split("__")[1]: continue #print key, category if (isinstance(factors, list)): for factor in factors: catname = factor[0] catname = catname.replace('Anti-b-tag_rate', 'AntiBTag') catname = catname.replace('1-b-tag_rate', '1_BTag') catname = catname.replace('2-b-tag_rate', '2_BTag') catname = catname.replace('W-tag_rate', 'WTag') catname = catname.replace('top-tag_rate', 'TopTag') #print category, catname if catname in category: print 'scaling', key, 'by', factor[1] hist = work_file.Get(key) hist.Scale(factor[1]) hist_list.append(hist) work_file.Delete(key + ';*') #hist.Write('',TObject.kOverwrite); else: print 'scaling', key, 'by', factors hist = work_file.Get(key) hist.Scale(factors) hist_list.append(hist) for item in hist_list: item.Write('', TObject.kOverwrite) item.SetDirectory(0) work_file.Close() del hist_list
# print h_Info[0],h_Info[1],h_Info[2] # tree.Draw("%s>>%s_%s"%(h_Info[0],h_Info[1],sample),evtWeight) tree.Draw("%s:%s>>%s_%s"%(h_Info[0],h_Info[1],h_Info[2],sample),evtWeight,"colz") #outputFile = TFile(outputhistName,"update") # if not onlyAddPlots: # outputFile.rmdir(sample) # outputFile.mkdir(sample) # outputFile.cd(sample) # for h in histograms: # if onlyAddPlots: # gDirectory.Delete("%s;*"%(h.GetName())) # h.Write() # outputFile.Close() if not os.path.exists(outputhistName): os.makedirs(outputhistName) outputFile = TFile("%s/%s.root"%(outputhistName,sample),"update") for h in histograms: outputFile.Delete("%s;*"%h.GetName()) if onlyAddPlots: gDirectory.Delete("%s;*"%(h.GetName())) h.Write() outputFile.Close()
r_values = [] output = output.splitlines() output.sort() for line in output: for i in range(nBin): if line.find('r_' + str(i) + ' ') != -1: r_values.append(getValues(line.split())) ## Diff plots h_diff_name = 'diff_' + observable if asimov != '': h_diff_name += '_' + asimov h_uncor = TH1F(h_diff_name + '_uncorrelated', h_diff_name + '_uncorrelated', nBin, 0, nBin) for i in range(nBin): h_uncor.SetBinContent(i + 1, r_values[i][1]) h_uncor.SetBinError(i + 1, r_values[i][3]) ## Save plots output_file = TFile('./results/histograms_24bins_time_' + year + '.root', 'UPDATE') for l in output_file.GetListOfKeys(): if l.GetName() == h_diff_name + '_uncorrelated': output_file.Delete(h_diff_name + '_uncorrelated' + ';1') h_uncor.Write() output_file.Close()
def AnalyseWaveforms(topology, PATH, PMT_data_filenames, root_filename, pre_PULSE_region, waveform_length): try: # Will not create a new file if one exists already waveformfile = open(root_filename, "r") print(">>> File exists") except IOError: print(">>> File not found") print(">>> Creating .root file with the Average Waveforms") root_file = TFile(root_filename, 'RECREATE') # First create all the conatiners for the histograms map_HT = TH2I("Mapping_HT", "Mapping_HT", topology[0], 0, topology[0], topology[1], 0, topology[1]) map_LTO = TH2I("Mapping_LTO", "Mapping_LTO", topology[0], 0, topology[0], topology[1], 0, topology[1]) map = TH2I("Mapping", "Mapping", topology[0], 0, topology[0], topology[1], 0, topology[1]) Spectra_hist_vector = [] Spectra_cal_hist_vector = [] Peak_Cell_hist_vector = [] Rise_Time_hist_vector = [] Amplitude_hist_vector = [] Ratio_hist_vector = [] amp_shape_vector = [] Average_Waveform_hist_vector = [] # Create a blank waveform for each OM average_pulse_trace_vector = [] average_pulse_counter_vector = [] event_num_HT_vector = [] event_num_LTO_vector = [] blank_num_vector = [] # Fill the containers with the objects for each OM in this Loop for slot_num in range(topology[0]): Spectra_hist_vector.append([]) Spectra_cal_hist_vector.append([]) Peak_Cell_hist_vector.append([]) Rise_Time_hist_vector.append([]) Amplitude_hist_vector.append([]) Ratio_hist_vector.append([]) Average_Waveform_hist_vector.append([]) average_pulse_trace_vector.append([]) average_pulse_counter_vector.append([]) event_num_HT_vector.append([]) event_num_LTO_vector.append([]) amp_shape_vector.append([]) blank_num_vector.append([]) for channel_num in range(topology[1]): Spectra_hist = TH1F( "Spectra_" + str(slot_num) + "_" + str(channel_num), "Spectra_" + str(slot_num) + "_" + str(channel_num), 200, 0, 200000) Spectra_hist.GetXaxis().SetTitle("Raw Charge") Spectra_hist_vector[slot_num].append(Spectra_hist) Spectra_cal_hist = TH1F( "Spectra_cal_" + str(slot_num) + "_" + str(channel_num), "Spectra_cal_" + str(slot_num) + "_" + str(channel_num), 200, 0, 200000) Spectra_cal_hist.GetXaxis().SetTitle("Raw Charge") Spectra_cal_hist_vector[slot_num].append(Spectra_cal_hist) Peak_Cell_hist = TH1F( "Peak_Cell_" + str(slot_num) + "_" + str(channel_num), "Peak_Cell_" + str(slot_num) + "_" + str(channel_num), waveform_length, 0, waveform_length) Peak_Cell_hist.GetXaxis().SetTitle( "Peak Cell Position /Sample Number") Peak_Cell_hist_vector[slot_num].append(Peak_Cell_hist) Rise_Time_hist = TH1F( "Rise_Time_" + str(slot_num) + "_" + str(channel_num), "Rise_Time_" + str(slot_num) + "_" + str(channel_num), 200, 50, 200) Rise_Time_hist.GetXaxis().SetTitle("Rise Time") Rise_Time_hist_vector[slot_num].append(Rise_Time_hist) Amplitude_hist = TH1F( "Amplitudes_" + str(slot_num) + "_" + str(channel_num), "Amplitudes_" + str(slot_num) + "_" + str(channel_num), 200, 0, 2100) Amplitude_hist.GetXaxis().SetTitle("Amplitude /ADC Counts") Amplitude_hist_vector[slot_num].append(Amplitude_hist) Ratio_hist = TH1F( "Ratio_" + str(slot_num) + "_" + str(channel_num), "Ratio_" + str(slot_num) + "_" + str(channel_num), 200, 0, 0.05) Ratio_hist.GetXaxis().SetTitle("Amplitude/Raw Charge") Ratio_hist_vector[slot_num].append(Ratio_hist) amp_shape_hist = TH2F( "amp_shape" + str(slot_num) + "_" + str(channel_num), "amp_shape" + str(slot_num) + "_" + str(channel_num), 100, 0, 2000, 100, 0, 1) amp_shape_hist.GetXaxis().SetTitle("Amplitude /mV") amp_shape_hist.GetYaxis().SetTitle("Shape") amp_shape_vector[slot_num].append(amp_shape_hist) average_pulse_trace_vector[slot_num].append([0] * waveform_length) average_pulse_counter_vector[slot_num].append(0) event_num_HT_vector[slot_num].append(0) event_num_LTO_vector[slot_num].append(0) blank_num_vector[slot_num].append(0) average_pulse_trace_hist = TH1F( "Waveform_" + str(slot_num) + "_" + str(channel_num) + "_average", "Waveform_" + str(slot_num) + "_" + str(channel_num) + "_average", waveform_length, 0, waveform_length) average_pulse_trace_hist.GetXaxis().SetTitle("Sample Number") average_pulse_trace_hist.GetYaxis().SetTitle( "Averaged ADC Counts /AU") Average_Waveform_hist_vector[slot_num].append( average_pulse_trace_hist) del Spectra_hist del Spectra_cal_hist del Peak_Cell_hist del Rise_Time_hist del Amplitude_hist del Ratio_hist del average_pulse_trace_hist print(">>> Create Templates... ") template = createTemplate(PATH + PMT_data_filenames[0].rstrip(), topology, waveform_length, pre_PULSE_region) # Now run ove the files print(">>> Reading Data Files... ") processing_start = time.time() temp_start = processing_start for file_num in range(len(PMT_data_filenames)): print(">>> File: ", PATH + PMT_data_filenames[file_num].rstrip()) Read_Data(PATH + PMT_data_filenames[file_num].rstrip(), pre_PULSE_region, average_pulse_counter_vector, event_num_HT_vector, event_num_LTO_vector, average_pulse_trace_vector, Spectra_hist_vector, Spectra_cal_hist_vector, Rise_Time_hist_vector, Peak_Cell_hist_vector, Amplitude_hist_vector, Ratio_hist_vector, amp_shape_vector, blank_num_vector, waveform_length, template) intermidiate = time.time() time_length = intermidiate - processing_start print(">>>\n>>> %.3f seconds.\n" % (intermidiate - temp_start)) temp_start = intermidiate print("Processed %.2f of data..." % (float(file_num + 1) / float(len(PMT_data_filenames)) * 100)) Estimate = (time_length / float(file_num + 1)) * ( len(PMT_data_filenames) - file_num - 1) print(">>> Estimated time till termination %.3f seconds\n\n" % Estimate) for slot_num in range(topology[0]): root_file.mkdir("Slot" + str(slot_num)) root_file.GetDirectory("Slot" + str(slot_num)).cd() Channel_Check = topology[1] for channel_num in range(topology[1]): root_file.GetDirectory("Slot" + str(slot_num)).mkdir("Channel" + str(channel_num)) root_file.GetDirectory("Slot" + str(slot_num)).GetDirectory( "Channel" + str(channel_num)).cd() Spectra_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) Spectra_cal_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) Rise_Time_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) Amplitude_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) Peak_Cell_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) Ratio_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) amp_shape_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) for bin in range( len(average_pulse_trace_vector[slot_num] [channel_num])): # Avoid dividing by zero if average_pulse_counter_vector[slot_num][ channel_num] == 0: average_pulse_counter_vector[slot_num][channel_num] = 1 Average_Waveform_hist_vector[ slot_num][channel_num].SetBinContent( bin, (average_pulse_trace_vector[slot_num][channel_num] [bin] / average_pulse_counter_vector[slot_num] [channel_num]) * 1000.0) Average_Waveform_hist_vector[slot_num][ channel_num].SetBinError(bin, 1.0) Average_Waveform_hist_vector[slot_num][channel_num].Write( "", TFile.kOverwrite) map.Fill( slot_num, channel_num, event_num_HT_vector[slot_num][channel_num] + event_num_LTO_vector[slot_num][channel_num]) map_HT.Fill(slot_num, channel_num, event_num_HT_vector[slot_num][channel_num]) map_LTO.Fill(slot_num, channel_num, event_num_LTO_vector[slot_num][channel_num]) if event_num_HT_vector[slot_num][ channel_num] + event_num_LTO_vector[slot_num][ channel_num] + blank_num_vector[slot_num][ channel_num] == 0: root_file.GetDirectory("Slot" + str(slot_num)).Delete( "Channel" + str(channel_num) + ";1") #print("\n>>> Deleted Slot: ",slot_num, " Channel: ", channel_num, " as there were no events\n") Channel_Check = Channel_Check - 1 if Channel_Check == 0: root_file.Delete("Slot" + str(slot_num) + ";1") print('\n>>> Deleted Slot: ', slot_num, ' as there were no events\n') root_file.cd() map.Write("", TFile.kOverwrite) map_HT.Write("", TFile.kOverwrite) map_LTO.Write("", TFile.kOverwrite) root_file.Close() print(">>>")
def Modify_AlignmentDBFile(dbfilename=None, planenumber=None, mode=None, value=None): """ Change single position or angle in alignment DB file :@dbfilename Name of the output root file :@planenumber Planenumber to be modified (starting with 0) :@mode Mode to be modified ['x','y','z','alpha','beta','gamma'] :@value New value in mm or rad :author: [email protected] """ if dbfilename == None: return None if planenumber == None: return None if mode == None: return None if value == None: return None if os.path.isfile(dbfilename): dbfile = TFile( dbfilename, 'UPDATE' ) else: raise ValueError('alignment DB ('+dbfilename+') file not found') # Get access to histogram if mode=='x': histo = dbfile.Get("hPositionX") dbfile.Delete("hPositionX;1") elif mode=='y': histo = dbfile.Get("hPositionY") dbfile.Delete("hPositionY;1") elif mode=='z': histo = dbfile.Get("hPositionZ") dbfile.Delete("hPositionZ;1") elif mode=='alpha': histo = dbfile.Get("hRotationAlpha") dbfile.Delete("hRotationAlpha:1") elif mode=='beta': histo = dbfile.Get("hRotationBeta") dbfile.Delete("hRotationBeta;1") elif mode=='gamma': histo = dbfile.Get("hRotationGamma") dbfile.Delete("hRotationGamma;1") else: return None histo.SetBinContent(planenumber+1,value) dbfile.Write() dbfile.Close()
# all_integrals_gamma=np.append(all_integrals_gamma,inte) # elif("PhS1" in sims): # all_integrals_elecphs1=np.append(all_integrals_elecphs1,inte) # else: # all_integrals_elec=np.append(all_integrals_elec,inte) # print("exists") except: print("file is",filename) print("!!!corrupt!!!") if (fi.IsZombie()): print("zombie") fi.Close() fi.Delete() plt.plot(all_integrals) plt.pause(0.01) input("") # plt.plot(all_integrals_gamma) # plt.pause(0.01) # input("") # plt.clf() # plt.cla() # plt.plot(all_integrals_gamma) # plt.pause(0.01) # input("") # plt.clf()