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

Last change on this file since 2925 was 2781, checked in by tbretz, 21 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@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 "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 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 MPedPhotCam 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 = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam");
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)
213 (*fPedest)[i].Set(0, val);
214 }
215}
216
217// --------------------------------------------------------------------------
218//
219// Read a data entry (line) from the file
220//
221void MCT1ReadAscii::ReadData()
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 for (Int_t i = 0; i<127; i++ )
238 {
239 Float_t nphot;
240
241 *fIn >> nphot;
242
243 if (nphot > 0.0)
244 fNphot->AddPixel(i, nphot, (*fPedest)[i].GetRms());
245 }
246 fNphot->FixSize();
247}
248
249// --------------------------------------------------------------------------
250//
251// Check for the event number and depending on this number decide if
252// pedestals or event data has to be read.
253//
254// If the end of the file is reached try to open the next in the list. If
255// there is now next file stop the eventloop.
256//
257Int_t MCT1ReadAscii::Process()
258{
259 //
260 // FIXME. This function should switch between reading pedestals and
261 // reading event data by the 'switch entry'.
262 // After reading it should set the InputStreamID correctly.
263 // (should use MPedPhotCam )
264 //
265
266 //
267 // read in the event nr
268 //
269 Int_t evtnr;
270 *fIn >> evtnr;
271
272 //
273 // check if we are done. Try to open the next file in chain.
274 // If it was possible start reading. If not break the event loop
275 //
276 if (fIn->eof())
277 return OpenNextFile() ? kCONTINUE : kFALSE;
278
279 //
280 // if the first number is negativ this is a pedestal line:
281 // read in pedestals
282 //
283 // FIXME! Set InputStreamID
284
285 if (evtnr < 0)
286 {
287 ReadPedestals();
288 return kCONTINUE;
289 }
290
291 ReadData();
292
293 return kTRUE;
294}
Note: See TracBrowser for help on using the repository browser.