source: trunk/Mars/mraw/MRawFitsRead.cc@ 12649

Last change on this file since 12649 was 12628, checked in by tbretz, 13 years ago
Make sure that no name for the mapping can be given; print the name in the error message; Get the runtype from the header; Use PCTime as a fallback to UnixTimePC
File size: 5.4 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): 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 "MRawRunHeader.h"
54#include "MRawEvtHeader.h"
55#include "MRawEvtData.h"
56
57ClassImp(MRawFitsRead);
58
59using namespace std;
60
61// --------------------------------------------------------------------------
62//
63// Default constructor. It tries to open the given file.
64//
65MRawFitsRead::MRawFitsRead(const char *fname, const char *name, const char *title)
66 : MRawFileRead(fname, name, title)
67{
68}
69
70Bool_t MRawFitsRead::LoadMap(const char *name)
71{
72 fPixelMap.resize(1440);
73
74 if (!name)
75 return kTRUE;
76
77 ifstream fin(name);
78
79 int l = 0;
80
81 string buf;
82 while (getline(fin, buf, '\n'))
83 {
84 if (l>1439)
85 break;
86
87 const TString bf = TString(buf.c_str()).Strip(TString::kBoth);
88 if (bf[0]=='#')
89 continue;
90
91 stringstream str(bf.Data());
92
93 int index, cbpx;
94
95 str >> index;
96 str >> cbpx;
97
98 const int c = cbpx/1000;
99 const int b = (cbpx/100)%10;
100 const int p = (cbpx/10)%10;
101 const int x = cbpx%10;
102
103 const int hw = x+p*9+b*36+c*360;
104 if (hw>1439)
105 break;
106
107 fPixelMap[hw] = index;
108 l++;
109 }
110
111 if (l!=1440)
112 {
113 gLog << err << "ERROR - Problems reading " << name << endl;
114 fPixelMap.resize(0);
115 return kFALSE;
116 }
117
118 return kTRUE;
119}
120
121Bool_t MRawFitsRead::IsFits(const char *name)
122{
123 return fits(name).good();
124}
125
126istream *MRawFitsRead::OpenFile(const char *filename)
127{
128 return new fits(filename);
129}
130
131Bool_t MRawFitsRead::ReadRunHeader(istream &stream)
132{
133 fits &fin = static_cast<fits&>(stream);
134
135 if (fin.GetStr("TELESCOP")!="FACT")
136 {
137 gLog << err << "Not a valid FACT FITS file (key TELESCOP not 'FACT')." << endl;
138 return kFALSE;
139 }
140
141 const string type = fin.GetStr("RUNTYPE");
142
143 fRawRunHeader->SetValidMagicNumber();
144 fRawRunHeader->SetNumEvents(fin.GetUInt("NAXIS2"));
145 fRawRunHeader->InitPixels(fin.GetUInt("NPIX"));
146 fRawRunHeader->SetObservation(type=="4294967295"?"":fin.GetStr("RUNTYPE"), "FACT");
147 fRawRunHeader->SetRunInfo(0, fin.GetUInt("NIGHT"), fin.GetUInt("RUNID"));
148 fRawRunHeader->InitFact(fin.GetUInt("NPIX")/9, 9, fin.GetUInt("NROI"), fPixelMap.size()==0?0:fPixelMap.data());
149 fRawRunHeader->SetFormat(0xf172, fin.GetUInt("BLDVER"));
150 fRawRunHeader->SetRunType(0/*data*/);
151
152 return
153 fin.HasKey("NPIX") && fin.HasKey("RUNID") &&
154 fin.HasKey("NROI") && fin.HasKey("BLDVER") &&
155 fin.HasKey("NIGHT");
156}
157
158Bool_t::MRawFitsRead::InitReadData(istream &stream)
159{
160 fits &fin = static_cast<fits&>(stream);
161
162 MArrayB **data = reinterpret_cast<MArrayB**>(fRawEvtData1->DataMember("fHiGainFadcSamples"));
163 MArrayS **cell = reinterpret_cast<MArrayS**>(fRawEvtData1->DataMember("fStartCells"));
164 UInt_t *evtnum = reinterpret_cast<UInt_t*> (fRawEvtHeader->DataMember("fDAQEvtNumber"));
165
166 if (!data || !cell || !evtnum)
167 return kFALSE;
168
169 fRawEvtData1->ResetPixels();
170 fRawEvtData2->ResetPixels(0, 0);
171 fRawEvtData1->InitStartCells();
172
173 if (!fin.SetRefAddress("EventNum", *evtnum))
174 return kFALSE;
175
176 fPCTime.resize(2);
177 if (!fin.SetVecAddress("UnixTimeUTC", fPCTime))
178 if (!fin.SetVecAddress("PCTime", fPCTime))
179 return kFALSE;
180
181 if (!fin.SetPtrAddress("Data", (int16_t*)(*data)->GetArray(), (*data)->GetSize()/2))
182 return kFALSE;
183
184 if (!fin.SetPtrAddress("StartCellData", (uint16_t*)(*cell)->GetArray(), (*cell)->GetSize()))
185 return kFALSE;
186
187 fRawEvtData1->SetIndices(fRawRunHeader->GetPixAssignment());
188
189 return kTRUE;
190}
191
192Bool_t MRawFitsRead::ReadEvent(istream &stream)
193{
194 if (!static_cast<fits&>(stream).GetNextRow())
195 return kFALSE;
196
197 fRawEvtTime->SetUnixTime(fPCTime[0], fPCTime[1]);
198
199 // FIXME: Correctly sort the pixels here!
200
201 fRawEvtData1->SetReadyToSave();
202 fRawEvtData2->SetReadyToSave();
203
204 return kTRUE;
205}
206
207void MRawFitsRead::SkipEvent(istream &fin)
208{
209 static_cast<fits&>(fin).SkipNextRow();
210}
Note: See TracBrowser for help on using the repository browser.