ctools  2.0.0
 All Classes Namespaces Files Functions Variables Macros Pages
ctexpcube.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * ctexpcube - Exposure cube generation tool *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2014-2022 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file ctexpcube.cpp
23  * @brief Exposure cube generation tool implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <cstdio>
32 #include "ctexpcube.hpp"
33 #include "GTools.hpp"
34 #include "GWcs.hpp"
35 
36 /* __ Method name definitions ____________________________________________ */
37 #define G_SET_FROM_CNTMAP "ctexpcube::set_from_cntmap(std::string&)"
38 
39 /* __ Debug definitions __________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 
44 /*==========================================================================
45  = =
46  = Constructors/destructors =
47  = =
48  ==========================================================================*/
49 
50 /***********************************************************************//**
51  * @brief Void constructor
52  ***************************************************************************/
54 {
55  // Initialise members
56  init_members();
57 
58  // Return
59  return;
60 }
61 
62 
63 /***********************************************************************//**
64  * @brief Observations constructor
65  *
66  * param[in] obs Observation container.
67  *
68  * This method creates an instance of the class by copying an existing
69  * observations container.
70  ***************************************************************************/
71 ctexpcube::ctexpcube(const GObservations& obs) :
72  ctobservation(CTEXPCUBE_NAME, VERSION, obs)
73 {
74  // Initialise members
75  init_members();
76 
77  // Return
78  return;
79 }
80 
81 
82 
83 /***********************************************************************//**
84  * @brief Command line constructor
85  *
86  * @param[in] argc Number of arguments in command line.
87  * @param[in] argv Array of command line arguments.
88  ***************************************************************************/
89 ctexpcube::ctexpcube(int argc, char *argv[]) :
90  ctobservation(CTEXPCUBE_NAME, VERSION, argc, argv)
91 {
92  // Initialise members
93  init_members();
94 
95  // Return
96  return;
97 }
98 
99 
100 /***********************************************************************//**
101  * @brief Copy constructor
102  *
103  * @param[in] app Application.
104  ***************************************************************************/
106 {
107  // Initialise members
108  init_members();
109 
110  // Copy members
111  copy_members(app);
112 
113  // Return
114  return;
115 }
116 
117 
118 /***********************************************************************//**
119  * @brief Destructor
120  ***************************************************************************/
122 {
123  // Free members
124  free_members();
125 
126  // Return
127  return;
128 }
129 
130 
131 /*==========================================================================
132  = =
133  = Operators =
134  = =
135  ==========================================================================*/
136 
137 /***********************************************************************//**
138  * @brief Assignment operator
139  *
140  * @param[in] app Application.
141  * @return Application.
142  ***************************************************************************/
144 {
145  // Execute only if object is not identical
146  if (this != &app) {
147 
148  // Copy base class members
149  this->ctobservation::operator=(app);
150 
151  // Free members
152  free_members();
153 
154  // Initialise members
155  init_members();
156 
157  // Copy members
158  copy_members(app);
159 
160  } // endif: object was not identical
161 
162  // Return this object
163  return *this;
164 }
165 
166 
167 /*==========================================================================
168  = =
169  = Public methods =
170  = =
171  ==========================================================================*/
172 
173 /***********************************************************************//**
174  * @brief Clear ctexpcube tool
175  *
176  * Clears ctexpcube tool.
177  ***************************************************************************/
179 {
180  // Free members
181  free_members();
183  this->ctool::free_members();
184 
185  // Clear base class (needed to conserve tool name and version)
186  this->GApplication::clear();
187 
188  // Initialise members
189  this->ctool::init_members();
191  init_members();
192 
193  // Write header into logger
194  log_header();
195 
196  // Return
197  return;
198 }
199 
200 
201 /***********************************************************************//**
202  * @brief Generate the exposure cube(s).
203  *
204  * This method reads the task parameters from the parfile, sets up the
205  * observation container, loops over all CTA observations in the container
206  * and generates an exposure cube from the CTA observations.
207  ***************************************************************************/
209 {
210  // Get task parameters
211  get_parameters();
212 
213  // Warn if there are not enough energy bins
214  log_string(TERSE, warn_too_few_energies(m_expcube.energies()));
215 
216  // Write input observation container into logger
217  log_observations(NORMAL, m_obs, "Input observation");
218 
219  // Initialise exposure cube
220  init_cube();
221 
222  // Write header into logger
223  log_header1(TERSE, "Generate exposure cube");
224 
225  // Set pointer to logger dependent on chattiness
226  GLog* logger = (logNormal()) ? &log : NULL;
227 
228  // Fill exposure cube
229  m_expcube.fill(m_obs, logger);
230 
231  // Write exposure cube into logger
232  log_string(EXPLICIT, m_expcube.print(m_chatter));
233 
234  // Optionally publish exposure cube
235  if (m_publish) {
236  publish();
237  }
238 
239  // Return
240  return;
241 }
242 
243 
244 /***********************************************************************//**
245  * @brief Save exposure cube
246  *
247  * Saves the exposure cube into a FITS file.
248  ***************************************************************************/
249 void ctexpcube::save(void)
250 {
251  // Write header into logger
252  log_header1(TERSE, "Save exposure cube");
253 
254  // Save exposure cube if filename is valid and the exposure cube is not
255  // empty
256  if ((*this)["outcube"].is_valid() && !m_expcube.cube().is_empty()) {
257 
258  // Get exposure cube filename
259  m_outcube = (*this)["outcube"].filename();
260 
261  // Save exposure cube
262  m_expcube.save(m_outcube, clobber());
263 
264  // Stamp exposure cube
265  stamp(m_outcube);
266 
267  }
268 
269  // Write into logger what has been done
270  std::string fname = (m_outcube.is_empty()) ? "NONE" : m_outcube.url();
271  if (m_expcube.cube().is_empty()) {
272  fname.append(" (cube is empty, no file created)");
273  }
274  log_value(NORMAL, "Exposure cube file", fname);
275 
276  // Return
277  return;
278 }
279 
280 
281 /***********************************************************************//**
282  * @brief Publish exposure cube
283  *
284  * @param[in] name Exposure cube name.
285  ***************************************************************************/
286 void ctexpcube::publish(const std::string& name)
287 {
288  // Write header into logger
289  log_header1(TERSE, "Publish exposure cube");
290 
291  // Set default name if user name is empty
292  std::string user_name(name);
293  if (user_name.empty()) {
294  user_name = CTEXPCUBE_NAME;
295  }
296 
297  // Write exposure cube name into logger
298  log_value(NORMAL, "Counts cube name", user_name);
299 
300  // Publish exposure cube
301  m_expcube.cube().publish(user_name);
302 
303  // Return
304  return;
305 }
306 
307 
308 /*==========================================================================
309  = =
310  = Private methods =
311  = =
312  ==========================================================================*/
313 
314 /***********************************************************************//**
315  * @brief Initialise class members
316  ***************************************************************************/
318 {
319  // Initialise user parameters
320  m_outcube.clear();
321  m_addbounds = true;
322  m_publish = false;
323  m_chatter = static_cast<GChatter>(2);
324 
325  // Initialise protected members
326  m_expcube.clear();
327 
328  // Return
329  return;
330 }
331 
332 
333 /***********************************************************************//**
334  * @brief Copy class members
335  *
336  * @param[in] app Application.
337  ***************************************************************************/
339 {
340  // Copy user parameters
341  m_outcube = app.m_outcube;
342  m_addbounds = app.m_addbounds;
343  m_publish = app.m_publish;
344  m_chatter = app.m_chatter;
345 
346  // Copy protected members
347  m_expcube = app.m_expcube;
348 
349  // Return
350  return;
351 }
352 
353 
354 /***********************************************************************//**
355  * @brief Delete class members
356  ***************************************************************************/
358 {
359  // Return
360  return;
361 }
362 
363 
364 /***********************************************************************//**
365  * @brief Get application parameters
366  *
367  * Get all task parameters from parameter file or (if required) by querying
368  * the user. The parameters are read in the correct order.
369  ***************************************************************************/
371 {
372  // Setup observations from "inobs" parameter. Do not accept counts cubes.
373  setup_observations(m_obs, true, true, false);
374 
375  // If the "incube" file name is valid then setup the exposure cube from
376  // the counts cube. Otherwise create a counts cube from the user
377  // parameters
378  GCTAEventCube cube = (*this)["incube"].is_valid()
379  ? GCTAEventCube((*this)["incube"].filename())
380  : create_cube(m_obs);
381 
382  // Define exposure cube
383  m_expcube = GCTACubeExposure(cube);
384 
385  // Get remaining parameters
386  m_addbounds = (*this)["addbounds"].boolean();
387  m_publish = (*this)["publish"].boolean();
388  m_chatter = static_cast<GChatter>((*this)["chatter"].integer());
389 
390  // If needed later, query output filename now
391  if (read_ahead()) {
392  (*this)["outcube"].query();
393  }
394 
395  // Write parameters into logger
396  log_parameters(TERSE);
397 
398  // Return
399  return;
400 }
401 
402 
403 /***********************************************************************//**
404  * @brief Initialise exposure cube
405  *
406  * Initialise the exposure cube.
407  ***************************************************************************/
409 {
410  // Write header into logger
411  log_header1(TERSE, "Initialise exposure cube");
412 
413  // Extract exposure cube definition
414  const GWcs* proj = static_cast<const GWcs*>(m_expcube.cube().projection());
415  std::string wcs = m_expcube.cube().projection()->code();
416  std::string coords = m_expcube.cube().projection()->coordsys();
417  double x = proj->crval(0);
418  double y = proj->crval(1);
419  double dx = proj->cdelt(0);
420  double dy = proj->cdelt(1);
421  int nx = m_expcube.cube().nx();
422  int ny = m_expcube.cube().ny();
423 
424  // Extract energies
425  GEnergies energies = m_expcube.energies();
426 
427  // If requested, insert energies at all event list energy boundaries
428  if (m_addbounds) {
429 
430  // Loop over all observations
431  for (int i = 0; i < m_obs.size(); ++i) {
432 
433  // Get observation and continue only if it is a CTA observation
434  const GCTAObservation* cta = dynamic_cast<const GCTAObservation*>
435  (m_obs[i]);
436 
437  // Skip observation if it's not a CTA observation
438  if (cta == NULL) {
439  continue;
440  }
441 
442  // Skip observation if it does not contain an event list
443  if (cta->eventtype() != "EventList") {
444  continue;
445  }
446 
447  // Insert energy boundaries
448  energies = insert_energy_boundaries(energies, *cta);
449 
450  } // endfor: looped over all observations
451 
452  } // endif: energy bin insertion requested
453 
454  // Setup exposure cube
455  m_expcube = GCTACubeExposure(wcs, coords, x, y, dx, dy, nx, ny, energies);
456 
457  // Write exposure cube in logger
458  log_string(NORMAL, m_expcube.print(m_chatter));
459 
460  // Return
461  return;
462 }
void free_members(void)
Delete class members.
Definition: ctexpcube.cpp:357
void setup_observations(GObservations &obs, const bool &response=true, const bool &list=true, const bool &cube=true)
Setup observation container.
Definition: ctool.cpp:431
GChatter m_chatter
Chattiness.
Definition: ctexpcube.hpp:76
void init_members(void)
Initialise class members.
Definition: ctexpcube.cpp:317
void process(void)
Generate the exposure cube(s).
Definition: ctexpcube.cpp:208
GCTACubeExposure m_expcube
Exposure cube.
Definition: ctexpcube.hpp:79
void init_members(void)
Initialise class members.
Definition: ctool.cpp:321
bool m_addbounds
Add energies at boundaries?
Definition: ctexpcube.hpp:74
bool m_publish
Publish exposure cube?
Definition: ctexpcube.hpp:75
ctexpcube & operator=(const ctexpcube &app)
Assignment operator.
Definition: ctexpcube.cpp:143
void copy_members(const ctexpcube &app)
Copy class members.
Definition: ctexpcube.cpp:338
void log_parameters(const GChatter &chatter)
Log application parameters.
Definition: ctool.cpp:1208
Base class for observation tools.
Exposure cube generation tool definition.
void free_members(void)
Delete class members.
void free_members(void)
Delete class members.
Definition: ctool.cpp:357
void save(void)
Save exposure cube.
Definition: ctexpcube.cpp:249
void init_members(void)
Initialise class members.
ctexpcube(void)
Void constructor.
Definition: ctexpcube.cpp:53
GCTAEventCube create_cube(const GObservations &obs)
Create a CTA event cube from user parameters.
Definition: ctool.cpp:1006
void publish(const std::string &name="")
Publish exposure cube.
Definition: ctexpcube.cpp:286
void init_cube(void)
Initialise exposure cube.
Definition: ctexpcube.cpp:408
std::string warn_too_few_energies(const GEnergies &energies) const
Set warning string if there are too few energies.
Definition: ctool.cpp:2386
ctobservation & operator=(const ctobservation &app)
Assignment operator.
const bool & read_ahead(void) const
Signal whether parameters should be read ahead.
Definition: ctool.hpp:177
void get_parameters(void)
Get application parameters.
Definition: ctexpcube.cpp:370
virtual ~ctexpcube(void)
Destructor.
Definition: ctexpcube.cpp:121
void clear(void)
Clear ctexpcube tool.
Definition: ctexpcube.cpp:178
GFilename m_outcube
Output exposure cube file.
Definition: ctexpcube.hpp:73
#define CTEXPCUBE_NAME
Definition: ctexpcube.hpp:36
void log_observations(const GChatter &chatter, const GObservations &obs, const std::string &what="Observation")
Log observation container.
Definition: ctool.cpp:1251
GObservations m_obs
Observation container.
Exposure cube generation tool.
Definition: ctexpcube.hpp:44
GEnergies insert_energy_boundaries(const GEnergies &energies, const GCTAObservation &obs)
Insert observation energy boundaries into list of energies.
Definition: ctool.cpp:2119