def getEff(self,emu,event_weight): """ Get efficiency and save to histogram """ custom_vars = addData.custom_variables(self.ttree,self.p_custom_vars,self.event_objects) eff_conditions = self.p_settings.get(self.p_cfg_name,'eff_y').split(',') # things like ==1 logic_operator = [] good_condition = [] for ec,eff_c in enumerate(eff_conditions): value = re.search(r'\d+',eff_c).group() # '==1' -> 1 cond = eff_c.split(value)[0] # '==1' -> '==' if eff_c.endswith('.'): good_condition.append( float(value) ) else: good_condition.append( int(value) ) logic_operator.append( cond ) # self.p_eff_x_vars # loop variables that we are getting the efficiency as a function of! # self.p_variables # loop variables that we are getting the efficiency of! for eff_x_var in self.p_eff_x_vars: for vv,variable in enumerate(self.p_variables): eff_c = value = re.search(r'\d+',self.eff_conditions[vv]).group() # '==1' -> 1 if eff_x_var in custom_vars.keys(): eff_x_val = custom_vars[eff_x_var] # getattr( reco,quantity )() else: eff_x_val = None # set it somewhere else; user needs to do this! (too modular...) # something like the object pT (below) or another object's pT (define here) _values = {'total':[],'good':[],'mis':[]} reco_name,attr = variable.split('_') quantity = self.key2attr[attr.lower()] truth_name = self.truthObj[reco_name]['name'] dr_match = self.truthObj[reco_name]['dr'] # deltaR match value reco_object = self.event_objects[reco_name] truth_object = self.event_objects[truth_name] # -- DeltaR match and fill hists # loop over objects (if more than 1) if type(reco_object)==list: for reco in reco_object: # fill total (matched) if reco.DeltaR(truth_object) < dr_match: result = self.getValue(reco,quantity) self.data[variable+eff_c][emu]['total'].Fill(eff_x_val,event_weight) # fill good (matched & tagged) if self.ops[logic_operator[vv]](result,good_condition[vv]): self.data[variable+eff_c][emu]['good'].Fill(eff_x_val,event_weight) # fill mis (matched & mis-tagged) else: self.data[variable+eff_c][emu]['mis'].Fill(eff_x_val,event_weight) else: if reco_object.DeltaR(truth_object) < dr_match: # fill total (matched) result = self.getValue(reco,quantity) self.data[variable+eff_c][emu]['total'].Fill(eff_x_val,event_weight) # fill good (matched & tagged) if self.ops[logic_operator[vv]](result,good_condition[vv]): self.data[variable+eff_c][emu]['good'].Fill(eff_x_val,event_weight) # fill mis (matched & mis-tagged) else: self.data[variable+eff_c][emu]['mis'].Fill(eff_x_val,event_weight) return
def EventLoop(self): """Loop over events.""" total_entries = self.ttree.GetEntries() if self.p_nevents < 0 or self.p_nevents > total_entries: nEntries = total_entries else: nEntries = self.p_nevents entry = 0 print print print " RUNNING OVER ",self.f.GetName(),": ",nEntries," entries" print while entry < nEntries: self.ttree.GetEntry(entry) if not entry%1000: print " -> Entry ",entry ## -- Get some event info if self.ttree.ejets: lep = 'el' else: lep = 'mu' if self.p_lepton!='muel' and lep!=self.p_lepton: entry+=1 continue self.BuildObjects() ## -- Apply cuts (if necessary) failed_event = False if self.p_cuts: failed_event = self.ApplyCuts() if failed_event: entry+=1 continue scaleFactor = self.getSF() if self.p_plot1d == 'efficiency': self.getEff(lep,scaleFactor) ## -- Loop over variables that we want else: custom_vars = addData.custom_variables(self.ttree,self.p_custom_vars,self.event_objects) for variable in self.p_variables: try: values = custom_vars[variable] except KeyError: if '.' in variable: name,attr = variable.split('.') quantity = self.key2attr[attr.lower()] if type(self.event_objects[name])==list: values = [] for py_object in self.event_objects[name]: values.append( self.getValue(py_object,quantity) ) else: values = [ self.getValue(py_object,quantity) ] else: values = self.event_objects[variable] ## -- If there are no values, no need to continue! if not values: continue ## -- Keeping everything as a list for uniform interfaces if type(values)==float: values = [values] scaleFactor = [scaleFactor for _ in values] ## Scale by GeV for variables with that unit if not any(x in variable for x in self.nonGeVvars): values = [i/self.GeV for i in values] ## saving the data to plot later ## self.save_event(variable,values,scaleFactor,lep) entry+=1 # -- end this entry -- # return