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

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