def __init__(self,m_cfg_parser): """Make the Data/MC plots.""" self.m_cfg_parser = m_cfg_parser ## -- INIT VARIABLES THAT CAN BE USED IN INHERITED CLASSES -- ## ## ++ These don't require the config file ## -- Details for making plots self.notforpre = info.noPrePlot() # variables that can't be plotted in pre-selection self.GeV = 1000. ## -- Initialize uncertainties self.unc = {'stat':{'up':[],'dn':[]},\ 'syst':{'up':[],'dn':[]},\ 'total':{'up':[],'dn':[]}} # recording uncertainties self.resid_unc = {'stat':{'up':[],'dn':[]},\ 'syst':{'up':[],'dn':[]},\ 'total':{'up':[],'dn':[]}} # recording residual uncertainties ## -- Plotting details self.timeStamp = strftime("%d%b%Y",localtime()) self.draw_ax1_unc = True # draw uncertainty on the prediction in the main plot self.label_size = 20 self.atlas_size = self.label_size+2 self.leg_txtsize = self.label_size-4 ## -- Sample details self.physics_samples = info.physicsSamples() self.background = self.physics_samples['backgrounds'] self.plot_keys = datamc_dicts.text_dicts()
def merge_json_files(self): """Merge json files that are created individually.""" print "\n -- Merging JSON files -- \n" names_dict = info.physicsSamples() names = names_dict['signal']+names_dict['backgrounds']+['data'] savePath = "{0}/{1}".format(info.getJsonPath(),self.p_lepton) jsonfiles = open("share/jsonfiles2plot.txt","r").readlines() # jsonfiles is the saved list of all json files made for var in self.p_varlist_nolead: print " Preparing JSON file for variable ",var pathData = info.getJsonPath()+"{0}/{1}_{2}.json".format(self.p_lepton,var,self.p_outfile) merged_data = {} for name in jsonfiles: logging.info(" Merging {0}".format(name)) json_filename = name%(var) json_filename = json_filename.rstrip('\n') if not os.path.isfile(json_filename): logging.info(" File {0} does not exist. ".format(json_filename)) continue temp_data = json.load(open(json_filename)) merged_data.update(temp_data) with open(pathData,'w') as outputfile: json.dump(merged_data, outputfile) return
def ROOT2json(cfg_parser): """ For converting data in the ROOT files to .json files for plotting. Increases ease-of-use for making Data/MC plots (don't have to re-process the ROOT file just to make a single plot). @param cfg_parser Object which parsed the configuration file """ logging.getLogger('share/datamc.log') loggingLEVEL = logging.getLogger().getEffectiveLevel() # DEBUG, INFO, ERROR, etc. logging.info("") logging.critical(" -- In root2json.py") logging.info(" ------------ ") logging.info(" Initializing the config file.") ## -- Configuration -- ## p_varList = cfg_parser.get('datamc','vars') # ex. 'share/varNames.txt' p_inputfiles = cfg_parser.get('datamc','rootfiles') # ex. 'share/datamc_ntuples.txt' p_lepton = cfg_parser.get('datamc','lepton') # ex. muel (both muon & electron) p_outfile = cfg_parser.get('datamc','jsonfilename') # ex. 'elLHMedium_pre_A1_1tagin' p_nEvents = int(cfg_parser.get('datamc','nEvents')) # ex. -1 ## ------------------- ## treename = info.treename() bckg_names = info.physicsSamples()['backgrounds'] savePath = "{0}/{1}".format(info.getJsonPath(),p_lepton) if not os.path.isdir(savePath): os.makedirs(savePath) logging.info(" Set the output path: {0}".format(savePath)) ## -- Load various files of data inputfiles = info.read_txt_file(p_inputfiles) if not inputfiles: print print " ERROR: File {0} is empty (no files!) ".format(p_inputfiles) print from sys import exit exit(1) i_varList = info.read_txt_file(p_varList) varList = [] for p_var in i_varList: p_var = p_var.split('[')[0] if p_var not in varList: varList.append(p_var) ## -- Loop over input files logging.info(" inputfiles = {0}".format(inputfiles)) logged_files = {} # keeping track if a sample has been used before # the list of input files may have multiple root files # for the same sample (ttbar, wjets, etc.) ## -- Make a simple text file that stores all of the json files we just made newfile = open("share/jsonfiles2plot.txt","w") for p in inputfiles: jsonData = config.AutoVivification() p_file = ROOT.TFile.Open(p) p_tree = p_file.Get(treename) p_tree.GetEntry(0) # just to get the mcChannelNumber name = config.getSampleName(root_tree=p_tree,dsid=p_tree.mcChannelNumber)['name'] # need different names from each file (otherwise different ttbar files # will overwrite each other!) ## -- load the new DataMC object if name not in logged_files.keys(): entry = DataMC_Type(name) logged_files[name] = entry for var in varList: entry.varVals[var] = [] entry.scaleFactors[var] = [] entry.lepCharges[var] = [] entry.lepNames[var] = [] else: entry = logged_files[name] print "\n ++ Producing json file from {0}\n".format(p) logging.info(" ++ Running {0}".format(name)) ## -- Attach the data (values,weights) to each DataMC object entry = addData.addData(entry, p_tree, varList, cfg_parser) # Get data from ROOT logging.info(" Exporting data to json format.") ## -- Log the DataMC object in the dictionary ## not sure that this is being used effectively... logged_files[name] = entry ## Save each json file now that we have looped over the file logging.info(" Saving json information.") outfile_name = '{0}/%s_{1}_{2}.json'.format(savePath,p_outfile,name) newfile.write("%s\n" % outfile_name) for var in varList: # put information in dictionaries to be saved to json jsonData[var][entry.name] = entry.varVals[var] jsonData[var][entry.name+'_weight'] = entry.scaleFactors[var] jsonData[var][entry.name+'_lepNames'] = entry.lepNames[var] jsonData[var]['LUMI'] = info.LUMI() print " Saving output to {0}".format(outfile_name%(var)) logging.info(" -- Saving output to {0}".format(outfile_name)) with open(outfile_name%(var),'w') as outputfile: json.dump(jsonData[var], outputfile) logging.info(" End root2json.py") return