def test_scrip_runs_and_produces_correct_outputs(folder_test_dst,
                                                 test_dst_file,
                                                 output_maps_tmdir,
                                                 test_map_file):
    """
    Run map creation script and check if an ASectormap is the output.
    """
    map_file_out = os.path.join(output_maps_tmdir, 'test_out_map.h5')
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo.h5')
    default_n_bins = 15
    run_number = 7517
    config = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_params_new = copy.copy(config.as_namespace.map_params)
    map_params_new['nmin'] = 100
    map_params_new['nStimeprofile'] = 1200
    map_params_new['z_range'] = (0, 10000)
    config.update(
        dict(folder=folder_test_dst,
             file_in=test_dst_file,
             file_out_map=map_file_out,
             file_out_hists=histo_file_out,
             default_n_bins=default_n_bins,
             run_number=run_number,
             map_params=map_params_new))
    map_builder(config.as_namespace)
    maps = read_maps(map_file_out)
    assert type(maps) == ASectorMap

    old_maps = read_maps(test_map_file)
    assert_dataframes_close(maps.e0, old_maps.e0, rtol=1e-5)
    assert_dataframes_close(maps.e0u, old_maps.e0u, rtol=1e-1)
    assert_dataframes_close(maps.lt, old_maps.lt, rtol=1e-5)
    assert_dataframes_close(maps.ltu, old_maps.ltu, rtol=1e-1)
def test_correct_map_with_unsorted_dst(folder_test_dst, test_dst_file,
                                       output_maps_tmdir):
    """
    This test shuffles the input dst, and checks that the map is the same
    as the one created with the same sorted dst.
    """
    map_file_sort = os.path.join(output_maps_tmdir, 'test_out_map.h5')
    map_file_unsort = os.path.join(output_maps_tmdir, 'test_out_unsort.h5')
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo.h5')

    dst = load_dst(folder_test_dst + test_dst_file, 'DST', 'Events')
    if "index" in dst: del dst["index"]
    dst = dst.sort_values(by=['S2e'])
    tmp_unsorted_dst = 'unsorted_dst.h5'
    dst.to_hdf(output_maps_tmdir + tmp_unsorted_dst,
               key="DST",
               mode="w",
               format="table",
               data_columns=True,
               complib="zlib",
               complevel=4)
    with tb.open_file(output_maps_tmdir + tmp_unsorted_dst, "r+") as file:
        file.rename_node(file.root.DST.table, "Events")
        file.root.DST.Events.title = "Events"

    default_n_bins = 15
    run_number = 7517
    config = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_params_new = config.as_namespace.map_params
    map_params_new['nmin'] = 100
    map_params_new['z_range'] = (0, 10000)
    config.update(
        dict(folder=output_maps_tmdir,
             file_in=tmp_unsorted_dst,
             file_out_map=map_file_unsort,
             file_out_hists=histo_file_out,
             default_n_bins=default_n_bins,
             run_number=run_number,
             map_params=map_params_new))
    map_builder(config.as_namespace)
    unsorted_maps = read_maps(map_file_unsort)
    sorted_maps = read_maps(map_file_sort)

    assert_dataframes_close(unsorted_maps.e0, sorted_maps.e0, rtol=1e-5)
    assert_dataframes_close(unsorted_maps.e0u, sorted_maps.e0u, rtol=1e-5)
    assert_dataframes_close(unsorted_maps.lt, sorted_maps.lt, rtol=1e-5)
    assert_dataframes_close(unsorted_maps.ltu, sorted_maps.ltu, rtol=1e-5)
def test_exception_rate(folder_test_dst, test_dst_file, output_maps_tmdir):
    """
    This test checks if exception raises when rate distribution is not flat enough.
    """
    conf = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_rate.h5')
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_rate.h5')
    n_dev_rate = 0.5
    run_number = 7517
    conf.update(
        dict(folder=folder_test_dst,
             file_in=test_dst_file,
             file_out_map=map_file_out,
             file_out_hists=histo_file_out,
             n_dev_rate=n_dev_rate,
             run_number=run_number))

    assert_raises(AbortingMapCreation, map_builder, conf.as_namespace)
