Ejemplo n.º 1
0
def I3CLSimMakePhotons(
        tray,
        name,
        UseCPUs=False,
        UseGPUs=True,
        UseOnlyDeviceNumber=None,
        MCTreeName="I3MCTree",
        OutputMCTreeName=None,
        FlasherInfoVectName=None,
        FlasherPulseSeriesName=None,
        MMCTrackListName="MMCTrackList",
        PhotonSeriesName="PhotonSeriesMap",
        ParallelEvents=1000,
        TotalEnergyToProcess=0.,
        RandomService=None,
        IceModelLocation=expandvars("$I3_SRC/clsim/resources/ice/spice_mie"),
        DisableTilt=False,
        UnWeightedPhotons=False,
        UnWeightedPhotonsScalingFactor=None,
        UseGeant4=False,
        CrossoverEnergyEM=None,
        CrossoverEnergyHadron=None,
        UseCascadeExtension=True,
        StopDetectedPhotons=True,
        PhotonHistoryEntries=0,
        DoNotParallelize=False,
        DOMOversizeFactor=5.,
        UnshadowedFraction=0.9,
        HoleIceParameterization=expandvars(
            "$I3_SRC/ice-models/resources/models/angsens/as.h2-50cm"),
        WavelengthAcceptance=None,
        DOMRadius=0.16510 * icetray.I3Units.m,  # 13" diameter
        OverrideApproximateNumberOfWorkItems=None,
        IgnoreSubdetectors=['IceTop'],
        ExtraArgumentsToI3CLSimModule=dict(),
        If=lambda f: True):
    """Do standard clsim processing up to the I3Photon level.
    These photons still need to be converted to I3MCPEs to be usable
    for further steps in the standard IceCube MC processing chain.
    Reads its particles from an I3MCTree and writes an I3PhotonSeriesMap.

    All available OpenCL GPUs (and optionally CPUs) will
    be used. This will take over your entire machine,
    so make sure to configure your batch jobs correctly
    when using this on a cluster.
    When using nVidia cards, you can set the
    CUDA_VISIBLE_DEVICES environment variable to
    limit GPU visibility. A setting of
    CUDA_VISIBLE_DEVICES="0,3" would only use cards
    #0 and #3 and ignore cards #1 and #2. In case you are
    using a batch system, chances are this variable is already
    set. Unfortunately, there is no corresponding setting
    for the AMD driver.

    This segment assumes that MMC has been applied to the
    I3MCTree and that MMC was *NOT* run using the "-recc" option.

    :param UseCPUs:
        Turn this on to also use CPU-based devices.
        (This may potentially slow down photon generation, which
        is also done on the CPU in parallel.)
    :param UseGPUs:
        Turn this off to not use GPU-based devices.
        This may be useful if your GPU is used for display
        purposes and you don't want it to slow down.
    :param UseOnlyDeviceNumber:
        Use only a single device number, even if there is more than
        one device found matching the required description. The numbering
        starts at 0.
    :param MCTreeName:
        The name of the I3MCTree containing the particles to propagate.
    :param OutputMCTreeName:
        A copy of the (possibly sliced) MCTree will be stored as this name.
    :param FlasherInfoVectName:
        Set this to the name of I3FlasherInfoVect objects in the frame to
        enable flasher simulation. The module will read I3FlasherInfoVect objects
        and generate photons according to assumed parameterizations.
    :param FlasherPulseSeriesName:
        Set this to the name of an I3CLSimFlasherPulseSeries object in the frame to
        enable flasher/Standard Candle simulation.
        This cannot be used at the same time as FlasherInfoVectName.
        (I3CLSimFlasherPulseSeries objects are clsim's internal flasher
        representation, if "FlasherInfoVectName" is used, the I3FlasherInfoVect
        objects are converted to I3CLSimFlasherPulseSeries objects.)
    :param MMCTrackListName:
        Only used if *ChopMuons* is active. Set it to the name
        of the I3MMCTrackList object that contains additional
        muon energy loss information.
    :param PhotonSeriesName:
        Configure this to enable writing an I3PhotonSeriesMap containing
        all photons that reached the DOM surface.
    :param ParallelEvents:
        clsim will work on a couple of events in parallel in order
        not to starve the GPU. Setting this too high will result
        in excessive memory usage (all your frames have to be cached
        in RAM). Setting it too low may impact simulation performance.
        The optimal value depends on your energy distribution/particle type.
    :param TotalEnergyToProcess:
       clsim will work on a couple of events in parallel in order
       not to starve the GPU. With this setting clsim will figure out
       how many frames to accumulate as to not starve the GPU based on 
       the energy of the light sources that are producing the photons 
       in the detector. Setting this too high will result
       in excessive memory usage (all your frames have to be cached
       in RAM). Setting it too low may impact simulation performance. 
       This cannot be used in flasher mode, since we cannot measure
       the energy of the light sources.
    :param RandomService:
        Set this to an instance of a I3RandomService. Alternatively,
        you can specify the name of a configured I3RandomServiceFactory
        added to I3Tray using tray.AddService(). If you don't configure
        this, the default I3RandomServiceFactory will be used.
    :param IceModelLocation:
        Set this either to a directory containing a PPC-compatible
        ice description (icemodel.dat, icemodel.par and cfg.txt) or
        to a photonics ice table file. PPC-compatible ice files should
        generally lead to faster execution times on GPUs since it involves
        less interpolation between table entries (the PPC ice-specification
        is parametric w.r.t. wavelength, whereas the photonics specification
        is not).
    :param DisableTilt:
        Do not simulate ice tilt, even if the ice model directory
        provides tilt information. (Photonics-based models will never
        have tilt.)
    :param UnWeightedPhotons:
        Enabling this setting will disable all optimizations. These
        are currently a DOM oversize factor of 5 (with the appropriate
        timing correction) and a biased initial photon spectrum that
        includes the DOM spectral acceptance. Enabling this setting
        essentially means that all photons that would be generated
        in the real detector *will* actually be generated. This will siginificatly
        slow down the simulation, but the optional ``PhotonSeries``
        will contain an unweighted sample of photons that arrive
        at your DOMs. This can be useful for DOM acceptance studies.
    :param UnWeightedPhotonsScalingFactor:
        If UnWeightedPhotons is turned on, this can be used to scale
        down the overall number of photons generated. This should normally
        not be touched (it can be used when generating photon paths
        for animation). Valid range is a float >0. and <=1.
    :param StopDetectedPhotons:
        Configures behaviour for photons that hit a DOM. If this is true (the default)
        photons will be stopped once they hit a DOM. If this is false, they continue to
        propagate.
    :param PhotonHistoryEntries:
        The maximum number of scatterings points to be saved for every photon hitting a DOM.
        Only the most recent positions are saved, older positions are overwritten if
        the maximum size is reached.
    :param UseGeant4:
        Enabling this setting will disable all cascade and muon light yield
        parameterizations. All particles will sent to Geant4 for a full
        simulation. This does **not** apply to muons that do have a length
        assigned. These are assumed to have been generated by MMC and
        their light is generated according to the usual parameterization.
    :param CrossoverEnergyEM:
        If set it defines the crossover energy between full Geant4 simulations and 
        light yield parameterizations for electro magnetic cascades. This only works
        when UseGeant4 is set to true. It works in conjunction with CrossoverEnergyHadron.
        If one of both is set to a positiv value greater 0 (GeV), the hybrid simulation
        is used.
        If CrossoverEnergyEM is set to None while CrossoverEnergyHadron is set so
        hybrid mode is working, GEANT4 is used for EM cascades.
        If CrossoverEnergyEM is set to 0 (GeV) while CrossoverEnergyHadron is set
        so hybrid mode is working, leptons and EM cascades will use parameterizations
        for the whole energy range.
    :param CrossoverEnergyHadron:
        If set it defines the crossover energy between full Geant4 simulations and
        light yield parameterizations for hadronic cascades. This only works when
        UseGeant4 is set to true. It works in conjunction with CrossoverEnergyEM.
        If one of both is set to a positiv value greater 0 (GeV), the hybrid simulation
        is used.
        If CrossoverEnergyHadron is set to None while CrossoverEnergyEM is set so
        hybrid mode is working, GEANT4 is used for hadronic cascades.
        If CrossoverEnergyHadron is set to 0 (GeV) while CrossoverEnergyHadron is
        set so hybrid mode is working, hadronic cascades will use parameterizations
        for the whole energy range.
    :param UseCascadeExtension:
    	If set, the cascade light emission parameterizations will include 
    	longitudinal extension. Otherwise, parameterized cascades will be 
    	treated as point-like. 
    :param DoNotParallelize:
        Try only using a single work item in parallel when running the
        OpenCL simulation. This might be useful if you want to run jobs
        in parallel on a batch system. This will only affect CPUs and
        will be a no-op for GPUs.
    :param DOMOversizeFactor:
        Set the DOM oversize factor. To disable oversizing, set this to 1.
    :param UnshadowedFraction:
        Fraction of photocathode available to receive light (e.g. unshadowed by the cable)
    :param HoleIceParameterization:
        Set this to a hole ice parameterization file. The default file contains the 
        coefficients for nominal angular acceptance correction due to hole ice (ice-models 
        project is required). Use file $I3_SRC/ice-models/resources/models/angsens/as.nominal 
        for no hole ice parameterization.
    :param WavelengthAcceptance:
        If specified, use this wavelength acceptance to scale the generated
        Cherenkov spectrum rather than using the DOM acceptance modified for
        oversizing and angular acceptance.
    :param DOMRadius:
        Allow the DOMRadius to be set externally, for things like mDOMs.
    :param OverrideApproximateNumberOfWorkItems:
        Allows to override the auto-detection for the maximum number of parallel work items.
        You should only change this if you know what you are doing.
    :param If:
        Python function to use as conditional execution test for segment modules.        
    """

    from icecube import icetray, dataclasses, phys_services, clsim

    # make sure the geometry is updated to the new granular format (in case it is supported)
    if hasattr(dataclasses, "I3ModuleGeo"):
        tray.AddModule("I3GeometryDecomposer",
                       name + "_decomposeGeometry",
                       If=lambda frame: If(frame) and
                       ("I3OMGeoMap" not in frame) and
                       ("I3ModuleGeoMap" not in frame))

    if UseGeant4:
        if not clsim.I3CLSimLightSourceToStepConverterGeant4.can_use_geant4:
            raise RuntimeError(
                "You have requested to use Geant 4, but clsim was compiled without Geant 4 support"
            )

    # at the moment the Geant4 paths need to be set, even if it isn't used
    # TODO: fix this
    if clsim.I3CLSimLightSourceToStepConverterGeant4.can_use_geant4:
        AutoSetGeant4Environment()

    # warn the user in case they might have done something they probably don't want
    if UnWeightedPhotons and (DOMOversizeFactor != 1.):
        print("********************")
        print(
            "Enabling the clsim.I3CLSimMakeHits() \"UnWeightedPhotons=True\" option without setting"
        )
        print(
            "\"DOMOversizeFactor=1.\" will still apply a constant weighting factor of DOMOversizeFactor**2."
        )
        print("If this is what you want, you can safely ignore this warning.")
        print("********************")

    # a constant
    Jitter = 2. * icetray.I3Units.ns

    if MMCTrackListName is None or MMCTrackListName == "":
        # the input does not seem to have been processed by MMC
        ChopMuons = False
    else:
        ChopMuons = True

    if MCTreeName is None or MCTreeName == "":
        clSimMCTreeName = ""
        if ChopMuons:
            raise RuntimeError(
                "You cannot have \"MMCTrackListName\" enabled with no MCTree!")
    else:
        clSimMCTreeName = MCTreeName

    if FlasherInfoVectName is None or FlasherInfoVectName == "":
        if (FlasherPulseSeriesName
                is not None) and (FlasherPulseSeriesName != ""):
            SimulateFlashers = True
            clSimFlasherPulseSeriesName = FlasherPulseSeriesName
            clSimOMKeyMaskName = ""
        else:
            SimulateFlashers = False
            clSimFlasherPulseSeriesName = ""
            clSimOMKeyMaskName = ""
    else:
        if (FlasherPulseSeriesName
                is not None) and (FlasherPulseSeriesName != ""):
            raise RuntimeError(
                "You cannot use the FlasherPulseSeriesName and FlasherInfoVectName parameters at the same time!"
            )

        SimulateFlashers = True
        clSimFlasherPulseSeriesName = FlasherInfoVectName + "_pulses"
        clSimOMKeyMaskName = FlasherInfoVectName + "_OMKeys"

        tray.AddModule(clsim.FlasherInfoVectToFlasherPulseSeriesConverter,
                       name + "_FlasherInfoVectToFlasherPulseSeriesConverter",
                       FlasherInfoVectName=FlasherInfoVectName,
                       FlasherPulseSeriesName=clSimFlasherPulseSeriesName,
                       FlasherOMKeyVectName=clSimOMKeyMaskName,
                       If=If)

    # (optional) pre-processing
    if ChopMuons:
        if OutputMCTreeName is not None:
            clSimMCTreeName_new = OutputMCTreeName
        else:
            clSimMCTreeName_new = clSimMCTreeName + "_sliced"

        tray.AddModule("I3MuonSlicer",
                       name + "_chopMuons",
                       InputMCTreeName=clSimMCTreeName,
                       MMCTrackListName=MMCTrackListName,
                       OutputMCTreeName=clSimMCTreeName_new,
                       If=If)
        clSimMCTreeName = clSimMCTreeName_new
    else:
        if (OutputMCTreeName is not None) and (OutputMCTreeName != ""):
            # copy the MCTree to the requested output name
            def copyMCTree(frame, inputName, outputName, If_=None):
                if If_ is not None:
                    if not If_(frame): return
                frame[outputName] = frame[inputName]

            tray.AddModule(copyMCTree,
                           name + "_copyMCTree",
                           inputName=clSimMCTreeName,
                           outputName=OutputMCTreeName,
                           Streams=[icetray.I3Frame.DAQ],
                           If_=If)
            clSimMCTreeName = OutputMCTreeName
        else:
            clSimMCTreeName = clSimMCTreeName

    # ice properties
    if isinstance(IceModelLocation, str):
        mediumProperties = parseIceModel(IceModelLocation,
                                         disableTilt=DisableTilt)
    else:
        # get ice model directly if not a string
        mediumProperties = IceModelLocation

    # detector properties
    if WavelengthAcceptance is None:
        # the hole ice acceptance curve peaks at a value different than 1
        peak = numpy.loadtxt(HoleIceParameterization)[
            0]  # The value at which the hole ice acceptance curve peaks
        domEfficiencyCorrection = UnshadowedFraction * peak * 1.35 * 1.01  # DeepCore DOMs have a relative efficiency of 1.35 plus security margin of +1%

        domAcceptance = clsim.GetIceCubeDOMAcceptance(
            domRadius=DOMRadius * DOMOversizeFactor,
            efficiency=domEfficiencyCorrection)
    else:
        domAcceptance = WavelengthAcceptance

    # photon generation wavelength bias
    if not UnWeightedPhotons:
        wavelengthGenerationBias = domAcceptance
        if UnWeightedPhotonsScalingFactor is not None:
            raise RuntimeError(
                "UnWeightedPhotonsScalingFactor should not be set when UnWeightedPhotons is not set"
            )
    else:
        if UnWeightedPhotonsScalingFactor is not None:
            print(
                "***** running unweighted simulation with a photon pre-scaling of",
                UnWeightedPhotonsScalingFactor)
            wavelengthGenerationBias = clsim.I3CLSimFunctionConstant(
                UnWeightedPhotonsScalingFactor)
        else:
            wavelengthGenerationBias = None

    # muon&cascade parameterizations
    ppcConverter = clsim.I3CLSimLightSourceToStepConverterPPC(
        photonsPerStep=200)
    ppcConverter.SetUseCascadeExtension(UseCascadeExtension)
    if not UseGeant4:
        particleParameterizations = GetDefaultParameterizationList(
            ppcConverter, muonOnly=False)
    else:
        if CrossoverEnergyEM > 0 or CrossoverEnergyHadron > 0:
            particleParameterizations = GetHybridParameterizationList(
                ppcConverter,
                CrossoverEnergyEM=CrossoverEnergyEM,
                CrossoverEnergyHadron=CrossoverEnergyHadron)
        elif MMCTrackListName is None or MMCTrackListName == "":
            particleParameterizations = [
            ]  # make sure absolutely **no** parameterizations are used
        else:
            # use no parameterizations except for muons with lengths assigned to them
            # (those are assumed to have been generated by MMC)
            particleParameterizations = GetDefaultParameterizationList(
                ppcConverter, muonOnly=True)

    # flasher parameterizations
    if SimulateFlashers:
        # this needs a spectrum table in order to pass spectra to OpenCL
        spectrumTable = clsim.I3CLSimSpectrumTable()
        particleParameterizations += GetFlasherParameterizationList(
            spectrumTable)

        print("number of spectra (1x Cherenkov + Nx flasher):",
              len(spectrumTable))
    else:
        # no spectrum table is necessary when only using the Cherenkov spectrum
        spectrumTable = None

    openCLDevices = configureOpenCLDevices(
        UseGPUs=UseGPUs,
        UseCPUs=UseCPUs,
        OverrideApproximateNumberOfWorkItems=
        OverrideApproximateNumberOfWorkItems,
        DoNotParallelize=DoNotParallelize,
        UseOnlyDeviceNumber=UseOnlyDeviceNumber)

    if PhotonHistoryEntries == 0:
        module = 'I3CLSimModule<I3CompressedPhotonSeriesMap>'
    else:
        module = 'I3CLSimModule<I3PhotonSeriesMap>'
    tray.AddModule(
        module,
        name + "_clsim",
        MCTreeName=clSimMCTreeName,
        PhotonSeriesMapName=PhotonSeriesName,
        DOMRadius=DOMRadius,
        DOMOversizeFactor=DOMOversizeFactor,
        DOMPancakeFactor=
        DOMOversizeFactor,  # you will probably want this to be the same as DOMOversizeFactor
        RandomService=RandomService,
        MediumProperties=mediumProperties,
        SpectrumTable=spectrumTable,
        FlasherPulseSeriesName=clSimFlasherPulseSeriesName,
        OMKeyMaskName=clSimOMKeyMaskName,
        #IgnoreNonIceCubeOMNumbers=False,
        GenerateCherenkovPhotonsWithoutDispersion=False,
        WavelengthGenerationBias=wavelengthGenerationBias,
        ParameterizationList=particleParameterizations,
        MaxNumParallelEvents=ParallelEvents,
        TotalEnergyToProcess=TotalEnergyToProcess,
        OpenCLDeviceList=openCLDevices,
        #UseHardcodedDeepCoreSubdetector=False, # setting this to true saves GPU constant memory but will reduce performance
        StopDetectedPhotons=StopDetectedPhotons,
        PhotonHistoryEntries=PhotonHistoryEntries,
        IgnoreSubdetectors=IgnoreSubdetectors,
        If=If,
        **ExtraArgumentsToI3CLSimModule)
