GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTAResponseCache.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTAResponseCache.cpp - CTA response cache class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2018-2019 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 GCTAResponseCache.cpp
23  * @brief CTA response cache class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GCTAResponseCache.hpp"
33 #include "GCTAInstDir.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 
37 /* __ Macros _____________________________________________________________ */
38 
39 /* __ Coding definitions _________________________________________________ */
40 
41 /* __ Debug definitions __________________________________________________ */
42 
43 
44 /*==========================================================================
45  = =
46  = Constructors/destructors =
47  = =
48  ==========================================================================*/
49 
50 /***********************************************************************//**
51  * @brief Void constructor
52  ***************************************************************************/
54 {
55  // Initialise class members
56  init_members();
57 
58  // Return
59  return;
60 }
61 
62 
63 /***********************************************************************//**
64  * @brief Copy constructor
65  *
66  * @param[in] cache CTA response cache.
67  ***************************************************************************/
69 {
70  // Initialise class members
71  init_members();
72 
73  // Copy members
74  copy_members(cache);
75 
76  // Return
77  return;
78 }
79 
80 
81 /***********************************************************************//**
82  * @brief Destructor
83  ***************************************************************************/
85 {
86  // Free members
87  free_members();
88 
89  // Return
90  return;
91 }
92 
93 
94 /*==========================================================================
95  = =
96  = Operators =
97  = =
98  ==========================================================================*/
99 
100 /***********************************************************************//**
101  * @brief Assignment operator
102  *
103  * @param[in] cache CTA response cache.
104  * @return CTA response cache.
105  ***************************************************************************/
107 {
108  // Execute only if object is not identical
109  if (this != &cache) {
110 
111  // Free members
112  free_members();
113 
114  // Initialise private members
115  init_members();
116 
117  // Copy members
118  copy_members(cache);
119 
120  } // endif: object was not identical
121 
122  // Return this object
123  return *this;
124 }
125 
126 
127 /*==========================================================================
128  = =
129  = Public methods =
130  = =
131  ==========================================================================*/
132 
133 /***********************************************************************//**
134  * @brief Clear CTA response cache
135  ***************************************************************************/
137 {
138  // Free members
139  free_members();
140 
141  // Initialise private members
142  init_members();
143 
144  // Return
145  return;
146 }
147 
148 
149 /***********************************************************************//**
150  * @brief Return number of elements in cache
151  *
152  * @return Number of elements in cache
153  *
154  * Returns the number of elements in the response cache.
155  ***************************************************************************/
156 int GCTAResponseCache::size(void) const
157 {
158  // Initialize size
159  int size = 0;
160 
161  // Compute size
162  for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin();
163  it_name != m_cache.end(); ++it_name) {
164  size += it_name->second.size();
165  }
166 
167  // Return size
168  return size;
169 }
170 
171 
172 /***********************************************************************//**
173  * @brief Set cache value
174  *
175  * @param[in] name Cache name.
176  * @param[in] ereco Reconstructed energy.
177  * @param[in] etrue True energy.
178  * @param[in] value Cache value.
179  *
180  * Set cache value for a given @p name, reconstructed energy @p ereco, and
181  * true energy @p etrue.
182  ***************************************************************************/
183 void GCTAResponseCache::set(const std::string& name,
184  const GEnergy& ereco,
185  const GEnergy& etrue,
186  const double& value)
187 {
188  // Get element identifier
189  double element = encode(ereco, etrue);
190 
191  // Set cache value
192  m_cache[name][element] = value;
193 
194  // Return
195  return;
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Set cache value
201  *
202  * @param[in] name Cache name.
203  * @param[in] dir Instrument direction.
204  * @param[in] ereco Reconstructed energy.
205  * @param[in] etrue True energy.
206  * @param[in] value Cache value.
207  *
208  * Set cache value for a given @p name, instrument direction @p dir,
209  * reconstructed energy @p ereco, and true energy @p etrue.
210  ***************************************************************************/
211 void GCTAResponseCache::set(const std::string& name,
212  const GCTAInstDir& dir,
213  const GEnergy& ereco,
214  const GEnergy& etrue,
215  const double& value)
216 {
217  // Get element identifier
218  double element = encode(dir, ereco, etrue);
219 
220  // Set cache value
221  m_cache[name][element] = value;
222 
223  // Return
224  return;
225 }
226 
227 
228 /***********************************************************************//**
229  * @brief Check if cache contains a value for specific parameters
230  *
231  * @param[in] name Cache name.
232  * @param[in] ereco Reconstructed energy.
233  * @param[in] etrue True energy.
234  * @param[out] value Pointer to cached value (only if found).
235  * @return True if cached value was found, otherwise false.
236  *
237  * Check if the cache contains a value for a given @p name, reconstructed
238  * energy @p ereco, and true energy @p etrue.
239  *
240  * If the @p value pointer argument is not NULL, the method will return the
241  * cached value through this argument in case that the value exists.
242  ***************************************************************************/
243 bool GCTAResponseCache::contains(const std::string& name,
244  const GEnergy& ereco,
245  const GEnergy& etrue,
246  double* value) const
247 {
248  // Initialise containment flag
249  bool contains = false;
250 
251  // Get element identifier
252  double element = encode(ereco, etrue);
253 
254  // Search for name in cache
255  GCTAResponseCacheName::const_iterator it_name = m_cache.find(name);
256  if (it_name != m_cache.end()) {
257 
258  // Search for element in cache
259  GCTAResponseCacheElement::const_iterator it_element =
260  it_name->second.find(element);
261  if (it_element != it_name->second.end()) {
262  contains = true;
263  if (value != NULL) {
264  *value = it_element->second;
265  }
266  } // endif: element found
267 
268  } // endif: name found
269 
270  // Return containment flag
271  return contains;
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Check if cache contains a value for specific parameters
277  *
278  * @param[in] name Cache name.
279  * @param[in] dir Sky direction.
280  * @param[in] ereco Reconstructed energy.
281  * @param[in] etrue True energy.
282  * @param[out] value Pointer to cached value (only if found).
283  * @return True if cached value was found, otherwise false.
284  *
285  * Check if the cache contains a value for a given @p name, instrument
286  * direction @p dir,reconstructed energy @p ereco, and true energy @p etrue.
287  *
288  * If the @p value pointer argument is not NULL, the method will return the
289  * cached value through this argument in case that the value exists.
290  ***************************************************************************/
291 bool GCTAResponseCache::contains(const std::string& name,
292  const GCTAInstDir& dir,
293  const GEnergy& ereco,
294  const GEnergy& etrue,
295  double* value) const
296 {
297  // Initialise containment flag
298  bool contains = false;
299 
300  // Get element identifier
301  double element = encode(dir, ereco, etrue);
302 
303  // Search for name in cache
304  GCTAResponseCacheName::const_iterator it_name = m_cache.find(name);
305  if (it_name != m_cache.end()) {
306 
307  // Search for element in cache
308  GCTAResponseCacheElement::const_iterator it_element =
309  it_name->second.find(element);
310  if (it_element != it_name->second.end()) {
311  contains = true;
312  if (value != NULL) {
313  *value = it_element->second;
314  }
315  } // endif: element found
316 
317  } // endif: name found
318 
319  // Return containment flag
320  return contains;
321 }
322 
323 
324 /***********************************************************************//**
325  * @brief Clone CTA response cache
326  *
327  * @return Pointer to deep copy of CTA response cache.
328  ***************************************************************************/
330 {
331  return new GCTAResponseCache(*this);
332 }
333 
334 
335 /***********************************************************************//**
336  * @brief Print CTA response cache
337  *
338  * @param[in] chatter Chattiness.
339  * @return String containing CTA response cache information.
340  *
341  * @todo Implement method.
342  ***************************************************************************/
343 std::string GCTAResponseCache::print(const GChatter& chatter) const
344 {
345  // Initialise result string
346  std::string result;
347 
348  // Continue only if chatter is not silent
349  if (chatter != SILENT) {
350 
351  // Append header
352  result.append("=== GCTAResponseCache ===");
353 
354  // Append total cache size
355  result.append("\n"+gammalib::parformat("Number of cached values"));
356  result.append(gammalib::str(size()));
357 
358  // Append information
359  for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin();
360  it_name != m_cache.end(); ++it_name) {
361 
362  // Append name
363  result.append("\n"+gammalib::parformat("Name")+it_name->first);
364 
365  // Compute number of elements
366  int nelement = it_name->second.size();
367 
368  // Append number of elements
369  result.append("\n"+gammalib::parformat("Elements"));
370  result.append(gammalib::str(nelement));
371 
372  } // endfor: looped over names
373 
374  } // endif: chatter was not silent
375 
376  // Return result
377  return result;
378 }
379 
380 
381 /*==========================================================================
382  = =
383  = Private methods =
384  = =
385  ==========================================================================*/
386 
387 /***********************************************************************//**
388  * @brief Initialise class members
389  ***************************************************************************/
391 {
392  // Initialise members
393  m_cache.clear();
394 
395  // Return
396  return;
397 }
398 
399 
400 /***********************************************************************//**
401  * @brief Copy class members
402  *
403  * @param[in] cache CTA response cache.
404  ***************************************************************************/
406 {
407  // Copy members
408  m_cache = cache.m_cache;
409 
410  // Return
411  return;
412 }
413 
414 
415 /***********************************************************************//**
416  * @brief Delete class members
417  ***************************************************************************/
419 {
420  // Return
421  return;
422 }
423 
424 
425 /***********************************************************************//**
426  * @brief Encode reconstructued and true energy
427  *
428  * @param[in] ereco Reconstructed energy.
429  * @param[in] etrue True energy.
430  * @return Encoded reconstructued and true energy
431  *
432  * Encodes the reconstructued and true energy in a single double precision
433  * value. The encoding is done using the following formula:
434  *
435  * \f[
436  * {\\ encode} = E_{\rm reco} \times 10^2 + E_{\rm true}
437  * \f]
438  *
439  * where
440  * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and
441  * - \f$E_{\rm true}\f$ is the true energy in TeV.
442  ***************************************************************************/
443 double GCTAResponseCache::encode(const GEnergy& ereco,
444  const GEnergy& etrue) const
445 {
446  // Construct encoded instrument direction and reconstructued energy
447  double encoded = ereco.TeV() * 1.0e2 + etrue.TeV();
448 
449  // Return encoded value
450  return encoded;
451 }
452 
453 
454 /***********************************************************************//**
455  * @brief Encode instrument direction, reconstructued and true energy
456  *
457  * @param[in] dir Instrument direction.
458  * @param[in] ereco Reconstructed energy.
459  * @param[in] etrue True energy.
460  * @return Encoded instrument direction, reconstructued and true energy
461  *
462  * Encodes the instrument direction, reconstructued and true energy in a
463  * single double precision value. The encoding is done using the following
464  * formula:
465  *
466  * \f[
467  * {\\ encode} = \alpha \times 10^8 + \delta \times 10^5 +
468  * E_{\rm reco} \times 10^2 + E_{\rm true}
469  * \f]
470  *
471  * where
472  * - \f$\alpha\f$ is the Right Ascension of the instrument direction in
473  * degrees,
474  * - \f$\delta\f$ is the Declination of the instrument direction in degrees,
475  * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and
476  * - \f$E_{\rm true}\f$ is the true energy in TeV.
477  ***************************************************************************/
479  const GEnergy& ereco,
480  const GEnergy& etrue) const
481 {
482  // Construct encoded instrument direction and reconstructued energy
483  double encoded = dir.dir().ra_deg() * 1.0e8 +
484  dir.dir().dec_deg() * 1.0e5 +
485  ereco.TeV() * 1.0e2 +
486  etrue.TeV();
487 
488  // Return encoded value
489  return encoded;
490 }
double TeV(void) const
Return energy in TeV.
Definition: GEnergy.cpp:348
GCTAResponseCacheName m_cache
CTA response cache class definition.
virtual ~GCTAResponseCache(void)
Destructor.
Gammalib tools definition.
void dir(const GSkyDir &dir)
Set sky direction.
bool contains(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, double *value=NULL) const
Check if cache contains a value for specific parameters.
void free_members(void)
Delete class members.
void clear(void)
Clear CTA response cache.
void init_members(void)
Initialise class members.
CTA instrument direction class interface definition.
GCTAResponseCache * clone(void) const
Clone CTA response cache.
GChatter
Definition: GTypemaps.hpp:33
double encode(const GEnergy &ereco, const GEnergy &etrue) const
Encode reconstructued and true energy.
int size(void) const
Return number of elements in cache.
std::string print(const GChatter &chatter=NORMAL) const
Print CTA response cache.
GCTAResponseCache & operator=(const GCTAResponseCache &cache)
Assignment operator.
CTA instrument direction class.
Definition: GCTAInstDir.hpp:59
void copy_members(const GCTAResponseCache &cache)
Copy class members.
void set(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, const double &value)
Set cache value.
GCTAResponseCache(void)
Void constructor.
CTA response cache class.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1033
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:415