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 "fits.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 | Int_t MRawFitsRead::PreProcess(MParList *pList)
|
---|
74 | {
|
---|
75 | fRawBoards = (MRawBoardsFACT*)pList->FindCreateObj("MRawBoardsFACT");
|
---|
76 | return fRawBoards!=0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | Bool_t MRawFitsRead::LoadMap(const char *name)
|
---|
80 | {
|
---|
81 | fPixelMap.resize(1440);
|
---|
82 |
|
---|
83 | if (!name)
|
---|
84 | return kTRUE;
|
---|
85 |
|
---|
86 | ifstream fin(name);
|
---|
87 |
|
---|
88 | int l = 0;
|
---|
89 |
|
---|
90 | string buf;
|
---|
91 | while (getline(fin, buf, '\n'))
|
---|
92 | {
|
---|
93 | if (l>1439)
|
---|
94 | break;
|
---|
95 |
|
---|
96 | const TString bf = TString(buf.c_str()).Strip(TString::kBoth);
|
---|
97 | if (bf[0]=='#')
|
---|
98 | continue;
|
---|
99 |
|
---|
100 | stringstream str(bf.Data());
|
---|
101 |
|
---|
102 | int index, cbpx;
|
---|
103 |
|
---|
104 | str >> index;
|
---|
105 | str >> cbpx;
|
---|
106 |
|
---|
107 | const int c = cbpx/1000;
|
---|
108 | const int b = (cbpx/100)%10;
|
---|
109 | const int p = (cbpx/10)%10;
|
---|
110 | const int x = cbpx%10;
|
---|
111 |
|
---|
112 | const int hw = x+p*9+b*36+c*360;
|
---|
113 | if (hw>1439)
|
---|
114 | break;
|
---|
115 |
|
---|
116 | fPixelMap[hw] = index;
|
---|
117 | l++;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (l!=1440)
|
---|
121 | {
|
---|
122 | gLog << err << "ERROR - Problems reading " << name << endl;
|
---|
123 | fPixelMap.resize(0);
|
---|
124 | return kFALSE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | return kTRUE;
|
---|
128 | }
|
---|
129 |
|
---|
130 | Bool_t MRawFitsRead::IsFits(const char *name)
|
---|
131 | {
|
---|
132 | return fits(name).good();
|
---|
133 | }
|
---|
134 |
|
---|
135 | istream *MRawFitsRead::OpenFile(const char *filename)
|
---|
136 | {
|
---|
137 | return new fits(filename);
|
---|
138 | }
|
---|
139 |
|
---|
140 | Bool_t MRawFitsRead::ReadRunHeader(istream &stream)
|
---|
141 | {
|
---|
142 | fits &fin = static_cast<fits&>(stream);
|
---|
143 |
|
---|
144 | if (fin.GetStr("TELESCOP")!="FACT")
|
---|
145 | {
|
---|
146 | gLog << err << "Not a valid FACT FITS file (key TELESCOP not 'FACT')." << endl;
|
---|
147 | return kFALSE;
|
---|
148 | }
|
---|
149 |
|
---|
150 | const string type = fin.GetStr("RUNTYPE");
|
---|
151 |
|
---|
152 | fRawRunHeader->SetValidMagicNumber();
|
---|
153 | fRawRunHeader->SetNumEvents(fin.GetUInt("NAXIS2"));
|
---|
154 | fRawRunHeader->InitPixels(fin.GetUInt("NPIX"));
|
---|
155 | fRawRunHeader->SetObservation(type=="4294967295"?"":fin.GetStr("RUNTYPE"), "FACT");
|
---|
156 | fRawRunHeader->SetRunInfo(0, fin.GetUInt("NIGHT"), fin.GetUInt("RUNID"));
|
---|
157 | fRawRunHeader->InitFact(fin.GetUInt("NPIX")/9, 9, fin.GetUInt("NROI"), fPixelMap.size()==0?0:fPixelMap.data());
|
---|
158 | fRawRunHeader->SetFormat(0xf172, fin.GetUInt("BLDVER"));
|
---|
159 | fRawRunHeader->SetRunType(0/*data*/);
|
---|
160 |
|
---|
161 | const string runstart = fin.GetStr("DATE-OBS");
|
---|
162 | const string runstop = fin.GetStr("DATE-END");
|
---|
163 |
|
---|
164 | fRawRunHeader->SetRunTime(MTime(runstart.c_str()), MTime(runstop.c_str()));
|
---|
165 |
|
---|
166 | return
|
---|
167 | fin.HasKey("NPIX") && fin.HasKey("RUNID") &&
|
---|
168 | fin.HasKey("NROI") && fin.HasKey("BLDVER") &&
|
---|
169 | fin.HasKey("NIGHT");
|
---|
170 | }
|
---|
171 |
|
---|
172 | Bool_t MRawFitsRead::InitReadData(istream &stream)
|
---|
173 | {
|
---|
174 | fits &fin = static_cast<fits&>(stream);
|
---|
175 |
|
---|
176 | MArrayB **data = reinterpret_cast<MArrayB**>(fRawEvtData1->DataMember("fHiGainFadcSamples"));
|
---|
177 | MArrayS **cell = reinterpret_cast<MArrayS**>(fRawEvtData1->DataMember("fStartCells"));
|
---|
178 | UInt_t *evtnum = reinterpret_cast<UInt_t*> (fRawEvtHeader->DataMember("fDAQEvtNumber"));
|
---|
179 | UShort_t *trg = reinterpret_cast<UShort_t*>(fRawEvtHeader->DataMember("fTrigPattern"));
|
---|
180 |
|
---|
181 | if (!data || !cell || !evtnum || !trg)
|
---|
182 | return kFALSE;
|
---|
183 |
|
---|
184 | fRawEvtData1->ResetPixels();
|
---|
185 | fRawEvtData2->ResetPixels(0, 0);
|
---|
186 | fRawEvtData1->InitStartCells();
|
---|
187 |
|
---|
188 | if (!fin.SetRefAddress("EventNum", *evtnum))
|
---|
189 | return kFALSE;
|
---|
190 |
|
---|
191 | if (!fin.SetRefAddress("TriggerType", *trg))
|
---|
192 | return kFALSE;
|
---|
193 |
|
---|
194 | fPCTime.resize(2);
|
---|
195 | if (!fin.SetVecAddress("UnixTimeUTC", fPCTime))
|
---|
196 | if (!fin.SetVecAddress("PCTime", fPCTime))
|
---|
197 | return kFALSE;
|
---|
198 |
|
---|
199 | if (!fin.SetPtrAddress("BoardTime", fRawBoards->fFadTime, 40))
|
---|
200 | return kFALSE;
|
---|
201 |
|
---|
202 | if (!fin.SetPtrAddress("Data", (int16_t*)(*data)->GetArray(), (*data)->GetSize()/2))
|
---|
203 | return kFALSE;
|
---|
204 |
|
---|
205 | if (!fin.SetPtrAddress("StartCellData", (uint16_t*)(*cell)->GetArray(), (*cell)->GetSize()))
|
---|
206 | return kFALSE;
|
---|
207 |
|
---|
208 | fRawEvtData1->SetIndices(fRawRunHeader->GetPixAssignment());
|
---|
209 |
|
---|
210 | return kTRUE;
|
---|
211 | }
|
---|
212 |
|
---|
213 | Bool_t MRawFitsRead::ReadEvent(istream &stream)
|
---|
214 | {
|
---|
215 | if (!static_cast<fits&>(stream).GetNextRow())
|
---|
216 | return kFALSE;
|
---|
217 |
|
---|
218 | fRawEvtTime->SetUnixTime(fPCTime[0], fPCTime[1]);
|
---|
219 |
|
---|
220 | // FIXME: Correctly sort the pixels here!
|
---|
221 |
|
---|
222 | fRawEvtData1->SetReadyToSave();
|
---|
223 | fRawEvtData2->SetReadyToSave();
|
---|
224 |
|
---|
225 | return kTRUE;
|
---|
226 | }
|
---|
227 |
|
---|
228 | void MRawFitsRead::SkipEvent(istream &fin)
|
---|
229 | {
|
---|
230 | static_cast<fits&>(fin).SkipNextRow();
|
---|
231 | }
|
---|