Ejemplo n.º 2
0
def I3CLSimTabulatePhotons(
        tray,
        name,
        UseCPUs=True,
        UseGPUs=False,
        UseOnlyDeviceNumber=None,
        MCTreeName="I3MCTree",
        OutputMCTreeName=None,
        FlasherInfoVectName=None,
        FlasherPulseSeriesName=None,
        MMCTrackListName="MMCTrackList",
        ParallelEvents=1000,
        RandomService=None,
        MediumProperties=expandvars("$I3_SRC/clsim/resources/ice/spice_mie"),
        UseGeant4=False,
        CrossoverEnergyEM=None,
        CrossoverEnergyHadron=None,
        UseCascadeExtension=False,
        DoNotParallelize=False,
        Area=None,
        WavelengthAcceptance=None,
        AngularAcceptance=None,
        UseHoleIceParameterization=True,
        OverrideApproximateNumberOfWorkItems=None,
        ExtraArgumentsToI3CLSimModule=dict(),
        If=lambda f: True):
    """Do standard clsim processing up to the I3Photon level.
    These photons still need to be converted to I3MCPEs to be usable
    for further steps in the standard IceCube MC processing chain.
    Reads its particles from an I3MCTree and writes an I3PhotonSeriesMap.

    All available OpenCL GPUs (and optionally CPUs) will
    be used. This will take over your entire machine,
    so make sure to configure your batch jobs correctly
    when using this on a cluster.
    When using nVidia cards, you can set the
    CUDA_VISIBLE_DEVICES environment variable to
    limit GPU visibility. A setting of
    CUDA_VISIBLE_DEVICES="0,3" would only use cards
    #0 and #3 and ignore cards #1 and #2. In case you are
    using a batch system, chances are this variable is already
    set. Unfortunately, there is no corresponding setting
    for the AMD driver.

    This segment assumes that MMC has been applied to the
    I3MCTree and that MMC was *NOT* run using the "-recc" option.

    :param UseCPUs:
        Turn this on to also use CPU-based devices.
        (This may potentially slow down photon generation, which
        is also done on the CPU in parallel.)
    :param UseGPUs:
        Turn this off to not use GPU-based devices.
        This may be useful if your GPU is used for display
        purposes and you don't want it to slow down.
    :param UseOnlyDeviceNumber:
        Use only a single device number, even if there is more than
        one device found matching the required description. The numbering
        starts at 0.
    :param MCTreeName:
        The name of the I3MCTree containing the particles to propagate.
    :param OutputMCTreeName:
        A copy of the (possibly sliced) MCTree will be stored as this name.
    :param FlasherInfoVectName:
        Set this to the name of I3FlasherInfoVect objects in the frame to
        enable flasher simulation. The module will read I3FlasherInfoVect objects
        and generate photons according to assumed parameterizations.
    :param FlasherPulseSeriesName:
        Set this to the name of an I3CLSimFlasherPulseSeries object in the frame to
        enable flasher/Standard Candle simulation.
        This cannot be used at the same time as FlasherInfoVectName.
        (I3CLSimFlasherPulseSeries objects are clsim's internal flasher
        representation, if "FlasherInfoVectName" is used, the I3FlasherInfoVect
        objects are converted to I3CLSimFlasherPulseSeries objects.)
    :param MMCTrackListName:
        Only used if *ChopMuons* is active. Set it to the name
        of the I3MMCTrackList object that contains additional
        muon energy loss information.
    :param ParallelEvents:
        clsim will work on a couple of events in parallel in order
        not to starve the GPU. Setting this too high will result
        in excessive memory usage (all your frames have to be cached
        in RAM). Setting it too low may impact simulation performance.
        The optimal value depends on your energy distribution/particle type.
    :param RandomService:
        Set this to an instance of a I3RandomService. Alternatively,
        you can specify the name of a configured I3RandomServiceFactory
        added to I3Tray using tray.AddService(). If you don't configure
        this, the default I3RandomServiceFactory will be used.
    :param MediumProperties:
        Set this either to a directory containing a PPC-compatible
        ice description (icemodel.dat, icemodel.par and cfg.txt) or
        to a photonics ice table file. PPC-compatible ice files should
        generally lead to faster execution times on GPUs since it involves
        less interpolation between table entries (the PPC ice-specification
        is parametric w.r.t. wavelength, whereas the photonics specification
        is not).
    :param Area:
        Geometric area of the sensor. If None, use the area of an IceCube DOM
    :param WavelengthAcceptance:
        Quantum efficiency of the sensor, relative to the geometric area. If
        None, use the IceCube DOM (standard QE)
    :param AngularAcceptance:
        Efficiency as a function of polar angle, relative to the geometric area.
        If None, use the IceCube angular efficiency, assuming hole ice.
    :param UseGeant4:
        Enabling this setting will disable all cascade and muon light yield
        parameterizations. All particles will sent to Geant4 for a full
        simulation. This does **not** apply to muons that do have a length
        assigned. These are assumed to have been generated by MMC and
        their light is generated according to the usual parameterization.
    :param CrossoverEnergyEM:
        If set it defines the crossover energy between full Geant4 simulations and 
        light yield parameterizations for electro magnetic cascades. This only works
        when UseGeant4 is set to true. It works in conjunction with CrossoverEnergyHadron.
        If one of both is set to a positiv value greater 0 (GeV), the hybrid simulation
        is used.
        If CrossoverEnergyEM is set to None while CrossoverEnergyHadron is set so
        hybrid mode is working, GEANT4 is used for EM cascades.
        If CrossoverEnergyEM is set to 0 (GeV) while CrossoverEnergyHadron is set
        so hybrid mode is working, leptons and EM cascades will use parameterizations
        for the whole energy range.
    :param CrossoverEnergyHadron:
        If set it defines the crossover energy between full Geant4 simulations and
        light yield parameterizations for hadronic cascades. This only works when
        UseGeant4 is set to true. It works in conjunction with CrossoverEnergyEM.
        If one of both is set to a positiv value greater 0 (GeV), the hybrid simulation
        is used.
        If CrossoverEnergyHadron is set to None while CrossoverEnergyEM is set so
        hybrid mode is working, GEANT4 is used for hadronic cascades.
        If CrossoverEnergyHadron is set to 0 (GeV) while CrossoverEnergyHadron is
        set so hybrid mode is working, hadronic cascades will use parameterizations
        for the whole energy range.
    :param UseCascadeExtension:
        If True, simulate the longitudinal development of cascades. Otherwise,
        simulate cascades as pointlike objects.
    :param DoNotParallelize:
        Try only using a single work item in parallel when running the
        OpenCL simulation. This might be useful if you want to run jobs
        in parallel on a batch system. This will only affect CPUs and
        will be a no-op for GPUs.
    :param OverrideApproximateNumberOfWorkItems:
        Allows to override the auto-detection for the maximum number of parallel work items.
        You should only change this if you know what you are doing.
    :param If:
        Python function to use as conditional execution test for segment modules.        
    """

    from icecube import icetray, dataclasses, phys_services, clsim

    # make sure the geometry is updated to the new granular format (in case it is supported)
    if hasattr(dataclasses, "I3ModuleGeo"):
        tray.AddModule("I3GeometryDecomposer",
                       name + "_decomposeGeometry",
                       If=lambda frame: If(frame) and
                       ("I3OMGeoMap" not in frame))

    if UseGeant4:
        if not clsim.I3CLSimLightSourceToStepConverterGeant4.can_use_geant4:
            raise RuntimeError(
                "You have requested to use Geant 4, but clsim was compiled without Geant 4 support"
            )

    # at the moment the Geant4 paths need to be set, even if it isn't used
    # TODO: fix this
    if clsim.I3CLSimLightSourceToStepConverterGeant4.can_use_geant4:
        AutoSetGeant4Environment()

    if MMCTrackListName is None or MMCTrackListName == "":
        # the input does not seem to have been processed by MMC
        ChopMuons = False
    else:
        ChopMuons = True

    if MCTreeName is None or MCTreeName == "":
        clSimMCTreeName = ""
        if ChopMuons:
            raise RuntimeError(
                "You cannot have \"MMCTrackListName\" enabled with no MCTree!")
    else:
        clSimMCTreeName = MCTreeName

    if FlasherInfoVectName is None or FlasherInfoVectName == "":
        if (FlasherPulseSeriesName
                is not None) and (FlasherPulseSeriesName != ""):
            SimulateFlashers = True
            clSimFlasherPulseSeriesName = FlasherPulseSeriesName
            clSimOMKeyMaskName = ""
        else:
            SimulateFlashers = False
            clSimFlasherPulseSeriesName = ""
            clSimOMKeyMaskName = ""
    else:
        if (FlasherPulseSeriesName
                is not None) and (FlasherPulseSeriesName != ""):
            raise RuntimeError(
                "You cannot use the FlasherPulseSeriesName and FlasherInfoVectName parameters at the same time!"
            )

        SimulateFlashers = True
        clSimFlasherPulseSeriesName = FlasherInfoVectName + "_pulses"
        clSimOMKeyMaskName = FlasherInfoVectName + "_OMKeys"

        tray.AddModule(clsim.FlasherInfoVectToFlasherPulseSeriesConverter,
                       name + "_FlasherInfoVectToFlasherPulseSeriesConverter",
                       FlasherInfoVectName=FlasherInfoVectName,
                       FlasherPulseSeriesName=clSimFlasherPulseSeriesName,
                       FlasherOMKeyVectName=clSimOMKeyMaskName,
                       If=If)

    # (optional) pre-processing
    if ChopMuons:
        if OutputMCTreeName is not None:
            clSimMCTreeName_new = OutputMCTreeName
        else:
            clSimMCTreeName_new = clSimMCTreeName + "_sliced"

        tray.AddModule("I3MuonSlicer",
                       name + "_chopMuons",
                       InputMCTreeName=clSimMCTreeName,
                       MMCTrackListName=MMCTrackListName,
                       OutputMCTreeName=clSimMCTreeName_new,
                       If=If)
        clSimMCTreeName = clSimMCTreeName_new
    else:
        if (OutputMCTreeName is not None) and (OutputMCTreeName != ""):
            # copy the MCTree to the requested output name
            def copyMCTree(frame, inputName, outputName, If_=None):
                if If_ is not None:
                    if not If_(frame): return
                frame[outputName] = frame[inputName]

            tray.AddModule(copyMCTree,
                           name + "_copyMCTree",
                           inputName=clSimMCTreeName,
                           outputName=OutputMCTreeName,
                           Streams=[icetray.I3Frame.DAQ],
                           If_=If)
            clSimMCTreeName = OutputMCTreeName
        else:
            clSimMCTreeName = clSimMCTreeName

    # some constants
    DOMRadius = 0.16510 * icetray.I3Units.m  # 13" diameter

    if Area is None:
        referenceArea = dataclasses.I3Constants.pi * DOMRadius**2
    else:
        referenceArea = Area
    if WavelengthAcceptance is None:
        domAcceptance = clsim.GetIceCubeDOMAcceptance(domRadius=DOMRadius)
    else:
        domAcceptance = WavelengthAcceptance
    if AngularAcceptance is None:
        angularAcceptance = clsim.GetIceCubeDOMAngularSensitivity(holeIce=True)
    else:
        angularAcceptance = AngularAcceptance

    # muon&cascade parameterizations
    ppcConverter = clsim.I3CLSimLightSourceToStepConverterPPC(
        photonsPerStep=200)
    ppcConverter.SetUseCascadeExtension(UseCascadeExtension)
    if not UseGeant4:
        particleParameterizations = GetDefaultParameterizationList(
            ppcConverter, muonOnly=False)
    else:
        if CrossoverEnergyEM > 0 or CrossoverEnergyHadron > 0:
            particleParameterizations = GetHybridParameterizationList(
                ppcConverter,
                CrossoverEnergyEM=CrossoverEnergyEM,
                CrossoverEnergyHadron=CrossoverEnergyHadron)
        elif MMCTrackListName is None or MMCTrackListName == "":
            particleParameterizations = [
            ]  # make sure absolutely **no** parameterizations are used
        else:
            # use no parameterizations except for muons with lengths assigned to them
            # (those are assumed to have been generated by MMC)
            particleParameterizations = GetDefaultParameterizationList(
                ppcConverter, muonOnly=True)

    # flasher parameterizations
    if SimulateFlashers:
        # this needs a spectrum table in order to pass spectra to OpenCL
        spectrumTable = clsim.I3CLSimSpectrumTable()
        particleParameterizations += GetFlasherParameterizationList(
            spectrumTable)

        icetray.logging.log_debug(
            "number of spectra (1x Cherenkov + Nx flasher): %d" %
            len(spectrumTable),
            unit="clsim")
    else:
        # no spectrum table is necessary when only using the Cherenkov spectrum
        spectrumTable = None

    openCLDevices = configureOpenCLDevices(
        UseGPUs=UseGPUs,
        UseCPUs=UseCPUs,
        OverrideApproximateNumberOfWorkItems=
        OverrideApproximateNumberOfWorkItems,
        DoNotParallelize=DoNotParallelize,
        UseOnlyDeviceNumber=UseOnlyDeviceNumber)

    tray.AddModule(
        "I3CLSimTabulatorModule",
        name + "_clsim",
        MCTreeName=clSimMCTreeName,
        RandomService=RandomService,
        MediumProperties=MediumProperties,
        SpectrumTable=spectrumTable,
        FlasherPulseSeriesName=clSimFlasherPulseSeriesName,
        Area=referenceArea,
        WavelengthAcceptance=domAcceptance,
        AngularAcceptance=angularAcceptance,
        ParameterizationList=particleParameterizations,
        # MaxNumParallelEvents=ParallelEvents,
        OpenCLDeviceList=openCLDevices,
        **ExtraArgumentsToI3CLSimModule)

    unpin_threads()
