def create_toy_mc(input_file, sample, output_folder, n_toy, centre_of_mass, ttbar_xsection):
    from tools.file_utilities import make_folder_if_not_exists
    from tools.toy_mc import generate_toy_MC_from_distribution, generate_toy_MC_from_2Ddistribution
    from tools.Unfolding import get_unfold_histogram_tuple
    make_folder_if_not_exists(output_folder)
    input_file_hists = File(input_file)
    output_file_name = get_output_file_name(output_folder, sample, n_toy, centre_of_mass)
    variable_bins = bin_edges_vis.copy()
    with root_open(output_file_name, 'recreate') as f_out:
        for channel in ['combined']:
            for variable in variable_bins:
                output_dir = f_out.mkdir(channel + '/' + variable, recurse=True)
                cd = output_dir.cd
                mkdir = output_dir.mkdir
                h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(input_file_hists,
                                                                        variable,
                                                                        channel,
                                                                        centre_of_mass = centre_of_mass,
                                                                        ttbar_xsection = ttbar_xsection,
                                                                        visiblePS = True,
                                                                        load_fakes=False)

                cd()

                mkdir('Original')
                cd ('Original')
                h_truth.Write('truth')
                h_measured.Write('measured')
                h_response.Write('response')

                for i in range(1, n_toy+1):
                    toy_id = 'toy_{0}'.format(i)
                    mkdir(toy_id)
                    cd(toy_id)
                    # create histograms
                    # add tuples (truth, measured, response) of histograms
                    truth = generate_toy_MC_from_distribution(h_truth)
                    measured = generate_toy_MC_from_distribution(h_measured)
                    response = generate_toy_MC_from_2Ddistribution(h_response)

                    truth.SetName('truth')
                    measured.SetName('measured')
                    response.SetName('response')

                    truth.Write()
                    measured.Write()
                    response.Write()
Exemplo n.º 2
0
def main():
    set_root_defaults()
    # prevent directory ownership of ROOT histograms (python does the garbage collection)
    TH1F.AddDirectory( False )
    parser = OptionParser()
    parser.add_option( "-n", "--n_toy_mc",
                      dest = "n_toy_mc", default = 300,
                      help = "number of toy MC to create", type = int )
    parser.add_option( "-o", "--output",
                      dest = "output_folder", default = 'data/toy_mc/',
                      help = "output folder for toy MC" )
    parser.add_option( "-v", "--variable", dest = "variable", default = 'MET',
                      help = "set the variable to analyse (MET, HT, ST, MT, WPT)" )
    parser.add_option( "-m", "--metType", dest = "metType", default = 'type1',
                      help = "set MET type for analysis of MET, ST or MT" )
    parser.add_option( "-c", "--centre-of-mass-energy", dest = "CoM", default = 8,
                      help = "set the centre of mass energy for analysis. Default = 8 [TeV]", type = int )
    parser.add_option( '-V', '--verbose', dest = "verbose", action = "store_true",
                      help = "Print the event number, reco and gen variable value" )

    ( options, _ ) = parser.parse_args()
    measurement_config = XSectionConfig( options.CoM )

    centre_of_mass = options.CoM
    ttbar_xsection = measurement_config.ttbar_xsection
    variable = options.variable
    met_type = measurement_config.translate_options[options.metType]
    n_toy_mc = options.n_toy_mc
    make_folder_if_not_exists( options.output_folder )
    
    # get histograms
    input_file_hists = File( measurement_config.unfolding_madgraph )
    # define output file
    out_file_template = '%s/toy_mc_%s_N_%d_%dTeV.root'
    out_file_name = out_file_template % (options.output_folder, variable, n_toy_mc, centre_of_mass)
    output = File( out_file_name, 'recreate' )
    
    for channel in ['electron', 'muon']:
        # first get the weights
        h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple( input_file_hists,
                                                                                       variable,
                                                                                       channel,
                                                                                       met_type,
                                                                                       centre_of_mass,
                                                                                       ttbar_xsection,
                                                                                       load_fakes = False )
        # create directories
        directory = output.mkdir( channel )
        mkdir = directory.mkdir
        cd = directory.cd
        cd()
        # generate toy MC
        for i in range( 1, n_toy_mc + 1 ):
            mkdir( 'toy_%d' % i )
            cd( 'toy_%d' % i )
            # create histograms
            # add tuples (truth, measured, response) of histograms
            truth = generate_toy_MC_from_distribution(h_truth)
            measured = generate_toy_MC_from_distribution(h_measured)
            response = generate_toy_MC_from_2Ddistribution(h_response)
            
            truth.SetName('truth')
            measured.SetName('measured')
            response.SetName('response')
            
            truth.Write()
            measured.Write()
            response.Write()
    output.Write()
    output.Close()