source: trunk/Mars/mtemp/mifae/library/MSrcPosFromFile.cc@ 17976

Last change on this file since 17976 was 4328, checked in by rico, 20 years ago
*** empty log message ***
File size: 6.2 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): Javier López 04/2004 <mailto:jlopez@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MSrcPosFromFile
28//
29// Task to set the position of the source as a function of run number according
30// to the positions read from an input file
31//
32// Output Containers:
33// MSrcPosCam
34//
35//////////////////////////////////////////////////////////////////////////////
36
37#include <fstream>
38
39#include "MParList.h"
40#include "MSrcPosFromFile.h"
41
42#include "MRawRunHeader.h"
43#include "MSrcPosCam.h"
44
45#include "MLog.h"
46#include "MLogManip.h"
47
48
49ClassImp(MSrcPosFromFile);
50
51using namespace std;
52
53static const TString gsDefName = "MSrcPosFromFile";
54static const TString gsDefTitle = "Set the position of the (off axis) source according to input file";
55
56// -------------------------------------------------------------------------
57//
58// Default constructor. cardpath is the name of the input file (.pos) where the
59// source positions are stored in the format Run# x y (in mm). mode indicates whether
60// to read or to save the positions of the source in/from the internal histogram
61//
62MSrcPosFromFile::MSrcPosFromFile(TString cardpath, OnOffMode_t mode, const char *name, const char *title)
63 : fRawRunHeader(NULL), fSourcePositionFilePath(cardpath)
64{
65 fName = name ? name : gsDefName.Data();
66 fTitle = title ? title : gsDefTitle.Data();
67 SetMode(mode);
68
69 fRunList=0x0;
70 fRunSrcPos=0x0;
71 fRunMap=0x0;
72
73 fLastRun = 0;
74 fFirstRun = 0;
75 fLastValidSrcPosCam=0x0;
76
77 SetInternalHistoName(TString(fName)+"Hist");
78}
79// -------------------------------------------------------------------------
80//
81// Destructor
82//
83MSrcPosFromFile::~MSrcPosFromFile()
84{
85 if(fRunList)
86 delete [] fRunList;
87 if(fRunSrcPos)
88 delete [] fRunSrcPos;
89 if(fRunMap)
90 delete fRunMap;
91}
92
93// -------------------------------------------------------------------------
94//
95// Open and read the input file.
96Int_t MSrcPosFromFile::PreProcess(MParList *pList)
97{
98 if(GetMode()==MSrcPlace::kOn)
99 {
100 // Count the number of runs in the card with the source poistions
101 ReadSourcePositionsFile(kCount);
102
103 if(!fRunList)
104 fRunList = new Int_t[fNumRuns];
105 if(!fRunSrcPos)
106 fRunSrcPos = new MSrcPosCam[fNumRuns];
107 if(!fRunMap)
108 fRunMap = new TExMap(fNumRuns);
109
110 // Read card with the source poistions
111 ReadSourcePositionsFile(kRead);
112 }
113
114 if(!MSrcPlace::PreProcess(pList))
115 return kFALSE;
116
117 fRawRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
118 if (!fRawRunHeader)
119 {
120 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found ... aborting" << endl;
121 return kFALSE;
122 }
123
124 return kTRUE;
125}
126
127// --------------------------------------------------------------------------
128//
129// In case we enter a new run, look for the position in the read list
130// If there is no value for that run, take the previous one
131//
132Int_t MSrcPosFromFile::ComputeNewSrcPosition()
133{
134 const UInt_t run = fRawRunHeader->GetRunNumber();
135 if(run!=fLastRun)
136 {
137 fLastRun=run;
138 MSrcPosCam* srcpos = (MSrcPosCam*)fRunMap->GetValue(run);
139
140 *fLog << inf << "Source position for run " << run;
141
142 if(srcpos)
143 fLastValidSrcPosCam = srcpos;
144 else if(run>fFirstRun)
145 for(UInt_t irun=run-1; irun>=fFirstRun;irun--)
146 if((srcpos=(MSrcPosCam*)fRunMap->GetValue(irun)))
147 {
148 *fLog << inf << " not found in file. Taking position for run "<< irun;
149 fLastValidSrcPosCam = srcpos;
150 break;
151 }
152 else if(fLastValidSrcPosCam)
153 *fLog << inf << " not found in file. Taking previous position: ";
154
155 if(!fLastValidSrcPosCam)
156 {
157 *fLog << warn << "MSrcPosFromFile::ComputeNewSrcPosition warning: no value for the first run. Taking first found run in file, run number " << fFirstRun;
158 fLastValidSrcPosCam = (MSrcPosCam*)fRunMap->GetValue(fFirstRun);
159 }
160
161 *fLog << inf << "\tX\t" << setprecision(3) << fLastValidSrcPosCam->GetX();
162 *fLog << inf << "\tY\t" << setprecision(3) << fLastValidSrcPosCam->GetY() << endl;
163 }
164
165 GetOutputSrcPosCam()->SetXY(fLastValidSrcPosCam->GetX(),fLastValidSrcPosCam->GetY());
166
167 return kTRUE;
168}
169
170// --------------------------------------------------------------------------
171//
172// Read file with table of source positions as function of run
173//
174Int_t MSrcPosFromFile::ReadSourcePositionsFile(UShort_t readmode)
175{
176
177 ifstream fin(fSourcePositionFilePath);
178 if(!fin)
179 {
180 *fLog << err << "MSrcPosFromFile::ReadSourcePositionsFile. Cannot open file " << fSourcePositionFilePath << endl;
181 return kFALSE;
182 }
183
184 UInt_t run;
185 Float_t x,y;
186
187 fNumRuns=0;
188
189 *fLog << dbg << "MSrcPosFromFile::ReadSourcePositionsFile(" << readmode << ')' << endl;
190 while(1)
191 {
192 fin >> run >> x >> y;
193 if(!fFirstRun) fFirstRun=run;
194 if(fin.eof())
195 break;
196
197 switch(readmode)
198 {
199 case kCount:
200 {
201
202 *fLog << dbg << "Source position for run " << run;
203 *fLog << dbg << "\tX\t" << x << " mm";
204 *fLog << dbg << "\tY\t" << y << " mm" << endl;
205
206 fNumRuns++;
207 break;
208 }
209 case kRead:
210 {
211 fRunList[fNumRuns] = run;
212 fRunSrcPos[fNumRuns].SetXY(x,y);
213 fRunMap->Add(fRunList[fNumRuns],(Long_t)&(fRunSrcPos[fNumRuns]));
214 fNumRuns++;
215 break;
216 }
217 default:
218 *fLog << err << "Read mode " << readmode << " node defined" << endl;
219 return kFALSE;
220 }
221 }
222
223 fin.close();
224
225
226 return kTRUE;
227}
Note: See TracBrowser for help on using the repository browser.