def test_closure(self): www_card_orig = read_card('vh3l_120.txt') www_card_file = open('vh3l_cut_rewritten.txt', 'w') write_card(www_card_file, www_card_orig) www_card_file.close() www_card_reread = read_card('vh3l_cut_rewritten.txt') self.assertEqual( www_card_reread.exp['ch1'], www_card_orig.exp['bin1'] ) self.assertEqual( www_card_reread.isSignal, www_card_orig.isSignal ) self.assertEqual( sorted(www_card_reread.processes), sorted(www_card_orig.processes) ) orig_systs = sorted(www_card_orig.systs, key=lambda x: x[0]) new_systs = sorted(www_card_reread.systs, key=lambda x: x[0]) for orig_syst, new_syst in zip(orig_systs, new_systs): # Format # name, shape, type, ?, bin-process map self.assertEqual( orig_syst[:4], new_syst[:4] ) self.assertEqual( orig_syst[4]['bin1'], new_syst[4]['ch1'] )
def interpolate_card(output_stream, lowcardfile, lowmass, highcardfile, highmass, target, processes): ''' Interpolate between two data cards The output card (with mass=target) is written to output_stream. The [processes] are interpolated. The processes can contain the format string {mass} ''' log.info("Parsing %s m:%i", lowcardfile, lowmass) lowcard = read_card(lowcardfile) log.info("Parsing %s m:%i", highcardfile, highmass) highcard = read_card(highcardfile) assert(lowcard.bins == highcard.bins) newcard = copy.deepcopy(lowcard) for process in processes: targetlabel = process.format(mass=target) log.info("Interpolating %s process" % targetlabel) lowlabel = process.format(mass=lowmass) highlabel = process.format(mass=highmass) # Check the yield for this process in each bin for bin in lowcard.bins: try: low_yield = lowcard.exp[bin][lowlabel] except KeyError: log.error("Error reading bin: %s process: %s from file: %s", bin, lowlabel, lowcardfile) raise try: high_yield = highcard.exp[bin][highlabel] except KeyError: log.error("Error reading bin: %s process: %s from file: %s", bin, highlabel, highcardfile) raise target_yield = morph.interpolate( lowmass, low_yield, highmass, high_yield, target ) #log.info("in bin", bin, "low yield is", low_yield, #"high yield is", high_yield, "=> target is ", target_yield) # We use the same label as lowlabel, and replace it later newcard.exp[bin][lowlabel] = target_yield log.info("Writing new card to output") write_card(output_stream, newcard)
def interpolate_card(output_stream, lowcardfile, lowmass, highcardfile, highmass, target, processes): ''' Interpolate between two data cards The output card (with mass=target) is written to output_stream. The [processes] are interpolated. The processes can contain the format string {mass} ''' log.info("Parsing %s m:%i", lowcardfile, lowmass) lowcard = read_card(lowcardfile) log.info("Parsing %s m:%i", highcardfile, highmass) highcard = read_card(highcardfile) assert (lowcard.bins == highcard.bins) newcard = copy.deepcopy(lowcard) for process in processes: targetlabel = process.format(mass=target) log.info("Interpolating %s process" % targetlabel) lowlabel = process.format(mass=lowmass) highlabel = process.format(mass=highmass) # Check the yield for this process in each bin for bin in lowcard.bins: try: low_yield = lowcard.exp[bin][lowlabel] except KeyError: log.error("Error reading bin: %s process: %s from file: %s", bin, lowlabel, lowcardfile) raise try: high_yield = highcard.exp[bin][highlabel] except KeyError: log.error("Error reading bin: %s process: %s from file: %s", bin, highlabel, highcardfile) raise target_yield = morph.interpolate(lowmass, low_yield, highmass, high_yield, target) #log.info("in bin", bin, "low yield is", low_yield, #"high yield is", high_yield, "=> target is ", target_yield) # We use the same label as lowlabel, and replace it later newcard.exp[bin][lowlabel] = target_yield log.info("Writing new card to output") write_card(output_stream, newcard)
def test_closure(self): www_card_orig = read_card('vh3l_120.txt') www_card_file = open('vh3l_cut_rewritten.txt', 'w') write_card(www_card_file, www_card_orig) www_card_file.close() www_card_reread = read_card('vh3l_cut_rewritten.txt') self.assertEqual(www_card_reread.exp['ch1'], www_card_orig.exp['bin1']) self.assertEqual(www_card_reread.isSignal, www_card_orig.isSignal) self.assertEqual(sorted(www_card_reread.processes), sorted(www_card_orig.processes)) orig_systs = sorted(www_card_orig.systs, key=lambda x: x[0]) new_systs = sorted(www_card_reread.systs, key=lambda x: x[0]) for orig_syst, new_syst in zip(orig_systs, new_systs): # Format # name, shape, type, ?, bin-process map self.assertEqual(orig_syst[:4], new_syst[:4]) self.assertEqual(orig_syst[4]['bin1'], new_syst[4]['ch1'])
log = logging.getLogger('remove_systematics') if __name__ == "__main__": args = parser.parse_args() logging.basicConfig(stream=sys.stderr, level=logging.INFO) input = read_card(args.card) clean_systematics = [] filters_applied = set([]) for syst in input.systs: is_clean = True for filter in args.toremove: if fnmatch.fnmatchcase(syst[0], filter): is_clean = False filters_applied.add(filter) break if not is_clean: log.info("Removing systematic: %s", syst[0]) else: clean_systematics.append(syst) for filter in args.toremove: if filter not in filters_applied: log.warning("Filter %s did not match any systematics!", filter) input.systs = clean_systematics log.info("Writing new card to stdout") write_card(sys.stdout, input)