def getSF(self): """Retrieve event weight""" sf = 1. if self.ttree.mcChannelNumber>0 and self.p_scalefactor: ew = vlq.compute_weight(self.ttree,self.p_btag_wkpt) sf *= ew return sf
def addData(DataMC, r2j_tree, varlist, cfgParser): """ Function that opens a ROOT file, and saves relevant data from the root file in the DataMC_Type objects. @param DataMC object that contains the variable information (values, weights) @param r2j_tree ROOT TTree @param varlist list of variables to save @param cfgParser ConfigParser object of settings """ logging.getLogger('share/datamc.log') loggingLEVEL = logging.getLogger().getEffectiveLevel() # DEBUG, INFO, ERROR, etc. logging.info("") logging.info(" -- In addData.py") logging.info(" ------------ ") logging.info(" Initializing the config file.") plot_keys = datamc_dicts.text_dicts() GeV = 1000. logging.info(" Initializing root file and tree ") r2j_file = r2j_tree.GetDirectory().GetName().split('/')[-1] # may need this later ## -- Configuration -- ## p_lep_name = cfgParser.get('datamc','lepton') # ex. muel (both muon & electron) p_nEvents = int(cfgParser.get('datamc','nEvents')) # ex. -1 p_event_selector = cfgParser.get('datamc','eventsel') # ex. custom file for applying extra event selection p_custom_vars = cfgParser.get('datamc','custom_vars') # ex. custom file for making variables not in the tree p_btag_wkpt = cfgParser.get('datamc','btag_wkpt') # ex. 77 (for 77%) ## ------------------- ## ## -- Load the custom selection script if p_event_selector: import pyMiniSL.selectionBase as pyEventSel evtSel = pyEventSel.SelectionBase(cfgParser,_cutsfile=p_event_selector) evtSel.initialize('miniSL') # setup some of the configurations evtSel.execute(r2j_tree,r2j_file) ## -- Sort out the loop over entries total_entries = r2j_tree.GetEntries() if p_nEvents < 0 or p_nEvents > total_entries: nEntries = total_entries else: nEntries = p_nEvents ## -- Actually doing the event loop here logging.info(" Begin the event loop ") logging.info(" Specified entries: {0}; setting: {1}".format(p_nEvents,nEntries)) ## -- Event Loop entry = 0 while entry < nEntries: if not entry%1000 and not p_event_selector: # only print if we aren't going to print the entry number elsewhere print " >> Entry {0}".format(entry) r2j_tree.GetEntry(entry) ## -- custom selection the user may add (e.g., b-tagging) passed_selection = True if p_event_selector: # only call this function if needed passed_selection = evtSel.event_loop(entry)['result'] if not passed_selection: logging.info(" Entry: {0}; ".format(entry)) logging.info(" Failed custom event selection.") entry+=1 continue ## Check the lepton! (syntax to avoid if/else) lepton_name = ['mu','el'][(r2j_tree.ejets)] logging.info(" Lepton type: {0}".format(lepton_name)) if p_lep_name!='muel' and p_lep_name!=lepton_name: logging.info(" Wrong lepton flavor: want = {0}; got = {1}".format(p_lep_name,lepton_name)) entry+=1 continue ## Custom variables that aren't branches logging.info(" Loading custom variables.") custom_vars = custom_variables(r2j_tree,p_custom_vars,None) ## Loop over the desired variables logging.info(" Looping through variable list ") for var in varlist: try: val = custom_vars[var] except KeyError: val = getattr(r2j_tree,var) ## if not choosing (sub-)leading, then we can plot the attributes of ## each object in the event (e.g., all jet pT) if "vector" in str(type(val)): val = list(val) ## If there are no values in the event, then there's no need to save the SF if type(val)==list and not val: logging.debug(" Variable {0} is a list and it's empty -- do not continue (issues with SF for no objects)!".format(var)) logging.debug(" > This probably happened because there aren't any fatjets in this event") continue ## Get the event weight for scaling MC scaleFactor = 1. if not 'data' in DataMC.name.lower(): event_weight = vlq.compute_weight(r2j_tree,p_btag_wkpt) scaleFactor *= event_weight ## Scale by GeV for variables with that unit if plot_keys['variables'][var]['gev']: val_type = str(type(val)) if "vector" in val_type or "list" in val_type: val = [i/GeV for i in val] else: val = val/GeV ## saving the data to plot later logging.info(" Exporting data to lists") logging.info(" Value = {0}".format(val)) logging.info(" Weight = {0}".format(scaleFactor)) DataMC.varVals[var].append(val) DataMC.scaleFactors[var].append(scaleFactor) DataMC.lepNames[var].append(lepton_name) entry+=1 logging.info(" End addData.py") return DataMC