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 |
|
---|
49 | ClassImp(MSrcPosFromFile);
|
---|
50 |
|
---|
51 | using namespace std;
|
---|
52 |
|
---|
53 | static const TString gsDefName = "MSrcPosFromFile";
|
---|
54 | static 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 | //
|
---|
62 | MSrcPosFromFile::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 | //
|
---|
83 | MSrcPosFromFile::~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.
|
---|
96 | Int_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 | //
|
---|
132 | Int_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(fLastValidSrcPosCam)
|
---|
145 | *fLog << inf << " not found in file. Taking previous position: ";
|
---|
146 | else
|
---|
147 | {
|
---|
148 | *fLog << warn << "MSrcPosFromFile::ComputeNewSrcPosition warning: no value for the first run. Taking first found run in file, run number " << fFirstRun << endl;
|
---|
149 | fLastValidSrcPosCam = (MSrcPosCam*)fRunMap->GetValue(run);
|
---|
150 | }
|
---|
151 |
|
---|
152 | *fLog << inf << "\tX\t" << setprecision(3) << fLastValidSrcPosCam->GetX();
|
---|
153 | *fLog << inf << "\tY\t" << setprecision(3) << fLastValidSrcPosCam->GetY() << endl;
|
---|
154 | }
|
---|
155 |
|
---|
156 | GetOutputSrcPosCam()->SetXY(fLastValidSrcPosCam->GetX(),fLastValidSrcPosCam->GetY());
|
---|
157 |
|
---|
158 | return kTRUE;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // --------------------------------------------------------------------------
|
---|
162 | //
|
---|
163 | // Read file with table of source positions as function of run
|
---|
164 | //
|
---|
165 | Int_t MSrcPosFromFile::ReadSourcePositionsFile(UShort_t readmode)
|
---|
166 | {
|
---|
167 |
|
---|
168 | ifstream fin(fSourcePositionFilePath);
|
---|
169 | if(!fin)
|
---|
170 | {
|
---|
171 | *fLog << err << "MSrcPosFromFile::ReadSourcePositionsFile. Cannot open file " << fSourcePositionFilePath << endl;
|
---|
172 | return kFALSE;
|
---|
173 | }
|
---|
174 |
|
---|
175 | UInt_t run;
|
---|
176 | Float_t x,y;
|
---|
177 |
|
---|
178 | fNumRuns=0;
|
---|
179 |
|
---|
180 | *fLog << dbg << "MSrcPosFromFile::ReadSourcePositionsFile(" << readmode << ')' << endl;
|
---|
181 | while(1)
|
---|
182 | {
|
---|
183 | fin >> run >> x >> y;
|
---|
184 | if(!fFirstRun) fFirstRun=run;
|
---|
185 | if(fin.eof())
|
---|
186 | break;
|
---|
187 |
|
---|
188 | switch(readmode)
|
---|
189 | {
|
---|
190 | case kCount:
|
---|
191 | {
|
---|
192 |
|
---|
193 | *fLog << dbg << "Source position for run " << run;
|
---|
194 | *fLog << dbg << "\tX\t" << x << " mm";
|
---|
195 | *fLog << dbg << "\tY\t" << y << " mm" << endl;
|
---|
196 |
|
---|
197 | fNumRuns++;
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | case kRead:
|
---|
201 | {
|
---|
202 | fRunList[fNumRuns] = run;
|
---|
203 | fRunSrcPos[fNumRuns].SetXY(x,y);
|
---|
204 | fRunMap->Add(fRunList[fNumRuns],(Long_t)&(fRunSrcPos[fNumRuns]));
|
---|
205 | fNumRuns++;
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | default:
|
---|
209 | *fLog << err << "Read mode " << readmode << " node defined" << endl;
|
---|
210 | return kFALSE;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | fin.close();
|
---|
215 |
|
---|
216 |
|
---|
217 | return kTRUE;
|
---|
218 | }
|
---|