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

Last change on this file since 6032 was 6016, checked in by mazin, 20 years ago
*** empty log message ***
File size: 11.0 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 = 1;
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=1;
153 while (tlist.Process())
154 {
155 MReportDrive* report = (MReportDrive*)plist.FindObject("MReportDrive");
156 MTime* reporttime = (MTime*)plist.FindObject("MTimeDrive");
157
158 if(n==1)
159 fFirstDriveTime = *reporttime;
160 else
161 fLastDriveTime = *reporttime;
162
163 //
164 // Update the number of entries
165 //
166 if (n>fNumStartEvents)
167 {
168 reportTime.Set(n);
169 currentZd.Set(n);
170 currentAz.Set(n);
171 nominalZd.Set(n);
172 nominalAz.Set(n);
173 ra.Set(n);
174 dec.Set(n);
175 }
176 //
177 // Sometimes there are two reports with the same time
178 //
179 if (reporttime->GetTime() == reportTime[n-1])
180 {
181 cout << warn <<"["<< GetName()
182 << "]: Warning: this report has the same time that the previous one...skipping it " << endl;
183 continue;
184 }
185
186 reportTime[n-1] = reporttime->GetTime();
187 currentZd [n-1] = report->GetCurrentZd();
188 currentAz [n-1] = report->GetCurrentAz();
189 nominalZd [n-1] = report->GetNominalZd();
190 nominalAz [n-1] = report->GetNominalAz();
191 ra [n-1] = report->GetRa();
192 dec [n-1] = report->GetDec();
193
194 if (fDebug)
195 {
196 *fLog << " GetTime(): " << reporttime->GetTime() << endl;
197 *fLog << " GetCurrentZd(): " << report->GetCurrentZd() << endl;
198 }
199 n++;
200 }
201
202 tlist.PrintStatistics();
203
204 *fLog << "["<< GetName() << "]: loaded " << n-1 << " ReportDrive from "
205 << fFirstDriveTime << " to " << fLastDriveTime << endl << endl;
206
207 if (fDebug)
208 {
209 for (int i=0;i<reportTime.GetSize();i++)
210 *fLog << i << " " << reportTime[i] << " "
211 << currentZd[i] << " " << currentAz[i] << " "
212 << nominalZd[i] << " " << nominalAz[i] << " "
213 << ra[i] << " " << dec[i] << endl;
214 }
215
216 fSplineZd = new TSpline3("zenith",
217 reportTime.GetArray(), nominalZd.GetArray(), n-1);
218 fSplineAz = new TSpline3("azimuth",
219 reportTime.GetArray(), nominalAz.GetArray(), n-1);
220 fSplineRa = new TSpline3("RA",
221 reportTime.GetArray(), ra.GetArray(), n-1);
222 fSplineDec = new TSpline3("DEC",
223 reportTime.GetArray(), dec.GetArray(), n-1);
224
225 if (fDebug)
226 {
227 TCanvas* c = new TCanvas();
228 c->Divide(2,2);
229 c->cd(1);
230 fSplineZd->Draw();
231 c->cd(2);
232 fSplineAz->Draw();
233 c->cd(3);
234 fSplineRa->Draw();
235 c->cd(4);
236 fSplineDec->Draw();
237 c->Modified();
238 c->Update();
239 c->SaveAs("pointing.root");
240 }
241 return kTRUE;
242}
243
244
245// --------------------------------------------------------------------------
246//
247// Input:
248// - MTime
249//
250// Output:
251// - MPointingPos
252//
253Int_t MInterpolatePointingPos::PreProcess( MParList *pList )
254{
255
256 Clear();
257
258 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
259 if (!fRunHeader)
260 {
261 *fLog << err << "MRunHeader not found... aborting." << endl;
262 return kFALSE;
263 }
264
265 fEvtTime = (MTime*)pList->FindObject("MTime");
266 if (!fEvtTime)
267 {
268 *fLog << err << "MTime not found... aborting." << endl;
269 return kFALSE;
270 }
271
272 fPointingPos = (MPointingPos*)pList->FindCreateObj("MPointingPos");
273 if (!fPointingPos)
274 return kFALSE;
275
276 if (fFilename.IsNull() && !fDirIter)
277 {
278 *fLog << err << "File name is empty or no MDirIter has been handed over... aborting" << endl;
279 return kFALSE;
280 }
281
282 if( !ReadDriveReport() )
283 return kFALSE;
284
285 return kTRUE;
286}
287
288
289// --------------------------------------------------------------------------
290//
291// Get the run start time, and get the pointing position for that time
292//
293Int_t MInterpolatePointingPos::Process()
294{
295
296 //const Int_t run = fRunHeader->GetRunNumber();
297 const MTime* StartRunTime = &fRunHeader->GetRunStart();
298 Int_t time = StartRunTime->GetTime();
299
300 switch(fTimeMode)
301 {
302 case kRunTime:
303 time = StartRunTime->GetTime();
304
305 //
306 // Check that we have drive report for this time
307 //
308 if( *StartRunTime<fFirstDriveTime || *StartRunTime>fLastDriveTime)
309 {
310 *fLog << err << GetDescriptor() << ": Run time " << *StartRunTime
311 << " outside range of drive reports (" << fFirstDriveTime
312 << ", " << fLastDriveTime << ")" << endl;
313
314 if ( *fEvtTime<fFirstDriveTime ) time = fFirstDriveTime.GetTime();
315 if ( *fEvtTime>fLastDriveTime ) time = fLastDriveTime.GetTime();
316
317 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << fSplineZd->Eval( time )<< ", " <<fSplineAz
318->Eval( time )<< ", " <<fSplineRa->Eval( time ) << ", " <<fSplineDec->Eval( time )<< ")" << endl;
319
320 //fError = kTRUE;
321 //return kFALSE;
322 }
323 break;
324
325 case kEventTime:
326 time = fEvtTime->GetTime();
327
328 if (fDebug)
329 {
330 cout << " real time : " << time
331 << " first time: " << fFirstDriveTime.GetTime()
332 << " last time: " << fLastDriveTime.GetTime() << endl;
333 }
334 //
335 // Check that we have drive report for this time
336 //
337 if( *fEvtTime<fFirstDriveTime || *fEvtTime>fLastDriveTime)
338 {
339 if (fDebug)
340 {
341 *fLog << err << GetDescriptor() << ": Run time = "
342 << *fEvtTime << " outside range of drive reports ("
343 << fFirstDriveTime << ", "<< fLastDriveTime << ")" << endl;
344
345 if ( *fEvtTime<fFirstDriveTime ) time = fFirstDriveTime.GetTime();
346 if ( *fEvtTime>fLastDriveTime ) time = fLastDriveTime.GetTime();
347
348 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << fSplineZd->Eval( time )<< ", " <<fSplineAz->Eval( time )<< ", " <<fSplineRa->Eval( time ) << ", " <<fSplineDec->Eval( time )<< ")" << endl;
349 }
350
351 //fError = kTRUE;
352 //return kFALSE;
353 //return kCONTINUE;
354 }
355 break;
356 }
357
358
359
360
361 //
362 // Check that we have drive report for this time
363 //
364 //if( *StartRunTime<fFirstDriveTime || *StartRunTime>fLastDriveTime)
365 if( *StartRunTime>fLastDriveTime)
366 {
367 *fLog << err << GetDescriptor() << ": Run time " << *StartRunTime
368 << " outside range of drive reports (" << fFirstDriveTime
369 << ", " << fLastDriveTime << ")" << endl;
370 return kERROR;
371 }
372
373 //if(run < 20000)
374 // time = fEvtTime->GetTime();
375 //else
376 // time = StartRunTime->GetTime();
377
378
379 const Double_t zd = fSplineZd->Eval( time );
380 const Double_t az = fSplineAz->Eval( time );
381 const Double_t ra = fSplineRa->Eval( time );
382 const Double_t dec = fSplineDec->Eval( time );
383
384
385 if(TMath::Abs(zd)>90 || TMath::Abs(az)>360 || ra>24 || ra<0 || TMath::Abs(dec)>90){
386 *fLog << err << GetDescriptor() << ": Wrong Interpolated Pointing Position." << endl;
387 *fLog << " PointingPos: time = " << time << " (" << *fEvtTime << ") (zd, az, ra, dec) = (" << zd << ", " << az << ", " << ra << ", " << dec << ")" << endl;
388 }
389
390 fPointingPos->SetLocalPosition( zd, az );
391 fPointingPos->SetSkyPosition( ra*TMath::DegToRad()/15, dec*TMath::DegToRad());
392
393 return kTRUE;
394}
395
396
397
398Int_t MInterpolatePointingPos::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
399{
400 Bool_t rc = kFALSE;
401
402 if (IsEnvDefined(env, prefix, "NumStartEvents", print))
403 {
404 SetNumStartEvents(GetEnvValue(env, prefix, "NumStartEvents", fNumStartEvents));
405 rc = kTRUE;
406 }
407 return rc;
408}
Note: See TracBrowser for help on using the repository browser.