source: trunk/MagicSoft/Mars/mpointing/MSrcPosCalc.cc@ 7181

Last change on this file since 7181 was 7181, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 14.5 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): Thomas Bretz 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Abelardo Moralejo 1/2005 <mailto:moralejo@pd.infn.it>
20!
21! Copyright: MAGIC Software Development, 2000-2005
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MSrcPosCalc
29//
30// Calculate the current source position in the camera from the (possibly
31// already corrected, by starguider) J2000 sky coordinates of the camera
32// center (contained in MPointingPos), and the source J2000 sky
33// coordinates contained in MSourcePos (of type MPointingPos as well). If
34// no MSourcePos is found in the parameter list, source position is
35// assumed to be the same for all events, that specified in MSrcPosCam
36// (if it already existed in the parameter list), or (0,0), the center of
37// the camera, if no MSrcPosCam was present in the parameter list. In both
38// cases, no calculation is necessary and then the PreProcess returns
39// kSKIP so that the task is removed from the task list.
40//
41// The conversion factor between the camera plain (mm) and the sky (deg)
42// is taken from MGeomCam. The time is taken from MTime, and the
43// coordinates of the observatory from MObservatory.
44//
45// Input Container:
46// MPointingPos
47// MObservatory
48// MGeomCam
49// MTime
50// [MSourcePos] (of type MPointingPos)
51//
52// Output Container:
53// MSrcPosCam
54//
55// To be done:
56// - a switch between using sky-coordinates and time or local-coordinates
57// from MPointingPos for determine the rotation angle
58///// NOTE, A. Moralejo: the above is not possible if MAstroSky2Local does not
59///// account for precession and nutation.
60//
61// - the center of rotation need not to be the camera center
62///// NOTE, A. Moralejo: I don't see the need for special code either.
63//
64//////////////////////////////////////////////////////////////////////////////
65#include "MSrcPosCalc.h"
66
67#include <TVector2.h>
68
69#include "MParList.h"
70
71#include "MLog.h"
72#include "MLogManip.h"
73
74#include "MGeomCam.h"
75#include "MObservatory.h"
76#include "MPointingPos.h"
77#include "MSrcPosCam.h"
78#include "MRawRunHeader.h"
79#include "MMcCorsikaRunHeader.h"
80
81#include "MAstro.h"
82#include "MVector3.h"
83#include "MAstroSky2Local.h"
84
85ClassImp(MSrcPosCalc);
86
87using namespace std;
88
89// --------------------------------------------------------------------------
90//
91// Initialize fY and fY with 0
92//
93MSrcPosCalc::MSrcPosCalc(const char *name, const char *title)
94 : fObservatory(NULL), fPointPos(NULL), fSourcePos(NULL), fSrcPosCam(NULL),
95 fSrcPosAnti(NULL), fGeom(NULL), fTime(NULL), fMode(kDefault)
96{
97 fName = name ? name : "MSrcPosCalc";
98 fTitle = title ? title : "Calculates the source position in the camera";
99
100 AddToBranchList("MTime.*");
101 AddToBranchList("MPointingPos.*");
102}
103
104// --------------------------------------------------------------------------
105//
106// delete fSourcePos if kIsOwner
107// set fSourcePos to NULL
108//
109void MSrcPosCalc::FreeSourcePos()
110{
111 if (fSourcePos && TestBit(kIsOwner))
112 delete fSourcePos;
113
114 fSourcePos = NULL;
115}
116
117// --------------------------------------------------------------------------
118//
119// ra [h]
120// dec [deg]
121//
122void MSrcPosCalc::SetSourcePos(Double_t ra, Double_t dec)
123{
124 FreeSourcePos();
125
126 fSourcePos = new MPointingPos("MSourcePos");
127 fSourcePos->SetSkyPosition(ra, dec);
128
129 SetOwner();
130}
131
132// --------------------------------------------------------------------------
133//
134// Return ra/dec as string
135//
136TString MSrcPosCalc::GetRaDec(const MPointingPos &pos) const
137{
138 const TString rstr = MAstro::Angle2Coordinate(pos.GetRa());
139 const TString dstr = MAstro::Angle2Coordinate(pos.GetDec());
140
141 return Form("Ra=%sh Dec=%sdeg", rstr.Data(), dstr.Data());
142}
143
144// --------------------------------------------------------------------------
145//
146// Search and if necessary create MSrcPosCam in the parameter list. Search
147// MSourcePos. If not found, do nothing else, and skip the task. If MSrcPosCam
148// did not exist before and has been created here, it will contain as source
149// position the camera center (0,0).
150// In the case that MSourcePos is found, go ahead in searching the rest of
151// necessary containers. The source position will be calculated for each
152// event in Process.
153//
154Int_t MSrcPosCalc::PreProcess(MParList *pList)
155{
156 fSrcPosCam = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam");
157 if (!fSrcPosCam)
158 return kFALSE;
159
160 fSrcPosAnti = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", "MSrcPosAnti");
161 if (!fSrcPosAnti)
162 return kFALSE;
163
164 if (!fSourcePos)
165 {
166 fSourcePos = (MPointingPos*)pList->FindObject("MSourcePos", "MPointingPos");
167 if (!fSourcePos)
168 {
169 *fLog << warn;
170 *fLog << "MSourcePos [MPointPos] not found... The source position" << endl;
171 *fLog << "set in MSrcPosCam (camera center if not set explicitely)" << endl;
172 *fLog << "will be left unchanged if not wobble mode Monte Carlo." << endl;
173 return kTRUE;
174 }
175 }
176 // FIXME: Maybe we have to call FreeSourcePos in PostProcess()?
177
178 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
179 if (!fGeom)
180 {
181 *fLog << err << "MGeomCam not found... aborting." << endl;
182 return kFALSE;
183 }
184
185 fPointPos = (MPointingPos*)pList->FindObject("MPointingPos");
186 if (!fPointPos)
187 {
188 *fLog << err << "MPointingPos not found... aborting." << endl;
189 return kFALSE;
190 }
191
192 *fLog << inf;
193 //*fLog << "Pointing Position: " << GetRaDec(*fPointPos) << endl;
194 *fLog << "Source Position: " << GetRaDec(*fSourcePos) << endl;
195
196 // For the case ReInit is never called we try:
197 fObservatory = (MObservatory*)pList->FindObject("MObservatory");
198 fTime = (MTime*) pList->FindObject("MTime");
199 fRunType = MRawRunHeader::kRTNone;
200
201 return kTRUE;
202}
203
204// --------------------------------------------------------------------------
205//
206// If fIsWobbleMode==kFALSE set source position to v and anto-source
207// position to -v, if fIsWobbleMode==kTRUE vice versa.
208//
209void MSrcPosCalc::SetSrcPos(TVector2 v) const
210{
211 if (fMode==kWobble)
212 {
213 fSrcPosAnti->SetXY(v);
214 v *= -1;
215 fSrcPosCam->SetXY(v);
216 }
217 else
218 {
219 fSrcPosCam->SetXY(v);
220 v *= -1;
221 fSrcPosAnti->SetXY(v);
222 }
223}
224
225// --------------------------------------------------------------------------
226//
227// Checking for file type. If the file type is Monte Carlo the
228// source position is arbitrarily determined from the MC headers.
229//
230Bool_t MSrcPosCalc::ReInit(MParList *plist)
231{
232 if (fMode==kOffData)
233 {
234 SetSrcPos(TVector2());
235 return kTRUE;
236 }
237
238 MRawRunHeader *run = (MRawRunHeader*)plist->FindObject("MRawRunHeader");
239 if (!run)
240 {
241 *fLog << err << "MRawRunHeader not found... aborting." << endl;
242 return kFALSE;
243 }
244
245 fRunType = run->GetRunType();
246
247 if (fRunType!=MRawRunHeader::kRTMonteCarlo)
248 {
249 fObservatory = (MObservatory*)plist->FindObject("MObservatory");
250 if (!fObservatory)
251 {
252 *fLog << err << "MObservatory not found... aborting." << endl;
253 return kFALSE;
254 }
255 fTime = (MTime*)plist->FindObject("MTime");
256 if (!fTime)
257 {
258 *fLog << err << "MTime not found... aborting." << endl;
259 return kFALSE;
260 }
261 return kTRUE;
262 }
263
264 MMcCorsikaRunHeader *h = (MMcCorsikaRunHeader*)plist->FindObject("MMcCorsikaRunHeader");
265 if (!h)
266 {
267 *fLog << err << "MMcCorsikaRunHeader not found... aborting." << endl;
268 return kFALSE;
269 }
270
271 TVector2 v(0, 0);
272 if (h->GetWobbleMode()>0.5)
273 v.Set(120., 0.);
274 if (h->GetWobbleMode()<-0.5)
275 v.Set(-120., 0.);
276
277
278 SetSrcPos(v);
279
280 *fLog << inf;
281 *fLog << "Source Position set to x=" << fSrcPosCam->GetX() << "mm ";
282 *fLog << "y=" << fSrcPosCam->GetY() << "mm" << endl;
283
284 return kTRUE;
285}
286
287// --------------------------------------------------------------------------
288//
289// Loc0LocToCam
290//
291// Input : (theta0, phi0) direction for the position (0,0) in the camera
292// ( theta, phi) some other direction
293//
294// Output : (X, Y) position in the camera corresponding to (theta, phi)
295//
296TVector2 MSrcPosCalc::CalcXYinCamera(const MVector3 &pos0, const MVector3 &pos) const
297{
298 const Double_t theta0 = pos0.Theta();
299 const Double_t phi0 = pos0.Phi();
300
301 const Double_t theta = pos.Theta();
302 const Double_t phi = pos.Phi();
303
304 //--------------------------------------------
305
306 /* --- OLD ---
307 const Double_t YC0 = TMath::Cos(theta0)*TMath::Tan(theta)*TMath::Cos(phi-phi0) - TMath::Sin(theta0);
308 const Double_t YC1 = TMath::Cos(theta0) + TMath::Sin(theta0)*TMath::Tan(theta);
309 const Double_t YC = YC0 / YC1;
310
311 const Double_t XC0 = TMath::Cos(theta0) - YC*TMath::Sin(theta0);
312 const Double_t XC = -TMath::Sin(phi-phi0) * TMath::Tan(theta) * XC0;
313 */
314
315 /* --- NEW --- */
316 const Double_t XC0 = TMath::Sin(theta)*TMath::Sin(phi-phi0);
317 const Double_t XC1 = TMath::Cos(theta0)*TMath::Cos(theta);
318 const Double_t XC2 = TMath::Sin(theta0)*TMath::Sin(theta)*TMath::Cos(phi-phi0);
319
320 const Double_t YC0 = TMath::Sin(theta0)*TMath::Cos(theta);
321 const Double_t YC1 = TMath::Cos(theta0)*TMath::Sin(theta)*TMath::Cos(phi-phi0);
322
323 const Double_t XC = - XC0 / (XC1 + XC2);
324 const Double_t YC = (-YC0+YC1) / (XC1 + XC2);
325
326 //--------------------------------------------
327 return TVector2(XC, YC);
328}
329
330// --------------------------------------------------------------------------
331//
332// Derotate fX/fY by the current rotation angle, set MSrcPosCam
333//
334Int_t MSrcPosCalc::Process()
335{
336 if (fRunType==MRawRunHeader::kRTMonteCarlo || !fSourcePos || !fTime || !fObservatory || fMode==kOffData)
337 return kTRUE;
338
339 // Set Sky coordinates of source, taken from container "MSourcePos"
340 // of type MPointingPos. The sky coordinates must be J2000, as the
341 // sky coordinates of the camera center that we get from the container
342 // "MPointingPos" filled by the Drive.
343 MVector3 pos; // pos: source position
344 pos.SetRaDec(fSourcePos->GetRaRad(), fSourcePos->GetDecRad());
345
346 // Convert sky coordinates of source to local coordinates. Warning! These are not the "true" local
347 // coordinates, since this transformation ignores precession and nutation effects.
348 const MAstroSky2Local conv(*fTime, *fObservatory);
349 pos *= conv;
350
351 // Set sky coordinates of camera center in pos0, and then convert to
352 // local. Same comment as above. These coordinates differ from the true
353 // local coordinates of the camera center that one could get from
354 // "MPointingPos", calculated by the Drive: the reason is that the Drive
355 // takes into account precession and nutation corrections, while
356 // MAstroSky2Local (as of Jan 27 2005 at least) does not. Since we just
357 // want to get the source position on the camera from the local
358 // coordinates of the center and the source, it does not matter that
359 // the coordinates contained in pos and pos0 ignore precession and
360 // nutation... since the shift would be the same in both cases. What
361 // would be wrong is to set in pos0 directly the local coordinates
362 // found in MPointingPos!
363 MVector3 pos0; // pos0: camera center
364 pos0.SetRaDec(fPointPos->GetRaRad(), fPointPos->GetDecRad());
365 pos0 *= conv;
366
367 // Calculate source position in camera, and convert to mm:
368 TVector2 v = CalcXYinCamera(pos0, pos)*fGeom->GetCameraDist()*1000;
369 SetSrcPos(v);
370
371 return kTRUE;
372}
373
374// --------------------------------------------------------------------------
375//
376// Convert a coordinate stored in str into a double, stored in ret.
377// Returns kTRUE on success, otherwise kFALSE
378//
379// Allowed formats are:
380// 12.5
381// -12.5
382// +12.5
383// -12:30:00.0
384// 12:30:00.0
385// +12:30:00.0
386//
387Bool_t MSrcPosCalc::GetCoordinate(TString str, Double_t &ret) const
388{
389 str = str.Strip(TString::kBoth);
390
391 if (str.First(':')<0)
392 {
393 ret = atof(str);
394 return kTRUE;
395 }
396
397 if (MAstro::Coordinate2Angle(str, ret))
398 return kTRUE;
399
400 *fLog << err << GetDescriptor() << endl;
401 *fLog << "Interpretation of Coordinate '" << str << "' not successfull... abort." << endl;
402 *fLog << "Corrdinate must have the format: '-12:30:00.0', '12:30:00.0' or '+12:30:00.0'" << endl;
403 return kFALSE;
404}
405
406// --------------------------------------------------------------------------
407//
408// Read the setup from a TEnv, eg:
409// MSrcPosCalc.SourceRa: ra
410// MSrcPosCalc.SourceDec: dec
411// MSrcPosCalc.SourcePos: ra dec
412//
413// For format of 'ra' and 'dec' see GetCoordinate()
414//
415// Coordinates are J2000.0
416//
417Int_t MSrcPosCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
418{
419 Double_t ra=0;
420 Double_t dec=0;
421
422 if (IsEnvDefined(env, prefix, "SourceRaDec", print))
423 {
424 TString str = GetEnvValue(env, prefix, "SourceRaDec", "");
425 str = str.Strip(TString::kBoth);
426
427 const Ssiz_t pos = str.First(' ');
428 if (pos<0)
429 {
430 *fLog << err << GetDescriptor() << "SourceRaDec empty... abort." << endl;
431 return kERROR;
432 }
433
434 if (!GetCoordinate(str(0, pos), ra))
435 return kERROR;
436 if (!GetCoordinate(str(pos+1, str.Length()), dec))
437 return kERROR;
438
439 SetSourcePos(ra, dec);
440 return kTRUE;
441 }
442
443 Bool_t rc = kFALSE;
444 if (IsEnvDefined(env, prefix, "SourceRa", print))
445 {
446 TString str = GetEnvValue(env, prefix, "SourceRa", "");
447
448 if (!GetCoordinate(str, ra))
449 return kERROR;
450
451 rc = kTRUE;
452 }
453
454 if (IsEnvDefined(env, prefix, "SourceDec", print))
455 {
456 TString str = GetEnvValue(env, prefix, "SourceDec", "");
457
458 if (!GetCoordinate(str, dec))
459 return kERROR;
460
461 rc = kTRUE;
462 }
463
464 if (!rc)
465 return kFALSE;
466
467 SetSourcePos(ra, dec);
468 return kTRUE;
469}
Note: See TracBrowser for help on using the repository browser.