source: tags/Mars-V0.8.1/mfileio/MCT1ReadAscii.cc

Last change on this file was 2239, checked in by moralejo, 22 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
41#include "MCT1ReadAscii.h"
42
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 "MPedestalPix.h"
55#include "MPedestalCam.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 delete [] expname;
135
136 const Bool_t noexist = !(*fIn);
137
138 if (noexist)
139 *fLog << dbginf << "Cannot open file '" << name << "'" << endl;
140 else
141 *fLog << "Open file: '" << name << "'" << endl;
142
143 //
144 // Remove this file from the list of pending files
145 //
146 fFileNames->Remove(file);
147
148 return !noexist;
149}
150
151// --------------------------------------------------------------------------
152//
153// Open the first file in the list. Check for the output containers or create
154// them if they don't exist.
155//
156// Initialize the size of the MPedestalCam container to 127 pixels (CT1 camera)
157//
158Int_t MCT1ReadAscii::PreProcess(MParList *pList)
159{
160 //
161 // Preprocessing
162 //
163
164 //
165 // Try to open at least one (the first) file
166 //
167 if (!OpenNextFile())
168 return kFALSE;
169
170 //
171 // look for the MCerPhotEvt class in the plist
172 //
173 fNphot = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt");
174 if (!fNphot)
175 return kFALSE;
176
177 //
178 // look for the pedestal class in the plist
179 //
180 fPedest = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
181 if (!fPedest)
182 return kFALSE;
183
184 fPedest->InitSize(127);
185
186 return kTRUE;
187}
188
189// --------------------------------------------------------------------------
190//
191// Read apedestal entry (line) from the file
192//
193void MCT1ReadAscii::ReadPedestals()
194{
195 //
196 // skip the next 4 values
197 //
198 Float_t val;
199
200 *fIn >> val;
201 *fIn >> val;
202 *fIn >> val;
203 *fIn >> val;
204
205 //
206 // read in the next 127 numbers as the pedestals
207 //
208 for (Int_t i = 0; i<127; i++)
209 {
210 *fIn >> val;
211
212 if (val > 0.0)
213 (*fPedest)[i].SetPedestalRms(val);
214 }
215}
216
217// --------------------------------------------------------------------------
218//
219// Read a data entry (line) from the file
220//
221void MCT1ReadAscii::ReadData()
222{
223 /*
224 //
225 // clear the list of cerphot-events
226 //
227 fNphot->Clear();
228 */
229
230 //
231 // five unsused numbers
232 //
233 Int_t val;
234
235 *fIn >> val; // ener
236 *fIn >> val; // zenang
237 *fIn >> val; // sec1
238 *fIn >> val; // sec2
239
240 //
241 // read in the number of cerenkov photons and add the 'new' pixel
242 // too the list with it's id, number of photons and error
243 //
244// fNphot->InitSize(127);
245
246 for (Int_t i = 0; i<127; i++ )
247 {
248 Float_t nphot;
249
250 *fIn >> nphot;
251
252 if (nphot > 0.0)
253 fNphot->AddPixel(i, nphot, (*fPedest)[i].GetPedestalRms());
254 }
255 fNphot->FixSize();
256}
257
258// --------------------------------------------------------------------------
259//
260// Check for the event number and depending on this number decide if
261// pedestals or event data has to be read.
262//
263// If the end of the file is reached try to open the next in the list. If
264// there is now next file stop the eventloop.
265//
266Int_t MCT1ReadAscii::Process()
267{
268 //
269 // FIXME. This function should switch between reading pedestals and
270 // reading event data by the 'switch entry'.
271 // After reading it should set the InputStreamID correctly.
272 // ( should use MPedestalCam )
273 //
274
275 //
276 // read in the event nr
277 //
278 Int_t evtnr;
279 *fIn >> evtnr;
280
281 //
282 // check if we are done. Try to open the next file in chain.
283 // If it was possible start reading. If not break the event loop
284 //
285 if (fIn->eof())
286 return OpenNextFile() ? kCONTINUE : kFALSE;
287
288 //
289 // if the first number is negativ this is a pedestal line:
290 // read in pedestals
291 //
292 // FIXME! Set InputStreamID
293
294 if (evtnr < 0)
295 {
296 ReadPedestals();
297 return kCONTINUE;
298 }
299
300 ReadData();
301
302 return kTRUE;
303}
Note: See TracBrowser for help on using the repository browser.