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

Last change on this file since 13518 was 12794, checked in by tbretz, 13 years ago
Get run start and end time.
File size: 5.7 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 const string runstart = fin.GetStr("DATE-OBS");
153 const string runstop = fin.GetStr("DATE-END");
154
155 fRawRunHeader->SetRunTime(MTime(runstart.c_str()), MTime(runstop.c_str()));
156
157 return
158 fin.HasKey("NPIX") && fin.HasKey("RUNID") &&
159 fin.HasKey("NROI") && fin.HasKey("BLDVER") &&
160 fin.HasKey("NIGHT");
161}
162
163Bool_t MRawFitsRead::InitReadData(istream &stream)
164{
165 fits &fin = static_cast<fits&>(stream);
166
167 MArrayB **data = reinterpret_cast<MArrayB**>(fRawEvtData1->DataMember("fHiGainFadcSamples"));
168 MArrayS **cell = reinterpret_cast<MArrayS**>(fRawEvtData1->DataMember("fStartCells"));
169 UInt_t *evtnum = reinterpret_cast<UInt_t*> (fRawEvtHeader->DataMember("fDAQEvtNumber"));
170 UShort_t *trg = reinterpret_cast<UShort_t*>(fRawEvtHeader->DataMember("fTrigPattern"));
171
172 if (!data || !cell || !evtnum || !trg)
173 return kFALSE;
174
175 fRawEvtData1->ResetPixels();
176 fRawEvtData2->ResetPixels(0, 0);
177 fRawEvtData1->InitStartCells();
178
179 if (!fin.SetRefAddress("EventNum", *evtnum))
180 return kFALSE;
181
182 if (!fin.SetRefAddress("TriggerType", *trg))
183 return kFALSE;
184
185 fPCTime.resize(2);
186 if (!fin.SetVecAddress("UnixTimeUTC", fPCTime))
187 if (!fin.SetVecAddress("PCTime", fPCTime))
188 return kFALSE;
189
190 if (!fin.SetPtrAddress("Data", (int16_t*)(*data)->GetArray(), (*data)->GetSize()/2))
191 return kFALSE;
192
193 if (!fin.SetPtrAddress("StartCellData", (uint16_t*)(*cell)->GetArray(), (*cell)->GetSize()))
194 return kFALSE;
195
196 fRawEvtData1->SetIndices(fRawRunHeader->GetPixAssignment());
197
198 return kTRUE;
199}
200
201Bool_t MRawFitsRead::ReadEvent(istream &stream)
202{
203 if (!static_cast<fits&>(stream).GetNextRow())
204 return kFALSE;
205
206 fRawEvtTime->SetUnixTime(fPCTime[0], fPCTime[1]);
207
208 // FIXME: Correctly sort the pixels here!
209
210 fRawEvtData1->SetReadyToSave();
211 fRawEvtData2->SetReadyToSave();
212
213 return kTRUE;
214}
215
216void MRawFitsRead::SkipEvent(istream &fin)
217{
218 static_cast<fits&>(fin).SkipNextRow();
219}
Note: See TracBrowser for help on using the repository browser.