def makeScanDir(slot, ohN, scanType, startTime, shelf=1, chamber_config=None): """ Makes a directory to store the output scan data and returns the directory path ohN - optohybrid number scanType - scanType, see ana_config.keys() from gempython.gemplotting.utils.anaInfo startTime - an instance of a datetime shelf - uTCA shelf number chamber_config - chamber_config dictionary """ ohKey = (shelf, slot, ohN) if chamber_config is None: from gempython.gemplotting.mapping.chamberInfo import chamber_config from gempython.gemplotting.utils.anautilities import getDirByAnaType if ohKey in chamber_config.keys(): dirPath = getDirByAnaType(scanType, chamber_config[ohKey]) else: dirPath = getDirByAnaType(scanType, "") setupCmds = [] setupCmds.append(["mkdir", "-p", dirPath + "/" + startTime]) setupCmds.append(["chmod", "g+rw", dirPath + "/" + startTime]) setupCmds.append(["unlink", dirPath + "/current"]) setupCmds.append(["ln", "-s", startTime, dirPath + "/current"]) for cmd in setupCmds: runCommand(cmd) return "{:s}/{:s}".format(dirPath, startTime)
def getVFATConfigFileDict(chamber_config, scandate, debug=False): """ Returns a dictionary of vfatConfig files. chamber_config - Dictionary whose key values are geographic address, e.g. (shelf,slot,link), and values are detector serial numbers scandate - Either None or a string specifying the scandate of the files of interest, in YYYY.MM.DD.hh.mm format debug - Prints debugging information if True """ fileDict = {} from gempython.gemplotting.utils.anaInfo import tree_names from gempython.gemplotting.utils.anautilities import getDirByAnaType import os listOfFoundFiles = [] for geoAddr, cName in chamber_config.iteritems(): filename = "{}/{}/vfatConfig.txt".format( getDirByAnaType("sbitRateor", cName), scandate) # Check that this file is not in listOfFoundFiles if ((filename not in listOfFoundFiles) and os.path.isfile(filename)): fileDict[cName] = filename listOfFoundFiles.append(filename) else: if debug: printYellow("filename not found: {}".format(filename)) pass return fileDict
def getChConfigFileDict(chamber_config, scandate, debug=False, takeFromSCurve=True, isTrimScandate=False): """ Returns a dictionary of chConfig files. chamber_config - Dictionary whose key values are geographic address, e.g. (shelf,slot,link), and values are detector serial numbers scandate - Either None or a string specifying the scandate of the files of interest, in YYYY.MM.DD.hh.mm format debug - Prints debugging information if True takeFromSCurve - chConfig file comes from an scurve measurement (e.g. either scurve or trim) isTrimScandate - chConfig file should come from a trim scandate; no effect if takeFromSCurve is False """ fileDict = {} from gempython.gemplotting.utils.anaInfo import tree_names from gempython.gemplotting.utils.anautilities import getDirByAnaType import os listOfFoundFiles = [] for geoAddr, cName in chamber_config.iteritems(): if takeFromSCurve: if isTrimScandate: filename = "{}/{}/chConfig.txt".format( getDirByAnaType("trimV3", cName), scandate) else: filename = "{}/{}/SCurveData/chConfig.txt".format( getDirByAnaType("scurve", cName), scandate) else: filename = "{}/{}/ThresholdScanData/chConfig_MasksUpdated.txt".format( getDirByAnaType("thresholdch", cName), scandate) # Check that this file is not in listOfFoundFiles if ((filename not in listOfFoundFiles) and os.path.isfile(filename)): fileDict[cName] = filename listOfFoundFiles.append(filename) else: if debug: printYellow("filename not found: {}".format(filename)) pass return fileDict
def makePlots(chamberName, anaType, vt1bump, elog_path): call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=threshold --make2D --alphaLabels -a --axisMax=10' os.system(call_command) call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=noise --make2D --alphaLabels -a --axisMin=0.05 --axisMax=0.3' os.system(call_command) call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=ped_eff --make2D --alphaLabels -a --axisMax=1' os.system(call_command) call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=mask --make2D --alphaLabels -a --axisMax=1' os.system(call_command) call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=maskReason --make2D --alphaLabels -a --axisMax=32' os.system(call_command) call_command = 'gemPlotter.py --skipBadFiles --infilename=' + getDirByAnaType( anaType, chamberName ) + 'listOfScanDates.txt --anaType=scurveAna --branchName=vthr --alphaLabels -a' os.system(call_command) call_command = 'mkdir -p ' + elog_path + '/timeSeriesPlots/' + chamberName + '/' + vt1bump + '/' os.system(call_command) call_command = 'mv ' + elog_path + '/summary*.png ' + elog_path + '/timeSeriesPlots/' + chamberName + '/' + vt1bump + '/' os.system(call_command) call_command = 'mv ' + elog_path + '/gemPlotter*.root ' + elog_path + '/timeSeriesPlots/' + chamberName + '/' + vt1bump + '/' os.system(call_command)
exit(os.EX_USAGE) # Append Each File to the tarBallCmd listOfChamberNames = [] for item in list_cmd_tuple: if item[1] not in listOfChamberNames: listOfChamberNames.append(item[1]) rawFile = (tree_names[item[0]])[0] anaKey = "%sAna" % item[0] if "threshold" in anaKey: anaKey = "thresholdAna" anaFile = (tree_names[anaKey])[0] rawFilePath = "%s/%s/%s" % (getDirByAnaType( anaType=item[0], cName=item[1], ztrim=options.ztrim), item[2], rawFile) # basePath/scandate/rawFile tarBallCmd.append(rawFilePath) if not options.onlyRawData: anaFilePath = "%s/%s/%s" % (getDirByAnaType( anaType=item[0], cName=item[1], ztrim=options.ztrim), item[2], anaFile) # basePath/scandate/anaFile tarBallCmd.append(anaFilePath) pass # Make the fake chamberInfo.py file tmpChamberInfoFile = open("chamberInfo.py_tmp", "w") # Write the chamber_config dict tmpChamberInfoFile.write('chamber_config = {\n') for i, cName in enumerate(listOfChamberNames):
envCheck('DATA_PATH') envCheck('ELOG_PATH') buildHome = os.getenv('BUILD_HOME') dataPath = os.getenv('DATA_PATH') elogPath = os.getenv('ELOG_PATH') # Get info from input file from gempython.gemplotting.utils.anautilities import getDirByAnaType, filePathExists, makeListOfScanDatesFile, parseListOfScanDatesFile if (listOfScanDatesFile is None and options.chamberName is not None): makeListOfScanDatesFile(options.chamberName, options.anaType, options.startDate, options.endDate, ztrim=options.ztrim) listOfScanDatesFile = '%s/listOfScanDates.txt' % (getDirByAnaType( options.anaType, options.chamberName, options.ztrim)) pass parsedTuple = parseListOfScanDatesFile(listOfScanDatesFile, alphaLabels=True) listChamberAndScanDate = parsedTuple[0] # Setup output scandates list outputScanDatesName = listOfScanDatesFile.strip('.txt') outputScanDatesName += "_Input4GemPlotter.txt" outputScanDatesFile = open(outputScanDatesName, 'w+') outputScanDatesFile.write('ChamberName\tscandate\n') # invert chamber_config from mapping.chamberInfo import chamber_config, GEBtype linkByChamber = dict(
def calcEff(cName, scandate, vfatList, latBin, bkgSub=False): """ Returns a tuple of (eff, sigma_eff) cName - chamber name, i.e. the value of a given key of the chamber_config dict scandate - scandate of the ultraLatency.py measurement vfatList - list of vfats to use for calculating the efficiency latBin - latency bin to determine the eff for bkgSub - Perform background subtraction """ from gempython.gemplotting.utils.anautilities import getDirByAnaType import os # Setup paths dirPath = "%s/%s" % (getDirByAnaType("latency", cName), scandate) filename_RAW = dirPath + "/LatencyScanData.root" filename_ANA = dirPath + "/LatencyScanData/latencyAna.root" # Load data - RAW import root_numpy as rp import numpy as np list_bNames = ["vfatN", "latency", "Nhits", "Nev"] try: array_VFATData = rp.root2array(filename_RAW, "latTree", list_bNames) pass except Exception as e: print '%s does not seem to exist' % filename_RAW print e exit(os.EX_NOINPUT) # Load data - ANA import ROOT as r try: anaFile = r.TFile(filename_ANA, "READ") except Exception as e: print '%s does not seem to exist' % filename_ANA print e exit(os.EX_NOINPUT) # Determine hits and triggers nTriggers = np.asscalar(np.unique(array_VFATData['Nev'])) nHits = 0.0 for vfat in vfatList: if bkgSub: try: gSignalNoBkg = anaFile.Get("grVFATNSignalNoBkg") vfatHits = r.Double() vfatPos = r.Double() gSignalNoBkg.GetPoint(vfat, vfatPos, vfatHits) nHits += vfatHits except Exception as e: print "grVFATNSignalNoBkg not present in TFile %s" % filename_ANA print "Maybe you forgot to analyze with the --fit, --latSigRange, and --latNoiseRange options?" print e exit(os.EX_DATAERR) else: vfatData = array_VFATData[array_VFATData['vfatN'] == vfat] latData = vfatData[vfatData['latency'] == latBin] nHits += np.asscalar(latData['Nhits']) # Calc Eff & Error return (nHits / nTriggers, calcEffErr(nHits / nTriggers, nTriggers))
def arbitraryPlotter2D(anaType, listDataPtTuples, rootFileName, treeName, branchName, vfat, ROBstr=True, ztrim=4, skipBad=False): """ Provides a list of tuples for 2D data where each element is of the ``(x,y,z)`` form: ``(indepVarVal, vfatCHOrROBstr, depVarVal)`` Args: anaType (string): type of analysis to perform, helps build the file path to the input file(s), from the keys of :any:`utils.anaInfo.ana_config` listDataPtTuples: list of tuples where each element is of the form: ``(cName, scandate, indepVar)``, note ``indepVar`` is expected to be numeric rootFileName (string): name of the ``TFile`` that will be found in the data path corresponding to ``anaType`` treeName (string): name of the ``TTree`` inside ``rootFileName`` branchName (string): name of a branch inside ``treeName`` that the dependent variable will be extracted from vfat (int): vfat number that plots should be made for skipBad (bool): if a file fails to open or the ``TTree`` cannot be found, the input is skipped and the processing continues rather than exiting """ from gempython.gemplotting.utils.anautilities import filePathExists, getDirByAnaType import numpy as np import os import root_numpy as rp # Make branches to load listNames = ["vfatN"] strChanName = "" #Name of channel TBranch, either 'ROBstr' or 'vfatCH' if ROBstr: listNames.append("ROBstr") strChanName = "ROBstr" else: listNames.append("vfatCH") strChanName = "vfatCH" listNames.append(branchName) # Load data listData = [] for dataPt in listDataPtTuples: # Get human readable info cName = dataPt[0] scandate = dataPt[1] indepVarVal = dataPt[2] # Setup Paths dirPath = getDirByAnaType(anaType.strip("Ana"), cName, ztrim) if not filePathExists(dirPath, scandate): print('Filepath {0}/{1} does not exist!'.format(dirPath, scandate)) if skipBad: print('Skipping') continue else: print('Please cross-check, exiting!') exit(os.EX_DATAERR) pass filename = "{0}/{1}/{2}".format(dirPath, scandate, rootFileName) # Get TTree try: dataFile = r.TFile(filename, "READ") dataTree = dataFile.Get(treeName) knownBranches = dataTree.GetListOfBranches() except AttributeError as e: print('{0} may not exist in {1}'.format(treeName, filename)) print(e) if skipBad: print('Skipping') continue else: print('Please cross-check, exiting!') exit(os.EX_DATAERR) pass pass # Check to make sure listNames are present in dataTree for testBranch in listNames: if testBranch not in knownBranches: print("Branch {0} not in TTree {1} of file {2}".format( branchName, treeName, filename)) print("Existing Branches are:") for realBranch in knownBranches: print(realBranch) print("Please try again using one of the existing branches") exit(os.EX_DATAERR) # Get dependent variable value - VFAT Level arrayVFATData = rp.tree2array(dataTree, listNames) dataThisVFAT = arrayVFATData[arrayVFATData['vfatN'] == vfat] #VFAT Level # Close the TFile dataFile.Close() # Get the data for each strip and store it as a tuple in the list to be returned for chan in range(0, 128): dataThisChan = dataThisVFAT[dataThisVFAT[strChanName] == chan] #Channel Level depVarVal = np.asscalar(np.mean(dataThisChan[branchName])) listData.append((indepVarVal, chan, depVarVal)) pass # Return Data return listData
def arbitraryPlotter(anaType, listDataPtTuples, rootFileName, treeName, branchName, vfat, vfatCH=None, strip=None, ztrim=4, skipBad=False): """ Provides a list of tuples for 1D data where each element is of the form: ``(indepVarVal, depVarVal, depVarValErr)`` Args: anaType (string): type of analysis to perform, helps build the file path to the input file(s), from the keys of :any:`utils.anaInfo.ana_config` listDataPtTuples: list of tuples where each element is of the form ``(cName, scandate, indepVar)``, note ``indepVar`` is expected to be numeric rootFileName (string): name of the ``TFile`` that will be found in the data path corresponding to ``anaType`` treeName (string): name of the ``TTree`` inside ``rootFileName`` branchName (string): name of a branch inside ``treeName`` that the dependent variable will be extracted from vfat (int): vfat number that plots should be made for vfatCH (int): channel of the vfat that should be used, if ``None`` an average is performed w/stdev for error bar, mutually exclusive w/ ``strip`` strip (int): strip of the detector that should be used, if ``None`` an average is performed w/stdev for error bar, mutually exclusive w/ ``vfatCH`` skipBad (bool): if a file fails to open or the ``TTree`` cannot be found, the input is skipped and the processing continues rather than exiting """ from gempython.gemplotting.utils.anautilities import filePathExists, getDirByAnaType import numpy as np import os import root_numpy as rp # Make branches to load listNames = ["vfatN"] if vfatCH is not None and strip is None: listNames.append("vfatCH") elif strip is not None and vfatCH is None: listNames.append("ROBstr") listNames.append(branchName) # Load data listData = [] for dataPt in listDataPtTuples: # Get human readable info cName = dataPt[0] scandate = dataPt[1] indepVarVal = dataPt[2] # Setup Paths dirPath = getDirByAnaType(anaType.strip("Ana"), cName, ztrim) if not filePathExists(dirPath, scandate): print('Filepath {0}/{1} does not exist!'.format(dirPath, scandate)) if skipBad: print('Skipping') continue else: print('Please cross-check, exiting!') exit(os.EX_DATAERR) pass filename = "{0}/{1}/{2}".format(dirPath, scandate, rootFileName) # Get TTree try: dataFile = r.TFile(filename, "READ") dataTree = dataFile.Get(treeName) knownBranches = dataTree.GetListOfBranches() except AttributeError as e: print('{0} may not exist in {1}'.format(treeName, filename)) print(e) if skipBad: print('Skipping') continue else: print('Please cross-check, exiting!') exit(os.EX_DATAERR) pass pass # Check to make sure listNames are present in dataTree for testBranch in listNames: if testBranch not in knownBranches: print("Branch {0} not in TTree {1} of file {2}".format( branchName, treeName, filename)) print("Existing Branches are:") for realBranch in knownBranches: print(realBranch) print("Please try again using one of the existing branches") exit(os.EX_DATAERR) # Get dependent variable value arrayVFATData = rp.tree2array(dataTree, listNames) dataThisVFAT = arrayVFATData[arrayVFATData['vfatN'] == vfat] #VFAT Level # Close the TFile dataFile.Close() if vfatCH is not None and strip is None: dataThisVFAT = dataThisVFAT[dataThisVFAT['vfatCH'] == vfatCH] #VFAT Channel Level elif strip is not None and vfatCH is None: dataThisVFAT = dataThisVFAT[dataThisVFAT['ROBstr'] == strip] #Readout Board Strip Level depVarVal = np.asscalar( np.mean(dataThisVFAT[branchName]) ) #If a vfatCH/ROBstr obs & chan/strip not give will be avg over all, else a number depVarValErr = np.asscalar( np.std(dataThisVFAT[branchName]) ) #If a vfatCH/ROBstr obs & chan/strip not given will be stdev over all, or zero #Record this dataPt listData.append((indepVarVal, depVarVal, depVarValErr)) # Return Data return listData
def compareSBitThreshResults(args): # Suppress all pop-ups from ROOT import ROOT as r r.gROOT.SetBatch(True) # Check Paths import os, sys from gempython.utils.wrappers import envCheck envCheck('DATA_PATH') envCheck('ELOG_PATH') elogPath = os.getenv('ELOG_PATH') # Get info from input file from gempython.gemplotting.utils.anautilities import getCyclicColor, getDirByAnaType, filePathExists, getSummaryCanvas, parseArmDacCalFile, parseListOfScanDatesFile parsedTuple = parseListOfScanDatesFile(args.filename, alphaLabels=args.alphaLabels) listChamberAndScanDate = parsedTuple[0] chamberName = listChamberAndScanDate[0][0] thrDacName = parsedTuple[1] gemType = "ge11" from gempython.tools.hw_constants import vfatsPerGemVariant # Parse calibration file if present arrayCalInfo = None if args.calFileARM is not None: arrayCalInfo = parseArmDacCalFile( args.calFileARM, gemType=gemType) # arrayCalInfo[i][vfatN] for coef of x^(4-i) legPlot = r.TLegend(0.5, 0.5, 0.9, 0.9) from gempython.utils.nesteddict import nesteddict as ndict dict_Histos = ndict() dict_Graphs = ndict() dict_MultiGraphs = {} for idx, infoTuple in enumerate(listChamberAndScanDate): # Setup the path dirPath = getDirByAnaType("sbitRateor", infoTuple[0]) if not filePathExists(dirPath, infoTuple[1]): print('Filepath {:s}/{:s} does not exist!'.format( dirPath, infoTuple[1])) print('Please cross-check, exiting!') sys.exit(os.EX_NOINPUT) filename = "{:s}/{:s}/SBitRatePlots.root".format(dirPath, infoTuple[1]) # Load the file r.TH1.AddDirectory(False) scanFile = r.TFile(filename, "READ") if scanFile.IsZombie(): print("{0} is a zombie!!!".format(filename)) print( "Please double check your input list of scandates: {0}".format( args.filename)) print("And then call this script again") raise IOError ################### # Get individual distributions ################### for vfat in range(vfatsPerGemVariant[gemType]): dict_Histos[infoTuple[2]][vfat] = scanFile.Get( "VFAT{0}/THR_ARM_DAC/g1D_rate_vs_THR-ARM-DAC_vfat{0}".format( vfat)) dict_Graphs[infoTuple[2]][vfat] = r.TGraph( dict_Histos[infoTuple[2]][vfat]) # Do we convert x-axis to charge units? if arrayCalInfo is not None: for pt in range(dict_Graphs[infoTuple[2]][vfat].GetN()): valX = r.Double() valY = r.Double() dict_Graphs[infoTuple[2]][vfat].GetPoint(pt, valX, valY) valX_Q = 0 for coef in range(0, 5): valX_Q += arrayCalInfo[coef][vfat] * pow( valX, 4 - coef) dict_Graphs[infoTuple[2]][vfat].SetPoint(pt, valX_Q, valY) pass pass # Make the TMultiGraph Objects suffix = "VFAT{0}".format(vfat) if idx == 0: dict_MultiGraphs[vfat] = r.TMultiGraph( "mGraph_RateVsThrDac_vfat{0}".format(suffix), suffix) # Set Style of TGraph dict_Graphs[infoTuple[2]][vfat].SetLineColor(getCyclicColor(idx)) dict_Graphs[infoTuple[2]][vfat].SetMarkerColor(getCyclicColor(idx)) dict_Graphs[infoTuple[2]][vfat].SetMarkerStyle(20 + idx) dict_Graphs[infoTuple[2]][vfat].SetMarkerSize(0.8) # Add to the MultiGraph dict_MultiGraphs[vfat].Add(dict_Graphs[infoTuple[2]][vfat]) ################### # Make an entry in the TLegend ################### if vfat == 0: legPlot.AddEntry(dict_Graphs[infoTuple[2]][vfat], "{0} = {1}".format(thrDacName, infoTuple[2]), "LPE") pass pass ################### # Make output ROOT file ################### outFileName = "{0}/compSbitThresh_{1}.root".format(elogPath, chamberName) outFile = r.TFile(outFileName, "RECREATE") # Plot Containers dict_canv = {} ################### # Now Make plots ################### for vfat in range(vfatsPerGemVariant[gemType]): # Make Output Canvas dict_canv[vfat] = r.TCanvas("canvSBitRate_VFAT{0}".format(vfat), "SBIT Rate by THR DAC", 700, 700) dict_canv[vfat].cd() dict_canv[vfat].cd().SetLogy() dict_MultiGraphs[vfat].Draw("APE1") dict_MultiGraphs[vfat].GetYaxis().SetTitle("Rate #left(Hz#right)") dict_MultiGraphs[vfat].GetYaxis().SetRangeUser(1e-1, 5e8) # max is 40 MHz if arrayCalInfo is not None: dict_MultiGraphs[vfat].GetXaxis().SetTitle( "Threshold #left(fC#right)") dict_MultiGraphs[vfat].GetXaxis().SetRangeUser(0, 20) else: dict_MultiGraphs[vfat].GetXaxis().SetTitle( "CFG_THR_ARM_DAC #left(DAC#right)") dict_MultiGraphs[vfat].GetXaxis().SetRangeUser(0, 125) pass dict_MultiGraphs[vfat].Draw("APE1") # Draw Legend? if not args.noLeg: legPlot.Draw("same") pass # Make output image? if args.savePlots: dict_canv[vfat].SaveAs("{0}/{1}.png".format( elogPath, dict_canv[vfat].GetName())) pass # Store Output thisDirectory = outFile.mkdir("VFAT{0}".format(vfat)) thisDirectory.cd() dict_MultiGraphs[vfat].Write() dict_canv[vfat].Write() pass # Make summary canvases, always save these canvSBitRate_Summary = getSummaryCanvas( dict_MultiGraphs.values()[0:vfatsPerGemVariant[gemType]], name="canvSBitRate_Summary", drawOpt="APE1", gemType=gemType) for vfatCanv in range(1, vfatsPerGemVariant[gemType] + 1): canvSBitRate_Summary.cd(vfatCanv).SetLogy() # Draw Legend? if not args.noLeg: canvSBitRate_Summary.cd(1) legPlot.Draw("same") pass # Save summary canvases (always) print("\nSaving Summary TCanvas Objects") canvSBitRate_Summary.SaveAs("{0}/{1}.png".format( elogPath, canvSBitRate_Summary.GetName())) # Close output files outFile.Close() print("You can find all ROOT objects in:") print("\n\t{0}/compSbitThresh_{1}.root\n".format(elogPath, chamberName)) print("You can find all plots in:") print("\n\t{0}\n".format(elogPath)) print("Done") return
dateOptions.add_option("--endDate", type="string", dest="endDate", default=None, help="Starting date range in YYYY.MM.DD format", metavar="endDate") parser.add_option_group(dateOptions) (options, args) = parser.parse_args() from gempython.gemplotting.mapping.chamberInfo import chamber_config from gempython.gemplotting.utils.anautilities import getDataPath, getElogPath dataPath = getDataPath() elog_path = getElogPath() vt1bump = 'vt1bump' + str(options.vt1bump) anaType = options.anaType print "Options: vt1bump=%s, dataPath=%s, anaType=%s" % (vt1bump, dataPath, anaType) for chamber in chamber_config.values(): makeListOfScanDatesFile(chamber, anaType, options.startDate, options.endDate) if not options.listOfScanDatesOnly: makePlots(chamber, anaType, vt1bump, elog_path) call_command = 'rm ' + getDirByAnaType( anaType, chamber) + 'listOfScanDates.txt' os.system(call_command) # remove the file lists pass pass
strRootName = "" for idx in range(len(listParsedExpress) - 1, -1, -1): strRootName = "%s_%s" % (strRootName, getStringNoSpecials(listParsedExpress[idx])) if idx > 0: strRootName = "%s_vs" % (strRootName) strRootName = elogPath + "/gemTreeDrawWrapper%s.root" % (strRootName) r.gROOT.SetBatch(True) outF = r.TFile(strRootName, options.rootOpt) # Loop Over inputs listPlots = [] for chamberAndScanDatePair in listChamberAndScanDate: # Setup the path dirPath = getDirByAnaType(options.anaType.strip("Ana"), chamberAndScanDatePair[0], options.ztrim) if not filePathExists(dirPath, chamberAndScanDatePair[1]): print 'Filepath %s/%s does not exist!' % (dirPath, scandate) print 'Please cross-check, exiting!' outF.Close() exit(os.EX_DATAERR) filename = "%s/%s/%s" % (dirPath, chamberAndScanDatePair[1], tree_names[options.anaType][0]) treeName = tree_names[options.anaType][1] # Get the Plot and Reset it's Name thisPlot = getPlotFromTree(filename, treeName, options.treeExpress, options.treeSel) strPlotName = thisPlot.GetName() strPlotName = "%s_%s_%s" % (strPlotName, chamberAndScanDatePair[0], chamberAndScanDatePair[1])
def arbitraryPlotter(anaType, listDataPtTuples, rootFileName, treeName, branchName, vfat, vfatCH=None, strip=None, ztrim=4, skipBad=False): """ Provides a list of tuples for 1D data where each element is of the form: (indepVarVal, depVarVal, depVarValErr) anaType - type of analysis to perform, helps build the file path to the input file(s), from set ana_config.keys() listDataPtTuples - list of tuples where each element is of the form: (cName, scandate, indepVar), note indepVar is expected to be numeric rootFileName - name of the TFile that will be found in the data path corresponding to anaType treeName - name of the TTree inside rootFileName branchName - name of a branch inside treeName that the dependent variable will be extracted from vfat - vfat number that plots should be made for vfatCH - channel of the vfat that should be used, if None an average is performed w/stdev for error bar, mutually exclusive w/strip strip - strip of the detector that should be used, if None an average is performed w/stdev for error bar, mutually exclusive w/vfatCH skipBad - if a file fails to open or the TTree cannot be found, the input is skipped and the processing continues rather than exiting """ from gempython.gemplotting.utils.anautilities import filePathExists, getDirByAnaType import numpy as np import os import root_numpy as rp # Make branches to load listNames = ["vfatN"] if vfatCH is not None and strip is None: listNames.append("vfatCH") elif strip is not None and vfatCH is None: listNames.append("ROBstr") listNames.append(branchName) # Load data listData = [] for dataPt in listDataPtTuples: # Get human readable info cName = dataPt[0] scandate = dataPt[1] indepVarVal = dataPt[2] # Setup Paths dirPath = getDirByAnaType(anaType.strip("Ana"), cName, ztrim) if not filePathExists(dirPath, scandate): print 'Filepath %s/%s does not exist!' % (dirPath, scandate) if skipBad: print 'Skipping' continue else: print 'Please cross-check, exiting!' exit(os.EX_DATAERR) pass filename = "%s/%s/%s" % (dirPath, scandate, rootFileName) # Get TTree try: dataFile = r.TFile(filename, "READ") dataTree = dataFile.Get(treeName) knownBranches = dataTree.GetListOfBranches() except Exception as e: print '%s may not exist in %s' % (treeName, filename) print e if skipBad: print 'Skipping' continue else: print 'Please cross-check, exiting!' exit(os.EX_DATAERR) pass pass # Check to make sure listNames are present in dataTree for testBranch in listNames: if testBranch not in knownBranches: print "Branch %s not in TTree %s of file %s" % ( branchName, treeName, filename) print "Existing Branches are:" for realBranch in knownBranches: print realBranch print "Please try again using one of the existing branches" exit(os.EX_DATAERR) # Get dependent variable value arrayVFATData = rp.tree2array(dataTree, listNames) dataThisVFAT = arrayVFATData[arrayVFATData['vfatN'] == vfat] #VFAT Level # Close the TFile dataFile.Close() if vfatCH is not None and strip is None: dataThisVFAT = dataThisVFAT[dataThisVFAT['vfatCH'] == vfatCH] #VFAT Channel Level elif strip is not None and vfatCH is None: dataThisVFAT = dataThisVFAT[dataThisVFAT['ROBstr'] == strip] #Readout Board Strip Level depVarVal = np.asscalar( np.mean(dataThisVFAT[branchName]) ) #If a vfatCH/ROBstr obs & chan/strip not give will be avg over all, else a number depVarValErr = np.asscalar( np.std(dataThisVFAT[branchName]) ) #If a vfatCH/ROBstr obs & chan/strip not given will be stdev over all, or zero #Record this dataPt listData.append((indepVarVal, depVarVal, depVarValErr)) # Return Data return listData
def arbitraryPlotter2D(anaType, listDataPtTuples, rootFileName, treeName, branchName, vfat, ROBstr=True, ztrim=4, skipBad=False): """ Provides a list of tuples for 2D data where each element is of the (x,y,z) form: (indepVarVal, vfatCHOrROBstr, depVarVal) anaType - type of analysis to perform, helps build the file path to the input file(s), from set ana_config.keys() listDataPtTuples - list of tuples where each element is of the form: (cName, scandate, indepVar), note indepVar is expected to be numeric rootFileName - name of the TFile that will be found in the data path corresponding to anaType treeName - name of the TTree inside rootFileName branchName - name of a branch inside treeName that the dependent variable will be extracted from vfat - vfat number that plots should be made for skipBad - if a file fails to open or the TTree cannot be found, the input is skipped and the processing continues rather than exiting """ from gempython.gemplotting.utils.anautilities import filePathExists, getDirByAnaType import numpy as np import os import root_numpy as rp # Make branches to load listNames = ["vfatN"] strChanName = "" #Name of channel TBranch, either 'ROBstr' or 'vfatCH' if ROBstr: listNames.append("ROBstr") strChanName = "ROBstr" else: listNames.append("vfatCH") strChanName = "vfatCH" listNames.append(branchName) # Load data listData = [] for dataPt in listDataPtTuples: # Get human readable info cName = dataPt[0] scandate = dataPt[1] indepVarVal = dataPt[2] # Setup Paths dirPath = getDirByAnaType(anaType.strip("Ana"), cName, ztrim) if not filePathExists(dirPath, scandate): print 'Filepath %s/%s does not exist!' % (dirPath, scandate) if skipBad: print 'Skipping' continue else: print 'Please cross-check, exiting!' exit(os.EX_DATAERR) pass filename = "%s/%s/%s" % (dirPath, scandate, rootFileName) # Get TTree try: dataFile = r.TFile(filename, "READ") dataTree = dataFile.Get(treeName) knownBranches = dataTree.GetListOfBranches() except Exception as e: print '%s may not exist in %s' % (treeName, filename) print e if skipBad: print 'Skipping' continue else: print 'Please cross-check, exiting!' exit(os.EX_DATAERR) pass pass # Check to make sure listNames are present in dataTree for testBranch in listNames: if testBranch not in knownBranches: print "Branch %s not in TTree %s of file %s" % ( branchName, treeName, filename) print "Existing Branches are:" for realBranch in knownBranches: print realBranch print "Please try again using one of the existing branches" exit(os.EX_DATAERR) # Get dependent variable value - VFAT Level arrayVFATData = rp.tree2array(dataTree, listNames) dataThisVFAT = arrayVFATData[arrayVFATData['vfatN'] == vfat] #VFAT Level # Close the TFile dataFile.Close() # Get the data for each strip and store it as a tuple in the list to be returned for chan in range(0, 128): dataThisChan = dataThisVFAT[dataThisVFAT[strChanName] == chan] #Channel Level depVarVal = np.asscalar(np.mean(dataThisChan[branchName])) listData.append((indepVarVal, chan, depVarVal)) pass # Return Data return listData
def launchAnaArgs(anaType, cName, cType, scandate, scandatetrim=None, ztrim=4.0, chConfigKnown=False, channels=False, panasonic=False, latFit=False, latSigRange=None, latSigMaskRange=None): import os import subprocess from subprocess import CalledProcessError from gempython.gemplotting.utils.anaInfo import ana_config from gempython.gemplotting.utils.anautilities import getDirByAnaType from gempython.utils.wrappers import runCommand #dataPath = os.getenv('DATA_PATH') dirPath = getDirByAnaType(anaType, cName, ztrim) elogPath = "%s/%s"%(os.getenv('ELOG_PATH'),scandate) print "Analysis Requested: %s"%(anaType) #Build Commands cmd = [ana_config[anaType]] postCmds = [] postCmds.append(["mkdir","-p","%s"%(elogPath)]) if anaType == "latency": dirPath = "%s/%s/"%(dirPath,scandate) filename = dirPath + "LatencyScanData.root" if not os.path.isfile(filename): print "No file to analyze. %s does not exist"%(filename) return os.EX_NOINPUT cmd.append("--infilename=%s"%(filename)) cmd.append("--outfilename=%s"%("latencyAna.root")) if latFit: cmd.append("--fit") cmd.append("--latSigMaskRange=%s"%(latSigMaskRange)) cmd.append("--latSigRange=%s"%(latSigRange)) postCmds.append(["cp","%s/LatencyScanData/Summary.png"%(dirPath), "%s/LatencySumary_%s.png"%(elogPath,cName)]) postCmds.append(["cp","%s/LatencyScanData/MaxHitsPerLatByVFAT.png"%(dirPath), "%s/MaxHitsPerLatByVFAT_%s.png"%(elogPath,cName)]) if latFit: postCmds.append(["cp","%s/LatencyScanData/SignalOverBkg.png"%(dirPath), "%s/SignalOverBkg_%s.png"%(elogPath,cName)]) postCmds.append(["cp","%s/LatencyScanData/SignalNoBkg.png"%(dirPath), "%s/SignalNoBkg_%s.png"%(elogPath,cName)]) pass elif anaType == "scurve": dirPath = "%s/%s/"%(dirPath,scandate) filename = dirPath + "SCurveData.root" if not os.path.isfile(filename): print "No file to analyze. %s does not exist"%(filename) return os.EX_NOINPUT cmd.append("--infilename=%s"%(filename)) cmd.append("--outfilename=%s"%("SCurveFitData.root")) cmd.append("--fit") cmd.append("--type=%s"%(cType)) if channels: cmd.append("--channels") pass if panasonic: cmd.append("--panasonic") pass postCmds.append(["cp","%s/SCurveData/Summary.png"%(dirPath), "%s/SCurveSummary_%s_ztrim%2.2f.png"%(elogPath,cName,ztrim)]) postCmds.append(["cp","%s/SCurveData/chConfig.txt"%(dirPath), "%s/chConfig_%s_ztrim%2.2f.txt"%(elogPath,cName,ztrim)]) pass elif "threshold" in anaType: dirPath = "%s/%s/"%(dirPath,scandate) filename = dirPath + "ThresholdScanData.root" if not os.path.isfile(filename): print "No threshold file to analyze. %s does not exist"%(filename) return os.EX_NOINPUT cmd.append("--infilename=%s"%(filename)) cmd.append("--outfilename=%s"%("ThresholdPlots.root")) if "thresholdvf" in anaType: cmd.append("--pervfat") if chConfigKnown: cmd.append("--chConfigKnown") # dirPath_Trim = "%s/%s/trim/z%f/%s/SCurveData_Trimmed/"%(dataPath,cName,ztrim,scandatetrim) dirPath_Trim = "%s/%s/SCurveData_Trimmed/"%(getDirByAnaType("trim", cName, ztrim),scandatetrim) filename_Trim = dirPath_Trim + "SCurveFitData.root" if not os.path.isfile(filename_Trim): print "No scurve fit data file to analyze. %s does not exist"%(filename_Trim) return os.EX_NOINPUT cmd.append("--fileScurveFitTree=%s"%(filename_Trim)) pass postCmds.append(["cp","%s/ThresholdScanData/ThreshSummary.png"%(dirPath), "%s/ThreshSummary_%s.png"%(elogPath,cName)]) postCmds.append(["cp","%s/ThresholdScanData/ThreshPrunedSummary.png"%(dirPath), "%s/ThreshPrunedSummary_%s.png"%(elogPath,cName)]) postCmds.append(["cp","%s/ThresholdScanData/vfatConfig.txt"%(dirPath), "%s/vfatConfig_%s.txt"%(elogPath,cName)]) if chConfigKnown: postCmds.append(["cp","%s/ThresholdScanData/chConfig_MasksUpdated.txt"%(dirPath), "%s/chConfig_MasksUpdated_%s.txt"%(elogPath,cName)]) pass pass elif anaType == "trim": dirPath = "%s/%s/"%(dirPath,scandate) filename = dirPath + "SCurveData_Trimmed.root" if not os.path.isfile(filename): print "No file to analyze. %s does not exist"%(filename) return os.EX_NOINPUT cmd.append("--infilename=%s"%(filename)) cmd.append("--outfilename=%s"%("SCurveFitData.root")) cmd.append("--fit") cmd.append("--type=%s"%(cType)) if channels: cmd.append("--channels") pass if panasonic: cmd.append("--panasonic") pass postCmds.append(["cp","%s/SCurveData_Trimmed/Summary.png"%(dirPath), "%s/SCurveSummaryTrimmed_%s_ztrim%2.2f.png"%(elogPath,cName,ztrim)]) postCmds.append(["cp","%s/SCurveData_Trimmed/chConfig.txt"%(dirPath), "%s/chConfigTrimmed_%s_ztrim%2.2f.txt"%(elogPath,cName,ztrim)]) pass #Execute Commands try: log = file("%s/anaLog.log"%(dirPath),"w") returncode = runCommand(cmd,log) if returncode != 0: print "Error: command exited with non-zero code %d" % returncode return returncode for item in postCmds: returncode = runCommand(item) if returncode != 0: print "Error: command exited with non-zero code %d" % returncode return returncode pass except CalledProcessError as e: print "Caught exception",e return -1 pass return 0
def compareLatResults(args): # Suppress all pop-ups from ROOT import ROOT as r r.gROOT.SetBatch(True) # Check Paths import os, sys from gempython.utils.wrappers import envCheck envCheck('DATA_PATH') envCheck('ELOG_PATH') elogPath = os.getenv('ELOG_PATH') # Get info from input file from gempython.gemplotting.utils.anautilities import getCyclicColor, getDirByAnaType, filePathExists, make3x8Canvas, parseListOfScanDatesFile parsedTuple = parseListOfScanDatesFile(args.filename,alphaLabels=args.alphaLabels) listChamberAndScanDate = parsedTuple[0] chamberName = listChamberAndScanDate[0][0] legPlot = r.TLegend(0.5,0.5,0.9,0.9) from gempython.utils.nesteddict import nesteddict as ndict dict_Histos = ndict() dict_Graphs = ndict() dict_MultiGraphs = {} print("Loading data from input list of scandates file") from gempython.gemplotting.utils.anaInfo import tree_names for idx,infoTuple in enumerate(listChamberAndScanDate): # Setup the path dirPath = getDirByAnaType("latency", infoTuple[0]) if not filePathExists(dirPath, infoTuple[1]): print('Filepath {:s}/{:s} does not exist!'.format(dirPath, infoTuple[1])) print('Please cross-check, exiting!') sys.exit(os.EX_NOINPUT) filename = "{:s}/{:s}/{:s}".format(dirPath, infoTuple[1], tree_names["latencyAna"][0]) if not os.path.isfile(filename): print('File {:s} does not exist!'.format(filename)) print('Please cross-check, exiting!') sys.exit(os.EX_NOINPUT) # Load the file r.TH1.AddDirectory(False) scanFile = r.TFile(filename,"READ") if scanFile.IsZombie(): print("{:s} is a zombie!!!".format(filename)) print("Please double check your input list of scandates: {:s}".format(args.filename)) print("And then call this script again") raise IOError ################### # Get individual distributions ################### for vfat in range(24): baseDir = "VFAT_Plots/VFAT{0:d}".format(vfat) dict_Histos[infoTuple[2]][vfat] = scanFile.Get("{:s}/vfat{:d}HitsVsLat".format(baseDir,vfat)) dict_Graphs[infoTuple[2]][vfat] = scanFile.Get("{:s}/g_N_vs_Lat_VFAT{:d}".format(baseDir,vfat)) # Make the TMultiGraph Objects suffix = "VFAT{:d}".format(vfat) if idx == 0: dict_MultiGraphs[vfat] = r.TMultiGraph("mGraph_RateVsThrDac_vfat{:s}".format(suffix),suffix) # Set Style of TGraph dict_Graphs[infoTuple[2]][vfat].SetLineColor(getCyclicColor(idx)) dict_Graphs[infoTuple[2]][vfat].SetMarkerColor(getCyclicColor(idx)) dict_Graphs[infoTuple[2]][vfat].SetMarkerStyle(20+idx) dict_Graphs[infoTuple[2]][vfat].SetMarkerSize(0.8) # Add to the MultiGraph dict_MultiGraphs[vfat].Add(dict_Graphs[infoTuple[2]][vfat]) ################### # Make an entry in the TLegend ################### if vfat == 0: print("infoTuple[2] = ", infoTuple[2]) legPlot.AddEntry( dict_Graphs[infoTuple[2]][vfat], "{0}".format(infoTuple[2]), "LPE") pass pass ################### # Make output ROOT file ################### outFileName = "{0}/compLatResults_{1}.root".format(elogPath,chamberName) outFile = r.TFile(outFileName,"RECREATE") ################### # Now Make plots ################### print("Making output plots") dict_canv = {} for vfat in range(24): # Make Output Canvas dict_canv[vfat] = r.TCanvas("canvLatResults_VFAT{0}".format(vfat),"Hits vs. CFG_LATENCY Value",700,700) dict_canv[vfat].cd() dict_MultiGraphs[vfat].Draw("APE1") dict_MultiGraphs[vfat].GetYaxis().SetTitle("Hits #left(A.U.#right)") dict_MultiGraphs[vfat].GetYaxis().SetRangeUser(0,args.eventsPerLat) # max is 40 MHz dict_MultiGraphs[vfat].GetXaxis().SetTitle("CFG_LATENCY #left(BX#right)") dict_MultiGraphs[vfat].Draw("APE1") # Draw Legend? if not args.noLeg: legPlot.Draw("same") pass # Make output image? if args.savePlots: dict_canv[vfat].SaveAs("{0}/{1}.png".format(elogPath,dict_canv[vfat].GetName())) pass # Store Output thisDirectory = outFile.mkdir("VFAT{0}".format(vfat)) thisDirectory.cd() dict_MultiGraphs[vfat].Write() dict_canv[vfat].Write() pass # Make summary canvases, always save these canvLatResults_Summary = make3x8Canvas("canvLatResults_Summary",dict_MultiGraphs.values()[0:24],"APE1") # Draw Legend? if not args.noLeg: canvLatResults_Summary.cd(1) legPlot.Draw("same") pass # Save summary canvases (always) print("\nSaving Summary TCanvas Objects") #canvLatResults_Summary.SaveAs("{0}/{1}.png".format(elogPath,canvLatResults_Summary.GetName())) # Close output files outFile.Close() print("You can find all ROOT objects in:") print("\n\t{0}//compLatResults_{1}.root\n".format(elogPath,chamberName)) print("You can find all plots in:") print("\n\t{0}\n".format(elogPath)) print("Done") return