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