import sys import ROOT from ROOT import TTree, TFile, AddressOf, gROOT, std, vector gROOT.ProcessLine("#include <vector>") events = [] if len(sys.argv) != 4: print( 'Usage: python h5toroot.py [h5 input file name] [root output file name] [max number of events]' ) sys.exit() ifile = h5py.File(sys.argv[1], 'r') ofile = ROOT.TFile(sys.argv[2], 'RECREATE') maxn = int(sys.argv[3]) ecalTree = TTree('ecalTree', "ecal Tree") #hcal = np.array(ifile['HCAL']) ecal = np.array(ifile['ECAL']) #target = np.array(ifile['target']) #print('TARGET:', target.shape) #print('ECAL:', ecal.shape) #print('HCAL:', hcal.shape) en = 0 ei = ecal.shape[1] ej = ecal.shape[2] ek = ecal.shape[3] #hi = hcal.shape[1] #hj = hcal.shape[2]
inputdata.start(batch_size) nevts = len(inputdata) if nprocess_events >= 0: nevts = nprocess_events nbatches = nevts / batch_size if nevts % batch_size != 0: nbatches += 1 # root hist to inspect diff f = TFile('test.root', 'recreate') h = TH1F('h1', 'diff', 150, -25., 25.) h2 = TH2F('h2', 'diff2d', 100, 0., 100., 100, 0., 100.) h3 = TH1F('h3', 'adc value', 100, 0., 100.) h4 = TH1F('h4', 'adc value', 100, 0., 100.) t = TTree('t1', 'diff histos') ientry = 0 averageacc2 = 0.0 averageacc5 = 0.0 averageacc10 = 0.0 averageacc20 = 0.0 for ibatch in range(nbatches): if verbose: print "=== [BATCH %d] ===" % (ibatch) # data = inputdata[0] # diff_h= array( 'diff', [ 0 ] ) t.Branch('diff', h, 'diff')
if (frameIt - frameStart) % nPayloadFrames == nPayloadFrames - 1: # counts events in hw nHw += 1 # add a null jet if no jet has been found, just as a place holder hwData.append(hwJets) del hwJets hwJets = [] hwSums.append(hwSum) hwSum = [0, 0, 0] # writing to tree hardwareOutputFile.cd() hardwareJetSumsTree = TTree( "HardwareEvents", "Tree with output from hardware in a event-like structure") emulatorJetSumsTree = TTree( "EmulatorEvents", "Tree with output from emulator in a event-like structure") # these trees can be used for analysis in FAST hardwareJetTree = TTree("HardwareJets", "Flat tree with jets in output from hardware") emulatorJetTree = TTree("EmulatorJets", "Flat tree with jets in output from emulator") sumsTree = TTree("Sums", "Flat tree with sums in output from hardware and emulator") # preparing data holders hardware_jetPt = array("f", maxNumberOfJetsPerEvent * [0])
def init_tree(self): return TTree(*[self.Config.get('TREE', 'name')] * 2)
file_header = sys.argv[2] caption = sys.argv[3] print "input folder: " + sys.argv[1] print "file header: " + sys.argv[2] print "caption: " + sys.argv[3] #if (sys.argc>2) NPULSES = sys.argv[2] #std::cout << "file_header: " << file_header << std::endl; #std::cout << "NPULSES to be processed: " << NPULSES << std::endl; output_file_name = output_folder + caption + ".root" output_file = TFile(output_file_name, "RECREATE") n = array('i', [0]) waveTree = TTree("waveTree", "waveTree") maxN = 80000 index = array('i', [0]) #t_time_l = array('f', maxN*[ 0. ] ) #t_amp_l = array('f', maxN*[ 0. ] ) #t_time = array('f', maxN*[ 0. ] ) #t_amp = array('f', maxN*[ 0. ] ) t_time_l = ROOT.std.vector(float)() t_amp_l = ROOT.std.vector(float)() t_time = ROOT.std.vector(float)() t_amp = ROOT.std.vector(float)() waveTree._t_time_l = t_time_l
def process_csv(inputfile): histosize = 252 # recordLength filename = inputfile.split('.')[0] print "now processing ", filename, "it will take minutes!" f0 = open(inputfile) reader = csv.reader(f0) csvdata = list(reader) csvdata = csvdata[1:] #delete the table title totalevents = len(csvdata) print "total events: " + str(totalevents) divide = totalevents / 10000 + 1 if divide != 0: print "divide into " + str(divide) + " files: " + str( divide - 1) + "*10000 + " + str(totalevents - (divide - 1) * 10000) + " events." lineNum = 0 # eventID == line number in data fN = 0 # file number for fN in range(divide): print 'process file ' + str(fN) evtID = array('I', [0]) #unsigned int timeStamp = array('L', [0]) #unsigned long energy = array('I', [0]) energyShort = array('I', [0]) flag = array('I', [0]) tree = TTree('T', 'dump CAEN data') tree.Branch('eventID', evtID, 'eventID/s') tree.Branch("timeStamp", timeStamp, "timeStamp/L") #ULong64_6 tree.Branch("energy", energy, "energy/s") tree.Branch("energyShort", energyShort, "energyShort/s") tree.Branch("flag", flag, "flag/s") #tree.Branch("hwf","TH1F", hwaveform) f = TFile("dump_" + filename + "_" + str(fN) + ".root", "recreate") lineNum = 0 + fN * 10000 if fN < divide - 1: csvdata_divide = csvdata[lineNum:lineNum + 10000] else: csvdata_divide = csvdata[lineNum:] for item in csvdata_divide: evtID[0] = lineNum timeStamp[0] = int(item[0]) energy[0] = int(item[1]) energyShort[0] = int(item[2]) flag[0] = int(item[3], 16) waveformdata = item[4:4 + histosize] waveformdata = map(int, waveformdata) #print evtID, timeStamp, energy,energyShort,flag, waveformdata hwaveform = TH1F("hwf", "", histosize - 1, 0, histosize) # fill waveform ibin = 0 for idata in waveformdata: hwaveform.SetBinContent(ibin + 1, idata) ibin = ibin + 1 f.cd() hwaveform.SetName("hwf" + str(lineNum)) hwaveform.Write() #hwaveform.BufferEmpty() tree.Fill() lineNum = lineNum + 1 f.cd() tree.Write() f.Write() f.Close() fN = fN + 1
def do_startRun(self, args): """Starts the TLU run. If a number is specified, this number will be appended to the file name as Run_#""" print "==== COMMAND RECEIVED: STARTING TLU RUN" #startTLU( uhalDevice = self.hw, pychipsBoard = self.board, writeTimestamps = ( options.writeTimestamps == "True" ) ) arglist = args.split() if len(arglist) == 0: print "\tno run# specified, using 1" runN= 1 else: runN= arglist[0] logdata= True # logdata= False #TLU.start(logdata) if (TLU.isRunning): #Prevent double start print " Run already in progress" return else: now = datetime.now().strftime('%Y%m%d_%H%M%S') default_filename = "./datafiles/"+ now + "_tluData_" + str(runN) + ".root" rootFname= default_filename print "OPENING ROOT FILE:", rootFname self.root_file = TFile( rootFname, 'RECREATE' ) # Create a root "tree" root_tree = TTree( 'T', 'TLU Data' ) #highWord =0 #lowWord =0 #evtNumber=0 #timeStamp=0 #evtType=0 #trigsFired=0 #bufPos = 0 #https://root-forum.cern.ch/t/long-integer/1961/2 gROOT.ProcessLine( "struct MyStruct {\ UInt_t raw0;\ UInt_t raw1;\ UInt_t raw2;\ UInt_t raw3;\ UInt_t raw4;\ UInt_t raw5;\ UInt_t evtNumber;\ ULong64_t tluTimeStamp;\ UChar_t tluEvtType;\ UChar_t tluTrigFired;\ };" ); mystruct= MyStruct() # Create a branch for each piece of data root_tree.Branch('EVENTS', mystruct, 'raw0/i:raw1/i:raw2/i:raw3/i:raw4/i:raw5/i:evtNumber/i:tluTimeStamp/l:tluEvtType/b:tluTrigFired/b' ) # root_tree.Branch( 'tluHighWord' , highWord , "HighWord/l") # root_tree.Branch( 'tluLowWord' , lowWord , "LowWord/l") # root_tree.Branch( 'tluExtWord' , extWord , "ExtWord/l") # root_tree.Branch( 'tluTimeStamp' , timeStamp , "TimeStamp/l") # root_tree.Branch( 'tluBufPos' , bufPos , "Bufpos/s") # root_tree.Branch( 'tluEvtNumber' , evtNumber , "EvtNumber/i") # root_tree.Branch( 'tluEvtType' , evtType , "EvtType/b") # root_tree.Branch( 'tluTrigFired' , trigsFired, "TrigsFired/b") #self.root_file.Write() daq_thread= threading.Thread(target = TLU.start, args=(logdata, runN, mystruct, root_tree)) daq_thread.start()
return sidesarr ########################################################################################## ########################################################################################## ########################################################################################## ROOT.gInterpreter.GenerateDictionary("vector<TPolyMarker3D>", "vector") clusters_xyz = ROOT.std.vector('TPolyMarker3D*')() clusters_type = ROOT.std.vector( int)() ## -1 for random noise or 0 for background track clusters_id = ROOT.std.vector( int)() ## the id of the generated background track (-1 for noise culsters) tF = TFile("../data/root/background_clusters_" + proc + ".root", "RECREATE") tF.cd() tT = TTree("clusters", "clusters") tT.Branch('clusters_xyz', clusters_xyz) tT.Branch('clusters_type', clusters_type) tT.Branch('clusters_id', clusters_id) sidesarr = getLogicSidesArr() n = 0 ### init n for n in range(Nevt): ## clear the output vectors clusters_xyz.clear() clusters_type.clear() clusters_id.clear() ### embed some noise ***clusters***
import numpy as np import sys, os import pickle # from matplotlib import pyplot as plt from ROOT import TFile, TTree, AddressOf import sys from DataFormats.FWLite import Events, Handle from array import array from math import hypot, pi import codecs ROOT.gSystem.Load("libFWCoreFWLite.so") ROOT.gSystem.Load("libDataFormatsFWLite.so") ROOT.gSystem.Load("libDataFormatsPatCandidates.so") out = TFile('outputRootFile.root', 'RECREATE') tree = TTree('test', 'a test tree') t1_px = array('d', [0]) t1_py = array('d', [0]) t1_pz = array('d', [0]) t1_e = array('d', [0]) t2_px = array('d', [0]) t2_py = array('d', [0]) t2_pz = array('d', [0]) t2_e = array('d', [0]) mtt = array('d', [0]) ht = array('d', [0]) wpt = array('d', [0]) pid = array('d', [0]) wpt_r = array('d', [0]) lx_r = array('d', [0]) nuy_r = array('d', [0])
mean, sigma = self.get_probability_visible(hidden) visible = self.sample_v_given_h(mean, sigma) return visible if __name__ == '__main__': # Input sample = np.loadtxt('../input/signal.d.txt', delimiter=',') sample[:, :3] = np.cos(sample[:, :3]) Nsample = 10000 sample = sample[:Nsample] # Output f = TFile('../output/test.root', 'recreate') tr = TTree('coll', 'coll') v1 = array('f', [0]) v2 = array('f', [0]) v3 = array('f', [0]) v4 = array('f', [0]) v5 = array('f', [0]) bv1 = array('f', [0]) bv2 = array('f', [0]) bv3 = array('f', [0]) bv4 = array('f', [0]) bv5 = array('f', [0]) bbv1 = array('f', [0]) bbv2 = array('f', [0]) bbv3 = array('f', [0]) bbv4 = array('f', [0])
def makeCutFiles(): from ROOT import TFile, TTree, MGTWaveform enrExc, natExc, enrRates, natRates = getOutliers(True) # enrExc, natExc: [:,0]=dsNum, [:,1]=cpd, [:,2]=bkgIdx # enrRates, natRates: [:,0]=rate1, [:,1]=rate2, [:,2]=expo, [:,3]=bkgIdx, [:,4]=cpd, [:,5]=ds cutType = "fr" # which burst cut do we want? pass2 = False outType = "frb2%d" % pctTot if pass2 else "frb%d" % pctTot for ds in [0, 1, 2, 3, 4, "5A", "5B", "5C", 6]: dsNum = int(ds[0]) if isinstance(ds, str) else ds nBkg = bkg.dsMap()[dsNum] bLo, bHi = 0, nBkg if ds == "5A": bLo, bHi = 0, 79 if ds == "5B": bLo, bHi = 80, 112 if ds == "5C": bLo, bHi = 113, 121 runRanges = bkg.getRanges(dsNum) chList = det.getGoodChanList(dsNum) # clear out any files from a previous attempt fList = [ "%s/bkg/cut/%s/%s_ds%d_%d_*.root" % (dsi.dataDir, outType, outType, dsNum, bIdx) for bIdx in range(bLo, bHi + 1) ] for f in fList: fTmp = glob.glob(f) for f in fTmp: os.remove(f) # build skip list dsTmp = ds if ds == "5A": dsTmp = 50 if ds == "5B": dsTmp = 51 if ds == "5C": dsTmp = 52 iE = np.where(enrExc[:, 0] == dsTmp) iN = np.where(natExc[:, 0] == dsTmp) skipList = np.vstack((enrExc[iE], natExc[iN])) print("DS-%s, skipList:" % ds) print(skipList) # load ds_livetime output tl = TFile("./data/ds_%s_livetime.root" % str(ds)) lt = tl.Get("dsTree") for bIdx in range(bLo, bHi + 1): print("DS-%s bIdx %d" % (ds, bIdx)) # get channel livetimes (to identify 'zombie' channels declared dead that have hits) live = {ch: 0 for ch in chList} n = lt.Draw( "run:channel:livetime", "run>=%d && run<=%d" % (runRanges[bIdx][0], runRanges[bIdx][-1]), 'goff') ltRun, ltChan, ltLive = lt.GetV1(), lt.GetV2(), lt.GetV3() for i in range(n): ch = ltChan[i] cpd = det.getChanCPD(dsNum, ch) detID = det.getDetIDChan(dsNum, ch) aMass = det.allActiveMasses[detID] live[ch] += ltLive[i] # load the cut files for ch in sorted(chList): cpd = det.getChanCPD(dsNum, ch) if len( np.where( (skipList == (dsTmp, int(cpd), bIdx)).all(axis=1))[0]) > 0: # print("skipping det %s in bkgIdx %d" % (cpd, bIdx)) continue # skip nonexistent files. other parts of chsel should tell us why these aren't here. fName = "%s/bkg/cut/%s%d/%s_ds%d_%d_ch%d.root" % ( dsi.dataDir, cutType, pctTot, cutType, dsNum, bIdx, ch) if not os.path.isfile(fName): # print("no file for det %s in bkgIdx %d" % (cpd, bIdx)) continue tf = TFile(fName) tt = tf.Get("skimTree") nEvt = tt.GetEntries() # skip zombie detectors if live[ch] == 0: print( "Zombie detector, cpd %s ch %d, lt=0, nHits %d. Excluding..." % (cpd, ch, nEvt)) continue outName = "%s/bkg/cut/%s/%s_ds%d_%d_ch%d.root" % ( dsi.dataDir, outType, outType, dsNum, bIdx, ch) outFile = TFile(outName, "RECREATE") outTree = TTree() outTree = tt.CopyTree("") # outTree = tt.CloneTree() # print("Wrote %d entries." % outTree.GetEntries()) # if nEvt != outTree.GetEntries(): # print("ERROR, number of entries don't match: input %d output %d" % (nEvt, outTree.GetEntries())) # return outTree.Write() outFile.Close() tf.Close()
def createplot(giffile, file, filename, folder): '''create graph of current of voltage, find spikes and linearity plots from GIF''' if "D" in filename: return None, None, None, None, None, None, None, None, None, None layer = filename[5:7] #get layer rootdirectory = directories[layer] #set root file directory sectorforarea = filename[8:9] chambertype = folder[0:3] #SM1 or SM2 LM1 or LM2 #to be checked areasSM1 = {"1":(30.0+52.)*(45.2/4.), "2":(52.+70.2)*(43.5/4.), "3":(70.2+84.2)*(43.5/4.), "4":(84.2+104.)*(43.5/4.), "5":(104.+114.9)*(43.5/4.)} areasSM2 = {"6":(130.3+117.1)*(45.7/4.), "7":(150.5+130.3)*(43.6/4.), "8":(166.4+150.5)*(45.6/4.)} areasLM2 = {"6":(189.2+194.482)*(47.46/4.), "7":(194.482+200.1)*(46.08/4.), "8":(200.1+201.4)*(46.8/4.)} areasLM1 = {"1":(48.8+85.)*(45.3/4.), "2":(85.+114.8)*(45.3/4.), "3":(114.8+137.7)*(43.5/4.), "4":(137.7+170.0)*(43.5/4.), "5":(170.0+187.8)*(43.5/4.)} if chambertype == "SM1": areas=areasSM1 elif chambertype == "SM2": areas=areasSM2 elif chambertype == "LM1": areas=areasSM1 elif chambertype == "LM2": areas=areasSM2 else: print "areas not found" area = areas[sectorforarea] times = [x.split(' ')[0] for x in open(file,"r").readlines()] if len(times) == 1: print "Exception -----> Only one data in "+str(filename)+".dat \n" #handle exception of empty files (should not be the case after daq fixing) return None, None, None, None, None, None, None, None, None if not times: print "Exception -----> File empty: "+str(filename)+".dat \n" return None, None, None, None, None, None, None, None, None times = [x.replace(':',' ') for x in times] times = [x.replace('/',' ') for x in times] times = [x.replace('_',' ') for x in times] times = [dt.strptime(x, '%m %d %Y %H %M %S') for x in times] starttime = times[0] dates = [starttime] times = [int((x-starttime).total_seconds()) for x in times] values = [float(x.split(' ')[1]) for x in open(file,"r").readlines()] newtimes = range(times[len(times)-1]) newvalues = [None]*len(newtimes) for counter, value in enumerate(newvalues): if counter in times: newvalues[counter] = values[times.index(counter)] else: newvalues[counter] = newvalues[counter-1] for counter in range(len(newtimes)-1): dates.append(dates[counter]+td(seconds=1)) rootdates = [TDatime(x.year, x.month, x.day, x.hour, x.minute, x.second) for x in dates] #end of creating vectors for times, dates, values (current or voltage) sectorscurrent = None #default values sectorsvoltage = None meancurrent = None nospike_meancurrent = None #to have current not affected by spikes meanvoltage = None notrips_meanvoltage = None duration = len(newtimes) #total seconds from start to stop #data from gif file (for attenuation)------------------------ atten = [x.split(' ') for x in open(giffile,"r").readlines()[1:]] #read attenutation factor exept first line (header) #atten = [x for x in atten if len(x) == 11] #used before change in daq atten_times = [x[0] for x in atten] atten_values = [float(x[1]) for x in atten] atten_times = [x.replace(':',' ') for x in atten_times] atten_times = [x.replace('/',' ') for x in atten_times] atten_times = [x.replace('_',' ') for x in atten_times] atten_times = [dt.strptime(x, '%m %d %Y %H %M %S') for x in atten_times] atten_starttime = atten_times[0] atten_dates = [atten_starttime] atten_times = [int((x-atten_starttime).total_seconds()) for x in atten_times] atten_newtimes = range(atten_times[len(atten_times)-1]) atten_newvalues = [None]*len(atten_newtimes) for atten_counter, atten_value in enumerate(atten_newvalues): if atten_counter in atten_times: atten_newvalues[atten_counter] = atten_values[atten_times.index(atten_counter)] else: atten_newvalues[atten_counter] = atten_newvalues[atten_counter-1] for counter in range(len(atten_newtimes)-1): atten_dates.append(atten_dates[counter]+td(seconds=1)) #end of creating arrays atten values and times source = [x.split(' ') for x in open(sourcefile,"r").readlines()[1:]] #read attenutation factor exept first line (header) #atten = [x for x in atten if len(x) == 11] #used before change in daq source_times = [x[0] for x in source] source_values = [float(x[1]) for x in source] #0.0 off 1.0 on source_times = [x.replace(':',' ') for x in source_times] source_times = [x.replace('/',' ') for x in source_times] source_times = [x.replace('_',' ') for x in source_times] source_times = [dt.strptime(x, '%m %d %Y %H %M %S') for x in source_times] source_starttime = source_times[0] source_dates = [source_starttime] source_times = [int((x-source_starttime).total_seconds()) for x in source_times] source_newtimes = range(source_times[len(source_times)-1]) source_newvalues = [None]*len(source_newtimes) for source_counter, source_value in enumerate(source_newvalues): if source_counter in source_times: source_newvalues[source_counter] = source_values[source_times.index(source_counter)] else: source_newvalues[source_counter] = source_newvalues[source_counter-1] for counter in range(len(source_newtimes)-1): source_dates.append(source_dates[counter]+td(seconds=1)) #end of creating arrays source values and times #now important to match starting time of source file and atten file if "i" in filename and "D" not in filename: if source_dates[10] in atten_dates: syncindex = atten_dates.index(source_dates[10]) source_dates = source_dates[10:] else: syncindex = atten_dates.index(source_dates[50]) source_dates = source_dates[50:] atten_dates = atten_dates[syncindex:] if len(atten_dates) > len(source_dates): atten_dates = atten_dates[0:len(source_dates)] atten_newvalues = atten_newvalues[0:len(source_newvalues)] #attenuation values sync to i values elif len(atten_dates) < len(source_dates): source_dates = source_dates[0:len(atten_dates)] source_newvalues = source_newvalues[0:len(atten_newvalues)] #syn complete #for counter in range(0,50): # print source_dates[counter],source_newvalues[counter],atten_dates[counter],atten_newvalues[counter] #part added on 7/10/2019 to sync also the end of data from source.dat and effectiveattenuation.dat if len(atten_newvalues)>len(source_newvalues): atten_newvalues = atten_newvalues[:len(source_newvalues)] else: source_newvalues = source_newvalues[:len(atten_newvalues)] for counter, atten_newvalue in enumerate(atten_newvalues): if counter < len(source_newvalues): if source_newvalues[counter] == 0.0: atten_newvalues[counter] = 0.0 #put attenuation to 0 il source is off, i will later remove zeros ''' else: #else assing 0 to all remaining current values if source was off for the last minutes if source_newvalues[len(source_newvalues)-1] == 0.0: for x in atten_newvalues[len(source_newvalues)-1::len(atten_newvalues)-1]: x = 0.0 ''' #completed date array and attenuation array from GIF #now important to match starting time with data from chamber #not done for drift if "i" in filename and "D" not in filename: if dates[10] in atten_dates: syncindex = atten_dates.index(dates[10]) dates = dates[10:] else: syncindex = atten_dates.index(dates[100]) #mettere 100 e provare! dates = dates[100:] atten_dates = atten_dates[syncindex:] if len(atten_dates) > len(dates): atten_dates = atten_dates[0:len(dates)] atten_newvalues = atten_newvalues[0:len(newvalues)] #attenuation values sync to i values elif len(atten_dates) < len(dates): dates = dates[0:len(atten_dates)] newvalues = newvalues[0:len(atten_newvalues)] #syn complete! #if want to check sync, first 50 seconds #for counter in range(0,50): # print dates[counter],newvalues[counter],atten_dates[counter],atten_newvalues[counter] #end data from gif file-------------------------------------- new_rootdates = [TDatime(x.year, x.month, x.day, x.hour, x.minute, x.second) for x in dates] #rootdates with updated dates #start analysis, i'm in "i" not drift files sectorscurrent = filename[5:9] #sector name meancurrent = np.mean(newvalues) #mean of current value #Identify drops valuesdeltas = np.diff(newvalues) valuesdeltas = [0]+valuesdeltas #differences in currents of voltages #remove spikes in current files copynewvalues = copy.copy(newvalues) #need to copy it to pass to function below nospike_newvalues = search.removespikes_atgif(valuesdeltas, copynewvalues, atten_newvalues) #remove spikes when source on at gif current files #for i in range(3): # nospike_newvalues = search.removespikes_atgif([0]+np.diff(nospike_newvalues), nospike_newvalues, atten_values) #two times to actually remove spikes #nospike_newvalues = search.removespikes_atgif([0]+np.diff(nospike_newvalues), nospike_newvalues, atten_values) #two times to actually remove spikes #nospike_newvalues = search.removespikes_atgif([0]+np.diff(nospike_newvalues), nospike_newvalues, atten_values) #two times to actually remove spikes #nospike_newvalues = search.removespikes_atgif([0]+np.diff(nospike_newvalues), nospike_newvalues, atten_values) #two times to actually remove spikes nospike_meancurrent = np.mean(nospike_newvalues) #used to have real baseline of the current under fixed flux setattenvalues = set(atten_newvalues) setattenvalues = [x for x in setattenvalues if float(x) != 0.] #remove 0 setattenvalues.sort(reverse=True) #from min to max setmeancurrents = [] normalizedsetmeancurrents = [] #print len(newvalues), len(atten_newvalues) currentatzero = [x for counter, x in enumerate(newvalues[0:len(atten_newvalues)]) if atten_newvalues[counter] == 0.] #current when source is off currentatzero = np.mean(currentatzero) #current mean when source is off for setattenuation in setattenvalues: #using nospike_newvalues here to not count spikes startindex = atten_newvalues.index(setattenuation) #first second at a given attenuation lastindex = len(atten_newvalues)-atten_newvalues[::-1].index(setattenuation)-1 #last second at a given attenuation middle = (lastindex-startindex)/2 found = [x for x in nospike_newvalues[lastindex-60:lastindex]] setmeancurrents.append(float(np.mean(found))) normalizedsetmeancurrents.append(float(np.mean(found))/area) #this way gave problem as underestimates the current values #found = [x for counter, x in enumerate(nospike_newvalues[0:len(atten_newvalues)]) if atten_newvalues[counter] == setattenuation] #setmeancurrents.append(float(np.mean(found))) #if want to remove offset ''' for counter in range(len(setmeancurrents)): setmeancurrents[counter] = setmeancurrents[counter] - currentatzero #remove offset ''' for counter, setattenvalue in enumerate(setattenvalues): print setattenvalue, setmeancurrents[counter] #to check linearity of sectors setattenvalues = [float(x)**(-1) for x in setattenvalues] #perform 1/attenfactor #tools.write_attenuationrootgraph(setattenvalues, normalizedsetmeancurrents, filename, "1/attenuation", "i", dir_summary) graphlinearity = classes.attenuationrootgraph(filename[5:9], setattenvalues, normalizedsetmeancurrents, filename, "1/attenuation", "i", dir_summary) #find spikes -> new part to use threshold over plateu at given attenuation filter without spikes spikecounter, filename, spikedates, spikeseconds, spikenames, spikeduration = search.findspikes_atgif(newvalues, atten_newvalues, setmeancurrents, setattenvalues, dates, newtimes, filename) if "i" in filename and "D" in filename: sectorscurrent = None nospike_meancurrent = None if "v" in filename: #it's a voltage file sectorsvoltage = filename[5:9] meanvoltage = np.mean(newvalues) #Identify drops valuesdeltas = np.diff(newvalues) valuesdeltas = [0]+valuesdeltas #differences in currents of voltages copynewvalues = copy.copy(newvalues) notrips_newvalues = search.removetrips(valuesdeltas, copynewvalues) notrips_meanvoltage = np.mean(notrips_newvalues) if "D" in filename: sectorsvoltage = None notrips_meanvoltage = None if "i" not in filename or "D" in filename: spikenames = None duration = None spikeseconds = None spikeduration = None #write layer graphs #tools.write_roothistogram(newvalues, filename, filename[0], "Entries", rootdirectory) #if you want additional histograms if "i" in filename and "D" not in filename: #tools.write_rootdategraph_fromgif(rootdates, nospike_newvalues, filename, "time (s)", filename[0], rootdirectory) #plot graphs #tools.write_rootdategraph_plusatten(new_rootdates, newvalues, atten_newvalues, filename, "time (s)", filename[0], rootdirectory) #plot graph current + source graph_atten = classes.rootdategraph_plusatten(filename[5:9], new_rootdates, newvalues, atten_newvalues, filename, "time (s)", filename[0], rootdirectory) #or nospike_newvalues tools.write_rootdategraph_fromgif(rootdates, newvalues, filename, "time (s)", filename[0], rootdirectory) else: tools.write_rootdategraph_fromgif(rootdates, newvalues, filename, "time (s)", filename[0], rootdirectory) #plot graphs graph_atten = None graphlinearity = None #tools.write_rootdategraph_fromgif(rootdates, newvalues, filename, "time (s)", filename[0], rootdirectory) #plot graphs if "D" not in filename: #create trees if "i" in filename: tree = TTree(filename, "tree") newvalue = array( 'f', [ 0 ] ) branch = tree.Branch(filename, newvalue, "newvalue/F") treesource = TTree(filename+"_source", "tree") atten = array('f', [0]) branch = treesource.Branch(filename, atten, "atten/F") for i in range(len(newvalues)): newvalue[0] = newvalues[i] tree.Fill() if i<len(atten_newvalues): atten[0] = atten_newvalues[i] treesource.Fill() tree.Write() treesource.Write() if "v" in filename: tree = TTree(filename, "tree") newvalue = array( 'f', [ 0 ] ) branch = tree.Branch(filename, newvalue, "newvalue/F") for i in range(len(newvalues)): newvalue[0] = newvalues[i] tree.Fill() tree.Write() return graph_atten, graphlinearity, spikenames, spikeduration, duration, sectorsvoltage, notrips_meanvoltage, sectorscurrent, nospike_meancurrent, spikeseconds
zi[zi < 0]= 0. zi[np.abs(xi) > eta_max/eta_scale]= 0. #print xi #print np.array(zi).shape return xi, yi, zi nnn = 40#2 Npix = int(nnn)#int(40)#int(128)#int(512)#int(32) imgpix = int(nnn)#int(40)#int(128)#int(512)#int(32) path = '/beegfs/desy/user/hezhiyua/backed/dustData/crab_folder_v2/' pathOut = '/beegfs/desy/user/hezhiyua/backed/dustData/crab_folder_v2/test/' Fname = 'VBFH_HToSSTobbbb_MH-125_MS-40_ctauS-500_TuneCUETP8M1_13TeV-powheg-pythia8_PRIVATE-MC.root' fout = TFile(pathOut + 'for2d.root','recreate') tout = TTree('tree44','tree44') img = np.zeros(imgpix**2, dtype=float) imgVec = ROOT.std.vector('float')() tout.Branch('imgv', imgVec) tout.Branch('img', img, 'img[' + str(imgpix**2) + ']/D') #entries = tin.GetEntriesFast() entries = 100
def main(barrel=first_arg, layer=second_arg): runs = askruns() staven = input('Type the number of the first stave in the (half-)layer: ') runnum = array('i', [0]) currstave = array('i', [0]) currchip = array('i', [0]) avgthr = array('f', [0.]) col = array('i', [0]) row = array('i', [0]) f = open("datatoanalyse.txt", "r") ## file with all paths #open file with all the paths ftree = TFile.Open( "../Data/{}_Layer{}_thresholds_tree_from_run{}_to_run{}.root".format( barrel, layer, runs[0], runs[1]), "recreate") ## file containing the tree roottree1 = TTree("thrscan_thr", "thrscan_thr") roottree1.Branch("runnum", runnum, "runnum/I") roottree1.Branch("stavenum", currstave, "stavenum/I") roottree1.Branch("chipnum", currchip, "chipnum/I") roottree1.Branch("avgchipthr", avgthr, "avgchipthr/F") roottree2 = TTree("thrscan_deadpix", "thrscan_deadpix") roottree2.Branch("runnum", runnum, "runnum/I") roottree2.Branch("stavenum", currstave, "stavenum/I") roottree2.Branch("chipnum", currchip, "chipnum/I") roottree2.Branch("col", col, "col/I") roottree2.Branch("row", row, "row/I") sum = [0] * 200 counter = [0] * 200 for xline in f: #loop on file lines (paths) run = re.search('run(.+?)/thr', xline) if (run): runnum[0] = int(run.group(1)) # run number if (runs[0] <= runnum[0] <= runs[1]): #read data datathr = readdata(str(xline.rstrip())) # calculate the average thr chip by chip for i in range(len(datathr)): ## loop on rows currstave[0] = int(staven) + math.floor(i / 512.) for j in range(len(datathr[i])): ##loop on columns currchip[0] = math.floor(j / 1024) if (datathr[i][j] != 0): sum[currchip[0]] += datathr[i][j] counter[currchip[0]] += 1 if (datathr[i][j] == 0): #dead pixels row[0] = i col[0] = j roottree2.Fill() if ( int(staven) + math.floor( (i + 1) / 512.) > currstave[0] ): ## when changing stave, write into the tree and reset the counters for ichip in range(len(counter)): if (counter[ichip] != 0): avgthr[0] = sum[ichip] / counter[ichip] currchip[0] = ichip roottree1.Fill() sum = [0] * 200 counter = [0] * 200 roottree1.Write() roottree2.Write() ftree.Close() f.close()
IsMC = True print() print('<EVID: %s> -- Load up calibration map.'%_tag) calibfile = TFile.Open(_calibmap,'read') calibMap0 = calibfile.Get("hImageCalibrationMap_00") calibMap1 = calibfile.Get("hImageCalibrationMap_01") calibMap2 = calibfile.Get("hImageCalibrationMap_02") calibMap_v = [calibMap0,calibMap1,calibMap2] print() print('<EVID: %s> -- Create target root file and get started!'%_tag) outFileName = 'FinalVertexVariables-prime_'+_tag+'_1M1P.root' outFileName = os.path.join(_dest,outFileName) outFile = TFile(outFileName,'RECREATE') outTree = TTree('FinalVertexVariables','Final Vertex Variable Tree') ## lepton agnostic _run = MakeTreeBranch(outTree,'run','int') _subrun = MakeTreeBranch(outTree,'subrun','int') _event = MakeTreeBranch(outTree,'event','int') _vtxid = MakeTreeBranch(outTree,'vtxid','int') _x = MakeTreeBranch(outTree,'Xreco','float') _y = MakeTreeBranch(outTree,'Yreco','float') _z = MakeTreeBranch(outTree,'Zreco','float') _infiducial = MakeTreeBranch(outTree,'InFiducial','int') _anyReco = MakeTreeBranch(outTree,'AnyReco','int') _ntracks = MakeTreeBranch(outTree,'NTracks','int') _n5tracks = MakeTreeBranch(outTree,'N5cmTracks','int') _passCuts = MakeTreeBranch(outTree,'PassSimpleCuts','int') _passShowerReco = MakeTreeBranch(outTree,'PassShowerReco','int')
default=0.81) (options, args) = parser.parse_args() #import pyroot_logon import root_logon from ROOT import gROOT gROOT.ProcessLine('.L truncRMS.cc+') from ROOT import TFile, TTree, gPad, TFitter, kBlue, kGreen, \ Double, Long, \ setData, hookupMinuit, makeHOFunc dataf = TFile(args[0]) dataTree = TTree() dataf.GetObject('plotanal/dataTree', dataTree) mipE = options.mip lblHB = 'HB_E' lblEB = 'EB_E' lblHO = '(HO_E' cuts = '(HB_E9 + EB_E9 > 5)' if (options.towers > 1): lblHB += str(options.towers) lblEB += str(options.towers) lblHO += str(options.towers) lblHO += '/' + str(mipE) + ')' barrelH = TH1D('barrelH', 'barrelH', 100, 0., hcalhits.GetMaximum(lblHB)) hcalhits.Draw(lblEB + ' + ' + lblHB + '>>barrelH', cuts)
info=info.replace('\t','') info=info.split('*') variables=[] for v in info: if v!='Row' and v!='': variables.append(v) variable_amount_array.append(len(variables)) #variable_amount_current = len(variables) #if (f > 0):# check to make sure all variables are in all files #if (variable_amount_current != variable_amount_prior): #sys.exit('differing variable amounts between \n' + files[f] + '\n and \n' + files[f-1]) #variable_amount_prior = len(variables) fout=TFile.Open(outfiles[f],"RECREATE") tout = TTree("tmvatree","tmvatree") N=tin.GetEntries() #a=numpy.zeros(1,dtype=float) #tout.Branch('test',a,'test/D') #for x in range(50): #a[0]=3.0 additionalvariables = ['XS','BR','LUM','NGE','B2','B3','RND','CUT','Thrust','DeltaPz','DeltaPhi_ZH','TransMass','TransMass_Eff','CScostheta']#,'CScostheta']#,'ST','CScos'] for v in additionalvariables: exec(v+' = numpy.zeros(1,dtype=float)') exec('tout.Branch(\''+v+'\','+v+',\''+v+'/D\')') #SetPtEtaPhiM #DeltaPhi_ll = fabs(L1_4.DeltaPhi(L2_4))
def test01TemplatedBranchCreation(self): """Templated call when creating a branch""" t = TTree() t.Branch("a", 0)
print(y_test) print(model.predict(X_test)) #make prediction myfile = TFile.Open("test.root") tree = myfile.Get("treeNN") beginentry = 0 #endentry=200 endentry = tree.GetEntries() output_file = TFile.Open('output.root', 'recreate') nn_psd = array('f', [0.]) psd = array('f', [0.]) tof = array('f', [0.]) treeout = TTree('mytree', '') treeout.Branch('nn_psd', nn_psd, 'nn_psd/F') treeout.Branch('psd', psd, 'psd/F') treeout.Branch('tof', tof, 'tof/F') for i in xrange(beginentry, endentry): tree.GetEntry(i) wsample = np.empty([1, wlen], dtype='float32') for j in xrange(wlen): wsample[0][j] = tree.w[j] nnpre = model.predict(wsample) nn_psd[0] = nnpre[0] #print nn_psd[0] psd[0] = tree.psd tof[0] = tree.tof treeout.Fill()
#!/afs/cern.ch/sw/lcg/releases/LCG_72a/Python/2.7.6/x86_64-slc6-gcc48-opt/bin/python # #!/usr/bin/python from ROOT import TDatime, TTree, TFile import numpy as np from os import walk, path, listdir, system import re import xml.etree.cElementTree as et #from array import array # open the output file f = TFile("tree2.root", "recreate") t = TTree("t", "t") # create 1 dimensional float arrays (python's float datatype corresponds to c++ doubles) # as fill variables run = np.zeros([1], dtype=np.uint32) good = np.zeros([1], dtype=np.bool) numberOfDefects = np.zeros([3,9,12,56], dtype=np.uint32) numberOfNewDefects = np.zeros([3,9,12,56], dtype=np.uint32) numberOfRefDefects = np.zeros([3,9,12,56], dtype=np.uint32) numberOfNPtGainDefects = np.zeros([3,9,12,56], dtype=np.uint32) numberOfNoiseOccupancyDefects = np.zeros([3,9,12,56], dtype=np.uint32) numberOfNoisyStripsMon = np.zeros([3,9,12,56], dtype=np.uint32) numberOfNoisyStripsMonOnly = np.zeros([3,9,12,56], dtype=np.uint32) channelID = np.zeros([3,9,12,56], dtype=np.uint32) noise_occup = np.zeros([3,9,12,56], dtype=np.float32) eff = np.zeros([3,9,3], dtype=np.float32) n_err_link = np.zeros([3,9,3], dtype=np.uint32) noisy_strips_measured = np.zeros([1], dtype=np.bool) noise_occup_measured = np.zeros([1], dtype=np.bool)
def main(args): start_time = time.time() # Upload data for analysis input_file = TFile.Open(args.path_to_file, "READ") input_tree = input_file.lumical print("Total n events in loaded files: ", input_tree.GetEntries()) # Create output root file. # Create output root file before the tree!!! It prevents memory leakage output_file = TFile("./lucas_geocuts.root", "RECREATE") output_tree = TTree('lumical', 'MC') # Create variables associated with the output tree n_triggers = array.array('i', [0]) trigger1 = array.array('i', [0]) trigger2 = array.array('i', [0]) trigger3 = array.array('i', [0]) tr1_n_hits = array.array('i', [0]) tr1_hit_pad = array.array('i', [0] * 128) tr1_hit_sector = array.array('i', [0] * 128) tr1_hit_layer = array.array('i', [0] * 128) tr1_hit_x = array.array('f', [0.0] * 128) tr1_hit_y = array.array('f', [0.0] * 128) tr1_hit_energy = array.array('f', [0.0] * 128) tr2_n_hits = array.array('i', [0]) tr2_hit_pad = array.array('i', [0] * 128) tr2_hit_sector = array.array('i', [0] * 128) tr2_hit_layer = array.array('i', [0] * 128) tr2_hit_x = array.array('f', [0.0] * 128) tr2_hit_y = array.array('f', [0.0] * 128) tr2_hit_energy = array.array('f', [0.0] * 128) cal_n_hits = array.array('i', [0]) cal_hit_pad = array.array('i', [0] * 128 * 5) cal_hit_sector = array.array('i', [0] * 128 * 5) cal_hit_layer = array.array('i', [0] * 128 * 5) cal_hit_x = array.array('f', [0.0] * 128 * 5) cal_hit_y = array.array('f', [0.0] * 128 * 5) cal_hit_energy = array.array('f', [0.0] * 128 * 5) # Create branches in the output tree for these variables output_tree.Branch('n_triggers', n_triggers, 'n_triggers/I') output_tree.Branch('trigger1', trigger1, 'trigger1/I') output_tree.Branch('trigger2', trigger2, 'trigger2/I') output_tree.Branch('trigger3', trigger3, 'trigger3/I') output_tree.Branch('tr1_n_hits', tr1_n_hits, 'tr1_n_hits/I') output_tree.Branch('tr1_hit_pad', tr1_hit_pad, 'tr1_hit_pad[tr1_n_hits]/I') output_tree.Branch('tr1_hit_sector', tr1_hit_sector, 'tr1_hit_sector[tr1_n_hits]/I') output_tree.Branch('tr1_hit_layer', tr1_hit_layer, 'tr1_hit_layer[tr1_n_hits]/I') output_tree.Branch('tr1_hit_x', tr1_hit_x, 'tr1_hit_x[tr1_n_hits]/F') output_tree.Branch('tr1_hit_y', tr1_hit_y, 'tr1_hit_y[tr1_n_hits]/F') output_tree.Branch('tr1_hit_energy', tr1_hit_energy, 'tr1_hit_energy[tr1_n_hits]/F') output_tree.Branch('tr2_n_hits', tr2_n_hits, 'tr2_n_hits/I') output_tree.Branch('tr2_hit_pad', tr2_hit_pad, 'tr2_hit_pad[tr2_n_hits]/I') output_tree.Branch('tr2_hit_sector', tr2_hit_sector, 'tr2_hit_sector[tr2_n_hits]/I') output_tree.Branch('tr2_hit_layer', tr2_hit_layer, 'tr2_hit_layer[tr2_n_hits]/I') output_tree.Branch('tr2_hit_x', tr2_hit_x, 'tr2_hit_x[tr2_n_hits]/F') output_tree.Branch('tr2_hit_y', tr2_hit_y, 'tr2_hit_y[tr2_n_hits]/F') output_tree.Branch('tr2_hit_energy', tr2_hit_energy, 'tr2_hit_energy[tr2_n_hits]/F') output_tree.Branch('cal_n_hits', cal_n_hits, 'cal_n_hits/I') output_tree.Branch('cal_hit_pad', cal_hit_pad, 'cal_hit_pad[cal_n_hits]/I') output_tree.Branch('cal_hit_sector', cal_hit_sector, 'cal_hit_sector[cal_n_hits]/I') output_tree.Branch('cal_hit_layer', cal_hit_layer, 'cal_hit_layer[cal_n_hits]/I') output_tree.Branch('cal_hit_x', cal_hit_x, 'cal_hit_x[cal_n_hits]/F') output_tree.Branch('cal_hit_y', cal_hit_y, 'cal_hit_y[cal_n_hits]/F') output_tree.Branch('cal_hit_energy', cal_hit_energy, 'cal_hit_energy[cal_n_hits]/F') n_events = input_tree.GetEntries() for idx, event in enumerate(input_tree): # if idx == 20000: # break if idx % (1000) == 0: time_min = (time.time() - start_time) // 60 time_sec = (time.time() - start_time) % 60 print('Event: {} out of {};'.format(idx, n_events), end=' ') print('{} min {} sec'.format(time_min, time_sec)) hits_tr1, hits_tr2, hits_cal = make_hits_lists(event) n_triggers[0] = event.n_triggers trigger1[0] = event.trigger1 trigger2[0] = event.trigger2 trigger3[0] = event.trigger3 tr1_n_hits[0] = len(hits_tr1) for i, hit in enumerate(hits_tr1): tr1_hit_pad[i] = hit.pad tr1_hit_sector[i] = hit.sector tr1_hit_layer[i] = hit.layer tr1_hit_energy[i] = hit.energy tr1_hit_x[i] = hit.x tr1_hit_y[i] = hit.y tr2_n_hits[0] = len(hits_tr2) for i, hit in enumerate(hits_tr2): tr2_hit_pad[i] = hit.pad tr2_hit_sector[i] = hit.sector tr2_hit_layer[i] = hit.layer tr2_hit_energy[i] = hit.energy tr2_hit_x[i] = hit.x tr2_hit_y[i] = hit.y cal_n_hits[0] = len(hits_cal) for i, hit in enumerate(hits_cal): cal_hit_pad[i] = hit.pad cal_hit_sector[i] = hit.sector cal_hit_layer[i] = hit.layer cal_hit_x[i] = hit.x cal_hit_y[i] = hit.y cal_hit_energy[i] = hit.energy output_tree.Fill() output_tree.Write() output_file.Close() print("Hooray, extracted tree file is ready, take it :3")
import service #Hardcoded calibration constants and Chi factor #You can estimate them with dedicated part scincalibconstant = 23.92 #MeV/MeV chercalibconstant = 23.00 #MeV/Cpe Chi = 0.50 #to be better defined HEcher = 0.360 HEscin = 0.679 #Set root file and tree to be analyzed gROOT.Reset() file = raw_input("Insert namefile: ") filename = str(file) + "B4.root" inputFile = TFile(str(filename)) #root input file tree = TTree() #Tree name B4 inputFile.GetObject("B4", tree) #--------------------------------------------------------------------------------------------------- #Section used only if you want to estimate the calibration constants and Chi factor of a module. #DREAM is calibrated with electrons. The input file must be from electron events with the same energy. #To estimate h/e Cher and scin values and the Chi factor a second root file with pions (+ or -) is needed. #To later perform h/e Cher and scin computation and Chi factor print "You have given an electron file event, now pass a pion one for h/e estimation.\n" namepionfile = raw_input("Insert name of ROOT pion (+ or -) file: ") #Set parameters NofEvents = tree.GetEntries() fastchercalibconstant = 0 #Done without building clusters fastscincalibconstant = 0 #Done without building clusters
from ROOT import TFile, TEnv, TTree from ROOT import std for key in signatures['tuning'].keys(): s = signatures['tuning'][key] m = signatures['metadata'] key = clearName( key ) f1 = TFile(('TrigL2CaloRinger%sConstants.root')%(key) ,'recreate') createRootParameter( 'int' , '__version__', 2).Write() f1.mkdir('tuning') f1.cd('tuning') # t->GetEntry(0) t->GetEntry(0);; Compile the neural network t = TTree('discriminators','') n = std.vector('unsigned int')() w = std.vector('double')() b = std.vector('double')() etbin = std.vector('double')() etabin = std.vector('double')() t.Branch( 'nodes' , 'vector<unsigned int>', n ) t.Branch( 'weights' , 'vector<double>', w ) t.Branch( 'bias' , 'vector<double>', b ) t.Branch( 'etBin' , 'vector<double>', etbin ) t.Branch( 'etaBin' , 'vector<double>', etabin ) for bkey in sorted(s.keys()): net = s[bkey] listToVector( net['discriminator']['weights'] , w )
DATE August 2020 \n''') def convert(ecms, save): pull = array('d', [999.]) save.Branch('pull', pull, 'pull/D') f_txt = open('./txts/xs_diff_' + str(ecms) + '.txt', 'r') for line in f_txt.readlines(): rs = line.rstrip('\n') pull[0] = float(rs) save.Fill() f_txt.close() if __name__ == '__main__': args = sys.argv[1:] if len(args)<1: usage() sys.exit() ecms = int(args[0]) if not os.path.exists('/besfs5/users/$USER/bes/DDPIPI/v0.2/ana/sys_err/omega/' + str(ecms) + '/'): os.system('mkdir -p /besfs5/users/$USER/bes/DDPIPI/v0.2/ana/sys_err/omega/' + str(ecms) + '/') f_root = TFile('/besfs5/users/$USER/bes/DDPIPI/v0.2/ana/sys_err/omega/' + str(ecms) + '/xs_diff_' + str(ecms) + '.root', 'RECREATE') save = TTree('save', 'save') convert(ecms, save) f_root.cd() save.Write() f_root.Close()
def __init__(self, name): print 'TreeProducerCommon is called', name # TREE self.outputfile = ROOT.TFile(name, 'RECREATE') self.tree = TTree('tree','tree') # HISTOGRAM self.cutflow = TH1D('cutflow', 'cutflow', 25, 0, 25) self.pileup = TH1D('pileup', 'pileup', 100, 0, 100) ## CHECK genPartFlav #self.flags_LTF_DM1 = TH1D('flags_LTF_DM1', "flags for l #rightarrow #tau_{h}, DM1", 18, 0, 18) #self.flags_LTF_DM0 = TH1D('flags_LTF_DM0', "flags for l #rightarrow #tau_{h}, DM0", 18, 0, 18) #self.flags_LTF_mis = TH1D('flags_LTF_mis', "flags for l #rightarrow #tau_{h}, DM1, wrong genPartFlav", 18, 0, 18) #self.flags_LTF_DM1_sn1 = TH1D('flags_LTF_DM1_sn1', "flags for l #rightarrow #tau_{h}, DM1 (status!=1)", 18, 0, 18) #self.flags_LTF_DM0_sn1 = TH1D('flags_LTF_DM0_sn1', "flags for l #rightarrow #tau_{h}, DM0 (status!=1)", 18, 0, 18) #self.flags_LTF_mis_sn1 = TH1D('flags_LTF_mis_sn1', "flags for l #rightarrow #tau_{h}, DM1, wrong genPartFlav (status!=1)", 18, 0, 18) #for hist in [self.flags_LTF_DM1, self.flags_LTF_DM0, self.flags_LTF_mis, self.flags_LTF_DM0_sn1, self.flags_LTF_DM1_sn1, self.flags_LTF_mis_sn1]: # hist.GetXaxis().SetBinLabel( 1, "isPrompt" ) # hist.GetXaxis().SetBinLabel( 2, "isDirectPromptTauDecayProduct" ) # hist.GetXaxis().SetBinLabel( 3, "isHardProcess" ) # hist.GetXaxis().SetBinLabel( 4, "fromHardProcess" ) # hist.GetXaxis().SetBinLabel( 5, "isDirectHardProcessTauDecayProduct" ) # hist.GetXaxis().SetBinLabel( 6, "fromHardProcessBeforeFSR" ) # hist.GetXaxis().SetBinLabel( 7, "isFirstCopy" ) # hist.GetXaxis().SetBinLabel( 8, "isLastCopy" ) # hist.GetXaxis().SetBinLabel( 9, "isLastCopyBeforeFSR" ) # hist.GetXaxis().SetBinLabel(10, "status==1" ) # hist.GetXaxis().SetBinLabel(11, "status==23" ) # hist.GetXaxis().SetBinLabel(12, "status==44" ) # hist.GetXaxis().SetBinLabel(13, "status==51" ) # hist.GetXaxis().SetBinLabel(14, "status==52" ) # hist.GetXaxis().SetBinLabel(15, "other status" ) # hist.GetXaxis().SetLabelSize(0.041) #self.genmatch_corr = TH2D("genmatch_corr","correlation between Tau_genPartFlav and genmatch",6,0,6,6,0,6) #self.genmatch_corr_DM0 = TH2D("genmatch_corr_DM0","correlation between Tau_genPartFlav and genmatch for DM0",6,0,6,6,0,6) #self.genmatch_corr_DM1 = TH2D("genmatch_corr_DM1","correlation between Tau_genPartFlav and genmatch for DM1",6,0,6,6,0,6) ############# # EVENT # ############# self.addBranch('run', int) self.addBranch('lumi', int) self.addBranch('event', int) self.addBranch('isData', bool) self.addBranch('nPU', int) self.addBranch('nTrueInt', int) self.addBranch('npvs', int) self.addBranch('npvsGood', int) self.addBranch('LHE_Njets', int) self.addBranch('metfilter', bool) ############## # WEIGHT # ############## self.addBranch('genweight', float) self.addBranch('weight', float) self.addBranch('trigweight', float) self.addBranch('puweight', float) self.addBranch('zptweight', float) self.addBranch('ttptweight', float) self.addBranch('idisoweight_1', float) self.addBranch('idisoweight_2', float) self.addBranch('btagweight', float) ############ # JETS # ############ self.addBranch('njets', int) self.addBranch('njets50', int) self.addBranch('ncjets', int) self.addBranch('nfjets', int) self.addBranch('nbtag', int) self.addBranch('jpt_1', float) self.addBranch('jeta_1', float) self.addBranch('jphi_1', float) self.addBranch('jdeepb_1', float) self.addBranch('jpt_2', float) self.addBranch('jeta_2', float) self.addBranch('jphi_2', float) self.addBranch('jdeepb_2', float) self.addBranch('bpt_1', float) self.addBranch('beta_1', float) self.addBranch('bpt_2', float) self.addBranch('beta_2', float) self.addBranch('met', float) self.addBranch('metphi', float) self.addBranch('genmet', float) self.addBranch('genmetphi', float) ###self.addBranch('puppimet', float) ###self.addBranch('puppimetphi', float) ###self.addBranch('metsignificance', float) ###self.addBranch('metcovXX', float) ###self.addBranch('metcovXY', float) ###self.addBranch('metcovYY', float) ###self.addBranch('fixedGridRhoFastjetAll', float) ############# # OTHER # ############# self.addBranch('pfmt_1', float) self.addBranch('pfmt_2', float) self.addBranch('m_vis', float) self.addBranch('pt_ll', float) self.addBranch('dR_ll', float) self.addBranch('dphi_ll', float) self.addBranch('deta_ll', float) self.addBranch('pzetamiss', float) self.addBranch('pzetavis', float) self.addBranch('dzeta', float) self.addBranch('dilepton_veto', bool) self.addBranch('extraelec_veto', bool) self.addBranch('extramuon_veto', bool) self.addBranch('lepton_vetos', bool) self.addBranch('ngentauhads', int) self.addBranch('ngentaus', int) self.addBranch('m_genboson', float) self.addBranch('pt_genboson', float) #self.addBranch('m_taub', float) #self.addBranch('m_taumub', float) #self.addBranch('m_tauj', float) #self.addBranch('m_muj', float) #self.addBranch('m_coll_muj', float) #self.addBranch('m_coll_tauj', float) #self.addBranch('mt_coll_muj', float) #self.addBranch('mt_coll_tauj', float) #self.addBranch('m_max_lj', float) #self.addBranch('m_max_lb', float) #self.addBranch('m_mub', float) self.nPU[0] = -1 self.nTrueInt[0] = -1 self.LHE_Njets[0] = -1 self.weight[0] = 1. self.genweight[0] = 1. self.trigweight[0] = 1. self.puweight[0] = 1. self.idisoweight_1[0] = 1. self.idisoweight_2[0] = 1. self.btagweight[0] = 1. self.zptweight[0] = 1. self.ttptweight[0] = 1. self.genmet[0] = -1 self.genmetphi[0] = -9 self.m_genboson[0] = -1 self.pt_genboson[0] = -1
#root_files_directory='/archive/mc/xenon100/alexkish/drw/Darwin3.0std_Bell-Co60_1e6_job1/' #root_file = sys.argv[1] filename = sys.argv[1] #filename = root_files_directory + root_file file = TFile(filename, 'read') tree = file.Get('t1') nb_entries = tree.GetEntries() #newfilename = root_files_directory #newfile = TFile(root_files_directory + root_file[:-5] + '-t2-z3mm-r10mm.root', 'recreate') newfilename = filename[:-5] + '-t2-z3mm-r10mm.root' newfile = TFile(newfilename, 'recreate') newtree = TTree('t2', '') #che eto takoe?? max_size = 10000 eventid = array('i', [0]) etot_NR = array('f', [0.]) etot_ER = array('f', [0.]) ns_NR = array('i', [0]) ns_ER = array('i', [0]) ed_NR = array('f', max_size * [0.]) ed_ER = array('f', max_size * [0.]) xp_NR = array('f', max_size * [0.]) xp_ER = array('f', max_size * [0.]) yp_NR = array('f', max_size * [0.]) yp_ER = array('f', max_size * [0.])
canv.SaveAs(calo_init.output(0) + ".png") plots = TFile(calo_init.output(0) + ".root", "RECREATE") if calo_init.args.preview: cPreview.SaveAs("preview_" + calo_init.output(0) + ".png") else: canv.SaveAs("sampling_fraction_plots.pdf") canv.SaveAs("sampling_fraction_plots.png") plots = TFile("sampling_fraction.root", "RECREATE") if calo_init.args.preview: cPreview.SaveAs("preview_sampling_fraction.png") for g in all_graphs: g.Write() mean = numpy.zeros(1, dtype=float) std = numpy.zeros(1, dtype=float) t = TTree("samplingFraction", "Sampling fraction for detector layers") t.Branch("mean", mean, "mean/D") t.Branch("std", std, "std/D") for islice in range(0, Nslicesmerged): for ilay in range(0, calo_init.args.merge[islice]): mean[0] = gSF.GetY()[islice] std[0] = gSF.GetErrorY(islice) t.Fill() plots.Write() plots.Close() print("============================================================") print("== to be used in FCCSW, with CalibrateInLayers algorithm: ==") print("============================================================") print "samplingFraction = ", for islice in range(0, Nslicesmerged):
draw_text([calo_init.args.specialLabel], [0.67, 0.78, 0.95, 0.88], kGray + 3, 0).SetTextSize(0.05 * factor) # Save canvas and root file with graph, const term and sampling term if calo_init.output(0): cRes.SaveAs(calo_init.output(0) + ".png") plots = TFile(calo_init.output(0) + ".root", "RECREATE") else: cRes.SaveAs("energy_resolution_plots.gif") plots = TFile("energy_resolution_plots.root", "RECREATE") gRes.Write() gLin.Write() const = numpy.zeros(1, dtype=float) sampl = numpy.zeros(1, dtype=float) constErr = numpy.zeros(1, dtype=float) samplErr = numpy.zeros(1, dtype=float) t = TTree("params", "Fit parameters") t.Branch("const", const, "const/D") t.Branch("sampl", sampl, "sampl/D") t.Branch("constErr", constErr, "constErr/D") t.Branch("samplErr", samplErr, "samplErr/D") const[0] = fitResult.Get().Parameter(0) sampl[0] = fitResult.Get().Parameter(1) constErr[0] = fitResult.Get().Error(0) samplErr[0] = fitResult.Get().Error(1) t.Fill() plots.Write() plots.Close() raw_input("Press ENTER to exit")
def __init__(self, args): # set up logging info logging.getLogger('Analysis') logging.basicConfig(level=logging.INFO, stream=sys.stderr, format='[%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M') logging.info('Beginning job...') # set defaults. These can be overridden with command line arguments # inputs self.filenames = [] self.treedir = 'makeroottree' self.infoname = 'AC1Binfo' self.luminame = 'AC1Blumi' self.treename = 'AC1B' input_file_list = args.input_file_list self.max_events = args.nevents self.skip_events = args.skipevents self.whichfile = args.whichfile self.data_dir = ('{0}/src/AnalysisToolLight/AnalysisTool' '/data'.format(os.environ['CMSSW_BASE'])) # outputs self.output = args.output_filename # put file names into a list called self.filenames with open(input_file_list, 'r') as f: for line in f.readlines(): fname_ = line.strip() if fname_.startswith('#'): continue if not fname_: continue # personal storage options if fname_.startswith('T2_CH_CERN'): fname_ = 'root://eoscms.cern.ch/{0}'.format(fname_[10:]) elif fname_.startswith('T2_US_UCSD'): fname_ = 'root://xrootd.t2.ucsd.edu/{0}'.format( fname_[10:]) self.filenames += [fname_] logging.info('Assembling job information...') # things we will check in the first file self.cmsswversion = '' self.dataset_source = '' self.isdata = None # open first file and load info tree tfile0 = TFile.Open(self.filenames[0]) infotree = tfile0.Get('{0}/{1}'.format(self.treedir, self.infoname)) infotree.GetEntry(0) self.isdata = bool(infotree.isdata) self.ismc = not self.isdata self.cmsswversion = str(infotree.CMSSW_version) # strip " and / from parent dataset name self.dataset_source = ''.join(c for c in str(infotree.source_dataset) if c not in '"/') tfile0.Close('R') # get short CMSSW version that was used to produce these self.cmsswversion = ''.join(self.cmsswversion.split('_')[1:3] + ['X']) # get dataset cross section try: self.nom_xsec = xsecs[self.dataset_source] except: logging.info(' * ') logging.info(' *******') logging.info(' No cross section information found for source:') logging.info(' "{0}".'.format(self.dataset_source)) logging.info(' *******') logging.info(' * ') self.nom_xsec = -1. # set up lumi info and see how many events we have to process infochain = TChain('{0}/{1}'.format(self.treedir, self.infoname)) self.nevents_to_process = 0 # find original sum weights lumichain = TChain('{0}/{1}'.format(self.treedir, self.luminame)) self.numlumis = 0 self.sumweights = 0 self.nevents = 0 for f, fname in enumerate(self.filenames): logging.info('Adding file {0}: {1}'.format(f + 1, fname)) lumichain.Add(fname) infochain.Add(fname) # iterate over lumis to find total number of events # and summed event weights logging.info('') logging.info('Counting events and lumiblocks...') self.numlumis = lumichain.GetEntries() self.numinfos = infochain.GetEntries() for entry in xrange(self.numlumis): lumichain.GetEntry(entry) self.nevents += lumichain.lumi_nevents self.sumweights += lumichain.lumi_sumweights for entry in xrange(self.numinfos): infochain.GetEntry(entry) self.nevents_to_process += infochain.nevents_filled logging.info( (' Number of events found: {0} in {1} lumi sections ' 'in {2} files').format(self.nevents_to_process, self.numlumis, len(self.filenames))) logging.info('Sample will be processed as ' '{0}'.format('DATA' if self.isdata else 'MC')) logging.info('Sample has been identified as coming from:') logging.info(' {0}'.format(self.dataset_source)) if self.ismc: logging.info(' with a nominal cross section of:') logging.info(' {0} pb.'.format(self.nom_xsec)) # initialize map of histograms as empty self.histograms = histograms self.extra_histogram_map = {} # initialise list of category trees self.category_trees = [] # initialise some other options that will be overridden # in the derived class self.path_for_trigger_scale_factors = '' self.do_pileup_reweighting = False self.pu_shift = '' self.include_trigger_scale_factors = False self.hlt_shift = '' self.include_lepton_scale_factors = False self.sf_shift = '' self.use_rochester_corrections = False self.jet_shift = '' self.jet_shift_down = None self.jet_shift_up = None # summary tree self.summary_tree = TTree('Summary', 'Summary') # branches of summary tree # python array: 'L' = unsigned long, 'l' = signed long, 'f' = float # TBranch: 'l' = unsigned long, 'L' = signed long, 'F' = float self.nevents_a = array('L', [self.nevents]) self.summary_tree.Branch('tNumEvts', self.nevents_a, 'tNumEvts/l') self.sumweights_a = array( 'f', [self.sumweights if self.sumweights != 0. else self.nevents]) self.summary_tree.Branch('tSumWts', self.sumweights_a, 'tSumWts/F') self.nom_xsec_a = array('f', [self.nom_xsec]) self.summary_tree.Branch('tCrossSec', self.nom_xsec_a, 'tCrossSec/F') self.summary_tree.Fill() # initialize output file self.outfile = TFile(self.output, 'RECREATE') # initialize event counters self.cutflow = initialize_cutflow(self)
parser.add_option('-b', action='store_true', dest='batch', help='run in batch mode without graphics', default=False) (opts, args) = parser.parse_args() from ROOT import TTree, TFile, TH1F, TH1, gPad, gROOT, TCanvas import sys sys.path.append('/uscms/home/andersj/pyroot') import pyroot_logon fname = args[0] pes = TTree() pes.ReadFile(fname, 'LayerPES[19]/F') BarrelPE = TH1F("BarrelPE", "Barrel PE", 100, 0., 1500. * 30.) BarrelSum = 0. for entry in pes: BarrelSum = 0. for i in range(0, 17): BarrelSum += entry.LayerPES[i] if (BarrelSum > 300): BarrelPE.Fill(BarrelSum) c1 = TCanvas() BarrelPE.Draw()