Commit 7a89f7f1f0d006a2d011ae16a790456e77647997
1 parent
c04093bc
addressed code duplication between stacked and On/Off case
Showing
1 changed file
with
27 additions
and
76 deletions
cscripts/cslightcrv.py
| ... | ... | @@ -364,73 +364,6 @@ class cslightcrv(ctools.csobservation): |
| 364 | 364 | # Return upper limit tuple |
| 365 | 365 | return ul_diff, ul_flux, ul_eflux |
| 366 | 366 | |
| 367 | - def _bin_observation(self, obs): | |
| 368 | - """ | |
| 369 | - Bin an observation if a binned analysis was requested | |
| 370 | - | |
| 371 | - Parameters | |
| 372 | - ---------- | |
| 373 | - obs : `~gammalib.GObservations` | |
| 374 | - Observation container | |
| 375 | - | |
| 376 | - Returns | |
| 377 | - ------- | |
| 378 | - obs : `~gammalib.GObservations` | |
| 379 | - Observation container where the first observation is a binned observation | |
| 380 | - """ | |
| 381 | - # Get new observation container with binned observation | |
| 382 | - new_obs = obsutils.get_stacked_obs(self, obs) | |
| 383 | - | |
| 384 | - # Extract models | |
| 385 | - models = new_obs.models() | |
| 386 | - | |
| 387 | - # Fix background models if required | |
| 388 | - if self['fix_bkg'].boolean(): | |
| 389 | - for model in models: | |
| 390 | - if model.classname() != 'GModelSky': | |
| 391 | - for par in model: | |
| 392 | - par.fix() | |
| 393 | - | |
| 394 | - # Put back models | |
| 395 | - new_obs.models(models) | |
| 396 | - | |
| 397 | - # Return new oberservation container | |
| 398 | - return new_obs | |
| 399 | - | |
| 400 | - def _gen_onoff(self, obs): | |
| 401 | - """ | |
| 402 | - Create On/Off observations if On/Off analysis was requested | |
| 403 | - | |
| 404 | - Parameters | |
| 405 | - ---------- | |
| 406 | - obs : `~gammalib.GObservations` | |
| 407 | - Observation container | |
| 408 | - | |
| 409 | - Returns | |
| 410 | - ------- | |
| 411 | - obs : `~gammalib.GObservations` | |
| 412 | - Observation container with On/Off observations | |
| 413 | - """ | |
| 414 | - # Get new observation container with binned observation | |
| 415 | - new_obs = obsutils.get_onoff_obs(self, obs) | |
| 416 | - | |
| 417 | - # Extract models | |
| 418 | - models = new_obs.models() | |
| 419 | - | |
| 420 | - # Fix background models if required | |
| 421 | - if self['fix_bkg'].boolean(): | |
| 422 | - for model in models: | |
| 423 | - if model.classname() != 'GModelSky': | |
| 424 | - for par in model: | |
| 425 | - par.fix() | |
| 426 | - | |
| 427 | - # Put back models | |
| 428 | - new_obs.models(models) | |
| 429 | - | |
| 430 | - # Return new oberservation container | |
| 431 | - return new_obs | |
| 432 | - | |
| 433 | - | |
| 434 | 367 | # Public methods |
| 435 | 368 | def run(self): |
| 436 | 369 | """ |
| ... | ... | @@ -523,15 +456,33 @@ class cslightcrv(ctools.csobservation): |
| 523 | 456 | # Retrieve observation |
| 524 | 457 | obs = select.obs() |
| 525 | 458 | |
| 526 | - # If a stacked analysis is requested then bin the events | |
| 527 | - # and compute the stacked response functions and setup | |
| 528 | - # an observation container with a single stacked observation. | |
| 529 | - if self._stacked: | |
| 530 | - obs = self._bin_observation(obs) | |
| 531 | - # Otherwise if On/Off analysis is requested generate | |
| 532 | - # the On/Off observations and response | |
| 533 | - if self._onoff: | |
| 534 | - obs = self._gen_onoff(obs) | |
| 459 | + # Deal with stacked and On/Off Observations | |
| 460 | + if self._stacked or self._onoff: | |
| 461 | + # If a stacked analysis is requested bin the events | |
| 462 | + # and compute the stacked response functions and setup | |
| 463 | + # an observation container with a single stacked observation. | |
| 464 | + if self._stacked: | |
| 465 | + new_obs = obsutils.get_stacked_obs(self, obs) | |
| 466 | + # Otherwise if On/Off analysis is requested generate | |
| 467 | + # the On/Off observations and response | |
| 468 | + elif self._onoff: | |
| 469 | + new_obs = obsutils.get_onoff_obs(self, obs) | |
| 470 | + | |
| 471 | + # Extract models | |
| 472 | + models = new_obs.models() | |
| 473 | + | |
| 474 | + # Fix background models if required | |
| 475 | + if self['fix_bkg'].boolean(): | |
| 476 | + for model in models: | |
| 477 | + if model.classname() != 'GModelSky': | |
| 478 | + for par in model: | |
| 479 | + par.fix() | |
| 480 | + | |
| 481 | + # Put back models | |
| 482 | + new_obs.models(models) | |
| 483 | + | |
| 484 | + # Continue with new oberservation container | |
| 485 | + obs = new_obs | |
| 535 | 486 | |
| 536 | 487 | # Header |
| 537 | 488 | self._log_header3(gammalib.EXPLICIT, 'Fitting the data') | ... | ... |