GammaLib 2.0.0
Loading...
Searching...
No Matches
GMWLObservation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GMWLObservation.cpp - Multi-wavelength observation class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2010-2021 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 GMWLObservation.cpp
23 * @brief Multi-wavelength observation class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <typeinfo>
32#include "GTools.hpp"
33#include "GFilename.hpp"
35#include "GException.hpp"
36#include "GMWLObservation.hpp"
37#include "GMWLSpectrum.hpp"
38
39/* __ Globals ____________________________________________________________ */
41const GObservationRegistry g_obs_mwl_registry(&g_obs_mwl_seed);
42
43/* __ Method name definitions ____________________________________________ */
44#define G_RESPONSE "GMWLObservation::response(GResponse&)"
45#define G_READ "GMWLObservation::read(GXmlElement&)"
46#define G_WRITE "GMWLObservation::write(GXmlElement&)"
47
48/* __ Macros _____________________________________________________________ */
49
50/* __ Coding definitions _________________________________________________ */
51
52/* __ Debug definitions __________________________________________________ */
53
54
55/*==========================================================================
56 = =
57 = Constructors/destructors =
58 = =
59 ==========================================================================*/
60
61/***********************************************************************//**
62 * @brief Void constructor
63 *
64 * Creates instance of an undefined observation.
65 ***************************************************************************/
67{
68 // Initialise members
70
71 // Return
72 return;
73}
74
75
76/***********************************************************************//**
77 * @brief File constructor
78 *
79 * @param[in] filename File name.
80 *
81 * Creates instance from file.
82 ***************************************************************************/
84{
85 // Initialise members
87
88 // Load observation
90
91 // Return
92 return;
93}
94
95
96/***********************************************************************//**
97 * @brief Copy constructor
98 *
99 * @param[in] obs Observation.
100 *
101 * Creates instance by copying an existing observation.
102 ***************************************************************************/
104{
105 // Initialise members
106 init_members();
107
108 // Copy members
109 copy_members(obs);
110
111 // Return
112 return;
113}
114
115
116/***********************************************************************//**
117 * @brief Destructor
118 *
119 * Destroy instance.
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] obs Observation.
141 *
142 * Copies observation into the instance.
143 ***************************************************************************/
145{
146 // Execute only if object is not identical
147 if (this != &obs) {
148
149 // Copy base class members
150 this->GObservation::operator=(obs);
151
152 // Free members
153 free_members();
154
155 // Initialise members
156 init_members();
157
158 // Copy members
159 copy_members(obs);
160
161 } // endif: object was not identical
162
163 // Return this object
164 return *this;
165}
166
167
168/*==========================================================================
169 = =
170 = Public methods =
171 = =
172 ==========================================================================*/
173
174/***********************************************************************//**
175 * @brief Clear instance
176 *
177 * This method properly resets the instance to an initial state.
178 ***************************************************************************/
180{
181 // Free class members (base and derived classes, derived class first)
182 free_members();
184
185 // Initialise members
187 init_members();
188
189 // Return
190 return;
191}
192
193
194/***********************************************************************//**
195 * @brief Clone instance
196***************************************************************************/
198{
199 return new GMWLObservation(*this);
200}
201
202
203/***********************************************************************//**
204 * @brief Set response function
205 *
206 * @param[in] rsp Response function.
207 *
208 * @exception GException::invalid_argument
209 * Response @p rsp in not a MWL response.
210 *
211 * Sets the response function for the observation. The argument has to be of
212 * type GMWLResponse, otherwise an exception is thrown.
213 ***************************************************************************/
215{
216 // Get pointer on MWL response
217 const GMWLResponse* mwlrsp = dynamic_cast<const GMWLResponse*>(&rsp);
218
219 // If pointer is not valid then throw an exception
220 if (mwlrsp == NULL) {
221 std::string cls = std::string(typeid(&rsp).name());
222 std::string msg = "Invalid response type \""+cls+"\" specified. "
223 "Please specify a \"GMWLResponse\" instance as "
224 "argument.";
226 }
227
228 // Copy response function
229 m_response = *mwlrsp;
230
231 // Return
232 return;
233}
234
235
236/***********************************************************************//**
237 * @brief Read observation from XML element
238 *
239 * @param[in] xml XML element.
240 *
241 * Reads information for a multi-wavelength observation from an XML element.
242 * The expected format of the XML element is
243 *
244 * <observation name="..." id="..." instrument="MWL">
245 * <parameter name="Instrument" value="..."/>
246 * <parameter name="Data" file="..."/>
247 * </observation>
248 *
249 * The extno and extname attributes of the data parameter are optional, and
250 * can be used to indicate the extension number or name from which the
251 * multi-wavelength data should be loaded. If both are given, the extension
252 * number will take precedence and the extension name is ignored.
253 ***************************************************************************/
255{
256 // Clear observation
257 clear();
258
259 // Get parameters
260 m_instrument = gammalib::xml_get_attr(G_READ, xml, "Instrument", "value");
261 std::string filename = gammalib::xml_get_attr(G_READ, xml, "Data", "file");
262
263 // Expand file names
265
266 // Load file
267 load(filename);
268
269 // Return
270 return;
271}
272
273
274/***********************************************************************//**
275 * @brief Write observation into XML element
276 *
277 * @param[in] xml XML element.
278 *
279 * Writes information for a multi-wavelength observation into an XML element.
280 * The format of the XML element is
281 *
282 * <observation name="..." id="..." instrument="MWL">
283 * <parameter name="Instrument" value="..."/>
284 * <parameter name="Data" file="..."/>
285 * </observation>
286 ***************************************************************************/
288{
289 // Allocate XML element pointer
290 GXmlElement* par;
291
292 // Set Instrument parameter
293 par = gammalib::xml_need_par(G_WRITE, xml, "Instrument");
295
296 // Set Data parameter
297 par = gammalib::xml_need_par(G_WRITE, xml, "Data");
299
300 // Return
301 return;
302}
303
304
305/***********************************************************************//**
306 * @brief Load observation
307 *
308 * @param[in] filename File name.
309 ***************************************************************************/
310void GMWLObservation::load(const GFilename& filename)
311{
312 // Clear observation
313 clear();
314
315 // Allocate spectrum
316 GMWLSpectrum* spec = new GMWLSpectrum;
317 m_events = spec;
318
319 // Load spectrum
320 spec->load(filename);
321
322 // Set attributes
323 name("Multi-wavelength observation");
324 id(filename);
326 m_instrument = spec->instrument();
327
328 // Return
329 return;
330}
331
332
333/***********************************************************************//**
334 * @brief Print multi-wavelength information
335 *
336 * @param[in] chatter Chattiness (defaults to NORMAL).
337 * @return String containing multi-wavelength information.
338 ***************************************************************************/
339std::string GMWLObservation::print(const GChatter& chatter) const
340{
341 // Initialise result string
342 std::string result;
343
344 // Continue only if chatter is not silent
345 if (chatter != SILENT) {
346
347 // Append header
348 result.append("=== GMWLObservation ===");
349
350 // Append information
351 result.append("\n"+gammalib::parformat("Name")+name());
352 result.append("\n"+gammalib::parformat("Identifier")+id());
353 result.append("\n"+gammalib::parformat("Instrument")+instrument());
354 result.append("\n"+gammalib::parformat("Statistic")+statistic());
355
356 // EXPLICIT: Append events
357 if (chatter >= EXPLICIT) {
358 if (m_events != NULL) {
359 result.append("\n"+m_events->print(chatter));
360 }
361 }
362
363 } // endif: chatter was not silent
364
365 // Return result
366 return result;
367}
368
369
370/*==========================================================================
371 = =
372 = Private methods =
373 = =
374 ==========================================================================*/
375
376/***********************************************************************//**
377 * @brief Initialise class members
378 *
379 * The instrument name is set here to "MWL" so that the registry has an
380 * instrument type with that name. This may be later overwritten by a
381 * specific instrument.
382 ***************************************************************************/
384{
385 // Initialise members
386 m_instrument = "MWL";
389
390 // Overwrite base class statistic
391 m_statistic = "Gaussian";
392
393 // Return
394 return;
395}
396
397
398/***********************************************************************//**
399 * @brief Copy class members
400 *
401 * @param[in] obs Observation to be copied
402 ***************************************************************************/
404{
405 // Copy members
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 = =
427 = Friends =
428 = =
429 ==========================================================================*/
#define G_WRITE
#define G_READ
#define G_RESPONSE
Exception handler interface definition.
Filename class interface definition.
const GMWLObservation g_obs_mwl_seed
Multi-wavelength observation class interface definition.
Multi-wavelength spectrum class interface definition.
Observation registry class definition.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ EXPLICIT
Definition GTypemaps.hpp:37
@ SILENT
Definition GTypemaps.hpp:34
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
Filename class.
Definition GFilename.hpp:62
void clear(void)
Clear file name.
Interface class for multi-wavelength observations.
GMWLResponse m_response
Response function.
virtual void clear(void)
Clear instance.
const GFilename & filename(void) const
Return filename.
virtual std::string instrument(void) const
Return instrument name.
GFilename m_filename
Filename.
void load(const GFilename &filename)
Load observation.
virtual GMWLObservation & operator=(const GMWLObservation &obs)
Assignment operator.
GMWLObservation(void)
Void constructor.
void free_members(void)
Delete class members.
virtual GMWLObservation * clone(void) const
Clone instance.
void init_members(void)
Initialise class members.
virtual const GMWLResponse * response(void) const
Return response.
void copy_members(const GMWLObservation &obs)
Copy class members.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print multi-wavelength information.
virtual void write(GXmlElement &xml) const
Write observation into XML element.
virtual ~GMWLObservation(void)
Destructor.
std::string m_instrument
Instrument name.
virtual void read(const GXmlElement &xml)
Read observation from XML element.
Multi-wavelength response class.
virtual void clear(void)
Clear instance.
Multi-wavelength spectrum class interface.
virtual void load(const GFilename &filename)
Load spectrum.
const std::string & instrument(void) const
Return instrument name.
Interface definition for the observation registry class.
Abstract observation base class.
std::string m_statistic
Optimizer statistic.
const std::string & statistic(void) const
Return optimizer statistic.
const std::string & id(void) const
Return observation identifier.
const std::string & name(void) const
Return observation name.
void init_members(void)
Initialise class members.
virtual GObservation & operator=(const GObservation &obs)
Assignment operator.
GEvents * m_events
Pointer to event container.
void free_members(void)
Delete class members.
Abstract instrument response base class.
Definition GResponse.hpp:77
XML element node class.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1143
std::string xml_get_attr(const std::string &origin, const GXmlElement &xml, const std::string &name, const std::string &attribute)
Return attribute value for a given parameter in XML element.
Definition GTools.cpp:1738
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition GTools.cpp:1637
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition GTools.cpp:1946
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition GTools.cpp:1889