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

Last change on this file since 7179 was 7179, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 14.4 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), fIsWobbleMode(kFALSE)
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 (fIsWobbleMode)
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 MRawRunHeader *run = (MRawRunHeader*)plist->FindObject("MRawRunHeader");
233 if (!run)
234 {
235 *fLog << err << "MRawRunHeader not found... aborting." << endl;
236 return kFALSE;
237 }
238
239 fRunType = run->GetRunType();
240
241 if (fRunType!=MRawRunHeader::kRTMonteCarlo)
242 {
243 fObservatory = (MObservatory*)plist->FindObject("MObservatory");
244 if (!fObservatory)
245 {
246 *fLog << err << "MObservatory not found... aborting." << endl;
247 return kFALSE;
248 }
249 fTime = (MTime*)plist->FindObject("MTime");
250 if (!fTime)
251 {
252 *fLog << err << "MTime not found... aborting." << endl;
253 return kFALSE;
254 }
255 return kTRUE;
256 }
257
258 MMcCorsikaRunHeader *h = (MMcCorsikaRunHeader*)plist->FindObject("MMcCorsikaRunHeader");
259 if (!h)
260 {
261 *fLog << err << "MMcCorsikaRunHeader not found... aborting." << endl;
262 return kFALSE;
263 }
264
265 TVector2 v(0, 0);
266 if (h->GetWobbleMode()>0.5)
267 v.Set(120., 0.);
268 if (h->GetWobbleMode()<-0.5)
269 v.Set(-120., 0.);
270
271
272 SetSrcPos(v);
273
274 *fLog << inf;
275 *fLog << "Source Position set to x=" << fSrcPosCam->GetX() << "mm ";
276 *fLog << "y=" << fSrcPosCam->GetY() << "mm" << endl;
277
278 return kTRUE;
279}
280
281// --------------------------------------------------------------------------
282//
283// Loc0LocToCam
284//
285// Input : (theta0, phi0) direction for the position (0,0) in the camera
286// ( theta, phi) some other direction
287//
288// Output : (X, Y) position in the camera corresponding to (theta, phi)
289//
290TVector2 MSrcPosCalc::CalcXYinCamera(const MVector3 &pos0, const MVector3 &pos) const
291{
292 const Double_t theta0 = pos0.Theta();
293 const Double_t phi0 = pos0.Phi();
294
295 const Double_t theta = pos.Theta();
296 const Double_t phi = pos.Phi();
297
298 //--------------------------------------------
299
300 /* --- OLD ---
301 const Double_t YC0 = TMath::Cos(theta0)*TMath::Tan(theta)*TMath::Cos(phi-phi0) - TMath::Sin(theta0);
302 const Double_t YC1 = TMath::Cos(theta0) + TMath::Sin(theta0)*TMath::Tan(theta);
303 const Double_t YC = YC0 / YC1;
304
305 const Double_t XC0 = TMath::Cos(theta0) - YC*TMath::Sin(theta0);
306 const Double_t XC = -TMath::Sin(phi-phi0) * TMath::Tan(theta) * XC0;
307 */
308
309 /* --- NEW --- */
310 const Double_t XC0 = TMath::Sin(theta)*TMath::Sin(phi-phi0);
311 const Double_t XC1 = TMath::Cos(theta0)*TMath::Cos(theta);
312 const Double_t XC2 = TMath::Sin(theta0)*TMath::Sin(theta)*TMath::Cos(phi-phi0);
313
314 const Double_t YC0 = TMath::Sin(theta0)*TMath::Cos(theta);
315 const Double_t YC1 = TMath::Cos(theta0)*TMath::Sin(theta)*TMath::Cos(phi-phi0);
316
317 const Double_t XC = - XC0 / (XC1 + XC2);
318 const Double_t YC = (-YC0+YC1) / (XC1 + XC2);
319
320 //--------------------------------------------
321 return TVector2(XC, YC);
322}
323
324// --------------------------------------------------------------------------
325//
326// Derotate fX/fY by the current rotation angle, set MSrcPosCam
327//
328Int_t MSrcPosCalc::Process()
329{
330 if (fRunType==MRawRunHeader::kRTMonteCarlo || !fSourcePos || !fTime || !fObservatory)
331 return kTRUE;
332
333 // Set Sky coordinates of source, taken from container "MSourcePos"
334 // of type MPointingPos. The sky coordinates must be J2000, as the
335 // sky coordinates of the camera center that we get from the container
336 // "MPointingPos" filled by the Drive.
337 MVector3 pos; // pos: source position
338 pos.SetRaDec(fSourcePos->GetRaRad(), fSourcePos->GetDecRad());
339
340 // Convert sky coordinates of source to local coordinates. Warning! These are not the "true" local
341 // coordinates, since this transformation ignores precession and nutation effects.
342 const MAstroSky2Local conv(*fTime, *fObservatory);
343 pos *= conv;
344
345 // Set sky coordinates of camera center in pos0, and then convert to
346 // local. Same comment as above. These coordinates differ from the true
347 // local coordinates of the camera center that one could get from
348 // "MPointingPos", calculated by the Drive: the reason is that the Drive
349 // takes into account precession and nutation corrections, while
350 // MAstroSky2Local (as of Jan 27 2005 at least) does not. Since we just
351 // want to get the source position on the camera from the local
352 // coordinates of the center and the source, it does not matter that
353 // the coordinates contained in pos and pos0 ignore precession and
354 // nutation... since the shift would be the same in both cases. What
355 // would be wrong is to set in pos0 directly the local coordinates
356 // found in MPointingPos!
357 MVector3 pos0; // pos0: camera center
358 pos0.SetRaDec(fPointPos->GetRaRad(), fPointPos->GetDecRad());
359 pos0 *= conv;
360
361 // Calculate source position in camera, and convert to mm:
362 TVector2 v = CalcXYinCamera(pos0, pos)*fGeom->GetCameraDist()*1000;
363 SetSrcPos(v);
364
365 return kTRUE;
366}
367
368// --------------------------------------------------------------------------
369//
370// Convert a coordinate stored in str into a double, stored in ret.
371// Returns kTRUE on success, otherwise kFALSE
372//
373// Allowed formats are:
374// 12.5
375// -12.5
376// +12.5
377// -12:30:00.0
378// 12:30:00.0
379// +12:30:00.0
380//
381Bool_t MSrcPosCalc::GetCoordinate(TString str, Double_t &ret) const
382{
383 str = str.Strip(TString::kBoth);
384
385 if (str.First(':')<0)
386 {
387 ret = atof(str);
388 return kTRUE;
389 }
390
391 if (MAstro::Coordinate2Angle(str, ret))
392 return kTRUE;
393
394 *fLog << err << GetDescriptor() << endl;
395 *fLog << "Interpretation of Coordinate '" << str << "' not successfull... abort." << endl;
396 *fLog << "Corrdinate must have the format: '-12:30:00.0', '12:30:00.0' or '+12:30:00.0'" << endl;
397 return kFALSE;
398}
399
400// --------------------------------------------------------------------------
401//
402// Read the setup from a TEnv, eg:
403// MSrcPosCalc.SourceRa: ra
404// MSrcPosCalc.SourceDec: dec
405// MSrcPosCalc.SourcePos: ra dec
406//
407// For format of 'ra' and 'dec' see GetCoordinate()
408//
409// Coordinates are J2000.0
410//
411Int_t MSrcPosCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
412{
413 Double_t ra=0;
414 Double_t dec=0;
415
416 if (IsEnvDefined(env, prefix, "SourceRaDec", print))
417 {
418 TString str = GetEnvValue(env, prefix, "SourceRaDec", "");
419 str = str.Strip(TString::kBoth);
420
421 const Ssiz_t pos = str.First(' ');
422 if (pos<0)
423 {
424 *fLog << err << GetDescriptor() << "SourceRaDec empty... abort." << endl;
425 return kERROR;
426 }
427
428 if (!GetCoordinate(str(0, pos), ra))
429 return kERROR;
430 if (!GetCoordinate(str(pos+1, str.Length()), dec))
431 return kERROR;
432
433 SetSourcePos(ra, dec);
434 return kTRUE;
435 }
436
437 Bool_t rc = kFALSE;
438 if (IsEnvDefined(env, prefix, "SourceRa", print))
439 {
440 TString str = GetEnvValue(env, prefix, "SourceRa", "");
441
442 if (!GetCoordinate(str, ra))
443 return kERROR;
444
445 rc = kTRUE;
446 }
447
448 if (IsEnvDefined(env, prefix, "SourceDec", print))
449 {
450 TString str = GetEnvValue(env, prefix, "SourceDec", "");
451
452 if (!GetCoordinate(str, dec))
453 return kERROR;
454
455 rc = kTRUE;
456 }
457
458 if (!rc)
459 return kFALSE;
460
461 SetSourcePos(ra, dec);
462 return kTRUE;
463}
Note: See TracBrowser for help on using the repository browser.