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

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