1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////
20 // Handling of timestamps for (astro)particle physics reserach.
22 // This class is derived from TTimeStamp and provides additional
23 // facilities (e.g. Julian date) which are commonly used in the
24 // field of (astro)particle physics.
26 // The Julian Date (JD) indicates the number of days since noon (UT) on
27 // 01 jan -4712 (i.e. noon 01 jan 4713 BC), being day 0 of the Julian calendar.
29 // The Modified Julian Date (MJD) indicates the number of days since midnight
30 // (UT) on 17-nov-1858, which corresponds to 2400000.5 days after day 0 of the
33 // The Truncated Julian Date (TJD) corresponds to 2440000.5 days after day 0
34 // of the Julian calendar and consequently TJD=MJD-40000.
35 // This TJD date indication was used by the Vela and Batse missions in
36 // view of Gamma Ray Burst investigations.
38 // The Julian Epoch (JE) indicates the fractional elapsed year count since
39 // midnight (UT) on 01-jan at the start of the Gregorian year count.
40 // A year is defined to be 365.25 days, so the integer part of JE corresponds
41 // to the usual Gregorian year count.
42 // So, 01-jan-1965 00:00:00 UT corresponds to JE=1965.0
44 // Because of the fact that the Julian date indicators are all w.r.t. UT
45 // they provide an absolute timescale irrespective of timezone or daylight
48 // This AliTimestamp facility allows for picosecond precision, in view
49 // of time of flight analyses for particle physics experiments.
50 // For normal date/time indication the standard nanosecond precision
51 // will in general be sufficient.
52 // Note that when the fractional JD, MJD and TJD counts are used instead
53 // of the integer (days,sec,ns) specification, the nanosecond precision
54 // may be lost due to computer accuracy w.r.t. floating point operations.
56 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
57 // which corresponds to JD=2440587.5 or the start of MJD=40587 or TJD=587.
58 // Using the corresponding MJD of this EPOCH allows construction of
59 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input (M/T)JD and time.
60 // Obviously this TTimeStamp implementation would prevent usage of values
61 // smaller than JD=2440587.5 or MJD=40587 or TJD=587.
62 // However, this AliTimestamp facility provides support for the full range
63 // of (M/T)JD values, but the setting of the corresponding TTimeStamp parameters
64 // is restricted to the values allowed by the TTimeStamp implementation.
65 // For these earlier (M/T)JD values, the standard TTimeStamp parameters will
66 // be set corresponding to the start of the TTimeStamp EPOCH.
67 // This implies that for these earlier (M/T)JD values the TTimeStamp parameters
68 // do not match the Julian parameters of AliTimestamp.
69 // As such the standard TTimeStamp parameters do not appear on the print output
70 // when invoking the Date() memberfunction for these earlier (M/T)JD values.
75 // Note : All TTimeStamp functionality is available as well.
81 // // Retrieve Julian Date
83 // t.GetJD(jd,jsec,jns);
85 // // Retrieve fractional Truncated Julian Date
86 // Double_t tjd=t.GetTJD();
88 // // Retrieve fractional Julian Epoch
89 // Double_t je=t.GetJE();
91 // // Set to a specific Modified Julian Date
95 // t.SetMJD(mjd,mjsec,mjns);
99 // // Time intervals for e.g. trigger or TOF analysis
101 // AliTrack* tx=evt.GetTrack(5);
102 // AliTimestamp* timex=tx->GetTimestamp();
103 // Double_t dt=evt.GetDifference(timex,"ps");
104 // AliTimestamp trig((AliTimestamp)evt);
105 // trig.Add(0,0,2,173);
106 // AliSignal* sx=evt.GetHit(23);
107 // AliTimestamp* timex=sx->GetTimestamp();
108 // Double_t dt=trig.GetDifference(timex,"ps");
110 // trig.GetDifference(timex,d,s,ns,ps);
112 // // Some practical conversion facilities
113 // // Note : They don't influence the actual date/time settings
114 // // and as such can also be invoked as AliTimestamp::Convert(...) etc...
122 // Double_t jdate=t.GetJD(y,m,d,hh,mm,ss,ns);
124 // Int_t days,secs,nsecs;
125 // Double_t date=421.1949327;
126 // t.Convert(date,days,secs,nsecs);
131 // date=t.Convert(days,secs,nsecs);
133 // Double_t mjdate=40563.823744;
134 // Double_t epoch=t.GetJE(mjdate,"mjd");
136 //--- Author: Nick van Eijndhoven 28-jan-2005 Utrecht University.
137 //- Modified: NvE $Date$ Utrecht University.
138 ///////////////////////////////////////////////////////////////////////////
140 #include "AliTimestamp.h"
141 #include "Riostream.h"
143 ClassImp(AliTimestamp) // Class implementation to enable ROOT I/O
145 AliTimestamp::AliTimestamp() : TTimeStamp()
147 // Default constructor
148 // Creation of an AliTimestamp object and initialisation of parameters.
149 // All attributes are initialised to the current date/time as specified
150 // in the docs of TTimeStamp.
155 ///////////////////////////////////////////////////////////////////////////
156 AliTimestamp::AliTimestamp(TTimeStamp& t) : TTimeStamp(t)
158 // Creation of an AliTimestamp object and initialisation of parameters.
159 // All attributes are initialised to the values of the input TTimeStamp.
164 ///////////////////////////////////////////////////////////////////////////
165 AliTimestamp::~AliTimestamp()
167 // Destructor to delete dynamically allocated memory.
169 ///////////////////////////////////////////////////////////////////////////
170 AliTimestamp::AliTimestamp(const AliTimestamp& t) : TTimeStamp(t)
181 ///////////////////////////////////////////////////////////////////////////
182 void AliTimestamp::Date(Int_t mode)
184 // Print date/time info.
186 // mode = 1 ==> Only the TTimeStamp yy-mm-dd hh:mm:ss:ns info is printed
187 // 2 ==> Only the Julian parameter info is printed
188 // 3 ==> Both the TTimeStamp and Julian parameter info is printed
190 // The default is mode=3.
192 // Note : In case the (M/T)JD falls outside the TTimeStamp range,
193 // the TTimeStamp info will not be printed.
195 Int_t mjd,mjsec,mjns;
196 GetMJD(mjd,mjsec,mjns);
198 if ((mode==1 || mode==3) && mjd>=40587) cout << " " << AsString() << endl;
199 if (mode==2 || mode==3)
203 Int_t tjd,tjsec,tjns;
204 GetTJD(tjd,tjsec,tjns);
205 cout << " Julian Epoch : " << setprecision(25) << GetJE() << endl;
206 cout << " JD : " << jd << " sec : " << jsec << " ns : " << jns << " ps : " << fJps
207 << " Fractional : " << setprecision(25) << GetJD() << endl;
208 cout << " MJD : " << mjd << " sec : " << mjsec << " ns : " << mjns << " ps : " << fJps
209 << " Fractional : " << setprecision(25) << GetMJD() << endl;
210 cout << " TJD : " << tjd << " sec : " << tjsec << " ns : " << tjns << " ps : " << fJps
211 << " Fractional : " << setprecision(25) << GetTJD() << endl;
214 ///////////////////////////////////////////////////////////////////////////
215 Double_t AliTimestamp::GetJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
217 // Provide the (fractional) Julian Date (JD) corresponding to the UT date
218 // and time in the Gregorian calendar as specified by the input arguments.
220 // The input arguments represent the following :
221 // y : year in UT (e.g. 1952, 2003 etc...)
222 // m : month in UT (1=jan 2=feb etc...)
223 // d : day in UT (1-31)
224 // hh : elapsed hours in UT (0-23)
225 // mm : elapsed minutes in UT (0-59)
226 // ss : elapsed seconds in UT (0-59)
227 // ns : remaining fractional elapsed second of UT in nanosecond
229 // This algorithm is valid for all AD dates in the Gregorian calendar
230 // following the recipe of R.W. Sinnott Sky & Telescope 82, (aug. 1991) 183.
231 // See also http://scienceworld.wolfram.com/astronomy/JulianDate.html
233 // In case of invalid input, a value of -1 is returned.
237 // This memberfunction only provides the JD corresponding to the
238 // UT input arguments. It does NOT set the corresponding Julian parameters
239 // for the current AliTimestamp instance.
240 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
241 // To set the Julian parameters for the current AliTimestamp instance,
242 // please use the corresponding SET() memberfunctions of either AliTimestamp
245 if (y<0 || m<1 || m>12 || d<1 || d>31) return -1;
246 if (hh<0 || hh>23 || mm<0 || mm>59 || ss<0 || ss>59 || ns<0 || ns>1e9) return -1;
248 // The UT daytime in fractional hours
249 Double_t ut=double(hh)+double(mm)/60.+(double(ss)+double(ns)*1.e-9)/3600.;
253 JD=367*y-int(7*(y+int((m+9)/12))/4)
254 -int(3*(int((y+(m-9)/7)/100)+1)/4)
255 +int(275*m/9)+d+1721028.5+ut/24.;
259 ///////////////////////////////////////////////////////////////////////////
260 Double_t AliTimestamp::GetMJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
262 // Provide the (fractional) Modified Julian Date corresponding to the UT
263 // date and time in the Gregorian calendar as specified by the input arguments.
265 // The input arguments represent the following :
266 // y : year in UT (e.g. 1952, 2003 etc...)
267 // m : month in UT (1=jan 2=feb etc...)
268 // d : day in UT (1-31)
269 // hh : elapsed hours in UT (0-23)
270 // mm : elapsed minutes in UT (0-59)
271 // ss : elapsed seconds in UT (0-59)
272 // ns : remaining fractional elapsed second of UT in nanosecond
274 // This algorithm is valid for all AD dates in the Gregorian calendar
275 // following the recipe of R.W. Sinnott Sky & Telescope 82, (aug. 1991) 183.
276 // See also http://scienceworld.wolfram.com/astronomy/JulianDate.html
278 // In case of invalid input, a value of -1 is returned.
282 // This memberfunction only provides the MJD corresponding to the
283 // UT input arguments. It does NOT set the corresponding Julian parameters
284 // for the current AliTimestamp instance.
285 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
286 // To set the Julian parameters for the current AliTimestamp instance,
287 // please use the corresponding SET() memberfunctions of either AliTimestamp
290 Double_t JD=GetJD(y,m,d,hh,mm,ss,ns);
294 Double_t MJD=JD-2400000.5;
298 ///////////////////////////////////////////////////////////////////////////
299 Double_t AliTimestamp::GetTJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
301 // Provide the (fractional) Truncated Julian Date corresponding to the UT
302 // date and time in the Gregorian calendar as specified by the input arguments.
304 // The input arguments represent the following :
305 // y : year in UT (e.g. 1952, 2003 etc...)
306 // m : month in UT (1=jan 2=feb etc...)
307 // d : day in UT (1-31)
308 // hh : elapsed hours in UT (0-23)
309 // mm : elapsed minutes in UT (0-59)
310 // ss : elapsed seconds in UT (0-59)
311 // ns : remaining fractional elapsed second of UT in nanosecond
313 // This algorithm is valid for all AD dates in the Gregorian calendar
314 // following the recipe of R.W. Sinnott Sky & Telescope 82, (aug. 1991) 183.
315 // See also http://scienceworld.wolfram.com/astronomy/JulianDate.html
317 // In case of invalid input, a value of -1 is returned.
321 // This memberfunction only provides the TJD corresponding to the
322 // UT input arguments. It does NOT set the corresponding Julian parameters
323 // for the current AliTimestamp instance.
324 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
325 // To set the Julian parameters for the current AliTimestamp instance,
326 // please use the corresponding SET() memberfunctions of either AliTimestamp
329 Double_t JD=GetJD(y,m,d,hh,mm,ss,ns);
333 Double_t TJD=JD-2440000.5;
337 ///////////////////////////////////////////////////////////////////////////
338 Double_t AliTimestamp::GetJE(Double_t date,TString mode) const
340 // Provide the Julian Epoch (JE) corresponding to the specified date.
341 // The argument "mode" indicates the type of the argument "date".
343 // Available modes are :
344 // mode = "jd" ==> date represents the Julian Date
345 // = "mjd" ==> date represents the Modified Julian Date
346 // = "tjd" ==> date represents the Truncated Julian Date
348 // The default is mode="jd".
350 // In case of invalid input, a value of -99999 is returned.
354 // This memberfunction only provides the JE corresponding to the
355 // input arguments. It does NOT set the corresponding Julian parameters
356 // for the current AliTimestamp instance.
357 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
358 // To set the Julian parameters for the current AliTimestamp instance,
359 // please use the corresponding SET() memberfunctions of either AliTimestamp
362 if ((mode != "jd") && (mode != "mjd") && (mode != "tjd")) return -99999;
365 if (mode=="mjd") jd=date+2400000.5;
366 if (mode=="tjd") jd=date+2440000.5;
368 Double_t je=2000.+(jd-2451545.)/365.25;
372 ///////////////////////////////////////////////////////////////////////////
373 void AliTimestamp::Convert(Double_t date,Int_t& days,Int_t& secs,Int_t& ns) const
375 // Convert date as fractional day count into integer days, secs and ns.
377 // Note : Due to computer accuracy the ns value may become inaccurate.
379 // The arguments represent the following :
380 // date : The input date as fractional day count
381 // days : Number of elapsed days
382 // secs : Remaining number of elapsed seconds
383 // ns : Remaining fractional elapsed second in nanoseconds
387 // This memberfunction only converts the input date into the corresponding
388 // integer parameters. It does NOT set the corresponding Julian parameters
389 // for the current AliTimestamp instance.
390 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
391 // To set the Julian parameters for the current AliTimestamp instance,
392 // please use the corresponding SET() memberfunctions of either AliTimestamp
396 date=date-double(days);
397 Int_t daysecs=24*3600;
398 date=date*double(daysecs);
400 date=date-double(secs);
403 ///////////////////////////////////////////////////////////////////////////
404 Double_t AliTimestamp::Convert(Int_t days,Int_t secs,Int_t ns) const
406 // Convert date in integer days, secs and ns into fractional day count.
408 // Note : Due to computer accuracy the ns precision may be lost.
410 // The input arguments represent the following :
411 // days : Number of elapsed days
412 // secs : Remaining number of elapsed seconds
413 // ns : Remaining fractional elapsed second in nanoseconds
417 // This memberfunction only converts the input integer parameters into the
418 // corresponding fractional day count. It does NOT set the corresponding
419 // Julian parameters for the current AliTimestamp instance.
420 // As such the TTimeStamp limitations do NOT apply to this memberfunction.
421 // To set the Julian parameters for the current AliTimestamp instance,
422 // please use the corresponding SET() memberfunctions of either AliTimestamp
425 Double_t frac=double(secs)+double(ns)*1.e-9;
426 Int_t daysecs=24*3600;
427 frac=frac/double(daysecs);
428 Double_t date=double(days)+frac;
431 ///////////////////////////////////////////////////////////////////////////
432 void AliTimestamp::FillJulian()
434 // Calculation and setting of the Julian date/time parameters corresponding
435 // to the current TTimeStamp date/time parameters.
437 UInt_t y,m,d,hh,mm,ss;
439 GetDate(kTRUE,0,&y,&m,&d);
440 GetTime(kTRUE,0,&hh,&mm,&ss);
441 Int_t ns=GetNanoSec();
443 Double_t mjd=GetMJD(y,m,d,hh,mm,ss,ns);
446 fJsec=GetSec()%(24*3600); // Daytime in elapsed seconds
447 fJns=ns; // Remaining fractional elapsed second in nanoseconds
449 // Store the TTimeStamp seconds and nanoseconds values
450 // for which this Julian calculation was performed.
452 fCalcns=GetNanoSec();
454 ///////////////////////////////////////////////////////////////////////////
455 void AliTimestamp::GetMJD(Int_t& mjd,Int_t& sec, Int_t& ns)
457 // Provide the Modified Julian Date (MJD) and time corresponding to the
458 // currently stored AliTimestamp date/time parameters.
460 // The returned arguments represent the following :
461 // mjd : The modified Julian date.
462 // sec : The number of seconds elapsed within the MJD.
463 // ns : The remaining fractional number of seconds (in ns) elapsed within the MJD.
465 if (fCalcs != GetSec() || fCalcns != GetNanoSec()) FillJulian();
471 ///////////////////////////////////////////////////////////////////////////
472 Double_t AliTimestamp::GetMJD()
474 // Provide the (fractional) Modified Julian Date (MJD) corresponding to the
475 // currently stored AliTimestamp date/time parameters.
477 // Due to computer accuracy the ns precision may be lost.
478 // It is advised to use the (mjd,sec,ns) getter instead.
485 Double_t date=Convert(mjd,sec,ns);
489 ///////////////////////////////////////////////////////////////////////////
490 void AliTimestamp::GetTJD(Int_t& tjd,Int_t& sec, Int_t& ns)
492 // Provide the Truncated Julian Date (TJD) and time corresponding to the
493 // currently stored AliTimestamp date/time parameters.
495 // The returned arguments represent the following :
496 // tjd : The modified Julian date.
497 // sec : The number of seconds elapsed within the MJD.
498 // ns : The remaining fractional number of seconds (in ns) elapsed within the MJD.
505 ///////////////////////////////////////////////////////////////////////////
506 Double_t AliTimestamp::GetTJD()
508 // Provide the (fractional) Truncated Julian Date (TJD) corresponding to the
509 // currently stored AliTimestamp date/time parameters.
511 // Due to computer accuracy the ns precision may be lost.
512 // It is advised to use the (mjd,sec,ns) getter instead.
519 Double_t date=Convert(tjd,sec,ns);
523 ///////////////////////////////////////////////////////////////////////////
524 void AliTimestamp::GetJD(Int_t& jd,Int_t& sec, Int_t& ns)
526 // Provide the Julian Date (JD) and time corresponding to the currently
527 // stored AliTimestamp date/time parameters.
529 // The returned arguments represent the following :
530 // jd : The Julian date.
531 // sec : The number of seconds elapsed within the JD.
532 // ns : The remaining fractional number of seconds (in ns) elapsed within the JD.
545 ///////////////////////////////////////////////////////////////////////////
546 Double_t AliTimestamp::GetJD()
548 // Provide the (fractional) Julian Date (JD) corresponding to the currently
549 // stored AliTimestamp date/time parameters.
551 // Due to computer accuracy the ns precision may be lost.
552 // It is advised to use the (jd,sec,ns) getter instead.
559 Double_t date=Convert(jd,sec,ns);
563 ///////////////////////////////////////////////////////////////////////////
564 Double_t AliTimestamp::GetJE()
566 // Provide the Julian Epoch (JE) corresponding to the currently stored
567 // AliTimestamp date/time parameters.
570 Double_t je=GetJE(jd);
573 ///////////////////////////////////////////////////////////////////////////
574 void AliTimestamp::SetMJD(Int_t mjd,Int_t sec,Int_t ns,Int_t ps)
576 // Set the Modified Julian Date (MJD) and time and update the TTimeStamp
577 // parameters accordingly (if possible).
581 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
582 // which corresponds to the start of MJD=40587.
583 // Using the corresponding MJD of this EPOCH allows construction of
584 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
585 // Obviously this TTimeStamp implementation would prevent usage of MJD values
586 // smaller than 40587.
587 // However, this AliTimestamp facility provides support for the full range
588 // of (M)JD values, but the setting of the corresponding TTimeStamp parameters
589 // is restricted to the values allowed by the TTimeStamp implementation.
590 // For these earlier MJD values, the standard TTimeStamp parameters will
591 // be set corresponding to the start of the TTimeStamp EPOCH.
592 // This implies that for these earlier MJD values the TTimeStamp parameters
593 // do not match the Julian parameters of AliTimestamp.
595 // The input arguments represent the following :
596 // mjd : The modified Julian date.
597 // sec : The number of seconds elapsed within the MJD.
598 // ns : The remaining fractional number of seconds (in ns) elapsed within the MJD.
599 // ps : The remaining fractional number of nanoseconds (in ps) elapsed within the MJD.
601 // Note : ps=0 is the default value.
603 if (sec<0 || sec>=24*3600 || ns<0 || ns>=1e9 || ps<0 || ps>=1000)
605 cout << " *AliTimestamp::SetMJD* Invalid input."
606 << " sec : " << sec << " ns : " << ns << endl;
619 Set(0,kFALSE,0,kFALSE);
623 // The elapsed time since start of EPOCH
624 Int_t days=mjd-epoch;
625 UInt_t secs=days*24*3600;
627 Set(secs,kFALSE,0,kFALSE);
628 Int_t date=GetDate();
629 Int_t time=GetTime();
630 Set(date,time,ns,kTRUE,0);
633 // Denote that the Julian and TTimeStamp parameters are synchronised,
634 // even in the case the MJD falls outside the TTimeStamp validity range.
635 // The latter still allows retrieval of Julian parameters for these
638 fCalcns=GetNanoSec();
640 ///////////////////////////////////////////////////////////////////////////
641 void AliTimestamp::SetMJD(Double_t mjd)
643 // Set the Modified Julian Date (MJD) and time and update the TTimeStamp
644 // parameters accordingly (if possible).
648 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
649 // which corresponds to the start of MJD=40587.
650 // Using the corresponding MJD of this EPOCH allows construction of
651 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
652 // Obviously this TTimeStamp implementation would prevent usage of MJD values
653 // smaller than 40587.
654 // However, this AliTimestamp facility provides support for the full range
655 // of (M)JD values, but the setting of the corresponding TTimeStamp parameters
656 // is restricted to the values allowed by the TTimeStamp implementation.
657 // For these earlier MJD values, the standard TTimeStamp parameters will
658 // be set corresponding to the start of the TTimeStamp EPOCH.
659 // This implies that for these earlier MJD values the TTimeStamp parameters
660 // do not match the Julian parameters of AliTimestamp.
662 // Due to computer accuracy the ns precision may be lost.
663 // It is advised to use the (mjd,sec,ns) setting instead.
665 // The input argument represents the following :
666 // mjd : The modified Julian date as fractional day count.
671 Convert(mjd,days,secs,ns);
672 SetMJD(days,secs,ns);
674 ///////////////////////////////////////////////////////////////////////////
675 void AliTimestamp::SetJD(Int_t jd,Int_t sec,Int_t ns,Int_t ps)
677 // Set the Julian Date (JD) and time and update the TTimeStamp
678 // parameters accordingly (if possible).
682 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
683 // which corresponds to JD=2440587.5 or the start of MJD=40587.
684 // Using the corresponding MJD of this EPOCH allows construction of
685 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
686 // Obviously this TTimeStamp implementation would prevent usage of values
687 // smaller than JD=2440587.5.
688 // However, this AliTimestamp facility provides support for the full range
689 // of (M)JD values, but the setting of the corresponding TTimeStamp parameters
690 // is restricted to the values allowed by the TTimeStamp implementation.
691 // For these earlier JD values, the standard TTimeStamp parameters will
692 // be set corresponding to the start of the TTimeStamp EPOCH.
693 // This implies that for these earlier (M)JD values the TTimeStamp parameters
694 // do not match the Julian parameters of AliTimestamp.
696 // The input arguments represent the following :
697 // jd : The Julian date.
698 // sec : The number of seconds elapsed within the JD.
699 // ns : The remaining fractional number of seconds (in ns) elapsed within the JD.
700 // ps : The remaining fractional number of nanoseconds (in ps) elapsed within the JD.
702 // Note : ps=0 is the default value.
704 Int_t mjd=jd-2400000;
712 SetMJD(mjd,sec,ns,ps);
714 ///////////////////////////////////////////////////////////////////////////
715 void AliTimestamp::SetJD(Double_t jd)
717 // Set the Julian Date (JD) and time and update the TTimeStamp
718 // parameters accordingly (if possible).
722 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
723 // which corresponds to JD=2440587.5 or the start of MJD=40587.
724 // Using the corresponding MJD of this EPOCH allows construction of
725 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
726 // Obviously this TTimeStamp implementation would prevent usage of values
727 // smaller than JD=2440587.5.
728 // However, this AliTimestamp facility provides support for the full range
729 // of (M)JD values, but the setting of the corresponding TTimeStamp parameters
730 // is restricted to the values allowed by the TTimeStamp implementation.
731 // For these earlier JD values, the standard TTimeStamp parameters will
732 // be set corresponding to the start of the TTimeStamp EPOCH.
733 // This implies that for these earlier (M)JD values the TTimeStamp parameters
734 // do not match the Julian parameters of AliTimestamp.
736 // Due to computer accuracy the ns precision may be lost.
737 // It is advised to use the (jd,sec,ns) setting instead.
739 // The input argument represents the following :
740 // jd : The Julian date as fractional day count.
745 Convert(jd,days,secs,ns);
749 ///////////////////////////////////////////////////////////////////////////
750 void AliTimestamp::SetTJD(Int_t tjd,Int_t sec,Int_t ns,Int_t ps)
752 // Set the Truncated Julian Date (TJD) and time and update the TTimeStamp
753 // parameters accordingly (if possible).
757 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
758 // which corresponds to JD=2440587.5 or the start of TJD=587.
759 // Using the corresponding MJD of this EPOCH allows construction of
760 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
761 // Obviously this TTimeStamp implementation would prevent usage of values
762 // smaller than TJD=587.
763 // However, this AliTimestamp facility provides support for the full range
764 // of (T)JD values, but the setting of the corresponding TTimeStamp parameters
765 // is restricted to the values allowed by the TTimeStamp implementation.
766 // For these earlier JD values, the standard TTimeStamp parameters will
767 // be set corresponding to the start of the TTimeStamp EPOCH.
768 // This implies that for these earlier (T)JD values the TTimeStamp parameters
769 // do not match the Julian parameters of AliTimestamp.
771 // The input arguments represent the following :
772 // tjd : The Truncated Julian date.
773 // sec : The number of seconds elapsed within the JD.
774 // ns : The remaining fractional number of seconds (in ns) elapsed within the JD.
775 // ps : The remaining fractional number of nanoseconds (in ps) elapsed within the JD.
777 // Note : ps=0 is the default value.
781 SetMJD(mjd,sec,ns,ps);
783 ///////////////////////////////////////////////////////////////////////////
784 void AliTimestamp::SetTJD(Double_t tjd)
786 // Set the Truncated Julian Date (TJD) and time and update the TTimeStamp
787 // parameters accordingly (if possible).
791 // The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00 UTC
792 // which corresponds to JD=2440587.5 or the start of TJD=587.
793 // Using the corresponding MJD of this EPOCH allows construction of
794 // the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input MJD and time.
795 // Obviously this TTimeStamp implementation would prevent usage of values
796 // smaller than TJD=587.
797 // However, this AliTimestamp facility provides support for the full range
798 // of (T)JD values, but the setting of the corresponding TTimeStamp parameters
799 // is restricted to the values allowed by the TTimeStamp implementation.
800 // For these earlier JD values, the standard TTimeStamp parameters will
801 // be set corresponding to the start of the TTimeStamp EPOCH.
802 // This implies that for these earlier (T)JD values the TTimeStamp parameters
803 // do not match the Julian parameters of AliTimestamp.
805 // Due to computer accuracy the ns precision may be lost.
806 // It is advised to use the (jd,sec,ns) setting instead.
808 // The input argument represents the following :
809 // tjd : The Truncated Julian date as fractional day count.
814 Convert(tjd,days,secs,ns);
816 SetTJD(days,secs,ns);
818 ///////////////////////////////////////////////////////////////////////////
819 void AliTimestamp::SetNs(Int_t ns)
821 // Set the remaining fractional number of seconds in nanosecond precision.
824 // 1) The allowed range for the argument "ns" is [0,99999999].
825 // Outside that range no action is performed.
826 // 2) The ns fraction can also be entered directly via SetMJD() etc...
827 // 3) For additional accuracy see SetPs().
829 if (ns>=0 && ns<=99999999) fJns=ns;
831 ///////////////////////////////////////////////////////////////////////////
832 Int_t AliTimestamp::GetNs() const
834 // Provide the remaining fractional number of seconds in nanosecond precision.
835 // This function allows trigger/timing analysis for (astro)particle physics
837 // Note : For additional accuracy see also GetPs().
841 ///////////////////////////////////////////////////////////////////////////
842 void AliTimestamp::SetPs(Int_t ps)
844 // Set the remaining fractional number of nanoseconds in picoseconds.
847 // 1) The allowed range for the argument "ps" is [0,999].
848 // Outside that range no action is performed.
849 // 2) The ps fraction can also be entered directly via SetMJD() etc...
851 if (ps>=0 && ps<=999) fJps=ps;
853 ///////////////////////////////////////////////////////////////////////////
854 Int_t AliTimestamp::GetPs() const
856 // Provide remaining fractional number of nanoseconds in picoseconds.
857 // This function allows time of flight analysis for particle physics
862 ///////////////////////////////////////////////////////////////////////////
863 void AliTimestamp::Add(Int_t d,Int_t s,Int_t ns,Int_t ps)
865 // Add (or subtract) a certain time difference to the current timestamp.
866 // Subtraction can be achieved by entering negative values as input arguments.
868 // The time difference is entered via the following input arguments :
870 // d : elapsed number of days
871 // s : (remaining) elapsed number of seconds
872 // ns : (remaining) elapsed number of nanoseconds
873 // ps : (remaining) elapsed number of picoseconds
875 // The specified d, s, ns and ps values will be used in an additive
876 // way to determine the time difference.
877 // So, specification of d=1, s=100, ns=0, ps=0 will result in the
878 // same time difference addition as d=0, s=24*3600+100, ns=0, ps=0.
879 // However, by making use of the latter the user should take care
880 // of possible integer overflow problems in the input arguments,
881 // which obviously will provide incorrect results.
883 // Note : ps=0 is the default value.
888 // Use Get functions to ensure updated Julian parameters.
889 GetMJD(days,secs,nsec);
912 while (nsec>999999999)
925 while (secs>=24*3600)
933 SetMJD(days,secs,nsec,psec);
935 ///////////////////////////////////////////////////////////////////////////
936 Int_t AliTimestamp::GetDifference(AliTimestamp* t,Int_t& d,Int_t& s,Int_t& ns,Int_t& ps)
938 // Provide the time difference w.r.t the AliTimestamp specified on the input.
939 // This memberfunction supports both very small (i.e. time of flight analysis
940 // for particle physics experiments) and very long (i.e. investigation of
941 // astrophysical phenomena) timescales.
943 // The time difference is returned via the following output arguments :
944 // d : elapsed number of days
945 // s : remaining elapsed number of seconds
946 // ns : remaining elapsed number of nanoseconds
947 // ps : remaining elapsed number of picoseconds
951 // The calculated time difference is the absolute value of the time interval.
952 // This implies that the values of d, s, ns and ps are always positive or zero.
954 // The integer return argument indicates whether the AliTimestamp specified
955 // on the input argument occurred earlier (-1), simultaneously (0) or later (1).
959 // Ensure updated Julian parameters for this AliTimestamp instance
960 if (fCalcs != GetSec() || fCalcns != GetNanoSec()) FillJulian();
962 // Use Get functions to ensure updated Julian parameters.
971 if (!d && !s && !ns && !ps) return 0;
978 if (!sign && s>0) sign=1;
979 if (!sign && s<0) sign=-1;
981 if (!sign && ns>0) sign=1;
982 if (!sign && ns<0) sign=-1;
984 if (!sign && ps>0) sign=1;
985 if (!sign && ps<0) sign=-1;
987 // In case the input stamp was earlier, take the reverse difference
988 // to simplify the algebra.
997 // Here we always have a positive time difference
998 // and can now unambiguously correct for other negative values.
1019 ///////////////////////////////////////////////////////////////////////////
1020 Int_t AliTimestamp::GetDifference(AliTimestamp& t,Int_t& d,Int_t& s,Int_t& ns,Int_t& ps)
1022 // Provide the time difference w.r.t the AliTimestamp specified on the input.
1023 // This memberfunction supports both very small (i.e. time of flight analysis
1024 // for particle physics experiments) and very long (i.e. investigation of
1025 // astrophysical phenomena) timescales.
1027 // The time difference is returned via the following output arguments :
1028 // d : elapsed number of days
1029 // s : remaining elapsed number of seconds
1030 // ns : remaining elapsed number of nanoseconds
1031 // ps : remaining elapsed number of picoseconds
1035 // The calculated time difference is the absolute value of the time interval.
1036 // This implies that the values of d, s, ns and ps are always positive or zero.
1038 // The integer return argument indicates whether the AliTimestamp specified
1039 // on the input argument occurred earlier (-1), simultaneously (0) or later (1).
1041 return GetDifference(&t,d,s,ns,ps);
1043 ///////////////////////////////////////////////////////////////////////////
1044 Double_t AliTimestamp::GetDifference(AliTimestamp* t,TString u,Int_t mode)
1046 // Provide the time difference w.r.t the AliTimestamp specified on the input
1047 // argument in the units as specified by the TString argument.
1048 // A positive return value means that the AliTimestamp specified on the input
1049 // argument occurred later, whereas a negative return value indicates an
1050 // earlier occurence.
1052 // The units may be specified as :
1053 // u = "d" ==> Time difference returned as (fractional) day count
1054 // "s" ==> Time difference returned as (fractional) second count
1055 // "ns" ==> Time difference returned as (fractional) nanosecond count
1056 // "ps" ==> Time difference returned as picosecond count
1058 // It may be clear that for a time difference of several days, the picosecond
1059 // and even the nanosecond accuracy may be lost.
1060 // To cope with this, the "mode" argument has been introduced to allow
1061 // timestamp comparison on only the specified units.
1063 // The following operation modes are supported :
1064 // mode = 1 : Full time difference is returned in specified units
1065 // 2 : Time difference is returned in specified units by
1066 // neglecting the elapsed time for the larger units than the
1068 // 3 : Time difference is returned in specified units by only
1069 // comparing the timestamps on the level of the specified units.
1073 // AliTimestamp t1; // Corresponding to days=3, secs=501, ns=31, ps=7
1074 // AliTimestamp t2; // Corresponding to days=5, secs=535, ns=12, ps=15
1076 // The statement : Double_t val=t1.GetDifference(t2,....)
1077 // would return the following values :
1078 // val=(2*24*3600)+34-(19*1e-9)+(8*1e-12) for u="s" and mode=1
1079 // val=34-(19*1e-9)+(8*1e-12) for u="s" and mode=2
1080 // val=34 for u="s" and mode=3
1081 // val=-19 for u="ns" and mode=3
1083 // The default is mode=1.
1085 if (!t || mode<1 || mode>3) return 0;
1089 // Ensure updated Julian parameters for this AliTimestamp instance
1090 if (fCalcs != GetSec() || fCalcns != GetNanoSec()) FillJulian();
1097 // Use Get functions to ensure updated Julian parameters.
1098 t->GetMJD(dd,ds,dns);
1106 // Time difference for the specified units only
1111 if (u=="ns") dt=dns;
1112 if (u=="ps") dt=dps;
1116 // Suppress elapsed time for the larger units than specified
1133 // Compute the time difference as requested
1134 if (u=="s" || u=="d")
1136 // The time difference in (fractional) seconds
1137 dt=double(dd*24*3600+ds)+(double(dns)*1e-9)+(double(dps)*1e-12);
1138 if (u=="d") dt=dt/double(24*3600);
1140 if (u=="ns") dt=(double(dd*24*3600+ds)*1e9)+double(dns)+(double(dps)*1e-3);
1141 if (u=="ps") dt=(double(dd*24*3600+ds)*1e12)+(double(dns)*1e3)+double(dps);
1145 ///////////////////////////////////////////////////////////////////////////
1146 Double_t AliTimestamp::GetDifference(AliTimestamp& t,TString u,Int_t mode)
1148 // Provide the time difference w.r.t the AliTimestamp specified on the input
1149 // argument in the units as specified by the TString argument.
1150 // A positive return value means that the AliTimestamp specified on the input
1151 // argument occurred later, whereas a negative return value indicates an
1152 // earlier occurence.
1154 // The units may be specified as :
1155 // u = "d" ==> Time difference returned as (fractional) day count
1156 // "s" ==> Time difference returned as (fractional) second count
1157 // "ns" ==> Time difference returned as (fractional) nanosecond count
1158 // "ps" ==> Time difference returned as picosecond count
1160 // It may be clear that for a time difference of several days, the picosecond
1161 // and even the nanosecond accuracy may be lost.
1162 // To cope with this, the "mode" argument has been introduced to allow
1163 // timestamp comparison on only the specified units.
1165 // The following operation modes are supported :
1166 // mode = 1 : Full time difference is returned in specified units
1167 // 2 : Time difference is returned in specified units by
1168 // neglecting the elapsed time for the larger units than the
1170 // 3 : Time difference is returned in specified units by only
1171 // comparing the timestamps on the level of the specified units.
1175 // AliTimestamp t1; // Corresponding to days=3, secs=501, ns=31, ps=7
1176 // AliTimestamp t2; // Corresponding to days=5, secs=535, ns=12, ps=15
1178 // The statement : Double_t val=t1.GetDifference(t2,....)
1179 // would return the following values :
1180 // val=(2*24*3600)+34-(19*1e-9)+(8*1e-12) for u="s" and mode=1
1181 // val=34-(19*1e-9)+(8*1e-12) for u="s" and mode=2
1182 // val=34 for u="s" and mode=3
1183 // val=-19 for u="ns" and mode=3
1185 // The default is mode=1.
1187 return GetDifference(&t,u,mode);
1189 ///////////////////////////////////////////////////////////////////////////