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