Commit bb9398a3941fba1a094b4fe2780113ddecace447
1 parent
0b326d4d
On/Off
Showing
1 changed file
with
58 additions
and
5 deletions
cscripts/csresspec.py
| ... | ... | @@ -375,7 +375,7 @@ class csresspec(ctools.csobservation): |
| 375 | 375 | # If name not empty add leading blank |
| 376 | 376 | if obs_id != '': |
| 377 | 377 | obs_id = ' ' + obs_id |
| 378 | - table.extname('RESIDUALS ' + obs_id) | |
| 378 | + table.extname('RESIDUALS' + obs_id) | |
| 379 | 379 | |
| 380 | 380 | # Add Header card to specify algorithm used for residual computation |
| 381 | 381 | table.card('ALGORITHM', self['algorithm'].string(), |
| ... | ... | @@ -395,7 +395,7 @@ class csresspec(ctools.csobservation): |
| 395 | 395 | Append optional column to residual table |
| 396 | 396 | :param table: `~gammalib.GFitsBinTable' |
| 397 | 397 | :param name: str column name |
| 398 | - :param data: `gammalib.GNdarray' table to be filled into new column | |
| 398 | + :param data: `gammalib.GNdarray' data to be filled into new column | |
| 399 | 399 | :return: `~gammalib.GFitsBinTable' |
| 400 | 400 | """ |
| 401 | 401 | # Check size compatibility |
| ... | ... | @@ -519,11 +519,64 @@ class csresspec(ctools.csobservation): |
| 519 | 519 | # otherwise, if On/Off |
| 520 | 520 | elif obs[0].classname() == 'GCTAOnOffObservation': |
| 521 | 521 | |
| 522 | - ## Calculate Model and residuals | |
| 522 | + onoff = obs[0] | |
| 523 | 523 | |
| 524 | - ## Calculate models of individual components if requested | |
| 524 | + ## Calculate Counts, Model and residuals | |
| 525 | + | |
| 526 | + # On spectrum | |
| 527 | + counts = onoff.on_spec().counts_spectrum() | |
| 528 | + | |
| 529 | + # Model for On region | |
| 530 | + background = onoff.model_background( | |
| 531 | + obs.models()).counts_spectrum() | |
| 532 | + alpha = onoff.on_spec().backscal_spectrum() | |
| 533 | + model = background.copy() | |
| 534 | + model *= alpha | |
| 535 | + model += onoff.model_gamma(obs.models()).counts_spectrum() | |
| 536 | + | |
| 537 | + # On Residuals | |
| 538 | + residuals = obsutils.residuals(self, counts, model) | |
| 539 | + | |
| 540 | + # Extract energy bounds | |
| 541 | + ebounds = onoff.on_spec().ebounds() | |
| 542 | + | |
| 543 | + # Fill results table | |
| 544 | + table = self._residuals_table(obs_id, ebounds, counts, model, | |
| 545 | + residuals) | |
| 546 | + | |
| 547 | + # Get Off spectrum and add to table | |
| 548 | + counts_off = onoff.off_spec().counts_spectrum() | |
| 549 | + table = self._append_column(table, 'Counts_Off', | |
| 550 | + counts_off) | |
| 551 | + | |
| 552 | + # Add background/Off model to table | |
| 553 | + table = self._append_column(table, 'Model_Off', | |
| 554 | + background) | |
| 525 | 555 | |
| 526 | - pass | |
| 556 | + # Calculate Off residuals and add to table | |
| 557 | + residuals_off = obsutils.residuals(self, counts_off, background) | |
| 558 | + table = self._append_column(table, 'Residuals_Off', | |
| 559 | + residuals_off) | |
| 560 | + | |
| 561 | + ## Calculate models of individual components if requested | |
| 562 | + if self['components'].boolean(): | |
| 563 | + for component in obs.models(): | |
| 564 | + # If background, append the values already calculated | |
| 565 | + if component.classname() == 'GCTAModelIrfBackground': | |
| 566 | + background *= onoff.on_spec().backscal_spectrum() | |
| 567 | + table = self._append_column(table, 'Background', | |
| 568 | + background) | |
| 569 | + # Otherwise calculate gamma component and append to Table | |
| 570 | + else: | |
| 571 | + # Create observation container for individual components | |
| 572 | + model_cont = gammalib.GModels() | |
| 573 | + model_cont.append(component) | |
| 574 | + # Calculate gamma model | |
| 575 | + model = onoff.model_gamma(model_cont) | |
| 576 | + model = model.counts_spectrum() | |
| 577 | + # Append to table | |
| 578 | + table = self._append_column(table, component.name(), | |
| 579 | + model) | |
| 527 | 580 | |
| 528 | 581 | ## Append results table to output file |
| 529 | 582 | self._fits.append(table) | ... | ... |