You need to sign in before continuing.

Commit 4d321317bdde2d22d5671dd365b09921127dd6b5

Authored by Jürgen Knödlseder
1 parent 5b407451

Improve csroot2caldb

Showing 1 changed file with 31 additions and 33 deletions
cscripts/csroot2caldb.py
... ... @@ -237,11 +237,9 @@ class csroot2caldb(ctools.cscript):
237 237 ds['CIF'] = gammalib.GFits(ds['BASE_PATH']+'/caldb.indx', True)
238 238  
239 239 # If file has no CIF extension than create it now
240   - try:
241   - ds['HDU_CIF'] = ds['CIF'].table('CIF')
242   - except:
  240 + if not ds['CIF'].contains('CIF'):
243 241 ds['CIF'].append(calutils.create_cif_table())
244   - ds['HDU_CIF'] = ds['CIF'].table('CIF')
  242 + ds['HDU_CIF'] = ds['CIF'].table('CIF')
245 243  
246 244 # Set IRF component filenames
247 245 ea_filename = ds['EA_PATH']+'/'+ds['EA_FILE']
... ... @@ -509,24 +507,24 @@ class csroot2caldb(ctools.cscript):
509 507 Directory structure dictionary
510 508 """
511 509 # Open ROOT performance file
512   - file = TFile(self['infile'].filename().url())
  510 + tfile = TFile(self['infile'].filename().url())
513 511  
514 512 # Create effective area
515   - self._root2ea(file, irf, ds)
  513 + self._root2ea(tfile, irf, ds)
516 514  
517 515 # Create point spread function
518   - self._root2psf(file, irf, ds)
  516 + self._root2psf(tfile, irf, ds)
519 517  
520 518 # Create energy dispersion
521   - self._root2edisp(file, irf, ds)
  519 + self._root2edisp(tfile, irf, ds)
522 520  
523 521 # Create background
524   - self._root2bgd(file, irf, ds)
  522 + self._root2bgd(tfile, irf, ds)
525 523  
526 524 # Return
527 525 return
528 526  
529   - def _root2ea(self, file, irf, ds):
  527 + def _root2ea(self, tfile, irf, ds):
530 528 """
531 529 Translate ROOT to CALDB effective area extension
532 530  
... ... @@ -536,7 +534,7 @@ class csroot2caldb(ctools.cscript):
536 534  
537 535 Parameters
538 536 ----------
539   - file : `~ROOT.TFile`
  537 + tfile : `~ROOT.TFile`
540 538 ROOT file
541 539 irf : dict
542 540 IRF metadata dictionary
... ... @@ -549,15 +547,15 @@ class csroot2caldb(ctools.cscript):
549 547 self._log.header1('Generate effective area extension')
550 548  
551 549 # Get relevant ROOT histograms
552   - etrue = file.Get('EffectiveAreaEtrue_offaxis')
553   - ereco = file.Get('EffectiveArea_offaxis')
  550 + etrue = tfile.Get('EffectiveAreaEtrue_offaxis')
  551 + ereco = tfile.Get('EffectiveArea_offaxis')
554 552  
555 553 # If requested then normalize the 2D histogram on the on-axis 1D
556 554 # histogram. This assures that the 2D histogram has the same on-axis
557 555 # effective area dependence as the 1D histogram.
558 556 if self['norm1d'].boolean():
559   - etrue_1D = file.Get('EffectiveAreaEtrue')
560   - ereco_1D = file.Get('EffectiveArea')
  557 + etrue_1D = tfile.Get('EffectiveAreaEtrue')
  558 + ereco_1D = tfile.Get('EffectiveArea')
561 559 self._renorm_onaxis(etrue, etrue_1D)
562 560 self._renorm_onaxis(ereco, ereco_1D)
563 561  
... ... @@ -609,13 +607,13 @@ class csroot2caldb(ctools.cscript):
609 607 # Return
610 608 return
611 609  
612   - def _root2psf(self, file, irf, ds):
  610 + def _root2psf(self, tfile, irf, ds):
613 611 """
614 612 Translate ROOT to CALDB point spread function extension
615 613  
616 614 Parameters
617 615 ----------
618   - file : `~ROOT.TFile`
  616 + tfile : `~ROOT.TFile`
619 617 ROOT file
620 618 irf : dict
621 619 IRF metadata dictionary
... ... @@ -629,16 +627,16 @@ class csroot2caldb(ctools.cscript):
629 627  
630 628 # King profile PSF
631 629 if self['psftype'].string() == 'King':
632   - self._root2psf_king(file, irf, ds)
  630 + self._root2psf_king(tfile, irf, ds)
633 631  
634 632 # ... otherwise use Gaussian profile PSF
635 633 else:
636   - self._root2psf_gauss(file, irf, ds)
  634 + self._root2psf_gauss(tfile, irf, ds)
637 635  
638 636 # Return
639 637 return
640 638  
641   - def _root2psf_gauss(self, file, irf, ds):
  639 + def _root2psf_gauss(self, tfile, irf, ds):
642 640 """
643 641 Translate ROOT to CALDB point spread function extension
644 642  
... ... @@ -652,7 +650,7 @@ class csroot2caldb(ctools.cscript):
652 650  
653 651 Parameters
654 652 ----------
655   - file : `~ROOT.TFile`
  653 + tfile : `~ROOT.TFile`
