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

Last change on this file since 9239 was 9239, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 10.5 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): Qi Zhe, 06/2007 <mailto:qizhe@astro.uni-wuerzburg.de>
20!
21! Copyright: 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
215// --------------------------------------------------------------------------
216//
217// Read the Event section from the file
218//
219Int_t MPhotonEvent::ReadCorsikaEvt(istream &fin)
220{
221 Int_t n = 0;
222
223 while (1)
224 {
225 // Check the first four bytes
226 char c[4];
227 fin.read(c, 4);
228
229 // End of stream
230 if (!fin)
231 return kFALSE;
232
233 // Check if we found the end of the event
234 if (!memcmp(c, "EVTE", 4))
235 break;
236
237 // The first for byte contained data already --> go back
238 fin.seekg(-4, ios::cur);
239
240 // Do not modify this. It is optimized for execution
241 // speed and flexibility!
242 MPhotonData &ph = Add(n);
243 // It checks how many entries the lookup table has. If it has enough
244 // entries and the entry was already allocated, we can re-use it,
245 // otherwise we have to allocate it.
246
247 // Now we read a single cherenkov bunch. Note that for speed reason we have not
248 // called the constructor if the event was already constructed (virtual table
249 // set), consequently we must make sure that ReadCorsikaEvent does reset
250 // all data mebers no matter whether they are read or not.
251 const Int_t rc = ph.ReadCorsikaEvt(fin);
252
253 // Evaluate result from reading event
254 switch (rc)
255 {
256 case kCONTINUE: continue; // No data in this bunch... skip it.
257 case kFALSE: return kFALSE; // End of stream
258 case kERROR: return kERROR; // Error occured
259 }
260
261 // FIXME: If fNumPhotons!=1 add the photon more than once
262
263 // Now increase the number of entries which are kept,
264 // i.e. keep this photon(s)
265 n++;
266 }
267
268 Shrink(n);
269 fData.UnSort();
270
271 SetReadyToSave();
272
273 //*fLog << all << "Number of photon bunches: " << fData.GetEntriesFast() << endl;
274 return kTRUE;
275}
276
277// --------------------------------------------------------------------------
278//
279Int_t MPhotonEvent::ReadRflEvt(std::istream &fin)
280{
281 Int_t n = 0;
282
283 while (1)
284 {
285 // Check the first four bytes
286 char c[13];
287 fin.read(c, 13);
288
289 // End of stream
290 if (!fin)
291 return kFALSE;
292
293 // Check if we found the end of the event
294 if (!memcmp(c, "\nEND---EVENT\n", 13))
295 break;
296
297 // The first for byte contained data already --> go back
298 fin.seekg(-13, ios::cur);
299
300 // Do not modify this. It is optimized for execution
301 // speed and flexibility!
302 //TObject *o = n<fData.GetSize() && fData.UncheckedAt(n) ? fData.UncheckedAt(n) : fData.New(n);
303
304 // Now we read a single cherenkov bunch
305 //const Int_t rc = static_cast<MPhotonData*>(o)->ReadRflEvt(fin);
306 const Int_t rc = Add(n).ReadRflEvt(fin);
307
308 // Evaluate result from reading event
309 switch (rc)
310 {
311 case kCONTINUE: continue; // No data in this bunch... skip it.
312 case kFALSE: return kFALSE; // End of stream
313 case kERROR: return kERROR; // Error occured
314 }
315
316 // Now increase the number of entries which are kept,
317 // i.e. keep this photon(s)
318 n++;
319 }
320
321 Shrink(n);
322
323 SetReadyToSave();
324
325 //*fLog << all << "Number of photon bunches: " << fData.GetEntriesFast() << endl;
326 return kTRUE;
327}
328
329// --------------------------------------------------------------------------
330//
331// Print the array
332//
333void MPhotonEvent::Print(Option_t *) const
334{
335 fData.Print();
336}
337
338// ------------------------------------------------------------------------
339//
340// You can call Draw() to add the photons to the current pad.
341// The photons are painted each tim ethe pad is updated.
342// Make sure that you use the right (world) coordinate system,
343// like created, eg. by the MHCamera histogram.
344//
345void MPhotonEvent::Paint(Option_t *)
346{
347 MPhotonData *ph=NULL;
348
349 TMarker m;
350 m.SetMarkerStyle(kFullDotMedium); // Gtypes.h
351
352 TIter Next(&fData);
353 while ((ph=(MPhotonData*)Next()))
354 {
355 m.SetX(ph->GetPosY()*10); // north
356 m.SetY(ph->GetPosX()*10); // east
357 m.Paint();
358 }
359}
Note: See TracBrowser for help on using the repository browser.