source: trunk/MagicSoft/Mars/mtemp/mifae/library/MLiveTimeCalc.cc@ 6977

Last change on this file since 6977 was 4429, checked in by jlopez, 20 years ago
*** empty log message ***
File size: 9.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17! Author(s): Javier López , 7/2004 <mailto:jlopez@ifae.es>
18!
19! Copyright: MAGIC Software Development, 2000-2004
20!
21!
22\* ======================================================================== */
23
24/////////////////////////////////////////////////////////////////////////////
25//
26// MLiveTimeCalc
27//
28/////////////////////////////////////////////////////////////////////////////
29#include "MLiveTimeCalc.h"
30
31#include "MRawRunHeader.h"
32#include "MRawEvtHeader.h"
33#include "MLiveTime.h"
34
35#include "MLog.h"
36#include "MLogManip.h"
37
38#include "MParList.h"
39#include "MTaskList.h"
40
41ClassImp(MLiveTimeCalc)
42using namespace std;
43
44Bool_t Debug = kFALSE;
45Bool_t PrintNewRun = kTRUE;
46
47MLiveTimeCalc::MLiveTimeCalc(const char *name, const char *title) : kSecTomSec(1e3), kDayToSec(24.*60.*60.), fRunHeader(NULL), fEvtHeader(NULL), fPresentEventTime(NULL), fLiveTime(NULL)
48{
49
50 fName = name ? name : "MLiveTimeCalc";
51 fTitle = title ? title : "Task to compute the live time from real data.";
52
53 for (Int_t i=0; i<10; i++)
54 fNumberLostEvents[i] = 0;
55
56 fFirstEventMjd = 0.;
57 fPresentEventMjd = 0.;
58 fLastEventMjd = 0.;
59
60 fPresentEventNumber = 0;
61 fLastEventNumber = 0;
62
63 fPresentEventRunNumber = 0;
64 fLastEventRunNumber = 0;
65
66 fRealTimeBinSize = 0.;
67
68 fPrintNextEvent = kFALSE;
69}
70
71Int_t MLiveTimeCalc::PreProcess(MParList *pList)
72{
73
74 fEvtHeader = (MRawEvtHeader*)pList->FindObject(AddSerialNumber("MRawEvtHeader"));
75 if (!fEvtHeader)
76 {
77 *fLog << err << AddSerialNumber("MRawEvtHeader") << " not found ... aborting" << endl;
78 return kFALSE;
79 }
80
81 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
82 if (!fRunHeader)
83 {
84 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found ... aborting" << endl;
85 return kFALSE;
86 }
87
88 fPresentEventTime = (MTime*)pList->FindObject(AddSerialNumber("MTime"));
89 if (!fPresentEventTime)
90 {
91 *fLog << err << AddSerialNumber("MTime") << " not found ... aborting" << endl;
92 return kFALSE;
93 }
94
95
96 fLiveTime = (MLiveTime*)pList->FindCreateObj(AddSerialNumber("MLiveTime"));
97 if (!fLiveTime)
98 {
99 *fLog << err << AddSerialNumber("MLiveTime") << " cannot be created ... aborting" << endl;
100 return kFALSE;
101 }
102
103 return kTRUE;
104}
105
106Int_t MLiveTimeCalc::Process()
107{
108
109 fPresentEventMjd = fPresentEventTime->GetMjd();
110 fLastEventMjd = fLastEventTime.GetMjd();
111 fPreaviusLastEventMjd = fPreaviusLastEventTime.GetMjd();
112
113 fPresentEventNumber = fEvtHeader->GetDAQEvtNumber();
114 fPresentEventRunNumber = fRunHeader->GetRunNumber();;
115
116 if (fPrintNextEvent && Debug)
117 {
118 *fLog << dbg << GetName() << ": Printing next event" << endl;
119 Print("all");
120 fLiveTime->Print("last");
121 *fLog << inf << endl;
122 fPrintNextEvent = kFALSE;
123 }
124
125 if (fFirstEventMjd == 0)
126 {
127 fFirstEventMjd = fPresentEventMjd;
128
129 fPreaviusLastEventTime = fLastEventTime;
130 fLastEventTime = *fPresentEventTime;
131
132 fPreaviusLastEventRunNumber = fLastEventRunNumber;
133 fLastEventRunNumber = fPresentEventRunNumber;
134
135 fPreaviusLastEventNumber = fLastEventNumber;
136 fLastEventNumber = fPresentEventNumber;
137 }
138 else
139 {
140 if (isTimeStampOk())
141 {
142 if (fRealTimeBinSize > 0 &&
143 (fPresentEventMjd - fFirstEventMjd)*kDayToSec > fRealTimeBinSize)
144 {
145
146 Double_t width = (fLastEventMjd - fFirstEventMjd)/2;
147 Double_t mean = fFirstEventMjd + width;
148
149 fLiveTime->SetRealTime(mean,width);
150
151 if (Debug)
152 {
153 *fLog << inf << GetName() << ": New time bin" << endl;
154 Print("all");
155 *fLog << GetName() << ": First event time " << setprecision(10) << fFirstEventMjd << setprecision(5) << endl;
156 fLiveTime->Print("last");
157 }
158
159 fLiveTime->AddBin();
160 fFirstEventMjd = fPresentEventMjd;
161 }
162
163 if (fPresentEventRunNumber!=fLastEventRunNumber)
164 {
165 if (fLastEventRunNumber != 0 && PrintNewRun)
166 {
167 *fLog << dbg << GetName() << ": New run" << endl;
168 Print("all");
169 fLiveTime->Print("last");
170 *fLog << inf << endl;
171 }
172 fLastEventRunNumber = fPresentEventRunNumber;
173 }
174 else
175 fLiveTime->AddTime((fPresentEventMjd-fLastEventMjd)*kDayToSec);
176
177
178
179 fPreaviusLastEventTime = fLastEventTime;
180 fLastEventTime = *fPresentEventTime;
181
182 fPreaviusLastEventRunNumber = fLastEventRunNumber;
183 fLastEventRunNumber = fPresentEventRunNumber;
184
185 fPreaviusLastEventNumber = fLastEventNumber;
186 fLastEventNumber = fPresentEventNumber;
187
188 }
189 }
190
191 return kTRUE;
192}
193
194Int_t MLiveTimeCalc::PostProcess()
195{
196 Double_t width = (fLastEventMjd - fFirstEventMjd)/2;
197 Double_t mean = fFirstEventMjd + width;
198
199 fLiveTime->SetRealTime(mean,width);
200
201 *fLog << dbg << endl;
202 *fLog << dbg << GetName() << ": PostProcess" << endl;
203 fLiveTime->Print("all");
204 *fLog << inf << endl;
205
206 *fLog << GetName() << " execution statistics:" << endl;
207 *fLog << dec << setfill(' ');
208
209 ULong_t fTotalNumberLostEvents = 0;
210 for (Int_t i=0; i<6; i++)
211 fTotalNumberLostEvents += fNumberLostEvents[i];
212
213 *fLog << " " << setw(7) << fTotalNumberLostEvents << " (" << setw(3) ;
214 *fLog << (Int_t)(fTotalNumberLostEvents*100/GetNumExecutions()) ;
215 *fLog << "%) bad time stamp events" << endl;
216
217 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[0]*100/fTotalNumberLostEvents) ;
218 *fLog << "%) time stamp == 0" << endl;
219
220 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[1]*100/fTotalNumberLostEvents) ;
221 *fLog << "%) last time stamp == 0" << endl;
222
223 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[2]*100/fTotalNumberLostEvents) ;
224 *fLog << "%) time stamp in the past" << endl;
225
226 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[3]*100/fTotalNumberLostEvents) ;
227 *fLog << "%) time stamp == last one" << endl;
228
229 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[4]*100/fTotalNumberLostEvents) ;
230 *fLog << "%) time stamp just with integer part" << endl;
231
232 *fLog << "\t\t(" << setw(3) << (Int_t)(fNumberLostEvents[5]*100/fTotalNumberLostEvents) ;
233 *fLog << "%) run number < last one" << endl;
234
235 return kTRUE;
236}
237
238Bool_t MLiveTimeCalc::isTimeStampOk()
239{
240
241 Bool_t result = kTRUE;
242
243
244 if (fPresentEventMjd == 0)
245 {
246
247 if (Debug)
248 {
249 *fLog << err << GetName() << ": Present event time stamp equal to 0" << endl;
250 Print("all");
251 }
252
253 fNumberLostEvents[0]++;
254 result = kFALSE;
255 fPrintNextEvent = kTRUE;
256 }
257 else if (fLastEventMjd == 0)
258 {
259
260 if (Debug)
261 {
262 *fLog << err << GetName() << ": Last event time stamp equal to 0" << endl;
263 Print("all");
264 }
265
266 fNumberLostEvents[1]++;
267 result = kFALSE;
268 fPrintNextEvent = kTRUE;
269 }
270 else if (fPresentEventMjd-fLastEventMjd < 0)
271 {
272
273 if (Debug)
274 {
275 *fLog << err << GetName() << ": Present event with time stamp in the past" << endl;
276 Print("all");
277 }
278
279 fNumberLostEvents[2]++;
280 result = kFALSE;
281 fPrintNextEvent = kTRUE;
282 }
283 else if (fPresentEventMjd-fLastEventMjd == 0)
284 {
285
286 if (Debug)
287 {
288 *fLog << err << GetName() << ": Present event time stamp equal than last event" << endl;
289 Print("all");
290 }
291
292 fNumberLostEvents[3]++;
293 result = kFALSE;
294 fPrintNextEvent = kTRUE;
295 }
296 else if (fPresentEventNumber- fLastEventNumber<= 0 && fPresentEventRunNumber == fLastEventRunNumber)
297 {
298
299 if (Debug)
300 {
301 *fLog << warn << GetName() << ": Present event number equal or smaller than last event" << endl;
302 Print("all");
303 }
304
305 result = kTRUE;
306 fPrintNextEvent = kTRUE;
307 }
308 else if ((Int_t)fPresentEventMjd == fPresentEventMjd)
309 {
310
311 if (Debug)
312 {
313 *fLog << err << GetName() << ": Present event time stamp idetical to midnight" << endl;
314 Print("all");
315 }
316
317 fNumberLostEvents[4]++;
318 result = kFALSE;
319 fPrintNextEvent = kTRUE;
320 }
321 else if ((fPresentEventMjd-fLastEventMjd)*kDayToSec > 1.)
322 {
323
324 if (Debug)
325 {
326 *fLog << warn << GetName() << ": Time from last event bigger than 1 sec" << endl;
327 Print("all");
328 }
329
330 result = kTRUE;
331 fPrintNextEvent = kTRUE;
332 }
333 else if (fPresentEventRunNumber-fLastEventRunNumber < 0)
334 {
335
336 if (Debug)
337 {
338 *fLog << warn << GetName() << ": Present run number previuos than last one" << endl;
339 Print("all");
340 }
341
342 fNumberLostEvents[5]++;
343 result = kTRUE;
344 fPrintNextEvent = kTRUE;
345 }
346
347 return result;
348}
349
350void MLiveTimeCalc::Print(const Option_t *o) const
351{
352
353 *fLog << "Present event run number [" << fPresentEventRunNumber << "] event number [" << fPresentEventNumber << ']' << endl;
354 *fLog << "Last event run number [" << fLastEventRunNumber << "] event number [" << fLastEventNumber << ']' << endl;
355 *fLog << "Preavius last event run number [" << fPreaviusLastEventRunNumber << "] event number [" << fPreaviusLastEventNumber << ']' << endl;
356 *fLog << "Present, last and preavius to last event times:" << endl;
357 fPresentEventTime->Print();
358 fLastEventTime.Print();
359 fPreaviusLastEventTime.Print();
360}
Note: See TracBrowser for help on using the repository browser.