Commit f506071d2a3fa6e57ea4417035b66d1599758cf7
1 parent
c3384d71
Select relevant observations in cslightcrv
Showing
2 changed files
with
74 additions
and
47 deletions
cscripts/cslightcrv.py
| @@ -21,6 +21,7 @@ | @@ -21,6 +21,7 @@ | ||
| 21 | import sys | 21 | import sys |
| 22 | import gammalib | 22 | import gammalib |
| 23 | import ctools | 23 | import ctools |
| 24 | +import cscripts | ||
| 24 | from cscripts import obsutils | 25 | from cscripts import obsutils |
| 25 | from cscripts import ioutils | 26 | from cscripts import ioutils |
| 26 | from cscripts import mputils | 27 | from cscripts import mputils |
| @@ -454,57 +455,79 @@ class cslightcrv(ctools.csobservation): | @@ -454,57 +455,79 @@ class cslightcrv(ctools.csobservation): | ||
| 454 | 'values': {}} | 455 | 'values': {}} |
| 455 | 456 | ||
| 456 | # Log information | 457 | # Log information |
| 457 | - self._log_header3(gammalib.EXPLICIT, 'Selecting events') | ||
| 458 | - | ||
| 459 | - # Select events | ||
| 460 | - select = ctools.ctselect(self.obs()) | ||
| 461 | - select['emin'] = self['emin'].real() | ||
| 462 | - select['emax'] = self['emax'].real() | ||
| 463 | - select['tmin'] = tmin | ||
| 464 | - select['tmax'] = tmax | ||
| 465 | - select['rad'] = 'UNDEFINED' | ||
| 466 | - select['ra'] = 'UNDEFINED' | ||
| 467 | - select['dec'] = 'UNDEFINED' | 458 | + self._log_header3(gammalib.EXPLICIT, 'Selecting observations') |
| 459 | + | ||
| 460 | + # Select observations | ||
| 461 | + select = cscripts.csobsselect(self.obs()) | ||
| 462 | + select['pntselect'] = 'CIRCLE' | ||
| 463 | + select['coordsys'] = 'GAL' | ||
| 464 | + select['glon'] = 0.0 | ||
| 465 | + select['glat'] = 0.0 | ||
| 466 | + select['rad'] = 180.0 | ||
| 467 | + select['tmin'] = tmin | ||
| 468 | + select['tmax'] = tmax | ||
| 468 | select.run() | 469 | select.run() |
| 469 | 470 | ||
| 470 | - # Retrieve observation | 471 | + # Retrieve observations |
| 471 | obs = select.obs() | 472 | obs = select.obs() |
| 472 | 473 | ||
| 473 | - # Deal with stacked and On/Off Observations | ||
| 474 | - if self._stacked or self._onoff: | 474 | + # If there are observations then select now events from them |
| 475 | + if obs.size() > 0: | ||
| 475 | 476 | ||
| 476 | - # If a stacked analysis is requested bin the events | ||
| 477 | - # and compute the stacked response functions and setup | ||
| 478 | - # an observation container with a single stacked observation. | ||
| 479 | - if self._stacked: | ||
| 480 | - new_obs = obsutils.get_stacked_obs(self, obs) | 477 | + # Log information |
| 478 | + self._log_header3(gammalib.EXPLICIT, 'Selecting events') | ||
| 481 | 479 | ||
| 482 | - # ... otherwise if On/Off analysis is requested generate | ||
| 483 | - # the On/Off observations and response | ||
| 484 | - elif self._onoff: | ||
| 485 | - new_obs = obsutils.get_onoff_obs(self, obs, nthreads=1) | 480 | + # Select events |
| 481 | + select = ctools.ctselect(obs) | ||
| 482 | + select['emin'] = self['emin'].real() | ||
| 483 | + select['emax'] = self['emax'].real() | ||
| 484 | + select['tmin'] = tmin | ||
| 485 | + select['tmax'] = tmax | ||
| 486 | + select['rad'] = 'UNDEFINED' | ||
| 487 | + select['ra'] = 'UNDEFINED' | ||
| 488 | + select['dec'] = 'UNDEFINED' | ||
| 489 | + select.run() | ||
| 486 | 490 | ||
| 487 | - # Extract models | ||
| 488 | - models = new_obs.models() | 491 | + # Retrieve observations |
| 492 | + obs = select.obs() | ||
| 489 | 493 | ||
| 490 | - # Fix background models if required | ||
| 491 | - if self['fix_bkg'].boolean(): | ||
| 492 | - for model in models: | ||
| 493 | - if model.classname() != 'GModelSky': | ||
| 494 | - for par in model: | ||
| 495 | - par.fix() | 494 | + # Continue only if there are observations |
| 495 | + if obs.size() > 0: | ||
| 496 | 496 | ||
| 497 | - # Put back models | ||
| 498 | - new_obs.models(models) | 497 | + # Deal with stacked and On/Off Observations |
| 498 | + if self._stacked or self._onoff: | ||
| 499 | 499 | ||
| 500 | - # Continue with new oberservation container | ||
| 501 | - obs = new_obs | 500 | + # If a stacked analysis is requested bin the events |
| 501 | + # and compute the stacked response functions and setup | ||
| 502 | + # an observation container with a single stacked observation. | ||
| 503 | + if self._stacked: | ||
| 504 | + new_obs = obsutils.get_stacked_obs(self, obs) | ||
| 502 | 505 | ||
| 503 | - # Header | ||
| 504 | - self._log_header3(gammalib.EXPLICIT, 'Fitting the data') | 506 | + # ... otherwise if On/Off analysis is requested generate |
| 507 | + # the On/Off observations and response | ||
| 508 | + elif self._onoff: | ||
| 509 | + new_obs = obsutils.get_onoff_obs(self, obs, nthreads=1) | ||
| 505 | 510 | ||
| 506 | - # Do maximum likelihood model fitting | ||
| 507 | - if obs.size() > 0: | 511 | + # Extract models |
| 512 | + models = new_obs.models() | ||
| 513 | + | ||
| 514 | + # Fix background models if required | ||
| 515 | + if self['fix_bkg'].boolean(): | ||
| 516 | + for model in models: | ||
| 517 | + if model.classname() != 'GModelSky': | ||
| 518 | + for par in model: | ||
| 519 | + par.fix() | ||
| 520 | + | ||
| 521 | + # Put back models | ||
| 522 | + new_obs.models(models) | ||
| 523 | + | ||
| 524 | + # Continue with new oberservation container | ||
| 525 | + obs = new_obs | ||
| 526 | + | ||
| 527 | + # Header | ||
| 528 | + self._log_header3(gammalib.EXPLICIT, 'Fitting the data') | ||
| 529 | + | ||
| 530 | + # Do maximum likelihood model fitting | ||
| 508 | like = ctools.ctlike(obs) | 531 | like = ctools.ctlike(obs) |
| 509 | like['edisp'] = self['edisp'].boolean() | 532 | like['edisp'] = self['edisp'].boolean() |
| 510 | like['nthreads'] = 1 # Avoids OpenMP conflict | 533 | like['nthreads'] = 1 # Avoids OpenMP conflict |
| @@ -533,7 +556,6 @@ class cslightcrv(ctools.csobservation): | @@ -533,7 +556,6 @@ class cslightcrv(ctools.csobservation): | ||
| 533 | result['values']['e_'+par] = source[par].error() | 556 | result['values']['e_'+par] = source[par].error() |
| 534 | 557 | ||
| 535 | # Calculate upper limit (-1 if not computed) | 558 | # Calculate upper limit (-1 if not computed) |
| 536 | - #ul_diff, ul_flux, ul_eflux = self._compute_ulimit(like.obs()) | ||
| 537 | ul_diff, ul_flux, ul_eflux = self._compute_ulimit(obs) | 559 | ul_diff, ul_flux, ul_eflux = self._compute_ulimit(obs) |
| 538 | if ul_diff > 0.0: | 560 | if ul_diff > 0.0: |
| 539 | result['ul_diff'] = ul_diff | 561 | result['ul_diff'] = ul_diff |
cscripts/csobsselect.py
| @@ -137,16 +137,18 @@ class csobsselect(ctools.csobservation): | @@ -137,16 +137,18 @@ class csobsselect(ctools.csobservation): | ||
| 137 | 137 | ||
| 138 | # Get observation start and stop time | 138 | # Get observation start and stop time |
| 139 | obs_gti = obs.gti() | 139 | obs_gti = obs.gti() |
| 140 | - tstart = obs_gti.tstart() | ||
| 141 | - tstop = obs_gti.tstop() | ||
| 142 | 140 | ||
| 143 | # Get User start and stop time in the time reference of the CTA | 141 | # Get User start and stop time in the time reference of the CTA |
| 144 | # observations | 142 | # observations |
| 145 | - gti = self._get_gti(obs_gti.reference()) | ||
| 146 | - | ||
| 147 | - # If the observation is not within the time selection then skip it | ||
| 148 | - if gti.size() > 0 and not gti.contains(tstart) and \ | ||
| 149 | - not gti.contains(tstop): | 143 | + gti = self._get_gti(obs_gti.reference()) |
| 144 | + tstart = gti.tstart() | ||
| 145 | + tstop = gti.tstop() | ||
| 146 | + | ||
| 147 | + # If there is a valid User start and stop time and if that start | ||
| 148 | + # or stop time is not within the observation GTI, then skip the | ||
| 149 | + # observation | ||
| 150 | + if gti.size() > 0 and not obs_gti.contains(tstart) and \ | ||
| 151 | + not obs_gti.contains(tstop): | ||
| 150 | self._log_selection(obs, 'outside time interval') | 152 | self._log_selection(obs, 'outside time interval') |
| 151 | 153 | ||
| 152 | # ... otherwise select spatially | 154 | # ... otherwise select spatially |
| @@ -301,6 +303,9 @@ class csobsselect(ctools.csobservation): | @@ -301,6 +303,9 @@ class csobsselect(ctools.csobservation): | ||
| 301 | # Initialise empty observation container for selected observations | 303 | # Initialise empty observation container for selected observations |
| 302 | selected_obs = gammalib.GObservations() | 304 | selected_obs = gammalib.GObservations() |
| 303 | 305 | ||
| 306 | + # Set models | ||
| 307 | + selected_obs.models(self.obs().models()) | ||
| 308 | + | ||
| 304 | # Write input observation container into logger | 309 | # Write input observation container into logger |
| 305 | self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation') | 310 | self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation') |
| 306 | 311 |