Commit 26046f94fef61519ab99f0f6828ce818df14318f

Authored by Jürgen Knödlseder
1 parent 58732e19

Defer import of ROOT module in csroot2caldb to run method (#2652)

ChangeLog
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 * Version 1.6.0 released 3 * Version 1.6.0 released
4 ======================== 4 ========================
5 5
6 - Do no longer automatically import csroot2caldb (#2652) 6 + Defer import of ROOT module in csroot2caldb to run method (#2652)
7 Transparently update parameter files (#2513) 7 Transparently update parameter files (#2513)
8 Change formula for number of true energy bins in csphagen (#2490) 8 Change formula for number of true energy bins in csphagen (#2490)
9 Remove ctools::is_valid_filename() method 9 Remove ctools::is_valid_filename() method
@@ -288,10 +288,8 @@ parameter due to a bug in the code. This bug was fixed (#2448). @@ -288,10 +288,8 @@ parameter due to a bug in the code. This bug was fixed (#2448).
288 288
289 csroot2caldb - Generates calibration database from ROOT file 289 csroot2caldb - Generates calibration database from ROOT file
290 ------------------------------------------------------------ 290 ------------------------------------------------------------
291 -The script is no longer automatically imported by a "import cscript" statement  
292 -since this led to some conflicts with the "--help" option due to the automatic  
293 -opening of a X windows link. To use the tool in Python, use  
294 -"from cscripts.csroot2caldb import csroot2caldb" (#2652). 291 +The ROOT Python module is now imported in the run method and not globally so
  292 +that no conflit arises with the "--help" option (#2652).
295 293
296 294
297 cssens - Computes sensitivity curve 295 cssens - Computes sensitivity curve
cscripts/__init__.py.in
@@ -80,6 +80,7 @@ from cscripts.csphasecrv import csphasecrv @@ -80,6 +80,7 @@ from cscripts.csphasecrv import csphasecrv
80 from cscripts.cspull import cspull 80 from cscripts.cspull import cspull
81 from cscripts.csresmap import csresmap 81 from cscripts.csresmap import csresmap
82 from cscripts.csresspec import csresspec 82 from cscripts.csresspec import csresspec
  83 +from cscripts.csroot2caldb import csroot2caldb
83 from cscripts.cssens import cssens 84 from cscripts.cssens import cssens
84 from cscripts.csspec import csspec 85 from cscripts.csspec import csspec
85 from cscripts.cssrcdetect import cssrcdetect 86 from cscripts.cssrcdetect import cssrcdetect
cscripts/csroot2caldb.py
@@ -27,13 +27,6 @@ import gammalib @@ -27,13 +27,6 @@ import gammalib
27 import ctools 27 import ctools
28 from cscripts import calutils 28 from cscripts import calutils
29 29
30 -# Optional ROOT import  
31 -try:  
32 - from ROOT import TFile, TH2F, TH3F  
33 - _has_root = True  
34 -except ImportError:  
35 - _has_root = False  
36 -  
37 30
38 # ================== # 31 # ================== #
39 # csroot2caldb class # 32 # csroot2caldb class #
@@ -329,7 +322,7 @@ class csroot2caldb(ctools.cscript): @@ -329,7 +322,7 @@ class csroot2caldb(ctools.cscript):
329 ds['PSF_FITS'].close() 322 ds['PSF_FITS'].close()
330 ds['EDISP_FITS'].close() 323 ds['EDISP_FITS'].close()
331 ds['BGD_FITS'].close() 324 ds['BGD_FITS'].close()
332 - 325 +
333 # Return 326 # Return
334 return 327 return
335 328
@@ -533,6 +526,9 @@ class csroot2caldb(ctools.cscript): @@ -533,6 +526,9 @@ class csroot2caldb(ctools.cscript):
533 ds : dict 526 ds : dict
534 Directory structure dictionary 527 Directory structure dictionary
535 """ 528 """
  529 + # Import TFile from ROOT module
  530 + from ROOT import TFile
  531 +
536 # Open ROOT performance file 532 # Open ROOT performance file
537 tfile = TFile(self['infile'].filename().url()) 533 tfile = TFile(self['infile'].filename().url())
538 534
@@ -554,7 +550,7 @@ class csroot2caldb(ctools.cscript): @@ -554,7 +550,7 @@ class csroot2caldb(ctools.cscript):
554 def _root2ea(self, tfile, irf, ds): 550 def _root2ea(self, tfile, irf, ds):
555 """ 551 """
556 Translate ROOT to CALDB effective area extension 552 Translate ROOT to CALDB effective area extension
557 - 553 +
558 The following ROOT histograms are used: 554 The following ROOT histograms are used:
559 - EffectiveAreaEtrueNoTheta2cut_offaxis -> EFFAREA 555 - EffectiveAreaEtrueNoTheta2cut_offaxis -> EFFAREA
560 - EffectiveAreaEtrueNoTheta2cut (for normalization) 556 - EffectiveAreaEtrueNoTheta2cut (for normalization)
@@ -675,6 +671,9 @@ class csroot2caldb(ctools.cscript): @@ -675,6 +671,9 @@ class csroot2caldb(ctools.cscript):
675 ndist = dists.GetNbins() 671 ndist = dists.GetNbins()
676 noffset = offsets.GetNbins() 672 noffset = offsets.GetNbins()
677 673
  674 + # Import TH2F histograms from ROOT module
  675 + from ROOT import TH2F
  676 +
678 # Create r68 and r80 histograms 677 # Create r68 and r80 histograms
679 r68 = TH2F('AngRes68Etrue_offaxis', 678 r68 = TH2F('AngRes68Etrue_offaxis',
680 'Angular resolution (68% containment)', 679 'Angular resolution (68% containment)',
@@ -688,7 +687,7 @@ class csroot2caldb(ctools.cscript): @@ -688,7 +687,7 @@ class csroot2caldb(ctools.cscript):
688 # Loop over all energy and offset angle bins 687 # Loop over all energy and offset angle bins
689 for ieng in range(neng): 688 for ieng in range(neng):
690 for ioff in range(noffset): 689 for ioff in range(noffset):
691 - 690 +
692 # Compute PDF integral 691 # Compute PDF integral
693 pdf_integral = 0.0 692 pdf_integral = 0.0
694 for idist in range(ndist): 693 for idist in range(ndist):
@@ -911,7 +910,7 @@ class csroot2caldb(ctools.cscript): @@ -911,7 +910,7 @@ class csroot2caldb(ctools.cscript):
911 'ieng=%d ioff=%d%s' % (ieng,ioff,status), 910 'ieng=%d ioff=%d%s' % (ieng,ioff,status),
912 'r68=%f r80=%f gamma=%f sigma=%f' % 911 'r68=%f r80=%f gamma=%f sigma=%f' %
913 (r_68, r_80, gamma, sigma)) 912 (r_68, r_80, gamma, sigma))
914 - 913 +
915 # Store surrent result as last result 914 # Store surrent result as last result
916 last_gamma = gamma 915 last_gamma = gamma
917 last_sigma = sigma 916 last_sigma = sigma
@@ -967,6 +966,9 @@ class csroot2caldb(ctools.cscript): @@ -967,6 +966,9 @@ class csroot2caldb(ctools.cscript):
967 # Set number of migration bins 966 # Set number of migration bins
968 nmigra = 300 967 nmigra = 300
969 968
  969 + # Import TH3F histogram from ROOT module
  970 + from ROOT import TH3F
  971 +
970 # Create migra histograms 972 # Create migra histograms
971 migra = TH3F('EestOverEtrue_offaxis', 973 migra = TH3F('EestOverEtrue_offaxis',
972 'Migration', 974 'Migration',
@@ -979,7 +981,7 @@ class csroot2caldb(ctools.cscript): @@ -979,7 +981,7 @@ class csroot2caldb(ctools.cscript):
979 981
980 # Get true energy in TeV 982 # Get true energy in TeV
981 e_true = math.pow(10.0, migra.GetXaxis().GetBinCenter(ietrue+1)) 983 e_true = math.pow(10.0, migra.GetXaxis().GetBinCenter(ietrue+1))
982 - 984 +
983 # Loop over migration values 985 # Loop over migration values
984 for imigra in range(nmigra): 986 for imigra in range(nmigra):
985 987
@@ -1738,6 +1740,13 @@ class csroot2caldb(ctools.cscript): @@ -1738,6 +1740,13 @@ class csroot2caldb(ctools.cscript):
1738 """ 1740 """
1739 Run the script 1741 Run the script
1740 """ 1742 """
  1743 + # Optional ROOT import
  1744 + try:
  1745 + from ROOT import TFile, TH2F, TH3F
  1746 + _has_root = True
  1747 + except ImportError:
  1748 + _has_root = False
  1749 +
1741 # Warn if ROOT module is missing 1750 # Warn if ROOT module is missing
1742 if not _has_root: 1751 if not _has_root:
1743 gammalib.warning('csroot2caldb', 'ROOT Python module not present, ' 1752 gammalib.warning('csroot2caldb', 'ROOT Python module not present, '
doc/source/admin/release_history/1.6.rst
@@ -20,7 +20,7 @@ Bug fixes @@ -20,7 +20,7 @@ Bug fixes
20 --------- 20 ---------
21 21
22 * [`2652 <https://cta-redmine.irap.omp.eu/issues/2652>`_] - 22 * [`2652 <https://cta-redmine.irap.omp.eu/issues/2652>`_] -
23 - Do no longer automatically import :ref:`csroot2caldb` 23 + Defer import of ``ROOT`` module in :ref:`csroot2caldb` to run method
24 * [`2456 <https://cta-redmine.irap.omp.eu/issues/2456>`_] - 24 * [`2456 <https://cta-redmine.irap.omp.eu/issues/2456>`_] -
25 Handle zero model for ``SUBDIV method`` in ``show_response.py`` 25 Handle zero model for ``SUBDIV method`` in ``show_response.py``
26 * [`2448 <https://cta-redmine.irap.omp.eu/issues/2448>`_] - 26 * [`2448 <https://cta-redmine.irap.omp.eu/issues/2448>`_] -
test/test_csroot2caldb.py
@@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
19 # 19 #
20 # ========================================================================== 20 # ==========================================================================
21 import os 21 import os
22 -from cscripts.csroot2caldb import csroot2caldb 22 +import cscripts
23 from testing import test 23 from testing import test
24 24
25 25
@@ -98,8 +98,7 @@ class Test(test): @@ -98,8 +98,7 @@ class Test(test):
98 """ 98 """
99 # Set-up csroot2caldb with a maximum of parameters so that the 99 # Set-up csroot2caldb with a maximum of parameters so that the
100 # corresponding code gets tests 100 # corresponding code gets tests
101 - #caldb = cscripts.csroot2caldb()  
102 - caldb = csroot2caldb() 101 + caldb = cscripts.csroot2caldb()
103 caldb['infile'] = self._infile 102 caldb['infile'] = self._infile
104 caldb['outdir'] = 'csroot2caldb_py1' 103 caldb['outdir'] = 'csroot2caldb_py1'
105 caldb['inst'] = 'prod3' 104 caldb['inst'] = 'prod3'