Ejemplo n.º 3
0
def TabulateRetroSources(
    tray,
    name,
    source_gcd_i3_md5,
    binning_kw,
    axes,
    ice_model,
    angular_sensitivity,
    disable_tilt,
    disable_anisotropy,
    hash_val,
    dom_spec,
    dom_x,
    dom_y,
    dom_z,
    dom_zenith,
    dom_azimuth,
    seed,
    n_events,
    tablepath,
    tile=None,
    record_errors=False,
):
    dom_x = dom_x * I3Units.m
    dom_y = dom_y * I3Units.m
    dom_z = dom_z * I3Units.m
    dom_zenith = dom_zenith * I3Units.rad
    dom_azimuth = dom_azimuth * I3Units.rad

    tablepath = expanduser(expandvars(tablepath))

    random_service = I3GSLRandomService(seed)

    tray.AddModule('I3InfiniteSource', name + 'streams', Stream=I3Frame.DAQ)

    tray.AddModule('I3MCEventHeaderGenerator',
                   name + 'gen_header',
                   Year=2009,
                   DAQTime=158100000000000000,
                   RunNumber=1,
                   EventID=1,
                   IncrementEventID=True)

    flasher_pulse_series_name = 'I3FlasherPulseSeriesMap'

    def reference_source(x, y, z, zenith, azimuth, scale):
        source = I3Particle()
        source.pos = I3Position(x, y, z)
        source.dir = I3Direction(zenith, azimuth)
        source.time = 0.0
        # Following are not used (at least not yet)
        source.type = I3Particle.ParticleType.EMinus
        source.energy = 1.0 * scale
        source.length = 0.0
        source.location_type = I3Particle.LocationType.InIce

        return source

    class MakeParticle(I3Module):
        def __init__(self, ctx):
            super(MakeParticle, self).__init__(ctx)
            self.AddOutBox('OutBox')
            self.AddParameter('source_function', '', lambda: None)
            self.AddParameter('n_events', '', 100)
            self.reference_source = None
            self.n_events = None
            self.emitted_events = None

        def Configure(self):
            self.reference_source = self.GetParameter('source_function')
            self.n_events = self.GetParameter('n_events')
            self.emitted_events = 0

        def DAQ(self, frame):
            pulseseries = I3CLSimFlasherPulseSeries()
            pulse = make_retro_pulse(
                x=dom_x,
                y=dom_y,
                z=dom_z,
                zenith=dom_zenith,
                azimuth=dom_azimuth,
            )
            pulseseries.append(pulse)
            frame[flasher_pulse_series_name] = pulseseries
            frame['ReferenceParticle'] = self.reference_source(
                x=dom_x,
                y=dom_y,
                z=dom_z,
                zenith=dom_zenith,
                azimuth=dom_azimuth,
                scale=1.0,
            )
            self.PushFrame(frame)
            self.emitted_events += 1
            if self.emitted_events >= self.n_events:
                self.RequestSuspension()

    tray.AddModule(
        MakeParticle,
        source_function=reference_source,
        n_events=n_events,
    )

    header = OrderedDict(FITSTable.empty_header)
    header['retro_dom_table'] = 0
    header['gcd_i3_md5_{:s}'.format(source_gcd_i3_md5)] = 0
    for n, axname in enumerate(binning_kw.keys()):
        header['ax_{:s}'.format(axname)] = n
        for key, val in binning_kw[axname].items():
            header['{}_{}'.format(axname, key)] = val
        if axname == 't':
            header['t_is_residual_time'] = 1
    header['ice_{:s}'.format(ice_model.replace('.', '_'))] = 0
    header['angsens_{:s}'.format(angular_sensitivity.replace('.', '_'))] = 0
    header['disable_tilt'] = disable_tilt
    header['disable_anisotropy'] = disable_anisotropy
    header['hash_{:s}'.format(hash_val)] = 0
    if tile is not None:
        header['tile'] = tile
    for key, value in dom_spec.items():
        header[key] = value
    header['dom_x'] = dom_x
    header['dom_y'] = dom_y
    header['dom_z'] = dom_z
    header['dom_zenith'] = dom_zenith
    header['dom_azimuth'] = dom_azimuth
    header['seed'] = seed
    header['n_events'] = n_events

    if hasattr(dataclasses, 'I3ModuleGeo'):
        tray.AddModule('I3GeometryDecomposer',
                       name + '_decomposeGeometry',
                       If=lambda frame: 'I3OMGeoMap' not in frame)

    # at the moment the Geant4 paths need to be set, even if it isn't used
    # TODO: fix this
    if I3CLSimLightSourceToStepConverterGeant4.can_use_geant4:
        AutoSetGeant4Environment()

    ppc_converter = I3CLSimLightSourceToStepConverterPPC(photonsPerStep=200)
    # Is this even necessary?
    ppc_converter.SetUseCascadeExtension(False)
    particle_parameterizations = GetDefaultParameterizationList(
        ppc_converter,
        muonOnly=False,
    )

    # need a spectrum table in order to pass spectra to OpenCL
    spectrum_table = I3CLSimSpectrumTable()
    particle_parameterizations += GetFlasherParameterizationList(
        spectrum_table)

    logging.log_debug(
        'number of spectra (1x Cherenkov + Nx flasher): %d' %
        len(spectrum_table),
        unit='clsim',
    )

    opencl_devices = configureOpenCLDevices(
        UseGPUs=False,
        UseCPUs=True,
        OverrideApproximateNumberOfWorkItems=None,
        DoNotParallelize=True,
        UseOnlyDeviceNumber=None,
    )

    medium_properties = parseIceModel(
        expandvars('$I3_SRC/ice-models/resources/models/' + ice_model),
        disableTilt=disable_tilt,
        disableAnisotropy=disable_anisotropy,
    )

    tray.AddModule(
        'I3CLSimTabulatorModule',
        name + '_clsim',
        MCTreeName='',  # doesn't apply since we use pulse series
        FlasherPulseSeriesName=flasher_pulse_series_name,
        RandomService=random_service,
        Area=DOM_SURFACE_AREA,
        WavelengthAcceptance=I3CLSimFunctionConstant(
            1.0),  #GetIceCubeDOMAcceptance(domRadius=DOM_RADIUS),
        AngularAcceptance=I3CLSimFunctionConstant(1.0),
        MediumProperties=medium_properties,
        ParameterizationList=particle_parameterizations,
        SpectrumTable=spectrum_table,
        OpenCLDeviceList=opencl_devices,
        PhotonsPerBunch=200,
        EntriesPerPhoton=5000,
        Filename=tablepath,
        RecordErrors=record_errors,
        TableHeader=header,
        Axes=axes,
        SensorNormalize=False)

    unpin_threads()