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 07/2011 <mailto:thomas.bretz@epfl.ch>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2011
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | //////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MRawFitsRead
|
---|
28 | //
|
---|
29 | // This tasks reads the fits data file in the form used by FACT.
|
---|
30 | //
|
---|
31 | // Input Containers:
|
---|
32 | // -/-
|
---|
33 | //
|
---|
34 | // Output Containers:
|
---|
35 | // MRawRunHeader, MRawEvtHeader, MRawEvtData, MRawEvtTime
|
---|
36 | //
|
---|
37 | //////////////////////////////////////////////////////////////////////////////
|
---|
38 | #include "MRawFitsRead.h"
|
---|
39 |
|
---|
40 | #include <fstream>
|
---|
41 |
|
---|
42 | #include <TClass.h>
|
---|
43 |
|
---|
44 | #include "MLog.h"
|
---|
45 | #include "MLogManip.h"
|
---|
46 |
|
---|
47 | #include "factfits.h"
|
---|
48 | #include "MTime.h"
|
---|
49 |
|
---|
50 | #include "MArrayB.h"
|
---|
51 | #include "MArrayS.h"
|
---|
52 |
|
---|
53 | #include "MParList.h"
|
---|
54 |
|
---|
55 | #include "MRawRunHeader.h"
|
---|
56 | #include "MRawEvtHeader.h"
|
---|
57 | #include "MRawEvtData.h"
|
---|
58 | #include "MRawBoardsFACT.h"
|
---|
59 |
|
---|
60 | ClassImp(MRawFitsRead);
|
---|
61 |
|
---|
62 | using namespace std;
|
---|
63 |
|
---|
64 | // --------------------------------------------------------------------------
|
---|
65 | //
|
---|
66 | // Default constructor. It tries to open the given file.
|
---|
67 | //
|
---|
68 | MRawFitsRead::MRawFitsRead(const char *fname, const char *name, const char *title)
|
---|
69 | : MRawFileRead(fname, name, title), fRawBoards(0)
|
---|
70 | {
|
---|
71 | }
|
---|
72 |
|
---|
73 | const fits *MRawFitsRead::GetFitsFile() const
|
---|
74 | {
|
---|
75 | return static_cast<const fits*>(GetStream());
|
---|
76 | }
|
---|
77 |
|
---|
78 | Int_t MRawFitsRead::PreProcess(MParList *pList)
|
---|
79 | {
|
---|
80 | fRawBoards = (MRawBoardsFACT*)pList->FindCreateObj("MRawBoardsFACT");
|
---|
81 | if (!fRawBoards)
|
---|
82 | return kFALSE;
|
---|
83 |
|
---|
84 | return MRawFileRead::PreProcess(pList);
|
---|
85 | }
|
---|
86 |
|
---|
87 | Bool_t MRawFitsRead::LoadMap(const char *name)
|
---|
88 | {
|
---|
89 | fPixelMap.resize(1440);
|
---|
90 |
|
---|
91 | if (!name)
|
---|
92 | return kTRUE;
|
---|
93 |
|
---|
94 | ifstream fin(name);
|
---|
95 |
|
---|
96 | int l = 0;
|
---|
97 |
|
---|
98 | string buf;
|
---|
99 | while (getline(fin, buf, '\n'))
|
---|
100 | {
|
---|
101 | if (l>1439)
|
---|
102 | break;
|
---|
103 |
|
---|
104 | const TString bf = TString(buf.c_str()).Strip(TString::kBoth);
|
---|
105 | if (bf[0]=='#')
|
---|
106 | continue;
|
---|
107 |
|
---|
108 | stringstream str(bf.Data());
|
---|
109 |
|
---|
110 | int index, cbpx;
|
---|
111 |
|
---|
112 | str >> index;
|
---|
113 | str >> cbpx;
|
---|
114 |
|
---|
115 | const int c = cbpx/1000;
|
---|
116 | const int b = (cbpx/100)%10;
|
---|
117 | const int p = (cbpx/10)%10;
|
---|
118 | const int x = cbpx%10;
|
---|
119 |
|
---|
120 | const int hw = x+p*9+b*36+c*360;
|
---|
121 | if (hw>1439)
|
---|
122 | break;
|
---|
123 |
|
---|
124 | fPixelMap[hw] = index;
|
---|
125 | l++;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (l!=1440)
|
---|
129 | {
|
---|
130 | gLog << err << "ERROR - Problems reading " << name << endl;
|
---|
131 | fPixelMap.resize(0);
|
---|
132 | return kFALSE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return kTRUE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | Bool_t MRawFitsRead::IsFits(const char *name)
|
---|
139 | {
|
---|
140 | return factfits(name).good();
|
---|
141 | }
|
---|
142 |
|
---|
143 | istream *MRawFitsRead::OpenFile(const char *filename)
|
---|
144 | {
|
---|
145 | return new factfits(filename);
|
---|
146 | }
|
---|
147 |
|
---|
148 | Bool_t MRawFitsRead::ReadRunHeader(istream &stream)
|
---|
149 | {
|
---|
150 | factfits &fin = static_cast<factfits&>(stream);
|
---|
151 |
|
---|
152 | if (fin.GetStr("TELESCOP")!="FACT")
|
---|
153 | {
|
---|
154 | gLog << err << "Not a valid FACT FITS file (key TELESCOP not 'FACT')." << endl;
|
---|
155 | return kFALSE;
|
---|
156 | }
|
---|
157 |
|
---|
158 | fIsMc = fin.HasKey("ISMC");
|
---|
159 |
|
---|
160 | const string type = fin.GetStr("RUNTYPE");
|
---|
161 |
|
---|
162 | fRawRunHeader->SetValidMagicNumber();
|
---|
163 | fRawRunHeader->SetNumEvents(fin.GetNumRows());//GetUInt("NAXIS2"));
|
---|
164 | fRawRunHeader->InitPixels(fin.GetUInt("NPIX"));
|
---|
165 | fRawRunHeader->SetObservation(type=="4294967295"?"":fin.GetStr("RUNTYPE"), "FACT");
|
---|
166 | fRawRunHeader->SetRunInfo(0, fin.GetUInt("NIGHT"), fin.GetUInt("RUNID"));
|
---|
167 | if (fin.GetStr("CAMERA")!="MGeomCamFAMOUS")
|
---|
168 | fRawRunHeader->InitFact(fin.GetUInt("NPIX")/9, 9, fin.GetUInt("NROI"), fPixelMap.size()==0?0:fPixelMap.data());
|
---|
169 | else
|
---|
170 | fRawRunHeader->InitFact(1, fin.GetUInt("NPIX"), fin.GetUInt("NROI"), fPixelMap.size()==0?0:fPixelMap.data());
|
---|
171 | fRawRunHeader->InitFact(1, fin.GetUInt("NPIX"), fin.GetUInt("NROI"), fPixelMap.size()==0?0:fPixelMap.data());
|
---|
172 | fRawRunHeader->SetFormat(0xf172, fIsMc ? 0 : fin.GetUInt("BLDVER"));
|
---|
173 | fRawRunHeader->SetRunType(fIsMc ? 0x0100/*mc*/ : 0/*data*/);
|
---|
174 |
|
---|
175 | if (!fIsMc)
|
---|
176 | {
|
---|
177 | const string runstart = fin.GetStr("DATE-OBS");
|
---|
178 | const string runstop = fin.GetStr("DATE-END");
|
---|
179 |
|
---|
180 | fRawRunHeader->SetRunTime(MTime(runstart.c_str()), MTime(runstop.c_str()));
|
---|
181 | }
|
---|
182 |
|
---|
183 | for (int i=0; i<1000; i++)
|
---|
184 | {
|
---|
185 | const string clnamed = Form("CLNAME%d", i);
|
---|
186 | if (!fin.HasKey(clnamed))
|
---|
187 | break;
|
---|
188 |
|
---|
189 | const string cltyped = Form("CLTYPE%d", i);
|
---|
190 | const string clname = fin.GetStr(clnamed);
|
---|
191 | const string cltype = fin.HasKey(cltyped) ? fin.GetStr(cltyped) : clname;
|
---|
192 |
|
---|
193 | MParContainer *par = (MParContainer*)fParList->FindCreateObj(cltype.c_str(), clname.c_str());
|
---|
194 | if (par && !par->SetupFits(fin))
|
---|
195 | {
|
---|
196 | *fLog << err << "ERROR - Setting up " << par->GetDescriptor() << " failed." << endl;
|
---|
197 | return kFALSE;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | return
|
---|
202 | fin.HasKey("NPIX") && fin.HasKey("RUNID") &&
|
---|
203 | fin.HasKey("NROI") && fin.HasKey("NIGHT");
|
---|
204 | }
|
---|
205 |
|
---|
206 | Bool_t MRawFitsRead::InitReadData(istream &stream)
|
---|
207 | {
|
---|
208 | factfits &fin = static_cast<factfits&>(stream);
|
---|
209 |
|
---|
210 | MArrayB **data = reinterpret_cast<MArrayB**>(fRawEvtData1->DataMember("fHiGainFadcSamples"));
|
---|
211 | MArrayS **cell = reinterpret_cast<MArrayS**>(fRawEvtData1->DataMember("fStartCells"));
|
---|
212 | UInt_t *evtnum = reinterpret_cast<UInt_t*> (fRawEvtHeader->DataMember("fDAQEvtNumber"));
|
---|
213 |
|
---|
214 | // The 'wrong' cast is intentional because we read only two bytes from the file
|
---|
215 | UShort_t *trg = reinterpret_cast<UShort_t*>(fRawEvtHeader->DataMember("fTrigPattern"));
|
---|
216 |
|
---|
217 | if (!data || !cell || !evtnum || !trg)
|
---|
218 | return kFALSE;
|
---|
219 |
|
---|
220 | fRawEvtData1->ResetPixels();
|
---|
221 | fRawEvtData2->ResetPixels(0, 0);
|
---|
222 | fRawEvtData1->InitStartCells();
|
---|
223 |
|
---|
224 | if (!fin.SetRefAddress("EventNum", *evtnum))
|
---|
225 | return kFALSE;
|
---|
226 |
|
---|
227 | if (!fin.SetRefAddress("TriggerType", *trg))
|
---|
228 | return kFALSE;
|
---|
229 |
|
---|
230 | if (!fIsMc)
|
---|
231 | {
|
---|
232 | if (!fin.SetRefAddress("NumBoards", fNumBoards))
|
---|
233 | return kFALSE;
|
---|
234 |
|
---|
235 | fPCTime.resize(2);
|
---|
236 | if (!fin.SetVecAddress("UnixTimeUTC", fPCTime))
|
---|
237 | if (!fin.SetVecAddress("PCTime", fPCTime))
|
---|
238 | return kFALSE;
|
---|
239 |
|
---|
240 | if (!fin.SetPtrAddress("BoardTime", fRawBoards->fFadTime, 40))
|
---|
241 | return kFALSE;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (!fin.SetPtrAddress("Data", reinterpret_cast<int16_t*>((*data)->GetArray()),
|
---|
245 | (*data)->GetSize()/2))
|
---|
246 | return kFALSE;
|
---|
247 |
|
---|
248 | if (!fin.SetPtrAddress("StartCellData", (uint16_t*)(*cell)->GetArray(), (*cell)->GetSize()))
|
---|
249 | return kFALSE;
|
---|
250 |
|
---|
251 | fRawEvtData1->SetIndices(fRawRunHeader->GetPixAssignment());
|
---|
252 |
|
---|
253 | return kTRUE;
|
---|
254 | }
|
---|
255 |
|
---|
256 | Bool_t MRawFitsRead::ReadEvent(istream &stream)
|
---|
257 | {
|
---|
258 | if (!static_cast<factfits&>(stream).GetNextRow())
|
---|
259 | return kFALSE;
|
---|
260 |
|
---|
261 | if (!fIsMc)
|
---|
262 | {
|
---|
263 | // Skip incomplete events
|
---|
264 | if (fNumBoards!=40)
|
---|
265 | return kCONTINUE;
|
---|
266 |
|
---|
267 | fRawEvtTime->SetUnixTime(fPCTime[0], fPCTime[1]);
|
---|
268 |
|
---|
269 | }
|
---|
270 | fRawEvtData1->SetReadyToSave();
|
---|
271 | fRawEvtData2->SetReadyToSave();
|
---|
272 |
|
---|
273 | return kTRUE;
|
---|
274 | }
|
---|
275 |
|
---|
276 | void MRawFitsRead::SkipEvent(istream &fin)
|
---|
277 | {
|
---|
278 | static_cast<factfits&>(fin).SkipNextRow();
|
---|
279 | }
|
---|