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

Last change on this file since 12704 was 12703, checked in by tbretz, 13 years ago
Extract the trigger type from the file.
File size: 5.5 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 UShort_t *trg = reinterpret_cast<UShort_t*>(fRawEvtHeader->DataMember("fTrigPattern"));
166
167 if (!data || !cell || !evtnum || !trg)
168 return kFALSE;
169
170 fRawEvtData1->ResetPixels();
171 fRawEvtData2->ResetPixels(0, 0);
172 fRawEvtData1->InitStartCells();
173
174 if (!fin.SetRefAddress("EventNum", *evtnum))
175 return kFALSE;
176
177 if (!fin.SetRefAddress("TriggerType", *trg))
178 return kFALSE;
179
180 fPCTime.resize(2);
181 if (!fin.SetVecAddress("UnixTimeUTC", fPCTime))
182 if (!fin.SetVecAddress("PCTime", fPCTime))
183 return kFALSE;
184
185 if (!fin.SetPtrAddress("Data", (int16_t*)(*data)->GetArray(), (*data)->GetSize()/2))
186 return kFALSE;
187
188 if (!fin.SetPtrAddress("StartCellData", (uint16_t*)(*cell)->GetArray(), (*cell)->GetSize()))
189 return kFALSE;
190
191 fRawEvtData1->SetIndices(fRawRunHeader->GetPixAssignment());
192
193 return kTRUE;
194}
195
196Bool_t MRawFitsRead::ReadEvent(istream &stream)
197{
198 if (!static_cast<fits&>(stream).GetNextRow())
199 return kFALSE;
200
201 fRawEvtTime->SetUnixTime(fPCTime[0], fPCTime[1]);
202
203 // FIXME: Correctly sort the pixels here!
204
205 fRawEvtData1->SetReadyToSave();
206 fRawEvtData2->SetReadyToSave();
207
208 return kTRUE;
209}
210
211void MRawFitsRead::SkipEvent(istream &fin)
212{
213 static_cast<fits&>(fin).SkipNextRow();
214}
Note: See TracBrowser for help on using the repository browser.