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

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