GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GCOMTools.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCOMTools.cpp - COMPTEL tools *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2021-2025 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 GCOMTools.hpp
23 * @brief Implementation of COMPTEL tools
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GTime.hpp"
32#include "GEnergy.hpp"
33#include "GNodeArray.hpp"
34#include "GTools.hpp"
35#include "GCOMTools.hpp"
36
37/* __ Method name definitions ____________________________________________ */
38#define G_COM_TOFCOR "com_tofcor(GEnergy&, GEnergy&, int&, int& tofmax)"
39
40/* __ Coding definitions _________________________________________________ */
41
42/* __ Debug definitions __________________________________________________ */
43
44
45/***********************************************************************//**
46 * @brief Convert TJD and COMPTEL tics in GTime object
47 *
48 * @param[in] tjd Truncated Julian Days (days).
49 * @param[in] tics COMPTEL tics (1/8 ms).
50 * @return Time.
51 *
52 * Converts TJD and COMPTEL tics into a GTime object. COMPTEL times are
53 * given in UTC, i.e. 8393:0 converts into 1991-05-17T00:00:00 UT
54 * (see COM-RP-UNH-DRG-037).
55 *
56 * The method applies the CGRO clock correction. The CGRO clock was too
57 * fast by 2.042144 seconds before 8798:28800000 (1992-06-25T01:00:00).
58 * After that date the clock was corrected.
59 ***************************************************************************/
60GTime gammalib::com_time(const int& tjd, const int& tics)
61{
62 // Compute tics to days conversion factor
63 const double tics2days = 0.000125 * gammalib::sec2day;
64 const double clockcor_days = 2.042144 * gammalib::sec2day;
65
66 // Compute MJD
67 double mjd = double(tjd) + 40000.0 + double(tics) * tics2days;
68
69 // Apply CGRO clock correction as the clock was too fast by 2.042144 sec
70 // before 8798:28800000 (1992-06-25T01:00:00).
71 if ((tjd < 8798) || ((tjd == 8798) && (tics < 28800000))) {
72 mjd -= clockcor_days;
73 }
74
75 // Set time in MJD in the UTC time system
76 GTime time;
77 time.mjd(mjd, "UTC");
78
79 // Return time
80 return time;
81}
82
83
84/***********************************************************************//**
85 * @brief Convert GTime in COMPTEL TJD
86 *
87 * @param[in] time Time.
88 * @return Truncated Julian Days (days).
89 *
90 * Converts GTime object in TJD.
91 *
92 * The method applies the CGRO clock correction. The CGRO clock was too
93 * fast by 2.042144 seconds before 8798:28800000 (1992-06-25T01:00:00).
94 * After that date the clock was corrected.
95 ***************************************************************************/
96int gammalib::com_tjd(const GTime& time)
97{
98 // Set MJD in UTC of clock correction
99 const double mjd_of_clockcor = 48798.04166666666424134746;
100 const double clockcor_days = 2.042144 * gammalib::sec2day;
101 const double half_tics2days = 0.5 * 0.000125 * gammalib::sec2day;
102
103 // Compute MJD, applying the CGRO clock correction before 8798:28800000
104 // (1992-06-25T01:00:00)
105 double mjd = time.mjd("UTC");
106 if (mjd < mjd_of_clockcor) {
107 mjd += clockcor_days;
108 }
109
110 // Compute TJD, rounding to the nearest tics
111 int tjd = int(mjd + half_tics2days) - 40000;
112
113 // Return TJD
114 return tjd;
115}
116
117
118/***********************************************************************//**
119 * @brief Convert GTime in COMPTEL tics
120 *
121 * @param[in] time Time.
122 * @return COMPTEL tics (1/8 ms).
123 *
124 * Converts GTime object in COMPTEL tics (1/8 ms).
125 *
126 * The method applies the CGRO clock correction. The CGRO clock was too
127 * fast by 2.042144 seconds before 8798:28800000 (1992-06-25T01:00:00).
128 * After that date the clock was corrected.
129 ***************************************************************************/
130int gammalib::com_tics(const GTime& time)
131{
132 // Set MJD in UTC of clock correction
133 const double mjd_of_clockcor = 48798.04166666666424134746;
134 const double clockcor_days = 2.042144 * gammalib::sec2day;
135 const int tics_in_day = 691200000;
136
137 // Compute MJD, applying the CGRO clock correction before 8798:28800000
138 // (1992-06-25T01:00:00)
139 double mjd = time.mjd("UTC");
140 if (mjd < mjd_of_clockcor) {
141 mjd += clockcor_days;
142 }
143
144 // Compute number of tics, rounding to nearest int
145 int tics = int((mjd - double(int(mjd))) * tics_in_day + 0.5);
146
147 // If number of tics exceeds number of tics in one day then decrement
148 // number of tics
149 while (tics >= tics_in_day) {
150 tics -= tics_in_day;
151 }
152
153 // Return tics
154 return tics;
155}
156
157
158/***********************************************************************//**
159 * @brief Compute ToF correction
160 *
161 * @param[in] emin Minimum energy.
162 * @param[in] emax Maximum energy.
163 * @param[in] tofmin Minimum ToF channel.
164 * @param[in] tofmax Maximum ToF channel.
165 * @return Time of flight correction.
166 *
167 * Compute the ToF correction according to COM-RP-ROL-DRG-057.
168 ***************************************************************************/
169double gammalib::com_tofcor(const GEnergy& emin, const GEnergy& emax,
170 const int& tofmin, const int& tofmax)
171{
172 // ToF correction factors according to Table 1 of COM-RP-ROL-DRG-057
173 const double tofcoreng[] = {0.8660, 1.7321, 5.4772, 17.3205};
174 const double tofcor110[] = {1.14, 1.07, 1.02, 1.01};
175 const double tofcor111[] = {1.17, 1.09, 1.03, 1.01};
176 const double tofcor112[] = {1.21, 1.11, 1.05, 1.02};
177 const double tofcor113[] = {1.26, 1.15, 1.07, 1.04};
178 const double tofcor114[] = {1.32, 1.20, 1.11, 1.06};
179 const double tofcor115[] = {1.40, 1.27, 1.17, 1.11};
180 const double tofcor116[] = {1.50, 1.36, 1.24, 1.17};
181 const double tofcor117[] = {1.63, 1.47, 1.35, 1.28};
182 const double tofcor118[] = {1.79, 1.63, 1.51, 1.43};
183 const double tofcor119[] = {2.01, 1.85, 1.73, 1.67};
184 const double* tofcor[] = {tofcor110, tofcor111, tofcor112,
185 tofcor113, tofcor114, tofcor115,
186 tofcor116, tofcor117, tofcor118,
187 tofcor119};
188
189 // Initialise ToF correction
190 double tofcorr = 1.0;
191
192 // Determine ToF correction
193 if (tofmin < 110) {
194 std::string msg = "Minimum of ToF selection window "+
195 gammalib::str(tofmin)+
196 " is smaller than 110. No ToF correction is "
197 "available for this value.";
199 }
200 else if (tofmin > 119) {
201 std::string msg = "Minimum of ToF selection window "+
202 gammalib::str(tofmin)+
203 " is larger than 119. No ToF correction is "
204 "available for this value.";
206 }
207 else if (tofmax != 130) {
208 std::string msg = "Maximum of ToF selection window "+
209 gammalib::str(tofmax)+
210 " is not 130. No ToF correction is available for "
211 "this value.";
213 }
214 else {
215
216 // Compute mean energy
217 double energy = std::sqrt(emin.MeV() * emax.MeV());
218
219 // Set node array
220 GNodeArray nodes(4, tofcoreng);
221
222 // Set interpolation value
223 nodes.set_value(energy);
224
225 // Get ToF correction index
226 int i = tofmin - 110;
227
228 // Interpolate
229 tofcorr = tofcor[i][nodes.inx_left()] * nodes.wgt_left() +
230 tofcor[i][nodes.inx_right()] * nodes.wgt_right();
231
232 }
233
234 // Return ToF correction
235 return tofcorr;
236}
#define G_COM_TOFCOR
Definition GCOMTools.cpp:38
Definition of COMPTEL tools.
Energy value class definition.
Node array class interface definition.
Time class interface definition.
Gammalib tools definition.
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
double MeV(void) const
Return energy in MeV.
Definition GEnergy.cpp:342
Node array class.
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
const int & inx_right(void) const
Returns right node index.
const int & inx_left(void) const
Returns left node index.
const double & wgt_right(void) const
Returns right node weight.
const double & wgt_left(void) const
Returns left node weight.
Time class.
Definition GTime.hpp:55
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition GTime.cpp:320
double com_tofcor(const GEnergy &emin, const GEnergy &emax, const int &tofmin, const int &tofmax)
Compute ToF correction.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508
const double sec2day
Definition GTools.hpp:50
int com_tics(const GTime &time)
Convert GTime in COMPTEL tics.
void warning(const std::string &origin, const std::string &message)
Emits warning.
Definition GTools.cpp:1405
int com_tjd(const GTime &time)
Convert GTime in COMPTEL TJD.
Definition GCOMTools.cpp:96
GTime com_time(const int &tjd, const int &tics)
Convert TJD and COMPTEL tics in GTime object.
Definition GCOMTools.cpp:60