source: trunk/MagicSoft/Mars/msim/MPhotonEvent.cc@ 9319

Last change on this file since 9319 was 9301, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 10.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of CheObs, the Modular 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 appears 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): Qi Zhe, 06/2007 <mailto:qizhe@astro.uni-wuerzburg.de>
20!
21! Copyright: CheObs Software Development, 2000-2009
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MPhotonEvent
29//
30// Storage container to store photon collections
31//
32// The class is designed to be extremely fast which is important taking into
33// account the extremely high number of photons. This has some impacts on
34// its handling.
35//
36// The list has to be kept consistent, i.e. without holes.
37//
38// There are two ways to achieve this:
39//
40// a) Use RemoveAt to remove an entry somewhere
41// b) Compress() the TClonesArray afterwards
42//
43// Compress is not the fastes, so there is an easier way.
44//
45// a) When you loop over the list and want to remove an entry copy all
46// following entry backward in the list, so that the hole will
47// be created at its end.
48// b) Call Shrink(n) with n the number of valid entries in your list.
49//
50// To loop over the TClonesArray you can use a TIter which has some
51// unnecessary overhead and therefore is slower than necessary.
52//
53// Since the list is kept consistent you can use a simple loop saving
54// a lot of CPU time taking into account the high number of calls to
55// TObjArrayIter::Next which you would create.
56//
57// Here is an example (how to remove every second entry)
58//
59// ---------------------------------------------------------------------
60//
61// Int_t cnt = 0;
62//
63// const Int_t num = event->GetNumPhotons();
64// for (Int_t idx=0; idx<num; idx++)
65// {
66// if (idx%2==0)
67// continue;
68//
69// MPhotonData *dat = (*event)[idx];
70//
71// (*event)[cnt++] = *dat;
72// }
73//
74// event->Shrink(cnt);
75//
76// ---------------------------------- or -------------------------------
77//
78// TClonesArray &arr = MPhotonEvent->GetArray();
79//
80// Int_t cnt = 0;
81//
82// const Int_t num = arr.GetEntriesFast();
83// for (Int_t idx=0; idx<num; idx++)
84// {
85// if (idx%2==0)
86// continue;
87//
88// MPhotonData *dat = static_cast<MPhotonData*>(arr.UncheckedAt(idx));
89//
90// *static_cast<MPhotonData*>(arr.UncheckedAt(cnt++)) = *dat;
91// }
92//
93// MPhotonEvent->Shrink(cnt);
94//
95// ---------------------------------------------------------------------
96//
97// The flag for a sorted array is for speed reasons not in all conditions
98// maintained automatically. Especially Add() doesn't reset it.
99//
100// So be sure that if you want to sort your array it is really sorted.
101//
102//
103// Version 1:
104// ----------
105// - First implementation
106//
107/////////////////////////////////////////////////////////////////////////////
108#include "MPhotonEvent.h"
109
110#include <fstream>
111#include <iostream>
112
113#include <TMarker.h>
114
115#include "MLog.h"
116#include "MLogManip.h"
117
118#include "MPhotonData.h"
119
120ClassImp(MPhotonEvent);
121
122using namespace std;
123
124// --------------------------------------------------------------------------
125//
126// Default constructor. It initializes all arrays with zero size.
127//
128MPhotonEvent::MPhotonEvent(const char *name, const char *title)
129 : fData("MPhotonData", 1)
130{
131 fName = name ? name : "MPhotonEvent";
132 fTitle = title ? title : "Corsika Event Data Information";
133
134 fData.SetBit(TClonesArray::kForgetBits);
135 fData.BypassStreamer(kFALSE);
136}
137
138/*
139const char *MPhotonEvent::GetClassName() const
140{
141 return static_cast<TObject*>(fData.GetClass())->GetName();
142}
143*/
144
145// --------------------------------------------------------------------------
146//
147// If n is smaller than the current allocated array size a reference to
148// the n-th entry is returned, otherwise an entry at n is created
149// calling the default constructor. Note, that there is no range check
150// but it is not recommended to call this function with
151// n>fData.GetSize()
152//
153MPhotonData &MPhotonEvent::Add(Int_t n)
154{
155 // Do not modify this. It is optimized for execution
156 // speed and flexibility!
157 TObject *o = n<fData.GetSize() && fData.UncheckedAt(n) ? fData.UncheckedAt(n) : fData.New(n);
158
159 // Now we read a single cherenkov bunch
160 return static_cast<MPhotonData&>(*o);
161}
162
163// --------------------------------------------------------------------------
164//
165// Add a new photon (MPhtonData) at the end of the array.
166// In this case the default constructor of MPhotonData is called.
167//
168// A reference to the new object is returned.
169//
170MPhotonData &MPhotonEvent::Add()
171{
172 return Add(GetNumPhotons());
173}
174
175// --------------------------------------------------------------------------
176//
177// Get the i-th photon from the array. Not, for speed reasons there is no
178// range check so you are responsible that you do not excess the number
179// of photons (GetNumPhotons)
180//
181MPhotonData &MPhotonEvent::operator[](UInt_t idx)
182{
183 return *static_cast<MPhotonData*>(fData.UncheckedAt(idx));
184}
185
186// --------------------------------------------------------------------------
187//
188// Get the i-th photon from the array. Not, for speed reasons there is no
189// range check so you are responsible that you do not excess the number
190// of photons (GetNumPhotons)
191//
192const MPhotonData &MPhotonEvent::operator[](UInt_t idx) const
193{
194 return *static_cast<MPhotonData*>(fData.UncheckedAt(idx));
195}
196
197// --------------------------------------------------------------------------
198//
199// Return a pointer to the first photon if available.
200//
201MPhotonData *MPhotonEvent::GetFirst() const
202{
203 return static_cast<MPhotonData*>(fData.First());
204}
205
206// --------------------------------------------------------------------------
207//
208// Return a pointer to the last photon if available.
209//
210MPhotonData *MPhotonEvent::GetLast() const
211{
212 return static_cast<MPhotonData*>(fData.Last());
213}
214
215Int_t MPhotonEvent::GetNumExternal() const
216{
217 Int_t n=0;
218
219 for (int i=0; i<GetNumPhotons(); i++)
220 if ((*this)[i].GetPrimary()!=MMcEvtBasic::kNightSky)
221 n++;
222
223 return n;
224}
225
226// --------------------------------------------------------------------------
227//
228// Read the Event section from the file
229//
230Int_t MPhotonEvent::ReadCorsikaEvt(istream &fin)
231{
232 Int_t n = 0;
233
234 while (1)
235 {
236 // Check the first four bytes
237 char c[4];
238 fin.read(c, 4);
239
240 // End of stream
241 if (!fin)
242 return kFALSE;
243
244 // Check if we found the end of the event
245 if (!memcmp(c, "EVTE", 4))
246 break;
247
248 // The first for byte contained data already --> go back
249 fin.seekg(-4, ios::cur);
250
251 // Do not modify this. It is optimized for execution
252 // speed and flexibility!
253 MPhotonData &ph = Add(n);
254 // It checks how many entries the lookup table has. If it has enough
255 // entries and the entry was already allocated, we can re-use it,
256 // otherwise we have to allocate it.
257
258 // Now we read a single cherenkov bunch. Note that for speed reason we have not
259 // called the constructor if the event was already constructed (virtual table
260 // set), consequently we must make sure that ReadCorsikaEvent does reset
261 // all data mebers no matter whether they are read or not.
262 const Int_t rc = ph.ReadCorsikaEvt(fin);
263
264 // Evaluate result from reading event
265 switch (rc)
266 {
267 case kCONTINUE: continue; // No data in this bunch... skip it.
268 case kFALSE: return kFALSE; // End of stream
269 case kERROR: return kERROR; // Error occured
270 }
271
272 // FIXME: If fNumPhotons!=1 add the photon more than once
273
274 // Now increase the number of entries which are kept,
275 // i.e. keep this photon(s)
276 n++;
277 }
278
279 Shrink(n);
280 fData.UnSort();
281
282 SetReadyToSave();
283
284 //*fLog << all << "Number of photon bunches: " << fData.GetEntriesFast() << endl;
285 return kTRUE;
286}
287
288// --------------------------------------------------------------------------
289//
290Int_t MPhotonEvent::ReadRflEvt(std::istream &fin)
291{
292 Int_t n = 0;
293
294 while (1)
295 {
296 // Check the first four bytes
297 char c[13];
298 fin.read(c, 13);
299
300 // End of stream
301 if (!fin)
302 return kFALSE;
303
304 // Check if we found the end of the event
305 if (!memcmp(c, "\nEND---EVENT\n", 13))
306 break;
307
308 // The first for byte contained data already --> go back
309 fin.seekg(-13, ios::cur);
310
311 // Do not modify this. It is optimized for execution
312 // speed and flexibility!
313 //TObject *o = n<fData.GetSize() && fData.UncheckedAt(n) ? fData.UncheckedAt(n) : fData.New(n);
314
315 // Now we read a single cherenkov bunch
316 //const Int_t rc = static_cast<MPhotonData*>(o)->ReadRflEvt(fin);
317 const Int_t rc = Add(n).ReadRflEvt(fin);
318
319 // Evaluate result from reading event
320 switch (rc)
321 {
322 case kCONTINUE: continue; // No data in this bunch... skip it.
323 case kFALSE: return kFALSE; // End of stream
324 case kERROR: return kERROR; // Error occured
325 }
326
327 // Now increase the number of entries which are kept,
328 // i.e. keep this photon(s)
329 n++;
330 }
331
332 Shrink(n);
333
334 SetReadyToSave();
335
336 //*fLog << all << "Number of photon bunches: " << fData.GetEntriesFast() << endl;
337 return kTRUE;
338}
339
340// --------------------------------------------------------------------------
341//
342// Print the array
343//
344void MPhotonEvent::Print(Option_t *) const
345{
346 fData.Print();
347}
348
349// ------------------------------------------------------------------------
350//
351// You can call Draw() to add the photons to the current pad.
352// The photons are painted each tim ethe pad is updated.
353// Make sure that you use the right (world) coordinate system,
354// like created, eg. by the MHCamera histogram.
355//
356void MPhotonEvent::Paint(Option_t *)
357{
358 MPhotonData *ph=NULL;
359
360 TMarker m;
361 m.SetMarkerStyle(kFullDotMedium); // Gtypes.h
362
363 TIter Next(&fData);
364 while ((ph=(MPhotonData*)Next()))
365 {
366 m.SetX(ph->GetPosY()*10); // north
367 m.SetY(ph->GetPosX()*10); // east
368 m.Paint();
369 }
370}
Note: See TracBrowser for help on using the repository browser.