source: trunk/MagicSoft/Mars/manalysis/MSourcePosfromStarPos.cc@ 3242

Last change on this file since 3242 was 3209, checked in by wittek, 21 years ago
*** empty log message ***
File size: 12.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! Author(s): Wolfgang Wittek 02/2004 <mailto:wittek@mppmu.mpg.de>
18!
19! Copyright: MAGIC Software Development, 2000-2004
20!
21!
22\* ======================================================================== */
23
24/////////////////////////////////////////////////////////////////////////////
25//
26// MSourcePosfromStarPos
27//
28// This is a task which
29// - calculates the position of the source in the camera
30// from the position of a known star in the camera
31// - and puts the source position into the container MSrcPosCam
32//
33// Input :
34// ASCII file containing for each run
35// - the run number
36// - the direction (theta, phi) the telescope is pointing to in [deg]
37// - the position (xStar, yStar) of a known star in the camera in [mm]
38// - the error (dxStar, dyStar) of this position in [mm]
39//
40// Output Containers :
41// MSrcPosCam
42//
43/////////////////////////////////////////////////////////////////////////////
44#include <TList.h>
45#include <TSystem.h>
46
47#include <fstream>
48
49#include "MSourcePosfromStarPos.h"
50
51#include "MParList.h"
52#include "MRawRunHeader.h"
53#include "MGeomCam.h"
54#include "MSrcPosCam.h"
55
56#include "MLog.h"
57#include "MLogManip.h"
58
59ClassImp(MSourcePosfromStarPos);
60
61using namespace std;
62
63// --------------------------------------------------------------------------
64//
65// Default constructor.
66//
67MSourcePosfromStarPos::MSourcePosfromStarPos(
68 const char *name, const char *title)
69 : fIn(NULL)
70{
71 fName = name ? name : "MSourcePosfromStarPos";
72 fTitle = title ? title : "Calculate source position from star position";
73
74 fFileNames = new TList;
75 fFileNames->SetOwner();
76}
77
78// --------------------------------------------------------------------------
79//
80// Delete the filename list and the input stream if one exists.
81//
82MSourcePosfromStarPos::~MSourcePosfromStarPos()
83{
84 delete fFileNames;
85 if (fIn)
86 delete fIn;
87}
88
89
90// --------------------------------------------------------------------------
91//
92// Set the sky coordinates of the source and of the star
93//
94void MSourcePosfromStarPos::SetSourceAndStarPosition(
95 Double_t decSource, Double_t raSource,
96 Double_t decStar, Double_t raStar)
97{
98 fDecSource = decSource;
99 fRaSource = raSource;
100
101 fDecStar = decStar;
102 fRaStar = raStar;
103
104 *fLog << all << "MSourcePosfromStarPos::SetSourceAndStarPosition; fDecSource, fRaSource, fDecStar, fRaStar were set to : "
105 << fDecSource << ", " << fRaSource << ", "
106 << fDecStar << ", " << fRaStar << endl;
107}
108
109// --------------------------------------------------------------------------
110//
111//
112Int_t MSourcePosfromStarPos::PreProcess(MParList *pList)
113{
114 MGeomCam *geom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
115 if (!geom)
116 {
117 *fLog << err << "MSourcePosfromStarPos : MGeomCam (Camera Geometry) missing in Parameter List... aborting." << endl;
118 return kFALSE;
119 }
120 fMm2Deg = geom->GetConvMm2Deg();
121
122
123 fRun = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
124 if (!fRun)
125 {
126 *fLog << err << "MRawRunHeader not found... aborting." << endl;
127 return kFALSE;
128 }
129
130
131 fSrcPos = (MSrcPosCam*)pList->FindObject(AddSerialNumber("MSrcPosCam"));
132 if (!fSrcPos)
133 {
134 *fLog << err << "MSourcePosfromStarPos : MSrcPosCam not found... aborting" << endl;
135 return kFALSE;
136 }
137
138 //---------------------------------------------------------------------
139 // read all files and call ReadData() to read and store the information
140 //
141
142 fRuns = 0;
143 fSize = 0;
144
145 while(1)
146 {
147 if (!OpenNextFile()) break;
148
149 if (fIn->eof())
150 if (!OpenNextFile()) break;
151
152 // FIXME! Set InputStreamID
153
154 ReadData();
155 }
156
157 FixSize();
158 //-------------------------------------------------------------
159
160 return kTRUE;
161}
162
163//=========================================================================
164//
165// SourcefromStar
166//
167// this routine calculates the position of a source (for example Crab) in the camera
168// from the position of a star (for example ZetaTauri) in the camera. The latter
169// position may have been determined by analysing the DC currents in the different
170// pixels.
171//
172// Input : thetaTel, phiTel the direction the telescope is pointing to,
173// in local coordinates
174// f the distance between camera and reflector
175// decStar, raStar the position of the star in sky coordinates
176// decSource, raSource the position of the source in sky coordinates
177// xStar, yStar the position of the star in the camera
178// dxStar, dyStar error of the position of the star in the camera
179//
180// Output : xSource, ySource the calculated position of the source in the camera
181// dxSource, dySource error of the calculated position of the source in
182// the camera
183//
184// Useful formulas can be found in TDAS 00-11 and TDAS 01-05
185//
186
187void MSourcePosfromStarPos::SourcefromStar(Double_t &f,
188 Double_t &decStar, Double_t &raStar,
189 Double_t &decSource, Double_t &raSource,
190 Double_t &thetaTel, Double_t &phiTel,
191 Double_t &xStar, Double_t &yStar,
192 Double_t &dxStar, Double_t &dyStar,
193 Double_t &xSource, Double_t &ySource,
194 Double_t &dxSource, Double_t &dySource)
195{
196 // the units are assumed to be radians for theta, phi, dec and ra
197 // and mm for f, x and y
198
199 // calculate coordinates of star and source in system B (see TDAS 00-11, eqs. (2))
200 Double_t xB = cos(decStar) * cos(raStar);
201 Double_t yB = cos(decStar) * sin(raStar);
202 Double_t zB = -sin(decStar);
203
204 Double_t xB0 = cos(decSource) * cos(raSource);
205 Double_t yB0 = cos(decSource) * sin(raSource);
206 Double_t zB0 = -sin(decSource);
207
208 // calculate rotation angle alpha of sky image in camera
209 // (see TDAS 00-11, eqs. (18) and (20))
210 // a1 = cos(Lat), a3 = -sin(Lat), where Lat is the geographical latitude of La Palma
211 Double_t a1 = 0.876627;
212 Double_t a3 = -0.481171;
213
214 Double_t denom = 1./ sqrt( sin(thetaTel)*sin(phiTel) * sin(thetaTel)*sin(phiTel) +
215 ( a1*cos(thetaTel)+a3*sin(thetaTel)*cos(phiTel) ) *
216 ( a1*cos(thetaTel)+a3*sin(thetaTel)*cos(phiTel) ) );
217 Double_t cosal = - (a3 * sin(thetaTel) + a1 * cos(thetaTel) * cos(phiTel)) * denom;
218 Double_t sinal = a1 * sin(phiTel) * denom;
219
220
221 // calculate coordinates of star in a system with the basis vectors e1, e2, e3
222 // where e1 is in the direction (r0 x a)
223 // e2 is in the direction (e1 x r0)
224 // and e3 is in the direction -r0;
225 // r0 is the direction the telescope is pointing to
226 // and a is the earth rotation axis (pointing to the celestial north pole)
227 //
228 Double_t x = (-xB*yB0 + xB0*yB) / sqrt( xB0*xB0 + yB0*yB0 );
229 Double_t y = ( xB*xB0*zB0 + yB*yB0*zB0 - zB*(xB0*xB0 + yB0*yB0) )
230 / sqrt( xB0*xB0 + yB0*yB0 );
231 Double_t z = -(xB*xB0 + yB*yB0 + zB*zB0);
232
233 // calculate coordinates of star in camera
234 Double_t xtilde = -f/z * (cosal*x - sinal*y);
235 Double_t ytilde = -f/z * (sinal*x + cosal*y);
236
237 // calculate coordinates of source in camera
238 // note : in real camera signs are inverted (therefore s = -1.0)
239 Double_t s = -1.0;
240 xSource = s * (s*xStar - xtilde);
241 ySource = s * (s*yStar - ytilde);
242
243 *fLog << "thetaTel, phiTel, cosal, sinal = " << thetaTel << ", "
244 << phiTel << ", " << cosal << ", " << sinal << endl;
245}
246
247
248// --------------------------------------------------------------------------
249//
250// Get the source position and put it into MSrcPosCam
251//
252//
253Bool_t MSourcePosfromStarPos::ReInit(MParList *pList)
254{
255 if (fDecStar == 0.0 || fRaStar == 0.0 ||
256 fDecSource == 0.0 || fRaSource == 0.0)
257 {
258 *fLog << err << "MSourcePosfromStarPos::ReInit; sky coordinates of star and source are not defined ... aborting"
259 << endl;
260 return kFALSE;
261 }
262
263 // f is the distance of the camera from the reflector center in [mm]
264 Double_t f = kRad2Deg / fMm2Deg;
265
266 for (Int_t i=0; i<fSize; i++)
267 {
268 Int_t run = fRun->GetRunNumber();
269 if (run == fRunNr[i])
270 {
271 MSourcePosfromStarPos::SourcefromStar( f,
272 fDecStar, fRaStar, fDecSource, fRaSource,
273 fThetaTel[i], fPhiTel[i],
274 fxStar[i], fyStar[i],
275 fdxStar[i], fdyStar[i],
276 fxSource, fySource,
277 fdxSource, fdySource);
278
279 fSrcPos->SetXY(fxSource, fySource);
280
281 *fLog << all << "MSourcePosfromStarPos::ReInit; f = " << f << endl;
282 *fLog << all << "MSourcePosfromStarPos::ReInit; fRunNr, fxSource, fySource = "
283 << fRunNr[i] << ", " << fxSource << ", " << fySource
284 << endl;
285
286 fSrcPos->SetReadyToSave();
287 }
288 }
289
290 return kTRUE;
291}
292
293// --------------------------------------------------------------------------
294//
295//
296Int_t MSourcePosfromStarPos::Process()
297{
298
299 return kTRUE;
300}
301
302// --------------------------------------------------------------------------
303//
304//
305Int_t MSourcePosfromStarPos::PostProcess()
306{
307
308 return kTRUE;
309}
310
311// --------------------------------------------------------------------------
312//
313// read the data from the ASCII file and store them
314//
315void MSourcePosfromStarPos::FixSize()
316{
317 fSize = fRuns;
318
319 fRunNr.Set(fSize);
320
321 fThetaTel.Set(fSize);
322 fPhiTel.Set(fSize);
323 fdThetaTel.Set(fSize);
324 fdPhiTel.Set(fSize);
325
326 fxStar.Set(fSize);
327 fyStar.Set(fSize);
328 fdxStar.Set(fSize);
329 fdyStar.Set(fSize);
330}
331
332// --------------------------------------------------------------------------
333//
334// read the data from the ASCII file and store them
335//
336void MSourcePosfromStarPos::ReadData()
337{
338 Float_t val;
339
340 if (fRuns >= fSize)
341 {
342 fSize += 100;
343
344 fRunNr.Set(fSize);
345
346 fThetaTel.Set(fSize);
347 fPhiTel.Set(fSize);
348 fdThetaTel.Set(fSize);
349 fdPhiTel.Set(fSize);
350
351 fxStar.Set(fSize);
352 fyStar.Set(fSize);
353 fdxStar.Set(fSize);
354 fdyStar.Set(fSize);
355 }
356
357 fRuns += 1;
358
359 *fIn >> val;
360 fRunNr.AddAt(val, fRuns-1);
361
362 *fIn >> val;
363 fThetaTel.AddAt(val, fRuns-1);
364 *fIn >> val;
365 fPhiTel.AddAt(val, fRuns-1);
366
367 *fIn >> val;
368 fdThetaTel.AddAt(val, fRuns-1);
369 *fIn >> val;
370 fdPhiTel.AddAt(val, fRuns-1);
371
372 *fIn >> val;
373 fxStar.AddAt(val, fRuns-1);
374 *fIn >> val;
375 fyStar.AddAt(val, fRuns-1);
376
377 *fIn >> val;
378 fdxStar.AddAt(val, fRuns-1);
379 *fIn >> val;
380 fdyStar.AddAt(val, fRuns-1);
381}
382
383// --------------------------------------------------------------------------
384//
385// Add this file as the last entry in the chain
386//
387Int_t MSourcePosfromStarPos::AddFile(const char *txt, Int_t)
388{
389 TNamed *name = new TNamed(txt, "");
390 fFileNames->AddLast(name);
391 return 1;
392}
393
394// --------------------------------------------------------------------------
395//
396// This opens the next file in the list and deletes its name from the list.
397//
398Bool_t MSourcePosfromStarPos::OpenNextFile()
399{
400 //
401 // open the input stream and check if it is really open (file exists?)
402 //
403 if (fIn)
404 delete fIn;
405 fIn = NULL;
406
407 //
408 // Check for the existence of a next file to read
409 //
410 TNamed *file = (TNamed*)fFileNames->First();
411 if (!file)
412 return kFALSE;
413
414 //
415 // open the file which is the first one in the chain
416 //
417 const char *name = file->GetName();
418
419 const char *expname = gSystem->ExpandPathName(name);
420 fIn = new ifstream(expname);
421 delete [] expname;
422
423 const Bool_t noexist = !(*fIn);
424
425 if (noexist)
426 *fLog << dbginf << "Cannot open file '" << name << "'" << endl;
427 else
428 *fLog << "Open file: '" << name << "'" << endl;
429
430 //
431 // Remove this file from the list of pending files
432 //
433 fFileNames->Remove(file);
434
435 return !noexist;
436}
437
438// --------------------------------------------------------------------------
439
440
441
442
443
444
445
446
447
448
Note: See TracBrowser for help on using the repository browser.