Exemple #4
0
def test_position_signal_kr(config_tmpdir, fullsim_data, test_config):

    PATH_OUT = os.path.join(config_tmpdir, 'Kr_fullsim.buffers.h5')

    conf = configure(['dummy', test_config])
    conf.update(dict(files_in=fullsim_data, file_out=PATH_OUT))

    cnt = position_signal(conf.as_namespace)

    with tb.open_file(fullsim_data, mode='r') as h5in, \
         tb.open_file(PATH_OUT    , mode='r') as h5out:

        assert hasattr(h5out.root, 'MC')
        assert hasattr(h5out.root, 'Run')
        assert hasattr(h5out.root.MC, 'extents')
        assert hasattr(h5out.root.MC, 'hits')
        assert hasattr(h5out.root.MC, 'particles')

        assert_tables_equality(h5in.root.MC.particles, h5out.root.MC.particles)
def test_exception_Zdst(folder_test_dst, test_dst_file, output_maps_tmdir):
    """
    This test checks if exception raises when Z distribution is not
    similar enough to the reference one.
    """
    conf = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_Z.h5')
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_Z.h5')
    nsigmas_Zdst = 0.5
    run_number = 7517
    conf.update(
        dict(folder=folder_test_dst,
             file_in=test_dst_file,
             file_out_map=map_file_out,
             file_out_hists=histo_file_out,
             nsigmas_Zdst=nsigmas_Zdst,
             run_number=run_number))

    assert_raises(AbortingMapCreation, map_builder, conf.as_namespace)
def test_exception_s2(folder_test_dst, test_dst_file, output_maps_tmdir):
    """
    This test checks if exception raises when nS2=1 efficiency is out of range.
    """
    conf = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_s2.h5')
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_s2.h5')
    min_eff_test = 0.
    max_eff_test = 0.9
    run_number = 7517
    conf.update(
        dict(folder=folder_test_dst,
             file_in=test_dst_file,
             file_out_map=map_file_out,
             file_out_hists=histo_file_out,
             nS2_eff_min=min_eff_test,
             nS2_eff_max=max_eff_test,
             run_number=run_number))

    assert_raises(AbortingMapCreation, map_builder, conf.as_namespace)
