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

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