656 654 ROOT file
657 655 irf : dict
658 656 IRF metadata dictionary
... ... @@ -660,7 +658,7 @@ class csroot2caldb(ctools.cscript):
660 658 Directory structure dictionary
661 659 """
662 660 # Get relevant ROOT histograms
663   - r68 = file.Get('AngRes_offaxis')
  661 + r68 = tfile.Get('AngRes_offaxis')
664 662  
665 663 # Extract number of bins in histogram
666 664 neng = r68.GetXaxis().GetNbins()
... ... @@ -721,7 +719,7 @@ class csroot2caldb(ctools.cscript):
721 719 # Return
722 720 return
723 721  
724   - def _root2psf_king(self, file, irf, ds):
  722 + def _root2psf_king(self, tfile, irf, ds):
725 723 """
726 724 Translate ROOT to CALDB point spread function extension
727 725  
... ... @@ -731,7 +729,7 @@ class csroot2caldb(ctools.cscript):
731 729  
732 730 Parameters
733 731 ----------
734   - file : `~ROOT.TFile`
  732 + tfile : `~ROOT.TFile`
735 733 ROOT file
736 734 irf : dict
737 735 IRF metadata dictionary
... ... @@ -739,8 +737,8 @@ class csroot2caldb(ctools.cscript):
739 737 Directory structure dictionary
740 738 """
741 739 # Get relevant ROOT histograms
742   - r68 = file.Get('AngRes_offaxis')
743   - r80 = file.Get('AngRes80_offaxis')
  740 + r68 = tfile.Get('AngRes_offaxis')
  741 + r80 = tfile.Get('AngRes80_offaxis')
744 742  
745 743 # Extract number of bins in histogram
746 744 neng = r68.GetXaxis().GetNbins()
... ... @@ -845,7 +843,7 @@ class csroot2caldb(ctools.cscript):
845 843 # Return
846 844 return
847 845  
848   - def _root2edisp(self, file, irf, ds):
  846 + def _root2edisp(self, tfile, irf, ds):
849 847 """
850 848 Translate ROOT to CALDB energy dispersion extension
851 849  
... ... @@ -854,7 +852,7 @@ class csroot2caldb(ctools.cscript):
854 852  
855 853 Parameters
856 854 ----------
857   - file : `~ROOT.TFile`
  855 + tfile : `~ROOT.TFile`
858 856 ROOT file
859 857 irf : dict
860 858 IRF metadata dictionary
... ... @@ -867,7 +865,7 @@ class csroot2caldb(ctools.cscript):
867 865 self._log.header1('Generate energy dispersion extension')
868 866  
869 867 # Get relevant ROOT histograms
870   - matrix = file.Get('EestOverEtrue_offaxis')
  868 + matrix = tfile.Get('EestOverEtrue_offaxis')
871 869  
872 870 # Set boundaries
873 871 bounds = self._make_3D_migra(matrix, ds['HDU_EDISP'], None, '')
... ... @@ -884,7 +882,7 @@ class csroot2caldb(ctools.cscript):
884 882 # Return
885 883 return
886 884  
887   - def _root2bgd(self, file, irf, ds):
  885 + def _root2bgd(self, tfile, irf, ds):
888 886 """
889 887 Translate ROOT to CALDB background extension.
890 888  
... ... @@ -893,7 +891,7 @@ class csroot2caldb(ctools.cscript):
893 891  
894 892 Parameters
895 893 ----------
896   - file : `~ROOT.TFile`
  894 + tfile : `~ROOT.TFile`
897 895 ROOT file
898 896 irf : dict
899 897 IRF metadata dictionary
... ... @@ -906,7 +904,7 @@ class csroot2caldb(ctools.cscript):
906 904 self._log.header1('Generate 3D background extension')
907 905  
908 906 # Get relevant ROOT histograms
909   - array = file.Get('BGRatePerSqDeg_offaxis')
  907 + array = tfile.Get('BGRatePerSqDeg_offaxis')
910 908  
911 909 # Replace 2D histogram values by power law extrapolation
912 910 self._plaw_replace(array, self['bgdethres'].real())
... ... @@ -919,7 +917,7 @@ class csroot2caldb(ctools.cscript):
919 917 # histogram. This assures that the 2D histogram has the same on-axis
920 918 # effective area dependence as the 1D histogram.
921 919 if self['norm1d'].boolean():
922   - array_1D = file.Get('BGRatePerSqDeg')
  920 + array_1D = tfile.Get('BGRatePerSqDeg')
923 921 self._renorm_onaxis(array, array_1D)
924 922  
925 923 # Set boundaries
... ...