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