Commit 96842f77dee981121f2d04e11f4efc6973603b4c
1 parent
f3f1a514
ctbin::cube() now returns a reference to a member
The building of a cuve on the fly raised issue with memory going out of scope. This is fixed by making the CTA event cube a member of ctbin, and having the cube() method returning a reference to that member
Showing
16 changed files
with
98 additions
and
72 deletions
cscripts/csinfo.py
| 1 | 1 | #!/usr/bin/env python |
| 2 | 2 | # ========================================================================== |
| 3 | -# Print info about Gammalib / ctools to the console. | |
| 3 | +# Print info about Gammalib / ctools to the console | |
| 4 | 4 | # |
| 5 | 5 | # Copyright (C) 2015-2016 Christoph Deil |
| 6 | 6 | # |
| ... | ... | @@ -87,8 +87,8 @@ def get_command_output(cmd): |
| 87 | 87 | output : str |
| 88 | 88 | Command output |
| 89 | 89 | """ |
| 90 | - # Try to import getoutput from subprocess, alternatively get it from | |
| 91 | - # commands | |
| 90 | + # Try to import getoutput from the "subprocess" module, alternatively get | |
| 91 | + # it from the "commands" module | |
| 92 | 92 | try: |
| 93 | 93 | from subprocess import getoutput |
| 94 | 94 | except ImportError: |
| ... | ... | @@ -287,8 +287,7 @@ def get_pkg_config_info(info, library): |
| 287 | 287 | # Execute command |
| 288 | 288 | out = get_command_output(cmd) |
| 289 | 289 | |
| 290 | - # If the command returns 'was not found' then pkg-config is not | |
| 291 | - # available | |
| 290 | + # If the command returns "not found" then pkg-config is not available | |
| 292 | 291 | if 'not found' in out: |
| 293 | 292 | out = 'Not available' |
| 294 | 293 | |
| ... | ... | @@ -322,8 +321,8 @@ def csinfo(argv): |
| 322 | 321 | """ |
| 323 | 322 | Print information about Gammalib and ctools to the console |
| 324 | 323 | """ |
| 325 | - # If there are no command line arguments then print help and exit with | |
| 326 | - # success | |
| 324 | + # If there are no command line arguments then print help text and exit | |
| 325 | + # with success | |
| 327 | 326 | if len(argv) <= 1: |
| 328 | 327 | csinfo_print_help() |
| 329 | 328 | sys.exit(0) | ... | ... |
cscripts/csobsdef.py
| ... | ... | @@ -28,7 +28,7 @@ import ctools |
| 28 | 28 | # ============== # |
| 29 | 29 | class csobsdef(ctools.cscript): |
| 30 | 30 | """ |
| 31 | - Creates an observation definition file from a pointing list. | |
| 31 | + Creates an observation definition file from a pointing list | |
| 32 | 32 | |
| 33 | 33 | The pointing list is a comma-separated value ASCII file with header |
| 34 | 34 | keywords in the first row followed by a list of pointings (one |
| ... | ... | @@ -92,8 +92,7 @@ class csobsdef(ctools.cscript): |
| 92 | 92 | self._pntdef = gammalib.GCsv() |
| 93 | 93 | self._tmin = 0.0 |
| 94 | 94 | |
| 95 | - # Initialise application by calling the appropriate class | |
| 96 | - # constructor. | |
| 95 | + # Initialise application by calling the appropriate class constructor | |
| 97 | 96 | self._init_cscript(argv) |
| 98 | 97 | |
| 99 | 98 | # Return |
| ... | ... | @@ -128,6 +127,20 @@ class csobsdef(ctools.cscript): |
| 128 | 127 | The method creates an XML element so that that the response XML |
| 129 | 128 | writer will write the database and response name into the |
| 130 | 129 | observation definition file. |
| 130 | + | |
| 131 | + Parameters | |
| 132 | + ---------- | |
| 133 | + obs : `~gammalib.GCTAObservation` | |
| 134 | + CTA observation | |
| 135 | + caldb : str | |
| 136 | + Calibration database | |
| 137 | + irf : str | |
| 138 | + Instrument response function | |
| 139 | + | |
| 140 | + Returns | |
| 141 | + obs : `~gammalib.GCTAObservation` | |
| 142 | + CTA observation with response attached | |
| 143 | + ------- | |
| 131 | 144 | """ |
| 132 | 145 | # Create XML element |
| 133 | 146 | xml = gammalib.GXmlElement() |
| ... | ... | @@ -156,7 +169,7 @@ class csobsdef(ctools.cscript): |
| 156 | 169 | Raises |
| 157 | 170 | ------ |
| 158 | 171 | RuntimeError |
| 159 | - Invalid pointing definition file format. | |
| 172 | + Invalid pointing definition file format | |
| 160 | 173 | """ |
| 161 | 174 | # Switch screen logging on in debug mode |
| 162 | 175 | if self._logDebug(): |
| ... | ... | @@ -170,21 +183,25 @@ class csobsdef(ctools.cscript): |
| 170 | 183 | self._log('\n') |
| 171 | 184 | self._log.header1('Creating observation definition XML file') |
| 172 | 185 | |
| 173 | - # Load pointing definition file if it is not already set | |
| 186 | + # Load pointing definition file if it is not already set. Extract | |
| 187 | + # the number of columns and pointings | |
| 174 | 188 | if self._pntdef.size() == 0: |
| 175 | 189 | self._pntdef = gammalib.GCsv(self['inpnt'].filename(), ',') |
| 176 | 190 | ncols = self._pntdef.ncols() |
| 177 | 191 | npnt = self._pntdef.nrows()-1 |
| 178 | 192 | |
| 179 | - # Throw an exception is there is no header information | |
| 193 | + # Raise an exception if there is no header information | |
| 180 | 194 | if self._pntdef.nrows() < 1: |
| 181 | 195 | raise RuntimeError('No header found in pointing definition file.') |
| 182 | 196 | |
| 183 | 197 | # Clear observation container |
| 184 | 198 | self._obs.clear() |
| 199 | + | |
| 200 | + # Initialise observation identifier counter | |
| 185 | 201 | identifier = 1 |
| 186 | 202 | |
| 187 | - # Extract header from pointing definition file | |
| 203 | + # Extract header columns from pointing definition file and put them | |
| 204 | + # into a list | |
| 188 | 205 | header = [] |
| 189 | 206 | for col in range(ncols): |
| 190 | 207 | header.append(self._pntdef[0,col]) |
| ... | ... | @@ -192,28 +209,31 @@ class csobsdef(ctools.cscript): |
| 192 | 209 | # Loop over all pointings |
| 193 | 210 | for pnt in range(npnt): |
| 194 | 211 | |
| 195 | - # Set row index | |
| 212 | + # Set pointing definition CSV file row index | |
| 196 | 213 | row = pnt + 1 |
| 197 | 214 | |
| 198 | - # Create CTA observation | |
| 215 | + # Create empty CTA observation | |
| 199 | 216 | obs = gammalib.GCTAObservation() |
| 200 | 217 | |
| 201 | - # Set observation name | |
| 218 | + # Set observation name. If no observation name was given then | |
| 219 | + # use "None". | |
| 202 | 220 | if 'name' in header: |
| 203 | 221 | name = self._pntdef[row, header.index('name')] |
| 204 | 222 | else: |
| 205 | 223 | name = 'None' |
| 206 | 224 | obs.name(name) |
| 207 | 225 | |
| 208 | - # Set identifier | |
| 226 | + # Set observation identifier. If no observation identified was | |
| 227 | + # given the use the internal counter. | |
| 209 | 228 | if 'id' in header: |
| 210 | - id_ = self._pntdef[row, header.index('id')] | |
| 229 | + obsid = self._pntdef[row, header.index('id')] | |
| 211 | 230 | else: |
| 212 | - id_ = '%6.6d' % identifier | |
| 231 | + obsid = '%6.6d' % identifier | |
| 213 | 232 | identifier += 1 |
| 214 | - obs.id(id_) | |
| 233 | + obs.id(obsid) | |
| 215 | 234 | |
| 216 | - # Set pointing | |
| 235 | + # Set pointing. Either use "ra" and "dec" or "lon" and "lat". | |
| 236 | + # If none of these pairs are given then raise an exception. | |
| 217 | 237 | if 'ra' in header and 'dec' in header: |
| 218 | 238 | ra = float(self._pntdef[row, header.index('ra')]) |
| 219 | 239 | dec = float(self._pntdef[row, header.index('dec')]) |
| ... | ... | @@ -229,7 +249,8 @@ class csobsdef(ctools.cscript): |
| 229 | 249 | 'found in pointing definition file.') |
| 230 | 250 | obs.pointing(gammalib.GCTAPointing(pntdir)) |
| 231 | 251 | |
| 232 | - # Set response function | |
| 252 | + # Set response function. If no "caldb" or "irf" information is | |
| 253 | + # provided then use the user parameter values. | |
| 233 | 254 | if 'caldb' in header: |
| 234 | 255 | caldb = self._pntdef[row, header.index('caldb')] |
| 235 | 256 | else: |
| ... | ... | @@ -241,14 +262,16 @@ class csobsdef(ctools.cscript): |
| 241 | 262 | if caldb != '' and irf != '': |
| 242 | 263 | obs = self._set_response(obs, caldb, irf) |
| 243 | 264 | |
| 244 | - # Set deadtime correction factor | |
| 265 | + # Set deadtime correction factor. If no information is provided | |
| 266 | + # then use the user parameter value "deadc". | |
| 245 | 267 | if 'deadc' in header: |
| 246 | 268 | deadc = float(self._pntdef[row, header.index('deadc')]) |
| 247 | 269 | else: |
| 248 | 270 | deadc = self['deadc'].real() |
| 249 | 271 | obs.deadc(deadc) |
| 250 | 272 | |
| 251 | - # Set Good Time Interval | |
| 273 | + # Set Good Time Interval. If no information is provided then use | |
| 274 | + # the user parameter value "duration". | |
| 252 | 275 | if 'duration' in header: |
| 253 | 276 | duration = float(self._pntdef[row, header.index('duration')]) |
| 254 | 277 | else: |
| ... | ... | @@ -263,7 +286,9 @@ class csobsdef(ctools.cscript): |
| 263 | 286 | obs.ontime(gti.ontime()) |
| 264 | 287 | obs.livetime(gti.ontime()*deadc) |
| 265 | 288 | |
| 266 | - # Set Energy Boundaries | |
| 289 | + # Set Energy Boundaries. If no "emin" or "emax" information is | |
| 290 | + # provided then use the user parameter values in case they are | |
| 291 | + # valid. | |
| 267 | 292 | has_emin = False |
| 268 | 293 | has_emax = False |
| 269 | 294 | if 'emin' in header: |
| ... | ... | @@ -285,7 +310,8 @@ class csobsdef(ctools.cscript): |
| 285 | 310 | ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, 'TeV'), |
| 286 | 311 | gammalib.GEnergy(emax, 'TeV')) |
| 287 | 312 | |
| 288 | - # Set ROI | |
| 313 | + # Set ROI. If no ROI radius is provided then use the user | |
| 314 | + # parameters "rad". | |
| 289 | 315 | has_roi = False |
| 290 | 316 | if 'rad' in header: |
| 291 | 317 | rad = float(self._pntdef[row, header.index('rad')]) |
| ... | ... | @@ -298,17 +324,17 @@ class csobsdef(ctools.cscript): |
| 298 | 324 | roi = gammalib.GCTARoi(gammalib.GCTAInstDir(pntdir), rad) |
| 299 | 325 | |
| 300 | 326 | # Create an empty event list |
| 301 | - list_ = gammalib.GCTAEventList() | |
| 302 | - list_.gti(gti) | |
| 327 | + event_list = gammalib.GCTAEventList() | |
| 328 | + event_list.gti(gti) | |
| 303 | 329 | |
| 304 | - # Set optional information | |
| 330 | + # If available, set the energy boundaries and the ROI | |
| 305 | 331 | if has_ebounds: |
| 306 | - list_.ebounds(ebounds) | |
| 332 | + event_list.ebounds(ebounds) | |
| 307 | 333 | if has_roi: |
| 308 | - list_.roi(roi) | |
| 334 | + event_list.roi(roi) | |
| 309 | 335 | |
| 310 | 336 | # Attach event list to CTA observation |
| 311 | - obs.events(list_) | |
| 337 | + obs.events(event_list) | |
| 312 | 338 | |
| 313 | 339 | # Write observation into logger |
| 314 | 340 | if self._logExplicit(): | ... | ... |
cscripts/obsutils.py
| ... | ... | @@ -136,15 +136,11 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0, |
| 136 | 136 | # cubes and append them to the observation |
| 137 | 137 | if len(sim.obs()) > 1: |
| 138 | 138 | |
| 139 | - # First get the cube. We need this because ctbin builds a cube on | |
| 140 | - # the fly. We should avoid this and make the cube part of ctbin | |
| 141 | - cube = bin.cube() | |
| 142 | - | |
| 143 | 139 | # Get xref, yref, coordsys and proj from counts cube |
| 144 | - _xref = cube.counts().projection().crval(0) | |
| 145 | - _yref = cube.counts().projection().crval(1) | |
| 146 | - _coordsys = cube.counts().projection().coordsys() | |
| 147 | - _proj = cube.counts().projection().code() | |
| 140 | + _xref = bin.cube().counts().projection().crval(0) | |
| 141 | + _yref = bin.cube().counts().projection().crval(1) | |
| 142 | + _coordsys = bin.cube().counts().projection().coordsys() | |
| 143 | + _proj = bin.cube().counts().projection().code() | |
| 148 | 144 | |
| 149 | 145 | # Kluge: map "EQU" to "CEL" coordinate system (not sure why the |
| 150 | 146 | # GSkyProjection::coordsys returns "EQU" instead of "CEL" | ... | ... |
examples/show_butterfly.py
examples/show_model.py
examples/show_obs.py
examples/show_pha.py
examples/show_pull_evolution.py
examples/show_pull_histogram.py
examples/show_response.py
examples/show_sensitivity.py
examples/show_spectrum.py
examples/show_ts_distribution.py
pyext/ctbin.i
src/ctbin/ctbin.cpp
| ... | ... | @@ -281,6 +281,10 @@ void ctbin::run(void) |
| 281 | 281 | |
| 282 | 282 | } // endfor: looped over observations |
| 283 | 283 | |
| 284 | + // Build event cube (needs to come before obs_cube() since this method | |
| 285 | + // relies on correct setting of m_cube) | |
| 286 | + m_cube = GCTAEventCube(m_counts, m_weights, m_ebounds, m_gti); | |
| 287 | + | |
| 284 | 288 | // Set a single cube in the observation container |
| 285 | 289 | obs_cube(); |
| 286 | 290 | |
| ... | ... | @@ -400,6 +404,7 @@ void ctbin::init_members(void) |
| 400 | 404 | m_weights.clear(); |
| 401 | 405 | m_ebounds.clear(); |
| 402 | 406 | m_gti.clear(); |
| 407 | + m_cube.clear(); | |
| 403 | 408 | m_ontime = 0.0; |
| 404 | 409 | m_livetime = 0.0; |
| 405 | 410 | |
| ... | ... | @@ -427,6 +432,7 @@ void ctbin::copy_members(const ctbin& app) |
| 427 | 432 | m_weights = app.m_weights; |
| 428 | 433 | m_ebounds = app.m_ebounds; |
| 429 | 434 | m_gti = app.m_gti; |
| 435 | + m_cube = app.m_cube; | |
| 430 | 436 | m_ontime = app.m_ontime; |
| 431 | 437 | m_livetime = app.m_livetime; |
| 432 | 438 | ... | ... |
src/ctbin/ctbin.hpp
| ... | ... | @@ -81,7 +81,7 @@ public: |
| 81 | 81 | void save(void); |
| 82 | 82 | void publish(const std::string& name = ""); |
| 83 | 83 | const GObservations& obs(void) const; |
| 84 | - GCTAEventCube cube(void) const; | |
| 84 | + const GCTAEventCube& cube(void) const; | |
| 85 | 85 | |
| 86 | 86 | protected: |
| 87 | 87 | // Protected methods |
| ... | ... | @@ -95,25 +95,28 @@ protected: |
| 95 | 95 | std::vector<bool> cube_layer_usage(const GEbounds& ebounds) const; |
| 96 | 96 | |
| 97 | 97 | // User parameters |
| 98 | - GFilename m_outcube; //!< Output counts map file name | |
| 99 | - bool m_usepnt; //!< Use pointing instead of xref/yref parameters | |
| 100 | - bool m_publish; //!< Publish counts cube? | |
| 101 | - GChatter m_chatter; //!< Chattiness | |
| 98 | + GFilename m_outcube; //!< Output counts map file name | |
| 99 | + bool m_usepnt; //!< Use pointing instead of xref/yref parameters | |
| 100 | + bool m_publish; //!< Publish counts cube? | |
| 101 | + GChatter m_chatter; //!< Chattiness | |
| 102 | 102 | |
| 103 | 103 | // Protected members |
| 104 | - GObservations m_obs; //!< Observation container | |
| 105 | - GSkyMap m_counts; //!< Event cube counts | |
| 106 | - GSkyMap m_weights; //!< Event cube weights | |
| 107 | - GEbounds m_ebounds; //!< Energy boundaries | |
| 108 | - GGti m_gti; //!< Good time intervals | |
| 109 | - double m_ontime; //!< Total ontime | |
| 110 | - double m_livetime; //!< Total livetime | |
| 104 | + GObservations m_obs; //!< Observation container | |
| 105 | + GSkyMap m_counts; //!< Event cube counts | |
| 106 | + GSkyMap m_weights; //!< Event cube weights | |
| 107 | + GEbounds m_ebounds; //!< Energy boundaries | |
| 108 | + GGti m_gti; //!< Good time intervals | |
| 109 | + GCTAEventCube m_cube; //!< Events cube (for cube() method) | |
| 110 | + double m_ontime; //!< Total ontime | |
| 111 | + double m_livetime; //!< Total livetime | |
| 111 | 112 | }; |
| 112 | 113 | |
| 113 | 114 | /***********************************************************************//** |
| 114 | 115 | * @brief Return observation container |
| 115 | 116 | * |
| 116 | 117 | * @return Reference to observation container |
| 118 | + * | |
| 119 | + * Returns a reference to an observation container. | |
| 117 | 120 | ***************************************************************************/ |
| 118 | 121 | inline |
| 119 | 122 | const GObservations& ctbin::obs(void) const |
| ... | ... | @@ -125,18 +128,14 @@ const GObservations& ctbin::obs(void) const |
| 125 | 128 | /***********************************************************************//** |
| 126 | 129 | * @brief Return event cube |
| 127 | 130 | * |
| 128 | - * @return Event cube | |
| 131 | + * @return Reference to event cube | |
| 129 | 132 | * |
| 130 | - * Returns a event cube built from the ctbin member information. | |
| 133 | + * Returns a reference to the event cube. | |
| 131 | 134 | ***************************************************************************/ |
| 132 | 135 | inline |
| 133 | -GCTAEventCube ctbin::cube(void) const | |
| 136 | +const GCTAEventCube& ctbin::cube(void) const | |
| 134 | 137 | { |
| 135 | - // Build event cube | |
| 136 | - GCTAEventCube cube(m_counts, m_weights, m_ebounds, m_gti); | |
| 137 | - | |
| 138 | - // Return cube | |
| 139 | - return cube; | |
| 138 | + return m_cube; | |
| 140 | 139 | } |
| 141 | 140 | |
| 142 | 141 | #endif /* CTBIN_HPP */ | ... | ... |