def _get_parameters(self): """ Get parameters from parfile """ # Initialise observation container if it is currently empty if self.obs().size() == 0: # If an observation definition file was provided then load it, # otherwise build a single observation with response information if self['inobs'].is_valid(): self.obs(gammalib.GObservations(self['inobs'].filename())) else: cta = gammalib.GCTAObservation() caldb = gammalib.GCaldb('cta', self['caldb'].string()) rsp = gammalib.GCTAResponseIrf(self['irf'].string(), caldb) cta.response(rsp) self.obs().append(cta) # Query input parameters self['emin'].real() self['emax'].real() self['aeffthres'].real() self['bkgthres'].real() # Query ahead output model filename if self._read_ahead(): self['outfile'].filename() # Write input parameters into logger self._log_parameters(gammalib.TERSE) # Return return
def _check_observation(self, ctbin, nevents, multiplier=1): """ Check content of an observation Parameters ---------- ctbin : `~ctools.ctbin` ctbin instance nevents : int Expected number of events multiplier : float, optional Observation multiplier """ # Test observation container obs = gammalib.GCTAObservation(ctbin.obs()[0]) pnt = obs.pointing() self.test_value(ctbin.obs().size(), 1, 'There is one observation') self.test_assert(obs.instrument() == 'CTA', 'Observation is CTA observation') self.test_value(obs.ontime(), 1800.0 * multiplier, 1.0e-6, 'Ontime is 1800 sec') self.test_value(obs.livetime(), 1710.0 * multiplier, 1.0e-6, 'Livetime is 1710 sec') self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, 'Pointing Right Ascension is 83.63 deg') self.test_value(pnt.dir().dec_deg(), 22.01, 1.0e-6, 'Pointing Declination is 22.01 deg') # Test event cube self._check_cube(obs.events(), nevents, multiplier=multiplier) # Return return
def _test_observation(self, ctobssim, nobs=1, pnts=[{'ra': 83.63, 'dec': 22.01}]): """ Test content of an observation Parameters ---------- ctobssim : `~ctools.ctobssim` ctobssim instance nobs : int Number of observations pnts : list of dict List of pointing dictionaries """ # Test observation container self.test_value(ctobssim.obs().size(), nobs, 'There is one observation') for i in range(ctobssim.obs().size()): obs = gammalib.GCTAObservation(ctobssim.obs()[i]) pnt = obs.pointing() self.test_assert(obs.instrument() == 'CTA', 'Observation is CTA observation') self.test_value(obs.ontime(), 1800.0, 1.0e-6, 'Ontime is 1800 sec') self.test_value(obs.livetime(), 1710.0, 1.0e-6, 'Livetime is 1710 sec') self.test_value(pnt.dir().ra_deg(), pnts[i]['ra'], 1.0e-6, 'Pointing Right Ascension is '+str(pnts[i]['ra'])+' deg') self.test_value(pnt.dir().dec_deg(), pnts[i]['dec'], 1.0e-6, 'Pointing Declination is '+str(pnts[i]['dec'])+' deg') # Return return
def _check_observation(self, ctbin, nevents, multiplier=1): """ Check content of an observation Parameters ---------- ctbin : `~ctools.ctbin` ctbin instance nevents : int Expected number of events multiplier : float, optional Ontime and livetime multiplier """ # Test observation container obs = gammalib.GCTAObservation(ctbin.obs()[0]) pnt = obs.pointing() self.test_value( ctbin.obs().size(), 1, 'Check that there is one observation on the observation container') self.test_value(obs.instrument(), 'CTA', 'Check that the one observation is a CTA observation') self.test_value(obs.ontime(), 300.0 * multiplier, 1.0e-6, 'Check ontime of observation') self.test_value(obs.livetime(), 294.0 * multiplier, 1.0e-6, 'Check livetime of observation') self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, 'Check pointing Right Ascension of observation') self.test_value(pnt.dir().dec_deg(), 22.51, 1.0e-6, 'Check pointing Declination of observation') # Test event cube self._check_cube(obs.events(), nevents, multiplier=multiplier) # Return return
def __init__(self, *argv): """ Constructor. """ # Set name and version self._name = 'csobs2caldb' self._version = '1.1.0' # Initialise members self._observation = gammalib.GCTAObservation() self._mission = 'cta' self._caldb = 'cta' self._outfile = gammalib.GFilename('irf_file.fits') self._base_dir = '' self._cal_dir = '' self._rsp_dir = '' self._caldb_inx = gammalib.GFits() self._irf_fits = gammalib.GFits() # Initialise observation container from constructor arguments. self._obs, argv = self._set_input_obs(argv) # Initialise script by calling the appropriate class constructor. self._init_cscript(argv) # Return return
def _masked_cube(self, cube, ra, dec, rad, emin='INDEF', emax='INDEF', regfile='NONE'): """ Mask an event cube and returns the masked cube Parameters ---------- cube : `~gammalib.GCTAEventCube` Event cube ra : float (str 'INDEF' for no selection on direction) Right Ascension (deg) dec : float (str 'INDEF' for no selection on direction) Declination (deg) rad : float (str 'INDEF' for no selection on direction) Radius (deg) emin : float (str 'INDEF' for no selection on energy) Minimum energy (TeV) emax : float (str 'INDEF' for no selection on energy) Maximum energy (TeV) Returns ------- cube : `~gammalib.GCTAEventCube` Event cube """ # Turn cube into observation container to feed to ctcubemask obs = gammalib.GCTAObservation() obs.events(cube) obs_cont = gammalib.GObservations() obs_cont.append(obs) # Use ctcubemask to mask event cube pixels cubemask = ctools.ctcubemask(obs_cont) cubemask['ra'] = ra cubemask['dec'] = dec cubemask['rad'] = rad cubemask['emin'] = emin cubemask['emax'] = emax cubemask['regfile'] = regfile cubemask.run() # Extract copy of cube from observation container (copy is needed to # avoid memory leaks in SWIG) cube = cubemask.obs()[0].events().copy() # Return cube return cube
def plot_phases(filename, plotfile, nphases=40): """ Plot phases Parameters ---------- filename : str Name of lightcurve FITS file plotfile : str Plot file name """ # Read observation container. If an exception occurs then try loading the # file as an event list and build an observation container. try: obs = gammalib.GObservations(filename) except: obs = gammalib.GObservations() obs.append(gammalib.GCTAObservation(filename)) # Initialise phases phases = [] # Loop over all observations for run in obs: # Get events events = run.events() # Loop over all events for event in events: # Get phase phase = event.phase() # Collect phase phases.append(phase) phases.append(phase + 1.0) # Plot phase plt.figure() plt.hist(phases, bins=nphases, range=(0.0, 2.0), facecolor='r') plt.xlabel('Phase') plt.ylabel('Events') # Show figure if len(plotfile) > 0: plt.savefig(plotfile) else: plt.show() # Return return
def selection_run(self, input_obs_list, output_obs_list, tmin=0, tmax=None, energy=[None, None], prefix='selected_', log_file='ctselect.log', force=False, save=False): if tmin < 0 or tmax < 1 or tmin >= tmax: raise Exception("ctselect needs better tmin/tmax") select = ctools.ctselect() if isinstance(input_obs_list, gammalib.GObservations): select.obs(input_obs_list) elif os.path.isfile(input_obs_list): # observations list from file select["inobs"] = input_obs_list else: raise Exception('Cannot understand input obs list for csphagen') select["rad"] = "INDEF" select["ra"] = "INDEF" select["dec"] = "INDEF" select["tmin"] = tmin select["tmax"] = tmax select["emin"] = energy[0] if energy[0] else "INDEF" select["emax"] = energy[1] if energy[1] else "INDEF" select["prefix"] = prefix select["outobs"] = output_obs_list select["logfile"] = log_file if force or not os.path.isfile(output_obs_list): select.logFileOpen() select.run() elif os.path.isfile(output_obs_list): basename, ext = os.path.splitext(output_obs_list) if ext == '.xml': select = ctools.ctselect( gammalib.GObservations(output_obs_list)) else: # .fits container = gammalib.GObservations() gcta_obs = gammalib.GCTAObservation(output_obs_list) container.append(gcta_obs) select.obs(container) else: raise Exception("Cannot proceed with ctselect") saved = False if (save and force) or (save and not os.path.isfile(output_obs_list)): select.save() # why it doesn't save anytime? saved = True logger.info("Files {} created.".format(output_obs_list)) return select
def createobs(ra=86.171648, dec=-1.4774586, rad=5.0, emin=0.1, emax=100.0, duration=360000.0, deadc=0.95, ): obs = gammalib.GCTAObservation() # Set pointing direction pntdir = gammalib.GSkyDir() pntdir.radec_deg(ra, dec) pnt = gammalib.GCTAPointing() pnt.dir(pntdir) obs.pointing(pnt) # Set ROI roi = gammalib.GCTARoi() instdir = gammalib.GCTAInstDir() instdir.dir(pntdir) roi.centre(instdir) roi.radius(rad) # Set GTI gti = gammalib.GGti() start = gammalib.GTime(0.0) stop = gammalib.GTime(duration) gti.append(start, stop) # Set energy boundaries ebounds = gammalib.GEbounds() e_min = gammalib.GEnergy() e_max = gammalib.GEnergy() e_min.TeV(emin) e_max.TeV(emax) ebounds.append(e_min, e_max) # Allocate event list events = gammalib.GCTAEventList() events.roi(roi) events.gti(gti) events.ebounds(ebounds) obs.events(events) # Set ontime, livetime, and deadtime correction factor obs.ontime(duration) obs.livetime(duration * deadc) obs.deadc(deadc) # Return observation return obs
def simulation_run(self, model_file, events_file, ra=None, dec=None, time=[0, 1800], energy=[None, None], log_file='ctobssim.log', force=False, save=False): sim = ctools.ctobssim() sim["inmodel"] = model_file sim["outevents"] = events_file sim["seed"] = self.seed sim["ra"] = ra if ra else self.ra sim["dec"] = dec if dec else self.dec sim["rad"] = self.rad sim["tmin"] = float(time[0]) sim["tmax"] = float(time[1]) sim["emin"] = energy[0] if energy[0] else self.energy_min sim["emax"] = energy[1] if energy[1] else self.energy_max sim["caldb"] = self.caldb sim["irf"] = self.irf sim["logfile"] = log_file sim["nthreads"] = self.nthreads if force or not os.path.isfile(events_file): sim.logFileOpen() sim.run() else: container = gammalib.GObservations() gcta_obs = gammalib.GCTAObservation(events_file) container.append(gcta_obs) sim.obs(container) sim.obs().models(gammalib.GModels(model_file)) saved = False if (save and force) or (save and not os.path.isfile(events_file)): sim.save() saved = True logger.info("Events file '{}' saved. time [{}-{}]".format( sim["outevents"].value(), time[0], time[1])) return sim
def __init__(self, *argv): """ Constructor """ # Initialise application by calling the appropriate class constructor self._init_csobservation(self.__class__.__name__, ctools.__version__, argv) # Initialise members self._observation = gammalib.GCTAObservation() self._mission = 'cta' self._caldb = 'cta' self._outfile = gammalib.GFilename('irf_file.fits') self._base_dir = '' self._cal_dir = '' self._rsp_dir = '' self._caldb_inx = gammalib.GFits() self._irf_fits = gammalib.GFits() # Return return
sim["outevents"] = filename sim["caldb"] = caldb_ sim["irf"] = irf_ sim["ra"] = ra sim["dec"] = dec sim["rad"] = rad sim["tmin"] = tstart sim["tmax"] = tstart + duration sim["emin"] = emin sim["emax"] = emax sim["debug"] = True sim.execute() #STORE THE OBSERVATION IN GObservations Class and store it in the .xml output file. #Allocate CTA observation obs = gammalib.GCTAObservation() #Set pointing direction pntdir = gammalib.GSkyDir() pntdir.radec_deg(ra, dec) pnt = gammalib.GCTAPointing() pnt.dir(pntdir) obs.pointing(pnt) #Set ROI roi = gammalib.GCTARoi() instdir = gammalib.GCTAInstDir() instdir.dir(pntdir) roi.centre(instdir) roi.radius(rad)
def _test_python(self): """ Test cslightcrv from Python """ # Set-up unbinned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = 51544.50 lcrv['tmax'] = 51544.53 lcrv['tbins'] = 3 lcrv['enumbins'] = 0 lcrv['emin'] = 0.1 lcrv['emax'] = 100.0 lcrv['outfile'] = 'cslightcrv_py1.fits' lcrv['logfile'] = 'cslightcrv_py1.log' lcrv['chatter'] = 2 # Run cslightcrv script and save light curve lcrv.logFileOpen() # Make sure we get a log file lcrv.run() lcrv.save() # Check light curve self._check_light_curve('cslightcrv_py1.fits', 3) # Now use FILE as time bin algorithm. For this we need first to # create an ASCII file. We use now 6 time bins. The ASCII file # is saved into the file "lightcurve_py2.dat". csv = gammalib.GCsv(2, 2) tmin = 51544.50 tdelta = 0.01 for i in range(csv.nrows()): csv[i, 0] = '%.5f' % (tmin + i * tdelta) csv[i, 1] = '%.5f' % (tmin + (i + 1) * tdelta) csv.save('cslightcrv_py2.dat', ' ', True) # Set-up unbinned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'FILE' lcrv['tbinfile'] = 'cslightcrv_py2.dat' lcrv['enumbins'] = 0 lcrv['emin'] = 0.1 lcrv['emax'] = 100.0 lcrv['fix_bkg'] = True lcrv['outfile'] = 'cslightcrv_py2.fits' lcrv['logfile'] = 'cslightcrv_py2.log' lcrv['chatter'] = 3 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py2.fits', 2) # Now we setup an observation container on input. We attached the # model to the observation container so that cslightcrv should # no longer query for the parameter. cta = gammalib.GCTAObservation(self._events) obs = gammalib.GObservations() obs.append(cta) obs.models(self._model) # Set-up unbinned cslightcrv from observation container. Now use # the GTI algorithm so that we test all timing algorithms. lcrv = cscripts.cslightcrv(obs) lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'GTI' lcrv['enumbins'] = 0 lcrv['emin'] = 0.1 lcrv['emax'] = 100.0 lcrv['outfile'] = 'cslightcrv_py3.fits' lcrv['logfile'] = 'cslightcrv_py3.log' lcrv['chatter'] = 4 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py3.fits', 1) # Finally we set-up a binned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = 51544.50 lcrv['tmax'] = 51544.53 lcrv['tbins'] = 2 lcrv['emin'] = 0.1 lcrv['emax'] = 100.0 lcrv['enumbins'] = 10 lcrv['coordsys'] = 'CEL' lcrv['proj'] = 'TAN' lcrv['xref'] = 83.63 lcrv['yref'] = 22.01 lcrv['nxpix'] = 20 lcrv['nypix'] = 20 lcrv['binsz'] = 0.02 lcrv['outfile'] = 'cslightcrv_py4.fits' lcrv['logfile'] = 'cslightcrv_py4.log' lcrv['chatter'] = 4 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py4.fits', 2) # Return return
def _test_python(self): """ Test ctbin from Python """ # Set-up ctbin bin = ctools.ctbin() bin['inobs'] = self._events bin['outcube'] = 'cntmap.fits' bin['ebinalg'] = 'LOG' bin['emin'] = 0.1 bin['emax'] = 100.0 bin['enumbins'] = 20 bin['nxpix'] = 200 bin['nypix'] = 200 bin['binsz'] = 0.02 bin['coordsys'] = 'CEL' bin['proj'] = 'CAR' bin['xref'] = 83.63 bin['yref'] = 22.01 bin['logfile'] = 'ctbin_py1.log' bin['chatter'] = 2 # Run ctbin tool #bin.logFileOpen() # Make sure we get a log file, but this leads # to a segmentation fault on Linux, e.g. CentOS 6. # see issue #1823 (need to fix that) bin.run() # Check content of observation and cube self._check_observation(bin, 5542) self._check_cube(bin.cube(), 5542) # Test copy constructor cpy_bin = bin.copy() # Check content of observation and cube self._check_observation(cpy_bin, 5542) self._check_cube(cpy_bin.cube(), 5542) # Run copy of ctbin tool again cpy_bin['logfile'] = 'ctbin_py2.log' cpy_bin['chatter'] = 3 cpy_bin.run() # Check content of observation and cube. We expect now an empty # event cube as on input the observation is binned, and any binned # observation will be skipped, hence the counts cube should be # empty. self._check_observation(cpy_bin, 0) self._check_cube(cpy_bin.cube(), 0) # Save counts cube bin.save() # Load counts cube and check content. evt = gammalib.GCTAEventCube('cntmap.fits') self._check_cube(evt, 5542) # Prepare observation container for stacked analysis cta = gammalib.GCTAObservation(self._events) obs = gammalib.GObservations() cta.id('0001') obs.append(cta) cta.id('0002') obs.append(cta) cta.id('0003') obs.append(cta) # Set-up ctbin using an observation container bin = ctools.ctbin(obs) bin['outcube'] = 'cntmap.fits' bin['ebinalg'] = 'LOG' bin['emin'] = 0.1 bin['emax'] = 100.0 bin['enumbins'] = 20 bin['nxpix'] = 200 bin['nypix'] = 200 bin['binsz'] = 0.02 bin['coordsys'] = 'CEL' bin['proj'] = 'CAR' bin['xref'] = 83.63 bin['yref'] = 22.01 bin['logfile'] = 'ctbin_py3.log' bin['chatter'] = 4 # Run ctbin tool bin.logFileOpen() # Make sure we get a log file bin.run() # Check content of observation and cube (need multiplier=3 since # three identical observations have been appended) self._check_observation(bin, 5542, multiplier=3) self._check_cube(bin.cube(), 5542, multiplier=3) # Set-up ctbin using an observation container bin = ctools.ctbin(obs) bin['outcube'] = 'cntmap2.fits' bin['ebinalg'] = 'LOG' bin['emin'] = 0.1 bin['emax'] = 100.0 bin['enumbins'] = 20 bin['nxpix'] = 200 bin['nypix'] = 200 bin['binsz'] = 0.02 bin['coordsys'] = 'CEL' bin['proj'] = 'CAR' bin['xref'] = 83.63 bin['yref'] = 22.01 bin['logfile'] = 'ctbin_py4.log' bin['chatter'] = 4 # Execute ctbin tool bin.execute() # Load counts cube and check content. evt = gammalib.GCTAEventCube('cntmap2.fits') self._check_cube(evt, 5542, multiplier=3) # Return return
def run(self): """ Run the script. Raises ------ RuntimeError Invalid pointing definition file format. """ # Switch screen logging on in debug mode if self._logDebug(): self._log.cout(True) # Get parameters self._get_parameters() # Write header into logger if self._logTerse(): self._log('\n') self._log.header1('Creating observation definition XML file') # Load pointing definition file if it is not already set if self._pntdef.size() == 0: self._pntdef = gammalib.GCsv(self['inpnt'].filename(), ',') ncols = self._pntdef.ncols() npnt = self._pntdef.nrows()-1 # Throw an exception is there is no header information if self._pntdef.nrows() < 1: raise RuntimeError('No header found in pointing definition file.') # Clear observation container self._obs.clear() identifier = 1 # Extract header from pointing definition file header = [] for col in range(ncols): header.append(self._pntdef[0,col]) # Loop over all pointings for pnt in range(npnt): # Set row index row = pnt + 1 # Create CTA observation obs = gammalib.GCTAObservation() # Set observation name if 'name' in header: name = self._pntdef[row, header.index('name')] else: name = 'None' obs.name(name) # Set identifier if 'id' in header: id_ = self._pntdef[row, header.index('id')] else: id_ = '%6.6d' % identifier identifier += 1 obs.id(id_) # Set pointing if 'ra' in header and 'dec' in header: ra = float(self._pntdef[row, header.index('ra')]) dec = float(self._pntdef[row, header.index('dec')]) pntdir = gammalib.GSkyDir() pntdir.radec_deg(ra,dec) elif 'lon' in header and 'lat' in header: lon = float(self._pntdef[row, header.index('lon')]) lat = float(self._pntdef[row, header.index('lat')]) pntdir = gammalib.GSkyDir() pntdir.lb_deg(lon,lat) else: raise RuntimeError('No (ra,dec) or (lon,lat) columns ' 'found in pointing definition file.') obs.pointing(gammalib.GCTAPointing(pntdir)) # Set response function if 'caldb' in header: caldb = self._pntdef[row, header.index('caldb')] else: caldb = self['caldb'].string() if 'irf' in header: irf = self._pntdef[row, header.index('irf')] else: irf = self['irf'].string() if caldb != '' and irf != '': obs = self._set_response(obs, caldb, irf) # Set deadtime correction factor if 'deadc' in header: deadc = float(self._pntdef[row, header.index('deadc')]) else: deadc = self['deadc'].real() obs.deadc(deadc) # Set Good Time Interval if 'duration' in header: duration = float(self._pntdef[row, header.index('duration')]) else: duration = self['duration'].real() tmin = self._tmin tmax = self._tmin + duration gti = gammalib.GGti(self._time_reference()) tstart = gammalib.GTime(tmin, self._time_reference()) tstop = gammalib.GTime(tmax, self._time_reference()) self._tmin = tmax gti.append(tstart, tstop) obs.ontime(gti.ontime()) obs.livetime(gti.ontime()*deadc) # Set Energy Boundaries has_emin = False has_emax = False if 'emin' in header: emin = float(self._pntdef[row, header.index('emin')]) has_emin = True else: if self['emin'].is_valid(): emin = self['emin'].real() has_emin = True if 'emax' in header: emax = float(self._pntdef[row, header.index('emax')]) has_emax = True else: if self['emax'].is_valid(): emax = self['emax'].real() has_emax = True has_ebounds = has_emin and has_emax if has_ebounds: ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, 'TeV'), gammalib.GEnergy(emax, 'TeV')) # Set ROI has_roi = False if 'rad' in header: rad = float(self._pntdef[row, header.index('rad')]) has_roi = True else: if self['rad'].is_valid(): rad = self['rad'].real() has_roi = True if has_roi: roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad) # Create an empty event list list_ = gammalib.GCTAEventList() list_.gti(gti) # Set optional information if has_ebounds: list_.ebounds(ebounds) if has_roi: list_.roi(roi) # Attach event list to CTA observation obs.events(list_) # Write observation into logger if self._logExplicit(): self._log(str(obs)) self._log('\n') elif self._logTerse(): self._log(gammalib.parformat(obs.instrument()+' observation')) self._log('Name="'+obs.name()+'" ') self._log('ID="'+obs.id()+'"\n') # Append observation self._obs.append(obs) # Return return
def _test_python(self): """ Test cspull from Python """ # Set-up unbinned cspull pull = cscripts.cspull() pull['inmodel'] = self._model pull['outfile'] = 'cspull_py1.dat' pull['ntrials'] = 3 pull['caldb'] = self._caldb pull['irf'] = self._irf pull['ra'] = 83.6331 pull['dec'] = 22.0145 pull['emin'] = 0.1 pull['emax'] = 100.0 pull['enumbins'] = 0 pull['tmax'] = 1800.0 pull['deadc'] = 0.95 pull['rad'] = 5.0 pull['logfile'] = 'cspull_py1.log' pull['chatter'] = 2 # Run cspull script pull.logFileOpen() # Make sure we get a log file pull.run() #pull.save() # Check pull distribution file self._check_pull_file('cspull_py1.dat') # Set-up binned cspull pull = cscripts.cspull() pull['inmodel'] = self._model pull['outfile'] = 'cspull_py2.dat' pull['ntrials'] = 3 pull['caldb'] = self._caldb pull['irf'] = self._irf pull['ra'] = 83.6331 pull['dec'] = 22.0145 pull['emin'] = 0.1 pull['emax'] = 100.0 pull['enumbins'] = 10 pull['tmax'] = 1800.0 pull['deadc'] = 0.95 pull['rad'] = 5.0 pull['npix'] = 100 pull['binsz'] = 0.02 pull['coordsys'] = 'CEL' pull['proj'] = 'TAN' pull['logfile'] = 'cspull_py2.log' pull['chatter'] = 3 # Execute cspull script pull.execute() # Check pull distribution file self._check_pull_file('cspull_py2.dat') # Set-up cspull from event list pull = cscripts.cspull() pull['inobs'] = self._events pull['inmodel'] = self._model pull['outfile'] = 'cspull_py3.dat' pull['ntrials'] = 3 pull['caldb'] = self._caldb pull['irf'] = self._irf pull['enumbins'] = 0 pull['logfile'] = 'cspull_py3.log' pull['chatter'] = 4 # Execute cspull script pull.execute() # Check pull distribution file self._check_pull_file('cspull_py3.dat') # Build observation container with unbinned observation cta = gammalib.GCTAObservation(self._events) obs = gammalib.GObservations() obs.append(cta) # Set-up cspull from observation container with unbinned observation pull = cscripts.cspull(obs) pull['inmodel'] = self._model pull['outfile'] = 'cspull_py4.dat' pull['ntrials'] = 3 pull['caldb'] = self._caldb pull['irf'] = self._irf pull['enumbins'] = 0 pull['logfile'] = 'cspull_py4.log' pull['chatter'] = 4 # Execute cspull script pull.execute() # Check pull distribution file self._check_pull_file('cspull_py4.dat') # Set-up stacked cspull with one observation where response cubes # are specified in the input observation pull = cscripts.cspull() pull['inobs'] = self._inobs pull['inmodel'] = self._stacked_model pull['outfile'] = 'cspull_py5.dat' pull['ntrials'] = 3 pull['enumbins'] = 0 pull['logfile'] = 'cspull_py5.log' pull['chatter'] = 4 # Execute cspull script pull.execute() # Check pull distribution file self._check_pull_file('cspull_py5.dat') # Set-up stacked cspull with two observations from which response # cubes will be computed internally. The IRFs are also specified # in the input observation, hence we do not need to query the IRF # parameters. pull = cscripts.cspull() pull['inobs'] = self._inobs_two pull['inmodel'] = self._model pull['outfile'] = 'cspull_py6.dat' pull['ntrials'] = 3 pull['emin'] = 0.02 pull['emax'] = 100.0 pull['enumbins'] = 10 pull['npix'] = 20 pull['binsz'] = 0.2 pull['coordsys'] = 'CEL' pull['proj'] = 'TAN' pull['logfile'] = 'cspull_py6.log' pull['chatter'] = 4 # Execute cspull script pull.execute() # Check pull distribution file self._check_pull_file('cspull_py6.dat') # Return return
import gammalib as g import sys import numpy as np gobs = g.GCTAObservation() caldb = g.GCaldb('cta', 'prod3b') irf = 'South_z20_average_30m' gobs.response(irf, caldb) rsp = gobs.response() # print(rsp) """ === GCTAResponseIrf === Caldb mission .............: cta Caldb instrument ..........: prod3b Response name .............: South_z20_average_30m Energy dispersion .........: Not used Safe energy range .........: undefined === GCaldb === Database root .............: /home/sim/anaconda3/envs/cta_pipe/share/caldb Selected Mission ..........: CTA Selected Instrument .......: PROD3B Calibration Index File ....: /home/sim/anaconda3/envs/cta_pipe/share/caldb/data/cta/prod3b/caldb.indx Number of entries .........: 144 === GCTAAeff2D === Filename ..................: /home/sim/anaconda3/envs/cta_pipe/share/caldb/data/cta/prod3b-v1/bcf/South_z20_average_30m/irf_file.fits Number of energy bins .....: 42 Number of offset bins .....: 6 Log10(Energy) range .......: 0.0125892544165254 - 199.526229858398 TeV Offset angle range ........: 0 - 6 deg Lower energy threshold ....: not specified
def _test_python(self): """ Test csbkgmodel from Python """ # Set-up csbkgmodel bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'GAUSS' bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py1.xml' bkgmodel['logfile'] = 'csbkgmodel_py1.log' # Run csbkgmodel script and save background model bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.run() bkgmodel.save() # Check background model self._check_bkg_model('csbkgmodel_py1.xml') # Now test without gradient, power law and not runwise bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'GAUSS' bkgmodel['gradient'] = False bkgmodel['spectral'] = 'PLAW' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = False bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 3 bkgmodel['outmodel'] = 'csbkgmodel_py2.xml' bkgmodel['logfile'] = 'csbkgmodel_py2.log' # Execute csbkgmodel script bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py2.xml') # Now test AEFF model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'AEFF' bkgmodel['gradient'] = False bkgmodel['spectral'] = 'PLAW' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = False bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 4 bkgmodel['outmodel'] = 'csbkgmodel_py3.xml' bkgmodel['logfile'] = 'csbkgmodel_py3.log' # Execute csbkgmodel script bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py3.xml') # Now test IRF model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'IRF' bkgmodel['gradient'] = False bkgmodel['spectral'] = 'PLAW' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = False bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 4 bkgmodel['outmodel'] = 'csbkgmodel_py4.xml' bkgmodel['logfile'] = 'csbkgmodel_py4.log' # Execute csbkgmodel script bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py4.xml') # Test with multiple input observations obs = gammalib.GObservations() for s, events in enumerate([self._myevents1, self._myevents2]): run = gammalib.GCTAObservation(events) run.id(str(s + 1)) run.response(self._irf, gammalib.GCaldb('cta', self._caldb)) obs.append(run) # Set-up csbkgmodel bkgmodel = cscripts.csbkgmodel(obs) bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'GAUSS' bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'POW' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['ebingamma'] = 1.1 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py5.xml' bkgmodel['logfile'] = 'csbkgmodel_py5.log' # Execute csbkgmodel script bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py5.xml', nmodels=2) # Test GAUSS(E) spatial model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'GAUSS(E)' bkgmodel['snumbins'] = 2 bkgmodel['smin'] = 1.0 bkgmodel['smax'] = 10.0 bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py6.xml' bkgmodel['logfile'] = 'csbkgmodel_py6.log' # Run csbkgmodel script and save background model bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py6.xml') # Test LOOKUP spatial model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'LOOKUP' bkgmodel['slufile'] = self._lookup bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py7.xml' bkgmodel['logfile'] = 'csbkgmodel_py7.log' # Run csbkgmodel script and save background model bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py7.xml') # Test PROFILE spatial model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'PROFILE' bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py8.xml' bkgmodel['logfile'] = 'csbkgmodel_py8.log' # Run csbkgmodel script and save background model bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py8.xml') # Test POLYNOM spatial model bkgmodel = cscripts.csbkgmodel() bkgmodel['inobs'] = self._events bkgmodel['caldb'] = self._caldb bkgmodel['irf'] = self._irf bkgmodel['instrument'] = 'CTA' bkgmodel['spatial'] = 'POLYNOM' bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'LOG' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py9.xml' bkgmodel['logfile'] = 'csbkgmodel_py9.log' # Run csbkgmodel script and save background model bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py9.xml') # Check with data from multiple instruments new_inst = 'INST2' obs_multi_inst = gammalib.GObservations(obs) for run in obs_multi_inst: run.instrument(new_inst) obs_multi_inst.extend(obs) # Set-up csbkgmodel bkgmodel = cscripts.csbkgmodel(obs_multi_inst) bkgmodel['instrument'] = new_inst bkgmodel['spatial'] = 'GAUSS' bkgmodel['gradient'] = True bkgmodel['spectral'] = 'NODES' bkgmodel['ebinalg'] = 'POW' bkgmodel['emin'] = 1.0 bkgmodel['emax'] = 100.0 bkgmodel['enumbins'] = 8 bkgmodel['ebingamma'] = 1.1 bkgmodel['runwise'] = True bkgmodel['rad'] = 2.0 bkgmodel['chatter'] = 2 bkgmodel['outmodel'] = 'csbkgmodel_py10.xml' bkgmodel['logfile'] = 'csbkgmodel_py10.log' # Execute csbkgmodel script bkgmodel.logFileOpen() # Make sure we get a log file bkgmodel.execute() # Check background model self._check_bkg_model('csbkgmodel_py10.xml', nmodels=obs.size()) # Return return
def prepare(obsname, bkgname, rad=2.0, emin=0.3, emax=50.0, ebins=20): """ Prepare events for analysis Parameters ---------- obsname : str Observation definition XML file bkgname : str Background model definition XML file rad : float, optional Selection radius (degrees) emin : float, optional Minimum energy for analysis (TeV) emax : float, optional Maximum energy for analysis (TeV) ebins : int, optional Number of energy bins """ # Set filenames cntcube = 'rx_stacked%2.2d_cntcube.fits' % ebins expcube = 'rx_stacked%2.2d_expcube.fits' % ebins psfcube = 'rx_stacked%2.2d_psfcube.fits' % ebins edispcube = 'rx_stacked%2.2d_edispcube.fits' % ebins bkgcube = 'rx_stacked%2.2d_bkgcube.fits' % ebins obsname_binned = add_attribute(obsname, '_binned%2.2d' % ebins) bkgname_stacked = add_attribute(bkgname, '_stacked') obsname_stacked = add_attribute(obsname, '_stacked%2.2d' % ebins) obsname_stacked_edisp = add_attribute(obsname_stacked, '_edisp') # Generate background lookup generate_background_lookup() # Continue only if selected events do not exist if not os.path.isfile(obsname): # Setup task parameters select = ctools.ctselect() select['inobs'] = '$HESSDATA/obs/obs_rx.xml' select['outobs'] = obsname select['ra'] = 'UNDEF' select['dec'] = 'UNDEF' select['rad'] = rad select['tmin'] = 'UNDEF' select['tmax'] = 'UNDEF' select['emin'] = emin select['emax'] = emax select['usethres'] = 'DEFAULT' select['logfile'] = 'rx_hess_select_events.log' select.logFileOpen() # Select events select.execute() # Continue only if background model does not exist if not os.path.isfile(bkgname): # Setup task parameters bkg = cscripts.csbkgmodel() bkg['inobs'] = '$HESSDATA/obs/obs_rx.xml' bkg['outmodel'] = bkgname bkg['instrument'] = 'HESS' bkg['spatial'] = 'LOOKUP' bkg['slufile'] = 'off_lookup.fits' bkg['gradient'] = True bkg['spectral'] = 'NODES' bkg['ebinalg'] = 'LOG' bkg['emin'] = emin bkg['emax'] = 30.0 bkg['enumbins'] = 8 bkg['runwise'] = True bkg['rad'] = rad bkg['logfile'] = 'rx_hess_create_background.log' bkg.logFileOpen() # Generate background model bkg.execute() # Continue only if counts cube does not exist if not os.path.isfile(cntcube): # Setup task parameters ctbin = ctools.ctbin() ctbin['inobs'] = obsname ctbin['outobs'] = cntcube ctbin['ebinalg'] = 'LOG' ctbin['emin'] = emin ctbin['emax'] = emax ctbin['enumbins'] = ebins ctbin['coordsys'] = 'CEL' ctbin['proj'] = 'TAN' ctbin['xref'] = 258.1125 ctbin['yref'] = -39.6867 ctbin['nxpix'] = 300 ctbin['nypix'] = 300 ctbin['binsz'] = 0.02 ctbin['logfile'] = 'rx_hess_create_cntcube.log' ctbin.logFileOpen() # Generate counts cube ctbin.execute() # Continue only if counts cubes for binned analysis do not exist if not os.path.isfile(obsname_binned): # Setup task parameters ctbin = ctools.ctbin() ctbin['inobs'] = obsname ctbin['outobs'] = obsname_binned ctbin['stack'] = False ctbin['usepnt'] = True ctbin['ebinalg'] = 'LOG' ctbin['emin'] = emin ctbin['emax'] = emax ctbin['enumbins'] = ebins ctbin['coordsys'] = 'CEL' ctbin['proj'] = 'TAN' ctbin['nxpix'] = 200 ctbin['nypix'] = 200 ctbin['binsz'] = 0.02 ctbin['logfile'] = 'rx_hess_create_cntcube_binned.log' ctbin.logFileOpen() # Generate counts cubes ctbin.execute() # Continue only if exposure cube does not exist if not os.path.isfile(expcube): # Setup task parameters ctexpcube = ctools.ctexpcube() ctexpcube['inobs'] = obsname ctexpcube['incube'] = 'NONE' ctexpcube['ebinalg'] = 'LOG' ctexpcube['emin'] = 0.1 # Full energy range ctexpcube['emax'] = 100.0 # Full energy range ctexpcube['enumbins'] = 300 # Factor ~3 oversampling of IRF ctexpcube['coordsys'] = 'CEL' ctexpcube['proj'] = 'TAN' ctexpcube['xref'] = 258.1125 ctexpcube['yref'] = -39.6867 ctexpcube['nxpix'] = 300 ctexpcube['nypix'] = 300 ctexpcube['binsz'] = 0.02 ctexpcube['outcube'] = expcube ctexpcube['logfile'] = 'rx_hess_create_expcube.log' ctexpcube.logFileOpen() # Generate exposure cube ctexpcube.execute() # Continue only if PSF cube does not exist if not os.path.isfile(psfcube): # Setup task parameters ctpsfcube = ctools.ctpsfcube() ctpsfcube['inobs'] = obsname ctpsfcube['incube'] = 'NONE' ctpsfcube['ebinalg'] = 'LOG' ctpsfcube['emin'] = 0.1 # Full energy range ctpsfcube['emax'] = 100.0 # Full energy range ctpsfcube['enumbins'] = 300 # Factor ~3 oversampling of IRF ctpsfcube['coordsys'] = 'CEL' ctpsfcube['proj'] = 'TAN' ctpsfcube['xref'] = 258.1125 ctpsfcube['yref'] = -39.6867 ctpsfcube['nxpix'] = 30 ctpsfcube['nypix'] = 30 ctpsfcube['binsz'] = 0.2 ctpsfcube['amax'] = 0.7 # Full H.E.S.S. PSF range ctpsfcube['anumbins'] = 300 # Factor ~2 oversampling of IRF ctpsfcube['outcube'] = psfcube ctpsfcube['logfile'] = 'rx_hess_create_psfcube.log' ctpsfcube.logFileOpen() # Generate PSF cube ctpsfcube.execute() # Continue only if energy dispersion cube does not exist if not os.path.isfile(edispcube): # Setup task parameters ctedispcube = ctools.ctedispcube() ctedispcube['inobs'] = obsname ctedispcube['incube'] = 'NONE' ctedispcube['ebinalg'] = 'LOG' ctedispcube['emin'] = 0.1 # Full energy range ctedispcube['emax'] = 100.0 # Full energy range ctedispcube['enumbins'] = 300 # Factor ~3 oversampling of IRF ctedispcube['coordsys'] = 'CEL' ctedispcube['proj'] = 'TAN' ctedispcube['xref'] = 258.1125 ctedispcube['yref'] = -39.6867 ctedispcube['nxpix'] = 30 ctedispcube['nypix'] = 30 ctedispcube['binsz'] = 0.2 ctedispcube['migramax'] = 5.0 ctedispcube['migrabins'] = 300 ctedispcube['outcube'] = edispcube ctedispcube['logfile'] = 'rx_hess_create_edispcube.log' ctedispcube.logFileOpen() # Generate energy dispersion cube ctedispcube.execute() # Continue only if background cube does not exist if not os.path.isfile(bkgcube): # Setup task parameters ctbkgcube = ctools.ctbkgcube() ctbkgcube['inobs'] = obsname ctbkgcube['incube'] = cntcube ctbkgcube['inmodel'] = bkgname ctbkgcube['outcube'] = bkgcube ctbkgcube['outmodel'] = bkgname_stacked ctbkgcube['logfile'] = 'rx_hess_create_bkgcube.log' ctbkgcube.logFileOpen() # Generate background cube ctbkgcube.execute() # Continue only if stacked observation definition XML file does not # exist if not os.path.isfile(obsname_stacked): # Build stacked observation run = gammalib.GCTAObservation(cntcube, expcube, psfcube, bkgcube) run.name('RX J1713.7-3946') run.instrument('HESS') # Append to observation container obs = gammalib.GObservations() obs.append(run) # Save observation container obs.save(obsname_stacked) # Continue only if stacked observation definition XML file with energy # energy dispersion enabled does not exist if not os.path.isfile(obsname_stacked_edisp): # Build stacked observation run = gammalib.GCTAObservation(cntcube, expcube, psfcube, edispcube, bkgcube) run.name('RX J1713.7-3946') run.instrument('HESS') # Append to observation container obs = gammalib.GObservations() obs.append(run) # Save observation container obs.save(obsname_stacked_edisp) # Continue only if stacked model definition XML file does exist if os.path.isfile(bkgname_stacked): # Load model definition XML files joint = gammalib.GModels(bkgname) stacked = gammalib.GModels(bkgname_stacked) # Get spectral component of joint file and remplace it as spectral # component of stacked file spectrum = joint[0].spectral() for i in range(spectrum.nodes()): spectrum.intensity(i, 1.0) spectrum.autoscale() stacked[0].spectral(spectrum) # Save stacked model stacked.save(bkgname_stacked) # Return return
def _test_python(self): """ Test csphagen from Python """ # Same test as from command line phagen = cscripts.csphagen() phagen['inobs'] = self._myevents1 phagen['inmodel'] = 'NONE' phagen['caldb'] = self._caldb phagen['irf'] = self._irf phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['coordsys'] = 'CEL' phagen['ra'] = 83.633 phagen['dec'] = 22.0145 phagen['rad'] = 0.2 phagen['stack'] = False phagen['inexclusion'] = self._exclusion phagen['bkgmethod'] = 'REFLECTED' phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['outobs'] = 'csphagen_py1_obs.xml' phagen['outmodel'] = 'csphagen_py1_model.xml' phagen['prefix'] = 'csphagen_py1' phagen['logfile'] = 'csphagen_py1.log' phagen['chatter'] = 1 # Execute script phagen.execute() # Check output self._check_output('csphagen_py1', self._nbins, self._nreg_with_excl) self._check_outobs('csphagen_py1_obs.xml', 1) # Now test without exclusion region phagen = cscripts.csphagen() phagen['inobs'] = self._myevents1 phagen['inmodel'] = 'NONE' phagen['caldb'] = self._caldb phagen['irf'] = self._irf phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['coordsys'] = 'CEL' phagen['ra'] = 83.633 phagen['dec'] = 22.0145 phagen['rad'] = 0.2 phagen['stack'] = False phagen['bkgmethod'] = 'REFLECTED' phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['outobs'] = 'csphagen_py2_obs.xml' phagen['outmodel'] = 'csphagen_py2_model.xml' phagen['prefix'] = 'csphagen_py2' phagen['logfile'] = 'csphagen_py2.log' phagen['chatter'] = 2 # Execute script phagen.execute() # Check output self._check_output('csphagen_py2', self._nbins, self._nreg_wo_excl) self._check_outobs('csphagen_py2_obs.xml', 1) # Test with multiple input observations, no stacking # Create observation container obs = gammalib.GObservations() for s, events in enumerate([self._myevents1, self._myevents2]): run = gammalib.GCTAObservation(events) run.id(str(s + 1)) run.response(self._irf, gammalib.GCaldb('cta', self._caldb)) obs.append(run) # Setup csphagen phagen = cscripts.csphagen(obs) phagen['inmodel'] = 'NONE' phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['coordsys'] = 'CEL' phagen['ra'] = 83.633 phagen['dec'] = 22.0145 phagen['rad'] = 0.2 phagen['stack'] = False phagen['inexclusion'] = self._exclusion phagen['bkgmethod'] = 'REFLECTED' phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['outobs'] = 'csphagen_py3_obs.xml' phagen['outmodel'] = 'csphagen_py3_model.xml' phagen['prefix'] = 'csphagen_py3' phagen['logfile'] = 'csphagen_py3.log' phagen['chatter'] = 3 # Run script phagen.execute() # Check output for s in range(2): self._check_output('csphagen_py3_' + str(s + 1), self._nbins, self._nreg_mul[s]) self._check_outobs('csphagen_py3_obs.xml', 2) # Setup csphagen for test with multiple input observations and stacking phagen = cscripts.csphagen(obs) phagen['inmodel'] = 'NONE' phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['coordsys'] = 'CEL' phagen['ra'] = 83.633 phagen['dec'] = 22.0145 phagen['rad'] = 0.2 phagen['stack'] = True phagen['inexclusion'] = self._exclusion phagen['bkgmethod'] = 'REFLECTED' phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['outobs'] = 'csphagen_py4_obs.xml' phagen['outmodel'] = 'csphagen_py4_model.xml' phagen['prefix'] = 'csphagen_py4' phagen['logfile'] = 'csphagen_py4.log' phagen['chatter'] = 4 # Execute script phagen.execute() # Check output for s in range(2): self._check_output('csphagen_py4_stacked', self._nbins, 0, check_regions=False) self._check_outobs('csphagen_py4_obs.xml', 1) # Setup csphagen for test with custom On and Off regions provided phagen = cscripts.csphagen() phagen['inobs'] = self._myevents1 phagen['inmodel'] = 'NONE' phagen['caldb'] = self._caldb phagen['irf'] = self._irf phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['bkgmethod'] = 'CUSTOM' phagen['srcregfile'] = self._regfile_src phagen['bkgregfile'] = self._regfile_bkg phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['stack'] = False phagen['outobs'] = 'csphagen_py5_obs.xml' phagen['outmodel'] = 'csphagen_py5_model.xml' phagen['prefix'] = 'csphagen_py5' phagen['logfile'] = 'csphagen_py5.log' phagen['chatter'] = 2 # Execute script phagen.execute() # Check output self._check_output('csphagen_py5', self._nbins, self._nreg_bkg_reg) self._check_outobs('csphagen_py5_obs.xml', 1) # Append off regions to observation container for run in obs: run.off_regions(gammalib.GSkyRegions(self._regfile_bkg)) # Setup csphagen for test with multiple input observations and stacking phagen = cscripts.csphagen(obs) phagen['inmodel'] = 'NONE' phagen['ebinalg'] = 'LOG' phagen['emin'] = 0.1 phagen['emax'] = 100.0 phagen['enumbins'] = self._nbins phagen['coordsys'] = 'CEL' phagen['stack'] = True phagen['inexclusion'] = self._exclusion phagen['bkgmethod'] = 'CUSTOM' phagen['srcregfile'] = self._regfile_src phagen['etruemin'] = 0.05 phagen['etruemax'] = 150.0 phagen['etruebins'] = 5 phagen['outobs'] = 'csphagen_py6_obs.xml' phagen['outmodel'] = 'csphagen_py6_model.xml' phagen['prefix'] = 'csphagen_py6' phagen['logfile'] = 'csphagen_py6.log' phagen['chatter'] = 4 # Execute script phagen.execute() # Check output for s in range(2): self._check_output('csphagen_py6_stacked', self._nbins, 0, check_regions=False) self._check_outobs('csphagen_py6_obs.xml', 1) # Return return
def run(self): """ Run the script Raises ------ RuntimeError Invalid pointing definition file format """ # Switch screen logging on in debug mode if self._logDebug(): self._log.cout(True) # Get parameters self._get_parameters() # Write header into logger self._log_header1(gammalib.TERSE, 'Creating observation definition XML file') # Load pointing definition file if it is not already set. Extract # the number of columns and pointings if self._pntdef.size() == 0: self._pntdef = gammalib.GCsv(self['inpnt'].filename(), ',') ncols = self._pntdef.ncols() npnt = self._pntdef.nrows() - 1 # Raise an exception if there is no header information if self._pntdef.nrows() < 1: raise RuntimeError('No header found in pointing definition file.') # Clear observation container self._obs.clear() # Initialise observation identifier counter identifier = 1 # Extract header columns from pointing definition file and put them # into a list header = [] for col in range(ncols): header.append(self._pntdef[0, col]) # Loop over all pointings for pnt in range(npnt): # Set pointing definition CSV file row index row = pnt + 1 # Create empty CTA observation obs = gammalib.GCTAObservation() # Set observation name. If no observation name was given then # use "None". if 'name' in header: name = self._pntdef[row, header.index('name')] else: name = self['name'].string() obs.name(name) # Set observation identifier. If no observation identified was # given the use the internal counter. if 'id' in header: obsid = self._pntdef[row, header.index('id')] else: obsid = '%6.6d' % identifier identifier += 1 obs.id(obsid) # Set pointing. Either use "ra" and "dec" or "lon" and "lat". # If none of these pairs are given then raise an exception. if 'ra' in header and 'dec' in header: ra = float(self._pntdef[row, header.index('ra')]) dec = float(self._pntdef[row, header.index('dec')]) pntdir = gammalib.GSkyDir() pntdir.radec_deg(ra, dec) elif 'lon' in header and 'lat' in header: lon = float(self._pntdef[row, header.index('lon')]) lat = float(self._pntdef[row, header.index('lat')]) pntdir = gammalib.GSkyDir() pntdir.lb_deg(lon, lat) else: raise RuntimeError('No (ra,dec) or (lon,lat) columns ' 'found in pointing definition file.') obs.pointing(gammalib.GCTAPointing(pntdir)) # Set response function. If no "caldb" or "irf" information is # provided then use the user parameter values. if 'caldb' in header: caldb = self._pntdef[row, header.index('caldb')] else: caldb = self['caldb'].string() if 'irf' in header: irf = self._pntdef[row, header.index('irf')] else: irf = self['irf'].string() if caldb != '' and irf != '': obs = self._set_irf(obs, caldb, irf) # Set deadtime correction factor. If no information is provided # then use the user parameter value "deadc". if 'deadc' in header: deadc = float(self._pntdef[row, header.index('deadc')]) else: deadc = self['deadc'].real() obs.deadc(deadc) # Set Good Time Interval. If no information is provided then use # the user parameter values "tmin" and "duration". if 'tmin' in header: self._tmin = float(self._pntdef[row, header.index('tmin')]) if 'duration' in header: duration = float(self._pntdef[row, header.index('duration')]) else: duration = self['duration'].real() tref = gammalib.GTimeReference(self['mjdref'].real(), 's') tmin = self._tmin tmax = self._tmin + duration gti = gammalib.GGti(tref) tstart = gammalib.GTime(tmin, tref) tstop = gammalib.GTime(tmax, tref) self._tmin = tmax gti.append(tstart, tstop) obs.ontime(gti.ontime()) obs.livetime(gti.ontime() * deadc) # Set Energy Boundaries. If no "emin" or "emax" information is # provided then use the user parameter values in case they are # valid. has_emin = False has_emax = False if 'emin' in header: emin = float(self._pntdef[row, header.index('emin')]) has_emin = True else: if self['emin'].is_valid(): emin = self['emin'].real() has_emin = True if 'emax' in header: emax = float(self._pntdef[row, header.index('emax')]) has_emax = True else: if self['emax'].is_valid(): emax = self['emax'].real() has_emax = True has_ebounds = has_emin and has_emax if has_ebounds: ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, 'TeV'), gammalib.GEnergy(emax, 'TeV')) # Set ROI. If no ROI radius is provided then use the user # parameters "rad". has_roi = False if 'rad' in header: rad = float(self._pntdef[row, header.index('rad')]) has_roi = True else: if self['rad'].is_valid(): rad = self['rad'].real() has_roi = True if has_roi: roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad) # Create an empty event list event_list = gammalib.GCTAEventList() event_list.gti(gti) # If available, set the energy boundaries and the ROI if has_ebounds: event_list.ebounds(ebounds) if has_roi: event_list.roi(roi) # Attach event list to CTA observation obs.events(event_list) # Write observation into logger name = obs.instrument() + ' observation' value = 'Name="%s" ID="%s"' % (obs.name(), obs.id()) self._log_value(gammalib.NORMAL, name, value) self._log_string(gammalib.EXPLICIT, str(obs) + '\n') # Append observation self._obs.append(obs) # Return return
def set(RA=83.63, DEC=22.01, tstart=0.0, duration=1800.0, deadc=0.95, emin=0.1, emax=100.0, rad=5.0, irf="cta_dummy_irf", caldb="$GAMMALIB/share/caldb/cta"): """ Create one CTA observation Copied from ctools/scripts/obsutils.py and modified a bit. """ # Allocate CTA observation obs = gammalib.GCTAObservation() # Set pointing direction pntdir = gammalib.GSkyDir() pntdir.radec_deg(RA, DEC) pnt = gammalib.GCTAPointing() pnt.dir(pntdir) obs.pointing(pnt) # Set ROI roi = gammalib.GCTARoi() instdir = gammalib.GCTAInstDir() instdir.dir(pntdir) roi.centre(instdir) roi.radius(rad) # Set GTI gti = gammalib.GGti() start = gammalib.GTime(tstart) stop = gammalib.GTime(tstart + duration) gti.append(start, stop) # Set energy boundaries ebounds = gammalib.GEbounds() e_min = gammalib.GEnergy() e_max = gammalib.GEnergy() e_min.TeV(emin) e_max.TeV(emax) ebounds.append(e_min, e_max) # Allocate event list events = gammalib.GCTAEventList() events.roi(roi) events.gti(gti) events.ebounds(ebounds) obs.events(events) # Set instrument response obs.response(irf, caldb) # Set ontime, livetime, and deadtime correction factor obs.ontime(duration) obs.livetime(duration * deadc) obs.deadc(deadc) # Return observation return obs
if args.tmax is not None: if tstart >= args.tmax: print("Stop time slices loop at slice {} where time >= limit".format(s['id']), file=sys.stderr) break if tstop > args.tmax: tstop = args.tmax index = int(s['id']) events_file = os.path.join(working_dir, "events_{0:02d}.fits".format(index)) log_file = os.path.join(working_dir, "ctobssim_{0:02d}.log".format(index)) sim = sobs.simulation_run(s['model_file'], events_file, ra=SOURCE['ra']+args.ra_shift, dec=SOURCE['dec']+args.dec_shift, time=[tstart, tstop], log_file=log_file, force=args.force, save=args.save) if sim.obs().size() != 1: raise Exception("None or too many simulated observations") gcta_obs = None if args.save: # only the files list gcta_obs = gammalib.GCTAObservation(events_file) else: gcta_obs = sim.obs()[0] # GCTAObservation gcta_obs.id("{0:02d}".format(index)) gcta_obs.name("{0}_{1:02d}".format(SOURCE["name"], index)) sim_obs_list.append(gcta_obs) if args.verbose > 1: print("Simulation {} done.".format(gcta_obs.name), file=sys.stderr) tstart=tstop if args.verbose > 0: print("Simulations list:\n", sim_obs_list) # GObservations if args.save: sim_obs_list.save(os.path.join(working_dir, 'sim_obs_list.xml')) exit(0)
def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ emin=0.1, emax=100.0, rad=5.0, \ irf="South_50h", caldb="prod2", id="000000"): """ Set a single CTA observation. The function sets a single CTA observation containing an empty CTA event list. By looping over this function you can add CTA observations to the observation container. Args: pntdir: Pointing direction [GSkyDir] Kwargs: tstart: Start time (seconds) (default: 0.0) duration: Duration of observation (seconds) (default: 1800.0) deadc: Deadtime correction factor (default: 0.95) emin: Minimum event energy (TeV) (default: 0.1) emax: Maximum event energy (TeV) (default: 100.0) rad: ROI radius used for analysis (deg) (default: 5.0) irf: Instrument response function (default: "South_50h") caldb: Calibration database path (default: "prod2") id: Run identifier (default: "000000") """ # Allocate CTA observation obs_cta = gammalib.GCTAObservation() # Set calibration database db = gammalib.GCaldb() if (gammalib.dir_exists(caldb)): db.rootdir(caldb) else: db.open("cta", caldb) # Set pointing direction pnt = gammalib.GCTAPointing() pnt.dir(pntdir) obs_cta.pointing(pnt) # Set ROI roi = gammalib.GCTARoi() instdir = gammalib.GCTAInstDir() instdir.dir(pntdir) roi.centre(instdir) roi.radius(rad) # Set GTI gti = gammalib.GGti() gti.append(gammalib.GTime(tstart), gammalib.GTime(tstart + duration)) # Set energy boundaries ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"), gammalib.GEnergy(emax, "TeV")) # Allocate event list events = gammalib.GCTAEventList() events.roi(roi) events.gti(gti) events.ebounds(ebounds) obs_cta.events(events) # Set instrument response obs_cta.response(irf, db) # Set ontime, livetime, and deadtime correction factor obs_cta.ontime(duration) obs_cta.livetime(duration * deadc) obs_cta.deadc(deadc) obs_cta.id(id) # Return CTA observation return obs_cta
def grb_simulation(sim_in, config_in, model_xml, fits_header_0, counter): """ Function to handle the GRB simulation. :param sim_in: the yaml file for the simulation (unpacked as a dict of dicts) :param config_in: the yaml file for the job handling (unpacked as a dict of dicts) :param model_xml: the XML model name for the source under analysis :param fits_header_0: header for the fits file of the GRB model to use. Used in the visibility calculation :param counter: integer number. counts the id of the source realization :return: significance obtained with the activated detection methods """ src_name = model_xml.split('/')[-1].split('model_')[1][:-4] print(src_name, counter) ctools_pipe_path = create_path(config_in['exe']['software_path']) ctobss_params = sim_in['ctobssim'] seed = int(counter)*10 # PARAMETERS FROM THE CTOBSSIM sim_t_min = u.Quantity(ctobss_params['time']['t_min']).to_value(u.s) sim_t_max = u.Quantity(ctobss_params['time']['t_max']).to_value(u.s) sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV) sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV) sim_rad = ctobss_params['radius'] models = sim_in['source'] source_type = models['type'] if source_type == "GRB": phase_path = "/" + models['phase'] elif source_type == "GW": phase_path = "" output_path = create_path(sim_in['output']['path'] + phase_path + '/' + src_name) save_simulation = ctobss_params['save_simulation'] with open(f"{output_path}/GRB-{src_name}_seed-{seed}.txt", "w") as f: f.write(f"GRB,seed,time_start,time_end,sigma_lima,sqrt_TS_onoff,sqrt_TS_std\n") # VISIBILITY PART # choose between AUTO mode (use visibility) and MANUAL mode (manually insert IRF) simulation_mode = sim_in['IRF']['mode'] if simulation_mode == "auto": print("using visibility to get IRFs") # GRB information from the fits header ra = fits_header_0['RA'] dec = fits_header_0['DEC'] t0 = Time(fits_header_0['GRBJD']) irf_dict = sim_in['IRF'] site = irf_dict['site'] obs_condition = Observability(site=site) obs_condition.set_irf(irf_dict) t_zero_mode = ctobss_params['time']['t_zero'].lower() if t_zero_mode == "VIS": # check if the source is visible one day after the onset of the source print("time starts when source becomes visible") obs_condition.Proposal_obTime = 86400 condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0) elif t_zero_mode == "ONSET": print("time starts from the onset of the GRB") condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0, t_min=sim_t_min, t_max=sim_t_max) else: print(f"Choose some proper mode between 'VIS' and 'ONSET'. {t_zero_mode} is not a valid one.") sys.exit() # NO IRF in AUTO mode ==> No simulation! == EXIT! if len(condition_check) == 0: f.write(f"{src_name},{seed}, -1, -1, -1, -1, -1\n") sys.exit() elif simulation_mode == "manual": print("manual picking IRF") # find proper IRF name irf = IRFPicker(sim_in, ctools_pipe_path) name_irf = irf.irf_pick() backgrounds_path = create_path(ctobss_params['bckgrnd_path']) fits_background_list = glob.glob( f"{backgrounds_path}/{irf.prod_number}_{irf.prod_version}_{name_irf}/background*.fits") if len(fits_background_list) == 0: print(f"No background for IRF {name_irf}") sys.exit() fits_background_list = sorted(fits_background_list, key=sort_background) background_fits = fits_background_list[int(counter) - 1] obs_back = gammalib.GCTAObservation(background_fits) else: print(f"wrong input for IRF - mode. Input is {simulation_mode}. Use 'auto' or 'manual' instead") sys.exit() if irf.prod_number == "3b" and irf.prod_version == 0: caldb = "prod3b" else: caldb = f'prod{irf.prod_number}-v{irf.prod_version}' # source simulation sim = ctools.ctobssim() sim['inmodel'] = model_xml sim['caldb'] = caldb sim['irf'] = name_irf sim['ra'] = 0.0 sim['dec'] = 0.0 sim['rad'] = sim_rad sim['tmin'] = sim_t_min sim['tmax'] = sim_t_max sim['emin'] = sim_e_min sim['emax'] = sim_e_max sim['seed'] = seed sim.run() obs = sim.obs() # # move the source photons from closer to (RA,DEC)=(0,0), where the background is located # for event in obs[0].events(): # # ra_evt = event.dir().dir().ra() # dec_evt = event.dir().dir().dec() # ra_evt_deg = event.dir().dir().ra_deg() # dec_evt_deg = event.dir().dir().dec_deg() # # ra_corrected = (ra_evt_deg - ra_pointing)*np.cos(dec_evt) # dec_corrected = dec_evt_deg - dec_pointing # event.dir().dir().radec_deg(ra_corrected, dec_corrected) # append all background events to GRB ones ==> there's just one observation and not two for event in obs_back.events(): obs[0].events().append(event) # ctselect to save data on disk if save_simulation: event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/") #obs.save(f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}.fits") select_time = ctools.ctselect(obs) select_time['rad'] = sim_rad select_time['tmin'] = sim_t_min select_time['tmax'] = sim_t_max select_time['emin'] = sim_e_min select_time['emax'] = sim_e_max select_time['outobs'] = f"{event_list_path}/event_list_source-{src_name}_{seed:03}.fits" select_time.run() sys.exit() # delete all 70+ models from the obs def file...not needed any more obs.models(gammalib.GModels()) # CTSELECT select_time = sim_in['ctselect']['time_cut'] slices = int(select_time['t_slices']) if slices == 0: times = [sim_t_min, sim_t_max] times_start = times[:-1] times_end = times[1:] elif slices > 0: time_mode = select_time['mode'] if time_mode == "log": times = np.logspace(np.log10(sim_t_min), np.log10(sim_t_max), slices + 1, endpoint=True) elif time_mode == "lin": times = np.linspace(sim_t_min, sim_t_max, slices + 1, endpoint=True) else: print(f"{time_mode} not valid. Use 'log' or 'lin' ") sys.exit() if select_time['obs_mode'] == "iter": times_start = times[:-1] times_end = times[1:] elif select_time['obs_mode'] == "cumul": times_start = np.repeat(times[0], slices) # this is to use the same array structure for the loop times_end = times[1:] elif select_time['obs_mode'] == "all": begins, ends = np.meshgrid(times[:-1], times[1:]) mask_times = begins < ends times_start = begins[mask_times].ravel() times_end = ends[mask_times].ravel() else: print(f"obs_mode: {select_time['obs_mode']} not supported") sys.exit() else: print(f"value {slices} not supported...check yaml file") sys.exit() # ------------------------------------ # ----- TIME LOOP STARTS HERE -------- # ------------------------------------ ctlike_mode = sim_in['detection'] mode_1 = ctlike_mode['counts'] mode_2 = ctlike_mode['ctlike-onoff'] mode_3 = ctlike_mode['ctlike-std'] for t_in, t_end in zip(times_start, times_end): sigma_onoff = 0 sqrt_ts_like_onoff = 0 sqrt_ts_like_std = 0 print("-----------------------------") print(f"t_in: {t_in:.2f}, t_end: {t_end:.2f}") # different ctlikes (onoff or std) need different files. # will be appended here and used later on for the final likelihood dict_obs_select_time = {} # perform time selection for this specific time bin select_time = ctools.ctselect(obs) select_time['rad'] = sim_rad select_time['tmin'] = t_in select_time['tmax'] = t_end select_time['emin'] = sim_e_min select_time['emax'] = sim_e_max select_time.run() if mode_1: fits_temp_title = f"skymap_{seed}_{t_in:.2f}_{t_end:.2f}.fits" pars_counts = ctlike_mode['pars_counts'] scale = float(pars_counts['scale']) npix = 2*int(sim_rad/scale) skymap = ctools.ctskymap(select_time.obs().copy()) skymap['emin'] = sim_e_min skymap['emax'] = sim_e_max skymap['nxpix'] = npix skymap['nypix'] = npix skymap['binsz'] = scale skymap['proj'] = 'TAN' skymap['coordsys'] = 'CEL' skymap['xref'] = 0 skymap['yref'] = 0 skymap['bkgsubtract'] = 'RING' skymap['roiradius'] = pars_counts['roiradius'] skymap['inradius'] = pars_counts['inradius'] skymap['outradius'] = pars_counts['outradius'] skymap['iterations'] = pars_counts['iterations'] skymap['threshold'] = pars_counts['threshold'] skymap['outmap'] = fits_temp_title skymap.execute() input_fits = fits.open(fits_temp_title) datain = input_fits[2].data datain[np.isnan(datain)] = 0.0 datain[np.isinf(datain)] = 0.0 sigma_onoff = np.max(datain) os.remove(fits_temp_title) if mode_3: dict_obs_select_time['std'] = select_time.obs().copy() if mode_2: onoff_time_sel = cscripts.csphagen(select_time.obs().copy()) onoff_time_sel['inmodel'] = 'NONE' onoff_time_sel['ebinalg'] = 'LOG' onoff_time_sel['emin'] = sim_e_min onoff_time_sel['emax'] = sim_e_max onoff_time_sel['enumbins'] = 30 onoff_time_sel['coordsys'] = 'CEL' onoff_time_sel['ra'] = 0.0 onoff_time_sel['dec'] = 0.5 onoff_time_sel['rad'] = 0.2 onoff_time_sel['bkgmethod'] = 'REFLECTED' onoff_time_sel['use_model_bkg'] = False onoff_time_sel['stack'] = False onoff_time_sel.run() dict_obs_select_time['onoff'] = onoff_time_sel.obs().copy() del onoff_time_sel # print(f"sigma ON/OFF: {sigma_onoff:.2f}") if mode_2 or mode_3: # Low Energy PL fitting # to be saved in this dict dict_pl_ctlike_out = {} e_min_pl_ctlike = 0.030 e_max_pl_ctlike = 0.080 # simple ctobssim copy and select for ctlike-std select_pl_ctlike = ctools.ctselect(select_time.obs().copy()) select_pl_ctlike['rad'] = 3 select_pl_ctlike['tmin'] = t_in select_pl_ctlike['tmax'] = t_end select_pl_ctlike['emin'] = e_min_pl_ctlike select_pl_ctlike['emax'] = e_max_pl_ctlike select_pl_ctlike.run() # create test source src_dir = gammalib.GSkyDir() src_dir.radec_deg(0, 0.5) spatial = gammalib.GModelSpatialPointSource(src_dir) # create and append source spectral model spectral = gammalib.GModelSpectralPlaw() spectral['Prefactor'].value(5.5e-16) spectral['Prefactor'].scale(1e-16) spectral['Index'].value(-2.6) spectral['Index'].scale(-1.0) spectral['PivotEnergy'].value(50000) spectral['PivotEnergy'].scale(1e3) model_src = gammalib.GModelSky(spatial, spectral) model_src.name('PL_fit_temp') model_src.tscalc(True) spectral_back = gammalib.GModelSpectralPlaw() spectral_back['Prefactor'].value(1.0) spectral_back['Prefactor'].scale(1.0) spectral_back['Index'].value(0) spectral_back['PivotEnergy'].value(300000) spectral_back['PivotEnergy'].scale(1e6) if mode_2: back_model = gammalib.GCTAModelIrfBackground() back_model.instruments('CTAOnOff') back_model.name('Background') back_model.spectral(spectral_back.copy()) onoff_pl_ctlike_lima = cscripts.csphagen(select_pl_ctlike.obs().copy()) onoff_pl_ctlike_lima['inmodel'] = 'NONE' onoff_pl_ctlike_lima['ebinalg'] = 'LOG' onoff_pl_ctlike_lima['emin'] = e_min_pl_ctlike onoff_pl_ctlike_lima['emax'] = e_max_pl_ctlike onoff_pl_ctlike_lima['enumbins'] = 30 onoff_pl_ctlike_lima['coordsys'] = 'CEL' onoff_pl_ctlike_lima['ra'] = 0.0 onoff_pl_ctlike_lima['dec'] = 0.5 onoff_pl_ctlike_lima['rad'] = 0.2 onoff_pl_ctlike_lima['bkgmethod'] = 'REFLECTED' onoff_pl_ctlike_lima['use_model_bkg'] = False onoff_pl_ctlike_lima['stack'] = False onoff_pl_ctlike_lima.run() onoff_pl_ctlike_lima.obs().models(gammalib.GModels()) onoff_pl_ctlike_lima.obs().models().append(model_src.copy()) onoff_pl_ctlike_lima.obs().models().append(back_model.copy()) like_pl = ctools.ctlike(onoff_pl_ctlike_lima.obs()) like_pl['refit'] = True like_pl.run() dict_pl_ctlike_out['onoff'] = like_pl.obs().copy() del onoff_pl_ctlike_lima del like_pl if mode_3: models_ctlike_std = gammalib.GModels() models_ctlike_std.append(model_src.copy()) back_model = gammalib.GCTAModelIrfBackground() back_model.instruments('CTA') back_model.name('Background') back_model.spectral(spectral_back.copy()) models_ctlike_std.append(back_model) # save models xmlmodel_PL_ctlike_std = 'test_model_PL_ctlike_std.xml' models_ctlike_std.save(xmlmodel_PL_ctlike_std) del models_ctlike_std like_pl = ctools.ctlike(select_pl_ctlike.obs().copy()) like_pl['inmodel'] = xmlmodel_PL_ctlike_std like_pl['refit'] = True like_pl.run() dict_pl_ctlike_out['std'] = like_pl.obs().copy() del like_pl del spatial del spectral del model_src del select_pl_ctlike # EXTENDED CTLIKE for key in dict_obs_select_time.keys(): likelihood_pl_out = dict_pl_ctlike_out[key] selected_data = dict_obs_select_time[key] pref_out_pl = likelihood_pl_out.models()[0]['Prefactor'].value() index_out_pl = likelihood_pl_out.models()[0]['Index'].value() pivot_out_pl = likelihood_pl_out.models()[0]['PivotEnergy'].value() expplaw = gammalib.GModelSpectralExpPlaw() expplaw['Prefactor'].value(pref_out_pl) expplaw['Index'].value(index_out_pl) expplaw['PivotEnergy'].value(pivot_out_pl) expplaw['CutoffEnergy'].value(80e3) if key == "onoff": selected_data.models()[0].name(src_name) selected_data.models()[0].tscalc(True) selected_data.models()[0].spectral(expplaw.copy()) like = ctools.ctlike(selected_data) like['refit'] = True like.run() ts = like.obs().models()[0].ts() if ts > 0: sqrt_ts_like_onoff = np.sqrt(like.obs().models()[0].ts()) else: sqrt_ts_like_onoff = 0 del like if key == "std": models_fit_ctlike = gammalib.GModels() # create test source src_dir = gammalib.GSkyDir() src_dir.radec_deg(0, 0.5) spatial = gammalib.GModelSpatialPointSource(src_dir) # append spatial and spectral models model_src = gammalib.GModelSky(spatial, expplaw.copy()) model_src.name('Source_fit') model_src.tscalc(True) models_fit_ctlike.append(model_src) # create and append background back_model = gammalib.GCTAModelIrfBackground() back_model.instruments('CTA') back_model.name('Background') spectral_back = gammalib.GModelSpectralPlaw() spectral_back['Prefactor'].value(1.0) spectral_back['Prefactor'].scale(1.0) spectral_back['Index'].value(0) spectral_back['PivotEnergy'].value(300000) spectral_back['PivotEnergy'].scale(1e6) back_model.spectral(spectral_back) models_fit_ctlike.append(back_model) # save models input_ctlike_xml = "model_GRB_fit_ctlike_in.xml" models_fit_ctlike.save(input_ctlike_xml) del models_fit_ctlike like = ctools.ctlike(selected_data) like['inmodel'] = input_ctlike_xml like['refit'] = True like.run() ts = like.obs().models()[0].ts() if ts > 0: sqrt_ts_like_std = np.sqrt(like.obs().models()[0].ts()) else: sqrt_ts_like_std = 0 del like # E_cut_off = like.obs().models()[0]['CutoffEnergy'].value() # E_cut_off_error = like.obs().models()[0]['CutoffEnergy'].error() # print(f"sqrt(TS) {key}: {np.sqrt(ts_like):.2f}") # print(f"E_cut_off {key}: {E_cut_off:.2f} +- {E_cut_off_error:.2f}") del dict_pl_ctlike_out f.write(f"{src_name},{seed},{t_in:.2f},{t_end:.2f},{sigma_onoff:.2f},{sqrt_ts_like_onoff:.2f},{sqrt_ts_like_std:.2f}\n") del dict_obs_select_time del select_time
def _test_python(self): """ Test ctlike from Python """ # Allocate ctlike like = ctools.ctlike() # Check that empty ctlike tool holds an empty observation and # optimizer self._check_obs(like.obs(), nobs=0, nmodels=0) self._check_opt(like.opt()) # Check that saving saves an empty model definition file like['outmodel'] = 'ctlike_py0.xml' like['logfile'] = 'ctlike_py0.log' like.logFileOpen() like.save() self._check_result_file('ctlike_py0.xml', nmodels=0) # Check that clearing does not lead to an exception or segfault like.clear() # Now set ctlike parameters like['inobs'] = self._events like['inmodel'] = self._model like['caldb'] = self._caldb like['irf'] = self._irf like['outmodel'] = 'ctlike_py1.xml' like['logfile'] = 'ctlike_py1.log' like['chatter'] = 2 # Run ctlike tool like.logFileOpen() # Make sure we get a log file like.run() like.save() # Check result file self._check_result_file('ctlike_py1.xml') # Copy ctlike tool cpy_like = like.copy() # Check observation and optimizer of copy self._check_obs(cpy_like.obs()) self._check_opt(cpy_like.opt()) # Execute copy of ctlike tool again, now with a higher chatter # level than before cpy_like['outmodel'] = 'ctlike_py2.xml' cpy_like['logfile'] = 'ctlike_py2.log' cpy_like['chatter'] = 3 cpy_like.logFileOpen() # Needed to get a new log file cpy_like.execute() # Check result file self._check_result_file('ctlike_py2.xml') # Now clear copy of ctlike tool cpy_like.clear() # Check that the cleared copy has also cleared the observations self._check_obs(cpy_like.obs(), nobs=0, nmodels=0) # Prepare observation container with a single event list obs = gammalib.GObservations() obs.append(gammalib.GCTAObservation(self._events)) # Prepare a special model to enforce TS computation models = gammalib.GModels(self._model) models['Crab'].tscalc(True) obs.models(models) # Allocate ctlike tool from observation container like = ctools.ctlike(obs) like['caldb'] = self._caldb like['irf'] = self._irf like['refit'] = True like['fix_spat_for_ts'] = True like['outmodel'] = 'ctlike_py3.xml' like['logfile'] = 'ctlike_py3.log' like['chatter'] = 4 # Execute ctlike like.logFileOpen() # Needed to get a new log file like.execute() # Check result file self._check_result_file('ctlike_py3.xml') # Return return
def _test_python(self): """ Test cterror from Python """ # Allocate cterror error = ctools.cterror() # Check that empty cterror tool holds an empty observation and # optimizer self._check_obs(error.obs(), nobs=0, nmodels=0) self._check_opt(error.opt()) # Check that saving saves an empty model definition file error['outmodel'] = 'cterror_py0.xml' error['logfile'] = 'cterror_py0.log' error.logFileOpen() error.save() self._check_result_file('cterror_py0.xml', nmodels=0) # Check that clearing does not lead to an exception or segfault error.clear() # Now set cterror parameters error['inobs'] = self._events error['inmodel'] = self._model error['srcname'] = 'Crab' error['caldb'] = self._caldb error['irf'] = self._irf error['tol'] = 0.1 error['outmodel'] = 'cterror_py1.xml' error['logfile'] = 'cterror_py1.log' error['chatter'] = 2 # Run cterror tool error.logFileOpen() # Make sure we get a log file error.run() error.save() # Check result file self._check_result_file('cterror_py1.xml') # Copy cterror tool cpy_error = error.copy() # Check observation and optimizer of copy self._check_obs(cpy_error.obs()) self._check_opt(cpy_error.opt()) # Execute copy of cterror tool again, now with a higher chatter # level than before cpy_error['outmodel'] = 'cterror_py2.xml' cpy_error['logfile'] = 'cterror_py2.log' cpy_error['chatter'] = 3 cpy_error.logFileOpen() # Needed to get a new log file cpy_error.execute() # Check result file self._check_result_file('cterror_py2.xml') # Now clear copy of cterror tool cpy_error.clear() # Check that the cleared copy has also cleared the observations self._check_obs(cpy_error.obs(), nobs=0, nmodels=0) # Prepare observation container with a single event list obs = gammalib.GObservations() obs.append(gammalib.GCTAObservation(self._events)) obs.models(gammalib.GModels(self._model)) # Setup cterror tool from observation container error = ctools.cterror(obs) error['srcname'] = 'Crab' error['caldb'] = self._caldb error['irf'] = self._irf error['tol'] = 0.1 error['outmodel'] = 'cterror_py3.xml' error['logfile'] = 'cterror_py3.log' error['chatter'] = 4 # Execute cterror tool error.logFileOpen() # Make sure we get a log file error.execute() # Check result file self._check_result_file('cterror_py3.xml') # And now a run with not enough iterations error['max_iter'] = 1 error['outmodel'] = 'cterror_py4.xml' error['logfile'] = 'cterror_py4.log' # Execute cterror tool and catch the exception error.logFileOpen() # Make sure we get a log file self.test_try('Test cterror with not enough iterations') try: error.execute() self.test_try_failure('Exception not thrown') except ValueError: self.test_try_success() # Return return
def get_pointings(filename): """ Extract pointings from XML file Parameters ---------- filename : str File name of observation definition XML file Returns ------- pnt : list of dict Pointings """ # Initialise pointings pnt = [] # Open XML file xml = gammalib.GXml(filename) # Get observation list obs = xml.element('observation_list') # Get number of observations nobs = obs.elements('observation') # Loop over observations for i in range(nobs): # Get observation run = obs.element('observation', i) # Get pointing parameter npars = run.elements('parameter') ra = None dec = None south = False evfile = None for k in range(npars): par = run.element('parameter', k) if par.attribute('name') == 'Pointing': ra = float(par.attribute('ra')) dec = float(par.attribute('dec')) elif par.attribute('name') == 'Calibration': if 'South' in par.attribute('response'): south = True else: south = False elif par.attribute('name') == 'EventList': evfile = par.attribute('file') # If no pointing was found then load observation if ra == None and evfile != None: cta = gammalib.GCTAObservation(gammalib.GFilename(evfile)) ra = cta.pointing().dir().ra_deg() dec = cta.pointing().dir().dec_deg() # Add valid pointing if ra != None: p = gammalib.GSkyDir() p.radec_deg(ra, dec) entry = { 'l': p.l_deg(), 'b': p.b_deg(), 'ra': ra, 'dec': dec, 'south': south } pnt.append(entry) # Return pointings return pnt
def _get_parameters(self): """ Get parameters from parfile and setup the observation. """ # Initialise some flags self._use_maps = False self._skip_binning = False # First check if the inobs parameter is a counts cube if self._obs.size() == 0 and self["inobs"].filename() != "NONE": filename = gammalib.GFilename(self["inobs"].filename()) if filename.is_fits(): cta = gammalib.GCTAObservation() cta.load(filename) if cta.eventtype() == "CountsCube": self._skip_binning = True # If we have a counts cube, then ask whether we also have a model if self._skip_binning: self._modcube = self["modcube"].filename() if self._modcube != "NONE": self._use_maps = True # If not two maps are given, proceed to set up observation if not self._use_maps: # Set observation if not done before if self._obs.size() == 0: self._require_inobs("csresmap.get_parameters()") self._obs = self._get_observations() # Check if we have exactly one binned CTA observation if self._obs.size() == 1: if self._obs[0].classname() == "GCTAObservation": if self._obs[0].eventtype() == "CountsCube": # Skip ctbin step later on self._skip_binning = True # Set models if we have none if self._obs.models().size() == 0: self._obs.models(self["inmodel"].filename()) # Skip query for spatial parameters if a binning is provided in the observation if not self._skip_binning: # Read other parameters self._xref = self["xref"].real() self._yref = self["yref"].real() self._emin = self["emin"].real() self._emax = self["emax"].real() self._enumbins = self["enumbins"].integer() self._ebinalg = self["ebinalg"].string() self._coordsys = self["coordsys"].string() self._proj = self["proj"].string() self._nxpix = self["nxpix"].integer() self._nypix = self["nypix"].integer() self._binsz = self["binsz"].real() # Read energy dispersion flag self._edisp = self["edisp"].boolean() # Read algorithm self._algorithm = self["algorithm"].string() # Read standard parameters self._publish = self["publish"].boolean() self._chatter = self["chatter"].integer() self._clobber = self["clobber"].boolean() self._debug = self["debug"].boolean() # Read ahead output parameters if (self._read_ahead()): self["outmap"].filename() # Write input parameters into logger if self._logTerse(): self._log_parameters() self._log("\n") # Return return
def _test_python(self): """ Test cslightcrv from Python """ # Set-up unbinned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = '2020-01-01T00:00:00' lcrv['tmax'] = '2020-01-01T00:05:00' lcrv['tbins'] = 2 lcrv['method'] = '3D' lcrv['enumbins'] = 0 lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['outfile'] = 'cslightcrv_py1.fits' lcrv['logfile'] = 'cslightcrv_py1.log' lcrv['chatter'] = 2 lcrv['publish'] = True # Run cslightcrv script and save light curve lcrv.logFileOpen() # Make sure we get a log file lcrv.run() lcrv.save() # Check light curve self._check_light_curve('cslightcrv_py1.fits', 2) # Now use FILE as time bin algorithm. For this we need first to # create an ASCII file. We use now 2 time bins. The ASCII file # is saved into the file "lightcurve_py2.dat". csv = gammalib.GCsv(2, 2) tmin = 58849.00 tdelta = 0.0017361 for i in range(csv.nrows()): csv[i, 0] = '%.5f' % (tmin + i * tdelta) csv[i, 1] = '%.5f' % (tmin + (i + 1) * tdelta) csv.save('cslightcrv_py2.dat', ' ', True) # Set-up unbinned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'FILE' lcrv['tbinfile'] = 'cslightcrv_py2.dat' lcrv['method'] = '3D' lcrv['enumbins'] = 0 lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['fix_bkg'] = True lcrv['outfile'] = 'cslightcrv_py2.fits' lcrv['logfile'] = 'cslightcrv_py2.log' lcrv['chatter'] = 3 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py2.fits', 2) # Now we setup an observation container on input. We attached the # model to the observation container so that cslightcrv should # no longer query for the parameter. cta = gammalib.GCTAObservation(self._events) obs = gammalib.GObservations() obs.append(cta) obs.models(self._model) # Set-up unbinned cslightcrv from observation container. Now use # the GTI algorithm so that we test all timing algorithms. lcrv = cscripts.cslightcrv(obs) lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'GTI' lcrv['method'] = '3D' lcrv['enumbins'] = 0 lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['outfile'] = 'cslightcrv_py3.fits' lcrv['logfile'] = 'cslightcrv_py3.log' lcrv['chatter'] = 4 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py3.fits', 1) # Binned cslightcrv lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = '2020-01-01T00:00:00' lcrv['tmax'] = '2020-01-01T00:05:00' lcrv['tbins'] = 2 lcrv['method'] = '3D' lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['enumbins'] = 3 lcrv['coordsys'] = 'CEL' lcrv['proj'] = 'TAN' lcrv['xref'] = 83.63 lcrv['yref'] = 22.01 lcrv['nxpix'] = 10 lcrv['nypix'] = 10 lcrv['binsz'] = 0.04 lcrv['outfile'] = 'cslightcrv_py4.fits' lcrv['logfile'] = 'cslightcrv_py4.log' lcrv['chatter'] = 4 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py4.fits', 2) # cslightcrv with classical analysis lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._offaxis_events lcrv['inmodel'] = self._model_onoff lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = '2020-01-01T00:00:00' lcrv['tmax'] = '2020-01-01T00:05:00' lcrv['tbins'] = 2 lcrv['method'] = 'ONOFF' lcrv['use_model_bkg'] = False # Needed even if WSTAT lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['enumbins'] = 2 lcrv['coordsys'] = 'CEL' lcrv['xref'] = 83.63 lcrv['yref'] = 22.01 lcrv['rad'] = 0.2 lcrv['etruemin'] = 1.0 lcrv['etruemax'] = 100.0 lcrv['etruebins'] = 5 lcrv['statistic'] = 'WSTAT' lcrv['outfile'] = 'cslightcrv_py5.fits' lcrv['logfile'] = 'cslightcrv_py5.log' lcrv['chatter'] = 4 # Execute cslightcrv script lcrv.execute() # Check light curve self._check_light_curve('cslightcrv_py5.fits', 2) # Set-up cslightcrv without multiprocessing lcrv = cscripts.cslightcrv() lcrv['inobs'] = self._events lcrv['inmodel'] = self._model lcrv['srcname'] = 'Crab' lcrv['caldb'] = self._caldb lcrv['irf'] = self._irf lcrv['tbinalg'] = 'LIN' lcrv['tmin'] = '2020-01-01T00:00:00' lcrv['tmax'] = '2020-01-01T00:05:00' lcrv['tbins'] = 2 lcrv['method'] = '3D' lcrv['enumbins'] = 0 lcrv['emin'] = 1.0 lcrv['emax'] = 100.0 lcrv['outfile'] = 'cslightcrv_py6.fits' lcrv['logfile'] = 'cslightcrv_py6.log' lcrv['chatter'] = 2 lcrv['publish'] = True lcrv['nthreads'] = 1 # Run cslightcrv script and save light curve lcrv.logFileOpen() # Make sure we get a log file lcrv.run() lcrv.save() # Check light curve self._check_light_curve('cslightcrv_py6.fits', 2) # Return return