source: trunk/MagicSoft/Mars/mtemp/mmpi/MInterpolatePointingPos.cc@ 5974

Last change on this file since 5974 was 5943, checked in by mazin, 21 years ago
*** empty log message ***
File size: 10.6 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!
18! Author(s): Marcos Lopez 03/2004 <mailto:marcos@gae.ucm.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MExtrapolatePointingPos
28//
29// In the PreProcess, read the drive report and store it in an TSpline.
30// In the Process, use the TSpline to calculate the PointingPos for the
31// time of the current event.
32//
33// Input Containers:
34// MRawEvtData
35//
36// Output Containers:
37// MPointingPos
38//
39//
40/////////////////////////////////////////////////////////////////////////////
41
42#include "MInterpolatePointingPos.h"
43
44#include "MLog.h"
45#include "MLogManip.h"
46
47#include <TSpline.h>
48
49#include "MTaskList.h"
50#include "MParList.h"
51#include "MEvtLoop.h"
52#include "MReadReports.h"
53#include "MReportDrive.h"
54#include "MPointingPos.h"
55#include "MTime.h"
56#include "MRawRunHeader.h"
57#include "MDirIter.h"
58
59#include <TCanvas.h>
60
61ClassImp(MInterpolatePointingPos);
62
63using namespace std;
64
65const Int_t MInterpolatePointingPos::fgNumStartEvents = 10000;
66// --------------------------------------------------------------------------
67//
68// Constructor
69//
70MInterpolatePointingPos::MInterpolatePointingPos(const char *name, const char *title)
71 : fEvtTime(NULL), fPointingPos(NULL), fRunHeader(NULL), fDirIter(NULL),
72 fSplineZd(NULL), fSplineAz(NULL), fSplineRa(NULL), fSplineDec(NULL),
73 fTimeMode(MInterpolatePointingPos::kEventTime)
74
75{
76 fName = name ? name : "MInterpolatePointingPos";
77 fTitle = title ? title : "Task to interpolate the pointing positons from drive reports";
78
79 fFilename = "";
80 fDebug = kFALSE;
81
82 SetNumStartEvents();
83}
84
85
86MInterpolatePointingPos::~MInterpolatePointingPos()
87{
88 Clear();
89}
90
91void MInterpolatePointingPos::Clear(Option_t *o)
92{
93 if(fSplineZd)
94 delete fSplineZd;
95 if(fSplineAz)
96 delete fSplineAz;
97 if(fSplineRa)
98 delete fSplineRa;
99 if(fSplineDec)
100 delete fSplineDec;
101}
102
103// ---------------------------------------------------------------------------
104//
105// Read the drive report file for the whole night, a build from it the splines
106//
107Bool_t MInterpolatePointingPos::ReadDriveReport()
108{
109
110 *fLog << endl << "["<< GetName()
111 << "]: Loading report file \"" << fFilename << "\" into TSpline..." << endl;
112
113 //
114 // ParList
115 //
116 MParList plist;
117 MTaskList tlist;
118 plist.AddToList(&tlist);
119
120
121 //
122 // TaskList
123 //
124 MReadReports read;
125 read.AddTree("Drive");
126 if (fDirIter)
127 read.AddFiles(*fDirIter);
128 else
129 read.AddFile(fFilename); // after the reading of the trees!!!
130
131 read.AddToBranchList("MReportDrive.*");
132
133 tlist.AddToList(&read);
134
135 //
136 // EventLoop
137 //
138 MEvtLoop evtloop;
139 evtloop.SetParList(&plist);
140
141 if (!evtloop.PreProcess())
142 return kFALSE;
143
144 TArrayD reportTime(fNumStartEvents);
145 TArrayD currentZd(fNumStartEvents);
146 TArrayD currentAz(fNumStartEvents);
147 TArrayD nominalZd(fNumStartEvents);
148 TArrayD nominalAz(fNumStartEvents);
149 TArrayD ra(fNumStartEvents);
150 TArrayD dec(fNumStartEvents);
151
152 Int_t n=0;
153 while (tlist.Process())
154 {
155 MReportDrive* report = (MReportDrive*)plist.FindObject("MReportDrive");
156 MTime* reporttime = (MTime*)plist.FindObject("MTimeDrive");
157
158 if(n==0)
159 fFirstDriveTime = *reporttime;
160 else
161 fLastDriveTime = *reporttime;
162
163 //
164 // Sometimes there are two reports with the same time
165 //
166 if (reporttime->GetTime() == reportTime[n-1])
167 {
168 cout << warn <<"["<< GetName()
169 << "]: Warning: this report has the same time that the previous one...skipping it " << endl;
170 continue;
171 }
172
173 reportTime[n] = reporttime->GetTime();
174 currentZd[n] = report->GetCurrentZd();
175 currentAz[n] = report->GetCurrentAz();
176 nominalZd[n] = report->GetNominalZd();
177 nominalAz[n] = report->GetNominalAz();
178 ra[n] = report->GetRa();
179 dec[n] = report->GetDec();
180
181//cout << " GetTime(): " << reporttime->GetTime() << endl;
182//cout << " GetCurrentZd(): " << report->GetCurrentZd() << endl;
183 n++;
184 }
185
186 tlist.PrintStatistics();
187
188 *fLog << "["<< GetName() << "]: loaded " << n << " ReportDrive from "
189 << fFirstDriveTime << " to " << fLastDriveTime << endl << endl;
190
191 //
192 // Update the number of entries
193 //
194 reportTime.Set(n);
195 currentZd.Set(n);
196 currentAz.Set(n);
197 nominalZd.Set(n);
198 nominalAz.Set(n);
199 ra.Set(n);
200 dec.Set(n);
201
202 if (fDebug)
203 {
204 for (int i=0;i<reportTime.GetSize();i++)
205 *fLog << i << " " << reportTime[i] << " "
206 << currentZd[i] << " " << currentAz[i] << " "
207 << nominalZd[i] << " " << nominalAz[i] << " "
208 << ra[i] << " " << dec[i] << endl;
209 }
210
211 fSplineZd = new TSpline3("zenith",
212 reportTime.GetArray(), nominalZd.GetArray(), n);
213 fSplineAz = new TSpline3("azimuth",
214 reportTime.GetArray(), nominalAz.GetArray(), n);
215 fSplineRa = new TSpline3("RA",
216 reportTime.GetArray(), ra.GetArray(), n);
217 fSplineDec = new TSpline3("DEC",
218 reportTime.GetArray(), dec.GetArray(), n);
219
220 if (fDebug)
221 {
222 TCanvas* c = new TCanvas();
223 c->Divide(2,2);
224 c->cd(1);
225 fSplineZd->Draw();
226 c->cd(2);
227 fSplineAz->Draw();
228 c->cd(3);
229 fSplineRa->Draw();
230 c->cd(4);
231 fSplineDec->Draw();
232 c->Modified();
233 c->Update();
234 }
235 return kTRUE;
236}
237
238
239// --------------------------------------------------------------------------
240//
241// Input:
242// - MTime
243//
244// Output:
245// - MPointingPos
246//
247Int_t MInterpolatePointingPos::PreProcess( MParList *pList )
248{
249
250 Clear();
251
252 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
253 if (!fRunHeader)
254 {
255 *fLog << err << "MRunHeader not found... aborting." << endl;
256 return kFALSE;
257 }
258
259 fEvtTime = (MTime*)pList->FindObject("MTime");
260 if (!fEvtTime)
261 {
262 *fLog << err << "MTime not found... aborting." << endl;
263 return kFALSE;
264 }
265
266 fPointingPos = (MPointingPos*)pList->FindCreateObj("MPointingPos");
267 if (!fPointingPos)
268 return kFALSE;
269
270 if (fFilename.IsNull() && !fDirIter)
271 {
272 *fLog << err << "File name is empty or no MDirIter has been handed over... aborting" << endl;
273 return kFALSE;
274 }
275
276 if( !ReadDriveReport() )
277 return kFALSE;
278
279 return kTRUE;
280}
281
282
283// --------------------------------------------------------------------------
284//
285// Get the run start time, and get the pointing position for that time
286//
287Int_t MInterpolatePointingPos::Process()
288{
289
290 //const Int_t run = fRunHeader->GetRunNumber();
291 const MTime* StartRunTime = &fRunHeader->GetRunStart();
292 Double_t time = StartRunTime->GetTime();
293
294 switch(fTimeMode)
295 {
296 case kRunTime:
297 time = StartRunTime->GetTime();
298
299 //
300 // Check that we have drive report for this time
301 //
302 if( *StartRunTime<fFirstDriveTime || *StartRunTime>fLastDriveTime)
303 {
304 *fLog << err << GetDescriptor() << ": Run time " << *StartRunTime
305 << " outside range of drive reports (" << fFirstDriveTime
306 << ", " << fLastDriveTime << ")" << endl;
307
308 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << fSplineZd->Eval( time )<< ", " <<fSplineAz
309->Eval( time )<< ", " <<fSplineRa->Eval( time ) << ", " <<fSplineDec->Eval( time )<< ")" << endl;
310
311 //fError = kTRUE;
312 //return kFALSE;
313 }
314 break;
315
316 case kEventTime:
317 time = fEvtTime->GetTime();
318
319 if (fDebug)
320 {
321 cout << " real time : " << time
322 << " first time: " << fFirstDriveTime.GetTime()
323 << " last time: " << fLastDriveTime.GetTime() << endl;
324 }
325 //
326 // Check that we have drive report for this time
327 //
328 if( *fEvtTime<fFirstDriveTime || *fEvtTime>fLastDriveTime)
329 {
330 *fLog << err << GetDescriptor() << ": Run time = "
331 << *fEvtTime << " outside range of drive reports ("
332 << fFirstDriveTime << ", "<< fLastDriveTime << ")" << endl;
333
334 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << fSplineZd->Eval( time )<< ", " <<fSplineAz->Eval( time )<< ", " <<fSplineRa->Eval( time ) << ", " <<fSplineDec->Eval( time )<< ")" << endl;
335
336 if ( *fEvtTime<fFirstDriveTime ) time = fFirstDriveTime.GetTime();
337 if ( *fEvtTime>fLastDriveTime ) time = fLastDriveTime.GetTime();
338 //fError = kTRUE;
339 //return kFALSE;
340 //return kCONTINUE;
341 }
342 break;
343 }
344
345
346
347
348 //
349 // Check that we have drive report for this time
350 //
351 if( *StartRunTime<fFirstDriveTime || *StartRunTime>fLastDriveTime)
352 {
353 *fLog << err << GetDescriptor() << ": Run time " << *StartRunTime
354 << " outside range of drive reports (" << fFirstDriveTime
355 << ", " << fLastDriveTime << ")" << endl;
356 return kERROR;
357 }
358
359 //if(run < 20000)
360 // time = fEvtTime->GetTime();
361 //else
362 // time = StartRunTime->GetTime();
363
364
365 const Double_t zd = fSplineZd->Eval( time );
366 const Double_t az = fSplineAz->Eval( time );
367 const Double_t ra = fSplineRa->Eval( time );
368 const Double_t dec = fSplineDec->Eval( time );
369
370
371 if(TMath::Abs(zd)>90 || TMath::Abs(az)>360 || ra>24 || ra<0 || TMath::Abs(dec)>90){
372 *fLog << err << GetDescriptor() << ": Wrong Interpolated Pointing Position." << endl;
373 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << zd << ", " << az << ", " << ra << ", " << dec << ")" << endl;
374 }
375
376 fPointingPos->SetLocalPosition( zd, az );
377 fPointingPos->SetSkyPosition( ra*TMath::DegToRad()/15, dec*TMath::DegToRad());
378
379 return kTRUE;
380}
381
382
383
384Int_t MInterpolatePointingPos::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
385{
386 Bool_t rc = kFALSE;
387
388 if (IsEnvDefined(env, prefix, "NumStartEvents", print))
389 {
390 SetNumStartEvents(GetEnvValue(env, prefix, "NumStartEvents", fNumStartEvents));
391 rc = kTRUE;
392 }
393 return rc;
394}
Note: See TracBrowser for help on using the repository browser.