source: trunk/MagicSoft/Mars/mfileio/MCT1ReadAscii.cc@ 3842

Last change on this file since 3842 was 3568, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 7.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, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer, 1/2001
20!
21! Copyright: MAGIC Software Development, 2000-2003
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27// //
28// MCT1ReadAscii //
29// //
30// Reads a ascii file with CT1 data. The file description and some example //
31// files can be found on the Magic homepage. //
32// //
33// Input Containers: //
34// -/- //
35// //
36// Output Containers: //
37// MCerPhotEvt //
38// //
39/////////////////////////////////////////////////////////////////////////////
40#include "MCT1ReadAscii.h"
41
42#include <errno.h>
43#include <fstream>
44
45#include <TList.h>
46#include <TSystem.h>
47
48#include "MLog.h"
49#include "MLogManip.h"
50
51#include "MParList.h"
52#include "MCerPhotEvt.h"
53
54#include "MPedPhotPix.h"
55#include "MPedPhotCam.h"
56
57ClassImp(MCT1ReadAscii);
58
59using namespace std;
60
61// --------------------------------------------------------------------------
62//
63// Default constructor. Creates an array which stores the file names of
64// the files which should be read. If a filename is given it is added
65// to the list.
66//
67MCT1ReadAscii::MCT1ReadAscii(const char *fname,
68 const char *name,
69 const char *title)
70 : fIn(NULL)
71{
72 fName = name ? name : "MCT1ReadAscii";
73 fTitle = title ? title : "Task to loop over events in CT1 ascii file";
74
75 //
76 // remember file name for opening the file in the preprocessor
77 //
78 fFileNames = new TList;
79 fFileNames->SetOwner();
80
81 if (fname)
82 AddFile(fname);
83}
84
85// --------------------------------------------------------------------------
86//
87// Delete the filename list and the input stream if one exists.
88//
89MCT1ReadAscii::~MCT1ReadAscii()
90{
91 delete fFileNames;
92 if (fIn)
93 delete fIn;
94}
95
96// --------------------------------------------------------------------------
97//
98// Add this file as the last entry in the chain
99//
100Int_t MCT1ReadAscii::AddFile(const char *txt, Int_t)
101{
102 TNamed *name = new TNamed(txt, "");
103 fFileNames->AddLast(name);
104 return 1;
105}
106
107// --------------------------------------------------------------------------
108//
109// This opens the next file in the list and deletes its name from the list.
110//
111Bool_t MCT1ReadAscii::OpenNextFile()
112{
113 //
114 // open the input stream and check if it is really open (file exists?)
115 //
116 if (fIn)
117 delete fIn;
118 fIn = NULL;
119
120 //
121 // Check for the existance of a next file to read
122 //
123 TNamed *file = (TNamed*)fFileNames->First();
124 if (!file)
125 return kFALSE;
126
127 //
128 // open the file which is the first one in the chain
129 //
130 const char *name = file->GetName();
131
132 const char *expname = gSystem->ExpandPathName(name);
133 fIn = new ifstream(expname);
134
135 const Bool_t noexist = !(*fIn);
136 if (noexist)
137 {
138 *fLog << err << "Cannot open file " << expname << ": ";
139 *fLog << strerror(errno) << endl;
140 }
141 else
142 *fLog << inf << "Open file: '" << name << "'" << endl;
143
144 delete [] expname;
145
146 //
147 // Remove this file from the list of pending files
148 //
149 fFileNames->Remove(file);
150
151 return !noexist;
152}
153
154// --------------------------------------------------------------------------
155//
156// Open the first file in the list. Check for the output containers or create
157// them if they don't exist.
158//
159// Initialize the size of the MPedPhotCam container to 127 pixels (CT1 camera)
160//
161Int_t MCT1ReadAscii::PreProcess(MParList *pList)
162{
163 //
164 // Preprocessing
165 //
166
167 //
168 // Try to open at least one (the first) file
169 //
170 if (!OpenNextFile())
171 return kFALSE;
172
173 //
174 // look for the MCerPhotEvt class in the plist
175 //
176 fNphot = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt");
177 if (!fNphot)
178 return kFALSE;
179
180 //
181 // look for the pedestal class in the plist
182 //
183 fPedest = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam");
184 if (!fPedest)
185 return kFALSE;
186
187 fPedest->InitSize(127);
188
189 return kTRUE;
190}
191
192// --------------------------------------------------------------------------
193//
194// Read apedestal entry (line) from the file
195//
196void MCT1ReadAscii::ReadPedestals()
197{
198 //
199 // skip the next 4 values
200 //
201 Float_t val;
202
203 *fIn >> val;
204 *fIn >> val;
205 *fIn >> val;
206 *fIn >> val;
207
208 //
209 // read in the next 127 numbers as the pedestals
210 //
211 for (Int_t i = 0; i<127; i++)
212 {
213 *fIn >> val;
214
215 if (val>0)
216 (*fPedest)[i].Set(0, val);
217 }
218}
219
220// --------------------------------------------------------------------------
221//
222// Read a data entry (line) from the file
223//
224void MCT1ReadAscii::ReadData()
225{
226 //
227 // five unsused numbers
228 //
229 Int_t val;
230
231 *fIn >> val; // ener
232 *fIn >> val; // zenang
233 *fIn >> val; // sec1
234 *fIn >> val; // sec2
235
236 //
237 // read in the number of cerenkov photons and add the 'new' pixel
238 // too the list with it's id, number of photons and error
239 //
240 for (Int_t i = 0; i<127; i++ )
241 {
242 Float_t nphot;
243
244 *fIn >> nphot;
245
246 if (nphot > 0.0)
247 fNphot->AddPixel(i, nphot, (*fPedest)[i].GetRms());
248 }
249 fNphot->FixSize();
250}
251
252// --------------------------------------------------------------------------
253//
254// Check for the event number and depending on this number decide if
255// pedestals or event data has to be read.
256//
257// If the end of the file is reached try to open the next in the list. If
258// there is now next file stop the eventloop.
259//
260Int_t MCT1ReadAscii::Process()
261{
262 //
263 // FIXME. This function should switch between reading pedestals and
264 // reading event data by the 'switch entry'.
265 // After reading it should set the InputStreamID correctly.
266 // (should use MPedPhotCam )
267 //
268
269 //
270 // read in the event nr
271 //
272 Int_t evtnr;
273 *fIn >> evtnr;
274
275 //
276 // check if we are done. Try to open the next file in chain.
277 // If it was possible start reading. If not break the event loop
278 //
279 if (fIn->eof())
280 return OpenNextFile() ? kCONTINUE : kFALSE;
281
282 //
283 // if the first number is negativ this is a pedestal line:
284 // read in pedestals
285 //
286 // FIXME! Set InputStreamID
287
288 if (evtnr < 0)
289 {
290 ReadPedestals();
291 return kCONTINUE;
292 }
293
294 ReadData();
295
296 return kTRUE;
297}
Note: See TracBrowser for help on using the repository browser.