def test_exception_bandsel(folder_test_dst, test_dst_file, output_maps_tmdir):
    """
    This test checks if exception raises when band selection efficiency is
    out of a given range.
    """
    conf = configure('maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_file_out   = os.path.join(output_maps_tmdir, 'test_out_map_bandsel.h5'  )
    histo_file_out = os.path.join(output_maps_tmdir, 'test_out_histo_bandsel.h5')
    band_sel_params_new = copy.copy(conf.as_namespace.band_sel_params)
    band_sel_params_new['eff_min'] = 0.
    band_sel_params_new['eff_max'] = 0.89
    run_number = 7517
    conf.update(dict(folder         = folder_test_dst,
                     file_in        = test_dst_file  ,
                     file_out_map   = map_file_out   ,
                     file_out_hists = histo_file_out ,
                     band_sel_params = band_sel_params_new,
                     run_number     = run_number     ))
    assert_raises(AbortingMapCreation,
                  map_builder        ,
                  conf.as_namespace  )
def test_exception_drift_v(folder_test_dst, test_dst_file, output_maps_tmdir):
    """
    This test checks that exception raises if drift_v fit
    fails in too many temporal bins.
    """
    conf = configure(
        'maps $ICARO/krcal/map_builder/config_LBphys.conf'.split())
    map_file_out = os.path.join(output_maps_tmdir, 'test_out_map_driftv.h5')
    histo_file_out = os.path.join(output_maps_tmdir,
                                  'test_out_histo_driftv.h5')
    map_params_new = copy.copy(conf.as_namespace.map_params)
    map_params_new['dv_maxFailed'] = 0.01
    map_params_new['nStimeprofile'] = 100
    run_number = 7517
    conf.update(
        dict(folder=folder_test_dst,
             file_in=test_dst_file,
             file_out_map=map_file_out,
             file_out_hists=histo_file_out,
             map_params=map_params_new,
             run_number=run_number))
    assert_raises(AbortingMapCreation, map_builder, conf.as_namespace)
Exemple #9
0
def test_position_signal_neut(config_tmpdir, neut_fullsim, test_config,
                              neut_buffers):

    PATH_OUT = os.path.join(config_tmpdir, 'neut_fullsim.buffers.h5')

    conf = configure(['dummy', test_config])
    conf.update(dict(files_in=neut_fullsim, file_out=PATH_OUT))

    cnt = position_signal(conf.as_namespace)

    with tb.open_file(neut_buffers, mode='r') as h5test, \
         tb.open_file(PATH_OUT    , mode='r') as h5out:

        pmt_out = h5out.root.pmtrd
        pmt_test = h5test.root.pmtrd

        assert pmt_out.shape == pmt_test.shape
        assert_tables_equality(pmt_out, pmt_test)

        sipm_out = h5out.root.sipmrd
        sipm_test = h5test.root.sipmrd

        assert sipm_out.shape == sipm_test.shape
        assert_tables_equality(sipm_out, sipm_test)
Exemple #10
0
    # Storing event counters as attributes
    tracks_filter_events = sum(events_dict['tracks_filter'])
    blobs_filter_events = sum(events_dict['blobs_filter'])
    roi_filter_events = sum(events_dict['roi_filter'])

    store_events_ana_counters(oFile, ana_group_name, simulated_events,
                              stored_events, smE_filter_events,
                              fid_filter_events, tracks_filter_events,
                              blobs_filter_events, roi_filter_events)

    ### Ending ...
    oFile.close()
    print('* Analysis done !!\n')

    print('''* Event counters:
  Simulated events:     {0:9}
  Stored events:        {1:9}
  smE_filter events:    {2:9}
  fid_filter events:    {3:9}
  tracks_filter events: {4:9}
  blobs_filter events:  {5:9}
  roi_filter events:    {6:9}'''.format(simulated_events, stored_events,
                                        smE_filter_events, fid_filter_events,
                                        tracks_filter_events,
                                        blobs_filter_events,
                                        roi_filter_events))


if __name__ == '__main__':
    result = fanal_ana(**configure(sys.argv))
Exemple #11
0
    with tb.open_file(file_out, "w", filters = tbl.filters(compression)) as h5out:

        write_mc         = fl.sink(mc_info_writer(h5out),
                                   args = ("mc", "evt"))
        buffer_writer_   = fl.sink(buffer_writer(h5out                  ,
                                                 n_sens_eng = npmt      ,
                                                 n_sens_trk = nsipm     ,
                                                 length_eng = nsamp_pmt ,
                                                 length_trk = nsamp_sipm),
                                   args = ("evt", "pmt_ord", "sipm_ord",
                                           "evt_times", "buffers"))

        save_run_info(h5out, run_number)
        return push(source = load_sensors(files_in, detector_db, run_number),
                    pipe   = pipe(pmt_binning         ,
                                  extract_minmax      ,
                                  sipm_binning        ,
                                  sensor_order_       ,
                                  trigger_finder_     ,
                                  event_times         ,
                                  calculate_buffers_  ,
                                  fork(buffer_writer_,
                                       write_mc      )))



if __name__ == "__main__":
    conf = configure(sys.argv).as_namespace
    position_signal(conf)
Exemple #12
0
import traceback
from sys import argv
from importlib import import_module
from invisible_cities.cities import esmeralda
from invisible_cities.core.configure import configure

args = argv
city_name = esmeralda

esmeralda.esmeralda(**configure(args))
Exemple #13
0
# This RWF file is produced as output of DIOMIRA. 
RWF_file= os.environ['IC_NOTEBOOK_DIR']  + '/files/electrons_40keV_z250_RWF_TMP.h5'

# era any previous temporary file
try:
    os.system("rm -f {}".format(RWF_file))
except(IOError):
    pass


# ### Running diomira

# In[13]:

# function configure reads the configuration file and returns a dictionary of options
CFP = configure(['DIOMIRA','-c', conf_file, '-i', MCRD_file, '-o', RWF_file])

# init machine
fpp = Diomira()
# set machine state throught the parameters read from config file
files_in = glob(CFP['FILE_IN'])
files_in.sort()
fpp.set_input_files(files_in)
fpp.set_output_file(CFP['FILE_OUT'],
                        compression=CFP['COMPRESSION'])
fpp.set_print(nprint=CFP['NPRINT'])
fpp.set_sipm_noise_cut(noise_cut=CFP["NOISE_CUT"])

# run for nevts
nevts = CFP['NEVENTS'] if not CFP['RUN_ALL'] else -1
t0 = time()
Exemple #14
0
            if (analyzed_events == (10 * toUpdate_events)): toUpdate_events *= 10
            

    ### STORING RECONSTRUCTION DATA
    # Storing events and voxels dataframes
    print('\n* Storing data in the output file: {}'.format(file_out))
    store_events_reco_dict(file_out, reco_group_name, events_dict)
    store_voxels_reco_dict(file_out, reco_group_name, voxels_dict)

    # Storing event counters as attributes
    smE_filter_events = sum(events_dict['smE_filter'])
    fid_filter_events = sum(events_dict['fid_filter'])
    store_events_reco_counters(oFile, reco_group_name, simulated_events,
                               stored_events, smE_filter_events, fid_filter_events)

    oFile.close()
    print('* Reconstruction done !!\n')

    # Printing reconstruction numbers
    print('* Event counters ...')
    print('''  Simulated events:  {0:9}
  Stored events:     {1:9}
  smE_filter events: {2:9}
  fid_filter events: {3:9}\n'''
        .format(simulated_events, stored_events, smE_filter_events, fid_filter_events))



if __name__ == '__main__':
	result = fanal_reco(**configure(sys.argv))
def xe137_normalization(conf_list, spec_shift=0, suppress_df=False):

    config = configure(conf_list).as_namespace

    flux_file = os.path.expandvars(config.flux_file)
    acti_file = os.path.expandvars(config.acti_file)
    out_file = os.path.expandvars(config.file_out)
    sim_muons = int(config.n_simulated_muons)
    if not hasattr(config, 'log_bins'):
        log_bins = False
    else:
        log_bins = bool(config.log_bins)
    lab_flux = float(config.lab_flux)
    lab_flux_e = float(config.lab_flux_err)
    gen_area = float(config.gen_area)

    if hasattr(config, 'bin_edges'):
        bins = config.bin_edges
    else:
        bin_range = config.bin_range
        if log_bins:
            bins = np.logspace(np.log10(bin_range[0]), np.log10(bin_range[0]),
                               bin_range[2])
        else:
            bins = np.linspace(*bin_range)

    binned_sim_muons, _ = np.histogram(np.random.uniform(
        bins[0], bins[-1], sim_muons),
                                       bins=bins)

    xe137_df = pd.read_hdf(acti_file)
    xe137_df['GeV'] = xe137_df.Xemunrg * 1e-3

    xe137_count, _ = np.histogram(xe137_df.GeV.values, bins=bins)
    xe137_exp = xe137_count / binned_sim_muons
    xe137_exp_err = xe137_exp * np.sqrt(1 / xe137_count + 1 / binned_sim_muons)

    ## Get flux in the same bins
    for i in range(10):

        vals = pd.read_hdf(flux_file, 'muon_flux_' + str(i))

        histo, _ = np.histogram(vals.E.values + spec_shift, bins=bins)

        try:
            flux_histo += histo
        except NameError:
            flux_histo = histo

    norm_flux = flux_histo / flux_histo.sum()
    flux_err = norm_flux * np.sqrt(1 / flux_histo + 1 / flux_histo.sum())
    fluxCMS = norm_flux * lab_flux
    fluxCMS_e = fluxCMS * np.sqrt((flux_err / norm_flux)**2 +
                                  (lab_flux_e / lab_flux)**2)
    fluxS = fluxCMS * gen_area
    fluxS_e = gen_area * fluxCMS_e
    xe137S = xe137_exp * fluxS  ## Xe137 per bin per second
    xe137S_e = xe137S * np.sqrt((xe137_exp_err / xe137_exp)**2 +
                                (fluxS_e / fluxS)**2)
    xe137Y = xe137S * 3.1536e7
    xe137Y_e = xe137S_e * 3.1536e7

    total_xe137PS = xe137S.sum()
    perSec_err = np.sqrt(np.sum(xe137S_e**2))
    total_xe137PY = xe137Y.sum()
    perYr_err = np.sqrt(np.sum(xe137Y_e**2))
    print('Xe-137 per second = ', total_xe137PS, '+/-', perSec_err)
    print('Xe-137 per calendar yr = ', total_xe137PY, '+/-', perYr_err)

    if suppress_df:
        ## Optimisation or error calc,
        ## just return the per sec prediction
        return total_xe137PS, perSec_err

    plt.errorbar(shift_to_bin_centers(bins),
                 xe137Y,
                 fmt='^',
                 xerr=np.diff(bins) / 2,
                 yerr=xe137Y_e)
    plt.xlabel('Muon energy (GeV)')
    plt.ylabel('Xe-137 expectation per yr per bin')
    plt.show()

    ## Output the calculation at each stage to file
    df = pd.DataFrame({
        "BinMin": bins[:-1],
        "BinMax": bins[1:],
        "n_xe137": xe137_count,
        "xe137PerMu": xe137_exp,
        "Err137PerMu": xe137_exp_err,
        "NormFlux": norm_flux,
        "FluxErr": flux_err,
        "FluxPerCM2PerS": fluxCMS,
        "FluxAreaSErr": fluxCMS_e,
        "FluxPerS": fluxS,
        "FluxSErr": fluxS_e,
        "xe137PerS": xe137S,
        "xe137SErr": xe137S_e,
        "xe137PerY": xe137Y,
        "xe137YErr": xe137Y_e
    })

    df.to_hdf(out_file, 'xe137')
def xe137_activation_prob(conf_list, spec_shift=0):
    '''
    It returns the translation factor from muon to Xe137
    '''

    config = configure(conf_list).as_namespace

    flux_file = os.path.expandvars(config.flux_file)
    acti_file = os.path.expandvars(config.acti_file)
    out_file = os.path.expandvars(config.file_out)
    sim_muons = int(config.n_simulated_muons)
    if not hasattr(config, 'log_bins'):
        log_bins = False
    else:
        log_bins = bool(config.log_bins)
    lab_flux = float(config.lab_flux)
    lab_flux_e = float(config.lab_flux_err)
    gen_area = float(config.gen_area)

    if hasattr(config, 'bin_edges'):
        bins = config.bin_edges
    else:
        bin_range = config.bin_range
        if log_bins:
            bins = np.logspace(np.log10(bin_range[0]), np.log10(bin_range[0]),
                               bin_range[2])
        else:
            bins = np.linspace(*bin_range)

    binned_sim_muons, _ = np.histogram(np.random.uniform(
        bins[0], bins[-1], sim_muons),
                                       bins=bins)

    xe137_df = pd.read_hdf(acti_file)
    xe137_df['GeV'] = xe137_df.Xemunrg * 1e-3

    xe137_count, _ = np.histogram(xe137_df.GeV.values, bins=bins)
    xe137_exp = xe137_count / binned_sim_muons
    xe137_exp_err = xe137_exp * np.sqrt(1 / xe137_count + 1 / binned_sim_muons)

    ## Get flux in the same bins
    for i in range(10):

        vals = pd.read_hdf(flux_file, 'muon_flux_' + str(i))

        histo, _ = np.histogram(vals.E.values + spec_shift, bins=bins)

        try:
            flux_histo += histo
        except NameError:
            flux_histo = histo

    norm_flux = flux_histo / flux_histo.sum()
    flux_err = norm_flux * np.sqrt(1 / flux_histo + 1 / flux_histo.sum())
    fluxCMS = norm_flux * lab_flux
    fluxCMS_e = fluxCMS * np.sqrt((flux_err / norm_flux)**2 +
                                  (lab_flux_e / lab_flux)**2)
    fluxS = fluxCMS * gen_area
    fluxS_e = gen_area * fluxCMS_e
    xe137S = xe137_exp * fluxS  ## Xe137 per bin per second
    xe137S_e = xe137S * np.sqrt((xe137_exp_err / xe137_exp)**2 +
                                (fluxS_e / fluxS)**2)
    xe137Y = xe137S * 3.1536e7
    xe137Y_e = xe137S_e * 3.1536e7

    total_xe137PS = xe137S.sum()
    perSec_err = np.sqrt(np.sum(xe137S_e**2))
    total_xe137PY = xe137Y.sum()
    perYr_err = np.sqrt(np.sum(xe137Y_e**2))
    print('Xe-137 per second = ', total_xe137PS, '+/-', perSec_err)
    print('Xe-137 per calendar yr = ', total_xe137PY, '+/-', perYr_err)

    return total_xe137PS, perSec_err